cregit-Linux how code gets into the kernel

Release 4.14 tools/objtool/check.c

Directory: tools/objtool
/*
 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>
#include <stdlib.h>

#include "check.h"
#include "elf.h"
#include "special.h"
#include "arch.h"
#include "warn.h"

#include <linux/hashtable.h>
#include <linux/kernel.h>


struct alternative {
	
struct list_head list;
	
struct instruction *insn;
};


const char *objname;

static bool no_fp;

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; }

Contributors

PersonTokensPropCommitsCommitProp
Josh Poimboeuf56100.00%1100.00%
Total56100.00%1100.00%


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; }

Contributors

PersonTokensPropCommitsCommitProp
Josh Poimboeuf59100.00%2100.00%
Total59100.00%2100.00%

#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)) /* * Check if the function has been manually whitelisted with the * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted * due to its use of a context switching instruction. */
static bool ignore_func(struct objtool_file *file, struct symbol *func) { struct rela *rela; /* check for STACK_FRAME_NON_STANDARD */ if (file->whitelist && file->whitelist->rela) list_for_each_entry(rela, &file->whitelist->rela->rela_list, list) { if (rela->sym->type == STT_SECTION && rela->sym->sec == func->sec && rela->addend == func->offset) return true; if (rela->sym->type == STT_FUNC && rela->sym == func) return true; } return false; }

Contributors

PersonTokensPropCommitsCommitProp
Josh Poimboeuf97100.00%1100.00%
Total97100.00%1100.00%

/* * 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. * * Returns: * -1: error * 0: no dead end * 1: dead end */
static int __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", }; if (func->bind == STB_WEAK) return 0; if (func->bind == STB_GLOBAL) for (i = 0; i < ARRAY_SIZE(global_noreturns); i++) if (!strcmp(func->name, global_noreturns[i])) return 1; if (!func->sec) return 0; func_for_each_insn(file, func, insn) { empty = false; if (insn->type == INSN_RETURN) return 0; } if (empty) return 0; /* * 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 (insn->sec != func->sec || insn->offset >= func->offset + func->len) break; if (insn->type == INSN_JUMP_UNCONDITIONAL) { struct instruction *dest = insn->jump_dest; struct symbol *dest_func; if (!dest) /* sibling call to another file */ return 0; if (dest->sec != func->sec || dest->offset < func->offset || dest->offset >= func->offset + func->len) { /* local sibling call */ dest_func = find_symbol_by_offset(dest->sec, dest->offset); if (!dest_func) continue; if (recursion == 5) { WARN_FUNC("infinite recursion (objtool bug!)", dest->sec, dest->offset); return -1; } return __dead_end_function(file, dest_func, recursion + 1); } } if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts)) /* sibling call */ return 0; } return 1; }

Contributors

PersonTokensPropCommitsCommitProp
Josh Poimboeuf337100.00%1100.00%
Total337100.00%1100.00%


static int dead_end_function(struct objtool_file *file, struct symbol *func) { return __dead_end_function(file, func, 0); }

Contributors

PersonTokensPropCommitsCommitProp
Josh Poimboeuf26100.00%1100.00%
Total26100.00%1100.00%


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; }

Contributors

PersonTokensPropCommitsCommitProp
Josh Poimboeuf85100.00%3100.00%
Total85100.00%3100.00%

