Skip to contentSkip to conversation

uprobes: Fix NULL pointer dereference in hprobe_expire()

PATCH Applied Breno Leitao 6 participants 10 messages Jul 29 – Jul 31

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

PATCH 1/1 uprobes: Fix NULL pointer dereference in hprobe_expire()

Forking a task that has a pending uretprobe can oops the kernel with a NULL pointer dereference in the clone() path:

  BUG: kernel NULL pointer dereference, address: 0000000000000018
  Oops: 0002 [#1] SMP NOPTI
  RIP: 0010:hprobe_expire
  CR2: 0000000000000018
  Call Trace:
   uprobe_copy_process
   copy_process
   kernel_clone
   __x64_sys_clone
   do_syscall_64
   entry_SYSCALL_64_after_hwframe

This was found on real hosts on Meta fleet.

I've got the impression that this is what is happening:

CPU 1 CPU 2 (traced task)
----- -------------------
hit uprobe, prepare_uretprobe():
hprobe LEASED, refcount >= 1
uprobe_unregister()

    put_uprobe(): refcount -> 0
                                 fork() -> dup_utask()
                                   hprobe_expire(hprobe, true)
                                     try_get_uprobe() -> NULL
                                     get_uprobe(NULL)   <-- Oops

Only take the extra reference when the uprobe is non-NULL; a NULL means it is gone and is the correct value to return.

Fixes dd1a7567784e ("uprobes: SRCU-protect uretprobe lifetime (with timeout)")
---
 kernel/events/uprobes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/events/uprobes.c +1 -1
index 07f69dd3093d5..950108f92079d 100644
@@ -832,7 +832,7 @@ static struct uprobe *hprobe_expire(struct hprobe *hprobe, bool get)
 		if (try_cmpxchg(&hprobe->state, &hstate, uprobe ? HPROBE_STABLE : HPROBE_GONE)) {
 			/* We won the race, we are the ones to unlock SRCU */
 			__srcu_read_unlock(&uretprobes_srcu, hprobe->srcu_idx);
-			return get ? get_uprobe(uprobe) : uprobe;
+			return get && uprobe ? get_uprobe(uprobe) : uprobe;
 		}
 
 		/*

---
base-commit: 3652b49adac266a3d27cb41cdfdb7d8790fc3633
change-id: 20260729-uprobe-b142b0863572

Best regards,

Signature · 1 line
Breno Leitao <[email protected]>
maintainer Acked-by on v1 1/1
#1 lore raw

kernel/events/uprobes.c static struct uprobe *hprobe_expire(struct hprobe *hprobe, bool get) · line 835

Quoted 9 lines · Breno Leitao
kernel/events/uprobes.c +1 -1
@@ -832,7 +832,7 @@ static struct uprobe *hprobe_expire(struct hprobe *hprobe, bool get)
 		if (try_cmpxchg(&hprobe->state, &hstate, uprobe ? HPROBE_STABLE : HPROBE_GONE)) {
 			/* We won the race, we are the ones to unlock SRCU */
 			__srcu_read_unlock(&uretprobes_srcu, hprobe->srcu_idx);
-			return get ? get_uprobe(uprobe) : uprobe;
+			return get && uprobe ? get_uprobe(uprobe) : uprobe;

Well, looks "obviously correct". At least the current code is obviously wrong, it even checks uprobe != NULL 3 lines above.

Andrii ?

#2 lore raw
Quoted 15 lines · Oleg Nesterov

kernel/events/uprobes.c static struct uprobe *hprobe_expire(struct hprobe *hprobe, bool get) · line 835

Quoted 9 lines · Breno Leitao
kernel/events/uprobes.c +1 -1
@@ -832,7 +832,7 @@ static struct uprobe *hprobe_expire(struct hprobe *hprobe, bool get)
              if (try_cmpxchg(&hprobe->state, &hstate, uprobe ? HPROBE_STABLE : HPROBE_GONE)) {
                      /* We won the race, we are the ones to unlock SRCU */
                      __srcu_read_unlock(&uretprobes_srcu, hprobe->srcu_idx);
-                     return get ? get_uprobe(uprobe) : uprobe;
+                     return get && uprobe ? get_uprobe(uprobe) : uprobe;

Well, looks "obviously correct". At least the current code is obviously wrong, it even checks uprobe != NULL 3 lines above.

Quoted 2 lines

Andrii ?

Yes, indeed, I'm more surprised this didn't come up much earlier (probably it's rare enough to have uretprobe with refcnt at zero during fork). The fix looks good, thanks!

Quoted 3 lines
#3 lore raw
Quoted 16 lines · On Wed, 29 Jul 2026 12:31:08 -0700 Andrii Nakryiko <[email protected]>…
Quoted 15 lines · Oleg Nesterov

kernel/events/uprobes.c static struct uprobe *hprobe_expire(struct hprobe *hprobe, bool get) · line 835

Quoted 9 lines · Breno Leitao
kernel/events/uprobes.c +1 -1
@@ -832,7 +832,7 @@ static struct uprobe *hprobe_expire(struct hprobe *hprobe, bool get)
              if (try_cmpxchg(&hprobe->state, &hstate, uprobe ? HPROBE_STABLE : HPROBE_GONE)) {
                      /* We won the race, we are the ones to unlock SRCU */
                      __srcu_read_unlock(&uretprobes_srcu, hprobe->srcu_idx);
-                     return get ? get_uprobe(uprobe) : uprobe;
+                     return get && uprobe ? get_uprobe(uprobe) : uprobe;

Well, looks "obviously correct". At least the current code is obviously wrong, it even checks uprobe != NULL 3 lines above.

Quoted 11 lines
Quoted 2 lines

Andrii ?

Yes, indeed, I'm more surprised this didn't come up much earlier (probably it's rare enough to have uretprobe with refcnt at zero during fork). The fix looks good, thanks!

Quoted 2 lines

Thanks, this seems good to me. (uprobe is NULL checked in try_cmpxchg(), so it could be NULL.)

Can I pick this to probes/fixes branch?

Thank you,

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
maintainer
#4 lore raw
Quoted 28 lines · Masami Hiramatsu
Quoted 25 lines · On Wed, 29 Jul 2026 12:31:08 -0700 Andrii Nakryiko <[email protected]>…
Quoted 15 lines · Oleg Nesterov

kernel/events/uprobes.c static struct uprobe *hprobe_expire(struct hprobe *hprobe, bool get) · line 835

Quoted 9 lines · Breno Leitao
kernel/events/uprobes.c +1 -1
@@ -832,7 +832,7 @@ static struct uprobe *hprobe_expire(struct hprobe *hprobe, bool get)
              if (try_cmpxchg(&hprobe->state, &hstate, uprobe ? HPROBE_STABLE : HPROBE_GONE)) {
                      /* We won the race, we are the ones to unlock SRCU */
                      __srcu_read_unlock(&uretprobes_srcu, hprobe->srcu_idx);
-                     return get ? get_uprobe(uprobe) : uprobe;
+                     return get && uprobe ? get_uprobe(uprobe) : uprobe;

Well, looks "obviously correct". At least the current code is obviously wrong, it even checks uprobe != NULL 3 lines above.

Quoted 2 lines

Andrii ?

Yes, indeed, I'm more surprised this didn't come up much earlier (probably it's rare enough to have uretprobe with refcnt at zero during fork). The fix looks good, thanks!

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

Thanks, this seems good to me. (uprobe is NULL checked in try_cmpxchg(), so it could be NULL.)

Can I pick this to probes/fixes branch?

tip/perf/urgent ?

#5 lore raw
Quoted 32 lines · On Thu, 30 Jul 2026 12:38:24 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 31 lines · Masami Hiramatsu
Quoted 27 lines · On Wed, 29 Jul 2026 12:31:08 -0700 Andrii Nakryiko <[email protected]>…
Quoted 15 lines · Oleg Nesterov

kernel/events/uprobes.c static struct uprobe *hprobe_expire(struct hprobe *hprobe, bool get) · line 835

Quoted 9 lines · Breno Leitao
kernel/events/uprobes.c +1 -1
@@ -832,7 +832,7 @@ static struct uprobe *hprobe_expire(struct hprobe *hprobe, bool get)
              if (try_cmpxchg(&hprobe->state, &hstate, uprobe ? HPROBE_STABLE : HPROBE_GONE)) {
                      /* We won the race, we are the ones to unlock SRCU */
                      __srcu_read_unlock(&uretprobes_srcu, hprobe->srcu_idx);
-                     return get ? get_uprobe(uprobe) : uprobe;
+                     return get && uprobe ? get_uprobe(uprobe) : uprobe;

Well, looks "obviously correct". At least the current code is obviously wrong, it even checks uprobe != NULL 3 lines above.

Quoted 2 lines

Andrii ?

Yes, indeed, I'm more surprised this didn't come up much earlier (probably it's rare enough to have uretprobe with refcnt at zero during fork). The fix looks good, thanks!

Quoted 2 lines
Quoted 6 lines
Quoted 4 lines

Thanks, this seems good to me. (uprobe is NULL checked in try_cmpxchg(), so it could be NULL.)

Can I pick this to probes/fixes branch?

tip/perf/urgent ?

Either way works. Can we update MAINTAINERS file if you will maintain uprobe in tip tree?

Thank you,

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
maintainer
#6 lore raw
Quoted 38 lines · Masami Hiramatsu
Quoted 35 lines · On Thu, 30 Jul 2026 12:38:24 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 34 lines · Masami Hiramatsu
Quoted 16 lines · On Wed, 29 Jul 2026 12:31:08 -0700 Andrii Nakryiko <[email protected]>…
Quoted 15 lines · Oleg Nesterov

kernel/events/uprobes.c static struct uprobe *hprobe_expire(struct hprobe *hprobe, bool get) · line 835

Quoted 9 lines · Breno Leitao
kernel/events/uprobes.c +1 -1
@@ -832,7 +832,7 @@ static struct uprobe *hprobe_expire(struct hprobe *hprobe, bool get)
              if (try_cmpxchg(&hprobe->state, &hstate, uprobe ? HPROBE_STABLE : HPROBE_GONE)) {
                      /* We won the race, we are the ones to unlock SRCU */
                      __srcu_read_unlock(&uretprobes_srcu, hprobe->srcu_idx);
-                     return get ? get_uprobe(uprobe) : uprobe;
+                     return get && uprobe ? get_uprobe(uprobe) : uprobe;

Well, looks "obviously correct". At least the current code is obviously wrong, it even checks uprobe != NULL 3 lines above.

Quoted 11 lines
Quoted 2 lines

Andrii ?

Yes, indeed, I'm more surprised this didn't come up much earlier (probably it's rare enough to have uretprobe with refcnt at zero during fork). The fix looks good, thanks!

Quoted 2 lines

Thanks, this seems good to me. (uprobe is NULL checked in try_cmpxchg(), so it could be NULL.)

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Can I pick this to probes/fixes branch?

tip/perf/urgent ?

Either way works. Can we update MAINTAINERS file if you will maintain uprobe in tip tree?

Well it is in kernel/events/ which MAINTAINERS should already assign to me, no?

maintainer
#7 lore raw
Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Masami Hiramatsu

On Thu, 30 Jul 2026 12:38:24 +0200

Either way works. Can we update MAINTAINERS file if you will maintain uprobe in tip tree?

Well it is in kernel/events/ which MAINTAINERS should already assign to me, no?

Confused... you are already listed as a maintainer. From MAINTAINERS:

UPROBES
M: Masami Hiramatsu <[email protected]>
M: Oleg Nesterov <[email protected]>
M: Peter Zijlstra <[email protected]>
L: [email protected]
L: [email protected]
S: Maintained
F: arch/*/include/asm/uprobes.h
F: arch/*/kernel/probes/uprobes.c
F: arch/*/kernel/uprobes.c
F: include/linux/uprobes.h
F: kernel/events/uprobes.c

Oleg.

maintainer
#8 lore raw
Quoted 13 lines · Oleg Nesterov
Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Masami Hiramatsu

On Thu, 30 Jul 2026 12:38:24 +0200

Either way works. Can we update MAINTAINERS file if you will maintain uprobe in tip tree?

Well it is in kernel/events/ which MAINTAINERS should already assign to me, no?

Confused... you are already listed as a maintainer. From MAINTAINERS:

Quoted 12 lines

UPROBES
M: Masami Hiramatsu <[email protected]>
M: Oleg Nesterov <[email protected]>
M: Peter Zijlstra <[email protected]>
L: [email protected]
L: [email protected]
S: Maintained
F: arch/*/include/asm/uprobes.h
F: arch/*/kernel/probes/uprobes.c
F: arch/*/kernel/uprobes.c
F: include/linux/uprobes.h
F: kernel/events/uprobes.c

Yes, but the 'PERFORMANCE EVENTS SUBSYSTEM' entry will also cover this. So I have it twice ;-)

PERFORMANCE EVENTS SUBSYSTEM
...
F: kernel/events/*

Anyway, patch should be in tip/perf/urgent now.

merge bot Applied
#9 lore raw
In reply to Breno Leitao
The following commit has been merged into the perf/urgent branch of tip:

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     cc679d7a6303e84d769f2afcde1fc51c51f127cd
Gitweb:        https://git.kernel.org/tip/cc679d7a6303e84d769f2afcde1fc51c51f127cd
Author:        Breno Leitao <[email protected]>
AuthorDate:    Wed, 29 Jul 2026 07:44:40 -07:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Fri, 31 Jul 2026 10:32:19 +02:00

uprobes: Fix NULL pointer dereference in hprobe_expire()

Forking a task that has a pending uretprobe can oops the kernel with a NULL pointer dereference in the clone() path:

  BUG: kernel NULL pointer dereference, address: 0000000000000018
  Oops: 0002 [#1] SMP NOPTI
  RIP: 0010:hprobe_expire
  CR2: 0000000000000018
  Call Trace:
   uprobe_copy_process
   copy_process
   kernel_clone
   __x64_sys_clone
   do_syscall_64
   entry_SYSCALL_64_after_hwframe

This was found on real hosts on Meta fleet.

I've got the impression that this is what is happening:

CPU 1 CPU 2 (traced task)
----- -------------------
hit uprobe, prepare_uretprobe():
hprobe LEASED, refcount >= 1
uprobe_unregister()

    put_uprobe(): refcount -> 0
                                 fork() -> dup_utask()
                                   hprobe_expire(hprobe, true)
                                     try_get_uprobe() -> NULL
                                     get_uprobe(NULL)   <-- Oops

Only take the extra reference when the uprobe is non-NULL; a NULL means it is gone and is the correct value to return.

---
 kernel/events/uprobes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/events/uprobes.c +1 -1
index 4084e92..6300b21 100644
@@ -830,7 +830,7 @@ static struct uprobe *hprobe_expire(struct hprobe *hprobe, bool get)
 		if (try_cmpxchg(&hprobe->state, &hstate, uprobe ? HPROBE_STABLE : HPROBE_GONE)) {
 			/* We won the race, we are the ones to unlock SRCU */
 			__srcu_read_unlock(&uretprobes_srcu, hprobe->srcu_idx);
-			return get ? get_uprobe(uprobe) : uprobe;
+			return get && uprobe ? get_uprobe(uprobe) : uprobe;
 		}
 
 		/*