Re: [PATCH] kretprobe: check re-registration of the same kretprobe earlier

From: Masami Hiramatsu
Date: Fri Mar 06 2020 - 10:21:22 EST


Hi Cheng,

On Fri, 6 Mar 2020 17:35:06 +0800
Cheng Jian <cj.chengjian@xxxxxxxxxx> wrote:

> Our system encountered a use-after-free when re-register a
> same kretprobe. it access the hlist node 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 memory
> leak and use-after-free.

I couldn't get how it cause use-after-free, but it causes memory leak.
Anyway, if we can help to find a wrong usage, it might be good.

Acked-by: Masami Hiramatsu <mhiramat@xxxxxxxxxx>

BTW, I think we should use WARN_ON() for this error, because this
means the caller is completely buggy.

Thank you,

>
> Signed-off-by: Cheng Jian <cj.chengjian@xxxxxxxxxx>
> ---
> kernel/kprobes.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> index 2625c24..f1fc921 100644
> --- a/kernel/kprobes.c
> +++ b/kernel/kprobes.c
> @@ -1946,6 +1946,11 @@ int register_kretprobe(struct kretprobe *rp)
> }
> }
>
> + /* Return error if it's being re-registered */
> + ret = check_kprobe_rereg(&rp->kp);
> + if (ret)
> + return ret;
> +
> rp->kp.pre_handler = pre_handler_kretprobe;
> rp->kp.post_handler = NULL;
> rp->kp.fault_handler = NULL;
> --
> 2.7.4
>


--
Masami Hiramatsu <mhiramat@xxxxxxxxxx>