/* * 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; if (!insn->type || insn->type > INSN_LAST) { WARN_FUNC("invalid instruction type %d", insn->sec, insn->offset, insn->type); ret = -1; 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) 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) if (!insn->func) insn->func = func; } } return 0; err: free(insn); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Josh Poimboeuf35894.96%375.00%
Kamalesh Babulal195.04%125.00%
Total377100.00%4100.00%

/* * 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; }

Contributors

PersonTokensPropCommitsCommitProp
Josh Poimboeuf441100.00%2100.00%
Total441100.00%2100.00%

/* * 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; for_each_sec(file, sec) { list_for_each_entry(func, &sec->symbol_list, list) { if (func->type != STT_FUNC) continue; if (!ignore_func(file, func)) continue; func_for_each_insn(file, func, insn) insn->ignore = true; } } }

Contributors

PersonTokensPropCommitsCommitProp
Josh Poimboeuf78100.00%2100.00%
Total78100.00%2100.00%

/* * Find the destination instructions for all jumps. */
static int add_jump_destinations(struct objtool_file *file) { struct instruction *insn; struct rela *rela; struct section *dest_sec; unsigned long dest_off; for_each_insn(file, insn) { if (insn->type != INSN_JUMP_CONDITIONAL && insn->type != INSN_JUMP_UNCONDITIONAL) continue; if (insn->ignore) continue; rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len); if (!rela) { dest_sec = insn->sec; dest_off = insn->offset + insn->len + insn->immediate; } else if (rela->sym->type == STT_SECTION) { dest_sec = rela->sym->sec; dest_off = rela->addend + 4; } else if (rela->sym->sec->idx) { dest_sec = rela->sym->sec; dest_off = rela->sym->sym.st_value + rela->addend + 4; } else { /* sibling call */ insn->jump_dest = 0; continue; } insn->jump_dest = find_insn(file, dest_sec, dest_off); if (!insn->jump_dest) { /* * This is a special case where an alt instruction * jumps past the end of the section. These are * handled later in handle_group_alt(). */ if (!strcmp(insn->sec->name, ".altinstr_replacement")) continue; WARN_FUNC("can't find jump dest instruction at %s+0x%lx", insn->sec, insn->offset, dest_sec->name, dest_off); return -1; } } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Josh Poimboeuf245100.00%2100.00%
Total245100.00%2100.00%

