From: Thomas Gleixner <tglx@kernel.org>
To: stable@vger.kernel.org
Cc: gregkh@linuxfoundation.org, Kyle Zeng <kylebot@openai.com>,
Peter Zijlstra <peterz@infradead.org>
Subject: [patch 6.18.y 1/5] futex/pi: Reject cross-mm private futex owners
Date: Tue, 25 Aug 2026 14:47:57 +0200 [thread overview]
Message-ID: <20260825091134.516981925@kernel.org> (raw)
In-Reply-To: 20260825091033.136528074@kernel.org
From: Kyle Zeng <kylebot@openai.com>
commit 59b3732f95dda1fbd2234514d35f4fb6b5bb6d85 upstream.
A private futex key borrows the waiter's mm without taking an mm_users
reference. Nevertheless, attach_to_pi_owner() currently accepts an owner
from a different address space and copies the private key into the owner's
PI state.
When that owner exits, exit_pi_state_list() uses the saved key to find the
hash bucket and acquires a reference to the waiter's private hash. If the
last user of the waiter's mm exits concurrently, futex_hash_free() frees
the hash while the owner still uses its bucket and reference.
Prevent this by validating in attach_to_pi_owner() that, for private
futexes, the owner mm and waiter mm are the same. Perform the check with
the owner's pi_lock held and after validating owner::futex::state to
serialize against a concurrent PI-state exit cleanup.
[ tglx: Amended comment ]
Fixes: 80367ad01d93 ("futex: Add basic infrastructure for local task local hash")
Signed-off-by: Kyle Zeng <kylebot@openai.com>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Acked-by: Peter Zijlstra <peterz@infradead.org>
Assisted-by: Codex:gpt-5.6-sol
Cc: stable@vger.kernel.org
---
kernel/futex/core.c | 9 ---------
kernel/futex/futex.h | 9 +++++++++
kernel/futex/pi.c | 20 ++++++++++++++++++++
3 files changed, 29 insertions(+), 9 deletions(-)
---
--- a/kernel/futex/core.c
+++ b/kernel/futex/core.c
@@ -133,15 +133,6 @@ static bool futex_ref_is_dead(struct fut
enum { FR_PERCPU = 0, FR_ATOMIC };
-static inline bool futex_key_is_private(union futex_key *key)
-{
- /*
- * Relies on get_futex_key() to set either bit for shared
- * futexes -- see comment with union futex_key.
- */
- return !(key->both.offset & (FUT_OFF_INODE | FUT_OFF_MMSHARED));
-}
-
static bool futex_private_hash_get(struct futex_private_hash *fph)
{
return futex_ref_get(fph);
--- a/kernel/futex/futex.h
+++ b/kernel/futex/futex.h
@@ -126,6 +126,15 @@ static inline bool should_fail_futex(boo
}
#endif
+static inline bool futex_key_is_private(union futex_key *key)
+{
+ /*
+ * Relies on get_futex_key() to set either bit for shared
+ * futexes -- see comment with union futex_key.
+ */
+ return !(key->both.offset & (FUT_OFF_INODE | FUT_OFF_MMSHARED));
+}
+
/*
* Hash buckets are shared by all the futex_keys that hash to the same
* location. Each key may have multiple futex_q structures, one for each task
--- a/kernel/futex/pi.c
+++ b/kernel/futex/pi.c
@@ -464,6 +464,26 @@ static int attach_to_pi_owner(u32 __user
return ret;
}
+ /*
+ * If the owner is about to exit() or exec() and tries to modify
+ * p::futex::exit_state it is serialized against this code by
+ * p::pi_lock.
+ */
+ if (IS_ENABLED(CONFIG_MMU) && futex_key_is_private(key)) {
+ /*
+ * A private futex key holds a pointer to the waiter's mm
+ * without holding a reference on it. So it must not be attached
+ * to an owner in a different address space. Otherwise that
+ * owner's exit cleanup could access the private hash after the
+ * key's mm is freed.
+ */
+ if (unlikely(p->mm != key->private.mm)) {
+ raw_spin_unlock_irq(&p->pi_lock);
+ put_task_struct(p);
+ return -EPERM;
+ }
+ }
+
__attach_to_pi_owner(p, key, ps);
raw_spin_unlock_irq(&p->pi_lock);
next prev parent reply other threads:[~2026-08-25 12:48 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-24 10:56 FAILED: patch "[PATCH] futex/pi: Reject cross-mm private futex owners" failed to apply to 6.18-stable tree gregkh
2026-08-25 12:47 ` [patch 6.18.y 0/5] futex: Stable backports for recent issues Thomas Gleixner
2026-08-25 12:47 ` Thomas Gleixner [this message]
2026-08-25 12:48 ` [patch 6.18.y 2/5] futex: Sanitize and document task_struct::futex::state transitions Thomas Gleixner
2026-08-25 12:48 ` [patch 6.18.y 3/5] futex/pi: Plug private futex exec() race Thomas Gleixner
2026-08-25 12:48 ` [patch 6.18.y 4/5] futex: Fix race on the initial mm->futex.phash.ref allocation Thomas Gleixner
2026-08-25 12:48 ` [patch 6.18.y 5/5] futex: Fix might_sleep() warning in futex_pivot_pending() Thomas Gleixner
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=20260825091134.516981925@kernel.org \
--to=tglx@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=kylebot@openai.com \
--cc=peterz@infradead.org \
--cc=stable@vger.kernel.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