0b0d35
Partial backport of:
0b0d35
0b0d35
commit 7d3db434f910c23591f748a6d0ac3548af1048bb
0b0d35
Author: Florian Weimer <fweimer@redhat.com>
0b0d35
Date:   Thu Oct 17 08:51:21 2019 +0200
0b0d35
0b0d35
    Rename and split elf/tst-dlopen-aout collection of tests
0b0d35
    
0b0d35
    From the beginning, elf/tst-dlopen-aout has exercised two different
0b0d35
    bugs: (a) failure to report errors for a dlopen of the executable
0b0d35
    itself in some cases (bug 24900) and (b) incorrect rollback of the
0b0d35
    TLS modid allocation in case of a dlopen failure (bug 16634).
0b0d35
    
0b0d35
    This commit replaces the test with elf/tst-dlopen-self for (a) and
0b0d35
    elf/tst-dlopen-tlsmodid for (b).  The latter tests use the
0b0d35
    elf/tst-dlopen-self binaries (or iconv) with dlopen, so they are
0b0d35
    no longer self-dlopen tests.
0b0d35
    
0b0d35
    Tested on x86_64-linux-gnu and i686-linux-gnu, with a toolchain that
0b0d35
    does not default to PIE.
0b0d35
0b0d35
Only the non-PIE, non-container test elf/tst-dlopen-tlsmodid is
0b0d35
included.  The reason is that the self-dlopen fixes and the PIE TLS
0b0d35
modid fix have not been backported, and that container testing support
0b0d35
is missing downstream.  The test binary is adjusted to tst-tls10
0b0d35
because tst-dlopen-self does not exist in the backport.
0b0d35
0b0d35
diff --git a/elf/Makefile b/elf/Makefile
0b0d35
index cfd039fc9dfb0be7..c22008b54afc91f5 100644
0b0d35
--- a/elf/Makefile
0b0d35
+++ b/elf/Makefile
0b0d35
@@ -153,7 +153,7 @@ tests += loadtest restest1 preloadtest loadfail multiload origtest resolvfail \
0b0d35
 	 tst-stackguard1 tst-addr1 tst-thrlock \
0b0d35
 	 tst-unique1 tst-unique2 tst-unique3 tst-unique4 \
0b0d35
 	 tst-initorder tst-initorder2 tst-relsort1 tst-ptrguard1 \
0b0d35
-	 tst-big-note
0b0d35
+	 tst-big-note tst-dlopen-tlsmodid
0b0d35
 #	 reldep9
0b0d35
 test-srcs = tst-pathopt
0b0d35
 selinux-enabled := $(shell cat /selinux/enforce 2> /dev/null)
0b0d35
@@ -1101,6 +1101,9 @@ $(objpfx)tst-addr1: $(libdl)
0b0d35
 
0b0d35
 $(objpfx)tst-thrlock: $(libdl) $(shared-thread-library)
0b0d35
 
0b0d35
+$(objpfx)tst-dlopen-tlsmodid: $(libdl) $(shared-thread-library)
0b0d35
+$(objpfx)tst-dlopen-tlsmodid.out: $(objpfx)tst-tls10
0b0d35
+
0b0d35
 CFLAGS-ifuncmain1pic.c += $(pic-ccflag)
0b0d35
 CFLAGS-ifuncmain1picstatic.c += $(pic-ccflag)
0b0d35
 CFLAGS-ifuncmain1staticpic.c += $(pic-ccflag)
