Message ID | b4926cb8-cef2-ef29-8407-d3fc3dc225a5@davidnewall.com |
---|---|
State | New |
Headers | show |
Series | ld.so: command argument "--preload" | expand |
* David Newall:
> +${test_wrapper} $rtld --library-path $library_path --preload $preload $test_program 2>&1 && rc=0 || rc=$?
I would suggest to use "" around shell variables where applicable, and
wrap this long line (using \).
There are also a few stylistic issues we need to fix: Two whitespace
problems (”. */” at end of comments, space before parenthesis in
function call, not after it). The copyright statement for the new file
needs a bit polishing.
But the change as such looks okay to me. We can commit this for your
after 2.30 development opens in Feburary. I'll supply a new NEWS entry
and a ChangeLog entry, too.
Thanks,
Florian
* David Newall: > Thanks, Florian and Carlos, for your guidance. This is my patch, > relative to master source, as pulled two days ago. Thanks, this is what I've pushed. Florian elf: Implement --preload option for the dynamic linker 2019-02-04 David Newall <glibc@davidnewall.com> elf: Implement --preload option for the dynamic linker. * elf/rtld.c (preloadarg): New variable. (handle_preload_list): Pass through “where” argument to do_preload. (dl_main): Handle "--preload" and add second call to handle_preload_list. * elf/Makefile (tests-special): Add tst-rtld-preload.out. (tst-rtld-preload-OBJS): Set variable. (tst-rtld-preload.out): New target. * elf/tst-rtld-preload.sh: New file. diff --git a/NEWS b/NEWS index 5cf568aed9..5d1de1f2f4 100644 --- a/NEWS +++ b/NEWS @@ -9,7 +9,8 @@ Version 2.30 Major new features: - [Add new features here] +* The dynamic linker accepts the --preload argument to preload shared + objects, in addition to the LD_PRELOAD environment variable. Deprecated and removed features, and other changes affecting compatibility: diff --git a/elf/Makefile b/elf/Makefile index 9cf5cd8dfd..db6a2a0c29 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -354,7 +354,8 @@ endif ifeq (yes,$(build-shared)) ifeq ($(run-built-tests),yes) -tests-special += $(objpfx)tst-pathopt.out $(objpfx)tst-rtld-load-self.out +tests-special += $(objpfx)tst-pathopt.out $(objpfx)tst-rtld-load-self.out \ + $(objpfx)tst-rtld-preload.out endif tests-special += $(objpfx)check-textrel.out $(objpfx)check-execstack.out \ $(objpfx)check-localplt.out $(objpfx)check-initfini.out @@ -883,6 +884,15 @@ $(objpfx)tst-rtld-load-self.out: tst-rtld-load-self.sh $(objpfx)ld.so $(SHELL) $^ '$(test-wrapper)' '$(test-wrapper-env)' > $@; \ $(evaluate-test) +tst-rtld-preload-OBJS = $(subst $(empty) ,:,$(strip $(preloadtest-preloads:=.so))) +$(objpfx)tst-rtld-preload.out: tst-rtld-preload.sh $(objpfx)ld.so \ + $(objpfx)preloadtest \ + $(preloadtest-preloads:%=$(objpfx)%.so) + $(SHELL) $< $(objpfx)ld.so $(objpfx)preloadtest \ + '$(test-wrapper)' '$(test-wrapper-env)' '$(run_program_env)' \ + '$(rpath-link)' '$(tst-rtld-preload-OBJS)' > $@; \ + $(evaluate-test) + $(objpfx)initfirst: $(libdl) $(objpfx)initfirst.out: $(objpfx)firstobj.so diff --git a/elf/rtld.c b/elf/rtld.c index 5d97f41b7b..5a90e78ed6 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -826,15 +826,18 @@ static const char *library_path attribute_relro; static const char *preloadlist attribute_relro; /* Nonzero if information about versions has to be printed. */ static int version_info attribute_relro; +/* The preload list passed as a command argument. */ +static const char *preloadarg attribute_relro; /* The LD_PRELOAD environment variable gives list of libraries separated by white space or colons that are loaded before the executable's dependencies and prepended to the global scope list. (If the binary is running setuid all elements containing a '/' are ignored since it is insecure.) Return the number of preloads - performed. */ + performed. Ditto for --preload command argument. */ unsigned int -handle_ld_preload (const char *preloadlist, struct link_map *main_map) +handle_preload_list (const char *preloadlist, struct link_map *main_map, + const char *where) { unsigned int npreloads = 0; const char *p = preloadlist; @@ -858,7 +861,7 @@ handle_ld_preload (const char *preloadlist, struct link_map *main_map) ++p; if (dso_name_valid_for_suid (fname)) - npreloads += do_preload (fname, main_map, "LD_PRELOAD"); + npreloads += do_preload (fname, main_map, where); } return npreloads; } @@ -974,6 +977,13 @@ dl_main (const ElfW(Phdr) *phdr, { process_dl_audit (_dl_argv[2]); + _dl_skip_args += 2; + _dl_argc -= 2; + _dl_argv += 2; + } + else if (! strcmp (_dl_argv[1], "--preload") && _dl_argc > 2) + { + preloadarg = _dl_argv[2]; _dl_skip_args += 2; _dl_argc -= 2; _dl_argv += 2; @@ -1006,7 +1016,8 @@ of this helper program; chances are you did not intend to run this program.\n\ variable LD_LIBRARY_PATH\n\ --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\ in LIST\n\ - --audit LIST use objects named in LIST as auditors\n"); + --audit LIST use objects named in LIST as auditors\n\ + --preload LIST preload objects named in LIST\n"); ++_dl_skip_args; --_dl_argc; @@ -1620,7 +1631,16 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n", if (__glibc_unlikely (preloadlist != NULL)) { HP_TIMING_NOW (start); - npreloads += handle_ld_preload (preloadlist, main_map); + npreloads += handle_preload_list (preloadlist, main_map, "LD_PRELOAD"); + HP_TIMING_NOW (stop); + HP_TIMING_DIFF (diff, start, stop); + HP_TIMING_ACCUM_NT (load_time, diff); + } + + if (__glibc_unlikely (preloadarg != NULL)) + { + HP_TIMING_NOW (start); + npreloads += handle_preload_list (preloadarg, main_map, "--preload"); HP_TIMING_NOW (stop); HP_TIMING_DIFF (diff, start, stop); HP_TIMING_ACCUM_NT (load_time, diff); diff --git a/elf/tst-rtld-preload.sh b/elf/tst-rtld-preload.sh new file mode 100755 index 0000000000..f0c0ca11ba --- /dev/null +++ b/elf/tst-rtld-preload.sh @@ -0,0 +1,38 @@ +#!/bin/sh +# Test --preload argument ld.so. +# Copyright (C) 2019 Free Software Foundation, Inc. +# This file is part of the GNU C Library. +# +# The GNU C Library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# The GNU C Library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with the GNU C Library; if not, see +# <http://www.gnu.org/licenses/>. + +set -e + +rtld=$1 +test_program=$2 +test_wrapper=$3 +test_wrapper_env=$4 +run_program_env=$5 +library_path=$6 +preload=$7 + +echo "# [${test_wrapper}] [$rtld] [--library-path] [$library_path]" \ + "[--preload] [$preload] [$test_program]" +${test_wrapper_env} \ +${run_program_env} \ +${test_wrapper} $rtld --library-path "$library_path" \ + --preload "$preload" $test_program 2>&1 && rc=0 || rc=$? +echo "# exit status $rc" + +exit $rc
I've added documentation for the --preload option to the ld.so(8) manual page. Please let me know if I missed anything in the text below. --preload list (since glibc 2.30) Preload the objects specified in list. The objects in list are delimited by colons or spaces. The objects are pre‐ loaded as explained in the description of the LD_PRELOAD environment variable below. By contrast with LD_PRELOAD, the --preload option provides a way to perform preloading for a single executable without affecting preloading performed in any child process that executes a new program. ... LD_PRELOAD ... There are various methods of specifying libraries to be preloaded, and these are handled in the following order: (1) The LD_PRELOAD environment variable. (2) The --preload command-line option when invoking the dynamic linker directly. (3) The /etc/ld.so.preload file (described below). Thanks, Michael diff --git a/man8/ld.so.8 b/man8/ld.so.8 index 6271f23ed..cdc4f233d 100644 --- a/man8/ld.so.8 +++ b/man8/ld.so.8 @@ -192,6 +192,24 @@ are delimited by colons or spaces. .B \-\-list List all dependencies and how they are resolved. .TP +.BR \-\-preload " \fIlist\fP (since glibc 2.30)" +Preload the objects specified in +.IR list . +The objects in +.I list +are delimited by colons or spaces. +The objects are preloaded as explained in the description of the +.BR LD_PRELOAD +environment variable below. +.IP +By contrast with +.BR LD_PRELOAD , +the +.BR \-\-preload +option provides a way to perform preloading for a single executable +without affecting preloading performed in any child process that executes +a new program. +.TP .B \-\-verify Verify that program is dynamically linked and this dynamic linker can handle it. @@ -347,6 +365,23 @@ as described above in .\" .\" which will preload the libmod.so in 'lib' or 'lib64', using it .\" in preference to the version in '.'. +.IP +There are various methods of specifying libraries to be preloaded, +and these are handled in the following order: +.RS +.IP (1) 4 +The +.BR LD_PRELOAD +environment variable. +.IP (2) +The +.B \-\-preload +command-line option when invoking the dynamic linker directly. +.IP (3) +The +.I /etc/ld.so.preload +file (described below). +.RE .TP .BR LD_TRACE_LOADED_OBJECTS If set (to any value), causes the program to list its dynamic
* Michael Kerrisk: > I've added documentation for the --preload option to the ld.so(8) > manual page. Please let me know if I missed anything in the text > below. Thanks, looks good to me. Florian
On Tue, 26 Feb 2019 at 13:01, Florian Weimer <fweimer@redhat.com> wrote: > > * Michael Kerrisk: > > > I've added documentation for the --preload option to the ld.so(8) > > manual page. Please let me know if I missed anything in the text > > below. > > Thanks, looks good to me. Thanks for checking it over, Florian! Cheers, Michael
On 26/2/19 10:17 pm, Michael Kerrisk wrote: > I've added documentation for the --preload option to the ld.so(8) > manual page. Please let me know if I missed anything in the text > below. Perfect.
On 2/27/19 2:18 AM, David Newall wrote: > On 26/2/19 10:17 pm, Michael Kerrisk wrote: >> I've added documentation for the --preload option to the ld.so(8) >> manual page. Please let me know if I missed anything in the text >> below. > > Perfect. Thanks for checking it over, David. Cheers, Michael
diff --git a/elf/Makefile b/elf/Makefile index d72e7b6..bc891b9 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -353,7 +353,8 @@ endif ifeq (yes,$(build-shared)) ifeq ($(run-built-tests),yes) -tests-special += $(objpfx)tst-pathopt.out $(objpfx)tst-rtld-load-self.out +tests-special += $(objpfx)tst-pathopt.out $(objpfx)tst-rtld-load-self.out \ + $(objpfx)tst-rtld-preload.out endif tests-special += $(objpfx)check-textrel.out $(objpfx)check-execstack.out \ $(objpfx)check-localplt.out $(objpfx)check-initfini.out @@ -882,6 +883,15 @@ $(objpfx)tst-rtld-load-self.out: tst-rtld-load-self.sh $(objpfx)ld.so $(SHELL) $^ '$(test-wrapper)' '$(test-wrapper-env)' > $@; \ $(evaluate-test) +tst-rtld-preload-OBJS = $(subst $(empty) ,:,$(strip $(preloadtest-preloads:=.so))) +$(objpfx)tst-rtld-preload.out: tst-rtld-preload.sh $(objpfx)ld.so \ + $(objpfx)preloadtest \ + $(preloadtest-preloads:%=$(objpfx)%.so) + $(SHELL) $< $(objpfx)ld.so $(objpfx)preloadtest \ + '$(test-wrapper)' '$(test-wrapper-env)' '$(run_program_env)' \ + '$(rpath-link)' '$(tst-rtld-preload-OBJS)' > $@; \ + $(evaluate-test) + $(objpfx)initfirst: $(libdl) $(objpfx)initfirst.out: $(objpfx)firstobj.so diff --git a/elf/rtld.c b/elf/rtld.c index 1b0c747..11ec55b 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -826,15 +826,18 @@ static const char *library_path attribute_relro; static const char *preloadlist attribute_relro; /* Nonzero if information about versions has to be printed. */ static int version_info attribute_relro; +/* The preload list passed as a command argument. */ +static const char *preloadarg attribute_relro; /* The LD_PRELOAD environment variable gives list of libraries separated by white space or colons that are loaded before the executable's dependencies and prepended to the global scope list. (If the binary is running setuid all elements containing a '/' are ignored since it is insecure.) Return the number of preloads - performed. */ + performed. Ditto for --preload command argument. */ unsigned int -handle_ld_preload (const char *preloadlist, struct link_map *main_map) +handle_preload_list (const char *preloadlist, struct link_map *main_map, + const char *where) { unsigned int npreloads = 0; const char *p = preloadlist; @@ -858,7 +861,7 @@ handle_ld_preload (const char *preloadlist, struct link_map *main_map) ++p; if (dso_name_valid_for_suid (fname)) - npreloads += do_preload (fname, main_map, "LD_PRELOAD"); + npreloads += do_preload (fname, main_map, where); } return npreloads; } @@ -978,6 +981,13 @@ dl_main (const ElfW(Phdr) *phdr, _dl_argc -= 2; _dl_argv += 2; } + else if (! strcmp(_dl_argv[1], "--preload") && _dl_argc > 2) + { + preloadarg = _dl_argv[2]; + _dl_skip_args += 2; + _dl_argc -= 2; + _dl_argv += 2; + } else break; @@ -1006,7 +1016,8 @@ of this helper program; chances are you did not intend to run this program.\n\ variable LD_LIBRARY_PATH\n\ --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\ in LIST\n\ - --audit LIST use objects named in LIST as auditors\n"); + --audit LIST use objects named in LIST as auditors\n\ + --preload LIST preload objects named in LIST\n"); ++_dl_skip_args; --_dl_argc; @@ -1620,7 +1631,16 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n", if (__glibc_unlikely (preloadlist != NULL)) { HP_TIMING_NOW (start); - npreloads += handle_ld_preload (preloadlist, main_map); + npreloads += handle_preload_list (preloadlist, main_map, "LD_PRELOAD"); + HP_TIMING_NOW (stop); + HP_TIMING_DIFF (diff, start, stop); + HP_TIMING_ACCUM_NT (load_time, diff); + } + + if (__glibc_unlikely (preloadarg != NULL)) + { + HP_TIMING_NOW (start); + npreloads += handle_preload_list (preloadarg, main_map, "--preload"); HP_TIMING_NOW (stop); HP_TIMING_DIFF (diff, start, stop); HP_TIMING_ACCUM_NT (load_time, diff); diff --git a/elf/tst-rtld-preload.sh b/elf/tst-rtld-preload.sh new file mode 100755 index 0000000..4a6a58d --- /dev/null +++ b/elf/tst-rtld-preload.sh @@ -0,0 +1,37 @@ +#!/bin/sh +# Test how rtld loads itself. +# Copyright (C) 2012-2016 Free Software Foundation, Inc. +# This file is part of the GNU C Library. +# + +# The GNU C Library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. + +# The GNU C Library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. + +# You should have received a copy of the GNU Lesser General Public +# License along with the GNU C Library; if not, see +# <http://www.gnu.org/licenses/>. + +set -e + +rtld=$1 +test_program=$2 +test_wrapper=$3 +test_wrapper_env=$4 +run_program_env=$5 +library_path=$6 +preload=$7 + +echo "# [${test_wrapper}] [$rtld] [--library-path] [$library_path] [--preload] [$preload] [$test_program]" +${test_wrapper_env} \ +${run_program_env} \ +${test_wrapper} $rtld --library-path $library_path --preload $preload $test_program 2>&1 && rc=0 || rc=$? +echo "# exit status $rc" + +exit $rc