abe59f
commit f0e23d34a7bdf6b90fba954ee741419171ac41b2
abe59f
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
abe59f
Date:   Mon Jul 19 18:42:26 2021 -0300
abe59f
abe59f
    elf: Issue audit la_objopen for vDSO
abe59f
    
abe59f
    The vDSO is is listed in the link_map chain, but is never the subject of
abe59f
    an la_objopen call.  A new internal flag __RTLD_VDSO is added that
abe59f
    acts as __RTLD_OPENEXEC to allocate the required 'struct auditstate'
abe59f
    extra space for the 'struct link_map'.
abe59f
    
abe59f
    The return value from the callback is currently ignored, since there
abe59f
    is no PLT call involved by glibc when using the vDSO, neither the vDSO
abe59f
    are exported directly.
abe59f
    
abe59f
    Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu.
abe59f
    
abe59f
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
abe59f
abe59f
Conflicts:
abe59f
	elf/Makefile
abe59f
abe59f
diff --git a/elf/Makefile b/elf/Makefile
abe59f
index d8d9734df0fea9a8..f047c1cce0c55da0 100644
abe59f
--- a/elf/Makefile
abe59f
+++ b/elf/Makefile
abe59f
@@ -222,6 +222,7 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
abe59f
 	 tst-audit17 \
abe59f
 	 tst-audit18 \
abe59f
 	 tst-audit19b \
abe59f
+	 tst-audit22 \
abe59f
 #	 reldep9
abe59f
 tests-internal += loadtest unload unload2 circleload1 \
abe59f
 	 neededtest neededtest2 neededtest3 neededtest4 \
abe59f
@@ -363,6 +364,7 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
abe59f
 		tst-auditmod19a \
abe59f
 		tst-auditmod19b \
abe59f
 		tst-audit19bmod \
abe59f
+		tst-auditmod22 \
abe59f
 
abe59f
 # Most modules build with _ISOMAC defined, but those filtered out
abe59f
 # depend on internal headers.
abe59f
@@ -1577,6 +1579,9 @@ $(objpfx)tst-audit19b.out: $(objpfx)tst-auditmod19b.so
abe59f
 $(objpfx)tst-audit19b: $(objpfx)tst-audit19bmod.so
abe59f
 tst-audit19b-ARGS = -- $(host-test-program-cmd)
abe59f
 
abe59f
+$(objpfx)tst-audit22.out: $(objpfx)tst-auditmod22.so
abe59f
+tst-audit22-ARGS = -- $(host-test-program-cmd)
abe59f
+
abe59f
 # tst-sonamemove links against an older implementation of the library.
abe59f
 LDFLAGS-tst-sonamemove-linkmod1.so = \
abe59f
   -Wl,--version-script=tst-sonamemove-linkmod1.map \
