| Date | Thu, 24 Feb 2022 15:52:15 +0100 | From | Peter Zijlstra <> | Subject | [PATCH v2 37/39] x86/ibt: Finish --ibt-fix-direct on module loading |
| |
Since modules are not fully linked objects, per construction, the LTO-like objtool pass cannot fix up the direct calls to external symbols.
Have the module loader finish the job.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> --- arch/x86/kernel/module.c | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-)
--- a/arch/x86/kernel/module.c +++ b/arch/x86/kernel/module.c @@ -24,6 +24,7 @@ #include <asm/page.h> #include <asm/setup.h> #include <asm/unwind.h> +#include <asm/ibt.h> #if 0 #define DEBUGP(fmt, ...) \ @@ -128,6 +129,33 @@ int apply_relocate(Elf32_Shdr *sechdrs, return 0; } #else /*X86_64*/ + +static inline void ibt_fix_direct(void *loc, u64 *val) +{ +#ifdef CONFIG_X86_KERNEL_IBT + const void *addr = (void *)(4 + *val); + union text_poke_insn text; + u32 insn; + + if (get_kernel_nofault(insn, addr)) + return; + + if (!is_endbr(insn)) + return; + + /* validate jmp.d32/call @ loc */ + if (WARN_ONCE(get_kernel_nofault(text, loc-1) || + (text.opcode != CALL_INSN_OPCODE && + text.opcode != JMP32_INSN_OPCODE), + "Unexpected code at: %pS\n", loc)) + return; + + DEBUGP("ibt_fix_direct: %pS\n", addr); + + *val += 4; +#endif +} + static int __apply_relocate_add(Elf64_Shdr *sechdrs, const char *strtab, unsigned int symindex, @@ -139,6 +167,7 @@ static int __apply_relocate_add(Elf64_Sh Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr; Elf64_Sym *sym; void *loc; + int type; u64 val; DEBUGP("Applying relocate section %u to %u\n", @@ -153,13 +182,14 @@ static int __apply_relocate_add(Elf64_Sh sym = (Elf64_Sym *)sechdrs[symindex].sh_addr + ELF64_R_SYM(rel[i].r_info); + type = ELF64_R_TYPE(rel[i].r_info); + DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n", - (int)ELF64_R_TYPE(rel[i].r_info), - sym->st_value, rel[i].r_addend, (u64)loc); + type, sym->st_value, rel[i].r_addend, (u64)loc); val = sym->st_value + rel[i].r_addend; - switch (ELF64_R_TYPE(rel[i].r_info)) { + switch (type) { case R_X86_64_NONE: break; case R_X86_64_64: @@ -185,6 +215,10 @@ static int __apply_relocate_add(Elf64_Sh case R_X86_64_PLT32: if (*(u32 *)loc != 0) goto invalid_relocation; + + if (type == R_X86_64_PLT32) + ibt_fix_direct(loc, &val); + val -= (u64)loc; write(loc, &val, 4); #if 0
|