Skip to contentSkip to conversation

commands/file: Fix null dereference in the knetbsd tests

PATCH In review Glenn Washburn 3 participants 3 messages Jan 5, 2025 – Feb 20, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 commands/file: Fix null dereference in the knetbsd tests

From: Lukas Fink <[email protected]>

The pointer returned by grub_elf_file() is not checked to verify it is not null before use. A null pointer may be returned when the given file does not have a valid ELF header.

---
 grub-core/commands/file.c | 2 ++
 1 file changed, 2 insertions(+)
grub-core/commands/file.c +2 -0
index 7c13e976b505..19602d75786b 100644
@@ -306,6 +306,8 @@ grub_cmd_file (grub_extcmd_context_t ctxt, int argc, char **args)
 
 	elf = grub_elf_file (file, file->name);
 
+	if (elf == NULL)
+	  break;
 	if (elf->ehdr.ehdr32.e_type != grub_cpu_to_le16_compile_time (ET_EXEC)
 	    || elf->ehdr.ehdr32.e_ident[EI_DATA] != ELFDATA2LSB)
 	  break;
Signature · 7 lines
2.34.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by Ross Philipson on v1 1/1
#1 lore raw
Quoted 5 lines · Glenn Washburn

From: Lukas Fink <[email protected]>

The pointer returned by grub_elf_file() is not checked to verify it is not null before use. A null pointer may be returned when the given file does not have a valid ELF header.

Indeed or for a number of other reasons. Also other places in the switch check if elf == NULL.

Quoted 13 lines
---
  grub-core/commands/file.c | 2 ++
  1 file changed, 2 insertions(+)
grub-core/commands/file.c +0 -0
index 7c13e976b505..19602d75786b 100644

grub-core/commands/file.c grub_cmd_file (grub_extcmd_context_t ctxt, int argc, char **args) · line 313

Quoted 8 lines
grub-core/commands/file.c +2 -0
@@ -306,6 +306,8 @@ grub_cmd_file (grub_extcmd_context_t ctxt, int argc, char **args)
 
  	elf = grub_elf_file (file, file->name);
 
+	if (elf == NULL)
+	  break;
  	if (elf->ehdr.ehdr32.e_type != grub_cpu_to_le16_compile_time (ET_EXEC)
  	    || elf->ehdr.ehdr32.e_ident[EI_DATA] != ELFDATA2LSB)
  	  break;

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

Reviewed-by
#2 lore raw
Quoted 11 lines · Ross Philipson via Grub-devel
Quoted 5 lines · Glenn Washburn

From: Lukas Fink <[email protected]>

The pointer returned by grub_elf_file() is not checked to verify it is not null before use. A null pointer may be returned when the given file does not have a valid ELF header.

Indeed or for a number of other reasons. Also other places in the switch check if elf == NULL.

Daniel

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