From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 B9CA73C7DF2 for ; Mon, 23 Mar 2026 17:24:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774286686; cv=none; b=TGuLilT8jggIDEIcmyf7o55BNlchng6/xhZ5BQMfzN/OIq3WgfHp5oVN+ndTxphgYiWnex/2Kw8Y5JMHe4FTeFt8CaST7/34F/kbtoQ+kSu3+jWLMgwgj2IbFJkCyjNrZc6LvudkWu+T5DZft5ItdmxJbP6C9Uai67ENAvfep68= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774286686; c=relaxed/simple; bh=5rreWIFoBoqRLvvete3U9VClnhxT5TadVzK4NkDP+EU=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=XUbQSl5RVBMsYvFp67+cq7eelWXYYOylLQ1ROanIM0LzTqHR5174CM8hqQgQaSBZwyZ+PO7n+rTJKVrvh9bhWfBs9c7fF6abwRzkcCnlA8FeMNQL1lbbeuIjM9YD9tCBIyYnmwi53zkpLQcBrta3gpzjeEQhHDwjRie2w5hH+ng= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=h6z1XBcx; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="h6z1XBcx" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B8CD3C2BCB3; Mon, 23 Mar 2026 17:24:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774286686; bh=5rreWIFoBoqRLvvete3U9VClnhxT5TadVzK4NkDP+EU=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=h6z1XBcxWz5pCI1SUvB5F7QjzqXt1wzbdHfwAi9WqwQ9jNLhm0RhQeXtYJtSYV5/5 zdsCQzwpXTE6sKPEXtsEdxGWpI9P02K3tuh+sXGI62N96fykAzXVHeEcEWwmuwjC88 pDP9ZMnP3tf51pQ5iVll69G4/NYkqGHk1/kLY8/5ft7PUJV5ERoFc9zDq7nt3qyxJY Ti9lYdG9BAnuFADdOg7HDhz3vzxcemQ18Exg4G2ZRNsLiBkJL/gROSlfY3WEDW7P8E H6Qt5rpQ6DVbzkZc3Z8QMa8FYS2/tvw8WX//0Xr9XSYOVagklAyGbNxAEp8da5EKsS ZKxT3bDHn8eNQ== From: Thomas Gleixner To: Hao-Yu Yang 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 In-Reply-To: <20260313124756.52461-1-naup96721@gmail.com> References: <20260313124756.52461-1-naup96721@gmail.com> Date: Mon, 23 Mar 2026 18:24:42 +0100 Message-ID: <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 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