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.

Signed-off-by: Avnish Chouhan <[email protected]>
---
 grub-core/osdep/linux/getroot.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/grub-core/osdep/linux/getroot.c b/grub-core/osdep/linux/getroot.c
index 38fe110..d71c373 100644
--- a/grub-core/osdep/linux/getroot.c
+++ b/grub-core/osdep/linux/getroot.c
@@ -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)
-- 
2.47.1


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

Reply via email to