076f82
commit fd9c4e8a1b72fa1372855051217f9480680d882a
076f82
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
076f82
Date:   Thu Jul 22 17:45:33 2021 -0300
076f82
076f82
    elf: Add _dl_audit_pltenter
076f82
    
076f82
    It consolidates the code required to call la_pltenter 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 eff687e8462b0eaf65992a6031b54a4b1cd16796)
076f82
076f82
diff --git a/elf/dl-audit.c b/elf/dl-audit.c
076f82
index 0b6fac8e48877c93..15250c67e8ac1658 100644
076f82
--- a/elf/dl-audit.c
076f82
+++ b/elf/dl-audit.c
076f82
@@ -17,7 +17,9 @@
076f82
    <https://www.gnu.org/licenses/>.  */
076f82
 
076f82
 #include <assert.h>
076f82
+#include <link.h>
076f82
 #include <ldsodefs.h>
076f82
+#include <dl-machine.h>
076f82
 
076f82
 void
076f82
 _dl_audit_activity_map (struct link_map *l, int action)
076f82
@@ -243,3 +245,78 @@ _dl_audit_symbind (struct link_map *l, struct reloc_result *reloc_result,
076f82
   reloc_result->flags = flags;
076f82
   *value = DL_FIXUP_ADDR_VALUE (sym.st_value);
076f82
 }
