08c3a6
commit bc56ab1f4aa937665034373d3e320d0779a839aa
08c3a6
Author: Florian Weimer <fweimer@redhat.com>
08c3a6
Date:   Tue Apr 26 14:23:02 2022 +0200
08c3a6
08c3a6
    dlfcn: Do not use rtld_active () to determine ld.so state (bug 29078)
08c3a6
    
08c3a6
    When audit modules are loaded, ld.so initialization is not yet
08c3a6
    complete, and rtld_active () returns false even though ld.so is
08c3a6
    mostly working.  Instead, the static dlopen hook is used, but that
08c3a6
    does not work at all because this is not a static dlopen situation.
08c3a6
    
08c3a6
    Commit 466c1ea15f461edb8e3ffaf5d86d708876343bbf ("dlfcn: Rework
08c3a6
    static dlopen hooks") moved the hook pointer into _rtld_global_ro,
08c3a6
    which means that separate protection is not needed anymore and the
08c3a6
    hook pointer can be checked directly.
08c3a6
    
08c3a6
    The guard for disabling libio vtable hardening in _IO_vtable_check
08c3a6
    should stay for now.
08c3a6
    
08c3a6
    Fixes commit 8e1472d2c1e25e6eabc2059170731365f6d5b3d1 ("ld.so:
08c3a6
    Examine GLRO to detect inactive loader [BZ #20204]").
08c3a6
    
08c3a6
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
08c3a6
    (cherry picked from commit 8dcb6d0af07fda3607b541857e4f3970a74ed55b)
08c3a6
08c3a6
diff --git a/dlfcn/dladdr.c b/dlfcn/dladdr.c
08c3a6
index 1cc305f0c46e7c3b..0d07ae1cd4dbb7a2 100644
08c3a6
--- a/dlfcn/dladdr.c
08c3a6
+++ b/dlfcn/dladdr.c
08c3a6
@@ -24,7 +24,7 @@ int
08c3a6
 __dladdr (const void *address, Dl_info *info)
08c3a6
 {
08c3a6
 #ifdef SHARED
08c3a6
-  if (!rtld_active ())
08c3a6
+  if (GLRO (dl_dlfcn_hook) != NULL)
08c3a6
     return GLRO (dl_dlfcn_hook)->dladdr (address, info);
08c3a6
 #endif
08c3a6
   return _dl_addr (address, info, NULL, NULL);
08c3a6
diff --git a/dlfcn/dladdr1.c b/dlfcn/dladdr1.c
08c3a6
index 78560dbac208c316..93ce68c1d6067fe2 100644
08c3a6
--- a/dlfcn/dladdr1.c
08c3a6
+++ b/dlfcn/dladdr1.c
08c3a6
@@ -24,7 +24,7 @@ int
08c3a6
 __dladdr1 (const void *address, Dl_info *info, void **extra, int flags)
08c3a6
 {
08c3a6
 #ifdef SHARED
08c3a6
-  if (!rtld_active ())
08c3a6
+  if (GLRO (dl_dlfcn_hook) != NULL)
08c3a6
     return GLRO (dl_dlfcn_hook)->dladdr1 (address, info, extra, flags);
08c3a6
 #endif
08c3a6
 
08c3a6
diff --git a/dlfcn/dlclose.c b/dlfcn/dlclose.c
08c3a6
index 6a013a81bb648191..07ecb21bf7d43be4 100644
08c3a6
--- a/dlfcn/dlclose.c
08c3a6
+++ b/dlfcn/dlclose.c
08c3a6
@@ -24,7 +24,7 @@ int
08c3a6
 __dlclose (void *handle)
08c3a6
 {
08c3a6
 #ifdef SHARED
08c3a6
-  if (!rtld_active ())
08c3a6
+  if (GLRO (dl_dlfcn_hook) != NULL)
08c3a6
     return GLRO (dl_dlfcn_hook)->dlclose (handle);
08c3a6
 #endif
08c3a6
 
08c3a6
diff --git a/dlfcn/dlerror.c b/dlfcn/dlerror.c
08c3a6
index 5047b140662bc33e..63da79c63000eef0 100644
08c3a6
--- a/dlfcn/dlerror.c
08c3a6
+++ b/dlfcn/dlerror.c
08c3a6
@@ -32,7 +32,7 @@ char *
08c3a6
 __dlerror (void)
08c3a6
 {
08c3a6
 # ifdef SHARED
08c3a6
-  if (!rtld_active ())
08c3a6
+  if (GLRO (dl_dlfcn_hook) != NULL)
08c3a6
     return GLRO (dl_dlfcn_hook)->dlerror ();
08c3a6
 # endif
08c3a6
 
08c3a6
diff --git a/dlfcn/dlinfo.c b/dlfcn/dlinfo.c
08c3a6
index c6f9a1da09ff8622..47d2daa96fa5986f 100644
08c3a6
--- a/dlfcn/dlinfo.c
08c3a6
+++ b/dlfcn/dlinfo.c
08c3a6
@@ -89,7 +89,7 @@ dlinfo_implementation (void *handle, int request, void *arg)
08c3a6
 int
08c3a6
 ___dlinfo (void *handle, int request, void *arg)
08c3a6
 {
08c3a6
-  if (!rtld_active ())
08c3a6
+  if (GLRO (dl_dlfcn_hook) != NULL)
08c3a6
     return GLRO (dl_dlfcn_hook)->dlinfo (handle, request, arg);
08c3a6
   else
08c3a6
     return dlinfo_implementation (handle, request, arg);
08c3a6
diff --git a/dlfcn/dlmopen.c b/dlfcn/dlmopen.c
08c3a6
index c171c8953da20fc7..2309224eb8484b1a 100644
08c3a6
--- a/dlfcn/dlmopen.c
08c3a6
+++ b/dlfcn/dlmopen.c
08c3a6
@@ -80,7 +80,7 @@ dlmopen_implementation (Lmid_t nsid, const char *file, int mode,
08c3a6
 void *
08c3a6
 ___dlmopen (Lmid_t nsid, const char *file, int mode)
08c3a6
 {
08c3a6
-  if (!rtld_active ())
08c3a6
+  if (GLRO (dl_dlfcn_hook) != NULL)
08c3a6
     return GLRO (dl_dlfcn_hook)->dlmopen (nsid, file, mode, RETURN_ADDRESS (0));
08c3a6
   else
08c3a6
     return dlmopen_implementation (nsid, file, mode, RETURN_ADDRESS (0));
08c3a6
diff --git a/dlfcn/dlopen.c b/dlfcn/dlopen.c
08c3a6
index e04b374b82b04337..9c59c751c4eaf7a7 100644
08c3a6
--- a/dlfcn/dlopen.c
08c3a6
+++ b/dlfcn/dlopen.c
08c3a6
@@ -75,7 +75,7 @@ dlopen_implementation (const char *file, int mode, void *dl_caller)
08c3a6
 void *
08c3a6
 ___dlopen (const char *file, int mode)
08c3a6
 {
08c3a6
-  if (!rtld_active ())
08c3a6
+  if (GLRO (dl_dlfcn_hook) != NULL)
08c3a6
     return GLRO (dl_dlfcn_hook)->dlopen (file, mode, RETURN_ADDRESS (0));
08c3a6
   else
08c3a6
     return dlopen_implementation (file, mode, RETURN_ADDRESS (0));
08c3a6
diff --git a/dlfcn/dlopenold.c b/dlfcn/dlopenold.c
08c3a6
index 9115501ac121eeca..c2f2a42194d50953 100644
08c3a6
--- a/dlfcn/dlopenold.c
08c3a6
+++ b/dlfcn/dlopenold.c
08c3a6
@@ -70,7 +70,7 @@ __dlopen_nocheck (const char *file, int mode)
08c3a6
     mode |= RTLD_LAZY;
08c3a6
   args.mode = mode;
08c3a6
 
08c3a6
-  if (!rtld_active ())
08c3a6
+  if (GLRO (dl_dlfcn_hook) != NULL)
08c3a6
     return GLRO (dl_dlfcn_hook)->dlopen (file, mode, RETURN_ADDRESS (0));
08c3a6
 
08c3a6
   return _dlerror_run (dlopen_doit, &args) ? NULL : args.new;
08c3a6
diff --git a/dlfcn/dlsym.c b/dlfcn/dlsym.c
08c3a6
index 43044cf7bb95801e..d3861170a7631d01 100644
08c3a6
--- a/dlfcn/dlsym.c
08c3a6
+++ b/dlfcn/dlsym.c
08c3a6
@@ -62,7 +62,7 @@ dlsym_implementation (void *handle, const char *name, void *dl_caller)
08c3a6
 void *
08c3a6
 ___dlsym (void *handle, const char *name)
08c3a6
 {
08c3a6
-  if (!rtld_active ())
08c3a6
+  if (GLRO (dl_dlfcn_hook) != NULL)
08c3a6
     return GLRO (dl_dlfcn_hook)->dlsym (handle, name, RETURN_ADDRESS (0));
08c3a6
   else
08c3a6
     return dlsym_implementation (handle, name, RETURN_ADDRESS (0));
08c3a6
diff --git a/dlfcn/dlvsym.c b/dlfcn/dlvsym.c
08c3a6
index 9b76f9afa513e11f..3af02109c306b800 100644
08c3a6
--- a/dlfcn/dlvsym.c
08c3a6
+++ b/dlfcn/dlvsym.c
08c3a6
@@ -65,7 +65,7 @@ dlvsym_implementation (void *handle, const char *name, const char *version,
08c3a6
 void *
08c3a6
 ___dlvsym (void *handle, const char *name, const char *version)
08c3a6
 {
08c3a6
-  if (!rtld_active ())
08c3a6
+  if (GLRO (dl_dlfcn_hook) != NULL)
08c3a6
     return GLRO (dl_dlfcn_hook)->dlvsym (handle, name, version,
08c3a6
 					 RETURN_ADDRESS (0));
08c3a6
   else
08c3a6
diff --git a/elf/Makefile b/elf/Makefile
08c3a6
index fec6e23b5b625e3b..c89a6a58690646ee 100644
08c3a6
--- a/elf/Makefile
08c3a6
+++ b/elf/Makefile
08c3a6
@@ -376,6 +376,7 @@ tests += \
08c3a6
   tst-audit24d \
08c3a6
   tst-audit25a \
08c3a6
   tst-audit25b \
08c3a6
+  tst-audit26 \
08c3a6
   tst-auditmany \
08c3a6
   tst-auxobj \
08c3a6
   tst-auxobj-dlopen \
08c3a6
@@ -721,6 +722,7 @@ modules-names = \
08c3a6
   tst-auditmod24c \
08c3a6
   tst-auditmod24d \
08c3a6
   tst-auditmod25 \
08c3a6
+  tst-auditmod26 \
08c3a6
   tst-auxvalmod \
08c3a6
   tst-big-note-lib \
08c3a6
   tst-deep1mod1 \
08c3a6
@@ -2194,6 +2196,10 @@ $(objpfx)tst-audit25b: $(objpfx)tst-audit25mod1.so \
08c3a6
 LDFLAGS-tst-audit25b = -Wl,-z,now
08c3a6
 tst-audit25b-ARGS = -- $(host-test-program-cmd)
08c3a6
 
08c3a6
+$(objpfx)tst-audit26.out: $(objpfx)tst-auditmod26.so
08c3a6
+$(objpfx)tst-auditmod26.so: $(libsupport)
08c3a6
+tst-audit26-ENV = LD_AUDIT=$(objpfx)tst-auditmod26.so
08c3a6
+
08c3a6
 # tst-sonamemove links against an older implementation of the library.
08c3a6
 LDFLAGS-tst-sonamemove-linkmod1.so = \
08c3a6
   -Wl,--version-script=tst-sonamemove-linkmod1.map \
08c3a6
diff --git a/elf/dl-libc.c b/elf/dl-libc.c
08c3a6
index d5bc4a277f4c6ef3..db4342a3256921f0 100644
08c3a6
--- a/elf/dl-libc.c
08c3a6
+++ b/elf/dl-libc.c
08c3a6
@@ -157,7 +157,7 @@ __libc_dlopen_mode (const char *name, int mode)
08c3a6
   args.caller_dlopen = RETURN_ADDRESS (0);
08c3a6
 
08c3a6
 #ifdef SHARED
08c3a6
-  if (!rtld_active ())
08c3a6
+  if (GLRO (dl_dlfcn_hook) != NULL)
08c3a6
     return GLRO (dl_dlfcn_hook)->libc_dlopen_mode (name, mode);
08c3a6
 #endif
08c3a6
   return dlerror_run (do_dlopen, &args) ? NULL : (void *) args.map;
08c3a6
@@ -185,7 +185,7 @@ __libc_dlsym (void *map, const char *name)
08c3a6
   args.name = name;
08c3a6
 
08c3a6
 #ifdef SHARED
08c3a6
-  if (!rtld_active ())
08c3a6
+  if (GLRO (dl_dlfcn_hook) != NULL)
08c3a6
     return GLRO (dl_dlfcn_hook)->libc_dlsym (map, name);
08c3a6
 #endif
08c3a6
   return (dlerror_run (do_dlsym, &args) ? NULL
08c3a6
@@ -199,7 +199,7 @@ void *
08c3a6
 __libc_dlvsym (void *map, const char *name, const char *version)
08c3a6
 {
08c3a6
 #ifdef SHARED
08c3a6
-  if (!rtld_active ())
08c3a6
+  if (GLRO (dl_dlfcn_hook) != NULL)
08c3a6
     return GLRO (dl_dlfcn_hook)->libc_dlvsym (map, name, version);
08c3a6
 #endif
08c3a6
 
08c3a6
@@ -222,7 +222,7 @@ int
08c3a6
 __libc_dlclose (void *map)
08c3a6
 {
08c3a6
 #ifdef SHARED
08c3a6
-  if (!rtld_active ())
08c3a6
+  if (GLRO (dl_dlfcn_hook) != NULL)
08c3a6
     return GLRO (dl_dlfcn_hook)->libc_dlclose (map);
08c3a6
 #endif
08c3a6
   return dlerror_run (do_dlclose, map);
08c3a6
diff --git a/elf/tst-audit26.c b/elf/tst-audit26.c
08c3a6
new file mode 100644
08c3a6
index 0000000000000000..3f920e83bac247a5
08c3a6
--- /dev/null
08c3a6
+++ b/elf/tst-audit26.c
08c3a6
@@ -0,0 +1,35 @@
08c3a6
+/* Check the usability of <dlfcn.h> functions in audit modules.
08c3a6
+   Copyright (C) 2022 Free Software Foundation, Inc.
08c3a6
+   This file is part of the GNU C Library.
08c3a6
+
08c3a6
+   The GNU C Library is free software; you can redistribute it and/or
08c3a6
+   modify it under the terms of the GNU Lesser General Public
08c3a6
+   License as published by the Free Software Foundation; either
08c3a6
+   version 2.1 of the License, or (at your option) any later version.
08c3a6
+
08c3a6
+   The GNU C Library is distributed in the hope that it will be useful,
08c3a6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
08c3a6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
08c3a6
+   Lesser General Public License for more details.
08c3a6
+
08c3a6
+   You should have received a copy of the GNU Lesser General Public
08c3a6
+   License along with the GNU C Library; if not, see
08c3a6
+   <https://www.gnu.org/licenses/>.  */
08c3a6
+
08c3a6
+#include <gnu/lib-names.h>
08c3a6
+
08c3a6
+#include <support/check.h>
08c3a6
+#include <support/xdlfcn.h>
08c3a6
+
08c3a6
+static int
08c3a6
+do_test (void)
08c3a6
+{
08c3a6
+  /* Check that the audit module has been loaded.  */
08c3a6
+  void *handle = xdlopen ("mapped to libc", RTLD_LOCAL | RTLD_NOW);
08c3a6
+  TEST_VERIFY (handle
08c3a6
+	       == xdlopen (LIBC_SO, RTLD_LOCAL | RTLD_NOW | RTLD_NOLOAD));
08c3a6
+
08c3a6
+  return 0;
08c3a6
+}
08c3a6
+
08c3a6
+#include <support/test-driver.c>
08c3a6
diff --git a/elf/tst-auditmod26.c b/elf/tst-auditmod26.c
08c3a6
new file mode 100644
08c3a6
index 0000000000000000..db7ba95abec20f53
08c3a6
--- /dev/null
08c3a6
+++ b/elf/tst-auditmod26.c
08c3a6
@@ -0,0 +1,104 @@
08c3a6
+/* Check the usability of <dlfcn.h> functions in audit modules.  Audit module.
08c3a6
+   Copyright (C) 2022 Free Software Foundation, Inc.
08c3a6
+   This file is part of the GNU C Library.
08c3a6
+
08c3a6
+   The GNU C Library is free software; you can redistribute it and/or
08c3a6
+   modify it under the terms of the GNU Lesser General Public
08c3a6
+   License as published by the Free Software Foundation; either
08c3a6
+   version 2.1 of the License, or (at your option) any later version.
08c3a6
+
08c3a6
+   The GNU C Library is distributed in the hope that it will be useful,
08c3a6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
08c3a6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
08c3a6
+   Lesser General Public License for more details.
08c3a6
+
08c3a6
+   You should have received a copy of the GNU Lesser General Public
08c3a6
+   License along with the GNU C Library; if not, see
08c3a6
+   <https://www.gnu.org/licenses/>.  */
08c3a6
+
08c3a6
+#include <dlfcn.h>
08c3a6
+#include <first-versions.h>
08c3a6
+#include <gnu/lib-names.h>
08c3a6
+#include <link.h>
08c3a6
+#include <stdio.h>
08c3a6
+#include <string.h>
08c3a6
+#include <unistd.h>
08c3a6
+
08c3a6
+#include <support/check.h>
08c3a6
+#include <support/xdlfcn.h>
08c3a6
+
08c3a6
+unsigned int
08c3a6
+la_version (unsigned int current)
08c3a6
+{
08c3a6
+  /* Exercise various <dlfcn.h> functions.  */
08c3a6
+
08c3a6
+  /* Check dlopen, dlsym, dlclose.   */
08c3a6
+  void *handle = xdlopen (LIBM_SO, RTLD_LOCAL | RTLD_NOW);
08c3a6
+  void *ptr = xdlsym (handle, "sincos");
08c3a6
+  TEST_VERIFY (ptr != NULL);
08c3a6
+  ptr = dlsym (handle, "SINCOS");
08c3a6
+  TEST_VERIFY (ptr == NULL);
08c3a6
+  const char *message = dlerror ();
08c3a6
+  TEST_VERIFY (strstr (message, ": undefined symbol: SINCOS") != NULL);
08c3a6
+  ptr = dlsym (handle, "SINCOS");
08c3a6
+  TEST_VERIFY (ptr == NULL);
08c3a6
+  xdlclose (handle);
08c3a6
+  TEST_COMPARE_STRING (dlerror (), NULL);
08c3a6
+
08c3a6
+  handle = xdlopen (LIBC_SO, RTLD_LOCAL | RTLD_NOW | RTLD_NOLOAD);
08c3a6
+
08c3a6
+  /* Check dlvsym.  _exit is unlikely to gain another symbol
08c3a6
+     version.  */
08c3a6
+  TEST_VERIFY (xdlsym (handle, "_exit")
08c3a6
+               == xdlvsym (handle, "_exit", FIRST_VERSION_libc__exit_STRING));
08c3a6
+
08c3a6
+  /* Check dlinfo.  */
08c3a6
+  {
08c3a6
+    void *handle2 = NULL;
08c3a6
+    TEST_COMPARE (dlinfo (handle, RTLD_DI_LINKMAP, &handle2), 0);
08c3a6
+    TEST_VERIFY (handle2 == handle);
08c3a6
+  }
08c3a6
+
08c3a6
+  /* Check dladdr and dladdr1.  */
08c3a6
+  Dl_info info = { };
08c3a6
+  TEST_VERIFY (dladdr (&_exit, &info) != 0);
08c3a6
+  if (strcmp (info.dli_sname, "_Exit") != 0) /* _Exit is an alias.  */
08c3a6
+    TEST_COMPARE_STRING (info.dli_sname, "_exit");
08c3a6
+  TEST_VERIFY (info.dli_saddr == &_exit);
08c3a6
+  TEST_VERIFY (strstr (info.dli_fname, LIBC_SO));
08c3a6
+  void *extra_info;
08c3a6
+  memset (&info, 0, sizeof (info));
08c3a6
+  TEST_VERIFY (dladdr1 (&_exit, &info, &extra_info, RTLD_DL_LINKMAP) != 0);
08c3a6
+  TEST_VERIFY (extra_info == handle);
08c3a6
+
08c3a6
+  /* Verify that dlmopen creates a new namespace.  */
08c3a6
+  void *dlmopen_handle = xdlmopen (LM_ID_NEWLM, LIBC_SO, RTLD_NOW);
08c3a6
+  TEST_VERIFY (dlmopen_handle != handle);
08c3a6
+  memset (&info, 0, sizeof (info));
08c3a6
+  extra_info = NULL;
08c3a6
+  ptr = xdlsym (dlmopen_handle, "_exit");
08c3a6
+  TEST_VERIFY (dladdr1 (ptr, &info, &extra_info, RTLD_DL_LINKMAP) != 0);
08c3a6
+  TEST_VERIFY (extra_info == dlmopen_handle);
08c3a6
+  xdlclose (dlmopen_handle);
08c3a6
+
08c3a6
+  /* Terminate the process with an error state.  This does not happen
08c3a6
+     automatically because the audit module state is not shared with
08c3a6
+     the main program.  */
08c3a6
+  if (support_record_failure_is_failed ())
08c3a6
+    {
08c3a6
+      fflush (stdout);
08c3a6
+      fflush (stderr);
08c3a6
+      _exit (1);
08c3a6
+    }
08c3a6
+
08c3a6
+  return LAV_CURRENT;
08c3a6
+}
08c3a6
+
08c3a6
+char *
08c3a6
+la_objsearch (const char *name, uintptr_t *cookie, unsigned int flag)
08c3a6
+{
08c3a6
+  if (strcmp (name, "mapped to libc") == 0)
08c3a6
+    return (char *) LIBC_SO;
08c3a6
+  else
08c3a6
+    return (char *) name;
08c3a6
+}