Skip to contentSkip to conversation

x86/kprobes: Fix KRETPROBES when CONFIG_KRETPROBE_ON_RETHOOK is set

PATCH In review Adam Zabrocki 3 participants 4 messages Apr 22, 2022 – Apr 26, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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

[PATCH bpf] 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
maintainer
#1 lore raw
Quoted 9 lines · Adam Zabrocki

[PATCH bpf] 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.

Quoted 5 lines

This patch adds such verification in kretprobe_rethook_handler() function.

Fixes 73f9b911faa7 ("kprobes: Use rethook for kretprobe if possible")

I don't mind if this fix gets routed via bpf tree if all parties are okay with it (Masami? Steven?). Just noting that there is currently no specific dependency in bpf tree for it, but if it's easier to route this way, happy to take it.

Quoted 10 lines
---
  kernel/kprobes.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/kprobes.c +0 -0
index dbe57df2e199..dd58c0be9ce2 100644

kernel/kprobes.c static void kretprobe_rethook_handler(struct rethook_node *rh, void *data, · line 2132

Quoted 8 lines
kernel/kprobes.c +1 -1
@@ -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);

Thanks,
Daniel

maintainer
#2 lore raw
Quoted 12 lines · On Mon, 25 Apr 2022 16:42:12 +0200 Daniel Borkmann <[email protected]> wrote:
Quoted 11 lines · Adam Zabrocki

[PATCH bpf] 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.

Quoted 7 lines
Quoted 3 lines
Fixes 73f9b911faa7 ("kprobes: Use rethook for kretprobe if possible")

I don't mind if this fix gets routed via bpf tree if all parties are okay with it (Masami? Steven?). Just noting that there is currently no specific dependency in bpf tree for it, but if it's easier to route this way, happy to take it.

Yeah, I and Steve talked about it and he suggested this to be merged via BPF tree since the original commit came from the BPF tree.

Thank you,

Quoted 22 lines
Quoted 11 lines
---
  kernel/kprobes.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/kprobes.c +0 -0
index dbe57df2e199..dd58c0be9ce2 100644

kernel/kprobes.c static void kretprobe_rethook_handler(struct rethook_node *rh, void *data, · line 2132

Quoted 8 lines
kernel/kprobes.c +1 -1
@@ -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);

Thanks,
Daniel

Signature · 1 line
Masami Hiramatsu <[email protected]>
maintainer
#3 lore raw
Quoted 18 lines · Masami Hiramatsu
Quoted 16 lines · On Mon, 25 Apr 2022 16:42:12 +0200 Daniel Borkmann <[email protected]> wrote:
Quoted 14 lines · Adam Zabrocki

[PATCH bpf] 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")
Quoted 6 lines
Quoted 3 lines

I don't mind if this fix gets routed via bpf tree if all parties are okay with it (Masami? Steven?). Just noting that there is currently no specific dependency in bpf tree for it, but if it's easier to route this way, happy to take it.

Yeah, I and Steve talked about it and he suggested this to be merged via BPF tree since the original commit came from the BPF tree.

Okay, I just applied it to bpf tree then.

Thanks everyone,
Daniel