Skip to contentSkip to conversation

Adding a failure check after grub_malloc()

PATCH In review Avnish Chouhan 3 participants 10 messages Oct 28, 2025 – Nov 5, 2025

lore
Share & tools
lore
#0 lore raw

cover Adding a failure check after grub_malloc()

This patch series adds a NULL check after calling grub_malloc(). Missing a failure check after calling grub_malloc() can lead to undefined behavior. If the allocation 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-normal-Adding-a-grub_malloc-failure-check-in-completion-c.patch) Patch 2/4 (0002-Adding-a-grub_malloc-failure-check-in-msdos-c.patch) Patch 3/4 (0003-Adding-a-grub_malloc-failure-check-in-mmap-c.patch) Patch 4/4 (0004-Adding-a-grub_malloc-failure-check-in-legacy_parse-c.patch)

Avnish Chouhan (4):
normal: Adding a grub_malloc failure check in completion.c Adding a grub_malloc failure check in msdos.c Adding a grub_malloc failure check in mmap.c Adding a grub_malloc failure check in legacy_parse.c

 grub-core/normal/completion.c | 3 +++
 grub-core/partmap/msdos.c     | 3 +++
 grub-core/mmap/mmap.c         | 3 +++
 grub-core/lib/legacy_parse.c  | 3 +++
 4 files changed
Signature · 7 lines
2.31.1


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

PATCH 1/4 normal: Adding a grub_malloc failure check in completion.c

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

---
 grub-core/normal/completion.c | 3 +++
 1 file changed, 3 insertions(+)
