e38cb5
From 2c42257314536b94cc8d52edede86e94e98c1436 Mon Sep 17 00:00:00 2001
e38cb5
From: Florian Weimer <fweimer@redhat.com>
e38cb5
Date: Fri, 14 Oct 2022 11:02:25 +0200
e38cb5
Subject: [PATCH] elf: Do not completely clear reused namespace in dlmopen (bug
e38cb5
 29600)
e38cb5
Content-type: text/plain; charset=UTF-8
e38cb5
e38cb5
The data in the _ns_debug member must be preserved, otherwise
e38cb5
_dl_debug_initialize enters an infinite loop.  To be conservative,
e38cb5
only clear the libc_map member for now, to fix bug 29528.
e38cb5
e38cb5
Fixes commit d0e357ff45a75553dee3b17ed7d303bfa544f6fe
e38cb5
("elf: Call __libc_early_init for reused namespaces (bug 29528)"),
e38cb5
by reverting most of it.
e38cb5
e38cb5
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
e38cb5
Tested-by: Carlos O'Donell <carlos@redhat.com>
e38cb5
---
e38cb5
 elf/dl-open.c           | 14 ++++++--------
e38cb5
 elf/tst-dlmopen-twice.c | 28 ++++++++++++++++++++++++----
e38cb5
 2 files changed, 30 insertions(+), 12 deletions(-)
e38cb5
e38cb5
diff --git a/elf/dl-open.c b/elf/dl-open.c
e38cb5
index 46e8066fd8..e7db5e9642 100644
e38cb5
--- a/elf/dl-open.c
e38cb5
+++ b/elf/dl-open.c
e38cb5
@@ -836,15 +836,13 @@ _dl_open (const char *file, int mode, co
e38cb5
 	  _dl_signal_error (EINVAL, file, NULL, N_("\
e38cb5
 no more namespaces available for dlmopen()"));
e38cb5
 	}
e38cb5
+      else if (nsid == GL(dl_nns))
e38cb5
+	{
e38cb5
+	  __rtld_lock_initialize (GL(dl_ns)[nsid]._ns_unique_sym_table.lock);
e38cb5
+	  ++GL(dl_nns);
e38cb5
+	}
e38cb5
 
e38cb5
-      if (nsid == GL(dl_nns))
e38cb5
-	++GL(dl_nns);
e38cb5
-
e38cb5
-      /* Initialize the new namespace.  Most members are
e38cb5
-	 zero-initialized, only the lock needs special treatment.  */
e38cb5
-      memset (&GL(dl_ns)[nsid], 0, sizeof (GL(dl_ns)[nsid]));
e38cb5
-      __rtld_lock_initialize (GL(dl_ns)[nsid]._ns_unique_sym_table.lock);
e38cb5
-
e38cb5
+      GL(dl_ns)[nsid].libc_map = NULL;
e38cb5
       _dl_debug_initialize (0, nsid)->r_state = RT_CONSISTENT;
e38cb5
     }
e38cb5
   /* Never allow loading a DSO in a namespace which is empty.  Such
e38cb5
diff --git a/elf/tst-dlmopen-twice.c b/elf/tst-dlmopen-twice.c
e38cb5
index 449f3c8fa9..70c71fe19c 100644
e38cb5
--- a/elf/tst-dlmopen-twice.c
e38cb5
+++ b/elf/tst-dlmopen-twice.c
e38cb5
@@ -16,18 +16,38 @@
e38cb5
    License along with the GNU C Library; if not, see
e38cb5
    <https://www.gnu.org/licenses/>.  */
e38cb5
 
e38cb5
-#include <support/xdlfcn.h>
e38cb5
+#include <stdio.h>
e38cb5
 #include <support/check.h>
e38cb5
+#include <support/xdlfcn.h>
e38cb5
 
e38cb5
-static int
e38cb5
-do_test (void)
e38cb5
+/* Run the test multiple times, to check finding a new namespace while
e38cb5
+   another namespace is already in use.  This used to trigger bug 29600.  */
e38cb5
+static void
e38cb5
+recurse (int depth)
e38cb5
 {
e38cb5
-  void *handle = xdlmopen (LM_ID_NEWLM, "tst-dlmopen-twice-mod1.so", RTLD_NOW);
e38cb5
+  if (depth == 0)
e38cb5
+    return;
e38cb5
+
e38cb5
+  printf ("info: running at depth %d\n", depth);
e38cb5
+  void *handle = xdlmopen (LM_ID_NEWLM, "tst-dlmopen-twice-mod1.so",
e38cb5
+                           RTLD_NOW);
e38cb5
   xdlclose (handle);
e38cb5
   handle = xdlmopen (LM_ID_NEWLM, "tst-dlmopen-twice-mod2.so", RTLD_NOW);
e38cb5
   int (*run_check) (void) = xdlsym (handle, "run_check");
e38cb5
   TEST_COMPARE (run_check (), 0);
e38cb5
+  recurse (depth - 1);
e38cb5
   xdlclose (handle);
e38cb5
+}
e38cb5
+
e38cb5
+static int
e38cb5
+do_test (void)
e38cb5
+{
e38cb5
+  /* First run the test without nesting.  */
e38cb5
+  recurse (1);
e38cb5
+
e38cb5
+  /* Then with nesting.  The constant needs to be less than the
e38cb5
+     internal DL_NNS namespace constant.  */
e38cb5
+  recurse (10);
e38cb5
   return 0;
e38cb5
 }
e38cb5
 
e38cb5
-- 
e38cb5
2.31.1
e38cb5