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] mm/mempolicy: fix copy_nodes_to_user() for odd inputs
Date: Wed, 18 Nov 2026 14:26:51 +0000	[thread overview]
Message-ID: <sashiko-outbox-221201@kernel.org> (raw)
In-Reply-To: <20261117142628.3931151-1-arnd@kernel.org>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] mm/mempolicy: unwritten 4-byte gap in copy_nodes_to_user() for
  compat
- [High] mm/mempolicy: userspace buffer overflow in copy_nodes_to_user()
--

commit 15cbc49ca2cc79737596e7f00e2ef7f9966ffbe5
Author: Arnd Bergmann <arnd@arndb.de>

mm/mempolicy: fix copy_nodes_to_user() for odd inputs

This commit updates copy_nodes_to_user() to align maxnode directly to
BITS_PER_LONG or BITS_PER_COMPAT_LONG instead of aligning maxnode - 1 to
64.

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

> diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> index fb627b37afcbb..32b984cb9babf 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, BITS_PER_LONG) / 8;

[Severity: High]
Can removing the - 1 from maxnode cause copy_nodes_to_user() to overflow
the caller's userspace nodemask buffer by one unsigned long?

get_nodes() still decrements maxnode on entry, and existing callers
(including libnuma and in-tree KVM selftests) allocate N words and pass
maxnode = N * BITS_PER_LONG + 1:

tools/testing/selftests/kvm/include/numaif.h:kvm_get_numa_memory_nodes() {
#define MAXNODE_FOR_MASK(mask) (BITS_PER_TYPE(mask) + 1)
...
	r = get_mempolicy(NULL, nodemask, MAXNODE_FOR_MASK(*nodemask), 0,
			  MPOL_F_MEMS_ALLOWED);
}

With ALIGN(maxnode, BITS_PER_LONG) / 8, passing maxnode = 65 for an
8-byte unsigned long nodemask computes copy = 16 bytes and writes 8 bytes
past the end of the caller's buffer.  Notice that the compat stack leak
in e130242dc351 ("mm: simplify compat numa syscalls") happened because
compat_put_bitmap() was passed the undecremented maxnode rather than
maxnode - 1 when copy <= nbytes.

>  	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, BITS_PER_COMPAT_LONG) / 8;

[Severity: High]
Does dropping the compat calculation for nbytes leave a 4-byte gap in
mask that is neither written by compat_put_bitmap() nor zeroed by
clear_user()?

On a 64-bit kernel with 1 <= nr_node_ids <= 32, nbytes is 8 bytes.  When
a compat caller passes a larger buffer (copy > nbytes, such as
maxnode = 1024), clear_user() zeroes starting at mask + 8 and clamps
maxnode to nr_node_ids, while compat_put_bitmap() rounds nr_node_ids up
to BITS_PER_COMPAT_LONG and writes only 4 bytes at mask, leaving bytes
4..7 of the userspace buffer untouched.  Additionally, when a compat
caller passes 33 <= maxnode <= 64 on such a system, copy is 8 and copy >
nbytes is false, so maxnode is no longer clamped to nr_node_ids.

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


      parent reply	other threads:[~2026-11-18 14:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 14:25 [PATCH] mm/mempolicy: fix copy_nodes_to_user() for odd inputs Arnd Bergmann
2026-11-17 14:59 ` Lorenzo Stoakes (ARM)
2026-11-17 15:26   ` Arnd Bergmann
2026-11-17 15:36     ` Lorenzo Stoakes (ARM)
2026-11-17 19:59       ` Gregory Price (Meta)
2026-11-17 20:31         ` Arnd Bergmann
2026-11-18 14:26 ` 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-221201@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