Skip to contentSkip to conversation

mm/mempolicy: fix copy_nodes_to_user() for odd inputs

PATCH Superseded Arnd Bergmann 5 participants 11 messages Nov 17 – Nov 20

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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

From: Arnd Bergmann <[email protected]>

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.

---
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(-)
mm/mempolicy.c +4 -4
index 6d662a9a8c74..e471ce615691 100644
@@ -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)
@@ -1762,12 +1762,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;
 }
Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 2/2 mm/mempolicy: replace copy_nodes_to_user() interface with put_nodes()

From: Arnd Bergmann <[email protected]>

The get_nodes() function handles copying node masks from userspace, while copy_nodes_to_user() copies the same masks to userspace, but with a different argument order and slightly different meaning of the same variables.

To make that less confusing, change copy_nodes_to_user() to look more like get_nodes(), using similar name and arguments as well as adjusting the maxnode value once, with a comment explaining it in both places.

---
 mm/mempolicy.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
mm/mempolicy.c +11 -8
index e471ce615691..bb18867ba41e 100644
@@ -1713,7 +1713,7 @@ static int get_bitmap(unsigned long *mask, const unsigned long __user *nmask,
 static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
 		     unsigned long maxnode)
 {
-	--maxnode;
+	--maxnode; /* user space ABI uses number of bits plus 1 */
 	nodes_clear(*nodes);
 	if (maxnode == 0 || !nmask)
 		return 0;
@@ -1746,15 +1746,18 @@ static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
 }
 
 /* Copy a kernel node mask to user space */
-static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
-			      nodemask_t *nodes)
+static int put_nodes(unsigned long __user *mask, nodemask_t *nodes,
+		     unsigned long maxnode)
 {
-	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();
+	unsigned long copy;
+
+	--maxnode; /* user space ABI uses number of bits plus 1 */
 
+	copy = ALIGN(maxnode, BITS_PER_LONG) / 8;
 	if (compat)
-		copy = ALIGN(maxnode-1, BITS_PER_COMPAT_LONG) / 8;
+		copy = ALIGN(maxnode, BITS_PER_COMPAT_LONG) / 8;
 
 	if (copy > nbytes) {
 		if (copy > PAGE_SIZE)
@@ -1762,12 +1765,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+1;
+		maxnode = nr_node_ids;
 	}
 
 	if (compat)
 		return compat_put_bitmap((compat_ulong_t __user *)mask,
-					 nodes_addr(*nodes), maxnode-1);
+					 nodes_addr(*nodes), maxnode);
 
 	return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
 }
@@ -2030,7 +2033,7 @@ static int kernel_get_mempolicy(int __user *policy,
 		return -EFAULT;
 
 	if (nmask)
-		err = copy_nodes_to_user(nmask, maxnode, &nodes);
+		err = put_nodes(nmask, &nodes, maxnode);
 
 	return err;
 }
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 2/2] [v2] mm/mempolicy: replace copy_nodes_to_user() interface with put_nodes()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] mm/mempolicy: unadjusted nbytes for compat in put_nodes() leaves 4-byte gap --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] mm/mempolicy: unadjusted nbytes for compat in put_nodes() leaves 4-byte gap
--

commit 2830eda1a2add7e034fa70931e6090d2764cf894
Author: Arnd Bergmann <[email protected]>

mm/mempolicy: replace copy_nodes_to_user() interface with put_nodes()

This commit renames copy_nodes_to_user() to put_nodes(), reorders its arguments to match get_nodes(), and decrements maxnode once at the start of the function.

Quoted 4 lines
mm/mempolicy.c +0 -0
index c10f9d4ba38f8..47bd36ac70787 100644

[ ... ]

Quoted 25 lines
mm/mempolicy.c +7 -4
@@ -1717,15 +1717,18 @@ static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
 }
 
 /* Copy a kernel node mask to user space */
