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