From: "Masami Hiramatsu (Google)" <mhiramat@kernel.org>
To: Steven Rostedt <rostedt@goodmis.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Naveen N Rao <naveen@kernel.org>,
"David S . Miller" <davem@davemloft.net>
Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org
Subject: [PATCH 0/8] kprobes: Fixes for registration errors, ftrace ops and optprobe state machine
Date: Wed, 4 Nov 2026 08:58:18 +0900 [thread overview]
Message-ID: <179375029807.247640.13024754173032838618.stgit@devnote2> (raw)
Hi,
Here is a series of bug fixes for kprobes and optprobes, addressing error
recovery, ftrace ops management, and optprobe asynchronous state machine
edge cases:
1. Registration & Unregistration Error Handling (Patches 1-3):
- In __register_kprobe(), when arm_kprobe() fails, the error code was
ignored and the instruction slot was leaked.
- In unregister_kretprobes(), failing __unregister_kprobe_top() still
invoked rethook/rph freeing, causing a double free or NULL deref.
Clearing rp->rh is also deferred until after synchronize_rcu() to
prevent racing with in-flight pre_handler_kretprobe() invocations.
- In register_kretprobe(), re-registration validation was skipped when
registered by symbol_name without an explicit addr, leading to
destruction of the live probe's rethook instance on failure.
2. Ftrace Ops Switching for Post-Handlers (Patch 4):
- 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.
3. Optprobe State Machine & Tasks RCU Quiescence (Patches 5-7):
- 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.
- Re-arming an unoptimizing probe before the unoptimization pass ran
overwrote the first byte of the relative jump with a breakpoint,
corrupting the jump opcode in text.
- Probes killed at module unload while child probes were still active
were prematurely freed or improperly unhashed before Tasks RCU
quiescence elapsed.
In addition to the bugfixes, the last patch adds KUnit test cases for
these bugs. (Patch 8)
Summary of patches in this series:
[01/08] kprobes: Fix error return and slot leak in __register_kprobe()
- Release allocated instruction slot via arch_remove_kprobe(p) and
return the arm_kprobe() error code upon failure.
[02/08] kprobes: Fix double free and NULL deref in unregister_kretprobes()
- Skip rethook_free() and rph cleanup if __unregister_kprobe_top()
fails, and clear rp->rh after synchronize_rcu().
[03/08] kprobes: Validate re-registration earlier in register_kretprobe()
- Resolve target address via _kprobe_addr() up-front and check whether
the probe is already registered via check_kprobe_rereg_at() before
allocating rethook resources, dropping the WARN_ON_ONCE().
[04/08] kprobes: Switch ftrace ops when adding probe with post_handler
- Switch from kprobe_ftrace_ops to kprobe_ipmodify_ops in
register_aggr_kprobe() outside of text_mutex and cpus_read_lock.
- Restore ftrace ops counter and registration if ftrace_set_filter_ip()
fails during disarm.
[05/08] kprobes: Fix UAF and queue cooling probes on forced unoptimization
- Queue forcibly unoptimized probes onto cur->cooling_list (or
cur->freeing_list if unused), include cooling_list in
optprobe_has_queued_probes(), and isolate newly unoptimized probes
into a local list in do_unoptimize_kprobes() to prevent double disarm.
[06/08] kprobes: Do not overwrite jump instruction when re-arming
unoptimizing probe
- Skip arch_arm_kprobe() in __arm_kprobe() when kprobe_queued_unopt()
is true, keeping the relative jump intact while unoptimization is
cancelled.
- Force unoptimization immediately if optimize_kprobe() cannot optimize
the re-armed probe.
- Avoid premature flag clearing or dequeuing in reuse_unused_kprobe().
[07/08] kprobes: Fix slot reclaim and sibling re-opt in optprobe state machine
- In kill_optimized_kprobe(), queue in-use probes on unoptimizing_list
to cooling_list rather than freeing_list to preserve kprobe_table
lookup and ensure Tasks RCU elapses before slot reclamation.
- Reclaim optinsn slots for kprobe_gone() probes upon cooling completion
in optprobe_finalize_generation().
- Re-optimize blocked sibling probes when a cooling probe finishes
cooling to safely disarmed.
[08/08] kprobes: Add KUnit test cases for kprobes regressions
- Add test cases to lib/tests/test_kprobes.c covering unregistered
kretprobe cleanup, kretprobe re-registration validation by symbol,
ftrace ops dynamic switching on post_handler addition, and optprobe
re-arming while unoptimizing.
Thank you,
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Masami Hiramatsu (Google) (8):
kprobes: Fix error return and slot leak in __register_kprobe()
kprobes: Fix double free and NULL deref in unregister_kretprobes()
kprobes: Validate re-registration earlier in register_kretprobe()
kprobes: Switch ftrace ops when adding probe with post_handler
kprobes: Fix UAF and queue cooling probes on forced unoptimization
kprobes: Do not overwrite jump instruction when re-arming unoptimizing probe
kprobes: Fix slot reclaim and sibling re-opt in optprobe state machine
kprobes: Add KUnit test cases for kprobes regressions
kernel/kprobes.c | 226 ++++++++++++++++++++++++++++++----------------
lib/tests/test_kprobes.c | 123 +++++++++++++++++++++++++
2 files changed, 271 insertions(+), 78 deletions(-)
--
Masami Hiramatsu (Google) <mhiramat@kernel.org>
next reply other threads:[~2026-11-03 23:58 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-03 23:58 Masami Hiramatsu (Google) [this message]
2026-11-03 23:58 ` [PATCH 1/8] kprobes: Fix error return and slot leak in __register_kprobe() Masami Hiramatsu (Google)
2026-11-03 23:58 ` [PATCH 2/8] kprobes: Fix double free and NULL deref in unregister_kretprobes() Masami Hiramatsu (Google)
2026-11-03 23:58 ` [PATCH 3/8] kprobes: Validate re-registration earlier in register_kretprobe() Masami Hiramatsu (Google)
2026-11-03 23:58 ` [PATCH 4/8] kprobes: Switch ftrace ops when adding probe with post_handler Masami Hiramatsu (Google)
2026-11-04 0:11 ` sashiko-bot
2026-11-03 23:59 ` [PATCH 5/8] kprobes: Fix UAF and queue cooling probes on forced unoptimization Masami Hiramatsu (Google)
2026-11-04 0:25 ` sashiko-bot
2026-11-03 23:59 ` [PATCH 6/8] kprobes: Do not overwrite jump instruction when re-arming unoptimizing probe Masami Hiramatsu (Google)
2026-11-04 0:12 ` sashiko-bot
2026-11-03 23:59 ` [PATCH 7/8] kprobes: Fix slot reclaim and sibling re-opt in optprobe state machine Masami Hiramatsu (Google)
2026-11-04 0:30 ` sashiko-bot
2026-11-03 23:59 ` [PATCH 8/8] kprobes: Add KUnit test cases for kprobes regressions Masami Hiramatsu (Google)
2026-11-04 0:25 ` sashiko-bot
2026-11-04 16:31 ` [PATCH 0/8] kprobes: Fixes for registration errors, ftrace ops and optprobe state machine Masami Hiramatsu (Google)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179375029807.247640.13024754173032838618.stgit@devnote2 \
--to=mhiramat@kernel.org \
--cc=davem@davemloft.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=naveen@kernel.org \
--cc=rostedt@goodmis.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox