Blame SOURCES/glibc-rh1630440-1.patch

147e83
commit e5d262effe3a87164308a3f37e61b32d0348692a
147e83
Author: Tulio Magno Quites Machado Filho <tuliom@linux.ibm.com>
147e83
Date:   Fri Nov 30 18:05:32 2018 -0200
147e83
147e83
    Fix _dl_profile_fixup data-dependency issue (Bug 23690)
147e83
    
147e83
    There is a data-dependency between the fields of struct l_reloc_result
147e83
    and the field used as the initialization guard. Users of the guard
147e83
    expect writes to the structure to be observable when they also observe
147e83
    the guard initialized. The solution for this problem is to use an acquire
147e83
    and release load and store to ensure previous writes to the structure are
147e83
    observable if the guard is initialized.
147e83
    
147e83
    The previous implementation used DL_FIXUP_VALUE_ADDR (l_reloc_result->addr)
147e83
    as the initialization guard, making it impossible for some architectures
147e83
    to load and store it atomically, i.e. hppa and ia64, due to its larger size.
147e83
    
147e83
    This commit adds an unsigned int to l_reloc_result to be used as the new
147e83
    initialization guard of the struct, making it possible to load and store
147e83
    it atomically in all architectures. The fix ensures that the values
147e83
    observed in l_reloc_result are consistent and do not lead to crashes.
147e83
    The algorithm is documented in the code in elf/dl-runtime.c
147e83
    (_dl_profile_fixup). Not all data races have been eliminated.
147e83
    
147e83
    Tested with build-many-glibcs and on powerpc, powerpc64, and powerpc64le.
147e83
    
