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