[PATCH] mm/mempolicy: do not allow illegal MPOL_F_NUMA_BALANCING | MPOL_LOCAL in mbind()

47 views
Skip to first unread message

Eric Dumazet

unread,
Oct 1, 2021, 1:49:54 PM10/1/21
to Andrew Morton, linux-kernel, linux-mm, Eric Dumazet, Eric Dumazet, syzbot, Huang, Ying, Mel Gorman
From: Eric Dumazet <edum...@google.com>

syzbot reported access to unitialized memory in mbind() [1]

Issue came with commit bda420b98505 ("numa balancing: migrate on
fault among multiple bound nodes")

This commit added a new bit in MPOL_MODE_FLAGS, but only checked
valid combination (MPOL_F_NUMA_BALANCING can only be used with MPOL_BIND)
in do_set_mempolicy()

This patch moves the check in sanitize_mpol_flags() so that it
is also used by mbind()

[1]
BUG: KMSAN: uninit-value in __mpol_equal+0x567/0x590 mm/mempolicy.c:2260
__mpol_equal+0x567/0x590 mm/mempolicy.c:2260
mpol_equal include/linux/mempolicy.h:105 [inline]
vma_merge+0x4a1/0x1e60 mm/mmap.c:1190
mbind_range+0xcc8/0x1e80 mm/mempolicy.c:811
do_mbind+0xf42/0x15f0 mm/mempolicy.c:1333
kernel_mbind mm/mempolicy.c:1483 [inline]
__do_sys_mbind mm/mempolicy.c:1490 [inline]
__se_sys_mbind+0x437/0xb80 mm/mempolicy.c:1486
__x64_sys_mbind+0x19d/0x200 mm/mempolicy.c:1486
do_syscall_x64 arch/x86/entry/common.c:51 [inline]
do_syscall_64+0x54/0xd0 arch/x86/entry/common.c:82
entry_SYSCALL_64_after_hwframe+0x44/0xae

Uninit was created at:
slab_alloc_node mm/slub.c:3221 [inline]
slab_alloc mm/slub.c:3230 [inline]
kmem_cache_alloc+0x751/0xff0 mm/slub.c:3235
mpol_new mm/mempolicy.c:293 [inline]
do_mbind+0x912/0x15f0 mm/mempolicy.c:1289
kernel_mbind mm/mempolicy.c:1483 [inline]
__do_sys_mbind mm/mempolicy.c:1490 [inline]
__se_sys_mbind+0x437/0xb80 mm/mempolicy.c:1486
__x64_sys_mbind+0x19d/0x200 mm/mempolicy.c:1486
do_syscall_x64 arch/x86/entry/common.c:51 [inline]
do_syscall_64+0x54/0xd0 arch/x86/entry/common.c:82
entry_SYSCALL_64_after_hwframe+0x44/0xae
=====================================================
Kernel panic - not syncing: panic_on_kmsan set ...
CPU: 0 PID: 15049 Comm: syz-executor.0 Tainted: G B 5.15.0-rc2-syzkaller #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
Call Trace:
__dump_stack lib/dump_stack.c:88 [inline]
dump_stack_lvl+0x1ff/0x28e lib/dump_stack.c:106
dump_stack+0x25/0x28 lib/dump_stack.c:113
panic+0x44f/0xdeb kernel/panic.c:232
kmsan_report+0x2ee/0x300 mm/kmsan/report.c:186
__msan_warning+0xd7/0x150 mm/kmsan/instrumentation.c:208
__mpol_equal+0x567/0x590 mm/mempolicy.c:2260
mpol_equal include/linux/mempolicy.h:105 [inline]
vma_merge+0x4a1/0x1e60 mm/mmap.c:1190
mbind_range+0xcc8/0x1e80 mm/mempolicy.c:811
do_mbind+0xf42/0x15f0 mm/mempolicy.c:1333
kernel_mbind mm/mempolicy.c:1483 [inline]
__do_sys_mbind mm/mempolicy.c:1490 [inline]
__se_sys_mbind+0x437/0xb80 mm/mempolicy.c:1486
__x64_sys_mbind+0x19d/0x200 mm/mempolicy.c:1486
do_syscall_x64 arch/x86/entry/common.c:51 [inline]
do_syscall_64+0x54/0xd0 arch/x86/entry/common.c:82
entry_SYSCALL_64_after_hwframe+0x44/0xae
RIP: 0033:0x7f4a41b2c709
Code: ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 40 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 bc ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007f4a3f0a3188 EFLAGS: 00000246 ORIG_RAX: 00000000000000ed
RAX: ffffffffffffffda RBX: 00007f4a41c30f60 RCX: 00007f4a41b2c709
RDX: 0000000000002001 RSI: 0000000000c00007 RDI: 0000000020012000
RBP: 00007f4a41b86cb4 R08: 0000000000000000 R09: 0000010000000002
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007f4a42164b2f R14: 00007f4a3f0a3300 R15: 0000000000022000

Fixes: bda420b98505 ("numa balancing: migrate on fault among multiple bound nodes")
Signed-off-by: Eric Dumazet <edum...@google.com>
Reported-by: syzbot <syzk...@googlegroups.com>
Cc: "Huang, Ying" <ying....@intel.com>
Cc: Mel Gorman <mgo...@suse.de>
---
mm/mempolicy.c | 16 +++++-----------
1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 1592b081c58ef6dd63c6f075ad24722f2be7cb5d..7a7abdbec7af66f88bb68cb4f47e9722d56bfd5b 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -856,16 +856,6 @@ static long do_set_mempolicy(unsigned short mode, unsigned short flags,
goto out;
}

- if (flags & MPOL_F_NUMA_BALANCING) {
- if (new && new->mode == MPOL_BIND) {
- new->flags |= (MPOL_F_MOF | MPOL_F_MORON);
- } else {
- ret = -EINVAL;
- mpol_put(new);
- goto out;
- }
- }
-
ret = mpol_set_nodemask(new, nodes, scratch);
if (ret) {
mpol_put(new);
@@ -1458,7 +1448,11 @@ static inline int sanitize_mpol_flags(int *mode, unsigned short *flags)
return -EINVAL;
if ((*flags & MPOL_F_STATIC_NODES) && (*flags & MPOL_F_RELATIVE_NODES))
return -EINVAL;
-
+ if (*flags & MPOL_F_NUMA_BALANCING) {
+ if (mode != MPOL_BIND)
+ return -EINVAL;
+ *flags |= (MPOL_F_MOF | MPOL_F_MORON);
+ }
return 0;
}

--
2.33.0.800.g4c38ced690-goog

Eric Dumazet

unread,
Oct 1, 2021, 5:56:35 PM10/1/21
to Andrew Morton, linux-kernel, linux-mm, Eric Dumazet, Eric Dumazet, syzbot, Huang, Ying, Mel Gorman
v2: fix compiler warning reported by kernel test robot <l...@intel.com>

mm/mempolicy.c | 16 +++++-----------
1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 1592b081c58ef6dd63c6f075ad24722f2be7cb5d..d12e0608fced235dc9137d0628437046299c7cfc 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -856,16 +856,6 @@ static long do_set_mempolicy(unsigned short mode, unsigned short flags,
goto out;
}

- if (flags & MPOL_F_NUMA_BALANCING) {
- if (new && new->mode == MPOL_BIND) {
- new->flags |= (MPOL_F_MOF | MPOL_F_MORON);
- } else {
- ret = -EINVAL;
- mpol_put(new);
- goto out;
- }
- }
-
ret = mpol_set_nodemask(new, nodes, scratch);
if (ret) {
mpol_put(new);
@@ -1458,7 +1448,11 @@ static inline int sanitize_mpol_flags(int *mode, unsigned short *flags)
return -EINVAL;
if ((*flags & MPOL_F_STATIC_NODES) && (*flags & MPOL_F_RELATIVE_NODES))
return -EINVAL;
-
+ if (*flags & MPOL_F_NUMA_BALANCING) {
+ if (*mode != MPOL_BIND)

kernel test robot

unread,
Oct 2, 2021, 3:36:31 AM10/2/21
to Eric Dumazet, Andrew Morton, kbuil...@lists.01.org, Linux Memory Management List, linux-kernel, Eric Dumazet, syzbot, Huang, Ying, Mel Gorman
Hi Eric,

I love your patch! Perhaps something to improve:

[auto build test WARNING on hnaz-mm/master]

url: https://github.com/0day-ci/linux/commits/Eric-Dumazet/mm-mempolicy-do-not-allow-illegal-MPOL_F_NUMA_BALANCING-MPOL_LOCAL-in-mbind/20211002-015054
base: https://github.com/hnaz/linux-mm master
config: ia64-defconfig (attached as .config)
compiler: ia64-linux-gcc (GCC) 11.2.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/fe87354835a4bf5cad6d885f0ba558c767fee6f1
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Eric-Dumazet/mm-mempolicy-do-not-allow-illegal-MPOL_F_NUMA_BALANCING-MPOL_LOCAL-in-mbind/20211002-015054
git checkout fe87354835a4bf5cad6d885f0ba558c767fee6f1
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=ia64 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <l...@intel.com>

All warnings (new ones prefixed by >>):

mm/mempolicy.c: In function 'sanitize_mpol_flags':
>> mm/mempolicy.c:1452:26: warning: comparison between pointer and integer
1452 | if (mode != MPOL_BIND)
| ^~


vim +1452 mm/mempolicy.c

1440
1441 /* Basic parameter sanity check used by both mbind() and set_mempolicy() */
1442 static inline int sanitize_mpol_flags(int *mode, unsigned short *flags)
1443 {
1444 *flags = *mode & MPOL_MODE_FLAGS;
1445 *mode &= ~MPOL_MODE_FLAGS;
1446
1447 if ((unsigned int)(*mode) >= MPOL_MAX)
1448 return -EINVAL;
1449 if ((*flags & MPOL_F_STATIC_NODES) && (*flags & MPOL_F_RELATIVE_NODES))
1450 return -EINVAL;
1451 if (*flags & MPOL_F_NUMA_BALANCING) {
> 1452 if (mode != MPOL_BIND)
1453 return -EINVAL;
1454 *flags |= (MPOL_F_MOF | MPOL_F_MORON);
1455 }
1456 return 0;
1457 }
1458

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuil...@lists.01.org
.config.gz

Eric Dumazet

unread,
Oct 2, 2021, 3:36:31 AM10/2/21
to kernel test robot, Eric Dumazet, Andrew Morton, kbuil...@lists.01.org, Linux Memory Management List, linux-kernel, syzbot, Huang, Ying, Mel Gorman
Oh well, last minute changes, and indeed my compiler warned me while I
was away :/

mm/mempolicy.c:1452:12: warning: comparison between pointer and
integer ('int *' and 'int') [-Wpointer-integer-compare]
if (mode != MPOL_BIND)
~~~~ ^ ~~~~~~~~~

I am sending a v2, thanks.

Andrew Morton

unread,
Oct 2, 2021, 3:36:31 AM10/2/21
to Eric Dumazet, linux-kernel, linux-mm, Eric Dumazet, syzbot, Huang, Ying, Mel Gorman
On Fri, 1 Oct 2021 14:56:30 -0700 Eric Dumazet <eric.d...@gmail.com> wrote:

> From: Eric Dumazet <edum...@google.com>
>
> syzbot reported access to unitialized memory in mbind() [1]

I'm lazy. What memory is being accessed-unintialized?

> Issue came with commit bda420b98505 ("numa balancing: migrate on
> fault among multiple bound nodes")

No cc:stable? What's the worst-case user-visible impact here?


Eric Dumazet

unread,
Oct 2, 2021, 3:36:43 AM10/2/21
to Andrew Morton, Eric Dumazet, linux-kernel, linux-mm, syzbot, Huang, Ying, Mel Gorman
On Fri, Oct 1, 2021 at 3:49 PM Andrew Morton <ak...@linux-foundation.org> wrote:
>
> On Fri, 1 Oct 2021 14:56:30 -0700 Eric Dumazet <eric.d...@gmail.com> wrote:
>
> > From: Eric Dumazet <edum...@google.com>
> >
> > syzbot reported access to unitialized memory in mbind() [1]
>
> I'm lazy. What memory is being accessed-unintialized?

I think you can clearly see that with this debug patch (courtesy of
Alexander Potapenko) :
(Then issue various mbind( ...MPOL_F_NUMA_BALANCING | MPOL_LOCAL ...)
in a loop... )


diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 1592b081c58e..95a4d71efe99 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -291,6 +291,7 @@ static struct mempolicy *mpol_new(unsigned short
mode, unsigned short flags,
} else if (nodes_empty(*nodes))
return ERR_PTR(-EINVAL);
policy = kmem_cache_alloc(policy_cache, GFP_KERNEL);
+ memset(policy, 0xAA, sizeof(struct mempolicy));
if (!policy)
return ERR_PTR(-ENOMEM);
atomic_set(&policy->refcnt, 1);
@@ -2256,9 +2257,12 @@ bool __mpol_equal(struct mempolicy *a, struct
mempolicy *b)
return false;
if (a->flags != b->flags)
return false;
- if (mpol_store_user_nodemask(a))
+ if (mpol_store_user_nodemask(a)) {
+ pr_err("struct mempolicy *a: %px, nodemask: %px\n", a,
*(void**)&(a->w.user_nodemask));
+ pr_err("struct mempolicy *b: %px, nodemask: %px\n", b,
*(void**)&(b->w.user_nodemask));
if (!nodes_equal(a->w.user_nodemask, b->w.user_nodemask))
return false;
+ }

switch (a->mode) {
case MPOL_BIND:


>
> > Issue came with commit bda420b98505 ("numa balancing: migrate on
> > fault among multiple bound nodes")
>
> No cc:stable? What's the worst-case user-visible impact here?

I added the more precise tag : Fixes: bda420b98505 ("numa balancing:
migrate on fault among multiple bound nodes")
I only put Fixes: tag, so that stable teams can use their automation just fine.

worst-case impact, I am not sure if any application ever used this
undocumented combinations of flags ?
Also, it is generally advised that accessing garbage values has
undocumented behavior.
A host could for example crash (it certainly does with KMSAN)

Matthew Wilcox

unread,
Oct 3, 2021, 3:12:07 AM10/3/21
to Eric Dumazet, Andrew Morton, Eric Dumazet, linux-kernel, linux-mm, syzbot, Huang, Ying, Mel Gorman
On Fri, Oct 01, 2021 at 04:37:40PM -0700, Eric Dumazet wrote:
> > > Issue came with commit bda420b98505 ("numa balancing: migrate on
> > > fault among multiple bound nodes")
> >
> > No cc:stable? What's the worst-case user-visible impact here?
>
> I added the more precise tag : Fixes: bda420b98505 ("numa balancing:
> migrate on fault among multiple bound nodes")
> I only put Fixes: tag, so that stable teams can use their automation just fine.
>
> worst-case impact, I am not sure if any application ever used this
> undocumented combinations of flags ?
> Also, it is generally advised that accessing garbage values has
> undocumented behavior.
> A host could for example crash (it certainly does with KMSAN)

mm has special stable rules; fixes only get backported if explicitly
requested instead of automatically like most of the rest of the kernel.

Huang, Ying

unread,
Oct 7, 2021, 11:17:54 PM10/7/21
to Eric Dumazet, Andrew Morton, linux-kernel, linux-mm, Eric Dumazet, syzbot, Mel Gorman
Hi, Eric,

Eric Dumazet <eric.d...@gmail.com> writes:

> From: Eric Dumazet <edum...@google.com>
>
> syzbot reported access to unitialized memory in mbind() [1]
>
> Issue came with commit bda420b98505 ("numa balancing: migrate on
> fault among multiple bound nodes")
>
> This commit added a new bit in MPOL_MODE_FLAGS, but only checked
> valid combination (MPOL_F_NUMA_BALANCING can only be used with MPOL_BIND)
> in do_set_mempolicy()
>
> This patch moves the check in sanitize_mpol_flags() so that it
> is also used by mbind()

Good catch! Thanks! When MPOL_F_NUMA_BALANCING is introduced, it is
intended to be used with set_memopolicy() syscall only, it is not
allowed to be used with mbind() syscall at least for now. But I
misunderstood the original code apparently.

So I think it may be better to return EINVAL for mbind() +
MPOL_F_NUMA_BALANCING?

Best Regards,
Huang, Ying

Dongliang Mu

unread,
Oct 8, 2021, 1:26:05 AM10/8/21
to Eric Dumazet, Andrew Morton, linux-kernel, linux-mm, Eric Dumazet, syzbot, Huang, Ying, Mel Gorman
Hi Eric,

this crash seems like the bug report [1] on Syzbot dashboard.

[1] https://syzkaller.appspot.com/bug?id=028833194204bb15c2c43d822b8f7401360d57f5

If yes, maybe you can push a patch testing request.
> --
> You received this message because you are subscribed to the Google Groups "syzkaller" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to syzkaller+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/syzkaller/20211001174947.135123-1-eric.dumazet%40gmail.com.

Eric Dumazet

unread,
Oct 8, 2021, 12:20:56 PM10/8/21
to Dongliang Mu, Eric Dumazet, Andrew Morton, linux-kernel, linux-mm, syzbot, Huang, Ying, Mel Gorman
No, it was a different syzbot report, currently held in our queue.

(Andrew Morton has access to our internal bug, I CC him (b/201266830))

I can release it, at the expense of more noise to various mailing
lists, but really the bug is trivial.

Mel Gorman

unread,
Oct 11, 2021, 6:23:22 AM10/11/21
to Eric Dumazet, Andrew Morton, linux-kernel, linux-mm, Eric Dumazet, syzbot, Huang, Ying
Acked-by: Mel Gorman <mgo...@suse.de>

--
Mel Gorman
SUSE Labs

Andrew Morton

unread,
Oct 18, 2021, 6:15:51 PM10/18/21
to ak...@linux-foundation.org, edum...@google.com, linu...@kvack.org, mgo...@suse.de, mm-co...@vger.kernel.org, sta...@vger.kernel.org, syzk...@googlegroups.com, torv...@linux-foundation.org, wi...@infradead.org, ying....@intel.com
From: Eric Dumazet <edum...@google.com>
Subject: mm/mempolicy: do not allow illegal MPOL_F_NUMA_BALANCING | MPOL_LOCAL in mbind()
Link: https://lkml.kernel.org/r/20211001215630.810...@gmail.com
Fixes: bda420b98505 ("numa balancing: migrate on fault among multiple bound nodes")
Signed-off-by: Eric Dumazet <edum...@google.com>
Reported-by: syzbot <syzk...@googlegroups.com>
Acked-by: Mel Gorman <mgo...@suse.de>
Cc: "Huang, Ying" <ying....@intel.com>
Cc: Matthew Wilcox <wi...@infradead.org>
Cc: <sta...@vger.kernel.org>
Signed-off-by: Andrew Morton <ak...@linux-foundation.org>
---

mm/mempolicy.c | 16 +++++-----------
1 file changed, 5 insertions(+), 11 deletions(-)

--- a/mm/mempolicy.c~mm-mempolicy-do-not-allow-illegal-mpol_f_numa_balancing-mpol_local-in-mbind
+++ a/mm/mempolicy.c
@@ -856,16 +856,6 @@ static long do_set_mempolicy(unsigned sh
goto out;
}

- if (flags & MPOL_F_NUMA_BALANCING) {
- if (new && new->mode == MPOL_BIND) {
- new->flags |= (MPOL_F_MOF | MPOL_F_MORON);
- } else {
- ret = -EINVAL;
- mpol_put(new);
- goto out;
- }
- }
-
ret = mpol_set_nodemask(new, nodes, scratch);
if (ret) {
mpol_put(new);
@@ -1458,7 +1448,11 @@ static inline int sanitize_mpol_flags(in
return -EINVAL;
if ((*flags & MPOL_F_STATIC_NODES) && (*flags & MPOL_F_RELATIVE_NODES))
return -EINVAL;
-
+ if (*flags & MPOL_F_NUMA_BALANCING) {
+ if (*mode != MPOL_BIND)
+ return -EINVAL;
+ *flags |= (MPOL_F_MOF | MPOL_F_MORON);
+ }
return 0;
}

_

Greg Kroah-Hartman

unread,
Oct 25, 2021, 3:41:58 PM10/25/21
to linux-...@vger.kernel.org, Greg Kroah-Hartman, sta...@vger.kernel.org, Eric Dumazet, syzbot, Mel Gorman, Huang, Ying, Matthew Wilcox, Andrew Morton, Linus Torvalds
From: Eric Dumazet <edum...@google.com>

commit 6d2aec9e123bb9c49cb5c7fc654f25f81e688e8c upstream.
Link: https://lkml.kernel.org/r/20211001215630.810...@gmail.com
Fixes: bda420b98505 ("numa balancing: migrate on fault among multiple bound nodes")
Signed-off-by: Eric Dumazet <edum...@google.com>
Reported-by: syzbot <syzk...@googlegroups.com>
Acked-by: Mel Gorman <mgo...@suse.de>
Cc: "Huang, Ying" <ying....@intel.com>
Cc: Matthew Wilcox <wi...@infradead.org>
Cc: <sta...@vger.kernel.org>
Signed-off-by: Andrew Morton <ak...@linux-foundation.org>
Signed-off-by: Linus Torvalds <torv...@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gre...@linuxfoundation.org>
---
mm/mempolicy.c | 16 +++++-----------
1 file changed, 5 insertions(+), 11 deletions(-)

--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -857,16 +857,6 @@ static long do_set_mempolicy(unsigned sh
goto out;
}

- if (flags & MPOL_F_NUMA_BALANCING) {
- if (new && new->mode == MPOL_BIND) {
- new->flags |= (MPOL_F_MOF | MPOL_F_MORON);
- } else {
- ret = -EINVAL;
- mpol_put(new);
- goto out;
- }
- }
-
ret = mpol_set_nodemask(new, nodes, scratch);
if (ret) {
mpol_put(new);
@@ -1450,7 +1440,11 @@ static inline int sanitize_mpol_flags(in
Reply all
Reply to author
Forward
0 new messages