From mboxrd@z Thu Jan 1 00:00:00 1970 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-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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; } - err = -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 = -EPERM; - goto out_put; - } - 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)) { @@ -1908,8 +1897,19 @@ static int kernel_migrate_pages(pid_t pid, unsigned long maxnode, } task_nodes = cpuset_mems_allowed(current); - if (!nodes_and(*new, *new, task_nodes)) + if (!nodes_and(*new, *new, task_nodes)) { + err = -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 = -EPERM; + goto out_put; + } err = security_task_movememory(task); if (err) -- 2.52.0