mirror of
https://github.com/torvalds/linux.git
synced 2026-11-23 23:11:40 +00:00
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:
+6
-2
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user