d8307d
commit 440b7f8653e4ed8f6e1425145208050b795e9a6c
d8307d
Author: Florian Weimer <fweimer@redhat.com>
d8307d
Date:   Thu Oct 31 18:25:39 2019 +0100
d8307d
d8307d
    Avoid late failure in dlopen in global scope update [BZ #25112]
d8307d
    
d8307d
    The call to add_to_global in dl_open_worker happens after running ELF
d8307d
    constructors for new objects.  At this point, proper recovery from
d8307d
    malloc failure would be quite complicated: We would have to run the
d8307d
    ELF destructors and close all opened objects, something that we
d8307d
    currently do not do.
d8307d
    
d8307d
    Instead, this change splits add_to_global into two phases,
d8307d
    add_to_global_resize (which can raise an exception, called before ELF
d8307d
    constructors run), and add_to_global_update (which cannot, called
d8307d
    after ELF constructors).  A complication arises due to recursive
d8307d
    dlopen: After the inner dlopen consumes some space, the pre-allocation
d8307d
    in the outer dlopen may no longer be sufficient.  A new member in the
d8307d
    namespace structure, _ns_global_scope_pending_adds keeps track of the
d8307d
    maximum number of objects that need to be added to the global scope.
d8307d
    This enables the inner add_to_global_resize call to take into account
d8307d
    the needs of an outer dlopen.
d8307d
    
d8307d
    Most code in the dynamic linker assumes that the number of global
d8307d
    scope entries fits into an unsigned int (matching the r_nlist member
d8307d
    of struct r_scop_elem).  Therefore, change the type of
d8307d
    _ns_global_scope_alloc to unsigned int (from size_t), and add overflow
d8307d
    checks.
d8307d
    
d8307d
    Change-Id: Ie08e2f318510d5a6a4bcb1c315f46791b5b77524
d8307d
d8307d
diff --git a/elf/dl-open.c b/elf/dl-open.c
d8307d
index c9c0254ee74c4f4b..85db4f0ecb5f29ce 100644
d8307d
--- a/elf/dl-open.c
d8307d
+++ b/elf/dl-open.c
d8307d
@@ -50,22 +50,38 @@ struct dl_open_args
d8307d
   struct link_map *map;
d8307d
   /* Namespace ID.  */
d8307d
   Lmid_t nsid;
d8307d
+
d8307d
+  /* Original value of _ns_global_scope_pending_adds.  Set by
d8307d
+     dl_open_worker.  Only valid if nsid is a real namespace
d8307d
+     (non-negative).  */
d8307d
+  unsigned int original_global_scope_pending_adds;
d8307d
+
d8307d
   /* Original parameters to the program and the current environment.  */
d8307d
   int argc;
d8307d
   char **argv;
d8307d
   char **env;
d8307d
 };
d8307d
 
d8307d
+/* Called in case the global scope cannot be extended.  */
d8307d
+static void __attribute__ ((noreturn))
d8307d
+add_to_global_resize_failure (struct link_map *new)
d8307d
+{
d8307d
+  _dl_signal_error (ENOMEM, new->l_libname->name, NULL,
d8307d
+		    N_ ("cannot extend global scope"));
d8307d
+}
d8307d
 
