d8307d
commit a509eb117fac1d764b15eba64993f4bdb63d7f3c
d8307d
Author: Florian Weimer <fweimer@redhat.com>
d8307d
Date:   Wed Nov 27 16:37:17 2019 +0100
d8307d
d8307d
    Avoid late dlopen failure due to scope, TLS slotinfo updates [BZ #25112]
d8307d
    
d8307d
    This change splits the scope and TLS slotinfo updates in dlopen into
d8307d
    two parts: one to resize the data structures, and one to actually apply
d8307d
    the update.  The call to add_to_global_resize in dl_open_worker is moved
d8307d
    before the demarcation point at which no further memory allocations are
d8307d
    allowed.
d8307d
    
d8307d
    _dl_add_to_slotinfo is adjusted to make the list update optional.  There
d8307d
    is some optimization possibility here because we could grow the slotinfo
d8307d
    list of arrays in a single call, one the largest TLS modid is known.
d8307d
    
d8307d
    This commit does not fix the fatal meory allocation failure in
d8307d
    _dl_update_slotinfo.  Ideally, this error during dlopen should be
d8307d
    recoverable.
d8307d
    
d8307d
    The update order of scopes and TLS data structures is retained, although
d8307d
    it appears to be more correct to fully initialize TLS first, and then
d8307d
    expose symbols in the newly loaded objects via the scope update.
d8307d
    
d8307d
    Tested on x86_64-linux-gnu.
d8307d
    
d8307d
    Change-Id: I240c58387dabda3ca1bcab48b02115175fa83d6c
d8307d
d8307d
diff --git a/elf/dl-open.c b/elf/dl-open.c
d8307d
index 85db4f0ecb5f29ce..b330cff7d349224a 100644
d8307d
--- a/elf/dl-open.c
d8307d
+++ b/elf/dl-open.c
d8307d
@@ -33,6 +33,7 @@
d8307d
 #include <stap-probe.h>
d8307d
 #include <atomic.h>
d8307d
 #include <libc-internal.h>
d8307d
+#include <array_length.h>
d8307d
 
d8307d
 #include <dl-dst.h>
d8307d
 #include <dl-prop.h>
d8307d
@@ -214,6 +215,215 @@ _dl_find_dso_for_object (const ElfW(Addr) addr)
d8307d
 }
d8307d
 rtld_hidden_def (_dl_find_dso_for_object);
d8307d
 
