94084c
commit cf8c6a634c0a04a9f5d198ef05310f85f7338839
94084c
Author: Florian Weimer <fweimer@redhat.com>
94084c
Date:   Fri Nov 5 17:01:24 2021 +0100
94084c
94084c
    elf: Earlier missing dynamic segment check in _dl_map_object_from_fd
94084c
    
94084c
    Separated debuginfo files have PT_DYNAMIC with p_filesz == 0.  We
94084c
    need to check for that before the _dl_map_segments call because
94084c
    that could attempt to write to mappings that extend beyond the end
94084c
    of the file, resulting in SIGBUS.
94084c
    
94084c
    Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
94084c
    (cherry picked from commit ea32ec354c65ddad11b82ca9d057010df13a9cea)
94084c
94084c
diff --git a/elf/dl-load.c b/elf/dl-load.c
94084c
index 4445c28ef3fb4a7e..0976977fbdf21902 100644
94084c
--- a/elf/dl-load.c
94084c
+++ b/elf/dl-load.c
94084c
@@ -1130,6 +1130,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
94084c
     struct loadcmd loadcmds[l->l_phnum];
94084c
     size_t nloadcmds = 0;
94084c
     bool has_holes = false;
94084c
+    bool empty_dynamic = false;
94084c
 
94084c
     /* The struct is initialized to zero so this is not necessary:
94084c
     l->l_ld = 0;
94084c
@@ -1142,7 +1143,9 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
94084c
 	     segments are mapped in.  We record the addresses it says
94084c
 	     verbatim, and later correct for the run-time load address.  */
94084c
 	case PT_DYNAMIC:
94084c
-	  if (ph->p_filesz)
94084c
+	  if (ph->p_filesz == 0)
94084c
+	    empty_dynamic = true; /* Usually separate debuginfo.  */
94084c
+	  else
94084c
 	    {
94084c
 	      /* Debuginfo only files from "objcopy --only-keep-debug"
94084c
 		 contain a PT_DYNAMIC segment with p_filesz == 0.  Skip
94084c
@@ -1265,6 +1268,13 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
94084c
 	goto lose;
94084c
       }
94084c
 
94084c
+    /* This check recognizes most separate debuginfo files.  */
94084c
+    if (__glibc_unlikely ((l->l_ld == 0 && type == ET_DYN) || empty_dynamic))
94084c
+      {
94084c
+	errstring = N_("object file has no dynamic section");
94084c
+	goto lose;
94084c
+      }
94084c
+
94084c
     /* Length of the sections to be loaded.  */
94084c
     maplength = loadcmds[nloadcmds - 1].allocend - loadcmds[0].mapstart;
94084c
 
94084c
@@ -1282,15 +1292,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
94084c
       }
94084c
   }
94084c
 
94084c
-  if (l->l_ld == 0)
94084c
-    {
94084c
-      if (__glibc_unlikely (type == ET_DYN))
94084c
-	{
94084c
-	  errstring = N_("object file has no dynamic section");
94084c
-	  goto lose;
94084c
-	}
94084c
-    }
94084c
-  else
94084c
+  if (l->l_ld != 0)
94084c
     l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr);
94084c
 
94084c
   elf_get_dynamic_info (l);