* [PATCH] mm/mempolicy: limit compat bitmap reads to the source nodemask
@ 2026-11-17 10:19 Gregory Price (Meta)
2026-11-17 11:10 ` David Hildenbrand (Arm)
2026-11-17 12:44 ` Arnd Bergmann
0 siblings, 2 replies; 4+ messages in thread
From: Gregory Price (Meta) @ 2026-11-17 10:19 UTC (permalink / raw)
To: linux-mm
Cc: linux-kernel, kernel-team, david, gourry, ziy, joshua.hahnjy,
rakie.kim, ying.huang, akpm, arnd, larips, stable
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@gmail.com>
Fixes: e130242dc351 ("mm: simplify compat numa syscalls")
Cc: stable@vger.kernel.org
Signed-off-by: Gregory Price (Meta) <gourry@gourry.net>
---
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
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] mm/mempolicy: limit compat bitmap reads to the source nodemask 2026-11-17 10:19 [PATCH] mm/mempolicy: limit compat bitmap reads to the source nodemask Gregory Price (Meta) @ 2026-11-17 11:10 ` David Hildenbrand (Arm) 2026-11-17 12:44 ` Arnd Bergmann 1 sibling, 0 replies; 4+ messages in thread From: David Hildenbrand (Arm) @ 2026-11-17 11:10 UTC (permalink / raw) To: Gregory Price (Meta), linux-mm Cc: linux-kernel, kernel-team, ziy, joshua.hahnjy, rakie.kim, ying.huang, akpm, arnd, larips, stable On 11/17/26 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. > > 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@gmail.com> > Fixes: e130242dc351 ("mm: simplify compat numa syscalls") > Cc: stable@vger.kernel.org > Signed-off-by: Gregory Price (Meta) <gourry@gourry.net> > --- > 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; > } LGTM Staring at this once more, I think it's all confusing how we handle compat_put_bitmap() vs. copy_to_user(). with "copy" and "nbytes". Likely we really should be working on bits, to then only align it up for the copy_to_user() case. I'll wait a bit for more review before picking this, Reviewed-by: David Hildenbrand (Arm) <david@kernel.org> -- Cheers, David ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mm/mempolicy: limit compat bitmap reads to the source nodemask 2026-11-17 10:19 [PATCH] mm/mempolicy: limit compat bitmap reads to the source nodemask Gregory Price (Meta) 2026-11-17 11:10 ` David Hildenbrand (Arm) @ 2026-11-17 12:44 ` Arnd Bergmann 2026-11-17 13:51 ` Arnd Bergmann 1 sibling, 1 reply; 4+ messages in thread From: Arnd Bergmann @ 2026-11-17 12:44 UTC (permalink / raw) To: Gregory Price, linux-mm Cc: linux-kernel, kernel-team, David Hildenbrand (Red Hat), Zi Yan, Joshua Hahn, rakie.kim, Ying Huang, Andrew Morton, larips, stable 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 ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mm/mempolicy: limit compat bitmap reads to the source nodemask 2026-11-17 12:44 ` Arnd Bergmann @ 2026-11-17 13:51 ` Arnd Bergmann 0 siblings, 0 replies; 4+ messages in thread From: Arnd Bergmann @ 2026-11-17 13:51 UTC (permalink / raw) To: Gregory Price, linux-mm Cc: linux-kernel, kernel-team, David Hildenbrand (Red Hat), Zi Yan, Joshua Hahn, rakie.kim, Ying Huang, Andrew Morton, larips, stable On Tue, Nov 17, 2026, at 13:44, Arnd Bergmann wrote: > 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; I guess this would be an ABI change for 32-bit userspace asking for maxnum that results in an odd number of 32-bit words. The man page documents this as "The value specified by maxnode is always rounded to a multiple of sizeof(unsigned long)*8", while the code here has always rounded it up to a multiple of 64 in both native (no longer supported on 32-bit) and compat syscalls. numactl (1) always uses 128 or 2048 bit masks and won't notice it either way, so we can possibly change the ABI here and do what the man page says, or keep the current behavior and update the man page. It looks like the other part of the problem is the off-by-one error: the "ALIGN(maxnode-1, 64) / 8" rounds down maxnode=65 to copy=8 which is the same as nbytes, but the compat_put_bitmap() treats it as a 65-bit map that needs 96 bytes. This means we have a related bug on native syscalls as well: asking for a 65 bit mask in mempolicy only returns 8 bytes without clearing the next word like it would for a 66 bit mask. If we just fix the off-by-one and keep the 64 bit alignment (with a manpage update), the version below be should be sufficient. Arnd --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -1749,13 +1749,10 @@ 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, 64) / 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); - if (copy > nbytes) { if (copy > PAGE_SIZE) return -EINVAL; ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-17 13:52 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-11-17 10:19 [PATCH] mm/mempolicy: limit compat bitmap reads to the source nodemask Gregory Price (Meta) 2026-11-17 11:10 ` David Hildenbrand (Arm) 2026-11-17 12:44 ` Arnd Bergmann 2026-11-17 13:51 ` Arnd Bergmann
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®