d8307d
commit 79e0cd7b3c997e211fad44a81fd839dc5b2546e8
d8307d
Author: Florian Weimer <fweimer@redhat.com>
d8307d
Date:   Wed Nov 27 16:20:47 2019 +0100
d8307d
d8307d
    Lazy binding failures during dlopen/dlclose must be fatal [BZ #24304]
d8307d
    
d8307d
    If a lazy binding failure happens during the execution of an ELF
d8307d
    constructor or destructor, the dynamic loader catches the error
d8307d
    and reports it using the dlerror mechanism.  This is undesirable
d8307d
    because there could be other constructors and destructors that
d8307d
    need processing (which are skipped), and the process is in an
d8307d
    inconsistent state at this point.  Therefore, we have to issue
d8307d
    a fatal dynamic loader error error and terminate the process.
d8307d
    
d8307d
    Note that the _dl_catch_exception in _dl_open is just an inner catch,
d8307d
    to roll back some state locally.  If called from dlopen, there is
d8307d
    still an outer catch, which is why calling _dl_init via call_dl_init
d8307d
    and a no-exception is required and cannot be avoiding by moving the
d8307d
    _dl_init call directly into _dl_open.
d8307d
    
d8307d
    _dl_fini does not need changes because it does not install an error
d8307d
    handler, so errors are already fatal there.
d8307d
    
d8307d
    Change-Id: I6b1addfe2e30f50a1781595f046f44173db9491a
d8307d
d8307d
Conflicts:
d8307d
	elf/Makefile
d8307d
	  (Usual conflicts due to test backport differences.)
d8307d
d8307d
diff --git a/elf/Makefile b/elf/Makefile
d8307d
index 74a240b3a68ff5e2..b752f6366400d221 100644
d8307d
--- a/elf/Makefile
d8307d
+++ b/elf/Makefile
d8307d
@@ -191,7 +191,7 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
d8307d
 	 tst-nodelete2 tst-audit11 tst-audit12 tst-dlsym-error tst-noload \
d8307d
 	 tst-latepthread tst-tls-manydynamic tst-nodelete-dlclose \
d8307d
 	 tst-debug1 tst-main1 tst-absolute-sym tst-absolute-zero tst-big-note \
d8307d
-	 tst-sonamemove-link tst-sonamemove-dlopen
d8307d
+	 tst-sonamemove-link tst-sonamemove-dlopen tst-initfinilazyfail
d8307d
 #	 reldep9
d8307d
 tests-internal += loadtest unload unload2 circleload1 \
d8307d
 	 neededtest neededtest2 neededtest3 neededtest4 \
d8307d
@@ -281,7 +281,8 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
d8307d
 		tst-main1mod tst-libc_dlvsym-dso tst-absolute-sym-lib \
d8307d
 		tst-absolute-zero-lib tst-big-note-lib \
d8307d
 		tst-sonamemove-linkmod1 \
d8307d
-		tst-sonamemove-runmod1 tst-sonamemove-runmod2
d8307d
+		tst-sonamemove-runmod1 tst-sonamemove-runmod2 \
d8307d
+		tst-initlazyfailmod tst-finilazyfailmod
d8307d
 
d8307d
 ifeq (yes,$(have-mtls-dialect-gnu2))
d8307d
 tests += tst-gnu2-tls1
d8307d
@@ -1526,3 +1527,13 @@ tst-libc_dlvsym-static-ENV = \
d8307d
 $(objpfx)tst-libc_dlvsym-static.out: $(objpfx)tst-libc_dlvsym-dso.so
d8307d
 
d8307d
 $(objpfx)tst-big-note: $(objpfx)tst-big-note-lib.so
d8307d
+
d8307d
+$(objpfx)tst-initfinilazyfail: $(libdl)
d8307d
+$(objpfx)tst-initfinilazyfail.out: \
d8307d
+  $(objpfx)tst-initlazyfailmod.so $(objpfx)tst-finilazyfailmod.so
d8307d
+# Override -z defs, so that we can reference an undefined symbol.
d8307d
+# Force lazy binding for the same reason.
d8307d
+LDFLAGS-tst-initlazyfailmod.so = \
d8307d
+  -Wl,-z,lazy -Wl,--unresolved-symbols=ignore-all
d8307d
+LDFLAGS-tst-finilazyfailmod.so = \
d8307d
+  -Wl,-z,lazy -Wl,--unresolved-symbols=ignore-all
d8307d
diff --git a/elf/dl-close.c b/elf/dl-close.c
d8307d
index ecd6729704ea3294..88aeea25839a34e0 100644
d8307d
--- a/elf/dl-close.c
d8307d
+++ b/elf/dl-close.c
d8307d
@@ -106,6 +106,30 @@ remove_slotinfo (size_t idx, struct dtv_slotinfo_list *listp, size_t disp,
d8307d
   return false;
d8307d
 }
d8307d
 
d8307d
+/* Invoke dstructors for CLOSURE (a struct link_map *).  Called with
d8307d
+   exception handling temporarily disabled, to make errors fatal.  */
d8307d
+static void
d8307d
+call_destructors (void *closure)
d8307d
+{
d8307d
+  struct link_map *map = closure;
d8307d
+
d8307d
+  if (map->l_info[DT_FINI_ARRAY] != NULL)
d8307d
+    {
d8307d
+      ElfW(Addr) *array =
d8307d
+	(ElfW(Addr) *) (map->l_addr
d8307d
+			+ map->l_info[DT_FINI_ARRAY]->d_un.d_ptr);
d8307d
+      unsigned int sz = (map->l_info[DT_FINI_ARRAYSZ]->d_un.d_val
d8307d
+			 / sizeof (ElfW(Addr)));
d8307d
+
d8307d
+      while (sz-- > 0)
d8307d
+	((fini_t) array[sz]) ();
d8307d
+    }
d8307d
+
d8307d
+  /* Next try the old-style destructor.  */
d8307d
+  if (map->l_info[DT_FINI] != NULL)
d8307d
+    DL_CALL_DT_FINI (map, ((void *) map->l_addr
d8307d
+			   + map->l_info[DT_FINI]->d_un.d_ptr));
d8307d
+}
d8307d
 
d8307d
 void
d8307d
 _dl_close_worker (struct link_map *map, bool force)
d8307d
@@ -267,7 +291,8 @@ _dl_close_worker (struct link_map *map, bool force)
d8307d
 		  && (imap->l_flags_1 & DF_1_NODELETE) == 0);
