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