[PATCH 6/8] kprobes: Do not overwrite jump instruction when re-arming unoptimizing probe

From: Masami Hiramatsu (Google)

Date: Tue Nov 03 2026 - 19:04:46 EST


From: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>

When an optimized probe is temporarily disarmed or disabled,
unoptimize_kprobe() places it on the unoptimizing list, but leaves
the relative jump in text until the optimizer thread runs.
__disarm_kprobe() explicitly skips arch_disarm_kprobe() when
kprobe_queued_unopt(p) is true.

However, if __arm_kprobe() is called before the unoptimization pass
executes, it unconditionally called arch_arm_kprobe(p), writing a
breakpoint instruction over the first byte of the jump while leaving the
trailing 4 bytes intact. optimize_kprobe(p) then dequeued the probe from
the unoptimizing queue, leaving the jump opcode overwritten and corrupted
in text.

Skip calling arch_arm_kprobe() in __arm_kprobe() when
kprobe_queued_unopt(p) is true, keeping the existing relative jump intact
while unoptimization is cancelled. If optimize_kprobe() cannot optimize
the probe (e.g. due to a post_handler or disabled optimization), force
unoptimization immediately. Also, in reuse_unused_kprobe(), do not
prematurely clear KPROBE_FLAG_DISABLED or call optimize_kprobe(), leaving
arming and optimization to register_aggr_kprobe().

Fixes: 06e0d3b89da1 ("kprobes: Make optprobe optimizer multi-generational and asynchronous")
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
---
kernel/kprobes.c | 39 +++++++++++++++++----------------------
1 file changed, 17 insertions(+), 22 deletions(-)

diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index ceacd56245c2..0ef3a8701eda 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -1059,19 +1059,11 @@ static int reuse_unused_kprobe(struct kprobe *ap)
{
struct optimized_kprobe *op;

- /*
- * Unused kprobe MUST be on the way of delayed unoptimizing (means
- * there is still a relative jump) and disabled.
- */
op = container_of(ap, struct optimized_kprobe, kp);
WARN_ON_ONCE(list_empty(&op->list));
- /* Enable the probe again */
- ap->flags &= ~KPROBE_FLAG_DISABLED;
- /* Optimize it again. (remove from 'op->list') */
if (!kprobe_optready(ap))
return -EINVAL;

- optimize_kprobe(ap);
return 0;
}

@@ -1262,6 +1254,19 @@ static void __init kprobe_sysctls_init(void)
}
#endif /* CONFIG_SYSCTL */

+/* Return true if the probe is queued on unoptimizing lists */
+static bool kprobe_queued_unopt(struct kprobe *p)
+{
+ struct optimized_kprobe *op;
+
+ if (kprobe_aggrprobe(p)) {
+ op = container_of(p, struct optimized_kprobe, kp);
+ if (optprobe_queued_unopt(op))
+ return true;
+ }
+ return false;
+}
+
/* Put a breakpoint for a probe. */
static void __arm_kprobe(struct kprobe *p)
{
@@ -1275,21 +1280,11 @@ static void __arm_kprobe(struct kprobe *p)
/* Fallback to unoptimized kprobe */
unoptimize_kprobe(_p, true);

- arch_arm_kprobe(p);
+ if (!kprobe_queued_unopt(p))
+ arch_arm_kprobe(p);
optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
-}
-
-/* Return true if the probe is queued on unoptimizing lists */
-static bool kprobe_queued_unopt(struct kprobe *p)
-{
- struct optimized_kprobe *op;
-
- if (kprobe_aggrprobe(p)) {
- op = container_of(p, struct optimized_kprobe, kp);
- if (optprobe_queued_unopt(op))
- return true;
- }
- return false;
+ if (unlikely(kprobe_queued_unopt(p)))
+ unoptimize_kprobe(p, true);
}

/* Remove the breakpoint of a probe. */