076f82
commit efb21b5fb27fbad447d9f242436fb591870f0045
076f82
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
076f82
Date:   Mon Jan 24 10:46:16 2022 -0300
076f82
076f82
    elf: Fix initial-exec TLS access on audit modules (BZ #28096)
076f82
    
076f82
    For audit modules and dependencies with initial-exec TLS, we can not
076f82
    set the initial TLS image on default loader initialization because it
076f82
    would already be set by the audit setup.  However, subsequent thread
076f82
    creation would need to follow the default behaviour.
076f82
    
076f82
    This patch fixes it by setting l_auditing link_map field not only
076f82
    for the audit modules, but also for all its dependencies.  This is
076f82
    used on _dl_allocate_tls_init to avoid the static TLS initialization
076f82
    at load time.
076f82
    
076f82
    Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu.
076f82
    
076f82
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
076f82
    Tested-by: Carlos O'Donell <carlos@redhat.com>
076f82
    (cherry picked from commit 254d3d5aef2fd8430c469e1938209ac100ebf132)
076f82
076f82
diff --git a/elf/Makefile b/elf/Makefile
076f82
index 407aaeaeb8c84020..3ccf78f62985e2d0 100644
076f82
--- a/elf/Makefile
076f82
+++ b/elf/Makefile
076f82
@@ -367,6 +367,7 @@ tests += \
076f82
   tst-audit18 \
076f82
   tst-audit19b \
076f82
   tst-audit20 \
076f82
+  tst-audit21 \
076f82
   tst-audit22 \
076f82
   tst-audit23 \
076f82
   tst-auditmany \
076f82
@@ -654,6 +655,8 @@ modules-names = \
076f82
   tst-auditmod19a \
076f82
   tst-auditmod19b \
076f82
   tst-auditmod20 \
076f82
+  tst-auditmod21a \
076f82
+  tst-auditmod21b \
076f82
   tst-auditmod22 \
076f82
   tst-auditmod23 \
076f82
   tst-auxvalmod \
076f82
@@ -2045,6 +2048,11 @@ tst-audit19b-ARGS = -- $(host-test-program-cmd)
076f82
 $(objpfx)tst-audit20.out: $(objpfx)tst-auditmod20.so
076f82
 tst-audit20-ENV = LD_AUDIT=$(objpfx)tst-auditmod20.so
076f82
 
076f82
+$(objpfx)tst-audit21: $(shared-thread-library)
076f82
+$(objpfx)tst-audit21.out: $(objpfx)tst-auditmod21a.so
076f82
+$(objpfx)tst-auditmod21a.so: $(objpfx)tst-auditmod21b.so
076f82
+tst-audit21-ENV = LD_AUDIT=$(objpfx)tst-auditmod21a.so
076f82
+
076f82
 $(objpfx)tst-audit22.out: $(objpfx)tst-auditmod22.so
076f82
 tst-audit22-ARGS = -- $(host-test-program-cmd)
076f82
 
076f82
diff --git a/elf/dl-tls.c b/elf/dl-tls.c
076f82
index e2012d0cd515103b..fab6546e2d31edd4 100644
076f82
--- a/elf/dl-tls.c
076f82
+++ b/elf/dl-tls.c
076f82
@@ -519,8 +519,12 @@ _dl_resize_dtv (dtv_t *dtv, size_t max_modid)
076f82
 }
076f82
 
076f82
 
076f82
+/* Allocate initial TLS.  RESULT should be a non-NULL pointer to storage
076f82
+   for the TLS space.  The DTV may be resized, and so this function may
076f82
+   call malloc to allocate that space.  The loader's GL(dl_load_tls_lock)
076f82
+   is taken when manipulating global TLS-related data in the loader.  */
076f82
 void *
076f82
-_dl_allocate_tls_init (void *result)
076f82
+_dl_allocate_tls_init (void *result, bool init_tls)
076f82
 {
076f82
   if (result == NULL)
076f82
     /* The memory allocation failed.  */
076f82
@@ -593,7 +597,14 @@ _dl_allocate_tls_init (void *result)
076f82
 	     some platforms use in static programs requires it.  */
076f82
 	  dtv[map->l_tls_modid].pointer.val = dest;
076f82
 
076f82
-	  /* Copy the initialization image and clear the BSS part.  */
076f82
+	  /* Copy the initialization image and clear the BSS part.  For
076f82
+	     audit modules or dependencies with initial-exec TLS, we can not
076f82
+	     set the initial TLS image on default loader initialization
076f82
+	     because it would already be set by the audit setup.  However,
076f82
+	     subsequent thread creation would need to follow the default
076f82
+	     behaviour.   */
076f82
+	  if (map->l_ns != LM_ID_BASE && !init_tls)
076f82
+	    continue;
076f82
 	  memset (__mempcpy (dest, map->l_tls_initimage,
076f82
 			     map->l_tls_initimage_size), '\0',
076f82
 		  map->l_tls_blocksize - map->l_tls_initimage_size);
076f82
@@ -620,7 +631,7 @@ _dl_allocate_tls (void *mem)
076f82
 {
076f82
   return _dl_allocate_tls_init (mem == NULL
076f82
 				? _dl_allocate_tls_storage ()
076f82
-				: allocate_dtv (mem));
076f82
+				: allocate_dtv (mem), true);
076f82
 }
076f82
 rtld_hidden_def (_dl_allocate_tls)
076f82
 
