Skip to contentSkip to conversation

efiemu/loadcore: Add grub_calloc failure check

PATCH Superseded Avnish Chouhan 3 participants 3 messages Nov 26, 2025 – Nov 27, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 efiemu/loadcore: Add grub_calloc failure check

Adding a failure check in grub_calloc(). If grub_calloc fails, (e.g., due to memory allocation failure), it returns NULL. Then, using grub_efiemu_elfsyms (which would be NULL) later will result in a null pointer dereference,
and can cause an undefined behavior.

---
 grub-core/efiemu/loadcore.c | 3 +++
 1 file changed, 3 insertions(+)
grub-core/efiemu/loadcore.c +3 -0
index 2b92462..5a6d244 100644
@@ -203,6 +203,9 @@ grub_efiemu_count_symbols (const Elf_Ehdr *e)
   grub_efiemu_elfsyms = (struct grub_efiemu_elf_sym *)
     grub_calloc (grub_efiemu_nelfsyms, sizeof (struct grub_efiemu_elf_sym));
 
+  if (grub_efiemu_elfsyms == NULL)
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
+
   /* Relocators */
   for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
        i < e->e_shnum;
Signature · 7 lines
2.46.0


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

Adding a failure check in grub_calloc(). If grub_calloc fails, (e.g., due to memory allocation failure), it returns NULL. Then, using grub_efiemu_elfsyms (which would be NULL) later will result in a null pointer dereference,
and can cause an undefined behavior.

Thanks,
Sudhakar

Quoted 26 lines

---
grub-core/efiemu/loadcore.c | 3 +++

1 file changed, 3 insertions(+)
grub-core/efiemu/loadcore.c +3 -0
index 2b92462..5a6d244 100644
@@ -203,6 +203,9 @@ grub_efiemu_count_symbols (const Elf_Ehdr *e)
  grub_efiemu_elfsyms = (struct grub_efiemu_elf_sym *)
    grub_calloc (grub_efiemu_nelfsyms, sizeof (struct grub_efiemu_elf_sym));
 
+  if (grub_efiemu_elfsyms == NULL)
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
+
  /* Relocators */
  for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
       i < e->e_shnum;
Signature · 7 lines
2.46.0


_______________________________________________
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

#2 lore raw
In reply to Avnish Chouhan

grub_calloc already sets error variables. So you need to return grub_errno and not call grub_error

Regards
Vladimir 'phcoder' Serbinenko

Quoted 26 lines · Avnish Chouhan

Adding a failure check in grub_calloc(). If grub_calloc fails, (e.g., due to memory allocation failure), it returns NULL. Then, using grub_efiemu_elfsyms (which would be NULL) later will result in a null pointer dereference,
and can cause an undefined behavior.

---
 grub-core/efiemu/loadcore.c | 3 +++
 1 file changed, 3 insertions(+)
grub-core/efiemu/loadcore.c +3 -0
index 2b92462..5a6d244 100644
@@ -203,6 +203,9 @@ grub_efiemu_count_symbols (const Elf_Ehdr *e)
   grub_efiemu_elfsyms = (struct grub_efiemu_elf_sym *)
     grub_calloc (grub_efiemu_nelfsyms, sizeof (struct
 grub_efiemu_elf_sym));
 
+  if (grub_efiemu_elfsyms == NULL)
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
+
   /* Relocators */
   for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
        i < e->e_shnum;
Quoted 9 lines

--
2.46.0

_______________________________________________
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