+2014-11-16 Mark Wielaard <mjw@redhat.com>
+
+ * elf32_getphdr.c (getphdr_wrlock): Check e_phoff isn't zero.
+ Check for too many pheaders.
+ * elf_getphdrnum.c (__elf_getphdrnum_rdlock): Check section zero
+ actually exists before handling PN_XNUM.
+
2014-11-16 Mark Wielaard <mjw@redhat.com>
* gelf_getnote.c (gelf_getnote): Check padding overflow.
size_t phnum;
if (__elf_getphdrnum_rdlock (elf, &phnum) != 0)
goto out;
- if (phnum == 0)
+ if (phnum == 0 || ehdr->e_phoff == 0)
{
__libelf_seterrno (ELF_E_NO_PHDR);
goto out;
}
+ /* Check this doesn't overflow. */
size_t size = phnum * sizeof (ElfW2(LIBELFBITS,Phdr));
- if (ehdr->e_phoff > elf->maximum_size
+ if (phnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))
+ || ehdr->e_phoff > elf->maximum_size
|| elf->maximum_size - ehdr->e_phoff < size)
{
__libelf_seterrno (ELF_E_INVALID_DATA);
/* Return number of program headers in the ELF file.
- Copyright (C) 2010 Red Hat, Inc.
+ Copyright (C) 2010, 2014 Red Hat, Inc.
This file is part of elfutils.
This file is free software; you can redistribute it and/or modify
/* If there are no section headers, perhaps this is really just 65536
written without PN_XNUM support. Either that or it's bad data. */
- if (likely (scns->cnt > 0))
- *dst = (elf->class == ELFCLASS32
- ? scns->data[0].shdr.e32->sh_info
- : scns->data[0].shdr.e64->sh_info);
+ if (elf->class == ELFCLASS32)
+ {
+ if (likely (scns->cnt > 0
+ && elf->state.elf32.scns.data[0].shdr.e32 != NULL))
+ *dst = scns->data[0].shdr.e32->sh_info;
+ }
+ else
+ {
+ if (likely (scns->cnt > 0
+ && elf->state.elf64.scns.data[0].shdr.e64 != NULL))
+ *dst = scns->data[0].shdr.e64->sh_info;
+ }
}
return 0;
+2014-11-16 Mark Wielaard <mjw@redhat.com>
+
+ * readelf (process_elf_file): Set phnum to zero if there aren't
+ actually any pheaders.
+ (print_phdr): Check there actually is a phdr.
+
2014-11-16 Mark Wielaard <mjw@redhat.com>
* readelf.c (print_cfa_program): Check block len before calling
gettext ("cannot determine number of program headers: %s"),
elf_errmsg (-1));
+ /* If there isn't actually a program header then set phnum to zero.
+ Don't do any extra work. gelf_getphdr will always return NULL. */
+ if (ehdr->e_phoff == 0)
+ phnum = 0;
+
/* For an ET_REL file, libdwfl has adjusted the in-core shdrs
and may have applied relocation to some sections.
So we need to get a fresh Elf handle on the file to display those. */
static void
print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
{
- if (ehdr->e_phnum == 0)
+ if (ehdr->e_phnum == 0 || ehdr->e_phoff == 0)
/* No program header, this is OK in relocatable objects. */
return;