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