kprobes: Fix error return and slot leak in __register_kprobe()

When arm_kprobe() fails in __register_kprobe(), the probe is removed
from kprobe_table and synchronize_rcu() is executed, but the error code
ret was ignored and try_to_optimize_kprobe(p) was executed, returning 0.
Additionally, the instruction slot allocated by prepare_kprobe() was
leaked.

Fix this by releasing the instruction slot via arch_remove_kprobe(p)
and returning ret when arm_kprobe() fails.

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

Fixes: 587e8e6d64 ("kprobes: Adopt guard() and scoped_guard()")
Cc: [email protected]
Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
This commit is contained in:
Masami Hiramatsu (Google)
2026-11-05 09:08:25 +09:00
parent 6628e7d4c6
commit 6a658cc2d6
+2
View File
@@ -1946,6 +1946,8 @@ static int __register_kprobe(struct kprobe *p)
if (ret) {
hlist_del_rcu(&p->hlist);
synchronize_rcu();
arch_remove_kprobe(p);
return ret;
}
}