Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Arnd Bergmann" <arnd@kernel.org>,
	"David Hildenbrand" <david@kernel.org>,
	"Gregory Price" <gourry@gourry.net>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Arnd Bergmann" <arnd@arndb.de>
Cc: larips <larips950@gmail.com>, "Zi Yan" <ziy@nvidia.com>,
	"Joshua Hahn" <joshua.hahnjy@gmail.com>,
	"Rakie Kim" <rakie.kim@sk.com>,
	"Ying Huang" <ying.huang@linux.alibaba.com>,
	"Lorenzo Stoakes (ARM)" <ljs@kernel.org>,
	"Brendan Jackman" <brendan.jackman@linux.dev>,
	"Johannes Weiner" <hannes@cmpxchg.org>,
	"Jackie Liu" <liuyun01@kylinos.cn>,
	linux-mm@kvack.org
Subject: Re: [PATCH 1/2] [v2] mm/mempolicy: fix copy_nodes_to_user() for odd inputs
Date: Wed, 18 Nov 2026 21:16:54 +0000	[thread overview]
Message-ID: <sashiko-outbox-221983@kernel.org> (raw)
In-Reply-To: <20261117211635.2472067-1-arnd@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] mm/mempolicy: missing compat nbytes update in
  copy_nodes_to_user()
--

commit 8559c85c3c122388b2f3048335523c715d5cc2db
Author: Arnd Bergmann <arnd@arndb.de>

mm/mempolicy: fix copy_nodes_to_user() for odd inputs

This commit fixes the copy size alignment and the maxnode off-by-one
calculation in copy_nodes_to_user() for native and compat callers.

Link: https://lore.kernel.org/all/20261117101910.1820066-1-gourry@gourry.net/

> diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> index fb627b37afcbb..c10f9d4ba38f8 100644
> --- a/mm/mempolicy.c
> +++ b/mm/mempolicy.c
> @@ -1720,12 +1720,12 @@ static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
>  static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
>  			      nodemask_t *nodes)
>  {
> -	unsigned long copy = ALIGN(maxnode-1, 64) / 8;
> +	unsigned long copy = ALIGN(maxnode-1, BITS_PER_LONG) / 8;
>  	unsigned int nbytes = BITS_TO_LONGS(nr_node_ids) * sizeof(long);
>  	bool compat = in_compat_syscall();
>  
>  	if (compat)
> -		nbytes = BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t);
> +		copy = ALIGN(maxnode-1, BITS_PER_COMPAT_LONG) / 8;
>  
>  	if (copy > nbytes) {
>  		if (copy > PAGE_SIZE)
> @@ -1733,12 +1733,12 @@ static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
>  		if (clear_user((char __user *)mask + nbytes, copy - nbytes))
>  			return -EFAULT;
>  		copy = nbytes;
> -		maxnode = nr_node_ids;
> +		maxnode = nr_node_ids+1;
>  	}
>  
>  	if (compat)
>  		return compat_put_bitmap((compat_ulong_t __user *)mask,
> -					 nodes_addr(*nodes), maxnode);
> +					 nodes_addr(*nodes), maxnode-1);
>  
>  	return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
>  }

[Severity: High]
Does replacing the compat nbytes assignment with the compat copy
assignment leave a 4-byte gap in the userspace nodemask when
BITS_TO_COMPAT_LONGS(nr_node_ids) is odd?

Without the compat nbytes update, nbytes stays a multiple of sizeof(long)
(e.g., 8 bytes when nr_node_ids <= 32).  When a compat caller passes a
buffer with copy > nbytes, clear_user() zeroes starting at mask + nbytes
and maxnode is clamped to nr_node_ids + 1, but compat_put_bitmap() aligns
maxnode - 1 to BITS_PER_COMPAT_LONG and only writes
BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t) bytes (4 bytes),
leaving the word at nbytes - sizeof(compat_long_t) untouched.  When
copy == nbytes (e.g., 34 <= maxnode <= 65 with nr_node_ids <= 32),
copy > nbytes is false, so maxnode is not clamped and compat_put_bitmap()
copies the upper 32 bits of the kernel unsigned long instead of zeroing
that word.

It looks like the if (compat) block needs to set both nbytes and copy (the
follow-up commit "mm/mempolicy: use put_bitmap() instead of
copy_to_user()" has the same issue in put_nodes()).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117211635.2472067-1-arnd@kernel.org?part=1


      parent reply	other threads:[~2026-11-18 21:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 21:14 [PATCH 1/2] [v2] mm/mempolicy: fix copy_nodes_to_user() for odd inputs Arnd Bergmann
2026-11-17 21:14 ` [PATCH 2/2] [v2] mm/mempolicy: replace copy_nodes_to_user() interface with put_nodes() Arnd Bergmann
2026-11-18 21:16   ` sashiko-bot
2026-11-18  3:09 ` [PATCH 1/2] [v2] mm/mempolicy: fix copy_nodes_to_user() for odd inputs Gregory Price (Meta)
2026-11-18 21:16 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-221983@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=arnd@kernel.org \
    --cc=brendan.jackman@linux.dev \
    --cc=david@kernel.org \
    --cc=gourry@gourry.net \
    --cc=hannes@cmpxchg.org \
    --cc=joshua.hahnjy@gmail.com \
    --cc=larips950@gmail.com \
    --cc=linux-mm@kvack.org \
    --cc=liuyun01@kylinos.cn \
    --cc=ljs@kernel.org \
    --cc=rakie.kim@sk.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=ying.huang@linux.alibaba.com \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox