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