malloc: Fix tst-mallocalign1 macro spacing.
[glibc.git] / elf / dl-audit.c
blob715de53272271130712f89437ecf244a90676f92
1 /* Audit common functions.
2 Copyright (C) 2021-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
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>
26 void
27 _dl_audit_activity_map (struct link_map *l, int action)
29 struct audit_ifaces *afct = GLRO(dl_audit);
30 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
32 if (afct->activity != NULL)
33 afct->activity (&link_map_audit_state (l, cnt)->cookie, action);
34 afct = afct->next;
38 void
39 _dl_audit_activity_nsid (Lmid_t nsid, int action)
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;
49 _dl_audit_activity_map (head, action);
52 const char *
53 _dl_audit_objsearch (const char *name, struct link_map *l, unsigned int code)
55 if (l == NULL || l->l_auditing || code == 0)
56 return name;
58 struct audit_ifaces *afct = GLRO(dl_audit);
59 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
61 if (afct->objsearch != NULL)
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;
68 afct = afct->next;
71 return name;
74 void
75 _dl_audit_objopen (struct link_map *l, Lmid_t nsid)
77 if (__glibc_likely (GLRO(dl_naudit) == 0))
78 return;
80 struct audit_ifaces *afct = GLRO(dl_audit);
81 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
83 if (afct->objopen != NULL)
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;
90 afct = afct->next;
94 void
95 _dl_audit_objclose (struct link_map *l)
97 if (__glibc_likely (GLRO(dl_naudit) == 0)
98 || GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
99 return;
101 struct audit_ifaces *afct = GLRO(dl_audit);
102 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
104 if (afct->objclose != NULL)
106 struct auditstate *state= link_map_audit_state (l, cnt);
107 /* Return value is ignored. */
108 afct->objclose (&state->cookie);
111 afct = afct->next;
115 void
116 _dl_audit_preinit (struct link_map *l)
118 if (__glibc_likely (GLRO(dl_naudit) == 0))
119 return;
121 struct audit_ifaces *afct = GLRO(dl_audit);
122 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
124 if (afct->preinit != NULL)
125 afct->preinit (&link_map_audit_state (l, cnt)->cookie);
126 afct = afct->next;
130 void
131 _dl_audit_symbind_alt (struct link_map *l, const ElfW(Sym) *ref, void **value,
132 lookup_t result)
134 if ((l->l_audit_any_plt | result->l_audit_any_plt) == 0)
135 return;
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]));
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;
147 struct audit_ifaces *afct = GLRO(dl_audit);
148 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
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)))
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)
164 altvalue = LA_SYMB_ALTVALUE;
165 sym.st_value = new_value;
168 afct = afct->next;
171 *value = (void *) sym.st_value;
174 rtld_hidden_def (_dl_audit_symbind_alt)
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)
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]));
187 if ((l->l_audit_any_plt | result->l_audit_any_plt) == 0)
189 /* Set all bits since this symbol binding is not interesting. */
190 reloc_result->enterexit = (1u << DL_NNS) - 1;
191 return;
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);
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;
204 const char *strtab2 = (const void *) D_PTR (result, l_info[DT_STRTAB]);
206 unsigned int flags = 0;
207 struct audit_ifaces *afct = GLRO(dl_audit);
208 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
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)
216 if (afct->symbind != NULL)
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)
226 flags |= LA_SYMB_ALTVALUE;
227 sym.st_value = new_value;
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));
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;
247 reloc_result->flags = flags;
248 *value = DL_FIXUP_ADDR_VALUE (sym.st_value);
251 void
252 _dl_audit_pltenter (struct link_map *l, struct reloc_result *reloc_result,
253 DL_FIXUP_VALUE_TYPE *value, void *regs, long int *framesize)
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;
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);
267 /* Set up the sym parameter. */
268 ElfW(Sym) sym = *defsym;
269 sym.st_value = DL_FIXUP_VALUE_ADDR (*value);
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;
276 /* Keep track of overwritten addresses. */
277 unsigned int flags = reloc_result->flags;
279 struct audit_ifaces *afct = GLRO(dl_audit);
280 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
282 if (afct->ARCH_LA_PLTENTER != NULL
283 && (reloc_result->enterexit
284 & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0)
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)
296 flags |= LA_SYMB_ALTVALUE;
297 sym.st_value = new_value;
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)));
306 if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT
307 << (2 * (cnt + 1))))
308 == 0 && new_framesize != -1 && *framesize != -2)
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);
320 afct = afct->next;
323 *value = DL_FIXUP_ADDR_VALUE (sym.st_value);
326 void
327 DL_ARCH_FIXUP_ATTRIBUTE
328 _dl_audit_pltexit (struct link_map *l, ElfW(Word) reloc_arg,
329 const void *inregs, void *outregs)
331 const uintptr_t pltgot = (uintptr_t) D_PTR (l, l_info[DT_PLTGOT]);
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);
343 /* Set up the sym parameter. */
344 ElfW(Sym) sym = *defsym;
345 sym.st_value = DL_FIXUP_VALUE_ADDR (reloc_result->addr);
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;
352 struct audit_ifaces *afct = GLRO(dl_audit);
353 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
355 if (afct->ARCH_LA_PLTEXIT != NULL
356 && (reloc_result->enterexit
357 & (LA_SYMB_NOPLTEXIT >> (2 * cnt))) == 0)
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);
367 afct = afct->next;