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

From: Arnd Bergmann

Date: Tue Nov 17 2026 - 07:46:28 EST


On Tue, Nov 17, 2026, at 11:19, Gregory Price (Meta) wrote:
> 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.

I don't quite follow the explanation yet:

if maxnode=65, shouldn't that result in copying 16 bytes on x86-64
and 12 bytes on compat mode?

It looks like compat mode adjusts the wrong variable and we should
be doing this instead:

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 6d662a9a8c74..f9a7be9f4432 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;

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

Now in the maxnode=65 example, it should clear 4 bytes and put copy
the 8 byte bitmap.

Arnd