Re: [PATCH 1/2] [v2] mm/mempolicy: fix copy_nodes_to_user() for odd inputs

From: Gregory Price (Meta)

Date: Tue Nov 17 2026 - 22:09:21 EST


On 2026-11-17 22:14 +0100, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@xxxxxxxx>
>
> The compat_put_bitmap() argument has an off-by-one error that leads to
> copying an extra 32-bit word from the kernel stack to userspace when
> called with an argument that is one larger than a 64-bit number.
>
> 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.
>
> Fix the calculation where needed to use the correct -1 offset
> and pad as needed.
>
> Reported-by: larips <larips950@xxxxxxxxx>
> Reported-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
> Fixes: e130242dc351 ("mm: simplify compat numa syscalls")
> Link: https://lore.kernel.org/all/20261117101910.1820066-1-gourry@xxxxxxxxxx/
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Arnd Bergmann <arnd@xxxxxxxx>
> ---
> v2: as Lorenzo pointed out, the off-by-one mistake is part of the ABI
> and cannot change, we just have to use it consistently.
> ---
> mm/mempolicy.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> index 6d662a9a8c74..e471ce615691 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-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;

bleh this is such a mess to reason about.

Pretty sure you need to keep both of these lines:

if (compat) {
copy = ALIGN(maxnode, BITS_PER_COMPAT_LONG) / 8;
nbytes = BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t);
}

Otherwise now we can get the opposite problem. If the buffer is longer
than nbytes, we'll write too few.

Test:
```
/* 32-bit get_mempolicy() leaves nodemask word 1 stale (needs <= 32 nodes). */
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>

int main(void)
{
/* int $0x80 is a compat syscall: pointers must be below 4G */
unsigned int *m = mmap(0, 4096, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
long ret;

memset(m, 0xa5, 4096);
/* i386 get_mempolicy(275)(&policy, mask, maxnode=1025, 0, MPOL_F_MEMS_ALLOWED) */
asm volatile("int $0x80" : "=a"(ret)
: "a"(275), "b"(m + 512), "c"(m), "d"(1025), "S"(0), "D"(4)
: "memory");
printf("ret=%ld word0=%08x word1=%08x -> %s\n", ret, m[0], m[1],
m[1] == 0xa5a5a5a5 ? "FAIL: word 1 stale" : "ok");
return m[1] == 0xa5a5a5a5;
}
```

base - word0=00000003 word1=00000000 -> ok
this patch - word0=00000003 word1=a5a5a5a5 -> FAIL
fix above - word0=00000003 word1=00000000 -> ok

--
~Gregory