[X/B/D/E/F] mm: mempolicy: require at least one nodeid for MPOL_PREFERRED

Thadeu Lima de Souza Cascardo cascardo at canonical.com
Thu Apr 16 20:01:12 UTC 2020

From: Randy Dunlap <rdunlap at infradead.org>

Using an empty (malformed) nodelist that is not caught during mount option
parsing leads to a stack-out-of-bounds access.

The option string that was used was: "mpol=prefer:,".  However,
MPOL_PREFERRED requires a single node number, which is not being provided

Add a check that 'nodes' is not empty after parsing for MPOL_PREFERRED's

Fixes: 095f1fc4ebf3 ("mempolicy: rework shmem mpol parsing and display")
Reported-by: Entropy Moe <3ntr0py1337 at gmail.com>
Reported-by: syzbot+b055b1a6b2b958707a21 at syzkaller.appspotmail.com
Signed-off-by: Randy Dunlap <rdunlap at infradead.org>
Signed-off-by: Andrew Morton <akpm at linux-foundation.org>
Tested-by: syzbot+b055b1a6b2b958707a21 at syzkaller.appspotmail.com
Cc: Lee Schermerhorn <lee.schermerhorn at hp.com>
Link: http://lkml.kernel.org/r/89526377-7eb6-b662-e1d8-4430928abde9@infradead.org
Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
(cherry picked from commit aa9f7d5172fac9bf1f09e678c35e287a40a7b7dd)
Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo at canonical.com>
 mm/mempolicy.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index fbb3258af275..787c5fc91b21 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -2822,7 +2822,9 @@ int mpol_parse_str(char *str, struct mempolicy **mpol)
 	switch (mode) {
-		 * Insist on a nodelist of one node only
+		 * Insist on a nodelist of one node only, although later
+		 * we use first_node(nodes) to grab a single node, so here
+		 * nodelist (or nodes) cannot be empty.
 		if (nodelist) {
 			char *rest = nodelist;
@@ -2830,6 +2832,8 @@ int mpol_parse_str(char *str, struct mempolicy **mpol)
 			if (*rest)
 				goto out;
+			if (nodes_empty(nodes))
+				goto out;

More information about the kernel-team mailing list