All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sami Tolvanen <samitolvanen@google.com>
To: x86@kernel.org
Cc: Kees Cook <keescook@chromium.org>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Nathan Chancellor <nathan@kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Sedat Dilek <sedat.dilek@gmail.com>,
	linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org,
	clang-built-linux@googlegroups.com,
	Sami Tolvanen <samitolvanen@google.com>
Subject: [PATCH 02/15] objtool: Add CONFIG_CFI_CLANG support
Date: Fri, 16 Apr 2021 13:38:31 -0700	[thread overview]
Message-ID: <20210416203844.3803177-3-samitolvanen@google.com> (raw)
In-Reply-To: <20210416203844.3803177-1-samitolvanen@google.com>

With CONFIG_CFI_CLANG, the compiler replaces function references with
references to the CFI jump table, which confuses objtool. This change,
based on Josh's initial patch [1], goes through the list of relocations
and replaces jump table symbols with the actual function symbols.

[1] https://lore.kernel.org/r/d743f4b36e120c06506567a9f87a062ae03da47f.1611263462.git.jpoimboe@redhat.com/

Reported-by: Sedat Dilek <sedat.dilek@gmail.com>
Suggested-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
---
 tools/objtool/elf.c                 | 48 +++++++++++++++++++++++++++++
 tools/objtool/include/objtool/elf.h |  2 +-
 2 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index d08f5f3670f8..5cf2c61ce7b8 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -273,6 +273,10 @@ static int read_sections(struct elf *elf)
 		}
 		sec->len = sec->sh.sh_size;
 
+		/* Detect -fsanitize=cfi jump table sections */
+		if (!strncmp(sec->name, ".text..L.cfi.jumptable", 22))
+			sec->cfi_jt = true;
+
 		list_add_tail(&sec->list, &elf->sections);
 		elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
 		elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
@@ -548,6 +552,48 @@ static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc, unsi
 	return 0;
 }
 
+static int fix_cfi_relocs(const struct elf *elf)
+{
+	struct section *sec, *tmpsec;
+	struct reloc *reloc, *tmpreloc;
+
+	list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
+		list_for_each_entry_safe(reloc, tmpreloc, &sec->reloc_list, list) {
+			struct symbol *sym;
+			struct reloc *func_reloc;
+
+			/*
+			 * CONFIG_CFI_CLANG replaces function relocations to refer
+			 * to an intermediate jump table. Undo the conversion so
+			 * objtool can make sense of things.
+			 */
+			if (!reloc->sym->sec->cfi_jt)
+				continue;
+
+			if (reloc->sym->type == STT_SECTION)
+				sym = find_func_by_offset(reloc->sym->sec,
+							  reloc->addend);
+			else
+				sym = reloc->sym;
+
+			if (!sym || !sym->sec)
+				continue;
+
+			/*
+			 * The jump table immediately jumps to the actual function,
+			 * so look up the relocation there.
+			 */
+			func_reloc = find_reloc_by_dest_range(elf, sym->sec, sym->offset, 5);
+			if (!func_reloc || !func_reloc->sym)
+				continue;
+
+			reloc->sym = func_reloc->sym;
+		}
+	}
+
+	return 0;
+}
+
 static int read_relocs(struct elf *elf)
 {
 	struct section *sec;
@@ -608,6 +654,8 @@ static int read_relocs(struct elf *elf)
 		tot_reloc += nr_reloc;
 	}
 
