Fix illegal memory accesses in readelf when parsing a corrupt binary.
authorNick Clifton <nickc@redhat.com>
Fri, 17 Feb 2017 15:59:45 +0000 (15:59 +0000)
committerNick Clifton <nickc@redhat.com>
Fri, 17 Feb 2017 15:59:45 +0000 (15:59 +0000)
PR binutils/21156
* readelf.c (find_section_in_set): Test for invalid section
indicies.

binutils/ChangeLog
binutils/readelf.c

index 07c7aa964e811ac4593b295ebf7e81d6123f4718..9ae381fd81f9f4b72716fb37bf22005e53d9cee3 100644 (file)
@@ -1,3 +1,9 @@
+2017-02-17  Nick Clifton  <nickc@redhat.com>
+
+       PR binutils/21156
+       * readelf.c (find_section_in_set): Test for invalid section
+       indicies.
+
 2017-02-17  Nick Clifton  <nickc@redhat.com>
 
        * readelf.c (get_section_type_name): Add decoding of GNU section
index ea9da7afb799d18ef7535149c6620e0d457386ff..20df6f899609536adc50ca69fd4c3c84267c42f9 100644 (file)
@@ -676,8 +676,14 @@ find_section_in_set (const char * name, unsigned int * set)
   if (set != NULL)
     {
       while ((i = *set++) > 0)
-       if (streq (SECTION_NAME (section_headers + i), name))
-         return section_headers + i;
+       {
+         /* See PR 21156 for a reproducer.  */
+         if (i >= elf_header.e_shnum)
+           continue; /* FIXME: Should we issue an error message ?  */
+
+         if (streq (SECTION_NAME (section_headers + i), name))
+           return section_headers + i;
+       }
     }
 
   return find_section (name);