08c3a6
commit 2255621f0e2cd07f7a6147928ce644e13526ffb6
08c3a6
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
08c3a6
Date:   Wed Jun 30 17:33:57 2021 -0300
08c3a6
08c3a6
    elf: Do not fail for failed dlmopen on audit modules (BZ #28061)
08c3a6
    
08c3a6
    The dl_main sets the LM_ID_BASE to RT_ADD just before starting to
08c3a6
    add load new shared objects.  The state is set to RT_CONSISTENT just
08c3a6
    after all objects are loaded.
08c3a6
    
08c3a6
    However if a audit modules tries to dlmopen an inexistent module,
08c3a6
    the _dl_open will assert that the namespace is in an inconsistent
08c3a6
    state.
08c3a6
    
08c3a6
    This is different than dlopen, since first it will not use
08c3a6
    LM_ID_BASE and second _dl_map_object_from_fd is the sole responsible
08c3a6
    to set and reset the r_state value.
08c3a6
    
08c3a6
    So the assert on _dl_open can not really be seen if the state is
08c3a6
    consistent, since _dt_main resets it.  This patch removes the assert.
08c3a6
    
08c3a6
    Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu.
08c3a6
    
08c3a6
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
08c3a6
    (cherry picked from commit 484e672ddabe0a919a692520e6ac8f2580866235)
08c3a6
    
08c3a6
    Resolved conflicts:
08c3a6
            elf/Makefile
08c3a6
            elf/dl-open.c
08c3a6
08c3a6
diff --git a/elf/Makefile b/elf/Makefile
08c3a6
index 465442bf59fa9794..91b2269257523a64 100644
08c3a6
--- a/elf/Makefile
08c3a6
+++ b/elf/Makefile
08c3a6
@@ -366,6 +366,7 @@ tests += \
08c3a6
   tst-audit17 \
08c3a6
   tst-audit18 \
08c3a6
   tst-audit19b \
08c3a6
+  tst-audit20 \
08c3a6
   tst-audit22 \
08c3a6
   tst-auditmany \
08c3a6
   tst-auxobj \
08c3a6
@@ -650,6 +651,7 @@ modules-names = \
08c3a6
   tst-auditmod18 \
08c3a6
   tst-auditmod19a \
08c3a6
   tst-auditmod19b \
08c3a6
+  tst-auditmod20 \
08c3a6
   tst-auditmod22 \
08c3a6
   tst-auxvalmod \
08c3a6
   tst-big-note-lib \
08c3a6
@@ -2037,6 +2039,9 @@ $(objpfx)tst-audit19b.out: $(objpfx)tst-auditmod19b.so
08c3a6
 $(objpfx)tst-audit19b: $(objpfx)tst-audit19bmod.so
08c3a6
 tst-audit19b-ARGS = -- $(host-test-program-cmd)
08c3a6
 
08c3a6
+$(objpfx)tst-audit20.out: $(objpfx)tst-auditmod20.so
08c3a6
+tst-audit20-ENV = LD_AUDIT=$(objpfx)tst-auditmod20.so
08c3a6
+
08c3a6
 $(objpfx)tst-audit22.out: $(objpfx)tst-auditmod22.so
08c3a6
 tst-audit22-ARGS = -- $(host-test-program-cmd)
08c3a6
 
08c3a6
diff --git a/elf/dl-open.c b/elf/dl-open.c
08c3a6
index 3f01aa480730da13..bc6872632880634e 100644
08c3a6
--- a/elf/dl-open.c
08c3a6
+++ b/elf/dl-open.c
08c3a6
@@ -914,8 +914,6 @@ no more namespaces available for dlmopen()"));
08c3a6
 	     the flag here.  */
08c3a6
 	}
08c3a6
 
08c3a6
-      assert (_dl_debug_initialize (0, args.nsid)->r_state == RT_CONSISTENT);
08c3a6
-
08c3a6
       /* Release the lock.  */
08c3a6
       __rtld_lock_unlock_recursive (GL(dl_load_lock));
08c3a6
 
