5f7b84
commit 8692ebdb1259be60c545fa509d4852b26703777e
5f7b84
Author: David Newall <glibc@davidnewall.com>
5f7b84
Date:   Mon Feb 4 13:35:11 2019 +0100
5f7b84
5f7b84
    elf: Implement --preload option for the dynamic linker
5f7b84
5f7b84
diff --git a/elf/Makefile b/elf/Makefile
5f7b84
index 9cf5cd8dfd..db6a2a0c29 100644
5f7b84
--- a/elf/Makefile
5f7b84
+++ b/elf/Makefile
5f7b84
@@ -354,7 +354,8 @@ endif
5f7b84
 
5f7b84
 ifeq (yes,$(build-shared))
5f7b84
 ifeq ($(run-built-tests),yes)
5f7b84
-tests-special += $(objpfx)tst-pathopt.out $(objpfx)tst-rtld-load-self.out
5f7b84
+tests-special += $(objpfx)tst-pathopt.out $(objpfx)tst-rtld-load-self.out \
5f7b84
+		 $(objpfx)tst-rtld-preload.out
5f7b84
 endif
5f7b84
 tests-special += $(objpfx)check-textrel.out $(objpfx)check-execstack.out \
5f7b84
 		 $(objpfx)check-localplt.out $(objpfx)check-initfini.out
5f7b84
@@ -883,6 +884,15 @@ $(objpfx)tst-rtld-load-self.out: tst-rtld-load-self.sh $(objpfx)ld.so
5f7b84
 	$(SHELL) $^ '$(test-wrapper)' '$(test-wrapper-env)' > $@; \
5f7b84
 	$(evaluate-test)
5f7b84
 
5f7b84
+tst-rtld-preload-OBJS = $(subst $(empty) ,:,$(strip $(preloadtest-preloads:=.so)))
5f7b84
+$(objpfx)tst-rtld-preload.out: tst-rtld-preload.sh $(objpfx)ld.so \
5f7b84
+			       $(objpfx)preloadtest \
5f7b84
+			       $(preloadtest-preloads:%=$(objpfx)%.so)
5f7b84
+	$(SHELL) $< $(objpfx)ld.so $(objpfx)preloadtest \
5f7b84
+		    '$(test-wrapper)' '$(test-wrapper-env)' '$(run_program_env)' \
5f7b84
+		    '$(rpath-link)' '$(tst-rtld-preload-OBJS)' > $@; \
5f7b84
+	$(evaluate-test)
5f7b84
+
5f7b84
 $(objpfx)initfirst: $(libdl)
5f7b84
 $(objpfx)initfirst.out: $(objpfx)firstobj.so
5f7b84
 
5f7b84
diff --git a/elf/rtld.c b/elf/rtld.c
5f7b84
index 5d97f41b7b..5a90e78ed6 100644
5f7b84
--- a/elf/rtld.c
5f7b84
+++ b/elf/rtld.c
5f7b84
@@ -826,15 +826,18 @@ static const char *library_path attribute_relro;
5f7b84
 static const char *preloadlist attribute_relro;
5f7b84
 /* Nonzero if information about versions has to be printed.  */
5f7b84
 static int version_info attribute_relro;
5f7b84
+/* The preload list passed as a command argument.  */
5f7b84
+static const char *preloadarg attribute_relro;
5f7b84
 
5f7b84
 /* The LD_PRELOAD environment variable gives list of libraries
5f7b84
    separated by white space or colons that are loaded before the
5f7b84
    executable's dependencies and prepended to the global scope list.
5f7b84
    (If the binary is running setuid all elements containing a '/' are
5f7b84
    ignored since it is insecure.)  Return the number of preloads
5f7b84
-   performed.  */
5f7b84
+   performed.   Ditto for --preload command argument.  */
5f7b84
 unsigned int
5f7b84
-handle_ld_preload (const char *preloadlist, struct link_map *main_map)
5f7b84
+handle_preload_list (const char *preloadlist, struct link_map *main_map,
5f7b84
+		     const char *where)
5f7b84
 {
5f7b84
   unsigned int npreloads = 0;
5f7b84
   const char *p = preloadlist;
5f7b84
@@ -858,7 +861,7 @@ handle_ld_preload (const char *preloadlist, struct link_map *main_map)
5f7b84
 	++p;
5f7b84
 
5f7b84
       if (dso_name_valid_for_suid (fname))
5f7b84
-	npreloads += do_preload (fname, main_map, "LD_PRELOAD");
5f7b84
+	npreloads += do_preload (fname, main_map, where);
5f7b84
     }
5f7b84
   return npreloads;
5f7b84
 }
