mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] x86/kprobes: Fix boundary check in insn_jump_into_range()
@ 2026-11-17 12:25 Jinke Han
  2026-11-19 19:41 ` Dave Hansen
  0 siblings, 1 reply; 5+ messages in thread
From: Jinke Han @ 2026-11-17 12:25 UTC (permalink / raw)
  To: linux-kernel, tglx, mingo, bp, dave.hansen, x86, hpa,
	nik.borisov, rppt, peterz, mhiramat, jinkehan

The range checked by insn_jump_into_range() is half-open:
[start, start + len). However, the current check treats the upper
bound as inclusive, so a jump target at `probe address +
JMP32_INSN_SIZE` is incorrectly considered to jump into the
optimized region.

A valid jump target must be an instruction boundary. A target at
`probe address + JMP32_INSN_SIZE` lies just outside the overwritten
region, so allow the jmp optimization in this case.

Fixes: c0f7ac3a9edde ("kprobes/x86: Support kprobes jump optimization on x86")
Signed-off-by: Jinke Han <jinkehan@didiglobal.com>
---
 arch/x86/kernel/kprobes/opt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kernel/kprobes/opt.c b/arch/x86/kernel/kprobes/opt.c
index 3f8fea52619f..a920ecb99381 100644
--- a/arch/x86/kernel/kprobes/opt.c
+++ b/arch/x86/kernel/kprobes/opt.c
@@ -252,7 +252,7 @@ static int insn_jump_into_range(struct insn *insn, unsigned long start, int len)
 	}
 	target = (unsigned long)insn->next_byte + insn->immediate.value;
 
-	return (start <= target && target <= start + len);
+	return (start <= target && target < start + len);
 }
 
 /* Decode whole function to ensure any instructions don't jump into target */
-- 
2.34.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] x86/kprobes: Fix boundary check in insn_jump_into_range()
  2026-11-17 12:25 [PATCH] x86/kprobes: Fix boundary check in insn_jump_into_range() Jinke Han
@ 2026-11-19 19:41 ` Dave Hansen
  2026-11-20  0:36   ` Jinke Han
  0 siblings, 1 reply; 5+ messages in thread
From: Dave Hansen @ 2026-11-19 19:41 UTC (permalink / raw)
  To: linux-kernel, tglx, mingo, bp, dave.hansen, x86, hpa,
	nik.borisov, rppt, peterz, mhiramat

On 11/17/26 04:25, Jinke Han wrote:
> The range checked by insn_jump_into_range() is half-open:
> [start, start + len). However, the current check treats the upper
> bound as inclusive, so a jump target at `probe address +
> JMP32_INSN_SIZE` is incorrectly considered to jump into the
> optimized region.
> 
> A valid jump target must be an instruction boundary. A target at
> `probe address + JMP32_INSN_SIZE` lies just outside the overwritten
> region, so allow the jmp optimization in this case.

What is the end user visible effect of this issue, and of fixing it?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* 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; 5+ 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] 5+ messages in thread

* Re: [PATCH] x86/kprobes: Fix boundary check in insn_jump_into_range()
  2026-11-20  0:36   ` Jinke Han
@ 2026-11-20  0:41     ` Dave Hansen
  2026-11-20  2:23       ` Jinke Han
  0 siblings, 1 reply; 5+ messages in thread
From: Dave Hansen @ 2026-11-20  0:41 UTC (permalink / raw)
  To: linux-kernel, tglx, mingo, bp, dave.hansen, x86, hpa,
	nik.borisov, rppt, peterz, mhiramat

On 11/19/26 16:36, Jinke Han wrote:
> 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.

The Fixes: is fine, but it would be nice to have a better changelog
about what's going on.

Not voluminous, just better, please.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] x86/kprobes: Fix boundary check in insn_jump_into_range()
  2026-11-20  0:41     ` Dave Hansen
@ 2026-11-20  2:23       ` Jinke Han
  0 siblings, 0 replies; 5+ messages in thread
From: Jinke Han @ 2026-11-20  2:23 UTC (permalink / raw)
  To: Dave Hansen
  Cc: linux-kernel, tglx, mingo, bp, dave.hansen, x86, hpa,
	nik.borisov, rppt, peterz, mhiramat

> On Thu, Nov 19, 2026 at 04:41:04PM -0800, Dave Hansen wrote:
> > On 11/19/26 16:36, Jinke Han wrote:
> >> 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.
> > 
> > The Fixes: is fine, but it would be nice to have a better changelog
> > about what's going on.
> > 
> > Not voluminous, just better, please.

I'll send a v2 to improve the changelog.

Thanks,
Jinke Han

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-20  2:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 12:25 [PATCH] x86/kprobes: Fix boundary check in insn_jump_into_range() Jinke Han
2026-11-19 19:41 ` Dave Hansen
2026-11-20  0:36   ` Jinke Han
2026-11-20  0:41     ` Dave Hansen
2026-11-20  2:23       ` Jinke Han

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®