147e83
            [BZ #23690]
147e83
            * elf/dl-runtime.c (_dl_profile_fixup): Guarantee memory
147e83
            modification order when accessing reloc_result->addr.
147e83
            * include/link.h (reloc_result): Add field init.
147e83
            * nptl/Makefile (tests): Add tst-audit-threads.
147e83
            (modules-names): Add tst-audit-threads-mod1 and
147e83
            tst-audit-threads-mod2.
147e83
            Add rules to build tst-audit-threads.
147e83
            * nptl/tst-audit-threads-mod1.c: New file.
147e83
            * nptl/tst-audit-threads-mod2.c: Likewise.
147e83
            * nptl/tst-audit-threads.c: Likewise.
147e83
            * nptl/tst-audit-threads.h: Likewise.
147e83
    
147e83
    Signed-off-by: Tulio Magno Quites Machado Filho <tuliom@linux.ibm.com>
147e83
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
147e83
147e83
(elf/dl-runtime.c adjusted here for lack of __builtin_expect cleanup,
147e83
nptl/Makefile for the usual test-related conflicts.)
147e83
147e83
diff --git a/elf/dl-runtime.c b/elf/dl-runtime.c
147e83
index a42e3c4924e067ba..3678a98c98d726f3 100644
147e83
--- a/elf/dl-runtime.c
147e83
+++ b/elf/dl-runtime.c
147e83
@@ -183,10 +183,36 @@ _dl_profile_fixup (
147e83
   /* This is the address in the array where we store the result of previous
147e83
      relocations.  */
147e83
   struct reloc_result *reloc_result = &l->l_reloc_result[reloc_index];
147e83
-  DL_FIXUP_VALUE_TYPE *resultp = &reloc_result->addr;
147e83
 
147e83
-  DL_FIXUP_VALUE_TYPE value = *resultp;
147e83
-  if (DL_FIXUP_VALUE_CODE_ADDR (value) == 0)
147e83
+ /* CONCURRENCY NOTES:
147e83
+
147e83
+  Multiple threads may be calling the same PLT sequence and with
147e83
+  LD_AUDIT enabled they will be calling into _dl_profile_fixup to
147e83
+  update the reloc_result with the result of the lazy resolution.
147e83
+  The reloc_result guard variable is reloc_init, and we use
147e83
+  acquire/release loads and store to it to ensure that the results of
147e83
+  the structure are consistent with the loaded value of the guard.
147e83
+  This does not fix all of the data races that occur when two or more
147e83
+  threads read reloc_result->reloc_init with a value of zero and read
147e83
+  and write to that reloc_result concurrently.  The expectation is
147e83
+  generally that while this is a data race it works because the
147e83
+  threads write the same values.  Until the data races are fixed
147e83
+  there is a potential for problems to arise from these data races.
147e83
+  The reloc result updates should happen in parallel but there should
147e83
+  be an atomic RMW which does the final update to the real result
147e83
+  entry (see bug 23790).
147e83
+
147e83
+  The following code uses reloc_result->init set to 0 to indicate if it is
147e83
+  the first time this object is being relocated, otherwise 1 which
147e83
+  indicates the object has already been relocated.
147e83
+
147e83
+  Reading/Writing from/to reloc_result->reloc_init must not happen
147e83
+  before previous writes to reloc_result complete as they could
147e83
+  end-up with an incomplete struct.  */
147e83
+  DL_FIXUP_VALUE_TYPE value;
147e83
+  unsigned int init = atomic_load_acquire (&reloc_result->init);
147e83
+
147e83
+  if (init == 0)
147e83
     {
147e83
       /* This is the first time we have to relocate this object.  */
147e83
       const ElfW(Sym) *const symtab
147e83
@@ -347,20 +373,32 @@ _dl_profile_fixup (
147e83
 #endif
147e83
 
147e83
       /* Store the result for later runs.  */
147e83
-      if (__builtin_expect (! GLRO(dl_bind_not), 1))
147e83
-	*resultp = value;
147e83
+      if (__glibc_likely (! GLRO(dl_bind_not)))
147e83
+	{
147e83
+	  reloc_result->addr = value;
147e83
+	  /* Guarantee all previous writes complete before
147e83
+	     init is updated.  See CONCURRENCY NOTES earlier  */
147e83
+	  atomic_store_release (&reloc_result->init, 1);
147e83
+	}
147e83
+      init = 1;
147e83
     }
147e83
+  else
147e83
+    value = reloc_result->addr;
147e83
 
147e83
   /* By default we do not call the pltexit function.  */
147e83
   long int framesize = -1;
147e83
 
147e83
+
147e83
 #ifdef SHARED
147e83
   /* Auditing checkpoint: report the PLT entering and allow the
147e83
      auditors to change the value.  */
147e83
-  if (DL_FIXUP_VALUE_CODE_ADDR (value) != 0 && GLRO(dl_naudit) > 0
147e83
+  if (GLRO(dl_naudit) > 0
147e83
       /* Don't do anything if no auditor wants to intercept this call.  */
147e83
       && (reloc_result->enterexit & LA_SYMB_NOPLTENTER) == 0)
147e83
     {
147e83
+      /* Sanity check:  DL_FIXUP_VALUE_CODE_ADDR (value) should have been
147e83
+	 initialized earlier in this function or in another thread.  */
147e83
+      assert (DL_FIXUP_VALUE_CODE_ADDR (value) != 0);
147e83
       ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
147e83
 						l_info[DT_SYMTAB])
147e83
 			   + reloc_result->boundndx);
147e83
diff --git a/include/link.h b/include/link.h
147e83
index d7590640aa9285e5..22d020d833ae3a7c 100644
147e83
--- a/include/link.h
147e83
+++ b/include/link.h
147e83
@@ -206,6 +206,10 @@ struct link_map
147e83
       unsigned int boundndx;
147e83
       uint32_t enterexit;
147e83
       unsigned int flags;
147e83
+      /* CONCURRENCY NOTE: This is used to guard the concurrent initialization
147e83
+	 of the relocation result across multiple threads.  See the more
147e83
+	 detailed notes in elf/dl-runtime.c.  */
147e83
+      unsigned int init;
147e83
     } *l_reloc_result;
147e83
 
147e83
     /* Pointer to the version information if available.  */
147e83
diff --git a/nptl/Makefile b/nptl/Makefile
147e83
index cf47a6f097916766..1b9639f3566a63fd 100644
147e83
--- a/nptl/Makefile
147e83
+++ b/nptl/Makefile
147e83
@@ -298,7 +298,7 @@ tests += tst-cancelx2 tst-cancelx3 tst-cancelx4 tst-cancelx5 \
147e83
 endif
147e83
 ifeq ($(build-shared),yes)
147e83
 tests += tst-atfork2 tst-tls3 tst-tls4 tst-tls5 tst-_res1 tst-fini1 \
147e83
-	 tst-stackguard1
147e83
+	 tst-stackguard1 tst-audit-threads
147e83
 tests-nolibpthread += tst-fini1
147e83
 ifeq ($(have-z-execstack),yes)
147e83
 tests += tst-execstack
147e83
@@ -309,7 +309,7 @@ modules-names = tst-atfork2mod tst-tls3mod tst-tls4moda tst-tls4modb \
147e83
 		tst-tls5mod tst-tls5moda tst-tls5modb tst-tls5modc \
147e83
 		tst-tls5modd tst-tls5mode tst-tls5modf tst-stack4mod \
147e83
 		tst-_res1mod1 tst-_res1mod2 tst-execstack-mod tst-fini1mod \
147e83
-		tst-join7mod
147e83
+		tst-join7mod tst-audit-threads-mod1 tst-audit-threads-mod2
147e83
 extra-test-objs += $(addsuffix .os,$(strip $(modules-names))) tst-cleanup4aux.o
147e83
 test-extras += $(modules-names) tst-cleanup4aux
147e83
 test-modules = $(addprefix $(objpfx),$(addsuffix .so,$(modules-names)))
147e83
@@ -627,6 +627,14 @@ $(objpfx)tst-oddstacklimit.out: $(objpfx)tst-oddstacklimit $(objpfx)tst-basic1
147e83
 	$(run-program-prefix) $< --command '$(host-built-program-cmd)' > $@
147e83
 endif
147e83
 
147e83
+# Protect against a build using -Wl,-z,now.
147e83
+LDFLAGS-tst-audit-threads-mod1.so = -Wl,-z,lazy
147e83
+LDFLAGS-tst-audit-threads-mod2.so = -Wl,-z,lazy
147e83
+LDFLAGS-tst-audit-threads = -Wl,-z,lazy
147e83
+$(objpfx)tst-audit-threads: $(objpfx)tst-audit-threads-mod2.so
147e83
+$(objpfx)tst-audit-threads.out: $(objpfx)tst-audit-threads-mod1.so
147e83
+tst-audit-threads-ENV = LD_AUDIT=$(objpfx)tst-audit-threads-mod1.so
147e83
+
147e83
 # The tests here better do not run in parallel
147e83
 ifneq ($(filter %tests,$(MAKECMDGOALS)),)
147e83
 .NOTPARALLEL:
147e83
diff --git a/nptl/tst-audit-threads-mod1.c b/nptl/tst-audit-threads-mod1.c
147e83
new file mode 100644
147e83
index 0000000000000000..615d5ee5121962df
147e83
--- /dev/null
147e83
+++ b/nptl/tst-audit-threads-mod1.c
147e83
@@ -0,0 +1,74 @@
147e83
+/* Dummy audit library for test-audit-threads.
147e83
+
147e83
+   Copyright (C) 2018 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+#include <elf.h>
147e83
+#include <link.h>
147e83
+#include <stdio.h>
147e83
+#include <assert.h>
147e83
+#include <string.h>
147e83
+
147e83
+/* We must use a dummy LD_AUDIT module to force the dynamic loader to
147e83
+   *not* update the real PLT, and instead use a cached value for the
147e83
+   lazy resolution result.  It is the update of that cached value that
147e83
+   we are testing for correctness by doing this.  */
147e83
+
147e83
+/* Library to be audited.  */
147e83
+#define LIB "tst-audit-threads-mod2.so"
147e83
+/* CALLNUM is the number of retNum functions.  */
147e83
+#define CALLNUM 7999
147e83
+
147e83
+#define CONCATX(a, b) __CONCAT (a, b)
147e83
+
147e83
+static int previous = 0;
147e83
+
147e83
+unsigned int
147e83
+la_version (unsigned int ver)
147e83
+{
147e83
+  return 1;
147e83
+}
147e83
+
147e83
+unsigned int
147e83
+la_objopen (struct link_map *map, Lmid_t lmid, uintptr_t *cookie)
147e83
+{
147e83
+  return LA_FLG_BINDTO | LA_FLG_BINDFROM;
147e83
+}
147e83
+
147e83
+uintptr_t
147e83
+CONCATX(la_symbind, __ELF_NATIVE_CLASS) (ElfW(Sym) *sym,
147e83
+					unsigned int ndx,
147e83
+					uintptr_t *refcook,
147e83
+					uintptr_t *defcook,
147e83
+					unsigned int *flags,
147e83
+					const char *symname)
147e83
+{
147e83
+  const char * retnum = "retNum";
147e83
+  char * num = strstr (symname, retnum);
147e83
+  int n;
147e83
+  /* Validate if the symbols are getting called in the correct order.
147e83
+     This code is here to verify binutils does not optimize out the PLT
147e83
+     entries that require the symbol binding.  */
147e83
+  if (num != NULL)
147e83
+    {
147e83
+      n = atoi (num);
147e83
+      assert (n >= previous);
147e83
+      assert (n <= CALLNUM);
147e83
+      previous = n;
147e83
+    }
147e83
+  return sym->st_value;
147e83
+}
147e83
diff --git a/nptl/tst-audit-threads-mod2.c b/nptl/tst-audit-threads-mod2.c
147e83
new file mode 100644
147e83
index 0000000000000000..f9817dd3dc7f4910
147e83
--- /dev/null
147e83
+++ b/nptl/tst-audit-threads-mod2.c
147e83
@@ -0,0 +1,22 @@
147e83
+/* Shared object with a huge number of functions for test-audit-threads.
147e83
+
147e83
+   Copyright (C) 2018 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+/* Define all the retNumN functions in a library.  */
147e83
+#define definenum
147e83
+#include "tst-audit-threads.h"
147e83
diff --git a/nptl/tst-audit-threads.c b/nptl/tst-audit-threads.c
147e83
new file mode 100644
147e83
index 0000000000000000..e4bf433bd85f3715
147e83
--- /dev/null
147e83
+++ b/nptl/tst-audit-threads.c
147e83
@@ -0,0 +1,97 @@
147e83
+/* Test multi-threading using LD_AUDIT.
147e83
+
147e83
+   Copyright (C) 2018 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+/* This test uses a dummy LD_AUDIT library (test-audit-threads-mod1) and a
147e83
+   library with a huge number of functions in order to validate lazy symbol
147e83
+   binding with an audit library.  We use one thread per CPU to test that
147e83
+   concurrent lazy resolution does not have any defects which would cause
147e83
+   the process to fail.  We use an LD_AUDIT library to force the testing of
147e83
+   the relocation resolution caching code in the dynamic loader i.e.
147e83
+   _dl_runtime_profile and _dl_profile_fixup.  */
147e83
+
147e83
+#include <support/xthread.h>
147e83
+#include <strings.h>
147e83
+#include <stdlib.h>
147e83
+#include <sys/sysinfo.h>
147e83
+
147e83
+static int do_test (void);
147e83
+
147e83
+/* This test usually takes less than 3s to run.  However, there are cases that
147e83
+   take up to 30s.  */
147e83
+#define TIMEOUT 60
147e83
+#define TEST_FUNCTION do_test ()
147e83
+#include "../test-skeleton.c"
147e83
+
147e83
+/* Declare the functions we are going to call.  */
147e83
+#define externnum
147e83
+#include "tst-audit-threads.h"
147e83
+#undef externnum
147e83
+
147e83
+int num_threads;
147e83
+pthread_barrier_t barrier;
147e83
+
147e83
+void
147e83
+sync_all (int num)
147e83
+{
147e83
+  pthread_barrier_wait (&barrier);
147e83
+}
147e83
+
147e83
+void
147e83
+call_all_ret_nums (void)
147e83
+{
147e83
+  /* Call each function one at a time from all threads.  */
147e83
+#define callnum
147e83
+#include "tst-audit-threads.h"
147e83
+#undef callnum
147e83
+}
147e83
+
147e83
+void *
147e83
+thread_main (void *unused)
147e83
+{
147e83
+  call_all_ret_nums ();
147e83
+  return NULL;
147e83
+}
147e83
+
147e83
+#define STR2(X) #X
147e83
+#define STR(X) STR2(X)
147e83
+
147e83
+static int
147e83
+do_test (void)
147e83
+{
147e83
+  int i;
147e83
+  pthread_t *threads;
147e83
+
147e83
+  num_threads = get_nprocs ();
147e83
+  if (num_threads <= 1)
147e83
+    num_threads = 2;
147e83
+
147e83
+  /* Used to synchronize all the threads after calling each retNumN.  */
147e83
+  xpthread_barrier_init (&barrier, NULL, num_threads);
147e83
+
147e83
+  threads = (pthread_t *) xcalloc (num_threads, sizeof(pthread_t));
147e83
+  for (i = 0; i < num_threads; i++)
147e83
+    threads[i] = xpthread_create(NULL, thread_main, NULL);
147e83
+
147e83
+  for (i = 0; i < num_threads; i++)
147e83
+    xpthread_join(threads[i]);
147e83
+
147e83
+  free (threads);
147e83
+
147e83
+  return 0;
147e83
+}
147e83
diff --git a/nptl/tst-audit-threads.h b/nptl/tst-audit-threads.h
147e83
new file mode 100644
147e83
index 0000000000000000..1c9ecc08dfcd3e65
147e83
--- /dev/null
147e83
+++ b/nptl/tst-audit-threads.h
147e83
@@ -0,0 +1,92 @@
147e83
+/* Helper header for test-audit-threads.
147e83
+
147e83
+   Copyright (C) 2018 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+/* We use this helper to create a large number of functions, all of
147e83
+   which will be resolved lazily and thus have their PLT updated.
147e83
+   This is done to provide enough functions that we can statistically
147e83
+   observe a thread vs. PLT resolution failure if one exists.  */
147e83
+
147e83
+#define CONCAT(a, b) a ## b
147e83
+#define NUM(x, y) CONCAT (x, y)
147e83
+
147e83
+#define FUNC10(x)	\
147e83
+  FUNC (NUM (x, 0));	\
147e83
+  FUNC (NUM (x, 1));	\
147e83
+  FUNC (NUM (x, 2));	\
147e83
+  FUNC (NUM (x, 3));	\
147e83
+  FUNC (NUM (x, 4));	\
147e83
+  FUNC (NUM (x, 5));	\
147e83
+  FUNC (NUM (x, 6));	\
147e83
+  FUNC (NUM (x, 7));	\
147e83
+  FUNC (NUM (x, 8));	\
147e83
+  FUNC (NUM (x, 9))
147e83
+
147e83
+#define FUNC100(x)	\
147e83
+  FUNC10 (NUM (x, 0));	\
147e83
+  FUNC10 (NUM (x, 1));	\
147e83
+  FUNC10 (NUM (x, 2));	\
147e83
+  FUNC10 (NUM (x, 3));	\
147e83
+  FUNC10 (NUM (x, 4));	\
147e83
+  FUNC10 (NUM (x, 5));	\
147e83
+  FUNC10 (NUM (x, 6));	\
147e83
+  FUNC10 (NUM (x, 7));	\
147e83
+  FUNC10 (NUM (x, 8));	\
147e83
+  FUNC10 (NUM (x, 9))
147e83
+
147e83
+#define FUNC1000(x)		\
147e83
+  FUNC100 (NUM (x, 0));		\
147e83
+  FUNC100 (NUM (x, 1));		\
147e83
+  FUNC100 (NUM (x, 2));		\
147e83
+  FUNC100 (NUM (x, 3));		\
147e83
+  FUNC100 (NUM (x, 4));		\
147e83
+  FUNC100 (NUM (x, 5));		\
147e83
+  FUNC100 (NUM (x, 6));		\
147e83
+  FUNC100 (NUM (x, 7));		\
147e83
+  FUNC100 (NUM (x, 8));		\
147e83
+  FUNC100 (NUM (x, 9))
147e83
+
147e83
+#define FUNC7000()	\
147e83
+  FUNC1000 (1);		\
147e83
+  FUNC1000 (2);		\
147e83
+  FUNC1000 (3);		\
147e83
+  FUNC1000 (4);		\
147e83
+  FUNC1000 (5);		\
147e83
+  FUNC1000 (6);		\
147e83
+  FUNC1000 (7);
147e83
+
147e83
+#ifdef FUNC
147e83
+# undef FUNC
147e83
+#endif
147e83
+
147e83
+#ifdef externnum
147e83
+# define FUNC(x) extern int CONCAT (retNum, x) (void)
147e83
+#endif
147e83
+
147e83
+#ifdef definenum
147e83
+# define FUNC(x) int CONCAT (retNum, x) (void) { return x; }
147e83
+#endif
147e83
+
147e83
+#ifdef callnum
147e83
+# define FUNC(x) CONCAT (retNum, x) (); sync_all (x)
147e83
+#endif
147e83
+
147e83
+/* A value of 7000 functions is chosen as an arbitrarily large
147e83
+   number of functions that will allow us enough attempts to
147e83
+   verify lazy resolution operation.  */
147e83
+FUNC7000 ();