Compare

89bab1dba8a0167ef0d819e0f55b6bafa038e87f..4505a262deae7df06e33617dfc104d5541eec91d

x86/kcfi: Decode the kCFI check from the trap address

Since commit b2250d63e6422 ("x86/kcfi: Optimize call sequence"), a
CONFIG_FINEIBT=y kernel running in kCFI mode rewrites each kCFI call
site at boot so that a failed check traps on the 0xd6 byte of
"test $0xd6, %al", one byte past where the compiler's "ud2" was.
handle_cfi_failure() steps "addr" back one byte to find the trap in
.kcfi_traps, but decode_cfi_insn() still decodes from regs->ip, so it
reads each instruction starting at its second byte.
"movl $-<id>, %r10d" then decodes as "movl $-<id>, %edx", so the
reported type is right by accident, but "addl -15(%r11), %r10d" decodes
as "addl -15(%rbx), %edx", so the reported target is whatever %rbx holds:

  CFI failure at lkdtm_indirect_call+0x16/0x20 (target: 0x12; expected type: 0xc79a2d1b)

Pass the adjusted address to decode_cfi_insn() and decode from it.

Tested ARCH=x86_64 defconfig with CONFIG_CFI=y and CONFIG_LKDTM=y under
QEMU, with LKDTM's CFI_FORWARD_PROTO and both Clang 24.0.0git and GCC
17.0.0 (experimental, with the kCFI series). With CONFIG_FINEIBT=y in
kCFI mode, the report gives %rbx as the target without this change, and
the expected lkdtm_increment_int() with it. With cfi=fineibt, with
cfi=fineibt,paranoid, and with CONFIG_FINEIBT=n, it gives
lkdtm_increment_int() both with and without this change.

Fixes: b2250d63e6422 ("x86/kcfi: Optimize call sequence")
Assisted-by: LLM
Signed-off-by: Kees Cook <kees@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/20261012090436.1676525-1-kees@kernel.org

Kees Cook <kees@kernel.org> · 2026-10-12 09:04 UTC · committed by Peter Zijlstra

