Skip to contentSkip to conversation

reboot: validate reboot CPU against the possible mask

PATCH In review Avi Weiss 3 participants 5 messages Nov 24

lore
Share & tools
lore

Avi Weiss

author
#0 lore raw

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.

Fixes df5b0ab3e08a ("reboot: fix overflow parsing reboot cpu number")
Fixes 2c622ed0eaa3 ("reboot: allow to specify reboot mode via sysfs")

---

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
index 9a84401adde5..f57b9aaef3f0 100644
@@ -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

#1 lore raw
Quoted 12 lines · Avi Weiss

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.

Quoted 7 lines
Fixes df5b0ab3e08a ("reboot: fix overflow parsing reboot cpu number")
Fixes 2c622ed0eaa3 ("reboot: allow to specify reboot mode via sysfs")

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 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
index 9a84401adde5..f57b9aaef3f0 100644
@@ -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!

Reviewed-by
#2 lore raw
Quoted 19 lines · Bradley Morgan
Quoted 12 lines · Avi Weiss

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.

Quoted 6 lines
Fixes df5b0ab3e08a ("reboot: fix overflow parsing reboot cpu number")
Fixes 2c622ed0eaa3 ("reboot: allow to specify reboot mode via sysfs")

Link:

Quoted 6 lines

https://lore.kernel.org/r/[email protected]

Quoted 1 line

How did you account for this?

https://lore.kernel.org/all/[email protected]/

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 kernels

v1:

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
index 9a84401adde5..f57b9aaef3f0 100644
@@ -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 -1
 			if (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 -1
 	if (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 -0
 		return -ERANGE;
 
 	reboot_default = 0;

base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f

--- Thanks!

--- Thanks!

tree notice
#3 lore raw

+ reboot-validate-reboot-cpu-against-the-possible-mask.patch added to mm-nonmm-unstable branch

In reply to Avi Weiss
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…

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.

Fixes df5b0ab3e08a ("reboot: fix overflow parsing reboot cpu number")
Fixes 2c622ed0eaa3 ("reboot: allow to specify reboot mode via sysfs")
Cc Matteo Croce <[email protected]>
Cc Petr Mladek <[email protected]>
 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 kobject
 	if (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

#4 lore raw
In reply to Avi Weiss
Quoted 11 lines · On Tue, 24 Nov 2026 10:29:14 +0200 Avi Weiss <[email protected]> wrote:

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.

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
index 9a84401adde5..f57b9aaef3f0 100644
@@ -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 -5
 			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;

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.