Compare
7cbaa0f4579f48e92639c6abb761fdef8fbade47..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
5 files changed,
+40
−69
- arch/x86/kernel/cfi.c
- include/linux/futex.h
- include/linux/futex_types.h
- include/linux/irq-entry-common.h
- kernel/futex/core.c
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 include/linux/futex.h
| @@ -23,32 +23,28 @@ struct task_struct; | |||
| 23 | 23 | * 01 : Shared futex (PTHREAD_PROCESS_SHARED) | |
| 24 | 24 | * mapped on a file (reference on the underlying inode) | |
| 25 | 25 | * 10 : Shared futex (PTHREAD_PROCESS_SHARED) | |
| 26 | - | * (but private mapping on an mm) | |
| 26 | + | * (but private mapping on an mm, and reference taken on it) | |
| 27 | 27 | */ | |
| 28 | 28 | ||
| 29 | - | #define FUT_OFF_INODE 1 /* We set bit 0 if shared key identifies an inode */ | |
| 30 | - | #define FUT_OFF_MMSHARED 2 /* We set bit 1 if shared key identifies an mm */ | |
| 29 | + | #define FUT_OFF_INODE 1 /* We set bit 0 if key has a reference on inode */ | |
| 30 | + | #define FUT_OFF_MMSHARED 2 /* We set bit 1 if key has a reference on mm */ | |
| 31 | 31 | ||
| 32 | 32 | union futex_key { | |
| 33 | - | /* For FUT_OFF_INODE */ | |
| 34 | 33 | struct { | |
| 35 | 34 | u64 i_seq; | |
| 36 | 35 | unsigned long pgoff; | |
| 37 | 36 | unsigned int offset; | |
| 38 | 37 | /* unsigned int node; */ | |
| 39 | 38 | } shared; | |
| 40 | - | ||
| 41 | - | /* For FUT_OFF_MMSHARED or private */ | |
| 42 | 39 | struct { | |
| 43 | 40 | union { | |
| 44 | - | struct mm_struct *mm; /* for private */ | |
| 45 | - | u64 mm_seq; /* for FUT_OFF_MMSHARED */ | |
| 41 | + | struct mm_struct *mm; | |
| 42 | + | u64 __tmp; | |
| 46 | 43 | }; | |
| 47 | 44 | unsigned long address; | |
| 48 | 45 | unsigned int offset; | |
| 49 | 46 | /* unsigned int node; */ | |
| 50 | 47 | } private; | |
| 51 | - | ||
| 52 | 48 | struct { | |
| 53 | 49 | u64 ptr; | |
| 54 | 50 | unsigned long word; | |
| @@ -156,10 +152,11 @@ static inline void futex_set_vdso_cs_range(struct futex_mm_data *fd, unsigned in | |||
| 156 | 152 | static inline void futex_fixup_robust_unlock(struct pt_regs *regs) { } | |
| 157 | 153 | #endif /* !CONFIG_FUTEX_ROBUST_UNLOCK */ | |
| 158 | 154 | ||
| 159 | - | #ifdef CONFIG_FUTEX | |
| 155 | + | ||
| 156 | + | #if defined(CONFIG_FUTEX_PRIVATE_HASH) || defined(CONFIG_FUTEX_ROBUST_UNLOCK) | |
| 160 | 157 | void futex_mm_init(struct mm_struct *mm); | |
| 161 | 158 | #else | |
| 162 | 159 | static inline void futex_mm_init(struct mm_struct *mm) { } | |
| 163 | - | #endif /* !CONFIG_FUTEX */ | |
| 160 | + | #endif | |
| 164 | 161 | ||
| 165 | 162 | #endif /* _LINUX_FUTEX_H */ | |
modified include/linux/futex_types.h
| @@ -85,12 +85,10 @@ struct futex_unlock_cs_ranges { }; | |||
| 85 | 85 | * struct futex_mm_data - Futex related per MM data | |
| 86 | 86 | * @phash: Futex private hash related data | |
| 87 | 87 | * @unlock: Futex unlock VDSO critical sections | |
| 88 | - | * @unique_id: Unique ID of the MM, for FUT_OFF_MMSHARED futexes | |
| 89 | 88 | */ | |
| 90 | 89 | struct futex_mm_data { | |
| 91 | 90 | struct futex_mm_phash phash; | |
| 92 | 91 | struct futex_unlock_cs_ranges unlock; | |
| 93 | - | atomic64_t unique_id; | |
| 94 | 92 | }; | |
| 95 | 93 | #else /* CONFIG_FUTEX */ | |
| 96 | 94 | struct futex_sched_data { }; | |
modified include/linux/irq-entry-common.h
| @@ -468,25 +468,6 @@ static inline void irqentry_exit_to_kernel_mode_preempt(struct pt_regs *regs, | |||
| 468 | 468 | irqentry_exit_cond_resched(); | |
| 469 | 469 | } | |
| 470 | 470 | ||
| 471 | - | /* | |
| 472 | - | * The deferred hrtimer rearm is the tail of the timer interrupt, moved to the | |
| 473 | - | * interrupt return path so that it runs after a possible reschedule. By now | |
| 474 | - | * irq_exit_rcu() has left lockdep's hardirq context. Re-enter it for the | |
| 475 | - | * rearm, so that the hrtimer base lock and the timekeeping sequence count are | |
| 476 | - | * not chained onto the locks held by the interrupted context, which would add | |
| 477 | - | * new lock chains for every lock stack an interrupt hits. | |
| 478 | - | * | |
| 479 | - | * The rearm on return to user mode needs no such annotation: no locks are | |
| 480 | - | * held there, so it uses the same lock chains as the rearm in hardirq | |
| 481 | - | * context. | |
| 482 | - | */ | |
| 483 | - | static __always_inline void irqentry_hrtimer_rearm_deferred(void) | |
| 484 | - | { | |
| 485 | - | lockdep_hardirq_enter(); | |
| 486 | - | hrtimer_rearm_deferred(); | |
| 487 | - | lockdep_hardirq_exit(); | |
| 488 | - | } | |
| 489 | - | ||
| 490 | 471 | /** | |
| 491 | 472 | * irqentry_exit_to_kernel_mode_after_preempt - Establish trace, lockdep and RCU state | |
| 492 | 473 | * @regs: Pointer to current's pt_regs | |
| @@ -510,7 +491,7 @@ irqentry_exit_to_kernel_mode_after_preempt(struct pt_regs *regs, irqentry_state_ | |||
| 510 | 491 | */ | |
| 511 | 492 | if (state.exit_rcu) { | |
| 512 | 493 | instrumentation_begin(); | |
| 513 | - | irqentry_hrtimer_rearm_deferred(); | |
| 494 | + | hrtimer_rearm_deferred(); | |
| 514 | 495 | /* Tell the tracer that IRET will enable interrupts */ | |
| 515 | 496 | trace_hardirqs_on_prepare(); | |
| 516 | 497 | lockdep_hardirqs_on_prepare(); | |
| @@ -521,7 +502,7 @@ irqentry_exit_to_kernel_mode_after_preempt(struct pt_regs *regs, irqentry_state_ | |||
| 521 | 502 | } | |
| 522 | 503 | ||
| 523 | 504 | instrumentation_begin(); | |
| 524 | - | irqentry_hrtimer_rearm_deferred(); | |
| 505 | + | hrtimer_rearm_deferred(); | |
| 525 | 506 | /* Covers both tracing and lockdep */ | |
| 526 | 507 | trace_hardirqs_on(); | |
| 527 | 508 | instrumentation_end(); | |
modified kernel/futex/core.c
| @@ -439,51 +439,45 @@ struct hrtimer_sleeper *futex_setup_timer(ktime_t *time, struct hrtimer_sleeper | |||
| 439 | 439 | } | |
| 440 | 440 | ||
| 441 | 441 | /* | |
| 442 | - | * Generate a machine wide unique identifier for this object (inode or mm). | |
| 442 | + | * Generate a machine wide unique identifier for this inode. | |
| 443 | 443 | * | |
| 444 | 444 | * This relies on u64 not wrapping in the life-time of the machine; which with | |
| 445 | 445 | * 1ns resolution means almost 585 years. | |
| 446 | + | * | |
| 447 | + | * This further relies on the fact that a well formed program will not unmap | |
| 448 | + | * the file while it has a (shared) futex waiting on it. This mapping will have | |
| 449 | + | * a file reference which pins the mount and inode. | |
| 450 | + | * | |
| 451 | + | * If for some reason an inode gets evicted and read back in again, it will get | |
| 452 | + | * a new sequence number and will _NOT_ match, even though it is the exact same | |
| 453 | + | * file. | |
| 454 | + | * | |
| 455 | + | * It is important that futex_match() will never have a false-positive, esp. | |
| 456 | + | * for PI futexes that can mess up the state. The above argues that false-negatives | |
| 457 | + | * are only possible for malformed programs. | |
| 446 | 458 | */ | |
| 447 | - | static u64 get_object_id(atomic64_t *object_seq) | |
| 459 | + | static u64 get_inode_sequence_number(struct inode *inode) | |
| 448 | 460 | { | |
| 449 | - | static atomic64_t last_assigned_seq; | |
| 461 | + | static atomic64_t i_seq; | |
| 450 | 462 | u64 old; | |
| 451 | 463 | ||
| 452 | - | /* Does the object already have a sequence number? */ | |
| 453 | - | old = atomic64_read(object_seq); | |
| 464 | + | /* Does the inode already have a sequence number? */ | |
| 465 | + | old = atomic64_read(&inode->i_sequence); | |
| 454 | 466 | if (likely(old)) | |
| 455 | 467 | return old; | |
| 456 | 468 | ||
| 457 | 469 | for (;;) { | |
| 458 | - | u64 new = atomic64_inc_return(&last_assigned_seq); | |
| 470 | + | u64 new = atomic64_inc_return(&i_seq); | |
| 459 | 471 | if (WARN_ON_ONCE(!new)) | |
| 460 | 472 | continue; | |
| 461 | 473 | ||
| 462 | 474 | old = 0; | |
| 463 | - | if (!atomic64_try_cmpxchg_relaxed(object_seq, &old, new)) | |
| 475 | + | if (!atomic64_try_cmpxchg_relaxed(&inode->i_sequence, &old, new)) | |
| 464 | 476 | return old; | |
| 465 | 477 | return new; | |
| 466 | 478 | } | |
| 467 | 479 | } | |
| 468 | 480 | ||
| 469 | - | /* | |
| 470 | - | * This relies on the fact that a well formed program will not unmap | |
| 471 | - | * the file while it has a (shared) futex waiting on it. This mapping will have | |
| 472 | - | * a file reference which pins the mount and inode. | |
| 473 | - | * | |
| 474 | - | * If for some reason an inode gets evicted and read back in again, it will get | |
| 475 | - | * a new sequence number and will _NOT_ match, even though it is the exact same | |
| 476 | - | * file. | |
| 477 | - | * | |
| 478 | - | * It is important that futex_match() will never have a false-positive, esp. | |
| 479 | - | * for PI futexes that can mess up the state. The above argues that false-negatives | |
| 480 | - | * are only possible for malformed programs. | |
| 481 | - | */ | |
| 482 | - | static u64 get_inode_sequence_number(struct inode *inode) | |
| 483 | - | { | |
| 484 | - | return get_object_id(&inode->i_sequence); | |
| 485 | - | } | |
| 486 | - | ||
| 487 | 481 | /** | |
| 488 | 482 | * get_futex_key() - Get parameters which are the keys for a futex | |
| 489 | 483 | * @uaddr: virtual address of the futex | |
| @@ -689,8 +683,8 @@ again: | |||
| 689 | 683 | goto out; | |
| 690 | 684 | } | |
| 691 | 685 | ||
| 692 | - | key->both.offset |= FUT_OFF_MMSHARED; | |
| 693 | - | key->private.mm_seq = get_object_id(&mm->futex.unique_id); | |
| 686 | + | key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */ | |
| 687 | + | key->private.mm = mm; | |
| 694 | 688 | key->private.address = address; | |
| 695 | 689 | ||
| 696 | 690 | } else { | |
| @@ -2061,12 +2055,13 @@ static void futex_robust_unlock_init_mm(struct futex_mm_data *fd) | |||
| 2061 | 2055 | static inline void futex_robust_unlock_init_mm(struct futex_mm_data *fd) { } | |
| 2062 | 2056 | #endif /* !CONFIG_FUTEX_ROBUST_UNLOCK */ | |
| 2063 | 2057 | ||
| 2058 | + | #if defined(CONFIG_FUTEX_PRIVATE_HASH) || defined(CONFIG_FUTEX_ROBUST_UNLOCK) | |
| 2064 | 2059 | void futex_mm_init(struct mm_struct *mm) | |
| 2065 | 2060 | { | |
| 2066 | - | atomic64_set(&mm->futex.unique_id, 0); | |
| 2067 | 2061 | futex_hash_init_mm(&mm->futex); | |
| 2068 | 2062 | futex_robust_unlock_init_mm(&mm->futex); | |
| 2069 | 2063 | } | |
| 2064 | + | #endif | |
| 2070 | 2065 | ||
| 2071 | 2066 | int futex_hash_prctl(unsigned long arg2, unsigned long arg3, unsigned long arg4) | |
| 2072 | 2067 | { | |