abe59f
commit aee6e90f93e285016b6cd9c8bd00402c19ba271b
abe59f
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
abe59f
Date:   Mon Jul 19 15:47:51 2021 -0300
abe59f
abe59f
    elf: Add _dl_audit_objopen
abe59f
    
abe59f
    It consolidates the code required to call la_objopen audit callback.
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/Makefile
abe59f
abe59f
diff --git a/elf/Makefile b/elf/Makefile
abe59f
index 2312184692433313..08a32a712a34f2cc 100644
abe59f
--- a/elf/Makefile
abe59f
+++ b/elf/Makefile
abe59f
@@ -64,7 +64,8 @@ elide-routines.os = $(all-dl-routines) dl-support enbl-secure dl-origin \
abe59f
 # interpreter and operating independent of libc.
abe59f
 rtld-routines	= rtld $(all-dl-routines) dl-sysdep dl-environ dl-minimal \
abe59f
   dl-error-minimal dl-conflict dl-hwcaps dl-hwcaps_split dl-hwcaps-subdirs \
abe59f
-  dl-usage dl-diagnostics dl-diagnostics-kernel dl-diagnostics-cpu
abe59f
+  dl-usage dl-diagnostics dl-diagnostics-kernel dl-diagnostics-cpu \
abe59f
+  dl-audit
abe59f
 all-rtld-routines = $(rtld-routines) $(sysdep-rtld-routines)
abe59f
 
abe59f
 CFLAGS-dl-runtime.c += -fexceptions -fasynchronous-unwind-tables
abe59f
diff --git a/elf/dl-audit.c b/elf/dl-audit.c
abe59f
new file mode 100644
abe59f
index 0000000000000000..4066dfe85146b9d4
abe59f
--- /dev/null
abe59f
+++ b/elf/dl-audit.c
abe59f
@@ -0,0 +1,39 @@
abe59f
+/* Audit common functions.
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 <ldsodefs.h>
abe59f
+
abe59f
+void
abe59f
+_dl_audit_objopen (struct link_map *l, Lmid_t nsid)
abe59f
+{
abe59f
+  if (__glibc_likely (GLRO(dl_naudit) == 0))
abe59f
+    return;
abe59f
+
abe59f
+  struct audit_ifaces *afct = GLRO(dl_audit);
abe59f
+  for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
abe59f
+    {
abe59f
+      if (afct->objopen != NULL)
abe59f
+	{
abe59f
+	  struct auditstate *state = link_map_audit_state (l, cnt);
abe59f
+	  state->bindflags = afct->objopen (l, nsid, &state->cookie);
abe59f
+	  l->l_audit_any_plt |= state->bindflags != 0;
abe59f
+	}
abe59f
+
abe59f
+      afct = afct->next;
abe59f
+   }
abe59f
+}
abe59f
diff --git a/elf/dl-load.c b/elf/dl-load.c
abe59f
index de5aef5777045da5..c11b1d1781e9b40b 100644
abe59f
--- a/elf/dl-load.c
abe59f
+++ b/elf/dl-load.c
abe59f
@@ -1436,22 +1436,8 @@ cannot enable executable stack as shared object requires");
abe59f
 
abe59f
 #ifdef SHARED
abe59f
   /* Auditing checkpoint: we have a new object.  */
abe59f
-  if (__glibc_unlikely (GLRO(dl_naudit) > 0)
abe59f
-      && !GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
abe59f
-    {
abe59f
-      struct audit_ifaces *afct = GLRO(dl_audit);
abe59f
-      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
abe59f
-	{
abe59f
-	  if (afct->objopen != NULL)
abe59f
-	    {
abe59f
-	      struct auditstate *state = link_map_audit_state (l, cnt);
abe59f
-	      state->bindflags = afct->objopen (l, nsid, &state->cookie);
abe59f
-	      l->l_audit_any_plt |= state->bindflags != 0;
abe59f
-	    }
abe59f
-
abe59f
-	  afct = afct->next;
abe59f
-	}
abe59f
-    }
abe59f
+  if (!GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
abe59f
+    _dl_audit_objopen (l, nsid);
abe59f
 #endif
abe59f
 
abe59f
   return l;
abe59f
diff --git a/elf/rtld.c b/elf/rtld.c
abe59f
index f3836b8a78faaf27..1982e42390760e0a 100644
abe59f
--- a/elf/rtld.c
abe59f
+++ b/elf/rtld.c
abe59f
@@ -1075,25 +1075,6 @@ ERROR: audit interface '%s' requires version %d (maximum supported version %d);
abe59f
   dlmargs.map->l_auditing = 1;
abe59f
 }
abe59f
 
abe59f
-/* Notify the the audit modules that the object MAP has already been
abe59f
-   loaded.  */
abe59f
-static void
abe59f
-notify_audit_modules_of_loaded_object (struct link_map *map)
abe59f
-{
abe59f
-  struct audit_ifaces *afct = GLRO(dl_audit);
abe59f
-  for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
abe59f
-    {
abe59f
-      if (afct->objopen != NULL)
abe59f
-	{
abe59f
-	  struct auditstate *state = link_map_audit_state (map, cnt);
abe59f
-	  state->bindflags = afct->objopen (map, LM_ID_BASE, &state->cookie);
abe59f
-	  map->l_audit_any_plt |= state->bindflags != 0;
abe59f
-	}
abe59f
-
abe59f
-      afct = afct->next;
abe59f
-    }
abe59f
-}
abe59f
-
abe59f
 /* Load all audit modules.  */
abe59f
 static void
abe59f
 load_audit_modules (struct link_map *main_map, struct audit_list *audit_list)
abe59f
@@ -1112,8 +1093,8 @@ load_audit_modules (struct link_map *main_map, struct audit_list *audit_list)
abe59f
      program and the dynamic linker itself).  */
abe59f
   if (GLRO(dl_naudit) > 0)
abe59f
     {
abe59f
-      notify_audit_modules_of_loaded_object (main_map);
abe59f
-      notify_audit_modules_of_loaded_object (&GL(dl_rtld_map));
abe59f
+      _dl_audit_objopen (main_map, LM_ID_BASE);
abe59f
+      _dl_audit_objopen (&GL(dl_rtld_map), LM_ID_BASE);
abe59f
     }
abe59f
 }
abe59f
 
abe59f
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
abe59f
index 2dd6f0c3c4aaaef5..410f070e28b74bdf 100644
abe59f
--- a/sysdeps/generic/ldsodefs.h
abe59f
+++ b/sysdeps/generic/ldsodefs.h
abe59f
@@ -1268,6 +1268,11 @@ link_map_audit_state (struct link_map *l, size_t index)
abe59f
 {
abe59f
   return &l->l_audit[index];
abe59f
 }
abe59f
+
abe59f
+/* Call the la_objopen from the audit modules for the link_map L on the
abe59f
+   namespace identification NSID.  */
abe59f
+void _dl_audit_objopen (struct link_map *l, Lmid_t nsid)
abe59f
+  attribute_hidden;
abe59f
 #endif /* SHARED */
abe59f
 
abe59f
 __END_DECLS