c5d972
Added $(objpfx)tst-audit19a: $(libdl) to elf/Makefile since
c5d972
we still need $(libdl) in RHEL8.
c5d972
c5d972
commit 063f9ba220f434c7f30dd65c4cff17c0c458a7cf
c5d972
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
c5d972
Date:   Wed Jun 30 10:24:09 2021 -0300
c5d972
c5d972
    elf: Avoid unnecessary slowdown from profiling with audit (BZ#15533)
c5d972
    
c5d972
    The rtld-audit interfaces introduces a slowdown due to enabling
c5d972
    profiling instrumentation (as if LD_AUDIT implied LD_PROFILE).
c5d972
    However, instrumenting is only necessary if one of audit libraries
c5d972
    provides PLT callbacks (la_pltenter or la_pltexit symbols).  Otherwise,
c5d972
    the slowdown can be avoided.
c5d972
    
c5d972
    The following patch adjusts the logic that enables profiling to iterate
c5d972
    over all audit modules and check if any of those provides a PLT hook.
c5d972
    To keep la_symbind to work even without PLT callbacks, _dl_fixup now
c5d972
    calls the audit callback if the modules implements it.
c5d972
    
c5d972
    Co-authored-by: Alexander Monakov <amonakov@ispras.ru>
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 08a32a712a34f2cc..0cc03ffe2984ee50 100644
c5d972
--- a/elf/Makefile
c5d972
+++ b/elf/Makefile
c5d972
@@ -221,12 +221,14 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
c5d972
 	 tst-dlmopen-gethostbyname \
c5d972
 	 tst-audit17 \
c5d972
 	 tst-audit18 \
c5d972
+	 tst-audit19b \
c5d972
 #	 reldep9
c5d972
 tests-internal += loadtest unload unload2 circleload1 \
c5d972
 	 neededtest neededtest2 neededtest3 neededtest4 \
c5d972
 	 tst-tls3 tst-tls6 tst-tls7 tst-tls8 tst-dlmopen2 \
c5d972
 	 tst-ptrguard1 tst-stackguard1 tst-libc_dlvsym \
c5d972
-	 tst-create_format1 tst-tls-surplus tst-dl-hwcaps_split
c5d972
+	 tst-create_format1 tst-tls-surplus tst-dl-hwcaps_split \
c5d972
+	 tst-audit19a
c5d972
 tests-container += tst-pldd tst-preload-pthread-libc
c5d972
 ifeq ($(build-hardcoded-path-in-tests),yes)
c5d972
 tests += tst-dlopen-aout
c5d972
@@ -358,6 +360,9 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
c5d972
 		tst-dlmopen-gethostbyname-mod \
c5d972
 		tst-auditmod18 \
c5d972
 		tst-audit18mod \
c5d972
+		tst-auditmod19a \
c5d972
+		tst-auditmod19b \
c5d972
+		tst-audit19bmod \
c5d972
 
c5d972
 # Most modules build with _ISOMAC defined, but those filtered out
c5d972
 # depend on internal headers.
c5d972
@@ -1548,6 +1553,14 @@ $(objpfx)tst-audit18.out: $(objpfx)tst-auditmod18.so \
c5d972
 			  $(objpfx)tst-audit18mod.so
c5d972
 tst-audit18-ARGS = -- $(host-test-program-cmd)
c5d972
 
c5d972
+$(objpfx)tst-audit19a: $(libdl)
c5d972
+$(objpfx)tst-audit19a.out: $(objpfx)tst-auditmod19a.so
c5d972
+tst-audit19a-ENV = LD_AUDIT=$(objpfx)tst-auditmod19a.so
c5d972
+
c5d972
+$(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
 # 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-reloc.c b/elf/dl-reloc.c
c5d972
index 19de5de067a5ef07..7a84b1fa8c3a7fdd 100644
c5d972
--- a/elf/dl-reloc.c
c5d972
+++ b/elf/dl-reloc.c
c5d972
@@ -178,12 +178,28 @@ _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[],
c5d972
   int skip_ifunc = reloc_mode & __RTLD_NOIFUNC;
c5d972
 
c5d972
 #ifdef SHARED
c5d972
+  bool consider_symbind = false;
c5d972
   /* If we are auditing, install the same handlers we need for profiling.  */
c5d972
   if ((reloc_mode & __RTLD_AUDIT) == 0)
c5d972
-    consider_profiling |= GLRO(dl_audit) != NULL;
c5d972
+    {
c5d972
+      struct audit_ifaces *afct = GLRO(dl_audit);
c5d972
+      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
c5d972
+	{
c5d972
+	  /* Profiling is needed only if PLT hooks are provided.  */
c5d972
+	  if (afct->ARCH_LA_PLTENTER != NULL
c5d972
+	      || afct->ARCH_LA_PLTEXIT != NULL)
c5d972
+	    consider_profiling = 1;
c5d972
+	  if (afct->symbind != NULL)
c5d972
+	    consider_symbind = true;
c5d972
+
c5d972
+	  afct = afct->next;
c5d972
+	}
c5d972
+    }
c5d972
 #elif defined PROF
c5d972
   /* Never use dynamic linker profiling for gprof profiling code.  */
c5d972
 # define consider_profiling 0
c5d972
+#else
c5d972
+# define consider_symbind 0
c5d972
 #endif
c5d972
 
c5d972
   if (l->l_relocated)
c5d972
@@ -278,7 +294,7 @@ _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[],
c5d972
     ELF_DYNAMIC_RELOCATE (l, scope, lazy, consider_profiling, skip_ifunc);
c5d972
 
c5d972
 #ifndef PROF
c5d972
-    if (__glibc_unlikely (consider_profiling)
c5d972
+    if ((consider_profiling || consider_symbind)
c5d972
 	&& l->l_info[DT_PLTRELSZ] != NULL)
c5d972
       {
c5d972
 	/* Allocate the array which will contain the already found
c5d972
diff --git a/elf/dl-runtime.c b/elf/dl-runtime.c
c5d972
index ec0b2164825fa538..71ec65264ff780fb 100644
c5d972
--- a/elf/dl-runtime.c
c5d972
+++ b/elf/dl-runtime.c
c5d972
@@ -123,6 +123,37 @@ _dl_fixup (
c5d972
       && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0))
c5d972
     value = elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (value));
c5d972
 
c5d972
+#ifdef SHARED
c5d972
+  /* Auditing checkpoint: we have a new binding.  Provide the auditing
c5d972
+     libraries the possibility to change the value and tell us whether further
c5d972
+     auditing is wanted.
c5d972
+     The l_reloc_result is only allocated if there is an audit module which
c5d972
+     provides a la_symbind.  */
c5d972
+  if (l->l_reloc_result != NULL)
c5d972
+    {
c5d972
+      /* This is the address in the array where we store the result of previous
c5d972
+	 relocations.  */
c5d972
+      struct reloc_result *reloc_result
c5d972
+	= &l->l_reloc_result[reloc_index (pltgot, reloc_arg, sizeof (PLTREL))];
c5d972
+      unsigned int init = atomic_load_acquire (&reloc_result->init);
c5d972
+      if (init == 0)
c5d972
+	{
c5d972
+	  _dl_audit_symbind (l, reloc_result, sym, &value, result);
c5d972
+
c5d972
+	  /* Store the result for later runs.  */
c5d972
+	  if (__glibc_likely (! GLRO(dl_bind_not)))
c5d972
+	    {
c5d972
+	      reloc_result->addr = value;
c5d972
+	      /* Guarantee all previous writes complete before init is
c5d972
+		 updated.  See CONCURRENCY NOTES below.  */
c5d972
+	      atomic_store_release (&reloc_result->init, 1);
c5d972
+	    }
c5d972
+	}
c5d972
+      else
c5d972
+	value = reloc_result->addr;
c5d972
+    }
c5d972
+#endif
c5d972
+
c5d972
   /* Finally, fix up the plt itself.  */
c5d972
   if (__glibc_unlikely (GLRO(dl_bind_not)))
c5d972
     return value;
c5d972
diff --git a/elf/rtld.c b/elf/rtld.c
c5d972
index 767acd122262b824..2994578ba3a5f911 100644
c5d972
--- a/elf/rtld.c
c5d972
+++ b/elf/rtld.c
c5d972
@@ -1027,13 +1027,7 @@ ERROR: audit interface '%s' requires version %d (maximum supported version %d);
c5d972
     "la_objsearch\0"
c5d972
     "la_objopen\0"
c5d972
     "la_preinit\0"
c5d972
-#if __ELF_NATIVE_CLASS == 32
c5d972
-    "la_symbind32\0"
c5d972
-#elif __ELF_NATIVE_CLASS == 64
c5d972
-    "la_symbind64\0"
c5d972
-#else
c5d972
-# error "__ELF_NATIVE_CLASS must be defined"
c5d972
-#endif
c5d972
+    LA_SYMBIND "\0"
c5d972
 #define STRING(s) __STRING (s)
c5d972
     "la_" STRING (ARCH_LA_PLTENTER) "\0"
c5d972
     "la_" STRING (ARCH_LA_PLTEXIT) "\0"
c5d972
diff --git a/elf/tst-audit19a.c b/elf/tst-audit19a.c
c5d972
new file mode 100644
c5d972
index 0000000000000000..035cde9351c2711b
c5d972
--- /dev/null
c5d972
+++ b/elf/tst-audit19a.c
c5d972
@@ -0,0 +1,38 @@
c5d972
+/* Check if DT_AUDIT a module without la_plt{enter,exit} symbols does not incur
c5d972
+   in profiling (BZ#15533).
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 <support/xdlfcn.h>
c5d972
+#include <support/check.h>
c5d972
+
c5d972
+static int
c5d972
+do_test (void)
c5d972
+{
c5d972
+  void *h = xdlopen ("tst-auditmod19a.so", RTLD_NOW);
c5d972
+
c5d972
+  struct link_map *lmap;
c5d972
+  TEST_VERIFY_EXIT (dlinfo (h, RTLD_DI_LINKMAP, &lmap) == 0);
c5d972
+
c5d972
+  /* The internal array is only allocated if profiling is enabled.  */
c5d972
+  TEST_VERIFY (lmap->l_reloc_result == NULL);
c5d972
+
c5d972
+  return 0;
c5d972
+}
c5d972
+
c5d972
+#include <support/test-driver.c>
c5d972
diff --git a/elf/tst-audit19b.c b/elf/tst-audit19b.c
c5d972
new file mode 100644
c5d972
index 0000000000000000..da015734f24e0d79
c5d972
--- /dev/null
c5d972
+++ b/elf/tst-audit19b.c
c5d972
@@ -0,0 +1,94 @@
c5d972
+/* Check if DT_AUDIT a module with la_plt{enter,exit} call la_symbind
c5d972
+   for lazy resolution.
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 <getopt.h>
c5d972
+#include <support/capture_subprocess.h>
c5d972
+#include <support/check.h>
c5d972
+#include <support/xstdio.h>
c5d972
+#include <stdlib.h>
c5d972
+#include <string.h>
c5d972
+#include <stdbool.h>
c5d972
+
c5d972
+static int restart;
c5d972
+#define CMDLINE_OPTIONS \
c5d972
+  { "restart", no_argument, &restart, 1 },
c5d972
+
c5d972
+int tst_audit18bmod1_func (void);
c5d972
+
c5d972
+static int
c5d972
+handle_restart (void)
c5d972
+{
c5d972
+  TEST_COMPARE (tst_audit18bmod1_func (), 10);
c5d972
+  return 0;
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 ? false : memcmp (pre, str, lenpre) == 0;
c5d972
+}
c5d972
+
c5d972
+static int
c5d972
+do_test (int argc, char *argv[])
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
+
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-auditmod18b.so", 0);
c5d972
+  struct support_capture_subprocess result
c5d972
+    = support_capture_subprogram (spargv[0], spargv);
c5d972
+  support_capture_subprocess_check (&result, "tst-audit18b", 0, sc_allow_stderr);
c5d972
+
c5d972
+  bool find_symbind = false;
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
+    if (startswith (buffer, "la_symbind: tst_audit18bmod1_func") == 0)
c5d972
+      find_symbind = true;
c5d972
+
c5d972
+  TEST_COMPARE (find_symbind, true);
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-audit19bmod.c b/elf/tst-audit19bmod.c
c5d972
new file mode 100644
c5d972
index 0000000000000000..9ffdcd8f3ffbc38e
c5d972
--- /dev/null
c5d972
+++ b/elf/tst-audit19bmod.c
c5d972
@@ -0,0 +1,23 @@
c5d972
+/* Extra module for tst-audit18b.
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
+int
c5d972
+tst_audit18bmod1_func (void)
c5d972
+{
c5d972
+  return 10;
c5d972
+}
c5d972
diff --git a/elf/tst-auditmod19a.c b/elf/tst-auditmod19a.c
c5d972
new file mode 100644
c5d972
index 0000000000000000..f58204099457743d
c5d972
--- /dev/null
c5d972
+++ b/elf/tst-auditmod19a.c
c5d972
@@ -0,0 +1,25 @@
c5d972
+/* Audit module for tst-audit18a.
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
+
c5d972
+unsigned int
c5d972
+la_version (unsigned int version)
c5d972
+{
c5d972
+  return LAV_CURRENT;
c5d972
+}
c5d972
diff --git a/elf/tst-auditmod19b.c b/elf/tst-auditmod19b.c
c5d972
new file mode 100644
c5d972
index 0000000000000000..e2248b2a75946746
c5d972
--- /dev/null
c5d972
+++ b/elf/tst-auditmod19b.c
c5d972
@@ -0,0 +1,46 @@
c5d972
+/* Audit module for tst-audit18b.
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 <string.h>
c5d972
+#include <stdio.h>
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
+  return LA_FLG_BINDTO | LA_FLG_BINDFROM;
c5d972
+}
c5d972
+
c5d972
+uintptr_t
c5d972
+#if __ELF_NATIVE_CLASS == 32
c5d972
+la_symbind32 (Elf32_Sym *sym, unsigned int ndx, uintptr_t *refcook,
c5d972
+	      uintptr_t *defcook, unsigned int *flags, const char *symname)
c5d972
+#else
c5d972
+la_symbind64 (Elf64_Sym *sym, unsigned int ndx, uintptr_t *refcook,
c5d972
+	      uintptr_t *defcook, unsigned int *flags, const char *symname)
c5d972
+#endif
c5d972
+{
c5d972
+  fprintf (stderr, "la_symbind: %s\n", symname);
c5d972
+  return sym->st_value;
c5d972
+}
c5d972
diff --git a/include/link.h b/include/link.h
c5d972
index cdd011f59445e490..dd491989beb41353 100644
c5d972
--- a/include/link.h
c5d972
+++ b/include/link.h
c5d972
@@ -353,8 +353,10 @@ struct link_map
c5d972
 
c5d972
 #if __ELF_NATIVE_CLASS == 32
c5d972
 # define symbind symbind32
c5d972
+# define LA_SYMBIND "la_symbind32"
c5d972
 #elif __ELF_NATIVE_CLASS == 64
c5d972
 # define symbind symbind64
c5d972
+# define LA_SYMBIND "la_symbind64"
c5d972
 #else
c5d972
 # error "__ELF_NATIVE_CLASS must be defined"
c5d972
 #endif