Skip to contentSkip to conversation

reboot: validate reboot CPU against the possible mask

PATCH In review Avi Weiss 2 participants 3 messages Oct 31 1 message not in the archive

lore
Share & tools
lore

Avi Weiss

author
#0 lore raw

PATCH 1/1 reboot: validate reboot CPU against the possible mask

reboot_setup() and cpu_store() compare the requested CPU ID against num_possible_cpus(), which is a count rather than the highest possible CPU ID. On sparse masks, this rejects valid high-numbered CPUs and accepts impossible CPU IDs.

Validate the CPU ID against nr_cpu_ids and cpu_possible() instead. Reject negative parsed values before the mask lookup and update the error message to avoid assuming contiguous CPU numbering.

On an arm64 QEMU guest with possible CPUs 0,2-3, reboot=s3 selected CPU 0 and sysfs rejected CPU 3 with -ERANGE. With the fix, reboot=s3 selects CPU 3 and sysfs accepts it, while both interfaces reject impossible CPU 1.

Fixes df5b0ab3e08a ("reboot: fix overflow parsing reboot cpu number")
Fixes 2c622ed0eaa3 ("reboot: allow to specify reboot mode via sysfs")
---
 kernel/reboot.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
kernel/reboot.c +4 -5
index 9a84401adde5..7b09116376e3 100644
@@ -1146,10 +1146,9 @@ static int __init reboot_setup(char *str)
 			if (isdigit(str[0])) {
 				int cpu = simple_strtoul(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 < 0 || cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
+					pr_err("Ignoring invalid CPU %d 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: 36aad21d50d1af3f6954bb0c988257a908202477

Signature · 1 line
2.43.0
#1 lore raw
Quoted 5 lines · Avi Weiss

reboot_setup() and cpu_store() compare the requested CPU ID against num_possible_cpus(), which is a count rather than the highest possible CPU ID. On sparse masks, this rejects valid high-numbered CPUs and accepts impossible CPU IDs.

Quoted 8 lines

Validate the CPU ID against nr_cpu_ids and cpu_possible() instead. Reject negative parsed values before the mask lookup and update the error message to avoid assuming contiguous CPU numbering.

On an arm64 QEMU guest with possible CPUs 0,2-3, reboot=s3 selected CPU 0 and sysfs rejected CPU 3 with -ERANGE. With the fix, reboot=s3 selects CPU 3 and sysfs accepts it, while both interfaces reject impossible CPU 1.

Hmm? Did you have to hack the dtb?

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

---
kernel/reboot.c | 9 ++++-----

1 file changed, 4 insertions(+), 5 deletions(-)
kernel/reboot.c +0 -0
index 9a84401adde5..7b09116376e3 100644

kernel/reboot.c static int __init reboot_setup(char *str) · line 1151

Quoted 10 lines
kernel/reboot.c +3 -4
@@ -1146,10 +1146,9 @@ static int __init reboot_setup(char *str)
 			if (isdigit(str[0])) {
 				int cpu = simple_strtoul(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 < 0 || cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
+					pr_err("Ignoring invalid CPU %d in reboot= option\n",
+					       cpu);

Could the parse be unsigned? simple_strtoul() returns an unsigned long, the int cast cuts it down, so reboot=s4294967296 wraps to 0 on 64 bit and you reboot on CPU 0 anyway. And cpu < 0 goes away too, here's a something you could apply on top of Ur patch.

kernel/reboot.c +3 -3
index 7b09116376e35..4f28f7eb21a13 100644
@@ -1144,10 +1144,10 @@ 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 cpu = simple_strtoul(str, NULL, 0);
 
-				if (cpu < 0 || cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
-					pr_err("Ignoring invalid CPU %d in reboot= option\n",
+				if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
+					pr_err("Ignoring invalid CPU %lu in reboot= option\n",
 					       cpu);
 					break;
 				}

kstrtoul() chokes on the ,... tail, so simple_strtoul() has to stay.

kernel/reboot.c static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr, · line 1356

Quoted 12 lines
kernel/reboot.c +1 -1
 					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;

One more thing, and this is the suspicious part. Youve sent 101 patches since july, 15 today alone, across 59 different subsystems and drivers, and
lore has the whole run of them.

Id reckon you'd be using a LLM to develop them, and that's fine, but other subsystems may not like this kind of thing, so perhaps be more considerate of others when submitting the patches?

Perhaps Google maintainer burnout and that'll help you.

Quoted 3 lines

base-commit: 36aad21d50d1af3f6954bb0c988257a908202477

--- Thanks!

A message that is not in the archive (sent off-list or not yet received): CA+jvY9F9DS4SwAg=xPS-8GRee_01V5LpbCpSWdP581MOuwBz_A@mail.gmail.com

#3 lore raw
Quoted 10 lines · Avi

Hi Bradley,
Sure no worries - I'll check your suggestions and after I re-validate it I'll submit a v2.
Regarding the patches - yes I'm working through a big list of (potential) bugs I had flagged from last year, but have a chance now on some holidays to run through them.
I only submit things I'm confident of and have reproduced. I'll take your advice on board.
Cheers,
Avi

Please don't lie. It's not embarrassing to use AI for things... Really it's not, anyone who embarrasses or says your wrong for using AI is wrong in my opinion.

Quoted 102 lines
Quoted 33 lines · Bradley Morgan
Quoted 5 lines · Avi Weiss

reboot_setup() and cpu_store() compare the requested CPU ID against num_possible_cpus(), which is a count rather than the highest possible CPU ID. On sparse masks, this rejects valid high-numbered CPUs and accepts impossible CPU IDs.

Quoted 8 lines

Validate the CPU ID against nr_cpu_ids and cpu_possible() instead. Reject negative parsed values before the mask lookup and update the error message to avoid assuming contiguous CPU numbering.

On an arm64 QEMU guest with possible CPUs 0,2-3, reboot=s3 selected CPU 0 and sysfs rejected CPU 3 with -ERANGE. With the fix, reboot=s3 selects CPU 3 and sysfs accepts it, while both interfaces reject impossible CPU 1.

Hmm? Did you have to hack the dtb?

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

---
kernel/reboot.c | 9 ++++-----

1 file changed, 4 insertions(+), 5 deletions(-)
kernel/reboot.c +0 -0
index 9a84401adde5..7b09116376e3 100644
@@ -1146,10 +1146,9 @@ static int __init reboot_setup(char *str)
                      if (isdigit(str[0])) {
                              int cpu = simple_strtoul(str, NULL, 0);
 
Quoted 2 lines

kernel/reboot.c static int __init reboot_setup(char *str) · line 1150

Quoted 2 lines
kernel/reboot.c +0 -2
-                              if (cpu >= num_possible_cpus()) {
-                                      pr_err("Ignoring the CPU number

in

Quoted 7 lines

reboot= option. "

kernel/reboot.c static int __init reboot_setup(char *str) · line 1151

Quoted 1 line
kernel/reboot.c +0 -1
-                                      "CPU %d exceeds possible cpu

number %d\n",

kernel/reboot.c static int __init reboot_setup(char *str) · line 1149

Quoted 2 lines
kernel/reboot.c +1 -1
-                                      cpu, num_possible_cpus());
+                              if (cpu < 0 || cpu >= nr_cpu_ids ||

!cpu_possible(cpu)) {

Quoted 1 line
kernel/reboot.c +1 -0
+                                      pr_err("Ignoring invalid CPU %d

in

Quoted 13 lines

reboot= option\n",

kernel/reboot.c static int __init reboot_setup(char *str) · line 1151

Quoted 1 line
kernel/reboot.c +1 -0
+                                             cpu);

Could the parse be unsigned? simple_strtoul() returns an unsigned long, the int cast cuts it down, so reboot=s4294967296 wraps to 0 on 64 bit and you reboot on CPU 0 anyway. And cpu < 0 goes away too, here's a something you could apply on top of Ur patch.

kernel/reboot.c +0 -0
index 7b09116376e35..4f28f7eb21a13 100644

kernel/reboot.c static int __init reboot_setup(char *str) · line 1152

Quoted 10 lines
kernel/reboot.c +1 -3
@@ -1144,10 +1144,10 @@ 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 cpu = simple_strtoul(str,
 NULL, 0);
 
-                               if (cpu < 0 || cpu >= nr_cpu_ids ||
 !cpu_possible(cpu)) {
-                                       pr_err("Ignoring invalid CPU %d

in

Quoted 30 lines
kernel/reboot.c +1 -0
 reboot= option\n",
+                               if (cpu >= nr_cpu_ids ||

!cpu_possible(cpu)) {
+ pr_err("Ignoring invalid CPU %lu
in reboot= option\n",

                                               cpu);
                                        break;
                                }

kstrtoul() chokes on the ,... tail, so simple_strtoul() has to stay.

kernel/reboot.c static ssize_t cpu_store(struct kobject *kobj,

Quoted 4 lines
kernel/reboot.c +0 -0
                                      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, · line 1356

Quoted 8 lines
kernel/reboot.c +1 -1
      if (rc)
              return rc;
 
-      if (cpunum >= num_possible_cpus())
+      if (cpunum >= nr_cpu_ids || !cpu_possible(cpunum))
              return -ERANGE;
 
      reboot_default = 0;

One more thing, and this is the suspicious part. Youve sent 101 patches since july, 15 today alone, across 59 different subsystems and drivers, and
lore has the whole run of them.

Quoted 15 lines
Quoted 1 line

Id reckon you'd be using a LLM to develop them, and that's fine, but

other

Quoted 1 line

subsystems may not like this kind of thing, so perhaps be more

considerate

Quoted 10 lines

of others when submitting the patches?

Perhaps Google maintainer burnout and that'll help you.

Quoted 3 lines

base-commit: 36aad21d50d1af3f6954bb0c988257a908202477

--- Thanks!

--- Thanks!