reboot: validate reboot CPU against the possible mask
PATCH v2 1/1 reboot: validate reboot CPU against the possible mask
reboot_setup() and cpu_store() compare CPU IDs with num_possible_cpus(), which is a count rather than an upper bound. With a sparse possible mask, this rejects valid CPUs and accepts impossible IDs. reboot_setup() also narrows the parsed CPU number to int before checking it, allowing large values to be truncated.
Validate against nr_cpu_ids and cpu_possible() in both interfaces. Use simple_strtoull() and unsigned long long for the boot option, so the value is not narrowed before validation on 32- or 64-bit kernels. Preserve the comma-separated reboot= syntax.
---
v2:
- apply Bradley Morgan's unsigned parsing review, widening to 64 bits for consistent behavior on 32- and 64-bit kernels
v1: https://lore.kernel.org/r/[email protected]
kernel/reboot.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
kernel/reboot.c +5 -6
@@ -1144,12 +1144,11 @@ static int __init reboot_setup(char *str) str += str[1 ] =='m' && str[2 ] =='p' ?3 :1 ;if (isdigit (str[0 ])) { -int cpu =simple_strtoul (str,NULL ,0 ); +unsigned long long cpu =simple_strtoull (str,NULL ,0 ); -if (cpu >=num_possible_cpus ()) { -pr_err ("Ignoring the CPU number in reboot= option. " -"CPU %d exceeds possible cpu number %d\n" , - cpu,num_possible_cpus ()); +if (cpu >= nr_cpu_ids || !cpu_possible (cpu)) { +pr_err ("Ignoring invalid CPU %llu in reboot= option\n" , + cpu);break ; } reboot_cpu = cpu;
@@ -1351,7 +1350,7 @@ static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr,if (rc)return rc; -if (cpunum >=num_possible_cpus ()) +if (cpunum >= nr_cpu_ids || !cpu_possible (cpunum))return -ERANGE ; reboot_default =0 ;
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
Quoted 12 lines · Avi Weiss
reboot_setup()andcpu_store()compare CPU IDs withnum_possible_cpus(), which is a count rather than an upper bound. With a sparse possible mask, this rejects valid CPUs and accepts impossible IDs.reboot_setup()also narrows the parsed CPU number to int before checking it, allowing large values to be truncated.Validate against
nr_cpu_idsandcpu_possible()in both interfaces. Usesimple_strtoull()and unsigned long long for the boot option, so the value is not narrowed before validation on 32- or 64-bit kernels. Preserve the comma-separated reboot= syntax.
Quoted 7 lines
Fixes df5b0ab3e08a ("reboot: fix overflow parsing reboot cpu number")Fixes 2c622ed0eaa3 ("reboot: allow to specify reboot mode via sysfs")Reported-by Bradley Morgan <[email protected]>Suggested-by Bradley Morgan <[email protected]>Signed-off-by Avi Weiss <[email protected]>
How did you account for this?
https://lore.kernel.org/all/[email protected]/
Quoted 42 lines
---
v2:
- apply Bradley Morgan's unsigned parsing review, widening to 64 bits for consistent behavior on 32- and 64-bit kernelsv1: https://lore.kernel.org/r/[email protected]
kernel/reboot.c| 11 +++++------1 file changed, 5 insertions(+), 6 deletions(-)kernel/reboot.c +5
-6@@ -1144,12 +1144,11 @@ static int __init reboot_setup(char *str) str += str[1 ] =='m' && str[2 ] =='p' ?3 :1 ;if (isdigit (str[0 ])) { -int cpu =simple_strtoul (str,NULL ,0 ); +unsigned long long cpu =simple_strtoull (str,NULL ,0 ); -if (cpu >=num_possible_cpus ()) { -pr_err ("Ignoring the CPU number in reboot= option. " -"CPU %d exceeds possible cpu number %d\n" , - cpu,num_possible_cpus ()); +if (cpu >= nr_cpu_ids || !cpu_possible (cpu)) { +pr_err ("Ignoring invalid CPU %llu in reboot= option\n" , + cpu);break ; } reboot_cpu = cpu;@@ -1351,7 +1350,7 @@ static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr,if (rc)return rc; -if (cpunum >=num_possible_cpus ()) +if (cpunum >= nr_cpu_ids || !cpu_possible (cpunum))return -ERANGE ; reboot_default =0 ;
Quoted 2 lines
base-commit:
b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
--- Thanks!
Bradley Morgan
Reviewed-byQuoted 19 lines · Bradley Morgan
Quoted 12 lines · Avi Weiss
reboot_setup()andcpu_store()compare CPU IDs withnum_possible_cpus(), which is a count rather than an upper bound. With a sparse possible mask, this rejects valid CPUs and accepts impossible IDs.reboot_setup()also narrows the parsed CPU number to int before checking it, allowing large values to be truncated.Validate against
nr_cpu_idsandcpu_possible()in both interfaces. Usesimple_strtoull()and unsigned long long for the boot option, so the value is not narrowed before validation on 32- or 64-bit kernels. Preserve the comma-separated reboot= syntax.Quoted 6 lines
Fixes df5b0ab3e08a ("reboot: fix overflow parsing reboot cpu number")Fixes 2c622ed0eaa3 ("reboot: allow to specify reboot mode via sysfs")Reported-by Bradley Morgan <[email protected]>Suggested-by Bradley Morgan <[email protected]>Link:
Quoted 6 lines
https://lore.kernel.org/r/[email protected]
Quoted 1 line
Signed-off-by Avi Weiss <[email protected]>How did you account for this?
ah crap, realised kstrtoul() chokes on the ,... tail. stupid mistake from me.
yeah, ok
few questions, won't affect tag.
Quoted 20 lines
Quoted 8 lines
---
v2:
- apply Bradley Morgan's unsigned parsing review, widening to 64 bits for consistent behavior on 32- and 64-bit kernelsv1:
https://lore.kernel.org/r/[email protected]
Quoted 11 lines
kernel/reboot.c| 11 +++++------1 file changed, 5 insertions(+), 6 deletions(-)kernel/reboot.c +0
-0@@ -1144,12 +1144,11 @@ static int __init reboot_setup(char *str) str += str[1 ] =='m' && str[2 ] =='p' ?3 :1 ;
Quoted 3 lines
kernel/reboot.c static int __init reboot_setup(char *str) · line 1147
Quoted 3 lines
kernel/reboot.c +1
-1if (isdigit (str[0 ])) { -int cpu =simple_strtoul (str,NULL ,0 ); +unsigned long long cpu =simple_strtoull (str,NULL ,0 );
why this? I swear I suggested leaving it the same except from the unsigned long.
Quoted 7 lines
kernel/reboot.c static int __init reboot_setup(char *str) · line 1150
Quoted 7 lines
kernel/reboot.c +2
-4-if (cpu >=num_possible_cpus ()) { -pr_err ("Ignoring the CPU number in reboot= option. " -"CPU %d exceeds possible cpu number %d\n" , - cpu,num_possible_cpus ()); +if (cpu >= nr_cpu_ids || !cpu_possible (cpu)) { + pr_err("Ignoring invalid CPU %llu in reboot= option\n",
i feel a follow up is clarifying this better. maybe a why?
Quoted 11 lines
kernel/reboot.c static ssize_t cpu_store(struct kobject *kobj,
Quoted 5 lines
kernel/reboot.c +1
-0+ cpu);break ; } reboot_cpu = cpu;@@ -1351,7 +1350,7 @@ static ssize_t cpu_store(struct kobject *kobj,struct
kobj_attribute*attr,kernel/reboot.c static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr, · line 1353
Quoted 5 lines
kernel/reboot.c +1
-1if (rc)return rc; -if (cpunum >=num_possible_cpus ()) +if (cpunum >= nr_cpu_ids || !cpu_possible (cpunum))
Ack.
Quoted 8 lines
Quoted 6 lines
kernel/reboot.c +0
-0return -ERANGE ; reboot_default =0 ;base-commit:
b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f--- Thanks!
--- Thanks!
Andrew Morton
tree notice+ reboot-validate-reboot-cpu-against-the-possible-mask.patch added to mm-nonmm-unstable branch
In reply to Avi WeissThe patch titled Subject: reboot: validate reboot CPU against the possible mask has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: reboot: validate reboot CPU against the possible mask
has been added to the -mm mm-nonmm-unstable branch. Its filename is
reboot-validate-reboot-cpu-against-the-possible-mask.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/reboot-validate-reboot-cpu-against-the-possible-mask.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Avi Weiss <[email protected]>
Subject: reboot: validate reboot CPU against the possible mask
Date: Tue, 24 Nov 2026 10:29:14 +0200
reboot_setup() and cpu_store() compare CPU IDs with num_possible_cpus(), which is a count rather than an upper bound. With a sparse possible mask, this rejects valid CPUs and accepts impossible IDs. reboot_setup() also narrows the parsed CPU number to int before checking it, allowing large values to be truncated.
Validate against nr_cpu_ids and cpu_possible() in both interfaces. Use simple_strtoull() and unsigned long long for the boot option, so the value is not narrowed before validation on 32- or 64-bit kernels. Preserve the comma-separated reboot= syntax.
kernel/reboot.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
kernel/reboot.c renamed from kernel/reboot.c~reboot-validate-reboot-cpu-against-the-possible-mask +5 -6
@@ -1147,12 +1147,11 @@ static int __init reboot_setup(char *str str += str[1 ] =='m' && str[2 ] =='p' ?3 :1 ;if (isdigit (str[0 ])) { -int cpu =simple_strtoul (str,NULL ,0 ); +unsigned long long cpu =simple_strtoull (str,NULL ,0 ); -if (cpu >=num_possible_cpus ()) { -pr_err ("Ignoring the CPU number in reboot= option. " -"CPU %d exceeds possible cpu number %d\n" , - cpu,num_possible_cpus ()); +if (cpu >= nr_cpu_ids || !cpu_possible (cpu)) { +pr_err ("Ignoring invalid CPU %llu in reboot= option\n" , + cpu);break ; } reboot_cpu = cpu;
@@ -1354,7 +1353,7 @@ static ssize_t cpu_store(struct kobjectif (rc)return rc; -if (cpunum >=num_possible_cpus ()) +if (cpunum >= nr_cpu_ids || !cpu_possible (cpunum))return -ERANGE ; reboot_default =0 ;
_
Patches currently in -mm which might be from [email protected] are
mm-page-writeback-avoid-unnecessary-strictlimit-background-wakeups.patch reboot-validate-reboot-cpu-against-the-possible-mask.patch
Quoted 11 lines · On Tue, 24 Nov 2026 10:29:14 +0200 Avi Weiss <[email protected]> wrote:
reboot_setup()andcpu_store()compare CPU IDs withnum_possible_cpus(), which is a count rather than an upper bound. With a sparse possible mask, this rejects valid CPUs and accepts impossible IDs.reboot_setup()also narrows the parsed CPU number to int before checking it, allowing large values to be truncated.Validate against
nr_cpu_idsandcpu_possible()in both interfaces. Usesimple_strtoull()and unsigned long long for the boot option, so the value is not narrowed before validation on 32- or 64-bit kernels. Preserve the comma-separated reboot= syntax.
So we're supporting num_possible_cpus>4G? That's future-safety!
kernel/reboot.c static int __init reboot_setup(char *str)
Quoted 5 lines
kernel/reboot.c +0
-0@@ -1144,12 +1144,11 @@ static int __init reboot_setup(char *str)
but it's __init so who cares.
Quoted 17 lines
kernel/reboot.c +4
-5str += str[1 ] =='m' && str[2 ] =='p' ?3 :1 ;if (isdigit (str[0 ])) { -int cpu =simple_strtoul (str,NULL ,0 ); +unsigned long long cpu =simple_strtoull (str,NULL ,0 ); -if (cpu >=num_possible_cpus ()) { -pr_err ("Ignoring the CPU number in reboot= option. " -"CPU %d exceeds possible cpu number %d\n" , - cpu,num_possible_cpus ()); +if (cpu >= nr_cpu_ids || !cpu_possible (cpu)) { +pr_err ("Ignoring invalid CPU %llu in reboot= option\n" , + cpu);break ; } reboot_cpu = cpu;
kernel/reboot.c static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr, · line 1354
Quoted 6 lines
kernel/reboot.c +1
-1@@ -1351,7 +1350,7 @@ static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr,if (rc)return rc; -if (cpunum >=num_possible_cpus ()) +if (cpunum >= nr_cpu_ids || !cpu_possible (cpunum))return -ERANGE ;
lgtm, thanks.