5f7b84
@@ -974,6 +977,13 @@ dl_main (const ElfW(Phdr) *phdr,
5f7b84
 	  {
5f7b84
 	    process_dl_audit (_dl_argv[2]);
5f7b84
 
5f7b84
+	    _dl_skip_args += 2;
5f7b84
+	    _dl_argc -= 2;
5f7b84
+	    _dl_argv += 2;
5f7b84
+	  }
5f7b84
+	else if (! strcmp (_dl_argv[1], "--preload") && _dl_argc > 2)
5f7b84
+	  {
5f7b84
+	    preloadarg = _dl_argv[2];
5f7b84
 	    _dl_skip_args += 2;
5f7b84
 	    _dl_argc -= 2;
5f7b84
 	    _dl_argv += 2;
5f7b84
@@ -1006,7 +1016,8 @@ of this helper program; chances are you did not intend to run this program.\n\
5f7b84
 			variable LD_LIBRARY_PATH\n\
5f7b84
   --inhibit-rpath LIST  ignore RUNPATH and RPATH information in object names\n\
5f7b84
 			in LIST\n\
5f7b84
-  --audit LIST          use objects named in LIST as auditors\n");
5f7b84
+  --audit LIST          use objects named in LIST as auditors\n\
5f7b84
+  --preload LIST        preload objects named in LIST\n");
5f7b84
 
5f7b84
       ++_dl_skip_args;
5f7b84
       --_dl_argc;
5f7b84
@@ -1620,7 +1631,16 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
5f7b84
   if (__glibc_unlikely (preloadlist != NULL))
5f7b84
     {
5f7b84
       HP_TIMING_NOW (start);
5f7b84
-      npreloads += handle_ld_preload (preloadlist, main_map);
5f7b84
+      npreloads += handle_preload_list (preloadlist, main_map, "LD_PRELOAD");
5f7b84
+      HP_TIMING_NOW (stop);
5f7b84
+      HP_TIMING_DIFF (diff, start, stop);
5f7b84
+      HP_TIMING_ACCUM_NT (load_time, diff);
5f7b84
+    }
5f7b84
+
5f7b84
+  if (__glibc_unlikely (preloadarg != NULL))
5f7b84
+    {
5f7b84
+      HP_TIMING_NOW (start);
5f7b84
+      npreloads += handle_preload_list (preloadarg, main_map, "--preload");
5f7b84
       HP_TIMING_NOW (stop);
5f7b84
       HP_TIMING_DIFF (diff, start, stop);
5f7b84
       HP_TIMING_ACCUM_NT (load_time, diff);
5f7b84
diff --git a/elf/tst-rtld-preload.sh b/elf/tst-rtld-preload.sh
5f7b84
new file mode 100755
5f7b84
index 0000000000..f0c0ca11ba
5f7b84
--- /dev/null
5f7b84
+++ b/elf/tst-rtld-preload.sh
5f7b84
@@ -0,0 +1,38 @@
5f7b84
+#!/bin/sh
5f7b84
+# Test --preload argument ld.so.
5f7b84
+# Copyright (C) 2019 Free Software Foundation, Inc.
5f7b84
+# This file is part of the GNU C Library.
5f7b84
+#
5f7b84
+# The GNU C Library is free software; you can redistribute it and/or
5f7b84
+# modify it under the terms of the GNU Lesser General Public
5f7b84
+# License as published by the Free Software Foundation; either
5f7b84
+# version 2.1 of the License, or (at your option) any later version.
5f7b84
+#
5f7b84
+# The GNU C Library is distributed in the hope that it will be useful,
5f7b84
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
5f7b84
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5f7b84
+# Lesser General Public License for more details.
5f7b84
+#
5f7b84
+# You should have received a copy of the GNU Lesser General Public
5f7b84
+# License along with the GNU C Library; if not, see
5f7b84
+# <http://www.gnu.org/licenses/>.
5f7b84
+
5f7b84
+set -e
5f7b84
+
5f7b84
+rtld=$1
5f7b84
+test_program=$2
5f7b84
+test_wrapper=$3
5f7b84
+test_wrapper_env=$4
5f7b84
+run_program_env=$5
5f7b84
+library_path=$6
5f7b84
+preload=$7
5f7b84
+
5f7b84
+echo "# [${test_wrapper}] [$rtld] [--library-path] [$library_path]" \
5f7b84
+     "[--preload] [$preload] [$test_program]"
5f7b84
+${test_wrapper_env} \
5f7b84
+${run_program_env} \
5f7b84
+${test_wrapper} $rtld --library-path "$library_path" \
5f7b84
+  --preload "$preload" $test_program 2>&1 && rc=0 || rc=$?
5f7b84
+echo "# exit status $rc"
5f7b84
+
5f7b84
+exit $rc