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