[PATCH v2] kretprobe: check re-registration of the same kretprobe earlier
From: Cheng Jian
Date: Mon Mar 09 2020 - 09:32:42 EST
Our system encountered a use-after-free when re-register the same
kretprobe, it access the kretprobe_instance in rp->free_instances
which has been released already.
Prevent re-registration has been implemented for kprobe before, but
it's too late for kretprobe. We must check the re-registration before
re-initializing the kretprobe, otherwise it will destroy the data and
struct of the kretprobe registered, it can lead to use-after-free,
memory leak, system crash, and even other unexpected behaviors.
Use check_kprobe_rereg() to check re-registration, also give a warning
message.
Signed-off-by: Cheng Jian <cj.chengjian@xxxxxxxxxx>
Acked-by: Masami Hiramatsu <mhiramat@xxxxxxxxxx>
---
kernel/kprobes.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index 2625c24..9cb2a0d 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -1946,6 +1946,14 @@ int register_kretprobe(struct kretprobe *rp)
}
}
+ /*
+ * Return error if it's being re-registered,
+ * also give a warning message to the developer.
+ */
+ ret = check_kprobe_rereg(&rp->kp);
+ if (WARN_ON(ret))
+ return ret;
+
rp->kp.pre_handler = pre_handler_kretprobe;
rp->kp.post_handler = NULL;
rp->kp.fault_handler = NULL;
--
2.7.4