[v2] elf: ld.so --help calls _dl_init_paths without a main map [BZ #27577]

Message ID 878s6ov8xy.fsf@oldenburg.str.redhat.com
State Committed
Commit 4e6db99c665d3b82a70a3e218860ef087b1555b4
Headers
Series [v2] elf: ld.so --help calls _dl_init_paths without a main map [BZ #27577] |

Commit Message

Florian Weimer March 15, 2021, 1:32 p.m. UTC
  In this case, use the link map of the dynamic loader itself as
a replacement.  This is more than just a hack: if we ever support
DT_RUNPATH/DT_RPATH for the dynamic loader, reporting it for
ld.so --help (without further command line arguments) would be the
right thing to do.

Fixes commit 332421312576bd7095e70589154af99b124dd2d1 ("elf: Always
set l in _dl_init_paths (bug 23462)").

---
v2: Switch to just one test for checking ld.so --help output.

 elf/Makefile  | 18 ++++++++++++++++--
 elf/dl-load.c |  8 +++++++-
 2 files changed, 23 insertions(+), 3 deletions(-)
  

Comments

H.J. Lu March 15, 2021, 1:40 p.m. UTC | #1
On Mon, Mar 15, 2021 at 6:33 AM Florian Weimer via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> In this case, use the link map of the dynamic loader itself as
> a replacement.  This is more than just a hack: if we ever support
> DT_RUNPATH/DT_RPATH for the dynamic loader, reporting it for
> ld.so --help (without further command line arguments) would be the
> right thing to do.
>
> Fixes commit 332421312576bd7095e70589154af99b124dd2d1 ("elf: Always
> set l in _dl_init_paths (bug 23462)").
>
> ---
> v2: Switch to just one test for checking ld.so --help output.
>
>  elf/Makefile  | 18 ++++++++++++++++--
>  elf/dl-load.c |  8 +++++++-
>  2 files changed, 23 insertions(+), 3 deletions(-)
>
> diff --git a/elf/Makefile b/elf/Makefile
> index 4c9e63dac9..ba4689a7fa 100644
> --- a/elf/Makefile
> +++ b/elf/Makefile
> @@ -245,7 +245,7 @@ tests += $(tests-execstack-$(have-z-execstack))
>  ifeq ($(run-built-tests),yes)
>  tests-special += $(objpfx)tst-leaks1-mem.out \
>                  $(objpfx)tst-leaks1-static-mem.out $(objpfx)noload-mem.out \
> -                $(objpfx)tst-ldconfig-X.out
> +                $(objpfx)tst-ldconfig-X.out $(objpfx)tst-rtld-help.out
>  endif
>  tlsmod17a-suffixes = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
>  tlsmod18a-suffixes = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
> @@ -433,7 +433,8 @@ endif
>  ifeq (yes,$(build-shared))
>  ifeq ($(run-built-tests),yes)
>  tests-special += $(objpfx)tst-pathopt.out $(objpfx)tst-rtld-load-self.out \
> -                $(objpfx)tst-rtld-preload.out $(objpfx)argv0test.out
> +                $(objpfx)tst-rtld-preload.out $(objpfx)argv0test.out \
> +                $(objpfx)tst-rtld-help.out
>  endif
>  tests-special += $(objpfx)check-textrel.out $(objpfx)check-execstack.out \
>                  $(objpfx)check-wx-segment.out \
> @@ -1907,3 +1908,16 @@ $(objpfx)list-tunables.out: tst-rtld-list-tunables.sh $(objpfx)ld.so
>         $(evaluate-test)
>
>  tst-dst-static-ENV = LD_LIBRARY_PATH='$$ORIGIN'
> +
> +$(objpfx)tst-rtld-help.out: $(objpfx)ld.so
> +       $(test-wrapper) $(rtld-prefix) --help > $@; \
> +       status=$$?; \
> +       echo "info: ld.so exit status: $$status" >> $@; \
> +       if ! grep -q 'Legacy HWCAP subdirectories under library search path directories' $@; then \
> +         echo "error: missing subdirectory pattern" >> $@; \
> +         if test $$status -eq 0; then \
> +           status=1; \
> +         fi; \
> +       fi; \
> +       (exit $$status); \
> +       $(evaluate-test)
> diff --git a/elf/dl-load.c b/elf/dl-load.c
> index 376a2e64d6..2f760503c5 100644
> --- a/elf/dl-load.c
> +++ b/elf/dl-load.c
> @@ -758,8 +758,14 @@ _dl_init_paths (const char *llp, const char *source,
>    max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
>    *aelem = NULL;
>
> -  /* This points to the map of the main object.  */
> +  /* This points to the map of the main object.  If there is no main
> +     object (e.g., under --help, use the dynamic loader itself as a
> +     stand-in.  */
>    l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
> +#ifdef SHARED
> +  if (l == NULL)
> +    l = &GL (dl_rtld_map);
> +#endif
>    assert (l->l_type != lt_loaded);
>
>    if (l->l_info[DT_RUNPATH])
>

LGTM.

Thanks.
  

Patch

diff --git a/elf/Makefile b/elf/Makefile
index 4c9e63dac9..ba4689a7fa 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -245,7 +245,7 @@  tests += $(tests-execstack-$(have-z-execstack))
 ifeq ($(run-built-tests),yes)
 tests-special += $(objpfx)tst-leaks1-mem.out \
 		 $(objpfx)tst-leaks1-static-mem.out $(objpfx)noload-mem.out \
-		 $(objpfx)tst-ldconfig-X.out
+		 $(objpfx)tst-ldconfig-X.out $(objpfx)tst-rtld-help.out
 endif
 tlsmod17a-suffixes = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 tlsmod18a-suffixes = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
@@ -433,7 +433,8 @@  endif
 ifeq (yes,$(build-shared))
 ifeq ($(run-built-tests),yes)
 tests-special += $(objpfx)tst-pathopt.out $(objpfx)tst-rtld-load-self.out \
-		 $(objpfx)tst-rtld-preload.out $(objpfx)argv0test.out
+		 $(objpfx)tst-rtld-preload.out $(objpfx)argv0test.out \
+		 $(objpfx)tst-rtld-help.out
 endif
 tests-special += $(objpfx)check-textrel.out $(objpfx)check-execstack.out \
 		 $(objpfx)check-wx-segment.out \
@@ -1907,3 +1908,16 @@  $(objpfx)list-tunables.out: tst-rtld-list-tunables.sh $(objpfx)ld.so
 	$(evaluate-test)
 
 tst-dst-static-ENV = LD_LIBRARY_PATH='$$ORIGIN'
+
+$(objpfx)tst-rtld-help.out: $(objpfx)ld.so
+	$(test-wrapper) $(rtld-prefix) --help > $@; \
+	status=$$?; \
+	echo "info: ld.so exit status: $$status" >> $@; \
+	if ! grep -q 'Legacy HWCAP subdirectories under library search path directories' $@; then \
+	  echo "error: missing subdirectory pattern" >> $@; \
+	  if test $$status -eq 0; then \
+	    status=1; \
+	  fi; \
+	fi; \
+	(exit $$status); \
+	$(evaluate-test)
diff --git a/elf/dl-load.c b/elf/dl-load.c
index 376a2e64d6..2f760503c5 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -758,8 +758,14 @@  _dl_init_paths (const char *llp, const char *source,
   max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
   *aelem = NULL;
 
-  /* This points to the map of the main object.  */
+  /* This points to the map of the main object.  If there is no main
+     object (e.g., under --help, use the dynamic loader itself as a
+     stand-in.  */
   l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
+#ifdef SHARED
+  if (l == NULL)
+    l = &GL (dl_rtld_map);
+#endif
   assert (l->l_type != lt_loaded);
 
   if (l->l_info[DT_RUNPATH])