The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] mm/mempolicy: fix copy_nodes_to_user() for odd inputs
@ 2026-11-17 14:25 Arnd Bergmann
  2026-11-17 14:59 ` Lorenzo Stoakes (ARM)
  0 siblings, 1 reply; 4+ messages in thread
From: Arnd Bergmann @ 2026-11-17 14:25 UTC (permalink / raw)
  To: David Hildenbrand, Gregory Price, Andrew Morton, Arnd Bergmann
  Cc: larips, stable, Zi Yan, Joshua Hahn, Rakie Kim, Ying Huang,
	Lorenzo Stoakes (ARM), Brendan Jackman, Johannes Weiner,
	Jackie Liu, linux-mm, linux-kernel

From: Arnd Bergmann <arnd@arndb.de>

The ALIGN(maxnode-1, 64) check has an off-by-one error that leads to
rounding down too much, and ends up not copying the last word when
userspace asks for one bit over a multiple of 64.

For compat syscalls, the effect is worse, as the actual calculation for
the size does include the extra word, but the check for the input size
does not take it into account and can leak a word of stack data.

In addition, the size calculation rounds up to multiples of 64 bits
instead of multiples of word size as documented in the man page and
expected by numactl.

Fortunately numactl always passes multiples of 64 here, so none of
this affects userspace we care about. Finally, the 'nbytes' calculation
for compat mode makes no sense since that is the size of the kernel's
internal representation, not the userspace side.

Fix the 'copy' calculation to address all of the above. Note that the
off-by-one error has been in there since the beginning, but commit
e130242dc351 ("mm: simplify compat numa syscalls") is where we starting
copying the wrong data.

Reported-by: larips <larips950@gmail.com>
Reported-by: Gregory Price (Meta) <gourry@gourry.net>
Fixes: e130242dc351 ("mm: simplify compat numa syscalls")
Link: https://lore.kernel.org/all/20261117101910.1820066-1-gourry@gourry.net/
Cc: stable@vger.kernel.org
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
Please double-check the calculation and retest this with the original
reproducer. The off-by-one error does explain all of this, I'm still
worried about getting this version wrong again as everyone including
myself seems to have missed this in the past 25 years and there may
still be additional subtle issues around it. Apparently we had many
instances of the same off-by-one in this file but all (most?) others
got fixed inadvertently by other changes in the past.
---
 mm/mempolicy.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 6d662a9a8c74..f318e1155770 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -1749,12 +1749,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;
 	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;
 
 	if (copy > nbytes) {
 		if (copy > PAGE_SIZE)
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm/mempolicy: fix copy_nodes_to_user() for odd inputs
  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
  0 siblings, 1 reply; 4+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-17 14:59 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: David Hildenbrand, Gregory Price, Andrew Morton, Arnd Bergmann,
	larips, stable, Zi Yan, Joshua Hahn, Rakie Kim, Ying Huang,
	Brendan Jackman, Johannes Weiner, Jackie Liu, linux-mm,
	linux-kernel

On Tue, Nov 17, 2026 at 03:25:53PM +0100, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
>
> The ALIGN(maxnode-1, 64) check has an off-by-one error that leads to
> rounding down too much, and ends up not copying the last word when
> userspace asks for one bit over a multiple of 64.

From what I can tell, this isn't an off-by-one, but just the (possibly weird)
convention in mempolicy.c. E.g. in get_nodes():

static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
		     unsigned long maxnode)
{
	--maxnode;
	< etc. >
}

>
> For compat syscalls, the effect is worse, as the actual calculation for
> the size does include the extra word, but the check for the input size
> does not take it into account and can leak a word of stack data.
>
> In addition, the size calculation rounds up to multiples of 64 bits
> instead of multiples of word size as documented in the man page and
> expected by numactl.
>
> Fortunately numactl always passes multiples of 64 here, so none of

(Claude) went looking at numactl source (e.g. via libnuma) and it seems it
passes size + 1 matching the convention?

> this affects userspace we care about. Finally, the 'nbytes' calculation
> for compat mode makes no sense since that is the size of the kernel's
> internal representation, not the userspace side.
>
> Fix the 'copy' calculation to address all of the above. Note that the
> off-by-one error has been in there since the beginning, but commit
> e130242dc351 ("mm: simplify compat numa syscalls") is where we starting
> copying the wrong data.
>
> Reported-by: larips <larips950@gmail.com>
> Reported-by: Gregory Price (Meta) <gourry@gourry.net>
> Fixes: e130242dc351 ("mm: simplify compat numa syscalls")
> Link: https://lore.kernel.org/all/20261117101910.1820066-1-gourry@gourry.net/
> Cc: stable@vger.kernel.org
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
> Please double-check the calculation and retest this with the original
> reproducer. The off-by-one error does explain all of this, I'm still
> worried about getting this version wrong again as everyone including
> myself seems to have missed this in the past 25 years and there may
> still be additional subtle issues around it. Apparently we had many
> instances of the same off-by-one in this file but all (most?) others
> got fixed inadvertently by other changes in the past.
> ---
>  mm/mempolicy.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> index 6d662a9a8c74..f318e1155770 100644
> --- a/mm/mempolicy.c
> +++ b/mm/mempolicy.c
> @@ -1749,12 +1749,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;

This looks like it'll be broken given the convention of passing bits + 1,
e.g. ALIGN(1025, 64) / 8 != ALIGN(1024, 64) / 8.

Then later that could case a buffer overflow potentially?

>  	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;
>

Claude suggests that this is potentially broken too for similar reasons.

>  	if (copy > nbytes) {
>  		if (copy > PAGE_SIZE)
> --
> 2.53.0
>

Overall it seems the issue is that e130242dc351 (understandably!) passes maxnode
rather than maxnode-1.

So a really small fixup would be something like:

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index fb627b37afcb..051d411c04ee 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -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;
 }

Maybe doing that's the easiest route for this?

--
Cheers, Lorenzo

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm/mempolicy: fix copy_nodes_to_user() for odd inputs
  2026-11-17 14:59 ` Lorenzo Stoakes (ARM)
