]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
readelf: More sanity checks before trying to display interpreter string.
authorMark Wielaard <mjw@redhat.com>
Mon, 24 Feb 2014 16:44:42 +0000 (17:44 +0100)
committerMark Wielaard <mjw@redhat.com>
Mon, 24 Feb 2014 16:44:42 +0000 (17:44 +0100)
Check there is a SHT_PROGBITS section at the offset given by p_offsets for
a PT_INTERP segment before trying to display the interpreter string.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
src/ChangeLog
src/readelf.c

index ad3b2b1356c4874ad8a3676dca4f72fc412f8740..80be466a2e638e5f6ba075f65844f7bea4ea8e13 100644 (file)
@@ -1,3 +1,9 @@
+2014-02-24  Mark Wielaard  <mjw@redhat.com>
+
+       * readelf (print_phdr): Check there is a SHT_PROGBITS section at the
+       offset given by p_offsets for a PT_INTERP segment before trying to
+       display the interpreter string.
+
 2014-02-07  Mark Wielaard  <mjw@redhat.com>
 
        * readelf.c (print_phdr): Check phdr->p_filesz and make sure
index fb9546359ec57e0dc728607a4851e3b97567e3aa..63675c6022dedd4af5f72225e8a8a038f68fabec 100644 (file)
@@ -1187,11 +1187,25 @@ print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
 
       if (phdr->p_type == PT_INTERP)
        {
-         /* We can show the user the name of the interpreter.  */
+         /* If we are sure the file offset is valid then we can show
+            the user the name of the interpreter.  We check whether
+            there is a section at the file offset.  Normally there
+            would be a section called ".interp".  But in separate
+            .debug files it is a NOBITS section (and so doesn't match
+            with gelf_offscn).  Which probably means the offset is
+            not valid another reason could be because the ELF file
+            just doesn't contain any section headers, in that case
+            just play it safe and don't display anything.  */
+
+         Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
+         GElf_Shdr shdr_mem;
+         GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
+
          size_t maxsize;
          char *filedata = elf_rawfile (ebl->elf, &maxsize);
 
-         if (filedata != NULL && phdr->p_offset < maxsize
+         if (shdr != NULL && shdr->sh_type == SHT_PROGBITS
+             && filedata != NULL && phdr->p_offset < maxsize
              && phdr->p_filesz <= maxsize - phdr->p_offset
              && memchr (filedata + phdr->p_offset, '\0',
                         phdr->p_filesz) != NULL)