00c0d4
commit aee6e90f93e285016b6cd9c8bd00402c19ba271b
00c0d4
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
00c0d4
Date:   Mon Jul 19 15:47:51 2021 -0300
00c0d4
00c0d4
    elf: Add _dl_audit_objopen
00c0d4
    
00c0d4
    It consolidates the code required to call la_objopen audit callback.
00c0d4
    
00c0d4
    Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu.
00c0d4
    
00c0d4
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
00c0d4
00c0d4
Conflicts:
00c0d4
	elf/Makefile
00c0d4
00c0d4
diff --git a/elf/Makefile b/elf/Makefile
00c0d4
index 2312184692433313..08a32a712a34f2cc 100644
00c0d4
--- a/elf/Makefile
00c0d4
+++ b/elf/Makefile
00c0d4
@@ -64,7 +64,8 @@ elide-routines.os = $(all-dl-routines) dl-support enbl-secure dl-origin \
00c0d4
 # interpreter and operating independent of libc.
00c0d4
 rtld-routines	= rtld $(all-dl-routines) dl-sysdep dl-environ dl-minimal \
00c0d4
   dl-error-minimal dl-conflict dl-hwcaps dl-hwcaps_split dl-hwcaps-subdirs \
00c0d4
-  dl-usage dl-diagnostics dl-diagnostics-kernel dl-diagnostics-cpu
00c0d4
+  dl-usage dl-diagnostics dl-diagnostics-kernel dl-diagnostics-cpu \
00c0d4
+  dl-audit
00c0d4
 all-rtld-routines = $(rtld-routines) $(sysdep-rtld-routines)
00c0d4
 
00c0d4
 CFLAGS-dl-runtime.c += -fexceptions -fasynchronous-unwind-tables
00c0d4
diff --git a/elf/dl-audit.c b/elf/dl-audit.c
00c0d4
new file mode 100644
00c0d4
index 0000000000000000..4066dfe85146b9d4
00c0d4
--- /dev/null
00c0d4
+++ b/elf/dl-audit.c
00c0d4
@@ -0,0 +1,39 @@
00c0d4
+/* Audit common functions.
00c0d4
+   Copyright (C) 2021 Free Software Foundation, Inc.
00c0d4
+   This file is part of the GNU C Library.
00c0d4
+
00c0d4
+   The GNU C Library is free software; you can redistribute it and/or
00c0d4
+   modify it under the terms of the GNU Lesser General Public
00c0d4
+   License as published by the Free Software Foundation; either
00c0d4
+   version 2.1 of the License, or (at your option) any later version.
00c0d4
+
00c0d4
+   The GNU C Library is distributed in the hope that it will be useful,
00c0d4
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00c0d4
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00c0d4
+   Lesser General Public License for more details.
00c0d4
+
00c0d4
+   You should have received a copy of the GNU Lesser General Public
00c0d4
+   License along with the GNU C Library; if not, see
00c0d4
+   <https://www.gnu.org/licenses/>.  */
00c0d4
+
00c0d4
+#include <ldsodefs.h>
00c0d4
+
00c0d4
+void
00c0d4
+_dl_audit_objopen (struct link_map *l, Lmid_t nsid)
00c0d4
+{
00c0d4
+  if (__glibc_likely (GLRO(dl_naudit) == 0))
00c0d4
+    return;
00c0d4
+
00c0d4
+  struct audit_ifaces *afct = GLRO(dl_audit);
00c0d4
+  for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
00c0d4
+    {
00c0d4
+      if (afct->objopen != NULL)
00c0d4
+	{
00c0d4
+	  struct auditstate *state = link_map_audit_state (l, cnt);
00c0d4
+	  state->bindflags = afct->objopen (l, nsid, &state->cookie);
00c0d4
+	  l->l_audit_any_plt |= state->bindflags != 0;
00c0d4
+	}
00c0d4
+
00c0d4
+      afct = afct->next;
00c0d4
+   }
00c0d4
+}
00c0d4
diff --git a/elf/dl-load.c b/elf/dl-load.c
00c0d4
index de5aef5777045da5..c11b1d1781e9b40b 100644
00c0d4
--- a/elf/dl-load.c
00c0d4
+++ b/elf/dl-load.c
00c0d4
@@ -1436,22 +1436,8 @@ cannot enable executable stack as shared object requires");
00c0d4
 
