// SPDX-License-Identifier: GPL-2.0-or-later/* * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com> */ #include <string.h>#include <stdlib.h>#include <inttypes.h>#include <sys/mman.h> #include <arch/elf.h>#include <objtool/builtin.h>#include <objtool/cfi.h>#include <objtool/arch.h>#include <objtool/check.h>#include <objtool/special.h>#include <objtool/warn.h>#include <objtool/endianness.h> #include <linux/objtool.h>#include <linux/hashtable.h>#include <linux/kernel.h>#include <linux/static_call_types.h> struct alternative { struct list_head list; struct instruction *insn; bool skip_orig;}; static unsigned long nr_cfi, nr_cfi_reused, nr_cfi_cache; static struct cfi_init_state initial_func_cfi;static struct cfi_state init_cfi;static struct cfi_state func_cfi; struct instruction *find_insn(struct objtool_file *file, struct section *sec, unsigned long offset){ struct instruction *insn; hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) { if (insn->sec == sec && insn->offset == offset) return insn; } return NULL;} static struct instruction *next_insn_same_sec(struct objtool_file *file, struct instruction *insn){ struct instruction *next = list_next_entry(insn, list); if (!next || &next->list == &file->insn_list || next->sec != insn->sec) return NULL; return next;} static struct instruction *next_insn_same_func(struct objtool_file *file, struct instruction *insn){ struct instruction *next = list_next_entry(insn, list); struct symbol *func = insn_func(insn); if (!func) return NULL; if (&next->list != &file->insn_list && insn_func(next) == func) return next; /* Check if we're already in the subfunction: */ if (func == func->cfunc) return NULL; /* Move to the subfunction: */ return find_insn(file, func->cfunc->sec, func->cfunc->offset);} static struct instruction *prev_insn_same_sym(struct objtool_file *file, struct instruction *insn){ struct instruction *prev = list_prev_entry(insn, list); if (&prev->list != &file->insn_list && insn_func(prev) == insn_func(insn)) return prev; return NULL;} #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->list != &file->insn_list && \ insn->sec == sym->sec && \ insn->offset < sym->offset + sym->len; \ insn = list_next_entry(insn, list)) #define sym_for_each_insn_continue_reverse(file, sym, insn) \ for (insn = list_prev_entry(insn, list); \ &insn->list != &file->insn_list && \ insn->sec == sym->sec && insn->offset >= sym->offset; \ insn = list_prev_entry(insn, list)) #define sec_for_each_insn_from(file, insn) \ for (; insn; insn = next_insn_same_sec(file, insn)) #define sec_for_each_insn_continue(file, insn) \ for (insn = next_insn_same_sec(file, insn); insn; \ insn = next_insn_same_sec(file, insn)) static bool is_jump_table_jump(struct instruction *insn){ struct alt_group *alt_group = insn->alt_group; if (insn->jump_table) return true; /* Retpoline alternative for a jump table? */ return alt_group && alt_group->orig_group && alt_group->orig_group->first_insn->jump_table;} static bool is_sibling_call(struct instruction *insn){ /* * Assume only STT_FUNC calls have jump-tables. */ if (insn_func(insn)) { /* An indirect jump is either a sibling call or a jump to a table. */ if (insn->type == INSN_JUMP_DYNAMIC) return !is_jump_table_jump(insn); } /* add_jump_destinations() sets insn->call_dest for sibling calls. */ return (is_static_jump(insn) && insn->call_dest);} /* * This checks to see if the given function is a "noreturn" function. * * For global functions which are outside the scope of this object file, we * have to keep a manual list of them. * * For local functions, we have to detect them manually by simply looking for * the lack of a return instruction. */static bool __dead_end_function(struct objtool_file *file, struct symbol *func, int recursion){ int i; struct instruction *insn; bool empty = true; /* * Unfortunately these have to be hard coded because the noreturn * attribute isn't provided in ELF data. Keep 'em sorted. */ static const char * const global_noreturns[] = { "__invalid_creds", "__module_put_and_kthread_exit", "__reiserfs_panic", "__stack_chk_fail", "__ubsan_handle_builtin_unreachable", "cpu_bringup_and_idle", "cpu_startup_entry", "do_exit", "do_group_exit", "do_task_dead", "ex_handler_msr_mce", "fortify_panic", "kthread_complete_and_exit", "kthread_exit", "kunit_try_catch_throw", "lbug_with_loc", "machine_real_restart", "make_task_dead", "panic", "rewind_stack_and_make_dead", "sev_es_terminate", "snp_abort", "stop_this_cpu", "usercopy_abort", "xen_start_kernel", }; if (!func) return false; if (func->bind == STB_WEAK) return false; if (func->bind == STB_GLOBAL) for (i = 0; i < ARRAY_SIZE(global_noreturns); i++) if (!strcmp(func->name, global_noreturns[i])) return true; if (!func->len) return false; insn = find_insn(file, func->sec, func->offset); if (!insn || !insn_func(insn)) return false; func_for_each_insn(file, func, insn) { empty = false; if (insn->type == INSN_RETURN) return false; } if (empty) return false; /* * A function can have a sibling call instead of a return. In that * case, the function's dead-end status depends on whether the target * of the sibling call returns. */ func_for_each_insn(file, func, insn) { if (is_sibling_call(insn)) { struct instruction *dest = insn->jump_dest; if (!dest) /* sibling call to another file */ return false; /* local sibling call */ if (recursion == 5) { /* * Infinite recursion: two functions have * sibling calls to each other. This is a very * rare case. It means they aren't dead ends. */ return false; } return __dead_end_function(file, insn_func(dest), recursion+1); } } return true;} static bool dead_end_function(struct objtool_file *file, struct symbol *func){ return __dead_end_function(file, func, 0);} static void init_cfi_state(struct cfi_state *cfi){ int i; for (i = 0; i < CFI_NUM_REGS; i++) { cfi->regs[i].base = CFI_UNDEFINED; cfi->vals[i].base = CFI_UNDEFINED; } cfi->cfa.base = CFI_UNDEFINED; cfi->drap_reg = CFI_UNDEFINED; cfi->drap_offset = -1;} static void init_insn_state(struct objtool_file *file, struct insn_state *state, struct section *sec){ memset(state, 0, sizeof(*state)); init_cfi_state(&state->cfi); /* * We need the full vmlinux for noinstr validation, otherwise we can * not correctly determine insn->call_dest->sec (external symbols do * not have a section). */ if (opts.link && opts.noinstr && sec) state->noinstr = sec->noinstr;} static struct cfi_state *cfi_alloc(void){ struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1); if (!cfi) { WARN("calloc failed"); exit(1); } nr_cfi++; return cfi;} static int cfi_bits;static struct hlist_head *cfi_hash; static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2){ return memcmp((void *)cfi1 + sizeof(cfi1->hash), (void *)cfi2 + sizeof(cfi2->hash), sizeof(struct cfi_state) - sizeof(struct hlist_node));} static inline u32 cfi_key(struct cfi_state *cfi){ return jhash((void *)cfi + sizeof(cfi->hash), sizeof(*cfi) - sizeof(cfi->hash), 0);} static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi){ struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)]; struct cfi_state *obj; hlist_for_each_entry(obj, head, hash) { if (!cficmp(cfi, obj)) { nr_cfi_cache++; return obj; } } obj = cfi_alloc(); *obj = *cfi; hlist_add_head(&obj->hash, head); return obj;} static void cfi_hash_add(struct cfi_state *cfi){ struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)]; hlist_add_head(&cfi->hash, head);} static void *cfi_hash_alloc(unsigned long size){ cfi_bits = max(10, ilog2(size)); cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); if (cfi_hash == (void *)-1L) { WARN("mmap fail cfi_hash"); cfi_hash = NULL; } else if (opts.stats) { printf("cfi_bits: %d\n", cfi_bits); } return cfi_hash;} static unsigned long nr_insns;static unsigned long nr_insns_visited; /* * Call the arch-specific instruction decoder for all the instructions and add * them to the global instruction list. */static int decode_instructions(struct objtool_file *file){ struct section *sec; struct symbol *func; unsigned long offset; struct instruction *insn; int ret; for_each_sec(file, sec) { if (!(sec->sh.sh_flags & SHF_EXECINSTR)) continue; if (strcmp(sec->name, ".altinstr_replacement") && strcmp(sec->name, ".altinstr_aux") && strncmp(sec->name, ".discard.", 9)) sec->text = true; if (!strcmp(sec->name, ".noinstr.text") || !strcmp(sec->name, ".entry.text") || !strncmp(sec->name, ".text.__x86.", 12)) sec->noinstr = true; /* * .init.text code is ran before userspace and thus doesn't * strictly need retpolines, except for modules which are * loaded late, they very much do need retpoline in their * .init.text */ if (!strcmp(sec->name, ".init.text") && !opts.module) sec->init = true; for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) { insn = malloc(sizeof(*insn)); if (!insn) { WARN("malloc failed"); return -1; } memset(insn, 0, sizeof(*insn)); INIT_LIST_HEAD(&insn->alts); INIT_LIST_HEAD(&insn->stack_ops); INIT_LIST_HEAD(&insn->call_node); insn->sec = sec; insn->offset = offset; ret = arch_decode_instruction(file, sec, offset, sec->sh.sh_size - offset, &insn->len, &insn->type, &insn->immediate, &insn->stack_ops); if (ret) goto err; /* * By default, "ud2" is a dead end unless otherwise * annotated, because GCC 7 inserts it for certain * divide-by-zero cases. */ if (insn->type == INSN_BUG) insn->dead_end = true; hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset)); list_add_tail(&insn->list, &file->insn_list); nr_insns++; } list_for_each_entry(func, &sec->symbol_list, list) { if (func->type != STT_NOTYPE && func->type != STT_FUNC) continue; if (func->offset == sec->sh.sh_size) { /* Heuristic: likely an "end" symbol */ if (func->type == STT_NOTYPE) continue; WARN("%s(): STT_FUNC at end of section", func->name); return -1; } if (func->return_thunk || func->alias != func) continue; if (!find_insn(file, sec, func->offset)) { WARN("%s(): can't find starting instruction", func->name); return -1; } sym_for_each_insn(file, func, insn) { insn->sym = func; if (func->type == STT_FUNC && insn->type == INSN_ENDBR && list_empty(&insn->call_node)) { if (insn->offset == func->offset) { list_add_tail(&insn->call_node, &file->endbr_list); file->nr_endbr++; } else { file->nr_endbr_int++; } } } } } if (opts.stats) printf("nr_insns: %lu\n", nr_insns); return 0; err: free(insn); return ret;} /* * Read the pv_ops[] .data table to find the static initialized values. */static int add_pv_ops(struct objtool_file *file, const char *symname){ struct symbol *sym, *func; unsigned long off, end; struct reloc *rel; int idx; sym = find_symbol_by_name(file->elf, symname); if (!sym) return 0; off = sym->offset; end = off + sym->len; for (;;) { rel = find_reloc_by_dest_range(file->elf, sym->sec, off, end - off); if (!rel) break; func = rel->sym; if (func->type == STT_SECTION) func = find_symbol_by_offset(rel->sym->sec, rel->addend); idx = (rel->offset - sym->offset) / sizeof(unsigned long); Lines 1–500 of 4596. Load more
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com> */ #include <string.h> #include <stdlib.h> #include <inttypes.h> #include <sys/mman.h> #include <arch/elf.h> #include <objtool/builtin.h> #include <objtool/cfi.h> #include <objtool/arch.h> #include <objtool/check.h> #include <objtool/special.h> #include <objtool/warn.h> #include <objtool/endianness.h> #include <linux/objtool.h> #include <linux/hashtable.h> #include <linux/kernel.h> #include <linux/static_call_types.h> struct alternative { struct list_head list; struct instruction *insn; bool skip_orig; }; static unsigned long nr_cfi, nr_cfi_reused, nr_cfi_cache; static struct cfi_init_state initial_func_cfi; static struct cfi_state init_cfi; static struct cfi_state func_cfi; struct instruction *find_insn(struct objtool_file *file, struct section *sec, unsigned long offset) { struct instruction *insn; hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) { if (insn->sec == sec && insn->offset == offset) return insn; } return NULL; } static struct instruction *next_insn_same_sec(struct objtool_file *file, struct instruction *insn) { struct instruction *next = list_next_entry(insn, list); if (!next || &next->list == &file->insn_list || next->sec != insn->sec) return NULL; return next; } static struct instruction *next_insn_same_func(struct objtool_file *file, struct instruction *insn) { struct instruction *next = list_next_entry(insn, list); struct symbol *func = insn_func(insn); if (!func) return NULL; if (&next->list != &file->insn_list && insn_func(next) == func) return next; /* Check if we're already in the subfunction: */ if (func == func->cfunc) return NULL; /* Move to the subfunction: */ return find_insn(file, func->cfunc->sec, func->cfunc->offset); } static struct instruction *prev_insn_same_sym(struct objtool_file *file, struct instruction *insn) { struct instruction *prev = list_prev_entry(insn, list); if (&prev->list != &file->insn_list && insn_func(prev) == insn_func(insn)) return prev; return NULL; } #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->list != &file->insn_list && \ insn->sec == sym->sec && \ insn->offset < sym->offset + sym->len; \ insn = list_next_entry(insn, list)) #define sym_for_each_insn_continue_reverse(file, sym, insn) \ for (insn = list_prev_entry(insn, list); \ &insn->list != &file->insn_list && \ insn->sec == sym->sec && insn->offset >= sym->offset; \ insn = list_prev_entry(insn, list)) #define sec_for_each_insn_from(file, insn) \ for (; insn; insn = next_insn_same_sec(file, insn)) #define sec_for_each_insn_continue(file, insn) \ for (insn = next_insn_same_sec(file, insn); insn; \ insn = next_insn_same_sec(file, insn)) static bool is_jump_table_jump(struct instruction *insn) { struct alt_group *alt_group = insn->alt_group; if (insn->jump_table) return true; /* Retpoline alternative for a jump table? */ return alt_group && alt_group->orig_group && alt_group->orig_group->first_insn->jump_table; } static bool is_sibling_call(struct instruction *insn) { /* * Assume only STT_FUNC calls have jump-tables. */ if (insn_func(insn)) { /* An indirect jump is either a sibling call or a jump to a table. */ if (insn->type == INSN_JUMP_DYNAMIC) return !is_jump_table_jump(insn); } /* add_jump_destinations() sets insn->call_dest for sibling calls. */ return (is_static_jump(insn) && insn->call_dest); } /* * This checks to see if the given function is a "noreturn" function. * * For global functions which are outside the scope of this object file, we * have to keep a manual list of them. * * For local functions, we have to detect them manually by simply looking for * the lack of a return instruction. */ static bool __dead_end_function(struct objtool_file *file, struct symbol *func, int recursion) { int i; struct instruction *insn; bool empty = true; /* * Unfortunately these have to be hard coded because the noreturn * attribute isn't provided in ELF data. Keep 'em sorted. */ static const char * const global_noreturns[] = { "__invalid_creds", "__module_put_and_kthread_exit", "__reiserfs_panic", "__stack_chk_fail", "__ubsan_handle_builtin_unreachable", "cpu_bringup_and_idle", "cpu_startup_entry", "do_exit", "do_group_exit", "do_task_dead", "ex_handler_msr_mce", "fortify_panic", "kthread_complete_and_exit", "kthread_exit", "kunit_try_catch_throw", "lbug_with_loc", "machine_real_restart", "make_task_dead", "panic", "rewind_stack_and_make_dead", "sev_es_terminate", "snp_abort", "stop_this_cpu", "usercopy_abort", "xen_start_kernel", }; if (!func) return false; if (func->bind == STB_WEAK) return false; if (func->bind == STB_GLOBAL) for (i = 0; i < ARRAY_SIZE(global_noreturns); i++) if (!strcmp(func->name, global_noreturns[i])) return true; if (!func->len) return false; insn = find_insn(file, func->sec, func->offset); if (!insn || !insn_func(insn)) return false; func_for_each_insn(file, func, insn) { empty = false; if (insn->type == INSN_RETURN) return false; } if (empty) return false; /* * A function can have a sibling call instead of a return. In that * case, the function's dead-end status depends on whether the target * of the sibling call returns. */ func_for_each_insn(file, func, insn) { if (is_sibling_call(insn)) { struct instruction *dest = insn->jump_dest; if (!dest) /* sibling call to another file */ return false; /* local sibling call */ if (recursion == 5) { /* * Infinite recursion: two functions have * sibling calls to each other. This is a very * rare case. It means they aren't dead ends. */ return false; } return __dead_end_function(file, insn_func(dest), recursion+1); } } return true; } static bool dead_end_function(struct objtool_file *file, struct symbol *func) { return __dead_end_function(file, func, 0); } static void init_cfi_state(struct cfi_state *cfi) { int i; for (i = 0; i < CFI_NUM_REGS; i++) { cfi->regs[i].base = CFI_UNDEFINED; cfi->vals[i].base = CFI_UNDEFINED; } cfi->cfa.base = CFI_UNDEFINED; cfi->drap_reg = CFI_UNDEFINED; cfi->drap_offset = -1; } static void init_insn_state(struct objtool_file *file, struct insn_state *state, struct section *sec) { memset(state, 0, sizeof(*state)); init_cfi_state(&state->cfi); /* * We need the full vmlinux for noinstr validation, otherwise we can * not correctly determine insn->call_dest->sec (external symbols do * not have a section). */ if (opts.link && opts.noinstr && sec) state->noinstr = sec->noinstr; } static struct cfi_state *cfi_alloc(void) { struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1); if (!cfi) { WARN("calloc failed"); exit(1); } nr_cfi++; return cfi; } static int cfi_bits; static struct hlist_head *cfi_hash; static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2) { return memcmp((void *)cfi1 + sizeof(cfi1->hash), (void *)cfi2 + sizeof(cfi2->hash), sizeof(struct cfi_state) - sizeof(struct hlist_node)); } static inline u32 cfi_key(struct cfi_state *cfi) { return jhash((void *)cfi + sizeof(cfi->hash), sizeof(*cfi) - sizeof(cfi->hash), 0); } static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi) { struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)]; struct cfi_state *obj; hlist_for_each_entry(obj, head, hash) { if (!cficmp(cfi, obj)) { nr_cfi_cache++; return obj; } } obj = cfi_alloc(); *obj = *cfi; hlist_add_head(&obj->hash, head); return obj; } static void cfi_hash_add(struct cfi_state *cfi) { struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)]; hlist_add_head(&cfi->hash, head); } static void *cfi_hash_alloc(unsigned long size) { cfi_bits = max(10, ilog2(size)); cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); if (cfi_hash == (void *)-1L) { WARN("mmap fail cfi_hash"); cfi_hash = NULL; } else if (opts.stats) { printf("cfi_bits: %d\n", cfi_bits); } return cfi_hash; } static unsigned long nr_insns; static unsigned long nr_insns_visited; /* * Call the arch-specific instruction decoder for all the instructions and add * them to the global instruction list. */ static int decode_instructions(struct objtool_file *file) { struct section *sec; struct symbol *func; unsigned long offset; struct instruction *insn; int ret; for_each_sec(file, sec) { if (!(sec->sh.sh_flags & SHF_EXECINSTR)) continue; if (strcmp(sec->name, ".altinstr_replacement") && strcmp(sec->name, ".altinstr_aux") && strncmp(sec->name, ".discard.", 9)) sec->text = true; if (!strcmp(sec->name, ".noinstr.text") || !strcmp(sec->name, ".entry.text") || !strncmp(sec->name, ".text.__x86.", 12)) sec->noinstr = true; /* * .init.text code is ran before userspace and thus doesn't * strictly need retpolines, except for modules which are * loaded late, they very much do need retpoline in their * .init.text */ if (!strcmp(sec->name, ".init.text") && !opts.module) sec->init = true; for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) { insn = malloc(sizeof(*insn)); if (!insn) { WARN("malloc failed"); return -1; } memset(insn, 0, sizeof(*insn)); INIT_LIST_HEAD(&insn->alts); INIT_LIST_HEAD(&insn->stack_ops); INIT_LIST_HEAD(&insn->call_node); insn->sec = sec; insn->offset = offset; ret = arch_decode_instruction(file, sec, offset, sec->sh.sh_size - offset, &insn->len, &insn->type, &insn->immediate, &insn->stack_ops); if (ret) goto err; /* * By default, "ud2" is a dead end unless otherwise * annotated, because GCC 7 inserts it for certain * divide-by-zero cases. */ if (insn->type == INSN_BUG) insn->dead_end = true; hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset)); list_add_tail(&insn->list, &file->insn_list); nr_insns++; } list_for_each_entry(func, &sec->symbol_list, list) { if (func->type != STT_NOTYPE && func->type != STT_FUNC) continue; if (func->offset == sec->sh.sh_size) { /* Heuristic: likely an "end" symbol */ if (func->type == STT_NOTYPE) continue; WARN("%s(): STT_FUNC at end of section", func->name); return -1; } if (func->return_thunk || func->alias != func) continue; if (!find_insn(file, sec, func->offset)) { WARN("%s(): can't find starting instruction", func->name); return -1; } sym_for_each_insn(file, func, insn) { insn->sym = func; if (func->type == STT_FUNC && insn->type == INSN_ENDBR && list_empty(&insn->call_node)) { if (insn->offset == func->offset) { list_add_tail(&insn->call_node, &file->endbr_list); file->nr_endbr++; } else { file->nr_endbr_int++; } } } } } if (opts.stats) printf("nr_insns: %lu\n", nr_insns); return 0; err: free(insn); return ret; } /* * Read the pv_ops[] .data table to find the static initialized values. */ static int add_pv_ops(struct objtool_file *file, const char *symname) { struct symbol *sym, *func; unsigned long off, end; struct reloc *rel; int idx; sym = find_symbol_by_name(file->elf, symname); if (!sym) return 0; off = sym->offset; end = off + sym->len; for (;;) { rel = find_reloc_by_dest_range(file->elf, sym->sec, off, end - off); if (!rel) break; func = rel->sym; if (func->type == STT_SECTION) func = find_symbol_by_offset(rel->sym->sec, rel->addend); idx = (rel->offset - sym->offset) / sizeof(unsigned long);