c5d972
commit cda4f265c65fb6c4ce38ca1cf0a7e527c5e77cd5
c5d972
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
c5d972
Date:   Tue Jul 20 15:58:35 2021 -0300
c5d972
c5d972
    elf: Add _dl_audit_symbind_alt and _dl_audit_symbind
c5d972
    
c5d972
    It consolidates the code required to call la_symbind{32,64} 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/Versions b/elf/Versions
c5d972
index be88c48e6d45a937..c5d4342cf1f5124c 100644
c5d972
--- a/elf/Versions
c5d972
+++ b/elf/Versions
c5d972
@@ -59,6 +59,7 @@ ld {
c5d972
     _dl_argv; _dl_find_dso_for_object; _dl_get_tls_static_info;
c5d972
     _dl_deallocate_tls; _dl_make_stack_executable;
c5d972
     _dl_rtld_di_serinfo; _dl_starting_up; _dl_fatal_printf;
c5d972
+    _dl_audit_symbind_alt;
c5d972
     _rtld_global; _rtld_global_ro;
c5d972
 
c5d972
     # Only here for gdb while a better method is developed.
c5d972
diff --git a/elf/dl-audit.c b/elf/dl-audit.c
c5d972
index cb1c3de93cba447b..a21530f30bc5524b 100644
c5d972
--- a/elf/dl-audit.c
c5d972
+++ b/elf/dl-audit.c
c5d972
@@ -16,6 +16,7 @@
c5d972
    License along with the GNU C Library; if not, see
c5d972
    <https://www.gnu.org/licenses/>.  */
c5d972
 
c5d972
+#include <assert.h>
c5d972
 #include <ldsodefs.h>
c5d972
 
c5d972
 void
c5d972
@@ -106,3 +107,124 @@ _dl_audit_objclose (struct link_map *l)
c5d972
       afct = afct->next;
c5d972
     }
c5d972
 }
