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