* Re: [PATCH] x86/kprobes: Fix boundary check in insn_jump_into_range()
2026-11-19 19:41 ` Dave Hansen
@ 2026-11-20 0:36 ` Jinke Han
2026-11-20 0:41 ` Dave Hansen
0 siblings, 1 reply; 4+ messages in thread
From: Jinke Han @ 2026-11-20 0:36 UTC (permalink / raw)
To: Dave Hansen
Cc: linux-kernel, tglx, mingo, bp, dave.hansen, x86, hpa,
nik.borisov, rppt, peterz, mhiramat
On 11/19/26 11:41, Dave Hansen wrote:
> What is the end user visible effect of this issue, and of fixing it?
There is no correctness impact: before this change the probe simply stays
unoptimized and falls back to the int3-based path, which is functionally
correct but pays trap overhead on every hit. So the user-visible effect is
limited to probe overhead, and only for probe points that hit this false
positive.
I do have a concrete in-tree example, on queued_spin_lock_slowpath+17.
Original code:
crash> dis queued_spin_lock_slowpath
0xffffffff823ebdf0 <queued_spin_lock_slowpath>: endbr64
0xffffffff823ebdf4 <queued_spin_lock_slowpath+4>: push %r14
0xffffffff823ebdf6 <queued_spin_lock_slowpath+6>: push %r13
0xffffffff823ebdf8 <queued_spin_lock_slowpath+8>: push %r12
0xffffffff823ebdfa <queued_spin_lock_slowpath+10>: push %rbp
0xffffffff823ebdfb <queued_spin_lock_slowpath+11>: mov %rdi,%rbp
0xffffffff823ebdfe <queued_spin_lock_slowpath+14>: push %rbx
0xffffffff823ebdff <queued_spin_lock_slowpath+15>: xchg %ax,%ax
0xffffffff823ebe01 <queued_spin_lock_slowpath+17>: mov $0x1,%edx
0xffffffff823ebe06 <queued_spin_lock_slowpath+22>: mov 0x0(%rbp),%eax
0xffffffff823ebe09 <queued_spin_lock_slowpath+25>: test %eax,%eax
0xffffffff823ebe0b <queued_spin_lock_slowpath+27>: jne 0xffffffff823ebe7d <queued_spin_lock_slowpath+141>
0xffffffff823ebe0d <queued_spin_lock_slowpath+29>: lock cmpxchg %edx,0x0(%rbp)
Further down in the same function there is a back edge that jumps to +22:
0xffffffff823ebe7f <queued_spin_lock_slowpath+143>: jmp 0xffffffff823ebe06 <queued_spin_lock_slowpath+22>
0xffffffff823ebe81 <queued_spin_lock_slowpath+145>: mov (%rdi),%esi
0xffffffff823ebe83 <queued_spin_lock_slowpath+147>: mov $0x200,%eax
0xffffffff823ebe88 <queued_spin_lock_slowpath+152>: cmp $0x100,%esi
With paddr = queued_spin_lock_slowpath+17, the probed instruction
"mov $0x1,%edx" is exactly 5 bytes, so copy_optimized_instructions()
copies exactly JMP32_INSN_SIZE bytes and the overwritten region is
[paddr, paddr+5) = [+17, +22). The back edge at +143 therefore targets
paddr + JMP32_INSN_SIZE, i.e. exactly the address the detour buffer
returns to after executing the copied instruction.
can_optimize() checks
[paddr + INT3_INSN_SIZE, paddr + INT3_INSN_SIZE + DISP32_SIZE]
which is [+18, +22] with an inclusive upper bound, so the +143 jump was
counted as jumping into the optimized region and the probe was left
unoptimized:
crash> dis queued_spin_lock_slowpath
0xffffffff823ebdf0 <queued_spin_lock_slowpath>: endbr64
0xffffffff823ebdf4 <queued_spin_lock_slowpath+4>: push %r14
0xffffffff823ebdf6 <queued_spin_lock_slowpath+6>: push %r13
0xffffffff823ebdf8 <queued_spin_lock_slowpath+8>: push %r12
0xffffffff823ebdfa <queued_spin_lock_slowpath+10>: push %rbp
0xffffffff823ebdfb <queued_spin_lock_slowpath+11>: mov %rdi,%rbp
0xffffffff823ebdfe <queued_spin_lock_slowpath+14>: push %rbx
0xffffffff823ebdff <queued_spin_lock_slowpath+15>: xchg %ax,%ax
0xffffffff823ebe01 <queued_spin_lock_slowpath+17>: int3
0xffffffff823ebe02 <queued_spin_lock_slowpath+18>: add %eax,(%rax)
0xffffffff823ebe04 <queued_spin_lock_slowpath+20>: add %al,(%rax)
0xffffffff823ebe06 <queued_spin_lock_slowpath+22>: mov 0x0(%rbp),%eax
0xffffffff823ebe09 <queued_spin_lock_slowpath+25>: test %eax,%eax
0xffffffff823ebe0b <queued_spin_lock_slowpath+27>: jne 0xffffffff823ebe7d <queued_spin_lock_slowpath+141>
0xffffffff823ebe0d <queued_spin_lock_slowpath+29>: lock cmpxchg %edx,0x0(%rbp)
(The +18/+20 "add" instructions are just the remaining 4 bytes of the
5-byte "mov $0x1,%edx" being misdecoded after the int3 overwrite.)
With the fix the checked range becomes half-open [+18, +22), the +143
target is no longer rejected, and the probe is optimized:
crash> dis queued_spin_lock_slowpath
0xffffffff823ebdf0 <queued_spin_lock_slowpath>: endbr64
0xffffffff823ebdf4 <queued_spin_lock_slowpath+4>: push %r14
0xffffffff823ebdf6 <queued_spin_lock_slowpath+6>: push %r13
0xffffffff823ebdf8 <queued_spin_lock_slowpath+8>: push %r12
0xffffffff823ebdfa <queued_spin_lock_slowpath+10>: push %rbp
0xffffffff823ebdfb <queued_spin_lock_slowpath+11>: mov %rdi,%rbp
0xffffffff823ebdfe <queued_spin_lock_slowpath+14>: push %rbx
0xffffffff823ebdff <queued_spin_lock_slowpath+15>: xchg %ax,%ax
0xffffffff823ebe01 <queued_spin_lock_slowpath+17>: jmp 0xffffffffc0401000
0xffffffff823ebe06 <queued_spin_lock_slowpath+22>: mov 0x0(%rbp),%eax
0xffffffff823ebe09 <queued_spin_lock_slowpath+25>: test %eax,%eax
0xffffffff823ebe0b <queued_spin_lock_slowpath+27>: jne 0xffffffff823ebe7d <queued_spin_lock_slowpath+141>
0xffffffff823ebe0d <queued_spin_lock_slowpath+29>: lock cmpxchg %edx,0x0(%rbp)
So the effect is: the same probe point now uses an optimized jump into
the detour buffer instead of an int3 trap, which is measurable under
lock contention since queued_spin_lock_slowpath is hot. Semantics are
unchanged; the +143 back edge bypasses the probe point in both cases, so
the hit count is the same.
Regarding the generality of the false positive: it needs a jump whose
target is exactly paddr + JMP32_INSN_SIZE while the copied length is
also exactly JMP32_INSN_SIZE. When the copied length is larger,
paddr + JMP32_INSN_SIZE lands inside an instruction and cannot be a real
jump target, so allowing it does not let a jump into the overwritten
region slip through.
Since there is no reported failure, I'm fine with dropping the Fixes:
tag if you'd prefer to treat this as a cleanup. I can also post a
separate change that makes the check use the actual optimized length
instead of the open-coded DISP32_SIZE, if you think that's the better
direction, and add a regression case under
tools/testing/selftests/kprobes.
Thanks,
Jinke Han
^ permalink raw reply [flat|nested] 4+ messages in thread