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=1796206278; b=l457be9+u0tBvAmxnWTkavwpMNF8cmbsB0HLPKKEu6oBXqOm9lxRYvbcA3Pfu9ON1vuT QbIyLR9ZBk8RfbIVnnmmOafqLK0+d9PfPsZnWLmbHI/d1FZyjtX3QF+cf35SrrPIuUsDy 7N1oggG+NwOfjNYSGRydXzpiVlNasM0QGA1Hj5H4h9ofJOU0GXAAw0OHmHVHI67XxxIu+ rN+UU7QwPH5Gt8ZoblTJOZyvZJyd83TvsUlpiLItKSEmtoywQYMrEfWpV5cWt8sxjl6dt b0m55xrH1lRrhG3SuhJnSBz1sYy7a+Oy5XfZe4HVRlv2/LHl0jez7iCoWhwnr9TyTsw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796206278; h=Received:DKIM-Signature:From:To:Cc:Subject:In-Reply-To:References: Date:Message-ID:MIME-Version:Content-Type; bh=UYH/D+njXf3kH1006ta3PSwqi3lcD23kNX4b206Jtvw=; b=J1VaqYmpiI2te/YI4VODXbsTepUB7ASufa6I0phCZ6e1eg8xNkV1e5vaV0NCHxSDTxPd JpOtPiWPFGmO+XY9c/y2EGkIWuzf02iaBPrpM37L8zWkntO77oXjyXasaadRjzs93unJp rzViaKTjbNkBwmrxYyw7NXOOKhntI8ImVQcgs83fp/WmOJ0TrxvB/inZ6hk5w7oQbNpmo gjcsQhxxyXHutiu8WAn/Y12kSagnWK1WKwKIi9lloW68um9h+zeS4UYwnMRJ4Cjs+1nGn 1WwqjzPHS60J3srjJJ0koeXHI5CtwTbto5HL3xju472F+HP4A+y+O0VpJvQAvRHA/kg== 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=Kq87Uemx; 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 6D733606A323 for ; Wed, 2 Dec 2026 10:11:18 +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=Kq87Uemx Received: by smtp.kernel.org (Postfix) with ESMTPSA id 58D351F00A1E; Wed, 2 Dec 2026 10:11:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796206278; bh=UYH/D+njXf3kH1006ta3PSwqi3lcD23kNX4b206Jtvw=; h=From:To:Cc:Subject:In-Reply-To:References:Date; b=Kq87UemxTV5LHcq1otpV15oYj30ytd3B7djivFVmJgYWayHGvQy0v5uCMeWyJvyKD BgVp876gGeA+5Cl1sbmgZL7W3s1BBOhtynDrhPepnk/il7csY3MmT1cLnbgukywcYh y/6vmodIogAONeleDfIN/r74b669K0qlvGo+1yWeWnlMXfViBii+Bba1hr4VxyLNIr p8Nam/B6iA9d50sq8qzntYOZnFheGSokoxH01zJ8deXYUbPQWFz/wK4+xiX/E7O0Mr HcTUryBrEfMu/U8uROlNWQ6Mq2PLEC4Nw5CkQRDvCQtSLF+R/TZv55bE0iynzJWLwP IbCRwKqvFNJyw== From: Thomas Gleixner To: Christian Brauner Cc: Daniel Wade , Ingo Molnar , Peter Zijlstra , Darren Hart , Davidlohr Bueso , Andre Almeida , Sebastian Andrzej Siewior , Kyle Zeng , linux-kernel@vger.kernel.org Subject: [PATCH] futex/pi: Don't attach to tasks without MM In-Reply-To: <20261202-bilden-mimik-absetzbar-9f6b714bb231@brauner> References: <20261116132622.34793-1-danjwade95@gmail.com> <87h5gyqv01.ffs@fw13> <20261201-fotoreporter-fastnacht-siegreich-312e2a63bbba@brauner> <87v75cofql.ffs@fw13> <87se0go9fa.ffs@fw13> <20261202-bilden-mimik-absetzbar-9f6b714bb231@brauner> Date: Wed, 02 Dec 2026 11:11:15 +0100 Message-ID: <87mrqongak.ffs@fw13> 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 Content-Type: text/plain Daniel debugged and reported the following issue with user mode helper threads and PI futexes: attach_to_pi_owner() uses the TID stored in the futex word to find and attach to the owner task. It only rejects attaching to tasks which have PF_KTHREAD set. But a usermode helper thread runs with PF_KTHREAD cleared and has initially no MM. The task's TID is already visible in user space before it can acquire a MM in exec_mmap(), so user space can set the TID as owner of a shared futex in a file mapping, invoke futex(LOCK_PI) and attach a pi_state() to the task. That's initially harmless, but if the user mode helper exits before acquiring a MM or reaches mm_exit_exec_release() in begin_new_exec() the futex cleanup observes the pi_state and invokes exit_pi_state_list(). exit_pi_state_list() invokes guard(private_hash, current->mm), which tries to take a reference on the MM's private futex hash resulting in a NULL pointer dereference. Daniel proposed to check for tsk::mm in attach_to_pi_owner() after the futex state checks to distinguish it from a exiting task which cleared the MM pointer. That works, but that would require a WRITE_ONCE/READ_ONCE() annotation for storing tsk::mm to keep KCSAN silent and it works around the underlying problem: copy_process() initializes tsk::futex.state unconditionally to FUTEX_STATE_OK. That's inconsistent for tasks which do not have a MM when started. Fix this by: - Adding a new state FUTEX_STATE_NOMM - Making make the state initialization conditional on tsk::mm - Rejecting the attachment on a task with state FUTEX_STATE_NOMM. Additionally provide sanity checks in futex_exit_exec_release() to catch cases which end up there with inconsistent state. Fixes: 80367ad01d93 ("futex: Add basic infrastructure for local task local hash") Reported-by: Daniel Wade Signed-off-by: Thomas Gleixner Cc: stable@vger.kernel.org Closes: https://patch.msgid.link/20261116132622.34793-1-danjwade95@gmail.com --- include/linux/futex.h | 3 ++- kernel/futex/core.c | 38 ++++++++++++++++++++++++++++++++++---- kernel/futex/pi.c | 11 ++++++++++- 3 files changed, 46 insertions(+), 6 deletions(-) --- a/include/linux/futex.h +++ b/include/linux/futex.h @@ -61,6 +61,7 @@ union futex_key { #ifdef CONFIG_FUTEX enum { + FUTEX_STATE_NOMM, FUTEX_STATE_OK, FUTEX_STATE_EXITING, FUTEX_STATE_DEAD, @@ -70,7 +71,7 @@ static inline void futex_init_task(struc { memset(&tsk->futex, 0, sizeof(tsk->futex)); INIT_LIST_HEAD(&tsk->futex.pi_state_list); - tsk->futex.state = FUTEX_STATE_OK; + tsk->futex.state = tsk->mm ? FUTEX_STATE_OK : FUTEX_STATE_NOMM; mutex_init(&tsk->futex.exit_mutex); } --- a/kernel/futex/core.c +++ b/kernel/futex/core.c @@ -1549,6 +1549,25 @@ static void futex_cleanup_end(struct tas mutex_unlock(&tsk->futex.exit_mutex); } +/* This is safe lockless because all accessed state is thread local */ +static bool futex_exit_required(struct task_struct *tsk) +{ + switch (tsk->futex.state) { + case FUTEX_STATE_NOMM: + WARN_ON_ONCE(!list_empty(&tsk->futex.pi_state_list)); + return false; + case FUTEX_STATE_OK: + return !WARN_ON_ONCE(!tsk->mm); + case FUTEX_STATE_EXITING: + case FUTEX_STATE_DEAD: + WARN_ON_ONCE(1); + return false; + default: + WARN_ON_ONCE(1); + return false; + } +} + /* * Invoked from mm_exit_exec_release() to cleanup the robust lists and pi state * of the outgoing task. @@ -1564,9 +1583,18 @@ static void futex_cleanup_end(struct tas * * After the task has switched to the new mm it sets it to * FUTEX_STATE_OK again in futex_exec_done(). + * + * This also covers the case of a user mode helper, which starts with no MM and + * acquires a MM in exec(). The task's TID is visible to user space before that + * point, so user space could attach a PI futex to the task That's not possible + * because attach_to_pi_owner() catches that case due to their initial + * tsk::futex::state == FUTEX_STATE_NOMM. */ void futex_exit_exec_release(struct task_struct *tsk) { + if (!futex_exit_required(tsk)) + return; + futex_cleanup_begin(tsk); futex_cleanup(tsk); futex_cleanup_end(tsk); @@ -1581,11 +1609,13 @@ void futex_exec_done(struct task_struct * This store does not have to take tsk::futex::exit_mutex because the * phase where waiters block on it during state FUTEX_STATE_EXITING has * been finished when futex_cleanup_end() set the state to - * FUTEX_STATE_DEAD. + * FUTEX_STATE_DEAD. The same applies to (user mode helper) tasks which + * started with FUTEX_STATE_NOMM. * - * This transitions back from FUTEX_STATE_DEAD to FUTEX_STATE_OK. The - * ordering guarantee required here is that the previous store to - * tsk::mm in the calling code cannot be reordered against this store. + * This transitions from FUTEX_STATE_DEAD or FUTEX_STATE_NOMM to + * FUTEX_STATE_OK. The ordering guarantee required here is that the + * previous store to tsk::mm in the calling code cannot be reordered + * against this store. */ guard(raw_spinlock_irq)(&tsk->pi_lock); tsk->futex.state = FUTEX_STATE_OK; --- a/kernel/futex/pi.c +++ b/kernel/futex/pi.c @@ -508,7 +508,16 @@ static int attach_to_pi_owner(u32 __user return -EBUSY; } - int ret = handle_exit_race(uaddr, uval); + int ret; + /* + * futex::state is FUTEX_STATE_NOMM when a task starts without a + * MM. Only user mode helpers should reach this point because + * they do not have PF_KTHREAD set. + */ + if (p->futex.state == FUTEX_STATE_NOMM) + ret = -EPERM; + else + ret = handle_exit_race(uaddr, uval); raw_spin_unlock_irq(&p->pi_lock); put_task_struct(p);