From nobody Tue Jun 9 00:58:48 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 A35133955F1 for ; Sun, 24 May 2026 16:58:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779641897; cv=none; b=Ck3gYZEah8Cm/El5yChUf1udV+R6whzE8/No5J2EM80ZJL2GELgVchb8VLK4/28znWS7Lsgc7nymcT5qLDZ9XXrS9Qb7R2G6BDzFmdLuNigpKXUqIKoF9P7dtkhH/Qiw4Ckjn/lXOL0QLHAoYvgGSwrW8NIL2SvNROoQEYMvTps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779641897; c=relaxed/simple; bh=F/LG87ymRdVrDmc3k6wNOwla5PWNTmNyt+0M/WxwHgE=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; b=QQfZ/p9ybqHAqDMADl4Vkch46SPd0cRiZI+eGEJ4YKzP+p5DIqRN/1fnjapfXnfi8iI8Y1BsLH8qMMBbCtBI6LAMpD5GUrXz0e0wP9rU1TayGNJ6KvYh7dTlM3Xl1n4q9R25n5pytqz2oG3cWKc0ZKs7BIhjiFx0pARzqiEn16k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=N3AR3VZS; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="N3AR3VZS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779641894; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to; bh=uF5Ltar24PxhfK2RJK52qj/LVT6BFAvA6BDox8hw3ZE=; b=N3AR3VZSIRbyChHx1QhTvFeYpWD+fH1HoIvYOLDcfdt+1ialNby64uxFytBLcmSSm5FBuz oz1iO0RIAfL6o3mmsr/WHs0MKYL3A5ZnZLJc2309RUHbCinHCZ3N5i2GPrNz3fXH8lDd1M UpglhEMVQ5sgxmFtqMbwIqbYHT483ns= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-47-zju1gaudOq27gyZFRB49-w-1; Sun, 24 May 2026 12:58:08 -0400 X-MC-Unique: zju1gaudOq27gyZFRB49-w-1 X-Mimecast-MFC-AGG-ID: zju1gaudOq27gyZFRB49-w_1779641886 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E01551956094; Sun, 24 May 2026 16:58:05 +0000 (UTC) Received: from fedora (unknown [10.44.48.14]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id D8272300019F; Sun, 24 May 2026 16:58:00 +0000 (UTC) Received: by fedora (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Sun, 24 May 2026 18:58:05 +0200 (CEST) Date: Sun, 24 May 2026 18:57:59 +0200 From: Oleg Nesterov To: Alistair Popple , Andrew Morton , Byungchul Park , David Hildenbrand , Gregory Price , Joshua Hahn , Matthew Brost , Rakie Kim , Ying Huang , Zi Yan Cc: Jann Horn , Kees Cook , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/4] mm/mempolicy: kernel_migrate_pages: simplify the usage of put_task_struct() Message-ID: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" kernel_migrate_pages() calls put_task_struct() twice; this complicates the code. Move put_task_struct() to the unified exit path. This also allows us to move the "out_put" label up and avoid another goto. Signed-off-by: Oleg Nesterov Reviewed-by: Gregory Price --- mm/mempolicy.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 4e4421b22b59..4326dff16aa6 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -1922,25 +1922,20 @@ static int kernel_migrate_pages(pid_t pid, unsigned= long maxnode, goto out_put; =20 mm =3D get_task_mm(task); - put_task_struct(task); - if (!mm) { err =3D -EINVAL; - goto out; + goto out_put; } =20 err =3D do_migrate_pages(mm, old, new, capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE); =20 mmput(mm); +out_put: + put_task_struct(task); out: NODEMASK_SCRATCH_FREE(scratch); - return err; - -out_put: - put_task_struct(task); - goto out; } =20 SYSCALL_DEFINE4(migrate_pages, pid_t, pid, unsigned long, maxnode, --=20 2.52.0 From nobody Tue Jun 9 00:58:48 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 097B93AE198 for ; Sun, 24 May 2026 16:58:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779641900; cv=none; b=oFmo2AcnOwwV6ZYFIBRM6SBqX+U8hwI4Pny/zLx3GWxaCWVmDMeMUFprunNWCzlmKc6NosD3oMsyoEM8J+pidww8hLN4/zE+VTE6ImUNtFaZuCwK/ir7GWfegI9dWffeYh+ldkkI2JcofR1Oestnq04Z2kyPL8h8zqwfZDz8QV4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779641900; c=relaxed/simple; bh=woxAs0TjB/f6Esra3HmYDkKs7vg26AUVV3klXb4VxuM=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; b=SZiAGASDCDm2wBMRl5lJSwu2NE3Gd+hk5zjWt1JOa9//O9Y6zI7UXMv15HIT7ebftwvB7f99Eo+5RAd55hErwXVzzanzhsEPXOFv1nIxofNrD95zoT/qleqmYziMl/LdJ/4csT7ZwtYF1Sqrthojvm00/Mty017A1U25P/0tWTI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=QYIaenur; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="QYIaenur" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779641898; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to; bh=XYbRs3tCnjBDML/yVXAZgXnRWFC9EoMypl12Py3Braw=; b=QYIaenurbHO4Hr8ns65VqjcxAl7pqeBmCfr9mbk/xq3VrPTLPmk1+QPnxISaxLZBVyi4lY h2k94QX03DMK1QgGPkQxZ4NROhb8zbrcPpCj5RO85jXQp5gezqkVY3GN/rOXDGHLVzUUzD A68xNr7T1/Ml6BcuyffYbPDta3WSduE= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-438-7I3IUvEbM8mkaBpGZEJI4A-1; Sun, 24 May 2026 12:58:15 -0400 X-MC-Unique: 7I3IUvEbM8mkaBpGZEJI4A-1 X-Mimecast-MFC-AGG-ID: 7I3IUvEbM8mkaBpGZEJI4A_1779641893 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id EF71F180044D; Sun, 24 May 2026 16:58:12 +0000 (UTC) Received: from fedora (unknown [10.44.48.14]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id CF0FE1685; Sun, 24 May 2026 16:58:07 +0000 (UTC) Received: by fedora (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Sun, 24 May 2026 18:58:12 +0200 (CEST) Date: Sun, 24 May 2026 18:58:06 +0200 From: Oleg Nesterov To: Alistair Popple , Andrew Morton , Byungchul Park , David Hildenbrand , Gregory Price , Joshua Hahn , Matthew Brost , Rakie Kim , Ying Huang , Zi Yan Cc: Jann Horn , Kees Cook , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/4] mm/mempolicy: kernel_migrate_pages: use find_get_task_by_vpid() Message-ID: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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(). Better yet, if pid !=3D 0, we can use find_get_task_by_vpid() which does get_task_struct() itself, and avoid get/put_task_struct() otherwise. Signed-off-by: Oleg Nesterov --- mm/mempolicy.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 4326dff16aa6..2ec14001e4dc 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -1883,15 +1883,11 @@ static int kernel_migrate_pages(pid_t pid, unsigned= long maxnode, if (err) goto out; =20 - /* Find the mm_struct */ - rcu_read_lock(); - task =3D pid ? find_task_by_vpid(pid) : current; + task =3D pid ? find_get_task_by_vpid(pid) : current; if (!task) { - rcu_read_unlock(); err =3D -ESRCH; goto out; } - get_task_struct(task); =20 err =3D -EINVAL; =20 @@ -1900,11 +1896,9 @@ 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 =3D -EPERM; goto out_put; } - rcu_read_unlock(); =20 task_nodes =3D cpuset_mems_allowed(task); /* Is the user allowed to access the target nodes? */ @@ -1932,7 +1926,8 @@ static int kernel_migrate_pages(pid_t pid, unsigned l= ong maxnode, =20 mmput(mm); out_put: - put_task_struct(task); + if (pid) + put_task_struct(task); out: NODEMASK_SCRATCH_FREE(scratch); return err; --=20 2.52.0 From nobody Tue Jun 9 00:58:48 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 75EB63ACA50 for ; Sun, 24 May 2026 16:58:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779641909; cv=none; b=jZDVx8aGXeRsXldxpgvzhEiSSWcfxKRZqNbDbcOV1S7TmF6pgPHH6dZu7nmhxPIkeC/4k8V12ncyQK44Rj7bwPXwWg9M72o8JMC4c262GyGyD6MOcPbXScxIuDap/WVMboYjzG5OCTTaVwW8uC2qB79wvRmMu2DdzZU9qN+chps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779641909; c=relaxed/simple; bh=nY6YkLXuCFkr2priCKrRogtK5H73SQd8EZF0mZTybtE=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; b=eBNTPSMnOCcUiLvr5nlUZamleUeTXsGEvwYGHvHA9wntMQ4RarH7LkH62Ew/g7hoIaEOnOS3MZXNFxnrwQ9iOxvAV6KlKLO9Vw/BOvPXKVv9dYOk7IC67U9shf/B52l80tzVqFUFIHUBxaHTRPP4M3ZwpfE5Y6VRuIsbs8relUI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=cZJzO71y; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="cZJzO71y" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779641907; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to; bh=yLFlGxdl4EO7bP4Z/X8AUVIY28BTwYFrCTa4X/x2wjA=; b=cZJzO71ylJpI33sIFUM0qJjBNEeRuj9C9HWmlCGKiMayEBvKU4Qt7tVfKNX5cTf9bB784V CzbBTIsLeyibLqqpb8ldVKSzUySublsKXfSWyjga6xEKmPfDiVUADUOZkbvQULZ2TknSyo 3eUiKQ6/GgsZHgcAvpmsTv/2u1O7MpQ= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-201-j0TEpCOBM_asKwnPEo2EwA-1; Sun, 24 May 2026 12:58:21 -0400 X-MC-Unique: j0TEpCOBM_asKwnPEo2EwA-1 X-Mimecast-MFC-AGG-ID: j0TEpCOBM_asKwnPEo2EwA_1779641899 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CE02F180034A; Sun, 24 May 2026 16:58:19 +0000 (UTC) Received: from fedora (unknown [10.44.48.14]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id C61E71800465; Sun, 24 May 2026 16:58:14 +0000 (UTC) Received: by fedora (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Sun, 24 May 2026 18:58:19 +0200 (CEST) Date: Sun, 24 May 2026 18:58:13 +0200 From: Oleg Nesterov To: Alistair Popple , Andrew Morton , Byungchul Park , David Hildenbrand , Gregory Price , Joshua Hahn , Matthew Brost , Rakie Kim , Ying Huang , Zi Yan Cc: Jann Horn , Kees Cook , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/4] mm/mempolicy: kernel_migrate_pages: check ptrace_may_access() after nodes_and() Message-ID: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the ptrace_may_access() check down, after the nodemask validation. This is just a preparation to simplify the next patch. Note that because of this reordering, if the caller passes an invalid nodemask and also lacks permissions, the syscall will now return -EINVAL instead of -EPERM. Hopefully, nobody relies on the old error precedence. Signed-off-by: Oleg Nesterov --- mm/mempolicy.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 2ec14001e4dc..c09ff9f9aa96 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -1889,17 +1889,6 @@ static int kernel_migrate_pages(pid_t pid, unsigned = long maxnode, goto out; } =20 - err =3D -EINVAL; - - /* - * Check if this process has the right to modify the specified process. - * Use the regular "ptrace_may_access()" checks. - */ - if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { - err =3D -EPERM; - goto out_put; - } - task_nodes =3D cpuset_mems_allowed(task); /* Is the user allowed to access the target nodes? */ if (!nodes_subset(*new, task_nodes) && !capable(CAP_SYS_NICE)) { @@ -1908,8 +1897,19 @@ static int kernel_migrate_pages(pid_t pid, unsigned = long maxnode, } =20 task_nodes =3D cpuset_mems_allowed(current); - if (!nodes_and(*new, *new, task_nodes)) + if (!nodes_and(*new, *new, task_nodes)) { + err =3D -EINVAL; goto out_put; + } + + /* + * Check if this process has the right to modify the specified process. + * Use the regular "ptrace_may_access()" checks. + */ + if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { + err =3D -EPERM; + goto out_put; + } =20 err =3D security_task_movememory(task); if (err) --=20 2.52.0 From nobody Tue Jun 9 00:58:48 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 080783AC0C5 for ; Sun, 24 May 2026 16:58:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779641917; cv=none; b=JZeAl9FNdJWXuHIyH2tn6RTczJiEDWCkj9qBOCY9ojNtK20KZBYbS5Yam+RQ6qNxXm2Cmk2guCtZh3aKiPBlW7kjspvU6hYDFEOIuh19Ew8XrGFZ6HTlw8oGamIRpuVL7GiASxo0ZZgyXRZekintj3rnNOOyw+Zp10Atco1yLSw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779641917; c=relaxed/simple; bh=oraoDfKvAg6Qzi7tm1y6JPOAo7evISin/YWdv0pGGe4=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; b=Ma1Y7/hryiFzy+Htgttt35gBWl5aWtvYp0LloDeMhlytdEBu+008onbU04aDjpUDsVCEWHL/Qedyud/+cxSrtm5o9WZPc56uj7WriN4qOszImqzHChu+Gm8TcRl4DLHBUuGguHLhBbG0EArltuWBS/sa4p45Zpv99ZG/kljrtJ0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=HhSMCbH8; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="HhSMCbH8" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779641915; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to; bh=fx9P0XwhXW7SmBYCB2M08iDg2xYh5gHDBQzMHIVQsRI=; b=HhSMCbH8nDMdUqUuwOa+Hp4QRzq0tybHvbUgjxQZQKqYMO10jayNpySZkPj82lCAA4Nc4+ W+YLxniyd1N3lu2ZPTRf7fdG8dKAXj8KqMNSnci107HMU8FA5SQCKY6z9Mmmyc81mwDda+ Dud5OKhhZKQLPXrei2w0KuwiqICjIb4= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-80-SAQWZFcuOPSreD9kou7wRA-1; Sun, 24 May 2026 12:58:29 -0400 X-MC-Unique: SAQWZFcuOPSreD9kou7wRA-1 X-Mimecast-MFC-AGG-ID: SAQWZFcuOPSreD9kou7wRA_1779641907 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C661419560A1; Sun, 24 May 2026 16:58:26 +0000 (UTC) Received: from fedora (unknown [10.44.48.14]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id BE6D119560A3; Sun, 24 May 2026 16:58:21 +0000 (UTC) Received: by fedora (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Sun, 24 May 2026 18:58:26 +0200 (CEST) Date: Sun, 24 May 2026 18:58:20 +0200 From: Oleg Nesterov To: Alistair Popple , Andrew Morton , Byungchul Park , David Hildenbrand , Gregory Price , Joshua Hahn , Matthew Brost , Rakie Kim , Ying Huang , Zi Yan Cc: Jann Horn , Kees Cook , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/4] mm/mempolicy: kernel_migrate_pages: fix race between security checks and suid exec Message-ID: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The target task can execute a setuid binary between ptrace_may_access() and get_task_mm(). Protect this critical section with exec_update_lock. Sadly, we don't have DEFINE_LOCK_GUARD_1_COND(rwsem_read, _kill) yet. Signed-off-by: Oleg Nesterov --- mm/mempolicy.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index c09ff9f9aa96..cbb45a876a93 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -1902,24 +1902,31 @@ static int kernel_migrate_pages(pid_t pid, unsigned= long maxnode, goto out_put; } =20 + err =3D down_read_killable(&task->signal->exec_update_lock); + if (err) + goto out_put; /* * Check if this process has the right to modify the specified process. * Use the regular "ptrace_may_access()" checks. */ if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { err =3D -EPERM; - goto out_put; + goto unlock; } =20 err =3D security_task_movememory(task); if (err) - goto out_put; + goto unlock; =20 mm =3D get_task_mm(task); if (!mm) { err =3D -EINVAL; - goto out_put; + goto unlock; } +unlock: + up_read(&task->signal->exec_update_lock); + if (err) + goto out_put; =20 err =3D do_migrate_pages(mm, old, new, capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE); --=20 2.52.0