[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20220224151324.198359442@infradead.org>
Date: Thu, 24 Feb 2022 15:52:15 +0100
From: Peter Zijlstra <peterz@...radead.org>
To: x86@...nel.org, joao@...rdrivepizza.com, hjl.tools@...il.com,
jpoimboe@...hat.com, andrew.cooper3@...rix.com
Cc: linux-kernel@...r.kernel.org, peterz@...radead.org,
ndesaulniers@...gle.com, keescook@...omium.org,
samitolvanen@...gle.com, mark.rutland@....com,
alyssa.milburn@...el.com, mbenes@...e.cz, rostedt@...dmis.org,
mhiramat@...nel.org, alexei.starovoitov@...il.com
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@...radead.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
Powered by blists - more mailing lists