Fix passing symbol value to pltexit callbacks when ld.so auditing.
[platform/upstream/glibc.git] / elf / dl-runtime.c
1 /* On-demand PLT fixup for shared objects.
2    Copyright (C) 1995-2009, 2010, 2011 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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #define IN_DL_RUNTIME 1         /* This can be tested in dl-machine.h.  */
21
22 #include <alloca.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/param.h>
26 #include <ldsodefs.h>
27 #include <sysdep-cancel.h>
28 #include "dynamic-link.h"
29 #include <tls.h>
30
31
32 #if (!defined ELF_MACHINE_NO_RELA && !defined ELF_MACHINE_PLT_REL) \
33     || ELF_MACHINE_NO_REL
34 # define PLTREL  ElfW(Rela)
35 #else
36 # define PLTREL  ElfW(Rel)
37 #endif
38
39 #ifndef VERSYMIDX
40 # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
41 #endif
42
43 /* The fixup functions might have need special attributes.  If none
44    are provided define the macro as empty.  */
45 #ifndef ARCH_FIXUP_ATTRIBUTE
46 # define ARCH_FIXUP_ATTRIBUTE
47 #endif
48
49 #ifndef reloc_offset
50 # define reloc_offset reloc_arg
51 # define reloc_index  reloc_arg / sizeof (PLTREL)
52 #endif
53
54
55
56 /* This function is called through a special trampoline from the PLT the
57    first time each PLT entry is called.  We must perform the relocation
58    specified in the PLT of the given shared object, and return the resolved
59    function address to the trampoline, which will restart the original call
60    to that address.  Future calls will bounce directly from the PLT to the
61    function.  */
62
63 #ifndef ELF_MACHINE_NO_PLT
64 DL_FIXUP_VALUE_TYPE
65 __attribute ((noinline)) ARCH_FIXUP_ATTRIBUTE
66 _dl_fixup (
67 # ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
68            ELF_MACHINE_RUNTIME_FIXUP_ARGS,
69 # endif
70            /* GKM FIXME: Fix trampoline to pass bounds so we can do
71               without the `__unbounded' qualifier.  */
72            struct link_map *__unbounded l, ElfW(Word) reloc_arg)
73 {
74   const ElfW(Sym) *const symtab
75     = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
76   const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
77
78   const PLTREL *const reloc
79     = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
80   const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
81   void *const rel_addr = (void *)(l->l_addr + reloc->r_offset);
82   lookup_t result;
83   DL_FIXUP_VALUE_TYPE value;
84
85   /* Sanity check that we're really looking at a PLT relocation.  */
86   assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
87
88    /* Look up the target symbol.  If the normal lookup rules are not
89       used don't look in the global scope.  */
90   if (__builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
91     {
92       const struct r_found_version *version = NULL;
93
94       if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
95         {
96           const ElfW(Half) *vernum =
97             (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
98           ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
99           version = &l->l_versions[ndx];
100           if (version->hash == 0)
101             version = NULL;
102         }
103
104       /* We need to keep the scope around so do some locking.  This is
105          not necessary for objects which cannot be unloaded or when
106          we are not using any threads (yet).  */
107       int flags = DL_LOOKUP_ADD_DEPENDENCY;
108       if (!RTLD_SINGLE_THREAD_P)
109         {
110           THREAD_GSCOPE_SET_FLAG ();
111           flags |= DL_LOOKUP_GSCOPE_LOCK;
112         }
113
114 #ifdef RTLD_ENABLE_FOREIGN_CALL
115       RTLD_ENABLE_FOREIGN_CALL;
116 #endif
117
118       result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym, l->l_scope,
119                                     version, ELF_RTYPE_CLASS_PLT, flags, NULL);
120
121       /* We are done with the global scope.  */
122       if (!RTLD_SINGLE_THREAD_P)
123         THREAD_GSCOPE_RESET_FLAG ();
124
125 #ifdef RTLD_FINALIZE_FOREIGN_CALL
126       RTLD_FINALIZE_FOREIGN_CALL;
127 #endif
128
129       /* Currently result contains the base load address (or link map)
130          of the object that defines sym.  Now add in the symbol
131          offset.  */
132       value = DL_FIXUP_MAKE_VALUE (result,
133                                    sym ? (LOOKUP_VALUE_ADDRESS (result)
134                                           + sym->st_value) : 0);
135     }
136   else
137     {
138       /* We already found the symbol.  The module (and therefore its load
139          address) is also known.  */
140       value = DL_FIXUP_MAKE_VALUE (l, l->l_addr + sym->st_value);
141       result = l;
142     }
143
144   /* And now perhaps the relocation addend.  */
145   value = elf_machine_plt_value (l, reloc, value);
146
147   if (sym != NULL
148       && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0))
149     value = ((DL_FIXUP_VALUE_TYPE (*) (void)) DL_FIXUP_VALUE_ADDR (value)) ();
150
151   /* Finally, fix up the plt itself.  */
152   if (__builtin_expect (GLRO(dl_bind_not), 0))
153     return value;
154
155   return elf_machine_fixup_plt (l, result, reloc, rel_addr, value);
156 }
157 #endif
158
159 #if !defined PROF && !defined ELF_MACHINE_NO_PLT && !__BOUNDED_POINTERS__
160 DL_FIXUP_VALUE_TYPE
161 __attribute ((noinline)) ARCH_FIXUP_ATTRIBUTE
162 _dl_profile_fixup (
163 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
164                    ELF_MACHINE_RUNTIME_FIXUP_ARGS,
165 #endif
166                    struct link_map *l, ElfW(Word) reloc_arg,
167                    ElfW(Addr) retaddr, void *regs, long int *framesizep)
168 {
169   void (*mcount_fct) (ElfW(Addr), ElfW(Addr)) = INTUSE(_dl_mcount);
170
171   /* This is the address in the array where we store the result of previous
172      relocations.  */
173   struct reloc_result *reloc_result = &l->l_reloc_result[reloc_index];
174   DL_FIXUP_VALUE_TYPE *resultp = &reloc_result->addr;
175
176   DL_FIXUP_VALUE_TYPE value = *resultp;
177   if (DL_FIXUP_VALUE_CODE_ADDR (value) == 0)
178     {
179       /* This is the first time we have to relocate this object.  */
180       const ElfW(Sym) *const symtab
181         = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
182       const char *strtab = (const char *) D_PTR (l, l_info[DT_STRTAB]);
183
184       const PLTREL *const reloc
185         = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
186       const ElfW(Sym) *refsym = &symtab[ELFW(R_SYM) (reloc->r_info)];
187       const ElfW(Sym) *defsym = refsym;
188       lookup_t result;
189
190       /* Sanity check that we're really looking at a PLT relocation.  */
191       assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
192
193       /* Look up the target symbol.  If the symbol is marked STV_PROTECTED
194          don't look in the global scope.  */
195       if (__builtin_expect (ELFW(ST_VISIBILITY) (refsym->st_other), 0) == 0)
196         {
197           const struct r_found_version *version = NULL;
198
199           if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
200             {
201               const ElfW(Half) *vernum =
202                 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
203               ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
204               version = &l->l_versions[ndx];
205               if (version->hash == 0)
206                 version = NULL;
207             }
208
209           /* We need to keep the scope around so do some locking.  This is
210              not necessary for objects which cannot be unloaded or when
211              we are not using any threads (yet).  */
212           int flags = DL_LOOKUP_ADD_DEPENDENCY;
213           if (!RTLD_SINGLE_THREAD_P)
214             {
215               THREAD_GSCOPE_SET_FLAG ();
216               flags |= DL_LOOKUP_GSCOPE_LOCK;
217             }
218
219           result = _dl_lookup_symbol_x (strtab + refsym->st_name, l,
220                                         &defsym, l->l_scope, version,
221                                         ELF_RTYPE_CLASS_PLT, flags, NULL);
222
223           /* We are done with the global scope.  */
224           if (!RTLD_SINGLE_THREAD_P)
225             THREAD_GSCOPE_RESET_FLAG ();
226
227           /* Currently result contains the base load address (or link map)
228              of the object that defines sym.  Now add in the symbol
229              offset.  */
230           value = DL_FIXUP_MAKE_VALUE (result,
231                                        defsym != NULL
232                                        ? LOOKUP_VALUE_ADDRESS (result)
233                                          + defsym->st_value : 0);
234
235           if (defsym != NULL
236               && __builtin_expect (ELFW(ST_TYPE) (defsym->st_info)
237                                    == STT_GNU_IFUNC, 0))
238             value = ((DL_FIXUP_VALUE_TYPE (*) (void))
239                      DL_FIXUP_VALUE_ADDR (value)) ();
240         }
241       else
242         {
243           /* We already found the symbol.  The module (and therefore its load
244              address) is also known.  */
245           value = DL_FIXUP_MAKE_VALUE (l, l->l_addr + refsym->st_value);
246
247           if (__builtin_expect (ELFW(ST_TYPE) (refsym->st_info)
248                                 == STT_GNU_IFUNC, 0))
249             value = ((DL_FIXUP_VALUE_TYPE (*) (void))
250                      DL_FIXUP_VALUE_ADDR (value)) ();
251
252           result = l;
253         }
254       /* And now perhaps the relocation addend.  */
255       value = elf_machine_plt_value (l, reloc, value);
256
257 #ifdef SHARED
258       /* Auditing checkpoint: we have a new binding.  Provide the
259          auditing libraries the possibility to change the value and
260          tell us whether further auditing is wanted.  */
261       if (defsym != NULL && GLRO(dl_naudit) > 0)
262         {
263           reloc_result->bound = result;
264           /* Compute index of the symbol entry in the symbol table of
265              the DSO with the definition.  */
266           reloc_result->boundndx = (defsym
267                                     - (ElfW(Sym) *) D_PTR (result,
268                                                            l_info[DT_SYMTAB]));
269
270           /* Determine whether any of the two participating DSOs is
271              interested in auditing.  */
272           if ((l->l_audit_any_plt | result->l_audit_any_plt) != 0)
273             {
274               unsigned int altvalue = 0;
275               struct audit_ifaces *afct = GLRO(dl_audit);
276               /* Synthesize a symbol record where the st_value field is
277                  the result.  */
278               ElfW(Sym) sym = *defsym;
279               sym.st_value = DL_FIXUP_VALUE_ADDR (value);
280
281               /* Keep track whether there is any interest in tracing
282                  the call in the lower two bits.  */
283               assert (DL_NNS * 2 <= sizeof (reloc_result->flags) * 8);
284               assert ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT) == 3);
285               reloc_result->enterexit = LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT;
286
287               const char *strtab2 = (const void *) D_PTR (result,
288                                                           l_info[DT_STRTAB]);
289
290               for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
291                 {
292                   /* XXX Check whether both DSOs must request action or
293                      only one */
294                   if ((l->l_audit[cnt].bindflags & LA_FLG_BINDFROM) != 0
295                       && (result->l_audit[cnt].bindflags & LA_FLG_BINDTO) != 0)
296                     {
297                       unsigned int flags = altvalue;
298                       if (afct->symbind != NULL)
299                         {
300                           uintptr_t new_value
301                             = afct->symbind (&sym, reloc_result->boundndx,
302                                              &l->l_audit[cnt].cookie,
303                                              &result->l_audit[cnt].cookie,
304                                              &flags,
305                                              strtab2 + defsym->st_name);
306                           if (new_value != (uintptr_t) sym.st_value)
307                             {
308                               altvalue = LA_SYMB_ALTVALUE;
309                               sym.st_value = new_value;
310                             }
311                         }
312
313                       /* Remember the results for every audit library and
314                          store a summary in the first two bits.  */
315                       reloc_result->enterexit
316                         &= flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT);
317                       reloc_result->enterexit
318                         |= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
319                             << ((cnt + 1) * 2));
320                     }
321                   else
322                     /* If the bind flags say this auditor is not interested,
323                        set the bits manually.  */
324                     reloc_result->enterexit
325                       |= ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT)
326                           << ((cnt + 1) * 2));
327
328                   afct = afct->next;
329                 }
330
331               reloc_result->flags = altvalue;
332               value = DL_FIXUP_ADDR_VALUE (sym.st_value);
333             }
334           else
335             /* Set all bits since this symbol binding is not interesting.  */
336             reloc_result->enterexit = (1u << DL_NNS) - 1;
337         }
338 #endif
339
340       /* Store the result for later runs.  */
341       if (__builtin_expect (! GLRO(dl_bind_not), 1))
342         *resultp = value;
343     }
344
345   /* By default we do not call the pltexit function.  */
346   long int framesize = -1;
347
348 #ifdef SHARED
349   /* Auditing checkpoint: report the PLT entering and allow the
350      auditors to change the value.  */
351   if (DL_FIXUP_VALUE_CODE_ADDR (value) != 0 && GLRO(dl_naudit) > 0
352       /* Don't do anything if no auditor wants to intercept this call.  */
353       && (reloc_result->enterexit & LA_SYMB_NOPLTENTER) == 0)
354     {
355       ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
356                                                 l_info[DT_SYMTAB])
357                            + reloc_result->boundndx);
358
359       /* Set up the sym parameter.  */
360       ElfW(Sym) sym = *defsym;
361       sym.st_value = DL_FIXUP_VALUE_ADDR (value);
362
363       /* Get the symbol name.  */
364       const char *strtab = (const void *) D_PTR (reloc_result->bound,
365                                                  l_info[DT_STRTAB]);
366       const char *symname = strtab + sym.st_name;
367
368       /* Keep track of overwritten addresses.  */
369       unsigned int altvalue = reloc_result->flags;
370
371       struct audit_ifaces *afct = GLRO(dl_audit);
372       for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
373         {
374           if (afct->ARCH_LA_PLTENTER != NULL
375               && (reloc_result->enterexit
376                   & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0)
377             {
378               unsigned int flags = altvalue;
379               long int new_framesize = -1;
380               uintptr_t new_value
381                 = afct->ARCH_LA_PLTENTER (&sym, reloc_result->boundndx,
382                                           &l->l_audit[cnt].cookie,
383                                           &reloc_result->bound->l_audit[cnt].cookie,
384                                           regs, &flags, symname,
385                                           &new_framesize);
386               if (new_value != (uintptr_t) sym.st_value)
387                 {
388                   altvalue = LA_SYMB_ALTVALUE;
389                   sym.st_value = new_value;
390                 }
391
392               /* Remember the results for every audit library and
393                  store a summary in the first two bits.  */
394               reloc_result->enterexit
395                 |= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
396                     << (2 * (cnt + 1)));
397
398               if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT
399                                               << (2 * (cnt + 1))))
400                   == 0 && new_framesize != -1 && framesize != -2)
401                 {
402                   /* If this is the first call providing information,
403                      use it.  */
404                   if (framesize == -1)
405                     framesize = new_framesize;
406                   /* If two pltenter calls provide conflicting information,
407                      use the larger value.  */
408                   else if (new_framesize != framesize)
409                     framesize = MAX (new_framesize, framesize);
410                 }
411             }
412
413           afct = afct->next;
414         }
415
416       value = DL_FIXUP_ADDR_VALUE (sym.st_value);
417     }
418 #endif
419
420   /* Store the frame size information.  */
421   *framesizep = framesize;
422
423   (*mcount_fct) (retaddr, DL_FIXUP_VALUE_CODE_ADDR (value));
424
425   return value;
426 }
427
428 #endif /* PROF && ELF_MACHINE_NO_PLT */
429
430
431 #include <stdio.h>
432 void
433 ARCH_FIXUP_ATTRIBUTE
434 _dl_call_pltexit (struct link_map *l, ElfW(Word) reloc_arg,
435                   const void *inregs, void *outregs)
436 {
437 #ifdef SHARED
438   /* This is the address in the array where we store the result of previous
439      relocations.  */
440   // XXX Maybe the bound information must be stored on the stack since
441   // XXX with bind_not a new value could have been stored in the meantime.
442   struct reloc_result *reloc_result = &l->l_reloc_result[reloc_index];
443   ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
444                                             l_info[DT_SYMTAB])
445                        + reloc_result->boundndx);
446
447   /* Set up the sym parameter.  */
448   ElfW(Sym) sym = *defsym;
449   sym.st_value = DL_FIXUP_VALUE_ADDR (reloc_result->addr);
450
451   /* Get the symbol name.  */
452   const char *strtab = (const void *) D_PTR (reloc_result->bound,
453                                              l_info[DT_STRTAB]);
454   const char *symname = strtab + sym.st_name;
455
456   struct audit_ifaces *afct = GLRO(dl_audit);
457   for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
458     {
459       if (afct->ARCH_LA_PLTEXIT != NULL
460           && (reloc_result->enterexit
461               & (LA_SYMB_NOPLTEXIT >> (2 * cnt))) == 0)
462         {
463           afct->ARCH_LA_PLTEXIT (&sym, reloc_result->boundndx,
464                                  &l->l_audit[cnt].cookie,
465                                  &reloc_result->bound->l_audit[cnt].cookie,
466                                  inregs, outregs, symname);
467         }
468
469       afct = afct->next;
470     }
471 #endif
472 }