// SPDX-License-Identifier: GPL-2.0-or-later/* * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com> */ #include <string.h>#include <stdlib.h> #include "builtin.h"#include "check.h"#include "elf.h"#include "special.h"#include "arch.h"#include "warn.h" #include <linux/hashtable.h>#include <linux/kernel.h> #define FAKE_JUMP_OFFSET -1 #define C_JUMP_TABLE_SECTION ".rodata..c_jump_table" struct alternative { struct list_head list; struct instruction *insn; bool skip_orig;}; const char *objname;struct cfi_state initial_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, 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; if (!func) return NULL; if (&next->list != &file->insn_list && next->func == 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);} #define func_for_each_insn_all(file, func, insn) \ for (insn = find_insn(file, func->sec, func->offset); \ insn; \ insn = next_insn_same_func(file, insn)) #define func_for_each_insn(file, func, insn) \ for (insn = find_insn(file, func->sec, func->offset); \ insn && &insn->list != &file->insn_list && \ insn->sec == func->sec && \ insn->offset < func->offset + func->len; \ insn = list_next_entry(insn, list)) #define func_for_each_insn_continue_reverse(file, func, insn) \ for (insn = list_prev_entry(insn, list); \ &insn->list != &file->insn_list && \ insn->sec == func->sec && insn->offset >= func->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_sibling_call(struct instruction *insn){ /* An indirect jump is either a sibling call or a jump to a table. */ if (insn->type == INSN_JUMP_DYNAMIC) return list_empty(&insn->alts); if (insn->type != INSN_JUMP_CONDITIONAL && insn->type != INSN_JUMP_UNCONDITIONAL) return false; /* add_jump_destinations() sets insn->call_dest for sibling calls. */ return !!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. */ static const char * const global_noreturns[] = { "__stack_chk_fail", "panic", "do_exit", "do_task_dead", "__module_put_and_exit", "complete_and_exit", "kvm_spurious_fault", "__reiserfs_panic", "lbug_with_loc", "fortify_panic", "usercopy_abort", "machine_real_restart", "rewind_stack_do_exit", }; 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->func) return false; func_for_each_insn_all(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_all(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, dest->func, 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 clear_insn_state(struct insn_state *state){ int i; memset(state, 0, sizeof(*state)); state->cfa.base = CFI_UNDEFINED; for (i = 0; i < CFI_NUM_REGS; i++) { state->regs[i].base = CFI_UNDEFINED; state->vals[i].base = CFI_UNDEFINED; } state->drap_reg = CFI_UNDEFINED; state->drap_offset = -1;} /* * 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; for (offset = 0; offset < sec->len; offset += insn->len) { insn = malloc(sizeof(*insn)); if (!insn) { WARN("malloc failed"); return -1; } memset(insn, 0, sizeof(*insn)); INIT_LIST_HEAD(&insn->alts); clear_insn_state(&insn->state); insn->sec = sec; insn->offset = offset; ret = arch_decode_instruction(file->elf, sec, offset, sec->len - offset, &insn->len, &insn->type, &insn->immediate, &insn->stack_op); if (ret) goto err; hash_add(file->insn_hash, &insn->hash, insn->offset); list_add_tail(&insn->list, &file->insn_list); } list_for_each_entry(func, &sec->symbol_list, list) { if (func->type != STT_FUNC || func->alias != func) continue; if (!find_insn(file, sec, func->offset)) { WARN("%s(): can't find starting instruction", func->name); return -1; } func_for_each_insn(file, func, insn) insn->func = func; } } return 0; err: free(insn); return ret;} /* * Mark "ud2" instructions and manually annotated dead ends. */static int add_dead_ends(struct objtool_file *file){ struct section *sec; struct rela *rela; struct instruction *insn; bool found; /* * By default, "ud2" is a dead end unless otherwise annotated, because * GCC 7 inserts it for certain divide-by-zero cases. */ for_each_insn(file, insn) if (insn->type == INSN_BUG) insn->dead_end = true; /* * Check for manually annotated dead ends. */ sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); if (!sec) goto reachable; list_for_each_entry(rela, &sec->rela_list, list) { if (rela->sym->type != STT_SECTION) { WARN("unexpected relocation symbol type in %s", sec->name); return -1; } insn = find_insn(file, rela->sym->sec, rela->addend); if (insn) insn = list_prev_entry(insn, list); else if (rela->addend == rela->sym->sec->len) { found = false; list_for_each_entry_reverse(insn, &file->insn_list, list) { if (insn->sec == rela->sym->sec) { found = true; break; } } if (!found) { WARN("can't find unreachable insn at %s+0x%x", rela->sym->sec->name, rela->addend); return -1; } } else { WARN("can't find unreachable insn at %s+0x%x", rela->sym->sec->name, rela->addend); return -1; } insn->dead_end = true; } reachable: /* * These manually annotated reachable checks are needed for GCC 4.4, * where the Linux unreachable() macro isn't supported. In that case * GCC doesn't know the "ud2" is fatal, so it generates code as if it's * not a dead end. */ sec = find_section_by_name(file->elf, ".rela.discard.reachable"); if (!sec) return 0; list_for_each_entry(rela, &sec->rela_list, list) { if (rela->sym->type != STT_SECTION) { WARN("unexpected relocation symbol type in %s", sec->name); return -1; } insn = find_insn(file, rela->sym->sec, rela->addend); if (insn) insn = list_prev_entry(insn, list); else if (rela->addend == rela->sym->sec->len) { found = false; list_for_each_entry_reverse(insn, &file->insn_list, list) { if (insn->sec == rela->sym->sec) { found = true; break; } } if (!found) { WARN("can't find reachable insn at %s+0x%x", rela->sym->sec->name, rela->addend); return -1; } } else { WARN("can't find reachable insn at %s+0x%x", rela->sym->sec->name, rela->addend); return -1; } insn->dead_end = false; } return 0;} /* * Warnings shouldn't be reported for ignored functions. */static void add_ignores(struct objtool_file *file){ struct instruction *insn; struct section *sec; struct symbol *func; struct rela *rela; sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard"); if (!sec) return; list_for_each_entry(rela, &sec->rela_list, list) { switch (rela->sym->type) { case STT_FUNC: func = rela->sym; break; case STT_SECTION: func = find_symbol_by_offset(rela->sym->sec, rela->addend); if (!func || func->type != STT_FUNC) continue; break; default: WARN("unexpected relocation symbol type in %s: %d", sec->name, rela->sym->type); continue; } func_for_each_insn_all(file, func, insn) insn->ignore = true; }} /* * This is a whitelist of functions that is allowed to be called with AC set. * The list is meant to be minimal and only contains compiler instrumentation * ABI and a few functions used to implement *_{to,from}_user() functions. * * These functions must not directly change AC, but may PUSHF/POPF. */static const char *uaccess_safe_builtin[] = { /* KASAN */ "kasan_report", "check_memory_region", /* KASAN out-of-line */ "__asan_loadN_noabort", "__asan_load1_noabort", "__asan_load2_noabort", "__asan_load4_noabort", "__asan_load8_noabort", "__asan_load16_noabort", "__asan_storeN_noabort", "__asan_store1_noabort", "__asan_store2_noabort", "__asan_store4_noabort", "__asan_store8_noabort", "__asan_store16_noabort", /* KASAN in-line */ "__asan_report_load_n_noabort", "__asan_report_load1_noabort", "__asan_report_load2_noabort", "__asan_report_load4_noabort", "__asan_report_load8_noabort", "__asan_report_load16_noabort", "__asan_report_store_n_noabort", "__asan_report_store1_noabort", "__asan_report_store2_noabort", "__asan_report_store4_noabort", "__asan_report_store8_noabort", "__asan_report_store16_noabort", /* KCOV */ "write_comp_data", "__sanitizer_cov_trace_pc", "__sanitizer_cov_trace_const_cmp1", "__sanitizer_cov_trace_const_cmp2", "__sanitizer_cov_trace_const_cmp4", "__sanitizer_cov_trace_const_cmp8", "__sanitizer_cov_trace_cmp1", "__sanitizer_cov_trace_cmp2", "__sanitizer_cov_trace_cmp4", "__sanitizer_cov_trace_cmp8", /* UBSAN */ "ubsan_type_mismatch_common", "__ubsan_handle_type_mismatch", "__ubsan_handle_type_mismatch_v1", /* misc */ "csum_partial_copy_generic", "__memcpy_mcsafe", "mcsafe_handle_tail", "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */ NULL}; static void add_uaccess_safe(struct objtool_file *file){ struct symbol *func; const char **name; if (!uaccess) return; Lines 1–500 of 2497. 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 "builtin.h" #include "check.h" #include "elf.h" #include "special.h" #include "arch.h" #include "warn.h" #include <linux/hashtable.h> #include <linux/kernel.h> #define FAKE_JUMP_OFFSET -1 #define C_JUMP_TABLE_SECTION ".rodata..c_jump_table" struct alternative { struct list_head list; struct instruction *insn; bool skip_orig; }; const char *objname; struct cfi_state initial_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, 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; if (!func) return NULL; if (&next->list != &file->insn_list && next->func == 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); } #define func_for_each_insn_all(file, func, insn) \ for (insn = find_insn(file, func->sec, func->offset); \ insn; \ insn = next_insn_same_func(file, insn)) #define func_for_each_insn(file, func, insn) \ for (insn = find_insn(file, func->sec, func->offset); \ insn && &insn->list != &file->insn_list && \ insn->sec == func->sec && \ insn->offset < func->offset + func->len; \ insn = list_next_entry(insn, list)) #define func_for_each_insn_continue_reverse(file, func, insn) \ for (insn = list_prev_entry(insn, list); \ &insn->list != &file->insn_list && \ insn->sec == func->sec && insn->offset >= func->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_sibling_call(struct instruction *insn) { /* An indirect jump is either a sibling call or a jump to a table. */ if (insn->type == INSN_JUMP_DYNAMIC) return list_empty(&insn->alts); if (insn->type != INSN_JUMP_CONDITIONAL && insn->type != INSN_JUMP_UNCONDITIONAL) return false; /* add_jump_destinations() sets insn->call_dest for sibling calls. */ return !!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. */ static const char * const global_noreturns[] = { "__stack_chk_fail", "panic", "do_exit", "do_task_dead", "__module_put_and_exit", "complete_and_exit", "kvm_spurious_fault", "__reiserfs_panic", "lbug_with_loc", "fortify_panic", "usercopy_abort", "machine_real_restart", "rewind_stack_do_exit", }; 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->func) return false; func_for_each_insn_all(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_all(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, dest->func, 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 clear_insn_state(struct insn_state *state) { int i; memset(state, 0, sizeof(*state)); state->cfa.base = CFI_UNDEFINED; for (i = 0; i < CFI_NUM_REGS; i++) { state->regs[i].base = CFI_UNDEFINED; state->vals[i].base = CFI_UNDEFINED; } state->drap_reg = CFI_UNDEFINED; state->drap_offset = -1; } /* * 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; for (offset = 0; offset < sec->len; offset += insn->len) { insn = malloc(sizeof(*insn)); if (!insn) { WARN("malloc failed"); return -1; } memset(insn, 0, sizeof(*insn)); INIT_LIST_HEAD(&insn->alts); clear_insn_state(&insn->state); insn->sec = sec; insn->offset = offset; ret = arch_decode_instruction(file->elf, sec, offset, sec->len - offset, &insn->len, &insn->type, &insn->immediate, &insn->stack_op); if (ret) goto err; hash_add(file->insn_hash, &insn->hash, insn->offset); list_add_tail(&insn->list, &file->insn_list); } list_for_each_entry(func, &sec->symbol_list, list) { if (func->type != STT_FUNC || func->alias != func) continue; if (!find_insn(file, sec, func->offset)) { WARN("%s(): can't find starting instruction", func->name); return -1; } func_for_each_insn(file, func, insn) insn->func = func; } } return 0; err: free(insn); return ret; } /* * Mark "ud2" instructions and manually annotated dead ends. */ static int add_dead_ends(struct objtool_file *file) { struct section *sec; struct rela *rela; struct instruction *insn; bool found; /* * By default, "ud2" is a dead end unless otherwise annotated, because * GCC 7 inserts it for certain divide-by-zero cases. */ for_each_insn(file, insn) if (insn->type == INSN_BUG) insn->dead_end = true; /* * Check for manually annotated dead ends. */ sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); if (!sec) goto reachable; list_for_each_entry(rela, &sec->rela_list, list) { if (rela->sym->type != STT_SECTION) { WARN("unexpected relocation symbol type in %s", sec->name); return -1; } insn = find_insn(file, rela->sym->sec, rela->addend); if (insn) insn = list_prev_entry(insn, list); else if (rela->addend == rela->sym->sec->len) { found = false; list_for_each_entry_reverse(insn, &file->insn_list, list) { if (insn->sec == rela->sym->sec) { found = true; break; } } if (!found) { WARN("can't find unreachable insn at %s+0x%x", rela->sym->sec->name, rela->addend); return -1; } } else { WARN("can't find unreachable insn at %s+0x%x", rela->sym->sec->name, rela->addend); return -1; } insn->dead_end = true; } reachable: /* * These manually annotated reachable checks are needed for GCC 4.4, * where the Linux unreachable() macro isn't supported. In that case * GCC doesn't know the "ud2" is fatal, so it generates code as if it's * not a dead end. */ sec = find_section_by_name(file->elf, ".rela.discard.reachable"); if (!sec) return 0; list_for_each_entry(rela, &sec->rela_list, list) { if (rela->sym->type != STT_SECTION) { WARN("unexpected relocation symbol type in %s", sec->name); return -1; } insn = find_insn(file, rela->sym->sec, rela->addend); if (insn) insn = list_prev_entry(insn, list); else if (rela->addend == rela->sym->sec->len) { found = false; list_for_each_entry_reverse(insn, &file->insn_list, list) { if (insn->sec == rela->sym->sec) { found = true; break; } } if (!found) { WARN("can't find reachable insn at %s+0x%x", rela->sym->sec->name, rela->addend); return -1; } } else { WARN("can't find reachable insn at %s+0x%x", rela->sym->sec->name, rela->addend); return -1; } insn->dead_end = false; } return 0; } /* * Warnings shouldn't be reported for ignored functions. */ static void add_ignores(struct objtool_file *file) { struct instruction *insn; struct section *sec; struct symbol *func; struct rela *rela; sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard"); if (!sec) return; list_for_each_entry(rela, &sec->rela_list, list) { switch (rela->sym->type) { case STT_FUNC: func = rela->sym; break; case STT_SECTION: func = find_symbol_by_offset(rela->sym->sec, rela->addend); if (!func || func->type != STT_FUNC) continue; break; default: WARN("unexpected relocation symbol type in %s: %d", sec->name, rela->sym->type); continue; } func_for_each_insn_all(file, func, insn) insn->ignore = true; } } /* * This is a whitelist of functions that is allowed to be called with AC set. * The list is meant to be minimal and only contains compiler instrumentation * ABI and a few functions used to implement *_{to,from}_user() functions. * * These functions must not directly change AC, but may PUSHF/POPF. */ static const char *uaccess_safe_builtin[] = { /* KASAN */ "kasan_report", "check_memory_region", /* KASAN out-of-line */ "__asan_loadN_noabort", "__asan_load1_noabort", "__asan_load2_noabort", "__asan_load4_noabort", "__asan_load8_noabort", "__asan_load16_noabort", "__asan_storeN_noabort", "__asan_store1_noabort", "__asan_store2_noabort", "__asan_store4_noabort", "__asan_store8_noabort", "__asan_store16_noabort", /* KASAN in-line */ "__asan_report_load_n_noabort", "__asan_report_load1_noabort", "__asan_report_load2_noabort", "__asan_report_load4_noabort", "__asan_report_load8_noabort", "__asan_report_load16_noabort", "__asan_report_store_n_noabort", "__asan_report_store1_noabort", "__asan_report_store2_noabort", "__asan_report_store4_noabort", "__asan_report_store8_noabort", "__asan_report_store16_noabort", /* KCOV */ "write_comp_data", "__sanitizer_cov_trace_pc", "__sanitizer_cov_trace_const_cmp1", "__sanitizer_cov_trace_const_cmp2", "__sanitizer_cov_trace_const_cmp4", "__sanitizer_cov_trace_const_cmp8", "__sanitizer_cov_trace_cmp1", "__sanitizer_cov_trace_cmp2", "__sanitizer_cov_trace_cmp4", "__sanitizer_cov_trace_cmp8", /* UBSAN */ "ubsan_type_mismatch_common", "__ubsan_handle_type_mismatch", "__ubsan_handle_type_mismatch_v1", /* misc */ "csum_partial_copy_generic", "__memcpy_mcsafe", "mcsafe_handle_tail", "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */ NULL }; static void add_uaccess_safe(struct objtool_file *file) { struct symbol *func; const char **name; if (!uaccess) return;