From patchwork Fri Oct 11 17:08:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13832740 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6BD4F1A08A4 for ; Fri, 11 Oct 2024 17:09:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728666549; cv=none; b=GKeqShX/IT00j4RwccSlXQ+el5NQ8sUg4GKAulfHgaB6D2bVbvZL/DoZIBNF6v0JEww5cdr8gWC+76rwE89FajU4roafiMZvQEg98V/As1QYYWAbD5Di/zxWFe3WH6fCWWhJ4+ZFHrKMNO1pxcJu6405I7Ld6+WDlyqjFv1BKaA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728666549; c=relaxed/simple; bh=i07p+euZWicvD4DcduG6W8DaYxJyYJVcmSBCOXNMKfI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IPKJxYQN8uwOZTJDbYt+/2lWKgXmGryuf1lAvk680RZV/nQ9nOzqDzo3Ek7IC+ng0PYgByoIftS6JOls0FSi+x7c7PlIUtpflDGFV/ZRCayDOr3k14aBa7lYC4M64XcPKmDPXtStV1o4R1MVsDMNnieNUwChylrm7TYtJrAtKUc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=jnMqtx0o; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jnMqtx0o" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e29135d1d0cso2460370276.1 for ; Fri, 11 Oct 2024 10:09:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728666546; x=1729271346; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=3qdWVRrVbX3ToZU4Q5VrJuHDTTEiDMG3j538Hbt/T70=; b=jnMqtx0oHMW1aQE7Sf7L8F16R6JC4ng7z+VfLXTnUI1LXXD1CwPp/AHetlZviYmOgJ WIanZlVOYBnAWdTyjhy4tW7jCBgc2E8x3nnxA9UV76ZmfR23WQZbLCpvhHbBlXL7TPAJ 0PuL1dbM2TJ2rRh3zfOzsqQqq1n1syvNd8mZ2LJTNoh7xMmPXhaC44PJvP4OrFJ0pDSA /xCyUFn59BWhpJ796YZ1K7yD6hDs/8NA6nsGNpGoLWrUpz4+CVzC6D3UeTWFX48fxLpe k03G2YSDKzjMG+Bnl82b8cnMHXpDI30P/ViVtQK+fK5VEWt4wYPGdJcQg6JHqOQjLKuF gitA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728666546; x=1729271346; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3qdWVRrVbX3ToZU4Q5VrJuHDTTEiDMG3j538Hbt/T70=; b=IPxfkfHaOO9JgzTFYrFkrjegarROrcfAOWbyXyhkShmKgz9IJ8TbO+1+jVx/pYOsG1 qcZYWZyhkuFcebeDn1Hm6BpMx0rGOeQk5o5FuS/36y3GykZst/oxs14p5MN2U7enA6oN 0NhLubBS9dUBScnAbL3DurxAYel/Bz3g6a5jdDGJO/4nAXZaOsUugS5riwR+DUekZ0Lh HOP6Px7ZEiWqfaC5cRLOl/6aNIm6+HQDcBPQQTlakqBQICxs/KIS/c4na+Sla+21LBYV WFX/LHe/esUw72qwN+O+/SwhsnBhm7S5beFZ5rKRluGhF8hry8UWjG2zV/BuHuQcYPZP a33Q== X-Forwarded-Encrypted: i=1; AJvYcCWkfLAKWh0SpUTSwkklmlB2nb8hBfr4p+dBfgwsrLn+YXEHranA1a8KpkbK4tIdudc+ZjnW93Ran8hRYF2RsyE=@vger.kernel.org X-Gm-Message-State: AOJu0YwtyMiTR6Ypn6KuxOahO48OKioPm4WxCfBxlifeT8N23waLLb4j a1OdxDoWzuaLSv2iJ++4AdmXn1+DGVw4gSIYr176beZZNk0nPU7vqT038nQVG1aL+UAtAw== X-Google-Smtp-Source: AGHT+IFwVip20YS7zwHFgmSUuevuRJVKrZKv+7d3ChvsNAmYTytYT9sUmJLT2/L4RZPtMTdbzwTJa4eT X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a25:8702:0:b0:e29:1864:5215 with SMTP id 3f1490d57ef6-e2919dbd114mr2278276.1.1728666546408; Fri, 11 Oct 2024 10:09:06 -0700 (PDT) Date: Fri, 11 Oct 2024 19:08:49 +0200 In-Reply-To: <20241011170847.334429-10-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241011170847.334429-10-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=5192; i=ardb@kernel.org; h=from:subject; bh=fphk4j28lP0ahlwTC1zZcurlE54ngflWbp7fP3s2REU=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIZ0zeuFpSY6nGevt91Rsfhp+64WWcP6J5R9O/6l/rSZ90 fnRMgvHjlIWBjEOBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjCRTi2G/36ng8X4bqic8m/f wLih91nnA+6eUw/bohXsLjPVTN+2sJ6R4ZjGX/tvOUt3RjnytGbvmluqrf16c+Cn67sN3ilkF01 bxwAA X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241011170847.334429-11-ardb+git@google.com> Subject: [PATCH v3 1/8] objtool: Deal with relative jump tables correctly From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: llvm@lists.linux.dev, keescook@chromium.org, linux-hardening@vger.kernel.org, nathan@kernel.org, Ard Biesheuvel , Josh Poimboeuf , Peter Zijlstra , Jan Beulich , "Jose E. Marchesi" , Kees Cook From: Ard Biesheuvel Relative jump tables contain entries that carry the offset between the target of the jump and the start of the jump table. This permits the use of the PIC idiom of leaq jump_table(%rip), %tbl movslq (%tbl,%idx,4), %offset addq %offset, %tbl jmp *%tbl The jump table entries are decorated with PC32 relocations, which record the offset of the referenced symbol relative to the target of the relocation, which is the individual entry in the table. This means that only the first entry produces the correct value directly; the subsequent ones need to be corrected to produce the offset relative to the start of the table, by applying an addend. Given that the referenced symbols are anonymous, and thus already expressed in terms of sections and addends, e.g., .text+0x5df9, the correction is incorporated into the existing addend. The upshot of this is that chasing the reference to find the target instruction needs to take this second addend into account as well. Signed-off-by: Ard Biesheuvel --- tools/objtool/arch/x86/special.c | 8 ------- tools/objtool/check.c | 24 +++++++++++++++++--- tools/objtool/include/objtool/elf.h | 6 +++++ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/tools/objtool/arch/x86/special.c b/tools/objtool/arch/x86/special.c index 4ea0f9815fda..415e4d035e53 100644 --- a/tools/objtool/arch/x86/special.c +++ b/tools/objtool/arch/x86/special.c @@ -150,13 +150,5 @@ struct reloc *arch_find_switch_table(struct objtool_file *file, if (!rodata_reloc) return NULL; - /* - * Use of RIP-relative switch jumps is quite rare, and - * indicates a rare GCC quirk/bug which can leave dead - * code behind. - */ - if (reloc_type(text_reloc) == R_X86_64_PC32) - file->ignore_unreachables = true; - return rodata_reloc; } diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 2b0965153b25..aa07fdf1cf13 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -2101,6 +2101,8 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn, { struct symbol *pfunc = insn_func(insn)->pfunc; struct reloc *table = insn_jump_table(insn); + unsigned int rtype = reloc_type(table); + bool pcrel = (rtype == elf_text_rela_type(file->elf)); struct instruction *dest_insn; unsigned int prev_offset = 0; struct reloc *reloc = table; @@ -2111,13 +2113,18 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn, * instruction. */ for_each_reloc_from(table->sec, reloc) { + unsigned long addend = reloc_addend(reloc); /* Check for the end of the table: */ if (reloc != table && reloc == next_table) break; + /* Each entry in the jump table should use the same relocation type */ + if (reloc_type(reloc) != rtype) + break; + /* Make sure the table entries are consecutive: */ - if (prev_offset && reloc_offset(reloc) != prev_offset + 8) + if (prev_offset && reloc_offset(reloc) != prev_offset + (pcrel ? 4 : 8)) break; /* Detect function pointers from contiguous objects: */ @@ -2125,7 +2132,15 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn, reloc_addend(reloc) == pfunc->offset) break; - dest_insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc)); + /* + * Place-relative jump tables carry offsets relative to the + * start of the jump table, not to the entry itself. So correct + * the addend for the location of the entry in the table. + */ + if (pcrel) + addend -= reloc_offset(reloc) - reloc_offset(table); + + dest_insn = find_insn(file, reloc->sym->sec, addend); if (!dest_insn) break; @@ -2133,6 +2148,9 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn, if (!insn_func(dest_insn) || insn_func(dest_insn)->pfunc != pfunc) break; + if (pcrel) + reloc->sym_offset = addend; + alt = malloc(sizeof(*alt)); if (!alt) { WARN("malloc failed"); @@ -4535,7 +4553,7 @@ static int validate_ibt_data_reloc(struct objtool_file *file, struct instruction *dest; dest = find_insn(file, reloc->sym->sec, - reloc->sym->offset + reloc_addend(reloc)); + reloc->sym->offset + reloc_sym_offset(reloc)); if (!dest) return 0; diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h index d7e815c2fd15..f4a6307f4c08 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -78,6 +78,7 @@ struct reloc { struct section *sec; struct symbol *sym; struct reloc *sym_next_reloc; + s64 sym_offset; }; struct elf { @@ -251,6 +252,11 @@ static inline s64 reloc_addend(struct reloc *reloc) return __get_reloc_field(reloc, r_addend); } +static inline s64 reloc_sym_offset(struct reloc *reloc) +{ + return reloc->sym_offset ?: reloc_addend(reloc); +} + static inline void set_reloc_addend(struct elf *elf, struct reloc *reloc, s64 addend) { __set_reloc_field(reloc, r_addend, addend); From patchwork Fri Oct 11 17:08:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13832741 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7B5BA1C460C for ; Fri, 11 Oct 2024 17:09:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728666552; cv=none; b=bRN3ybnLxkBVRmnMWloHN4jU3ULHhZvyKcW30znaqaBK4kI16yZ0yMH3+PgFZhoWhJBhPipOltx0FsoyhKmc6JTalX4MTR1kR0P9EK4GAfWZ6+GgHI1tSJi9EWuliAYSOrbLeY/Rb7cU5EXf/cPVal0XWON37W5pRNV1ynIMGo8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728666552; c=relaxed/simple; bh=VUujYBiQvSszgV0qs3fJK1Hvr5k7z7tzS0djEyuvxZs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lJXm1IvJQm5RrVP3WYRyEMJ5Y54G9bfoej2Le3STEpvwFwSHFlZoQoJVKS7XXyYTPvx5R3FD4ngiz0GqPutaQYCr7AaUg/pnSLGOzGegO/h1FEf+D64eqXfs9DLFaVICopM7Ko5nT4IR6q3YK/f++XhmATTq+MaPK8oRj7r098E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vIFY2t62; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vIFY2t62" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-37d5ca192b8so356117f8f.1 for ; Fri, 11 Oct 2024 10:09:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728666549; x=1729271349; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=bfj0B3nnHjUxcCyCalhugd5ngmM/JrZzfePhPsFLeh4=; b=vIFY2t62Kfu9sD1TJtD3E5c4RPwLlZA/U+7yY5NfJhMwZlWqcT9jQRfMBnfpb+pjGg voO+yx8Em89gfalkzcJRLCWQC3WsUHGzLxLod9w0Tm1GEOyBxjP2Nk2TPwLlMaE2ZQo2 X/4si/pDbdI4gzEm2rpjSO0c2MyLMhmmwZluMD7TRqJ3Ia6kncF4Ciu2sTl52sBV0dYz Zn8diq3+bald2w7c/7SMa501mLH0rZUd9w4n+ttrZoLNLNtSpeuXyyOdqaRPa6P+e9lq eXQ4HfLGNXWmEf4mVktMAPrBeRs8pTcE/nbvAh9v1eKnNaCUeNx5ZCJYbU3buOMDPL3r oh8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728666549; x=1729271349; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bfj0B3nnHjUxcCyCalhugd5ngmM/JrZzfePhPsFLeh4=; b=WP6eozeMtj76LmZnX8pDkhBJsecFXLb3ZFCUWdjXSw0Nc5rIWFZLEAl4V0AZ12Qn4Z GWGevznQBxKnK+qyOq7iHT9ZVyPBjg+oRDUwRk6tZ8ZV+nOQFgMEc6EniWR5cCKAM9Hi N9rZDppnuBtABcR8PdUvWLYroac/o75+ICBhxRjiOd5Nzg3g/0uLaMwcGCGR3810SvFw dCtd1/iLNE9/7uAWvdB4tJlyQ2aU/jC2lDdKlkP9wTmKsjsN+tMIKPTlru6pewiEpEGP 3BRyqljQPmIH7gA6N/SEUAH1nYC1p7CznzhOMr68lZmPDy3KKftEHmYj+LaTMQCQ3IER uzvA== X-Forwarded-Encrypted: i=1; AJvYcCUFQXdBdFChO9/28Bmkqgm+XLUXkWkX2HJHJ9Qcp6LDzNtITBokjc7pCyZJGI3JbLkKTKekO64w3JVAkTvxlgU=@vger.kernel.org X-Gm-Message-State: AOJu0Yzj+T+wfOKbQ29WEuQgpQ84PxlgNio2ZNfLyDUBkkuGmOnGvgWK JQaavFo8QmrPR/uALj1oQs0vU6Ox/G8K1c/l4Y2nyfV8vVbUoWNPbPT/6ytBTJEJvBsUaw== X-Google-Smtp-Source: AGHT+IFvX/saq2RUfGzHvn3Yuhv8T3E5DzVonOGZChHXCjZPWicr/2IaSFuxMZRFRkaqCDpZgMyInJz/ X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a5d:6890:0:b0:374:badf:9b16 with SMTP id ffacd0b85a97d-37d54de7715mr5662f8f.0.1728666548505; Fri, 11 Oct 2024 10:09:08 -0700 (PDT) Date: Fri, 11 Oct 2024 19:08:50 +0200 In-Reply-To: <20241011170847.334429-10-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241011170847.334429-10-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=6898; i=ardb@kernel.org; h=from:subject; bh=2jvHADU7c8PNtY41id5iwZpe+A4RE0tCIX4zDl/IOwI=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIZ0zevGeuTemSKv5/VnNF3SJkT37m5jGny9B0qcX3hCdy FY1fcO/jlIWBjEOBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjCR2tMM/wteCl2WXp4vrqrU 8/PM1Tu7eE0Ka+2OVWe/PrexdoZEgQ3DP7MzGyWTi0uENn70OL9B8qPnx/4X27Z1bYtzXq58O1l YjB8A X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241011170847.334429-12-ardb+git@google.com> Subject: [PATCH v3 2/8] objtool: Allow arch code to discover jump table size From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: llvm@lists.linux.dev, keescook@chromium.org, linux-hardening@vger.kernel.org, nathan@kernel.org, Ard Biesheuvel , Josh Poimboeuf , Peter Zijlstra , Jan Beulich , "Jose E. Marchesi" , Kees Cook From: Ard Biesheuvel In preparation for adding support for annotated jump tables, where ELF relocations and symbols are used to describe the locations of jump tables in the executable, refactor the jump table discovery logic so the table size can be returned from arch_find_switch_table(). Signed-off-by: Ard Biesheuvel --- tools/objtool/arch/loongarch/special.c | 3 +- tools/objtool/arch/powerpc/special.c | 3 +- tools/objtool/arch/x86/special.c | 4 ++- tools/objtool/check.c | 31 +++++++++++++------- tools/objtool/include/objtool/check.h | 5 +++- tools/objtool/include/objtool/special.h | 3 +- 6 files changed, 33 insertions(+), 16 deletions(-) diff --git a/tools/objtool/arch/loongarch/special.c b/tools/objtool/arch/loongarch/special.c index 9bba1e9318e0..87230ed570fd 100644 --- a/tools/objtool/arch/loongarch/special.c +++ b/tools/objtool/arch/loongarch/special.c @@ -9,7 +9,8 @@ bool arch_support_alt_relocation(struct special_alt *special_alt, } struct reloc *arch_find_switch_table(struct objtool_file *file, - struct instruction *insn) + struct instruction *insn, + unsigned long *table_size) { return NULL; } diff --git a/tools/objtool/arch/powerpc/special.c b/tools/objtool/arch/powerpc/special.c index d33868147196..51610689abf7 100644 --- a/tools/objtool/arch/powerpc/special.c +++ b/tools/objtool/arch/powerpc/special.c @@ -13,7 +13,8 @@ bool arch_support_alt_relocation(struct special_alt *special_alt, } struct reloc *arch_find_switch_table(struct objtool_file *file, - struct instruction *insn) + struct instruction *insn, + unsigned long *table_size) { exit(-1); } diff --git a/tools/objtool/arch/x86/special.c b/tools/objtool/arch/x86/special.c index 415e4d035e53..f8fb67636384 100644 --- a/tools/objtool/arch/x86/special.c +++ b/tools/objtool/arch/x86/special.c @@ -109,7 +109,8 @@ bool arch_support_alt_relocation(struct special_alt *special_alt, * NOTE: MITIGATION_RETPOLINE made it harder still to decode dynamic jumps. */ struct reloc *arch_find_switch_table(struct objtool_file *file, - struct instruction *insn) + struct instruction *insn, + unsigned long *table_size) { struct reloc *text_reloc, *rodata_reloc; struct section *table_sec; @@ -150,5 +151,6 @@ struct reloc *arch_find_switch_table(struct objtool_file *file, if (!rodata_reloc) return NULL; + *table_size = 0; return rodata_reloc; } diff --git a/tools/objtool/check.c b/tools/objtool/check.c index aa07fdf1cf13..b73e43b9b9e3 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -150,6 +150,15 @@ static inline struct reloc *insn_jump_table(struct instruction *insn) return NULL; } +static inline unsigned long insn_jump_table_size(struct instruction *insn) +{ + if (insn->type == INSN_JUMP_DYNAMIC || + insn->type == INSN_CALL_DYNAMIC) + return insn->_jump_table_size; + + return 0; +} + static bool is_jump_table_jump(struct instruction *insn) { struct alt_group *alt_group = insn->alt_group; @@ -2099,6 +2108,7 @@ static int add_special_section_alts(struct objtool_file *file) static int add_jump_table(struct objtool_file *file, struct instruction *insn, struct reloc *next_table) { + unsigned long table_size = insn_jump_table_size(insn); struct symbol *pfunc = insn_func(insn)->pfunc; struct reloc *table = insn_jump_table(insn); unsigned int rtype = reloc_type(table); @@ -2116,6 +2126,8 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn, unsigned long addend = reloc_addend(reloc); /* Check for the end of the table: */ + if (table_size && reloc_offset(reloc) - reloc_offset(table) >= table_size) + break; if (reloc != table && reloc == next_table) break; @@ -2175,12 +2187,12 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn, * find_jump_table() - Given a dynamic jump, find the switch jump table * associated with it. */ -static struct reloc *find_jump_table(struct objtool_file *file, - struct symbol *func, - struct instruction *insn) +static void find_jump_table(struct objtool_file *file, struct symbol *func, + struct instruction *insn) { struct reloc *table_reloc; struct instruction *dest_insn, *orig_insn = insn; + unsigned long table_size; /* * Backward search using the @first_jump_src links, these help avoid @@ -2201,17 +2213,17 @@ static struct reloc *find_jump_table(struct objtool_file *file, insn->jump_dest->offset > orig_insn->offset)) break; - table_reloc = arch_find_switch_table(file, insn); + table_reloc = arch_find_switch_table(file, insn, &table_size); if (!table_reloc) continue; dest_insn = find_insn(file, table_reloc->sym->sec, reloc_addend(table_reloc)); if (!dest_insn || !insn_func(dest_insn) || insn_func(dest_insn)->pfunc != func) continue; - return table_reloc; + orig_insn->_jump_table = table_reloc; + orig_insn->_jump_table_size = table_size; + break; } - - return NULL; } /* @@ -2222,7 +2234,6 @@ static void mark_func_jump_tables(struct objtool_file *file, struct symbol *func) { struct instruction *insn, *last = NULL; - struct reloc *reloc; func_for_each_insn(file, func, insn) { if (!last) @@ -2245,9 +2256,7 @@ static void mark_func_jump_tables(struct objtool_file *file, if (insn->type != INSN_JUMP_DYNAMIC) continue; - reloc = find_jump_table(file, func, insn); - if (reloc) - insn->_jump_table = reloc; + find_jump_table(file, func, insn); } } diff --git a/tools/objtool/include/objtool/check.h b/tools/objtool/include/objtool/check.h index daa46f1f0965..e1cd13cd28a3 100644 --- a/tools/objtool/include/objtool/check.h +++ b/tools/objtool/include/objtool/check.h @@ -71,7 +71,10 @@ struct instruction { struct instruction *first_jump_src; union { struct symbol *_call_dest; - struct reloc *_jump_table; + struct { + struct reloc *_jump_table; + unsigned long _jump_table_size; + }; }; struct alternative *alts; struct symbol *sym; diff --git a/tools/objtool/include/objtool/special.h b/tools/objtool/include/objtool/special.h index 86d4af9c5aa9..e7ee7ffccefd 100644 --- a/tools/objtool/include/objtool/special.h +++ b/tools/objtool/include/objtool/special.h @@ -38,5 +38,6 @@ bool arch_support_alt_relocation(struct special_alt *special_alt, struct instruction *insn, struct reloc *reloc); struct reloc *arch_find_switch_table(struct objtool_file *file, - struct instruction *insn); + struct instruction *insn, + unsigned long *table_size); #endif /* _SPECIAL_H */ From patchwork Fri Oct 11 17:08:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13832742 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D87681C68BF for ; Fri, 11 Oct 2024 17:09:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728666554; cv=none; b=AcXfdk2infhgU5jkfNYX/m8lZLPoB1W0v/ajG64hyntwIgZQiarcFRTlYARd8G+EhWrm86WWO1HpzNw2bHEb9n/GO8V7sJtcm4zXOqrB91+sVvQENVnWYVfeNkvaIHgW3JnqobcvEaqSAVV77ikH7BboYdJH5awaj3DR49zVmM8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728666554; c=relaxed/simple; bh=0an784L5kpDmdK8mN18dKa6e13LcwOa+SLNxCEIV8x4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=LqUyMLeNGWI4K41cNJZU/fO2CHEkcreXIz46C9VVykXfoKM3uQ/DCO9H4y/PdzTKzQnRubl0MTfjgJNo3Ix12Caqaq5CqGZmcyi+rRrQVljf0RI2adovI4LJEtIy/r5RY1BKYB4dSwxbxnp/0BFDCOMXQ6q0rNgi2Qxh8Nr0TIc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=AoZYz15p; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="AoZYz15p" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-431186841c1so10429395e9.3 for ; Fri, 11 Oct 2024 10:09:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728666551; x=1729271351; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=WkO7vfvBh/UE7nLoKyOKLo43oPVJ4Pq/WcQaO1W6Bnw=; b=AoZYz15pq/n41Tj3w748Un0IG7GKI9FNw6zZN5LtJ1D7SBxT4ERepGdv7XVRvQb8Hi r3KXGb9k04ERYJeHk44FmCzUnRACMaqhdUxRC1Pb2o2BPd38Ut29nxHu6+8+cyzM/ktS ZdfmW4PwVF3NDwpdTtN8hko3ut+zUxfoYxN0vQb6SLg2R66x7bR2lv70TDo7ZhjfiC6u 48BkEy3JeB/IP3n0eqS/gDvvJv62sffqDaQSFMmfYJ1kbiH2yqeSo0UtCWWELjilxbb7 JsOblYpppQzEmETlo2xjnbTyUbiQMR8pyfGKOsrhBj/Yx5ukhPq2otXfVmNmLBvq0Vs4 jTLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728666551; x=1729271351; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WkO7vfvBh/UE7nLoKyOKLo43oPVJ4Pq/WcQaO1W6Bnw=; b=OXLIyAWEfr7Bp3nZzd/PjZJRkipyApAq/M7wjGI9e/NF/52+n4OYVCRRVTiPqXsi31 Nq4Q/2k5EBEgodbSqCnZ6SqW6i6na19smdj1pVUY+KuhLveI7mfqOuEwaha6JMR/XMRr hv1C1c43w6p6dPobwqP6aOGmlbJmAq/Gdb684rznSbJRQIzMKYI5iUExkwaw08D8DT7g j8Pp30Tidr/5m8bd+60xyJA4WmsHv7DPya/9sCPhhmIlLwZ2jsrCO3C6bgbx8SoSqNEk 8mvA7CTnZ8PczkrQc6AbcBVrXnsqbXbo4wWVPiswjCjzQSwdEI3nTr2Y2UphpfBjp6QA bpsQ== X-Forwarded-Encrypted: i=1; AJvYcCVTR9eM4b4tKhD4S5zp3/EvXt9dlYCoX0pbU7HuV2QMMsS33QfBtHOeWS726rjKNisIcRcYlL72OGfgOvNVZV8=@vger.kernel.org X-Gm-Message-State: AOJu0YysHMwm1laFNVlS6LHKJAelY6ggvrOfPxoRB6YGsQqV+xeMCkF8 DO0IAianmHA2wL/fBRHSj1DLdrFHHd2TDEOnZWHkGRnCK0G3AzPIUn0ymzbGgMLELje+vQ== X-Google-Smtp-Source: AGHT+IFed+Jkm09W0qDIY/JP8fTr1qhYOY3doVvTKyQfexNDvmU8Qio0ZZDRztBB0SAbldwZsy30PHAh X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:600c:1c96:b0:42c:a8b5:c1b with SMTP id 5b1f17b1804b1-4311de86414mr276615e9.2.1728666551095; Fri, 11 Oct 2024 10:09:11 -0700 (PDT) Date: Fri, 11 Oct 2024 19:08:51 +0200 In-Reply-To: <20241011170847.334429-10-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241011170847.334429-10-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=3782; i=ardb@kernel.org; h=from:subject; bh=A2/RO6uk5klpid4u/+fW8dDiGa2vurlmiGxn2abwXOw=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIZ0zesm2OynG/CefvNqzuP2B3f2Z556oucZEiLe5TWrVM c4UbnjcUcrCIMbBICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACYy8Rwjw9964dRnPnNTMjNf xZ67JjnNas4TGxb2uVeiciP75zndvMvwz/bi+aiTD+/42b7psc+aa3V49Z5j2h/UKj5/OSr6N9j kBD8A X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241011170847.334429-13-ardb+git@google.com> Subject: [PATCH v3 3/8] objtool: Make some helper functions globally accessible From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: llvm@lists.linux.dev, keescook@chromium.org, linux-hardening@vger.kernel.org, nathan@kernel.org, Ard Biesheuvel , Josh Poimboeuf , Peter Zijlstra , Jan Beulich , "Jose E. Marchesi" , Kees Cook From: Ard Biesheuvel Move some helpers around so they can be used from arch specific jump table code that is getting refactored in the next patch. Signed-off-by: Ard Biesheuvel --- tools/objtool/check.c | 22 ++++---------------- tools/objtool/include/objtool/check.h | 16 ++++++++++++++ 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index b73e43b9b9e3..fbb05e973acc 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -61,8 +61,8 @@ struct instruction *next_insn_same_sec(struct objtool_file *file, return insn; } -static struct instruction *next_insn_same_func(struct objtool_file *file, - struct instruction *insn) +struct instruction *next_insn_same_func(struct objtool_file *file, + struct instruction *insn) { struct instruction *next = next_insn_same_sec(file, insn); struct symbol *func = insn_func(insn); @@ -93,8 +93,8 @@ static struct instruction *prev_insn_same_sec(struct objtool_file *file, return insn - 1; } -static struct instruction *prev_insn_same_sym(struct objtool_file *file, - struct instruction *insn) +struct instruction *prev_insn_same_sym(struct objtool_file *file, + struct instruction *insn) { struct instruction *prev = prev_insn_same_sec(file, insn); @@ -110,11 +110,6 @@ static struct instruction *prev_insn_same_sym(struct objtool_file *file, for_each_sec(file, __sec) \ sec_for_each_insn(file, __sec, insn) -#define func_for_each_insn(file, func, insn) \ - for (insn = find_insn(file, func->sec, func->offset); \ - insn; \ - insn = next_insn_same_func(file, insn)) - #define sym_for_each_insn(file, sym, insn) \ for (insn = find_insn(file, sym->sec, sym->offset); \ insn && insn->offset < sym->offset + sym->len; \ @@ -141,15 +136,6 @@ static inline struct symbol *insn_call_dest(struct instruction *insn) return insn->_call_dest; } -static inline struct reloc *insn_jump_table(struct instruction *insn) -{ - if (insn->type == INSN_JUMP_DYNAMIC || - insn->type == INSN_CALL_DYNAMIC) - return insn->_jump_table; - - return NULL; -} - static inline unsigned long insn_jump_table_size(struct instruction *insn) { if (insn->type == INSN_JUMP_DYNAMIC || diff --git a/tools/objtool/include/objtool/check.h b/tools/objtool/include/objtool/check.h index e1cd13cd28a3..e2f755484c4a 100644 --- a/tools/objtool/include/objtool/check.h +++ b/tools/objtool/include/objtool/check.h @@ -114,14 +114,30 @@ static inline bool is_jump(struct instruction *insn) return is_static_jump(insn) || is_dynamic_jump(insn); } +static inline struct reloc *insn_jump_table(struct instruction *insn) +{ + if (insn->type == INSN_JUMP_DYNAMIC || + insn->type == INSN_CALL_DYNAMIC) + return insn->_jump_table; + + return NULL; +} + struct instruction *find_insn(struct objtool_file *file, struct section *sec, unsigned long offset); +struct instruction *prev_insn_same_sym(struct objtool_file *file, struct instruction *insn); struct instruction *next_insn_same_sec(struct objtool_file *file, struct instruction *insn); +struct instruction *next_insn_same_func(struct objtool_file *file, struct instruction *insn); #define sec_for_each_insn(file, _sec, insn) \ for (insn = find_insn(file, _sec, 0); \ insn && insn->sec == _sec; \ insn = next_insn_same_sec(file, insn)) +#define func_for_each_insn(file, func, insn) \ + for (insn = find_insn(file, func->sec, func->offset); \ + insn; \ + insn = next_insn_same_func(file, insn)) + #endif /* _CHECK_H */ From patchwork Fri Oct 11 17:08:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13832743 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD5991C8FAA for ; Fri, 11 Oct 2024 17:09:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728666556; cv=none; b=BC5Qh1d4dsfnJ953jBx3Htw2anrrhZip1G87oCBYFOaSbs+PvPxL2gGN51w0V1u4FTCJtLj2H64QtNnm5kXWVay5m2EYhf85+0gko/57098LHXmSoawDILSYVnA0fd4V+QfiSrAJ4fizJy3HIRAb9GB3VPAKr+dB41PorXlQpj0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728666556; c=relaxed/simple; bh=sRBC3imC2fsNC2651TKH0KS++dKocaTpJWGS92trpJI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VilgCtFtWWF+alUuZNcqwU4YiJvnMzzKS4XJ8XzWXWzH7I+tLQ6QBw63CCe3z2H2RODyOM+69ZW3Qmv/UJEaPiG6A7hbRrfB+WYTmv9ilFiR9939PUEYQeP9XX9FJJXJwntBO3S1QxgbmXo28PR1noUYQpa+JQsO9wBlPf1X3Es= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=uSOi1Eyo; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uSOi1Eyo" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e35865abe9so7554487b3.0 for ; Fri, 11 Oct 2024 10:09:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728666554; x=1729271354; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=5NFjU8MWypV/vmYVD9Z8mN2L/hjHRNn6Cg6IdwFQVZM=; b=uSOi1EyooFA6gjHawXWVqci9Bykcixp/hd/8E8yf4qkif4inwH/yokm4/itPW1NNgy qT9NkcV2jPLvwTUVQ6o1hXfB/ugtb3hwNs7GjpLNbabP6qvou7p6AlRu41w+2UQaSQ5K CjvCO8dibaLYnxFHI3MoNbn900Agf5091GfNTy0E4Ehu5MFDG/tVh2pZfTM3fCfFz2Ft qC8YfjwkWLyoPet7IFsRJd15yXRfRFoS09M+AWrhfXbXZcwdpHBQW3H+qNEZOctgBq+E Oc114aJ9nVeDP5sjmDQj1gvEGzJ63arVHYTK9kBju/i5my4V2GwjhBJJn9KHV26dZJ5W FY7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728666554; x=1729271354; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5NFjU8MWypV/vmYVD9Z8mN2L/hjHRNn6Cg6IdwFQVZM=; b=lP5aFS/Wfek+lbiO8nWcX3ANPyZNzis54fLpCo3eQ6S2M+QLUpIFiBQbBU+nB5T3gi lhlOu9PVL3PKnsqpsERCMeJgJtRMGyjFfxH+MHpzeuVuhd6i+Mq2zGTred5GoSyYiXD5 2coPgxNLqch/onCVq7wHPqaM+gRd8hPTv/z6m8Jgt1oOCxrpadZ5jieBrM2Ixt7fXpk5 VBi28WrIV0jPAj7pvLHdYJj+hf4d08de0FNp9+FyJu2PLVMKD8amRLLXyuuhPI9axY5b xk7sdK7XKbM3b2WwaMS5zuw3yKRfUlLep4L4CBL8tZCQw3qJQ50Ss1kORvwVzp2jvkvE R3kw== X-Forwarded-Encrypted: i=1; AJvYcCVVE9DLO/wLiBoaCNrNrYq0956G+ku6j4wfXfJCcgkWvT9BxpueEWmQKz4R7EOUHzqjF+hddLqY45Jmg8fuqdo=@vger.kernel.org X-Gm-Message-State: AOJu0YzNwkfLX4yJcgI7OrqrHh1gRdQHt8dHyBfvRr8CAF4fGm0JCL29 cwy2zokUVOLCwi8UdMy89Ra6g723KHWHM+3b8SMATy0Gby6ZKMDf9MlInRxEs/c5OdauUA== X-Google-Smtp-Source: AGHT+IGMNagPLd2f5e3cks8tlI/NbU5Eiyxm0UEjGwCJmB1Pvx62gpt8GDgIRWCX+baPrvA1I2aArE9A X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:690c:5048:b0:6db:bd8f:2c59 with SMTP id 00721157ae682-6e347b3aa77mr657047b3.4.1728666553837; Fri, 11 Oct 2024 10:09:13 -0700 (PDT) Date: Fri, 11 Oct 2024 19:08:52 +0200 In-Reply-To: <20241011170847.334429-10-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241011170847.334429-10-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=10349; i=ardb@kernel.org; h=from:subject; bh=Ubt7RQjuRDIfCARVycqrXHI/7Y7hGzCx+rj5JuA2brA=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIZ0zemnHHr2KoxPDrWaG8jOvEHNbtmqutkHemsRvq+fMK TE41jWjo5SFQYyDQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAEyku5zhf8LMnDCH1BctSt81 m+5cDbr5KerLtMDgrU32/x4zLv9pWs3IsK/V6oTzsQ/P/+/V7FUU+mq3Skz3cuI6ZgO+fEnha5l PGQE= X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241011170847.334429-14-ardb+git@google.com> Subject: [PATCH v3 4/8] objtool: Move jump table heuristics to a x86 specific source file From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: llvm@lists.linux.dev, keescook@chromium.org, linux-hardening@vger.kernel.org, nathan@kernel.org, Ard Biesheuvel , Josh Poimboeuf , Peter Zijlstra , Jan Beulich , "Jose E. Marchesi" , Kees Cook From: Ard Biesheuvel In preparation for implementing support for the use of compiler emitted jump table annotations, move the existing code out of the generic sources. This will permit a clean separation between the two approaches, where the old one will not be wired up for architectures other than x86. Signed-off-by: Ard Biesheuvel --- tools/objtool/arch/loongarch/special.c | 7 -- tools/objtool/arch/powerpc/special.c | 7 -- tools/objtool/arch/x86/special.c | 114 +++++++++++++++++++- tools/objtool/check.c | 112 +------------------ tools/objtool/include/objtool/special.h | 9 +- 5 files changed, 122 insertions(+), 127 deletions(-) diff --git a/tools/objtool/arch/loongarch/special.c b/tools/objtool/arch/loongarch/special.c index 87230ed570fd..acf3a391a2f9 100644 --- a/tools/objtool/arch/loongarch/special.c +++ b/tools/objtool/arch/loongarch/special.c @@ -7,10 +7,3 @@ bool arch_support_alt_relocation(struct special_alt *special_alt, { return false; } - -struct reloc *arch_find_switch_table(struct objtool_file *file, - struct instruction *insn, - unsigned long *table_size) -{ - return NULL; -} diff --git a/tools/objtool/arch/powerpc/special.c b/tools/objtool/arch/powerpc/special.c index 51610689abf7..3a108437cfa6 100644 --- a/tools/objtool/arch/powerpc/special.c +++ b/tools/objtool/arch/powerpc/special.c @@ -11,10 +11,3 @@ bool arch_support_alt_relocation(struct special_alt *special_alt, { exit(-1); } - -struct reloc *arch_find_switch_table(struct objtool_file *file, - struct instruction *insn, - unsigned long *table_size) -{ - exit(-1); -} diff --git a/tools/objtool/arch/x86/special.c b/tools/objtool/arch/x86/special.c index f8fb67636384..cd964b85e2b1 100644 --- a/tools/objtool/arch/x86/special.c +++ b/tools/objtool/arch/x86/special.c @@ -108,9 +108,9 @@ bool arch_support_alt_relocation(struct special_alt *special_alt, * * NOTE: MITIGATION_RETPOLINE made it harder still to decode dynamic jumps. */ -struct reloc *arch_find_switch_table(struct objtool_file *file, - struct instruction *insn, - unsigned long *table_size) +static struct reloc *find_switch_table(struct objtool_file *file, + struct instruction *insn, + unsigned long *table_size) { struct reloc *text_reloc, *rodata_reloc; struct section *table_sec; @@ -154,3 +154,111 @@ struct reloc *arch_find_switch_table(struct objtool_file *file, *table_size = 0; return rodata_reloc; } + +/* + * find_jump_table() - Given a dynamic jump, find the switch jump table + * associated with it. + */ +static void find_jump_table(struct objtool_file *file, + struct symbol *func, + struct instruction *insn) +{ + struct reloc *table_reloc; + struct instruction *dest_insn, *orig_insn = insn; + unsigned long table_size; + + /* + * Backward search using the @first_jump_src links, these help avoid + * much of the 'in between' code. Which avoids us getting confused by + * it. + */ + for (; + insn && insn_func(insn) && insn_func(insn)->pfunc == func; + insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) { + + if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC) + break; + + /* allow small jumps within the range */ + if (insn->type == INSN_JUMP_UNCONDITIONAL && + insn->jump_dest && + (insn->jump_dest->offset <= insn->offset || + insn->jump_dest->offset > orig_insn->offset)) + break; + + table_reloc = find_switch_table(file, insn, &table_size); + if (!table_reloc) + continue; + dest_insn = find_insn(file, table_reloc->sym->sec, reloc_addend(table_reloc)); + if (!dest_insn || !insn_func(dest_insn) || insn_func(dest_insn)->pfunc != func) + continue; + + orig_insn->_jump_table = table_reloc; + orig_insn->_jump_table_size = table_size; + break; + } +} + +/* + * First pass: Mark the head of each jump table so that in the next pass, + * we know when a given jump table ends and the next one starts. + */ +static void mark_func_jump_tables(struct objtool_file *file, + struct symbol *func) +{ + struct instruction *insn, *last = NULL; + + func_for_each_insn(file, func, insn) { + if (!last) + last = insn; + + /* + * Store back-pointers for unconditional forward jumps such + * that find_jump_table() can back-track using those and + * avoid some potentially confusing code. + */ + if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest && + insn->offset > last->offset && + insn->jump_dest->offset > insn->offset && + !insn->jump_dest->first_jump_src) { + + insn->jump_dest->first_jump_src = insn; + last = insn->jump_dest; + } + + if (insn->type == INSN_JUMP_DYNAMIC) + find_jump_table(file, func, insn); + } +} + +int add_func_jump_tables(struct objtool_file *file, + struct symbol *func) +{ + struct instruction *insn, *insn_t1 = NULL, *insn_t2; + int ret = 0; + + mark_func_jump_tables(file, func); + + func_for_each_insn(file, func, insn) { + if (!insn_jump_table(insn)) + continue; + + if (!insn_t1) { + insn_t1 = insn; + continue; + } + + insn_t2 = insn; + + ret = add_jump_table(file, insn_t1, insn_jump_table(insn_t2)); + if (ret) + return ret; + + insn_t1 = insn_t2; + } + + if (insn_t1) + ret = add_jump_table(file, insn_t1, NULL); + + return ret; +} diff --git a/tools/objtool/check.c b/tools/objtool/check.c index fbb05e973acc..389475dde47c 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -2091,8 +2091,8 @@ static int add_special_section_alts(struct objtool_file *file) return ret; } -static int add_jump_table(struct objtool_file *file, struct instruction *insn, - struct reloc *next_table) +int add_jump_table(struct objtool_file *file, struct instruction *insn, + struct reloc *next_table) { unsigned long table_size = insn_jump_table_size(insn); struct symbol *pfunc = insn_func(insn)->pfunc; @@ -2169,111 +2169,10 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn, return 0; } -/* - * find_jump_table() - Given a dynamic jump, find the switch jump table - * associated with it. - */ -static void find_jump_table(struct objtool_file *file, struct symbol *func, - struct instruction *insn) +int __weak add_func_jump_tables(struct objtool_file *file, + struct symbol *func) { - struct reloc *table_reloc; - struct instruction *dest_insn, *orig_insn = insn; - unsigned long table_size; - - /* - * Backward search using the @first_jump_src links, these help avoid - * much of the 'in between' code. Which avoids us getting confused by - * it. - */ - for (; - insn && insn_func(insn) && insn_func(insn)->pfunc == func; - insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) { - - if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC) - break; - - /* allow small jumps within the range */ - if (insn->type == INSN_JUMP_UNCONDITIONAL && - insn->jump_dest && - (insn->jump_dest->offset <= insn->offset || - insn->jump_dest->offset > orig_insn->offset)) - break; - - table_reloc = arch_find_switch_table(file, insn, &table_size); - if (!table_reloc) - continue; - dest_insn = find_insn(file, table_reloc->sym->sec, reloc_addend(table_reloc)); - if (!dest_insn || !insn_func(dest_insn) || insn_func(dest_insn)->pfunc != func) - continue; - - orig_insn->_jump_table = table_reloc; - orig_insn->_jump_table_size = table_size; - break; - } -} - -/* - * First pass: Mark the head of each jump table so that in the next pass, - * we know when a given jump table ends and the next one starts. - */ -static void mark_func_jump_tables(struct objtool_file *file, - struct symbol *func) -{ - struct instruction *insn, *last = NULL; - - func_for_each_insn(file, func, insn) { - if (!last) - last = insn; - - /* - * Store back-pointers for unconditional forward jumps such - * that find_jump_table() can back-track using those and - * avoid some potentially confusing code. - */ - if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest && - insn->offset > last->offset && - insn->jump_dest->offset > insn->offset && - !insn->jump_dest->first_jump_src) { - - insn->jump_dest->first_jump_src = insn; - last = insn->jump_dest; - } - - if (insn->type != INSN_JUMP_DYNAMIC) - continue; - - find_jump_table(file, func, insn); - } -} - -static int add_func_jump_tables(struct objtool_file *file, - struct symbol *func) -{ - struct instruction *insn, *insn_t1 = NULL, *insn_t2; - int ret = 0; - - func_for_each_insn(file, func, insn) { - if (!insn_jump_table(insn)) - continue; - - if (!insn_t1) { - insn_t1 = insn; - continue; - } - - insn_t2 = insn; - - ret = add_jump_table(file, insn_t1, insn_jump_table(insn_t2)); - if (ret) - return ret; - - insn_t1 = insn_t2; - } - - if (insn_t1) - ret = add_jump_table(file, insn_t1, NULL); - - return ret; + return 0; } /* @@ -2293,7 +2192,6 @@ static int add_jump_table_alts(struct objtool_file *file) if (func->type != STT_FUNC) continue; - mark_func_jump_tables(file, func); ret = add_func_jump_tables(file, func); if (ret) return ret; diff --git a/tools/objtool/include/objtool/special.h b/tools/objtool/include/objtool/special.h index e7ee7ffccefd..019b511eca6e 100644 --- a/tools/objtool/include/objtool/special.h +++ b/tools/objtool/include/objtool/special.h @@ -37,7 +37,10 @@ void arch_handle_alternative(unsigned short feature, struct special_alt *alt); bool arch_support_alt_relocation(struct special_alt *special_alt, struct instruction *insn, struct reloc *reloc); -struct reloc *arch_find_switch_table(struct objtool_file *file, - struct instruction *insn, - unsigned long *table_size); + +int add_func_jump_tables(struct objtool_file *file, struct symbol *func); + +int add_jump_table(struct objtool_file *file, struct instruction *insn, + struct reloc *next_table); + #endif /* _SPECIAL_H */ From patchwork Fri Oct 11 17:08:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13832744 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4D7C1CC162 for ; Fri, 11 Oct 2024 17:09:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728666558; cv=none; b=FBW6OlEP2S41mZM/q7hzDXlpEvQ/xuVrMHk+C2RksV8RQv3KxBbCUon/8Toi4e0b/h8rdWGAYox+e9aeXVWBVjg3DGeoxHiDP8S7aJz6Gqdrifw4Rc1mHya3gHAeB/Segry1gKw6mdLe6OmLNpqggw8pTDz2fjvAOr6c06czkC4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728666558; c=relaxed/simple; bh=sAeCweVaF++1xPImV9kFPIzONcEieIfdQoCU4nI32ZM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cIE+RoDwnqIALmq3s7UU6UNA5KSxADr25EAiIep3SC/Vb0cjTJ7N5H9EYpX9D3AWVXIi//iVyxJUNyA7+UEYSX2Qnm3f+BSH38O9v4xEhhQgSaVk5phGdWvhA/AIeXdhal4nwiW3Xbn3F6Bo4jxj7PZ4uG2szwmg8O8fy7Eb65I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ubrs4Hq2; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ubrs4Hq2" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e347b1e29dso14573297b3.0 for ; Fri, 11 Oct 2024 10:09:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728666556; x=1729271356; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=tkyFy75UvalNrKBAT+vGmyHx9oLFSJKfRi17QpMpSIY=; b=ubrs4Hq22sL0CeNjkrYSUGpWEWqjBdPyccWJxkRo55hazWY6aQjm94wIZOHwPwBkPj BszVaqeprZbYwjWnKTky7VNRZfsHohHmRoOh4zUr1BsYY5g+IG5GN6MNsrlyHFdBBGkn 4ESYDuQN4l9qoKj9/n2ZBsUTB5aXW2lLgosTz+MKF7xMIOHhgJLD0mvuV+H6OecZzhr9 km/oUyxuyBdy3572TGTxxUrpDnzRlH6WE7bh1llSV+Nq331Pr7A/MguHlp06YY2kbXeK tTb+lJWNOqL95N5ebUnVd9drk+qbECgJHPVNfe6S25XusNWNw3gEZC6XFIJBqGJXdwda iz6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728666556; x=1729271356; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tkyFy75UvalNrKBAT+vGmyHx9oLFSJKfRi17QpMpSIY=; b=dEUBPna+UnAWxjmOEosYuxqERYAT8LPuZPAkcfK+qxVU8aPWKw6Lo/uPgI901+MUXD j5+kTsNorYr7YjI9qEigoewFM/9OzLwfDKMctz2xfYuW/tjf6rnWUK8B61SzVCNI1Fw3 0udnQVHbftdK03PVOe3W9R0qy5ufZ8o2bSs71gSSFZwd7d3hh39v6IqJ2sw1SIdkLLJW NFAVO8Q4gMDdQRYej9I20jdVLUa/MEkQUUzjwyJ0cAY716g7CoV+UI1JGKf1fcq24vRM rP1i5HRKHFpi7NFyxgKMQuzIMYKeZp3ofILsAiv0YaHo2FxtUgUvLrGKBSe4sSKjS5w/ xNgw== X-Forwarded-Encrypted: i=1; AJvYcCXQaa3pmj2YgQAewhBfyTpHTHBhKqmTx1yb282jyD7B43RyrEhsw74B+SPrrsqUF/PaVFqt4bDLNjOKVfqRpY8=@vger.kernel.org X-Gm-Message-State: AOJu0YxVUyUT4dCS5lzfBFnxEdskDXMKDB3hMiVOWD6mbB9c+vaPJMSs Tay7s6e4Zyj+qMK5VghlQloUAE/Kp0WnJpuUwPghjp8Ry+RUMNgBq59ARKFZt5PLq/+MnA== X-Google-Smtp-Source: AGHT+IG8uyTeSXYrP2xbYDgV1i6m+IIf5hhHRlJp4OnDNqMleSdN0I9pfGlD5rZwlLnRFzNchILK3KTJ X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:690c:2e01:b0:6dd:fda3:6568 with SMTP id 00721157ae682-6e347b36782mr258307b3.3.1728666556111; Fri, 11 Oct 2024 10:09:16 -0700 (PDT) Date: Fri, 11 Oct 2024 19:08:53 +0200 In-Reply-To: <20241011170847.334429-10-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241011170847.334429-10-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=7204; i=ardb@kernel.org; h=from:subject; bh=gMBS2qjTsD8TMc2JpqTm0UjfHcu7H8Q7pFD2ywB6NtE=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIZ0zevm59bWM+taeTtUnFd+8kE8slUlWup/d9WzJm8u1R 0TuHcvsKGVhEONgkBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABNZFc7wP/hEvt/0VkWLiV+k /TlDwo0Yp6ZNLDw3ndm25V+cllivHiPDtZwqc5PY5VPdi48k9an47rLmOcLw181DRpn7DAevpCQ zAA== X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241011170847.334429-15-ardb+git@google.com> Subject: [PATCH v3 5/8] objtool: Add generic support for jump table annotations From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: llvm@lists.linux.dev, keescook@chromium.org, linux-hardening@vger.kernel.org, nathan@kernel.org, Ard Biesheuvel , Josh Poimboeuf , Peter Zijlstra , Jan Beulich , "Jose E. Marchesi" , Kees Cook From: Ard Biesheuvel Refactor the jump table handling code so that a generic code path is provided that can identify jump tables attached to indirect jumps based only on compiler provided annotations. This will be used by non-x86 architectures which do not support jump tables at all at this point. Refactor the x86 code to share the logic that follows relocations on instructions into the .rodata section and finds the associated symbols. Signed-off-by: Ard Biesheuvel --- tools/objtool/arch/x86/special.c | 46 ++++------ tools/objtool/check.c | 88 +++++++++++++++++++- tools/objtool/include/objtool/check.h | 4 + 3 files changed, 106 insertions(+), 32 deletions(-) diff --git a/tools/objtool/arch/x86/special.c b/tools/objtool/arch/x86/special.c index cd964b85e2b1..08a5ce662974 100644 --- a/tools/objtool/arch/x86/special.c +++ b/tools/objtool/arch/x86/special.c @@ -112,46 +112,34 @@ static struct reloc *find_switch_table(struct objtool_file *file, struct instruction *insn, unsigned long *table_size) { - struct reloc *text_reloc, *rodata_reloc; - struct section *table_sec; - unsigned long table_offset; - - /* look for a relocation which references .rodata */ - text_reloc = find_reloc_by_dest_range(file->elf, insn->sec, - insn->offset, insn->len); - if (!text_reloc || text_reloc->sym->type != STT_SECTION || - !text_reloc->sym->sec->rodata) - return NULL; - - table_offset = reloc_addend(text_reloc); - table_sec = text_reloc->sym->sec; + struct reloc *rodata_reloc; + struct symbol *sym = NULL; - if (reloc_type(text_reloc) == R_X86_64_PC32) - table_offset += 4; + /* + * Each table entry has a rela associated with it. The rela + * should reference text in the same function as the original + * instruction. + */ + rodata_reloc = find_rodata_sym_reference(file, insn, &sym); /* - * Make sure the .rodata address isn't associated with a - * symbol. GCC jump tables are anonymous data. + * Annotations, if present, are attached to the indirect jump + * instruction directly. In this case, a symbol annotation is + * expected. + * + * Otherwise, make sure the .rodata address isn't associated with + * a symbol. Unannotated GCC jump tables are anonymous data. * * Also support C jump tables which are in the same format as * switch jump tables. For objtool to recognize them, they * need to be placed in the C_JUMP_TABLE_SECTION section. They * have symbols associated with them. */ - if (find_symbol_containing(table_sec, table_offset) && - strcmp(table_sec->name, C_JUMP_TABLE_SECTION)) - return NULL; - - /* - * Each table entry has a rela associated with it. The rela - * should reference text in the same function as the original - * instruction. - */ - rodata_reloc = find_reloc_by_dest(file->elf, table_sec, table_offset); - if (!rodata_reloc) + if (insn->type != INSN_JUMP_DYNAMIC && sym && + strcmp(sym->sec->name, C_JUMP_TABLE_SECTION)) return NULL; - *table_size = 0; + *table_size = sym ? sym->len : 0; return rodata_reloc; } diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 389475dde47c..b923d4a4efcb 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -1372,6 +1372,8 @@ __weak const char *arch_nop_fentry_call(int len) static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn) { + unsigned long offset = insn->offset; + unsigned int len = insn->len; struct reloc *reloc; if (insn->no_reloc) @@ -1380,8 +1382,12 @@ static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *i if (!file) return NULL; - reloc = find_reloc_by_dest_range(file->elf, insn->sec, - insn->offset, insn->len); + do { + /* Skip any R_*_NONE relocations */ + reloc = find_reloc_by_dest_range(file->elf, insn->sec, + offset++, len--); + } while (len && reloc && reloc_type(reloc) == R_NONE); + if (!reloc) { insn->no_reloc = 1; return NULL; @@ -2169,10 +2175,86 @@ int add_jump_table(struct objtool_file *file, struct instruction *insn, return 0; } +struct reloc *find_rodata_sym_reference(struct objtool_file *file, + struct instruction *insn, + struct symbol **table_sym) +{ + struct reloc *text_reloc, *rodata_reloc; + unsigned long addend; + struct symbol *sym; + + /* + * Look for a relocation which references .rodata. We must use + * find_reloc_by_dest_range() directly here, as insn_reloc() filters + * out R_*_NONE relocations which are used for jump table annotations. + */ + text_reloc = find_reloc_by_dest_range(file->elf, insn->sec, + insn->offset, insn->len); + if (!text_reloc) { + insn->no_reloc = 1; + return NULL; + } + + sym = text_reloc->sym; + if (!sym->sec->rodata) + return NULL; + + if (reloc_type(text_reloc) == elf_data_rela_type(file->elf)) + addend = arch_dest_reloc_offset(reloc_addend(text_reloc)); + else + addend = reloc_addend(text_reloc); + + rodata_reloc = find_reloc_by_dest(file->elf, sym->sec, + sym->offset + addend); + if (!rodata_reloc) + return NULL; + + /* + * Find the ELF symbol covering the destination of the relocation. This + * is trivial if the reloc refers to a STT_OBJECT directly, but it may + * have been emitted as section relative as well. + */ + if (sym->type == STT_SECTION) + sym = find_symbol_containing(sym->sec, addend); + + *table_sym = sym; + return rodata_reloc; +} + +/* + * Generic version of jump table handling, relying strictly on annotations + * provided by the compiler. Overridden for x86 using heuristics that attempt + * to correlate indirect jump instructions with preceding .rodata references. + */ int __weak add_func_jump_tables(struct objtool_file *file, struct symbol *func) { - return 0; + struct instruction *insn; + int ret = 0; + + func_for_each_insn(file, func, insn) { + struct reloc *reloc; + struct symbol *sym; + + if (insn->type != INSN_JUMP_DYNAMIC) + continue; + + /* + * Look for a relocation attached to this indirect jump that + * references an ELF object in .rodata. This should be the jump + * table annotation emitted by the compiler. + */ + reloc = find_rodata_sym_reference(file, insn, &sym); + if (reloc && sym && sym->len) { + insn->_jump_table = reloc; + insn->_jump_table_size = sym->len; + + ret = add_jump_table(file, insn, NULL); + if (ret) + break; + } + } + return ret; } /* diff --git a/tools/objtool/include/objtool/check.h b/tools/objtool/include/objtool/check.h index e2f755484c4a..7781100c9340 100644 --- a/tools/objtool/include/objtool/check.h +++ b/tools/objtool/include/objtool/check.h @@ -140,4 +140,8 @@ struct instruction *next_insn_same_func(struct objtool_file *file, struct instru insn; \ insn = next_insn_same_func(file, insn)) +struct reloc *find_rodata_sym_reference(struct objtool_file *file, + struct instruction *insn, + struct symbol **sym); + #endif /* _CHECK_H */ From patchwork Fri Oct 11 17:08:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13832745 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 46D421CEE97 for ; Fri, 11 Oct 2024 17:09:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728666561; cv=none; b=SvirF+NcIBjVgIl+7tQgJGpuBf1tmLkQg0TlYg4WNx7lpk5RO7oFlHY2At3Z3PPLzAjB9JVZdXreytP8erAvb955sNf1GgXbX6xXAkhxvB1gv6P2nYbFayMTL5cUjOqvMy76fZnr4+EiZlNdf28gc70PzIcQlyPbLSFCGuxxFus= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728666561; c=relaxed/simple; bh=Je70niWxlsrHVgag6J/kzDZ6SflRUbpCOeNQjbweCs8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gjVIizUDLafDbJKMfG+gLOqDTWTLfW5X1QI58bq1frzfrOcnTfIilulHAxjJw0RruQgf6fEj9H4C9KSw1Mp4Kv7ipIi5AUlACEewnQMeOaHARbJvJIkUHDZK6meufzcRoHuIPlDABdnPi8uMTLKKqSnOr4C6K2tuqFgnDovf0wo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vonpA/0D; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vonpA/0D" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e165fc5d94fso3508949276.2 for ; Fri, 11 Oct 2024 10:09:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728666558; x=1729271358; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=2msbXnFeBvXSlHbQOfDHqTuvhI8e8tpl7Wq7z97LNd0=; b=vonpA/0DrKj/nu24S0NQvMlQVCqGphKlv774uf58UTncKfYXIntqZ7iESi//wTv/XY H9l9PKjKowcuPZNyjNjV0KCPUpMglH24j2c2G6NCkOx/RDEaLzhpuw28hUZqTfQKV0dD xO/4fDebSGe7unKwS48P+v7rMqp12wCtI+5qh27GV5pZoZSpLNvfK22NF9TvT6ay20Eo EO72QN7yVff248fYhpafUaBUG2Nfhw3qeBJeo50XNZd8G01fHqklOko9izeFI5vg8aEg 3c/Euio/OzGHjV3l6yJKRBKwZCBmvCbgXB2SXk/t9NbD2DAim7h40LxlivIV2bTOH+e+ uIbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728666558; x=1729271358; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2msbXnFeBvXSlHbQOfDHqTuvhI8e8tpl7Wq7z97LNd0=; b=Y3wUg5oHlFW5K0Wd8H+y/OOujeFqDzEnFRYVZDskkZKWrs2AnjhzSG3zPct32/ZPyO Nc/zCwM51Tvz33laB7KDgGckPVd81YUeSfeCknyhtKYKn2H5hc+sPjSBBdRA/HDHnYNN GA+4lZuEuuRumgK55lNobRuT5FMNZuXiGYNp0IM/QUsM4m/wfPIO7YnWgq71cln2nja2 vS/YId2bLH+7CdGHNoZnMvz4tBz8WHRYqbBsyFy45627IBZ5rFBsATEwsKYb/5IgUpNp 1BWMTibJ7Z2ocnJoBlHHJbD4SFxvhz+i2ZOkYiKDhulU2yicGum2RAwDGR5gXsCF15nh brsw== X-Forwarded-Encrypted: i=1; AJvYcCXavvj2i4UZt3ier1okwYF/p4cyb5G3DbSVsrzkcC5YsEwLOxdo38zj6rC3hZBdtTu+wpTVEswK7IPwNoiLIGA=@vger.kernel.org X-Gm-Message-State: AOJu0YxgzJmzGuJ3js3Jlr85hxVqHWyPGsFKJo2nXbGoWahqeUEnbpOZ PZh/QWiPGMDjgzH3g326LZHxfImtj3CFhWnGvf27Xne1B8e8sPgVwKpXIFp8FNUeDF5o8w== X-Google-Smtp-Source: AGHT+IENvAkwhnL9xlh8jftp93Hwt0IxXAkKRHhznXl4M3FYlzwM5kPXAaj0d7TOhJKUCzOqrr+HUbBx X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a25:8050:0:b0:e11:584c:76e2 with SMTP id 3f1490d57ef6-e2919d82cf4mr6763276.2.1728666558271; Fri, 11 Oct 2024 10:09:18 -0700 (PDT) Date: Fri, 11 Oct 2024 19:08:54 +0200 In-Reply-To: <20241011170847.334429-10-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241011170847.334429-10-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1552; i=ardb@kernel.org; h=from:subject; bh=gpTsTI/SBCSlqxjQQW/Apkngf1lwQqJWx5rnk4mqsKs=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIZ0zesVLQ/4ILm0m3gzzi8t+H5uyXLmzrXVBp+fyFyIi2 5hyBSs6SlkYxDgYZMUUWQRm/3238/REqVrnWbIwc1iZQIYwcHEKwETOZDIydBzYErU8dkXsl0N1 UzvNQll2yxz6sciz7Knz2Rmny7UkqhkZpi2fGtp7k5eFx1K/hIlvhupTrt8fQjr+SSl//VjDsM2 dBwA= X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241011170847.334429-16-ardb+git@google.com> Subject: [PATCH v3 6/8] crypto: x86/crc32c - Use idiomatic relative jump table From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: llvm@lists.linux.dev, keescook@chromium.org, linux-hardening@vger.kernel.org, nathan@kernel.org, Ard Biesheuvel , Josh Poimboeuf , Peter Zijlstra , Jan Beulich , "Jose E. Marchesi" , Kees Cook From: Ard Biesheuvel The original crc32c code used a place-relative jump table but with a slightly awkward use of two separate symbols. To help objtool, this was replaced with a bog-standard position dependent jump table call, which was subsequently tweaked to use a RIP-relative reference to the table, but still populate it with absolute 64-bit references. Given that objtool will need to be taught about the jump table idiom that compilers use when running with -fpie enabled, let's update the jump table in the crc32c code once again to use this standard idiom, where the jump table carries 32-bit references relative to the start of the table, and the destination address can be obtained by adding the two. Signed-off-by: Ard Biesheuvel --- arch/x86/crypto/crc32c-pcl-intel-asm_64.S | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/arch/x86/crypto/crc32c-pcl-intel-asm_64.S b/arch/x86/crypto/crc32c-pcl-intel-asm_64.S index bbcff1fb78cb..45b005935194 100644 --- a/arch/x86/crypto/crc32c-pcl-intel-asm_64.S +++ b/arch/x86/crypto/crc32c-pcl-intel-asm_64.S @@ -53,7 +53,7 @@ .endm .macro JMPTBL_ENTRY i -.quad .Lcrc_\i +.long .Lcrc_\i - jump_table .endm .macro JNC_LESS_THAN j @@ -169,7 +169,8 @@ SYM_FUNC_START(crc_pcl) ## branch into array leaq jump_table(%rip), %bufp - mov (%bufp,%rax,8), %bufp + movslq (%bufp,%rax,4), len + addq len, %bufp JMP_NOSPEC bufp ################################################################ From patchwork Fri Oct 11 17:08:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13832746 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A9D501CF7AD for ; Fri, 11 Oct 2024 17:09:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728666563; cv=none; b=T5HyO+5iejFIK6q5Fk0KfA7oKrjYyHL5tnYyvrcxZd4yutgaMxl+CG/0ZG84rMkFKHMZO3iQaK1K+7xkGClTO5AoEZgOIZRecQIujOqA5mQRk3ljZBYdipVOv0oXzy84093S7seuSwOD1pSJajHUcYRsn0N41yZTFlbo3roXA7I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728666563; c=relaxed/simple; bh=2PjQVk510tcbfgZib7AgPxWnnYDh0QAwPChoXMnvxLk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rz/OY2c79yAFBFWIkQm70/I+pHUscuvJzSLQH6ioqYJqSv1s8TSd9WCLe43sUnyOeUcREB8WNfTbtkB98OH6E3RwWbaY9Gy3CrlSsNu9QhqcwqiUv1cU1Kw9QfVt9poq1FP31O2IkivF6OMOgImbZOLo8lyRjRyhwFK5QFm3nQc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=0pU1pj/n; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="0pU1pj/n" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e2904a54a10so3227301276.3 for ; Fri, 11 Oct 2024 10:09:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728666561; x=1729271361; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=EqKDRpMnKUFDCBzPjda6uGGHQMDnCKHOVUyUzH1aSkY=; b=0pU1pj/nOtxR1b9qf2oydny5YZiBFCdQo/bvL3t88g7ESgUDmCE/fsPperThBrJ2zo gG2YBaNw3RhQJvX6ZisFZaTh0Ww2aEWf92dNWu6JraqfmQrzY1V7+izF5f/r7ZaoNv09 m80IuAaZqqmBMZp1kts535w08WCVKnd27Eupw58j7n29ULi2lwzuzIuLGudfC5IKqPt0 MD3N6QW/Kaes6pAWRSry8svpJi4+qXUqkN7kCwOLPHFIwTEaDXpu6Pf4mMeiiysRPHyn COZWh6+3/x5gSijXPABL+JM5ZtmVKtI/icuaD/vujC2UYo0FYpSR8S+pxHjzdpvhl9g1 Q9Hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728666561; x=1729271361; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EqKDRpMnKUFDCBzPjda6uGGHQMDnCKHOVUyUzH1aSkY=; b=ssqhfUvL9UuB3Q8wNfv3VW5ZOCc1PvffZLEZpAVon05+X1aI+NtJknGjhtpeRQNifU 3Y0UXRpmwK9737GVBnEPDyeNzq6uwrE8vShAzGkPePSiyDBvVXqNTb4kha0c65isvmV2 G8GXOWMXkG6rjtW8hC/j+b5/fVlwmhnZ0H0M1HdDxBTAOVtewtRpXZQgy15q8n6yPv5F DxcfVEBPMd7sAZdYC6/NELWT4oAU+IwzQP87pvS3elhlAyPgrjRIHABsJRCEGP6UBRRB 5KkVv+CZDRSTxAfkd5eGdPiOdrhl75YXEWYJio4ZQnM/w4mpAAOFqyhOVZ5hACu/Nywy evXQ== X-Forwarded-Encrypted: i=1; AJvYcCVJFtEXa6Or/CPq7sR7B3Z4MU8nXLDOHa8Qhidcr7WsCZZ6Y2YQ/rOmeqFV0qNjZpvnuHpQMDKAg4Qbt5+dhbA=@vger.kernel.org X-Gm-Message-State: AOJu0YwXXG/J50Rcp9trJROnSVWmJhwPO3yOXvI265g2h3ZqZPyiyj/B 1f2Ms3QVaLn0rFI2GN3VkNR5f1PPH+WczAcPMdBNO11BIjD9EXnf/UGhndyHusDeXc85hg== X-Google-Smtp-Source: AGHT+IGig0d5gAOPvWubCGMNO+HS6pD/uIfah+XgCdryUbEN/NLF/DgvzT5KU18B+Vqf6kzsVXzoL8F5 X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a25:ef0c:0:b0:e1c:ed3d:7bb7 with SMTP id 3f1490d57ef6-e2919c5a54bmr1972276.1.1728666560536; Fri, 11 Oct 2024 10:09:20 -0700 (PDT) Date: Fri, 11 Oct 2024 19:08:55 +0200 In-Reply-To: <20241011170847.334429-10-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241011170847.334429-10-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1122; i=ardb@kernel.org; h=from:subject; bh=O1XJt0IMD45pv2Yzif9KcFzsw4OlwGa7+DE3CnbJzlw=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIZ0zehXXKTcG3hKH5ilqN7VPNj45fmOiRemqqZWcpXs++ IUkec3qKGVhEONgkBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABNhM2JkeLw7Ybp/inLsketP lx1a+fIA5+GjMSmi/rejKnVb7vCfPMXwPyxi5YQ3XxViDx1bJ8Bs1tXxo+r6YkO91rWPWL/sCUt N4wEA X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241011170847.334429-17-ardb+git@google.com> Subject: [PATCH v3 7/8] crypto: x86/crc32c - Add jump table annotation From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: llvm@lists.linux.dev, keescook@chromium.org, linux-hardening@vger.kernel.org, nathan@kernel.org, Ard Biesheuvel , Josh Poimboeuf , Peter Zijlstra , Jan Beulich , "Jose E. Marchesi" , Kees Cook From: Ard Biesheuvel Annotate the indirect jump with a relocation that correlates it with the jump table emitted into .rodata. This helps objtool identify the jump table, allowing it to infer the places in the code that are reachable from the jump. Signed-off-by: Ard Biesheuvel --- arch/x86/crypto/crc32c-pcl-intel-asm_64.S | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/crypto/crc32c-pcl-intel-asm_64.S b/arch/x86/crypto/crc32c-pcl-intel-asm_64.S index 45b005935194..7292090e76dd 100644 --- a/arch/x86/crypto/crc32c-pcl-intel-asm_64.S +++ b/arch/x86/crypto/crc32c-pcl-intel-asm_64.S @@ -171,6 +171,7 @@ SYM_FUNC_START(crc_pcl) leaq jump_table(%rip), %bufp movslq (%bufp,%rax,4), len addq len, %bufp + .reloc ., R_X86_64_NONE, jump_table JMP_NOSPEC bufp ################################################################ @@ -327,6 +328,8 @@ JMPTBL_ENTRY %i i=i+1 .endr +.size jump_table, . - jump_table +.type jump_table, @object ################################################################ ## PCLMULQDQ tables From patchwork Fri Oct 11 17:08:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13832747 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E88DD1CFEB0 for ; Fri, 11 Oct 2024 17:09:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728666565; cv=none; b=S6f1NXYol0QImzC+8WEaRdxGG4yIA6XBReFiNh8e9tnIHeix+jeHAf5sh3GsDG4KNo0C9aVg0lVOH4TJQNRNy03dcfAtKR/I306wzF3wj+ejEBFk6E9MZpH+3CUq0nSqiPGVG7b/yARBh3E2X965+e2FSPWHFOCSilxE4+OB3Ik= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728666565; c=relaxed/simple; bh=dmzPGrFW6aOYMzz1IH4ndQp0FtWFFlmkgAnMYm8LZsM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XB7zLOmeuudW287hKJ3aSm/fM/nd0AFW+2HN7O1fZ78WwGd+OHrUXjvAkHuJ/3Q4+5PoiPZY3i4JPCVTlBKxTV39kLxvm2n6B29CHW2rKishl3sRNZThm5+Gqxh/2MPPrTpX/7d++FNR2W/Bq74Ks2biENcRdE/8azPS26n2L1Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=GI35fbnM; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GI35fbnM" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e22f8dc491so42603657b3.1 for ; Fri, 11 Oct 2024 10:09:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728666563; x=1729271363; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=e8u1CNDm2+Bhw3m92z5B6moO+Nki7WTk10wOgvZqSdE=; b=GI35fbnMCkySVNH2TvfqLhyFIeEiYu2bCrc6zquv+r7LOeNciqEOaqWoaI9gR4BtnP sxvyx2trxM3rx954sxtyswuACZ2ICjFVNgxiQuhA4npiAFb4PFrOMaZ9oUvzZoq2dp71 1e8CYOvGj6lCFoqx220whbnLJJUXOfOBjMjGmoF3mNpRI/sSS9OpGdxAvDnan1DEwTiK EIwQkTxZgh8trrZ7Kr0ooT4pN/MH4ADkaoF4e7ANmNuV0gRWb75HVA6nfdO/xzPNiCIF O36CdW0iM1H1TTNrYTc8CA91+GLo9ZKvhf6Mjc0111laX5Bv44GO7Jn1r5z6qCqdar7H ML3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728666563; x=1729271363; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=e8u1CNDm2+Bhw3m92z5B6moO+Nki7WTk10wOgvZqSdE=; b=AY+HlEU6HMzcmvoy5nEqfgOtOCeFbmKbMIsOkM4BrMImSvRYJh8LGTZhGUHZvjQeXF zEoib5q85a7buN8TriVIWJFwAqy735ubhsIZse/3cccjovBmvHryYdRhPd5DBIVN4bQv eh4BuMgOH08eWE58cPOqDpllUFiE0Pu+CXPJ5lITRPFnM3BdH2rH63YQ17HYTzxU1SP3 H2aG9d+TzQ/RRs0tLlc2D+/8v9pYT9ljeSXKeMaAFNr1TQJTI74Gc+0SRjVKlbBPQJfz CK1zjkg59ghKWGav2ypA3pIA1Jqgd92ytKgc7p/NjRyKNkycDsUuuRgjkLztCDtltxhC MFBA== X-Forwarded-Encrypted: i=1; AJvYcCXYf29vIhGR8oA5NoxjbqHp2vd30WETD7D19j2+qQOssVxxWy8GsghEbv7yfuCseoZZaXeAzIaLJofY89hNtKQ=@vger.kernel.org X-Gm-Message-State: AOJu0YwNWCbfgbSFwQtcNGfhZ3D7/tUMW5b3JfIFCvGB9hDcjNhHns1w qB5oXeZgHK3wBfDgn8XmNaHTOIh/LYNZ0aKjlTvpalgu9nOD3IRckm0M2trT9FB+5s6OzQ== X-Google-Smtp-Source: AGHT+IELPWPtiT4Q82wAv9kxv2a+A02raZYpuKW6YkhpqdM8qkrH2v2YSZ25ymK9rG/ipEu3ysaoSds0 X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:690c:3190:b0:6e2:120b:be57 with SMTP id 00721157ae682-6e347b4aec8mr83757b3.5.1728666562988; Fri, 11 Oct 2024 10:09:22 -0700 (PDT) Date: Fri, 11 Oct 2024 19:08:56 +0200 In-Reply-To: <20241011170847.334429-10-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241011170847.334429-10-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=3351; i=ardb@kernel.org; h=from:subject; bh=Hfh910Y5sSnn5hBkeQ8p28eswlQr4LHqPbKbxMAVhc0=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIZ0zenW+ZiGDhuwr6Rk3jm4WtI45MFlvU1xb1zP3k1/yF Rq5VdI7SlkYxDgYZMUUWQRm/3238/REqVrnWbIwc1iZQIYwcHEKwEQW9zMyrPey+hV86aRT/v8I 8defMvfutpU/FbW3Yf+0ugM2qzYLZDH8D5urnSY/RSlbw4UtxHhzheGW9zE9B0xVErOyXG3KbtV xAAA= X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241011170847.334429-18-ardb+git@google.com> Subject: [PATCH v3 8/8] crypto: x86/crc32c-intel - Tweaks to make objtool's life harder From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: llvm@lists.linux.dev, keescook@chromium.org, linux-hardening@vger.kernel.org, nathan@kernel.org, Ard Biesheuvel , Josh Poimboeuf , Peter Zijlstra , Jan Beulich , "Jose E. Marchesi" , Kees Cook From: Ard Biesheuvel Tweak the asm crc32c asm code so that the jump table is more difficult to decipher for objtool: - load the address in the prologue - move the jump table to the middle of .rodata, so that the section offset and the symbol offset differ - emit an entry following the jump table that is unrelated - add unconditional ENDBRs so we can test --ibt regardless of the kernel config Test code only. Not intended for merging. --- arch/x86/crypto/crc32c-pcl-intel-asm_64.S | 50 +++++++++++--------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/arch/x86/crypto/crc32c-pcl-intel-asm_64.S b/arch/x86/crypto/crc32c-pcl-intel-asm_64.S index 7292090e76dd..cbedf5820e30 100644 --- a/arch/x86/crypto/crc32c-pcl-intel-asm_64.S +++ b/arch/x86/crypto/crc32c-pcl-intel-asm_64.S @@ -93,10 +93,14 @@ SYM_FUNC_START(crc_pcl) #define crc1 %r9 #define crc2 %r10 + endbr64 + pushq %rbp pushq %rbx pushq %rdi pushq %rsi + leaq jump_table(%rip), %rbp + ## Move crc_init for Linux to a different mov crc_init_arg, crc_init @@ -168,9 +172,8 @@ SYM_FUNC_START(crc_pcl) xor crc2, crc2 ## branch into array - leaq jump_table(%rip), %bufp - movslq (%bufp,%rax,4), len - addq len, %bufp + movslq (%rbp,%rax,4), %bufp + addq %rbp, %bufp .reloc ., R_X86_64_NONE, jump_table JMP_NOSPEC bufp @@ -197,7 +200,7 @@ SYM_FUNC_START(crc_pcl) .altmacro LABEL crc_ %i .noaltmacro - ENDBR + endbr64 crc32q -i*8(block_0), crc_init crc32q -i*8(block_1), crc1 crc32q -i*8(block_2), crc2 @@ -207,7 +210,7 @@ LABEL crc_ %i .altmacro LABEL crc_ %i .noaltmacro - ENDBR + endbr64 crc32q -i*8(block_0), crc_init crc32q -i*8(block_1), crc1 # SKIP crc32 -i*8(block_2), crc2 ; Don't do this one yet @@ -241,7 +244,7 @@ LABEL crc_ %i ################################################################ LABEL crc_ 0 - ENDBR + endbr64 mov tmp, len cmp $128*24, tmp jae .Lfull_block @@ -311,26 +314,11 @@ LABEL less_than_ %j # less_than_j: Length should be in popq %rsi popq %rdi popq %rbx + popq %rbp RET SYM_FUNC_END(crc_pcl) .section .rodata, "a", @progbits - ################################################################ - ## jump table Table is 129 entries x 2 bytes each - ################################################################ -.align 4 -jump_table: - i=0 -.rept 129 -.altmacro -JMPTBL_ENTRY %i -.noaltmacro - i=i+1 -.endr - -.size jump_table, . - jump_table -.type jump_table, @object - ################################################################ ## PCLMULQDQ tables ## Table is 128 entries x 2 words (8 bytes) each @@ -465,3 +453,21 @@ K_table: .long 0x45cddf4e, 0xe0ac139e .long 0xacfa3103, 0x6c23e841 .long 0xa51b6135, 0x170076fa + + ################################################################ + ## jump table Table is 129 entries x 2 bytes each + ################################################################ +.align 4 +jump_table: + i=0 +.rept 129 +.altmacro +JMPTBL_ENTRY %i +.noaltmacro + i=i+1 +.endr + +.size jump_table, . - jump_table +.type jump_table, @object + + .long crc_pcl - .