Skip to contentSkip to conversation

Silently ignore grub_file_read when p_filesz == 0

PATCH In review Kalamatee 2 participants 2 messages Mar 21, 2008 – Mar 24, 2008

lore
Share & tools
lore

Kalamatee

author
#0 lore raw

PATCH 1/1 Silently ignore grub_file_read when p_filesz == 0

After reading through the ELF specs (
http://www.muppetlabs.com/~breadbox/software/ELF.txt) its become clear that a PT_LOAD with p_filesz == 0,is valid - and after checking some elf binaries ive found a number of cases where this is true (including the bootstrap of AROS64)

please find a patch enclosed to change the behaviour of multiboot.c's elf loader to silently ignore loading the data if p_filesz == 0

2008-03-21 Kalamatee [email protected]

* loader/i386/pc/multiboot.c (grub_multiboot_load_elf32): If p_filesz == 0, do not attempt to
read data from the file.

        (grub_multiboot_load_elf64): If p_filesz == 0, do not attempt to

read data from the file.

i386/pc/multiboot.c 21 Mar 2008 11:20:01 -0000 renamed from i386/pc/multiboot.c 5 Mar 2008 05:09:35 -0000 1.19 +4 -4
diff -u -8 -p -r1.19 multiboot.c
@@ -134,18 +134,18 @@ grub_multiboot_load_elf32 (grub_file_t f
                               phdr->p_paddr + phdr->p_memsz,
                               grub_os_area_addr + grub_os_area_size);
 
           if (grub_file_seek (file, (grub_off_t) phdr->p_offset)
              == (grub_off_t) -1)
            return grub_error (GRUB_ERR_BAD_OS,
                               "invalid offset in program header");
 
-          if (grub_file_read (file, (void *) phdr->p_paddr, phdr->p_filesz)
-              != (grub_ssize_t) phdr->p_filesz)
+          if ((phdr->p_filesz > 0) && (grub_file_read (file, (void *) phdr->p_paddr, phdr->p_filesz)
+              != (grub_ssize_t) phdr->p_filesz))
            return grub_error (GRUB_ERR_BAD_OS,
                               "couldn't read segment from file");
 
           if (phdr->p_filesz < phdr->p_memsz)
             grub_memset ((char *) phdr->p_paddr + phdr->p_filesz, 0,
                         phdr->p_memsz - phdr->p_filesz);
 
           if ((entry >= phdr->p_vaddr) &&
@@ -221,18 +221,18 @@ grub_multiboot_load_elf64 (grub_file_t f
                               phdr->p_paddr + phdr->p_memsz,
                               (grub_uint64_t) grub_os_area_addr + (grub_uint64_t) grub_os_area_size);
 
          if (grub_file_seek (file, (grub_off_t) phdr->p_offset)
              == (grub_off_t) -1)
            return grub_error (GRUB_ERR_BAD_OS,
                               "invalid offset in program header");
 
-         if (grub_file_read (file, (void *) ((grub_uint32_t) phdr->p_paddr),
-                             phdr->p_filesz)
+         if ((phdr->p_filesz > 0) && (grub_file_read (file, (void *) ((grub_uint32_t) phdr->p_paddr),
+                             phdr->p_filesz))
               != (grub_ssize_t) phdr->p_filesz)
            return grub_error (GRUB_ERR_BAD_OS,
                               "couldn't read segment from file");
 
           if (phdr->p_filesz < phdr->p_memsz)
            grub_memset (((char *) ((grub_uint32_t) phdr->p_paddr)
                          + phdr->p_filesz),
                         0,
#1 lore raw
Quoted 9 lines · Kalamatee

After reading through the ELF specs
(http://www.muppetlabs.com/~breadbox/software/ELF.txt) its become clear that a PT_LOAD with p_filesz == 0,is valid - and after checking some elf binaries ive found a number of cases where this is true (including the bootstrap of AROS64)

please find a patch enclosed to change the behaviour of multiboot.c's elf loader to silently ignore loading the data if p_filesz == 0

Quoted 7 lines

2008-03-21 Kalamatee [email protected]

* loader/i386/pc/multiboot.c (grub_multiboot_load_elf32): If p_filesz == 0, do not attempt to
read data from the file.

        (grub_multiboot_load_elf64): If p_filesz == 0, do not attempt to

read data from the file.

I think the better way to fix this problem is to change grub_file_read itself, as using length 0 as a indicator to read until the end of file is not consistent with the standard read function, and it could easily cause buffer overflown problem. However, i don't know if any part of grub2 is actually rely on this feature, so need to check with it first.

Signature · 1 line
Bean