Skip to contentSkip to conversation

ieee1275 : Add a check for invalid partition number

PATCH Superseded Avnish Chouhan 2 participants 4 messages Dec 2, 2025 – Jan 8

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ieee1275 : Add a check for invalid partition number

Adding a check for invalid partition number. grub_strtoul() can fail in several scenarios like invalid input, overflow, etc will result in an invalid partition number which could lead to an undefined behavior.

---
 grub-core/kern/ieee1275/openfw.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
grub-core/kern/ieee1275/openfw.c +12 -1
index 3b492dd..e82dc34 100644
@@ -512,7 +512,18 @@ grub_ieee1275_encode_devname (const char *path)
     }
   if (partition && partition[0])
     {
-      unsigned int partno = grub_strtoul (partition, 0, 0);
+      char *endptr;
+      grub_errno = GRUB_ERR_NONE;
+      unsigned int partno = grub_strtoul (partition, &endptr, 0);
+
+      if (grub_errno != GRUB_ERR_NONE || *endptr != '\0')
+        {
+          grub_free (partition);
+          grub_free (device);
+          grub_free (encoding);
+          grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid partition number"));
+          return NULL;
+        }
 
       *optr++ = ',';
 
Signature · 7 lines
2.52.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw
Quoted 14 lines · Avnish Chouhan

Adding a check for invalid partition number. grub_strtoul() can fail in several scenarios like invalid input, overflow, etc will result in an invalid partition number which could lead to an undefined behavior.

---
 grub-core/kern/ieee1275/openfw.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
grub-core/kern/ieee1275/openfw.c +0 -0
index 3b492dd..e82dc34 100644

grub-core/kern/ieee1275/openfw.c grub_ieee1275_encode_devname (const char *path) · line 516

Quoted 6 lines
grub-core/kern/ieee1275/openfw.c +2 -1
@@ -512,7 +512,18 @@ grub_ieee1275_encode_devname (const char *path)
     }
   if (partition && partition[0])
     {
-      unsigned int partno = grub_strtoul (partition, 0, 0);
+      char *endptr;
+      grub_errno = GRUB_ERR_NONE;

You should do this reset after grub_strtoul() call. The commit 533cd4d68 (blsuki: Fix grub_errno leakage in blsuki_is_default_entry()) explains why.

grub-core/kern/ieee1275/openfw.c grub_ieee1275_encode_devname (const char *path) · line 517

Quoted 1 line
grub-core/kern/ieee1275/openfw.c +1 -0
+      unsigned int partno = grub_strtoul (partition, &endptr, 0);

Do not cast result immediately to shorter type. You are not able to detect overflow then. First assign result to type size equal to type returned by the grub_strtoul() function and then check for overflows properly.

grub-core/kern/ieee1275/openfw.c grub_ieee1275_encode_devname (const char *path) · line 519

Quoted 1 line
grub-core/kern/ieee1275/openfw.c +1 -0
+      if (grub_errno != GRUB_ERR_NONE || *endptr != '\0')

This check is not reliable. Please take a look at the commit ac8a37dda (net/http: Allow use of non-standard TCP/IP ports). It shows how it should be done correctly. Even it is reverted now.

By the way, it would be nice if you could verify correctness of strtoul()/grub_strtoul()/... calls/checks in the GRUB code after the release.

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#2 lore raw
Quoted 10 lines · Daniel Kiper
Quoted 9 lines · Avnish Chouhan

Adding a check for invalid partition number. grub_strtoul() can fail in several scenarios like invalid input, overflow, etc will result in an invalid partition number which could lead to an undefined behavior.

---
 grub-core/kern/ieee1275/openfw.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
Quoted 15 lines

grub-core/kern/ieee1275/openfw.c grub_ieee1275_encode_devname (const char *path) · line 516

Quoted 11 lines
grub-core/kern/ieee1275/openfw.c +2 -1
index 3b492dd..e82dc34 100644
@@ -512,7 +512,18 @@ grub_ieee1275_encode_devname (const char *path)
     }
   if (partition && partition[0])
     {
-      unsigned int partno = grub_strtoul (partition, 0, 0);
+      char *endptr;
+      grub_errno = GRUB_ERR_NONE;

You should do this reset after grub_strtoul() call. The commit 533cd4d68 (blsuki: Fix grub_errno leakage in blsuki_is_default_entry()) explains why.

Hi Daniel,
Thank you so much for reviewing the patch!

This I have added so that we'll not catch any earlier errors. I will reset grub_errno after we verify the grub_strtoul as you suggested.

Quoted 7 lines

grub-core/kern/ieee1275/openfw.c grub_ieee1275_encode_devname (const char *path) · line 517

Quoted 1 line
grub-core/kern/ieee1275/openfw.c +1 -0
+      unsigned int partno = grub_strtoul (partition, &endptr, 0);

Do not cast result immediately to shorter type. You are not able to detect overflow then. First assign result to type size equal to type returned by the grub_strtoul() function and then check for overflows properly.

Sure. I'll use unsigned long!

Quoted 6 lines

grub-core/kern/ieee1275/openfw.c grub_ieee1275_encode_devname (const char *path) · line 519

Quoted 1 line
grub-core/kern/ieee1275/openfw.c +1 -0
+      if (grub_errno != GRUB_ERR_NONE || *endptr != '\0')

This check is not reliable. Please take a look at the commit ac8a37dda (net/http: Allow use of non-standard TCP/IP ports). It shows how it should be done correctly. Even it is reverted now.

This I have added based on the Alec's suggestion in v1, same as you suggested. I have used the same check as in the commit ac8a37dda, just skipping the range check due to use of "grub_errno != GRUB_ERR_NONE" condition. Would you like me to add range check here?

Quoted 3 lines

By the way, it would be nice if you could verify correctness of strtoul()/grub_strtoul()/... calls/checks in the GRUB code after the release.

Sure Daniel, I will surly do!
Thank you!

Regards,
Avnish Chouhan

Quoted 2 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#3 lore raw
Quoted 28 lines · Avnish Chouhan
Quoted 10 lines · Daniel Kiper
Quoted 9 lines · Avnish Chouhan

Adding a check for invalid partition number. grub_strtoul() can fail in several scenarios like invalid input, overflow, etc will result in an invalid partition number which could lead to an undefined behavior.

---
 grub-core/kern/ieee1275/openfw.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
Quoted 16 lines

grub-core/kern/ieee1275/openfw.c grub_ieee1275_encode_devname (const char *path) · line 516

Quoted 12 lines
grub-core/kern/ieee1275/openfw.c +2 -1
index 3b492dd..e82dc34 100644
@@ -512,7 +512,18 @@ grub_ieee1275_encode_devname (const char *path)
     }
   if (partition && partition[0])
     {
-      unsigned int partno = grub_strtoul (partition, 0, 0);
+      char *endptr;
+      grub_errno = GRUB_ERR_NONE;

You should do this reset after grub_strtoul() call. The commit 533cd4d68 (blsuki: Fix grub_errno leakage in blsuki_is_default_entry()) explains why.

Quoted 2 lines

Hi Daniel,
Thank you so much for reviewing the patch!

You are welcome!

Quoted 2 lines

This I have added so that we'll not catch any earlier errors. I will reset grub_errno after we verify the grub_strtoul as you suggested.

Cool!

Quoted 9 lines
Quoted 7 lines

grub-core/kern/ieee1275/openfw.c grub_ieee1275_encode_devname (const char *path) · line 517

Quoted 1 line
grub-core/kern/ieee1275/openfw.c +1 -0
+      unsigned int partno = grub_strtoul (partition, &endptr, 0);

Do not cast result immediately to shorter type. You are not able to detect overflow then. First assign result to type size equal to type returned by the grub_strtoul() function and then check for overflows properly.

Sure. I'll use unsigned long!

Great!

Quoted 7 lines
Quoted 6 lines

grub-core/kern/ieee1275/openfw.c grub_ieee1275_encode_devname (const char *path) · line 519

Quoted 1 line
grub-core/kern/ieee1275/openfw.c +1 -0
+      if (grub_errno != GRUB_ERR_NONE || *endptr != '\0')

This check is not reliable. Please take a look at the commit ac8a37dda (net/http: Allow use of non-standard TCP/IP ports). It shows how it should be done correctly. Even it is reverted now.

Quoted 5 lines

This I have added based on the Alec's suggestion in v1, same as you suggested. I have used the same check as in the commit ac8a37dda, just skipping the range check due to use of "grub_errno != GRUB_ERR_NONE" condition.
Would you like me to add range check here?

Please check range and drop "grub_errno != GRUB_ERR_NONE" check.

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel