Skip to content

Commit 9a479f7

Browse files
author
Peter Zijlstra
committed
objtool: Add --cfi to generate the .cfi_sites section
Add the location of all __cfi_##name symbols (as generated by kCFI) to a section such that we might re-write things at kernel boot. Notably; boot time re-hashing and FineIBT are the intended use of this. Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent b341b20 commit 9a479f7

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

tools/objtool/builtin-check.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const struct option check_options[] = {
8080
OPT_BOOLEAN('s', "stackval", &opts.stackval, "validate frame pointer rules"),
8181
OPT_BOOLEAN('t', "static-call", &opts.static_call, "annotate static calls"),
8282
OPT_BOOLEAN('u', "uaccess", &opts.uaccess, "validate uaccess rules for SMAP"),
83+
OPT_BOOLEAN(0 , "cfi", &opts.cfi, "annotate kernel control flow integrity (kCFI) function preambles"),
8384
OPT_CALLBACK_OPTARG(0, "dump", NULL, NULL, "orc", "dump metadata", parse_dump),
8485

8586
OPT_GROUP("Options:"),

tools/objtool/check.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,68 @@ static int create_ibt_endbr_seal_sections(struct objtool_file *file)
861861
return 0;
862862
}
863863

864+
static int create_cfi_sections(struct objtool_file *file)
865+
{
866+
struct section *sec, *s;
867+
struct symbol *sym;
868+
unsigned int *loc;
869+
int idx;
870+
871+
sec = find_section_by_name(file->elf, ".cfi_sites");
872+
if (sec) {
873+
INIT_LIST_HEAD(&file->call_list);
874+
WARN("file already has .cfi_sites section, skipping");
875+
return 0;
876+
}
877+
878+
idx = 0;
879+
for_each_sec(file, s) {
880+
if (!s->text)
881+
continue;
882+
883+
list_for_each_entry(sym, &s->symbol_list, list) {
884+
if (sym->type != STT_FUNC)
885+
continue;
886+
887+
if (strncmp(sym->name, "__cfi_", 6))
888+
continue;
889+
890+
idx++;
891+
}
892+
}
893+
894+
sec = elf_create_section(file->elf, ".cfi_sites", 0, sizeof(unsigned int), idx);
895+
if (!sec)
896+
return -1;
897+
898+
idx = 0;
899+
for_each_sec(file, s) {
900+
if (!s->text)
901+
continue;
902+
903+
list_for_each_entry(sym, &s->symbol_list, list) {
904+
if (sym->type != STT_FUNC)
905+
continue;
906+
907+
if (strncmp(sym->name, "__cfi_", 6))
908+
continue;
909+
910+
loc = (unsigned int *)sec->data->d_buf + idx;
911+
memset(loc, 0, sizeof(unsigned int));
912+
913+
if (elf_add_reloc_to_insn(file->elf, sec,
914+
idx * sizeof(unsigned int),
915+
R_X86_64_PC32,
916+
s, sym->offset))
917+
return -1;
918+
919+
idx++;
920+
}
921+
}
922+
923+
return 0;
924+
}
925+
864926
static int create_mcount_loc_sections(struct objtool_file *file)
865927
{
866928
struct section *sec;
@@ -4430,6 +4492,13 @@ int check(struct objtool_file *file)
44304492
warnings += ret;
44314493
}
44324494

4495+
if (opts.cfi) {
4496+
ret = create_cfi_sections(file);
4497+
if (ret < 0)
4498+
goto out;
4499+
warnings += ret;
4500+
}
4501+
44334502
if (opts.rethunk) {
44344503
ret = create_return_sites_sections(file);
44354504
if (ret < 0)

tools/objtool/include/objtool/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct opts {
2727
bool static_call;
2828
bool uaccess;
2929
int prefix;
30+
bool cfi;
3031

3132
/* options: */
3233
bool backtrace;

0 commit comments

Comments
 (0)