-static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
-			      nodemask_t *nodes)
+static int put_nodes(unsigned long __user *mask, nodemask_t *nodes,
+		     unsigned long maxnode)
 {
-	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();
+	unsigned long copy;
+
+	--maxnode; /* user space ABI uses number of bits plus 1 */
 
+	copy = ALIGN(maxnode, BITS_PER_LONG) / 8;
 	if (compat)
-		copy = ALIGN(maxnode-1, BITS_PER_COMPAT_LONG) / 8;
+		copy = ALIGN(maxnode, BITS_PER_COMPAT_LONG) / 8;
 
 	if (copy > nbytes) {
 		if (copy > PAGE_SIZE)
@@ -1733,12 +1736,12 @@ static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
 		if (clear_user((char __user *)mask + nbytes, copy - nbytes))

mm/mempolicy.c static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode, · line 1744

Quoted 10 lines
mm/mempolicy.c +2 -2
 			return -EFAULT;
 		copy = nbytes;
-		maxnode = nr_node_ids+1;
+		maxnode = nr_node_ids;
 	}
 
 	if (compat)
 		return compat_put_bitmap((compat_ulong_t __user *)mask,
-					 nodes_addr(*nodes), maxnode-1);
+					 nodes_addr(*nodes), maxnode);

[Severity: High]
Can this leave a 4-byte gap of uninitialized user memory in mask when called from a 32-bit compat syscall?

nbytes is sized in unsigned long units (a multiple of 8 bytes on 64-bit kernels) and is not adjusted for compat, whereas compat_put_bitmap() rounds bitmap_size up to a 32-bit compat_ulong_t boundary:

kernel/compat.c:compat_put_bitmap() {
	/* align bitmap up to nearest compat_long_t boundary */
	bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
	nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
    ...
}

When nr_node_ids spans an odd number of 32-bit words (for example, 1 to 32 nodes, where nbytes is 8) and a compat caller passes a buffer with copy > nbytes, clear_user() zeroes starting at mask + nbytes while compat_put_bitmap() with maxnode = nr_node_ids writes only nbytes - 4 bytes, leaving the 4 bytes at [nbytes - 4, nbytes) untouched. In addition, when maxnode is between 34 and 65 on such a system, copy is 8 (equal to nbytes), so the copy > nbytes check is skipped and maxnode is not clamped to nr_node_ids.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#3 lore raw

Re: [PATCH 2/2] [v2] mm/mempolicy: replace copy_nodes_to_user() interface with put_nodes()

Quoted 21 lines · On Wed, Nov 18, 2026, at 22:16, [email protected] wrote:

mm/mempolicy.c static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode, · line 1744

Quoted 12 lines
mm/mempolicy.c +2 -2
@@ -1733,12 +1736,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+1;
+		maxnode = nr_node_ids;
 	}
 
 	if (compat)
 		return compat_put_bitmap((compat_ulong_t __user *)mask,
-					 nodes_addr(*nodes), maxnode-1);
+					 nodes_addr(*nodes), maxnode);

[Severity: High]
Can this leave a 4-byte gap of uninitialized user memory in mask when called from a 32-bit compat syscall?

nbytes is sized in unsigned long units (a multiple of 8 bytes on 64-bit kernels) and is not adjusted for compat, whereas compat_put_bitmap() rounds bitmap_size up to a 32-bit compat_ulong_t boundary:

Quoted 6 lines
kernel/compat.c:compat_put_bitmap() {
	/* align bitmap up to nearest compat_long_t boundary */
	bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
	nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
    ...
}

Correct.

Quoted 8 lines

When nr_node_ids spans an odd number of 32-bit words (for example, 1 to 32 nodes, where nbytes is 8) and a compat caller passes a buffer with copy > nbytes, clear_user() zeroes starting at mask + nbytes while compat_put_bitmap() with maxnode = nr_node_ids writes only nbytes - 4 bytes, leaving the 4 bytes at [nbytes - 4, nbytes) untouched. In addition, when maxnode is between 34 and 65 on such a system, copy is 8 (equal to nbytes), so the copy > nbytes check is skipped and maxnode is not clamped to nr_node_ids.

