[PATCH] futex/pi: Don't attach to tasks without MM
From: Thomas Gleixner
Date: Wed Dec 02 2026 - 05:12:24 EST
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@xxxxxxxxx>
Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Closes: https://patch.msgid.link/20261116132622.34793-1-danjwade95@xxxxxxxxx
---
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);