BPF List
 help / color / mirror / Atom feed
From: Chris Mason <mason@kernel.org>
To: brauner@kernel.org, oleg@redhat.com, ast@kernel.org,
	daniel@iogearbox.net, andrii@kernel.org, bpf@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: akpm@linux-foundation.org, peterz@infradead.org,
	mingo@redhat.com, kees@kernel.org, tj@kernel.org,
	eddyz87@gmail.com, memxor@gmail.com, yonghong.song@linux.dev,
	martin.lau@linux.dev, song@kernel.org, jolsa@kernel.org,
	emil@etsalapatis.com, ihor.solodrai@linux.dev,
	sched-ext@lists.linux.dev, mason@kernel.org
Subject: [PATCH 3/3] bpf: Don't start a thread walk at a released task
Date: Thu,  3 Dec 2026 15:06:12 -0500	[thread overview]
Message-ID: <20261203200612.26376-4-mason@kernel.org> (raw)
In-Reply-To: <20261203200612.26376-1-mason@kernel.org>

bpf_iter_task_next() hits a use-after-free on a task_struct if a
BPF_TASK_ITER_PROC_THREADS walk starts at a released task.

list_del_rcu() leaves thread_node.next of a released task in place, so
it dangles once the next thread is released and freed. But
bpf_iter_task_new() only checks the start task for NULL, and a trusted
task, e.g. a kptr taken from a map, may have been released long before
the RCU section that KF_RCU_PROTECTED requires:

    T and the next thread N are released, N is freed

    bpf_iter_task_new(&it, T, BPF_TASK_ITER_PROC_THREADS)
    bpf_iter_task_next()             returns T
      kit->pos = __next_thread(T)    T->thread_node.next is still N
    bpf_iter_task_next()             returns N
      kit->pos = __next_thread(N)    reads N->signal

The fix used here is to reject a start task that fails pid_alive(), as
next_tid() does.

Fixes: c68a78ffe2cb ("bpf: Introduce task open coded iterator kfuncs")
Signed-off-by: Chris Mason <mason@kernel.org>
---
 kernel/bpf/task_iter.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c
index 2ee7b3b3c10be..a5f0849500715 100644
--- a/kernel/bpf/task_iter.c
+++ b/kernel/bpf/task_iter.c
@@ -1145,6 +1145,9 @@ __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it,
 	case BPF_TASK_ITER_PROC_THREADS:
 		if (!task__nullable)
 			return -EINVAL;
+		/* ->thread_node of a released task is stale, see next_tid() */
+		if (!pid_alive(task__nullable))
+			return -ESRCH;
 		break;
 	default:
 		return -EINVAL;
-- 
2.55.0


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

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 20:06 [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers Chris Mason
2026-12-03 20:06 ` [PATCH 1/3] fork: make the creator the parent of an unlinked task Chris Mason
2026-12-03 20:06 ` [PATCH 2/3] exit: don't leave stale parent and leader pointers Chris Mason
2026-12-03 20:06 ` Chris Mason [this message]

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=20261203200612.26376-4-mason@kernel.org \
    --to=mason@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brauner@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jolsa@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=mingo@redhat.com \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=song@kernel.org \
    --cc=tj@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