[v4,12/21] elf: Add _dl_audit_pltenter

Message ID 20211014174752.1651347-13-adhemerval.zanella@linaro.org
State Superseded
Headers
Series Some rtld-audit fixes |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent

Commit Message

Adhemerval Zanella Oct. 14, 2021, 5:47 p.m. UTC
  It consolidates the code required to call la_pltenter() audit
callback.

Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu.
---
 elf/dl-audit.c             | 77 ++++++++++++++++++++++++++++++++++++++
 elf/dl-runtime.c           | 73 +-----------------------------------
 sysdeps/generic/ldsodefs.h |  3 ++
 3 files changed, 81 insertions(+), 72 deletions(-)
  

Patch

diff --git a/elf/dl-audit.c b/elf/dl-audit.c
index a968cbde16..7d410bc128 100644
--- a/elf/dl-audit.c
+++ b/elf/dl-audit.c
@@ -17,7 +17,9 @@ 
    <https://www.gnu.org/licenses/>.  */
 
 #include <assert.h>
+#include <link.h>
 #include <ldsodefs.h>
+#include <dl-machine.h>
 
 #ifdef SHARED
 void
@@ -257,4 +259,79 @@  _dl_audit_symbind (struct link_map *l, struct reloc_result *reloc_result,
   reloc_result->flags = flags;
   *value = DL_FIXUP_ADDR_VALUE (sym.st_value);
 }
+
+void
+_dl_audit_pltenter (struct link_map *l, struct reloc_result *reloc_result,
+		    DL_FIXUP_VALUE_TYPE *value, void *regs, long int *framesize)
+{
+  /* Don't do anything if no auditor wants to intercept this call.  */
+  if (GLRO(dl_naudit) == 0
+      || (reloc_result->enterexit & LA_SYMB_NOPLTENTER))
+    return;
+
+  /* Sanity check:  DL_FIXUP_VALUE_CODE_ADDR (value) should have been
+     initialized earlier in this function or in another thread.  */
+  assert (DL_FIXUP_VALUE_CODE_ADDR (*value) != 0);
+  ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
+					    l_info[DT_SYMTAB])
+		       + reloc_result->boundndx);
+
+  /* Set up the sym parameter.  */
+  ElfW(Sym) sym = *defsym;
+  sym.st_value = DL_FIXUP_VALUE_ADDR (*value);
+
+  /* Get the symbol name.  */
+  const char *strtab = (const void *) D_PTR (reloc_result->bound,
+					     l_info[DT_STRTAB]);
+  const char *symname = strtab + sym.st_name;
+
+  /* Keep track of overwritten addresses.  */
+  unsigned int flags = reloc_result->flags;
+
+  struct audit_ifaces *afct = GLRO(dl_audit);
+  for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
+    {
+      if (afct->ARCH_LA_PLTENTER != NULL
+	  && (reloc_result->enterexit
+	      & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0)
+	{
+	  long int new_framesize = -1;
+	  struct auditstate *l_state = link_map_audit_state (l, cnt);
+	  struct auditstate *bound_state
+	    = link_map_audit_state (reloc_result->bound, cnt);
+	  uintptr_t new_value
+	    = afct->ARCH_LA_PLTENTER (&sym, reloc_result->boundndx,
+				      &l_state->cookie, &bound_state->cookie,
+				      regs, &flags, symname, &new_framesize);
+	  if (new_value != (uintptr_t) sym.st_value)
+	    {
+	      flags |= LA_SYMB_ALTVALUE;
+	      sym.st_value = new_value;
+	    }
+
+	  /* Remember the results for every audit library and store a summary
+	     in the first two bits.  */
+	  reloc_result->enterexit |= ((flags & (LA_SYMB_NOPLTENTER
+						| LA_SYMB_NOPLTEXIT))
+				      << (2 * (cnt + 1)));
+
+	  if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT
+					  << (2 * (cnt + 1))))
+	      == 0 && new_framesize != -1 && *framesize != -2)
+	    {
+	      /* If this is the first call providing information, use it.  */
+	      if (*framesize == -1)
+		*framesize = new_framesize;
+	      /* If two pltenter calls provide conflicting information, use
+		 the larger value.  */
+	      else if (new_framesize != *framesize)
+		*framesize = MAX (new_framesize, *framesize);
+	    }
+	}
+
+      afct = afct->next;
+    }
+
+  *value = DL_FIXUP_ADDR_VALUE (sym.st_value);
+}
 #endif