d8307d
 
d8307d
 	  /* Call its termination function.  Do not do it for
d8307d
-	     half-cooked objects.  */
d8307d
+	     half-cooked objects.  Temporarily disable exception
d8307d
+	     handling, so that errors are fatal.  */
d8307d
 	  if (imap->l_init_called)
d8307d
 	    {
d8307d
 	      /* When debugging print a message first.  */
d8307d
@@ -276,22 +301,9 @@ _dl_close_worker (struct link_map *map, bool force)
d8307d
 		_dl_debug_printf ("\ncalling fini: %s [%lu]\n\n",
d8307d
 				  imap->l_name, nsid);
d8307d
 
d8307d
-	      if (imap->l_info[DT_FINI_ARRAY] != NULL)
d8307d
-		{
d8307d
-		  ElfW(Addr) *array =
d8307d
-		    (ElfW(Addr) *) (imap->l_addr
d8307d
-				    + imap->l_info[DT_FINI_ARRAY]->d_un.d_ptr);
d8307d
-		  unsigned int sz = (imap->l_info[DT_FINI_ARRAYSZ]->d_un.d_val
d8307d
-				     / sizeof (ElfW(Addr)));
d8307d
-
d8307d
-		  while (sz-- > 0)
d8307d
-		    ((fini_t) array[sz]) ();
d8307d
-		}
d8307d
-
d8307d
-	      /* Next try the old-style destructor.  */
d8307d
-	      if (imap->l_info[DT_FINI] != NULL)
d8307d
-		DL_CALL_DT_FINI (imap, ((void *) imap->l_addr
d8307d
-			 + imap->l_info[DT_FINI]->d_un.d_ptr));
d8307d
+	      if (imap->l_info[DT_FINI_ARRAY] != NULL
d8307d
+		  || imap->l_info[DT_FINI] != NULL)
d8307d
+		_dl_catch_exception (NULL, call_destructors, imap);
d8307d
 	    }
d8307d
 
d8307d
 #ifdef SHARED
d8307d
diff --git a/elf/dl-open.c b/elf/dl-open.c
d8307d
index 518a6cad699ec6d0..c9c0254ee74c4f4b 100644
d8307d
--- a/elf/dl-open.c
d8307d
+++ b/elf/dl-open.c
d8307d
@@ -177,6 +177,23 @@ _dl_find_dso_for_object (const ElfW(Addr) addr)
d8307d
 }
d8307d
 rtld_hidden_def (_dl_find_dso_for_object);
d8307d
 