@ 2026-11-17 15:26   ` Arnd Bergmann
  2026-11-17 15:36     ` Lorenzo Stoakes (ARM)
  0 siblings, 1 reply; 4+ messages in thread
From: Arnd Bergmann @ 2026-11-17 15:26 UTC (permalink / raw)
  To: Lorenzo Stoakes, Arnd Bergmann
  Cc: David Hildenbrand (Red Hat), Gregory Price, Andrew Morton, larips,
	stable, Zi Yan, Joshua Hahn, Rakie Kim, Ying Huang,
	Brendan Jackman, Johannes Weiner, Jackie Liu, linux-mm,
	linux-kernel

On Tue, Nov 17, 2026, at 15:59, Lorenzo Stoakes (ARM) wrote:
> On Tue, Nov 17, 2026 at 03:25:53PM +0100, Arnd Bergmann wrote:
>> From: Arnd Bergmann <arnd@arndb.de>
>> For compat syscalls, the effect is worse, as the actual calculation for
>> the size does include the extra word, but the check for the input size
>> does not take it into account and can leak a word of stack data.
>>
>> In addition, the size calculation rounds up to multiples of 64 bits
>> instead of multiples of word size as documented in the man page and
>> expected by numactl.
>>
>> Fortunately numactl always passes multiples of 64 here, so none of
>
> (Claude) went looking at numactl source (e.g. via libnuma) and it seems it
> passes size + 1 matching the convention?

Ok, I see it now as well. The man page again says "maxnode specifies
the number of node IDs that can be stored into  nodemask——that is,
the maximum node ID plus one.", which I'd naïvely interpret as meaning
that a system with 64 nodes would have a "maximum node ID" of 63 and
require passing one more (64), but clearly the implementation on
both sides expects a 65 here.

>> diff --git a/mm/mempolicy.c b/mm/mempolicy.c
>> index 6d662a9a8c74..f318e1155770 100644
>> --- a/mm/mempolicy.c
>> +++ b/mm/mempolicy.c
>> @@ -1749,12 +1749,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;
>
> This looks like it'll be broken given the convention of passing bits + 1,
> e.g. ALIGN(1025, 64) / 8 != ALIGN(1024, 64) / 8.
>
> Then later that could case a buffer overflow potentially?

Right, this would then overflow the user buffer.

> Overall it seems the issue is that e130242dc351 (understandably!) passes maxnode
> rather than maxnode-1.
>
> So a really small fixup would be something like:
>
> diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> index fb627b37afcb..051d411c04ee 100644
> --- a/mm/mempolicy.c
> +++ b/mm/mempolicy.c
> @@ -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;
>  }
>
> Maybe doing that's the easiest route for this?