c5d972
+
c5d972
+void
c5d972
+_dl_audit_symbind_alt (struct link_map *l, const ElfW(Sym) *ref, void **value,
c5d972
+		       lookup_t result)
c5d972
+{
c5d972
+  if ((l->l_audit_any_plt | result->l_audit_any_plt) == 0)
c5d972
+    return;
c5d972
+
c5d972
+  const char *strtab = (const char *) D_PTR (result, l_info[DT_STRTAB]);
c5d972
+  /* Compute index of the symbol entry in the symbol table of the DSO with
c5d972
+     the definition.  */
c5d972
+  unsigned int ndx = (ref - (ElfW(Sym) *) D_PTR (result, l_info[DT_SYMTAB]));
c5d972
+
c5d972
+  unsigned int altvalue = 0;
c5d972
+  /* Synthesize a symbol record where the st_value field is the result.  */
c5d972
+  ElfW(Sym) sym = *ref;
c5d972
+  sym.st_value = (ElfW(Addr)) *value;
c5d972
+
c5d972
+  struct audit_ifaces *afct = GLRO(dl_audit);
c5d972
+  for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
c5d972
+    {
c5d972
+      struct auditstate *match_audit = link_map_audit_state (l, cnt);
c5d972
+      struct auditstate *result_audit = link_map_audit_state (result, cnt);
c5d972
+      if (afct->symbind != NULL
c5d972
+	  && ((match_audit->bindflags & LA_FLG_BINDFROM) != 0
c5d972
+	      || ((result_audit->bindflags & LA_FLG_BINDTO)
c5d972
+		  != 0)))
c5d972
+	{
c5d972
+	  unsigned int flags = altvalue | LA_SYMB_DLSYM;
c5d972
+	  uintptr_t new_value = afct->symbind (&sym, ndx,
c5d972
+					       &match_audit->cookie,
c5d972
+					       &result_audit->cookie,
c5d972
+					       &flags, strtab + ref->st_name);
c5d972
+	  if (new_value != (uintptr_t) sym.st_value)
c5d972
+	    {
c5d972
+	      altvalue = LA_SYMB_ALTVALUE;
c5d972
+	      sym.st_value = new_value;
c5d972
+	    }
c5d972
+
c5d972
+	  afct = afct->next;
c5d972
+	}
c5d972
+
c5d972
+      *value = (void *) sym.st_value;
c5d972
+    }
c5d972
+}
c5d972
+rtld_hidden_def (_dl_audit_symbind_alt)
c5d972
+
c5d972
+void
c5d972
+_dl_audit_symbind (struct link_map *l, struct reloc_result *reloc_result,
c5d972
+		   const ElfW(Sym) *defsym, DL_FIXUP_VALUE_TYPE *value,
c5d972
+		   lookup_t result)
c5d972
+{
c5d972
+  reloc_result->bound = result;
c5d972
+  /* Compute index of the symbol entry in the symbol table of the DSO with the
c5d972
+     definition.  */
c5d972
+  reloc_result->boundndx = (defsym - (ElfW(Sym) *) D_PTR (result,
c5d972
+							  l_info[DT_SYMTAB]));
c5d972
+
c5d972
+  if ((l->l_audit_any_plt | result->l_audit_any_plt) == 0)
c5d972
+    {
c5d972
+      /* Set all bits since this symbol binding is not interesting.  */
c5d972
+      reloc_result->enterexit = (1u << DL_NNS) - 1;
c5d972
+      return;
c5d972
+    }
c5d972
+
c5d972
+  /* Synthesize a symbol record where the st_value field is the result.  */
c5d972
+  ElfW(Sym) sym = *defsym;
c5d972
+  sym.st_value = DL_FIXUP_VALUE_ADDR (*value);
c5d972
+
c5d972
+  /* Keep track whether there is any interest in tracing the call in the lower
c5d972
+     two bits.  */
c5d972
+  assert (DL_NNS * 2 <= sizeof (reloc_result->flags) * 8);
c5d972
+  assert ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT) == 3);
c5d972
+  reloc_result->enterexit = LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT;
c5d972
+
c5d972
+  const char *strtab2 = (const void *) D_PTR (result, l_info[DT_STRTAB]);
c5d972
+
c5d972
+  unsigned int flags = 0;
c5d972
+  struct audit_ifaces *afct = GLRO(dl_audit);
c5d972
+  for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
c5d972
+    {
c5d972
+      /* XXX Check whether both DSOs must request action or only one */
c5d972
+      struct auditstate *l_state = link_map_audit_state (l, cnt);
c5d972
+      struct auditstate *result_state = link_map_audit_state (result, cnt);
c5d972
+      if ((l_state->bindflags & LA_FLG_BINDFROM) != 0
c5d972
+	  && (result_state->bindflags & LA_FLG_BINDTO) != 0)
c5d972
+	{
c5d972
+	  if (afct->symbind != NULL)
c5d972
+	    {
c5d972
+	      uintptr_t new_value = afct->symbind (&sym,
c5d972
+						   reloc_result->boundndx,
c5d972
+						   &l_state->cookie,
c5d972
+						   &result_state->cookie,
c5d972
+						   &flags,
c5d972
+						   strtab2 + defsym->st_name);
c5d972
+	      if (new_value != (uintptr_t) sym.st_value)
c5d972
+		{
c5d972
+		  flags |= LA_SYMB_ALTVALUE;
c5d972
+		  sym.st_value = new_value;
c5d972
+		}
c5d972
+	    }
c5d972
+
c5d972
+	  /* Remember the results for every audit library and store a summary
c5d972
+	     in the first two bits.  */
c5d972
+	  reloc_result->enterexit &= flags & (LA_SYMB_NOPLTENTER
c5d972
+					      | LA_SYMB_NOPLTEXIT);
c5d972
+	  reloc_result->enterexit |= ((flags & (LA_SYMB_NOPLTENTER
c5d972
+						| LA_SYMB_NOPLTEXIT))
c5d972
+				      << ((cnt + 1) * 2));
c5d972
+	}
c5d972
+      else
c5d972
+	/* If the bind flags say this auditor is not interested, set the bits
c5d972
+	   manually.  */
c5d972
+	reloc_result->enterexit |= ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT)
c5d972
+				    << ((cnt + 1) * 2));
c5d972
+      afct = afct->next;
c5d972
+    }
c5d972
+
c5d972
+  reloc_result->flags = flags;
c5d972
+  *value = DL_FIXUP_ADDR_VALUE (sym.st_value);
c5d972
+}
c5d972
diff --git a/elf/dl-runtime.c b/elf/dl-runtime.c
c5d972
index 4ccd7c30678fafad..d4840a7c17441126 100644
c5d972
--- a/elf/dl-runtime.c
c5d972
+++ b/elf/dl-runtime.c
c5d972
@@ -296,84 +296,7 @@ _dl_profile_fixup (
c5d972
 	 auditing libraries the possibility to change the value and
c5d972
 	 tell us whether further auditing is wanted.  */
c5d972
       if (defsym != NULL && GLRO(dl_naudit) > 0)
c5d972
-	{
c5d972
-	  reloc_result->bound = result;
c5d972
-	  /* Compute index of the symbol entry in the symbol table of
c5d972
-	     the DSO with the definition.  */
c5d972
-	  reloc_result->boundndx = (defsym
c5d972
-				    - (ElfW(Sym) *) D_PTR (result,
c5d972
-							   l_info[DT_SYMTAB]));
c5d972
-
c5d972
-	  /* Determine whether any of the two participating DSOs is
c5d972
-	     interested in auditing.  */
c5d972
-	  if ((l->l_audit_any_plt | result->l_audit_any_plt) != 0)
c5d972
-	    {
c5d972
-	      unsigned int flags = 0;
c5d972
-	      struct audit_ifaces *afct = GLRO(dl_audit);
c5d972
-	      /* Synthesize a symbol record where the st_value field is
c5d972
-		 the result.  */
c5d972
-	      ElfW(Sym) sym = *defsym;
c5d972
-	      sym.st_value = DL_FIXUP_VALUE_ADDR (value);
c5d972
-
c5d972
-	      /* Keep track whether there is any interest in tracing
c5d972
-		 the call in the lower two bits.  */
c5d972
-	      assert (DL_NNS * 2 <= sizeof (reloc_result->flags) * 8);
c5d972
-	      assert ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT) == 3);
c5d972
-	      reloc_result->enterexit = LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT;
c5d972
-
c5d972
-	      const char *strtab2 = (const void *) D_PTR (result,
c5d972
-							  l_info[DT_STRTAB]);
c5d972
-
c5d972
-	      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
c5d972
-		{
c5d972
-		  /* XXX Check whether both DSOs must request action or
c5d972
-		     only one */
c5d972
-		  struct auditstate *l_state = link_map_audit_state (l, cnt);
c5d972
-		  struct auditstate *result_state
c5d972
-		    = link_map_audit_state (result, cnt);
c5d972
-		  if ((l_state->bindflags & LA_FLG_BINDFROM) != 0
c5d972
-		      && (result_state->bindflags & LA_FLG_BINDTO) != 0)
c5d972
-		    {
c5d972
-		      if (afct->symbind != NULL)
c5d972
-			{
c5d972
-			  uintptr_t new_value
c5d972
-			    = afct->symbind (&sym, reloc_result->boundndx,
c5d972
-					     &l_state->cookie,
c5d972
-					     &result_state->cookie,
c5d972
-					     &flags,
c5d972
-					     strtab2 + defsym->st_name);
c5d972
-			  if (new_value != (uintptr_t) sym.st_value)
c5d972
-			    {
c5d972
-			      flags |= LA_SYMB_ALTVALUE;
c5d972
-			      sym.st_value = new_value;
c5d972
-			    }
c5d972
-			}
c5d972
-
c5d972
-		      /* Remember the results for every audit library and
c5d972
-			 store a summary in the first two bits.  */
c5d972
-		      reloc_result->enterexit
c5d972
-			&= flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT);
c5d972
-		      reloc_result->enterexit
c5d972
-			|= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
c5d972
-			    << ((cnt + 1) * 2));
c5d972
-		    }
c5d972
-		  else
c5d972
-		    /* If the bind flags say this auditor is not interested,
c5d972
-		       set the bits manually.  */
c5d972
-		    reloc_result->enterexit
c5d972
-		      |= ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT)
c5d972
-			  << ((cnt + 1) * 2));
c5d972
-
c5d972
-		  afct = afct->next;
c5d972
-		}
c5d972
-
c5d972
-	      reloc_result->flags = flags;
c5d972
-	      value = DL_FIXUP_ADDR_VALUE (sym.st_value);
c5d972
-	    }
c5d972
-	  else
c5d972
-	    /* Set all bits since this symbol binding is not interesting.  */
c5d972
-	    reloc_result->enterexit = (1u << DL_NNS) - 1;
c5d972
-	}
c5d972
+	_dl_audit_symbind (l, reloc_result, defsym, &value, result);
c5d972
 #endif
