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