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