diff --git a/elf/dl-runtime.c b/elf/dl-runtime.c
index bfbe8f4be4..c3fc6777ff 100644
--- a/elf/dl-runtime.c
+++ b/elf/dl-runtime.c
@@ -352,78 +352,7 @@  _dl_profile_fixup (
 #ifdef SHARED
   /* Auditing checkpoint: report the PLT entering and allow the
      auditors to change the value.  */
-  if (GLRO(dl_naudit) > 0
-      /* Don't do anything if no auditor wants to intercept this call.  */
-      && (reloc_result->enterexit & LA_SYMB_NOPLTENTER) == 0)
-    {
-      /* Sanity check:  DL_FIXUP_VALUE_CODE_ADDR (value) should have been
-	 initialized earlier in this function or in another thread.  */
-      assert (DL_FIXUP_VALUE_CODE_ADDR (value) != 0);
-      ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
-						l_info[DT_SYMTAB])
-			   + reloc_result->boundndx);
-
-      /* Set up the sym parameter.  */
-      ElfW(Sym) sym = *defsym;
-      sym.st_value = DL_FIXUP_VALUE_ADDR (value);
-
-      /* Get the symbol name.  */
-      const char *strtab = (const void *) D_PTR (reloc_result->bound,
-						 l_info[DT_STRTAB]);
-      const char *symname = strtab + sym.st_name;
-
-      /* Keep track of overwritten addresses.  */
-      unsigned int flags = reloc_result->flags;
-
-      struct audit_ifaces *afct = GLRO(dl_audit);
-      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
-	{
-	  if (afct->ARCH_LA_PLTENTER != NULL
-	      && (reloc_result->enterexit
-		  & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0)
-	    {
-	      long int new_framesize = -1;
-	      struct auditstate *l_state = link_map_audit_state (l, cnt);
-	      struct auditstate *bound_state
-		= link_map_audit_state (reloc_result->bound, cnt);
-	      uintptr_t new_value
-		= afct->ARCH_LA_PLTENTER (&sym, reloc_result->boundndx,
-					  &l_state->cookie,
-					  &bound_state->cookie,
-					  regs, &flags, symname,
-					  &new_framesize);
-	      if (new_value != (uintptr_t) sym.st_value)
-		{
-		  flags |= LA_SYMB_ALTVALUE;
-		  sym.st_value = new_value;
-		}
-
-	      /* Remember the results for every audit library and
-		 store a summary in the first two bits.  */
-	      reloc_result->enterexit
-		|= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
-		    << (2 * (cnt + 1)));
-
-	      if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT
-					      << (2 * (cnt + 1))))
-		  == 0 && new_framesize != -1 && framesize != -2)
-		{
-		  /* If this is the first call providing information,
-		     use it.  */
-		  if (framesize == -1)
-		    framesize = new_framesize;
-		  /* If two pltenter calls provide conflicting information,
-		     use the larger value.  */
-		  else if (new_framesize != framesize)
-		    framesize = MAX (new_framesize, framesize);
-		}
-	    }
-
-	  afct = afct->next;
-	}
-
-      value = DL_FIXUP_ADDR_VALUE (sym.st_value);
-    }
+  _dl_audit_pltenter (l, reloc_result, &value, regs, &framesize);
 #endif
 
   /* Store the frame size information.  */
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
index c69c54200f..39762e1db3 100644
--- a/sysdeps/generic/ldsodefs.h
+++ b/sysdeps/generic/ldsodefs.h
@@ -1382,6 +1382,9 @@  void _dl_audit_symbind (struct link_map *l, struct reloc_result *reloc_result,
 void _dl_audit_symbind_alt (struct link_map *l, const ElfW(Sym) *ref,
 			    void **value, lookup_t result);
 rtld_hidden_proto (_dl_audit_symbind_alt)
+void _dl_audit_pltenter (struct link_map *l, struct reloc_result *reloc_result,
+			 DL_FIXUP_VALUE_TYPE *value, void *regs,
+			 long int *framesize);
 #endif /* SHARED */
 
 #if PTHREAD_IN_LIBC && defined SHARED