076f82
+
076f82
+void
076f82
+_dl_audit_pltenter (struct link_map *l, struct reloc_result *reloc_result,
076f82
+		    DL_FIXUP_VALUE_TYPE *value, void *regs, long int *framesize)
076f82
+{
076f82
+  /* Don't do anything if no auditor wants to intercept this call.  */
076f82
+  if (GLRO(dl_naudit) == 0
076f82
+      || (reloc_result->enterexit & LA_SYMB_NOPLTENTER))
076f82
+    return;
076f82
+
076f82
+  /* Sanity check:  DL_FIXUP_VALUE_CODE_ADDR (value) should have been
076f82
+     initialized earlier in this function or in another thread.  */
076f82
+  assert (DL_FIXUP_VALUE_CODE_ADDR (*value) != 0);
076f82
+  ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
076f82
+					    l_info[DT_SYMTAB])
076f82
+		       + reloc_result->boundndx);
076f82
+
076f82
+  /* Set up the sym parameter.  */
076f82
+  ElfW(Sym) sym = *defsym;
076f82
+  sym.st_value = DL_FIXUP_VALUE_ADDR (*value);
076f82
+
076f82
+  /* Get the symbol name.  */
076f82
+  const char *strtab = (const void *) D_PTR (reloc_result->bound,
076f82
+					     l_info[DT_STRTAB]);
076f82
+  const char *symname = strtab + sym.st_name;
076f82
+
076f82
+  /* Keep track of overwritten addresses.  */
076f82
+  unsigned int flags = reloc_result->flags;
076f82
+
076f82
+  struct audit_ifaces *afct = GLRO(dl_audit);
076f82
+  for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
076f82
+    {
076f82
+      if (afct->ARCH_LA_PLTENTER != NULL
076f82
+	  && (reloc_result->enterexit
076f82
+	      & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0)
076f82
+	{
076f82
+	  long int new_framesize = -1;
076f82
+	  struct auditstate *l_state = link_map_audit_state (l, cnt);
076f82
+	  struct auditstate *bound_state
076f82
+	    = link_map_audit_state (reloc_result->bound, cnt);
076f82
+	  uintptr_t new_value
076f82
+	    = afct->ARCH_LA_PLTENTER (&sym, reloc_result->boundndx,
076f82
+				      &l_state->cookie, &bound_state->cookie,
076f82
+				      regs, &flags, symname, &new_framesize);
076f82
+	  if (new_value != (uintptr_t) sym.st_value)
076f82
+	    {
076f82
+	      flags |= LA_SYMB_ALTVALUE;
076f82
+	      sym.st_value = new_value;
076f82
+	    }
076f82
+
076f82
+	  /* Remember the results for every audit library and store a summary
076f82
+	     in the first two bits.  */
076f82
+	  reloc_result->enterexit |= ((flags & (LA_SYMB_NOPLTENTER
076f82
+						| LA_SYMB_NOPLTEXIT))
076f82
+				      << (2 * (cnt + 1)));
076f82
+
076f82
+	  if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT
076f82
+					  << (2 * (cnt + 1))))
076f82
+	      == 0 && new_framesize != -1 && *framesize != -2)
076f82
+	    {
076f82
+	      /* If this is the first call providing information, use it.  */
076f82
+	      if (*framesize == -1)
076f82
+		*framesize = new_framesize;
076f82
+	      /* If two pltenter calls provide conflicting information, use
076f82
+		 the larger value.  */
076f82
+	      else if (new_framesize != *framesize)
076f82
+		*framesize = MAX (new_framesize, *framesize);
076f82
+	    }
076f82
+	}
076f82
+
076f82
+      afct = afct->next;
076f82
+    }
076f82
+
076f82
+  *value = DL_FIXUP_ADDR_VALUE (sym.st_value);
076f82
+}
076f82
diff --git a/elf/dl-runtime.c b/elf/dl-runtime.c
076f82
index c4413c9165cec8cb..dfedeaf2dd1c7253 100644
076f82
--- a/elf/dl-runtime.c
076f82
+++ b/elf/dl-runtime.c
076f82
@@ -320,78 +320,7 @@ _dl_profile_fixup (
076f82
 #ifdef SHARED
076f82
   /* Auditing checkpoint: report the PLT entering and allow the
076f82
      auditors to change the value.  */
076f82
-  if (GLRO(dl_naudit) > 0
076f82
-      /* Don't do anything if no auditor wants to intercept this call.  */
076f82
-      && (reloc_result->enterexit & LA_SYMB_NOPLTENTER) == 0)
076f82
-    {
076f82
-      /* Sanity check:  DL_FIXUP_VALUE_CODE_ADDR (value) should have been
076f82
-	 initialized earlier in this function or in another thread.  */
076f82
-      assert (DL_FIXUP_VALUE_CODE_ADDR (value) != 0);
076f82
-      ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
076f82
-						l_info[DT_SYMTAB])
076f82
-			   + reloc_result->boundndx);
076f82
-
076f82
-      /* Set up the sym parameter.  */
076f82
-      ElfW(Sym) sym = *defsym;
076f82
-      sym.st_value = DL_FIXUP_VALUE_ADDR (value);
076f82
-
076f82
-      /* Get the symbol name.  */
076f82
-      const char *strtab = (const void *) D_PTR (reloc_result->bound,
076f82
-						 l_info[DT_STRTAB]);
076f82
-      const char *symname = strtab + sym.st_name;
076f82
-
076f82
-      /* Keep track of overwritten addresses.  */
076f82
-      unsigned int flags = reloc_result->flags;
076f82
-
076f82
-      struct audit_ifaces *afct = GLRO(dl_audit);
076f82
-      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
076f82
-	{
076f82
-	  if (afct->ARCH_LA_PLTENTER != NULL
076f82
-	      && (reloc_result->enterexit
076f82
-		  & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0)
076f82
-	    {
076f82
-	      long int new_framesize = -1;
076f82
-	      struct auditstate *l_state = link_map_audit_state (l, cnt);
076f82
-	      struct auditstate *bound_state
076f82
-		= link_map_audit_state (reloc_result->bound, cnt);
076f82
-	      uintptr_t new_value
076f82
-		= afct->ARCH_LA_PLTENTER (&sym, reloc_result->boundndx,
076f82
-					  &l_state->cookie,
076f82
-					  &bound_state->cookie,
076f82
-					  regs, &flags, symname,
076f82
-					  &new_framesize);
076f82
-	      if (new_value != (uintptr_t) sym.st_value)
076f82
-		{
076f82
-		  flags |= LA_SYMB_ALTVALUE;
076f82
-		  sym.st_value = new_value;
076f82
-		}
076f82
-
076f82
-	      /* Remember the results for every audit library and
076f82
-		 store a summary in the first two bits.  */
076f82
-	      reloc_result->enterexit
076f82
-		|= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
076f82
-		    << (2 * (cnt + 1)));
076f82
-
076f82
-	      if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT
076f82
-					      << (2 * (cnt + 1))))
076f82
-		  == 0 && new_framesize != -1 && framesize != -2)
076f82
-		{
076f82
-		  /* If this is the first call providing information,
076f82
-		     use it.  */
076f82
-		  if (framesize == -1)
076f82
-		    framesize = new_framesize;
076f82
-		  /* If two pltenter calls provide conflicting information,
076f82
-		     use the larger value.  */
076f82
-		  else if (new_framesize != framesize)
076f82
-		    framesize = MAX (new_framesize, framesize);
076f82
-		}
076f82
-	    }
076f82
-
076f82
-	  afct = afct->next;
076f82
-	}
076f82
-
076f82
-      value = DL_FIXUP_ADDR_VALUE (sym.st_value);
076f82
-    }
076f82
+  _dl_audit_pltenter (l, reloc_result, &value, regs, &framesize);
076f82
 #endif
076f82
 
076f82
   /* Store the frame size information.  */
076f82
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
076f82
index 91193a036fc5c6ef..ea187dd266f14e06 100644
076f82
--- a/sysdeps/generic/ldsodefs.h
076f82
+++ b/sysdeps/generic/ldsodefs.h
076f82
@@ -1411,6 +1411,10 @@ void _dl_audit_symbind (struct link_map *l, struct reloc_result *reloc_result,
076f82
 void _dl_audit_symbind_alt (struct link_map *l, const ElfW(Sym) *ref,
076f82
 			    void **value, lookup_t result);
076f82
 rtld_hidden_proto (_dl_audit_symbind_alt)
076f82
+void _dl_audit_pltenter (struct link_map *l, struct reloc_result *reloc_result,
076f82
+			 DL_FIXUP_VALUE_TYPE *value, void *regs,
076f82
+			 long int *framesize)
076f82
+  attribute_hidden;
076f82
 #endif /* SHARED */
076f82
 
076f82
 #if PTHREAD_IN_LIBC && defined SHARED