Skip to contentSkip to conversation

KRETPROBES are broken since the commit 73f9b911faa7

PATCH In review Adam Zabrocki 3 participants 7 messages Apr 15, 2022 – Apr 22, 2022 kprobes

lore
Share & tools
lore
#1 lore raw

PATCH 1/1 x86/kprobes: Fix KRETPROBES when CONFIG_KRETPROBE_ON_RETHOOK is set

In reply to Adam Zabrocki

[PATCH] x86/kprobes: Fix KRETPROBES when CONFIG_KRETPROBE_ON_RETHOOK is set

The recent kernel change "kprobes: Use rethook for kretprobe if possible", introduced a potential NULL pointer dereference bug in the KRETPROBE mechanism. The official Kprobes documentation defines that "Any or all handlers can be NULL". Unfortunately, there is a missing return handler verification to fulfill these requirements and can result in a NULL pointer dereference bug.

This patch adds such verification in kretprobe_rethook_handler() function.

Fixes 73f9b911faa7 ("kprobes: Use rethook for kretprobe if possible")
---
 kernel/kprobes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/kprobes.c +1 -1
index dbe57df2e199..dd58c0be9ce2 100644
@@ -2126,7 +2126,7 @@ static void kretprobe_rethook_handler(struct rethook_node *rh, void *data,
 	struct kprobe_ctlblk *kcb;
 
 	/* The data must NOT be null. This means rethook data structure is broken. */
-	if (WARN_ON_ONCE(!data))
+	if (WARN_ON_ONCE(!data) || !rp->handler)
 		return;
 
 	__this_cpu_write(current_kprobe, &rp->kp);
Signature · 2 lines
pi3 (pi3ki31ny) - pi3 (at) itsec pl
http://pi3.com.pl
#0 lore raw

Hello,

The recent KRETPROBE kernel changes introduced a potential NULL pointer dereference bug when return handler is not set. The root of the problem are the following commits related to "kprobes: Use rethook for kretprobe if possible":
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=f3a112c0c40dd96d53c8bdf3ea8d94d528f3b7b8
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=73f9b911faa74ac5107879de05c9489c419f41bb

When user sets up a KRETPROBE, kernel eventually ends up here:

static void kretprobe_rethook_handler(struct rethook_node *rh, void *data,
struct pt_regs *regs)

{
    struct kretprobe *rp = (struct kretprobe *)data;
    struct kretprobe_instance *ri;
    struct kprobe_ctlblk *kcb;

    /* The data must NOT be null. This means rethook data structure is broken. */
    if (WARN_ON_ONCE(!data))
        return;

    __this_cpu_write(current_kprobe, &rp->kp);
    kcb = get_kprobe_ctlblk();
    kcb->kprobe_status = KPROBE_HIT_ACTIVE;

    ri = container_of(rh, struct kretprobe_instance, node);
    rp->handler(ri, regs);

    __this_cpu_write(current_kprobe, NULL);
}

NOKPROBE_SYMBOL(kretprobe_rethook_handler);

Unfortunately, rp->handler is not verified against NULL. If user does not set up a return handler, KRETPROBE shouldn't call it (that's the defined behavior in the documentation, quoting "Any or all handlers can be NULL"). However, kretprobe_rethook_handler function does not enforce such behavior.

I'm going to send a patch for it in a reply to this message.

Thanks,
Adam

Signature · 2 lines
pi3 (pi3ki31ny) - pi3 (at) itsec pl
http://pi3.com.pl
maintainer Acked-by on v1 1/1
#2 lore raw

Re: [PATCH] x86/kprobes: Fix KRETPROBES when CONFIG_KRETPROBE_ON_RETHOOK is set

In reply to Adam Zabrocki
Quoted 11 lines · On Fri, 15 Apr 2022 20:10:06 +0200 Adam Zabrocki <[email protected]> wrote:

[PATCH] x86/kprobes: Fix KRETPROBES when CONFIG_KRETPROBE_ON_RETHOOK is set

The recent kernel change "kprobes: Use rethook for kretprobe if possible", introduced a potential NULL pointer dereference bug in the KRETPROBE mechanism. The official Kprobes documentation defines that "Any or all handlers can be NULL". Unfortunately, there is a missing return handler verification to fulfill these requirements and can result in a NULL pointer dereference bug.

This patch adds such verification in kretprobe_rethook_handler() function.

Good catch! I forgot that user can register kretprobe without handler...

Thank you!

Quoted 24 lines
Fixes 73f9b911faa7 ("kprobes: Use rethook for kretprobe if possible")
---
 kernel/kprobes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/kprobes.c +1 -1