abe59f
diff --git a/elf/dl-object.c b/elf/dl-object.c
abe59f
index 05a7750c65305771..3be309ecf1b5d4e2 100644
abe59f
--- a/elf/dl-object.c
abe59f
+++ b/elf/dl-object.c
abe59f
@@ -59,16 +59,19 @@ _dl_new_object (char *realname, const char *libname, int type,
abe59f
 {
abe59f
 #ifdef SHARED
abe59f
   unsigned int naudit;
abe59f
-  if (__glibc_unlikely ((mode & __RTLD_OPENEXEC) != 0))
abe59f
+  if (__glibc_unlikely ((mode & (__RTLD_OPENEXEC | __RTLD_VDSO)) != 0))
abe59f
     {
abe59f
-      assert (type == lt_executable);
abe59f
-      assert (nsid == LM_ID_BASE);
abe59f
+      if (mode & __RTLD_OPENEXEC)
abe59f
+	{
abe59f
+	  assert (type == lt_executable);
abe59f
+	  assert (nsid == LM_ID_BASE);
abe59f
 
abe59f
-      /* Ignore the specified libname for the main executable.  It is
abe59f
-	 only known with an explicit loader invocation.  */
abe59f
-      libname = "";
abe59f
+	  /* Ignore the specified libname for the main executable.  It is
abe59f
+	     only known with an explicit loader invocation.  */
abe59f
+	  libname = "";
abe59f
+	}
abe59f
 
abe59f
-      /* We create the map for the executable before we know whether
abe59f
+      /* We create the map for the executable and vDSO before we know whether
abe59f
 	 we have auditing libraries and if yes, how many.  Assume the
abe59f
 	 worst.  */
abe59f
       naudit = DL_NNS;
abe59f
diff --git a/elf/rtld.c b/elf/rtld.c
abe59f
index 2994578ba3a5f911..efcbeac6c24c4b7b 100644
abe59f
--- a/elf/rtld.c
abe59f
+++ b/elf/rtld.c
abe59f
@@ -1917,6 +1917,12 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
abe59f
       assert (i == npreloads);
abe59f
     }
abe59f
 
abe59f
+#ifdef NEED_DL_SYSINFO_DSO
abe59f
+  /* Now that the audit modules are opened, call la_objopen for the vDSO.  */
abe59f
+  if (GLRO(dl_sysinfo_map) != NULL)
abe59f
+    _dl_audit_objopen (GLRO(dl_sysinfo_map), LM_ID_BASE);
abe59f
+#endif
abe59f
+
abe59f
   /* Load all the libraries specified by DT_NEEDED entries.  If LD_PRELOAD
abe59f
      specified some libraries to load, these are inserted before the actual
abe59f
      dependencies in the executable's searchlist for symbol resolution.  */
abe59f
diff --git a/elf/setup-vdso.h b/elf/setup-vdso.h
abe59f
index 34b1d5e8c37c2610..d2b35a080b57c183 100644
abe59f
--- a/elf/setup-vdso.h
abe59f
+++ b/elf/setup-vdso.h
abe59f
@@ -30,7 +30,7 @@ setup_vdso (struct link_map *main_map __attribute__ ((unused)),
abe59f
      We just want our data structures to describe it as if we had just
abe59f
      mapped and relocated it normally.  */
abe59f
   struct link_map *l = _dl_new_object ((char *) "", "", lt_library, NULL,
abe59f
-				       0, LM_ID_BASE);
abe59f
+				       __RTLD_VDSO, LM_ID_BASE);
abe59f
   if (__glibc_likely (l != NULL))
abe59f
     {
abe59f
       static ElfW(Dyn) dyn_temp[DL_RO_DYN_TEMP_CNT] attribute_relro;
abe59f
diff --git a/elf/tst-audit22.c b/elf/tst-audit22.c
abe59f
new file mode 100644
abe59f
index 0000000000000000..18fd22a760ddc3d8
abe59f
--- /dev/null
abe59f
+++ b/elf/tst-audit22.c
abe59f
@@ -0,0 +1,124 @@
abe59f
+/* Check DTAUDIT and vDSO interaction.
abe59f
+   Copyright (C) 2021 Free Software Foundation, Inc.
abe59f
+   This file is part of the GNU C Library.
abe59f
+
abe59f
+   The GNU C Library is free software; you can redistribute it and/or
abe59f
+   modify it under the terms of the GNU Lesser General Public
abe59f
+   License as published by the Free Software Foundation; either
abe59f
+   version 2.1 of the License, or (at your option) any later version.
abe59f
+
abe59f
+   The GNU C Library is distributed in the hope that it will be useful,
abe59f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
abe59f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
abe59f
+   Lesser General Public License for more details.
abe59f
+
abe59f
+   You should have received a copy of the GNU Lesser General Public
abe59f
+   License along with the GNU C Library; if not, see
abe59f
+   <https://www.gnu.org/licenses/>.  */
abe59f
+
abe59f
+#include <errno.h>
abe59f
+#include <getopt.h>
abe59f
+#include <limits.h>
abe59f
+#include <inttypes.h>
abe59f
+#include <string.h>
abe59f
+#include <stdlib.h>
abe59f
+#include <support/capture_subprocess.h>
abe59f
+#include <support/check.h>
abe59f
+#include <support/xstdio.h>
abe59f
+#include <support/support.h>
abe59f
+#include <sys/auxv.h>
abe59f
+
abe59f
+static int restart;
abe59f
+#define CMDLINE_OPTIONS \
abe59f
+  { "restart", no_argument, &restart, 1 },
abe59f
+
abe59f
+static uintptr_t vdso_addr;
abe59f
+
abe59f
+static int
abe59f
+handle_restart (void)
abe59f
+{
abe59f
+  fprintf (stderr, "vdso: %p\n", (void*) vdso_addr);
abe59f
+  return 0;
abe59f
+}
abe59f
+
abe59f
+static uintptr_t
abe59f
+parse_address (const char *str)
abe59f
+{
abe59f
+  void *r;
abe59f
+  TEST_COMPARE (sscanf (str, "%p\n", &r), 1);
abe59f
+  return (uintptr_t) r;
abe59f
+}
abe59f
+
abe59f
+static inline bool
abe59f
+startswith (const char *str, const char *pre)
abe59f
+{
abe59f
+  size_t lenpre = strlen (pre);
abe59f
+  size_t lenstr = strlen (str);
abe59f
+  return lenstr >= lenpre && memcmp (pre, str, lenpre) == 0;
abe59f
+}
abe59f
+
abe59f
+static int
abe59f
+do_test (int argc, char *argv[])
abe59f
+{
abe59f
+  vdso_addr = getauxval (AT_SYSINFO_EHDR);
abe59f
+  if (vdso_addr == 0)
abe59f
+    FAIL_UNSUPPORTED ("getauxval (AT_SYSINFO_EHDR) returned 0");
abe59f
+
abe59f
+  /* We must have either:
abe59f
+     - One our fource parameters left if called initially:
abe59f
+       + path to ld.so         optional
abe59f
+       + "--library-path"      optional
abe59f
+       + the library path      optional
abe59f
+       + the application name  */
abe59f
+  if (restart)
abe59f
+    return handle_restart ();
abe59f
+
abe59f
+  char *spargv[9];
abe59f
+  int i = 0;
abe59f
+  for (; i < argc - 1; i++)
abe59f
+    spargv[i] = argv[i + 1];
abe59f
+  spargv[i++] = (char *) "--direct";
abe59f
+  spargv[i++] = (char *) "--restart";
abe59f
+  spargv[i] = NULL;
abe59f
+
abe59f
+  setenv ("LD_AUDIT", "tst-auditmod22.so", 0);
abe59f
+  struct support_capture_subprocess result
abe59f
+    = support_capture_subprogram (spargv[0], spargv);
abe59f
+  support_capture_subprocess_check (&result, "tst-audit22", 0, sc_allow_stderr);
abe59f
+
abe59f
+  /* The respawned process should always print the vDSO address (otherwise it
abe59f
+     will fails as unsupported).  However, on some architectures the audit
abe59f
+     module might see the vDSO with l_addr being 0, meaning a fixed mapping
abe59f
+     (linux-gate.so).  In this case we don't check its value against
abe59f
+     AT_SYSINFO_EHDR one.  */
abe59f
+  uintptr_t vdso_process = 0;
abe59f
+  bool vdso_audit_found = false;
abe59f
+  uintptr_t vdso_audit = 0;
abe59f
+
abe59f
+  FILE *out = fmemopen (result.err.buffer, result.err.length, "r");
abe59f
+  TEST_VERIFY (out != NULL);
abe59f
+  char *buffer = NULL;
abe59f
+  size_t buffer_length = 0;
abe59f
+  while (xgetline (&buffer, &buffer_length, out))
abe59f
+    {
abe59f
+      if (startswith (buffer, "vdso: "))
abe59f
+	vdso_process = parse_address (buffer + strlen ("vdso: "));
abe59f
+      else if (startswith (buffer, "vdso found: "))
abe59f
+	{
abe59f
+	  vdso_audit = parse_address (buffer + strlen ("vdso found: "));
abe59f
+          vdso_audit_found = true;
abe59f
+	}
abe59f
+    }
abe59f
+
abe59f
+  TEST_COMPARE (vdso_audit_found, true);
abe59f
+  if (vdso_audit != 0)
abe59f
+    TEST_COMPARE (vdso_process, vdso_audit);
abe59f
+
abe59f
+  free (buffer);
abe59f
+  xfclose (out);
abe59f
+
abe59f
+  return 0;
abe59f
+}
abe59f
+
abe59f
+#define TEST_FUNCTION_ARGV do_test
abe59f
+#include <support/test-driver.c>
abe59f
diff --git a/elf/tst-auditmod22.c b/elf/tst-auditmod22.c
abe59f
new file mode 100644
abe59f
index 0000000000000000..8e05ce8cbb215dd5
abe59f
--- /dev/null
abe59f
+++ b/elf/tst-auditmod22.c
abe59f
@@ -0,0 +1,51 @@
abe59f
+/* Check DTAUDIT and vDSO interaction.
abe59f
+   Copyright (C) 2021 Free Software Foundation, Inc.
abe59f
+   This file is part of the GNU C Library.
abe59f
+
abe59f
+   The GNU C Library is free software; you can redistribute it and/or
abe59f
+   modify it under the terms of the GNU Lesser General Public
abe59f
+   License as published by the Free Software Foundation; either
abe59f
+   version 2.1 of the License, or (at your option) any later version.
abe59f
+
abe59f
+   The GNU C Library is distributed in the hope that it will be useful,
abe59f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
abe59f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
abe59f
+   Lesser General Public License for more details.
abe59f
+
abe59f
+   You should have received a copy of the GNU Lesser General Public
abe59f
+   License along with the GNU C Library; if not, see
abe59f
+   <https://www.gnu.org/licenses/>.  */
abe59f
+
abe59f
+#include <link.h>
abe59f
+#include <inttypes.h>
abe59f
+#include <stdbool.h>
abe59f
+#include <string.h>
abe59f
+#include <stdio.h>
abe59f
+#include <sys/auxv.h>
abe59f
+
abe59f
+static inline bool
abe59f
+startswith (const char *str, const char *pre)
abe59f
+{
abe59f
+  size_t lenpre = strlen (pre);
abe59f
+  size_t lenstr = strlen (str);
abe59f
+  return lenstr < lenpre ? false : memcmp (pre, str, lenpre) == 0;
abe59f
+}
abe59f
+
abe59f
+unsigned int
abe59f
+la_version (unsigned int version)
abe59f
+{
abe59f
+  return LAV_CURRENT;
abe59f
+}
abe59f
+
abe59f
+unsigned int
abe59f
+la_objopen (struct link_map *map, Lmid_t lmid, uintptr_t *cookie)
abe59f
+{
abe59f
+  /* The linux-gate.so is placed at a fixed address, thus l_addr being 0,
abe59f
+     and it might be the value reported as the AT_SYSINFO_EHDR.  */
abe59f
+  if (map->l_addr == 0 && startswith (map->l_name, "linux-gate.so"))
abe59f
+    fprintf (stderr, "vdso found: %p\n", NULL);
abe59f
+  else if (map->l_addr == getauxval (AT_SYSINFO_EHDR))
abe59f
+    fprintf (stderr, "vdso found: %p\n", (void*) map->l_addr);
abe59f
+
abe59f
+  return 0;
abe59f
+}
abe59f
diff --git a/include/dlfcn.h b/include/dlfcn.h
abe59f
index 109586a1d968b630..a39cc9c69f55a56a 100644
abe59f
--- a/include/dlfcn.h
abe59f
+++ b/include/dlfcn.h
abe59f
@@ -12,6 +12,8 @@
abe59f
 #define __RTLD_AUDIT	0x08000000
abe59f
 #define __RTLD_SECURE	0x04000000 /* Apply additional security checks.  */
abe59f
 #define __RTLD_NOIFUNC	0x02000000 /* Suppress calling ifunc functions.  */
abe59f
+#define __RTLD_VDSO	0x01000000 /* Tell _dl_new_object the object is
abe59f
+				      system-loaded.  */
abe59f
 
abe59f
 #define __LM_ID_CALLER	-2
abe59f