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