4c1956
commit b2964eb1d9a6b8ab1250e8a881cf406182da5875
4c1956
Author: Florian Weimer <fweimer@redhat.com>
4c1956
Date:   Wed Apr 21 19:49:51 2021 +0200
4c1956
4c1956
    dlfcn: Failures after dlmopen should not terminate process [BZ #24772]
4c1956
    
4c1956
    Commit 9e78f6f6e7134a5f299cc8de77370218f8019237 ("Implement
4c1956
    _dl_catch_error, _dl_signal_error in libc.so [BZ #16628]") has the
4c1956
    side effect that distinct namespaces, as created by dlmopen, now have
4c1956
    separate implementations of the rtld exception mechanism.  This means
4c1956
    that the call to _dl_catch_error from libdl in a secondary namespace
4c1956
    does not actually install an exception handler because the
4c1956
    thread-local variable catch_hook in the libc.so copy in the secondary
4c1956
    namespace is distinct from that of the base namepace.  As a result, a
4c1956
    dlsym/dlopen/... failure in a secondary namespace terminates the process
4c1956
    with a dynamic linker error because it looks to the exception handler
4c1956
    mechanism as if no handler has been installed.
4c1956
    
4c1956
    This commit restores GLRO (dl_catch_error) and uses it to set the
4c1956
    handler in the base namespace.
4c1956
    
4c1956
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
4c1956
4c1956
Conflicts:
4c1956
	elf/Makefile
4c1956
4c1956
diff --git a/dlfcn/dlerror.c b/dlfcn/dlerror.c
4c1956
index 06732460ea1512cd..e08ac3afef302817 100644
4c1956
--- a/dlfcn/dlerror.c
4c1956
+++ b/dlfcn/dlerror.c
4c1956
@@ -167,8 +167,10 @@ _dlerror_run (void (*operate) (void *), void *args)
4c1956
       result->errstring = NULL;
4c1956
     }
4c1956
 
4c1956
-  result->errcode = _dl_catch_error (&result->objname, &result->errstring,
4c1956
-				     &result->malloced, operate, args);
4c1956
+  result->errcode = GLRO (dl_catch_error) (&result->objname,
4c1956
+					   &result->errstring,
4c1956
+					   &result->malloced,
4c1956
+					   operate, args);
4c1956
 
4c1956
   /* If no error we mark that no error string is available.  */
4c1956
   result->returned = result->errstring == NULL;
4c1956
diff --git a/elf/Makefile b/elf/Makefile
4c1956
index a811919ba4568d64..e0919486a14cab1a 100644
4c1956
--- a/elf/Makefile
4c1956
+++ b/elf/Makefile
4c1956
@@ -216,6 +216,7 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
4c1956
 	 tst-glibc-hwcaps tst-glibc-hwcaps-prepend tst-glibc-hwcaps-mask \
4c1956
 	 tst-tls20 tst-tls21 \
4c1956
 	 tst-rtld-run-static \
4c1956
+	 tst-dlmopen-dlerror \
4c1956
 #	 reldep9
4c1956
 tests-internal += loadtest unload unload2 circleload1 \
4c1956
 	 neededtest neededtest2 neededtest3 neededtest4 \
4c1956
@@ -349,6 +350,7 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
4c1956
 		libmarkermod4-1 libmarkermod4-2 libmarkermod4-3 libmarkermod4-4 \
4c1956
 		libmarkermod5-1 libmarkermod5-2 libmarkermod5-3 libmarkermod5-4 \
4c1956
 		libmarkermod5-5 tst-tls20mod-bad tst-tls21mod \
4c1956
+		tst-dlmopen-dlerror-mod \
4c1956
 
4c1956
 # Most modules build with _ISOMAC defined, but those filtered out
4c1956
 # depend on internal headers.
4c1956
@@ -1546,6 +1548,10 @@ $(objpfx)tst-sonamemove-dlopen.out: \
4c1956
   $(objpfx)tst-sonamemove-runmod1.so \
4c1956
   $(objpfx)tst-sonamemove-runmod2.so
4c1956
 
4c1956
+$(objpfx)tst-dlmopen-dlerror: $(libdl)
4c1956
+$(objpfx)tst-dlmopen-dlerror-mod.so: $(libdl) $(libsupport)
4c1956
+$(objpfx)tst-dlmopen-dlerror.out: $(objpfx)tst-dlmopen-dlerror-mod.so
4c1956
+
4c1956
 # Override -z defs, so that we can reference an undefined symbol.
4c1956
 # Force lazy binding for the same reason.
4c1956
 LDFLAGS-tst-latepthreadmod.so = \
4c1956
diff --git a/elf/dl-error-skeleton.c b/elf/dl-error-skeleton.c
4c1956
index 9cb002ccfed2c7b4..7801aa433b12275f 100644
4c1956
--- a/elf/dl-error-skeleton.c
4c1956
+++ b/elf/dl-error-skeleton.c
4c1956
@@ -248,4 +248,16 @@ _dl_receive_error (receiver_fct fct, void (*operate) (void *), void *args)
4c1956
   catch_hook = old_catch;
4c1956
   receiver = old_receiver;
4c1956
 }
4c1956
+
4c1956
+/* Forwarder used for initializing GLRO (_dl_catch_error).  */
4c1956
+int
4c1956
+_rtld_catch_error (const char **objname, const char **errstring,
4c1956
+		   bool *mallocedp, void (*operate) (void *),
4c1956
+		   void *args)
4c1956
+{
4c1956
+  /* The reference to _dl_catch_error will eventually be relocated to
4c1956
+     point to the implementation in libc.so.  */
4c1956
+  return _dl_catch_error (objname, errstring, mallocedp, operate, args);
4c1956
+}
4c1956
+
4c1956
 #endif /* DL_ERROR_BOOTSTRAP */
4c1956
diff --git a/elf/rtld.c b/elf/rtld.c
4c1956
index 461d8c114a875a9b..c445b5ca25dea193 100644
4c1956
--- a/elf/rtld.c
4c1956
+++ b/elf/rtld.c
4c1956
@@ -365,6 +365,7 @@ struct rtld_global_ro _rtld_global_ro attribute_relro =
4c1956
     ._dl_lookup_symbol_x = _dl_lookup_symbol_x,
4c1956
     ._dl_open = _dl_open,
4c1956
     ._dl_close = _dl_close,
4c1956
+    ._dl_catch_error = _rtld_catch_error,
4c1956
     ._dl_tls_get_addr_soft = _dl_tls_get_addr_soft,
4c1956
 #ifdef HAVE_DL_DISCOVER_OSVERSION
4c1956
     ._dl_discover_osversion = _dl_discover_osversion
4c1956
diff --git a/elf/tst-dlmopen-dlerror-mod.c b/elf/tst-dlmopen-dlerror-mod.c
4c1956
new file mode 100644
4c1956
index 0000000000000000..7e95dcdeacf005be
4c1956
--- /dev/null
4c1956
+++ b/elf/tst-dlmopen-dlerror-mod.c
4c1956
@@ -0,0 +1,41 @@
4c1956
+/* Check that dlfcn errors are reported properly after dlmopen.  Test module.
4c1956
+   Copyright (C) 2021 Free Software Foundation, Inc.
4c1956
+   This file is part of the GNU C Library.
4c1956
+
4c1956
+   The GNU C Library is free software; you can redistribute it and/or
4c1956
+   modify it under the terms of the GNU Lesser General Public
4c1956
+   License as published by the Free Software Foundation; either
4c1956
+   version 2.1 of the License, or (at your option) any later version.
4c1956
+
4c1956
+   The GNU C Library is distributed in the hope that it will be useful,
4c1956
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
4c1956
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c1956
+   Lesser General Public License for more details.
4c1956
+
4c1956
+   You should have received a copy of the GNU Lesser General Public
4c1956
+   License along with the GNU C Library; if not, see
4c1956
+   <http://www.gnu.org/licenses/>.  */
4c1956
+
4c1956
+#include <dlfcn.h>
4c1956
+#include <stddef.h>
4c1956
+#include <support/check.h>
4c1956
+
4c1956
+/* Note: This object is not linked into the main program, so we cannot
4c1956
+   use delayed test failure reporting via TEST_VERIFY etc., and have
4c1956
+   to use FAIL_EXIT1 (or something else that calls exit).  */
4c1956
+
4c1956
+void
4c1956
+call_dlsym (void)
4c1956
+{
4c1956
+  void *ptr = dlsym (NULL, "does not exist");
4c1956
+  if (ptr != NULL)
4c1956
+    FAIL_EXIT1 ("dlsym did not fail as expected");
4c1956
+}
4c1956
+
4c1956
+void
4c1956
+call_dlopen (void)
4c1956
+{
4c1956
+  void *handle = dlopen ("tst-dlmopen-dlerror does not exist", RTLD_NOW);
4c1956
+  if (handle != NULL)
4c1956
+    FAIL_EXIT1 ("dlopen did not fail as expected");
4c1956
+}
4c1956
diff --git a/elf/tst-dlmopen-dlerror.c b/elf/tst-dlmopen-dlerror.c
4c1956
new file mode 100644
4c1956
index 0000000000000000..e864d2fe4c3484ab
4c1956
--- /dev/null
4c1956
+++ b/elf/tst-dlmopen-dlerror.c
4c1956
@@ -0,0 +1,37 @@
4c1956
+/* Check that dlfcn errors are reported properly after dlmopen.
4c1956
+   Copyright (C) 2021 Free Software Foundation, Inc.
4c1956
+   This file is part of the GNU C Library.
4c1956
+
4c1956
+   The GNU C Library is free software; you can redistribute it and/or
4c1956
+   modify it under the terms of the GNU Lesser General Public
4c1956
+   License as published by the Free Software Foundation; either
4c1956
+   version 2.1 of the License, or (at your option) any later version.
4c1956
+
4c1956
+   The GNU C Library is distributed in the hope that it will be useful,
4c1956
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
4c1956
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c1956
+   Lesser General Public License for more details.
4c1956
+
4c1956
+   You should have received a copy of the GNU Lesser General Public
4c1956
+   License along with the GNU C Library; if not, see
4c1956
+   <http://www.gnu.org/licenses/>.  */
4c1956
+
4c1956
+#include <stddef.h>
4c1956
+#include <support/check.h>
4c1956
+#include <support/xdlfcn.h>
4c1956
+
4c1956
+static int
4c1956
+do_test (void)
4c1956
+{
4c1956
+  void *handle = xdlmopen (LM_ID_NEWLM, "tst-dlmopen-dlerror-mod.so",
4c1956
+                           RTLD_NOW);
4c1956
+  void (*call_dlsym) (void) = xdlsym (handle, "call_dlsym");
4c1956
+  void (*call_dlopen) (void) = xdlsym (handle, "call_dlopen");
4c1956
+
4c1956
+  call_dlsym ();
4c1956
+  call_dlopen ();
4c1956
+
4c1956
+  return 0;
4c1956
+}
4c1956
+
4c1956
+#include <support/test-driver.c>
4c1956
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
4c1956
index 7b0a667629ddc06a..d6d02aa3ccffba33 100644
4c1956
--- a/sysdeps/generic/ldsodefs.h
4c1956
+++ b/sysdeps/generic/ldsodefs.h
4c1956
@@ -647,6 +647,12 @@ struct rtld_global_ro
4c1956
   void *(*_dl_open) (const char *file, int mode, const void *caller_dlopen,
4c1956
 		     Lmid_t nsid, int argc, char *argv[], char *env[]);
4c1956
   void (*_dl_close) (void *map);
4c1956
+  /* libdl in a secondary namespace (after dlopen) must use
4c1956
+     _dl_catch_error from the main namespace, so it has to be
4c1956
+     exported in some way.  */
4c1956
+  int (*_dl_catch_error) (const char **objname, const char **errstring,
4c1956
+			  bool *mallocedp, void (*operate) (void *),
4c1956
+			  void *args);
4c1956
   void *(*_dl_tls_get_addr_soft) (struct link_map *);
4c1956
 #ifdef HAVE_DL_DISCOVER_OSVERSION
4c1956
   int (*_dl_discover_osversion) (void);
4c1956
@@ -889,6 +895,9 @@ extern int _dl_catch_error (const char **objname, const char **errstring,
4c1956
 			    void *args);
4c1956
 libc_hidden_proto (_dl_catch_error)
4c1956
 
4c1956
+/* Used for initializing GLRO (_dl_catch_error).  */
4c1956
+extern __typeof__ (_dl_catch_error) _rtld_catch_error attribute_hidden;
4c1956
+
4c1956
 /* Call OPERATE (ARGS).  If no error occurs, set *EXCEPTION to zero.
4c1956
    Otherwise, store a copy of the raised exception in *EXCEPTION,
4c1956
    which has to be freed by _dl_exception_free.  As a special case, if