[patch 6.18.y 1/5] futex/pi: Reject cross-mm private futex owners
Thomas Gleixner <[email protected]>
| Newsgroups | org.kernel.vger.stable |
|---|---|
| Message-ID | <[email protected]> |
From: Kyle Zeng <[email protected]> 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 <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Acked-by: Peter Zijlstra <[email protected]> Assisted-by: Codex:gpt-5.6-sol Cc: [email protected] --- 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);