+	fix_cfi_relocs(elf);
+
 	if (stats) {
 		printf("max_reloc: %lu\n", max_reloc);
 		printf("tot_reloc: %lu\n", tot_reloc);
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index 45e5ede363b0..ef19578fc5e4 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -39,7 +39,7 @@ struct section {
 	char *name;
 	int idx;
 	unsigned int len;
-	bool changed, text, rodata, noinstr;
+	bool changed, text, rodata, noinstr, cfi_jt;
 };
 
 struct symbol {
-- 
2.31.1.368.gbe11c130af-goog


  parent reply	other threads:[~2021-04-16 20:38 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-16 20:38 [PATCH 00/15] x86: Add support for Clang CFI Sami Tolvanen
2021-04-16 20:38 ` [PATCH 01/15] objtool: Find a destination for jumps beyond the section end Sami Tolvanen
2021-04-20 18:14   ` Josh Poimboeuf
2021-04-20 20:25     ` Sami Tolvanen
2021-04-20 22:55       ` Josh Poimboeuf
2021-04-20 22:58         ` Nick Desaulniers
2021-04-16 20:38 ` Sami Tolvanen [this message]
2021-04-20 19:47   ` [PATCH 02/15] objtool: Add CONFIG_CFI_CLANG support Josh Poimboeuf
2021-04-20 20:45     ` Sami Tolvanen
2021-04-16 20:38 ` [PATCH 03/15] objtool: Add ASM_STACK_FRAME_NON_STANDARD Sami Tolvanen
2021-04-16 20:38 ` [PATCH 04/15] static_call: Use global functions for the self-test Sami Tolvanen
2021-04-16 21:37   ` Thomas Gleixner
2021-04-17  0:16     ` Thomas Gleixner
2021-04-16 20:38 ` [PATCH 05/15] x86: Implement function_nocfi Sami Tolvanen
2021-04-16 21:18   ` Borislav Petkov
2021-04-16 21:49     ` Sami Tolvanen
2021-04-16 22:02       ` Borislav Petkov
2021-04-16 22:06         ` Andy Lutomirski
2021-04-16 22:14           ` Borislav Petkov
2021-04-16 22:20             ` Andy Lutomirski
2021-04-16 22:37               ` Kees Cook
2021-04-16 23:02                 ` Thomas Gleixner
2021-04-17 10:16                   ` Thomas Gleixner
2021-04-19 15:13                     ` Sami Tolvanen
2021-04-16 22:28           ` Kees Cook
2021-04-16 22:52             ` Andy Lutomirski
2021-04-16 22:58               ` Kees Cook
2021-04-16 23:40               ` Kees Cook
2021-04-17 23:19                 ` Andy Lutomirski
2021-04-17 23:53                   ` Thomas Gleixner
2021-04-18  0:11                     ` Andy Lutomirski
2021-04-18 16:17                       ` Thomas Gleixner
2021-04-18 22:57                         ` Andy Lutomirski
2021-04-19 15:20                           ` Sami Tolvanen
2021-04-19 15:26                       ` David Laight
2021-04-19 17:46                         ` Andy Lutomirski
2021-04-17 14:20             ` David Laight
2021-04-17 15:48               ` Andy Lutomirski
2021-04-19  8:40             ` Rasmus Villemoes
2021-04-19 16:45               ` Joao Moreira
2021-04-19 21:52               ` David Laight
2021-04-16 22:16         ` Kees Cook
2021-04-16 22:13       ` Thomas Gleixner
2021-04-16 20:38 ` [PATCH 06/15] x86: Avoid CFI jump tables in IDT and entry points Sami Tolvanen
2021-04-16 22:26   ` Thomas Gleixner
2021-04-16 23:56     ` Kees Cook
2021-04-17  0:02       ` Thomas Gleixner
2021-04-16 20:38 ` [PATCH 07/15] x86/ftrace: Use function_nocfi in MCOUNT_ADDR Sami Tolvanen
2021-04-16 20:38 ` [PATCH 08/15] x86/extable: Do not mark exception callback as CFI Sami Tolvanen
2021-04-16 20:38 ` [PATCH 09/15] x86/alternatives: Use C int3 selftest but disable KASAN Sami Tolvanen
2021-04-17 11:37   ` Peter Zijlstra
2021-04-19 15:26     ` Sami Tolvanen
2021-04-20  7:19       ` Peter Zijlstra
2021-04-16 20:38 ` [PATCH 10/15] x86/purgatory: Disable CFI Sami Tolvanen
2021-04-16 20:38 ` [PATCH 11/15] x86, relocs: Ignore __typeid__ relocations Sami Tolvanen
2021-04-16 20:38 ` [PATCH 12/15] x86, module: " Sami Tolvanen
2021-04-16 20:38 ` [PATCH 13/15] x86, cpu: Use LTO for cpu.c with CFI Sami Tolvanen
2021-04-16 20:38 ` [PATCH 14/15] x86, kprobes: Fix optprobe_template_func type mismatch Sami Tolvanen
2021-04-16 20:38 ` [PATCH 15/15] x86, build: Allow CONFIG_CFI_CLANG to be selected Sami Tolvanen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210416203844.3803177-3-samitolvanen@google.com \
    --to=samitolvanen@google.com \
    --cc=clang-built-linux@googlegroups.com \
    --cc=jpoimboe@redhat.com \
    --cc=keescook@chromium.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=peterz@infradead.org \
    --cc=sedat.dilek@gmail.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.