|
|
548bcb |
commit c1cb2deeca1a85c6fc5bd41b90816d48a95bc434
|
|
|
548bcb |
Author: Florian Weimer <fweimer@redhat.com>
|
|
|
548bcb |
Date: Sun Dec 5 11:28:34 2021 +0100
|
|
|
548bcb |
|
|
|
548bcb |
elf: execve statically linked programs instead of crashing [BZ #28648]
|
|
|
548bcb |
|
|
|
548bcb |
Programs without dynamic dependencies and without a program
|
|
|
548bcb |
interpreter are now run via execve.
|
|
|
548bcb |
|
|
|
548bcb |
Previously, the dynamic linker either crashed while attempting to
|
|
|
548bcb |
read a non-existing dynamic segment (looking for DT_AUDIT/DT_DEPAUDIT
|
|
|
548bcb |
data), or the self-relocated in the static PIE executable crashed
|
|
|
548bcb |
because the outer dynamic linker had already applied RELRO protection.
|
|
|
548bcb |
|
|
|
548bcb |
<dl-execve.h> is needed because execve is not available in the
|
|
|
548bcb |
dynamic loader on Hurd.
|
|
|
548bcb |
|
|
|
548bcb |
Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
|
|
|
548bcb |
|
|
|
548bcb |
Conflicts:
|
|
|
548bcb |
elf/Makefile
|
|
|
548bcb |
(some missing backports)
|
|
|
548bcb |
elf/rtld.c
|
|
|
548bcb |
(missing rework of ld.so self-relocation downstream,
|
|
|
548bcb |
always print error as a number due to missing
|
|
|
548bcb |
sterrorname_np, also fix errcode/errno glitch)
|
|
|
548bcb |
sysdeps/unix/sysv/linux/dl-execve.h
|
|
|
548bcb |
(missing INTERNAL_SYSCALL_CALL refactoring to Linux-like
|
|
|
548bcb |
calling convention)
|
|
|
548bcb |
|
|
|
548bcb |
diff --git a/elf/Makefile b/elf/Makefile
|
|
|
548bcb |
index d246f1c0d9e019fd..b3e8ab2792608de7 100644
|
|
|
548bcb |
--- a/elf/Makefile
|
|
|
548bcb |
+++ b/elf/Makefile
|
|
|
548bcb |
@@ -214,7 +214,8 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
|
|
|
548bcb |
tst-tls-ie tst-tls-ie-dlmopen \
|
|
|
548bcb |
argv0test \
|
|
|
548bcb |
tst-glibc-hwcaps tst-glibc-hwcaps-prepend tst-glibc-hwcaps-mask \
|
|
|
548bcb |
- tst-tls20 tst-tls21
|
|
|
548bcb |
+ tst-tls20 tst-tls21 \
|
|
|
548bcb |
+ tst-rtld-run-static \
|
|
|
548bcb |
# reldep9
|
|
|
548bcb |
tests-internal += loadtest unload unload2 circleload1 \
|
|
|
548bcb |
neededtest neededtest2 neededtest3 neededtest4 \
|
|
|
548bcb |
@@ -1917,3 +1918,5 @@ $(objpfx)tst-tls20.out: $(objpfx)tst-tls20mod-bad.so \
|
|
|
548bcb |
$(objpfx)tst-tls21: $(libdl) $(shared-thread-library)
|
|
|
548bcb |
$(objpfx)tst-tls21.out: $(objpfx)tst-tls21mod.so
|
|
|
548bcb |
$(objpfx)tst-tls21mod.so: $(tst-tls-many-dynamic-modules:%=$(objpfx)%.so)
|
|
|
548bcb |
+
|
|
|
548bcb |
+$(objpfx)tst-rtld-run-static.out: $(objpfx)/ldconfig
|
|
|
548bcb |
diff --git a/elf/rtld.c b/elf/rtld.c
|
|
|
548bcb |
index d14c388f548d6d51..461d8c114a875a9b 100644
|
|
|
548bcb |
--- a/elf/rtld.c
|
|
|
548bcb |
+++ b/elf/rtld.c
|
|
|
548bcb |
@@ -48,6 +48,7 @@
|
|
|
548bcb |
#include <dl-main.h>
|
|
|
548bcb |
#include <gnu/lib-names.h>
|
|
|
548bcb |
#include <dl-tunables.h>
|
|
|
548bcb |
+#include <dl-execve.h>
|
|
|
548bcb |
|
|
|
548bcb |
#include <assert.h>
|
|
|
548bcb |
|
|
|
548bcb |
@@ -1114,6 +1115,40 @@ load_audit_modules (struct link_map *main_map, struct audit_list *audit_list)
|
|
|
548bcb |
}
|
|
|
548bcb |
}
|
|
|
548bcb |
|
|
|
548bcb |
+/* Check if the executable is not actualy dynamically linked, and
|
|
|
548bcb |
+ invoke it directly in that case. */
|
|
|
548bcb |
+static void
|
|
|
548bcb |
+rtld_chain_load (struct link_map *main_map, char *argv0)
|
|
|
548bcb |
+{
|
|
|
548bcb |
+ /* The dynamic loader run against itself. */
|
|
|
548bcb |
+ const char *rtld_soname
|
|
|
548bcb |
+ = ((const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
|
|
|
548bcb |
+ + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val);
|
|
|
548bcb |
+ if (main_map->l_info[DT_SONAME] != NULL
|
|
|
548bcb |
+ && strcmp (rtld_soname,
|
|
|
548bcb |
+ ((const char *) D_PTR (main_map, l_info[DT_STRTAB])
|
|
|
548bcb |
+ + main_map->l_info[DT_SONAME]->d_un.d_val)) == 0)
|
|
|
548bcb |
+ _dl_fatal_printf ("%s: loader cannot load itself\n", rtld_soname);
|
|
|
548bcb |
+
|
|
|
548bcb |
+ /* With DT_NEEDED dependencies, the executable is dynamically
|
|
|
548bcb |
+ linked. */
|
|
|
548bcb |
+ if (__glibc_unlikely (main_map->l_info[DT_NEEDED] != NULL))
|
|
|
548bcb |
+ return;
|
|
|
548bcb |
+
|
|
|
548bcb |
+ /* If the executable has program interpreter, it is dynamically
|
|
|
548bcb |
+ linked. */
|
|
|
548bcb |
+ for (size_t i = 0; i < main_map->l_phnum; ++i)
|
|
|
548bcb |
+ if (main_map->l_phdr[i].p_type == PT_INTERP)
|
|
|
548bcb |
+ return;
|
|
|
548bcb |
+
|
|
|
548bcb |
+ const char *pathname = _dl_argv[0];
|
|
|
548bcb |
+ if (argv0 != NULL)
|
|
|
548bcb |
+ _dl_argv[0] = argv0;
|
|
|
548bcb |
+ int errcode = __rtld_execve (pathname, _dl_argv, _environ);
|
|
|
548bcb |
+ _dl_fatal_printf("%s: cannot execute %s: %d\n",
|
|
|
548bcb |
+ rtld_soname, pathname, errcode);
|
|
|
548bcb |
+}
|
|
|
548bcb |
+
|
|
|
548bcb |
static void
|
|
|
548bcb |
dl_main (const ElfW(Phdr) *phdr,
|
|
|
548bcb |
ElfW(Word) phnum,
|
|
|
548bcb |
@@ -1384,14 +1419,8 @@ dl_main (const ElfW(Phdr) *phdr,
|
|
|
548bcb |
/* Now the map for the main executable is available. */
|
|
|
548bcb |
main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
|
|
|
548bcb |
|
|
|
548bcb |
- if (__glibc_likely (state.mode == rtld_mode_normal)
|
|
|
548bcb |
- && GL(dl_rtld_map).l_info[DT_SONAME] != NULL
|
|
|
548bcb |
- && main_map->l_info[DT_SONAME] != NULL
|
|
|
548bcb |
- && strcmp ((const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
|
|
|
548bcb |
- + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val,
|
|
|
548bcb |
- (const char *) D_PTR (main_map, l_info[DT_STRTAB])
|
|
|
548bcb |
- + main_map->l_info[DT_SONAME]->d_un.d_val) == 0)
|
|
|
548bcb |
- _dl_fatal_printf ("loader cannot load itself\n");
|
|
|
548bcb |
+ if (__glibc_likely (state.mode == rtld_mode_normal))
|
|
|
548bcb |
+ rtld_chain_load (main_map, argv0);
|
|
|
548bcb |
|
|
|
548bcb |
phdr = main_map->l_phdr;
|
|
|
548bcb |
phnum = main_map->l_phnum;
|
|
|
548bcb |
diff --git a/elf/tst-rtld-run-static.c b/elf/tst-rtld-run-static.c
|
|
|
548bcb |
new file mode 100644
|
|
|
548bcb |
index 0000000000000000..7281093504b675c4
|
|
|
548bcb |
--- /dev/null
|
|
|
548bcb |
+++ b/elf/tst-rtld-run-static.c
|
|
|
548bcb |
@@ -0,0 +1,62 @@
|
|
|
548bcb |
+/* Test running statically linked programs using ld.so.
|
|
|
548bcb |
+ Copyright (C) 2021 Free Software Foundation, Inc.
|
|
|
548bcb |
+ This file is part of the GNU C Library.
|
|
|
548bcb |
+
|
|
|
548bcb |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
548bcb |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
548bcb |
+ License as published by the Free Software Foundation; either
|
|
|
548bcb |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
548bcb |
+
|
|
|
548bcb |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
548bcb |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
548bcb |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
548bcb |
+ Lesser General Public License for more details.
|
|
|
548bcb |
+
|
|
|
548bcb |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
548bcb |
+ License along with the GNU C Library; if not, see
|
|
|
548bcb |
+ <https://www.gnu.org/licenses/>. */
|
|
|
548bcb |
+
|
|
|
548bcb |
+#include <support/check.h>
|
|
|
548bcb |
+#include <support/support.h>
|
|
|
548bcb |
+#include <support/capture_subprocess.h>
|
|
|
548bcb |
+#include <string.h>
|
|
|
548bcb |
+#include <stdlib.h>
|
|
|
548bcb |
+
|
|
|
548bcb |
+static int
|
|
|
548bcb |
+do_test (void)
|
|
|
548bcb |
+{
|
|
|
548bcb |
+ char *ldconfig_path = xasprintf ("%s/elf/ldconfig", support_objdir_root);
|
|
|
548bcb |
+
|
|
|
548bcb |
+ {
|
|
|
548bcb |
+ char *argv[] = { (char *) "ld.so", ldconfig_path, (char *) "--help", NULL };
|
|
|
548bcb |
+ struct support_capture_subprocess cap
|
|
|
548bcb |
+ = support_capture_subprogram (support_objdir_elf_ldso, argv);
|
|
|
548bcb |
+ support_capture_subprocess_check (&cap, "no --argv0", 0, sc_allow_stdout);
|
|
|
548bcb |
+ puts ("info: output without --argv0:");
|
|
|
548bcb |
+ puts (cap.out.buffer);
|
|
|
548bcb |
+ TEST_VERIFY (strstr (cap.out.buffer, "Usage: ldconfig [OPTION...]\n")
|
|
|
548bcb |
+ == cap.out.buffer);
|
|
|
548bcb |
+ support_capture_subprocess_free (&cap);
|
|
|
548bcb |
+ }
|
|
|
548bcb |
+
|
|
|
548bcb |
+ {
|
|
|
548bcb |
+ char *argv[] =
|
|
|
548bcb |
+ {
|
|
|
548bcb |
+ (char *) "ld.so", (char *) "--argv0", (char *) "ldconfig-argv0",
|
|
|
548bcb |
+ ldconfig_path, (char *) "--help", NULL
|
|
|
548bcb |
+ };
|
|
|
548bcb |
+ struct support_capture_subprocess cap
|
|
|
548bcb |
+ = support_capture_subprogram (support_objdir_elf_ldso, argv);
|
|
|
548bcb |
+ support_capture_subprocess_check (&cap, "with --argv0", 0, sc_allow_stdout);
|
|
|
548bcb |
+ puts ("info: output with --argv0:");
|
|
|
548bcb |
+ puts (cap.out.buffer);
|
|
|
548bcb |
+ TEST_VERIFY (strstr (cap.out.buffer, "Usage: ldconfig-argv0 [OPTION...]\n")
|
|
|
548bcb |
+ == cap.out.buffer);
|
|
|
548bcb |
+ support_capture_subprocess_free (&cap);
|
|
|
548bcb |
+ }
|
|
|
548bcb |
+
|
|
|
548bcb |
+ free (ldconfig_path);
|
|
|
548bcb |
+ return 0;
|
|
|
548bcb |
+}
|
|
|
548bcb |
+
|
|
|
548bcb |
+#include <support/test-driver.c>
|
|
|
548bcb |
diff --git a/sysdeps/generic/dl-execve.h b/sysdeps/generic/dl-execve.h
|
|
|
548bcb |
new file mode 100644
|
|
|
548bcb |
index 0000000000000000..5fd097df69e1770c
|
|
|
548bcb |
--- /dev/null
|
|
|
548bcb |
+++ b/sysdeps/generic/dl-execve.h
|
|
|
548bcb |
@@ -0,0 +1,25 @@
|
|
|
548bcb |
+/* execve for the dynamic linker. Generic stub version.
|
|
|
548bcb |
+ Copyright (C) 2021 Free Software Foundation, Inc.
|
|
|
548bcb |
+ This file is part of the GNU C Library.
|
|
|
548bcb |
+
|
|
|
548bcb |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
548bcb |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
548bcb |
+ License as published by the Free Software Foundation; either
|
|
|
548bcb |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
548bcb |
+
|
|
|
548bcb |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
548bcb |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
548bcb |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
548bcb |
+ Lesser General Public License for more details.
|
|
|
548bcb |
+
|
|
|
548bcb |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
548bcb |
+ License along with the GNU C Library; if not, see
|
|
|
548bcb |
+ <https://www.gnu.org/licenses/>. */
|
|
|
548bcb |
+
|
|
|
548bcb |
+#include <errno.h>
|
|
|
548bcb |
+
|
|
|
548bcb |
+static int
|
|
|
548bcb |
+__rtld_execve (const char *path, char *const *argv, char *const *envp)
|
|
|
548bcb |
+{
|
|
|
548bcb |
+ return ENOSYS;
|
|
|
548bcb |
+}
|
|
|
548bcb |
diff --git a/sysdeps/unix/sysv/linux/dl-execve.h b/sysdeps/unix/sysv/linux/dl-execve.h
|
|
|
548bcb |
new file mode 100644
|
|
|
548bcb |
index 0000000000000000..9ec6539286bb0589
|
|
|
548bcb |
--- /dev/null
|
|
|
548bcb |
+++ b/sysdeps/unix/sysv/linux/dl-execve.h
|
|
|
548bcb |
@@ -0,0 +1,30 @@
|
|
|
548bcb |
+/* execve for the dynamic linker. Linux version.
|
|
|
548bcb |
+ Copyright (C) 2021 Free Software Foundation, Inc.
|
|
|
548bcb |
+ This file is part of the GNU C Library.
|
|
|
548bcb |
+
|
|
|
548bcb |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
548bcb |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
548bcb |
+ License as published by the Free Software Foundation; either
|
|
|
548bcb |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
548bcb |
+
|
|
|
548bcb |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
548bcb |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
548bcb |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
548bcb |
+ Lesser General Public License for more details.
|
|
|
548bcb |
+
|
|
|
548bcb |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
548bcb |
+ License along with the GNU C Library; if not, see
|
|
|
548bcb |
+ <https://www.gnu.org/licenses/>. */
|
|
|
548bcb |
+
|
|
|
548bcb |
+#include <errno.h>
|
|
|
548bcb |
+
|
|
|
548bcb |
+static inline int
|
|
|
548bcb |
+__rtld_execve (const char *path, char *const *argv, char *const *envp)
|
|
|
548bcb |
+{
|
|
|
548bcb |
+ INTERNAL_SYSCALL_DECL (err);
|
|
|
548bcb |
+ long int r = INTERNAL_SYSCALL_CALL (execve, err, path, argv, envp);
|
|
|
548bcb |
+ if (INTERNAL_SYSCALL_ERROR_P (r, err))
|
|
|
548bcb |
+ return INTERNAL_SYSCALL_ERRNO (r, err);
|
|
|
548bcb |
+ else
|
|
|
548bcb |
+ return 0;
|
|
|
548bcb |
+}
|