readelf PT_PHDR check
authorAlan Modra <amodra@gmail.com>
Fri, 25 Oct 2019 01:51:35 +0000 (12:21 +1030)
committerAlan Modra <amodra@gmail.com>
Fri, 25 Oct 2019 02:59:44 +0000 (13:29 +1030)
When PT_PHDR isn't covered by a PT_LOAD header, p_vaddr in PT_PHDR
isn't valid but the value might just pass a vaddr test.  So test
p_offset as well.

* readelf.c (process_program_headers): Check PT_PHDR p_offset
as well as p_vaddr.  Use p_filesz, not p_memsz, in vaddr test.

binutils/ChangeLog
binutils/readelf.c

index 74a223bfc845499bd13cd799523094cf93078287..f1b3c0e72cdefab38fb9b9eb90c223c0f0a5b5c5 100644 (file)
@@ -1,3 +1,8 @@
+2019-10-25  Alan Modra  <amodra@gmail.com>
+
+       * readelf.c (process_program_headers): Check PT_PHDR p_offset
+       as well as p_vaddr.  Use p_filesz, not p_memsz, in vaddr test.
+
 2019-10-21  Alan Modra  <amodra@gmail.com>
 
        PR 452
index de77237e0ec9ffddf22437b644f8b81407ce1f14..370bc4c1b7d9483b6ce9335171ae19a6feb29f8e 100644 (file)
@@ -5262,11 +5262,17 @@ process_program_headers (Filedata * filedata)
              unsigned int j;
 
              for (j = 1; j < filedata->file_header.e_phnum; j++)
-               if (filedata->program_headers[j].p_vaddr <= segment->p_vaddr
-                   && (filedata->program_headers[j].p_vaddr
-                       + filedata->program_headers[j].p_memsz)
-                   >= (segment->p_vaddr + segment->p_filesz))
-                 break;
+               {
+                 Elf_Internal_Phdr *load = filedata->program_headers + j;
+                 if (load->p_type == PT_LOAD
+                     && load->p_offset <= segment->p_offset
+                     && (load->p_offset + load->p_filesz
+                         >= segment->p_offset + segment->p_filesz)
+                     && load->p_vaddr <= segment->p_vaddr
+                     && (load->p_vaddr + load->p_filesz
+                         >= segment->p_vaddr + segment->p_filesz))
+                   break;
+               }
              if (j == filedata->file_header.e_phnum)
                error (_("the PHDR segment is not covered by a LOAD segment\n"));
            }