Blame SOURCES/gdb-rhbz1742099-reject-sections-with-invalid-sizes.patch

501b51
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
501b51
From: Keith Seitz <keiths@redhat.com>
501b51
Date: Thu, 17 Oct 2019 09:44:15 -0700
501b51
Subject: gdb-rhbz1742099-reject-sections-with-invalid-sizes.patch
501b51
MIME-Version: 1.0
501b51
Content-Type: text/plain; charset=UTF-8
501b51
Content-Transfer-Encoding: 8bit
501b51
501b51
;; Prevent buffer overflow with sections with invalid sizes.
501b51
;; Keith Seitz, RH BZ 1740299.
501b51
501b51
DWARF reader: Reject sections with invalid sizes
501b51
501b51
This is another fuzzer bug, gdb/23567.  This time, the fuzzer has
501b51
specifically altered the size of .debug_str:
501b51
501b51
$ eu-readelf -S objdump
501b51
Section Headers:
501b51
[Nr] Name                 Type         Addr             Off      Size     ES Flags Lk Inf Al
501b51
[31] .debug_str           PROGBITS     0000000000000000 0057116d ffffffffffffffff  1 MS     0   0  1
501b51
501b51
When this file is loaded into GDB, the DWARF reader crashes attempting
501b51
to access the string table (or it may just store a bunch of nonsense):
501b51
501b51
[gdb-8.3-6-fc30]
501b51
$ gdb -nx -q objdump
501b51
BFD: warning: /path/to/objdump has a corrupt section with a size (ffffffffffffffff) larger than the file size
501b51
Reading symbols from /path/to/objdump...
501b51
Segmentation fault (core dumped)
501b51
501b51
Nick has already committed a BFD patch to issue the warning seen above.
501b51
501b51
[gdb master 6acc1a0b]
501b51
$ gdb -BFD: warning: /path/to/objdump has a corrupt section with a size (ffffffffffffffff) larger than the file size
501b51
Reading symbols from /path/to/objdump...
501b51
(gdb) inf func
501b51
All defined functions:
501b51
501b51
File ./../include/dwarf2.def:
501b51
186:    const
501b51
501b51
              8 *>(.:
501b51
                     ;'@�B);
501b51
747:    const
501b51
501b51
              8 *�(.:
501b51
                     ;'@�B);
501b51
701:    const
501b51
501b51
              8 *�D �
501b51
                     (.:
501b51
                        ;'@�B);
501b51
71:     const
501b51
501b51
              8 *(.:
501b51
                    ;'@�B);
501b51
/* and more gibberish  */
501b51
501b51
Consider read_indirect_string_at_offset_from:
501b51
501b51
static const char *
501b51
read_indirect_string_at_offset_from (struct objfile *objfile,
501b51
                                     bfd *abfd, LONGEST str_offset,
501b51
                                     struct dwarf2_section_info *sect,
501b51
                                     const char *form_name,
501b51
                                     const char *sect_name)
501b51
{
501b51
  dwarf2_read_section (objfile, sect);
501b51
  if (sect->buffer == NULL)
501b51
    error (_("%s used without %s section [in module %s]"),
501b51
           form_name, sect_name, bfd_get_filename (abfd));
501b51
  if (str_offset >= sect->size)
501b51
    error (_("%s pointing outside of %s section [in module %s]"),
501b51
           form_name, sect_name, bfd_get_filename (abfd));
501b51
  gdb_assert (HOST_CHAR_BIT == 8);
501b51
  if (sect->buffer[str_offset] == '\0')
501b51
    return NULL;
501b51
  return (const char *) (sect->buffer + str_offset);
501b51
}
501b51
501b51
With sect_size being ginormous, the code attempts to access
501b51
sect->buffer[GINORMOUS], and depending on the layout of memory,
501b51
GDB either stores a bunch of gibberish strings or crashes.
501b51
501b51
This is an attempt to mitigate this by implementing a similar approach
501b51
used by BFD. In our case, we simply reject the section with the invalid
501b51
length:
501b51
501b51
$ ./gdb -nx -q objdump
501b51
BFD: warning: /path/to/objdump has a corrupt section with a size (ffffffffffffffff) larger than the file size
501b51
Reading symbols from /path/to/objdump...
501b51
501b51
warning: Discarding section .debug_str which has a section size (ffffffffffffffff) larger than the file size [in module /path/to/objdump]
501b51
DW_FORM_strp used without .debug_str section [in module /path/to/objdump]
501b51
(No debugging symbols found in /path/to/objdump)
501b51
(gdb)
501b51
501b51
Unfortunately, I have not found a way to regression test this, since it
501b51
requires poking ELF section headers.
501b51
501b51
gdb/ChangeLog:
501b51
2019-10-16  Keith Seitz  <keiths@redhat.com>
501b51
501b51
        PR gdb/23567
501b51
        * dwarf2read.c (dwarf2_per_objfile::locate_sections): Discard
501b51
        sections whose size is greater than the file size.
501b51
501b51
Change-Id: I896ac3b4eb2207c54e8e05c16beab3051d9b4b2f
501b51
501b51
diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
501b51
--- a/gdb/dwarf2read.c
501b51
+++ b/gdb/dwarf2read.c
501b51
@@ -2335,6 +2335,15 @@ dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
501b51
   if ((aflag & SEC_HAS_CONTENTS) == 0)
501b51
     {
501b51
     }
501b51
+  else if (elf_section_data (sectp)->this_hdr.sh_size
501b51
+	   > bfd_get_file_size (abfd))
501b51
+    {
501b51
+      bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
501b51
+      warning (_("Discarding section %s which has a section size (%s"
501b51
+		 ") larger than the file size [in module %s]"),
501b51
+	       bfd_section_name (abfd, sectp), phex_nz (size, sizeof (size)),
501b51
+	       bfd_get_filename (abfd));
501b51
+    }
501b51
   else if (section_is_p (sectp->name, &names.info))
501b51
     {
501b51
       this->info.s.section = sectp;