about summary refs log tree commit diff
path: root/elf
diff options
context:
space:
mode:
Diffstat (limited to 'elf')
-rw-r--r--elf/dl-error.c9
-rw-r--r--elf/dl-load.c7
-rw-r--r--elf/dlerror.c2
-rw-r--r--elf/link.h5
-rw-r--r--elf/rtld.c115
5 files changed, 110 insertions, 28 deletions
diff --git a/elf/dl-error.c b/elf/dl-error.c
index 2eaa7e03d1..40ded4f1d2 100644
--- a/elf/dl-error.c
+++ b/elf/dl-error.c
@@ -20,13 +20,15 @@ Cambridge, MA 02139, USA.  */
 #include <stddef.h>
 #include <link.h>
 #include <setjmp.h>
+#include <stdlib.h>
 #include <string.h>
 
 /* This structure communicates state between _dl_catch_error and
    _dl_signal_error.  */
 struct catch
   {
-    const char *errstring, *objname; /* Error detail filled in here.  */
+    char *errstring;		/* Error detail filled in here.  */
+    const char *objname;
     jmp_buf env;		/* longjmp here on error.  */
   };
 
@@ -69,7 +71,7 @@ _dl_signal_error (int errcode,
 }
 
 int
-_dl_catch_error (const char **errstring,
+_dl_catch_error (char **errstring,
 		 const char **objname,
 		 void (*operate) (void))
 {
@@ -82,7 +84,8 @@ _dl_catch_error (const char **errstring,
       catch = &c;
       (*operate) ();
       catch = NULL;
-      *errstring = *objname = NULL;
+      *errstring = NULL;
+      *objname = NULL;
       return 0;
     }
 
diff --git a/elf/dl-load.c b/elf/dl-load.c
index 6fd6a6cbe0..b56303fa4a 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -481,6 +481,7 @@ _dl_map_object (struct link_map *loader, const char *name, int type)
 {
   int fd;
   char *realname;
+  char *name_copy;
   struct link_map *l;
 
   /* Look for this name among those already loaded.  */
@@ -572,8 +573,8 @@ _dl_map_object (struct link_map *loader, const char *name, int type)
 
   if (fd != -1)
     {
-      name = local_strdup (name);
-      if (name == NULL)
+      name_copy = local_strdup (name);
+      if (name_copy == NULL)
 	{
 	  __close (fd);
 	  fd = -1;
@@ -583,5 +584,5 @@ _dl_map_object (struct link_map *loader, const char *name, int type)
   if (fd == -1)
     _dl_signal_error (errno, name, "cannot open shared object file");
 
-  return _dl_map_object_from_fd (name, fd, realname, loader, type);
+  return _dl_map_object_from_fd (name_copy, fd, realname, loader, type);
 }
diff --git a/elf/dlerror.c b/elf/dlerror.c
index 663207d708..9b78e47a57 100644
--- a/elf/dlerror.c
+++ b/elf/dlerror.c
@@ -24,7 +24,7 @@ Cambridge, MA 02139, USA.  */
 #include <stdlib.h>
 
 static int last_errcode;
-static const char *last_errstring;
+static char *last_errstring;
 static const char *last_object_name;
 
 char *
diff --git a/elf/link.h b/elf/link.h
index a9637000e5..a6281726c3 100644
--- a/elf/link.h
+++ b/elf/link.h
@@ -178,8 +178,9 @@ extern void _dl_signal_error (int errcode,
 /* Call OPERATE, catching errors from `dl_signal_error'.  If there is no
    error, *ERRSTRING is set to null.  If there is an error, *ERRSTRING and
    *OBJECT are set to the strings passed to _dl_signal_error, and the error
-   code passed is the return value.  */
-extern int _dl_catch_error (const char **errstring,
+   code passed is the return value.  ERRSTRING if nonzero points to a
+   malloc'ed string which the caller has to free after use.  */
+extern int _dl_catch_error (char **errstring,
 			    const char **object,
 			    void (*operate) (void));
 
diff --git a/elf/rtld.c b/elf/rtld.c
index 8b754920f8..0736218536 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -38,6 +38,12 @@ extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
 						     ElfW(Addr) *user_entry));
 extern void _dl_sysdep_start_cleanup (void);
 
+/* System-dependent function to read a file's whole contents
+   in the most convenient manner available.  */
+extern void *_dl_sysdep_read_whole_file (const char *filename,
+					 size_t *filesize_ptr,
+					 int mmap_prot);
+
 int _dl_argc;
 char **_dl_argv;
 const char *_dl_rpath;
@@ -138,6 +144,8 @@ dl_main (const ElfW(Phdr) *phdr,
   enum { normal, list, verify, trace } mode;
   struct link_map **preloads;
   unsigned int npreloads;
+  size_t file_size;
+  char *file;
 
   mode = getenv ("LD_TRACE_LOADED_OBJECTS") != NULL ? trace : normal;
 
@@ -296,8 +304,77 @@ of this helper program; chances are you did not intend to run this program.\n",
   l->l_next = &_dl_rtld_map;
   _dl_rtld_map.l_prev = l;
 
+  /* We have two ways to specify objects to preload: via environment
+     variable and via the file /etc/ld.so.preload.  The later can also
+     be used when security is enabled.  */
   preloads = NULL;
   npreloads = 0;
+
+  /* Read the contents of the file.  */
+  file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size,
+				     PROT_READ | PROT_WRITE);
+  if (file)
+    {
+      /* Parse the file.  It contains names of libraries to be loaded,
+	 separated by white spaces or `:'.  It may also contain
+	 comments introduced by `#'.  */
+      char *problem;
+      char *runp;
+      size_t rest;
+
+      /* Eliminate comments.  */
+      runp = file;
+      rest = file_size;
+      while (rest > 0)
+	{
+	  char *comment = memchr (runp, '#', rest);
+	  if (comment == NULL)
+	    break;
+
+	  rest -= comment - runp;
+	  do
+	    *comment = ' ';
+	  while (--rest > 0 && *++comment != '\n');
+	}
+
+      /* We have one problematic case: if we have a name at the end of
+	 the file without a trailing terminating characters, we cannot
+	 place the \0.  Handle the case separately.  */
+      if (file[file_size - 1] != ' ' && file[file_size] != '\t'
+	  && file[file_size] != '\n')
+	{
+	  problem = &file[file_size];
+	  while (problem > file && problem[-1] != ' ' && problem[-1] != '\t'
+		 && problem[-1] != '\n')
+	    --problem;
+
+	  if (problem > file)
+	    problem[-1] = '\0';
+	}
+      else
+	problem = NULL;
+
+      if (file != problem)
+	{
+	  char *p;
+	  runp = file;
+	  while ((p = strsep (&runp, ": \t\n")) != NULL)
+	    {
+	      (void) _dl_map_object (NULL, p, lt_library);
+	      ++npreloads;
+	    }
+	}
+
+      if (problem != NULL)
+	{
+	  char *p = strndupa (problem, file_size - (problem - file));
+	  (void) _dl_map_object (NULL, p, lt_library);
+	}
+
+      /* We don't need the file anymore.  */
+      __munmap (file, file_size);
+    }
+
   if (! __libc_enable_secure)
     {
       const char *preloadlist = getenv ("LD_PRELOAD");
@@ -313,25 +390,25 @@ of this helper program; chances are you did not intend to run this program.\n",
 	      (void) _dl_map_object (NULL, p, lt_library);
 	      ++npreloads;
 	    }
-
-	  if (npreloads != 0)
-	    {
-	      /* Set up PRELOADS with a vector of the preloaded libraries.  */
-	      struct link_map *l;
-	      unsigned int i;
-	      preloads = __alloca (npreloads * sizeof preloads[0]);
-	      l = _dl_rtld_map.l_next; /* End of the chain before preloads.  */
-	      i = 0;
-	      do
-		{
-		  preloads[i++] = l;
-		  l = l->l_next;
-		} while (l);
-	      assert (i == npreloads);
-	    }
 	}
     }
 
+  if (npreloads != 0)
+    {
+      /* Set up PRELOADS with a vector of the preloaded libraries.  */
+      struct link_map *l;
+      unsigned int i;
+      preloads = __alloca (npreloads * sizeof preloads[0]);
+      l = _dl_rtld_map.l_next; /* End of the chain before preloads.  */
+      i = 0;
+      do
+	{
+	  preloads[i++] = l;
+	  l = l->l_next;
+	} while (l);
+      assert (i == npreloads);
+    }
+
   /* Load all the libraries specified by DT_NEEDED entries.  If LD_PRELOAD
      specified some libraries to load, these are inserted before the actual
      dependencies in the executable's searchlist for symbol resolution.  */
@@ -386,7 +463,7 @@ of this helper program; chances are you did not intend to run this program.\n",
 	    char buf[20], *bp;
 	    buf[sizeof buf - 1] = '\0';
 	    bp = _itoa (l->l_addr, &buf[sizeof buf - 1], 16, 0);
-	    while (&buf[sizeof buf - 1] - bp < sizeof l->l_addr * 2)
+	    while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof l->l_addr * 2)
 	      *--bp = '0';
 	    _dl_sysdep_message ("\t", l->l_libname, " => ", l->l_name,
 				" (0x", bp, ")\n", NULL);
@@ -403,12 +480,12 @@ of this helper program; chances are you did not intend to run this program.\n",
 	    char buf[20], *bp;
 	    buf[sizeof buf - 1] = '\0';
 	    bp = _itoa (ref->st_value, &buf[sizeof buf - 1], 16, 0);
-	    while (&buf[sizeof buf - 1] - bp < sizeof loadbase * 2)
+	    while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
 	      *--bp = '0';
 	    _dl_sysdep_message (_dl_argv[i], " found at 0x", bp, NULL);
 	    buf[sizeof buf - 1] = '\0';
 	    bp = _itoa (loadbase, &buf[sizeof buf - 1], 16, 0);
-	    while (&buf[sizeof buf - 1] - bp < sizeof loadbase * 2)
+	    while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
 	      *--bp = '0';
 	    _dl_sysdep_message (" in object at 0x", bp, "\n", NULL);
 	  }