I would still try to fix the other mistakes (aligning to 64 instead
of BITS_PER_LONG/BITS_PER_COMPAT_LONG and the incorrect nbytes).
Maybe it's clearer to just pass the adjusted maxnode into the
function directly and keep that extra '-1' in one place?

     Arnd

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 6d662a9a8c74..931194421c4f 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -1749,12 +1749,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;
 	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;
 
 	if (copy > nbytes) {
 		if (copy > PAGE_SIZE)
@@ -2030,7 +2030,7 @@ static int kernel_get_mempolicy(int __user *policy,
 		return -EFAULT;
 
 	if (nmask)
-		err = copy_nodes_to_user(nmask, maxnode, &nodes);
+		err = copy_nodes_to_user(nmask, maxnode-1, &nodes);
 
 	return err;
 }

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm/mempolicy: fix copy_nodes_to_user() for odd inputs
  2026-11-17 15:26   ` Arnd Bergmann
@ 2026-11-17 15:36     ` Lorenzo Stoakes (ARM)
  0 siblings, 0 replies; 4+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-17 15:36 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Arnd Bergmann, David Hildenbrand (Red Hat), Gregory Price,
	Andrew Morton, larips, stable, Zi Yan, Joshua Hahn, Rakie Kim,
	Ying Huang, Brendan Jackman, Johannes Weiner, Jackie Liu,
	linux-mm, linux-kernel

On Tue, Nov 17, 2026 at 04:26:48PM +0100, Arnd Bergmann wrote:
> On Tue, Nov 17, 2026, at 15:59, Lorenzo Stoakes (ARM) wrote:
> > On Tue, Nov 17, 2026 at 03:25:53PM +0100, Arnd Bergmann wrote:
> >> From: Arnd Bergmann <arnd@arndb.de>
> >> For compat syscalls, the effect is worse, as the actual calculation for
> >> the size does include the extra word, but the check for the input size
> >> does not take it into account and can leak a word of stack data.
> >>
> >> In addition, the size calculation rounds up to multiples of 64 bits
> >> instead of multiples of word size as documented in the man page and
> >> expected by numactl.
> >>
> >> Fortunately numactl always passes multiples of 64 here, so none of
> >
> > (Claude) went looking at numactl source (e.g. via libnuma) and it seems it
> > passes size + 1 matching the convention?
>
> Ok, I see it now as well. The man page again says "maxnode specifies
> the number of node IDs that can be stored into  nodemask——that is,
> the maximum node ID plus one.", which I'd naïvely interpret as meaning
> that a system with 64 nodes would have a "maximum node ID" of 63 and
> require passing one more (64), but clearly the implementation on
> both sides expects a 65 here.

Yeah which is... special isn't it? :))

>
> >> diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> >> index 6d662a9a8c74..f318e1155770 100644
> >> --- a/mm/mempolicy.c
> >> +++ b/mm/mempolicy.c
> >> @@ -1749,12 +1749,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;
> >
> > This looks like it'll be broken given the convention of passing bits + 1,
> > e.g. ALIGN(1025, 64) / 8 != ALIGN(1024, 64) / 8.
> >
> > Then later that could case a buffer overflow potentially?
>
> Right, this would then overflow the user buffer.
>
> > Overall it seems the issue is that e130242dc351 (understandably!) passes maxnode
> > rather than maxnode-1.
> >
> > So a really small fixup would be something like:
> >
> > diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> > index fb627b37afcb..051d411c04ee 100644
> > --- a/mm/mempolicy.c
> > +++ b/mm/mempolicy.c
> > @@ -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;
> >  }
> >
> > Maybe doing that's the easiest route for this?
>
> I would still try to fix the other mistakes (aligning to 64 instead
> of BITS_PER_LONG/BITS_PER_COMPAT_LONG and the incorrect nbytes).
> Maybe it's clearer to just pass the adjusted maxnode into the
> function directly and keep that extra '-1' in one place?

Yeah it'd be nice to get away from that convention which is... very
confusing :) Especially as 'max' implies to me an inclusive bound.

You could have a wrapper get_nodes() that calls __get_nodes() that has a
'lastnode' parameter for instance?

--
Cheers, Lorenzo

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-17 15:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox