08c3a6
commit 98047ba95caf9ed596908ca73a22070c5e27597b
08c3a6
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
08c3a6
Date:   Mon Jan 24 10:46:15 2022 -0300
08c3a6
08c3a6
    elf: Add la_activity during application exit
08c3a6
    
08c3a6
    la_activity is not called during application exit, even though
08c3a6
    la_objclose is.
08c3a6
    
08c3a6
    Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu.
08c3a6
    
08c3a6
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
08c3a6
    Tested-by: Carlos O'Donell <carlos@redhat.com>
08c3a6
    (cherry picked from commit 5fa11a2bc94c912c3b25860065086902674537ba)
08c3a6
08c3a6
diff --git a/elf/Makefile b/elf/Makefile
08c3a6
index 91b2269257523a64..407aaeaeb8c84020 100644
08c3a6
--- a/elf/Makefile
08c3a6
+++ b/elf/Makefile
08c3a6
@@ -368,6 +368,7 @@ tests += \
08c3a6
   tst-audit19b \
08c3a6
   tst-audit20 \
08c3a6
   tst-audit22 \
08c3a6
+  tst-audit23 \
08c3a6
   tst-auditmany \
08c3a6
   tst-auxobj \
08c3a6
   tst-auxobj-dlopen \
08c3a6
@@ -631,6 +632,7 @@ modules-names = \
08c3a6
   tst-audit13mod1 \
08c3a6
   tst-audit18mod \
08c3a6
   tst-audit19bmod \
08c3a6
+  tst-audit23mod \
08c3a6
   tst-auditlogmod-1 \
08c3a6
   tst-auditlogmod-2 \
08c3a6
   tst-auditlogmod-3 \
08c3a6
@@ -653,6 +655,7 @@ modules-names = \
08c3a6
   tst-auditmod19b \
08c3a6
   tst-auditmod20 \
08c3a6
   tst-auditmod22 \
08c3a6
+  tst-auditmod23 \
08c3a6
   tst-auxvalmod \
08c3a6
   tst-big-note-lib \
08c3a6
   tst-deep1mod1 \
08c3a6
@@ -2045,6 +2048,10 @@ tst-audit20-ENV = LD_AUDIT=$(objpfx)tst-auditmod20.so
08c3a6
 $(objpfx)tst-audit22.out: $(objpfx)tst-auditmod22.so
08c3a6
 tst-audit22-ARGS = -- $(host-test-program-cmd)
08c3a6
 
08c3a6
+$(objpfx)tst-audit23.out: $(objpfx)tst-auditmod23.so \
08c3a6
+			  $(objpfx)tst-audit23mod.so
08c3a6
+tst-audit23-ARGS = -- $(host-test-program-cmd)
08c3a6
+
08c3a6
 # tst-sonamemove links against an older implementation of the library.
08c3a6
 LDFLAGS-tst-sonamemove-linkmod1.so = \
08c3a6
   -Wl,--version-script=tst-sonamemove-linkmod1.map \
08c3a6
diff --git a/elf/dl-fini.c b/elf/dl-fini.c
08c3a6
index b789cfb9f2ac6c85..fa876da0ffa1cf97 100644
08c3a6
--- a/elf/dl-fini.c
08c3a6
+++ b/elf/dl-fini.c
08c3a6
@@ -64,6 +64,10 @@ _dl_fini (void)
08c3a6
 	__rtld_lock_unlock_recursive (GL(dl_load_lock));
08c3a6
       else
08c3a6
 	{
08c3a6
+#ifdef SHARED
08c3a6
+	  _dl_audit_activity_nsid (ns, LA_ACT_DELETE);
08c3a6
+#endif
08c3a6
+
08c3a6
 	  /* Now we can allocate an array to hold all the pointers and
08c3a6
 	     copy the pointers in.  */
08c3a6
 	  struct link_map *maps[nloaded];
08c3a6
@@ -153,6 +157,10 @@ _dl_fini (void)
08c3a6
 	      /* Correct the previous increment.  */
08c3a6
 	      --l->l_direct_opencount;
08c3a6
 	    }
08c3a6
+
08c3a6
+#ifdef SHARED
08c3a6
+	  _dl_audit_activity_nsid (ns, LA_ACT_CONSISTENT);
08c3a6
+#endif
08c3a6
 	}
08c3a6
     }
08c3a6
 