/* * Find the destination instructions for all calls. */
static int add_call_destinations(struct objtool_file *file) { struct instruction *insn; unsigned long dest_off; struct rela *rela; for_each_insn(file, insn) { if (insn->type != INSN_CALL) continue; rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len); if (!rela) { dest_off = insn->offset + insn->len + insn->immediate; insn->call_dest = find_symbol_by_offset(insn->sec, dest_off); if (!insn->call_dest) { WARN_FUNC("can't find call dest symbol at offset 0x%lx", insn->sec, insn->offset, dest_off); return -1; } } else if (rela->sym->type == STT_SECTION) { insn->call_dest = find_symbol_by_offset(rela->sym->sec, rela->addend+4); if (!insn->call_dest || insn->call_dest->type != STT_FUNC) { WARN_FUNC("can't find call dest symbol at %s+0x%x", insn->sec, insn->offset, rela->sym->sec->name, rela->addend + 4); return -1; } } else insn->call_dest = rela->sym; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Josh Poimboeuf213100.00%1100.00%
Total213100.00%1100.00%

/* * The .alternatives section requires some extra special care, over and above * what other special sections require: * * 1. Because alternatives are patched in-place, we need to insert a fake jump * instruction at the end so that validate_branch() skips all the original * replaced instructions when validating the new instruction path. * * 2. An added wrinkle is that the new instruction length might be zero. In * that case the old instructions are replaced with noops. We simulate that * by creating a fake jump as the only new instruction. * * 3. In some cases, the alternative section includes an instruction which * conditionally jumps to the _end_ of the entry. We have to modify these * jumps' destinations to point back to .text rather than the end of the * entry in .altinstr_replacement. * * 4. It has been requested that we don't validate the !POPCNT feature path * which is a "very very small percentage of machines". */
static int handle_group_alt(struct objtool_file *file, struct special_alt *special_alt, struct instruction *orig_insn, struct instruction **new_insn) { struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump; unsigned long dest_off; last_orig_insn = NULL; insn = orig_insn; sec_for_each_insn_from(file, insn) { if (insn->offset >= special_alt->orig_off + special_alt->orig_len) break; if (special_alt->skip_orig) insn->type = INSN_NOP; insn->alt_group = true; last_orig_insn = insn; } if (!next_insn_same_sec(file, last_orig_insn)) { WARN("%s: don't know how to handle alternatives at end of section", special_alt->orig_sec->name); return -1; } fake_jump = malloc(sizeof(*fake_jump)); if (!fake_jump) { WARN("malloc failed"); return -1; } memset(fake_jump, 0, sizeof(*fake_jump)); INIT_LIST_HEAD(&fake_jump->alts); clear_insn_state(&fake_jump->state); fake_jump->sec = special_alt->new_sec; fake_jump->offset = -1; fake_jump->type = INSN_JUMP_UNCONDITIONAL; fake_jump->jump_dest = list_next_entry(last_orig_insn, list); fake_jump->ignore = true; if (!special_alt->new_len) { *new_insn = fake_jump; return 0; } last_new_insn = NULL; insn = *new_insn; sec_for_each_insn_from(file, insn) { if (insn->offset >= special_alt->new_off + special_alt->new_len) break; last_new_insn = insn; if (insn->type != INSN_JUMP_CONDITIONAL && insn->type != INSN_JUMP_UNCONDITIONAL) continue; if (!insn->immediate) continue; dest_off = insn->offset + insn->len + insn->immediate; if (dest_off == special_alt->new_off + special_alt->new_len) insn->jump_dest = fake_jump; if (!insn->jump_dest) { WARN_FUNC("can't find alternative jump destination", insn->sec, insn->offset); return -1; } } if (!last_new_insn) { WARN_FUNC("can't find last new alternative instruction", special_alt->new_sec, special_alt->new_off); return -1; } list_add(&fake_jump->list, &last_new_insn->list); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Josh Poimboeuf393100.00%2100.00%
Total393100.00%2100.00%

/* * A jump table entry can either convert a nop to a jump or a jump to a nop. * If the original instruction is a jump, make the alt entry an effective nop * by just skipping the original instruction. */
static int handle_jump_alt(struct objtool_file *file, struct special_alt *special_alt, struct instruction *orig_insn, struct instruction **new_insn) { if (orig_insn->type == INSN_NOP) return 0; if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) { WARN_FUNC("unsupported instruction at jump label", orig_insn->sec, orig_insn->offset); return -1; } *new_insn = list_next_entry(orig_insn, list); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Josh Poimboeuf78100.00%1100.00%
Total78100.00%1100.00%

/* * Read all the special sections which have alternate instructions which can be * patched in or redirected to at runtime. Each instruction having alternate * instruction(s) has them added to its insn->alts list, which will be * traversed in validate_branch(). */
static int add_special_section_alts(struct objtool_file *file) { struct list_head special_alts; struct instruction *orig_insn, *new_insn; struct special_alt *special_alt, *tmp; struct alternative *alt; int ret; ret = special_get_alts(file->elf, &special_alts); if (ret) return ret; list_for_each_entry_safe(special_alt, tmp, &special_alts, list) { alt = malloc(sizeof(*alt)); if (!alt) { WARN("malloc failed"); ret = -1; goto out; } orig_insn = find_insn(file, special_alt->orig_sec, special_alt->orig_off); if (!orig_insn) { WARN_FUNC("special: can't find orig instruction", special_alt->orig_sec, special_alt->orig_off); ret = -1; goto out; } new_insn = NULL; if (!special_alt->group || special_alt->new_len) { new_insn = find_insn(file, special_alt->new_sec, special_alt->new_off); if (!new_insn) { WARN_FUNC("special: can't find new instruction", special_alt->new_sec, special_alt->new_off); ret = -1; goto out; } } if (special_alt->group) { ret = handle_group_alt(file, special_alt, orig_insn, &new_insn); if (ret) goto out; } else if (special_alt->jump_or_nop) { ret = handle_jump_alt(file, special_alt, orig_insn, &new_insn); if (ret) goto out; } alt->insn = new_insn; list_add_tail(&alt->list, &orig_insn->alts); list_del(&special_alt->list); free(special_alt); } out: return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Josh Poimboeuf300100.00%1100.00%
Total300100.00%1100.00%


static int add_switch_table(struct objtool_file *file, struct symbol *func, struct instruction *insn, struct rela *table, struct rela *next_table) { struct rela *rela = table; struct instruction *alt_insn; struct alternative *alt; list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) { if (rela == next_table) break; if (rela->sym->sec != insn->sec || rela->addend <= func->offset || rela->addend >= func->offset + func->len) break; alt_insn = find_insn(file, insn->sec, rela->addend); if (!alt_insn) { WARN("%s: can't find instruction at %s+0x%x", file->rodata->rela->name, insn->sec->name, rela->addend); return -1; } alt = malloc(sizeof(*alt)); if (!alt) { WARN("malloc failed"); return -1; } alt->insn = alt_insn; list_add_tail(&alt->list, &insn->alts); } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Josh Poimboeuf196100.00%1100.00%
Total196100.00%1100.00%