d8307d
+/* Return true if NEW is found in the scope for MAP.  */
d8307d
+static size_t
d8307d
+scope_has_map (struct link_map *map, struct link_map *new)
d8307d
+{
d8307d
+  size_t cnt;
d8307d
+  for (cnt = 0; map->l_scope[cnt] != NULL; ++cnt)
d8307d
+    if (map->l_scope[cnt] == &new->l_searchlist)
d8307d
+      return true;
d8307d
+  return false;
d8307d
+}
d8307d
+
d8307d
+/* Return the length of the scope for MAP.  */
d8307d
+static size_t
d8307d
+scope_size (struct link_map *map)
d8307d
+{
d8307d
+  size_t cnt;
d8307d
+  for (cnt = 0; map->l_scope[cnt] != NULL; )
d8307d
+    ++cnt;
d8307d
+  return cnt;
d8307d
+}
d8307d
+
d8307d
+/* Resize the scopes of depended-upon objects, so that the new object
d8307d
+   can be added later without further allocation of memory.  This
d8307d
+   function can raise an exceptions due to malloc failure.  */
d8307d
+static void
d8307d
+resize_scopes (struct link_map *new)
d8307d
+{
d8307d
+  /* If the file is not loaded now as a dependency, add the search
d8307d
+     list of the newly loaded object to the scope.  */
d8307d
+  for (unsigned int i = 0; i < new->l_searchlist.r_nlist; ++i)
d8307d
+    {
d8307d
+      struct link_map *imap = new->l_searchlist.r_list[i];
d8307d
+
d8307d
+      /* If the initializer has been called already, the object has
d8307d
+	 not been loaded here and now.  */
d8307d
+      if (imap->l_init_called && imap->l_type == lt_loaded)
d8307d
+	{
d8307d
+	  if (scope_has_map (imap, new))
d8307d
+	    /* Avoid duplicates.  */
d8307d
+	    continue;
d8307d
+
d8307d
+	  size_t cnt = scope_size (imap);
d8307d
+	  if (__glibc_unlikely (cnt + 1 >= imap->l_scope_max))
d8307d
+	    {
d8307d
+	      /* The l_scope array is too small.  Allocate a new one
d8307d
+		 dynamically.  */
d8307d
+	      size_t new_size;
d8307d
+	      struct r_scope_elem **newp;
d8307d
+
d8307d
+	      if (imap->l_scope != imap->l_scope_mem
d8307d
+		  && imap->l_scope_max < array_length (imap->l_scope_mem))
d8307d
+		{
d8307d
+		  /* If the current l_scope memory is not pointing to
d8307d
+		     the static memory in the structure, but the
d8307d
+		     static memory in the structure is large enough to
d8307d
+		     use for cnt + 1 scope entries, then switch to
d8307d
+		     using the static memory.  */
d8307d
+		  new_size = array_length (imap->l_scope_mem);
d8307d
+		  newp = imap->l_scope_mem;
d8307d
+		}
d8307d
+	      else
d8307d
+		{
d8307d
+		  new_size = imap->l_scope_max * 2;
d8307d
+		  newp = (struct r_scope_elem **)
d8307d
+		    malloc (new_size * sizeof (struct r_scope_elem *));
d8307d
+		  if (newp == NULL)
d8307d
+		    _dl_signal_error (ENOMEM, "dlopen", NULL,
d8307d
+				      N_("cannot create scope list"));
d8307d
+		}
d8307d
+
d8307d
+	      /* Copy the array and the terminating NULL.  */
d8307d
+	      memcpy (newp, imap->l_scope,
d8307d
+		      (cnt + 1) * sizeof (imap->l_scope[0]));
d8307d
+	      struct r_scope_elem **old = imap->l_scope;
d8307d
+
d8307d
+	      imap->l_scope = newp;
d8307d
+
d8307d
+	      if (old != imap->l_scope_mem)
d8307d
+		_dl_scope_free (old);
d8307d
+
d8307d
+	      imap->l_scope_max = new_size;
d8307d
+	    }
d8307d
+	}
d8307d
+    }
d8307d
+}
d8307d
+
d8307d
+/* Second stage of resize_scopes: Add NEW to the scopes.  Also print
d8307d
+   debugging information about scopes if requested.
d8307d
+
d8307d
+   This function cannot raise an exception because all required memory
d8307d
+   has been allocated by a previous call to resize_scopes.  */
d8307d
+static void
d8307d
+update_scopes (struct link_map *new)
d8307d
+{
d8307d
+  for (unsigned int i = 0; i < new->l_searchlist.r_nlist; ++i)
d8307d
+    {
d8307d
+      struct link_map *imap = new->l_searchlist.r_list[i];
d8307d
+      int from_scope = 0;
d8307d
+
d8307d
+      if (imap->l_init_called && imap->l_type == lt_loaded)
d8307d
+	{
d8307d
+	  if (scope_has_map (imap, new))
d8307d
+	    /* Avoid duplicates.  */
d8307d
+	    continue;
d8307d
+
d8307d
+	  size_t cnt = scope_size (imap);
d8307d
+	  /* Assert that resize_scopes has sufficiently enlarged the
d8307d
+	     array.  */
d8307d
+	  assert (cnt + 1 < imap->l_scope_max);
d8307d
+
d8307d
+	  /* First terminate the extended list.  Otherwise a thread
d8307d
+	     might use the new last element and then use the garbage
d8307d
+	     at offset IDX+1.  */
d8307d
+	  imap->l_scope[cnt + 1] = NULL;
d8307d
+	  atomic_write_barrier ();
d8307d
+	  imap->l_scope[cnt] = &new->l_searchlist;
d8307d
+
d8307d
+	  from_scope = cnt;
d8307d
+	}
d8307d
+
d8307d
+      /* Print scope information.  */
d8307d
+      if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES))
d8307d
+	_dl_show_scope (imap, from_scope);
d8307d
+    }
d8307d
+}
d8307d
+
d8307d
+/* Call _dl_add_to_slotinfo with DO_ADD set to false, to allocate
d8307d
+   space in GL (dl_tls_dtv_slotinfo_list).  This can raise an
d8307d
+   exception.  The return value is true if any of the new objects use
d8307d
+   TLS.  */
d8307d
+static bool
d8307d
+resize_tls_slotinfo (struct link_map *new)
d8307d
+{
d8307d
+  bool any_tls = false;
d8307d
+  for (unsigned int i = 0; i < new->l_searchlist.r_nlist; ++i)
d8307d
+    {
d8307d
+      struct link_map *imap = new->l_searchlist.r_list[i];
d8307d
+
d8307d
+      /* Only add TLS memory if this object is loaded now and
d8307d
+	 therefore is not yet initialized.  */
d8307d
+      if (! imap->l_init_called && imap->l_tls_blocksize > 0)
d8307d
+	{
d8307d
+	  _dl_add_to_slotinfo (imap, false);
d8307d
+	  any_tls = true;
d8307d
+	}
d8307d
+    }
d8307d
+  return any_tls;
d8307d
+}
d8307d
+
d8307d
+/* Second stage of TLS update, after resize_tls_slotinfo.  This
d8307d
+   function does not raise any exception.  It should only be called if
d8307d
+   resize_tls_slotinfo returned true.  */
d8307d
+static void
d8307d
+update_tls_slotinfo (struct link_map *new)
d8307d
+{
d8307d
+  unsigned int first_static_tls = new->l_searchlist.r_nlist;
d8307d
+  for (unsigned int i = 0; i < new->l_searchlist.r_nlist; ++i)
d8307d
+    {
d8307d
+      struct link_map *imap = new->l_searchlist.r_list[i];
d8307d
+
d8307d
+      /* Only add TLS memory if this object is loaded now and
d8307d
+	 therefore is not yet initialized.  */
d8307d
+      if (! imap->l_init_called && imap->l_tls_blocksize > 0)
d8307d
+	{
d8307d
+	  _dl_add_to_slotinfo (imap, true);
d8307d
+
d8307d
+	  if (imap->l_need_tls_init
d8307d
+	      && first_static_tls == new->l_searchlist.r_nlist)
d8307d
+	    first_static_tls = i;
d8307d
+	}
d8307d
+    }
d8307d
+
d8307d
+  if (__builtin_expect (++GL(dl_tls_generation) == 0, 0))
d8307d
+    _dl_fatal_printf (N_("\
d8307d
+TLS generation counter wrapped!  Please report this."));
d8307d
+
d8307d
+  /* We need a second pass for static tls data, because
d8307d
+     _dl_update_slotinfo must not be run while calls to
d8307d
+     _dl_add_to_slotinfo are still pending.  */
d8307d
+  for (unsigned int i = first_static_tls; i < new->l_searchlist.r_nlist; ++i)
d8307d
+    {
d8307d
+      struct link_map *imap = new->l_searchlist.r_list[i];
d8307d
+
d8307d
+      if (imap->l_need_tls_init
d8307d
+	  && ! imap->l_init_called
d8307d
+	  && imap->l_tls_blocksize > 0)
d8307d
+	{
d8307d
+	  /* For static TLS we have to allocate the memory here and
d8307d
+	     now, but we can delay updating the DTV.  */
d8307d
+	  imap->l_need_tls_init = 0;
d8307d
+#ifdef SHARED
d8307d
+	  /* Update the slot information data for at least the
d8307d
+	     generation of the DSO we are allocating data for.  */
d8307d
+
d8307d
+	  /* FIXME: This can terminate the process on memory
d8307d
+	     allocation failure.  It is not possible to raise
d8307d
+	     exceptions from this context; to fix this bug,
d8307d
+	     _dl_update_slotinfo would have to be split into two
d8307d
+	     operations, similar to resize_scopes and update_scopes
d8307d
+	     above.  This is related to bug 16134.  */
d8307d
+	  _dl_update_slotinfo (imap->l_tls_modid);
d8307d
+#endif
d8307d
+
d8307d
+	  GL(dl_init_static_tls) (imap);
d8307d
+	  assert (imap->l_need_tls_init == 0);
d8307d
+	}
d8307d
+    }
d8307d
+}
d8307d
+
d8307d
 /* struct dl_init_args and call_dl_init are used to call _dl_init with
d8307d
    exception handling disabled.  */
d8307d
 struct dl_init_args
d8307d
@@ -431,133 +641,40 @@ dl_open_worker (void *a)
d8307d
      relocation.  */
d8307d
   _dl_open_check (new);
d8307d
 
d8307d
-  /* If the file is not loaded now as a dependency, add the search
d8307d
-     list of the newly loaded object to the scope.  */
d8307d
-  bool any_tls = false;
d8307d
-  unsigned int first_static_tls = new->l_searchlist.r_nlist;
d8307d
-  for (unsigned int i = 0; i < new->l_searchlist.r_nlist; ++i)
d8307d
-    {
d8307d
-      struct link_map *imap = new->l_searchlist.r_list[i];
d8307d
-      int from_scope = 0;
d8307d
+  /* This only performs the memory allocations.  The actual update of
d8307d
+     the scopes happens below, after failure is impossible.  */
d8307d
+  resize_scopes (new);
d8307d
 
d8307d
-      /* If the initializer has been called already, the object has
d8307d
-	 not been loaded here and now.  */
d8307d
-      if (imap->l_init_called && imap->l_type == lt_loaded)
d8307d
-	{
d8307d
-	  struct r_scope_elem **runp = imap->l_scope;
d8307d
-	  size_t cnt = 0;
d8307d
-
d8307d
-	  while (*runp != NULL)
d8307d
-	    {
d8307d
-	      if (*runp == &new->l_searchlist)
d8307d
-		break;
d8307d
-	      ++cnt;
d8307d
-	      ++runp;
d8307d
-	    }
d8307d
-
d8307d
-	  if (*runp != NULL)
d8307d
-	    /* Avoid duplicates.  */
d8307d
-	    continue;
d8307d
-
d8307d
-	  if (__glibc_unlikely (cnt + 1 >= imap->l_scope_max))
d8307d
-	    {
d8307d
-	      /* The 'r_scope' array is too small.  Allocate a new one
d8307d
-		 dynamically.  */
d8307d
-	      size_t new_size;
d8307d
-	      struct r_scope_elem **newp;
d8307d
-
d8307d
-#define SCOPE_ELEMS(imap) \
d8307d
-  (sizeof (imap->l_scope_mem) / sizeof (imap->l_scope_mem[0]))
d8307d
+  /* Increase the size of the GL (dl_tls_dtv_slotinfo_list) data
d8307d
+     structure.  */
d8307d
+  bool any_tls = resize_tls_slotinfo (new);
d8307d
 
d8307d
-	      if (imap->l_scope != imap->l_scope_mem
d8307d
-		  && imap->l_scope_max < SCOPE_ELEMS (imap))
d8307d
-		{
d8307d
-		  new_size = SCOPE_ELEMS (imap);
d8307d
-		  newp = imap->l_scope_mem;
d8307d
-		}
d8307d
-	      else
d8307d
-		{
d8307d
-		  new_size = imap->l_scope_max * 2;
d8307d
-		  newp = (struct r_scope_elem **)
d8307d
-		    malloc (new_size * sizeof (struct r_scope_elem *));
d8307d
-		  if (newp == NULL)
d8307d
-		    _dl_signal_error (ENOMEM, "dlopen", NULL,
d8307d
-				      N_("cannot create scope list"));
d8307d
-		}
d8307d
-
d8307d
-	      memcpy (newp, imap->l_scope, cnt * sizeof (imap->l_scope[0]));
d8307d
-	      struct r_scope_elem **old = imap->l_scope;
d8307d
-
d8307d
-	      imap->l_scope = newp;
d8307d
-
d8307d
-	      if (old != imap->l_scope_mem)
d8307d
-		_dl_scope_free (old);
d8307d
-
d8307d
-	      imap->l_scope_max = new_size;
d8307d
-	    }
d8307d
-
d8307d
-	  /* First terminate the extended list.  Otherwise a thread
d8307d
-	     might use the new last element and then use the garbage
d8307d
-	     at offset IDX+1.  */
d8307d
-	  imap->l_scope[cnt + 1] = NULL;
d8307d
-	  atomic_write_barrier ();
d8307d
-	  imap->l_scope[cnt] = &new->l_searchlist;
d8307d
-
d8307d
-	  /* Print only new scope information.  */
d8307d
-	  from_scope = cnt;
d8307d
-	}
d8307d
-      /* Only add TLS memory if this object is loaded now and
d8307d
-	 therefore is not yet initialized.  */
d8307d
-      else if (! imap->l_init_called
d8307d
-	       /* Only if the module defines thread local data.  */
d8307d
-	       && __builtin_expect (imap->l_tls_blocksize > 0, 0))
d8307d
-	{
d8307d
-	  /* Now that we know the object is loaded successfully add
d8307d
-	     modules containing TLS data to the slot info table.  We
d8307d
-	     might have to increase its size.  */
d8307d
-	  _dl_add_to_slotinfo (imap);
d8307d
-
d8307d
-	  if (imap->l_need_tls_init
d8307d
-	      && first_static_tls == new->l_searchlist.r_nlist)
d8307d
-	    first_static_tls = i;
d8307d
-
d8307d
-	  /* We have to bump the generation counter.  */
d8307d
-	  any_tls = true;
d8307d
-	}
d8307d
-
d8307d
-      /* Print scope information.  */
d8307d
-      if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES))
d8307d
-	_dl_show_scope (imap, from_scope);
d8307d
-    }
d8307d
-
d8307d
-  /* Bump the generation number if necessary.  */
d8307d
-  if (any_tls && __builtin_expect (++GL(dl_tls_generation) == 0, 0))
d8307d
-    _dl_fatal_printf (N_("\
d8307d
-TLS generation counter wrapped!  Please report this."));
d8307d
-
d8307d
-  /* We need a second pass for static tls data, because _dl_update_slotinfo
d8307d
-     must not be run while calls to _dl_add_to_slotinfo are still pending.  */
d8307d
-  for (unsigned int i = first_static_tls; i < new->l_searchlist.r_nlist; ++i)
d8307d
-    {
d8307d
-      struct link_map *imap = new->l_searchlist.r_list[i];
d8307d
-
d8307d
-      if (imap->l_need_tls_init
d8307d
-	  && ! imap->l_init_called
d8307d
-	  && imap->l_tls_blocksize > 0)
d8307d
-	{
d8307d
-	  /* For static TLS we have to allocate the memory here and
d8307d
-	     now, but we can delay updating the DTV.  */
d8307d
-	  imap->l_need_tls_init = 0;
d8307d
-#ifdef SHARED
d8307d
-	  /* Update the slot information data for at least the
d8307d
-	     generation of the DSO we are allocating data for.  */
d8307d
-	  _dl_update_slotinfo (imap->l_tls_modid);
d8307d
-#endif
d8307d
+  /* Perform the necessary allocations for adding new global objects
d8307d
+     to the global scope below.  */
d8307d
+  if (mode & RTLD_GLOBAL)
d8307d
+    add_to_global_resize (new);
d8307d
 
d8307d
-	  GL(dl_init_static_tls) (imap);
d8307d
-	  assert (imap->l_need_tls_init == 0);
d8307d
-	}
d8307d
-    }
d8307d
+  /* Demarcation point: After this, no recoverable errors are allowed.
d8307d
+     All memory allocations for new objects must have happened
d8307d
+     before.  */
d8307d
+
d8307d
+  /* Second stage after resize_scopes: Actually perform the scope
d8307d
+     update.  After this, dlsym and lazy binding can bind to new
d8307d
+     objects.  */
d8307d
+  update_scopes (new);
d8307d
+
d8307d
+  /* FIXME: It is unclear whether the order here is correct.
d8307d
+     Shouldn't new objects be made available for binding (and thus
d8307d
+     execution) only after there TLS data has been set up fully?
d8307d
+     Fixing bug 16134 will likely make this distinction less
d8307d
+     important.  */
d8307d
+
d8307d
+  /* Second stage after resize_tls_slotinfo: Update the slotinfo data
d8307d
+     structures.  */
d8307d
+  if (any_tls)
d8307d
+    /* FIXME: This calls _dl_update_slotinfo, which aborts the process
d8307d
+       on memory allocation failure.  See bug 16134.  */
d8307d
+    update_tls_slotinfo (new);
d8307d
 
d8307d
   /* Notify the debugger all new objects have been relocated.  */
d8307d
   if (relocation_in_progress)
d8307d
diff --git a/elf/dl-tls.c b/elf/dl-tls.c
d8307d
index c87caf13d6a97ba4..a2def280b7096960 100644
d8307d
--- a/elf/dl-tls.c
d8307d
+++ b/elf/dl-tls.c
d8307d
@@ -883,7 +883,7 @@ _dl_tls_get_addr_soft (struct link_map *l)
d8307d
 
d8307d
 
d8307d
 void
d8307d
-_dl_add_to_slotinfo (struct link_map *l)
d8307d
+_dl_add_to_slotinfo (struct link_map *l, bool do_add)
d8307d
 {
d8307d
   /* Now that we know the object is loaded successfully add
d8307d
      modules containing TLS data to the dtv info table.  We
d8307d
@@ -939,6 +939,9 @@ cannot create TLS data structures"));
d8307d
     }
d8307d
 
d8307d
   /* Add the information into the slotinfo data structure.  */
d8307d
-  listp->slotinfo[idx].map = l;
d8307d
-  listp->slotinfo[idx].gen = GL(dl_tls_generation) + 1;
d8307d
+  if (do_add)
d8307d
+    {
d8307d
+      listp->slotinfo[idx].map = l;
d8307d
+      listp->slotinfo[idx].gen = GL(dl_tls_generation) + 1;
d8307d
+    }
d8307d
 }
d8307d
diff --git a/elf/rtld.c b/elf/rtld.c
d8307d
index 4ec26a79cbb0aa4f..0aa1a2a19f649e16 100644
d8307d
--- a/elf/rtld.c
d8307d
+++ b/elf/rtld.c
d8307d
@@ -2167,7 +2167,7 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
d8307d
 
d8307d
 	  /* Add object to slot information data if necessasy.  */
d8307d
 	  if (l->l_tls_blocksize != 0 && tls_init_tp_called)
d8307d
-	    _dl_add_to_slotinfo (l);
d8307d
+	    _dl_add_to_slotinfo (l, true);
d8307d
 	}
d8307d
     }