d8307d
-static int
d8307d
-add_to_global (struct link_map *new)
d8307d
+/* Grow the global scope array for the namespace, so that all the new
d8307d
+   global objects can be added later in add_to_global_update, without
d8307d
+   risk of memory allocation failure.  add_to_global_resize raises
d8307d
+   exceptions for memory allocation errors.  */
d8307d
+static void
d8307d
+add_to_global_resize (struct link_map *new)
d8307d
 {
d8307d
-  struct link_map **new_global;
d8307d
-  unsigned int to_add = 0;
d8307d
-  unsigned int cnt;
d8307d
+  struct link_namespaces *ns = &GL (dl_ns)[new->l_ns];
d8307d
 
d8307d
   /* Count the objects we have to put in the global scope.  */
d8307d
-  for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
d8307d
+  unsigned int to_add = 0;
d8307d
+  for (unsigned int cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
d8307d
     if (new->l_searchlist.r_list[cnt]->l_global == 0)
d8307d
       ++to_add;
d8307d
 
d8307d
@@ -83,47 +99,51 @@ add_to_global (struct link_map *new)
d8307d
      in an realloc() call.  Therefore we allocate a completely new
d8307d
      array the first time we have to add something to the locale scope.  */
d8307d
 
d8307d
-  struct link_namespaces *ns = &GL(dl_ns)[new->l_ns];
d8307d
+  if (__builtin_add_overflow (ns->_ns_global_scope_pending_adds, to_add,
d8307d
+			      &ns->_ns_global_scope_pending_adds))
d8307d
+    add_to_global_resize_failure (new);
d8307d
+
d8307d
+  unsigned int new_size = 0; /* 0 means no new allocation.  */
d8307d
+  void *old_global = NULL; /* Old allocation if free-able.  */
d8307d
+
d8307d
+  /* Minimum required element count for resizing.  Adjusted below for
d8307d
+     an exponential resizing policy.  */
d8307d
+  size_t required_new_size;
d8307d
+  if (__builtin_add_overflow (ns->_ns_main_searchlist->r_nlist,
d8307d
+			      ns->_ns_global_scope_pending_adds,
d8307d
+			      &required_new_size))
d8307d
+    add_to_global_resize_failure (new);
d8307d
+
d8307d
   if (ns->_ns_global_scope_alloc == 0)
d8307d
     {
d8307d
-      /* This is the first dynamic object given global scope.  */
d8307d
-      ns->_ns_global_scope_alloc
d8307d
-	= ns->_ns_main_searchlist->r_nlist + to_add + 8;
d8307d
-      new_global = (struct link_map **)
d8307d
-	malloc (ns->_ns_global_scope_alloc * sizeof (struct link_map *));
d8307d
-      if (new_global == NULL)
d8307d
-	{
d8307d
-	  ns->_ns_global_scope_alloc = 0;
d8307d
-	nomem:
d8307d
-	  _dl_signal_error (ENOMEM, new->l_libname->name, NULL,
d8307d
-			    N_("cannot extend global scope"));
d8307d
-	  return 1;
d8307d
-	}
d8307d
+      if (__builtin_add_overflow (required_new_size, 8, &new_size))
d8307d
+	add_to_global_resize_failure (new);
d8307d
+    }
d8307d
+  else if (required_new_size > ns->_ns_global_scope_alloc)
d8307d
+    {
d8307d
+      if (__builtin_mul_overflow (required_new_size, 2, &new_size))
d8307d
+	add_to_global_resize_failure (new);
d8307d
 
d8307d
-      /* Copy over the old entries.  */
d8307d
-      ns->_ns_main_searchlist->r_list
d8307d
-	= memcpy (new_global, ns->_ns_main_searchlist->r_list,
d8307d
-		  (ns->_ns_main_searchlist->r_nlist
d8307d
-		   * sizeof (struct link_map *)));
d8307d
+      /* The old array was allocated with our malloc, not the minimal
d8307d
+	 malloc.  */
d8307d
+      old_global = ns->_ns_main_searchlist->r_list;
d8307d
     }
d8307d
-  else if (ns->_ns_main_searchlist->r_nlist + to_add
d8307d
-	   > ns->_ns_global_scope_alloc)
d8307d
+
d8307d
+  if (new_size > 0)
d8307d
     {
d8307d
-      /* We have to extend the existing array of link maps in the
d8307d
-	 main map.  */
d8307d
-      struct link_map **old_global
d8307d
-	= GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_list;
d8307d
-      size_t new_nalloc = ((ns->_ns_global_scope_alloc + to_add) * 2);
d8307d
-
d8307d
-      new_global = (struct link_map **)
d8307d
-	malloc (new_nalloc * sizeof (struct link_map *));
d8307d
+      size_t allocation_size;
d8307d
+      if (__builtin_mul_overflow (new_size, sizeof (struct link_map *),
d8307d
+				  &allocation_size))
d8307d
+	add_to_global_resize_failure (new);
d8307d
+      struct link_map **new_global = malloc (allocation_size);
d8307d
       if (new_global == NULL)
d8307d
-	goto nomem;
d8307d
+	add_to_global_resize_failure (new);
d8307d
 
d8307d
-      memcpy (new_global, old_global,
d8307d
-	      ns->_ns_global_scope_alloc * sizeof (struct link_map *));
d8307d
+      /* Copy over the old entries.  */
d8307d
+      memcpy (new_global, ns->_ns_main_searchlist->r_list,
d8307d
+	      ns->_ns_main_searchlist->r_nlist * sizeof (struct link_map *));
d8307d
 
d8307d
-      ns->_ns_global_scope_alloc = new_nalloc;
d8307d
+      ns->_ns_global_scope_alloc = new_size;
d8307d
       ns->_ns_main_searchlist->r_list = new_global;
d8307d
 
d8307d
       if (!RTLD_SINGLE_THREAD_P)
d8307d
@@ -131,16 +151,28 @@ add_to_global (struct link_map *new)
d8307d
 
d8307d
       free (old_global);
d8307d
     }
d8307d
+}
d8307d
+
d8307d
+/* Actually add the new global objects to the global scope.  Must be
d8307d
+   called after add_to_global_resize.  This function cannot fail.  */
d8307d
+static void
d8307d
+add_to_global_update (struct link_map *new)
d8307d
+{
d8307d
+  struct link_namespaces *ns = &GL (dl_ns)[new->l_ns];
d8307d
 
d8307d
   /* Now add the new entries.  */
d8307d
   unsigned int new_nlist = ns->_ns_main_searchlist->r_nlist;
d8307d
-  for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
d8307d
+  for (unsigned int cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
d8307d
     {
d8307d
       struct link_map *map = new->l_searchlist.r_list[cnt];
d8307d
 
d8307d
       if (map->l_global == 0)
d8307d
 	{
d8307d
 	  map->l_global = 1;
d8307d
+
d8307d
+	  /* The array has been resized by add_to_global_resize.  */
d8307d
+	  assert (new_nlist < ns->_ns_global_scope_alloc);
d8307d
+
d8307d
 	  ns->_ns_main_searchlist->r_list[new_nlist++] = map;
d8307d
 
d8307d
 	  /* We modify the global scope.  Report this.  */
d8307d
@@ -149,10 +181,15 @@ add_to_global (struct link_map *new)
d8307d
 			      map->l_name, map->l_ns);
d8307d
 	}
d8307d
     }
d8307d
+
d8307d
+  /* Some of the pending adds have been performed by the loop above.
d8307d
+     Adjust the counter accordingly.  */
d8307d
+  unsigned int added = new_nlist - ns->_ns_main_searchlist->r_nlist;
d8307d
+  assert (added <= ns->_ns_global_scope_pending_adds);
d8307d
+  ns->_ns_global_scope_pending_adds -= added;
d8307d
+
d8307d
   atomic_write_barrier ();
d8307d
   ns->_ns_main_searchlist->r_nlist = new_nlist;
d8307d
-
d8307d
-  return 0;
d8307d
 }
d8307d
 
d8307d
 /* Search link maps in all namespaces for the DSO that contains the object at
d8307d
@@ -225,6 +262,10 @@ dl_open_worker (void *a)
d8307d
 	args->nsid = call_map->l_ns;
d8307d
     }
d8307d
 
d8307d
+  /* Retain the old value, so that it can be restored.  */
d8307d
+  args->original_global_scope_pending_adds
d8307d
+    = GL (dl_ns)[args->nsid]._ns_global_scope_pending_adds;
d8307d
+
d8307d
   /* One might be tempted to assert that we are RT_CONSISTENT at this point, but that
d8307d
      may not be true if this is a recursive call to dlopen.  */
d8307d
   _dl_debug_initialize (0, args->nsid);
d8307d
@@ -266,7 +307,10 @@ dl_open_worker (void *a)
d8307d
       /* If the user requested the object to be in the global namespace
d8307d
 	 but it is not so far, add it now.  */
d8307d
       if ((mode & RTLD_GLOBAL) && new->l_global == 0)
d8307d
-	(void) add_to_global (new);
d8307d
+	{
d8307d
+	  add_to_global_resize (new);
d8307d
+	  add_to_global_update (new);
d8307d
+	}
d8307d
 
d8307d
       assert (_dl_debug_initialize (0, args->nsid)->r_state == RT_CONSISTENT);
d8307d
 
d8307d
@@ -523,6 +567,11 @@ TLS generation counter wrapped!  Please report this."));
d8307d
   DL_STATIC_INIT (new);
d8307d
 #endif
d8307d
 
d8307d
+  /* Perform the necessary allocations for adding new global objects
d8307d
+     to the global scope below, via add_to_global_update.  */
d8307d
+  if (mode & RTLD_GLOBAL)
d8307d
+    add_to_global_resize (new);
d8307d
+
d8307d
   /* Run the initializer functions of new objects.  Temporarily
d8307d
      disable the exception handler, so that lazy binding failures are
d8307d
      fatal.  */
d8307d
@@ -539,10 +588,7 @@ TLS generation counter wrapped!  Please report this."));
d8307d
 
d8307d
   /* Now we can make the new map available in the global scope.  */
d8307d
   if (mode & RTLD_GLOBAL)
d8307d
-    /* Move the object in the global namespace.  */
d8307d
-    if (add_to_global (new) != 0)
d8307d
-      /* It failed.  */
d8307d
-      return;
d8307d
+    add_to_global_update (new);
d8307d
 
d8307d
 #ifndef SHARED
d8307d
   /* We must be the static _dl_open in libc.a.  A static program that
d8307d
@@ -556,7 +602,6 @@ TLS generation counter wrapped!  Please report this."));
d8307d
 		      new->l_name, new->l_ns, new->l_direct_opencount);
d8307d
 }
d8307d
 
d8307d
-
d8307d
 void *
d8307d
 _dl_open (const char *file, int mode, const void *caller_dlopen, Lmid_t nsid,
d8307d
 	  int argc, char *argv[], char *env[])
d8307d
@@ -624,6 +669,19 @@ no more namespaces available for dlmopen()"));
d8307d
   _dl_unload_cache ();
d8307d
 #endif
d8307d
 
d8307d
+  /* Do this for both the error and success cases.  The old value has
d8307d
+     only been determined if the namespace ID was assigned (i.e., it
d8307d
+     is not __LM_ID_CALLER).  In the success case, we actually may
d8307d
+     have consumed more pending adds than planned (because the local
d8307d
+     scopes overlap in case of a recursive dlopen, the inner dlopen
d8307d
+     doing some of the globalization work of the outer dlopen), so the
d8307d
+     old pending adds value is larger than absolutely necessary.
d8307d
+     Since it is just a conservative upper bound, this is harmless.
d8307d
+     The top-level dlopen call will restore the field to zero.  */
d8307d
+  if (args.nsid >= 0)
d8307d
+    GL (dl_ns)[args.nsid]._ns_global_scope_pending_adds
d8307d
+      = args.original_global_scope_pending_adds;
d8307d
+
d8307d
   /* See if an error occurred during loading.  */
d8307d
   if (__glibc_unlikely (exception.errstring != NULL))
d8307d
     {
d8307d
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
d8307d
index 6c5298a80bff8e96..57fbefea3cb841e9 100644
d8307d
--- a/sysdeps/generic/ldsodefs.h
d8307d
+++ b/sysdeps/generic/ldsodefs.h
d8307d
@@ -311,7 +311,14 @@ struct rtld_global
d8307d
     /* This is zero at program start to signal that the global scope map is
d8307d
        allocated by rtld.  Later it keeps the size of the map.  It might be
d8307d
        reset if in _dl_close if the last global object is removed.  */
d8307d
-    size_t _ns_global_scope_alloc;
d8307d
+    unsigned int _ns_global_scope_alloc;
d8307d
+
d8307d
+    /* During dlopen, this is the number of objects that still need to
d8307d
+       be added to the global scope map.  It has to be taken into
d8307d
+       account when resizing the map, for future map additions after
d8307d
+       recursive dlopen calls from ELF constructors.  */
d8307d
+    unsigned int _ns_global_scope_pending_adds;
d8307d
+
d8307d
     /* Search table for unique objects.  */
d8307d
     struct unique_sym_table
d8307d
     {