d8307d
commit 2c75b545de6fe3c44138799c68217a94bc669a88
d8307d
Author: Florian Weimer <fweimer@redhat.com>
d8307d
Date:   Tue Jun 18 16:42:10 2019 +0200
d8307d
d8307d
    elf: Refuse to dlopen PIE objects [BZ #24323]
d8307d
    
d8307d
    Another executable has already been mapped, so the dynamic linker
d8307d
    cannot perform relocations correctly for the second executable.
d8307d
d8307d
diff --git a/elf/Makefile b/elf/Makefile
d8307d
index 08e2f99..27a2fa8 100644
d8307d
--- a/elf/Makefile
d8307d
+++ b/elf/Makefile
d8307d
@@ -310,7 +310,7 @@ test-xfail-tst-protected1b = yes
d8307d
 endif
d8307d
 ifeq (yesyes,$(have-fpie)$(build-shared))
d8307d
 modules-names += tst-piemod1
d8307d
-tests += tst-pie1 tst-pie2
d8307d
+tests += tst-pie1 tst-pie2 tst-dlopen-pie
d8307d
 tests-pie += tst-pie1 tst-pie2
d8307d
 ifeq (yes,$(have-protected-data))
d8307d
 tests += vismain
d8307d
@@ -1084,6 +1084,8 @@ CFLAGS-tst-pie2.c += $(pie-ccflag)
d8307d
 
d8307d
 $(objpfx)tst-piemod1.so: $(libsupport)
d8307d
 $(objpfx)tst-pie1: $(objpfx)tst-piemod1.so
d8307d
+$(objpfx)tst-dlopen-pie: $(libdl)
d8307d
+$(objpfx)tst-dlopen-pie.out: $(objpfx)tst-pie1
d8307d
 
d8307d
 ifeq (yes,$(build-shared))
d8307d
 # NB: Please keep cet-built-dso in sysdeps/x86/Makefile in sync with
d8307d
diff --git a/elf/dl-load.c b/elf/dl-load.c
d8307d
index 2bbef81..5abeb86 100644
d8307d
--- a/elf/dl-load.c
d8307d
+++ b/elf/dl-load.c
d8307d
@@ -1158,6 +1158,10 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
d8307d
 	goto call_lose;
d8307d
       }
d8307d
 
d8307d
+    /* dlopen of an executable is not valid because it is not possible
d8307d
+       to perform proper relocations, handle static TLS, or run the
d8307d
+       ELF constructors.  For PIE, the check needs the dynamic
d8307d
+       section, so there is another check below.  */
d8307d
     if (__glibc_unlikely (type != ET_DYN)
d8307d
 	&& __glibc_unlikely ((mode & __RTLD_OPENEXEC) == 0))