d8307d
   else
d8307d
@@ -2215,7 +2215,7 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
d8307d
 
d8307d
 	  /* Add object to slot information data if necessasy.  */
d8307d
 	  if (l->l_tls_blocksize != 0 && tls_init_tp_called)
d8307d
-	    _dl_add_to_slotinfo (l);
d8307d
+	    _dl_add_to_slotinfo (l, true);
d8307d
 	}
d8307d
       HP_TIMING_NOW (stop);
d8307d
 
d8307d
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
d8307d
index 57fbefea3cb841e9..c6b7e61badbfd513 100644
d8307d
--- a/sysdeps/generic/ldsodefs.h
d8307d
+++ b/sysdeps/generic/ldsodefs.h
d8307d
@@ -1135,8 +1135,15 @@ extern void *_dl_open (const char *name, int mode, const void *caller,
d8307d
    old scope, OLD can't be freed until no thread is using it.  */
d8307d
 extern int _dl_scope_free (void *) attribute_hidden;
d8307d
 
d8307d
-/* Add module to slot information data.  */
d8307d
-extern void _dl_add_to_slotinfo (struct link_map  *l) attribute_hidden;
d8307d
+
d8307d
+/* Add module to slot information data.  If DO_ADD is false, only the
d8307d
+   required memory is allocated.  Must be called with GL
d8307d
+   (dl_load_lock) acquired.  If the function has already been called
d8307d
+   for the link map L with !do_add, then this function will not raise
d8307d
+   an exception, otherwise it is possible that it encounters a memory
d8307d
+   allocation failure.  */
d8307d
+extern void _dl_add_to_slotinfo (struct link_map *l, bool do_add)
d8307d
+  attribute_hidden;
d8307d
 
d8307d
 /* Update slot information data for at least the generation of the
d8307d
    module with the given index.  */