This all sounds like it should works as designed:

- when userspace asks for a larger mask and only the first 1 through 32 nodes are set, nbytes=8 is correct as it copies all eight bytes of the in-kernel nodemask and clears all additional bytes.

- When maxnode is between 34 and 65, userspace expects a 64-bit mask to be written, which the kernel correctly writes from the first 64-bit word of the on-stack 'nodes' mask, which are always valid.

Setting nbytes to the smaller value as I originally did would also work in both cases but seems more confusing. The only difference is that it would pad four bytes explicitly with clear_user() rather than copy the zeroes from the in-kernel nodemask.

Arnd

#4 lore raw
In reply to Arnd Bergmann
Quoted 32 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

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.

---
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(-)
mm/mempolicy.c +0 -0
index 6d662a9a8c74..e471ce615691 100644
@@ -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,

mm/mempolicy.c static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask, · line 1757

Quoted 10 lines
mm/mempolicy.c +2 -2
 			      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
Signature · 1 line
~Gregory
#5 lore raw
In reply to Arnd Bergmann
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] mm/mempolicy: missing compat nbytes update in copy_nodes_to_user() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] mm/mempolicy: missing compat nbytes update in copy_nodes_to_user()
--

commit 8559c85c3c122388b2f3048335523c715d5cc2db
Author: Arnd Bergmann <[email protected]>

mm/mempolicy: fix copy_nodes_to_user() for odd inputs

This commit fixes the copy size alignment and the maxnode off-by-one calculation in copy_nodes_to_user() for native and compat callers.

Quoted 24 lines
mm/mempolicy.c +2 -3
index fb627b37afcbb..c10f9d4ba38f8 100644
@@ -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-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)
@@ -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;

mm/mempolicy.c static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode, · line 1744

Quoted 10 lines
mm/mempolicy.c +2 -1
+		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;
 }

[Severity: High]
Does replacing the compat nbytes assignment with the compat copy assignment leave a 4-byte gap in the userspace nodemask when BITS_TO_COMPAT_LONGS(nr_node_ids) is odd?

Without the compat nbytes update, nbytes stays a multiple of sizeof(long) (e.g., 8 bytes when nr_node_ids <= 32). When a compat caller passes a buffer with copy > nbytes, clear_user() zeroes starting at mask + nbytes and maxnode is clamped to nr_node_ids + 1, but compat_put_bitmap() aligns maxnode - 1 to BITS_PER_COMPAT_LONG and only writes BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t) bytes (4 bytes), leaving the word at nbytes - sizeof(compat_long_t) untouched. When copy == nbytes (e.g., 34 <= maxnode <= 65 with nr_node_ids <= 32), copy > nbytes is false, so maxnode is not clamped and compat_put_bitmap() copies the upper 32 bits of the kernel unsigned long instead of zeroing that word.

It looks like the if (compat) block needs to set both nbytes and copy (the follow-up commit "mm/mempolicy: use put_bitmap() instead of copy_to_user()" has the same issue in put_nodes()).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#6 lore raw
Quoted 6 lines · On Wed, Nov 18, 2026, at 22:16, [email protected] wrote:

[Severity: High]
Does replacing the compat nbytes assignment with the compat copy assignment leave a 4-byte gap in the userspace nodemask when BITS_TO_COMPAT_LONGS(nr_node_ids) is odd?

Quoted 11 lines

