df07cd5
commit ea5a537e879bb667e03435a2308d915dc89448a6
df07cd5
Author: Carlos O'Donell <carlos@redhat.com>
df07cd5
Date:   Fri Mar 12 16:44:47 2021 +0100
df07cd5
df07cd5
    elf: Always set l in _dl_init_paths (bug 23462)
df07cd5
    
df07cd5
    After d1d5471579eb0426671bf94f2d71e61dfb204c30 ("Remove dead
df07cd5
    DL_DST_REQ_STATIC code.") we always setup the link map l to make the
df07cd5
    static and shared cases the same.  The bug is that in elf/dl-load.c
df07cd5
    (_dl_init_paths) we conditionally set l only in the #ifdef SHARED
df07cd5
    case, but unconditionally use it later.  The simple solution is to
df07cd5
    remove the #ifdef SHARED conditional, because it's no longer needed,
df07cd5
    and unconditionally setup l for both the static and shared cases. A
df07cd5
    regression test is added to run a static binary with
df07cd5
    LD_LIBRARY_PATH='$ORIGIN' which crashes before the fix and runs after
df07cd5
    the fix.
df07cd5
    
df07cd5
    Co-Authored-By: Florian Weimer <fweimer@redhat.com>
df07cd5
    (cherry picked from commit 332421312576bd7095e70589154af99b124dd2d1)
df07cd5
df07cd5
diff --git a/elf/Makefile b/elf/Makefile
df07cd5
index 7610fa080f3f4738..63da0ed64f6234a8 100644
df07cd5
--- a/elf/Makefile
df07cd5
+++ b/elf/Makefile
df07cd5
@@ -164,7 +164,8 @@ tests-static-normal := tst-leaks1-static tst-array1-static tst-array5-static \
df07cd5
 	       tst-dl-iter-static \
df07cd5
 	       tst-tlsalign-static tst-tlsalign-extern-static \
df07cd5
 	       tst-linkall-static tst-env-setuid tst-env-setuid-tunables \
df07cd5
-	       tst-single_threaded-static tst-single_threaded-pthread-static
df07cd5
+	       tst-single_threaded-static tst-single_threaded-pthread-static \
df07cd5
+	       tst-dst-static
df07cd5
 
df07cd5
 tests-static-internal := tst-tls1-static tst-tls2-static \
df07cd5
 	       tst-ptrguard1-static tst-stackguard1-static \
df07cd5
@@ -1905,3 +1906,5 @@ $(objpfx)list-tunables.out: tst-rtld-list-tunables.sh $(objpfx)ld.so
df07cd5
 	cmp tst-rtld-list-tunables.exp \
df07cd5
 	    $(objpfx)/tst-rtld-list-tunables.out > $@; \
df07cd5
 	$(evaluate-test)
df07cd5
+
df07cd5
+tst-dst-static-ENV = LD_LIBRARY_PATH='$$ORIGIN'
df07cd5
diff --git a/elf/dl-load.c b/elf/dl-load.c
df07cd5
index 9e2089cfaa7dd4b1..376a2e64d6a33535 100644
df07cd5
--- a/elf/dl-load.c
df07cd5
+++ b/elf/dl-load.c
df07cd5
@@ -758,50 +758,45 @@ _dl_init_paths (const char *llp, const char *source,
df07cd5
   max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
df07cd5
   *aelem = NULL;
df07cd5
 
df07cd5
-#ifdef SHARED
df07cd5
   /* This points to the map of the main object.  */
df07cd5
   l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
df07cd5
-  if (l != NULL)
df07cd5
+  assert (l->l_type != lt_loaded);
df07cd5
+
df07cd5
+  if (l->l_info[DT_RUNPATH])
df07cd5
+    {
df07cd5
+      /* Allocate room for the search path and fill in information
df07cd5
+	 from RUNPATH.  */
df07cd5
+      decompose_rpath (&l->l_runpath_dirs,
df07cd5
+		       (const void *) (D_PTR (l, l_info[DT_STRTAB])
df07cd5
+				       + l->l_info[DT_RUNPATH]->d_un.d_val),
df07cd5
+		       l, "RUNPATH");
df07cd5
+      /* During rtld init the memory is allocated by the stub malloc,
df07cd5
+	 prevent any attempt to free it by the normal malloc.  */
df07cd5
+      l->l_runpath_dirs.malloced = 0;
df07cd5
+
df07cd5
+      /* The RPATH is ignored.  */
df07cd5
+      l->l_rpath_dirs.dirs = (void *) -1;
df07cd5
+    }
df07cd5
+  else
df07cd5
     {
df07cd5
-      assert (l->l_type != lt_loaded);
df07cd5
+      l->l_runpath_dirs.dirs = (void *) -1;
df07cd5
 
df07cd5
-      if (l->l_info[DT_RUNPATH])
df07cd5
+      if (l->l_info[DT_RPATH])
df07cd5
 	{
df07cd5
 	  /* Allocate room for the search path and fill in information
df07cd5
-	     from RUNPATH.  */
df07cd5
-	  decompose_rpath (&l->l_runpath_dirs,
df07cd5
+	     from RPATH.  */
df07cd5
+	  decompose_rpath (&l->l_rpath_dirs,
df07cd5
 			   (const void *) (D_PTR (l, l_info[DT_STRTAB])
df07cd5
-					   + l->l_info[DT_RUNPATH]->d_un.d_val),
df07cd5
-			   l, "RUNPATH");
df07cd5
-	  /* During rtld init the memory is allocated by the stub malloc,
df07cd5
-	     prevent any attempt to free it by the normal malloc.  */
df07cd5
-	  l->l_runpath_dirs.malloced = 0;
df07cd5
-
df07cd5
-	  /* The RPATH is ignored.  */
df07cd5
-	  l->l_rpath_dirs.dirs = (void *) -1;
df07cd5
+					   + l->l_info[DT_RPATH]->d_un.d_val),
df07cd5
+			   l, "RPATH");
df07cd5
+	  /* During rtld init the memory is allocated by the stub
df07cd5
+	     malloc, prevent any attempt to free it by the normal
df07cd5
+	     malloc.  */
df07cd5
+	  l->l_rpath_dirs.malloced = 0;
df07cd5
 	}
df07cd5
       else
df07cd5
-	{
df07cd5
-	  l->l_runpath_dirs.dirs = (void *) -1;
df07cd5
-
df07cd5
-	  if (l->l_info[DT_RPATH])
df07cd5
-	    {
df07cd5
-	      /* Allocate room for the search path and fill in information
df07cd5
-		 from RPATH.  */
df07cd5
-	      decompose_rpath (&l->l_rpath_dirs,
df07cd5
-			       (const void *) (D_PTR (l, l_info[DT_STRTAB])
df07cd5
-					       + l->l_info[DT_RPATH]->d_un.d_val),
df07cd5
-			       l, "RPATH");
df07cd5
-	      /* During rtld init the memory is allocated by the stub
df07cd5
-		 malloc, prevent any attempt to free it by the normal
df07cd5
-		 malloc.  */
df07cd5
-	      l->l_rpath_dirs.malloced = 0;
df07cd5
-	    }
df07cd5
-	  else
df07cd5
-	    l->l_rpath_dirs.dirs = (void *) -1;
df07cd5
-	}
df07cd5
+	l->l_rpath_dirs.dirs = (void *) -1;
df07cd5
     }
df07cd5
-#endif	/* SHARED */
df07cd5
 
df07cd5
   if (llp != NULL && *llp != '\0')
df07cd5
     {
df07cd5
diff --git a/elf/tst-dst-static.c b/elf/tst-dst-static.c
df07cd5
new file mode 100644
df07cd5
index 0000000000000000..56eb371c96f85276
df07cd5
--- /dev/null
df07cd5
+++ b/elf/tst-dst-static.c
df07cd5
@@ -0,0 +1,32 @@
df07cd5
+/* Test DST expansion for static binaries doesn't carsh.  Bug 23462.
df07cd5
+   Copyright (C) 2021 Free Software Foundation, Inc.
df07cd5
+   This file is part of the GNU C Library.
df07cd5
+
df07cd5
+   The GNU C Library is free software; you can redistribute it and/or
df07cd5
+   modify it under the terms of the GNU Lesser General Public
df07cd5
+   License as published by the Free Software Foundation; either
df07cd5
+   version 2.1 of the License, or (at your option) any later version.
df07cd5
+
df07cd5
+   The GNU C Library is distributed in the hope that it will be useful,
df07cd5
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
df07cd5
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
df07cd5
+   Lesser General Public License for more details.
df07cd5
+
df07cd5
+   You should have received a copy of the GNU Lesser General Public
df07cd5
+   License along with the GNU C Library; if not, see
df07cd5
+   <https://www.gnu.org/licenses/>.  */
df07cd5
+
df07cd5
+/* The purpose of this test is to exercise the code in elf/dl-loac.c
df07cd5
+   (_dl_init_paths) or thereabout and ensure that static binaries
df07cd5
+   don't crash when expanding DSTs.
df07cd5
+
df07cd5
+   If the dynamic loader code linked into the static binary cannot
df07cd5
+   handle expanding the DSTs e.g. null-deref on an incomplete link
df07cd5
+   map, then it will crash before reaching main, so the test harness
df07cd5
+   is unnecessary.  */
df07cd5
+
df07cd5
+int
df07cd5
+main (void)
df07cd5
+{
df07cd5
+  return 0;
df07cd5
+}