[PATCH] mm/mempolicy: limit compat bitmap reads to the source nodemask

From: Gregory Price (Meta)

Date: Tue Nov 17 2026 - 05:20:06 EST


A 32-bit get_mempolicy() call can read past the kernel's stack nodemask.
With 64 possible nodes and maxnode=65, copy_nodes_to_user() computes an
eight-byte copy, but compat_put_bitmap() reads two native words.

A kernel built with CONFIG_NODES_SHIFT=6 reports:

BUG: KASAN: stack-out-of-bounds in compat_put_bitmap+0x113/0x120
Read of size 8 at addr ff110000099afeb0 by task mpol32/246
Call Trace:
compat_put_bitmap+0x113/0x120
copy_nodes_to_user+0x137/0x200
__ia32_sys_get_mempolicy+0x1a6/0x280

Bound the compat bitmap read by MAX_NUMNODES.

Clamping every call to nr_node_ids would remove the extra userspace
word when the kernel nodemask is large enough to contain it, but this
keep that existing compat behavior while preventing reads beyond the
source allocation.

Reported-by: larips <larips950@xxxxxxxxx>
Fixes: e130242dc351 ("mm: simplify compat numa syscalls")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
---
mm/mempolicy.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 79053ece02cd4..d9177c5504879 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -1715,7 +1715,8 @@ static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,

if (compat)
return compat_put_bitmap((compat_ulong_t __user *)mask,
- nodes_addr(*nodes), maxnode);
+ nodes_addr(*nodes),
+ min(maxnode, MAX_NUMNODES));

return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
}
--
2.53.1-Meta