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