4c1956
Added $(objpfx)tst-audit18: $(libdl) in elf/Makefile since
4c1956
we still have $(libdl) in RHEL8.
4c1956
4c1956
commit ed3ce71f5c64c5f07cbde0ef03554ea8950d8f2c
4c1956
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
4c1956
Date:   Thu Nov 11 09:28:21 2021 -0300
4c1956
4c1956
    elf: Move la_activity (LA_ACT_ADD) after _dl_add_to_namespace_list() (BZ #28062)
4c1956
    
4c1956
    It ensures that the the namespace is guaranteed to not be empty.
4c1956
    
4c1956
    Checked on x86_64-linux-gnu.
4c1956
    
4c1956
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
4c1956
4c1956
Conflicts:
4c1956
	elf/Makefile
4c1956
	elf/dl-load.c
4c1956
		Conflict with missing MAP_ANON removal.
4c1956
4c1956
diff --git a/elf/Makefile b/elf/Makefile
4c1956
index b9751e8bd87c4f71..2312184692433313 100644
4c1956
--- a/elf/Makefile
4c1956
+++ b/elf/Makefile
4c1956
@@ -219,6 +219,7 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
4c1956
 	 tst-dlmopen-dlerror \
4c1956
 	 tst-dlmopen-gethostbyname \
4c1956
 	 tst-audit17 \
4c1956
+	 tst-audit18 \
4c1956
 #	 reldep9
4c1956
 tests-internal += loadtest unload unload2 circleload1 \
4c1956
 	 neededtest neededtest2 neededtest3 neededtest4 \
4c1956
@@ -354,6 +355,8 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
4c1956
 		libmarkermod5-5 tst-tls20mod-bad tst-tls21mod \
4c1956
 		tst-dlmopen-dlerror-mod \
4c1956
 		tst-dlmopen-gethostbyname-mod \
4c1956
+		tst-auditmod18 \
4c1956
+		tst-audit18mod \
4c1956
 
4c1956
 # Most modules build with _ISOMAC defined, but those filtered out
4c1956
 # depend on internal headers.
4c1956
@@ -1539,6 +1542,11 @@ $(objpfx)tst-auditmod17.so: $(objpfx)tst-auditmod17.os
4c1956
 CFLAGS-.os += $(call elide-stack-protector,.os,tst-auditmod17)
4c1956
 tst-audit17-ENV = LD_AUDIT=$(objpfx)tst-auditmod17.so
4c1956
 
4c1956
+$(objpfx)tst-audit18: $(libdl)
4c1956
+$(objpfx)tst-audit18.out: $(objpfx)tst-auditmod18.so \
4c1956
+			  $(objpfx)tst-audit18mod.so
4c1956
+tst-audit18-ARGS = -- $(host-test-program-cmd)
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/dl-load.c b/elf/dl-load.c
4c1956
index 303e6594f9af9b7e..de5aef5777045da5 100644
4c1956
--- a/elf/dl-load.c
4c1956
+++ b/elf/dl-load.c
4c1956
@@ -978,42 +978,6 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
4c1956
     }
4c1956
 #endif
4c1956
 
