BPF List
 help / color / mirror / Atom feed
* [PATCH bpf] bpf: Read the pid ns once in bpf_task_from_vpid()
@ 2026-12-03 20:13 Chris Mason
  0 siblings, 0 replies; only message in thread
From: Chris Mason @ 2026-12-03 20:13 UTC (permalink / raw)
  To: bpf, ast, daniel, andrii
  Cc: eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
	ihor.solodrai, juntong.deng, rhkrqnwk98, mason

bpf_task_from_vpid() dereferences a NULL pid namespace if another CPU
reaps current during the call.

Commit 50dff0061552 ("bpf: Fix NULL pointer dereference in
bpf_task_from_vpid()") made the kfunc return early if current has no pid
namespace. But find_task_by_vpid() reads current's namespace again. A
program can run on a task that is between exit_notify() and its last
context switch, and release_task() does not wait for that switch:

    bpf_task_from_vpid()               wait_task_zombie()
    ====================               ==================
    task_active_pid_ns(current)
      is not NULL
                                       release_task()
                                         __exit_signal()
                                           __unhash_process()
                                             detach_pid()
                                               thread_pid = NULL
    find_task_by_vpid()
      task_active_pid_ns(current)
        is NULL
      find_task_by_pid_ns()
        find_pid_ns()
          idr_find(&ns->idr)

The fix used here is to read current's namespace once, and to use that
value for both the test and the lookup.

Fixes: 675c3596ff32 ("bpf: Add bpf_task_from_vpid() kfunc")
Signed-off-by: Chris Mason <mason@kernel.org>
---
This is an AI generated fix, on top of v7.4-rc5.

 kernel/bpf/helpers.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index cebf9d8dfe36d..0f4a709682406 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -3076,13 +3076,16 @@ __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)
  */
 __bpf_kfunc struct task_struct *bpf_task_from_vpid(s32 vpid)
 {
+	struct pid_namespace *ns;
 	struct task_struct *p;
 
 	guard(rcu)();
-	if (!task_active_pid_ns(current))
+	/* A zombie current can be reaped at any time, so read its ns once. */
+	ns = task_active_pid_ns(current);
+	if (!ns)
 		return NULL;
 
-	p = find_task_by_vpid(vpid);
+	p = find_task_by_pid_ns(vpid, ns);
 	if (p)
 		p = bpf_task_acquire(p);
 

base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
-- 
2.55.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-03 20:13 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 20:13 [PATCH bpf] bpf: Read the pid ns once in bpf_task_from_vpid() Chris Mason

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox