076f82
commit ce0cb6d1d2daac2d58006a41c3d19c551b86f255
076f82
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
076f82
Date:   Mon Jul 19 15:47:51 2021 -0300
076f82
076f82
    elf: Add _dl_audit_objopen
076f82
    
076f82
    It consolidates the code required to call la_objopen audit callback.
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 aee6e90f93e285016b6cd9c8bd00402c19ba271b)
076f82
    
076f82
    Resolved conflicts:
076f82
            elf/Makefile
076f82
076f82
diff --git a/elf/Makefile b/elf/Makefile
076f82
index bf6da98bdd15a18d..85165c0591412a45 100644
076f82
--- a/elf/Makefile
076f82
+++ b/elf/Makefile
076f82
@@ -118,6 +118,7 @@ elide-routines.os = \
076f82
 # interpreter and operating independent of libc.
076f82
 rtld-routines = \
076f82
   $(all-dl-routines) \
076f82
+  dl-audit \
076f82
   dl-compat \
076f82
   dl-conflict \
076f82
   dl-diagnostics \
076f82
diff --git a/elf/dl-audit.c b/elf/dl-audit.c
076f82
new file mode 100644
076f82
index 0000000000000000..4066dfe85146b9d4
076f82
--- /dev/null
076f82
+++ b/elf/dl-audit.c
076f82
@@ -0,0 +1,39 @@
076f82
+/* Audit common functions.
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 <ldsodefs.h>
076f82
+
076f82
+void
076f82
+_dl_audit_objopen (struct link_map *l, Lmid_t nsid)
076f82
+{
076f82
+  if (__glibc_likely (GLRO(dl_naudit) == 0))
076f82
+    return;
076f82
+
076f82
+  struct audit_ifaces *afct = GLRO(dl_audit);
076f82
+  for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
076f82
+    {
076f82
+      if (afct->objopen != NULL)
076f82
+	{
076f82
+	  struct auditstate *state = link_map_audit_state (l, cnt);
076f82
+	  state->bindflags = afct->objopen (l, nsid, &state->cookie);
076f82
+	  l->l_audit_any_plt |= state->bindflags != 0;
076f82
+	}
076f82
+
076f82
+      afct = afct->next;
076f82
+   }
076f82
+}
076f82
diff --git a/elf/dl-load.c b/elf/dl-load.c
076f82
index a8c6df3959f2b331..a2d73d025c65cd79 100644
076f82
--- a/elf/dl-load.c
076f82
+++ b/elf/dl-load.c
076f82
@@ -1515,22 +1515,8 @@ cannot enable executable stack as shared object requires");
076f82
 
076f82
 #ifdef SHARED
076f82
   /* Auditing checkpoint: we have a new object.  */
076f82
-  if (__glibc_unlikely (GLRO(dl_naudit) > 0)
076f82
-      && !GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
076f82
-    {
076f82
-      struct audit_ifaces *afct = GLRO(dl_audit);
076f82
-      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
076f82
-	{
076f82
-	  if (afct->objopen != NULL)
076f82
-	    {
076f82
-	      struct auditstate *state = link_map_audit_state (l, cnt);
076f82
-	      state->bindflags = afct->objopen (l, nsid, &state->cookie);
076f82
-	      l->l_audit_any_plt |= state->bindflags != 0;
076f82
-	    }
076f82
-
076f82
-	  afct = afct->next;
076f82
-	}
076f82
-    }
076f82
+  if (!GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
076f82
+    _dl_audit_objopen (l, nsid);
076f82
 #endif
076f82
 
076f82
   return l;
076f82
diff --git a/elf/rtld.c b/elf/rtld.c
076f82
index ad5ddb2a0ab94e7f..45fec0df3043b90a 100644
076f82
--- a/elf/rtld.c
076f82
+++ b/elf/rtld.c
076f82
@@ -1064,25 +1064,6 @@ ERROR: audit interface '%s' requires version %d (maximum supported version %d);
076f82
   dlmargs.map->l_auditing = 1;
076f82
 }
076f82
 
076f82
-/* Notify the the audit modules that the object MAP has already been
076f82
-   loaded.  */
076f82
-static void
076f82
-notify_audit_modules_of_loaded_object (struct link_map *map)
076f82
-{
076f82
-  struct audit_ifaces *afct = GLRO(dl_audit);
076f82
-  for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
076f82
-    {
076f82
-      if (afct->objopen != NULL)
076f82
-	{
076f82
-	  struct auditstate *state = link_map_audit_state (map, cnt);
076f82
-	  state->bindflags = afct->objopen (map, LM_ID_BASE, &state->cookie);
076f82
-	  map->l_audit_any_plt |= state->bindflags != 0;
076f82
-	}
076f82
-
076f82
-      afct = afct->next;
076f82
-    }
076f82
-}
076f82
-
076f82
 /* Load all audit modules.  */
076f82
 static void
076f82
 load_audit_modules (struct link_map *main_map, struct audit_list *audit_list)
076f82
@@ -1101,8 +1082,8 @@ load_audit_modules (struct link_map *main_map, struct audit_list *audit_list)
076f82
      program and the dynamic linker itself).  */
076f82
   if (GLRO(dl_naudit) > 0)
076f82
     {
076f82
-      notify_audit_modules_of_loaded_object (main_map);
076f82
-      notify_audit_modules_of_loaded_object (&GL(dl_rtld_map));
076f82
+      _dl_audit_objopen (main_map, LM_ID_BASE);
076f82
+      _dl_audit_objopen (&GL(dl_rtld_map), LM_ID_BASE);
076f82
     }
076f82
 }
076f82
 
076f82
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
076f82
index bcf1f199c5985c65..5709e4e48dff4355 100644
076f82
--- a/sysdeps/generic/ldsodefs.h
076f82
+++ b/sysdeps/generic/ldsodefs.h
076f82
@@ -1372,6 +1372,11 @@ link_map_audit_state (struct link_map *l, size_t index)
076f82
       return &base[index];
076f82
     }
076f82
 }
076f82
+
076f82
+/* Call the la_objopen from the audit modules for the link_map L on the
076f82
+   namespace identification NSID.  */
076f82
+void _dl_audit_objopen (struct link_map *l, Lmid_t nsid)
076f82
+  attribute_hidden;
076f82
 #endif /* SHARED */
076f82
 
076f82
 #if PTHREAD_IN_LIBC && defined SHARED