4c1956
-  /* Signal that we are going to add new objects.  */
4c1956
-  if (r->r_state == RT_CONSISTENT)
4c1956
-    {
4c1956
-#ifdef SHARED
4c1956
-      /* Auditing checkpoint: we are going to add new objects.  */
4c1956
-      if ((mode & __RTLD_AUDIT) == 0
4c1956
-	  && __glibc_unlikely (GLRO(dl_naudit) > 0))
4c1956
-	{
4c1956
-	  struct link_map *head = GL(dl_ns)[nsid]._ns_loaded;
4c1956
-	  /* Do not call the functions for any auditing object.  */
4c1956
-	  if (head->l_auditing == 0)
4c1956
-	    {
4c1956
-	      struct audit_ifaces *afct = GLRO(dl_audit);
4c1956
-	      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
4c1956
-		{
4c1956
-		  if (afct->activity != NULL)
4c1956
-		    afct->activity (&link_map_audit_state (head, cnt)->cookie,
4c1956
-				    LA_ACT_ADD);
4c1956
-
4c1956
-		  afct = afct->next;
4c1956
-		}
4c1956
-	    }
4c1956
-	}
4c1956
-#endif
4c1956
-
4c1956
-      /* Notify the debugger we have added some objects.  We need to
4c1956
-	 call _dl_debug_initialize in a static program in case dynamic
4c1956
-	 linking has not been used before.  */
4c1956
-      r->r_state = RT_ADD;
4c1956
-      _dl_debug_state ();
4c1956
-      LIBC_PROBE (map_start, 2, nsid, r);
4c1956
-      make_consistent = true;
4c1956
-    }
4c1956
-  else
4c1956
-    assert (r->r_state == RT_ADD);
4c1956
-
4c1956
   /* Enter the new object in the list of loaded objects.  */
4c1956
   l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
4c1956
   if (__glibc_unlikely (l == NULL))
4c1956
@@ -1432,6 +1396,44 @@ cannot enable executable stack as shared object requires");
4c1956
   /* Now that the object is fully initialized add it to the object list.  */
4c1956
   _dl_add_to_namespace_list (l, nsid);
4c1956
 
4c1956
+  /* Signal that we are going to add new objects.  */
4c1956
+  if (r->r_state == RT_CONSISTENT)
4c1956
+    {
4c1956
+#ifdef SHARED
4c1956
+      /* Auditing checkpoint: we are going to add new objects.  Since this
4c1956
+         is called after _dl_add_to_namespace_list the namespace is guaranteed
4c1956
+	 to not be empty.  */
4c1956
+      if ((mode & __RTLD_AUDIT) == 0
4c1956
+	  && __glibc_unlikely (GLRO(dl_naudit) > 0))
4c1956
+	{
4c1956
+	  struct link_map *head = GL(dl_ns)[nsid]._ns_loaded;
4c1956
+	  /* Do not call the functions for any auditing object.  */
4c1956
+	  if (head->l_auditing == 0)
4c1956
+	    {
4c1956
+	      struct audit_ifaces *afct = GLRO(dl_audit);
4c1956
+	      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
4c1956
+		{
4c1956
+		  if (afct->activity != NULL)
4c1956
+		    afct->activity (&link_map_audit_state (head, cnt)->cookie,
4c1956
+				    LA_ACT_ADD);
4c1956
+
4c1956
+		  afct = afct->next;
4c1956
+		}
4c1956
+	    }
4c1956
+	}
4c1956
+#endif
4c1956
+
4c1956
+      /* Notify the debugger we have added some objects.  We need to
4c1956
+	 call _dl_debug_initialize in a static program in case dynamic
4c1956
+	 linking has not been used before.  */
4c1956
+      r->r_state = RT_ADD;
4c1956
+      _dl_debug_state ();
4c1956
+      LIBC_PROBE (map_start, 2, nsid, r);
4c1956
+      make_consistent = true;
4c1956
+    }
4c1956
+  else
4c1956
+    assert (r->r_state == RT_ADD);
4c1956
+
4c1956
 #ifdef SHARED
4c1956
   /* Auditing checkpoint: we have a new object.  */
