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=1796476374; b=KgneNwUNjkOtg6pysId6HmVwRrTP4cerfHg/Ab/kwHEEmfLQe5gJqS1r/71Fz4pFjsYT TpRCs+KBGGwqvxeirvdbdUmmwPrPyRoUFwjZMBhPV6AF52iUDR1j1iUx7fDr52NTr07Ky jrYrBhC1eFSD5pdCV7aLqDyyQseEfQHUZzuP2Z4TMJdqjvgaMQ0SXlEbPR8LmnKFP3B4u osJu82QCWJ0eTrwzwoXrDn6fNei/M0iMnMj91sI5dQYBgTZ9KEEs63CGPLy162OMYRW2d wRuw1qqGN+h1Jjzd1u8Hkyj//XQGfGO/JahdubRlMRuAmxi1DjVH6rsyT9svWm0AkmQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796476374; h=Received:DKIM-Signature:Received:Received:X-ME-Sender: X-ME-Proxy-Cause:X-ME-Proxy:Feedback-ID:Received:X-Mailer: MIME-Version:X-ThreadId:Date:From:To:Cc:Message-Id:In-Reply-To: References:Subject:Content-Type:Content-Transfer-Encoding; bh=0eln7ggYcfiQHv+vERHjy8vzT7SxgrlgyRJkkJ1mOXE=; b=gIe7tN++kl2kiAeZJHbQCTozNiYEK6L0Vwg7Fq1H5dYDSKPT1Vzv5Y0GSyz1lmKrfQoC ESZf6c9KM43rP0GaQ70qbrEWA9TVj3giYG9Pol6vyfb97tn0z4PX8cm10hdwGviXboO4j nv6JjX5ctsn49E2YVVn+9oL6HtaV+gSGC3MazuIhSFFHZkNrbfAAuIy5Iq3qW1brCVENM x/WibHShmQel8wZPm2T6e/NLWEkYFOqlVyDAqHDliV3GGnM3ANEU+IdZSiWyNBa3WVlLe H4VBdcuoqTFDbtKwG0IjVHY9qQrcEVktF539pdKcuKrXvUlhW9lRzpbfwUr5VISWhHg== 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="WCURz/GQ"; 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 32558606A322 for ; Sat, 5 Dec 2026 13:12:54 +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=WCURz/GQ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 85C391F00A10; Sat, 5 Dec 2026 13:12:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796476374; bh=0eln7ggYcfiQHv+vERHjy8vzT7SxgrlgyRJkkJ1mOXE=; h=Date:From:To:Cc:In-Reply-To:References:Subject; b=WCURz/GQm0FjX0apHt+UA3NNEEJoyDppfwsnmGg0hHgqb/Av+ZNErmdEqskDm9KNx xCJfblfQNkHemlCDLVTupuUUt8s6BwCZ0SgO0eOQfR6fZ3FhOZ2u85YoU3D0vF6+u6 o+J//fZFODYWZw6m2HigV/kdvh6vImvdFqaisMRlALn19iREqWgWTYDGsAD3iGKvDG t3bEkeKLfWGkimQQHi/G1j0Cm1jzR/yAuxcHq8Qrhb2xHCGUvzotkR7u8U0UrGtr73 gMWh+zsOXo6P0DREvHsuXd8XiQbCEPf8nA5q7r35lSbTCxWJ1Izz4TgVRLLSsuJp/D 9bYOs0fbkJIeA== Received: from stl-compute-04.internal (stl-compute-04.internal [10.204.2.64]) by mailfauth.stl.internal (Postfix) with ESMTP id E33BB780043; Sat, 5 Dec 2026 08:12:51 -0500 (EST) Received: from stl-imap-13 ([10.204.2.104]) by stl-compute-04.internal (MEProxy); Sat, 05 Dec 2026 08:12:51 -0500 X-ME-Sender: X-ME-Proxy-Cause: dmFkZTG2z1lP+la6ZTh+FeIAPpdva49rz+ZMWlkFNpAYSMiW4Kf0223VUI632ajwD+deSv QSSGr+iYB7MdDLZwl6xQP40Qg6OGe3e0No2qdOpR5mKlhszFOGiV6kaHv0AUQvIwTZ1K7L 9NwaFiXVSL9VYHwKdepFNo7b4aKcAcrpCLbDeuAThYEzxnFtjUBriV8S9aYccE6Q+dp8Hi xcxbKadWpvyF+1Tf1mx6Tq3iKUclLku4EQJsb3RfwFG8QznyJsMKRpOglsEZjPmW2C+2c6 tQLlBZb1dljSsf32JCOxWeJi/PKrRtbJJnNdeAlZmK0FO+YLQyXPT4y/r22IDwEaJRLfYx Fu46sn3mIzu6LTEKSxRgGXV72tfSzwIfakZI61kZjssRyg1Q2HnNJFiAkjRhgwLzIbu5Tg oqTWh4DN82hoaSvDQ603PaPOns705MZ3sS4Y3LldUK8ns7DnjLDnHJcEBuznnUAGZq/Icu P1YpZJHAtDkkVkDxUA3Ty0BMUxVcDHvaqBkNiWGSLZT6EGrPP++Aa8o26OlDa93USjwKg1 aMDs+NQSWp2UKmWeSLpgPXJoRE9mIipGaDMryu4izkyHoIZgu/rfQVgE1il1pV1VJw6/sB n3nUQUiJDRviJKh5eXRCkwznkRMGohGT+ZLupyr2xkftyMOuA/AZDCB08phg X-ME-Proxy: Feedback-ID: iccae4b50:Fastmail Received: by mailuser.stl.internal (Postfix, from userid 501) id 98347F60088; Sat, 5 Dec 2026 08:12:51 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-ThreadId: AuvvtD4j5FdZ Date: Sat, 05 Dec 2026 08:12:31 -0500 From: "Chris Mason" 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 Message-Id: <20261204233428.14861-5-mason@kernel.org> In-Reply-To: <20261204233428.14861-1-mason@kernel.org> References: <20261204233428.14861-1-mason@kernel.org> Subject: [PATCH v2 4/8] fork: don't leave stale parent pointers in a new task Content-Type: text/plain Content-Transfer-Encoding: 7bit copy_process() causes a use-after-free in BPF programs if the forking task's parent is freed during the fork. forget_original_parent() doesn't find a new task until copy_process() has added it to the process tree. But BPF programs can dereference its real_parent before that, in the task_alloc LSM hook and in sched_ext's ops.init_task(): copy_process() parent exits ============== ============ p = dup_task_struct(current, node) /* copies real_parent */ forget_original_parent() /* doesn't find p */ release_task(parent) /* freed after a grace period */ security_task_alloc(p) BPF program: p->real_parent->pid /* use-after-free */ If the fork fails, the same can happen in ops.exit_task(), and later in a BPF program attached to free_task(), when the forking task can be gone too. pid_alive(p) catches none of this, as thread_pid is copied too. The idle threads from fork_idle() never get into the process tree, but BPF programs see them in sched_switch. Their real_parent is init's first thread, which is freed if another thread of init calls execve(). The fix used here is to use the forking task as the parent during the fork, and init_task if the new task never gets into the process tree. Signed-off-by: Chris Mason --- include/linux/pid.h | 1 + kernel/fork.c | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/include/linux/pid.h b/include/linux/pid.h index 185bf0cde2d14..26eee52fb1e30 100644 --- a/include/linux/pid.h +++ b/include/linux/pid.h @@ -319,6 +319,7 @@ static inline pid_t task_tgid_nr(struct task_struct *tsk) * @p: Task structure to be checked. * * Test if a process is not yet dead (at most zombie state) + * and no longer being forked. * If pid_alive fails, then pointers within the task structure * can be stale and must not be dereferenced. * diff --git a/kernel/fork.c b/kernel/fork.c index d55a7d21a8fb2..c99d7147f1f1d 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -928,6 +928,14 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node) if (err) goto free_tsk; + /* + * pid_alive() has to fail until copy_process() links the task. @orig is + * a safe parent until then, as it can't exit during its own fork. + */ + tsk->thread_pid = NULL; + RCU_INIT_POINTER(tsk->real_parent, orig); + RCU_INIT_POINTER(tsk->parent, orig); + err = alloc_thread_stack_node(tsk, node); if (err) goto free_tsk; @@ -2480,14 +2488,12 @@ __latent_entropy struct task_struct *copy_process( /* CLONE_PARENT re-uses the old parent */ if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { - p->real_parent = current->real_parent; p->parent_exec_id = current->parent_exec_id; if (clone_flags & CLONE_THREAD) p->exit_signal = -1; else p->exit_signal = current->group_leader->exit_signal; } else { - p->real_parent = current; p->parent_exec_id = current->self_exec_id; p->exit_signal = args->exit_signal; } @@ -2520,6 +2526,10 @@ __latent_entropy struct task_struct *copy_process( /* No more failure paths after this point. */ + /* Not earlier, as nobody updates it if the fork fails. */ + if (clone_flags & (CLONE_PARENT | CLONE_THREAD)) + p->real_parent = current->real_parent; + /* * One for the user space visible state that goes away when reaped. * One for the scheduler. @@ -2678,6 +2688,8 @@ __latent_entropy struct task_struct *copy_process( * can access it concurrently anymore, but prevent later access. */ p->mm = NULL; + /* current can exit before p is freed. */ + reset_parent_and_leader(p); WRITE_ONCE(p->__state, TASK_DEAD); exit_task_stack_account(p); put_task_stack(p); @@ -2718,6 +2730,7 @@ struct task_struct * __init fork_idle(int cpu) task = copy_process(&init_struct_pid, 0, cpu_to_node(cpu), &args); if (!IS_ERR(task)) { + reset_parent_and_leader(task); init_idle_pids(task); init_idle(task, cpu); }