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