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