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

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