Fix address violations when parsing a corrupt DWARF linenumber table.
authorNick Clifton <nickc@redhat.com>
Tue, 26 Sep 2017 11:14:42 +0000 (12:14 +0100)
committerNick Clifton <nickc@redhat.com>
Tue, 26 Sep 2017 11:14:42 +0000 (12:14 +0100)
PR 22154
* dwarf.c (get_line_filename_and_dirname): Add extra checks for
buffer overruns.

binutils/ChangeLog
binutils/dwarf.c

index 80fff9ccd14bf0a7b40dfb5f9a141ac11041da0c..dc4faf553f0830e1bdf3680b8156375848857dc3 100644 (file)
@@ -1,3 +1,9 @@
+2017-09-26  Nick Clifton  <nickc@redhat.com>
+
+       PR 22154
+       * dwarf.c (get_line_filename_and_dirname): Add extra checks for
+       buffer overruns.
+
 2017-09-26  Nick Clifton  <nickc@redhat.com>
 
        * README-how-to-make-a-release: New file.
index d4156e480ad8751d22bf5d3358b1df8ca49813b4..edc65aa39aa1ad0492df5330818f0b6460715565 100644 (file)
@@ -4742,13 +4742,21 @@ get_line_filename_and_dirname (dwarf_vma line_offset,
     return NULL;
 
   hdrptr += opcode_base - 1;
+  if (hdrptr >= end)
+    return NULL;
+
   dirtable = hdrptr;
   /* Skip over dirname table.  */
   while (*hdrptr != '\0')
-    hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
+    {
+      hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
+      if (hdrptr >= end)
+       return NULL;
+    }
   hdrptr++;                /* Skip the NUL at the end of the table.  */
+
   /* Now skip over preceding filename table entries.  */
-  for (; *hdrptr != '\0' && fileidx > 1; fileidx--)
+  for (; hdrptr < end && *hdrptr != '\0' && fileidx > 1; fileidx--)
     {
       hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
       read_uleb128 (hdrptr, &bytes_read, end);
@@ -4758,16 +4766,19 @@ get_line_filename_and_dirname (dwarf_vma line_offset,
       read_uleb128 (hdrptr, &bytes_read, end);
       hdrptr += bytes_read;
     }
-  if (hdrptr == end || *hdrptr == '\0')
+  if (hdrptr >= end || *hdrptr == '\0')
     return NULL;
+
   file_name = hdrptr;
   hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
+  if (hdrptr >= end)
+    return NULL;
   diridx = read_uleb128 (hdrptr, &bytes_read, end);
   if (diridx == 0)
     return file_name;
-  for (; *dirtable != '\0' && diridx > 1; diridx--)
+  for (; dirtable < end && *dirtable != '\0' && diridx > 1; diridx--)
     dirtable += strnlen ((char *) dirtable, end - dirtable) + 1;
-  if (*dirtable == '\0')
+  if (dirtable >= end || *dirtable == '\0')
     return NULL;
   *dir_name = dirtable;
   return file_name;
This page took 0.031857 seconds and 4 git commands to generate.