[PATCH 23/32] mm/mempolicy: check access under exec_update_lock

From: David Hildenbrand (Arm)

Date: Sun Nov 29 2026 - 05:14:28 EST


From: Chris Mason <mason@xxxxxxxxxx>

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 <oleg@xxxxxxxxxx>
Closes: https://lore.kernel.org/r/ahMt6xyUNnacZU8-@xxxxxxxxxx
Suggested-by: Oleg Nesterov <oleg@xxxxxxxxxx>
Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
[ david: simplify description and comment ]
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
mm/mempolicy.c | 20 ++++++++++++++------
1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 1d999a037d1b8..1f68a463b3bc7 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