]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
libdwfl: Read no more than required in dwfl_segment_report_module
authorGavin Li <gavin@matician.com>
Wed, 30 Nov 2022 17:26:19 +0000 (18:26 +0100)
committerMark Wielaard <mark@klomp.org>
Tue, 13 Dec 2022 16:40:31 +0000 (17:40 +0100)
Since read_portion and the standard dwfl_elf_phdr_memory_callback
functions make sure to read at least minread bytes there is no need
for dwfl_segment_report_module to check and adjust the data to the
actual buffer size read. Reading beyond the end of the expected data
size (if the buffer read is much larger) actually causes issues when
passing the data to elfXX_xlatetom() because it is possible that
src->d_size is not a multiple of recsize (for ELF_T_DYN, recsize is 16
while the minimum required alignment is 8), causing elfXX_xlatetom()
to return ELF_E_INVALID_DATA.

Signed-off-by: Gavin Li <gavin@matician.com>
Signed-off-by: Mark Wielaard <mark@klomp.org>
libdwfl/ChangeLog
libdwfl/dwfl_segment_report_module.c

index 6dd84a6f83f18d73ae64c8e5c42ea59137815324..685273276a1c9b182289d4b075ff93161b5a36fc 100644 (file)
@@ -1,3 +1,9 @@
+2022-11-28  Gavin Li  <gavin@matician.com>
+           Mark Wielaard  <mark@klomp.org>
+
+       * dwfl_segment_report_module.c (dwfl_segment_report_module): Remove
+       data size check after read_portion memory_callback.
+
 2022-10-21  Yonggang Luo  <luoyonggang@gmail.com>
 
        * argp-std.c: Don't include unistd.h.
index 287fc002d8d3fda7d9926f6b3e01760c4e2e7f66..19fa6ded7ba9bf14267f397ba572ef746b2f7a9f 100644 (file)
@@ -441,17 +441,6 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
                    start + phoff, xlatefrom.d_size))
     goto out;
 
-  /* ph_buffer_size will be zero if we got everything from the initial
-     buffer, otherwise it will be the size of the new buffer that
-     could be read.  */
-  if (ph_buffer_size != 0)
-    {
-      phnum = ph_buffer_size / phentsize;
-      if (phnum == 0)
-       goto out;
-      xlatefrom.d_size = ph_buffer_size;
-    }
-
   xlatefrom.d_buf = ph_buffer;
 
   bool class32 = ei_class == ELFCLASS32;
@@ -533,18 +522,12 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
               /* We calculate from the p_offset of the note segment,
                because we don't yet know the bias for its p_vaddr.  */
               const GElf_Addr note_vaddr = start + offset;
-              void *data;
-              size_t data_size;
+              void *data = NULL;
+              size_t data_size = 0;
               if (read_portion (&read_state, &data, &data_size,
                                start, segment, note_vaddr, filesz))
                 continue; /* Next header */
 
-              /* data_size will be zero if we got everything from the initial
-                 buffer, otherwise it will be the size of the new buffer that
-                 could be read.  */
-              if (data_size != 0)
-                filesz = data_size;
-
              if (filesz > SIZE_MAX / sizeof (Elf32_Nhdr))
                continue;
 
@@ -821,12 +804,6 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
       && ! read_portion (&read_state, &dyn_data, &dyn_data_size,
                         start, segment, dyn_vaddr, dyn_filesz))
     {
-      /* dyn_data_size will be zero if we got everything from the initial
-         buffer, otherwise it will be the size of the new buffer that
-         could be read.  */
-      if (dyn_data_size != 0)
-       dyn_filesz = dyn_data_size;
-
       if ((dyn_filesz / dyn_entsize) == 0
          || dyn_filesz > (SIZE_MAX / dyn_entsize))
        goto out;