08c3a6
commit 198660741b23ec9defb19e22951d4a721de603c8
08c3a6
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
08c3a6
Date:   Tue Jul 20 14:04:51 2021 -0300
08c3a6
08c3a6
    elf: Add _dl_audit_objclose
08c3a6
    
08c3a6
    It consolidates the code required to call la_objclose audit
08c3a6
    callback.
08c3a6
    
08c3a6
    Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu.
08c3a6
    
08c3a6
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
08c3a6
    (cherry picked from commit 311c9ee54ea963ff69bd3a2e6981c37e893b4c3e)
08c3a6
08c3a6
diff --git a/elf/dl-audit.c b/elf/dl-audit.c
08c3a6
index 5682427220569d90..cb1c3de93cba447b 100644
08c3a6
--- a/elf/dl-audit.c
08c3a6
+++ b/elf/dl-audit.c
08c3a6
@@ -85,3 +85,24 @@ _dl_audit_objopen (struct link_map *l, Lmid_t nsid)
08c3a6
       afct = afct->next;
08c3a6
    }
08c3a6
 }
08c3a6
+
08c3a6
+void
08c3a6
+_dl_audit_objclose (struct link_map *l)
08c3a6
+{
08c3a6
+  if (__glibc_likely (GLRO(dl_naudit) == 0)
08c3a6
+      || GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
08c3a6
+    return;
08c3a6
+
08c3a6
+  struct audit_ifaces *afct = GLRO(dl_audit);
08c3a6
+  for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
08c3a6
+    {
08c3a6
+      if (afct->objclose != NULL)
08c3a6
+	{
08c3a6
+	  struct auditstate *state= link_map_audit_state (l, cnt);
08c3a6
+	  /* Return value is ignored.  */
08c3a6
+	  afct->objclose (&state->cookie);
08c3a6
+	}
08c3a6
+
08c3a6
+      afct = afct->next;
08c3a6
+    }
08c3a6
+}
08c3a6
diff --git a/elf/dl-close.c b/elf/dl-close.c
08c3a6
index 5a8cc9e7cb5186cc..985cd4e2821436af 100644
08c3a6
--- a/elf/dl-close.c
08c3a6
+++ b/elf/dl-close.c
08c3a6
@@ -260,9 +260,6 @@ _dl_close_worker (struct link_map *map, bool force)
08c3a6
   _dl_sort_maps (maps, nloaded, (nsid == LM_ID_BASE), true);
08c3a6
 
08c3a6
   /* Call all termination functions at once.  */
08c3a6
-#ifdef SHARED
08c3a6
-  bool do_audit = GLRO(dl_naudit) > 0 && !ns->_ns_loaded->l_auditing;
08c3a6
-#endif
08c3a6
   bool unload_any = false;
08c3a6
   bool scope_mem_left = false;
08c3a6
   unsigned int unload_global = 0;
08c3a6
@@ -296,22 +293,7 @@ _dl_close_worker (struct link_map *map, bool force)
08c3a6
 
08c3a6
 #ifdef SHARED
08c3a6
 	  /* Auditing checkpoint: we remove an object.  */
08c3a6
-	  if (__glibc_unlikely (do_audit))
08c3a6
-	    {
08c3a6
-	      struct audit_ifaces *afct = GLRO(dl_audit);
08c3a6
-	      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
08c3a6
-		{
08c3a6
-		  if (afct->objclose != NULL)
08c3a6
-		    {
08c3a6
-		      struct auditstate *state
08c3a6
-			= link_map_audit_state (imap, cnt);
08c3a6
-		      /* Return value is ignored.  */
08c3a6
-		      (void) afct->objclose (&state->cookie);
08c3a6
-		    }
08c3a6
-
08c3a6
-		  afct = afct->next;
08c3a6
-		}
08c3a6
-	    }
08c3a6
+	  _dl_audit_objclose (imap);
08c3a6
 #endif
08c3a6
 
08c3a6
 	  /* This object must not be used anymore.  */
08c3a6
diff --git a/elf/dl-fini.c b/elf/dl-fini.c
08c3a6
index c683884c355dfd52..b789cfb9f2ac6c85 100644
08c3a6
--- a/elf/dl-fini.c
08c3a6
+++ b/elf/dl-fini.c
08c3a6
@@ -146,21 +146,7 @@ _dl_fini (void)
08c3a6
 
08c3a6
 #ifdef SHARED
08c3a6
 		  /* Auditing checkpoint: another object closed.  */
08c3a6
-		  if (!do_audit && __builtin_expect (GLRO(dl_naudit) > 0, 0))
08c3a6
-		    {
08c3a6
-		      struct audit_ifaces *afct = GLRO(dl_audit);
08c3a6
-		      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
08c3a6
-			{
08c3a6
-			  if (afct->objclose != NULL)
08c3a6
-			    {
08c3a6
-			      struct auditstate *state
08c3a6
-				= link_map_audit_state (l, cnt);
08c3a6
-			      /* Return value is ignored.  */
08c3a6
-			      (void) afct->objclose (&state->cookie);
08c3a6
-			    }
08c3a6
-			  afct = afct->next;
08c3a6
-			}
08c3a6
-		    }
08c3a6
+		  _dl_audit_objclose (l);
08c3a6
 #endif
08c3a6
 		}
08c3a6
 
08c3a6
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
08c3a6
index 1f212a18d7bfc440..982f23c0287955fe 100644
08c3a6
--- a/sysdeps/generic/ldsodefs.h
08c3a6
+++ b/sysdeps/generic/ldsodefs.h
08c3a6
@@ -1394,6 +1394,10 @@ void _dl_audit_activity_nsid (Lmid_t nsid, int action)
08c3a6
    namespace identification NSID.  */
08c3a6
 void _dl_audit_objopen (struct link_map *l, Lmid_t nsid)
08c3a6
   attribute_hidden;
08c3a6
+
08c3a6
+/* Call the la_objclose from the audit modules for the link_map L.  */
08c3a6
+void _dl_audit_objclose (struct link_map *l)
08c3a6
+  attribute_hidden;
08c3a6
 #endif /* SHARED */
08c3a6
 
08c3a6
 #if PTHREAD_IN_LIBC && defined SHARED