mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Arnd Bergmann" <arnd@arndb.de>
To: "Gregory Price" <gourry@gourry.net>, "Lorenzo Stoakes" <ljs@kernel.org>
Cc: "Arnd Bergmann" <arnd@kernel.org>,
	"David Hildenbrand (Red Hat)" <david@kernel.org>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	larips <larips950@gmail.com>,
	stable@vger.kernel.org, "Zi Yan" <ziy@nvidia.com>,
	"Joshua Hahn" <joshua.hahnjy@gmail.com>,
	"Rakie Kim" <rakie.kim@sk.com>,
	"Ying Huang" <ying.huang@linux.alibaba.com>,
	"Brendan Jackman" <brendan.jackman@linux.dev>,
	"Johannes Weiner" <hannes@cmpxchg.org>,
	"Jackie Liu" <liuyun01@kylinos.cn>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] mm/mempolicy: fix copy_nodes_to_user() for odd inputs
Date: Tue, 17 Nov 2026 21:31:16 +0100	[thread overview]
Message-ID: <9e7805c3-d41b-4d5e-ac67-8474a5671d86@app.fastmail.com> (raw)
In-Reply-To: <179494557732.482102.7719202079181127037.b4-reply@b4>

On Tue, Nov 17, 2026, at 20:59, Gregory Price (Meta) wrote:
> What about the following:
>
> --- 1/2 mm/mempolicy: convert maxnode to a bit count in copy_nodes_to_user()
>
> Converts maxnode to a bit count on entry to copy_nodes_to_user() and 
> get_nodes(), fixing the compat KASAN stack over-read and stack leak. 
> Fixes e130242dc351, Cc stable.
>
>
> Subject: [PATCH 1/2] mm/mempolicy: convert maxnode to a bit count in
>  copy_nodes_to_user()
>
> Whenever maxnode - 1 is a multiple of 32, compat_put_bitmap() reads one

'multiple of 64' I think

> more compat_ulong_t from the kernel nodemask than the caller asked for,
> and writes it past the end of the caller's bitmap.
>
> When the kernel nodemask ends there, that read goes past the on-stack
> nodemask_t in kernel_get_mempolicy(), and its low 32 bits are copied
> to userspace.
>
> With CONFIG_NODES_SHIFT=6, 64 possible nodes and a 32-bit caller
> passing maxnode=65:
>
>   BUG: KASAN: stack-out-of-bounds in compat_put_bitmap+0x122/0x130
>   Read of size 8 at addr ffff8880150d7ec0 by task mpoltest/241
>    compat_put_bitmap+0x122/0x130
>    copy_nodes_to_user+0x14a/0x250
>    __ia32_sys_get_mempolicy+0x1b0/0x2b0
>
> Convert maxnode to a bit count once, on entry, and use only that in the
> rest of copy_nodes_to_user(), so the compat path gets the same size as
> the native one. Do the same in get_nodes().
>
> Fixes: e130242dc351 ("mm: simplify compat numa syscalls")

I like the renaming as a cleanup, though it does make it a little
harder to backport to stable kernels from context changes, maybe
it's better to do do the minimal fix first.

>  mm/mempolicy.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> index 390a31a99b4ff..cddca1db3d9a1 100644
> --- a/mm/mempolicy.c
> +++ b/mm/mempolicy.c
> @@ -1725,8 +1725,10 @@ static int copy_nodes_to_user(unsigned long 
> __user *mask, unsigned long maxnode,
>  	unsigned int nbytes = BITS_TO_LONGS(nr_node_ids) * sizeof(long);
>  	bool compat = in_compat_syscall();
> 
> -	if (compat)
> +	if (compat) {
> +		copy = ALIGN(nbits, BITS_PER_COMPAT_LONG) / 8;
>  		nbytes = BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t);
> +	}

It fixes the bug, but I'd drop the nbytes= assignment here, as it
has no benefit: The only configurations where nbytes actually gets
a different value is for NODES_SHIFT<6, but as the on-stack
bitmask is still a 64-bit word here, the only difference is whether
the extra zeroes come from memset() or compat_put_bitmap().

I assume that when I introduced this bug, I simply confused which of
the two variables needed to be adjusted.

      Arnd

      reply	other threads:[~2026-11-17 20:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 14:25 Arnd Bergmann
2026-11-17 14:59 ` Lorenzo Stoakes (ARM)
2026-11-17 15:26   ` Arnd Bergmann
2026-11-17 15:36     ` Lorenzo Stoakes (ARM)
2026-11-17 19:59       ` Gregory Price (Meta)
2026-11-17 20:31         ` 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=9e7805c3-d41b-4d5e-ac67-8474a5671d86@app.fastmail.com \
    --to=arnd@arndb.de \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@kernel.org \
    --cc=brendan.jackman@linux.dev \
    --cc=david@kernel.org \
    --cc=gourry@gourry.net \
    --cc=hannes@cmpxchg.org \
    --cc=joshua.hahnjy@gmail.com \
    --cc=larips950@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=liuyun01@kylinos.cn \
    --cc=ljs@kernel.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®