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