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