Skip to contentSkip to conversation

Adding missing grub_strdup()/strdup() failure checks

PATCH In review Avnish Chouhan 3 participants 10 messages Nov 10, 2025 – Nov 12, 2025

lore
Share & tools
lore
#0 lore raw

cover Adding missing grub_strdup()/strdup() failure checks

This patch series adds a failure check after calling grub_strdup()/strdup. Missing a failure check after calling grub_strdup() can lead to undefined behavior. If it fails and returns NULL, subsequent dereferencing or writing to the pointer will likely result in a runtime error such as a segmentation fault.

Patch 1/4 (0001-ieee1275-openfw-add-missing-grub_strdup-failure-chec.patch) Patch 2/4 (0002-script-execute-add-missing-grub_strdup-failure-check.patch) Patch 3/4 (0003-kern-mips-arc-add-missing-grub_strdup-failure-check.patch) Patch 4/4 (0004-osdep-linux-getroot-add-missing-strdup-failure-check.patch)

Avnish Chouhan (4):
ieee1275/openfw: add missing grub_strdup failure checks script/execute: add missing grub_strdup failure check kern/mips/arc: add missing grub_strdup failure check osdep/linux/getroot: add missing strdup failure checks

 grub-core/kern/ieee1275/openfw.c | 17 ++++++++++++++---
 grub-core/script/execute.c       | 3 +++
 grub-core/kern/mips/arc/init.c   | 3 +++
 grub-core/osdep/linux/getroot.c  | 10 ++++++++++
 4 files changed
Signature · 7 lines
2.47.1


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

PATCH 1/4 ieee1275/openfw: add missing grub_strdup failure checks

Adding a failure check in grub_strdup. If grub_strdup() fails, it returns NULL and passing NULL to a grub_ieee1275_next_property() can lead to segmentation faults or undefined behavior.

---
 grub-core/kern/ieee1275/openfw.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
grub-core/kern/ieee1275/openfw.c +14 -3
index 11b2beb..ba3307d 100644
@@ -201,6 +201,11 @@ grub_ieee1275_devalias_next (struct grub_ieee1275_devalias *alias)
 	  alias->path = 0;
 	}
       tmp = grub_strdup (alias->name);
+      if (tmp == NULL)
+        {
+          grub_ieee1275_devalias_free (alias);
+          return 0;
+        }
       if (grub_ieee1275_next_property (alias->parent_dev, tmp,
 				       alias->name) <= 0)
 	{
@@ -432,9 +437,15 @@ grub_ieee1275_parse_args (const char *path, enum grub_ieee1275_parse_type ptype)
 	  ret = grub_strdup (args);
 	else
 	  ret = grub_strndup (args, (grub_size_t)(comma - args));
-	/* Consistently provide numbered partitions to GRUB.
-	   OpenBOOT traditionally uses alphabetical partition
-	   specifiers.  */
+
+        if (ret == NULL)
+          return 0;
+
+        /*
+         * Consistently provide numbered partitions to GRUB.
+         * OpenBOOT traditionally uses alphabetical partition
+         * specifiers.
+         */
 	if (ret[0] >= 'a' && ret[0] <= 'z')
 	    ret[0] = '1' + (ret[0] - 'a');
 	grub_free (args);
Signature · 7 lines
2.47.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] ieee1275/openfw: add missing grub_strdup failure checks

Quoted 9 lines · Avnish Chouhan

Adding a failure check in grub_strdup. If grub_strdup() fails, it returns NULL and passing NULL to a grub_ieee1275_next_property() can lead to segmentation faults or undefined behavior.

Thanks,
Sudhakar

Quoted 36 lines

---
grub-core/kern/ieee1275/openfw.c | 17 ++++++++++++++---

1 file changed, 14 insertions(+), 3 deletions(-)
grub-core/kern/ieee1275/openfw.c +5 -0
index 11b2beb..ba3307d 100644
@@ -201,6 +201,11 @@ grub_ieee1275_devalias_next (struct grub_ieee1275_devalias *alias)
 alias->path = 0;
 }
      tmp = grub_strdup (alias->name);
