From: "Arnd Bergmann" <arnd@arndb.de>
To: "Gregory Price" <gourry@gourry.net>, linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org, kernel-team@meta.com,
"David Hildenbrand (Red Hat)" <david@kernel.org>,
"Zi Yan" <ziy@nvidia.com>,
"Joshua Hahn" <joshua.hahnjy@gmail.com>,
rakie.kim@sk.com, "Ying Huang" <ying.huang@linux.alibaba.com>,
"Andrew Morton" <akpm@linux-foundation.org>,
larips <larips950@gmail.com>,
stable@vger.kernel.org
Subject: Re: [PATCH] mm/mempolicy: limit compat bitmap reads to the source nodemask
Date: Tue, 17 Nov 2026 14:51:49 +0100 [thread overview]
Message-ID: <6695bd2a-5450-48b6-8ebc-8b847e7c1dad@app.fastmail.com> (raw)
In-Reply-To: <3b9d974c-18d3-4113-b7a6-1f785f19b318@app.fastmail.com>
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;
prev parent reply other threads:[~2026-11-17 13:52 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 10:19 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 message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6695bd2a-5450-48b6-8ebc-8b847e7c1dad@app.fastmail.com \
--to=arnd@arndb.de \
--cc=akpm@linux-foundation.org \
--cc=david@kernel.org \
--cc=gourry@gourry.net \
--cc=joshua.hahnjy@gmail.com \
--cc=kernel-team@meta.com \
--cc=larips950@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=rakie.kim@sk.com \
--cc=stable@vger.kernel.org \
--cc=ying.huang@linux.alibaba.com \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®