00c0d4
Backport of the new test from this upstream commit:
00c0d4
00c0d4
commit 8dcb6d0af07fda3607b541857e4f3970a74ed55b
00c0d4
Author: Florian Weimer <fweimer@redhat.com>
00c0d4
Date:   Tue Apr 26 14:23:02 2022 +0200
00c0d4
00c0d4
    dlfcn: Do not use rtld_active () to determine ld.so state (bug 29078)
00c0d4
00c0d4
    When audit modules are loaded, ld.so initialization is not yet
00c0d4
    complete, and rtld_active () returns false even though ld.so is
00c0d4
    mostly working.  Instead, the static dlopen hook is used, but that
00c0d4
    does not work at all because this is not a static dlopen situation.
00c0d4
00c0d4
    Commit 466c1ea15f461edb8e3ffaf5d86d708876343bbf ("dlfcn: Rework
00c0d4
    static dlopen hooks") moved the hook pointer into _rtld_global_ro,
00c0d4
    which means that separate protection is not needed anymore and the
00c0d4
    hook pointer can be checked directly.
00c0d4
00c0d4
    The guard for disabling libio vtable hardening in _IO_vtable_check
00c0d4
    should stay for now.
00c0d4
00c0d4
    Fixes commit 8e1472d2c1e25e6eabc2059170731365f6d5b3d1 ("ld.so:
00c0d4
    Examine GLRO to detect inactive loader [BZ #20204]").
00c0d4
00c0d4
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
00c0d4
00c0d4
Conflicts:
00c0d4
	dlfcn/dladdr.c
00c0d4
	dlfcn/dladdr1.c
00c0d4
	dlfcn/dlclose.c
00c0d4
	dlfcn/dlerror.c
00c0d4
	dlfcn/dlinfo.c
00c0d4
	dlfcn/dlmopen.c
00c0d4
	dlfcn/dlopen.c
00c0d4
	dlfcn/dlopenold.c
00c0d4
	dlfcn/dlsym.c
00c0d4
	dlfcn/dlvsym.c
00c0d4
	elf/dl-libc.c
00c0d4
	  (Code changes not needed.)
00c0d4
	elf/Makefile
00c0d4
	  (Usual test list conflicts.  Also added $(libdl).)
00c0d4
00c0d4
diff --git a/elf/Makefile b/elf/Makefile
00c0d4
index 6d39b400060a73f3..3fae27d32676caf9 100644
00c0d4
--- a/elf/Makefile
00c0d4
+++ b/elf/Makefile
00c0d4
@@ -353,8 +353,7 @@ tests += \
00c0d4
   tst-audit24d \
00c0d4
   tst-audit25a \
00c0d4
   tst-audit25b \
00c0d4
-  tst-audit8 \
00c0d4
-  tst-audit9 \
00c0d4
+  tst-audit26 \
00c0d4
   tst-auditmany \
00c0d4
   tst-auxobj \
00c0d4
   tst-auxobj-dlopen \
00c0d4
@@ -659,6 +658,7 @@ modules-names = \
00c0d4
   tst-auditmod24c \
00c0d4
   tst-auditmod24d \
00c0d4
   tst-auditmod25 \
00c0d4
+  tst-auditmod26 \
00c0d4
   tst-big-note-lib \
00c0d4
   tst-deep1mod1 \
00c0d4
   tst-deep1mod2 \
00c0d4
@@ -2145,6 +2145,11 @@ $(objpfx)tst-audit25b: $(objpfx)tst-audit25mod1.so \
00c0d4
 LDFLAGS-tst-audit25b = -Wl,-z,now
00c0d4
 tst-audit25b-ARGS = -- $(host-test-program-cmd)
00c0d4
 
00c0d4
+$(objpfx)tst-audit26: $(libdl)
00c0d4
+$(objpfx)tst-audit26.out: $(objpfx)tst-auditmod26.so
00c0d4
+$(objpfx)tst-auditmod26.so: $(libsupport) $(libdl)
00c0d4
+tst-audit26-ENV = LD_AUDIT=$(objpfx)tst-auditmod26.so
00c0d4
+
00c0d4
 # tst-sonamemove links against an older implementation of the library.
00c0d4
 LDFLAGS-tst-sonamemove-linkmod1.so = \
00c0d4
   -Wl,--version-script=tst-sonamemove-linkmod1.map \
00c0d4
diff --git a/elf/tst-audit26.c b/elf/tst-audit26.c
00c0d4
new file mode 100644
00c0d4
index 0000000000000000..3f920e83bac247a5
00c0d4
--- /dev/null
00c0d4
+++ b/elf/tst-audit26.c
00c0d4
@@ -0,0 +1,35 @@
00c0d4
+/* Check the usability of <dlfcn.h> functions in audit modules.
00c0d4
+   Copyright (C) 2022 Free Software Foundation, Inc.
00c0d4
+   This file is part of the GNU C Library.
00c0d4
+
00c0d4
+   The GNU C Library is free software; you can redistribute it and/or
00c0d4
+   modify it under the terms of the GNU Lesser General Public
00c0d4
+   License as published by the Free Software Foundation; either
00c0d4
+   version 2.1 of the License, or (at your option) any later version.
00c0d4
+
00c0d4
+   The GNU C Library is distributed in the hope that it will be useful,
00c0d4
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00c0d4
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00c0d4
+   Lesser General Public License for more details.
00c0d4
+
00c0d4
+   You should have received a copy of the GNU Lesser General Public
00c0d4
+   License along with the GNU C Library; if not, see
00c0d4
+   <https://www.gnu.org/licenses/>.  */
00c0d4
+
00c0d4
+#include <gnu/lib-names.h>
00c0d4
+
00c0d4
+#include <support/check.h>
00c0d4
+#include <support/xdlfcn.h>
00c0d4
+
00c0d4
+static int
00c0d4
+do_test (void)
00c0d4
+{
00c0d4
+  /* Check that the audit module has been loaded.  */
00c0d4
+  void *handle = xdlopen ("mapped to libc", RTLD_LOCAL | RTLD_NOW);
00c0d4
+  TEST_VERIFY (handle
00c0d4
+	       == xdlopen (LIBC_SO, RTLD_LOCAL | RTLD_NOW | RTLD_NOLOAD));
00c0d4
+
00c0d4
+  return 0;
00c0d4
+}
00c0d4
+
00c0d4
+#include <support/test-driver.c>
00c0d4
diff --git a/elf/tst-auditmod26.c b/elf/tst-auditmod26.c
00c0d4
new file mode 100644
00c0d4
index 0000000000000000..db7ba95abec20f53
00c0d4
--- /dev/null
00c0d4
+++ b/elf/tst-auditmod26.c
00c0d4
@@ -0,0 +1,104 @@
00c0d4
+/* Check the usability of <dlfcn.h> functions in audit modules.  Audit module.
00c0d4
+   Copyright (C) 2022 Free Software Foundation, Inc.
00c0d4
+   This file is part of the GNU C Library.
00c0d4
+
00c0d4
+   The GNU C Library is free software; you can redistribute it and/or
00c0d4
+   modify it under the terms of the GNU Lesser General Public
00c0d4
+   License as published by the Free Software Foundation; either
00c0d4
+   version 2.1 of the License, or (at your option) any later version.
00c0d4
+
00c0d4
+   The GNU C Library is distributed in the hope that it will be useful,
00c0d4
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00c0d4
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00c0d4
+   Lesser General Public License for more details.
00c0d4
+
00c0d4
+   You should have received a copy of the GNU Lesser General Public
00c0d4
+   License along with the GNU C Library; if not, see
00c0d4
+   <https://www.gnu.org/licenses/>.  */
00c0d4
+
00c0d4
+#include <dlfcn.h>
00c0d4
+#include <first-versions.h>
00c0d4
+#include <gnu/lib-names.h>
00c0d4
+#include <link.h>
00c0d4
+#include <stdio.h>
00c0d4
+#include <string.h>
00c0d4
+#include <unistd.h>
00c0d4
+
00c0d4
+#include <support/check.h>
00c0d4
+#include <support/xdlfcn.h>
00c0d4
+
00c0d4
+unsigned int
00c0d4
+la_version (unsigned int current)
00c0d4
+{
00c0d4
+  /* Exercise various <dlfcn.h> functions.  */
00c0d4
+
00c0d4
+  /* Check dlopen, dlsym, dlclose.   */
00c0d4
+  void *handle = xdlopen (LIBM_SO, RTLD_LOCAL | RTLD_NOW);
00c0d4
+  void *ptr = xdlsym (handle, "sincos");
00c0d4
+  TEST_VERIFY (ptr != NULL);
00c0d4
+  ptr = dlsym (handle, "SINCOS");
00c0d4
+  TEST_VERIFY (ptr == NULL);
00c0d4
+  const char *message = dlerror ();
00c0d4
+  TEST_VERIFY (strstr (message, ": undefined symbol: SINCOS") != NULL);
00c0d4
+  ptr = dlsym (handle, "SINCOS");
00c0d4
+  TEST_VERIFY (ptr == NULL);
00c0d4
+  xdlclose (handle);
00c0d4
+  TEST_COMPARE_STRING (dlerror (), NULL);
00c0d4
+
00c0d4
+  handle = xdlopen (LIBC_SO, RTLD_LOCAL | RTLD_NOW | RTLD_NOLOAD);
00c0d4
+
00c0d4
+  /* Check dlvsym.  _exit is unlikely to gain another symbol
00c0d4
+     version.  */
00c0d4
+  TEST_VERIFY (xdlsym (handle, "_exit")
00c0d4
+               == xdlvsym (handle, "_exit", FIRST_VERSION_libc__exit_STRING));
00c0d4
+
00c0d4
+  /* Check dlinfo.  */
00c0d4
+  {
00c0d4
+    void *handle2 = NULL;
00c0d4
+    TEST_COMPARE (dlinfo (handle, RTLD_DI_LINKMAP, &handle2), 0);
00c0d4
+    TEST_VERIFY (handle2 == handle);
00c0d4
+  }
00c0d4
+
00c0d4
+  /* Check dladdr and dladdr1.  */
00c0d4
+  Dl_info info = { };
00c0d4
+  TEST_VERIFY (dladdr (&_exit, &info) != 0);
00c0d4
+  if (strcmp (info.dli_sname, "_Exit") != 0) /* _Exit is an alias.  */
00c0d4
+    TEST_COMPARE_STRING (info.dli_sname, "_exit");
00c0d4
+  TEST_VERIFY (info.dli_saddr == &_exit);
00c0d4
+  TEST_VERIFY (strstr (info.dli_fname, LIBC_SO));
00c0d4
+  void *extra_info;
00c0d4
+  memset (&info, 0, sizeof (info));
00c0d4
+  TEST_VERIFY (dladdr1 (&_exit, &info, &extra_info, RTLD_DL_LINKMAP) != 0);
00c0d4
+  TEST_VERIFY (extra_info == handle);
00c0d4
+
00c0d4
+  /* Verify that dlmopen creates a new namespace.  */
00c0d4
+  void *dlmopen_handle = xdlmopen (LM_ID_NEWLM, LIBC_SO, RTLD_NOW);
00c0d4
+  TEST_VERIFY (dlmopen_handle != handle);
00c0d4
+  memset (&info, 0, sizeof (info));
00c0d4
+  extra_info = NULL;
00c0d4
+  ptr = xdlsym (dlmopen_handle, "_exit");
00c0d4
+  TEST_VERIFY (dladdr1 (ptr, &info, &extra_info, RTLD_DL_LINKMAP) != 0);
00c0d4
+  TEST_VERIFY (extra_info == dlmopen_handle);
00c0d4
+  xdlclose (dlmopen_handle);
00c0d4
+
00c0d4
+  /* Terminate the process with an error state.  This does not happen
00c0d4
+     automatically because the audit module state is not shared with
00c0d4
+     the main program.  */
00c0d4
+  if (support_record_failure_is_failed ())
00c0d4
+    {
00c0d4
+      fflush (stdout);
00c0d4
+      fflush (stderr);
00c0d4
+      _exit (1);
00c0d4
+    }
00c0d4
+
00c0d4
+  return LAV_CURRENT;
00c0d4
+}
00c0d4
+
00c0d4
+char *
00c0d4
+la_objsearch (const char *name, uintptr_t *cookie, unsigned int flag)
00c0d4
+{
00c0d4
+  if (strcmp (name, "mapped to libc") == 0)
00c0d4
+    return (char *) LIBC_SO;
00c0d4
+  else
00c0d4
+    return (char *) name;
00c0d4
+}