The Linux Kernel Mailing 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, viro@zeniv.linux.org.uk,
	jack@suse.cz, tj@kernel.org, hannes@cmpxchg.org,
	mkoutny@suse.com, bsingharora@gmail.com, 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,
	cgroups@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	linux-mm@kvack.org, mason@kernel.org
Subject: [PATCH v2 5/8] bpf: Don't start a thread walk at a released task
Date: Sat, 05 Dec 2026 08:12:40 -0500	[thread overview]
Message-ID: <20261204233428.14861-6-mason@kernel.org> (raw)
In-Reply-To: <20261204233428.14861-1-mason@kernel.org>

bpf_iter_task_next() can hit a use-after-free if a
BPF_TASK_ITER_PROC_THREADS walk starts at a released task.

bpf_iter_task_new() doesn't check if the task is on the thread list. If it
isn't, thread_node.next can point to a freed thread:

    release_task(A)
      list_del_rcu(&A->thread_node)  /* A still points to B */
    release_task(B)                  /* freed after a grace period */

    bpf_iter_task_new(A)
    bpf_iter_task_next()             /* returns A */
    bpf_iter_task_next()
      __next_thread(B)               /* use-after-free */

The fix used here is to refuse the walk if pid_alive() fails.

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;


  parent reply	other threads:[~2026-12-05 13:13 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 13:11 [PATCH v2 0/8] fork, exit: fix use-after-frees on task pointers Chris Mason
2026-12-05 13:11 ` [PATCH v2 1/8] tsacct: don't dereference a stale group_leader Chris Mason
2026-12-05 13:12 ` [PATCH v2 2/8] exec, cgroup, prlimit: prepare for group_leader resets Chris Mason
2026-12-05 13:12 ` [PATCH v2 3/8] exit: reset a released task's parent and leader Chris Mason
2026-12-05 13:54   ` bot+bpf-ci
2026-12-05 13:12 ` [PATCH v2 4/8] fork: don't leave stale parent pointers in a new task Chris Mason
2026-12-05 13:12 ` Chris Mason [this message]
2026-12-05 13:12 ` [PATCH v2 6/8] drm/amdkfd: fix crashes if a debugged process exits Chris Mason
2026-12-05 13:13 ` [RFC PATCH v2 7/8] signal: add task_group_leader() Chris Mason
2026-12-05 13:13 ` [RFC PATCH v2 8/8] signal: move group_leader into signal_struct Chris Mason
2026-12-05 13:54   ` bot+bpf-ci

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=20261204233428.14861-6-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=bsingharora@gmail.com \
    --cc=cgroups@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=hannes@cmpxchg.org \
    --cc=ihor.solodrai@linux.dev \
    --cc=jack@suse.cz \
    --cc=jolsa@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=mingo@redhat.com \
    --cc=mkoutny@suse.com \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=song@kernel.org \
    --cc=tj@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --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