From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796389048; b=G2iup6RbfisvLHq2BWTTIoOyXfKlEJvBAmfvgb4IJ02lTuSMOFJPhaRskFudmMd8UgIa ivP4BX91ObldmY+rJvnkkXRemxk7tOC1MS0nf2+klwlSOPHIUflMJS6SFi/9V8+QAitxl 631INRwz3ojy1mVjbsJn109XYHM9LxO5kyMlQ3j4jSq69R6HifelJ0ag2mbeQWZf8MA7h mJeeNwxbMdE40QuoN7tXS6Y5OgygVSsT9VzobG2IOQTn+xMfgEPMJwXlzBTtZrdpcaFIq Q+dJQ6gwZQFZgTPCB4otyZlcIW/t7eUrHOrRybDmlclvD83g4AkMuaT4JrB5E3zpudw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796389048; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=QoVe5LFqrpuD96wmGH26IAmWdfDG45aEGxJDZkDZ7pM=; b=JDiFyhCkUURvoXofSZ3/v/nU2dxcfwjiFzH/B7738C5NxiDlOasbbpXVxXE4prHNj73e UQ+jfrhsOq0+mpA7SLf/oqIQvpy47NuLHZaM20VUYCHlpp+EabMGI+VS7hMfetUPYvmjv C2bz/EvIhuvYOJh+mhWyDHoP1184QZ7PhJtwIn048nip95h8thfwuNSUo1YnppcnMlO50 pIl517sx2f7NX+jfX9H1eeqgEz4tPPPhrPySq+uF5IYuCBo4lugUUMQ6pFukU21DGBr03 5qn+NMb+WN2H8VkIkCNDZKohrGGP9qT9WF1K3cs9PB2kjNKNutegHVK46Fl9d8OKVxA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="GYTZe/8G"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 086FA606A322; Fri, 4 Dec 2026 12:57:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=GYTZe/8G Received: by smtp.kernel.org (Postfix) with ESMTPSA id 42BBD1F000CE; Fri, 4 Dec 2026 12:57:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796389047; bh=QoVe5LFqrpuD96wmGH26IAmWdfDG45aEGxJDZkDZ7pM=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=GYTZe/8GoZBid8HUPQG6PP15iKJRG6coCCoah1W4rAvwhZULTFeFV+NvJUpSON3sj rS1jJ/d9wp157aDh44WryrNjW9t9R51PH/Zsxsyf3YwuaCqFElpsuXfGp6Y9PTjhE9 JZzwQ/FrcI7Wd9Q+lJ2Bo6de6wkWfqTPy+3VsybI+XUOUaY3Bx83EHc34nMvfKIY6p Y0rq45D6Z3wHa5GDp5rbncu6kzCjIFwEMzWMvyiK6OD+iLK1oYHWMak9Q1T7HAIaK0 vLGPNqHX6CGVmiS7ohRsDJzZceobZhj45g4hpFHCHHQjSTRPLo3kfNjcOpGzwi/rAD wt7npihAsXRzQ== From: "David Hildenbrand (Arm)" Date: Fri, 04 Dec 2026 13:50:14 +0100 Subject: [PATCH v2 23/31] mm/mempolicy: check access under exec_update_lock X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261204-fixes_core_3-v2-23-9aad65727632@kernel.org> References: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> In-Reply-To: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> To: Hugh Dickins , Baolin Wang , Andrew Morton , Mike Rapoport , Peter Xu , Michal Hocko , Kiryl Shutsemau , Manfred Spraul , Davidlohr Bueso , Cyrill Gorcunov , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Lorenzo Stoakes , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Yang Shi , Zach O'Keefe , Song Liu , David Stevens , Kefeng Wang , Alex Williamson , Suren Baghdasaryan , Dave Jiang , Matthew Wilcox , Miaohe Lin , David Rientjes , Shakeel Butt , Jan Kara , Mel Gorman , Jason Gunthorpe , John Hubbard , Gerd Hoffmann , Vivek Kasireddy , Dave Airlie , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Hagen Paul Pfeifer , James Bottomley , Felix Kuehling , Alex Sierra , Balbir Singh , Kairui Song , Qi Zheng , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Oscar Salvador , Muchun Song , Dan Williams , Rakie Kim , Hao Li , Christoph Lameter , Roman Gushchin , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Alexei Starovoitov , Xu Xin , Chengming Zhou , Shivank Garg Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org, linux-rt-devel@lists.linux.dev, SJ Park , "David Hildenbrand (Arm)" , Chris Mason , Oleg Nesterov X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3559; i=david@kernel.org; h=from:subject:message-id; bh=69qCQNLUXRa7v+I04CKYe5s6BCx/sRRpG+MLr4iiZ34=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLKFtjOoVTyIdbHf2vuyjPOk55t7zqfcgj+8OnKi2efC0 yd8j3T0OkpZGMQ4GGTFFFmCgu8bvHy5rrbajY8ZZg4rE8gQBi5OAZiI2RyG/9HbYvZvm6VhdPKf GZvQ1mOqoXWh+vN3+89a+VCLe9/q578Y/nttDw/XtTy74jbHO85XPEu5j2yuD5pnOc9ks0igX5S AHxMA X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A From: Chris Mason The access check in migrate_pages() can race with concurrent execve(): if the target task execs a setuid binary, migrate_pages() can migrate the pages of the new mm, which the checks would otherwise deny. The impact is limited, but we should just handle it consistently. kernel_migrate_pages() calls ptrace_may_access() on the target and takes its mm with get_task_mm() later, so the credentials that the check sees have to belong to that mm. execve() replaces the mm in exec_mmap() and the credentials in commit_creds(), and holds signal->exec_update_lock for writing across both. Let's fix it by holding the target's exec_update_lock for reading until we have grabbed the mm, as move_pages() does ever since commit 13f77972b94c ("mm/migrate: find_mm_struct: fix race between security checks and suid exec"). Regarding dropping the RCU read lock earlier, as explained by Oleg: "kernel_migrate_pages() calls ptrace_may_access() under rcu_read_lock() for no reason. This is a leftover from before the commit 313674661925 ("Unify migrate_pages and move_pages access checks"), where rcu_read_lock() was needed to protect __task_cred(task). So we can drop the RCU lock right after get_task_struct()." Waiting for concurrent execve() is not really relevant, and move_pages() already does the same. This has low urgency, as the impact is limited. No need to CC stable. Fixes: 313674661925 ("Unify migrate_pages and move_pages access checks") Reported-by: Oleg Nesterov Closes: https://lore.kernel.org/r/ahMt6xyUNnacZU8-@redhat.com Suggested-by: Oleg Nesterov Signed-off-by: Chris Mason [ david: simplify description and comment ] Acked-by: Gregory Price (Meta) Signed-off-by: David Hildenbrand (Arm) --- mm/mempolicy.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 9e4e8ecd7e69a..8c1fe94a08971 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -1981,6 +1981,12 @@ static int kernel_migrate_pages(pid_t pid, unsigned long maxnode, goto out; } get_task_struct(task); + rcu_read_unlock(); + + /* Exclude execve() until we have grabbed the MM from this task. */ + err = down_read_killable(&task->signal->exec_update_lock); + if (err) + goto out_put; err = -EINVAL; @@ -1989,28 +1995,28 @@ static int kernel_migrate_pages(pid_t pid, unsigned long maxnode, * Use the regular "ptrace_may_access()" checks. */ if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { - rcu_read_unlock(); err = -EPERM; - goto out_put; + goto out_unlock; } - rcu_read_unlock(); task_nodes = cpuset_mems_allowed(task); /* Is the user allowed to access the target nodes? */ if (!nodes_subset(*new, task_nodes) && !capable(CAP_SYS_NICE)) { err = -EPERM; - goto out_put; + goto out_unlock; } task_nodes = cpuset_mems_allowed(current); if (!nodes_and(*new, *new, task_nodes)) - goto out_put; + goto out_unlock; err = security_task_movememory(task); if (err) - goto out_put; + goto out_unlock; mm = get_task_mm(task); + up_read(&task->signal->exec_update_lock); + put_task_struct(task); if (!mm) { @@ -2027,6 +2033,8 @@ static int kernel_migrate_pages(pid_t pid, unsigned long maxnode, return err; +out_unlock: + up_read(&task->signal->exec_update_lock); out_put: put_task_struct(task); goto out; -- 2.56.1