e354a5
commit 9ffa50b26b0cb5d3043adf6d3d0b1ea735acc147
e354a5
Author: Florian Weimer <fweimer@redhat.com>
e354a5
Date:   Fri Dec 11 17:30:03 2020 +0100
e354a5
e354a5
    elf: Include libc.so.6 as main program in dependency sort (bug 20972)
e354a5
    
e354a5
    _dl_map_object_deps always sorts the initially loaded object first
e354a5
    during dependency sorting.  This means it is relocated last in
e354a5
    dl_open_worker.  This results in crashes in IFUNC resolvers without
e354a5
    lazy bindings if libraries are preloaded that refer to IFUNCs in
e354a5
    libc.so.6: the resolvers are called when libc.so.6 has not been
e354a5
    relocated yet, so references to _rtld_global_ro etc. crash.
e354a5
    
e354a5
    The fix is to check against the libc.so.6 link map recorded by the
e354a5
    __libc_early_init framework, and let it participate in the dependency
e354a5
    sort.
e354a5
    
e354a5
    This fixes bug 20972.
e354a5
    
e354a5
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
e354a5
e354a5
Conflicts:
e354a5
	elf/Makefile
e354a5
	  (Usual test backport differences.)
e354a5
e354a5
diff --git a/elf/Makefile b/elf/Makefile
e354a5
index 67029930dd2cb461..fc9c685b9d23bb6c 100644
e354a5
--- a/elf/Makefile
e354a5
+++ b/elf/Makefile
e354a5
@@ -215,7 +215,7 @@ tests-internal += loadtest unload unload2 circleload1 \
e354a5
 	 tst-tls3 tst-tls6 tst-tls7 tst-tls8 tst-dlmopen2 \
e354a5
 	 tst-ptrguard1 tst-stackguard1 tst-libc_dlvsym \
e354a5
 	 tst-create_format1 tst-tls-surplus tst-dl-hwcaps_split
e354a5
-tests-container += tst-pldd
e354a5
+tests-container += tst-pldd tst-preload-pthread-libc
e354a5
 ifeq ($(build-hardcoded-path-in-tests),yes)
e354a5
 tests += tst-dlopen-aout
e354a5
 tst-dlopen-aout-no-pie = yes
e354a5
diff --git a/elf/dl-deps.c b/elf/dl-deps.c
e354a5
index 50f053a1586efdc3..007069f670eced95 100644
e354a5
--- a/elf/dl-deps.c
e354a5
+++ b/elf/dl-deps.c
e354a5
@@ -610,7 +610,12 @@ Filters not supported with LD_TRACE_PRELINKING"));
e354a5
     memcpy (l_initfini, map->l_searchlist.r_list,
e354a5
 	    nlist * sizeof (struct link_map *));
e354a5
 
e354a5
-  _dl_sort_maps (&l_initfini[1], nlist - 1, NULL, false);
e354a5
+  /* If libc.so.6 is the main map, it participates in the sort, so
e354a5
+     that the relocation order is correct regarding libc.so.6.  */
e354a5
+  if (l_initfini[0] == GL (dl_ns)[l_initfini[0]->l_ns].libc_map)
e354a5
+    _dl_sort_maps (l_initfini, nlist, NULL, false);
e354a5
+  else
e354a5
+    _dl_sort_maps (&l_initfini[1], nlist - 1, NULL, false);
e354a5
 
e354a5
   /* Terminate the list of dependencies.  */
e354a5
   l_initfini[nlist] = NULL;
e354a5
diff --git a/elf/tst-preload-pthread-libc.c b/elf/tst-preload-pthread-libc.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..48cb512a93f3da19
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-preload-pthread-libc.c
e354a5
@@ -0,0 +1,36 @@
e354a5
+/* Test relocation ordering if the main executable is libc.so.6 (bug 20972).
e354a5
+   Copyright (C) 2020 Free Software Foundation, Inc.
e354a5
+   This file is part of the GNU C Library.
e354a5
+
e354a5
+   The GNU C Library is free software; you can redistribute it and/or
e354a5
+   modify it under the terms of the GNU Lesser General Public
e354a5
+   License as published by the Free Software Foundation; either
e354a5
+   version 2.1 of the License, or (at your option) any later version.
e354a5
+
e354a5
+   The GNU C Library is distributed in the hope that it will be useful,
e354a5
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
e354a5
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e354a5
+   Lesser General Public License for more details.
e354a5
+
e354a5
+   You should have received a copy of the GNU Lesser General Public
e354a5
+   License along with the GNU C Library; if not, see
e354a5
+   <https://www.gnu.org/licenses/>.  */
e354a5
+
e354a5
+#include <gnu/lib-names.h>
e354a5
+#include <stdio.h>
e354a5
+#include <support/support.h>
e354a5
+#include <unistd.h>
e354a5
+
e354a5
+int
e354a5
+main (void)
e354a5
+{
e354a5
+  char *libc = xasprintf ("%s/%s", support_slibdir_prefix, LIBC_SO);
e354a5
+  char *argv[] = { libc, NULL };
e354a5
+  char *envp[] = { (char *) "LD_PRELOAD=" LIBPTHREAD_SO,
e354a5
+    /* Relocation ordering matters most without lazy binding.  */
e354a5
+    (char *) "LD_BIND_NOW=1",
e354a5
+    NULL };
e354a5
+  execve (libc, argv, envp);
e354a5
+  printf ("execve of %s failed: %m\n", libc);
e354a5
+  return 1;
e354a5
+}