abe59f
Added $(objpfx)tst-auditmod20: $(libdl) in elf/Makefile since
abe59f
we still have $(libdl) in RHEL8.
abe59f
abe59f
commit 484e672ddabe0a919a692520e6ac8f2580866235
abe59f
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
abe59f
Date:   Wed Jun 30 17:33:57 2021 -0300
abe59f
abe59f
    elf: Do not fail for failed dlmopen on audit modules (BZ #28061)
abe59f
    
abe59f
    The dl_main sets the LM_ID_BASE to RT_ADD just before starting to
abe59f
    add load new shared objects.  The state is set to RT_CONSISTENT just
abe59f
    after all objects are loaded.
abe59f
    
abe59f
    However if a audit modules tries to dlmopen an inexistent module,
abe59f
    the _dl_open will assert that the namespace is in an inconsistent
abe59f
    state.
abe59f
    
abe59f
    This is different than dlopen, since first it will not use
abe59f
    LM_ID_BASE and second _dl_map_object_from_fd is the sole responsible
abe59f
    to set and reset the r_state value.
abe59f
    
abe59f
    So the assert on _dl_open can not really be seen if the state is
abe59f
    consistent, since _dt_main resets it.  This patch removes the assert.
abe59f
    
abe59f
    Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu.
abe59f
    
abe59f
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
abe59f
abe59f
Conflicts:
abe59f
	elf/dl-open.c
abe59f
		Uses dl_debug_initialize instead of dl_debug_update.
abe59f
abe59f
diff --git a/elf/Makefile b/elf/Makefile
abe59f
index f047c1cce0c55da0..7c7b9e1937d3e41c 100644
abe59f
--- a/elf/Makefile
abe59f
+++ b/elf/Makefile
abe59f
@@ -222,6 +222,7 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
abe59f
 	 tst-audit17 \
abe59f
 	 tst-audit18 \
abe59f
 	 tst-audit19b \
abe59f
+	 tst-audit20 \
abe59f
 	 tst-audit22 \
abe59f
 #	 reldep9
abe59f
 tests-internal += loadtest unload unload2 circleload1 \
abe59f
@@ -364,6 +365,7 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
abe59f
 		tst-auditmod19a \
abe59f
 		tst-auditmod19b \
abe59f
 		tst-audit19bmod \
abe59f
+		tst-auditmod20 \
abe59f
 		tst-auditmod22 \
abe59f
 
abe59f
 # Most modules build with _ISOMAC defined, but those filtered out
abe59f
@@ -1579,6 +1581,10 @@ $(objpfx)tst-audit19b.out: $(objpfx)tst-auditmod19b.so
abe59f
 $(objpfx)tst-audit19b: $(objpfx)tst-audit19bmod.so
abe59f
 tst-audit19b-ARGS = -- $(host-test-program-cmd)
abe59f
 
abe59f
+$(objpfx)tst-audit20.out: $(objpfx)tst-auditmod20.so
abe59f
+tst-audit20-ENV = LD_AUDIT=$(objpfx)tst-auditmod20.so
abe59f
+$(objpfx)tst-auditmod20.so: $(libdl)
abe59f
+
abe59f
 $(objpfx)tst-audit22.out: $(objpfx)tst-auditmod22.so
abe59f
 tst-audit22-ARGS = -- $(host-test-program-cmd)
abe59f
 
abe59f
diff --git a/elf/dl-open.c b/elf/dl-open.c
abe59f
index 660a56b2fb2639cd..6b85e9ab4e249f86 100644
abe59f
--- a/elf/dl-open.c
abe59f
+++ b/elf/dl-open.c
abe59f
@@ -911,8 +911,6 @@ no more namespaces available for dlmopen()"));
abe59f
 	     the flag here.  */
abe59f
 	}
abe59f
 
abe59f
-      assert (_dl_debug_initialize (0, args.nsid)->r_state == RT_CONSISTENT);
abe59f
-
abe59f
       /* Release the lock.  */
abe59f
       __rtld_lock_unlock_recursive (GL(dl_load_lock));
