Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] [v2] mm/mempolicy: fix copy_nodes_to_user() for odd inputs
@ 2026-11-17 21:14 Arnd Bergmann
  2026-11-17 21:14 ` [PATCH 2/2] [v2] mm/mempolicy: replace copy_nodes_to_user() interface with put_nodes() Arnd Bergmann
  0 siblings, 1 reply; 2+ messages in thread
From: Arnd Bergmann @ 2026-11-17 21:14 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 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@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>
---
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;
 
 	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;
 }
-- 
2.53.0



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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 21:14 [PATCH 1/2] [v2] mm/mempolicy: fix copy_nodes_to_user() for odd inputs Arnd Bergmann
2026-11-17 21:14 ` [PATCH 2/2] [v2] mm/mempolicy: replace copy_nodes_to_user() interface with put_nodes() Arnd Bergmann

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