From: Thomas Gleixner <tglx@kernel.org>
To: Christian Brauner <brauner@kernel.org>
Cc: Daniel Wade <danjwade95@gmail.com>,
Ingo Molnar <mingo@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
Darren Hart <dvhart@infradead.org>,
Davidlohr Bueso <dave@stgolabs.net>,
Andre Almeida <andrealmeid@igalia.com>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Kyle Zeng <kylebot@openai.com>,
linux-kernel@vger.kernel.org
Subject: [PATCH] futex/pi: Don't attach to tasks without MM
Date: Wed, 02 Dec 2026 11:11:15 +0100 [thread overview]
Message-ID: <87mrqongak.ffs@fw13> (raw)
In-Reply-To: <20261202-bilden-mimik-absetzbar-9f6b714bb231@brauner>
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 <danjwade95@gmail.com>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
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);
prev parent reply other threads:[~2026-12-02 10:11 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 13:26 [PATCH] futex/pi: Reject owners that have no mm Daniel Wade
2026-11-30 14:00 ` Thomas Gleixner
2026-12-01 20:31 ` Christian Brauner
2026-12-01 21:25 ` Thomas Gleixner
2026-12-01 23:42 ` Thomas Gleixner
2026-12-02 7:56 ` Christian Brauner
2026-12-02 10:11 ` Thomas Gleixner [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=87mrqongak.ffs@fw13 \
--to=tglx@kernel.org \
--cc=andrealmeid@igalia.com \
--cc=bigeasy@linutronix.de \
--cc=brauner@kernel.org \
--cc=danjwade95@gmail.com \
--cc=dave@stgolabs.net \
--cc=dvhart@infradead.org \
--cc=kylebot@openai.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
/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