[PATCH v2 0/4] kprobes: Fixes for ftrace ops and optprobe state machine

From: Masami Hiramatsu (Google)

Date: Mon Nov 09 2026 - 20:36:51 EST


Hi,

Here is v2 of the remaining bug fixes for kprobes and optprobes, rebased
on the latest probes/fixes branch (which already includes patches 1-3 from
v1 and Mani Tofigh's optprobe state fixes that superseded patch 6 from v1).
The previous version is here:

https://lore.kernel.org/all/179375029807.247640.13024754173032838618.stgit@devnote2/

This includes following fixes.

1. Ftrace Ops Switching for Post-Handlers (Patch 1):
- When adding a second probe with a post_handler to an existing ftrace
kprobe, the aggregate probe's post_handler was set but remained armed
on kprobe_ftrace_ops. Subsequent disarming attempted to disarm from
kprobe_ipmodify_ops, failing with -ENOENT and leaking the armed probe.
Switch ftrace ops safely in register_aggr_kprobe() outside of
text_mutex and cpus_read_lock.

2. Optprobe State Machine & Tasks RCU Quiescence (Patches 2-3):
- Forcibly unoptimized probes (unoptimize_kprobe(force = true)) were
not queued to cooling_list, causing kprobe_disarmed() to return true
prematurely and risking use-after-free in the optinsn detour buffer
before Tasks RCU completed.
- When a disabled cooling probe finishes cooling and transitions to
safely disarmed in optprobe_finalize_generation(), re-optimize any
blocked sibling probe whose jump range covers its address.

3. KUnit Regression Tests (Patch 4):
- Add KUnit test cases to lib/tests/test_kprobes.c covering the recent
kprobes, kretprobes, ftrace ops, and optprobe fixes.

Changes in v2:
- Rebased on the latest probes/fixes branch (v1 patches 1-3 merged;
v1 patch 6 superseded by Mani Tofigh's optprobe fixes).
[01/04]
- Defer add_new_kprobe() until after ftrace_switch_ops completes so
that p is not visible on ap->list if switching ftrace ops fails.
- Set KPROBE_FLAG_DISABLED on ap if rollback arm_kprobe_ftrace() fails.
[02/04]
- Drop the unreachable kprobe_unused(&op->kp) branch when force is true
and list_empty(&op->list) in unoptimize_kprobe().
[03/04]
- Drop the unoptimize_kprobe() and reuse_unused_kprobe() hunks which
were superseded by commits 0be7a931c5e4 and 1ffd6efb1b58 in
probes/fixes.
- Drop the kill_optimized_kprobe() cooling deferral since kill_kprobe()
runs at MODULE_STATE_GOING when no tasks execute the module text.
[04/04]
- Probe a notrace target function (kprobe_opt_target) in
test_optprobe_rearm_unoptimizing() so that jump optimization is tested
even when CONFIG_KPROBES_ON_FTRACE=y.
- Guard test_optprobe_rearm_unoptimizing() with
!IS_MODULE(CONFIG_KPROBES_SANITY_TEST) since
wait_for_kprobe_optimizer() is not exported to modules.
- Replace KUNIT_ASSERT_EQ() with KUNIT_EXPECT_EQ() while stack-allocated
probes are registered to prevent stack use-after-free on test failure.

Thank you,

---
base-commit: 1ffd6efb1b58e596ed70f660182803264a4c84d9

Masami Hiramatsu (Google) (4):
kprobes: Switch ftrace ops when adding probe with post_handler
kprobes: Fix UAF and queue cooling probes on forced unoptimization
kprobes: Re-optimize blocked sibling probe when cooling finishes
kprobes: Add KUnit test cases for kprobes regressions


kernel/kprobes.c | 85 ++++++++++++++++++++++-------
lib/tests/test_kprobes.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 200 insertions(+), 21 deletions(-)

--
Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>