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