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