From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7754E22E3E9 for ; Wed, 25 Mar 2026 15:22:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774452137; cv=none; b=fyXQXkgqHUfXSXIm880jreeyqH4m9rAZLUaI4oNHHcJr6TnGVhb/47iulPET2q3GwK3daErc6Qa+wOJ0wiReXfEFIQTdn2RVtFDSdFr5GLQIRI6d5F/yxO8G/PLHY9UVSXi0soIB+do7NMKuqbozzbOAhlI2a3ppWe/xgk5I8+4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774452137; c=relaxed/simple; bh=31UJnV9IDfoklhOtZoNlFPBxg624YRh4P0t8hX+LcsM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=rbkr+IJIRmV3Dtxc4QtiGdg/7B4MMKxcWmKW2ZVYpBoLKch0UlzlZrzCOHqJOExkH9EDY6/2qbqh5xcIMD3ymc4XyzKJOZ5A2Hu9GjKTCEepWwzaAG4igod7h1Sfvrq6IaTKxjsxRmj0D0klRXQkpfVdOybZ5B8tWVe0yDLa2vI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=ngPfHWaN; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="ngPfHWaN" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Transfer-Encoding: Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Sender:Reply-To:Content-ID:Content-Description; bh=Ouu8y2VMlIQ3NjrnnBtkXQCrxDc+lrHn6mGN4M1Qu2g=; b=ngPfHWaNuTdEWFecBlMqJVZLbZ UjHCci+mc4+m2b0xrxeof+0JLPJLA5Ay080MBB0KUiBec1WZzBd6QVghx4Hc6onu3YjOD1it1aNRm nbU4BkhtlbPZZz9KQaxZ8iLrkrVd7aEQHd4YsUzIA+M1lAFddPnAyZM3/FUuaWxqufFbCAooNN7OE CqWF2vOwAqgGUgQtMvDGC2UKqpME31EUzTF2OKPorCoZkumQ5dcsYt23fTpah08Z9lQBHkutGPPOm EgPyQm29HSb12y+Lg3jEAQXpwgst++yyfiwkQMOPU6oMGUQ1rjlSHkwzrfAt4sahU7l392dF0/gTq CUtY6WsA==; Received: from 77-249-17-252.cable.dynamic.v4.ziggo.nl ([77.249.17.252] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98.2 #2 (Red Hat Linux)) id 1w5Q3c-0000000G3qq-22UA; Wed, 25 Mar 2026 15:22:08 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id C80DD300312; Wed, 25 Mar 2026 16:22:06 +0100 (CET) Date: Wed, 25 Mar 2026 16:22:06 +0100 From: Peter Zijlstra To: Eric Dumazet Cc: "David Hildenbrand (Arm)" , Thomas Gleixner , Hao-Yu Yang , mingo@redhat.com, linux-kernel@vger.kernel.org, Andrew Morton , linux-mm@kvack.org, Lorenzo Stoakes , "Liam R. Howlett" Subject: Re: [PATCH v2] futex: Use-after-free between futex_key_to_node_opt and vma_replace_policy Message-ID: <20260325152206.GH3738786@noisy.programming.kicks-ass.net> References: <20260313124756.52461-1-naup96721@gmail.com> <87a4vyihlx.ffs@tglx> <20260324140019.GE3738010@noisy.programming.kicks-ass.net> <87fr5pgp5x.ffs@tglx> <20260324174418.GB1850007@noisy.programming.kicks-ass.net> <20260325151445.GH3738010@noisy.programming.kicks-ass.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Wed, Mar 25, 2026 at 08:19:24AM -0700, Eric Dumazet wrote: > On Wed, Mar 25, 2026 at 8:14 AM Peter Zijlstra wrote: > > > > On Tue, Mar 24, 2026 at 09:27:41PM +0100, David Hildenbrand (Arm) wrote: > > > So IIUC, futex_key_to_node_opt() looks up a VMA under RCU, without > > > holding the mmap lock. Concurrent mmap-write lock is detected by using > > > the mmap_lock_speculate_try_begin()/mmap_lock_speculate_retry() seqcount. > > > > > > After looking up the VMA, we access the VMA policy. > > > > > > vma_policy() does a straight vma->vm_policy. > > > > > > What prevents the compiler here to do some load tearing while it is > > > getting modified by mbind()? Or what stops the writer side to to some > > > store tearing? > > > > > > Shouldn't we be using at least READ_ONCE/WRITE_ONCE() etc? > > > > Bah, at that point we might as well RCU the thing like so, I suppose. > > > > --- a/mm/mempolicy.c > > +++ b/mm/mempolicy.c > > @@ -1026,7 +1026,7 @@ static int vma_replace_policy(struct vm_ > > } > > > > old = vma->vm_policy; > > - vma->vm_policy = new; /* protected by mmap_lock */ > > + rcu_assign_pointer(vma->vm_policy, new); /* protected by mmap_lock */ > > mpol_put(old); > > > > return 0; > > diff --git a/kernel/futex/core.c b/kernel/futex/core.c > > index 4bacf5565368..6336a80e3dca 100644 > > --- a/kernel/futex/core.c > > +++ b/kernel/futex/core.c > > @@ -342,7 +342,7 @@ static int __futex_key_to_node(struct mm_struct *mm, unsigned long addr) > > if (!vma) > > return FUTEX_NO_NODE; > > > > - mpol = vma_policy(vma); > > + mpol = rcu_dereference_raw(vma->vm_policy); > > if (!mpol) > > return FUTEX_NO_NODE; > > Yes, but sparse will bite :) Oh gawd, yes, and then people will go 'fix' it and it'll turn into an unholy mess. > READ_ONCE()/WRITE_ONCE() on these two locations seems acceptable. Fair enough. Like so then.. --- a/kernel/futex/core.c +++ b/kernel/futex/core.c @@ -342,7 +342,7 @@ static int __futex_key_to_node(struct mm if (!vma) return FUTEX_NO_NODE; - mpol = vma_policy(vma); + mpol = READ_ONCE(vma->vm_policy); if (!mpol) return FUTEX_NO_NODE; --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -1026,7 +1026,7 @@ static int vma_replace_policy(struct vm_ } old = vma->vm_policy; - vma->vm_policy = new; /* protected by mmap_lock */ + WRITE_ONCE(vma->vm_policy, new); /* protected by mmap_lock */ mpol_put(old); return 0;