All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] reboot: validate reboot CPU against the possible mask
@ 2026-11-24  8:29 Avi Weiss
  2026-11-24 16:11 ` Bradley Morgan
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Avi Weiss @ 2026-11-24  8:29 UTC (permalink / raw)
  To: akpm; +Cc: mcroce, pmladek, brads, linux-kernel


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")
Reported-by: Bradley Morgan <include@grrlz.net>
Link: https://lore.kernel.org/r/20260622154216.10064-1-include@grrlz.net
Suggested-by: Bradley Morgan <brads@mainlining.org>
Link: https://lore.kernel.org/r/D92156F5-5924-45CD-BCFE-AF385BDD15CF@mainlining.org
Signed-off-by: Avi Weiss <thnkslprpt@gmail.com>
---

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/20261031060602.2949895-2-thnkslprpt@gmail.com

 kernel/reboot.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/kernel/reboot.c b/kernel/reboot.c
index 9a84401adde5..f57b9aaef3f0 100644
--- a/kernel/reboot.c
+++ b/kernel/reboot.c
@@ -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

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] reboot: validate reboot CPU against the possible mask
  2026-11-24  8:29 [PATCH v2] reboot: validate reboot CPU against the possible mask Avi Weiss
@ 2026-11-24 16:11 ` Bradley Morgan
  2026-11-24 17:46   ` Bradley Morgan
  2026-11-24 22:56 ` + reboot-validate-reboot-cpu-against-the-possible-mask.patch added to mm-nonmm-unstable branch Andrew Morton
  2026-11-24 22:58 ` [PATCH v2] reboot: validate reboot CPU against the possible mask Andrew Morton
  2 siblings, 1 reply; 5+ messages in thread
From: Bradley Morgan @ 2026-11-24 16:11 UTC (permalink / raw)
  To: Avi Weiss, akpm; +Cc: mcroce, pmladek, linux-kernel

On 24 November 2026 08:29:14 GMT, Avi Weiss <thnkslprpt@gmail.com> 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.
>
>Fixes: df5b0ab3e08a ("reboot: fix overflow parsing reboot cpu number")
>Fixes: 2c622ed0eaa3 ("reboot: allow to specify reboot mode via sysfs")
>Reported-by: Bradley Morgan <include@grrlz.net>
>Link: https://lore.kernel.org/r/20260622154216.10064-1-include@grrlz.net
>Suggested-by: Bradley Morgan <brads@mainlining.org>
>Link: https://lore.kernel.org/r/D92156F5-5924-45CD-BCFE-AF385BDD15CF@mainlining.org
>Signed-off-by: Avi Weiss <thnkslprpt@gmail.com>

How did you account for this?

https://lore.kernel.org/all/20260624174642.da2cc6912a9b19accd378538@linux-foundation.org/

>---
>
>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/20261031060602.2949895-2-thnkslprpt@gmail.com
>
> kernel/reboot.c | 11 +++++------
> 1 file changed, 5 insertions(+), 6 deletions(-)
>
>diff --git a/kernel/reboot.c b/kernel/reboot.c
>index 9a84401adde5..f57b9aaef3f0 100644
>--- a/kernel/reboot.c
>+++ b/kernel/reboot.c
>@@ -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
>

--- Thanks!

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] reboot: validate reboot CPU against the possible mask
  2026-11-24 16:11 ` Bradley Morgan
@ 2026-11-24 17:46   ` Bradley Morgan
  0 siblings, 0 replies; 5+ messages in thread
From: Bradley Morgan @ 2026-11-24 17:46 UTC (permalink / raw)
  To: Avi Weiss, akpm; +Cc: mcroce, pmladek, linux-kernel

On 24 November 2026 16:11:16 GMT, Bradley Morgan <bradley.morgan@linux.dev>
wrote:
>On 24 November 2026 08:29:14 GMT, Avi Weiss <thnkslprpt@gmail.com> 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.
>>
>>Fixes: df5b0ab3e08a ("reboot: fix overflow parsing reboot cpu number")
>>Fixes: 2c622ed0eaa3 ("reboot: allow to specify reboot mode via sysfs")
>>Reported-by: Bradley Morgan <include@grrlz.net>
>>Link: https://lore.kernel.org/r/20260622154216.10064-1-include@grrlz.net
>>Suggested-by: Bradley Morgan <brads@mainlining.org>
>>Link:
>https://lore.kernel.org/r/D92156F5-5924-45CD-BCFE-AF385BDD15CF@mainlining.org
>>Signed-off-by: Avi Weiss <thnkslprpt@gmail.com>
>
>How did you account for this?
>
>https://lore.kernel.org/all/20260624174642.da2cc6912a9b19accd378538@linux-foundation.org/

ah crap, realised kstrtoul() chokes on the ,... tail. stupid mistake from
me.

yeah, ok


Reviewed-by: Bradley Morgan <bradley.morgan@linux.dev>

few questions, won't affect tag.

>
>>---
>>
>>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/20261031060602.2949895-2-thnkslprpt@gmail.com
>>
>> kernel/reboot.c | 11 +++++------
>> 1 file changed, 5 insertions(+), 6 deletions(-)
>>
>>diff --git a/kernel/reboot.c b/kernel/reboot.c
>>index 9a84401adde5..f57b9aaef3f0 100644
>>--- a/kernel/reboot.c
>>+++ b/kernel/reboot.c
>>@@ -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);

why this? I swear I suggested leaving it the same except from the unsigned
long.

>> 
>>-				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?

>>+					       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))

Ack.

>> 		return -ERANGE;
>> 
>> 	reboot_default = 0;
>>
>>base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
>>
>
>--- Thanks!

--- Thanks!

^ permalink raw reply	[flat|nested] 5+ messages in thread

* + reboot-validate-reboot-cpu-against-the-possible-mask.patch added to mm-nonmm-unstable branch
  2026-11-24  8:29 [PATCH v2] reboot: validate reboot CPU against the possible mask Avi Weiss
  2026-11-24 16:11 ` Bradley Morgan
