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