From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796330544; b=HNlctkANCfsFR2hQHffs9nWwsKSFR6sv/0Zif+1Co3w0MJLDTIGMs7p/8ntd2LAqLIsh BqxLe0piIcqc1bWTLnTTHR7Mt5TgU/UqQFLMVlfPgVpEwjE4dL6uepind4zd8yP9sUE8X iLxMM1f+ZvyyCBkeecE+EGF8R6MSl+yv5WOlIIENaWp2MF5CitNBeCELJ+hycPJY6SjkJ ETmmYqrHAoO3DyA72pJjxe8bTg//K3LxJJS3eh0bzmNhkVlRp3aM1PKJFVxpm4EuEMqkK KeYlHad8eSCuKL+zs395ChTS12eEgaEbSedO/fw/RIxJlXzds26MtziEvI1QZu5/F0A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796330544; h=Received:DKIM-Signature:Content-Type:MIME-Version:Message-Id: In-Reply-To:References:Subject:From:To:Cc:Date; bh=9enXZsgl3AbvzXgqjcjX/qkNfkebpJ07AxyvsBCX6jQ=; b=RymCyXMBrqJONx9bvRjFmDghSyarOqQ1A7geSa814XnRfSvRXNnHKtBrrKRoEpWTuK/P R1zFYS1ch27wdkc35xrVVvuPjG4yCbuJeJ+IErzbiPzRwr+o96QFX6ffqkIAd9QCnvtX6 04P5FvVvj4IPfflen/W8YzJtqpzMJOfBbttNhdkX7L2clCg92KlDQOEWMdlPjQJGHFB9r WykUcXI3pv+fSC/WxLCtgP8BWVGCJ5HabR6/crAzE74faGYmAYwNsXjtz2mMPY6yC2vSR cVwlA1ruONGoCV2cnCGkXj70+CNWNEMzKOT8w8lcWVsdthyU62UjObMWO3gGZFWWRHg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=L8qJ+gxo; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C3C03606A32C; Thu, 3 Dec 2026 20:42:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=L8qJ+gxo Received: by smtp.kernel.org (Postfix) with ESMTPSA id C67551F000CE; Thu, 3 Dec 2026 20:42:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796330544; bh=9enXZsgl3AbvzXgqjcjX/qkNfkebpJ07AxyvsBCX6jQ=; h=In-Reply-To:References:Subject:From:To:Cc:Date; b=L8qJ+gxoKaNQMtz9QiSBL8bbr7LcLfQ8Ekd3/mElj2tNoSC++90a2kLp1HGbpw6/a i/zsZQJmaIH6RPaNiqwgqqRMhWSrlfh+zVCbaG4WGp8KxKfUeGUsdYHBe3Tn7QzUe6 HHvYcCNqj1AGGwRQrpU4JHwyionHpMzXrkvFSpJrZXUyErPtAJl3UhJdNYXHgKLU8z l8RrLFz9hC+GTE3yNIQFGehKo3KtZI37vtaQC/KGZEPoiJE1h98bnQMK7+vj7RkRJy D/JQeGDeJ2Df22OITDmk4f5sBpT+Jm0+3Avt/K4tdRZQULCeOYU4fQs18VqpiVyZOu mn3jSq0JFbKTg== Content-Type: multipart/mixed; boundary="===============7999093304141819722==" X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Message-Id: In-Reply-To: <20261203200612.26376-4-mason@kernel.org> References: <20261203200612.26376-4-mason@kernel.org> Subject: Re: [PATCH 3/3] bpf: Don't start a thread walk at a released task From: bot+bpf-ci@kernel.org To: mason@kernel.org,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,ast@kernel.org,andrii@kernel.org,daniel@iogearbox.net,martin.lau@kernel.org,eddyz87@gmail.com,yonghong.song@linux.dev,mason@kernel.org,ihor.solodrai@linux.dev Date: Thu, 3 Dec 2026 20:42:21 +0000 (UTC) --===============7999093304141819722== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit > bpf: Don't start a thread walk at a released task TL;DR: bpf_iter_task_new()'s pid_alive() check may miss a task still inside copy_process(). A BPF program could then walk a stale thread_node into a freed sibling thread, a possible use-after-free. > diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c > --- a/kernel/bpf/task_iter.c > +++ b/kernel/bpf/task_iter.c > @@ -1161,6 +1161,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; This isn't a bug in the patch itself, but does the pid_alive() check leave a similar case uncovered for a task that is still inside copy_process()? dup_task_struct() copies the parent with arch_dup_task_struct(), and nothing in copy_process() touches ->thread_pid until: init_task_pid(p, PIDTYPE_PID, pid) so pid_alive(p) is true for the whole fork. In that window p->thread_node is a byte copy of current->thread_node, and its ->next points at &X->thread_node for a sibling thread X. That pointer holds no reference and no RCU section covers it, so X can exit, go through release_task() and delayed_put_task_struct(), and be freed while copy_process() sleeps in copy_creds(), alloc_pid(), cgroup_can_fork() and so on. BPF programs get a trusted pointer to p in this window, from the LSM task_alloc hook (security_task_alloc(), bpf_lsm_task_alloc is not in untrusted_lsm_hooks), from sched_ext ops.init_task() via sched_cgroup_fork()->scx_fork(), and from ops.exit_task() via scx_cancel_fork(). bpf_iter_task_new() is in common_kfunc_set, so these programs can call it: copy_process() sibling X of current dup_task_struct() p->thread_node.next = &X->thread_node do_exit() -> release_task(X) , X freed security_task_alloc(p) / scx_fork(p) bpf_iter_task_new(&it, p, BPF_TASK_ITER_PROC_THREADS) pid_alive(p) == 1 thread_pid copied from current bpf_iter_task_next() returns p kit->pos = __next_thread(p) reads stale p->thread_node.next == X bpf_iter_task_next() returns freed X to the program __next_thread(X) reads X->signal next_tid() in fs/proc/base.c can rely on pid_alive() because procfs only reaches hashed tasks, but that doesn't hold for the start tasks BPF can pass here. Is the PROC_THREADS walk intended to be safe for a task that is still being forked? If so, could the start-task check also reject unlinked tasks, for example a task whose ->thread_pid is not yet attached, or one with rcu_users == 0 which bpf_task_acquire() already treats as unusable? --- AI reviewed your patch. Please fix the bug or email reply why it's not a bug. See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md CI run summary: https://github.com/kernel-patches/bpf/actions/runs/47195022171 --===============7999093304141819722==--