Skip to contentSkip to conversation

hw/elf_ops: defend against weird elf headers

PATCH In review Alex Bennée 3 participants 3 messages Aug 12 – Aug 15

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 hw/elf_ops: defend against weird elf headers

According to the ELF spec:

PT_LOAD

The array element specifies a loadable segment, described by p_filesz and p_memsz. The bytes from the file are mapped to the beginning of the memory segment. If the segment's memory size (p_memsz) is larger than the file size (p_filesz), the ``extra'' bytes are defined to hold the value 0 and to follow the segment's initialized area. The file size may not be larger than the memory size. Loadable segment entries in the program header table appear in ascending order, sorted on the p_vaddr member.

which implies while both p_filesz and p_memsz can be zero we should never see a case where p_filesz is greater than the in memory size. Indeed it has been reported such a hand crafted ELF can blow up, for example during rom_reset():

address_space_set(rom->as, rom->addr + rom->datasize, 0,

                    rom->romsize - rom->datasize,
                    MEMTXATTRS_UNSPECIFIED);

which could trigger and underflow leaving QEMU slowly filling a very large buffer.

---
v2
- ret = ELF_LOAD_TOO_BIG
- tweak subject

---
 include/hw/elf_ops.h.inc | 5 +++++
 1 file changed, 5 insertions(+)
include/hw/elf_ops.h.inc +5 -0
index 9c35d1b9da6..044e72de2a2 100644
@@ -427,6 +427,11 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
             file_size = ph->p_filesz; /* Size of the allocated data */
             data_offset = ph->p_offset; /* Offset where the data is located */
 
+            if (file_size > mem_size) {
+                ret = ELF_LOAD_TOO_BIG;
+                goto fail;
+            }
+
             if (file_size > 0) {
                 if (g_mapped_file_get_length(mapped_file) <
                     file_size + data_offset) {
Signature · 1 line
2.47.3
Reviewed-by on v2 1/1
#1 lore raw
Quoted 29 lines · Alex Bennée

According to the ELF spec:

PT_LOAD

The array element specifies a loadable segment, described by p_filesz and p_memsz. The bytes from the file are mapped to the beginning of the memory segment. If the segment's memory size (p_memsz) is larger than the file size (p_filesz), the ``extra'' bytes are defined to hold the value 0 and to follow the segment's initialized area. The file size may not be larger than the memory size. Loadable segment entries in the program header table appear in ascending order, sorted on the p_vaddr member.

which implies while both p_filesz and p_memsz can be zero we should never see a case where p_filesz is greater than the in memory size. Indeed it has been reported such a hand crafted ELF can blow up, for example during rom_reset():

   address_space_set(rom->as, rom->addr + rom->datasize, 0,
                     rom->romsize - rom->datasize,
                     MEMTXATTRS_UNSPECIFIED);

which could trigger and underflow leaving QEMU slowly filling a very large buffer.

Quoted 7 lines

---
v2
- ret = ELF_LOAD_TOO_BIG
- tweak subject

---
  include/hw/elf_ops.h.inc | 5 +++++
  1 file changed, 5 insertions(+)

r~

Reviewed-by on v2 1/1
#2 lore raw
In reply to Alex Bennée
Quoted 29 lines · Alex Bennée

According to the ELF spec:

PT_LOAD

The array element specifies a loadable segment, described by p_filesz and p_memsz. The bytes from the file are mapped to the beginning of the memory segment. If the segment's memory size (p_memsz) is larger than the file size (p_filesz), the ``extra'' bytes are defined to hold the value 0 and to follow the segment's initialized area. The file size may not be larger than the memory size. Loadable segment entries in the program header table appear in ascending order, sorted on the p_vaddr member.

which implies while both p_filesz and p_memsz can be zero we should never see a case where p_filesz is greater than the in memory size. Indeed it has been reported such a hand crafted ELF can blow up, for example during rom_reset():

   address_space_set(rom->as, rom->addr + rom->datasize, 0,
                     rom->romsize - rom->datasize,
                     MEMTXATTRS_UNSPECIFIED);

which could trigger and underflow leaving QEMU slowly filling a very large buffer.

Quoted 7 lines

---
v2
- ret = ELF_LOAD_TOO_BIG
- tweak subject

---
  include/hw/elf_ops.h.inc | 5 +++++
  1 file changed, 5 insertions(+)

and queued via hw-misc tree, thanks.