d8307d
       {
d8307d
@@ -1194,9 +1198,11 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
d8307d
   elf_get_dynamic_info (l, NULL);
d8307d
 
d8307d
   /* Make sure we are not dlopen'ing an object that has the
d8307d
-     DF_1_NOOPEN flag set.  */
d8307d
-  if (__glibc_unlikely (l->l_flags_1 & DF_1_NOOPEN)
d8307d
-      && (mode & __RTLD_DLOPEN))
d8307d
+     DF_1_NOOPEN flag set, or a PIE object.  */
d8307d
+  if ((__glibc_unlikely (l->l_flags_1 & DF_1_NOOPEN)
d8307d
+       && (mode & __RTLD_DLOPEN))
d8307d
+      || (__glibc_unlikely (l->l_flags_1 & DF_1_PIE)
d8307d
+	  && __glibc_unlikely ((mode & __RTLD_OPENEXEC) == 0)))
d8307d
     {
d8307d
       /* We are not supposed to load this object.  Free all resources.  */
d8307d
       _dl_unmap_segments (l);
d8307d
@@ -1207,7 +1213,11 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
d8307d
       if (l->l_phdr_allocated)
d8307d
 	free ((void *) l->l_phdr);
d8307d
 
d8307d
-      errstring = N_("shared object cannot be dlopen()ed");
d8307d
+      if (l->l_flags_1 & DF_1_PIE)
d8307d
+	errstring
d8307d
+	  = N_("cannot dynamically load position-independent executable");
d8307d
+      else
d8307d
+	errstring = N_("shared object cannot be dlopen()ed");
d8307d
       goto call_lose;
d8307d
     }
d8307d
 
d8307d
diff --git a/elf/tst-dlopen-pie.c b/elf/tst-dlopen-pie.c
d8307d
new file mode 100644
d8307d
index 0000000..6a41c73
d8307d
--- /dev/null
d8307d
+++ b/elf/tst-dlopen-pie.c
d8307d
@@ -0,0 +1,49 @@
d8307d
+/* dlopen test for PIE objects.
d8307d
+   Copyright (C) 2019 Free Software Foundation, Inc.
d8307d
+   This file is part of the GNU C Library.
d8307d
+
d8307d
+   The GNU C Library is free software; you can redistribute it and/or
d8307d
+   modify it under the terms of the GNU Lesser General Public
d8307d
+   License as published by the Free Software Foundation; either
d8307d
+   version 2.1 of the License, or (at your option) any later version.
d8307d
+
d8307d
+   The GNU C Library is distributed in the hope that it will be useful,
d8307d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
+   Lesser General Public License for more details.
d8307d
+
d8307d
+   You should have received a copy of the GNU Lesser General Public
d8307d
+   License along with the GNU C Library; if not, see
d8307d
+   <http://www.gnu.org/licenses/>.  */
d8307d
+
d8307d
+/* This test attempts to open the (otherwise unrelated) PIE test
d8307d
+   program elf/tst-pie1 and expects the attempt to fail.  */
d8307d
+
d8307d
+#include <dlfcn.h>
d8307d
+#include <stddef.h>
d8307d
+#include <string.h>
d8307d
+#include <support/check.h>
d8307d
+#include <support/support.h>
d8307d
+
d8307d
+static void
d8307d
+test_mode (int mode)
d8307d
+{
d8307d
+  char *pie_path = xasprintf ("%s/elf/tst-pie1", support_objdir_root);
d8307d
+  if (dlopen (pie_path, mode) != NULL)
d8307d
+    FAIL_EXIT1 ("dlopen succeeded unexpectedly (%d)", mode);
d8307d
+  const char *message = dlerror ();
d8307d
+  const char *expected
d8307d
+    = "cannot dynamically load position-independent executable";
d8307d
+  if (strstr (message, expected) == NULL)
d8307d
+    FAIL_EXIT1 ("unexpected error message (mode %d): %s", mode, message);
d8307d
+}
d8307d
+
d8307d
+static int
d8307d
+do_test (void)
d8307d
+{
d8307d
+  test_mode (RTLD_LAZY);
d8307d
+  test_mode (RTLD_NOW);
d8307d
+  return 0;
d8307d
+}
d8307d
+
d8307d
+#include <support/test-driver.c>
d8307d
diff --git a/include/elf.h b/include/elf.h
d8307d
index ab76aaf..14ed67f 100644
d8307d
--- a/include/elf.h
d8307d
+++ b/include/elf.h
d8307d
@@ -23,7 +23,7 @@
d8307d
 # endif
d8307d
 # define DT_1_SUPPORTED_MASK \
d8307d
    (DF_1_NOW | DF_1_NODELETE | DF_1_INITFIRST | DF_1_NOOPEN \
d8307d
-    | DF_1_ORIGIN | DF_1_NODEFLIB)
d8307d
+    | DF_1_ORIGIN | DF_1_NODEFLIB | DF_1_PIE)
d8307d
 
d8307d
 #endif /* !_ISOMAC */
d8307d
 #endif /* elf.h */