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