00c0d4
 #ifdef SHARED
00c0d4
   /* Auditing checkpoint: we have a new object.  */
00c0d4
-  if (__glibc_unlikely (GLRO(dl_naudit) > 0)
00c0d4
-      && !GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
00c0d4
-    {
00c0d4
-      struct audit_ifaces *afct = GLRO(dl_audit);
00c0d4
-      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
00c0d4
-	{
00c0d4
-	  if (afct->objopen != NULL)
00c0d4
-	    {
00c0d4
-	      struct auditstate *state = link_map_audit_state (l, cnt);
00c0d4
-	      state->bindflags = afct->objopen (l, nsid, &state->cookie);
00c0d4
-	      l->l_audit_any_plt |= state->bindflags != 0;
00c0d4
-	    }
00c0d4
-
00c0d4
-	  afct = afct->next;
00c0d4
-	}
00c0d4
-    }
00c0d4
+  if (!GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
00c0d4
+    _dl_audit_objopen (l, nsid);
00c0d4
 #endif
00c0d4
 
00c0d4
   return l;
00c0d4
diff --git a/elf/rtld.c b/elf/rtld.c
00c0d4
index f3836b8a78faaf27..1982e42390760e0a 100644
00c0d4
--- a/elf/rtld.c
00c0d4
+++ b/elf/rtld.c
00c0d4
@@ -1075,25 +1075,6 @@ ERROR: audit interface '%s' requires version %d (maximum supported version %d);
00c0d4
   dlmargs.map->l_auditing = 1;
00c0d4
 }
00c0d4
 
00c0d4
-/* Notify the the audit modules that the object MAP has already been
00c0d4
-   loaded.  */
00c0d4
-static void
00c0d4
-notify_audit_modules_of_loaded_object (struct link_map *map)
00c0d4
-{
00c0d4
-  struct audit_ifaces *afct = GLRO(dl_audit);
00c0d4
-  for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
00c0d4
-    {
00c0d4
-      if (afct->objopen != NULL)
00c0d4
-	{
00c0d4
-	  struct auditstate *state = link_map_audit_state (map, cnt);
00c0d4
-	  state->bindflags = afct->objopen (map, LM_ID_BASE, &state->cookie);
00c0d4
-	  map->l_audit_any_plt |= state->bindflags != 0;
00c0d4
-	}
00c0d4
-
00c0d4
-      afct = afct->next;
00c0d4
-    }
00c0d4
-}
00c0d4
-
00c0d4
 /* Load all audit modules.  */
00c0d4
 static void
00c0d4
 load_audit_modules (struct link_map *main_map, struct audit_list *audit_list)
00c0d4
@@ -1112,8 +1093,8 @@ load_audit_modules (struct link_map *main_map, struct audit_list *audit_list)
00c0d4
      program and the dynamic linker itself).  */
00c0d4
   if (GLRO(dl_naudit) > 0)
00c0d4
     {
00c0d4
-      notify_audit_modules_of_loaded_object (main_map);
00c0d4
-      notify_audit_modules_of_loaded_object (&GL(dl_rtld_map));
00c0d4
+      _dl_audit_objopen (main_map, LM_ID_BASE);
00c0d4
+      _dl_audit_objopen (&GL(dl_rtld_map), LM_ID_BASE);
00c0d4
     }
00c0d4
 }
00c0d4
 
00c0d4
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
00c0d4
index 2dd6f0c3c4aaaef5..410f070e28b74bdf 100644
00c0d4
--- a/sysdeps/generic/ldsodefs.h
00c0d4
+++ b/sysdeps/generic/ldsodefs.h
00c0d4
@@ -1268,6 +1268,11 @@ link_map_audit_state (struct link_map *l, size_t index)
00c0d4
 {
00c0d4
   return &l->l_audit[index];
00c0d4
 }
00c0d4
+
00c0d4
+/* Call the la_objopen from the audit modules for the link_map L on the
00c0d4
+   namespace identification NSID.  */
00c0d4
+void _dl_audit_objopen (struct link_map *l, Lmid_t nsid)
00c0d4
+  attribute_hidden;
00c0d4
 #endif /* SHARED */
00c0d4
 
00c0d4
 __END_DECLS