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