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