From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Arnd Bergmann <arnd@arndb.de>,
sashiko-reviews@lists.linux.dev, Arnd Bergmann <arnd@kernel.org>,
Gregory Price <gourry@gourry.net>,
Andrew Morton <akpm@linux-foundation.org>
Cc: larips <larips950@gmail.com>, Zi Yan <ziy@nvidia.com>,
Joshua Hahn <joshua.hahnjy@gmail.com>,
Rakie Kim <rakie.kim@sk.com>,
Ying Huang <ying.huang@linux.alibaba.com>,
Lorenzo Stoakes <ljs@kernel.org>,
Brendan Jackman <brendan.jackman@linux.dev>,
Johannes Weiner <hannes@cmpxchg.org>,
Jackie Liu <liuyun01@kylinos.cn>,
linux-mm@kvack.org
Subject: Re: [PATCH 1/2] [v2] mm/mempolicy: fix copy_nodes_to_user() for odd inputs
Date: Fri, 20 Nov 2026 10:55:39 +0100 [thread overview]
Message-ID: <3b00e53f-b4eb-41d4-b3e6-791660da3342@kernel.org> (raw)
In-Reply-To: <c420ec17-b297-469a-927a-85934a05fda4@app.fastmail.com>
On 11/19/26 16:52, Arnd Bergmann wrote:
> On Wed, Nov 18, 2026, at 22:16, sashiko-bot@kernel.org 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.
>
> 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:
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index c7d4d149c9b88..5b99f7b95dade 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -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() */
--
--
Cheers,
David
next prev parent reply other threads:[~2026-11-20 9:55 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
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
2026-11-18 21:16 ` sashiko-bot
2026-11-19 15:41 ` Arnd Bergmann
2026-11-18 3:09 ` [PATCH 1/2] [v2] mm/mempolicy: fix copy_nodes_to_user() for odd inputs Gregory Price (Meta)
2026-11-18 21:16 ` sashiko-bot
2026-11-19 15:52 ` Arnd Bergmann
2026-11-20 9:55 ` David Hildenbrand (Arm) [this message]
2026-11-20 10:16 ` Arnd Bergmann
2026-11-20 10:39 ` David Hildenbrand (Arm)
2026-11-20 18:57 ` Arnd Bergmann
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=3b00e53f-b4eb-41d4-b3e6-791660da3342@kernel.org \
--to=david@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=arnd@kernel.org \
--cc=brendan.jackman@linux.dev \
--cc=gourry@gourry.net \
--cc=hannes@cmpxchg.org \
--cc=joshua.hahnjy@gmail.com \
--cc=larips950@gmail.com \
--cc=linux-mm@kvack.org \
--cc=liuyun01@kylinos.cn \
--cc=ljs@kernel.org \
--cc=rakie.kim@sk.com \
--cc=sashiko-reviews@lists.linux.dev \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.