grub-core/normal/completion.c +3 -0
index 18cadfa..4058e0a 100644
@@ -490,6 +490,9 @@ grub_normal_do_completion (char *buf, int *restore,
 	  spaces++;
 
       ret = grub_malloc (match_len - current_len + grub_strlen (suffix) + spaces + 1);
+      if (ret == NULL)
+        goto fail;
+
       newstr = ret;
       for (escstr = match + current_len; *escstr; escstr++)
 	{
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] normal: Adding a grub_malloc failure check in completion.c

Quoted 7 lines · Avnish Chouhan

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

Thanks,
Sudhakar

Quoted 26 lines

---
grub-core/normal/completion.c | 3 +++

1 file changed, 3 insertions(+)
grub-core/normal/completion.c +3 -0
index 18cadfa..4058e0a 100644
@@ -490,6 +490,9 @@ grub_normal_do_completion (char *buf, int *restore,
 spaces++;
 
      ret = grub_malloc (match_len - current_len + grub_strlen (suffix) + spaces + 1);
+      if (ret == NULL)
+        goto fail;
+
      newstr = ret;
      for (escstr = match + current_len; *escstr; escstr++)
 {
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 Adding a grub_malloc failure check in msdos.c

In reply to Avnish Chouhan

Adding a NULL check in grub_malloc(). Missing a failure check after calling grub_malloc() can lead to undefined behavior. If the allocation fails and returns NULL, subsequent dereferencing or writing to the pointer will likely result in a runtime
error such as a segmentation fault.

---
 grub-core/partmap/msdos.c | 3 +++
 1 file changed, 3 insertions(+)
grub-core/partmap/msdos.c +3 -0
index c85bb74..bf92617 100644
@@ -348,6 +348,9 @@ pc_partition_map_embed (struct grub_disk *disk, unsigned int *nsectors,
        * area.
        */
       embed_signature_check = grub_malloc (GRUB_DISK_SECTOR_SIZE);
+      if (embed_signature_check == NULL)
+        return grub_errno;
+
       for (i = 0; i < *nsectors; i++)
 	{
 	  if (grub_disk_read (disk, (*sectors)[i], 0, GRUB_DISK_SECTOR_SIZE,
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] Adding a grub_malloc failure check in msdos.c

Quoted 9 lines · Avnish Chouhan

Adding a NULL check in grub_malloc(). Missing a failure check after calling grub_malloc() can lead to undefined behavior. If the allocation fails and returns NULL, subsequent dereferencing or writing to the pointer will likely result in a runtime
error such as a segmentation fault.

Thanks,
Sudhakar

Quoted 26 lines

---
grub-core/partmap/msdos.c | 3 +++

1 file changed, 3 insertions(+)
grub-core/partmap/msdos.c +3 -0
index c85bb74..bf92617 100644
@@ -348,6 +348,9 @@ pc_partition_map_embed (struct grub_disk *disk, unsigned int *nsectors,
       * area.
       */
      embed_signature_check = grub_malloc (GRUB_DISK_SECTOR_SIZE);
+      if (embed_signature_check == NULL)
+        return grub_errno;
+
      for (i = 0; i < *nsectors; i++)
 {
 if (grub_disk_read (disk, (*sectors)[i], 0, GRUB_DISK_SECTOR_SIZE,
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 Adding a grub_malloc failure check in mmap.c

In reply to Avnish Chouhan

Adding a failure check after calling grub_malloc() as it can lead to undefined behavior. If the allocation fails and returns NULL, subsequent dereferencing or writing to the pointer will likely result in a runtime error such as a segmentation fault.

---
 grub-core/mmap/mmap.c | 3 +++
 1 file changed, 3 insertions(+)
grub-core/mmap/mmap.c +3 -0
index c8c8312..8f03b77 100644
@@ -242,6 +242,9 @@ grub_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
 	  else
 	    {
 	      struct mm_list *n = grub_malloc (sizeof (*n));
+              if (n == NULL)
+                return grub_errno;
+
 	      n->val = ctx.scanline_events[i].memtype;
 	      n->present = 1;
 	      n->next = present[ctx.scanline_events[i].priority].next;
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] Adding a grub_malloc failure check in mmap.c

Quoted 10 lines · Avnish Chouhan

Adding a failure check after calling grub_malloc() as it can lead to undefined behavior. If the allocation fails and returns NULL, subsequent dereferencing or writing to the pointer will likely result in a runtime error such as a segmentation fault.

Thanks,
Sudhakar

Quoted 26 lines

---
grub-core/mmap/mmap.c | 3 +++

1 file changed, 3 insertions(+)
grub-core/mmap/mmap.c +3 -0
index c8c8312..8f03b77 100644
@@ -242,6 +242,9 @@ grub_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
 else
   {
     struct mm_list *n = grub_malloc (sizeof (*n));
+              if (n == NULL)
+                return grub_errno;
+
     n->val = ctx.scanline_events[i].memtype;
     n->present = 1;
     n->next = present[ctx.scanline_events[i].priority].next;
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 Adding a grub_malloc failure check in legacy_parse.c

In reply to Avnish Chouhan

Adding a failure check after calling grub_malloc() as it can lead to undefined behavior. If the allocation fails and returns NULL, subsequent dereferencing or writing to the pointer will likely result in a runtime error such as a segmentation fault.

---
 grub-core/lib/legacy_parse.c | 3 +++
 1 file changed, 3 insertions(+)
grub-core/lib/legacy_parse.c +3 -0
index fa0131a..8995309 100644
@@ -508,6 +508,9 @@ grub_legacy_parse (const char *buf, char **entryname, char **suffix)
       char *ret;
       int len = grub_strlen (buf);
       ret = grub_malloc (len + 2);
+      if (ret == NULL)
+        return NULL;
+
       grub_memcpy (ret, buf, len);
       if (len && ret[len - 1] == '\n')
 	ret[len] = 0;
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] Adding a grub_malloc failure check in legacy_parse.c

Quoted 10 lines · Avnish Chouhan

Adding a failure check after calling grub_malloc() as it can lead to undefined behavior. If the allocation fails and returns NULL, subsequent dereferencing or writing to the pointer will likely result in a runtime error such as a segmentation fault.

Thanks,
Sudhakar

Quoted 26 lines

---
grub-core/lib/legacy_parse.c | 3 +++

1 file changed, 3 insertions(+)
grub-core/lib/legacy_parse.c +3 -0
index fa0131a..8995309 100644
@@ -508,6 +508,9 @@ grub_legacy_parse (const char *buf, char **entryname, char **suffix)
      char *ret;
      int len = grub_strlen (buf);
      ret = grub_malloc (len + 2);
+      if (ret == NULL)
+        return NULL;
+
      grub_memcpy (ret, buf, len);
      if (len && ret[len - 1] == '\n')
 ret[len] = 0;
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

#9 lore raw
In reply to Avnish Chouhan
Quoted 11 lines · Avnish Chouhan

This patch series adds a NULL check after calling grub_malloc(). Missing a failure check after calling grub_malloc() can lead to undefined behavior. If the allocation 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-normal-Adding-a-grub_malloc-failure-check-in-completion-c.patch) Patch 2/4 (0002-Adding-a-grub_malloc-failure-check-in-msdos-c.patch) Patch 3/4 (0003-Adding-a-grub_malloc-failure-check-in-mmap-c.patch) Patch 4/4 (0004-Adding-a-grub_malloc-failure-check-in-legacy_parse-c.patch)

Quoted 5 lines

Avnish Chouhan (4):
normal: Adding a grub_malloc failure check in completion.c Adding a grub_malloc failure check in msdos.c Adding a grub_malloc failure check in mmap.c Adding a grub_malloc failure check in legacy_parse.c

For all patches Reviewed-by: Daniel Kiper <[email protected]>...

Thank you for fixing these issues!

Daniel

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