d8307d
+/* struct dl_init_args and call_dl_init are used to call _dl_init with
d8307d
+   exception handling disabled.  */
d8307d
+struct dl_init_args
d8307d
+{
d8307d
+  struct link_map *new;
d8307d
+  int argc;
d8307d
+  char **argv;
d8307d
+  char **env;
d8307d
+};
d8307d
+
d8307d
+static void
d8307d
+call_dl_init (void *closure)
d8307d
+{
d8307d
+  struct dl_init_args *args = closure;
d8307d
+  _dl_init (args->new, args->argc, args->argv, args->env);
d8307d
+}
d8307d
+
d8307d
 static void
d8307d
 dl_open_worker (void *a)
d8307d
 {
d8307d
@@ -506,8 +523,19 @@ TLS generation counter wrapped!  Please report this."));
d8307d
   DL_STATIC_INIT (new);
d8307d
 #endif
d8307d
 
d8307d
-  /* Run the initializer functions of new objects.  */
d8307d
-  _dl_init (new, args->argc, args->argv, args->env);
d8307d
+  /* Run the initializer functions of new objects.  Temporarily
d8307d
+     disable the exception handler, so that lazy binding failures are
d8307d
+     fatal.  */
d8307d
+  {
d8307d
+    struct dl_init_args init_args =
d8307d
+      {
d8307d
+        .new = new,
d8307d
+        .argc = args->argc,
d8307d
+        .argv = args->argv,
d8307d
+        .env = args->env
d8307d
+      };
d8307d
+    _dl_catch_exception (NULL, call_dl_init, &init_args);
d8307d
+  }
d8307d
 
d8307d
   /* Now we can make the new map available in the global scope.  */
d8307d
   if (mode & RTLD_GLOBAL)
d8307d
diff --git a/elf/tst-finilazyfailmod.c b/elf/tst-finilazyfailmod.c
d8307d
new file mode 100644
d8307d
index 0000000000000000..2670bd1a9400d0ef
d8307d
--- /dev/null
d8307d
+++ b/elf/tst-finilazyfailmod.c
d8307d
@@ -0,0 +1,27 @@
d8307d
+/* Helper module for tst-initfinilazyfail: lazy binding failure in destructor.
d8307d
+   Copyright (C) 2019 Free Software Foundation, Inc.
d8307d
+   This file is part of the GNU C Library.
d8307d
+
d8307d
+   The GNU C Library is free software; you can redistribute it and/or
d8307d
+   modify it under the terms of the GNU Lesser General Public
d8307d
+   License as published by the Free Software Foundation; either
d8307d
+   version 2.1 of the License, or (at your option) any later version.
d8307d
+
d8307d
+   The GNU C Library is distributed in the hope that it will be useful,
d8307d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
+   Lesser General Public License for more details.
d8307d
+
d8307d
+   You should have received a copy of the GNU Lesser General Public
d8307d
+   License along with the GNU C Library; if not, see
d8307d
+   <https://www.gnu.org/licenses/>.  */
d8307d
+
d8307d
+/* An undefined function.  Calling it will cause a lazy binding
d8307d
+   failure.  */
d8307d
+void undefined_function (void);
d8307d
+
d8307d
+static void __attribute__ ((destructor))
d8307d
+fini (void)
d8307d
+{
d8307d
+  undefined_function ();
d8307d
+}
d8307d
diff --git a/elf/tst-initfinilazyfail.c b/elf/tst-initfinilazyfail.c
d8307d
new file mode 100644
d8307d
index 0000000000000000..9b4a3d0c0ffbb7c6
d8307d
--- /dev/null
d8307d
+++ b/elf/tst-initfinilazyfail.c
d8307d
@@ -0,0 +1,84 @@
d8307d
+/* Test that lazy binding failures in constructors and destructors are fatal.
d8307d
+   Copyright (C) 2019 Free Software Foundation, Inc.
d8307d
+   This file is part of the GNU C Library.
d8307d
+
d8307d
+   The GNU C Library is free software; you can redistribute it and/or
d8307d
+   modify it under the terms of the GNU Lesser General Public
d8307d
+   License as published by the Free Software Foundation; either
d8307d
+   version 2.1 of the License, or (at your option) any later version.
d8307d
+
d8307d
+   The GNU C Library is distributed in the hope that it will be useful,
d8307d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
+   Lesser General Public License for more details.
d8307d
+
d8307d
+   You should have received a copy of the GNU Lesser General Public
d8307d
+   License along with the GNU C Library; if not, see
d8307d
+   <https://www.gnu.org/licenses/>.  */
d8307d
+
d8307d
+#include <dlfcn.h>
d8307d
+#include <string.h>
d8307d
+#include <support/capture_subprocess.h>
d8307d
+#include <support/check.h>
d8307d
+#include <support/xdlfcn.h>
d8307d
+
d8307d
+static void
d8307d
+test_constructor (void *closure)
d8307d
+{
d8307d
+  void *handle = dlopen ("tst-initlazyfailmod.so", RTLD_LAZY);
d8307d
+  if (handle == NULL)
d8307d
+    FAIL_EXIT (2, "dlopen did not terminate the process: %s", dlerror ());
d8307d
+  else
d8307d
+    FAIL_EXIT (2, "dlopen did not terminate the process (%p)", handle);
d8307d
+}
d8307d
+
d8307d
+static void
d8307d
+test_destructor (void *closure)
d8307d
+{
d8307d
+  void *handle = xdlopen ("tst-finilazyfailmod.so", RTLD_LAZY);
d8307d
+  int ret = dlclose (handle);
d8307d
+  const char *message = dlerror ();
d8307d
+  if (message != NULL)
d8307d
+    FAIL_EXIT (2, "dlclose did not terminate the process: %d, %s",
d8307d
+               ret, message);
d8307d
+  else
d8307d
+    FAIL_EXIT (2, "dlopen did not terminate the process: %d", ret);
d8307d
+}
d8307d
+
d8307d
+static int
d8307d
+do_test (void)
d8307d
+{
d8307d
+  {
d8307d
+    struct support_capture_subprocess proc
d8307d
+      = support_capture_subprocess (test_constructor, NULL);
d8307d
+    support_capture_subprocess_check (&proc, "constructor", 127,
d8307d
+                                      sc_allow_stderr);
d8307d
+    printf ("info: constructor failure output: [[%s]]\n", proc.err.buffer);
d8307d
+    TEST_VERIFY (strstr (proc.err.buffer,
d8307d
+                         "tst-initfinilazyfail: symbol lookup error: ")
d8307d
+                 != NULL);
d8307d
+    TEST_VERIFY (strstr (proc.err.buffer,
d8307d
+                         "tst-initlazyfailmod.so: undefined symbol:"
d8307d
+                         " undefined_function\n") != NULL);
d8307d
+    support_capture_subprocess_free (&proc;;
d8307d
+  }
d8307d
+
d8307d
+  {
d8307d
+    struct support_capture_subprocess proc
d8307d
+      = support_capture_subprocess (test_destructor, NULL);
d8307d
+    support_capture_subprocess_check (&proc, "destructor", 127,
d8307d
+                                      sc_allow_stderr);
d8307d
+    printf ("info: destructor failure output: [[%s]]\n", proc.err.buffer);
d8307d
+    TEST_VERIFY (strstr (proc.err.buffer,
d8307d
+                         "tst-initfinilazyfail: symbol lookup error: ")
d8307d
+                 != NULL);
d8307d
+    TEST_VERIFY (strstr (proc.err.buffer,
d8307d
+                         "tst-finilazyfailmod.so: undefined symbol:"
d8307d
+                         " undefined_function\n") != NULL);
d8307d
+    support_capture_subprocess_free (&proc;;
d8307d
+  }
d8307d
+
d8307d
+  return 0;
d8307d
+}
d8307d
+
d8307d
+#include <support/test-driver.c>
d8307d
diff --git a/elf/tst-initlazyfailmod.c b/elf/tst-initlazyfailmod.c
d8307d
new file mode 100644
d8307d
index 0000000000000000..36348b58d634d2bb
d8307d
--- /dev/null
d8307d
+++ b/elf/tst-initlazyfailmod.c
d8307d
@@ -0,0 +1,27 @@
d8307d
+/* Helper module for tst-initfinilazyfail: lazy binding failure in constructor.
d8307d
+   Copyright (C) 2019 Free Software Foundation, Inc.
d8307d
+   This file is part of the GNU C Library.
d8307d
+
d8307d
+   The GNU C Library is free software; you can redistribute it and/or
d8307d
+   modify it under the terms of the GNU Lesser General Public
d8307d
+   License as published by the Free Software Foundation; either
d8307d
+   version 2.1 of the License, or (at your option) any later version.
d8307d
+
d8307d
+   The GNU C Library is distributed in the hope that it will be useful,
d8307d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
+   Lesser General Public License for more details.
d8307d
+
d8307d
+   You should have received a copy of the GNU Lesser General Public
d8307d
+   License along with the GNU C Library; if not, see
d8307d
+   <https://www.gnu.org/licenses/>.  */
d8307d
+
d8307d
+/* An undefined function.  Calling it will cause a lazy binding
d8307d
+   failure.  */
d8307d
+void undefined_function (void);
d8307d
+
d8307d
+static void __attribute__ ((constructor))
d8307d
+init (void)
d8307d
+{
d8307d
+  undefined_function ();
d8307d
+}