@@ -109,15 +109,19 @@ enum bug_trap_type handle_cfi_failure(struct pt_regs *regs);
extern u32 cfi_bpf_hash;
extern u32 cfi_bpf_subprog_hash;
+#ifdef CONFIG_CALL_PADDING
+#define FINEIBT_INSN_OFFSET 16
+#define CFI_INSN_OFFSET CONFIG_FUNCTION_ALIGNMENT
+#else
+#define CFI_INSN_OFFSET 5
+#endif
+
static inline int cfi_get_offset(void)
{
switch (cfi_mode) {
case CFI_FINEIBT:
- return 16;
case CFI_KCFI:
- if (IS_ENABLED(CONFIG_CALL_PADDING))
- return 16;
- return 5;
+ return CFI_INSN_OFFSET;
default:
return 0;
}
@@ -908,7 +908,7 @@ void __init_or_module noinline apply_seal_endbr(s32 *start, s32 *end, struct mod
poison_endbr(addr, wr_addr, true);
if (IS_ENABLED(CONFIG_FINEIBT))
- poison_cfi(addr - 16, wr_addr - 16);
+ poison_cfi(addr, wr_addr);
}
}
@@ -974,12 +974,15 @@ u32 cfi_get_func_hash(void *func)
{
u32 hash;
- func -= cfi_get_offset();
switch (cfi_mode) {
+#ifdef CONFIG_FINEIBT
case CFI_FINEIBT:
+ func -= FINEIBT_INSN_OFFSET;
func += 7;
break;
+#endif
case CFI_KCFI:
+ func -= CFI_INSN_OFFSET;
func += 1;
break;
default:
@@ -1068,7 +1071,7 @@ early_param("cfi", cfi_parse_cmdline);
*
* caller: caller:
* movl $(-0x12345678),%r10d // 6 movl $0x12345678,%r10d // 6
- * addl $-15(%r11),%r10d // 4 sub $16,%r11 // 4
+ * addl $-15(%r11),%r10d // 4 sub $FINEIBT_INSN_OFFSET,%r11 // 4
* je 1f // 2 nop4 // 4
* ud2 // 2
* 1: call __x86_indirect_thunk_r11 // 5 call *%r11; nop2; // 5
@@ -1092,10 +1095,14 @@ extern u8 fineibt_preamble_end[];
#define fineibt_preamble_size (fineibt_preamble_end - fineibt_preamble_start)
#define fineibt_preamble_hash 7
+#define ___OFFSET_STR(x) #x
+#define __OFFSET_STR(x) ___OFFSET_STR(x)
+#define OFFSET_STR __OFFSET_STR(FINEIBT_INSN_OFFSET)
+
asm( ".pushsection .rodata \n"
"fineibt_caller_start: \n"
" movl $0x12345678, %r10d \n"
- " sub $16, %r11 \n"
+ " sub $"OFFSET_STR", %r11 \n"
ASM_NOP4
"fineibt_caller_end: \n"
".popsection \n"
@@ -1225,6 +1232,7 @@ static int cfi_rewrite_preamble(s32 *start, s32 *end, struct module *mod)
addr, addr, 5, addr))
return -EINVAL;
+ wr_addr += (CFI_INSN_OFFSET - FINEIBT_INSN_OFFSET);
text_poke_early(wr_addr, fineibt_preamble_start, fineibt_preamble_size);
WARN_ON(*(u32 *)(wr_addr + fineibt_preamble_hash) != 0x12345678);
text_poke_early(wr_addr + fineibt_preamble_hash, &hash, 4);
@@ -1241,7 +1249,8 @@ static void cfi_rewrite_endbr(s32 *start, s32 *end, struct module *mod)
void *addr = (void *)s + *s;
void *wr_addr = module_writable_address(mod, addr);
- poison_endbr(addr + 16, wr_addr + 16, false);
+ poison_endbr(addr + CFI_INSN_OFFSET, wr_addr + CFI_INSN_OFFSET,
+ false);
}
}
@@ -1347,12 +1356,12 @@ static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,
return;
case CFI_FINEIBT:
- /* place the FineIBT preamble at func()-16 */
+ /* place the FineIBT preamble at func()-FINEIBT_INSN_OFFSET */
ret = cfi_rewrite_preamble(start_cfi, end_cfi, mod);
if (ret)
goto err;
- /* rewrite the callers to target func()-16 */
+ /* rewrite the callers to target func()-FINEIBT_INSN_OFFSET */
ret = cfi_rewrite_callers(start_retpoline, end_retpoline, mod);
if (ret)
goto err;
@@ -1381,6 +1390,8 @@ static void poison_cfi(void *addr, void *wr_addr)
{
switch (cfi_mode) {
case CFI_FINEIBT:
+ addr -= FINEIBT_INSN_OFFSET;
+ wr_addr -= FINEIBT_INSN_OFFSET;
/*
* __cfi_\func:
* osp nopl (%rax)
@@ -1394,6 +1405,8 @@ static void poison_cfi(void *addr, void *wr_addr)
break;
case CFI_KCFI:
+ addr -= CFI_INSN_OFFSET;
+ wr_addr -= CFI_INSN_OFFSET;
/*
* __cfi_\func:
* movl $0, %eax
@@ -414,6 +414,12 @@ static void emit_nops(u8 **pprog, int len)
static void emit_fineibt(u8 **pprog, u32 hash)
{
u8 *prog = *pprog;
+#ifdef CONFIG_CALL_PADDING
+ int i;
+
+ for (i = 0; i < CFI_INSN_OFFSET - 16; i++)
+ EMIT1(0x90);
+#endif
EMIT_ENDBR();
EMIT3_off32(0x41, 0x81, 0xea, hash); /* subl $hash, %r10d */
@@ -428,20 +434,14 @@ static void emit_fineibt(u8 **pprog, u32 hash)
static void emit_kcfi(u8 **pprog, u32 hash)
{
u8 *prog = *pprog;
+#ifdef CONFIG_CALL_PADDING
+ int i;
+#endif
EMIT1_off32(0xb8, hash); /* movl $hash, %eax */
#ifdef CONFIG_CALL_PADDING
- EMIT1(0x90);
- EMIT1(0x90);
- EMIT1(0x90);
- EMIT1(0x90);
- EMIT1(0x90);
- EMIT1(0x90);
- EMIT1(0x90);
- EMIT1(0x90);
- EMIT1(0x90);
- EMIT1(0x90);
- EMIT1(0x90);
+ for (i = 0; i < CFI_INSN_OFFSET - 5; i++)
+ EMIT1(0x90);
#endif
EMIT_ENDBR();
For now, the layout of cfi and fineibt is hard coded, and the padding is fixed on 16 bytes. Factor out FINEIBT_INSN_OFFSET and CFI_INSN_OFFSET. CFI_INSN_OFFSET is the offset of cfi, which is the same as FUNCTION_ALIGNMENT when CALL_PADDING is enabled. And FINEIBT_INSN_OFFSET is the offset where we put the fineibt preamble on, which is 16 for now. When the FUNCTION_ALIGNMENT is bigger than 16, we place the fineibt preamble on the last 16 bytes of the padding for better performance, which means the fineibt preamble don't use the space that cfi uses. Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn> --- arch/x86/include/asm/cfi.h | 12 ++++++++---- arch/x86/kernel/alternative.c | 27 ++++++++++++++++++++------- arch/x86/net/bpf_jit_comp.c | 22 +++++++++++----------- 3 files changed, 39 insertions(+), 22 deletions(-)