0b0d35
diff --git a/elf/tst-dlopen-tlsmodid.c b/elf/tst-dlopen-tlsmodid.c
0b0d35
new file mode 100644
0b0d35
index 0000000000000000..c5b1c39369aa610c
0b0d35
--- /dev/null
0b0d35
+++ b/elf/tst-dlopen-tlsmodid.c
0b0d35
@@ -0,0 +1,25 @@
0b0d35
+/* Test case for BZ #16634.  Non-PIE version.
0b0d35
+
0b0d35
+   Verify that incorrectly dlopen()ing an executable without
0b0d35
+   __RTLD_OPENEXEC does not cause assertion in ld.so, and that it
0b0d35
+   actually results in an error.
0b0d35
+
0b0d35
+   Copyright (C) 2014-2019 Free Software Foundation, Inc.
0b0d35
+   This file is part of the GNU C Library.
0b0d35
+
0b0d35
+   The GNU C Library is free software; you can redistribute it and/or
0b0d35
+   modify it under the terms of the GNU Lesser General Public
0b0d35
+   License as published by the Free Software Foundation; either
0b0d35
+   version 2.1 of the License, or (at your option) any later version.
0b0d35
+
0b0d35
+   The GNU C Library is distributed in the hope that it will be useful,
0b0d35
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
0b0d35
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0b0d35
+   Lesser General Public License for more details.
0b0d35
+
0b0d35
+   You should have received a copy of the GNU Lesser General Public
0b0d35
+   License along with the GNU C Library; if not, see
0b0d35
+   <https://www.gnu.org/licenses/>.  */
0b0d35
+
0b0d35
+#define TST_DLOPEN_TLSMODID_PATH "tst-tls10"
0b0d35
+#include "tst-dlopen-tlsmodid.h"
0b0d35
diff --git a/elf/tst-dlopen-tlsmodid.h b/elf/tst-dlopen-tlsmodid.h
0b0d35
new file mode 100644
0b0d35
index 0000000000000000..c747cb14911c72fa
0b0d35
--- /dev/null
0b0d35
+++ b/elf/tst-dlopen-tlsmodid.h
0b0d35
@@ -0,0 +1,87 @@
0b0d35
+/* Common code for tst-dlopen-tlsmodid, tst-dlopen-tlsmodid-pie,
0b0d35
+   tst-dlopen-tlsmodid-container.
0b0d35
+
0b0d35
+   Verify that incorrectly dlopen()ing an executable without
0b0d35
+   __RTLD_OPENEXEC does not cause assertion in ld.so, and that it
0b0d35
+   actually results in an error.
0b0d35
+
0b0d35
+   Copyright (C) 2014-2019 Free Software Foundation, Inc.
0b0d35
+   This file is part of the GNU C Library.
0b0d35
+
0b0d35
+   The GNU C Library is free software; you can redistribute it and/or
0b0d35
+   modify it under the terms of the GNU Lesser General Public
0b0d35
+   License as published by the Free Software Foundation; either
0b0d35
+   version 2.1 of the License, or (at your option) any later version.
0b0d35
+
0b0d35
+   The GNU C Library is distributed in the hope that it will be useful,
0b0d35
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
0b0d35
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0b0d35
+   Lesser General Public License for more details.
0b0d35
+
0b0d35
+   You should have received a copy of the GNU Lesser General Public
0b0d35
+   License along with the GNU C Library; if not, see
0b0d35
+   <https://www.gnu.org/licenses/>.  */
0b0d35
+
0b0d35
+/* Before including this file, the macro TST_DLOPEN_TLSMODID_PATH must
0b0d35
+   be defined, to specify the path used for the open operation.  */
0b0d35
+
0b0d35
+#include <dlfcn.h>
0b0d35
+#include <pthread.h>
0b0d35
+#include <stdio.h>
0b0d35
+#include <stdlib.h>
0b0d35
+#include <string.h>
0b0d35
+#include <support/check.h>
0b0d35
+#include <support/support.h>
0b0d35
+#include <support/xthread.h>
0b0d35
+
0b0d35
+__thread int x;
0b0d35
+
0b0d35
+void *
0b0d35
+fn (void *p)
0b0d35
+{
0b0d35
+  return p;
0b0d35
+}
0b0d35
+
0b0d35
+/* Call dlopen and check that fails with an error message indicating
0b0d35
+   an attempt to open an ET_EXEC or PIE object.  */
0b0d35
+static void
0b0d35
+check_dlopen_failure (void)
0b0d35
+{
0b0d35
+  void *handle = dlopen (TST_DLOPEN_TLSMODID_PATH, RTLD_LAZY);
0b0d35
+  if (handle != NULL)
0b0d35
+    FAIL_EXIT1 ("dlopen succeeded unexpectedly: %s", TST_DLOPEN_TLSMODID_PATH);
0b0d35
+
0b0d35
+  const char *message = dlerror ();
0b0d35
+  TEST_VERIFY_EXIT (message != NULL);
0b0d35
+  if ((strstr (message,
0b0d35
+	       "cannot dynamically load position-independent executable")
0b0d35
+       == NULL)
0b0d35
+      && strstr (message, "cannot dynamically load executable") == NULL)
0b0d35
+    FAIL_EXIT1 ("invalid dlopen error message: \"%s\"", message);
0b0d35
+}
0b0d35
+
0b0d35
+static int
0b0d35
+do_test (int argc, char *argv[])
0b0d35
+{
0b0d35
+  int j;
0b0d35
+
0b0d35
+  for (j = 0; j < 100; ++j)
0b0d35
+    {
0b0d35
+      pthread_t thr;
0b0d35
+
0b0d35
+      check_dlopen_failure ();
0b0d35
+
0b0d35
+      /* We create threads to force TLS allocation, which triggers
0b0d35
+	 the original bug i.e. running out of surplus slotinfo entries
0b0d35
+	 for TLS.  */
0b0d35
+      thr = xpthread_create (NULL, fn, NULL);
0b0d35
+      xpthread_join (thr);
0b0d35
+    }
0b0d35
+
0b0d35
+  check_dlopen_failure ();
0b0d35
+
0b0d35
+  return 0;
0b0d35
+}
0b0d35
+
0b0d35
+#define TEST_FUNCTION_ARGV do_test
0b0d35
+#include <support/test-driver.c>