076f82
commit d1b9bee29a1c4e0b80db53f228e22550c3604894
076f82
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
076f82
Date:   Mon Jul 19 18:42:26 2021 -0300
076f82
076f82
    elf: Issue audit la_objopen for vDSO
076f82
    
076f82
    The vDSO is is listed in the link_map chain, but is never the subject of
076f82
    an la_objopen call.  A new internal flag __RTLD_VDSO is added that
076f82
    acts as __RTLD_OPENEXEC to allocate the required 'struct auditstate'
076f82
    extra space for the 'struct link_map'.
076f82
    
076f82
    The return value from the callback is currently ignored, since there
076f82
    is no PLT call involved by glibc when using the vDSO, neither the vDSO
076f82
    are exported directly.
076f82
    
076f82
    Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu.
076f82
    
076f82
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
076f82
    (cherry picked from commit f0e23d34a7bdf6b90fba954ee741419171ac41b2)
076f82
    
076f82
    Resolved conflicts:
076f82
            elf/Makefile
076f82
076f82
diff --git a/elf/Makefile b/elf/Makefile
076f82
index 29f545d2272bf6e2..465442bf59fa9794 100644
076f82
--- a/elf/Makefile
076f82
+++ b/elf/Makefile
076f82
@@ -366,6 +366,7 @@ tests += \
076f82
   tst-audit17 \
076f82
   tst-audit18 \
076f82
   tst-audit19b \
076f82
+  tst-audit22 \
076f82
   tst-auditmany \
076f82
   tst-auxobj \
076f82
   tst-auxobj-dlopen \
076f82
@@ -649,6 +650,7 @@ modules-names = \
076f82
   tst-auditmod18 \
076f82
   tst-auditmod19a \
076f82
   tst-auditmod19b \
076f82
+  tst-auditmod22 \
076f82
   tst-auxvalmod \
076f82
   tst-big-note-lib \
076f82
   tst-deep1mod1 \
076f82
@@ -2035,6 +2037,9 @@ $(objpfx)tst-audit19b.out: $(objpfx)tst-auditmod19b.so
076f82
 $(objpfx)tst-audit19b: $(objpfx)tst-audit19bmod.so
076f82
 tst-audit19b-ARGS = -- $(host-test-program-cmd)
076f82
 
