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