c5d972
commit c91008d3490e4e3ce29520068405f081f0d368ca
c5d972
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
c5d972
Date:   Tue Jul 20 13:47:36 2021 -0300
c5d972
c5d972
    elf: Add _dl_audit_objsearch
c5d972
    
c5d972
    It consolidates the code required to call la_objsearch audit
c5d972
    callback.
c5d972
    
c5d972
    Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu.
c5d972
    
c5d972
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
c5d972
c5d972
diff --git a/elf/dl-audit.c b/elf/dl-audit.c
c5d972
index 74b87f4b39be75e1..5682427220569d90 100644
c5d972
--- a/elf/dl-audit.c
c5d972
+++ b/elf/dl-audit.c
c5d972
@@ -44,6 +44,28 @@ _dl_audit_activity_nsid (Lmid_t nsid, int action)
c5d972
   _dl_audit_activity_map (head, action);
c5d972
 }
c5d972
 
c5d972
+const char *
c5d972
+_dl_audit_objsearch (const char *name, struct link_map *l, unsigned int code)
c5d972
+{
c5d972
+  if (l == NULL || l->l_auditing || code == 0)
c5d972
+    return name;
c5d972
+
c5d972
+  struct audit_ifaces *afct = GLRO(dl_audit);
c5d972
+  for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
c5d972
+    {
c5d972
+      if (afct->objsearch != NULL)
c5d972
+	{
c5d972
+	  struct auditstate *state = link_map_audit_state (l, cnt);
c5d972
+	  name = afct->objsearch (name, &state->cookie, code);
c5d972
+	  if (name == NULL)
c5d972
+	    return NULL;
c5d972
+	}
c5d972
+      afct = afct->next;
c5d972
+   }
c5d972
+
c5d972
+  return name;
c5d972
+}
c5d972
+
c5d972
 void
c5d972
 _dl_audit_objopen (struct link_map *l, Lmid_t nsid)
c5d972
 {
c5d972
diff --git a/elf/dl-load.c b/elf/dl-load.c
c5d972
index 8a18c761bb753e37..1613217a236c7fc3 100644
c5d972
--- a/elf/dl-load.c
c5d972
+++ b/elf/dl-load.c
c5d972
@@ -1517,32 +1517,20 @@ open_verify (const char *name, int fd,
c5d972
 
c5d972
 #ifdef SHARED
c5d972
   /* Give the auditing libraries a chance.  */
c5d972
-  if (__glibc_unlikely (GLRO(dl_naudit) > 0) && whatcode != 0
c5d972
-      && loader->l_auditing == 0)
c5d972
+  if (__glibc_unlikely (GLRO(dl_naudit) > 0))
c5d972
     {
c5d972
       const char *original_name = name;
c5d972
-      struct audit_ifaces *afct = GLRO(dl_audit);
c5d972
-      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
c5d972
-	{
c5d972
-	  if (afct->objsearch != NULL)
c5d972
-	    {
c5d972
-	      struct auditstate *state = link_map_audit_state (loader, cnt);
c5d972
-	      name = afct->objsearch (name, &state->cookie, whatcode);
c5d972
-	      if (name == NULL)
c5d972
-		/* Ignore the path.  */
c5d972
-		return -1;
c5d972
-	    }
c5d972
-
c5d972
-	  afct = afct->next;
c5d972
-	}
c5d972
+      name = _dl_audit_objsearch (name, loader, whatcode);
c5d972
+      if (name == NULL)
c5d972
+	return -1;
c5d972
 
c5d972
       if (fd != -1 && name != original_name && strcmp (name, original_name))
c5d972
-        {
c5d972
-          /* An audit library changed what we're supposed to open,
c5d972
-             so FD no longer matches it.  */
c5d972
-          __close_nocancel (fd);
c5d972
-          fd = -1;
c5d972
-        }
c5d972
+	{
c5d972
+	  /* An audit library changed what we're supposed to open,
c5d972
+	     so FD no longer matches it.  */
c5d972
+	  __close_nocancel (fd);
c5d972
+	  fd = -1;
c5d972
+	}
c5d972
     }
c5d972
 #endif
c5d972
 
c5d972
@@ -1992,36 +1980,17 @@ _dl_map_object (struct link_map *loader, const char *name,
c5d972
 #ifdef SHARED
c5d972
   /* Give the auditing libraries a chance to change the name before we
c5d972
      try anything.  */
c5d972
-  if (__glibc_unlikely (GLRO(dl_naudit) > 0)
c5d972
-      && (loader == NULL || loader->l_auditing == 0))
c5d972
+  if (__glibc_unlikely (GLRO(dl_naudit) > 0))
c5d972
     {
c5d972
-      struct audit_ifaces *afct = GLRO(dl_audit);
c5d972
-      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
c5d972
+      const char *before = name;
c5d972
+      name = _dl_audit_objsearch (name, loader, LA_SER_ORIG);
c5d972
+      if (name == NULL)
c5d972
 	{
c5d972
-	  if (afct->objsearch != NULL)
c5d972
-	    {
c5d972
-	      const char *before = name;
c5d972
-	      struct auditstate *state = link_map_audit_state (loader, cnt);
c5d972
-	      name = afct->objsearch (name, &state->cookie, LA_SER_ORIG);
c5d972
-	      if (name == NULL)
c5d972
-		{
c5d972
-		  /* Do not try anything further.  */
c5d972
-		  fd = -1;
c5d972
-		  goto no_file;
c5d972
-		}
c5d972
-	      if (before != name && strcmp (before, name) != 0)
c5d972
-		{
c5d972
-		  if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
c5d972
-		    _dl_debug_printf ("audit changed filename %s -> %s\n",
c5d972
-				      before, name);
c5d972
-
c5d972
-		  if (origname == NULL)
c5d972
-		    origname = before;
c5d972
-		}
c5d972
-	    }
c5d972
-
c5d972
-	  afct = afct->next;
c5d972
+	  fd = -1;
c5d972
+	  goto no_file;
c5d972
 	}
c5d972
+      if (before != name && strcmp (before, name) != 0)
c5d972
+	origname = before;
c5d972
     }
c5d972
 #endif
c5d972
 
c5d972
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
c5d972
index 05737342d6287233..da83e717e8cd8e0b 100644
c5d972
--- a/sysdeps/generic/ldsodefs.h
c5d972
+++ b/sysdeps/generic/ldsodefs.h
c5d972
@@ -1269,6 +1269,13 @@ link_map_audit_state (struct link_map *l, size_t index)
c5d972
   return &l->l_audit[index];
c5d972
 }
c5d972
 
c5d972
+/* Call the la_objsearch from the audit modules from the link map L.  If
c5d972
+   ORIGNAME is non NULL, it is updated with the revious name prior calling
c5d972
+   la_objsearch.  */
c5d972
+const char *_dl_audit_objsearch (const char *name, struct link_map *l,
c5d972
+				 unsigned int code)
c5d972
+   attribute_hidden;
c5d972
+
c5d972
 /* Call the la_activity from the audit modules from the link map L and issues
c5d972
    the ACTION argument.  */
c5d972
 void _dl_audit_activity_map (struct link_map *l, int action)