4c1956
   if (__glibc_unlikely (GLRO(dl_naudit) > 0)
4c1956
diff --git a/elf/tst-audit18.c b/elf/tst-audit18.c
4c1956
new file mode 100644
4c1956
index 0000000000000000..ef784908f60d50aa
4c1956
--- /dev/null
4c1956
+++ b/elf/tst-audit18.c
4c1956
@@ -0,0 +1,129 @@
4c1956
+/* Check DT_AUDIT with dlmopen.
4c1956
+   Copyright (C) 2021 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 <array_length.h>
4c1956
+#include <getopt.h>
4c1956
+#include <string.h>
4c1956
+#include <stdlib.h>
4c1956
+#include <unistd.h>
4c1956
+#include <gnu/lib-names.h>
4c1956
+#include <support/capture_subprocess.h>
4c1956
+#include <support/check.h>
4c1956
+#include <support/xdlfcn.h>
4c1956
+#include <support/xstdio.h>
4c1956
+#include <support/support.h>
4c1956
+
4c1956
+static int restart;
4c1956
+#define CMDLINE_OPTIONS \
4c1956
+  { "restart", no_argument, &restart, 1 },
4c1956
+
4c1956
+static int
4c1956
+handle_restart (void)
4c1956
+{
4c1956
+  {
4c1956
+    void *h = xdlmopen (LM_ID_NEWLM, LIBC_SO, RTLD_NOW);
4c1956
+
4c1956
+    pid_t (*s) (void) = xdlsym (h, "getpid");
4c1956
+    TEST_COMPARE (s (), getpid ());
4c1956
+
4c1956
+    xdlclose (h);
4c1956
+  }
4c1956
+
4c1956
+  {
4c1956
+    void *h = xdlmopen (LM_ID_NEWLM, "tst-audit18mod.so", RTLD_NOW);
4c1956
+
4c1956
+    int (*foo) (void) = xdlsym (h, "foo");
4c1956
+    TEST_COMPARE (foo (), 10);
4c1956
+
4c1956
+    xdlclose (h);
4c1956
+  }
4c1956
+
4c1956
+  return 0;
4c1956
+}
4c1956
+
4c1956
+static int
4c1956
+do_test (int argc, char *argv[])
4c1956
+{
4c1956
+  /* We must have either:
4c1956
+     - One our fource parameters left if called initially:
4c1956
+       + path to ld.so         optional
4c1956
+       + "--library-path"      optional
4c1956
+       + the library path      optional
4c1956
+       + the application name  */
4c1956
+
4c1956
+  if (restart)
4c1956
+    return handle_restart ();
4c1956
+
4c1956
+  char *spargv[9];
4c1956
+  int i = 0;
4c1956
+  for (; i < argc - 1; i++)
4c1956
+    spargv[i] = argv[i + 1];
4c1956
+  spargv[i++] = (char *) "--direct";
4c1956
+  spargv[i++] = (char *) "--restart";
4c1956
+  spargv[i] = NULL;
4c1956
+
4c1956
+  setenv ("LD_AUDIT", "tst-auditmod18.so", 0);
4c1956
+  struct support_capture_subprocess result
4c1956
+    = support_capture_subprogram (spargv[0], spargv);
4c1956
+  support_capture_subprocess_check (&result, "tst-audit18", 0, sc_allow_stderr);
4c1956
+
4c1956
+  struct
4c1956
+  {
4c1956
+    const char *name;
4c1956
+    bool found;
4c1956
+  } audit_iface[] =
4c1956
+  {
4c1956
+    { "la_version", false },
4c1956
+    { "la_objsearch", false },
4c1956
+    { "la_activity", false },
4c1956
+    { "la_objopen", false },
4c1956
+    { "la_objclose", false },
4c1956
+    { "la_preinit", false },
4c1956
+#if __WORDSIZE == 32
4c1956
+    { "la_symbind32", false },
4c1956
+#elif __WORDSIZE == 64
4c1956
+    { "la_symbind64", false },
4c1956
+#endif
4c1956
+  };
4c1956
+
4c1956
+  /* Some hooks are called more than once but the test only check if any
4c1956
+     is called at least once.  */
4c1956
+  FILE *out = fmemopen (result.err.buffer, result.err.length, "r");
4c1956
+  TEST_VERIFY (out != NULL);
4c1956
+  char *buffer = NULL;
4c1956
+  size_t buffer_length = 0;
4c1956
+  while (xgetline (&buffer, &buffer_length, out))
4c1956
+    {
4c1956
+      for (int i = 0; i < array_length (audit_iface); i++)
4c1956
+	if (strncmp (buffer, audit_iface[i].name,
4c1956
+		     strlen (audit_iface[i].name)) == 0)
4c1956
+	  audit_iface[i].found = true;
4c1956
+    }
4c1956
+  free (buffer);
4c1956
+  xfclose (out);
4c1956
+
4c1956
+  for (int i = 0; i < array_length (audit_iface); i++)
4c1956
+    TEST_COMPARE (audit_iface[i].found, true);
4c1956
+
4c1956
+  support_capture_subprocess_free (&result);
4c1956
+
4c1956
+  return 0;
4c1956
+}
4c1956
+
4c1956
+#define TEST_FUNCTION_ARGV do_test
4c1956
+#include <support/test-driver.c>
4c1956
diff --git a/elf/tst-audit18mod.c b/elf/tst-audit18mod.c
4c1956
new file mode 100644
4c1956
index 0000000000000000..096a9167c9f8353f
4c1956
--- /dev/null
4c1956
+++ b/elf/tst-audit18mod.c
4c1956
@@ -0,0 +1,23 @@
4c1956
+/* Check DT_AUDIT with dlmopen.
4c1956
+   Copyright (C) 2021 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
+int
4c1956
+foo (void)
4c1956
+{
4c1956
+  return 10;
4c1956
+}
4c1956
diff --git a/elf/tst-auditmod18.c b/elf/tst-auditmod18.c
4c1956
new file mode 100644
4c1956
index 0000000000000000..182992e9fdb1620c
4c1956
--- /dev/null
4c1956
+++ b/elf/tst-auditmod18.c
4c1956
@@ -0,0 +1,73 @@
4c1956
+/* Check DT_AUDIT with dlmopen.
4c1956
+   Copyright (C) 2021 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 <stdio.h>
4c1956
+#include <link.h>
4c1956
+
4c1956
+unsigned int
4c1956
+la_version (unsigned int version)
4c1956
+{
4c1956
+  fprintf (stderr, "%s\n", __func__);
4c1956
+  return LAV_CURRENT;
4c1956
+}
4c1956
+
4c1956
+char *
4c1956
+la_objsearch (const char *name, uintptr_t *cookie, unsigned int flag)
4c1956
+{
4c1956
+  fprintf (stderr, "%s\n", __func__);
4c1956
+  return (char *) name;
4c1956
+}
4c1956
+
4c1956
+void
4c1956
+la_activity (uintptr_t *cookie, unsigned int flag)
4c1956
+{
4c1956
+  fprintf (stderr, "%s\n", __func__);
4c1956
+}
4c1956
+
4c1956
+unsigned int
4c1956
+la_objopen (struct link_map *map, Lmid_t lmid, uintptr_t *cookie)
4c1956
+{
4c1956
+  fprintf (stderr, "%s\n", __func__);
4c1956
+  return LA_FLG_BINDTO | LA_FLG_BINDFROM;
4c1956
+}
4c1956
+
4c1956
+unsigned int
4c1956
+la_objclose (uintptr_t *cookie)
4c1956
+{
4c1956
+  fprintf (stderr, "%s\n", __func__);
4c1956
+  return 0;
4c1956
+}
4c1956
+
4c1956
+void
4c1956
+la_preinit (uintptr_t *cookie)
4c1956
+{
4c1956
+  fprintf (stderr, "%s\n", __func__);
4c1956
+}
4c1956
+
4c1956
+uintptr_t
4c1956
+#if __ELF_NATIVE_CLASS == 32
4c1956
+la_symbind32 (Elf32_Sym *sym, unsigned int ndx, uintptr_t *refcook,
4c1956
+              uintptr_t *defcook, unsigned int *flags, const char *symname)
4c1956
+#else
4c1956
+la_symbind64 (Elf64_Sym *sym, unsigned int ndx, uintptr_t *refcook,
4c1956
+              uintptr_t *defcook, unsigned int *flags, const char *symname)
4c1956
+#endif
4c1956
+{
4c1956
+  fprintf (stderr, "%s\n", __func__);
4c1956
+  return sym->st_value;
4c1956
+}