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