9832fd
Partial backport of:
9832fd
9832fd
commit a42faf59d6d9f82e5293a9ebcc26d9c9e562b12b
9832fd
Author: Paul Pluzhnikov <ppluzhnikov@google.com>
9832fd
Date:   Mon Mar 24 10:58:26 2014 -0700
9832fd
9832fd
    Fix BZ #16634.
9832fd
    
9832fd
    An application that erroneously tries to repeatedly dlopen("a.out", ...)
9832fd
    may hit assertion failure:
9832fd
    
9832fd
      Inconsistency detected by ld.so: dl-tls.c: 474: _dl_allocate_tls_init:
9832fd
      Assertion `listp != ((void *)0)' failed!
9832fd
    
9832fd
    dlopen() actually fails with  "./a.out: cannot dynamically load executable",
9832fd
    but it does so after incrementing dl_tls_max_dtv_idx.
9832fd
    
9832fd
    Once we run out of TLS_SLOTINFO_SURPLUS (62), we exit with above assertion
9832fd
    failure.
9832fd
    
9832fd
    2014-03-24  Paul Pluzhnikov  <ppluzhnikov@google.com>
9832fd
    
9832fd
            [BZ #16634]
9832fd
    
9832fd
            * elf/dl-load.c (open_verify): Add mode parameter.
9832fd
            Error early when ET_EXEC and mode does not have __RTLD_OPENEXEC.
9832fd
            (open_path): Change from boolean 'secure' to complete flag 'mode'
9832fd
            (_dl_map_object): Adjust.
9832fd
            * elf/Makefile (tests): Add tst-dlopen-aout.
9832fd
            * elf/tst-dlopen-aout.c: New test.
9832fd
9832fd
Only the change to elf/dl-load.c is included here.  The upstream test
9832fd
does not work because it depends on --enable-hardcoded-path-in-tests
9832fd
(which is not available in this tree, despite being documented in the
9832fd
manual).
9832fd
9832fd
diff --git a/elf/dl-load.c b/elf/dl-load.c
9832fd
index 6a0005da502c8f37..0ba0712aa5201fa0 100644
9832fd
--- a/elf/dl-load.c
9832fd
+++ b/elf/dl-load.c
9832fd
@@ -1686,7 +1686,7 @@ print_search_path (struct r_search_path_elem **list,
9832fd
    user might want to know about this.  */
9832fd
 static int
9832fd
 open_verify (const char *name, struct filebuf *fbp, struct link_map *loader,
9832fd
-	     int whatcode, bool *found_other_class, bool free_name)
9832fd
+	     int whatcode, int mode, bool *found_other_class, bool free_name)
9832fd
 {
9832fd
   /* This is the expected ELF header.  */
9832fd
 #define ELF32_CLASS ELFCLASS32
9832fd
@@ -1863,6 +1863,17 @@ open_verify (const char *name, struct filebuf *fbp, struct link_map *loader,
9832fd
 	  errstring = N_("only ET_DYN and ET_EXEC can be loaded");
9832fd
 	  goto call_lose;
9832fd
 	}
9832fd
+      else if (__glibc_unlikely (ehdr->e_type == ET_EXEC
9832fd
+				 && (mode & __RTLD_OPENEXEC) == 0))
9832fd
+	{
9832fd
+	  /* BZ #16634. It is an error to dlopen ET_EXEC (unless
9832fd
+	     __RTLD_OPENEXEC is explicitly set).  We return error here
9832fd
+	     so that code in _dl_map_object_from_fd does not try to set
9832fd
+	     l_tls_modid for this module.  */
9832fd
+
9832fd
+	  errstring = N_("cannot dynamically load executable");
9832fd
+	  goto call_lose;
9832fd
+	}
9832fd
       else if (__builtin_expect (ehdr->e_phentsize, sizeof (ElfW(Phdr)))
9832fd
 	       != sizeof (ElfW(Phdr)))
9832fd
 	{
9832fd
@@ -1964,7 +1975,7 @@ open_verify (const char *name, struct filebuf *fbp, struct link_map *loader,
9832fd
    if MAY_FREE_DIRS is true.  */
9832fd
 
9832fd
 static int
9832fd
-open_path (const char *name, size_t namelen, int secure,
9832fd
+open_path (const char *name, size_t namelen, int mode,
9832fd
 	   struct r_search_path_struct *sps, char **realname,
9832fd
 	   struct filebuf *fbp, struct link_map *loader, int whatcode,
9832fd
 	   bool *found_other_class)
9832fd
@@ -2016,8 +2027,8 @@ open_path (const char *name, size_t namelen, int secure,
9832fd
 	  if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0))
9832fd
 	    _dl_debug_printf ("  trying file=%s\n", buf);
9832fd
 
9832fd
-	  fd = open_verify (buf, fbp, loader, whatcode, found_other_class,
9832fd
-			    false);
9832fd
+	  fd = open_verify (buf, fbp, loader, whatcode, mode,
9832fd
+			    found_other_class, false);
9832fd
 	  if (this_dir->status[cnt] == unknown)
9832fd
 	    {
9832fd
 	      if (fd != -1)
9832fd
@@ -2046,7 +2057,7 @@ open_path (const char *name, size_t namelen, int secure,
9832fd
 	  /* Remember whether we found any existing directory.  */
9832fd
 	  here_any |= this_dir->status[cnt] != nonexisting;
9832fd
 
9832fd
-	  if (fd != -1 && __builtin_expect (secure, 0)
9832fd
+	  if (fd != -1 && __builtin_expect (mode & __RTLD_SECURE, 0)
9832fd
 	      && INTUSE(__libc_enable_secure))
9832fd
 	    {
9832fd
 	      /* This is an extra security effort to make sure nobody can
9832fd
@@ -2236,7 +2247,7 @@ _dl_map_object (struct link_map *loader, const char *name,
9832fd
 	  for (l = loader; l; l = l->l_loader)
9832fd
 	    if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
9832fd
 	      {
9832fd
-		fd = open_path (name, namelen, mode & __RTLD_SECURE,
9832fd
+		fd = open_path (name, namelen, mode,
9832fd
 				&l->l_rpath_dirs,
9832fd
 				&realname, &fb, loader, LA_SER_RUNPATH,
9832fd
 				&found_other_class);
9832fd
@@ -2252,7 +2263,7 @@ _dl_map_object (struct link_map *loader, const char *name,
9832fd
 	      && main_map != NULL && main_map->l_type != lt_loaded
9832fd
 	      && cache_rpath (main_map, &main_map->l_rpath_dirs, DT_RPATH,
9832fd
 			      "RPATH"))
9832fd
-	    fd = open_path (name, namelen, mode & __RTLD_SECURE,
9832fd
+	    fd = open_path (name, namelen, mode,
9832fd
 			    &main_map->l_rpath_dirs,
9832fd
 			    &realname, &fb, loader ?: main_map, LA_SER_RUNPATH,
9832fd
 			    &found_other_class);
9832fd
@@ -2260,7 +2271,7 @@ _dl_map_object (struct link_map *loader, const char *name,
9832fd
 
9832fd
       /* Try the LD_LIBRARY_PATH environment variable.  */
9832fd
       if (fd == -1 && env_path_list.dirs != (void *) -1)
9832fd
-	fd = open_path (name, namelen, mode & __RTLD_SECURE, &env_path_list,
9832fd
+	fd = open_path (name, namelen, mode, &env_path_list,
9832fd
 			&realname, &fb,
9832fd
 			loader ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded,
9832fd
 			LA_SER_LIBPATH, &found_other_class);
9832fd
@@ -2269,7 +2280,7 @@ _dl_map_object (struct link_map *loader, const char *name,
9832fd
       if (fd == -1 && loader != NULL
9832fd
 	  && cache_rpath (loader, &loader->l_runpath_dirs,
9832fd
 			  DT_RUNPATH, "RUNPATH"))
9832fd
-	fd = open_path (name, namelen, mode & __RTLD_SECURE,
9832fd
+	fd = open_path (name, namelen, mode,
9832fd
 			&loader->l_runpath_dirs, &realname, &fb, loader,
9832fd
 			LA_SER_RUNPATH, &found_other_class);
9832fd
 
9832fd
@@ -2326,7 +2337,8 @@ _dl_map_object (struct link_map *loader, const char *name,
9832fd
 		{
9832fd
 		  fd = open_verify (cached,
9832fd
 				    &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded,
9832fd
-				    LA_SER_CONFIG, &found_other_class, false);
9832fd
+				    LA_SER_CONFIG, mode, &found_other_class,
9832fd
+				    false);
9832fd
 		  if (__builtin_expect (fd != -1, 1))
9832fd
 		    realname = cached;
9832fd
 		  else
9832fd
@@ -2341,7 +2353,7 @@ _dl_map_object (struct link_map *loader, const char *name,
9832fd
 	  && ((l = loader ?: GL(dl_ns)[nsid]._ns_loaded) == NULL
9832fd
 	      || __builtin_expect (!(l->l_flags_1 & DF_1_NODEFLIB), 1))
9832fd
 	  && rtld_search_dirs.dirs != (void *) -1)
9832fd
-	fd = open_path (name, namelen, mode & __RTLD_SECURE, &rtld_search_dirs,
9832fd
+	fd = open_path (name, namelen, mode, &rtld_search_dirs,
9832fd
 			&realname, &fb, l, LA_SER_DEFAULT, &found_other_class);
9832fd
 
9832fd
       /* Add another newline when we are tracing the library loading.  */
9832fd
@@ -2359,7 +2371,7 @@ _dl_map_object (struct link_map *loader, const char *name,
9832fd
       else
9832fd
 	{
9832fd
 	  fd = open_verify (realname, &fb,
9832fd
-			    loader ?: GL(dl_ns)[nsid]._ns_loaded, 0,
9832fd
+			    loader ?: GL(dl_ns)[nsid]._ns_loaded, 0, mode,
9832fd
 			    &found_other_class, true);
9832fd
 	  if (__builtin_expect (fd, 0) == -1)
9832fd
 	    free (realname);