From 81a3dc3655d097611c9091af6934ed47a6d96fd8 Mon Sep 17 00:00:00 2001 From: Liu Yuntao Date: Tue, 8 Mar 2022 15:23:39 +0800 Subject: [PATCH] hugetlbfs: fix a truncation issue in hugepages parameter mainline inclusion from mainline-v5.17-rc6 commit e79ce9832316e09529b212a21278d68240ccbf1f category: bugfix bugzilla: 186043 CVE: NA ------------------------------------------------- When we specify a large number for node in hugepages parameter, it may be parsed to another number due to truncation in this statement: node = tmp; For example, add following parameter in command line: hugepagesz=1G hugepages=4294967297:5 and kernel will allocate 5 hugepages for node 1 instead of ignoring it. I move the validation check earlier to fix this issue, and slightly simplifies the condition here. Link: https://lkml.kernel.org/r/20220209134018.8242-1-liuyuntao10@huawei.com Fixes: b5389086ad7be0 ("hugetlbfs: extend the definition of hugepages parameter to support node allocation") Signed-off-by: Liu Yuntao Reviewed-by: Mike Kravetz Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Liu Shixin Reviewed-by: Kefeng Wang Signed-off-by: Yang Yingliang --- mm/hugetlb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index f7e41390f3d8..68cec97bbd06 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3151,10 +3151,10 @@ static int __init hugetlb_nrpages_setup(char *s) pr_warn("HugeTLB: architecture can't support node specific alloc, ignoring!\n"); return 0; } + if (tmp >= nr_online_nodes) + goto invalid; node = tmp; p += count + 1; - if (node < 0 || node >= nr_online_nodes) - goto invalid; /* Parse hugepages */ if (sscanf(p, "%lu%n", &tmp, &count) != 1) goto invalid; -- GitLab