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