446cf2
commit 17796419b5fd694348cceb65c3f77601faae082c
446cf2
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
446cf2
Date:   Tue Jul 7 10:49:11 2020 +0100
446cf2
446cf2
    rtld: Account static TLS surplus for audit modules
446cf2
    
446cf2
    The new static TLS surplus size computation is
446cf2
    
446cf2
      surplus_tls = 192 * (nns-1) + 144 * nns + 512
446cf2
    
446cf2
    where nns is controlled via the rtld.nns tunable. This commit
446cf2
    accounts audit modules too so nns = rtld.nns + audit modules.
446cf2
    
446cf2
    rtld.nns should only include the namespaces required by the
446cf2
    application, namespaces for audit modules are accounted on top
446cf2
    of that so audit modules don't use up the static TLS that is
446cf2
    reserved for the application. This allows loading many audit
446cf2
    modules without tuning rtld.nns or using up static TLS, and it
446cf2
    fixes
446cf2
    
446cf2
    FAIL: elf/tst-auditmany
446cf2
    
446cf2
    Note that DL_NNS is currently a hard upper limit for nns, and
446cf2
    if rtld.nns + audit modules go over the limit that's a fatal
446cf2
    error. By default rtld.nns is 4 which allows 12 audit modules.
446cf2
    
446cf2
    Counting the audit modules is based on existing audit string
446cf2
    parsing code, we cannot use GLRO(dl_naudit) before the modules
446cf2
    are actually loaded.
446cf2
446cf2
Conflicts:
446cf2
	elf/rtld.c
446cf2
	  (Caused by glibc-fedora-__libc_multiple_libcs.patch.)
446cf2
446cf2
diff --git a/csu/libc-tls.c b/csu/libc-tls.c
446cf2
index 08ed2b988b58ac6c..6f2a47dc86222407 100644
446cf2
--- a/csu/libc-tls.c
446cf2
+++ b/csu/libc-tls.c
446cf2
@@ -132,8 +132,8 @@ __libc_setup_tls (void)
446cf2
 	  break;
446cf2
 	}
446cf2
 
446cf2
-  /* Calculate the size of the static TLS surplus.  */
446cf2
-  _dl_tls_static_surplus_init ();
446cf2
+  /* Calculate the size of the static TLS surplus, with 0 auditors.  */
446cf2
+  _dl_tls_static_surplus_init (0);
446cf2
 
446cf2
   /* We have to set up the TCB block which also (possibly) contains
446cf2
      'errno'.  Therefore we avoid 'malloc' which might touch 'errno'.
446cf2
diff --git a/elf/dl-tls.c b/elf/dl-tls.c
446cf2
index ef57a21391bb36fa..cfda76f6de96df57 100644
446cf2
--- a/elf/dl-tls.c
446cf2
+++ b/elf/dl-tls.c
446cf2
@@ -49,7 +49,10 @@
446cf2
    that affects the size of the static TLS and by default it's small enough
446cf2
    not to cause problems with existing applications. The limit is not
446cf2
    enforced or checked: it is the user's responsibility to increase rtld.nns
446cf2
-   if more dlmopen namespaces are used.  */
446cf2
+   if more dlmopen namespaces are used.
446cf2
+
446cf2
+   Audit modules use their own namespaces, they are not included in rtld.nns,
446cf2
+   but come on top when computing the number of namespaces.  */
446cf2
 
446cf2
 /* Size of initial-exec TLS in libc.so.  */
446cf2
 #define LIBC_IE_TLS 192
446cf2
@@ -60,8 +63,11 @@
446cf2
 /* Size of additional surplus TLS, placeholder for TLS optimizations.  */
446cf2
 #define OPT_SURPLUS_TLS 512
446cf2
 
446cf2
+/* Calculate the size of the static TLS surplus, when the given
446cf2
+   number of audit modules are loaded.  Must be called after the
446cf2
+   number of audit modules is known and before static TLS allocation.  */
446cf2
 void
446cf2
-_dl_tls_static_surplus_init (void)
446cf2
+_dl_tls_static_surplus_init (size_t naudit)
446cf2
 {
446cf2
   size_t nns;
446cf2
 
446cf2
@@ -73,6 +79,11 @@ _dl_tls_static_surplus_init (void)
446cf2
 #endif
446cf2
   if (nns > DL_NNS)
446cf2
     nns = DL_NNS;
446cf2
+  if (DL_NNS - nns < naudit)
446cf2
+    _dl_fatal_printf ("Failed loading %lu audit modules, %lu are supported.\n",
446cf2
+		      (unsigned long) naudit, (unsigned long) (DL_NNS - nns));
446cf2
+  nns += naudit;
446cf2
+
446cf2
   GLRO(dl_tls_static_surplus) = ((nns - 1) * LIBC_IE_TLS
446cf2
 				 + nns * OTHER_IE_TLS
446cf2
 				 + OPT_SURPLUS_TLS);
446cf2
diff --git a/elf/rtld.c b/elf/rtld.c
446cf2
index a440741f4c1b3c91..67441ac6f252350e 100644
446cf2
--- a/elf/rtld.c
446cf2
+++ b/elf/rtld.c
446cf2
@@ -297,6 +297,23 @@ audit_list_next (struct audit_list *list)
446cf2
     }
446cf2
 }
446cf2
 