Without the compat nbytes update, nbytes stays a multiple of sizeof(long) (e.g., 8 bytes when nr_node_ids <= 32). When a compat caller passes a buffer with copy > nbytes, clear_user() zeroes starting at mask + nbytes and maxnode is clamped to nr_node_ids + 1, but compat_put_bitmap() aligns maxnode - 1 to BITS_PER_COMPAT_LONG and only writes BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t) bytes (4 bytes), leaving the word at nbytes - sizeof(compat_long_t) untouched. When copy == nbytes (e.g., 34 <= maxnode <= 65 with nr_node_ids <= 32), copy > nbytes is false, so maxnode is not clamped and compat_put_bitmap() copies the upper 32 bits of the kernel unsigned long instead of zeroing that word.

Same as my reply for patch 2/2: no, this is fine as the size of the in-kernel buffer does not depend on the word size in userspace. Copying the extra zeroes from it is fine.

Arnd

#7 lore raw
Quoted 20 lines · Arnd Bergmann
Quoted 6 lines · On Wed, Nov 18, 2026, at 22:16, [email protected] wrote:

[Severity: High]
Does replacing the compat nbytes assignment with the compat copy assignment leave a 4-byte gap in the userspace nodemask when BITS_TO_COMPAT_LONGS(nr_node_ids) is odd?

Quoted 11 lines

Without the compat nbytes update, nbytes stays a multiple of sizeof(long) (e.g., 8 bytes when nr_node_ids <= 32). When a compat caller passes a buffer with copy > nbytes, clear_user() zeroes starting at mask + nbytes and maxnode is clamped to nr_node_ids + 1, but compat_put_bitmap() aligns maxnode - 1 to BITS_PER_COMPAT_LONG and only writes BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t) bytes (4 bytes), leaving the word at nbytes - sizeof(compat_long_t) untouched. When copy == nbytes (e.g., 34 <= maxnode <= 65 with nr_node_ids <= 32), copy > nbytes is false, so maxnode is not clamped and compat_put_bitmap() copies the upper 32 bits of the kernel unsigned long instead of zeroing that word.

Quoted 3 lines

Same as my reply for patch 2/2: no, this is fine as the size of the in-kernel buffer does not depend on the word size in userspace. Copying the extra zeroes from it is fine.

I'm not convinced, especially given Gregory's test case. It's all a confusing mess.

I do wonder whether this whole function should be thinking in terms of bits to make it easier to follow. A bit more churn, but at least I can understand what is happening there:

mm/mempolicy.c +23 -14
index c7d4d149c9b88..5b99f7b95dade 100644
@@ -1713,27 +1713,36 @@ 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 int nbytes = BITS_TO_LONGS(nr_node_ids) * sizeof(long);
-	bool compat = in_compat_syscall();
+	const bool compat = in_compat_syscall();
+	const unsigned int word_bits = compat ? BITS_PER_COMPAT_LONG : BITS_PER_LONG;
+	unsigned long bits, node_bits;
 
-	if (compat)
-		nbytes = BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t);
+	/* User space passes the number of bits plus 1. Fix that up. */
+	maxnode -= 1;
 
-	if (copy > nbytes) {
-		if (copy > PAGE_SIZE)
-			return -EINVAL;
-		if (clear_user((char __user *)mask + nbytes, copy - nbytes))
+	/* Don't exceed what would fit into a single page. */
+	if (maxnode > PAGE_SIZE * BITS_PER_BYTE)
+		return -EINVAL;
+
+	/* Process full words, which depend on the user space ABI. */
+	bits = ALIGN(maxnode, word_bits);
+	node_bits = ALIGN(nr_node_ids, word_bits);
+
+	/* Clear anything we won't copy below. */
+	if (bits > node_bits) {
+		if (clear_user((char __user *)mask + node_bits / BITS_PER_BYTE,
+			       (bits - node_bits) / BITS_PER_BYTE))
 			return -EFAULT;
-		copy = nbytes;
-		maxnode = nr_node_ids;
+		bits = node_bits;
 	}
 
+	/* Finally, copy the bitmap. */
 	if (compat)
 		return compat_put_bitmap((compat_ulong_t __user *)mask,
-					 nodes_addr(*nodes), maxnode);
-
-	return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
+					 nodes_addr(*nodes), bits);
+	if (copy_to_user(mask, nodes_addr(*nodes), bits / BITS_PER_BYTE))
+		return -EFAULT;
+	return 0;
 }
 
 /* Basic parameter sanity check used by both mbind() and set_mempolicy() */
