* [PATCH] futex/pi: Reject owners that have no mm
@ 2026-11-16 13:26 Daniel Wade
2026-11-30 14:00 ` Thomas Gleixner
0 siblings, 1 reply; 5+ messages in thread
From: Daniel Wade @ 2026-11-16 13:26 UTC (permalink / raw)
To: Thomas Gleixner, Ingo Molnar, Peter Zijlstra, Darren Hart
Cc: Davidlohr Bueso, Andre Almeida, Sebastian Andrzej Siewior,
Kyle Zeng, linux-kernel
attach_to_pi_owner() attaches PI state to the task named in the futex word
and rejects kernel threads, which cannot own a user space futex.
A usermode helper thread runs with PF_KTHREAD cleared and acquires an mm at
exec(). While it is in that state, any task that can see its TID can name
it as the owner of a shared PI futex. Commit 59b3732f95dd ("futex/pi:
Reject cross-mm private futex owners") compares the owner mm with the
waiter mm, which applies to private keys only.
The helper then execs or exits, exit_pi_state_list() takes a reference on
the private hash of current->mm, and futex_private_hash() dereferences a
NULL mm. The oops path calls do_exit(), which runs the futex cleanup again
and blocks on the futex exit_mutex, so the thread stays there and with
preemptible RCU grace periods stop completing. Module auto-loading starts
usermode helpers on behalf of ordinary users, so an unprivileged task
reaches this.
Reject an owner that has no mm with -EPERM, as for a kernel thread, after
the futex state check so that an exited task still goes through
handle_exit_race().
Fixes: 80367ad01d93 ("futex: Add basic infrastructure for local task local hash")
Signed-off-by: Daniel Wade <danjwade95@gmail.com>
Cc: stable@vger.kernel.org
---
Tested on mainline b3dfbde62cf1 (kernel/futex identical to v7.4-rc3),
x86-64 with KASAN and lockdep, 2 vCPUs under KVM, as an ordinary user.
Without the patch a usermode helper accepted PI ownership 130 times, with
no EPERM refusal, and the kernel oopsed 22 s in:
Oops: stack segment: 0000 [#1] SMP KASAN NOPTI
RIP: 0010:futex_private_hash+0x4c/0x2b0
Call Trace:
futex_exit_exec_release
mm_exit_exec_release
begin_new_exec
call_usermodehelper_exec_async
after which the kworker never exited and RCU stall warnings repeated. With
the patch: 201472 refusals, 5822 owners that do have an mm still accepted,
no oops and no stall.
Not tested on other architectures.
kernel/futex/pi.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/kernel/futex/pi.c b/kernel/futex/pi.c
index 98f1b962e59a..77e88dd5dc22 100644
--- a/kernel/futex/pi.c
+++ b/kernel/futex/pi.c
@@ -515,6 +515,18 @@ static int attach_to_pi_owner(u32 __user *uaddr, u32 uval, union futex_key *key,
return ret;
}
+ /*
+ * A usermode helper thread runs with PF_KTHREAD cleared and acquires
+ * an mm at exec(), so the kernel thread test above lets it through.
+ * Its exec() or exit() cleanup runs exit_pi_state_list(), which uses
+ * current->mm.
+ */
+ if (unlikely(!p->mm)) {
+ raw_spin_unlock_irq(&p->pi_lock);
+ put_task_struct(p);
+ return -EPERM;
+ }
+
if (IS_ENABLED(CONFIG_MMU) && futex_key_is_private(key)) {
/*
* A private futex key holds a pointer to the waiter's mm
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] futex/pi: Reject owners that have no mm
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
0 siblings, 1 reply; 5+ messages in thread
From: Thomas Gleixner @ 2026-11-30 14:00 UTC (permalink / raw)
To: Daniel Wade, Ingo Molnar, Peter Zijlstra, Darren Hart
Cc: Davidlohr Bueso, Andre Almeida, Sebastian Andrzej Siewior,
Kyle Zeng, linux-kernel
On Mon, Nov 16 2026 at 23:56, Daniel Wade wrote:
> attach_to_pi_owner() attaches PI state to the task named in the futex word
> and rejects kernel threads, which cannot own a user space futex.
>
> A usermode helper thread runs with PF_KTHREAD cleared and acquires an mm at
> exec(). While it is in that state, any task that can see its TID can name
> it as the owner of a shared PI futex.
> The helper then execs or exits, exit_pi_state_list() takes a reference on
> the private hash of current->mm, and futex_private_hash() dereferences a
> NULL mm.
Indeed.
> The oops path calls do_exit(), which runs the futex cleanup again
> and blocks on the futex exit_mutex, so the thread stays there and with
> preemptible RCU grace periods stop completing.
That's not really relevant because that's just a subsequent failure.
> Module auto-loading starts usermode helpers on behalf of ordinary
> users, so an unprivileged task reaches this.
> diff --git a/kernel/futex/pi.c b/kernel/futex/pi.c
> index 98f1b962e59a..77e88dd5dc22 100644
> --- a/kernel/futex/pi.c
> +++ b/kernel/futex/pi.c
> @@ -515,6 +515,18 @@ static int attach_to_pi_owner(u32 __user *uaddr, u32 uval, union futex_key *key,
> return ret;
> }
>
> + /*
> + * A usermode helper thread runs with PF_KTHREAD cleared and acquires
> + * an mm at exec(), so the kernel thread test above lets it through.
> + * Its exec() or exit() cleanup runs exit_pi_state_list(), which uses
> + * current->mm.
That comment is confusing at best.
> + */
> + if (unlikely(!p->mm)) {
> + raw_spin_unlock_irq(&p->pi_lock);
> + put_task_struct(p);
> + return -EPERM;
> + }
Why putting this into the pi_lock held region? p->pi_lock protects
nothing here and the access to p->mm is racy in any case and will
trigger KCSAN splats.
So the below is sufficient.
Thanks,
tglx
---
--- a/kernel/futex/pi.c
+++ b/kernel/futex/pi.c
@@ -473,7 +473,13 @@ static int attach_to_pi_owner(u32 __user
if (!p)
return handle_exit_race(uaddr, uval);
- if (unlikely(p->flags & PF_KTHREAD)) {
+ /*
+ * Don't attach to kernel threads or user mode helpers without mm. The
+ * access to p->mm is racy as the mm could be swapped right at this
+ * moment, but the only relevant information at this point is whether
+ * it's NULL or not.
+ */
+ if (unlikely(p->flags & PF_KTHREAD || !data_race(p->mm))) {
put_task_struct(p);
return -EPERM;
}
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] futex/pi: Reject owners that have no mm
2026-11-30 14:00 ` Thomas Gleixner
@ 2026-12-01 20:31 ` Christian Brauner
2026-12-01 21:25 ` Thomas Gleixner
0 siblings, 1 reply; 5+ messages in thread
From: Christian Brauner @ 2026-12-01 20:31 UTC (permalink / raw)
To: Thomas Gleixner, Daniel Wade, Ingo Molnar, Peter Zijlstra, Darren Hart
Cc: Davidlohr Bueso, Andre Almeida, Sebastian Andrzej Siewior,
Kyle Zeng, linux-kernel
On Mon, Nov 30, 2026 at 03:00:46PM +0100, Thomas Gleixner wrote:
> Why putting this into the pi_lock held region? p->pi_lock protects
> nothing here and the access to p->mm is racy in any case and will
> trigger KCSAN splats.
>
> So the below is sufficient.
I'm not sure. I found the same bug while working on cleaning up exec
again. Afaict, checking p->mm before the futex state check breaks exit
race handling. But I'm not an expert so double-check. An exiting task
drops its mm after its futex state went to FUTEX_STATE_DEAD:
do_exit()
-> exit_mm()
-> mm_exit_exec_release()
-> futex_exit_exec_release()
-> futex_cleanup_end() // FUTEX_STATE_DEAD
-> current->mm = NULL
and it can still be found by its TID until it's reaped. IOW, a task that
has just exited also has no mm, because exit_mm() sets current->mm =
NULL after the futex state went DEAD. So a dead owner hits the new check
and the waiter gets -EPERM which means glibc aborts. So I think this
breaks things.
Checking under pi_lock means a dead task returns at the state check and
never reaches it. Only state OK with no mm gets -EPERM which is a umh
tha that never exec'ed.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] futex/pi: Reject owners that have no mm
2026-12-01 20:31 ` Christian Brauner
@ 2026-12-01 21:25 ` Thomas Gleixner
2026-12-01 23:42 ` Thomas Gleixner
0 siblings, 1 reply; 5+ messages in thread
From: Thomas Gleixner @ 2026-12-01 21:25 UTC (permalink / raw)
To: Christian Brauner, Daniel Wade, Ingo Molnar, Peter Zijlstra, Darren Hart
Cc: Davidlohr Bueso, Andre Almeida, Sebastian Andrzej Siewior,
Kyle Zeng, linux-kernel
On Tue, Dec 01 2026 at 21:31, Christian Brauner wrote:
> On Mon, Nov 30, 2026 at 03:00:46PM +0100, Thomas Gleixner wrote:
>> Why putting this into the pi_lock held region? p->pi_lock protects
>> nothing here and the access to p->mm is racy in any case and will
>> trigger KCSAN splats.
>>
>> So the below is sufficient.
>
> I'm not sure. I found the same bug while working on cleaning up exec
> again. Afaict, checking p->mm before the futex state check breaks exit
> race handling. But I'm not an expert so double-check. An exiting task
> drops its mm after its futex state went to FUTEX_STATE_DEAD:
>
> do_exit()
> -> exit_mm()
> -> mm_exit_exec_release()
> -> futex_exit_exec_release()
> -> futex_cleanup_end() // FUTEX_STATE_DEAD
> -> current->mm = NULL
>
> and it can still be found by its TID until it's reaped. IOW, a task that
> has just exited also has no mm, because exit_mm() sets current->mm =
> NULL after the futex state went DEAD. So a dead owner hits the new check
> and the waiter gets -EPERM which means glibc aborts. So I think this
> breaks things.
Fair enough.
> Checking under pi_lock means a dead task returns at the state check and
> never reaches it. Only state OK with no mm gets -EPERM which is a umh
> tha that never exec'ed.
But that really needs to be spelled out in the change log and comments.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] futex/pi: Reject owners that have no mm
2026-12-01 21:25 ` Thomas Gleixner
@ 2026-12-01 23:42 ` Thomas Gleixner
0 siblings, 0 replies; 5+ messages in thread
From: Thomas Gleixner @ 2026-12-01 23:42 UTC (permalink / raw)
To: Christian Brauner, Daniel Wade, Ingo Molnar, Peter Zijlstra, Darren Hart
Cc: Davidlohr Bueso, Andre Almeida, Sebastian Andrzej Siewior,
Kyle Zeng, linux-kernel
On Tue, Dec 01 2026 at 22:25, Thomas Gleixner wrote:
> On Tue, Dec 01 2026 at 21:31, Christian Brauner wrote:
>> I'm not sure. I found the same bug while working on cleaning up exec
>> again. Afaict, checking p->mm before the futex state check breaks exit
>> race handling. But I'm not an expert so double-check. An exiting task
>> drops its mm after its futex state went to FUTEX_STATE_DEAD:
>>
>> do_exit()
>> -> exit_mm()
>> -> mm_exit_exec_release()
>> -> futex_exit_exec_release()
>> -> futex_cleanup_end() // FUTEX_STATE_DEAD
>> -> current->mm = NULL
>>
>> and it can still be found by its TID until it's reaped. IOW, a task that
>> has just exited also has no mm, because exit_mm() sets current->mm =
>> NULL after the futex state went DEAD. So a dead owner hits the new check
>> and the waiter gets -EPERM which means glibc aborts. So I think this
>> breaks things.
>
> Fair enough.
>
>> Checking under pi_lock means a dead task returns at the state check and
>> never reaches it. Only state OK with no mm gets -EPERM which is a umh
>> tha that never exec'ed.
>
> But that really needs to be spelled out in the change log and comments.
I just read it again and it was spelled out in the change log in passing
in the last sentence but then the comment in the patch really confused
me.
But what really bothers me is the fact that the fix focusses on that
particular exit issue which makes the kernel go belly up.
Though it does not address the actually underlying problem:
task::futex::state is unconditionally initializied to FUTEX_STATE_OK
in copy_process().
That was "correct" before the private hash came around, but only if
"correct" is defined as "does not explode".
So I rather go and make this very explicit and not focussed on the
potential exit/exec cleanup accessing current->mm.
Something like the untested below.
Thanks,
tglx
---
diff --git a/include/linux/futex.h b/include/linux/futex.h
index a895d8c69d66..5cee821081f8 100644
--- 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(struct task_struct *tsk)
{
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);
}
diff --git a/kernel/futex/core.c b/kernel/futex/core.c
index a240638f69e5..f07e86d6bc2c 100644
--- a/kernel/futex/core.c
+++ b/kernel/futex/core.c
@@ -1549,6 +1549,26 @@ static void futex_cleanup_end(struct task_struct *tsk)
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:
+ WARN_ON_ONCE(!tsk->mm);
+ return true;
+ 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 +1584,19 @@ static void futex_cleanup_end(struct task_struct *tsk)
*
* 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, but that's
+ * prevented because attach_to_pi_owner() catches that case as those tasks have
+ * tsk::futex::state == FUTEX_STATE_NOMM up to the point where they invoke
+ * futex_exec_done().
*/
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 +1611,13 @@ void futex_exec_done(struct task_struct *tsk)
* 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;
diff --git a/kernel/futex/pi.c b/kernel/futex/pi.c
index 98f1b962e59a..1220c09e5552 100644
--- a/kernel/futex/pi.c
+++ b/kernel/futex/pi.c
@@ -508,7 +508,16 @@ static int attach_to_pi_owner(u32 __user *uaddr, u32 uval, union futex_key *key,
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 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);
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-01 23:42 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®