c5d972
 
c5d972
       /* Store the result for later runs.  */
c5d972
diff --git a/elf/dl-sym-post.h b/elf/dl-sym-post.h
c5d972
index 4c4f574633497789..f33934c92047f293 100644
c5d972
--- a/elf/dl-sym-post.h
c5d972
+++ b/elf/dl-sym-post.h
c5d972
@@ -52,54 +52,9 @@ _dl_sym_post (lookup_t result, const ElfW(Sym) *ref, void *value,
c5d972
      tell us whether further auditing is wanted.  */
c5d972
   if (__glibc_unlikely (GLRO(dl_naudit) > 0))
c5d972
     {
c5d972
-      const char *strtab = (const char *) D_PTR (result,
c5d972
-                                                 l_info[DT_STRTAB]);
c5d972
-      /* Compute index of the symbol entry in the symbol table of
c5d972
-         the DSO with the definition.  */
c5d972
-      unsigned int ndx = (ref - (ElfW(Sym) *) D_PTR (result,
c5d972
-                                                     l_info[DT_SYMTAB]));
c5d972
-
c5d972
       if (match == NULL)
c5d972
         match = _dl_sym_find_caller_link_map (caller);
c5d972
-
c5d972
-      if ((match->l_audit_any_plt | result->l_audit_any_plt) != 0)
c5d972
-        {
c5d972
-          unsigned int altvalue = 0;
c5d972
-          struct audit_ifaces *afct = GLRO(dl_audit);
c5d972
-          /* Synthesize a symbol record where the st_value field is
c5d972
-             the result.  */
c5d972
-          ElfW(Sym) sym = *ref;
c5d972
-          sym.st_value = (ElfW(Addr)) value;
c5d972
-
c5d972
-          for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
c5d972
-            {
c5d972
-              struct auditstate *match_audit
c5d972
-                = link_map_audit_state (match, cnt);
c5d972
-              struct auditstate *result_audit
c5d972
-                = link_map_audit_state (result, cnt);
c5d972
-              if (afct->symbind != NULL
c5d972
-                  && ((match_audit->bindflags & LA_FLG_BINDFROM) != 0
c5d972
-                      || ((result_audit->bindflags & LA_FLG_BINDTO)
c5d972
-                          != 0)))
c5d972
-                {
c5d972
-                  unsigned int flags = altvalue | LA_SYMB_DLSYM;
c5d972
-                  uintptr_t new_value
c5d972
-                    = afct->symbind (&sym, ndx,
c5d972
-                                     &match_audit->cookie,
c5d972
-                                     &result_audit->cookie,
c5d972
-                                     &flags, strtab + ref->st_name);
c5d972
-                  if (new_value != (uintptr_t) sym.st_value)
c5d972
-                    {
c5d972
-                      altvalue = LA_SYMB_ALTVALUE;
c5d972
-                      sym.st_value = new_value;
c5d972
-                    }
c5d972
-                }
c5d972
-
c5d972
-              afct = afct->next;
c5d972
-            }
c5d972
-
c5d972
-          value = (void *) sym.st_value;
c5d972
-        }
c5d972
+      _dl_audit_symbind_alt (match, ref, &value, result);
c5d972
     }
c5d972
 #endif
c5d972
   return value;
c5d972
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
c5d972
index 3db25c5be1acf871..fa55c3bde10de52e 100644
c5d972
--- a/sysdeps/generic/ldsodefs.h
c5d972
+++ b/sysdeps/generic/ldsodefs.h
c5d972
@@ -1294,6 +1294,16 @@ void _dl_audit_objopen (struct link_map *l, Lmid_t nsid)
c5d972
 /* Call the la_objclose from the audit modules for the link_map L.  */
c5d972
 void _dl_audit_objclose (struct link_map *l)
c5d972
   attribute_hidden;
c5d972
+
c5d972
+/* Call the la_symbind{32,64} from the audit modules for the link_map L.  */
c5d972
+void _dl_audit_symbind (struct link_map *l, struct reloc_result *reloc_result,
c5d972
+			const ElfW(Sym) *defsym, DL_FIXUP_VALUE_TYPE *value,
c5d972
+			lookup_t result)
c5d972
+  attribute_hidden;
c5d972
+/* Same as _dl_audit_symbind, but also sets LA_SYMB_DLSYM flag.  */
c5d972
+void _dl_audit_symbind_alt (struct link_map *l, const ElfW(Sym) *ref,
c5d972
+			    void **value, lookup_t result);
c5d972
+rtld_hidden_proto (_dl_audit_symbind_alt)
c5d972
 #endif /* SHARED */
c5d972
 
c5d972
 __END_DECLS