Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Artem Dinaburg <artem@trailofbits.com>
To: stable@vger.kernel.org
Cc: andrealmeid@igalia.com, dave@stgolabs.net, dvhart@infradead.org,
	linux-kernel@vger.kernel.org, mason@kernel.org, mingo@redhat.com,
	paulmck@kernel.org, peterz@infradead.org, tglx@kernel.org
Subject: [PATCH 6.18.y] futex: Fix private hash use-after-free on resize
Date: Mon, 30 Nov 2026 01:13:23 -0500	[thread overview]
Message-ID: <20261130061323.13637-1-artem@trailofbits.com> (raw)

From: Chris Mason <mason@kernel.org>
Date: Thu, 1 Oct 2026 13:50:22 +0000

[ Upstream commit f35e3b5784221654f9cdbd6222275a7fa203f6c3 ]

poll_state_synchronize_rcu(mm->futex.phash.batches) is used by
futex_ref_drop() to check that a grace period has passed since the
current hash was published. This relies on batches referencing a grace
period which started after the hash pointer was assigned.

__futex_pivot_hash() sets mmph->batches before it replaces mmph->hash:

    scoped_guard(rcu) {
            mmph->batches = get_state_synchronize_rcu();
            rcu_assign_pointer(mmph->hash, new);
    }

The scoped_guard(rcu) doesn't stop new grace periods from starting, and
if one starts between those two assignments, futex_ref_drop() can move
forward while a reader still holds a pointer to the old hash.

Fix things by setting mmph->batches after assigning mmph->hash.  The
scoped_guard(rcu) isn't needed, so let's drop that as well.

Fixes: 56180dd20c19 ("futex: Use RCU-based per-CPU reference counting instead of rcuref_t")
Assisted-by: kres
Signed-off-by: Chris Mason <mason@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Paul E. McKenney <paulmck@kernel.org>
Link: https://patch.msgid.link/20261001135022.2220288-1-mason@kernel.org
[ Backport to 6.18.y: 6.18 predates commit 1f7f4816b9b0 ("futex: Move
  futex related mm_struct data into a struct"), so use mm->futex_phash
  and mm->futex_batches instead of mmph->hash and mmph->batches. ]
Signed-off-by: Artem Dinaburg <artem@trailofbits.com>
---
Hi,

Please apply this to 6.18.y. It fixes CVE-2026-98788.

The upstream commit does not apply as-is because 6.18 still uses the
old mm_struct futex field names. The backport only renames those
fields; the code change is otherwise identical.

The fix has been in mainline since 7.3-rc6 and was backported to 7.2.y
in 7.2.10. Kernels 6.12.y and older do not contain 56180dd20c19 and
are not affected.

6.18.y already has the related futex fixes from 6.18.46 and 6.18.47:

  futex: Fix race in futex_pivot_pending() during private hash resize
  futex: Fix might_sleep() warning in futex_pivot_pending()
  futex: Avoid private hash use-after-free on final put
  futex: Fix race on the initial mm->futex.phash.ref allocation

None of them changes the ordering in __futex_pivot_hash(), so 6.18.y
still samples the RCU grace-period state before publishing the new
hash.

Compile-tested on 6.18.65 with CONFIG_FUTEX_PRIVATE_HASH=y.

I used an LLM to help adapt the patch and draft this email, and
reviewed the result myself.

Thanks,
Artem Dinaburg

 kernel/futex/core.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/kernel/futex/core.c b/kernel/futex/core.c
index e6fbeb2ac19014e08e636623d6cf2ba3a67c85f5..cb8cfa81d71ac4dc1cd4697624871e8d3bbd50dd 100644
--- a/kernel/futex/core.c
+++ b/kernel/futex/core.c
@@ -214,10 +214,12 @@ static bool __futex_pivot_hash(struct mm_struct *mm,
 		futex_rehash_private(fph, new);
 	}
 	new->state = FR_PERCPU;
-	scoped_guard(rcu) {
-		mm->futex_batches = get_state_synchronize_rcu();
-		rcu_assign_pointer(mm->futex_phash, new);
-	}
+	rcu_assign_pointer(mm->futex_phash, new);
+	/*
+	 * mm->futex_batches must reference a grace period which started after
+	 * mm->futex_phash was assigned. See futex_ref_drop().
+	 */
+	mm->futex_batches = get_state_synchronize_rcu();
 	kvfree_rcu(fph, rcu);
 	return true;
 }

                 reply	other threads:[~2026-11-30  6:13 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261130061323.13637-1-artem@trailofbits.com \
    --to=artem@trailofbits.com \
    --cc=andrealmeid@igalia.com \
    --cc=dave@stgolabs.net \
    --cc=dvhart@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mason@kernel.org \
    --cc=mingo@redhat.com \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=stable@vger.kernel.org \
    --cc=tglx@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