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