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