1 /* Audit common functions.
2    Copyright (C) 2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <assert.h>
20 #include <link.h>
21 #include <ldsodefs.h>
22 #include <dl-machine.h>
23 #include <dl-runtime.h>
24 #include <dl-fixup-attribute.h>
25 
26 void
_dl_audit_activity_map(struct link_map * l,int action)27 _dl_audit_activity_map (struct link_map *l, int action)
28 {
29   struct audit_ifaces *afct = GLRO(dl_audit);
30   for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
31     {
32       if (afct->activity != NULL)
33 	afct->activity (&link_map_audit_state (l, cnt)->cookie, action);
34       afct = afct->next;
35     }
36 }
37 
38 void
_dl_audit_activity_nsid(Lmid_t nsid,int action)39 _dl_audit_activity_nsid (Lmid_t nsid, int action)
40 {
41   /* If head is NULL, the namespace has become empty, and the audit interface
42      does not give us a way to signal LA_ACT_CONSISTENT for it because the
43      first loaded module is used to identify the namespace.  */
44   struct link_map *head = GL(dl_ns)[nsid]._ns_loaded;
45   if (__glibc_likely (GLRO(dl_naudit) == 0)
46       || head == NULL || head->l_auditing)
47     return;
48 
49   _dl_audit_activity_map (head, action);
50 }
51 
52 const char *
_dl_audit_objsearch(const char * name,struct link_map * l,unsigned int code)53 _dl_audit_objsearch (const char *name, struct link_map *l, unsigned int code)
54 {
55   if (l == NULL || l->l_auditing || code == 0)
56     return name;
57 
58   struct audit_ifaces *afct = GLRO(dl_audit);
59   for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
60     {
61       if (afct->objsearch != NULL)
62 	{
63 	  struct auditstate *state = link_map_audit_state (l, cnt);
64 	  name = afct->objsearch (name, &state->cookie, code);
65 	  if (name == NULL)
66 	    return NULL;
67 	}
68       afct = afct->next;
69    }
70 
71   return name;
72 }
73 
74 void
_dl_audit_objopen(struct link_map * l,Lmid_t nsid)75 _dl_audit_objopen (struct link_map *l, Lmid_t nsid)
76 {
77   if (__glibc_likely (GLRO(dl_naudit) == 0))
78     return;
79 
80   struct audit_ifaces *afct = GLRO(dl_audit);
81   for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
82     {
83       if (afct->objopen != NULL)
84 	{
85 	  struct auditstate *state = link_map_audit_state (l, cnt);
86 	  state->bindflags = afct->objopen (l, nsid, &state->cookie);
87 	  l->l_audit_any_plt |= state->bindflags != 0;
88 	}
89 
90       afct = afct->next;
91    }
92 }
93 
94 void
_dl_audit_objclose(struct link_map * l)95 _dl_audit_objclose (struct link_map *l)
96 {
97   if (__glibc_likely (GLRO(dl_naudit) == 0)
98       || GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
99     return;
100 
101   struct audit_ifaces *afct = GLRO(dl_audit);
102   for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
103     {
104       if (afct->objclose != NULL)
105 	{
106 	  struct auditstate *state= link_map_audit_state (l, cnt);
107 	  /* Return value is ignored.  */
108 	  afct->objclose (&state->cookie);
109 	}
110 
111       afct = afct->next;
112     }
113 }
114 
115 void
_dl_audit_preinit(struct link_map * l)116 _dl_audit_preinit (struct link_map *l)
117 {
118   if (__glibc_likely (GLRO(dl_naudit) == 0))
119     return;
120 
121   struct audit_ifaces *afct = GLRO(dl_audit);
122   for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
123     {
124       if (afct->preinit != NULL)
125 	afct->preinit (&link_map_audit_state (l, cnt)->cookie);
126       afct = afct->next;
127     }
128 }
129 
130 void
_dl_audit_symbind_alt(struct link_map * l,const ElfW (Sym)* ref,void ** value,lookup_t result)131 _dl_audit_symbind_alt (struct link_map *l, const ElfW(Sym) *ref, void **value,
132 		       lookup_t result)
133 {
134   if ((l->l_audit_any_plt | result->l_audit_any_plt) == 0)
135     return;
136 
137   const char *strtab = (const char *) D_PTR (result, l_info[DT_STRTAB]);
138   /* Compute index of the symbol entry in the symbol table of the DSO with
139      the definition.  */
140   unsigned int ndx = (ref - (ElfW(Sym) *) D_PTR (result, l_info[DT_SYMTAB]));
141 
142   unsigned int altvalue = 0;
143   /* Synthesize a symbol record where the st_value field is the result.  */
144   ElfW(Sym) sym = *ref;
145   sym.st_value = (ElfW(Addr)) *value;
146 
147   struct audit_ifaces *afct = GLRO(dl_audit);
148   for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
149     {
150       struct auditstate *match_audit = link_map_audit_state (l, cnt);
151       struct auditstate *result_audit = link_map_audit_state (result, cnt);
152       if (afct->symbind != NULL
153 	  && ((match_audit->bindflags & LA_FLG_BINDFROM) != 0
154 	      || ((result_audit->bindflags & LA_FLG_BINDTO)
155 		  != 0)))
156 	{
157 	  unsigned int flags = altvalue | LA_SYMB_DLSYM;
158 	  uintptr_t new_value = afct->symbind (&sym, ndx,
159 					       &match_audit->cookie,
160 					       &result_audit->cookie,
161 					       &flags, strtab + ref->st_name);
162 	  if (new_value != (uintptr_t) sym.st_value)
163 	    {
164 	      altvalue = LA_SYMB_ALTVALUE;
165 	      sym.st_value = new_value;
166 	    }
167 
168 	  afct = afct->next;
169 	}
170 
171       *value = (void *) sym.st_value;
172     }
173 }
rtld_hidden_def(_dl_audit_symbind_alt)174 rtld_hidden_def (_dl_audit_symbind_alt)
175 
176 void
177 _dl_audit_symbind (struct link_map *l, struct reloc_result *reloc_result,
178 		   const ElfW(Sym) *defsym, DL_FIXUP_VALUE_TYPE *value,
179 		   lookup_t result)
180 {
181   reloc_result->bound = result;
182   /* Compute index of the symbol entry in the symbol table of the DSO with the
183      definition.  */
184   reloc_result->boundndx = (defsym - (ElfW(Sym) *) D_PTR (result,
185 							  l_info[DT_SYMTAB]));
186 
187   if ((l->l_audit_any_plt | result->l_audit_any_plt) == 0)
188     {
189       /* Set all bits since this symbol binding is not interesting.  */
190       reloc_result->enterexit = (1u << DL_NNS) - 1;
191       return;
192     }
193 
194   /* Synthesize a symbol record where the st_value field is the result.  */
195   ElfW(Sym) sym = *defsym;
196   sym.st_value = DL_FIXUP_VALUE_ADDR (*value);
197 
198   /* Keep track whether there is any interest in tracing the call in the lower
199      two bits.  */
200   assert (DL_NNS * 2 <= sizeof (reloc_result->flags) * 8);
201   assert ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT) == 3);
202   reloc_result->enterexit = LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT;
203 
204   const char *strtab2 = (const void *) D_PTR (result, l_info[DT_STRTAB]);
205 
206   unsigned int flags = 0;
207   struct audit_ifaces *afct = GLRO(dl_audit);
208   for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
209     {
210       /* XXX Check whether both DSOs must request action or only one */
211       struct auditstate *l_state = link_map_audit_state (l, cnt);
212       struct auditstate *result_state = link_map_audit_state (result, cnt);
213       if ((l_state->bindflags & LA_FLG_BINDFROM) != 0
214 	  && (result_state->bindflags & LA_FLG_BINDTO) != 0)
215 	{
216 	  if (afct->symbind != NULL)
217 	    {
218 	      uintptr_t new_value = afct->symbind (&sym,
219 						   reloc_result->boundndx,
220 						   &l_state->cookie,
221 						   &result_state->cookie,
222 						   &flags,
223 						   strtab2 + defsym->st_name);
224 	      if (new_value != (uintptr_t) sym.st_value)
225 		{
226 		  flags |= LA_SYMB_ALTVALUE;
227 		  sym.st_value = new_value;
228 		}
229 	    }
230 
231 	  /* Remember the results for every audit library and store a summary
232 	     in the first two bits.  */
233 	  reloc_result->enterexit &= flags & (LA_SYMB_NOPLTENTER
234 					      | LA_SYMB_NOPLTEXIT);
235 	  reloc_result->enterexit |= ((flags & (LA_SYMB_NOPLTENTER
236 						| LA_SYMB_NOPLTEXIT))
237 				      << ((cnt + 1) * 2));
238 	}
239       else
240 	/* If the bind flags say this auditor is not interested, set the bits
241 	   manually.  */
242 	reloc_result->enterexit |= ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT)
243 				    << ((cnt + 1) * 2));
244       afct = afct->next;
245     }
246 
247   reloc_result->flags = flags;
248   *value = DL_FIXUP_ADDR_VALUE (sym.st_value);
249 }
250 
251 void
_dl_audit_pltenter(struct link_map * l,struct reloc_result * reloc_result,DL_FIXUP_VALUE_TYPE * value,void * regs,long int * framesize)252 _dl_audit_pltenter (struct link_map *l, struct reloc_result *reloc_result,
253 		    DL_FIXUP_VALUE_TYPE *value, void *regs, long int *framesize)
254 {
255   /* Don't do anything if no auditor wants to intercept this call.  */
256   if (GLRO(dl_naudit) == 0
257       || (reloc_result->enterexit & LA_SYMB_NOPLTENTER))
258     return;
259 
260   /* Sanity check:  DL_FIXUP_VALUE_CODE_ADDR (value) should have been
261      initialized earlier in this function or in another thread.  */
262   assert (DL_FIXUP_VALUE_CODE_ADDR (*value) != 0);
263   ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
264 					    l_info[DT_SYMTAB])
265 		       + reloc_result->boundndx);
266 
267   /* Set up the sym parameter.  */
268   ElfW(Sym) sym = *defsym;
269   sym.st_value = DL_FIXUP_VALUE_ADDR (*value);
270 
271   /* Get the symbol name.  */
272   const char *strtab = (const void *) D_PTR (reloc_result->bound,
273 					     l_info[DT_STRTAB]);
274   const char *symname = strtab + sym.st_name;
275 
276   /* Keep track of overwritten addresses.  */
277   unsigned int flags = reloc_result->flags;
278 
279   struct audit_ifaces *afct = GLRO(dl_audit);
280   for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
281     {
282       if (afct->ARCH_LA_PLTENTER != NULL
283 	  && (reloc_result->enterexit
284 	      & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0)
285 	{
286 	  long int new_framesize = -1;
287 	  struct auditstate *l_state = link_map_audit_state (l, cnt);
288 	  struct auditstate *bound_state
289 	    = link_map_audit_state (reloc_result->bound, cnt);
290 	  uintptr_t new_value
291 	    = afct->ARCH_LA_PLTENTER (&sym, reloc_result->boundndx,
292 				      &l_state->cookie, &bound_state->cookie,
293 				      regs, &flags, symname, &new_framesize);
294 	  if (new_value != (uintptr_t) sym.st_value)
295 	    {
296 	      flags |= LA_SYMB_ALTVALUE;
297 	      sym.st_value = new_value;
298 	    }
299 
300 	  /* Remember the results for every audit library and store a summary
301 	     in the first two bits.  */
302 	  reloc_result->enterexit |= ((flags & (LA_SYMB_NOPLTENTER
303 						| LA_SYMB_NOPLTEXIT))
304 				      << (2 * (cnt + 1)));
305 
306 	  if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT
307 					  << (2 * (cnt + 1))))
308 	      == 0 && new_framesize != -1 && *framesize != -2)
309 	    {
310 	      /* If this is the first call providing information, use it.  */
311 	      if (*framesize == -1)
312 		*framesize = new_framesize;
313 	      /* If two pltenter calls provide conflicting information, use
314 		 the larger value.  */
315 	      else if (new_framesize != *framesize)
316 		*framesize = MAX (new_framesize, *framesize);
317 	    }
318 	}
319 
320       afct = afct->next;
321     }
322 
323   *value = DL_FIXUP_ADDR_VALUE (sym.st_value);
324 }
325 
326 void
327 DL_ARCH_FIXUP_ATTRIBUTE
_dl_audit_pltexit(struct link_map * l,ElfW (Word)reloc_arg,const void * inregs,void * outregs)328 _dl_audit_pltexit (struct link_map *l, ElfW(Word) reloc_arg,
329 		   const void *inregs, void *outregs)
330 {
331   const uintptr_t pltgot = (uintptr_t) D_PTR (l, l_info[DT_PLTGOT]);
332 
333   /* This is the address in the array where we store the result of previous
334      relocations.  */
335   // XXX Maybe the bound information must be stored on the stack since
336   // XXX with bind_not a new value could have been stored in the meantime.
337   struct reloc_result *reloc_result =
338     &l->l_reloc_result[reloc_index (pltgot, reloc_arg, sizeof (PLTREL))];
339   ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
340 					    l_info[DT_SYMTAB])
341 		       + reloc_result->boundndx);
342 
343   /* Set up the sym parameter.  */
344   ElfW(Sym) sym = *defsym;
345   sym.st_value = DL_FIXUP_VALUE_ADDR (reloc_result->addr);
346 
347   /* Get the symbol name.  */
348   const char *strtab = (const void *) D_PTR (reloc_result->bound,
349 					     l_info[DT_STRTAB]);
350   const char *symname = strtab + sym.st_name;
351 
352   struct audit_ifaces *afct = GLRO(dl_audit);
353   for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
354     {
355       if (afct->ARCH_LA_PLTEXIT != NULL
356 	  && (reloc_result->enterexit
357 	      & (LA_SYMB_NOPLTEXIT >> (2 * cnt))) == 0)
358 	{
359 	  struct auditstate *l_state = link_map_audit_state (l, cnt);
360 	  struct auditstate *bound_state
361 	    = link_map_audit_state (reloc_result->bound, cnt);
362 	  afct->ARCH_LA_PLTEXIT (&sym, reloc_result->boundndx,
363 				 &l_state->cookie, &bound_state->cookie,
364 				 inregs, outregs, symname);
365 	}
366 
367       afct = afct->next;
368     }
369 }
370