Skip to contentSkip to conversation

kprobes: Fix NULL pointer dereference in unregister_kretprobes()

PATCH In review Luigi Rizzo 4 participants 6 messages Aug 5 – Aug 6

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

PATCH 1/1 kprobes: Fix NULL pointer dereference in unregister_kretprobes()

In unregister_kretprobes(), rps[i]->rph can be NULL e.g. when called after kretprobe failed registration. Under !CONFIG_KRETPROBE_ON_RETHOOK, the unconditional access to rps[i]->rph->rp, causes a kernel panic due to NULL pointer dereference.

Add a NULL check for rps[i]->rph before invoking rcu_assign_pointer().

Fixes d839a656d0f3 ("kprobes: consistent rcu api usage for kretprobe holder")
---
 kernel/kprobes.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/kprobes.c +2 -1
index bfc89083daa93..5dd4786c455de 100644
@@ -2359,7 +2359,8 @@ void unregister_kretprobes(struct kretprobe **rps, int num)
 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
 		rethook_free(rps[i]->rh);
 #else
-		rcu_assign_pointer(rps[i]->rph->rp, NULL);
+		if (rps[i]->rph)
+			rcu_assign_pointer(rps[i]->rph->rp, NULL);
 #endif
 	}
 
Signature · 1 line
2.48.1.500.g5897711438-goog
#2 lore raw
In reply to Luigi Rizzo
Quoted 8 lines · Luigi Rizzo

In unregister_kretprobes(), rps[i]->rph can be NULL e.g. when called after kretprobe failed registration. Under !CONFIG_KRETPROBE_ON_RETHOOK, the unconditional access to rps[i]->rph->rp, causes a kernel panic due to NULL pointer dereference.

Add a NULL check for rps[i]->rph before invoking rcu_assign_pointer().

Fixes d839a656d0f3 ("kprobes: consistent rcu api usage for kretprobe holder")

The bug was not introduced in this commit. It goes further back to:

d741bf41d7c7 ("kprobes: Remove kretprobe hash")

Quoted 19 lines
---
  kernel/kprobes.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
kernel/kprobes.c +2 -1
index bfc89083daa93..5dd4786c455de 100644
@@ -2359,7 +2359,8 @@ void unregister_kretprobes(struct kretprobe **rps, int num)
  #ifdef CONFIG_KRETPROBE_ON_RETHOOK
  		rethook_free(rps[i]->rh);
  #else
-		rcu_assign_pointer(rps[i]->rph->rp, NULL);
+		if (rps[i]->rph)
+			rcu_assign_pointer(rps[i]->rph->rp, NULL);
  #endif
  	}
 
#3 lore raw
In reply to Luigi Rizzo
Quoted 4 lines · On Wed, 5 Aug 2026 16:12:21 +0000 Luigi Rizzo <[email protected]> wrote:

In unregister_kretprobes(), rps[i]->rph can be NULL e.g. when called after kretprobe failed registration. Under !CONFIG_KRETPROBE_ON_RETHOOK, the unconditional access to rps[i]->rph->rp, causes a kernel panic due to NULL pointer dereference.

This is not a bug, since if register_kretprobe(rp) fails, rp must NOT be passed to unregister_kretprobe(rp). Or, do you find any cases where register_kretprobe() fails, preventing proper cleanup, and requiring unregister_kretprobe()? If so, we have to fix that case.

Quoted 3 lines

Add a NULL check for rps[i]->rph before invoking rcu_assign_pointer().

But this could be a kind of protective improvemet for someone misunderstand that.

Thank you,

Quoted 24 lines
Fixes d839a656d0f3 ("kprobes: consistent rcu api usage for kretprobe holder")
---
 kernel/kprobes.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/kprobes.c +2 -1
index bfc89083daa93..5dd4786c455de 100644
@@ -2359,7 +2359,8 @@ void unregister_kretprobes(struct kretprobe **rps, int num)
 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
 		rethook_free(rps[i]->rh);
 #else
-		rcu_assign_pointer(rps[i]->rph->rp, NULL);
+		if (rps[i]->rph)
+			rcu_assign_pointer(rps[i]->rph->rp, NULL);
 #endif
 	}
 
Signature · 1 line
2.48.1.500.g5897711438-goog
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#4 lore raw
Quoted 9 lines · Masami Hiramatsu
Quoted 4 lines · On Wed, 5 Aug 2026 16:12:21 +0000 Luigi Rizzo <[email protected]> wrote:

In unregister_kretprobes(), rps[i]->rph can be NULL e.g. when called after kretprobe failed registration. Under !CONFIG_KRETPROBE_ON_RETHOOK, the unconditional access to rps[i]->rph->rp, causes a kernel panic due to NULL pointer dereference.

Quoted 4 lines

This is not a bug, since if register_kretprobe(rp) fails, rp must NOT be passed to unregister_kretprobe(rp). Or, do you find any cases where register_kretprobe() fails, preventing proper cleanup, and requiring unregister_kretprobe()? If so, we have to fix that case.

Masami, you are right, the kernel tree does not call unregister_kretprobes() on a failed registration. I was confused by the unregister_kretprobes(rps, i); call in the cleanup in register_kretprobes(), but the failed entry i is not unregistered).

So aside from protective coding (but where would one stop ? null rps, null rps[i], ... ), there is no need for this patch.

thanks for the feedback
Luigi

#5 lore raw
Quoted 15 lines · On Thu, 6 Aug 2026 09:23:29 +0200 Luigi Rizzo <[email protected]> wrote:
Quoted 9 lines · Masami Hiramatsu
Quoted 4 lines · On Wed, 5 Aug 2026 16:12:21 +0000 Luigi Rizzo <[email protected]> wrote:

In unregister_kretprobes(), rps[i]->rph can be NULL e.g. when called after kretprobe failed registration. Under !CONFIG_KRETPROBE_ON_RETHOOK, the unconditional access to rps[i]->rph->rp, causes a kernel panic due to NULL pointer dereference.

Quoted 4 lines

This is not a bug, since if register_kretprobe(rp) fails, rp must NOT be passed to unregister_kretprobe(rp). Or, do you find any cases where register_kretprobe() fails, preventing proper cleanup, and requiring unregister_kretprobe()? If so, we have to fix that case.

Quoted 4 lines

Masami, you are right, the kernel tree does not call unregister_kretprobes() on a failed registration. I was confused by the unregister_kretprobes(rps, i); call in the cleanup in register_kretprobes(), but the failed entry i is not unregistered).

Yes, in that case rps[i] is not unregistered ;)

Quoted 3 lines

So aside from protective coding (but where would one stop ? null rps, null rps[i], ... ), there is no need for this patch.

OK, Thanks for the confirmation!

Thanks,

Quoted 3 lines

thanks for the feedback
Luigi

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>