[BZ #15022] Correct global-scope dlopen issues in static executables.
[platform/upstream/glibc.git] / elf / dl-support.c
1 /* Support for dynamic linking code in static libc.
2    Copyright (C) 1996-2013 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    <http://www.gnu.org/licenses/>.  */
18
19 /* This file defines some things that for the dynamic linker are defined in
20    rtld.c and dl-sysdep.c in ways appropriate to bootstrap dynamic linking.  */
21
22 #include <errno.h>
23 #include <libintl.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/param.h>
27 #include <stdint.h>
28 #include <ldsodefs.h>
29 #include <dl-machine.h>
30 #include <bits/libc-lock.h>
31 #include <dl-cache.h>
32 #include <dl-librecon.h>
33 #include <dl-procinfo.h>
34 #include <unsecvars.h>
35 #include <hp-timing.h>
36 #include <stackinfo.h>
37
38 extern char *__progname;
39 char **_dl_argv = &__progname;  /* This is checked for some error messages.  */
40
41 /* Name of the architecture.  */
42 const char *_dl_platform;
43 size_t _dl_platformlen;
44
45 int _dl_debug_mask;
46 int _dl_lazy;
47 ElfW(Addr) _dl_use_load_bias = -2;
48 int _dl_dynamic_weak;
49
50 /* If nonzero print warnings about problematic situations.  */
51 int _dl_verbose;
52
53 /* We never do profiling.  */
54 const char *_dl_profile;
55 const char *_dl_profile_output;
56
57 /* Names of shared object for which the RUNPATHs and RPATHs should be
58    ignored.  */
59 const char *_dl_inhibit_rpath;
60
61 /* The map for the object we will profile.  */
62 struct link_map *_dl_profile_map;
63
64 /* This is the address of the last stack address ever used.  */
65 void *__libc_stack_end;
66
67 /* Path where the binary is found.  */
68 const char *_dl_origin_path;
69
70 /* Nonzero if runtime lookup should not update the .got/.plt.  */
71 int _dl_bind_not;
72
73 /* A dummy link map for the executable, used by dlopen to access the global
74    scope.  We don't export any symbols ourselves, so this can be minimal.  */
75 static struct link_map _dl_main_map =
76   {
77     .l_name = (char *) "",
78     .l_real = &_dl_main_map,
79     .l_ns = LM_ID_BASE,
80     .l_libname = &(struct libname_list) { .name = "", .dont_free = 1 },
81     .l_searchlist =
82       {
83         .r_list = &(struct link_map *) { &_dl_main_map },
84         .r_nlist = 1,
85       },
86     .l_symbolic_searchlist = { .r_list = &(struct link_map *) { NULL } },
87     .l_type = lt_executable,
88     .l_scope_mem = { &_dl_main_map.l_searchlist },
89     .l_scope_max = (sizeof (_dl_main_map.l_scope_mem)
90                     / sizeof (_dl_main_map.l_scope_mem[0])),
91     .l_scope = _dl_main_map.l_scope_mem,
92     .l_local_scope = { &_dl_main_map.l_searchlist },
93     .l_used = 1,
94     .l_tls_offset = NO_TLS_OFFSET,
95     .l_serial = 1,
96   };
97
98 /* Namespace information.  */
99 struct link_namespaces _dl_ns[DL_NNS] =
100   {
101     [LM_ID_BASE] =
102       {
103         ._ns_loaded = &_dl_main_map,
104         ._ns_nloaded = 1,
105         ._ns_main_searchlist = &_dl_main_map.l_searchlist,
106       }
107   };
108 size_t _dl_nns = 1;
109
110 /* Incremented whenever something may have been added to dl_loaded. */
111 unsigned long long _dl_load_adds = 1;
112
113 /* Fake scope of the main application.  */
114 struct r_scope_elem _dl_initial_searchlist =
115   {
116     .r_list = &(struct link_map *) { &_dl_main_map },
117     .r_nlist = 1,
118   };
119
120 #ifndef HAVE_INLINED_SYSCALLS
121 /* Nonzero during startup.  */
122 int _dl_starting_up = 1;
123 #endif
124
125 /* Random data provided by the kernel.  */
126 void *_dl_random;
127
128 /* Get architecture specific initializer.  */
129 #include <dl-procinfo.c>
130
131 /* We expect less than a second for relocation.  */
132 #ifdef HP_SMALL_TIMING_AVAIL
133 # undef HP_TIMING_AVAIL
134 # define HP_TIMING_AVAIL HP_SMALL_TIMING_AVAIL
135 #endif
136
137 /* Initial value of the CPU clock.  */
138 #ifndef HP_TIMING_NONAVAIL
139 hp_timing_t _dl_cpuclock_offset;
140 #endif
141
142 void (*_dl_init_static_tls) (struct link_map *) = &_dl_nothread_init_static_tls;
143
144 size_t _dl_pagesize = EXEC_PAGESIZE;
145
146 int _dl_inhibit_cache;
147
148 unsigned int _dl_osversion;
149
150 /* All known directories in sorted order.  */
151 struct r_search_path_elem *_dl_all_dirs;
152
153 /* All directories after startup.  */
154 struct r_search_path_elem *_dl_init_all_dirs;
155
156 /* The object to be initialized first.  */
157 struct link_map *_dl_initfirst;
158
159 /* Descriptor to write debug messages to.  */
160 int _dl_debug_fd = STDERR_FILENO;
161
162 int _dl_correct_cache_id = _DL_CACHE_DEFAULT_ID;
163
164 ElfW(auxv_t) *_dl_auxv;
165 const ElfW(Phdr) *_dl_phdr;
166 size_t _dl_phnum;
167 uint64_t _dl_hwcap __attribute__ ((nocommon));
168
169 /* This is not initialized to HWCAP_IMPORTANT, matching the definition
170    of _dl_important_hwcaps, below, where no hwcap strings are ever
171    used.  This mask is still used to mediate the lookups in the cache
172    file.  Since there is no way to set this nonzero (we don't grok the
173    LD_HWCAP_MASK environment variable here), there is no real point in
174    setting _dl_hwcap nonzero below, but we do anyway.  */
175 uint64_t _dl_hwcap_mask __attribute__ ((nocommon));
176
177 /* Prevailing state of the stack.  Generally this includes PF_X, indicating it's
178  * executable but this isn't true for all platforms.  */
179 ElfW(Word) _dl_stack_flags = DEFAULT_STACK_PERMS;
180
181 /* If loading a shared object requires that we make the stack executable
182    when it was not, we do it by calling this function.
183    It returns an errno code or zero on success.  */
184 int (*_dl_make_stack_executable_hook) (void **) internal_function
185   = _dl_make_stack_executable;
186
187
188 /* Function in libpthread to wait for termination of lookups.  */
189 void (*_dl_wait_lookup_done) (void);
190
191 struct dl_scope_free_list *_dl_scope_free_list;
192
193 #ifdef NEED_DL_SYSINFO
194 /* Needed for improved syscall handling on at least x86/Linux.  */
195 uintptr_t _dl_sysinfo = DL_SYSINFO_DEFAULT;
196 #endif
197 #ifdef NEED_DL_SYSINFO_DSO
198 /* Address of the ELF headers in the vsyscall page.  */
199 const ElfW(Ehdr) *_dl_sysinfo_dso;
200
201 struct link_map *_dl_sysinfo_map;
202
203 # include "get-dynamic-info.h"
204 #endif
205 #include "setup-vdso.h"
206
207 /* During the program run we must not modify the global data of
208    loaded shared object simultanously in two threads.  Therefore we
209    protect `_dl_open' and `_dl_close' in dl-close.c.
210
211    This must be a recursive lock since the initializer function of
212    the loaded object might as well require a call to this function.
213    At this time it is not anymore a problem to modify the tables.  */
214 __rtld_lock_define_initialized_recursive (, _dl_load_lock)
215 /* This lock is used to keep __dl_iterate_phdr from inspecting the
216    list of loaded objects while an object is added to or removed from
217    that list.  */
218 __rtld_lock_define_initialized_recursive (, _dl_load_write_lock)
219
220
221 #ifdef HAVE_AUX_VECTOR
222 int _dl_clktck;
223
224 void
225 internal_function
226 _dl_aux_init (ElfW(auxv_t) *av)
227 {
228   int seen = 0;
229   uid_t uid = 0;
230   gid_t gid = 0;
231
232   _dl_auxv = av;
233   for (; av->a_type != AT_NULL; ++av)
234     switch (av->a_type)
235       {
236       case AT_PAGESZ:
237         if (av->a_un.a_val != 0)
238           GLRO(dl_pagesize) = av->a_un.a_val;
239         break;
240       case AT_CLKTCK:
241         GLRO(dl_clktck) = av->a_un.a_val;
242         break;
243       case AT_PHDR:
244         GL(dl_phdr) = (const void *) av->a_un.a_val;
245         break;
246       case AT_PHNUM:
247         GL(dl_phnum) = av->a_un.a_val;
248         break;
249       case AT_HWCAP:
250         GLRO(dl_hwcap) = (unsigned long int) av->a_un.a_val;
251         break;
252 #ifdef NEED_DL_SYSINFO
253       case AT_SYSINFO:
254         GL(dl_sysinfo) = av->a_un.a_val;
255         break;
256 #endif
257 #ifdef NEED_DL_SYSINFO_DSO
258       case AT_SYSINFO_EHDR:
259         GL(dl_sysinfo_dso) = (void *) av->a_un.a_val;
260         break;
261 #endif
262       case AT_UID:
263         uid ^= av->a_un.a_val;
264         seen |= 1;
265         break;
266       case AT_EUID:
267         uid ^= av->a_un.a_val;
268         seen |= 2;
269         break;
270       case AT_GID:
271         gid ^= av->a_un.a_val;
272         seen |= 4;
273         break;
274       case AT_EGID:
275         gid ^= av->a_un.a_val;
276         seen |= 8;
277         break;
278       case AT_SECURE:
279         seen = -1;
280         __libc_enable_secure = av->a_un.a_val;
281         __libc_enable_secure_decided = 1;
282         break;
283       case AT_RANDOM:
284         _dl_random = (void *) av->a_un.a_val;
285         break;
286 # ifdef DL_PLATFORM_AUXV
287       DL_PLATFORM_AUXV
288 # endif
289       }
290   if (seen == 0xf)
291     {
292       __libc_enable_secure = uid != 0 || gid != 0;
293       __libc_enable_secure_decided = 1;
294     }
295 }
296 #endif
297
298
299 void
300 internal_function
301 _dl_non_dynamic_init (void)
302 {
303   _dl_main_map.l_origin = _dl_get_origin ();
304
305   if (HP_TIMING_AVAIL)
306     HP_TIMING_NOW (_dl_cpuclock_offset);
307
308   _dl_verbose = *(getenv ("LD_WARN") ?: "") == '\0' ? 0 : 1;
309
310   /* Set up the data structures for the system-supplied DSO early,
311      so they can influence _dl_init_paths.  */
312   setup_vdso (NULL, NULL);
313
314   /* Initialize the data structures for the search paths for shared
315      objects.  */
316   _dl_init_paths (getenv ("LD_LIBRARY_PATH"));
317
318   /* Remember the last search directory added at startup.  */
319   _dl_init_all_dirs = GL(dl_all_dirs);
320
321   _dl_lazy = *(getenv ("LD_BIND_NOW") ?: "") == '\0';
322
323   _dl_bind_not = *(getenv ("LD_BIND_NOT") ?: "") != '\0';
324
325   _dl_dynamic_weak = *(getenv ("LD_DYNAMIC_WEAK") ?: "") == '\0';
326
327   _dl_profile_output = getenv ("LD_PROFILE_OUTPUT");
328   if (_dl_profile_output == NULL || _dl_profile_output[0] == '\0')
329     _dl_profile_output
330       = &"/var/tmp\0/var/profile"[__libc_enable_secure ? 9 : 0];
331
332   if (__libc_enable_secure)
333     {
334       static const char unsecure_envvars[] =
335         UNSECURE_ENVVARS
336 #ifdef EXTRA_UNSECURE_ENVVARS
337         EXTRA_UNSECURE_ENVVARS
338 #endif
339         ;
340       const char *cp = unsecure_envvars;
341
342       while (cp < unsecure_envvars + sizeof (unsecure_envvars))
343         {
344           __unsetenv (cp);
345           cp = (const char *) __rawmemchr (cp, '\0') + 1;
346         }
347
348       if (__access ("/etc/suid-debug", F_OK) != 0)
349         __unsetenv ("MALLOC_CHECK_");
350     }
351
352 #ifdef DL_PLATFORM_INIT
353   DL_PLATFORM_INIT;
354 #endif
355
356 #ifdef DL_OSVERSION_INIT
357   DL_OSVERSION_INIT;
358 #endif
359
360   /* Now determine the length of the platform string.  */
361   if (_dl_platform != NULL)
362     _dl_platformlen = strlen (_dl_platform);
363
364   /* Scan for a program header telling us the stack is nonexecutable.  */
365   if (_dl_phdr != NULL)
366     for (uint_fast16_t i = 0; i < _dl_phnum; ++i)
367       if (_dl_phdr[i].p_type == PT_GNU_STACK)
368         {
369           _dl_stack_flags = _dl_phdr[i].p_flags;
370           break;
371         }
372 }
373
374 #ifdef DL_SYSINFO_IMPLEMENTATION
375 DL_SYSINFO_IMPLEMENTATION
376 #endif