abe59f
 
abe59f
diff --git a/elf/tst-audit20.c b/elf/tst-audit20.c
abe59f
new file mode 100644
abe59f
index 0000000000000000..6f39ccee865b012b
abe59f
--- /dev/null
abe59f
+++ b/elf/tst-audit20.c
abe59f
@@ -0,0 +1,25 @@
abe59f
+/* Check dlopen failure on audit modules.
abe59f
+   Copyright (C) 2021 Free Software Foundation, Inc.
abe59f
+   This file is part of the GNU C Library.
abe59f
+
abe59f
+   The GNU C Library is free software; you can redistribute it and/or
abe59f
+   modify it under the terms of the GNU Lesser General Public
abe59f
+   License as published by the Free Software Foundation; either
abe59f
+   version 2.1 of the License, or (at your option) any later version.
abe59f
+
abe59f
+   The GNU C Library is distributed in the hope that it will be useful,
abe59f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
abe59f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
abe59f
+   Lesser General Public License for more details.
abe59f
+
abe59f
+   You should have received a copy of the GNU Lesser General Public
abe59f
+   License along with the GNU C Library; if not, see
abe59f
+   <https://www.gnu.org/licenses/>.  */
abe59f
+
abe59f
+static int
abe59f
+do_test (void)
abe59f
+{
abe59f
+  return 0;
abe59f
+}
abe59f
+
abe59f
+#include <support/test-driver.c>
abe59f
diff --git a/elf/tst-auditmod20.c b/elf/tst-auditmod20.c
abe59f
new file mode 100644
abe59f
index 0000000000000000..c57e50ee4e88dd6b
abe59f
--- /dev/null
abe59f
+++ b/elf/tst-auditmod20.c
abe59f
@@ -0,0 +1,57 @@
abe59f
+/* Check dlopen failure on audit modules.
abe59f
+   Copyright (C) 2021 Free Software Foundation, Inc.
abe59f
+   This file is part of the GNU C Library.
abe59f
+
abe59f
+   The GNU C Library is free software; you can redistribute it and/or
abe59f
+   modify it under the terms of the GNU Lesser General Public
abe59f
+   License as published by the Free Software Foundation; either
abe59f
+   version 2.1 of the License, or (at your option) any later version.
abe59f
+
abe59f
+   The GNU C Library is distributed in the hope that it will be useful,
abe59f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
abe59f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
abe59f
+   Lesser General Public License for more details.
abe59f
+
abe59f
+   You should have received a copy of the GNU Lesser General Public
abe59f
+   License along with the GNU C Library; if not, see
abe59f
+   <https://www.gnu.org/licenses/>.  */
abe59f
+
abe59f
+#include <dlfcn.h>
abe59f
+#include <link.h>
abe59f
+#include <stdlib.h>
abe59f
+
abe59f
+unsigned int
abe59f
+la_version (unsigned int v)
abe59f
+{
abe59f
+  return LAV_CURRENT;
abe59f
+}
abe59f
+
abe59f
+static void
abe59f
+check (void)
abe59f
+{
abe59f
+  {
abe59f
+    void *mod = dlopen ("nonexistent.so", RTLD_NOW);
abe59f
+    if (mod != NULL)
abe59f
+      abort ();
abe59f
+  }
abe59f
+
abe59f
+  {
abe59f
+    void *mod = dlmopen (LM_ID_BASE, "nonexistent.so", RTLD_NOW);
abe59f
+    if (mod != NULL)
abe59f
+      abort ();
abe59f
+  }
abe59f
+}
abe59f
+
abe59f
+void
abe59f
+la_activity (uintptr_t *cookie, unsigned int flag)
abe59f
+{
abe59f
+  if (flag != LA_ACT_CONSISTENT)
abe59f
+    return;
abe59f
+  check ();
abe59f
+}
abe59f
+
abe59f
+void
abe59f
+la_preinit (uintptr_t *cookie)
abe59f
+{
abe59f
+  check ();
abe59f
+}