Re: [PATCH 00/21] objtool: vmlinux.o and CLANG LTO support

From: Sedat Dilek
Date: Fri Jan 15 2021 - 15:20:13 EST


On Fri, Jan 15, 2021 at 8:52 PM Josh Poimboeuf <jpoimboe@xxxxxxxxxx> wrote:
>
> On Thu, Jan 14, 2021 at 04:41:28PM -0800, Sami Tolvanen wrote:
> > Hi Josh,
> >
> > On Thu, Jan 14, 2021 at 11:40 AM Josh Poimboeuf <jpoimboe@xxxxxxxxxx> wrote:
> > >
> > > Add support for proper vmlinux.o validation, which will be needed for
> > > Sami's upcoming x86 LTO set. (And vmlinux validation is the future for
> > > objtool anyway, for other reasons.)
> > >
> > > This isn't 100% done -- most notably, crypto still needs to be supported
> > > -- but I think this gets us most of the way there.
> > >
> > > This can also be found at
> > >
> > > git://git.kernel.org/pub/scm/linux/kernel/git/jpoimboe/linux.git objtool-vmlinux
> > >
> > > And for more testing it can be combined with Sami's x86 LTO patches:
> > >
> > > https://github.com/samitolvanen/linux clang-lto
> >
> > Thank you for sending these! I applied this series on top of the
> > clang-lto tree and built allyesconfig with LTO_CLANG enabled and the
> > following crypto options disabled:
> >
> > CRYPTO_AES_NI_INTEL
> > CRYPTO_CAMELLIA_AESNI_AVX2_X86_64
> > CRYPTO_SHA1_SSSE3
> > CRYPTO_SHA256_SSSE3
> > CRYPTO_SHA512_SSSE3
> > CRYPTO_CRC32C_INTEL
> >
> > I can confirm that all the warnings I previously saw are now fixed,
> > but I'm seeing a few new ones:
> >
> > vmlinux.o: warning: objtool: balance_leaf_when_delete()+0x17d4: stack
> > state mismatch: cfa1=7+192 cfa2=7+176
> > vmlinux.o: warning: objtool: internal_move_pointers_items()+0x9f7:
> > stack state mismatch: cfa1=7+160 cfa2=7+176
> > vmlinux.o: warning: objtool: strncpy_from_user()+0x181: call to
> > do_strncpy_from_user() with UACCESS enabled
> > vmlinux.o: warning: objtool: strnlen_user()+0x12b: call to
> > do_strnlen_user() with UACCESS enabled
> > vmlinux.o: warning: objtool: i915_gem_execbuffer2_ioctl()+0x390: call
> > to __ubsan_handle_negate_overflow() with UACCESS enabled
> > vmlinux.o: warning: objtool: .text.snd_trident_free_voice: unexpected
> > end of section
> >
> > I haven't had a chance to take a closer look yet, but some of these
> > are probably related to
> > https://github.com/ClangBuiltLinux/linux/issues/1192. However, I can
> > reproduce these also with ToT Clang, not just with Clang 11.
>
> Thanks, I'm able to reproduce these. Will take a look.
>

AFAICS, that misses the v2 diff (see attachment) you sent me when
dealing with objtool-vmlinux and clang-cfi.
It does not cleanly apply with the combination of your objtool-vmlinux
Git and clang-cfi Git.


- Sedat -
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 3d6cca126178..c8b2d26f4957 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -838,37 +838,6 @@ static int add_ignore_alternatives(struct objtool_file *file)
return 0;
}

-/*
- * CONFIG_CFI_CLANG: Check if the section is a CFI jump table or a
- * compiler-generated CFI handler.
- */
-static bool is_cfi_section(struct section *sec)
-{
- return (sec->name &&
- (!strncmp(sec->name, ".text..L.cfi.jumptable", 22) ||
- !strcmp(sec->name, ".text.__cfi_check")));
-}
-
-/*
- * CONFIG_CFI_CLANG: Ignore CFI jump tables.
- */
-static void add_cfi_jumptables(struct objtool_file *file)
-{
- struct section *sec;
- struct symbol *func;
- struct instruction *insn;
-
- for_each_sec(file, sec) {
- if (!is_cfi_section(sec))
- continue;
-
- list_for_each_entry(func, &sec->symbol_list, list) {
- sym_for_each_insn(file, func, insn)
- insn->ignore = true;
- }
- }
-}
-
/*
* Find the destination instructions for all jumps.
*/
@@ -939,9 +908,6 @@ static int add_jump_destinations(struct objtool_file *file)
if (!strcmp(insn->sec->name, ".altinstr_replacement"))
continue;

- if (is_cfi_section(insn->sec))
- continue;
-
WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
insn->sec, insn->offset, dest_sec->name,
dest_off);
@@ -1049,9 +1015,6 @@ static int add_call_destinations(struct objtool_file *file)
insn->call_dest = find_call_destination(reloc->sym->sec,
dest_off);
if (!insn->call_dest) {
- if (is_cfi_section(reloc->sym->sec))
- continue;
-
WARN_FUNC("can't find call dest symbol at %s+0x%lx",
insn->sec, insn->offset,
reloc->sym->sec->name,
@@ -1791,7 +1754,6 @@ static int decode_sections(struct objtool_file *file)

add_ignores(file);
add_uaccess_safe(file);
- add_cfi_jumptables(file);

ret = add_ignore_alternatives(file);
if (ret)
@@ -2654,8 +2616,7 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,
if (dead_end_function(file, insn->call_dest))
return 0;

- if (insn->type == INSN_CALL && insn->call_dest &&
- insn->call_dest->static_call_tramp) {
+ if (insn->type == INSN_CALL && insn->call_dest->static_call_tramp) {
list_add_tail(&insn->static_call_node,
&file->static_call_list);
}
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index ccee8fc331f0..b3b307bf7ec9 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -350,6 +350,11 @@ static int read_sections(struct elf *elf)
}
sec->len = sec->sh.sh_size;

+ /* Detect -fsanitize=cfi related sections */
+ if (!strcmp(sec->name, ".text.__cfi_check") ||
+ !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));
@@ -606,6 +611,33 @@ static int read_relocs(struct elf *elf)
return -1;
}

+ if (reloc->sym->sec->cfi_jt) {
+ struct symbol *sym = reloc->sym;
+ char *suffix;
+ char name[MAX_NAME_LEN + 1];
+ size_t name_len;
+ struct symbol *new_sym;
+
+ if (sym->type == STT_SECTION)
+ sym = find_func_by_offset(sym->sec,
+ reloc->addend);
+ if (sym) {
+ suffix = strstr(sym->name, ".cfi_jt");
+ if (suffix) {
+ name_len = suffix - sym->name;
+ strncpy(name, sym->name, name_len);
+ name[name_len] = '\0';
+ new_sym = find_symbol_by_name(elf, name);
+ if (!new_sym) {
+ WARN("wtf");
+ return -1;
+ }
+
+ reloc->sym = new_sym;
+ }
+ }
+ }
+
elf_add_reloc(elf, reloc);
nr_reloc++;
}
diff --git a/tools/objtool/elf.h b/tools/objtool/elf.h
index e6890cc70a25..bcc524d73f51 100644
--- a/tools/objtool/elf.h
+++ b/tools/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.30.0