446cf2
+/* Count audit modules before they are loaded so GLRO(dl_naudit)
446cf2
+   is not yet usable.  */
446cf2
+static size_t
446cf2
+audit_list_count (struct audit_list *list)
446cf2
+{
446cf2
+  /* Restore the audit_list iterator state at the end.  */
446cf2
+  const char *saved_tail = list->current_tail;
446cf2
+  size_t naudit = 0;
446cf2
+
446cf2
+  assert (list->current_index == 0);
446cf2
+  while (audit_list_next (list) != NULL)
446cf2
+    naudit++;
446cf2
+  list->current_tail = saved_tail;
446cf2
+  list->current_index = 0;
446cf2
+  return naudit;
446cf2
+}
446cf2
+
446cf2
 /* Set nonzero during loading and initialization of executable and
446cf2
    libraries, cleared before the executable's entry point runs.  This
446cf2
    must not be initialized to nonzero, because the unused dynamic
446cf2
@@ -734,7 +751,7 @@ match_version (const char *string, struct link_map *map)
446cf2
 static bool tls_init_tp_called;
446cf2
 
446cf2
 static void *
446cf2
-init_tls (void)
446cf2
+init_tls (size_t naudit)
446cf2
 {
446cf2
   /* Number of elements in the static TLS block.  */
446cf2
   GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
446cf2
@@ -777,7 +794,7 @@ init_tls (void)
446cf2
   assert (i == GL(dl_tls_max_dtv_idx));
446cf2
 
446cf2
   /* Calculate the size of the static TLS surplus.  */
446cf2
-  _dl_tls_static_surplus_init ();
446cf2
+  _dl_tls_static_surplus_init (naudit);
446cf2
 
446cf2
   /* Compute the TLS offsets for the various blocks.  */
446cf2
   _dl_determine_tlsoffset ();
446cf2
@@ -1659,9 +1676,11 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
446cf2
   bool need_security_init = true;
446cf2
   if (audit_list.length > 0)
446cf2
     {
446cf2
+      size_t naudit = audit_list_count (&audit_list);
446cf2
+
446cf2
       /* Since we start using the auditing DSOs right away we need to
446cf2
 	 initialize the data structures now.  */
446cf2
-      tcbp = init_tls ();
446cf2
+      tcbp = init_tls (naudit);
446cf2
 
446cf2
       /* Initialize security features.  We need to do it this early
446cf2
 	 since otherwise the constructors of the audit libraries will
446cf2
@@ -1671,6 +1690,10 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
446cf2
       need_security_init = false;
446cf2
 
446cf2
       load_audit_modules (main_map, &audit_list);
446cf2
+
446cf2
+      /* The count based on audit strings may overestimate the number
446cf2
+	 of audit modules that got loaded, but not underestimate.  */
446cf2
+      assert (GLRO(dl_naudit) <= naudit);
446cf2
     }
446cf2
 
446cf2
   /* Keep track of the currently loaded modules to count how many
446cf2
@@ -1914,7 +1937,7 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
446cf2
      multiple threads (from a non-TLS-using libpthread).  */
446cf2
   bool was_tls_init_tp_called = tls_init_tp_called;
446cf2
   if (tcbp == NULL)
446cf2
-    tcbp = init_tls ();
446cf2
+    tcbp = init_tls (0);
446cf2
 
446cf2
   if (__glibc_likely (need_security_init))
446cf2
     /* Initialize security features.  But only if we have not done it
446cf2
diff --git a/manual/tunables.texi b/manual/tunables.texi
446cf2
index e092b8e81a18d739..e6a3e9a2cf5c959c 100644
446cf2
--- a/manual/tunables.texi
446cf2
+++ b/manual/tunables.texi
446cf2
@@ -241,9 +241,12 @@ Sets the number of supported dynamic link namespaces (see @code{dlmopen}).
446cf2
 Currently this limit can be set between 1 and 16 inclusive, the default is 4.
446cf2
 Each link namespace consumes some memory in all thread, and thus raising the
446cf2
 limit will increase the amount of memory each thread uses. Raising the limit
446cf2
-is useful when your application uses more than 4 dynamic linker audit modules
446cf2
-e.g. @env{LD_AUDIT}, or will use more than 4 dynamic link namespaces as created
446cf2
-by @code{dlmopen} with an lmid argument of @code{LM_ID_NEWLM}.
446cf2
+is useful when your application uses more than 4 dynamic link namespaces as
446cf2
+created by @code{dlmopen} with an lmid argument of @code{LM_ID_NEWLM}.
446cf2
+Dynamic linker audit modules are loaded in their own dynamic link namespaces,
446cf2
+but they are not accounted for in @code{glibc.rtld.nns}.  They implicitly
446cf2
+increase the per-thread memory usage as necessary, so this tunable does
446cf2
+not need to be changed to allow many audit modules e.g. via @env{LD_AUDIT}.
446cf2
 @end deftp
446cf2
 
446cf2
 @node Elision Tunables
446cf2
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
446cf2
index e54105848c3cb7d1..293f3ab5a496afdf 100644
446cf2
--- a/sysdeps/generic/ldsodefs.h
446cf2
+++ b/sysdeps/generic/ldsodefs.h
446cf2
@@ -1104,8 +1104,9 @@ extern size_t _dl_count_modids (void) attribute_hidden;
446cf2
 /* Calculate offset of the TLS blocks in the static TLS block.  */
446cf2
 extern void _dl_determine_tlsoffset (void) attribute_hidden;
446cf2
 
446cf2
-/* Calculate the size of the static TLS surplus.  */
446cf2
-void _dl_tls_static_surplus_init (void) attribute_hidden;
446cf2
+/* Calculate the size of the static TLS surplus, when the given
446cf2
+   number of audit modules are loaded.  */
446cf2
+void _dl_tls_static_surplus_init (size_t naudit) attribute_hidden;
446cf2
 
446cf2
 #ifndef SHARED
446cf2
 /* Set up the TCB for statically linked applications.  This is called