[PATCH] readelf: fix SHT_GROUP, DT_RELR and string dump bounds

mustafa mustafa@medios.digiscrypt.com
Thu Nov 19 11:38:02 GMT 2026


Three separate problems in readelf, all reachable from a crafted file.

process_section_groups divides sh_size by sh_entsize without checking
for zero, and then reads the flag word and the member list as 4 byte
values even when sh_size is 1 to 3 bytes or sh_entsize is larger than
4.  Reject a zero sh_entsize or a section smaller than 4 bytes, and
do not count more members than fit in sh_size / 4.  Normal readelf
runs already replace a zero sh_entsize when the section headers are
loaded, so the division by zero was not reachable that way, but the
function should not depend on it.

When dumping DT_RELR relocations with -D -r --got-contents, the
buffer for the relocations was sized only if no relocations had been
counted before.  If DT_REL or DT_RELA came first, the RELR entries
were written into a buffer that was sized for those alone, which
overflows the heap.  Always reserve the slots, and use xcmalloc and
xcrealloc in update_all_relocations so that the size computation is
checked.

dump_section_as_strings loops on "while (maxlen)" but never changes
maxlen, so the loop only ends at a NUL byte.  Decompressed sections
(-z -p) have no terminating NUL, so the loop runs past the end of the
buffer.  Stop at the end of the data, check that there is data left
before looking at the byte after a newline, and give mbrtowc no more
bytes than remain.

	* readelf.c (update_all_relocations): Use xcmalloc and
	xcrealloc.
	(process_section_groups): Reject zero sh_entsize and sh_size
	below 4.  Limit the number of members to sh_size / 4.
	(process_relocs): Always count and reserve DT_RELR relocations.
	(dump_section_as_strings): Stop at the end of the section data.
	Bound the mbrtowc length.

Signed-off-by: Mohammed Mustafa <mustafa@medios.digiscrypt.com>
---
 binutils/readelf.c | 75 +++++++++++++++++++++++++++-------------------
 1 file changed, 44 insertions(+), 31 deletions(-)

diff --git a/binutils/readelf.c b/binutils/readelf.c
index 42734f621c5..a4612c3993a 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -1847,23 +1847,21 @@ symcmp (const void *p, const void *q)
 static void
 update_all_relocations (size_t nentries)
 {
-  size_t sz;
-
   if (nentries == 0 || !do_got_section_contents)
     return;

   if (!all_relocations_root)
     {
-      sz = nentries * sizeof (elf_relocation);
-      all_relocations_root = xmalloc (sz);
+      all_relocations_root = xcmalloc (nentries, sizeof (elf_relocation));
       all_relocations = all_relocations_root;
       all_relocations_count = nentries;
     }
   else
     {
       size_t orig_count = all_relocations_count;
-      sz = (orig_count + nentries) * sizeof (elf_relocation);
-      all_relocations_root = xrealloc (all_relocations_root, sz);
+      all_relocations_root = xcrealloc (all_relocations_root,
+					orig_count + nentries,
+					sizeof (elf_relocation));
       all_relocations = all_relocations_root + orig_count;
       all_relocations_count += nentries;
     }
@@ -9158,6 +9156,7 @@ process_section_groups (Filedata * filedata)
 	  unsigned char * start;
 	  unsigned char * indices;
 	  unsigned int entry, j, size;
+	  uint64_t nmembers;
 	  Elf_Internal_Shdr * sec;
 	  Elf_Internal_Sym * sym;

@@ -9244,6 +9243,16 @@ process_section_groups (Filedata * filedata)
 	      continue;
 	    }

+	  if (section->sh_entsize == 0 || section->sh_size < 4)
+	    {
+	      error (_("Section %s has invalid sh_entsize (%#" PRIx64 ")"
+		       " or sh_size (%#" PRIx64 ")\n"),
+		     printable_section_name (filedata, section),
+		     section->sh_entsize,
+		     section->sh_size);
+	      continue;
+	    }
+
 	  start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
                                               1, section->sh_size,
                                               _("section data"));
@@ -9251,7 +9260,11 @@ process_section_groups (Filedata * filedata)
 	    continue;

 	  indices = start;
-	  size = (section->sh_size / section->sh_entsize) - 1;
+	  /* Each member is a 4 byte word, whatever sh_entsize says.  */
+	  nmembers = section->sh_size / section->sh_entsize;
+	  if (nmembers > section->sh_size / 4)
+	    nmembers = section->sh_size / 4;
+	  size = nmembers - 1;
 	  entry = byte_get (indices, 4);
 	  indices += 4;

@@ -9772,27 +9785,24 @@ process_relocs (Filedata * filedata)

 	  if (rel_type == reltype_relr)
 	    {
-	      if (all_relocations_count == 0)
-		{
-		  /* Count DT_RELR relocations when "-D --got-contents"
-		     is passed to readelf.  */
-		  uint64_t num_reloc;
-		  uint64_t *relrs = NULL;
-		  Elf_Internal_Shdr section = {
-		    .sh_type = SHT_RELR,
-		    .sh_offset = filedata->dynamic_info[DT_RELR],
-		    .sh_size = rel_size,
-		    .sh_entsize = rel_entsz
-		  };
-		  validate_section_info (&section, NULL, 0, filedata);
-		  num_reloc = count_relr_relocations (filedata,
-						      &section,
-						      &relrs);
-		  free (relrs);
-		  if (num_reloc == 0)
-		    continue;
-		  update_all_relocations (num_reloc);
-		}
+	      /* Count DT_RELR relocations when "-D --got-contents"
+		 is passed to readelf.  */
+	      uint64_t num_reloc;
+	      uint64_t *relrs = NULL;
+	      Elf_Internal_Shdr section = {
+		.sh_type = SHT_RELR,
+		.sh_offset = filedata->dynamic_info[DT_RELR],
+		.sh_size = rel_size,
+		.sh_entsize = rel_entsz
+	      };
+	      validate_section_info (&section, NULL, 0, filedata);
+	      num_reloc = count_relr_relocations (filedata,
+						  &section,
+						  &relrs);
+	      free (relrs);
+	      if (num_reloc == 0)
+		continue;
+	      update_all_relocations (num_reloc);
 	      dump_relr_relocations (filedata,
 				     filedata->dynamic_info[DT_RELRSZ],
 				     filedata->dynamic_info[DT_RELRENT],
@@ -16866,7 +16876,7 @@ dump_section_as_strings (Elf_Internal_Shdr *
section, Filedata * filedata)
 	    {
 	      char c = 0;

-	      while (maxlen)
+	      while (data < end)
 		{
 		  c = *data++;

@@ -16877,7 +16887,7 @@ dump_section_as_strings (Elf_Internal_Shdr *
section, Filedata * filedata)
 		  if (c == '\n')
 		    {
 		      printf ("\\n\n");
-		      if (*data != 0)
+		      if (data < end && *data != 0)
 			continuing = true;
 		      break;
 		    }
@@ -16905,7 +16915,10 @@ dump_section_as_strings (Elf_Internal_Shdr *
section, Filedata * filedata)
 		      /* Try to find out how many bytes made up the character that was
 			 just printed.  Advance the symbol pointer past the bytes that
 			 were displayed.  */
-		      n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
+		      n = mbrtowc (& w, (char *)(data - 1),
+				   MB_CUR_MAX < (size_t) (end - (data - 1))
+				   ? MB_CUR_MAX : (size_t) (end - (data - 1)),
+				   & state);
 #else
 		      n = 1;
 #endif
-- 
2.53.0


More information about the Binutils mailing list