+      if (tmp == NULL)
+        {
+          grub_ieee1275_devalias_free (alias);
+          return 0;
+        }
      if (grub_ieee1275_next_property (alias->parent_dev, tmp,
      alias->name) <= 0)
 {
@@ -432,9 +437,15 @@ grub_ieee1275_parse_args (const char *path, enum grub_ieee1275_parse_type ptype)
 ret = grub_strdup (args);

else
ret = grub_strndup (args, (grub_size_t)(comma - args));
- /* Consistently provide numbered partitions to GRUB.

grub-core/kern/ieee1275/openfw.c +9 -2
-   OpenBOOT traditionally uses alphabetical partition
-   specifiers.  */
+
+        if (ret == NULL)
+          return 0;
+
+        /*
+         * Consistently provide numbered partitions to GRUB.
+         * OpenBOOT traditionally uses alphabetical partition
+         * specifiers.
+         */
Quoted 11 lines

if (ret[0] >= 'a' && ret[0] <= 'z')
ret[0] = '1' + (ret[0] - 'a');
grub_free (args);

Signature · 7 lines
2.47.1


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

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

#3 lore raw

PATCH 2/4 script/execute: add missing grub_strdup failure check

In reply to Avnish Chouhan

Adding a failure check in grub_strdup(). If grub_strdup fails (e.g., due to memory allocation failure), it returns NULL. Then, passing assign (which would be NULL) to grub_strchr() will result in a null pointer dereference, which can cause undefined behavior.

---
 grub-core/script/execute.c | 3 +++
 1 file changed, 3 insertions(+)
grub-core/script/execute.c +3 -0
index da99dfa..62afcdc 100644
@@ -1015,6 +1015,9 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
 	{
 	  /* As a last resort, try if it is an assignment.  */
 	  char *assign = grub_strdup (cmdname);
+          if (assign == NULL)
+            return grub_errno;
+
 	  char *eq = grub_strchr (assign, '=');
 
 	  if (eq)
Signature · 7 lines
2.47.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v1 2/4
#4 lore raw

Re: [PATCH 2/4] script/execute: add missing grub_strdup failure check

Quoted 11 lines · Avnish Chouhan

Adding a failure check in grub_strdup(). If grub_strdup fails (e.g., due to memory allocation failure), it returns NULL. Then, passing assign (which would be NULL) to grub_strchr() will result in a null pointer dereference, which can cause undefined behavior.

Thanks,
Sudhakar

Quoted 26 lines

---
grub-core/script/execute.c | 3 +++

1 file changed, 3 insertions(+)
grub-core/script/execute.c +3 -0
index da99dfa..62afcdc 100644
@@ -1015,6 +1015,9 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
 {
 /* As a last resort, try if it is an assignment.  */
 char *assign = grub_strdup (cmdname);
+          if (assign == NULL)
+            return grub_errno;
+
 char *eq = grub_strchr (assign, '=');
 
 if (eq)
Signature · 7 lines
2.47.1


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

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

#5 lore raw

PATCH 3/4 kern/mips/arc: add missing grub_strdup failure check

In reply to Avnish Chouhan

Adding a failure check in grub_strdup. If grub_strdup() fails, it returns NULL and can lead to segmentation faults or undefined behavior.

---
 grub-core/kern/mips/arc/init.c | 3 +++
 1 file changed, 3 insertions(+)
grub-core/kern/mips/arc/init.c +3 -0
index 2ed3ff3..2b76988 100644
@@ -403,6 +403,9 @@ grub_machine_get_bootlocation (char **device, char **path)
       if (!syspart)
 	return;
       loaddev = grub_strdup (syspart);
+      if (loaddev == NULL)
+        return;
+
     }
 
   partptr = get_part (loaddev);
Signature · 7 lines
2.47.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v1 3/4
#6 lore raw

Re: [PATCH 3/4] kern/mips/arc: add missing grub_strdup failure check

Quoted 8 lines · Avnish Chouhan

Adding a failure check in grub_strdup. If grub_strdup() fails, it returns NULL and can lead to segmentation faults or undefined behavior.

Thanks,
Sudhakar

Quoted 26 lines

---
grub-core/kern/mips/arc/init.c | 3 +++

1 file changed, 3 insertions(+)
grub-core/kern/mips/arc/init.c +3 -0
index 2ed3ff3..2b76988 100644
@@ -403,6 +403,9 @@ grub_machine_get_bootlocation (char **device, char **path)
      if (!syspart)
 return;
      loaddev = grub_strdup (syspart);
+      if (loaddev == NULL)
+        return;
+
    }
 
  partptr = get_part (loaddev);
Signature · 7 lines
2.47.1


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

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

#7 lore raw

PATCH 4/4 osdep/linux/getroot: add missing strdup failure checks

In reply to Avnish Chouhan

Adding a failure check in strdup. If strdup() fails, it returns NULL and passing NULL to a strchr() can lead to segmentation faults or undefined behavior.

---
 grub-core/osdep/linux/getroot.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
grub-core/osdep/linux/getroot.c +10 -0
index 38fe110..d71c373 100644
@@ -991,6 +991,8 @@ grub_util_get_raid_grub_dev (const char *os_dev)
       char *p, *q;
 
       p = strdup (os_dev + sizeof ("/dev/md_d") - 1);
+      if (p == NULL)
+        return NULL;
 
       q = strchr (p, 'p');
       if (q)
@@ -1006,6 +1008,8 @@ grub_util_get_raid_grub_dev (const char *os_dev)
       char *p, *q;
 
       p = strdup (os_dev + sizeof ("/dev/md/d") - 1);
+      if (p == NULL)
+        return NULL;
 
       q = strchr (p, 'p');
       if (q)
@@ -1019,6 +1023,8 @@ grub_util_get_raid_grub_dev (const char *os_dev)
       char *p , *q;
 
       p = strdup (os_dev + sizeof ("/dev/md") - 1);
+      if (p == NULL)
+        return NULL;
 
       q = strchr (p, 'p');
       if (q)
@@ -1032,6 +1038,8 @@ grub_util_get_raid_grub_dev (const char *os_dev)
       char *p , *q;
 
       p = strdup (os_dev + sizeof ("/dev/md/") - 1);
+      if (p == NULL)
+        return NULL;
 
       q = strchr (p, 'p');
       if (q)
@@ -1046,6 +1054,8 @@ grub_util_get_raid_grub_dev (const char *os_dev)
       char *p , *q;
 
       p = strdup (os_dev + sizeof ("/dev/md/") - 1);
+      if (p == NULL)
+        return NULL;
 
       q = strchr (p, 'p');
       if (q)
Signature · 7 lines
2.47.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v1 4/4
#8 lore raw

Re: [PATCH 4/4] osdep/linux/getroot: add missing strdup failure checks

Quoted 9 lines · Avnish Chouhan

Adding a failure check in strdup. If strdup() fails, it returns NULL and passing NULL to a strchr() can lead to segmentation faults or undefined behavior.

Thanks,
Sudhakar

Quoted 61 lines

---
grub-core/osdep/linux/getroot.c | 10 ++++++++++

1 file changed, 10 insertions(+)
grub-core/osdep/linux/getroot.c +10 -0
index 38fe110..d71c373 100644
@@ -991,6 +991,8 @@ grub_util_get_raid_grub_dev (const char *os_dev)
      char *p, *q;
 
      p = strdup (os_dev + sizeof ("/dev/md_d") - 1);
+      if (p == NULL)
+        return NULL;
 
      q = strchr (p, 'p');
      if (q)
@@ -1006,6 +1008,8 @@ grub_util_get_raid_grub_dev (const char *os_dev)
      char *p, *q;
 
      p = strdup (os_dev + sizeof ("/dev/md/d") - 1);
+      if (p == NULL)
+        return NULL;
 
      q = strchr (p, 'p');
      if (q)
@@ -1019,6 +1023,8 @@ grub_util_get_raid_grub_dev (const char *os_dev)
      char *p , *q;
 
      p = strdup (os_dev + sizeof ("/dev/md") - 1);
+      if (p == NULL)
+        return NULL;
 
      q = strchr (p, 'p');
      if (q)
@@ -1032,6 +1038,8 @@ grub_util_get_raid_grub_dev (const char *os_dev)
      char *p , *q;
 
      p = strdup (os_dev + sizeof ("/dev/md/") - 1);
+      if (p == NULL)
+        return NULL;
 
      q = strchr (p, 'p');
      if (q)
@@ -1046,6 +1054,8 @@ grub_util_get_raid_grub_dev (const char *os_dev)
      char *p , *q;
 
      p = strdup (os_dev + sizeof ("/dev/md/") - 1);
+      if (p == NULL)
+        return NULL;
 
      q = strchr (p, 'p');
      if (q)
Signature · 7 lines
2.47.1


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

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

Reviewed-by Daniel Kiper on the v1 series
#9 lore raw
In reply to Avnish Chouhan
Quoted 11 lines · Avnish Chouhan

This patch series adds a failure check after calling grub_strdup()/strdup. Missing a failure check after calling grub_strdup() can lead to undefined behavior. If it fails and returns NULL, subsequent dereferencing or writing to the pointer will likely result in a runtime error such as a segmentation fault.

Patch 1/4 (0001-ieee1275-openfw-add-missing-grub_strdup-failure-chec.patch) Patch 2/4 (0002-script-execute-add-missing-grub_strdup-failure-check.patch) Patch 3/4 (0003-kern-mips-arc-add-missing-grub_strdup-failure-check.patch) Patch 4/4 (0004-osdep-linux-getroot-add-missing-strdup-failure-check.patch)

Quoted 5 lines

Avnish Chouhan (4):
ieee1275/openfw: add missing grub_strdup failure checks script/execute: add missing grub_strdup failure check kern/mips/arc: add missing grub_strdup failure check osdep/linux/getroot: add missing strdup failure checks

Thank you for fixing these issues!

Daniel

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