From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2AD7D2D8385 for ; Mon, 23 Mar 2026 23:46:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774309583; cv=none; b=KsUMamFauCEMmDglsVOS39jH2YtNk5Kmdq3rueJmpKH2dREZztIX+vOi3napr+RLNuCInKtpRR5+R0gmrTH+Y390r4yABFKd2rqcYm5y4I68nKD70K9SVj91DIAugVedfET7rIBcdT4FV8sAPMVMSMsIfYaRJJQNm2j0S7j2wXo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774309583; c=relaxed/simple; bh=W5CooZzD2diH8zB+SckyYvW5iL3Vh6Zt6PPsoD3Bebk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=jx7KZLibeYQVOH/pYZaojhUJMQ2fyHHST1z4+oDuOrRb0+46O8VSS47ARSK7fhSG2T1N+SPxugZ1KPtyPeFJBbzFb9e3k5+2P5a7M8s4gIBHZjywRXp/70N4Pa/bhVkspavKApdeJjWdeJPKjdTtSiyfina+gAkXx5SKl4zJLDY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SreEwamu; arc=none smtp.client-ip=209.85.216.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SreEwamu" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-354bc7c2c46so2226810a91.0 for ; Mon, 23 Mar 2026 16:46:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774309581; x=1774914381; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=/Yh4+m9w0E7Q7phDH+oVMFKEtBHte8E5GSUxHvgsU7g=; b=SreEwamujD/D436NO1ksX9JbgFq5Rx4ST2kq9aTbwxCmgAwfRy91c1OBiTv3PmVQqD ct07HzDaM7avCDG8rXFkBZ9UmkbMrwbrwJM0ihfcCnrP7GWTJDZs4RrPOJruGMZF5Aqd zfQJfwCGerLuWCxkfp7eT3Gd4rbKhrcJhXaOsod6aY2TmvcmCExhIDdeLcTDHaDQEvsr DZZ8PVOPRgMtvqL87ZiNEObKHYWRAafgoDvZIHczyNcKvcgnqK23f31pdUxJAMyAEVFH zYpTBUniHlepW8F40CbWiXfEYLQGqZpxRWD4NQqH4cj3cp53Q221RBrpsor2/jeTJlSO l6cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774309581; x=1774914381; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/Yh4+m9w0E7Q7phDH+oVMFKEtBHte8E5GSUxHvgsU7g=; b=IKii6p+r7Csk7iRr+ZG2YN2Hj9HzG0sYuta9kDKFreCGWNykPn3WHRHLIhvLlEdgvy +XnKEkYf7+qPKVQDtUbsp3MVblmMxaYGP2aPjhkPC2otqCOewiUQ3KEVYcFES4VOuWqj 2qayb9GXYamacWdRUaiS7IOYExInbKDQiTY1iQeSsRh1gvN2Y+ma07A4DyQenFl2fvW4 7tIzKWdkaRNppnWquQGcWEk/vZh4tvsXRmKiWNrVoKTx2vGjQEqZ2k4X7PskgKMGMDDW eLMkBMBt9lccT8bpzMgu5/zTgLUJWTvFEAHr2R02gfQ7t2N7fYgpMqLVF60xeOVQNt6H 1hQg== X-Forwarded-Encrypted: i=1; AJvYcCW/lV2IO0psmop/yg9gxfIaKnxNeGEnyQAPk5LQC+TIqAMjFZLXmk8pWCh3OpIU2VsY7C9L9TEicqkFY20=@vger.kernel.org X-Gm-Message-State: AOJu0Ywa553R91HXXTQgH1kDwb680ycjrVAfkcpv1y6AyQV382OFQLOD mh8vGOKnqfJmkeFtwByHSB4AGefCaoW97ABq33+sxnGI5Vas53pF40TF X-Gm-Gg: ATEYQzz7q0LvSGOAtkdet6xOt2iplSKQl67wH4BnICeVYKBf7MijG5bbnlOXgoiAbmj mXRmRPQFvI1PPJuWG5B3tbtsy+ouaeVKt8Fl6qxTUEcPG6SXuxJ6TbIh5F3QTQuRhuxzmRPQ4Nh FbY1LhlKQXLXE52hO/15YH5BhvXuneYyw18DFHS4VP1bFChwTW3faNkTLZI4pyYziqp9JXfnpTM nZWJ4TezI2q6F8vJJ0R7v7kG7ziNVnYLgVYJ86B6QUK2GetmndOqaKWP31PTGPD3ef1i3Hx2+cg oYBkQsdEErf8yFXBOjAJtxGCQpOb+IJYXP93RPwInIRtW7VDAbdS5h8LkdZuO+PHUf7mQGaFUul ngkIZsP7fxJ+hrbMnmfL6Dec6W+qdElNUQTFl5IyASjhTyXda7umvz0EZj7GBj2eFWRv3vSx6so igOwYiGtKGv7zkE4IkUS/XSLKZcjNlvTtZsN0IPmGzGJpnMtE8 X-Received: by 2002:a17:90b:4ccb:b0:359:f0e1:f8c9 with SMTP id 98e67ed59e1d1-35bd2b97290mr11344524a91.6.1774309581298; Mon, 23 Mar 2026 16:46:21 -0700 (PDT) Received: from naup-virtual-machine ([140.113.92.221]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-35c03124a94sm220036a91.2.2026.03.23.16.46.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Mar 2026 16:46:20 -0700 (PDT) Date: Tue, 24 Mar 2026 07:46:17 +0800 From: Hao-Yu Yang To: Thomas Gleixner Cc: mingo@redhat.com, linux-kernel@vger.kernel.org, Andrew Morton , David Hillenbrand , Eric Dumazet , linux-mm@kvack.org, Peter Zijlstra Subject: Re: [PATCH v2] futex: Use-after-free between futex_key_to_node_opt and vma_replace_policy Message-ID: References: <20260313124756.52461-1-naup96721@gmail.com> <87a4vyihlx.ffs@tglx> 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=us-ascii Content-Disposition: inline In-Reply-To: <87a4vyihlx.ffs@tglx> Sorry, I'm not familier RCU and mempolicy. I don't know how to patch this part correctly. On Mon, Mar 23, 2026 at 06:24:42PM +0100, Thomas Gleixner wrote: > Hao-Yu! > > On Fri, Mar 13 2026 at 20:47, Hao-Yu Yang wrote: > > I've removed the security list as this is public already. > Also added the mm list and the maintainers. While it fixes the futex > problem it is a change to the MM subsystem, so those people need to be > involved. > > > During futex_key_to_node_opt() execution, vma->vm_policy is read under > > speculative mmap lock and RCU. Concurrently, mbind() may call > > vma_replace_policy() which frees the old mempolicy immediately via > > kmem_cache_free(). > > > > This creates a race where __futex_key_to_node() dereferences a freed > > mempolicy pointer, causing a use-after-free read of mpol->mode. > > > [ 151.412631] BUG: KASAN: slab-use-after-free in __futex_key_to_node (kernel/futex/core.c:349) > > [ 151.414046] Read of size 2 at addr ffff888001c49634 by task e/87 > > [ 151.414476] > > [ 151.415431] CPU: 1 UID: 1000 PID: 87 Comm: e Not tainted 7.0.0-rc3-g0257f64bdac7 #1 PREEMPT(lazy) > > [ 151.415758] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014 > > [ 151.415969] Call Trace: > > [ 151.416059] > > [ 151.416161] dump_stack_lvl (lib/dump_stack.c:123) > > [ 151.416299] print_report (mm/kasan/report.c:379 mm/kasan/report.c:482) > > [ 151.416359] ? __virt_addr_valid (./include/linux/mmzone.h:2046 ./include/linux/mmzone.h:2198 arch/x86/mm/physaddr.c:54) > > [ 151.416412] ? __futex_key_to_node (kernel/futex/core.c:349) > > [ 151.416517] ? kasan_complete_mode_report_info (mm/kasan/report_generic.c:182) > > [ 151.416583] ? __futex_key_to_node (kernel/futex/core.c:349) > > [ 151.416631] kasan_report (mm/kasan/report.c:597) > > [ 151.416677] ? __futex_key_to_node (kernel/futex/core.c:349) > > [ 151.416732] __asan_load2 (mm/kasan/generic.c:271) > > [ 151.416777] __futex_key_to_node (kernel/futex/core.c:349) > > [ 151.416822] get_futex_key (kernel/futex/core.c:374 kernel/futex/core.c:386 kernel/futex/core.c:593) > > [ 151.416871] ? __pfx_get_futex_key (kernel/futex/core.c:550) > > [ 151.416927] futex_wake (kernel/futex/waitwake.c:165) > > [ 151.416976] ? __pfx_futex_wake (kernel/futex/waitwake.c:156) > > [ 151.417022] ? __pfx___x64_sys_futex_wait (kernel/futex/syscalls.c:398) > > [ 151.417081] __x64_sys_futex_wake (kernel/futex/syscalls.c:382 kernel/futex/syscalls.c:366 kernel/futex/syscalls.c:366) > > [ 151.417129] x64_sys_call (arch/x86/entry/syscall_64.c:41) > > [ 151.417236] do_syscall_64 (arch/x86/entry/syscall_64.c:63 arch/x86/entry/syscall_64.c:94) > > [ 151.417342] entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:130) > > [ 151.418312] > > Please trim the backtrace so it only contains the real important > information. > > https://docs.kernel.org/process/submitting-patches.html#backtraces-in-commit-messages > > > Fix by adding rcu to __mpol_put(). > > > > change-log: > > v2-v1: add rcu to __mpol_put > > The change history is not part of the change log, it want's to be placed > after the --- separator. > > > Fixes: c042c505210d ("futex: Implement FUTEX2_MPOL") > > Reported-by: Hao-Yu Yang > > Signed-off-by: Hao-Yu Yang > > This should have a > > Suggested-by: Eric Dumazet > > tag. > > > --- > > include/linux/mempolicy.h | 1 + > > mm/mempolicy.c | 2 +- > > 2 files changed, 2 insertions(+), 1 deletion(-) > > > > diff --git a/include/linux/mempolicy.h b/include/linux/mempolicy.h > > index 0fe96f3ab3ef..65c732d440d2 100644 > > --- a/include/linux/mempolicy.h > > +++ b/include/linux/mempolicy.h > > @@ -55,6 +55,7 @@ struct mempolicy { > > nodemask_t cpuset_mems_allowed; /* relative to these nodes */ > > nodemask_t user_nodemask; /* nodemask passed by user */ > > } w; > > + struct rcu_head rcu; > > }; > > > > /* > > diff --git a/mm/mempolicy.c b/mm/mempolicy.c > > index 0e5175f1c767..6dc61a3d4a32 100644 > > --- a/mm/mempolicy.c > > +++ b/mm/mempolicy.c > > @@ -487,7 +487,7 @@ void __mpol_put(struct mempolicy *pol) > > { > > if (!atomic_dec_and_test(&pol->refcnt)) > > return; > > - kmem_cache_free(policy_cache, pol); > > + kfree_rcu(pol, rcu); > > } > > EXPORT_SYMBOL_FOR_MODULES(__mpol_put, "kvm"); > > While this looks functionally correct it is incomplete in terms of RCU. > > The vma->vm_policy pointer needs to be marked __rcu. That then requires > to use rcu_dereference_check() at the reader side and > rcu_assign_pointer() and rcu_replace_pointer() on the writer side. > > Especially the writer side is required so that the proper memory > barriers are inserted for architectures with a weakly ordered memory > model. > > Thanks, > > tglx