Blame SOURCES/binutils-CVE-2021-3487.patch

2e9b04
--- binutils.orig/bfd/dwarf2.c	2021-04-09 16:59:18.345187116 +0100
2e9b04
+++ binutils-2.35/bfd/dwarf2.c	2021-04-09 17:02:03.614064723 +0100
2e9b04
@@ -539,6 +539,8 @@ read_section (bfd *	      abfd,
2e9b04
   /* The section may have already been read.  */
2e9b04
   if (contents == NULL)
2e9b04
     {
2e9b04
+      ufile_ptr  filesize;
2e9b04
+
2e9b04
       msec = bfd_get_section_by_name (abfd, section_name);
2e9b04
       if (! msec)
2e9b04
 	{
2e9b04
@@ -554,10 +556,20 @@ read_section (bfd *	      abfd,
2e9b04
 	  return FALSE;
2e9b04
 	}
2e9b04
 
2e9b04
-      *section_size = msec->rawsize ? msec->rawsize : msec->size;
2e9b04
+      amt = bfd_get_section_limit_octets (abfd, msec);
2e9b04
+      filesize = bfd_get_file_size (abfd);
2e9b04
+      if (amt >= filesize)
2e9b04
+       {
2e9b04
+         /* PR 26946 */
2e9b04
+         _bfd_error_handler (_("DWARF error: section %s is larger than its filesize! (0x%lx vs 0x%lx)"),
2e9b04
+                             section_name, (long) amt, (long) filesize);
2e9b04
+         bfd_set_error (bfd_error_bad_value);
2e9b04
+         return FALSE;
2e9b04
+       }
2e9b04
+      *section_size = amt;
2e9b04
       /* Paranoia - alloc one extra so that we can make sure a string
2e9b04
 	 section is NUL terminated.  */
2e9b04
-      amt = *section_size + 1;
2e9b04
+      amt += 1;
2e9b04
       if (amt == 0)
2e9b04
 	{
2e9b04
 	  bfd_set_error (bfd_error_no_memory);