Browse files
6 files changed, +21 −22
modified arch/x86/kernel/cfi.c
@@ -10,11 +10,11 @@
10 10 #include <asm/insn-eval.h>
11 11
12 12 /*
13 - * Returns the target address and the expected type when regs->ip points
13 + * Returns the target address and the expected type when addr points
14 14 * to a compiler-generated CFI trap.
15 15 */
16 - static bool decode_cfi_insn(struct pt_regs *regs, unsigned long *target,
17 - u32 *type)
16 + static bool decode_cfi_insn(struct pt_regs *regs, unsigned long addr,
17 + unsigned long *target, u32 *type)
18 18 {
19 19 char buffer[MAX_INSN_SIZE];
20 20 struct insn insn;
@@ -29,13 +29,13 @@ static bool decode_cfi_insn(struct pt_regs *regs, unsigned long *target,
29 29 *   movl -<id>, %r10d ; 6 bytes
30 30 * addl -<pos>(%reg), %r10d; 4 bytes
31 31 * je .Ltmp1 ; 2 bytes
32 - * ud2 ; <- regs->ip
32 + * ud2 ; <- addr
33 33 * .Ltmp1:
34 34 *
35 35 * We can decode the expected type and the target address from the
36 36 * movl/addl instructions.
37 37 */
38 - if (copy_from_kernel_nofault(buffer, (void *)regs->ip - 12, MAX_INSN_SIZE))
38 + if (copy_from_kernel_nofault(buffer, (void *)addr - 12, MAX_INSN_SIZE))
39 39 return false;
40 40 if (insn_decode_kernel(&insn, &buffer[offset]))
41 41 return false;
@@ -44,7 +44,7 @@ static bool decode_cfi_insn(struct pt_regs *regs, unsigned long *target,
44 44
45 45 *type = -(u32)insn.immediate.value;
46 46
47 - if (copy_from_kernel_nofault(buffer, (void *)regs->ip - 6, MAX_INSN_SIZE))
47 + if (copy_from_kernel_nofault(buffer, (void *)addr - 6, MAX_INSN_SIZE))
48 48 return false;
49 49 if (insn_decode_kernel(&insn, &buffer[offset]))
50 50 return false;
@@ -82,7 +82,7 @@ enum bug_trap_type handle_cfi_failure(struct pt_regs *regs)
82 82 return BUG_TRAP_TYPE_NONE;
83 83 }
84 84
85 - if (!decode_cfi_insn(regs, &target, &type))
85 + if (!decode_cfi_insn(regs, addr, &target, &type))
86 86 return report_cfi_failure_noaddr(regs, addr);
87 87
88 88 break;
modified kernel/trace/Makefile
@@ -146,12 +146,11 @@ KASAN_SANITIZE_undefsyms_base.o := y
146 146
147 147 UNDEFINED_ALLOWLIST = __asan __gcov __kasan __kcsan __hwasan __sancov __sanitizer __tsan __ubsan __msan \
148 148 __aeabi_unwind_cpp __s390_indirect_jump __x86_indirect_thunk simple_ring_buffer \
149 - current_thread_info_reg \
150 - $(shell $(NM) -u $(obj)/undefsyms_base.o 2>/dev/null | awk '$$2 {print $$2}')
149 + $(shell $(NM) -u $(obj)/undefsyms_base.o 2>/dev/null | awk '{print $$2}')
151 150
152 151 quiet_cmd_check_undefined = NM $<
153 152 cmd_check_undefined = \
154 - undefsyms=$$($(NM) -u $< | awk '$$2 {print $$2}' | grep -v $(addprefix -e , $(UNDEFINED_ALLOWLIST)) || true); \
153 + undefsyms=$$($(NM) -u $< | grep -v $(addprefix -e , $(UNDEFINED_ALLOWLIST)) || true); \
155 154 if [ -n "$$undefsyms" ]; then \
156 155 echo "Unexpected symbols in $<:" >&2; \
157 156 echo "$$undefsyms" >&2; \
modified kernel/trace/ring_buffer.c
@@ -2380,9 +2380,6 @@ int ring_buffer_meta_seq_init(struct file *file, struct trace_buffer *buffer, in
2380 2380 struct seq_file *m;
2381 2381 int ret;
2382 2382
2383 - if (!cpumask_test_cpu(cpu, buffer->cpumask))
2384 - return -ENODEV;
2385 -
2386 2383 ret = seq_open(file, &rb_meta_seq_ops);
2387 2384 if (ret)
2388 2385 return ret;
@@ -3630,8 +3627,10 @@ static void rb_inc_iter(struct ring_buffer_iter *iter)
3630 3627 /* Return the index into the sub-buffers for a given sub-buffer */
3631 3628 static int rb_meta_subbuf_idx(struct ring_buffer_cpu_meta *meta, void *subbuf)
3632 3629 {
3633 - void *subbuf_array = rb_subbufs_from_meta(meta);
3630 + void *subbuf_array;
3634 3631
3632 + subbuf_array = (void *)meta + sizeof(int) * meta->nr_subbufs;
3633 + subbuf_array = (void *)ALIGN((unsigned long)subbuf_array, meta->subbuf_size);
3635 3634 return (subbuf - subbuf_array) / meta->subbuf_size;
3636 3635 }
3637 3636
@@ -7998,8 +7997,12 @@ consume:
7998 7997 goto out;
7999 7998 }
8000 7999
8000 + /* Did the reader catch up with the writer? */
8001 + if (cpu_buffer->reader_page == cpu_buffer->commit_page)
8002 + goto out;
8003 +
8001 8004 reader = rb_get_reader_page(cpu_buffer);
8002 - if (!reader)
8005 + if (WARN_ON(!reader))
8003 8006 goto out;
8004 8007
8005 8008 /* Check if any events were dropped */
modified kernel/trace/trace.c
@@ -5881,7 +5881,7 @@ static int tracing_buffer_meta_open(struct inode *inode, struct file *filp)
5881 5881
5882 5882 ret = ring_buffer_meta_seq_init(filp, tr->array_buffer.buffer, cpu);
5883 5883 if (ret < 0)
5884 - trace_array_put(tr);
5884 + __trace_array_put(tr);
5885 5885 return ret;
5886 5886 }
5887 5887
modified kernel/trace/trace_events_hist.c
@@ -289,7 +289,7 @@ static u64 hist_field_log2(struct hist_field *hist_field,
289 289
290 290 u64 val = hist_fn_call(operand, elt, buffer, rbe, event);
291 291
292 - return order_base_2(val);
292 + return (u64) ilog2(roundup_pow_of_two(val));
293 293 }
294 294
295 295 static u64 hist_field_bucket(struct hist_field *hist_field,
@@ -3090,7 +3090,7 @@ create_field_var_hist(struct hist_trigger_data *target_hist_data,
3090 3090 key_field = hist_data->fields[i];
3091 3091 if (!first)
3092 3092 seq_buf_putc(&s, ',');
3093 - expr_field_str(key_field, &s);
3093 + seq_buf_puts(&s, key_field->field->name);
3094 3094 first = false;
3095 3095 }
3096 3096
modified kernel/trace/undefsyms_base.c
@@ -9,7 +9,6 @@
9 9
10 10 #include <linux/atomic.h>
11 11 #include <linux/string.h>
12 - #include <asm/local.h>
13 12 #include <asm/page.h>
14 13
15 14 void undefsyms_base(void *p, int n);
@@ -19,13 +18,11 @@ static char page[PAGE_SIZE] __aligned(PAGE_SIZE);
19 18 void undefsyms_base(void *p, int n)
20 19 {
21 20 char buffer[256] = { 0 };
22 - u32 u = 0;
23 21
22 + u32 u = 0;
24 23 memset((char * volatile)page, 8, PAGE_SIZE);
25 24 memset((char * volatile)buffer, 8, sizeof(buffer));
26 25 memcpy((void * volatile)p, buffer, sizeof(buffer));
27 26 cmpxchg((u32 * volatile)&u, 0, 8);
28 - cmpxchg((unsigned long *)p, 0, 8);
29 - local_set((local_t *)p, 8);
30 27 WARN_ON(n == 0xdeadbeef);
31 28 }