kprobes: Fix double free and NULL deref in unregister_kretprobes()

In unregister_kretprobes(), if __unregister_kprobe_top() returns an error
(e.g., if the kretprobe was not registered or was already unregistered),
rethook_free(rps[i]->rh) or rcu_assign_pointer(rps[i]->rph->rp, NULL) is
still executed. For rethook-based kretprobes, calling rethook_free() on an
already-freed rethook causes a double-free / list corruption in RCU. For
legacy kretprobes, rps[i]->rph is NULL after previous cleanup, resulting
in a NULL pointer dereference.

Skip rethook_free() and rph cleanup if __unregister_kprobe_top() fails,
and clear rps[i]->rh after synchronize_rcu() has waited for in-flight
pre_handler_kretprobe() invocations to complete.

Link: https://lore.kernel.org/all/179375031660.247640.6349602689169856649.stgit@devnote2/

Fixes: 73f9b911fa ("kprobes: Use rethook for kretprobe if possible")
Cc: [email protected]
Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
This commit is contained in:
Masami Hiramatsu (Google)
2026-11-05 09:09:41 +09:00
parent 6a658cc2d6
commit 2053bac1d6
+6 -2
View File
@@ -2605,8 +2605,10 @@ void unregister_kretprobes(struct kretprobe **rps, int num)
for (i = 0; i < num; i++) {
guard(mutex)(&kprobe_mutex);
if (__unregister_kprobe_top(&rps[i]->kp) < 0)
if (__unregister_kprobe_top(&rps[i]->kp) < 0) {
rps[i]->kp.addr = NULL;
continue;
}
#ifdef CONFIG_KRETPROBE_ON_RETHOOK
rethook_free(rps[i]->rh);
#else
@@ -2618,7 +2620,9 @@ void unregister_kretprobes(struct kretprobe **rps, int num)
for (i = 0; i < num; i++) {
if (rps[i]->kp.addr) {
__unregister_kprobe_bottom(&rps[i]->kp);
#ifndef CONFIG_KRETPROBE_ON_RETHOOK
#ifdef CONFIG_KRETPROBE_ON_RETHOOK
rps[i]->rh = NULL;
#else
free_rp_inst(rps[i]);
#endif
}