076f82
commit e25fe992132c460fecc1ab9fade185d5dd3f91ff
076f82
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
076f82
Date:   Thu Nov 11 09:28:21 2021 -0300
076f82
076f82
    elf: Move la_activity (LA_ACT_ADD) after _dl_add_to_namespace_list() (BZ #28062)
076f82
    
076f82
    It ensures that the the namespace is guaranteed to not be empty.
076f82
    
076f82
    Checked on x86_64-linux-gnu.
076f82
    
076f82
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
076f82
    (cherry picked from commit ed3ce71f5c64c5f07cbde0ef03554ea8950d8f2c)
076f82
    
076f82
    Resolved conflicts:
076f82
            elf/Makefile
076f82
076f82
diff --git a/elf/Makefile b/elf/Makefile
076f82
index 7fa80946ff3aae42..bf6da98bdd15a18d 100644
076f82
--- a/elf/Makefile
076f82
+++ b/elf/Makefile
076f82
@@ -363,6 +363,7 @@ tests += \
076f82
   tst-audit15 \
076f82
   tst-audit16 \
076f82
   tst-audit17 \
076f82
+  tst-audit18 \
076f82
   tst-auditmany \
076f82
   tst-auxobj \
076f82
   tst-auxobj-dlopen \
076f82
@@ -623,6 +624,7 @@ modules-names = \
076f82
   tst-audit12mod2 \
076f82
   tst-audit12mod3 \
076f82
   tst-audit13mod1 \
076f82
+  tst-audit18mod \
076f82
   tst-auditlogmod-1 \
076f82
   tst-auditlogmod-2 \
076f82
   tst-auditlogmod-3 \
076f82
@@ -640,6 +642,7 @@ modules-names = \
076f82
   tst-auditmod9b \
076f82
   tst-auditmod11 \
076f82
   tst-auditmod12 \
076f82
+  tst-auditmod18 \
076f82
   tst-auxvalmod \
076f82
   tst-big-note-lib \
076f82
   tst-deep1mod1 \
076f82
@@ -1999,6 +2002,10 @@ $(objpfx)tst-auditmod17.so: $(objpfx)tst-auditmod17.os
076f82
 CFLAGS-.os += $(call elide-stack-protector,.os,tst-auditmod17)
076f82
 tst-audit17-ENV = LD_AUDIT=$(objpfx)tst-auditmod17.so
076f82
 
076f82
+$(objpfx)tst-audit18.out: $(objpfx)tst-auditmod18.so \
076f82
+			  $(objpfx)tst-audit18mod.so
076f82
+tst-audit18-ARGS = -- $(host-test-program-cmd)
076f82
+
076f82
 # tst-sonamemove links against an older implementation of the library.
076f82
 LDFLAGS-tst-sonamemove-linkmod1.so = \
076f82
   -Wl,--version-script=tst-sonamemove-linkmod1.map \
076f82
diff --git a/elf/dl-load.c b/elf/dl-load.c
076f82
index a920b12a906a9dec..a8c6df3959f2b331 100644
076f82
--- a/elf/dl-load.c
076f82
+++ b/elf/dl-load.c
076f82
@@ -1054,42 +1054,6 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
076f82
   /* This is the ELF header.  We read it in `open_verify'.  */
076f82
   header = (void *) fbp->buf;
076f82
 
076f82
-  /* Signal that we are going to add new objects.  */
076f82
-  if (r->r_state == RT_CONSISTENT)
076f82
-    {
076f82
-#ifdef SHARED
076f82
-      /* Auditing checkpoint: we are going to add new objects.  */
076f82
-      if ((mode & __RTLD_AUDIT) == 0
076f82
-	  && __glibc_unlikely (GLRO(dl_naudit) > 0))
076f82
-	{
076f82
-	  struct link_map *head = GL(dl_ns)[nsid]._ns_loaded;
076f82
-	  /* Do not call the functions for any auditing object.  */
076f82
-	  if (head->l_auditing == 0)
076f82
-	    {
076f82
-	      struct audit_ifaces *afct = GLRO(dl_audit);
076f82
-	      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
076f82
-		{
076f82
-		  if (afct->activity != NULL)
076f82
-		    afct->activity (&link_map_audit_state (head, cnt)->cookie,
076f82
-				    LA_ACT_ADD);
076f82
-
076f82
-		  afct = afct->next;
076f82
-		}
076f82
-	    }
076f82
-	}
076f82
-#endif
076f82
-
076f82
-      /* Notify the debugger we have added some objects.  We need to
076f82
-	 call _dl_debug_initialize in a static program in case dynamic
076f82
-	 linking has not been used before.  */
076f82
-      r->r_state = RT_ADD;
076f82
-      _dl_debug_state ();
076f82
-      LIBC_PROBE (map_start, 2, nsid, r);
076f82
-      make_consistent = true;
076f82
-    }
076f82
-  else
076f82
-    assert (r->r_state == RT_ADD);
076f82
-
076f82
   /* Enter the new object in the list of loaded objects.  */
076f82
   l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
076f82
   if (__glibc_unlikely (l == NULL))
076f82
@@ -1511,6 +1475,44 @@ cannot enable executable stack as shared object requires");
076f82
   /* Now that the object is fully initialized add it to the object list.  */
076f82
   _dl_add_to_namespace_list (l, nsid);
076f82
 
076f82
+  /* Signal that we are going to add new objects.  */
076f82
+  if (r->r_state == RT_CONSISTENT)
076f82
+    {
076f82
+#ifdef SHARED
076f82
+      /* Auditing checkpoint: we are going to add new objects.  Since this
076f82
+         is called after _dl_add_to_namespace_list the namespace is guaranteed
076f82
+	 to not be empty.  */
076f82
+      if ((mode & __RTLD_AUDIT) == 0
076f82
+	  && __glibc_unlikely (GLRO(dl_naudit) > 0))
076f82
+	{
076f82
+	  struct link_map *head = GL(dl_ns)[nsid]._ns_loaded;
076f82
+	  /* Do not call the functions for any auditing object.  */
076f82
+	  if (head->l_auditing == 0)
076f82
+	    {
076f82
+	      struct audit_ifaces *afct = GLRO(dl_audit);
076f82
+	      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
076f82
+		{
076f82
+		  if (afct->activity != NULL)
076f82
+		    afct->activity (&link_map_audit_state (head, cnt)->cookie,
076f82
+				    LA_ACT_ADD);
076f82
+
076f82
+		  afct = afct->next;
076f82
+		}
076f82
+	    }
076f82
+	}
076f82
+#endif
076f82
+
076f82
+      /* Notify the debugger we have added some objects.  We need to
076f82
+	 call _dl_debug_initialize in a static program in case dynamic
076f82
+	 linking has not been used before.  */
076f82
+      r->r_state = RT_ADD;
076f82
+      _dl_debug_state ();
076f82
+      LIBC_PROBE (map_start, 2, nsid, r);
076f82
+      make_consistent = true;
076f82
+    }
076f82
+  else
076f82
+    assert (r->r_state == RT_ADD);
076f82
+
076f82
 #ifdef SHARED
076f82
   /* Auditing checkpoint: we have a new object.  */
076f82
   if (__glibc_unlikely (GLRO(dl_naudit) > 0)
076f82
diff --git a/elf/tst-audit18.c b/elf/tst-audit18.c
076f82
new file mode 100644
076f82
index 0000000000000000..ef784908f60d50aa
076f82
--- /dev/null
076f82
+++ b/elf/tst-audit18.c
076f82
@@ -0,0 +1,129 @@
076f82
+/* Check DT_AUDIT with dlmopen.
076f82
+   Copyright (C) 2021 Free Software Foundation, Inc.
076f82
+   This file is part of the GNU C Library.
076f82
+
076f82
+   The GNU C Library is free software; you can redistribute it and/or
076f82
+   modify it under the terms of the GNU Lesser General Public
076f82
+   License as published by the Free Software Foundation; either
076f82
+   version 2.1 of the License, or (at your option) any later version.
076f82
+
076f82
+   The GNU C Library is distributed in the hope that it will be useful,
076f82
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
076f82
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
076f82
+   Lesser General Public License for more details.
076f82
+
076f82
+   You should have received a copy of the GNU Lesser General Public
076f82
+   License along with the GNU C Library; if not, see
076f82
+   <https://www.gnu.org/licenses/>.  */
076f82
+
076f82
+#include <array_length.h>
076f82
+#include <getopt.h>
076f82
+#include <string.h>
076f82
+#include <stdlib.h>
076f82
+#include <unistd.h>
076f82
+#include <gnu/lib-names.h>
076f82
+#include <support/capture_subprocess.h>
076f82
+#include <support/check.h>
076f82
+#include <support/xdlfcn.h>
076f82
+#include <support/xstdio.h>
076f82
+#include <support/support.h>
076f82
+
076f82
+static int restart;
076f82
+#define CMDLINE_OPTIONS \
076f82
+  { "restart", no_argument, &restart, 1 },
076f82
+
076f82
+static int
076f82
+handle_restart (void)
076f82
+{
076f82
+  {
076f82
+    void *h = xdlmopen (LM_ID_NEWLM, LIBC_SO, RTLD_NOW);
076f82
+
076f82
+    pid_t (*s) (void) = xdlsym (h, "getpid");
076f82
+    TEST_COMPARE (s (), getpid ());
076f82
+
076f82
+    xdlclose (h);
076f82
+  }
076f82
+
076f82
+  {
076f82
+    void *h = xdlmopen (LM_ID_NEWLM, "tst-audit18mod.so", RTLD_NOW);
076f82
+
076f82
+    int (*foo) (void) = xdlsym (h, "foo");
076f82
+    TEST_COMPARE (foo (), 10);
076f82
+
076f82
+    xdlclose (h);
076f82
+  }
076f82
+
076f82
+  return 0;
076f82
+}
076f82
+
076f82
+static int
076f82
+do_test (int argc, char *argv[])
076f82
+{
076f82
+  /* We must have either:
076f82
+     - One our fource parameters left if called initially:
076f82
+       + path to ld.so         optional
076f82
+       + "--library-path"      optional
076f82
+       + the library path      optional
076f82
+       + the application name  */
076f82
+
076f82
+  if (restart)
076f82
+    return handle_restart ();
076f82
+
076f82
+  char *spargv[9];
076f82
+  int i = 0;
076f82
+  for (; i < argc - 1; i++)
076f82
+    spargv[i] = argv[i + 1];
076f82
+  spargv[i++] = (char *) "--direct";
076f82
+  spargv[i++] = (char *) "--restart";
076f82
+  spargv[i] = NULL;
076f82
+
076f82
+  setenv ("LD_AUDIT", "tst-auditmod18.so", 0);
076f82
+  struct support_capture_subprocess result
076f82
+    = support_capture_subprogram (spargv[0], spargv);
076f82
+  support_capture_subprocess_check (&result, "tst-audit18", 0, sc_allow_stderr);
076f82
+
076f82
+  struct
076f82
+  {
076f82
+    const char *name;
076f82
+    bool found;
076f82
+  } audit_iface[] =
076f82
+  {
076f82
+    { "la_version", false },
076f82
+    { "la_objsearch", false },
076f82
+    { "la_activity", false },
076f82
+    { "la_objopen", false },
076f82
+    { "la_objclose", false },
076f82
+    { "la_preinit", false },
076f82
+#if __WORDSIZE == 32
076f82
+    { "la_symbind32", false },
076f82
+#elif __WORDSIZE == 64
076f82
+    { "la_symbind64", false },
076f82
+#endif
076f82
+  };
076f82
+
076f82
+  /* Some hooks are called more than once but the test only check if any
076f82
+     is called at least once.  */
076f82
+  FILE *out = fmemopen (result.err.buffer, result.err.length, "r");
076f82
+  TEST_VERIFY (out != NULL);
076f82
+  char *buffer = NULL;
076f82
+  size_t buffer_length = 0;
076f82
+  while (xgetline (&buffer, &buffer_length, out))
076f82
+    {
076f82
+      for (int i = 0; i < array_length (audit_iface); i++)
076f82
+	if (strncmp (buffer, audit_iface[i].name,
076f82
+		     strlen (audit_iface[i].name)) == 0)
076f82
+	  audit_iface[i].found = true;
076f82
+    }
076f82
+  free (buffer);
076f82
+  xfclose (out);
076f82
+
076f82
+  for (int i = 0; i < array_length (audit_iface); i++)
076f82
+    TEST_COMPARE (audit_iface[i].found, true);
076f82
+
076f82
+  support_capture_subprocess_free (&result);
076f82
+
076f82
+  return 0;
076f82
+}
076f82
+
076f82
+#define TEST_FUNCTION_ARGV do_test
076f82
+#include <support/test-driver.c>
076f82
diff --git a/elf/tst-audit18mod.c b/elf/tst-audit18mod.c
076f82
new file mode 100644
076f82
index 0000000000000000..096a9167c9f8353f
076f82
--- /dev/null
076f82
+++ b/elf/tst-audit18mod.c
076f82
@@ -0,0 +1,23 @@
076f82
+/* Check DT_AUDIT with dlmopen.
076f82
+   Copyright (C) 2021 Free Software Foundation, Inc.
076f82
+   This file is part of the GNU C Library.
076f82
+
076f82
+   The GNU C Library is free software; you can redistribute it and/or
076f82
+   modify it under the terms of the GNU Lesser General Public
076f82
+   License as published by the Free Software Foundation; either
076f82
+   version 2.1 of the License, or (at your option) any later version.
076f82
+
076f82
+   The GNU C Library is distributed in the hope that it will be useful,
076f82
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
076f82
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
076f82
+   Lesser General Public License for more details.
076f82
+
076f82
+   You should have received a copy of the GNU Lesser General Public
076f82
+   License along with the GNU C Library; if not, see
076f82
+   <https://www.gnu.org/licenses/>.  */
076f82
+
076f82
+int
076f82
+foo (void)
076f82
+{
076f82
+  return 10;
076f82
+}
076f82
diff --git a/elf/tst-auditmod18.c b/elf/tst-auditmod18.c
076f82
new file mode 100644
076f82
index 0000000000000000..182992e9fdb1620c
076f82
--- /dev/null
076f82
+++ b/elf/tst-auditmod18.c
076f82
@@ -0,0 +1,73 @@
076f82
+/* Check DT_AUDIT with dlmopen.
076f82
+   Copyright (C) 2021 Free Software Foundation, Inc.
076f82
+   This file is part of the GNU C Library.
076f82
+
076f82
+   The GNU C Library is free software; you can redistribute it and/or
076f82
+   modify it under the terms of the GNU Lesser General Public
076f82
+   License as published by the Free Software Foundation; either
076f82
+   version 2.1 of the License, or (at your option) any later version.
076f82
+
076f82
+   The GNU C Library is distributed in the hope that it will be useful,
076f82
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
076f82
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
076f82
+   Lesser General Public License for more details.
076f82
+
076f82
+   You should have received a copy of the GNU Lesser General Public
076f82
+   License along with the GNU C Library; if not, see
076f82
+   <https://www.gnu.org/licenses/>.  */
076f82
+
076f82
+#include <stdio.h>
076f82
+#include <link.h>
076f82
+
076f82
+unsigned int
076f82
+la_version (unsigned int version)
076f82
+{
076f82
+  fprintf (stderr, "%s\n", __func__);
076f82
+  return LAV_CURRENT;
076f82
+}
076f82
+
076f82
+char *
076f82
+la_objsearch (const char *name, uintptr_t *cookie, unsigned int flag)
076f82
+{
076f82
+  fprintf (stderr, "%s\n", __func__);
076f82
+  return (char *) name;
076f82
+}
076f82
+
076f82
+void
076f82
+la_activity (uintptr_t *cookie, unsigned int flag)
076f82
+{
076f82
+  fprintf (stderr, "%s\n", __func__);
076f82
+}
076f82
+
076f82
+unsigned int
076f82
+la_objopen (struct link_map *map, Lmid_t lmid, uintptr_t *cookie)
076f82
+{
076f82
+  fprintf (stderr, "%s\n", __func__);
076f82
+  return LA_FLG_BINDTO | LA_FLG_BINDFROM;
076f82
+}
076f82
+
076f82
+unsigned int
076f82
+la_objclose (uintptr_t *cookie)
076f82
+{
076f82
+  fprintf (stderr, "%s\n", __func__);
076f82
+  return 0;
076f82
+}
076f82
+
076f82
+void
076f82
+la_preinit (uintptr_t *cookie)
076f82
+{
076f82
+  fprintf (stderr, "%s\n", __func__);
076f82
+}
076f82
+
076f82
+uintptr_t
076f82
+#if __ELF_NATIVE_CLASS == 32
076f82
+la_symbind32 (Elf32_Sym *sym, unsigned int ndx, uintptr_t *refcook,
076f82
+              uintptr_t *defcook, unsigned int *flags, const char *symname)
076f82
+#else
076f82
+la_symbind64 (Elf64_Sym *sym, unsigned int ndx, uintptr_t *refcook,
076f82
+              uintptr_t *defcook, unsigned int *flags, const char *symname)
076f82
+#endif
076f82
+{
076f82
+  fprintf (stderr, "%s\n", __func__);
076f82
+  return sym->st_value;
076f82
+}