076f82
diff --git a/elf/rtld.c b/elf/rtld.c
076f82
index b089e5cf4740443e..26c6fb6479c9008c 100644
076f82
--- a/elf/rtld.c
076f82
+++ b/elf/rtld.c
076f82
@@ -2429,7 +2429,7 @@ dl_main (const ElfW(Phdr) *phdr,
076f82
      into the main thread's TLS area, which we allocated above.
076f82
      Note: thread-local variables must only be accessed after completing
076f82
      the next step.  */
076f82
-  _dl_allocate_tls_init (tcbp);
076f82
+  _dl_allocate_tls_init (tcbp, false);
076f82
 
076f82
   /* And finally install it for the main thread.  */
076f82
   if (! tls_init_tp_called)
076f82
diff --git a/elf/tst-audit21.c b/elf/tst-audit21.c
076f82
new file mode 100644
076f82
index 0000000000000000..3a47ab64d44421ee
076f82
--- /dev/null
076f82
+++ b/elf/tst-audit21.c
076f82
@@ -0,0 +1,42 @@
076f82
+/* Check LD_AUDIT with static TLS.
076f82
+   Copyright (C) 2022 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 <ctype.h>
076f82
+#include <support/xthread.h>
076f82
+#include <support/check.h>
076f82
+
076f82
+static volatile __thread int out __attribute__ ((tls_model ("initial-exec")));
076f82
+
076f82
+static void *
076f82
+tf (void *arg)
076f82
+{
076f82
+  TEST_COMPARE (out, 0);
076f82
+  out = isspace (' ');
076f82
+  return NULL;
076f82
+}
076f82
+
076f82
+int main (int argc, char *argv[])
076f82
+{
076f82
+  TEST_COMPARE (out, 0);
076f82
+  out = isspace (' ');
076f82
+
076f82
+  pthread_t t = xpthread_create (NULL, tf, NULL);
076f82
+  xpthread_join (t);
076f82
+
076f82
+  return 0;
076f82
+}
076f82
diff --git a/elf/tst-auditmod21a.c b/elf/tst-auditmod21a.c
076f82
new file mode 100644
076f82
index 0000000000000000..f6d51b5c0531c49d
076f82
--- /dev/null
076f82
+++ b/elf/tst-auditmod21a.c
076f82
@@ -0,0 +1,80 @@
076f82
+/* Check LD_AUDIT with static TLS.
076f82
+   Copyright (C) 2022 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 <ctype.h>
076f82
+#include <stdlib.h>
076f82
+#include <link.h>
076f82
+
076f82
+#define tls_ie __attribute__ ((tls_model ("initial-exec")))
076f82
+
076f82
+__thread int tls_var0 tls_ie;
076f82
+__thread int tls_var1 tls_ie = 0x10;
076f82
+
076f82
+/* Defined at tst-auditmod21b.so  */
076f82
+extern __thread int tls_var2;
076f82
+extern __thread int tls_var3;
076f82
+
076f82
+static volatile int out;
076f82
+
076f82
+static void
076f82
+call_libc (void)
076f82
+{
076f82
+  /* isspace accesses the initial-exec glibc TLS variables, which are
076f82
+     setup in glibc initialization.  */
076f82
+  out = isspace (' ');
076f82
+}
076f82
+
076f82
+unsigned int
076f82
+la_version (unsigned int v)
076f82
+{
076f82
+  tls_var0 = 0x1;
076f82
+  if (tls_var1 != 0x10)
076f82
+    abort ();
076f82
+  tls_var1 = 0x20;
076f82
+
076f82
+  tls_var2 = 0x2;
076f82
+  if (tls_var3 != 0x20)
076f82
+    abort ();
076f82
+  tls_var3 = 0x40;
076f82
+
076f82
+  call_libc ();
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
+  call_libc ();
076f82
+  *cookie = (uintptr_t) map;
076f82
+  return 0;
076f82
+}
076f82
+
076f82
+void
076f82
+la_activity (uintptr_t* cookie, unsigned int flag)
076f82
+{
076f82
+  if (tls_var0 != 0x1 || tls_var1 != 0x20)
076f82
+    abort ();
076f82
+  call_libc ();
076f82
+}
076f82
+
076f82
+void
076f82
+la_preinit (uintptr_t* cookie)
076f82
+{
076f82
+  call_libc ();
076f82
+}
076f82
diff --git a/elf/tst-auditmod21b.c b/elf/tst-auditmod21b.c
076f82
new file mode 100644
076f82
index 0000000000000000..6ba5335b7514c674
076f82
--- /dev/null
076f82
+++ b/elf/tst-auditmod21b.c
076f82
@@ -0,0 +1,22 @@
076f82
+/* Check LD_AUDIT with static TLS.
076f82
+   Copyright (C) 2022 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
+#define tls_ie __attribute__ ((tls_model ("initial-exec")))
076f82
+
076f82
+__thread int tls_var2 tls_ie;
076f82
+__thread int tls_var3 tls_ie = 0x20;
076f82
diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c
076f82
index 50065bc9bd8a28e5..554a721f814b53c4 100644
076f82
--- a/nptl/allocatestack.c
076f82
+++ b/nptl/allocatestack.c
076f82
@@ -139,7 +139,7 @@ get_cached_stack (size_t *sizep, void **memp)
076f82
   memset (dtv, '\0', (dtv[-1].counter + 1) * sizeof (dtv_t));
076f82
 
076f82
   /* Re-initialize the TLS.  */
076f82
-  _dl_allocate_tls_init (TLS_TPADJ (result));
076f82
+  _dl_allocate_tls_init (TLS_TPADJ (result), true);
076f82
 
076f82
   return result;
076f82
 }
076f82
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
076f82
index 686f0a7b9709eb10..a56060d0204cc453 100644
076f82
--- a/sysdeps/generic/ldsodefs.h
076f82
+++ b/sysdeps/generic/ldsodefs.h
076f82
@@ -1254,7 +1254,7 @@ extern void _dl_allocate_static_tls (struct link_map *map) attribute_hidden;
076f82
 /* These are internal entry points to the two halves of _dl_allocate_tls,
076f82
    only used within rtld.c itself at startup time.  */
076f82
 extern void *_dl_allocate_tls_storage (void) attribute_hidden;
076f82
-extern void *_dl_allocate_tls_init (void *);
076f82
+extern void *_dl_allocate_tls_init (void *, bool);
076f82
 rtld_hidden_proto (_dl_allocate_tls_init)
076f82
 
076f82
 /* Deallocate memory allocated with _dl_allocate_tls.  */