@ 2026-11-24 22:56 ` Andrew Morton
  2026-11-24 22:58 ` [PATCH v2] reboot: validate reboot CPU against the possible mask Andrew Morton
  2 siblings, 0 replies; 5+ messages in thread
From: Andrew Morton @ 2026-11-24 22:56 UTC (permalink / raw)
  To: mm-commits, pmladek, mcroce, bradley.morgan, thnkslprpt, akpm


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 <thnkslprpt@gmail.com>
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.

Link: https://patch.msgid.link/20261124082914.564587-1-thnkslprpt@gmail.com
Fixes: df5b0ab3e08a ("reboot: fix overflow parsing reboot cpu number")
Fixes: 2c622ed0eaa3 ("reboot: allow to specify reboot mode via sysfs")
Signed-off-by: Avi Weiss <thnkslprpt@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reported-by: Bradley Morgan <bradley.morgan@linux.dev>
Closes: https://lore.kernel.org/r/20260622154216.10064-1-include@grrlz.net
Suggested-by: Bradley Morgan <bradley.morgan@linux.dev>
Link: https://lore.kernel.org/r/D92156F5-5924-45CD-BCFE-AF385BDD15CF@mainlining.org
Reviewed-by: Bradley Morgan <bradley.morgan@linux.dev>
Cc: Matteo Croce <mcroce@microsoft.com>
Cc: Petr Mladek <pmladek@suse.com>

 kernel/reboot.c |   11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

--- a/kernel/reboot.c~reboot-validate-reboot-cpu-against-the-possible-mask
+++ a/kernel/reboot.c
@@ -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 thnkslprpt@gmail.com are

mm-page-writeback-avoid-unnecessary-strictlimit-background-wakeups.patch
reboot-validate-reboot-cpu-against-the-possible-mask.patch


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] reboot: validate reboot CPU against the possible mask
  2026-11-24  8:29 [PATCH v2] reboot: validate reboot CPU against the possible mask Avi Weiss
  2026-11-24 16:11 ` Bradley Morgan
  2026-11-24 22:56 ` + reboot-validate-reboot-cpu-against-the-possible-mask.patch added to mm-nonmm-unstable branch Andrew Morton
@ 2026-11-24 22:58 ` Andrew Morton
  2 siblings, 0 replies; 5+ messages in thread
From: Andrew Morton @ 2026-11-24 22:58 UTC (permalink / raw)
  To: Avi Weiss; +Cc: mcroce, pmladek, brads, linux-kernel

On Tue, 24 Nov 2026 10:29:14 +0200 Avi Weiss <thnkslprpt@gmail.com> 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!

> diff --git a/kernel/reboot.c b/kernel/reboot.c
> index 9a84401adde5..f57b9aaef3f0 100644
> --- a/kernel/reboot.c
> +++ b/kernel/reboot.c
> @@ -1144,12 +1144,11 @@ static int __init reboot_setup(char *str)

but it's __init so who cares.

>  			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;

lgtm, thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-24 22:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24  8:29 [PATCH v2] reboot: validate reboot CPU against the possible mask Avi Weiss
2026-11-24 16:11 ` Bradley Morgan
2026-11-24 17:46   ` Bradley Morgan
2026-11-24 22:56 ` + reboot-validate-reboot-cpu-against-the-possible-mask.patch added to mm-nonmm-unstable branch Andrew Morton
2026-11-24 22:58 ` [PATCH v2] reboot: validate reboot CPU against the possible mask Andrew Morton

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.