076f82
+$(objpfx)tst-audit22.out: $(objpfx)tst-auditmod22.so
076f82
+tst-audit22-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-object.c b/elf/dl-object.c
076f82
index 1875599eb274dc35..dee49a32d4fdf07d 100644
076f82
--- a/elf/dl-object.c
076f82
+++ b/elf/dl-object.c
076f82
@@ -59,16 +59,19 @@ _dl_new_object (char *realname, const char *libname, int type,
076f82
 {
076f82
 #ifdef SHARED
076f82
   unsigned int naudit;
076f82
-  if (__glibc_unlikely ((mode & __RTLD_OPENEXEC) != 0))
076f82
+  if (__glibc_unlikely ((mode & (__RTLD_OPENEXEC | __RTLD_VDSO)) != 0))
076f82
     {
076f82
-      assert (type == lt_executable);
076f82
-      assert (nsid == LM_ID_BASE);
076f82
+      if (mode & __RTLD_OPENEXEC)
076f82
+	{
076f82
+	  assert (type == lt_executable);
076f82
+	  assert (nsid == LM_ID_BASE);
076f82
 
076f82
-      /* Ignore the specified libname for the main executable.  It is
076f82
-	 only known with an explicit loader invocation.  */
076f82
-      libname = "";
076f82
+	  /* Ignore the specified libname for the main executable.  It is
076f82
+	     only known with an explicit loader invocation.  */
076f82
+	  libname = "";
076f82
+	}
076f82
 
076f82
-      /* We create the map for the executable before we know whether
076f82
+      /* We create the map for the executable and vDSO before we know whether
076f82
 	 we have auditing libraries and if yes, how many.  Assume the
076f82
 	 worst.  */
076f82
       naudit = DL_NNS;
076f82
diff --git a/elf/rtld.c b/elf/rtld.c
076f82
index f632a767d7a269ef..b089e5cf4740443e 100644
076f82
--- a/elf/rtld.c
076f82
+++ b/elf/rtld.c
076f82
@@ -1922,6 +1922,12 @@ dl_main (const ElfW(Phdr) *phdr,
076f82
       assert (i == npreloads);
076f82
     }
076f82
 
076f82
+#ifdef NEED_DL_SYSINFO_DSO
076f82
+  /* Now that the audit modules are opened, call la_objopen for the vDSO.  */
076f82
+  if (GLRO(dl_sysinfo_map) != NULL)
076f82
+    _dl_audit_objopen (GLRO(dl_sysinfo_map), LM_ID_BASE);
076f82
+#endif
076f82
+
076f82
   /* Load all the libraries specified by DT_NEEDED entries.  If LD_PRELOAD
076f82
      specified some libraries to load, these are inserted before the actual
076f82
      dependencies in the executable's searchlist for symbol resolution.  */
076f82
diff --git a/elf/setup-vdso.h b/elf/setup-vdso.h
076f82
index 3f20578046de76ed..2b013d974a377a83 100644
076f82
--- a/elf/setup-vdso.h
076f82
+++ b/elf/setup-vdso.h
076f82
@@ -30,7 +30,7 @@ setup_vdso (struct link_map *main_map __attribute__ ((unused)),
076f82
      We just want our data structures to describe it as if we had just
076f82
      mapped and relocated it normally.  */
076f82
   struct link_map *l = _dl_new_object ((char *) "", "", lt_library, NULL,
076f82
-				       0, LM_ID_BASE);
076f82
+				       __RTLD_VDSO, LM_ID_BASE);
076f82
   if (__glibc_likely (l != NULL))
076f82
     {
076f82
       l->l_phdr = ((const void *) GLRO(dl_sysinfo_dso)
076f82
diff --git a/elf/tst-audit22.c b/elf/tst-audit22.c
076f82
new file mode 100644
076f82
index 0000000000000000..18fd22a760ddc3d8
076f82
--- /dev/null
076f82
+++ b/elf/tst-audit22.c
076f82
@@ -0,0 +1,124 @@
076f82
+/* Check DTAUDIT and vDSO interaction.
076f82
+   Copyright (C) 2021 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 <errno.h>
076f82
+#include <getopt.h>
076f82
+#include <limits.h>
076f82
+#include <inttypes.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/support.h>
076f82
+#include <sys/auxv.h>
076f82
+
076f82
+static int restart;
076f82
+#define CMDLINE_OPTIONS \
076f82
+  { "restart", no_argument, &restart, 1 },
076f82
+
076f82
+static uintptr_t vdso_addr;
076f82
+
076f82
+static int
076f82
+handle_restart (void)
076f82
+{
076f82
+  fprintf (stderr, "vdso: %p\n", (void*) vdso_addr);
076f82
+  return 0;
076f82
+}
076f82
+
076f82
+static uintptr_t
076f82
+parse_address (const char *str)
076f82
+{
076f82
+  void *r;
076f82
+  TEST_COMPARE (sscanf (str, "%p\n", &r), 1);
076f82
+  return (uintptr_t) r;
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 int
076f82
+do_test (int argc, char *argv[])
076f82
+{
076f82
+  vdso_addr = getauxval (AT_SYSINFO_EHDR);
076f82
+  if (vdso_addr == 0)
076f82
+    FAIL_UNSUPPORTED ("getauxval (AT_SYSINFO_EHDR) returned 0");
076f82
+
076f82
+  /* We must have either:
076f82
+     - One our fource 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
+  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-auditmod22.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 respawned process should always print the vDSO address (otherwise it
076f82
+     will fails as unsupported).  However, on some architectures the audit
076f82
+     module might see the vDSO with l_addr being 0, meaning a fixed mapping
076f82
+     (linux-gate.so).  In this case we don't check its value against
076f82
+     AT_SYSINFO_EHDR one.  */
076f82
+  uintptr_t vdso_process = 0;
076f82
+  bool vdso_audit_found = false;
076f82
+  uintptr_t vdso_audit = 0;
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, "vdso: "))
076f82
+	vdso_process = parse_address (buffer + strlen ("vdso: "));
076f82
+      else if (startswith (buffer, "vdso found: "))
076f82
+	{
076f82
+	  vdso_audit = parse_address (buffer + strlen ("vdso found: "));
076f82
+          vdso_audit_found = true;
076f82
+	}
076f82
+    }
076f82
+
076f82
+  TEST_COMPARE (vdso_audit_found, true);
076f82
+  if (vdso_audit != 0)
076f82
+    TEST_COMPARE (vdso_process, vdso_audit);
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-auditmod22.c b/elf/tst-auditmod22.c
076f82
new file mode 100644
076f82
index 0000000000000000..8e05ce8cbb215dd5
076f82
--- /dev/null
076f82
+++ b/elf/tst-auditmod22.c
076f82
@@ -0,0 +1,51 @@
076f82
+/* Check DTAUDIT and vDSO interaction.
076f82
+   Copyright (C) 2021 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 <stdbool.h>
076f82
+#include <string.h>
076f82
+#include <stdio.h>
076f82
+#include <sys/auxv.h>
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 ? false : memcmp (pre, str, lenpre) == 0;
076f82
+}
076f82
+
076f82
+unsigned int
076f82
+la_version (unsigned int version)
076f82
+{
076f82
+  return LAV_CURRENT;
076f82
+}
076f82
+
076f82
+unsigned int
076f82
+la_objopen (struct link_map *map, Lmid_t lmid, uintptr_t *cookie)
076f82
+{
076f82
+  /* The linux-gate.so is placed at a fixed address, thus l_addr being 0,
076f82
+     and it might be the value reported as the AT_SYSINFO_EHDR.  */
076f82
+  if (map->l_addr == 0 && startswith (map->l_name, "linux-gate.so"))
076f82
+    fprintf (stderr, "vdso found: %p\n", NULL);
076f82
+  else if (map->l_addr == getauxval (AT_SYSINFO_EHDR))
076f82
+    fprintf (stderr, "vdso found: %p\n", (void*) map->l_addr);
076f82
+
076f82
+  return 0;
076f82
+}
076f82
diff --git a/include/dlfcn.h b/include/dlfcn.h
076f82
index a4c283728f94deb4..e73294b0af587913 100644
076f82
--- a/include/dlfcn.h
076f82
+++ b/include/dlfcn.h
076f82
@@ -12,6 +12,8 @@
076f82
 #define __RTLD_AUDIT	0x08000000
076f82
 #define __RTLD_SECURE	0x04000000 /* Apply additional security checks.  */
076f82
 #define __RTLD_NOIFUNC	0x02000000 /* Suppress calling ifunc functions.  */
076f82
+#define __RTLD_VDSO	0x01000000 /* Tell _dl_new_object the object is
076f82
+				      system-loaded.  */
076f82
 
076f82
 #define __LM_ID_CALLER	-2
076f82