This is the mail archive of the binutils-cvs@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[binutils-gdb] readelf: Fix overlarge memory allocation when reading a binary with an excessive number of program h


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=82156ab704b08b124d319c0decdbd48b3ca2dac5

commit 82156ab704b08b124d319c0decdbd48b3ca2dac5
Author: Nick Clifton <nickc@redhat.com>
Date:   Mon Apr 3 12:14:06 2017 +0100

    readelf: Fix overlarge memory allocation when reading a binary with an excessive number of program headers.
    
    	PR binutils/21345
    	* readelf.c (get_program_headers): Check for there being too many
    	program headers before attempting to allocate space for them.

Diff:
---
 binutils/ChangeLog |  6 ++++++
 binutils/readelf.c | 17 ++++++++++++++---
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/binutils/ChangeLog b/binutils/ChangeLog
index 438ea7f..5d81b35 100644
--- a/binutils/ChangeLog
+++ b/binutils/ChangeLog
@@ -1,5 +1,11 @@
 2017-04-03  Nick Clifton  <nickc@redhat.com>
 
+	PR binutils/21345
+	* readelf.c (get_program_headers): Check for there being too many
+	program headers before attempting to allocate space for them.
+
+2017-04-03  Nick Clifton  <nickc@redhat.com>
+
 	PR binutils/21344
 	* readelf.c (process_mips_specific): Check for an out of range GOT
 	entry before reading the module pointer.
diff --git a/binutils/readelf.c b/binutils/readelf.c
index 3665221..b573921 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -4794,9 +4794,19 @@ get_program_headers (FILE * file)
   if (program_headers != NULL)
     return TRUE;
 
-  phdrs = (Elf_Internal_Phdr *) cmalloc (elf_header.e_phnum,
-                                         sizeof (Elf_Internal_Phdr));
+  /* Be kind to memory checkers by looking for
+     e_phnum values which we know must be invalid.  */
+  if (elf_header.e_phnum
+      * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
+      >= current_file_size)
+    {
+      error (_("Too many program headers - %#x - the file is not that big\n"),
+	     elf_header.e_phnum);
+      return FALSE;
+    }
 
+  phdrs = (Elf_Internal_Phdr *) cmalloc (elf_header.e_phnum,
+					 sizeof (Elf_Internal_Phdr));
   if (phdrs == NULL)
     {
       error (_("Out of memory reading %u program headers\n"),
@@ -15470,7 +15480,8 @@ process_mips_specific (FILE * file)
 	  /* PR 21344 */
 	  if (data + ent - pltgot > data_end - addr_size)
 	    {
-	      error (_("Invalid got entry - %#lx - overflows GOT table\n"), ent);
+	      error (_("Invalid got entry - %#lx - overflows GOT table\n"),
+		     (long) ent);
 	      goto got_print_fail;
 	    }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]