BPF List
 help / color / mirror / Atom feed
From: Chris Mason <mason@kernel.org>
To: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net,
	andrii@kernel.org
Cc: eddyz87@gmail.com, memxor@gmail.com, martin.lau@linux.dev,
	song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org,
	emil@etsalapatis.com, ihor.solodrai@linux.dev,
	juntong.deng@outlook.com, rhkrqnwk98@gmail.com, mason@kernel.org
Subject: [PATCH bpf] bpf: Read the pid ns once in bpf_task_from_vpid()
Date: Thu,  3 Dec 2026 15:13:28 -0500	[thread overview]
Message-ID: <20261203201328.28379-1-mason@kernel.org> (raw)

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


                 reply	other threads:[~2026-12-03 20:13 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203201328.28379-1-mason@kernel.org \
    --to=mason@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jolsa@kernel.org \
    --cc=juntong.deng@outlook.com \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=rhkrqnwk98@gmail.com \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox