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