Compare

bd22f7e63509fa758cf500d9af3e88a3d3df96be..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
4 files changed, +12 −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 drivers/gpio/gpio-nct6694.c
@@ -444,7 +444,7 @@ static int nct6694_gpio_probe(struct platform_device *pdev)
444 444 data->gpio.set_config = nct6694_set_config;
445 445 data->gpio.init_valid_mask = nct6694_init_valid_mask;
446 446 data->gpio.base = -1;
447 - data->gpio.can_sleep = true;
447 + data->gpio.can_sleep = false;
448 448 data->gpio.owner = THIS_MODULE;
449 449 data->gpio.ngpio = NCT6694_NR_GPIO;
450 450
modified drivers/gpio/gpiolib-shared.c
@@ -450,6 +450,7 @@ int gpio_shared_add_proxy_lookup(struct device *consumer, struct fwnode_handle *
450 450 const char *con_id, unsigned long lflags)
451 451 {
452 452 const char *dev_id = dev_name(consumer);
453 + struct gpiod_lookup_table *lookup;
453 454 struct gpio_shared_entry *entry;
454 455 struct gpio_shared_ref *ref;
455 456
@@ -479,22 +480,18 @@ int gpio_shared_add_proxy_lookup(struct device *consumer, struct fwnode_handle *
479 480 if (!key)
480 481 return -ENOMEM;
481 482
482 - struct gpiod_lookup_table *lookup __free(kfree) =
483 - kzalloc_flex(*lookup, table, 2);
483 + lookup = kzalloc_flex(*lookup, table, 2);
484 484 if (!lookup)
485 485 return -ENOMEM;
486 486
487 487 pr_debug("Adding machine lookup entry for a shared GPIO for consumer %s, with key '%s' and con_id '%s'\n",
488 488 dev_id, key, ref->con_id ?: "none");
489 489
490 - lookup->dev_id = kstrdup(dev_id, GFP_KERNEL);
491 - if (!lookup->dev_id)
492 - return -ENOMEM;
493 -
490 + lookup->dev_id = dev_id;
494 491 lookup->table[0] = GPIO_LOOKUP(no_free_ptr(key), 0,
495 492 ref->con_id, lflags);
496 493
497 - ref->lookup = no_free_ptr(lookup);
494 + ref->lookup = lookup;
498 495 gpiod_add_lookup_table(ref->lookup);
499 496
500 497 return 0;
@@ -614,7 +611,6 @@ void gpio_device_teardown_shared(struct gpio_device *gdev)
614 611 if (ref->lookup) {
615 612 gpiod_remove_lookup_table(ref->lookup);
616 613 kfree(ref->lookup->table[0].key);
617 - kfree(ref->lookup->dev_id);
618 614 kfree(ref->lookup);
619 615 ref->lookup = NULL;
620 616 }
modified tools/gpio/Makefile
@@ -7,13 +7,7 @@ bindir ?= /usr/bin
7 7 # isn't set and when invoked from selftests build, where srctree
8 8 # is set to ".". building_out_of_srctree is undefined for in srctree
9 9 # builds
10 - ifeq ($(srctree),)
11 - update_srctree := 1
12 - endif
13 10 ifndef building_out_of_srctree
14 - update_srctree := 1
15 - endif
16 - ifeq ($(update_srctree),1)
17 11 srctree := $(patsubst %/,%,$(dir $(CURDIR)))
18 12 srctree := $(patsubst %/,%,$(dir $(srctree)))
19 13 endif