179894
commit 40ebfd016ad284872f434bdd76dbe9c708db4d6b
179894
Author: Florian Weimer <fweimer@redhat.com>
179894
Date:   Fri Jun 25 08:09:08 2021 +0200
179894
179894
    elf: Disable most of TLS modid gaps processing [BZ #27135]
179894
    
179894
    Revert "elf: Fix DTV gap reuse logic [BZ #27135]"
179894
    
179894
    This reverts commit 572bd547d57a39b6cf0ea072545dc4048921f4c3.
179894
    
179894
    It turns out that the _dl_next_tls_modid in _dl_map_object_from_fd keeps
179894
    returning the same modid over and over again if there is a gap and
179894
    more than TLS-using module is loaded in one dlopen call.  This corrupts
179894
    TLS data structures.  The bug is still present after a revert, but
179894
    empirically it is much more difficult to trigger (because it involves a
179894
    dlopen failure).
179894
179894
diff --git a/elf/dl-close.c b/elf/dl-close.c
179894
index 41cb6c58491c364b..7d2dc2272cd643f5 100644
179894
--- a/elf/dl-close.c
179894
+++ b/elf/dl-close.c
179894
@@ -88,11 +88,7 @@ remove_slotinfo (size_t idx, struct dtv_slotinfo_list *listp, size_t disp,
179894
       /* If this is not the last currently used entry no need to look
179894
 	 further.  */
179894
       if (idx != GL(dl_tls_max_dtv_idx))
179894
-	{
179894
-	  /* There is an unused dtv entry in the middle.  */
179894
-	  GL(dl_tls_dtv_gaps) = true;
179894
-	  return true;
179894
-	}
179894
+	return true;
179894
     }
179894
 
179894
   while (idx - disp > (disp == 0 ? 1 + GL(dl_tls_static_nelem) : 0))
179894
diff --git a/elf/dl-open.c b/elf/dl-open.c
179894
index 54727402750f4c0c..a67fb3aee40860e1 100644
179894
--- a/elf/dl-open.c
179894
+++ b/elf/dl-open.c
179894
@@ -896,6 +896,16 @@ no more namespaces available for dlmopen()"));
179894
 	 state if relocation failed, for example.  */
179894
       if (args.map)
179894
 	{
179894
+	  /* Maybe some of the modules which were loaded use TLS.
179894
+	     Since it will be removed in the following _dl_close call
179894
+	     we have to mark the dtv array as having gaps to fill the
179894
+	     holes.  This is a pessimistic assumption which won't hurt
179894
+	     if not true.  There is no need to do this when we are
179894
+	     loading the auditing DSOs since TLS has not yet been set
179894
+	     up.  */
179894
+	  if ((mode & __RTLD_AUDIT) == 0)
179894
+	    GL(dl_tls_dtv_gaps) = true;
179894
+
179894
 	  _dl_close_worker (args.map, true);
179894
 
179894
 	  /* All l_nodelete_pending objects should have been deleted
179894
diff --git a/elf/dl-tls.c b/elf/dl-tls.c
179894
index bacb4101e2e2c4e5..801eafad3961573c 100644
179894
--- a/elf/dl-tls.c
179894
+++ b/elf/dl-tls.c
179894
@@ -187,7 +187,10 @@ _dl_next_tls_modid (void)
179894
 size_t
179894
 _dl_count_modids (void)
179894
 {
179894
-  /* The count is the max unless dlclose or failed dlopen created gaps.  */
179894
+  /* It is rare that we have gaps; see elf/dl-open.c (_dl_open) where
179894
+     we fail to load a module and unload it leaving a gap.  If we don't
179894
+     have gaps then the number of modids is the current maximum so
179894
+     return that.  */
179894
   if (__glibc_likely (!GL(dl_tls_dtv_gaps)))
179894
     return GL(dl_tls_max_dtv_idx);
179894