08c3a6
diff --git a/elf/tst-audit20.c b/elf/tst-audit20.c
08c3a6
new file mode 100644
08c3a6
index 0000000000000000..6f39ccee865b012b
08c3a6
--- /dev/null
08c3a6
+++ b/elf/tst-audit20.c
08c3a6
@@ -0,0 +1,25 @@
08c3a6
+/* Check dlopen failure on audit modules.
08c3a6
+   Copyright (C) 2021 Free Software Foundation, Inc.
08c3a6
+   This file is part of the GNU C Library.
08c3a6
+
08c3a6
+   The GNU C Library is free software; you can redistribute it and/or
08c3a6
+   modify it under the terms of the GNU Lesser General Public
08c3a6
+   License as published by the Free Software Foundation; either
08c3a6
+   version 2.1 of the License, or (at your option) any later version.
08c3a6
+
08c3a6
+   The GNU C Library is distributed in the hope that it will be useful,
08c3a6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
08c3a6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
08c3a6
+   Lesser General Public License for more details.
08c3a6
+
08c3a6
+   You should have received a copy of the GNU Lesser General Public
08c3a6
+   License along with the GNU C Library; if not, see
08c3a6
+   <https://www.gnu.org/licenses/>.  */
08c3a6
+
08c3a6
+static int
08c3a6
+do_test (void)
08c3a6
+{
08c3a6
+  return 0;
08c3a6
+}
08c3a6
+
08c3a6
+#include <support/test-driver.c>
08c3a6
diff --git a/elf/tst-auditmod20.c b/elf/tst-auditmod20.c
08c3a6
new file mode 100644
08c3a6
index 0000000000000000..c57e50ee4e88dd6b
08c3a6
--- /dev/null
08c3a6
+++ b/elf/tst-auditmod20.c
08c3a6
@@ -0,0 +1,57 @@
08c3a6
+/* Check dlopen failure on audit modules.
08c3a6
+   Copyright (C) 2021 Free Software Foundation, Inc.
08c3a6
+   This file is part of the GNU C Library.
08c3a6
+
08c3a6
+   The GNU C Library is free software; you can redistribute it and/or
08c3a6
+   modify it under the terms of the GNU Lesser General Public
08c3a6
+   License as published by the Free Software Foundation; either
08c3a6
+   version 2.1 of the License, or (at your option) any later version.
08c3a6
+
08c3a6
+   The GNU C Library is distributed in the hope that it will be useful,
08c3a6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
08c3a6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
08c3a6
+   Lesser General Public License for more details.
08c3a6
+
08c3a6
+   You should have received a copy of the GNU Lesser General Public
08c3a6
+   License along with the GNU C Library; if not, see
08c3a6
+   <https://www.gnu.org/licenses/>.  */
08c3a6
+
08c3a6
+#include <dlfcn.h>
08c3a6
+#include <link.h>
08c3a6
+#include <stdlib.h>
08c3a6
+
08c3a6
+unsigned int
08c3a6
+la_version (unsigned int v)
08c3a6
+{
08c3a6
+  return LAV_CURRENT;
08c3a6
+}
08c3a6
+
08c3a6
+static void
08c3a6
+check (void)
08c3a6
+{
08c3a6
+  {
08c3a6
+    void *mod = dlopen ("nonexistent.so", RTLD_NOW);
08c3a6
+    if (mod != NULL)
08c3a6
+      abort ();
08c3a6
+  }
08c3a6
+
08c3a6
+  {
08c3a6
+    void *mod = dlmopen (LM_ID_BASE, "nonexistent.so", RTLD_NOW);
08c3a6
+    if (mod != NULL)
08c3a6
+      abort ();
08c3a6
+  }
08c3a6
+}
08c3a6
+
08c3a6
+void
08c3a6
+la_activity (uintptr_t *cookie, unsigned int flag)
08c3a6
+{
08c3a6
+  if (flag != LA_ACT_CONSISTENT)
08c3a6
+    return;
08c3a6
+  check ();
08c3a6
+}
08c3a6
+
08c3a6
+void
08c3a6
+la_preinit (uintptr_t *cookie)
08c3a6
+{
08c3a6
+  check ();
08c3a6
+}