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