index dbe57df2e199..dd58c0be9ce2 100644
@@ -2126,7 +2126,7 @@ static void kretprobe_rethook_handler(struct rethook_node *rh, void *data,
 	struct kprobe_ctlblk *kcb;
 
 	/* The data must NOT be null. This means rethook data structure is broken. */
-	if (WARN_ON_ONCE(!data))
+	if (WARN_ON_ONCE(!data) || !rp->handler)
 		return;
 
 	__this_cpu_write(current_kprobe, &rp->kp);
Signature · 3 lines
-- 
pi3 (pi3ki31ny) - pi3 (at) itsec pl
http://pi3.com.pl
Signature · 1 line
Masami Hiramatsu <[email protected]>
maintainer Acked-by on v1 1/1
#3 lore raw

Re: [PATCH] x86/kprobes: Fix KRETPROBES when CONFIG_KRETPROBE_ON_RETHOOK is set

In reply to Adam Zabrocki

Hi Steve,

Can you pick this fix to urgent branch? Or Should I ask bpf people?

Quoted 11 lines · On Fri, 15 Apr 2022 20:10:06 +0200 Adam Zabrocki <[email protected]> wrote:

[PATCH] x86/kprobes: Fix KRETPROBES when CONFIG_KRETPROBE_ON_RETHOOK is set

The recent kernel change "kprobes: Use rethook for kretprobe if possible", introduced a potential NULL pointer dereference bug in the KRETPROBE mechanism. The official Kprobes documentation defines that "Any or all handlers can be NULL". Unfortunately, there is a missing return handler verification to fulfill these requirements and can result in a NULL pointer dereference bug.

This patch adds such verification in kretprobe_rethook_handler() function.

Thank you,

Quoted 24 lines
Fixes 73f9b911faa7 ("kprobes: Use rethook for kretprobe if possible")
---
 kernel/kprobes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/kprobes.c +1 -1
index dbe57df2e199..dd58c0be9ce2 100644
@@ -2126,7 +2126,7 @@ static void kretprobe_rethook_handler(struct rethook_node *rh, void *data,
 	struct kprobe_ctlblk *kcb;
 
 	/* The data must NOT be null. This means rethook data structure is broken. */
-	if (WARN_ON_ONCE(!data))
+	if (WARN_ON_ONCE(!data) || !rp->handler)
 		return;
 
 	__this_cpu_write(current_kprobe, &rp->kp);
Signature · 3 lines
-- 
pi3 (pi3ki31ny) - pi3 (at) itsec pl
http://pi3.com.pl
Signature · 1 line
Masami Hiramatsu <[email protected]>
#4 lore raw

Re: [PATCH] x86/kprobes: Fix KRETPROBES when CONFIG_KRETPROBE_ON_RETHOOK is set

Quoted 3 lines · On Fri, 22 Apr 2022 22:05:23 +0900 Masami Hiramatsu <[email protected]> wrote:

Hi Steve,

Can you pick this fix to urgent branch? Or Should I ask bpf people?

It looks to fix a bug from a commit pulled in from the BPF folks. I would have them take it.

Thanks,

-- Steve

Quoted 2 lines
Quoted 2 lines
Fixes 73f9b911faa7 ("kprobes: Use rethook for kretprobe if possible")
maintainer
#5 lore raw

Re: [PATCH] x86/kprobes: Fix KRETPROBES when CONFIG_KRETPROBE_ON_RETHOOK is set

Quoted 9 lines · On Fri, 22 Apr 2022 09:43:50 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 3 lines · On Fri, 22 Apr 2022 22:05:23 +0900 Masami Hiramatsu <[email protected]> wrote:

Hi Steve,

Can you pick this fix to urgent branch? Or Should I ask bpf people?

It looks to fix a bug from a commit pulled in from the BPF folks. I would have them take it.

OK, thanks!

Adam, can you resend your patch with my Ack and modify the PATCH tag as
"[PATCH bpf]", to "Alexei Starovoitov <[email protected]>", "Andrii Nakryiko <[email protected]>",
and Cc: "[email protected]" ?

Thank you,

Quoted 6 lines

Thanks,

-- Steve

Quoted 2 lines
Quoted 2 lines
Fixes 73f9b911faa7 ("kprobes: Use rethook for kretprobe if possible")
Signature · 1 line
Masami Hiramatsu <[email protected]>
#6 lore raw

Re: [PATCH] x86/kprobes: Fix KRETPROBES when CONFIG_KRETPROBE_ON_RETHOOK is set

Sure, I will do it.

Thanks for help,
Adam

Quoted 31 lines · Masami Hiramatsu
Quoted 9 lines · On Fri, 22 Apr 2022 09:43:50 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 3 lines · On Fri, 22 Apr 2022 22:05:23 +0900 Masami Hiramatsu <[email protected]> wrote:

Hi Steve,

Can you pick this fix to urgent branch? Or Should I ask bpf people?

It looks to fix a bug from a commit pulled in from the BPF folks. I would have them take it.

OK, thanks!

Adam, can you resend your patch with my Ack and modify the PATCH tag as
"[PATCH bpf]", to "Alexei Starovoitov <[email protected]>", "Andrii Nakryiko <[email protected]>",
and Cc: "[email protected]" ?

Thank you,

Quoted 6 lines

Thanks,

-- Steve

Quoted 2 lines
Quoted 2 lines
Fixes 73f9b911faa7 ("kprobes: Use rethook for kretprobe if possible")
Signature · 1 line
Masami Hiramatsu <[email protected]>
Signature · 2 lines
pi3 (pi3ki31ny) - pi3 (at) itsec pl
http://pi3.com.pl