d8307d
commit a332bd1518af518c984fad73eba6f46dc5b2b2d4
d8307d
Author: Florian Weimer <fweimer@redhat.com>
d8307d
Date:   Thu Jan 16 16:53:58 2020 +0100
d8307d
d8307d
    elf: Add elf/tst-dlopenfail-2 [BZ #25396]
d8307d
    
d8307d
    Without CET, a jump into a newly loaded object through an overwritten
d8307d
    link map often does not crash, it just executes some random code.
d8307d
    CET detects this in some cases because the function pointer does not
d8307d
    point to the start of a function in the replacement shared object,
d8307d
    so there is no ENDBR instruction.
d8307d
    
d8307d
    The new test uses a small shared object and the existing dangling
d8307d
    link map to trigger the bug.
d8307d
    
d8307d
    Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
d8307d
d8307d
Conflicts:
d8307d
	elf/Makefile
d8307d
	  (Test backport differences.)
d8307d
d8307d
diff --git a/elf/Makefile b/elf/Makefile
d8307d
index 16a3e8dcda19b4ba..f1a16fe8ca594c57 100644
d8307d
--- a/elf/Makefile
d8307d
+++ b/elf/Makefile
d8307d
@@ -192,7 +192,7 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
d8307d
 	 tst-latepthread tst-tls-manydynamic tst-nodelete-dlclose \
d8307d
 	 tst-debug1 tst-main1 tst-absolute-sym tst-absolute-zero tst-big-note \
d8307d
 	 tst-sonamemove-link tst-sonamemove-dlopen tst-initfinilazyfail \
d8307d
-	 tst-dlopenfail
d8307d
+	 tst-dlopenfail tst-dlopenfail-2
d8307d
 #	 reldep9
d8307d
 tests-internal += loadtest unload unload2 circleload1 \
d8307d
 	 neededtest neededtest2 neededtest3 neededtest4 \
d8307d
@@ -301,7 +301,8 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
d8307d
 		tst-sonamemove-linkmod1 \
d8307d
 		tst-sonamemove-runmod1 tst-sonamemove-runmod2 \
d8307d
 		tst-initlazyfailmod tst-finilazyfailmod \
d8307d
-		tst-dlopenfailmod1 tst-dlopenfaillinkmod tst-dlopenfailmod2
d8307d
+		tst-dlopenfailmod1 tst-dlopenfaillinkmod tst-dlopenfailmod2 \
d8307d
+		tst-dlopenfailmod3
d8307d
 
d8307d
 ifeq (yes,$(have-mtls-dialect-gnu2))
d8307d
 tests += tst-gnu2-tls1
d8307d
@@ -1569,6 +1570,10 @@ $(objpfx)tst-dlopenfailmod1.so: \
d8307d
   $(shared-thread-library) $(objpfx)tst-dlopenfaillinkmod.so
d8307d
 LDFLAGS-tst-dlopenfaillinkmod.so = -Wl,-soname,tst-dlopenfail-missingmod.so
d8307d
 $(objpfx)tst-dlopenfailmod2.so: $(shared-thread-library)
d8307d
+$(objpfx)tst-dlopenfail-2: $(libdl)
d8307d
+$(objpfx)tst-dlopenfail.out: \
d8307d
+  $(objpfx)tst-dlopenfailmod1.so $(objpfx)tst-dlopenfailmod2.so \
d8307d
+  $(objpfx)tst-dlopenfailmod3.so
d8307d
 
d8307d
 $(objpfx)tst-dlopen-nodelete-reloc: $(libdl)
d8307d
 $(objpfx)tst-dlopen-nodelete-reloc.out: \
d8307d
diff --git a/elf/tst-dlopenfail-2.c b/elf/tst-dlopenfail-2.c
d8307d
new file mode 100644
d8307d
index 0000000000000000..35bbde64abbb6603
d8307d
--- /dev/null
d8307d
+++ b/elf/tst-dlopenfail-2.c
d8307d
@@ -0,0 +1,59 @@
d8307d
+/* Test unrelated dlopen after dlopen failure involving NODELETE.
d8307d
+   Copyright (C) 2019-2020 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
+   <https://www.gnu.org/licenses/>.  */
d8307d
+
d8307d
+#include <dlfcn.h>
d8307d
+#include <errno.h>
d8307d
+#include <gnu/lib-names.h>
d8307d
+#include <stddef.h>
d8307d
+#include <stdio.h>
d8307d
+#include <string.h>
d8307d
+#include <support/check.h>
d8307d
+#include <support/xdlfcn.h>
d8307d
+
d8307d
+static int
d8307d
+do_test (void)
d8307d
+{
d8307d
+  /* This test uses libpthread as the canonical NODELETE module.  If
d8307d
+     libpthread is no longer NODELETE because it has been merged into
d8307d
+     libc, the test needs to be updated.  */
d8307d
+  TEST_VERIFY (dlsym (NULL, "pthread_create") == NULL);
d8307d
+
d8307d
+  /* This is expected to fail because of the missing dependency.  */
d8307d
+  puts ("info: attempting to load tst-dlopenfailmod1.so");
d8307d
+  TEST_VERIFY (dlopen ("tst-dlopenfailmod1.so", RTLD_LAZY) == NULL);
d8307d
+  const char *message = dlerror ();
d8307d
+  TEST_COMPARE_STRING (message,
d8307d
+                       "tst-dlopenfail-missingmod.so:"
d8307d
+                       " cannot open shared object file:"
d8307d
+                       " No such file or directory");
d8307d
+
d8307d
+  /* Open a small shared object.  With a dangling GL (dl_initfirst)
d8307d
+     pointer, this is likely to crash because there is no longer any
d8307d
+     mapped text segment there (bug 25396).  */
d8307d
+
d8307d
+  puts ("info: attempting to load tst-dlopenfailmod3.so");
d8307d
+  xdlclose (xdlopen ("tst-dlopenfailmod3.so", RTLD_NOW));
d8307d
+
d8307d
+  return 0;
d8307d
+}
d8307d
+
d8307d
+/* Do not perturb the dangling link map.  With M_PERTURB, the link map
d8307d
+   appears to have l_init_called set, so there are no constructor
d8307d
+   calls and no crashes.  */
d8307d
+#define TEST_NO_MALLOPT
d8307d
+#include <support/test-driver.c>
d8307d
diff --git a/elf/tst-dlopenfailmod3.c b/elf/tst-dlopenfailmod3.c
d8307d
new file mode 100644
d8307d
index 0000000000000000..636e971264292110
d8307d
--- /dev/null
d8307d
+++ b/elf/tst-dlopenfailmod3.c
d8307d
@@ -0,0 +1,17 @@
d8307d
+/* Empty module for the tst-dlopenfail-2 test.
d8307d
+   Copyright (C) 2020 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
+   <https://www.gnu.org/licenses/>.  */