Skip to contentSkip to conversation

ieee1275 : Add a check for invalid partition number

PATCH Superseded Avnish Chouhan 3 participants 4 messages Jan 12

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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 | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
grub-core/kern/ieee1275/openfw.c +15 -2
index 3b492dd..f8ae515 100644
@@ -512,7 +512,20 @@ grub_ieee1275_encode_devname (const char *path)
     }
   if (partition && partition[0])
     {
-      unsigned int partno = grub_strtoul (partition, 0, 0);
+      char *endptr;
+
+      unsigned long partno = grub_strtoul (partition, &endptr, 0);
+      grub_errno = GRUB_ERR_NONE;
+
+      if (*endptr != '\0' || partno > 65535 ||
+          (partno == 0 && ! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_0_BASED_PARTITIONS)))
+        {
+          grub_free (partition);
+          grub_free (device);
+          grub_free (encoding);
+          grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid partition number"));
+          return NULL;
+        }
 
       *optr++ = ',';
 
@@ -520,7 +533,7 @@ grub_ieee1275_encode_devname (const char *path)
 	/* GRUB partition 1 is OF partition 0.  */
 	partno++;
 
-      grub_snprintf (optr, sizeof ("XXXXXXXXXXXX"), "%d", partno);
+      grub_snprintf (optr, sizeof ("XXXXXXXXXXXX"), "%lu", partno);
     }
   else
     *optr = '\0';
Signature · 7 lines
2.50.1 (Apple Git-155)


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

Adding a check for invalid partition number. grub_strtoul() can fail

s/Adding/Add/
s/can/may/

Quoted 1 line

in several scenarios like invalid input, overflow, etc will result in

s/etc/etc./

Quoted 1 line

an invalid partition number which could lead to an undefined behavior.

s/will result in an invalid partition number which could lead to an undefined behavior.
/Lack of proper check may lead to unexpected failures in the code further./

Quoted 10 lines
---
 grub-core/kern/ieee1275/openfw.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
grub-core/kern/ieee1275/openfw.c +0 -0
index 3b492dd..f8ae515 100644

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

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

I would do this

unsigned long partno;
char *endptr;

  partno = grub_strtoul (partition, &endptr, 0);
  grub_errno = GRUB_ERR_NONE;
Quoted 13 lines
grub-core/kern/ieee1275/openfw.c +9 -0
+      if (*endptr != '\0' || partno > 65535 ||
+          (partno == 0 && ! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_0_BASED_PARTITIONS)))
+        {
+          grub_free (partition);
+          grub_free (device);
+          grub_free (encoding);
+          grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid partition number"));
+          return NULL;
+        }
 
       *optr++ = ',';
 

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

Quoted 5 lines
grub-core/kern/ieee1275/openfw.c +1 -1
@@ -520,7 +533,7 @@ grub_ieee1275_encode_devname (const char *path)
 	/* GRUB partition 1 is OF partition 0.  */
 	partno++;
 
-      grub_snprintf (optr, sizeof ("XXXXXXXXXXXX"), "%d", partno);
+      grub_snprintf (optr, sizeof ("XXXXXXXXXXXX"), "%lu", partno);

Why is this change part of the patch? Even if it is valid it is logically different thing. And I would use "%zu" instead...

Daniel

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

#2 lore raw
Quoted 8 lines · Daniel Kiper
Quoted 1 line · Avnish Chouhan

Adding a check for invalid partition number. grub_strtoul() can fail

s/Adding/Add/
s/can/may/

Quoted 1 line

in several scenarios like invalid input, overflow, etc will result in

Quoted 9 lines

s/etc/etc./

Quoted 1 line

an invalid partition number which could lead to an undefined behavior.

s/will result in an invalid partition number which could lead to an undefined behavior.
/Lack of proper check may lead to unexpected failures in the code
further./

Hi Daniel,
Thank you for the review!

I'll change these as suggested.

Quoted 21 lines
Quoted 10 lines
---
 grub-core/kern/ieee1275/openfw.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
grub-core/kern/ieee1275/openfw.c +0 -0
index 3b492dd..f8ae515 100644

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

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

I would do this

Quoted 5 lines

unsigned long partno;
char *endptr;

  partno = grub_strtoul (partition, &endptr, 0);
  grub_errno = GRUB_ERR_NONE;

Sure. I'll change like this.

Quoted 19 lines
Quoted 19 lines
grub-core/kern/ieee1275/openfw.c +9 -0
+      if (*endptr != '\0' || partno > 65535 ||
+          (partno == 0 && ! grub_ieee1275_test_flag 
 (GRUB_IEEE1275_FLAG_0_BASED_PARTITIONS)))
+        {
+          grub_free (partition);
+          grub_free (device);
+          grub_free (encoding);
+          grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid partition 
 number"));
+          return NULL;
+        }
 
       *optr++ = ',';
@@ -520,7 +533,7 @@ grub_ieee1275_encode_devname (const char *path)
 	/* GRUB partition 1 is OF partition 0.  */
 	partno++;
 
Quoted 5 lines

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

Quoted 2 lines
grub-core/kern/ieee1275/openfw.c +1 -1
-      grub_snprintf (optr, sizeof ("XXXXXXXXXXXX"), "%d", partno);
+      grub_snprintf (optr, sizeof ("XXXXXXXXXXXX"), "%lu", partno);

Why is this change part of the patch? Even if it is valid it is logically different thing. And I would use "%zu" instead...

This we need due to change from "unsigned int partno" to "unsigned long partno". In present code, partno is unsigned int, and we were using the same (though in the present code, it is wrong. It must be '%u' instead of '%d' anyway). Reason we didn't change this in earlier patch versions.

We'll change it to zu as suggested!

Regards,
Avnish Chouhan

Quoted 2 lines

Daniel

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

#3 lore raw
In reply to Avnish Chouhan
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 | 17 +++++++++++++++--
  1 file changed, 15 insertions(+), 2 deletions(-)
grub-core/kern/ieee1275/openfw.c +0 -0
index 3b492dd..f8ae515 100644

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

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

I recommend setting the base instead of letting it be 0. An input string of "[" would be accepted as the number 43.

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

Quoted 1 line
grub-core/kern/ieee1275/openfw.c +1 -0
+      grub_errno = GRUB_ERR_NONE;

I believe grub_errno should be set before the call to strtoul() instead of after it. Setting grub_errno before would allow you to use it as part of your error checking below.

Regards,
Nicholas Vinson

Quoted 14 lines
grub-core/kern/ieee1275/openfw.c +10 -0
+
+      if (*endptr != '\0' || partno > 65535 ||
+          (partno == 0 && ! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_0_BASED_PARTITIONS)))
+        {
+          grub_free (partition);
+          grub_free (device);
+          grub_free (encoding);
+          grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid partition number"));
+          return NULL;
+        }
 
        *optr++ = ',';
 

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

Quoted 8 lines
grub-core/kern/ieee1275/openfw.c +1 -1
@@ -520,7 +533,7 @@ grub_ieee1275_encode_devname (const char *path)
  	/* GRUB partition 1 is OF partition 0.  */
  	partno++;
 
-      grub_snprintf (optr, sizeof ("XXXXXXXXXXXX"), "%d", partno);
+      grub_snprintf (optr, sizeof ("XXXXXXXXXXXX"), "%lu", partno);
      }
    else
      *optr = '\0';

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