Signature · 4 lines
-- 
Cheers,

David
#8 lore raw
Quoted 21 lines · David Hildenbrand (Arm)
Quoted 20 lines · Arnd Bergmann
Quoted 17 lines · On Wed, Nov 18, 2026, at 22:16, [email protected] wrote:

[Severity: High]
Does replacing the compat nbytes assignment with the compat copy assignment leave a 4-byte gap in the userspace nodemask when BITS_TO_COMPAT_LONGS(nr_node_ids) is odd?

Without the compat nbytes update, nbytes stays a multiple of sizeof(long) (e.g., 8 bytes when nr_node_ids <= 32). When a compat caller passes a buffer with copy > nbytes, clear_user() zeroes starting at mask + nbytes and maxnode is clamped to nr_node_ids + 1, but compat_put_bitmap() aligns maxnode - 1 to BITS_PER_COMPAT_LONG and only writes BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t) bytes (4 bytes), leaving the word at nbytes - sizeof(compat_long_t) untouched. When copy == nbytes (e.g., 34 <= maxnode <= 65 with nr_node_ids <= 32), copy > nbytes is false, so maxnode is not clamped and compat_put_bitmap() copies the upper 32 bits of the kernel unsigned long instead of zeroing that word.

Quoted 6 lines
Quoted 3 lines

Same as my reply for patch 2/2: no, this is fine as the size of the in-kernel buffer does not depend on the word size in userspace. Copying the extra zeroes from it is fine.

I'm not convinced, especially given Gregory's test case. It's all a confusing mess.

Right, I see now where I went wrong: 'maxnode' is adjusted down to the nr_node_ids, not the number of bits in the bitmap. So my patch was wrong, and Gregory's was correct.

Quoted 4 lines

I do wonder whether this whole function should be thinking in terms of bits to make
it easier to follow. A bit more churn, but at least I can understand what is happening there:

I like this version as well.

Arnd

#9 lore raw
Quoted 15 lines · Arnd Bergmann
Quoted 9 lines · David Hildenbrand (Arm)
Quoted 5 lines · Arnd Bergmann

Same as my reply for patch 2/2: no, this is fine as the size of the in-kernel buffer does not depend on the word size in userspace. Copying the extra zeroes from it is fine.

I'm not convinced, especially given Gregory's test case. It's all a confusing mess.

Right, I see now where I went wrong: 'maxnode' is adjusted down to the nr_node_ids, not the number of bits in the bitmap. So my patch was wrong, and Gregory's was correct.

Quoted 6 lines
Quoted 4 lines

I do wonder whether this whole function should be thinking in terms of bits to make
it easier to follow. A bit more churn, but at least I can understand what is happening there:

I like this version as well.

Does your #2 still apply once we would have that (in a reworked form)?

I'd appreciate if you could resend based on my suggestion. Feel free to adjust what you want and just keep it to a Suggested-by (No need for Author, co-developed-by or any of that).

Signature · 3 lines
Cheers,

David
#10 lore raw
Quoted 10 lines · David Hildenbrand (Arm)
Quoted 3 lines · Arnd Bergmann

On Fri, Nov 20, 2026, at 10:55, David Hildenbrand (Arm) wrote:

I like this version as well.

Does your #2 still apply once we would have that (in a reworked form)?

I'd appreciate if you could resend based on my suggestion. Feel free to adjust what you want and just keep it to a Suggested-by (No need for Author, co-developed-by or any of that).

Ok, sent a v3 with a minimal fix for backporting and the combination of your cleanup and mine as the second patch. Thanks,

Arnd