08c3a6
diff --git a/elf/tst-audit23.c b/elf/tst-audit23.c
08c3a6
new file mode 100644
08c3a6
index 0000000000000000..4904cf1340a97ee1
08c3a6
--- /dev/null
08c3a6
+++ b/elf/tst-audit23.c
08c3a6
@@ -0,0 +1,239 @@
08c3a6
+/* Check for expected la_objopen and la_objeclose for all objects.
08c3a6
+   Copyright (C) 2022 Free Software Foundation, Inc.
08c3a6
+   This file is part of the GNU C Library.
08c3a6
+
08c3a6
+   The GNU C Library is free software; you can redistribute it and/or
08c3a6
+   modify it under the terms of the GNU Lesser General Public
08c3a6
+   License as published by the Free Software Foundation; either
08c3a6
+   version 2.1 of the License, or (at your option) any later version.
08c3a6
+
08c3a6
+   The GNU C Library is distributed in the hope that it will be useful,
08c3a6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
08c3a6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
08c3a6
+   Lesser General Public License for more details.
08c3a6
+
08c3a6
+   You should have received a copy of the GNU Lesser General Public
08c3a6
+   License along with the GNU C Library; if not, see
08c3a6
+   <https://www.gnu.org/licenses/>.  */
08c3a6
+
08c3a6
+#include <array_length.h>
08c3a6
+#include <errno.h>
08c3a6
+#include <getopt.h>
08c3a6
+#include <link.h>
08c3a6
+#include <limits.h>
08c3a6
+#include <inttypes.h>
08c3a6
+#include <gnu/lib-names.h>
08c3a6
+#include <string.h>
08c3a6
+#include <stdlib.h>
08c3a6
+#include <support/capture_subprocess.h>
08c3a6
+#include <support/check.h>
08c3a6
+#include <support/xstdio.h>
08c3a6
+#include <support/xdlfcn.h>
08c3a6
+#include <support/support.h>
08c3a6
+
08c3a6
+static int restart;
08c3a6
+#define CMDLINE_OPTIONS \
08c3a6
+  { "restart", no_argument, &restart, 1 },
08c3a6
+
08c3a6
+static int
08c3a6
+handle_restart (void)
08c3a6
+{
08c3a6
+  xdlopen ("tst-audit23mod.so", RTLD_NOW);
08c3a6
+  xdlmopen (LM_ID_NEWLM, LIBC_SO, RTLD_NOW);
08c3a6
+
08c3a6
+  return 0;
08c3a6
+}
08c3a6
+
08c3a6
+static inline bool
08c3a6
+startswith (const char *str, const char *pre)
08c3a6
+{
08c3a6
+  size_t lenpre = strlen (pre);
08c3a6
+  size_t lenstr = strlen (str);
08c3a6
+  return lenstr >= lenpre && memcmp (pre, str, lenpre) == 0;
08c3a6
+}
08c3a6
+
08c3a6
+static inline bool
08c3a6
+is_vdso (const char *str)
08c3a6
+{
08c3a6
+  return startswith (str, "linux-gate")
08c3a6
+	 || startswith (str, "linux-vdso");
08c3a6
+}
08c3a6
+
08c3a6
+static int
08c3a6
+do_test (int argc, char *argv[])
08c3a6
+{
08c3a6
+  /* We must have either:
08c3a6
+     - One or four parameters left if called initially:
08c3a6
+       + path to ld.so         optional
08c3a6
+       + "--library-path"      optional
08c3a6
+       + the library path      optional
08c3a6
+       + the application name  */
08c3a6
+  if (restart)
08c3a6
+    return handle_restart ();
08c3a6
+
08c3a6
+  char *spargv[9];
08c3a6
+  TEST_VERIFY_EXIT (((argc - 1) + 3) < array_length (spargv));
08c3a6
+  int i = 0;
08c3a6
+  for (; i < argc - 1; i++)
08c3a6
+    spargv[i] = argv[i + 1];
08c3a6
+  spargv[i++] = (char *) "--direct";
08c3a6
+  spargv[i++] = (char *) "--restart";
08c3a6
+  spargv[i] = NULL;
08c3a6
+
08c3a6
+  setenv ("LD_AUDIT", "tst-auditmod23.so", 0);
08c3a6
+  struct support_capture_subprocess result
08c3a6
+    = support_capture_subprogram (spargv[0], spargv);
08c3a6
+  support_capture_subprocess_check (&result, "tst-audit22", 0, sc_allow_stderr);
08c3a6
+
08c3a6
+  /* The expected la_objopen/la_objclose:
08c3a6
+     1. executable
08c3a6
+     2. loader
08c3a6
+     3. libc.so
08c3a6
+     4. tst-audit23mod.so
08c3a6
+     5. libc.so (LM_ID_NEWLM).
08c3a6
+     6. vdso (optional and ignored).  */
08c3a6
+  enum { max_objs = 6 };
08c3a6
+  struct la_obj_t
08c3a6
+  {
08c3a6
+    char *lname;
08c3a6
+    uintptr_t laddr;
08c3a6
+    Lmid_t lmid;
08c3a6
+    bool closed;
08c3a6
+  } objs[max_objs] = { [0 ... max_objs-1] = { .closed = false } };
08c3a6
+  size_t nobjs = 0;
08c3a6
+
08c3a6
+  /* The expected namespaces are one for the audit module, one for the
08c3a6
+     application, and another for the dlmopen on handle_restart.  */
08c3a6
+  enum { max_ns = 3 };
08c3a6
+  uintptr_t acts[max_ns] = { 0 };
08c3a6
+  size_t nacts = 0;
08c3a6
+  int last_act = -1;
08c3a6
+  uintptr_t last_act_cookie = -1;
08c3a6
+  bool seen_first_objclose = false;
08c3a6
+
08c3a6
+  FILE *out = fmemopen (result.err.buffer, result.err.length, "r");
08c3a6
+  TEST_VERIFY (out != NULL);
08c3a6
+  char *buffer = NULL;
08c3a6
+  size_t buffer_length = 0;
08c3a6
+  while (xgetline (&buffer, &buffer_length, out))
08c3a6
+    {
08c3a6
+      if (startswith (buffer, "la_activity: "))
08c3a6
+	{
08c3a6
+	  uintptr_t cookie;
08c3a6
+	  int this_act;
08c3a6
+	  int r = sscanf (buffer, "la_activity: %d %"SCNxPTR"", &this_act,
08c3a6
+			  &cookie);
08c3a6
+	  TEST_COMPARE (r, 2);
08c3a6
+
08c3a6
+	  /* The cookie identifies the object at the head of the link map,
08c3a6
+	     so we only add a new namespace if it changes from the previous
08c3a6
+	     one.  This works since dlmopen is the last in the test body.  */
08c3a6
+	  if (cookie != last_act_cookie && last_act_cookie != -1)
08c3a6
+	    TEST_COMPARE (last_act, LA_ACT_CONSISTENT);
08c3a6
+
08c3a6
+	  if (this_act == LA_ACT_ADD && acts[nacts] != cookie)
08c3a6
+	    {
08c3a6
+	      acts[nacts++] = cookie;
08c3a6
+	      last_act_cookie = cookie;
08c3a6
+	    }
08c3a6
+	  /* The LA_ACT_DELETE is called in the reverse order of LA_ACT_ADD
08c3a6
+	     at program termination (if the tests adds a dlclose or a library
08c3a6
+	     with extra dependencies this will need to be adapted).  */
08c3a6
+	  else if (this_act == LA_ACT_DELETE)
08c3a6
+	    {
08c3a6
+	      last_act_cookie = acts[--nacts];
08c3a6
+	      TEST_COMPARE (acts[nacts], cookie);
08c3a6
+	      acts[nacts] = 0;
08c3a6
+	    }
08c3a6
+	  else if (this_act == LA_ACT_CONSISTENT)
08c3a6
+	    {
08c3a6
+	      TEST_COMPARE (cookie, last_act_cookie);
08c3a6
+
08c3a6
+	      /* LA_ACT_DELETE must always be followed by an la_objclose.  */
08c3a6
+	      if (last_act == LA_ACT_DELETE)
08c3a6
+		TEST_COMPARE (seen_first_objclose, true);
08c3a6
+	      else
08c3a6
+		TEST_COMPARE (last_act, LA_ACT_ADD);
08c3a6
+	    }
08c3a6
+
08c3a6
+	  last_act = this_act;
08c3a6
+	  seen_first_objclose = false;
08c3a6
+	}
08c3a6
+      else if (startswith (buffer, "la_objopen: "))
08c3a6
+	{
08c3a6
+	  char *lname;
08c3a6
+	  uintptr_t laddr;
08c3a6
+	  Lmid_t lmid;
08c3a6
+	  uintptr_t cookie;
08c3a6
+	  int r = sscanf (buffer, "la_objopen: %"SCNxPTR"  %ms %"SCNxPTR" %ld",
08c3a6
+			  &cookie, &lname, &laddr, &lmid);
08c3a6
+	  TEST_COMPARE (r, 4);
08c3a6
+
08c3a6
+	  /* la_objclose is not triggered by vDSO because glibc does not
08c3a6
+	     unload it.  */
08c3a6
+	  if (is_vdso (lname))
08c3a6
+	    continue;
08c3a6
+	  if (nobjs == max_objs)
08c3a6
+	    FAIL_EXIT1 ("non expected la_objopen: %s %"PRIxPTR" %ld",
08c3a6
+			lname, laddr, lmid);
08c3a6
+	  objs[nobjs].lname = lname;
08c3a6
+	  objs[nobjs].laddr = laddr;
08c3a6
+	  objs[nobjs].lmid = lmid;
08c3a6
+	  objs[nobjs].closed = false;
08c3a6
+	  nobjs++;
08c3a6
+
08c3a6
+	  /* This indirectly checks that la_objopen always comes before
08c3a6
+	     la_objclose btween la_activity calls.  */
08c3a6
+	  seen_first_objclose = false;
08c3a6
+	}
08c3a6
+      else if (startswith (buffer, "la_objclose: "))
08c3a6
+	{
08c3a6
+	  char *lname;
08c3a6
+	  uintptr_t laddr;
08c3a6
+	  Lmid_t lmid;
08c3a6
+	  uintptr_t cookie;
08c3a6
+	  int r = sscanf (buffer, "la_objclose: %"SCNxPTR" %ms %"SCNxPTR" %ld",
08c3a6
+			  &cookie, &lname, &laddr, &lmid);
08c3a6
+	  TEST_COMPARE (r, 4);
08c3a6
+
08c3a6
+	  for (size_t i = 0; i < nobjs; i++)
08c3a6
+	    {
08c3a6
+	      if (strcmp (lname, objs[i].lname) == 0 && lmid == objs[i].lmid)
08c3a6
+		{
08c3a6
+		  TEST_COMPARE (objs[i].closed, false);
08c3a6
+		  objs[i].closed = true;
08c3a6
+		  break;
08c3a6
+		}
08c3a6
+	    }
08c3a6
+
08c3a6
+	  /* la_objclose should be called after la_activity(LA_ACT_DELETE) for
08c3a6
+	     the closed object's namespace.  */
08c3a6
+	  TEST_COMPARE (last_act, LA_ACT_DELETE);
08c3a6
+	  if (!seen_first_objclose)
08c3a6
+	    {
08c3a6
+	      TEST_COMPARE (last_act_cookie, cookie);
08c3a6
+	      seen_first_objclose = true;
08c3a6
+	    }
08c3a6
+	}
08c3a6
+    }
08c3a6
+
08c3a6
+  for (size_t i = 0; i < nobjs; i++)
08c3a6
+    {
08c3a6
+      TEST_COMPARE (objs[i].closed, true);
08c3a6
+      free (objs[i].lname);
08c3a6
+    }
08c3a6
+
08c3a6
+  /* la_activity(LA_ACT_CONSISTENT) should be the last callback received.
08c3a6
+     Since only one link map may be not-CONSISTENT at a time, this also
08c3a6
+     ensures la_activity(LA_ACT_CONSISTENT) is the last callback received
08c3a6
+     for every namespace.  */
08c3a6
+  TEST_COMPARE (last_act, LA_ACT_CONSISTENT);
08c3a6
+
08c3a6
+  free (buffer);
08c3a6
+  xfclose (out);
08c3a6
+
08c3a6
+  return 0;
08c3a6
+}
08c3a6
+
08c3a6
+#define TEST_FUNCTION_ARGV do_test
08c3a6
+#include <support/test-driver.c>
08c3a6
diff --git a/elf/tst-audit23mod.c b/elf/tst-audit23mod.c
08c3a6
new file mode 100644
08c3a6
index 0000000000000000..30315687037d25e8
08c3a6
--- /dev/null
08c3a6
+++ b/elf/tst-audit23mod.c
08c3a6
@@ -0,0 +1,23 @@
08c3a6
+/* Extra module for tst-audit23
08c3a6
+   Copyright (C) 2022 Free Software Foundation, Inc.
08c3a6
+   This file is part of the GNU C Library.
08c3a6
+
08c3a6
+   The GNU C Library is free software; you can redistribute it and/or
08c3a6
+   modify it under the terms of the GNU Lesser General Public
08c3a6
+   License as published by the Free Software Foundation; either
08c3a6
+   version 2.1 of the License, or (at your option) any later version.
08c3a6
+
08c3a6
+   The GNU C Library is distributed in the hope that it will be useful,
08c3a6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
08c3a6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
08c3a6
+   Lesser General Public License for more details.
08c3a6
+
08c3a6
+   You should have received a copy of the GNU Lesser General Public
08c3a6
+   License along with the GNU C Library; if not, see
08c3a6
+   <https://www.gnu.org/licenses/>.  */
08c3a6
+
08c3a6
+int
08c3a6
+foo (void)
08c3a6
+{
08c3a6
+  return 0;
08c3a6
+}
08c3a6
diff --git a/elf/tst-auditmod23.c b/elf/tst-auditmod23.c
08c3a6
new file mode 100644
08c3a6
index 0000000000000000..d7c60d7a5cbc4f8a
08c3a6
--- /dev/null
08c3a6
+++ b/elf/tst-auditmod23.c
08c3a6
@@ -0,0 +1,74 @@
08c3a6
+/* Audit module loaded by tst-audit23.
08c3a6
+   Copyright (C) 2022 Free Software Foundation, Inc.
08c3a6
+   This file is part of the GNU C Library.
08c3a6
+
08c3a6
+   The GNU C Library is free software; you can redistribute it and/or
08c3a6
+   modify it under the terms of the GNU Lesser General Public
08c3a6
+   License as published by the Free Software Foundation; either
08c3a6
+   version 2.1 of the License, or (at your option) any later version.
08c3a6
+
08c3a6
+   The GNU C Library is distributed in the hope that it will be useful,
08c3a6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
08c3a6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
08c3a6
+   Lesser General Public License for more details.
08c3a6
+
08c3a6
+   You should have received a copy of the GNU Lesser General Public
08c3a6
+   License along with the GNU C Library; if not, see
08c3a6
+   <https://www.gnu.org/licenses/>.  */
08c3a6
+
08c3a6
+#include <link.h>
08c3a6
+#include <inttypes.h>
08c3a6
+#include <stdlib.h>
08c3a6
+#include <stdio.h>
08c3a6
+#include <string.h>
08c3a6
+#include <sys/auxv.h>
08c3a6
+
08c3a6
+unsigned int
08c3a6
+la_version (unsigned int version)
08c3a6
+{
08c3a6
+  return LAV_CURRENT;
08c3a6
+}
08c3a6
+
08c3a6
+struct map_desc_t
08c3a6
+{
08c3a6
+  char *lname;
08c3a6
+  uintptr_t laddr;
08c3a6
+  Lmid_t lmid;
08c3a6
+};
08c3a6
+
08c3a6
+void
08c3a6
+la_activity (uintptr_t *cookie, unsigned int flag)
08c3a6
+{
08c3a6
+  fprintf (stderr, "%s: %d %"PRIxPTR"\n", __func__, flag, (uintptr_t) cookie);
08c3a6
+}
08c3a6
+
08c3a6
+unsigned int
08c3a6
+la_objopen (struct link_map *map, Lmid_t lmid, uintptr_t *cookie)
08c3a6
+{
08c3a6
+  const char *l_name = map->l_name[0] == '\0' ? "mainapp" : map->l_name;
08c3a6
+  fprintf (stderr, "%s: %"PRIxPTR" %s %"PRIxPTR" %ld\n", __func__,
08c3a6
+	   (uintptr_t) cookie, l_name, map->l_addr, lmid);
08c3a6
+
08c3a6
+  struct map_desc_t *map_desc = malloc (sizeof (struct map_desc_t));
08c3a6
+  if (map_desc == NULL)
08c3a6
+    abort ();
08c3a6
+
08c3a6
+  map_desc->lname = strdup (l_name);
08c3a6
+  map_desc->laddr = map->l_addr;
08c3a6
+  map_desc->lmid = lmid;
08c3a6
+
08c3a6
+  *cookie = (uintptr_t) map_desc;
08c3a6
+
08c3a6
+  return 0;
08c3a6
+}
08c3a6
+
08c3a6
+unsigned int
08c3a6
+la_objclose (uintptr_t *cookie)
08c3a6
+{
08c3a6
+  struct map_desc_t *map_desc = (struct map_desc_t *) *cookie;
08c3a6
+  fprintf (stderr, "%s: %"PRIxPTR" %s %"PRIxPTR" %ld\n", __func__,
08c3a6
+	   (uintptr_t) cookie, map_desc->lname, map_desc->laddr,
08c3a6
+	   map_desc->lmid);
08c3a6
+
08c3a6
+  return 0;
08c3a6
+}