[PATCH] bfd: fix out-of-bounds access in stabs linking and remote memory reads

mustafa mustafa@medios.digiscrypt.com
Wed Nov 18 09:33:13 GMT 2026


Two places in bfd trust offsets from the input without checking them.

_bfd_link_section_stabs, when it looks ahead after an N_BINCL, builds
a pointer from stabstrbuf, the running string offset and the n_strx of
the N_BINCL entry and reads it up to a NUL.  The check that is done
for the entries in the main loop is missing here, so a large n_strx
makes ld read far outside the .stabstr buffer and crash or copy data
it should not see.  Reject the entry with "stabs entry has invalid
string index" as the main loop does.

bfd_from_remote_memory computes segment and section header ends with
unchecked 64-bit additions (p_offset + p_filesz, e_shoff +
e_shnum * e_shentsize, and the page rounding of the segment end).  A
wrapped value can make a later "start <= end" assumption false, so
target_read_memory gets a huge length and writes past the buffer that
was allocated for the image.  Also, when the section headers fit in
the memory that has been read, high_offset was set to size even if
that was smaller than the end of an earlier segment, which has the
same effect.  Fail with bfd_error_wrong_format when an addition
overflows, never lower high_offset, and check that each segment is
inside high_offset before reading it.

	* stabs.c (_bfd_link_section_stabs): Check the string index of
	the N_BINCL look-ahead against the size of the string table.
	* elfcode.h (bfd_from_remote_memory): Check p_offset + p_filesz,
	e_shoff + e_shnum * e_shentsize and the page rounding for
	overflow.  Do not lower high_offset.  Check that start and end
	are within high_offset before each read.

Signed-off-by: Mohammed Mustafa <mustafa@medios.digiscrypt.com>
---
 bfd/elfcode.h | 37 +++++++++++++++++++++++++++++++++----
 bfd/stabs.c   | 16 +++++++++++++---
 2 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/bfd/elfcode.h b/bfd/elfcode.h
index 8d919318cb8..3d09120d856 100644
--- a/bfd/elfcode.h
+++ b/bfd/elfcode.h
@@ -1886,7 +1886,15 @@ NAME(_bfd_elf,bfd_from_remote_memory)
       elf_swap_phdr_in (templ, &x_phdrs[i], &i_phdrs[i]);
       if (i_phdrs[i].p_type == PT_LOAD)
 	{
-	  bfd_vma segment_end = i_phdrs[i].p_offset + i_phdrs[i].p_filesz;
+	  bfd_vma segment_end;
+
+	  if (__builtin_add_overflow (i_phdrs[i].p_offset, i_phdrs[i].p_filesz,
+				      &segment_end))
+	    {
+	      free (x_phdrs);
+	      bfd_set_error (bfd_error_wrong_format);
+	      return NULL;
+	    }

 	  if (segment_end > high_offset)
 	    {
@@ -1925,7 +1933,14 @@ NAME(_bfd_elf,bfd_from_remote_memory)
   shdr_end = 0;
   if (i_ehdr.e_shoff != 0 && i_ehdr.e_shnum != 0 && i_ehdr.e_shentsize != 0)
     {
-      shdr_end = i_ehdr.e_shoff + i_ehdr.e_shnum * i_ehdr.e_shentsize;
+      if (__builtin_add_overflow (i_ehdr.e_shoff,
+				  (bfd_vma) i_ehdr.e_shnum * i_ehdr.e_shentsize,
+				  &shdr_end))
+	{
+	  free (x_phdrs);
+	  bfd_set_error (bfd_error_wrong_format);
+	  return NULL;
+	}

       if (last_phdr->p_filesz != last_phdr->p_memsz)
 	{
@@ -1934,7 +1949,10 @@ NAME(_bfd_elf,bfd_from_remote_memory)
 	     headers.  */
 	}
       else if (size >= shdr_end)
-	high_offset = size;
+	{
+	  if (size > high_offset)
+	    high_offset = size;
+	}
       else
 	{
 	  bfd_vma page_size = get_elf_backend_data (templ)->minpagesize;
@@ -1944,7 +1962,11 @@ NAME(_bfd_elf,bfd_from_remote_memory)
 	     section headers.  */
 	  if (page_size > 1 && shdr_end > segment_end)
 	    {
-	      bfd_vma page_end = (segment_end + page_size - 1) & -page_size;
+	      bfd_vma page_end;
+
+	      if (__builtin_add_overflow (segment_end, page_size - 1, &page_end))
+		page_end = 0;
+	      page_end &= -page_size;

 	      if (page_end >= shdr_end)
 		/* Whee, section headers covered.  */
@@ -1979,6 +2001,13 @@ NAME(_bfd_elf,bfd_from_remote_memory)
 	/* Extend the end of the last pt_load to cover section headers.  */
 	if (last_phdr == &i_phdrs[i])
 	  end = high_offset;
+	if (start > end || end > high_offset)
+	  {
+	    free (x_phdrs);
+	    free (contents);
+	    bfd_set_error (bfd_error_wrong_format);
+	    return NULL;
+	  }
 	err = target_read_memory (loadbase + vaddr / opb,
 				  contents + start, end - start);
 	if (err)
diff --git a/bfd/stabs.c b/bfd/stabs.c
index 5131b62875b..b813a6fa0e0 100644
--- a/bfd/stabs.c
+++ b/bfd/stabs.c
@@ -357,10 +357,20 @@ _bfd_link_section_stabs (bfd *abfd,
 	      else if (nest == 0)
 		{
 		  const char *str;
+		  bfd_size_type incl_stroff;

-		  str = ((char *) stabstrbuf
-			 + stroff
-			 + bfd_get_32 (abfd, incl_sym + STRDXOFF));
+		  incl_stroff = stroff + bfd_get_32 (abfd, incl_sym + STRDXOFF);
+		  if (incl_stroff >= stabstrsec->size)
+		    {
+		      _bfd_error_handler
+			/* xgettext:c-format */
+			(_("%pB(%pA+%#lx): stabs entry has invalid string index"),
+			 abfd, stabsec, (long) (incl_sym - stabbuf));
+		      bfd_set_error (bfd_error_bad_value);
+		      free (symb);
+		      goto error_return;
+		    }
+		  str = (char *) stabstrbuf + incl_stroff;
 		  for (; *str != '\0'; str++)
 		    {
 		      if (num_chars >= buf_len)
-- 
2.53.0


More information about the Binutils mailing list