]>
Commit | Line | Data |
---|---|---|
0a54e401 | 1 | /* Map in a shared object's segments from the file. |
04277e02 | 2 | Copyright (C) 1995-2019 Free Software Foundation, Inc. |
afd4eb37 | 3 | This file is part of the GNU C Library. |
d66e34cd | 4 | |
afd4eb37 | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
d66e34cd | 9 | |
afd4eb37 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
d66e34cd | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
d66e34cd | 18 | |
14e9dd67 | 19 | #include <elf.h> |
0a54e401 UD |
20 | #include <errno.h> |
21 | #include <fcntl.h> | |
8e17ea58 | 22 | #include <libintl.h> |
379d4ec4 | 23 | #include <stdbool.h> |
0a54e401 | 24 | #include <stdlib.h> |
d66e34cd | 25 | #include <string.h> |
d66e34cd | 26 | #include <unistd.h> |
a42195db | 27 | #include <ldsodefs.h> |
ca97fb53 | 28 | #include <bits/wordsize.h> |
0a54e401 | 29 | #include <sys/mman.h> |
f8f7e090 | 30 | #include <sys/param.h> |
0a54e401 UD |
31 | #include <sys/stat.h> |
32 | #include <sys/types.h> | |
f753fa7d L |
33 | |
34 | /* Type for the buffer we put the ELF header and hopefully the program | |
35 | header. This buffer does not really have to be too large. In most | |
36 | cases the program header follows the ELF header directly. If this | |
37 | is not the case all bets are off and we can make the header | |
38 | arbitrarily large and still won't get it read. This means the only | |
39 | question is how large are the ELF and program header combined. The | |
40 | ELF header 32-bit files is 52 bytes long and in 64-bit files is 64 | |
41 | bytes long. Each program header entry is again 32 and 56 bytes | |
42 | long respectively. I.e., even with a file which has 10 program | |
43 | header entries we only have to read 372B/624B respectively. Add to | |
44 | this a bit of margin for program notes and reading 512B and 832B | |
45 | for 32-bit and 64-bit files respecitvely is enough. If this | |
46 | heuristic should really fail for some file the code in | |
47 | `_dl_map_object_from_fd' knows how to recover. */ | |
48 | struct filebuf | |
49 | { | |
50 | ssize_t len; | |
51 | #if __WORDSIZE == 32 | |
52 | # define FILEBUF_SIZE 512 | |
53 | #else | |
54 | # define FILEBUF_SIZE 832 | |
55 | #endif | |
56 | char buf[FILEBUF_SIZE] __attribute__ ((aligned (__alignof (ElfW(Ehdr))))); | |
57 | }; | |
58 | ||
d66e34cd | 59 | #include "dynamic-link.h" |
a986484f | 60 | #include <abi-tag.h> |
664e7d93 | 61 | #include <stackinfo.h> |
606832e6 | 62 | #include <sysdep.h> |
815e6fa3 | 63 | #include <stap-probe.h> |
9090848d | 64 | #include <libc-pointer-arith.h> |
8a0b17e4 | 65 | #include <array_length.h> |
d66e34cd | 66 | |
dc5efe83 | 67 | #include <dl-dst.h> |
fcccd512 RM |
68 | #include <dl-load.h> |
69 | #include <dl-map-segments.h> | |
70 | #include <dl-unmap-segments.h> | |
d6f373d2 | 71 | #include <dl-machine-reject-phdr.h> |
c0d6f2a3 | 72 | #include <dl-sysdep-open.h> |
f753fa7d | 73 | #include <dl-prop.h> |
329ea513 | 74 | #include <not-cancel.h> |
9b8a44cd | 75 | |
d66e34cd RM |
76 | #include <endian.h> |
77 | #if BYTE_ORDER == BIG_ENDIAN | |
fcf70d41 | 78 | # define byteorder ELFDATA2MSB |
d66e34cd | 79 | #elif BYTE_ORDER == LITTLE_ENDIAN |
fcf70d41 | 80 | # define byteorder ELFDATA2LSB |
d66e34cd | 81 | #else |
fcf70d41 UD |
82 | # error "Unknown BYTE_ORDER " BYTE_ORDER |
83 | # define byteorder ELFDATANONE | |
d66e34cd RM |
84 | #endif |
85 | ||
14e9dd67 | 86 | #define STRING(x) __STRING (x) |
d66e34cd | 87 | |
dcca3fe2 | 88 | |
664e7d93 | 89 | int __stack_prot attribute_hidden attribute_relro |
44828b9d | 90 | #if _STACK_GROWS_DOWN && defined PROT_GROWSDOWN |
ecc1d0c3 | 91 | = PROT_GROWSDOWN; |
44828b9d | 92 | #elif _STACK_GROWS_UP && defined PROT_GROWSUP |
ecc1d0c3 | 93 | = PROT_GROWSUP; |
f8286ce6 RM |
94 | #else |
95 | = 0; | |
664e7d93 UD |
96 | #endif |
97 | ||
dcca3fe2 | 98 | |
7ef90c15 | 99 | /* This is the decomposed LD_LIBRARY_PATH search path. */ |
392a6b52 | 100 | static struct r_search_path_struct env_path_list attribute_relro; |
40a55d20 | 101 | |
12264bd7 | 102 | /* List of the hardware capabilities we might end up using. */ |
392a6b52 UD |
103 | static const struct r_strlenpair *capstr attribute_relro; |
104 | static size_t ncapstr attribute_relro; | |
105 | static size_t max_capstrlen attribute_relro; | |
12264bd7 | 106 | |
40a55d20 | 107 | |
8a0b17e4 FW |
108 | /* Get the generated information about the trusted directories. Use |
109 | an array of concatenated strings to avoid relocations. See | |
110 | gen-trusted-dirs.awk. */ | |
ab7eb292 UD |
111 | #include "trusted-dirs.h" |
112 | ||
113 | static const char system_dirs[] = SYSTEM_DIRS; | |
114 | static const size_t system_dirs_len[] = | |
115 | { | |
116 | SYSTEM_DIRS_LEN | |
117 | }; | |
8a0b17e4 | 118 | #define nsystem_dirs_len array_length (system_dirs_len) |
32c85e43 | 119 | |
47c3cd7a UD |
120 | static bool |
121 | is_trusted_path_normalize (const char *path, size_t len) | |
122 | { | |
22836f52 UD |
123 | if (len == 0) |
124 | return false; | |
125 | ||
47c3cd7a UD |
126 | char *npath = (char *) alloca (len + 2); |
127 | char *wnp = npath; | |
47c3cd7a UD |
128 | while (*path != '\0') |
129 | { | |
130 | if (path[0] == '/') | |
131 | { | |
132 | if (path[1] == '.') | |
133 | { | |
134 | if (path[2] == '.' && (path[3] == '/' || path[3] == '\0')) | |
135 | { | |
136 | while (wnp > npath && *--wnp != '/') | |
137 | ; | |
138 | path += 3; | |
139 | continue; | |
140 | } | |
141 | else if (path[2] == '/' || path[2] == '\0') | |
142 | { | |
143 | path += 2; | |
144 | continue; | |
145 | } | |
146 | } | |
147 | ||
148 | if (wnp > npath && wnp[-1] == '/') | |
149 | { | |
150 | ++path; | |
151 | continue; | |
152 | } | |
153 | } | |
154 | ||
155 | *wnp++ = *path++; | |
156 | } | |
22836f52 UD |
157 | |
158 | if (wnp == npath || wnp[-1] != '/') | |
47c3cd7a | 159 | *wnp++ = '/'; |
47c3cd7a | 160 | |
22836f52 UD |
161 | const char *trun = system_dirs; |
162 | ||
163 | for (size_t idx = 0; idx < nsystem_dirs_len; ++idx) | |
164 | { | |
165 | if (wnp - npath >= system_dirs_len[idx] | |
166 | && memcmp (trun, npath, system_dirs_len[idx]) == 0) | |
167 | /* Found it. */ | |
168 | return true; | |
169 | ||
170 | trun += system_dirs_len[idx] + 1; | |
171 | } | |
172 | ||
173 | return false; | |
47c3cd7a UD |
174 | } |
175 | ||
5aad5f61 CD |
176 | /* Given a substring starting at INPUT, just after the DST '$' start |
177 | token, determine if INPUT contains DST token REF, following the | |
178 | ELF gABI rules for DSTs: | |
179 | ||
180 | * Longest possible sequence using the rules (greedy). | |
181 | ||
182 | * Must start with a $ (enforced by caller). | |
183 | ||
184 | * Must follow $ with one underscore or ASCII [A-Za-z] (caller | |
185 | follows these rules for REF) or '{' (start curly quoted name). | |
47c3cd7a | 186 | |
5aad5f61 CD |
187 | * Must follow first two characters with zero or more [A-Za-z0-9_] |
188 | (enforced by caller) or '}' (end curly quoted name). | |
189 | ||
190 | If the sequence is a DST matching REF then the length of the DST | |
191 | (excluding the $ sign but including curly braces, if any) is | |
192 | returned, otherwise 0. */ | |
6d5d3ae3 | 193 | static size_t |
5aad5f61 | 194 | is_dst (const char *input, const char *ref) |
6d5d3ae3 | 195 | { |
379d4ec4 | 196 | bool is_curly = false; |
6d5d3ae3 | 197 | |
5aad5f61 CD |
198 | /* Is a ${...} input sequence? */ |
199 | if (input[0] == '{') | |
379d4ec4 UD |
200 | { |
201 | is_curly = true; | |
5aad5f61 | 202 | ++input; |
379d4ec4 | 203 | } |
6d5d3ae3 | 204 | |
5aad5f61 CD |
205 | /* Check for matching name, following closing curly brace (if |
206 | required), or trailing characters which are part of an | |
207 | identifier. */ | |
208 | size_t rlen = strlen (ref); | |
209 | if (strncmp (input, ref, rlen) != 0 | |
210 | || (is_curly && input[rlen] != '}') | |
211 | || ((input[rlen] >= 'A' && input[rlen] <= 'Z') | |
212 | || (input[rlen] >= 'a' && input[rlen] <= 'z') | |
213 | || (input[rlen] >= '0' && input[rlen] <= '9') | |
214 | || (input[rlen] == '_'))) | |
6d5d3ae3 UD |
215 | return 0; |
216 | ||
5aad5f61 CD |
217 | if (is_curly) |
218 | /* Count the two curly braces. */ | |
219 | return rlen + 2; | |
220 | else | |
221 | return rlen; | |
6d5d3ae3 UD |
222 | } |
223 | ||
a745c837 CD |
224 | /* INPUT should be the start of a path e.g DT_RPATH or name e.g. |
225 | DT_NEEDED. The return value is the number of known DSTs found. We | |
226 | count all known DSTs regardless of __libc_enable_secure; the caller | |
227 | is responsible for enforcing the security of the substitution rules | |
228 | (usually _dl_dst_substitute). */ | |
dc5efe83 | 229 | size_t |
5aad5f61 | 230 | _dl_dst_count (const char *input) |
f787edde | 231 | { |
f787edde | 232 | size_t cnt = 0; |
f787edde | 233 | |
5aad5f61 CD |
234 | input = strchr (input, '$'); |
235 | ||
236 | /* Most likely there is no DST. */ | |
237 | if (__glibc_likely (input == NULL)) | |
238 | return 0; | |
239 | ||
dc5efe83 | 240 | do |
f787edde | 241 | { |
379d4ec4 | 242 | size_t len; |
f787edde | 243 | |
5aad5f61 CD |
244 | ++input; |
245 | /* All DSTs must follow ELF gABI rules, see is_dst (). */ | |
246 | if ((len = is_dst (input, "ORIGIN")) != 0 | |
247 | || (len = is_dst (input, "PLATFORM")) != 0 | |
248 | || (len = is_dst (input, "LIB")) != 0) | |
f787edde UD |
249 | ++cnt; |
250 | ||
5aad5f61 CD |
251 | /* There may be more than one DST in the input. */ |
252 | input = strchr (input + len, '$'); | |
f787edde | 253 | } |
5aad5f61 | 254 | while (input != NULL); |
f787edde | 255 | |
dc5efe83 UD |
256 | return cnt; |
257 | } | |
f787edde | 258 | |
5aad5f61 CD |
259 | /* Process INPUT for DSTs and store in RESULT using the information |
260 | from link map L to resolve the DSTs. This function only handles one | |
261 | path at a time and does not handle colon-separated path lists (see | |
262 | fillin_rpath ()). Lastly the size of result in bytes should be at | |
263 | least equal to the value returned by DL_DST_REQUIRED. Note that it | |
264 | is possible for a DT_NEEDED, DT_AUXILIARY, and DT_FILTER entries to | |
265 | have colons, but we treat those as literal colons here, not as path | |
266 | list delimeters. */ | |
dc5efe83 | 267 | char * |
5aad5f61 | 268 | _dl_dst_substitute (struct link_map *l, const char *input, char *result) |
dc5efe83 | 269 | { |
5aad5f61 CD |
270 | /* Copy character-by-character from input into the working pointer |
271 | looking for any DSTs. We track the start of input and if we are | |
272 | going to check for trusted paths, all of which are part of $ORIGIN | |
273 | handling in SUID/SGID cases (see below). In some cases, like when | |
274 | a DST cannot be replaced, we may set result to an empty string and | |
275 | return. */ | |
47c3cd7a | 276 | char *wp = result; |
5aad5f61 | 277 | const char *start = input; |
47c3cd7a | 278 | bool check_for_trusted = false; |
dc5efe83 | 279 | |
f787edde UD |
280 | do |
281 | { | |
5aad5f61 | 282 | if (__glibc_unlikely (*input == '$')) |
f787edde | 283 | { |
2541eda0 | 284 | const char *repl = NULL; |
379d4ec4 | 285 | size_t len; |
2541eda0 | 286 | |
5aad5f61 CD |
287 | ++input; |
288 | if ((len = is_dst (input, "ORIGIN")) != 0) | |
e7c8359e | 289 | { |
5aad5f61 CD |
290 | /* For SUID/GUID programs we normally ignore the path with |
291 | $ORIGIN in DT_RUNPATH, or DT_RPATH. However, there is | |
292 | one exception to this rule, and it is: | |
293 | ||
294 | * $ORIGIN appears as the first path element, and is | |
295 | the only string in the path or is immediately | |
296 | followed by a path separator and the rest of the | |
a745c837 CD |
297 | path, |
298 | ||
299 | and ... | |
5aad5f61 CD |
300 | |
301 | * The path is rooted in a trusted directory. | |
302 | ||
303 | This exception allows such programs to reference | |
304 | shared libraries in subdirectories of trusted | |
305 | directories. The use case is one of general | |
306 | organization and deployment flexibility. | |
307 | Trusted directories are usually such paths as "/lib64" | |
308 | or "/usr/lib64", and the usual RPATHs take the form of | |
309 | [$ORIGIN/../$LIB/somedir]. */ | |
310 | if (__glibc_unlikely (__libc_enable_secure) | |
311 | && !(input == start + 1 | |
312 | && (input[len] == '\0' || input[len] == '/'))) | |
313 | repl = (const char *) -1; | |
314 | else | |
315 | repl = l->l_origin; | |
316 | ||
6bc6bd3b | 317 | check_for_trusted = (__libc_enable_secure |
47c3cd7a | 318 | && l->l_type == lt_executable); |
e7c8359e | 319 | } |
5aad5f61 | 320 | else if ((len = is_dst (input, "PLATFORM")) != 0) |
afdca0f2 | 321 | repl = GLRO(dl_platform); |
5aad5f61 | 322 | else if ((len = is_dst (input, "LIB")) != 0) |
27af5372 | 323 | repl = DL_DST_LIB; |
2541eda0 | 324 | |
2541eda0 UD |
325 | if (repl != NULL && repl != (const char *) -1) |
326 | { | |
327 | wp = __stpcpy (wp, repl); | |
5aad5f61 | 328 | input += len; |
2541eda0 | 329 | } |
5aad5f61 | 330 | else if (len != 0) |
f787edde | 331 | { |
5aad5f61 CD |
332 | /* We found a valid DST that we know about, but we could |
333 | not find a replacement value for it, therefore we | |
334 | cannot use this path and discard it. */ | |
335 | *result = '\0'; | |
336 | return result; | |
f787edde UD |
337 | } |
338 | else | |
27aa0631 | 339 | /* No DST we recognize. */ |
379d4ec4 | 340 | *wp++ = '$'; |
f787edde | 341 | } |
2541eda0 | 342 | else |
f787edde | 343 | { |
5aad5f61 | 344 | *wp++ = *input++; |
f787edde | 345 | } |
f787edde | 346 | } |
5aad5f61 | 347 | while (*input != '\0'); |
f787edde | 348 | |
47c3cd7a | 349 | /* In SUID/SGID programs, after $ORIGIN expansion the normalized |
5aad5f61 CD |
350 | path must be rooted in one of the trusted directories. The $LIB |
351 | and $PLATFORM DST cannot in any way be manipulated by the caller | |
352 | because they are fixed values that are set by the dynamic loader | |
353 | and therefore any paths using just $LIB or $PLATFORM need not be | |
354 | checked for trust, the authors of the binaries themselves are | |
355 | trusted to have designed this correctly. Only $ORIGIN is tested in | |
356 | this way because it may be manipulated in some ways with hard | |
357 | links. */ | |
1b26b855 | 358 | if (__glibc_unlikely (check_for_trusted) |
5aad5f61 CD |
359 | && !is_trusted_path_normalize (result, wp - result)) |
360 | { | |
361 | *result = '\0'; | |
362 | return result; | |
363 | } | |
47c3cd7a | 364 | |
f787edde UD |
365 | *wp = '\0'; |
366 | ||
367 | return result; | |
368 | } | |
369 | ||
dc5efe83 | 370 | |
5aad5f61 CD |
371 | /* Return a malloc allocated copy of INPUT with all recognized DSTs |
372 | replaced. On some platforms it might not be possible to determine the | |
373 | path from which the object belonging to the map is loaded. In this | |
374 | case the path containing the DST is left out. On error NULL | |
375 | is returned. */ | |
dc5efe83 | 376 | static char * |
5aad5f61 | 377 | expand_dynamic_string_token (struct link_map *l, const char *input) |
dc5efe83 UD |
378 | { |
379 | /* We make two runs over the string. First we determine how large the | |
844c394a | 380 | resulting string is and then we copy it over. Since this is no |
dc5efe83 UD |
381 | frequently executed operation we are looking here not for performance |
382 | but rather for code size. */ | |
383 | size_t cnt; | |
384 | size_t total; | |
385 | char *result; | |
386 | ||
5aad5f61 CD |
387 | /* Determine the number of DSTs. */ |
388 | cnt = _dl_dst_count (input); | |
dc5efe83 UD |
389 | |
390 | /* If we do not have to replace anything simply copy the string. */ | |
1b26b855 | 391 | if (__glibc_likely (cnt == 0)) |
5aad5f61 | 392 | return __strdup (input); |
dc5efe83 UD |
393 | |
394 | /* Determine the length of the substituted string. */ | |
5aad5f61 | 395 | total = DL_DST_REQUIRED (l, input, strlen (input), cnt); |
dc5efe83 UD |
396 | |
397 | /* Allocate the necessary memory. */ | |
398 | result = (char *) malloc (total + 1); | |
399 | if (result == NULL) | |
400 | return NULL; | |
401 | ||
5aad5f61 | 402 | return _dl_dst_substitute (l, input, result); |
dc5efe83 UD |
403 | } |
404 | ||
405 | ||
0413b54c UD |
406 | /* Add `name' to the list of names for a particular shared object. |
407 | `name' is expected to have been allocated with malloc and will | |
408 | be freed if the shared object already has this name. | |
409 | Returns false if the object already had this name. */ | |
76156ea1 | 410 | static void |
76156ea1 | 411 | add_name_to_object (struct link_map *l, const char *name) |
0a54e401 | 412 | { |
0413b54c UD |
413 | struct libname_list *lnp, *lastp; |
414 | struct libname_list *newname; | |
76156ea1 | 415 | size_t name_len; |
0413b54c UD |
416 | |
417 | lastp = NULL; | |
418 | for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next) | |
419 | if (strcmp (name, lnp->name) == 0) | |
76156ea1 | 420 | return; |
0413b54c | 421 | |
76156ea1 | 422 | name_len = strlen (name) + 1; |
839be784 | 423 | newname = (struct libname_list *) malloc (sizeof *newname + name_len); |
0413b54c | 424 | if (newname == NULL) |
da832465 UD |
425 | { |
426 | /* No more memory. */ | |
154d10bd | 427 | _dl_signal_error (ENOMEM, name, NULL, N_("cannot allocate name record")); |
76156ea1 | 428 | return; |
da832465 | 429 | } |
0413b54c UD |
430 | /* The object should have a libname set from _dl_new_object. */ |
431 | assert (lastp != NULL); | |
432 | ||
76156ea1 | 433 | newname->name = memcpy (newname + 1, name, name_len); |
0413b54c | 434 | newname->next = NULL; |
752a2a50 | 435 | newname->dont_free = 0; |
0413b54c | 436 | lastp->next = newname; |
0413b54c UD |
437 | } |
438 | ||
12264bd7 | 439 | /* Standard search directories. */ |
392a6b52 | 440 | static struct r_search_path_struct rtld_search_dirs attribute_relro; |
0a54e401 | 441 | |
a658675d | 442 | static size_t max_dirnamelen; |
0a54e401 | 443 | |
dd9423a6 | 444 | static struct r_search_path_elem ** |
0a54e401 | 445 | fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep, |
10e93d96 | 446 | const char *what, const char *where, struct link_map *l) |
0a54e401 UD |
447 | { |
448 | char *cp; | |
449 | size_t nelems = 0; | |
450 | ||
451 | while ((cp = __strsep (&rpath, sep)) != NULL) | |
452 | { | |
453 | struct r_search_path_elem *dirp; | |
3e3c904d AJ |
454 | char *to_free = NULL; |
455 | size_t len = 0; | |
2a939a7e | 456 | |
3e3c904d AJ |
457 | /* `strsep' can pass an empty string. */ |
458 | if (*cp != '\0') | |
459 | { | |
460 | to_free = cp = expand_dynamic_string_token (l, cp); | |
2a939a7e | 461 | |
3e3c904d AJ |
462 | /* expand_dynamic_string_token can return NULL in case of empty |
463 | path or memory allocation failure. */ | |
464 | if (cp == NULL) | |
465 | continue; | |
12264bd7 | 466 | |
3e3c904d AJ |
467 | /* Compute the length after dynamic string token expansion and |
468 | ignore empty paths. */ | |
469 | len = strlen (cp); | |
470 | if (len == 0) | |
471 | { | |
472 | free (to_free); | |
473 | continue; | |
474 | } | |
12264bd7 | 475 | |
3e3c904d AJ |
476 | /* Remove trailing slashes (except for "/"). */ |
477 | while (len > 1 && cp[len - 1] == '/') | |
478 | --len; | |
0a54e401 | 479 | |
3e3c904d AJ |
480 | /* Now add one if there is none so far. */ |
481 | if (len > 0 && cp[len - 1] != '/') | |
482 | cp[len++] = '/'; | |
483 | } | |
ab7eb292 | 484 | |
0a54e401 | 485 | /* See if this directory is already known. */ |
d6b5d570 | 486 | for (dirp = GL(dl_all_dirs); dirp != NULL; dirp = dirp->next) |
12264bd7 | 487 | if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0) |
0a54e401 UD |
488 | break; |
489 | ||
490 | if (dirp != NULL) | |
491 | { | |
12264bd7 | 492 | /* It is available, see whether it's on our own list. */ |
0a54e401 UD |
493 | size_t cnt; |
494 | for (cnt = 0; cnt < nelems; ++cnt) | |
495 | if (result[cnt] == dirp) | |
496 | break; | |
497 | ||
498 | if (cnt == nelems) | |
499 | result[nelems++] = dirp; | |
500 | } | |
501 | else | |
502 | { | |
12264bd7 | 503 | size_t cnt; |
839be784 | 504 | enum r_dir_status init_val; |
4a6d1198 | 505 | size_t where_len = where ? strlen (where) + 1 : 0; |
12264bd7 | 506 | |
0a54e401 | 507 | /* It's a new directory. Create an entry and add it. */ |
12264bd7 | 508 | dirp = (struct r_search_path_elem *) |
32ee8d95 | 509 | malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status) |
f55727ca | 510 | + where_len + len + 1); |
0a54e401 | 511 | if (dirp == NULL) |
154d10bd UD |
512 | _dl_signal_error (ENOMEM, NULL, NULL, |
513 | N_("cannot create cache for search path")); | |
0a54e401 | 514 | |
f55727ca UD |
515 | dirp->dirname = ((char *) dirp + sizeof (*dirp) |
516 | + ncapstr * sizeof (enum r_dir_status)); | |
104d0bd3 | 517 | *((char *) __mempcpy ((char *) dirp->dirname, cp, len)) = '\0'; |
0a54e401 | 518 | dirp->dirnamelen = len; |
12264bd7 UD |
519 | |
520 | if (len > max_dirnamelen) | |
521 | max_dirnamelen = len; | |
522 | ||
07ba7349 UD |
523 | /* We have to make sure all the relative directories are |
524 | never ignored. The current directory might change and | |
525 | all our saved information would be void. */ | |
526 | init_val = cp[0] != '/' ? existing : unknown; | |
839be784 UD |
527 | for (cnt = 0; cnt < ncapstr; ++cnt) |
528 | dirp->status[cnt] = init_val; | |
0a54e401 | 529 | |
b5efde2f | 530 | dirp->what = what; |
a1ffb40e | 531 | if (__glibc_likely (where != NULL)) |
f55727ca | 532 | dirp->where = memcpy ((char *) dirp + sizeof (*dirp) + len + 1 |
d6b5d570 | 533 | + (ncapstr * sizeof (enum r_dir_status)), |
4a6d1198 UD |
534 | where, where_len); |
535 | else | |
536 | dirp->where = NULL; | |
b5efde2f | 537 | |
d6b5d570 UD |
538 | dirp->next = GL(dl_all_dirs); |
539 | GL(dl_all_dirs) = dirp; | |
0a54e401 UD |
540 | |
541 | /* Put it in the result array. */ | |
542 | result[nelems++] = dirp; | |
543 | } | |
2a939a7e | 544 | free (to_free); |
0a54e401 UD |
545 | } |
546 | ||
547 | /* Terminate the array. */ | |
548 | result[nelems] = NULL; | |
549 | ||
550 | return result; | |
551 | } | |
552 | ||
553 | ||
2692deea | 554 | static bool |
f55727ca UD |
555 | decompose_rpath (struct r_search_path_struct *sps, |
556 | const char *rpath, struct link_map *l, const char *what) | |
0a54e401 UD |
557 | { |
558 | /* Make a copy we can work with. */ | |
f787edde | 559 | const char *where = l->l_name; |
0a54e401 UD |
560 | char *cp; |
561 | struct r_search_path_elem **result; | |
310930c1 | 562 | size_t nelems; |
39b3385d UD |
563 | /* Initialize to please the compiler. */ |
564 | const char *errstring = NULL; | |
310930c1 | 565 | |
fcf70d41 UD |
566 | /* First see whether we must forget the RUNPATH and RPATH from this |
567 | object. */ | |
1b26b855 | 568 | if (__glibc_unlikely (GLRO(dl_inhibit_rpath) != NULL) |
6bc6bd3b | 569 | && !__libc_enable_secure) |
310930c1 | 570 | { |
afdca0f2 | 571 | const char *inhp = GLRO(dl_inhibit_rpath); |
9710f75d UD |
572 | |
573 | do | |
310930c1 | 574 | { |
9710f75d UD |
575 | const char *wp = where; |
576 | ||
577 | while (*inhp == *wp && *wp != '\0') | |
578 | { | |
579 | ++inhp; | |
580 | ++wp; | |
581 | } | |
582 | ||
583 | if (*wp == '\0' && (*inhp == '\0' || *inhp == ':')) | |
310930c1 | 584 | { |
fcf70d41 UD |
585 | /* This object is on the list of objects for which the |
586 | RUNPATH and RPATH must not be used. */ | |
2692deea UD |
587 | sps->dirs = (void *) -1; |
588 | return false; | |
310930c1 | 589 | } |
9710f75d UD |
590 | |
591 | while (*inhp != '\0') | |
592 | if (*inhp++ == ':') | |
593 | break; | |
310930c1 | 594 | } |
9710f75d | 595 | while (*inhp != '\0'); |
310930c1 | 596 | } |
0a54e401 | 597 | |
dbba87d5 DL |
598 | /* Ignore empty rpaths. */ |
599 | if (*rpath == '\0') | |
600 | { | |
601 | sps->dirs = (struct r_search_path_elem **) -1; | |
602 | return false; | |
603 | } | |
604 | ||
2a939a7e | 605 | /* Make a writable copy. */ |
dbba87d5 | 606 | char *copy = __strdup (rpath); |
f787edde | 607 | if (copy == NULL) |
39b3385d UD |
608 | { |
609 | errstring = N_("cannot create RUNPATH/RPATH copy"); | |
610 | goto signal_error; | |
611 | } | |
f787edde | 612 | |
310930c1 | 613 | /* Count the number of necessary elements in the result array. */ |
310930c1 | 614 | nelems = 0; |
0a54e401 UD |
615 | for (cp = copy; *cp != '\0'; ++cp) |
616 | if (*cp == ':') | |
617 | ++nelems; | |
618 | ||
7ef90c15 UD |
619 | /* Allocate room for the result. NELEMS + 1 is an upper limit for the |
620 | number of necessary entries. */ | |
621 | result = (struct r_search_path_elem **) malloc ((nelems + 1 + 1) | |
0a54e401 UD |
622 | * sizeof (*result)); |
623 | if (result == NULL) | |
2692deea | 624 | { |
ce31a3b1 | 625 | free (copy); |
2692deea UD |
626 | errstring = N_("cannot create cache for search path"); |
627 | signal_error: | |
628 | _dl_signal_error (ENOMEM, NULL, NULL, errstring); | |
629 | } | |
0a54e401 | 630 | |
10e93d96 | 631 | fillin_rpath (copy, result, ":", what, where, l); |
f55727ca UD |
632 | |
633 | /* Free the copied RPATH string. `fillin_rpath' make own copies if | |
634 | necessary. */ | |
635 | free (copy); | |
636 | ||
3e3c904d AJ |
637 | /* There is no path after expansion. */ |
638 | if (result[0] == NULL) | |
639 | { | |
640 | free (result); | |
641 | sps->dirs = (struct r_search_path_elem **) -1; | |
642 | return false; | |
643 | } | |
644 | ||
f55727ca UD |
645 | sps->dirs = result; |
646 | /* The caller will change this value if we haven't used a real malloc. */ | |
647 | sps->malloced = 1; | |
2692deea | 648 | return true; |
0a54e401 UD |
649 | } |
650 | ||
45e4762c RM |
651 | /* Make sure cached path information is stored in *SP |
652 | and return true if there are any paths to search there. */ | |
25337753 | 653 | static bool |
45e4762c RM |
654 | cache_rpath (struct link_map *l, |
655 | struct r_search_path_struct *sp, | |
656 | int tag, | |
657 | const char *what) | |
658 | { | |
659 | if (sp->dirs == (void *) -1) | |
660 | return false; | |
661 | ||
662 | if (sp->dirs != NULL) | |
663 | return true; | |
664 | ||
665 | if (l->l_info[tag] == NULL) | |
666 | { | |
667 | /* There is no path. */ | |
668 | sp->dirs = (void *) -1; | |
669 | return false; | |
670 | } | |
671 | ||
672 | /* Make sure the cache information is available. */ | |
2692deea UD |
673 | return decompose_rpath (sp, (const char *) (D_PTR (l, l_info[DT_STRTAB]) |
674 | + l->l_info[tag]->d_un.d_val), | |
675 | l, what); | |
45e4762c RM |
676 | } |
677 | ||
0a54e401 UD |
678 | |
679 | void | |
880f421f | 680 | _dl_init_paths (const char *llp) |
0a54e401 | 681 | { |
ab7eb292 UD |
682 | size_t idx; |
683 | const char *strp; | |
12264bd7 UD |
684 | struct r_search_path_elem *pelem, **aelem; |
685 | size_t round_size; | |
2a939a7e | 686 | struct link_map __attribute__ ((unused)) *l = NULL; |
39b3385d UD |
687 | /* Initialize to please the compiler. */ |
688 | const char *errstring = NULL; | |
0a54e401 | 689 | |
7ef90c15 UD |
690 | /* Fill in the information about the application's RPATH and the |
691 | directories addressed by the LD_LIBRARY_PATH environment variable. */ | |
0a54e401 | 692 | |
4317f9e1 | 693 | /* Get the capabilities. */ |
afdca0f2 | 694 | capstr = _dl_important_hwcaps (GLRO(dl_platform), GLRO(dl_platformlen), |
12264bd7 UD |
695 | &ncapstr, &max_capstrlen); |
696 | ||
697 | /* First set up the rest of the default search directory entries. */ | |
f55727ca | 698 | aelem = rtld_search_dirs.dirs = (struct r_search_path_elem **) |
4a6d1198 | 699 | malloc ((nsystem_dirs_len + 1) * sizeof (struct r_search_path_elem *)); |
f55727ca | 700 | if (rtld_search_dirs.dirs == NULL) |
39b3385d UD |
701 | { |
702 | errstring = N_("cannot create search path array"); | |
703 | signal_error: | |
154d10bd | 704 | _dl_signal_error (ENOMEM, NULL, NULL, errstring); |
39b3385d | 705 | } |
12264bd7 UD |
706 | |
707 | round_size = ((2 * sizeof (struct r_search_path_elem) - 1 | |
708 | + ncapstr * sizeof (enum r_dir_status)) | |
709 | / sizeof (struct r_search_path_elem)); | |
710 | ||
8a0b17e4 FW |
711 | rtld_search_dirs.dirs[0] = malloc (nsystem_dirs_len * round_size |
712 | * sizeof (*rtld_search_dirs.dirs[0])); | |
f55727ca | 713 | if (rtld_search_dirs.dirs[0] == NULL) |
39b3385d UD |
714 | { |
715 | errstring = N_("cannot create cache for search path"); | |
716 | goto signal_error; | |
717 | } | |
12264bd7 | 718 | |
f55727ca | 719 | rtld_search_dirs.malloced = 0; |
d6b5d570 | 720 | pelem = GL(dl_all_dirs) = rtld_search_dirs.dirs[0]; |
ab7eb292 UD |
721 | strp = system_dirs; |
722 | idx = 0; | |
723 | ||
724 | do | |
12264bd7 UD |
725 | { |
726 | size_t cnt; | |
727 | ||
728 | *aelem++ = pelem; | |
729 | ||
12264bd7 UD |
730 | pelem->what = "system search path"; |
731 | pelem->where = NULL; | |
732 | ||
ab7eb292 UD |
733 | pelem->dirname = strp; |
734 | pelem->dirnamelen = system_dirs_len[idx]; | |
735 | strp += system_dirs_len[idx] + 1; | |
12264bd7 | 736 | |
f55727ca UD |
737 | /* System paths must be absolute. */ |
738 | assert (pelem->dirname[0] == '/'); | |
739 | for (cnt = 0; cnt < ncapstr; ++cnt) | |
740 | pelem->status[cnt] = unknown; | |
ab7eb292 | 741 | |
4a6d1198 | 742 | pelem->next = (++idx == nsystem_dirs_len ? NULL : (pelem + round_size)); |
ab7eb292 UD |
743 | |
744 | pelem += round_size; | |
12264bd7 | 745 | } |
4a6d1198 | 746 | while (idx < nsystem_dirs_len); |
ab7eb292 UD |
747 | |
748 | max_dirnamelen = SYSTEM_DIRS_MAX_LEN; | |
12264bd7 | 749 | *aelem = NULL; |
4317f9e1 | 750 | |
b5567b2a | 751 | #ifdef SHARED |
81e0cb2d | 752 | /* This points to the map of the main object. */ |
c0f62c56 | 753 | l = GL(dl_ns)[LM_ID_BASE]._ns_loaded; |
011ce8ed UD |
754 | if (l != NULL) |
755 | { | |
011ce8ed | 756 | assert (l->l_type != lt_loaded); |
40a55d20 | 757 | |
fcf70d41 UD |
758 | if (l->l_info[DT_RUNPATH]) |
759 | { | |
760 | /* Allocate room for the search path and fill in information | |
761 | from RUNPATH. */ | |
f55727ca UD |
762 | decompose_rpath (&l->l_runpath_dirs, |
763 | (const void *) (D_PTR (l, l_info[DT_STRTAB]) | |
764 | + l->l_info[DT_RUNPATH]->d_un.d_val), | |
765 | l, "RUNPATH"); | |
273cdee8 AS |
766 | /* During rtld init the memory is allocated by the stub malloc, |
767 | prevent any attempt to free it by the normal malloc. */ | |
768 | l->l_runpath_dirs.malloced = 0; | |
fcf70d41 UD |
769 | |
770 | /* The RPATH is ignored. */ | |
f55727ca | 771 | l->l_rpath_dirs.dirs = (void *) -1; |
fcf70d41 | 772 | } |
011ce8ed | 773 | else |
fcf70d41 | 774 | { |
f55727ca | 775 | l->l_runpath_dirs.dirs = (void *) -1; |
fcf70d41 UD |
776 | |
777 | if (l->l_info[DT_RPATH]) | |
f55727ca UD |
778 | { |
779 | /* Allocate room for the search path and fill in information | |
780 | from RPATH. */ | |
781 | decompose_rpath (&l->l_rpath_dirs, | |
782 | (const void *) (D_PTR (l, l_info[DT_STRTAB]) | |
fcf70d41 UD |
783 | + l->l_info[DT_RPATH]->d_un.d_val), |
784 | l, "RPATH"); | |
273cdee8 AS |
785 | /* During rtld init the memory is allocated by the stub |
786 | malloc, prevent any attempt to free it by the normal | |
787 | malloc. */ | |
f55727ca UD |
788 | l->l_rpath_dirs.malloced = 0; |
789 | } | |
fcf70d41 | 790 | else |
f55727ca | 791 | l->l_rpath_dirs.dirs = (void *) -1; |
fcf70d41 | 792 | } |
0a54e401 | 793 | } |
b5567b2a | 794 | #endif /* SHARED */ |
7ef90c15 UD |
795 | |
796 | if (llp != NULL && *llp != '\0') | |
0a54e401 | 797 | { |
bb195224 | 798 | char *llp_tmp = strdupa (llp); |
011ce8ed | 799 | |
7ef90c15 UD |
800 | /* Decompose the LD_LIBRARY_PATH contents. First determine how many |
801 | elements it has. */ | |
3ff3dfa5 FW |
802 | size_t nllp = 1; |
803 | for (const char *cp = llp_tmp; *cp != '\0'; ++cp) | |
804 | if (*cp == ':' || *cp == ';') | |
805 | ++nllp; | |
7ef90c15 | 806 | |
f55727ca | 807 | env_path_list.dirs = (struct r_search_path_elem **) |
7ef90c15 | 808 | malloc ((nllp + 1) * sizeof (struct r_search_path_elem *)); |
f55727ca | 809 | if (env_path_list.dirs == NULL) |
39b3385d UD |
810 | { |
811 | errstring = N_("cannot create cache for search path"); | |
812 | goto signal_error; | |
813 | } | |
7ef90c15 | 814 | |
b0ed91ae | 815 | (void) fillin_rpath (llp_tmp, env_path_list.dirs, ":;", |
10e93d96 | 816 | "LD_LIBRARY_PATH", NULL, l); |
6a7c9bb4 | 817 | |
f55727ca | 818 | if (env_path_list.dirs[0] == NULL) |
6a7c9bb4 | 819 | { |
f55727ca UD |
820 | free (env_path_list.dirs); |
821 | env_path_list.dirs = (void *) -1; | |
6a7c9bb4 | 822 | } |
f55727ca UD |
823 | |
824 | env_path_list.malloced = 0; | |
81e0cb2d | 825 | } |
152e7964 | 826 | else |
f55727ca | 827 | env_path_list.dirs = (void *) -1; |
0a54e401 UD |
828 | } |
829 | ||
830 | ||
a6291c3d | 831 | static void |
a481b13c | 832 | __attribute__ ((noreturn, noinline)) |
126b06f9 | 833 | lose (int code, int fd, const char *name, char *realname, struct link_map *l, |
815e6fa3 | 834 | const char *msg, struct r_debug *r, Lmid_t nsid) |
126b06f9 | 835 | { |
fb0356b9 | 836 | /* The file might already be closed. */ |
a481b13c | 837 | if (fd != -1) |
329ea513 | 838 | (void) __close_nocancel (fd); |
610f9ab4 | 839 | if (l != NULL && l->l_origin != (char *) -1l) |
fde56e5c | 840 | free ((char *) l->l_origin); |
f0967738 | 841 | free (l); |
126b06f9 | 842 | free (realname); |
bca2218b UD |
843 | |
844 | if (r != NULL) | |
845 | { | |
846 | r->r_state = RT_CONSISTENT; | |
847 | _dl_debug_state (); | |
815e6fa3 | 848 | LIBC_PROBE (map_failed, 2, nsid, r); |
bca2218b UD |
849 | } |
850 | ||
154d10bd | 851 | _dl_signal_error (code, name, NULL, msg); |
126b06f9 UD |
852 | } |
853 | ||
854 | ||
ea03559a RM |
855 | /* Map in the shared object NAME, actually located in REALNAME, and already |
856 | opened on FD. */ | |
857 | ||
f787edde UD |
858 | #ifndef EXTERNAL_MAP_FROM_FD |
859 | static | |
860 | #endif | |
ea03559a | 861 | struct link_map * |
a1b85ae8 FW |
862 | _dl_map_object_from_fd (const char *name, const char *origname, int fd, |
863 | struct filebuf *fbp, char *realname, | |
864 | struct link_map *loader, int l_type, int mode, | |
865 | void **stack_endp, Lmid_t nsid) | |
ea03559a | 866 | { |
622586fb | 867 | struct link_map *l = NULL; |
266180eb RM |
868 | const ElfW(Ehdr) *header; |
869 | const ElfW(Phdr) *phdr; | |
870 | const ElfW(Phdr) *ph; | |
8193034b | 871 | size_t maplength; |
b122c703 | 872 | int type; |
39b3385d UD |
873 | /* Initialize to keep the compiler happy. */ |
874 | const char *errstring = NULL; | |
875 | int errval = 0; | |
29f97654 | 876 | struct r_debug *r = _dl_debug_initialize (0, nsid); |
9dcafc55 | 877 | bool make_consistent = false; |
61e0617a | 878 | |
2b26b084 FW |
879 | /* Get file information. To match the kernel behavior, do not fill |
880 | in this information for the executable in case of an explicit | |
881 | loader invocation. */ | |
c01ae97e | 882 | struct r_file_id id; |
2b26b084 | 883 | if (mode & __RTLD_OPENEXEC) |
39b3385d | 884 | { |
2b26b084 FW |
885 | assert (nsid == LM_ID_BASE); |
886 | memset (&id, 0, sizeof (id)); | |
39b3385d | 887 | } |
2b26b084 FW |
888 | else |
889 | { | |
890 | if (__glibc_unlikely (!_dl_get_file_id (fd, &id))) | |
891 | { | |
892 | errstring = N_("cannot stat shared object"); | |
893 | call_lose_errno: | |
894 | errval = errno; | |
895 | call_lose: | |
896 | lose (errval, fd, name, realname, l, errstring, | |
897 | make_consistent ? r : NULL, nsid); | |
898 | } | |
d66e34cd | 899 | |
2b26b084 FW |
900 | /* Look again to see if the real name matched another already loaded. */ |
901 | for (l = GL(dl_ns)[nsid]._ns_loaded; l != NULL; l = l->l_next) | |
902 | if (!l->l_removed && _dl_file_id_match_p (&l->l_file_id, &id)) | |
903 | { | |
904 | /* The object is already loaded. | |
905 | Just bump its reference count and return it. */ | |
906 | __close_nocancel (fd); | |
907 | ||
908 | /* If the name is not in the list of names for this object add | |
909 | it. */ | |
910 | free (realname); | |
911 | add_name_to_object (l, name); | |
912 | ||
913 | return l; | |
914 | } | |
915 | } | |
d66e34cd | 916 | |
c0f62c56 UD |
917 | #ifdef SHARED |
918 | /* When loading into a namespace other than the base one we must | |
919 | avoid loading ld.so since there can only be one copy. Ever. */ | |
1b26b855 | 920 | if (__glibc_unlikely (nsid != LM_ID_BASE) |
c01ae97e | 921 | && (_dl_file_id_match_p (&id, &GL(dl_rtld_map).l_file_id) |
c0f62c56 UD |
922 | || _dl_name_match_p (name, &GL(dl_rtld_map)))) |
923 | { | |
924 | /* This is indeed ld.so. Create a new link_map which refers to | |
925 | the real one for almost everything. */ | |
926 | l = _dl_new_object (realname, name, l_type, loader, mode, nsid); | |
927 | if (l == NULL) | |
928 | goto fail_new; | |
929 | ||
930 | /* Refer to the real descriptor. */ | |
931 | l->l_real = &GL(dl_rtld_map); | |
932 | ||
933 | /* No need to bump the refcount of the real object, ld.so will | |
934 | never be unloaded. */ | |
329ea513 | 935 | __close_nocancel (fd); |
c0f62c56 | 936 | |
f0967738 AK |
937 | /* Add the map for the mirrored object to the object list. */ |
938 | _dl_add_to_namespace_list (l, nsid); | |
939 | ||
c0f62c56 UD |
940 | return l; |
941 | } | |
942 | #endif | |
943 | ||
2f54c82d | 944 | if (mode & RTLD_NOLOAD) |
96961bf7 AS |
945 | { |
946 | /* We are not supposed to load the object unless it is already | |
947 | loaded. So return now. */ | |
4bff6e01 | 948 | free (realname); |
329ea513 | 949 | __close_nocancel (fd); |
96961bf7 AS |
950 | return NULL; |
951 | } | |
bf8b3e74 | 952 | |
8193034b | 953 | /* Print debugging message. */ |
a1ffb40e | 954 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES)) |
c0f62c56 | 955 | _dl_debug_printf ("file=%s [%lu]; generating link map\n", name, nsid); |
8193034b | 956 | |
a35e137a UD |
957 | /* This is the ELF header. We read it in `open_verify'. */ |
958 | header = (void *) fbp->buf; | |
d66e34cd | 959 | |
9dcafc55 UD |
960 | /* Signal that we are going to add new objects. */ |
961 | if (r->r_state == RT_CONSISTENT) | |
962 | { | |
963 | #ifdef SHARED | |
964 | /* Auditing checkpoint: we are going to add new objects. */ | |
2ca285b0 | 965 | if ((mode & __RTLD_AUDIT) == 0 |
1b26b855 | 966 | && __glibc_unlikely (GLRO(dl_naudit) > 0)) |
9dcafc55 UD |
967 | { |
968 | struct link_map *head = GL(dl_ns)[nsid]._ns_loaded; | |
969 | /* Do not call the functions for any auditing object. */ | |
970 | if (head->l_auditing == 0) | |
971 | { | |
972 | struct audit_ifaces *afct = GLRO(dl_audit); | |
973 | for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) | |
974 | { | |
975 | if (afct->activity != NULL) | |
976 | afct->activity (&head->l_audit[cnt].cookie, LA_ACT_ADD); | |
977 | ||
978 | afct = afct->next; | |
979 | } | |
980 | } | |
981 | } | |
982 | #endif | |
983 | ||
984 | /* Notify the debugger we have added some objects. We need to | |
985 | call _dl_debug_initialize in a static program in case dynamic | |
986 | linking has not been used before. */ | |
987 | r->r_state = RT_ADD; | |
988 | _dl_debug_state (); | |
815e6fa3 | 989 | LIBC_PROBE (map_start, 2, nsid, r); |
9dcafc55 UD |
990 | make_consistent = true; |
991 | } | |
992 | else | |
993 | assert (r->r_state == RT_ADD); | |
994 | ||
ba79d61b | 995 | /* Enter the new object in the list of loaded objects. */ |
c0f62c56 | 996 | l = _dl_new_object (realname, name, l_type, loader, mode, nsid); |
a1ffb40e | 997 | if (__glibc_unlikely (l == NULL)) |
39b3385d | 998 | { |
7cb92a99 | 999 | #ifdef SHARED |
c0f62c56 | 1000 | fail_new: |
7cb92a99 | 1001 | #endif |
39b3385d UD |
1002 | errstring = N_("cannot create shared object descriptor"); |
1003 | goto call_lose_errno; | |
1004 | } | |
ba79d61b | 1005 | |
b122c703 | 1006 | /* Extract the remaining details we need from the ELF header |
32c85e43 | 1007 | and then read in the program header table. */ |
b122c703 RM |
1008 | l->l_entry = header->e_entry; |
1009 | type = header->e_type; | |
1010 | l->l_phnum = header->e_phnum; | |
32c85e43 UD |
1011 | |
1012 | maplength = header->e_phnum * sizeof (ElfW(Phdr)); | |
6dd67bd5 | 1013 | if (header->e_phoff + maplength <= (size_t) fbp->len) |
a35e137a | 1014 | phdr = (void *) (fbp->buf + header->e_phoff); |
32c85e43 UD |
1015 | else |
1016 | { | |
1017 | phdr = alloca (maplength); | |
95c10569 LP |
1018 | if ((size_t) __pread64_nocancel (fd, (void *) phdr, maplength, |
1019 | header->e_phoff) != maplength) | |
39b3385d UD |
1020 | { |
1021 | errstring = N_("cannot read file data"); | |
1022 | goto call_lose_errno; | |
1023 | } | |
32c85e43 | 1024 | } |
879bf2e6 | 1025 | |
30950a5f RA |
1026 | /* On most platforms presume that PT_GNU_STACK is absent and the stack is |
1027 | * executable. Other platforms default to a nonexecutable stack and don't | |
1028 | * need PT_GNU_STACK to do so. */ | |
1029 | uint_fast16_t stack_flags = DEFAULT_STACK_PERMS; | |
ecdeaac0 | 1030 | |
b122c703 RM |
1031 | { |
1032 | /* Scan the program header table, collecting its load commands. */ | |
fcccd512 | 1033 | struct loadcmd loadcmds[l->l_phnum]; |
b122c703 | 1034 | size_t nloadcmds = 0; |
6fffb9a2 | 1035 | bool has_holes = false; |
d66e34cd | 1036 | |
126b06f9 | 1037 | /* The struct is initialized to zero so this is not necessary: |
d66e34cd | 1038 | l->l_ld = 0; |
b122c703 | 1039 | l->l_phdr = 0; |
126b06f9 | 1040 | l->l_addr = 0; */ |
d66e34cd RM |
1041 | for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph) |
1042 | switch (ph->p_type) | |
1043 | { | |
1044 | /* These entries tell us where to find things once the file's | |
1045 | segments are mapped in. We record the addresses it says | |
1046 | verbatim, and later correct for the run-time load address. */ | |
1047 | case PT_DYNAMIC: | |
592d5c75 L |
1048 | if (ph->p_filesz) |
1049 | { | |
1050 | /* Debuginfo only files from "objcopy --only-keep-debug" | |
1051 | contain a PT_DYNAMIC segment with p_filesz == 0. Skip | |
1052 | such a segment to avoid a crash later. */ | |
1053 | l->l_ld = (void *) ph->p_vaddr; | |
1054 | l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn)); | |
1055 | } | |
d66e34cd | 1056 | break; |
39b3385d | 1057 | |
d66e34cd RM |
1058 | case PT_PHDR: |
1059 | l->l_phdr = (void *) ph->p_vaddr; | |
1060 | break; | |
1061 | ||
1062 | case PT_LOAD: | |
b122c703 RM |
1063 | /* A load command tells us to map in part of the file. |
1064 | We record the load commands and process them all later. */ | |
1b26b855 | 1065 | if (__glibc_unlikely ((ph->p_align & (GLRO(dl_pagesize) - 1)) != 0)) |
39b3385d UD |
1066 | { |
1067 | errstring = N_("ELF load command alignment not page-aligned"); | |
1068 | goto call_lose; | |
1069 | } | |
1b26b855 PP |
1070 | if (__glibc_unlikely (((ph->p_vaddr - ph->p_offset) |
1071 | & (ph->p_align - 1)) != 0)) | |
39b3385d UD |
1072 | { |
1073 | errstring | |
1074 | = N_("ELF load command address/offset not properly aligned"); | |
1075 | goto call_lose; | |
1076 | } | |
1077 | ||
fcccd512 | 1078 | struct loadcmd *c = &loadcmds[nloadcmds++]; |
60084e34 CD |
1079 | c->mapstart = ALIGN_DOWN (ph->p_vaddr, GLRO(dl_pagesize)); |
1080 | c->mapend = ALIGN_UP (ph->p_vaddr + ph->p_filesz, GLRO(dl_pagesize)); | |
cc12f2a4 UD |
1081 | c->dataend = ph->p_vaddr + ph->p_filesz; |
1082 | c->allocend = ph->p_vaddr + ph->p_memsz; | |
60084e34 | 1083 | c->mapoff = ALIGN_DOWN (ph->p_offset, GLRO(dl_pagesize)); |
cc12f2a4 | 1084 | |
6fffb9a2 UD |
1085 | /* Determine whether there is a gap between the last segment |
1086 | and this one. */ | |
1087 | if (nloadcmds > 1 && c[-1].mapend != c->mapstart) | |
1088 | has_holes = true; | |
1089 | ||
cc12f2a4 | 1090 | /* Optimize a common case. */ |
94a758fe | 1091 | #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7 |
cc12f2a4 UD |
1092 | c->prot = (PF_TO_PROT |
1093 | >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf; | |
94a758fe | 1094 | #else |
cc12f2a4 UD |
1095 | c->prot = 0; |
1096 | if (ph->p_flags & PF_R) | |
1097 | c->prot |= PROT_READ; | |
1098 | if (ph->p_flags & PF_W) | |
1099 | c->prot |= PROT_WRITE; | |
1100 | if (ph->p_flags & PF_X) | |
1101 | c->prot |= PROT_EXEC; | |
94a758fe | 1102 | #endif |
55c91021 | 1103 | break; |
96f208a4 | 1104 | |
96f208a4 | 1105 | case PT_TLS: |
2d148689 RM |
1106 | if (ph->p_memsz == 0) |
1107 | /* Nothing to do for an empty segment. */ | |
1108 | break; | |
1109 | ||
216455bc RM |
1110 | l->l_tls_blocksize = ph->p_memsz; |
1111 | l->l_tls_align = ph->p_align; | |
99fe3b0e UD |
1112 | if (ph->p_align == 0) |
1113 | l->l_tls_firstbyte_offset = 0; | |
1114 | else | |
1115 | l->l_tls_firstbyte_offset = ph->p_vaddr & (ph->p_align - 1); | |
216455bc RM |
1116 | l->l_tls_initimage_size = ph->p_filesz; |
1117 | /* Since we don't know the load address yet only store the | |
1118 | offset. We will adjust it later. */ | |
1119 | l->l_tls_initimage = (void *) ph->p_vaddr; | |
1120 | ||
77523d5e FW |
1121 | /* l->l_tls_modid is assigned below, once there is no |
1122 | possibility for failure. */ | |
2d148689 | 1123 | |
77523d5e FW |
1124 | if (l->l_type != lt_library |
1125 | && GL(dl_tls_dtv_slotinfo_list) == NULL) | |
1126 | { | |
11bf311e | 1127 | #ifdef SHARED |
77523d5e FW |
1128 | /* We are loading the executable itself when the dynamic |
1129 | linker was executed directly. The setup will happen | |
1130 | later. */ | |
1131 | assert (l->l_prev == NULL || (mode & __RTLD_AUDIT) != 0); | |
7a5e3d9d | 1132 | #else |
77523d5e | 1133 | assert (false && "TLS not initialized in static application"); |
11bf311e | 1134 | #endif |
77523d5e | 1135 | } |
807bce82 | 1136 | break; |
ecdeaac0 RM |
1137 | |
1138 | case PT_GNU_STACK: | |
1139 | stack_flags = ph->p_flags; | |
1140 | break; | |
ed20b3d9 UD |
1141 | |
1142 | case PT_GNU_RELRO: | |
1143 | l->l_relro_addr = ph->p_vaddr; | |
1144 | l->l_relro_size = ph->p_memsz; | |
1145 | break; | |
f753fa7d L |
1146 | |
1147 | case PT_NOTE: | |
1148 | if (_dl_process_pt_note (l, ph, fd, fbp)) | |
1149 | { | |
1150 | errstring = N_("cannot process note segment"); | |
1151 | goto call_lose; | |
1152 | } | |
1153 | break; | |
b122c703 | 1154 | } |
d66e34cd | 1155 | |
a1ffb40e | 1156 | if (__glibc_unlikely (nloadcmds == 0)) |
d8a5edc2 RM |
1157 | { |
1158 | /* This only happens for a bogus object that will be caught with | |
1159 | another error below. But we don't want to go through the | |
1160 | calculations below using NLOADCMDS - 1. */ | |
1161 | errstring = N_("object file has no loadable segments"); | |
1162 | goto call_lose; | |
1163 | } | |
1164 | ||
2c75b545 FW |
1165 | /* dlopen of an executable is not valid because it is not possible |
1166 | to perform proper relocations, handle static TLS, or run the | |
1167 | ELF constructors. For PIE, the check needs the dynamic | |
1168 | section, so there is another check below. */ | |
fcccd512 RM |
1169 | if (__glibc_unlikely (type != ET_DYN) |
1170 | && __glibc_unlikely ((mode & __RTLD_OPENEXEC) == 0)) | |
efec5079 | 1171 | { |
fcccd512 RM |
1172 | /* This object is loaded at a fixed address. This must never |
1173 | happen for objects loaded with dlopen. */ | |
efec5079 UD |
1174 | errstring = N_("cannot dynamically load executable"); |
1175 | goto call_lose; | |
4cca6b86 | 1176 | } |
b122c703 | 1177 | |
fcccd512 RM |
1178 | /* Length of the sections to be loaded. */ |
1179 | maplength = loadcmds[nloadcmds - 1].allocend - loadcmds[0].mapstart; | |
1180 | ||
1181 | /* Now process the load commands and map segments into memory. | |
1182 | This is responsible for filling in: | |
1183 | l_map_start, l_map_end, l_addr, l_contiguous, l_text_end, l_phdr | |
1184 | */ | |
1185 | errstring = _dl_map_segments (l, fd, header, type, loadcmds, nloadcmds, | |
c01ae97e | 1186 | maplength, has_holes, loader); |
fcccd512 RM |
1187 | if (__glibc_unlikely (errstring != NULL)) |
1188 | goto call_lose; | |
b122c703 | 1189 | } |
d66e34cd | 1190 | |
b122c703 RM |
1191 | if (l->l_ld == 0) |
1192 | { | |
a1ffb40e | 1193 | if (__glibc_unlikely (type == ET_DYN)) |
39b3385d UD |
1194 | { |
1195 | errstring = N_("object file has no dynamic section"); | |
1196 | goto call_lose; | |
1197 | } | |
b122c703 RM |
1198 | } |
1199 | else | |
14755b91 | 1200 | l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr); |
879bf2e6 | 1201 | |
479aa8ec | 1202 | elf_get_dynamic_info (l, NULL); |
2f54c82d | 1203 | |
efec5079 | 1204 | /* Make sure we are not dlopen'ing an object that has the |
2c75b545 FW |
1205 | DF_1_NOOPEN flag set, or a PIE object. */ |
1206 | if ((__glibc_unlikely (l->l_flags_1 & DF_1_NOOPEN) | |
1207 | && (mode & __RTLD_DLOPEN)) | |
1208 | || (__glibc_unlikely (l->l_flags_1 & DF_1_PIE) | |
1209 | && __glibc_unlikely ((mode & __RTLD_OPENEXEC) == 0))) | |
2f54c82d | 1210 | { |
2f54c82d | 1211 | /* We are not supposed to load this object. Free all resources. */ |
fcccd512 | 1212 | _dl_unmap_segments (l); |
2f54c82d | 1213 | |
11810621 UD |
1214 | if (!l->l_libname->dont_free) |
1215 | free (l->l_libname); | |
2f54c82d UD |
1216 | |
1217 | if (l->l_phdr_allocated) | |
1218 | free ((void *) l->l_phdr); | |
1219 | ||
2c75b545 FW |
1220 | if (l->l_flags_1 & DF_1_PIE) |
1221 | errstring | |
1222 | = N_("cannot dynamically load position-independent executable"); | |
1223 | else | |
1224 | errstring = N_("shared object cannot be dlopen()ed"); | |
fb0356b9 | 1225 | goto call_lose; |
2f54c82d UD |
1226 | } |
1227 | ||
efec5079 UD |
1228 | if (l->l_phdr == NULL) |
1229 | { | |
1230 | /* The program header is not contained in any of the segments. | |
1231 | We have to allocate memory ourself and copy it over from out | |
1232 | temporary place. */ | |
1233 | ElfW(Phdr) *newp = (ElfW(Phdr) *) malloc (header->e_phnum | |
1234 | * sizeof (ElfW(Phdr))); | |
1235 | if (newp == NULL) | |
1236 | { | |
1237 | errstring = N_("cannot allocate memory for program header"); | |
1238 | goto call_lose_errno; | |
1239 | } | |
1240 | ||
1241 | l->l_phdr = memcpy (newp, phdr, | |
1242 | (header->e_phnum * sizeof (ElfW(Phdr)))); | |
1243 | l->l_phdr_allocated = 1; | |
1244 | } | |
1245 | else | |
1246 | /* Adjust the PT_PHDR value by the runtime load address. */ | |
1247 | l->l_phdr = (ElfW(Phdr) *) ((ElfW(Addr)) l->l_phdr + l->l_addr); | |
1248 | ||
a1ffb40e | 1249 | if (__glibc_unlikely ((stack_flags &~ GL(dl_stack_flags)) & PF_X)) |
dcca3fe2 UD |
1250 | { |
1251 | /* The stack is presently not executable, but this module | |
1252 | requires that it be executable. We must change the | |
1253 | protection of the variable which contains the flags used in | |
1254 | the mprotect calls. */ | |
11bf311e | 1255 | #ifdef SHARED |
3e539cb4 | 1256 | if ((mode & (__RTLD_DLOPEN | __RTLD_AUDIT)) == __RTLD_DLOPEN) |
dcca3fe2 | 1257 | { |
e751d282 RM |
1258 | const uintptr_t p = (uintptr_t) &__stack_prot & -GLRO(dl_pagesize); |
1259 | const size_t s = (uintptr_t) (&__stack_prot + 1) - p; | |
1260 | ||
1261 | struct link_map *const m = &GL(dl_rtld_map); | |
1262 | const uintptr_t relro_end = ((m->l_addr + m->l_relro_addr | |
1263 | + m->l_relro_size) | |
1264 | & -GLRO(dl_pagesize)); | |
a1ffb40e | 1265 | if (__glibc_likely (p + s <= relro_end)) |
e751d282 RM |
1266 | { |
1267 | /* The variable lies in the region protected by RELRO. */ | |
0432680e PY |
1268 | if (__mprotect ((void *) p, s, PROT_READ|PROT_WRITE) < 0) |
1269 | { | |
1270 | errstring = N_("cannot change memory protections"); | |
1271 | goto call_lose_errno; | |
1272 | } | |
e751d282 RM |
1273 | __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC; |
1274 | __mprotect ((void *) p, s, PROT_READ); | |
1275 | } | |
1276 | else | |
ecc1d0c3 | 1277 | __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC; |
dcca3fe2 UD |
1278 | } |
1279 | else | |
1280 | #endif | |
ecc1d0c3 | 1281 | __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC; |
dcca3fe2 | 1282 | |
606832e6 UD |
1283 | #ifdef check_consistency |
1284 | check_consistency (); | |
1285 | #endif | |
1286 | ||
dcca3fe2 UD |
1287 | errval = (*GL(dl_make_stack_executable_hook)) (stack_endp); |
1288 | if (errval) | |
1289 | { | |
1290 | errstring = N_("\ | |
1291 | cannot enable executable stack as shared object requires"); | |
1292 | goto call_lose; | |
1293 | } | |
1294 | } | |
1295 | ||
efec5079 UD |
1296 | /* Adjust the address of the TLS initialization image. */ |
1297 | if (l->l_tls_initimage != NULL) | |
1298 | l->l_tls_initimage = (char *) l->l_tls_initimage + l->l_addr; | |
efec5079 UD |
1299 | |
1300 | /* We are done mapping in the file. We no longer need the descriptor. */ | |
329ea513 | 1301 | if (__glibc_unlikely (__close_nocancel (fd) != 0)) |
efec5079 UD |
1302 | { |
1303 | errstring = N_("cannot close file descriptor"); | |
1304 | goto call_lose_errno; | |
1305 | } | |
1306 | /* Signal that we closed the file. */ | |
1307 | fd = -1; | |
1308 | ||
798212a0 PP |
1309 | /* If this is ET_EXEC, we should have loaded it as lt_executable. */ |
1310 | assert (type != ET_EXEC || l->l_type == lt_executable); | |
efec5079 UD |
1311 | |
1312 | l->l_entry += l->l_addr; | |
1313 | ||
a1ffb40e | 1314 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES)) |
efec5079 UD |
1315 | _dl_debug_printf ("\ |
1316 | dynamic: 0x%0*lx base: 0x%0*lx size: 0x%0*Zx\n\ | |
1317 | entry: 0x%0*lx phdr: 0x%0*lx phnum: %*u\n\n", | |
1318 | (int) sizeof (void *) * 2, | |
1319 | (unsigned long int) l->l_ld, | |
1320 | (int) sizeof (void *) * 2, | |
1321 | (unsigned long int) l->l_addr, | |
1322 | (int) sizeof (void *) * 2, maplength, | |
1323 | (int) sizeof (void *) * 2, | |
1324 | (unsigned long int) l->l_entry, | |
1325 | (int) sizeof (void *) * 2, | |
1326 | (unsigned long int) l->l_phdr, | |
1327 | (int) sizeof (void *) * 2, l->l_phnum); | |
1328 | ||
1329 | /* Set up the symbol hash table. */ | |
1330 | _dl_setup_hash (l); | |
d66e34cd | 1331 | |
be935610 UD |
1332 | /* If this object has DT_SYMBOLIC set modify now its scope. We don't |
1333 | have to do this for the main map. */ | |
1fc07491 | 1334 | if ((mode & RTLD_DEEPBIND) == 0 |
1b26b855 | 1335 | && __glibc_unlikely (l->l_info[DT_SYMBOLIC] != NULL) |
c0a777e8 | 1336 | && &l->l_searchlist != l->l_scope[0]) |
be935610 UD |
1337 | { |
1338 | /* Create an appropriate searchlist. It contains only this map. | |
1fc07491 | 1339 | This is the definition of DT_SYMBOLIC in SysVr4. */ |
be935610 UD |
1340 | l->l_symbolic_searchlist.r_list[0] = l; |
1341 | l->l_symbolic_searchlist.r_nlist = 1; | |
be935610 UD |
1342 | |
1343 | /* Now move the existing entries one back. */ | |
c0a777e8 UD |
1344 | memmove (&l->l_scope[1], &l->l_scope[0], |
1345 | (l->l_scope_max - 1) * sizeof (l->l_scope[0])); | |
be935610 UD |
1346 | |
1347 | /* Now add the new entry. */ | |
c0a777e8 | 1348 | l->l_scope[0] = &l->l_symbolic_searchlist; |
be935610 UD |
1349 | } |
1350 | ||
5d916713 | 1351 | /* Remember whether this object must be initialized first. */ |
39b3385d | 1352 | if (l->l_flags_1 & DF_1_INITFIRST) |
d6b5d570 | 1353 | GL(dl_initfirst) = l; |
5d916713 | 1354 | |
61e0617a | 1355 | /* Finally the file information. */ |
c01ae97e | 1356 | l->l_file_id = id; |
61e0617a | 1357 | |
a1b85ae8 FW |
1358 | #ifdef SHARED |
1359 | /* When auditing is used the recorded names might not include the | |
1360 | name by which the DSO is actually known. Add that as well. */ | |
1361 | if (__glibc_unlikely (origname != NULL)) | |
1362 | add_name_to_object (l, origname); | |
1363 | #else | |
1364 | /* Audit modules only exist when linking is dynamic so ORIGNAME | |
1365 | cannot be non-NULL. */ | |
1366 | assert (origname == NULL); | |
1367 | #endif | |
1368 | ||
51f38e87 UD |
1369 | /* When we profile the SONAME might be needed for something else but |
1370 | loading. Add it right away. */ | |
1b26b855 | 1371 | if (__glibc_unlikely (GLRO(dl_profile) != NULL) |
51f38e87 UD |
1372 | && l->l_info[DT_SONAME] != NULL) |
1373 | add_name_to_object (l, ((const char *) D_PTR (l, l_info[DT_STRTAB]) | |
1374 | + l->l_info[DT_SONAME]->d_un.d_val)); | |
1375 | ||
77523d5e FW |
1376 | /* _dl_close can only eventually undo the module ID assignment (via |
1377 | remove_slotinfo) if this function returns a pointer to a link | |
1378 | map. Therefore, delay this step until all possibilities for | |
1379 | failure have been excluded. */ | |
1380 | if (l->l_tls_blocksize > 0 | |
1381 | && (__glibc_likely (l->l_type == lt_library) | |
1382 | /* If GL(dl_tls_dtv_slotinfo_list) == NULL, then rtld.c did | |
1383 | not set up TLS data structures, so don't use them now. */ | |
1384 | || __glibc_likely (GL(dl_tls_dtv_slotinfo_list) != NULL))) | |
1385 | /* Assign the next available module ID. */ | |
1386 | l->l_tls_modid = _dl_next_tls_modid (); | |
1387 | ||
47cc1490 CM |
1388 | #ifdef DL_AFTER_LOAD |
1389 | DL_AFTER_LOAD (l); | |
1390 | #endif | |
1391 | ||
f0967738 AK |
1392 | /* Now that the object is fully initialized add it to the object list. */ |
1393 | _dl_add_to_namespace_list (l, nsid); | |
1394 | ||
9dcafc55 UD |
1395 | #ifdef SHARED |
1396 | /* Auditing checkpoint: we have a new object. */ | |
1b26b855 | 1397 | if (__glibc_unlikely (GLRO(dl_naudit) > 0) |
29f97654 | 1398 | && !GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing) |
9dcafc55 UD |
1399 | { |
1400 | struct audit_ifaces *afct = GLRO(dl_audit); | |
1401 | for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) | |
1402 | { | |
1403 | if (afct->objopen != NULL) | |
1404 | { | |
1405 | l->l_audit[cnt].bindflags | |
1406 | = afct->objopen (l, nsid, &l->l_audit[cnt].cookie); | |
1407 | ||
1408 | l->l_audit_any_plt |= l->l_audit[cnt].bindflags != 0; | |
1409 | } | |
1410 | ||
1411 | afct = afct->next; | |
1412 | } | |
1413 | } | |
1414 | #endif | |
1415 | ||
d66e34cd RM |
1416 | return l; |
1417 | } | |
ba79d61b | 1418 | \f |
b5efde2f UD |
1419 | /* Print search path. */ |
1420 | static void | |
1421 | print_search_path (struct r_search_path_elem **list, | |
844c394a | 1422 | const char *what, const char *name) |
b5efde2f | 1423 | { |
12264bd7 | 1424 | char buf[max_dirnamelen + max_capstrlen]; |
b5efde2f UD |
1425 | int first = 1; |
1426 | ||
154d10bd | 1427 | _dl_debug_printf (" search path="); |
b5efde2f UD |
1428 | |
1429 | while (*list != NULL && (*list)->what == what) /* Yes, ==. */ | |
1430 | { | |
12264bd7 UD |
1431 | char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen); |
1432 | size_t cnt; | |
1433 | ||
1434 | for (cnt = 0; cnt < ncapstr; ++cnt) | |
1435 | if ((*list)->status[cnt] != nonexisting) | |
1436 | { | |
1437 | char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len); | |
143e2b96 UD |
1438 | if (cp == buf || (cp == buf + 1 && buf[0] == '/')) |
1439 | cp[0] = '\0'; | |
1440 | else | |
1441 | cp[-1] = '\0'; | |
fb0356b9 UD |
1442 | |
1443 | _dl_debug_printf_c (first ? "%s" : ":%s", buf); | |
1444 | first = 0; | |
12264bd7 | 1445 | } |
b5efde2f | 1446 | |
b5efde2f UD |
1447 | ++list; |
1448 | } | |
1449 | ||
1450 | if (name != NULL) | |
35fc382a | 1451 | _dl_debug_printf_c ("\t\t(%s from file %s)\n", what, |
b9375348 | 1452 | DSO_FILENAME (name)); |
b5efde2f | 1453 | else |
35fc382a | 1454 | _dl_debug_printf_c ("\t\t(%s)\n", what); |
b5efde2f UD |
1455 | } |
1456 | \f | |
a35e137a UD |
1457 | /* Open a file and verify it is an ELF file for this architecture. We |
1458 | ignore only ELF files for other architectures. Non-ELF files and | |
1459 | ELF files with different header information cause fatal errors since | |
1460 | this could mean there is something wrong in the installation and the | |
c0d6f2a3 RM |
1461 | user might want to know about this. |
1462 | ||
1463 | If FD is not -1, then the file is already open and FD refers to it. | |
1464 | In that case, FD is consumed for both successful and error returns. */ | |
a35e137a | 1465 | static int |
c0d6f2a3 RM |
1466 | open_verify (const char *name, int fd, |
1467 | struct filebuf *fbp, struct link_map *loader, | |
a42faf59 | 1468 | int whatcode, int mode, bool *found_other_class, bool free_name) |
a35e137a UD |
1469 | { |
1470 | /* This is the expected ELF header. */ | |
1471 | #define ELF32_CLASS ELFCLASS32 | |
1472 | #define ELF64_CLASS ELFCLASS64 | |
1473 | #ifndef VALID_ELF_HEADER | |
1474 | # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0) | |
1475 | # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV) | |
d8c47894 | 1476 | # define VALID_ELF_ABIVERSION(osabi,ver) (ver == 0) |
40e2fc8b UD |
1477 | #elif defined MORE_ELF_HEADER_DATA |
1478 | MORE_ELF_HEADER_DATA; | |
a35e137a | 1479 | #endif |
04f2902d | 1480 | static const unsigned char expected[EI_NIDENT] = |
a35e137a UD |
1481 | { |
1482 | [EI_MAG0] = ELFMAG0, | |
1483 | [EI_MAG1] = ELFMAG1, | |
1484 | [EI_MAG2] = ELFMAG2, | |
1485 | [EI_MAG3] = ELFMAG3, | |
1486 | [EI_CLASS] = ELFW(CLASS), | |
1487 | [EI_DATA] = byteorder, | |
1488 | [EI_VERSION] = EV_CURRENT, | |
1489 | [EI_OSABI] = ELFOSABI_SYSV, | |
1490 | [EI_ABIVERSION] = 0 | |
1491 | }; | |
39b3385d UD |
1492 | static const struct |
1493 | { | |
1494 | ElfW(Word) vendorlen; | |
1495 | ElfW(Word) datalen; | |
1496 | ElfW(Word) type; | |
55c91021 | 1497 | char vendor[4]; |
a986484f | 1498 | } expected_note = { 4, 16, 1, "GNU" }; |
39b3385d UD |
1499 | /* Initialize it to make the compiler happy. */ |
1500 | const char *errstring = NULL; | |
1501 | int errval = 0; | |
a35e137a | 1502 | |
9dcafc55 UD |
1503 | #ifdef SHARED |
1504 | /* Give the auditing libraries a chance. */ | |
1b26b855 | 1505 | if (__glibc_unlikely (GLRO(dl_naudit) > 0) && whatcode != 0 |
9dcafc55 UD |
1506 | && loader->l_auditing == 0) |
1507 | { | |
c0d6f2a3 | 1508 | const char *original_name = name; |
9dcafc55 UD |
1509 | struct audit_ifaces *afct = GLRO(dl_audit); |
1510 | for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) | |
1511 | { | |
1512 | if (afct->objsearch != NULL) | |
1513 | { | |
1514 | name = afct->objsearch (name, &loader->l_audit[cnt].cookie, | |
1515 | whatcode); | |
1516 | if (name == NULL) | |
1517 | /* Ignore the path. */ | |
1518 | return -1; | |
1519 | } | |
1520 | ||
1521 | afct = afct->next; | |
1522 | } | |
c0d6f2a3 RM |
1523 | |
1524 | if (fd != -1 && name != original_name && strcmp (name, original_name)) | |
1525 | { | |
1526 | /* An audit library changed what we're supposed to open, | |
1527 | so FD no longer matches it. */ | |
329ea513 | 1528 | __close_nocancel (fd); |
c0d6f2a3 RM |
1529 | fd = -1; |
1530 | } | |
9dcafc55 UD |
1531 | } |
1532 | #endif | |
1533 | ||
c0d6f2a3 RM |
1534 | if (fd == -1) |
1535 | /* Open the file. We always open files read-only. */ | |
329ea513 | 1536 | fd = __open64_nocancel (name, O_RDONLY | O_CLOEXEC); |
c0d6f2a3 | 1537 | |
a35e137a UD |
1538 | if (fd != -1) |
1539 | { | |
1540 | ElfW(Ehdr) *ehdr; | |
a986484f | 1541 | ElfW(Phdr) *phdr, *ph; |
2f6773b5 | 1542 | ElfW(Word) *abi_note; |
0065aaaa | 1543 | ElfW(Word) *abi_note_malloced = NULL; |
a986484f UD |
1544 | unsigned int osversion; |
1545 | size_t maplength; | |
a35e137a | 1546 | |
9f236c49 | 1547 | /* We successfully opened the file. Now verify it is a file |
a35e137a UD |
1548 | we can use. */ |
1549 | __set_errno (0); | |
88481c16 SP |
1550 | fbp->len = 0; |
1551 | assert (sizeof (fbp->buf) > sizeof (ElfW(Ehdr))); | |
1552 | /* Read in the header. */ | |
1553 | do | |
fcccd512 | 1554 | { |
329ea513 ZW |
1555 | ssize_t retlen = __read_nocancel (fd, fbp->buf + fbp->len, |
1556 | sizeof (fbp->buf) - fbp->len); | |
88481c16 SP |
1557 | if (retlen <= 0) |
1558 | break; | |
1559 | fbp->len += retlen; | |
1560 | } | |
1b26b855 | 1561 | while (__glibc_unlikely (fbp->len < sizeof (ElfW(Ehdr)))); |
a35e137a UD |
1562 | |
1563 | /* This is where the ELF header is loaded. */ | |
a35e137a UD |
1564 | ehdr = (ElfW(Ehdr) *) fbp->buf; |
1565 | ||
1566 | /* Now run the tests. */ | |
a1ffb40e | 1567 | if (__glibc_unlikely (fbp->len < (ssize_t) sizeof (ElfW(Ehdr)))) |
39b3385d UD |
1568 | { |
1569 | errval = errno; | |
1570 | errstring = (errval == 0 | |
1571 | ? N_("file too short") : N_("cannot read file data")); | |
1572 | call_lose: | |
2e0fc40c UD |
1573 | if (free_name) |
1574 | { | |
1575 | char *realname = (char *) name; | |
1576 | name = strdupa (realname); | |
1577 | free (realname); | |
1578 | } | |
815e6fa3 | 1579 | lose (errval, fd, name, NULL, NULL, errstring, NULL, 0); |
39b3385d | 1580 | } |
a35e137a UD |
1581 | |
1582 | /* See whether the ELF header is what we expect. */ | |
277ae3f1 | 1583 | if (__glibc_unlikely (! VALID_ELF_HEADER (ehdr->e_ident, expected, |
92ad15a8 | 1584 | EI_ABIVERSION) |
d8c47894 | 1585 | || !VALID_ELF_ABIVERSION (ehdr->e_ident[EI_OSABI], |
04f2902d UD |
1586 | ehdr->e_ident[EI_ABIVERSION]) |
1587 | || memcmp (&ehdr->e_ident[EI_PAD], | |
1588 | &expected[EI_PAD], | |
277ae3f1 | 1589 | EI_NIDENT - EI_PAD) != 0)) |
a35e137a UD |
1590 | { |
1591 | /* Something is wrong. */ | |
6cc8844f UD |
1592 | const Elf32_Word *magp = (const void *) ehdr->e_ident; |
1593 | if (*magp != | |
a35e137a | 1594 | #if BYTE_ORDER == LITTLE_ENDIAN |
34a5a146 JM |
1595 | ((ELFMAG0 << (EI_MAG0 * 8)) |
1596 | | (ELFMAG1 << (EI_MAG1 * 8)) | |
1597 | | (ELFMAG2 << (EI_MAG2 * 8)) | |
1598 | | (ELFMAG3 << (EI_MAG3 * 8))) | |
a35e137a | 1599 | #else |
34a5a146 JM |
1600 | ((ELFMAG0 << (EI_MAG3 * 8)) |
1601 | | (ELFMAG1 << (EI_MAG2 * 8)) | |
1602 | | (ELFMAG2 << (EI_MAG1 * 8)) | |
1603 | | (ELFMAG3 << (EI_MAG0 * 8))) | |
a35e137a UD |
1604 | #endif |
1605 | ) | |
39b3385d UD |
1606 | errstring = N_("invalid ELF header"); |
1607 | else if (ehdr->e_ident[EI_CLASS] != ELFW(CLASS)) | |
7f71c55d UD |
1608 | { |
1609 | /* This is not a fatal error. On architectures where | |
1610 | 32-bit and 64-bit binaries can be run this might | |
1611 | happen. */ | |
1612 | *found_other_class = true; | |
1613 | goto close_and_out; | |
1614 | } | |
39b3385d | 1615 | else if (ehdr->e_ident[EI_DATA] != byteorder) |
a35e137a UD |
1616 | { |
1617 | if (BYTE_ORDER == BIG_ENDIAN) | |
39b3385d | 1618 | errstring = N_("ELF file data encoding not big-endian"); |
a35e137a | 1619 | else |
39b3385d | 1620 | errstring = N_("ELF file data encoding not little-endian"); |
a35e137a | 1621 | } |
39b3385d UD |
1622 | else if (ehdr->e_ident[EI_VERSION] != EV_CURRENT) |
1623 | errstring | |
1624 | = N_("ELF file version ident does not match current one"); | |
a35e137a UD |
1625 | /* XXX We should be able so set system specific versions which are |
1626 | allowed here. */ | |
39b3385d UD |
1627 | else if (!VALID_ELF_OSABI (ehdr->e_ident[EI_OSABI])) |
1628 | errstring = N_("ELF file OS ABI invalid"); | |
d8c47894 UD |
1629 | else if (!VALID_ELF_ABIVERSION (ehdr->e_ident[EI_OSABI], |
1630 | ehdr->e_ident[EI_ABIVERSION])) | |
39b3385d | 1631 | errstring = N_("ELF file ABI version invalid"); |
04f2902d UD |
1632 | else if (memcmp (&ehdr->e_ident[EI_PAD], &expected[EI_PAD], |
1633 | EI_NIDENT - EI_PAD) != 0) | |
1634 | errstring = N_("nonzero padding in e_ident"); | |
39b3385d UD |
1635 | else |
1636 | /* Otherwise we don't know what went wrong. */ | |
1637 | errstring = N_("internal error"); | |
1638 | ||
1639 | goto call_lose; | |
a35e137a UD |
1640 | } |
1641 | ||
1b26b855 | 1642 | if (__glibc_unlikely (ehdr->e_version != EV_CURRENT)) |
39b3385d UD |
1643 | { |
1644 | errstring = N_("ELF file version does not match current one"); | |
1645 | goto call_lose; | |
1646 | } | |
277ae3f1 | 1647 | if (! __glibc_likely (elf_machine_matches_host (ehdr))) |
a986484f | 1648 | goto close_and_out; |
277ae3f1 PP |
1649 | else if (__glibc_unlikely (ehdr->e_type != ET_DYN |
1650 | && ehdr->e_type != ET_EXEC)) | |
39b3385d UD |
1651 | { |
1652 | errstring = N_("only ET_DYN and ET_EXEC can be loaded"); | |
1653 | goto call_lose; | |
1654 | } | |
1b26b855 | 1655 | else if (__glibc_unlikely (ehdr->e_phentsize != sizeof (ElfW(Phdr)))) |
5ce98c3f UD |
1656 | { |
1657 | errstring = N_("ELF file's phentsize not the expected size"); | |
1658 | goto call_lose; | |
1659 | } | |
a986484f UD |
1660 | |
1661 | maplength = ehdr->e_phnum * sizeof (ElfW(Phdr)); | |
6dd67bd5 | 1662 | if (ehdr->e_phoff + maplength <= (size_t) fbp->len) |
a986484f UD |
1663 | phdr = (void *) (fbp->buf + ehdr->e_phoff); |
1664 | else | |
1665 | { | |
1666 | phdr = alloca (maplength); | |
95c10569 LP |
1667 | if ((size_t) __pread64_nocancel (fd, (void *) phdr, maplength, |
1668 | ehdr->e_phoff) != maplength) | |
39b3385d UD |
1669 | { |
1670 | read_error: | |
1671 | errval = errno; | |
1672 | errstring = N_("cannot read file data"); | |
1673 | goto call_lose; | |
1674 | } | |
a986484f UD |
1675 | } |
1676 | ||
d6f373d2 MF |
1677 | if (__glibc_unlikely (elf_machine_reject_phdr_p |
1678 | (phdr, ehdr->e_phnum, fbp->buf, fbp->len, | |
1679 | loader, fd))) | |
1680 | goto close_and_out; | |
1681 | ||
a986484f UD |
1682 | /* Check .note.ABI-tag if present. */ |
1683 | for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph) | |
8a3ca0fd AS |
1684 | if (ph->p_type == PT_NOTE && ph->p_filesz >= 32 |
1685 | && (ph->p_align == 4 || ph->p_align == 8)) | |
a986484f | 1686 | { |
2f6773b5 UD |
1687 | ElfW(Addr) size = ph->p_filesz; |
1688 | ||
1689 | if (ph->p_offset + size <= (size_t) fbp->len) | |
a986484f UD |
1690 | abi_note = (void *) (fbp->buf + ph->p_offset); |
1691 | else | |
1692 | { | |
0065aaaa PP |
1693 | /* Note: __libc_use_alloca is not usable here, because |
1694 | thread info may not have been set up yet. */ | |
1695 | if (size < __MAX_ALLOCA_CUTOFF) | |
1696 | abi_note = alloca (size); | |
1697 | else | |
1698 | { | |
1699 | /* There could be multiple PT_NOTEs. */ | |
1700 | abi_note_malloced = realloc (abi_note_malloced, size); | |
1701 | if (abi_note_malloced == NULL) | |
1702 | goto read_error; | |
1703 | ||
1704 | abi_note = abi_note_malloced; | |
1705 | } | |
95c10569 LP |
1706 | if (__pread64_nocancel (fd, (void *) abi_note, size, |
1707 | ph->p_offset) != size) | |
0065aaaa PP |
1708 | { |
1709 | free (abi_note_malloced); | |
1710 | goto read_error; | |
1711 | } | |
2f6773b5 UD |
1712 | } |
1713 | ||
1714 | while (memcmp (abi_note, &expected_note, sizeof (expected_note))) | |
1715 | { | |
8d81ce0c L |
1716 | ElfW(Addr) note_size |
1717 | = ELF_NOTE_NEXT_OFFSET (abi_note[0], abi_note[1], | |
8a3ca0fd | 1718 | ph->p_align); |
39b3385d | 1719 | |
2f6773b5 UD |
1720 | if (size - 32 < note_size) |
1721 | { | |
1722 | size = 0; | |
1723 | break; | |
1724 | } | |
1725 | size -= note_size; | |
1726 | abi_note = (void *) abi_note + note_size; | |
a986484f UD |
1727 | } |
1728 | ||
2f6773b5 | 1729 | if (size == 0) |
a986484f UD |
1730 | continue; |
1731 | ||
55c91021 UD |
1732 | osversion = (abi_note[5] & 0xff) * 65536 |
1733 | + (abi_note[6] & 0xff) * 256 | |
1734 | + (abi_note[7] & 0xff); | |
1735 | if (abi_note[4] != __ABI_TAG_OS | |
afdca0f2 | 1736 | || (GLRO(dl_osversion) && GLRO(dl_osversion) < osversion)) |
a986484f UD |
1737 | { |
1738 | close_and_out: | |
329ea513 | 1739 | __close_nocancel (fd); |
a986484f UD |
1740 | __set_errno (ENOENT); |
1741 | fd = -1; | |
1742 | } | |
1743 | ||
1744 | break; | |
1745 | } | |
0065aaaa | 1746 | free (abi_note_malloced); |
a35e137a UD |
1747 | } |
1748 | ||
1749 | return fd; | |
1750 | } | |
1751 | \f | |
04ea3b0f | 1752 | /* Try to open NAME in one of the directories in *DIRSP. |
ba79d61b | 1753 | Return the fd, or -1. If successful, fill in *REALNAME |
04ea3b0f UD |
1754 | with the malloc'd full directory name. If it turns out |
1755 | that none of the directories in *DIRSP exists, *DIRSP is | |
1756 | replaced with (void *) -1, and the old value is free()d | |
1757 | if MAY_FREE_DIRS is true. */ | |
ba79d61b RM |
1758 | |
1759 | static int | |
a42faf59 | 1760 | open_path (const char *name, size_t namelen, int mode, |
a35e137a | 1761 | struct r_search_path_struct *sps, char **realname, |
7f71c55d UD |
1762 | struct filebuf *fbp, struct link_map *loader, int whatcode, |
1763 | bool *found_other_class) | |
ba79d61b | 1764 | { |
f55727ca | 1765 | struct r_search_path_elem **dirs = sps->dirs; |
ba79d61b | 1766 | char *buf; |
0a54e401 | 1767 | int fd = -1; |
b5efde2f | 1768 | const char *current_what = NULL; |
152e7964 | 1769 | int any = 0; |
ba79d61b | 1770 | |
a1ffb40e | 1771 | if (__glibc_unlikely (dirs == NULL)) |
ab1d521d RM |
1772 | /* We're called before _dl_init_paths when loading the main executable |
1773 | given on the command line when rtld is run directly. */ | |
1774 | return -1; | |
1775 | ||
5431ece5 | 1776 | buf = alloca (max_dirnamelen + max_capstrlen + namelen); |
ba79d61b RM |
1777 | do |
1778 | { | |
0a54e401 UD |
1779 | struct r_search_path_elem *this_dir = *dirs; |
1780 | size_t buflen = 0; | |
12264bd7 | 1781 | size_t cnt; |
b0b67c47 | 1782 | char *edp; |
f5858039 | 1783 | int here_any = 0; |
55c91021 | 1784 | int err; |
ba79d61b | 1785 | |
b5efde2f UD |
1786 | /* If we are debugging the search for libraries print the path |
1787 | now if it hasn't happened now. */ | |
1b26b855 | 1788 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS) |
cf197e41 | 1789 | && current_what != this_dir->what) |
b5efde2f UD |
1790 | { |
1791 | current_what = this_dir->what; | |
1792 | print_search_path (dirs, current_what, this_dir->where); | |
1793 | } | |
1794 | ||
b0b67c47 | 1795 | edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen); |
12264bd7 | 1796 | for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt) |
fd26970f | 1797 | { |
12264bd7 UD |
1798 | /* Skip this directory if we know it does not exist. */ |
1799 | if (this_dir->status[cnt] == nonexisting) | |
1800 | continue; | |
0a54e401 | 1801 | |
12264bd7 | 1802 | buflen = |
d6b5d570 UD |
1803 | ((char *) __mempcpy (__mempcpy (edp, capstr[cnt].str, |
1804 | capstr[cnt].len), | |
12264bd7 UD |
1805 | name, namelen) |
1806 | - buf); | |
1807 | ||
1808 | /* Print name we try if this is wanted. */ | |
a1ffb40e | 1809 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS)) |
154d10bd | 1810 | _dl_debug_printf (" trying file=%s\n", buf); |
b5efde2f | 1811 | |
c0d6f2a3 | 1812 | fd = open_verify (buf, -1, fbp, loader, whatcode, mode, |
a42faf59 | 1813 | found_other_class, false); |
12264bd7 | 1814 | if (this_dir->status[cnt] == unknown) |
6e4c40ba UD |
1815 | { |
1816 | if (fd != -1) | |
1817 | this_dir->status[cnt] = existing; | |
9dcafc55 UD |
1818 | /* Do not update the directory information when loading |
1819 | auditing code. We must try to disturb the program as | |
1820 | little as possible. */ | |
1821 | else if (loader == NULL | |
361a3706 | 1822 | || GL(dl_ns)[loader->l_ns]._ns_loaded->l_auditing == 0) |
6e4c40ba UD |
1823 | { |
1824 | /* We failed to open machine dependent library. Let's | |
1825 | test whether there is any directory at all. */ | |
5763742f | 1826 | struct stat64 st; |
6e4c40ba UD |
1827 | |
1828 | buf[buflen - namelen - 1] = '\0'; | |
1829 | ||
5763742f | 1830 | if (__xstat64 (_STAT_VER, buf, &st) != 0 |
6e4c40ba UD |
1831 | || ! S_ISDIR (st.st_mode)) |
1832 | /* The directory does not exist or it is no directory. */ | |
1833 | this_dir->status[cnt] = nonexisting; | |
1834 | else | |
1835 | this_dir->status[cnt] = existing; | |
1836 | } | |
1837 | } | |
fd26970f | 1838 | |
152e7964 | 1839 | /* Remember whether we found any existing directory. */ |
9dcafc55 | 1840 | here_any |= this_dir->status[cnt] != nonexisting; |
152e7964 | 1841 | |
1b26b855 | 1842 | if (fd != -1 && __glibc_unlikely (mode & __RTLD_SECURE) |
6bc6bd3b | 1843 | && __libc_enable_secure) |
c6222ab9 UD |
1844 | { |
1845 | /* This is an extra security effort to make sure nobody can | |
1846 | preload broken shared objects which are in the trusted | |
1847 | directories and so exploit the bugs. */ | |
5763742f | 1848 | struct stat64 st; |
c6222ab9 | 1849 | |
5763742f | 1850 | if (__fxstat64 (_STAT_VER, fd, &st) != 0 |
c6222ab9 UD |
1851 | || (st.st_mode & S_ISUID) == 0) |
1852 | { | |
1853 | /* The shared object cannot be tested for being SUID | |
1854 | or this bit is not set. In this case we must not | |
1855 | use this object. */ | |
329ea513 | 1856 | __close_nocancel (fd); |
c6222ab9 UD |
1857 | fd = -1; |
1858 | /* We simply ignore the file, signal this by setting | |
1859 | the error value which would have been set by `open'. */ | |
1860 | errno = ENOENT; | |
1861 | } | |
1862 | } | |
ba79d61b RM |
1863 | } |
1864 | ||
ba79d61b RM |
1865 | if (fd != -1) |
1866 | { | |
2f653c01 | 1867 | *realname = (char *) malloc (buflen); |
c6222ab9 | 1868 | if (*realname != NULL) |
ba79d61b RM |
1869 | { |
1870 | memcpy (*realname, buf, buflen); | |
1871 | return fd; | |
1872 | } | |
1873 | else | |
1874 | { | |
1875 | /* No memory for the name, we certainly won't be able | |
1876 | to load and link it. */ | |
329ea513 | 1877 | __close_nocancel (fd); |
ba79d61b RM |
1878 | return -1; |
1879 | } | |
1880 | } | |
55c91021 | 1881 | if (here_any && (err = errno) != ENOENT && err != EACCES) |
ba79d61b RM |
1882 | /* The file exists and is readable, but something went wrong. */ |
1883 | return -1; | |
f5858039 UD |
1884 | |
1885 | /* Remember whether we found anything. */ | |
1886 | any |= here_any; | |
ba79d61b | 1887 | } |
0a54e401 | 1888 | while (*++dirs != NULL); |
ba79d61b | 1889 | |
152e7964 | 1890 | /* Remove the whole path if none of the directories exists. */ |
a1ffb40e | 1891 | if (__glibc_unlikely (! any)) |
152e7964 | 1892 | { |
04ea3b0f UD |
1893 | /* Paths which were allocated using the minimal malloc() in ld.so |
1894 | must not be freed using the general free() in libc. */ | |
f55727ca UD |
1895 | if (sps->malloced) |
1896 | free (sps->dirs); | |
11bf311e | 1897 | |
9317ea65 | 1898 | /* rtld_search_dirs and env_path_list are attribute_relro, therefore |
c01ae97e | 1899 | avoid writing into it. */ |
9317ea65 | 1900 | if (sps != &rtld_search_dirs && sps != &env_path_list) |
ecc1d0c3 | 1901 | sps->dirs = (void *) -1; |
152e7964 UD |
1902 | } |
1903 | ||
ba79d61b RM |
1904 | return -1; |
1905 | } | |
1906 | ||
1907 | /* Map in the shared object file NAME. */ | |
1908 | ||
1909 | struct link_map * | |
8e9f92e9 | 1910 | _dl_map_object (struct link_map *loader, const char *name, |
c0f62c56 | 1911 | int type, int trace_mode, int mode, Lmid_t nsid) |
ba79d61b RM |
1912 | { |
1913 | int fd; | |
a1b85ae8 | 1914 | const char *origname = NULL; |
ba79d61b | 1915 | char *realname; |
14bab8de | 1916 | char *name_copy; |
ba79d61b | 1917 | struct link_map *l; |
a35e137a | 1918 | struct filebuf fb; |
ba79d61b | 1919 | |
c0f62c56 | 1920 | assert (nsid >= 0); |
22c83193 | 1921 | assert (nsid < GL(dl_nns)); |
c0f62c56 | 1922 | |
ba79d61b | 1923 | /* Look for this name among those already loaded. */ |
c0f62c56 | 1924 | for (l = GL(dl_ns)[nsid]._ns_loaded; l; l = l->l_next) |
f41c8091 UD |
1925 | { |
1926 | /* If the requested name matches the soname of a loaded object, | |
1927 | use that object. Elide this check for names that have not | |
1928 | yet been opened. */ | |
277ae3f1 | 1929 | if (__glibc_unlikely ((l->l_faked | l->l_removed) != 0)) |
f41c8091 UD |
1930 | continue; |
1931 | if (!_dl_name_match_p (name, l)) | |
1932 | { | |
1933 | const char *soname; | |
1934 | ||
1b26b855 | 1935 | if (__glibc_likely (l->l_soname_added) |
c91bc73e | 1936 | || l->l_info[DT_SONAME] == NULL) |
f41c8091 UD |
1937 | continue; |
1938 | ||
8699e7b1 UD |
1939 | soname = ((const char *) D_PTR (l, l_info[DT_STRTAB]) |
1940 | + l->l_info[DT_SONAME]->d_un.d_val); | |
f41c8091 UD |
1941 | if (strcmp (name, soname) != 0) |
1942 | continue; | |
1943 | ||
1944 | /* We have a match on a new name -- cache it. */ | |
76156ea1 | 1945 | add_name_to_object (l, soname); |
c91bc73e | 1946 | l->l_soname_added = 1; |
f41c8091 UD |
1947 | } |
1948 | ||
42c4f32a | 1949 | /* We have a match. */ |
f41c8091 UD |
1950 | return l; |
1951 | } | |
ba79d61b | 1952 | |
8193034b | 1953 | /* Display information if we are debugging. */ |
1b26b855 | 1954 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES) |
7969407a | 1955 | && loader != NULL) |
07df30d9 | 1956 | _dl_debug_printf ((mode & __RTLD_CALLMAP) == 0 |
9ac533d3 UD |
1957 | ? "\nfile=%s [%lu]; needed by %s [%lu]\n" |
1958 | : "\nfile=%s [%lu]; dynamically loaded by %s [%lu]\n", | |
b9375348 | 1959 | name, nsid, DSO_FILENAME (loader->l_name), loader->l_ns); |
8193034b | 1960 | |
9dcafc55 UD |
1961 | #ifdef SHARED |
1962 | /* Give the auditing libraries a chance to change the name before we | |
1963 | try anything. */ | |
1b26b855 | 1964 | if (__glibc_unlikely (GLRO(dl_naudit) > 0) |
9dcafc55 UD |
1965 | && (loader == NULL || loader->l_auditing == 0)) |
1966 | { | |
1967 | struct audit_ifaces *afct = GLRO(dl_audit); | |
1968 | for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) | |
1969 | { | |
1970 | if (afct->objsearch != NULL) | |
1971 | { | |
a1b85ae8 | 1972 | const char *before = name; |
9dcafc55 UD |
1973 | name = afct->objsearch (name, &loader->l_audit[cnt].cookie, |
1974 | LA_SER_ORIG); | |
1975 | if (name == NULL) | |
1976 | { | |
1977 | /* Do not try anything further. */ | |
1978 | fd = -1; | |
1979 | goto no_file; | |
1980 | } | |
a1b85ae8 FW |
1981 | if (before != name && strcmp (before, name) != 0) |
1982 | { | |
1983 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES)) | |
1984 | _dl_debug_printf ("audit changed filename %s -> %s\n", | |
1985 | before, name); | |
1986 | ||
1987 | if (origname == NULL) | |
1988 | origname = before; | |
1989 | } | |
9dcafc55 UD |
1990 | } |
1991 | ||
1992 | afct = afct->next; | |
1993 | } | |
1994 | } | |
1995 | #endif | |
1996 | ||
7f71c55d UD |
1997 | /* Will be true if we found a DSO which is of the other ELF class. */ |
1998 | bool found_other_class = false; | |
1999 | ||
ba79d61b RM |
2000 | if (strchr (name, '/') == NULL) |
2001 | { | |
2002 | /* Search for NAME in several places. */ | |
2003 | ||
2004 | size_t namelen = strlen (name) + 1; | |
2005 | ||
a1ffb40e | 2006 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS)) |
c0f62c56 | 2007 | _dl_debug_printf ("find library=%s [%lu]; searching\n", name, nsid); |
b5efde2f | 2008 | |
ba79d61b | 2009 | fd = -1; |
a23db8e4 | 2010 | |
fcf70d41 | 2011 | /* When the object has the RUNPATH information we don't use any |
844c394a | 2012 | RPATHs. */ |
4c540916 | 2013 | if (loader == NULL || loader->l_info[DT_RUNPATH] == NULL) |
fcf70d41 | 2014 | { |
a1f0de82 UD |
2015 | /* This is the executable's map (if there is one). Make sure that |
2016 | we do not look at it twice. */ | |
2017 | struct link_map *main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded; | |
2018 | bool did_main_map = false; | |
2019 | ||
fcf70d41 UD |
2020 | /* First try the DT_RPATH of the dependent object that caused NAME |
2021 | to be loaded. Then that object's dependent, and on up. */ | |
a1f0de82 | 2022 | for (l = loader; l; l = l->l_loader) |
45e4762c | 2023 | if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH")) |
a1f0de82 | 2024 | { |
a42faf59 | 2025 | fd = open_path (name, namelen, mode, |
8e9f92e9 | 2026 | &l->l_rpath_dirs, |
a1f0de82 UD |
2027 | &realname, &fb, loader, LA_SER_RUNPATH, |
2028 | &found_other_class); | |
2029 | if (fd != -1) | |
2030 | break; | |
2031 | ||
2032 | did_main_map |= l == main_map; | |
2033 | } | |
0a54e401 | 2034 | |
fcf70d41 | 2035 | /* If dynamically linked, try the DT_RPATH of the executable |
844c394a | 2036 | itself. NB: we do this for lookups in any namespace. */ |
a1f0de82 UD |
2037 | if (fd == -1 && !did_main_map |
2038 | && main_map != NULL && main_map->l_type != lt_loaded | |
2039 | && cache_rpath (main_map, &main_map->l_rpath_dirs, DT_RPATH, | |
2040 | "RPATH")) | |
a42faf59 | 2041 | fd = open_path (name, namelen, mode, |
8e9f92e9 | 2042 | &main_map->l_rpath_dirs, |
a1f0de82 UD |
2043 | &realname, &fb, loader ?: main_map, LA_SER_RUNPATH, |
2044 | &found_other_class); | |
fcf70d41 | 2045 | } |
fd26970f | 2046 | |
7ef90c15 | 2047 | /* Try the LD_LIBRARY_PATH environment variable. */ |
f55727ca | 2048 | if (fd == -1 && env_path_list.dirs != (void *) -1) |
a42faf59 | 2049 | fd = open_path (name, namelen, mode, &env_path_list, |
9dcafc55 UD |
2050 | &realname, &fb, |
2051 | loader ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded, | |
7f71c55d | 2052 | LA_SER_LIBPATH, &found_other_class); |
40a55d20 | 2053 | |
45e4762c RM |
2054 | /* Look at the RUNPATH information for this binary. */ |
2055 | if (fd == -1 && loader != NULL | |
2056 | && cache_rpath (loader, &loader->l_runpath_dirs, | |
2057 | DT_RUNPATH, "RUNPATH")) | |
a42faf59 | 2058 | fd = open_path (name, namelen, mode, |
9dcafc55 | 2059 | &loader->l_runpath_dirs, &realname, &fb, loader, |
7f71c55d | 2060 | LA_SER_RUNPATH, &found_other_class); |
fcf70d41 | 2061 | |
c0d6f2a3 RM |
2062 | if (fd == -1) |
2063 | { | |
2064 | realname = _dl_sysdep_open_object (name, namelen, &fd); | |
2065 | if (realname != NULL) | |
2066 | { | |
2067 | fd = open_verify (realname, fd, | |
2068 | &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded, | |
2069 | LA_SER_CONFIG, mode, &found_other_class, | |
2070 | false); | |
2071 | if (fd == -1) | |
2072 | free (realname); | |
2073 | } | |
2074 | } | |
2075 | ||
f57f8055 | 2076 | #ifdef USE_LDCONFIG |
55c91021 | 2077 | if (fd == -1 |
277ae3f1 | 2078 | && (__glibc_likely ((mode & __RTLD_SECURE) == 0) |
6bc6bd3b | 2079 | || ! __libc_enable_secure) |
1b26b855 | 2080 | && __glibc_likely (GLRO(dl_inhibit_cache) == 0)) |
f18edac3 RM |
2081 | { |
2082 | /* Check the list of libraries in the file /etc/ld.so.cache, | |
2083 | for compatibility with Linux's ldconfig program. */ | |
ccdb048d | 2084 | char *cached = _dl_load_cache_lookup (name); |
0f6b172f | 2085 | |
2f653c01 UD |
2086 | if (cached != NULL) |
2087 | { | |
c0f62c56 | 2088 | // XXX Correct to unconditionally default to namespace 0? |
6a5ee102 UD |
2089 | l = (loader |
2090 | ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded | |
0d23a5c1 MR |
2091 | # ifdef SHARED |
2092 | ?: &GL(dl_rtld_map) | |
f57f8055 | 2093 | # endif |
0d23a5c1 | 2094 | ); |
0f6b172f | 2095 | |
266bb989 UD |
2096 | /* If the loader has the DF_1_NODEFLIB flag set we must not |
2097 | use a cache entry from any of these directories. */ | |
1b26b855 | 2098 | if (__glibc_unlikely (l->l_flags_1 & DF_1_NODEFLIB)) |
266bb989 UD |
2099 | { |
2100 | const char *dirp = system_dirs; | |
2e47aff5 | 2101 | unsigned int cnt = 0; |
266bb989 UD |
2102 | |
2103 | do | |
2104 | { | |
2105 | if (memcmp (cached, dirp, system_dirs_len[cnt]) == 0) | |
2106 | { | |
2107 | /* The prefix matches. Don't use the entry. */ | |
ccdb048d | 2108 | free (cached); |
266bb989 UD |
2109 | cached = NULL; |
2110 | break; | |
2111 | } | |
2112 | ||
2113 | dirp += system_dirs_len[cnt] + 1; | |
2114 | ++cnt; | |
2115 | } | |
4a6d1198 | 2116 | while (cnt < nsystem_dirs_len); |
266bb989 UD |
2117 | } |
2118 | ||
2f653c01 | 2119 | if (cached != NULL) |
f18edac3 | 2120 | { |
c0d6f2a3 | 2121 | fd = open_verify (cached, -1, |
9dcafc55 | 2122 | &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded, |
a42faf59 PP |
2123 | LA_SER_CONFIG, mode, &found_other_class, |
2124 | false); | |
a1ffb40e | 2125 | if (__glibc_likely (fd != -1)) |
ccdb048d CD |
2126 | realname = cached; |
2127 | else | |
2128 | free (cached); | |
f18edac3 RM |
2129 | } |
2130 | } | |
2131 | } | |
f57f8055 | 2132 | #endif |
0a54e401 | 2133 | |
a23db8e4 | 2134 | /* Finally, try the default path. */ |
266bb989 | 2135 | if (fd == -1 |
c0f62c56 | 2136 | && ((l = loader ?: GL(dl_ns)[nsid]._ns_loaded) == NULL |
1b26b855 | 2137 | || __glibc_likely (!(l->l_flags_1 & DF_1_NODEFLIB))) |
f55727ca | 2138 | && rtld_search_dirs.dirs != (void *) -1) |
a42faf59 | 2139 | fd = open_path (name, namelen, mode, &rtld_search_dirs, |
7f71c55d | 2140 | &realname, &fb, l, LA_SER_DEFAULT, &found_other_class); |
b5efde2f | 2141 | |
fd77c361 | 2142 | /* Add another newline when we are tracing the library loading. */ |
a1ffb40e | 2143 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS)) |
844c394a | 2144 | _dl_debug_printf ("\n"); |
ba79d61b RM |
2145 | } |
2146 | else | |
2147 | { | |
f787edde UD |
2148 | /* The path may contain dynamic string tokens. */ |
2149 | realname = (loader | |
2bd86632 | 2150 | ? expand_dynamic_string_token (loader, name) |
cc00cece | 2151 | : __strdup (name)); |
f787edde UD |
2152 | if (realname == NULL) |
2153 | fd = -1; | |
2154 | else | |
ba79d61b | 2155 | { |
c0d6f2a3 | 2156 | fd = open_verify (realname, -1, &fb, |
a42faf59 | 2157 | loader ?: GL(dl_ns)[nsid]._ns_loaded, 0, mode, |
2e0fc40c | 2158 | &found_other_class, true); |
1b26b855 | 2159 | if (__glibc_unlikely (fd == -1)) |
f787edde | 2160 | free (realname); |
ba79d61b RM |
2161 | } |
2162 | } | |
2163 | ||
9dcafc55 UD |
2164 | #ifdef SHARED |
2165 | no_file: | |
2166 | #endif | |
c14e9135 UD |
2167 | /* In case the LOADER information has only been provided to get to |
2168 | the appropriate RUNPATH/RPATH information we do not need it | |
2169 | anymore. */ | |
2170 | if (mode & __RTLD_CALLMAP) | |
2171 | loader = NULL; | |
2172 | ||
1b26b855 | 2173 | if (__glibc_unlikely (fd == -1)) |
46ec036d | 2174 | { |
32e6df36 | 2175 | if (trace_mode |
1b26b855 | 2176 | && __glibc_likely ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK) == 0)) |
46ec036d UD |
2177 | { |
2178 | /* We haven't found an appropriate library. But since we | |
2179 | are only interested in the list of libraries this isn't | |
2180 | so severe. Fake an entry with all the information we | |
1ef32c3d | 2181 | have. */ |
a1eca9f3 | 2182 | static const Elf_Symndx dummy_bucket = STN_UNDEF; |
46ec036d | 2183 | |
f0967738 | 2184 | /* Allocate a new object map. */ |
cc00cece | 2185 | if ((name_copy = __strdup (name)) == NULL |
1fc07491 | 2186 | || (l = _dl_new_object (name_copy, name, type, loader, |
c0f62c56 | 2187 | mode, nsid)) == NULL) |
2e0fc40c UD |
2188 | { |
2189 | free (name_copy); | |
2190 | _dl_signal_error (ENOMEM, name, NULL, | |
2191 | N_("cannot create shared object descriptor")); | |
2192 | } | |
a881e0a0 UD |
2193 | /* Signal that this is a faked entry. */ |
2194 | l->l_faked = 1; | |
2195 | /* Since the descriptor is initialized with zero we do not | |
126b06f9 | 2196 | have do this here. |
126b06f9 | 2197 | l->l_reserved = 0; */ |
fd26970f UD |
2198 | l->l_buckets = &dummy_bucket; |
2199 | l->l_nbuckets = 1; | |
2200 | l->l_relocated = 1; | |
2201 | ||
f0967738 AK |
2202 | /* Enter the object in the object list. */ |
2203 | _dl_add_to_namespace_list (l, nsid); | |
2204 | ||
fd26970f | 2205 | return l; |
46ec036d | 2206 | } |
7f71c55d UD |
2207 | else if (found_other_class) |
2208 | _dl_signal_error (0, name, NULL, | |
2209 | ELFW(CLASS) == ELFCLASS32 | |
2210 | ? N_("wrong ELF class: ELFCLASS64") | |
2211 | : N_("wrong ELF class: ELFCLASS32")); | |
46ec036d | 2212 | else |
154d10bd UD |
2213 | _dl_signal_error (errno, name, NULL, |
2214 | N_("cannot open shared object file")); | |
46ec036d | 2215 | } |
ba79d61b | 2216 | |
d1fc817e | 2217 | void *stack_end = __libc_stack_end; |
a1b85ae8 FW |
2218 | return _dl_map_object_from_fd (name, origname, fd, &fb, realname, loader, |
2219 | type, mode, &stack_end, nsid); | |
ba79d61b | 2220 | } |
154d10bd | 2221 | |
b8c80a7e KS |
2222 | struct add_path_state |
2223 | { | |
2224 | bool counting; | |
2225 | unsigned int idx; | |
2226 | Dl_serinfo *si; | |
2227 | char *allocptr; | |
2228 | }; | |
2229 | ||
2230 | static void | |
2231 | add_path (struct add_path_state *p, const struct r_search_path_struct *sps, | |
2232 | unsigned int flags) | |
2233 | { | |
2234 | if (sps->dirs != (void *) -1) | |
2235 | { | |
2236 | struct r_search_path_elem **dirs = sps->dirs; | |
2237 | do | |
2238 | { | |
2239 | const struct r_search_path_elem *const r = *dirs++; | |
2240 | if (p->counting) | |
2241 | { | |
2242 | p->si->dls_cnt++; | |
2243 | p->si->dls_size += MAX (2, r->dirnamelen); | |
2244 | } | |
2245 | else | |
2246 | { | |
2247 | Dl_serpath *const sp = &p->si->dls_serpath[p->idx++]; | |
2248 | sp->dls_name = p->allocptr; | |
2249 | if (r->dirnamelen < 2) | |
2250 | *p->allocptr++ = r->dirnamelen ? '/' : '.'; | |
2251 | else | |
2252 | p->allocptr = __mempcpy (p->allocptr, | |
2253 | r->dirname, r->dirnamelen - 1); | |
2254 | *p->allocptr++ = '\0'; | |
2255 | sp->dls_flags = flags; | |
2256 | } | |
2257 | } | |
2258 | while (*dirs != NULL); | |
2259 | } | |
2260 | } | |
45e4762c RM |
2261 | |
2262 | void | |
45e4762c RM |
2263 | _dl_rtld_di_serinfo (struct link_map *loader, Dl_serinfo *si, bool counting) |
2264 | { | |
2265 | if (counting) | |
2266 | { | |
2267 | si->dls_cnt = 0; | |
2268 | si->dls_size = 0; | |
2269 | } | |
2270 | ||
b8c80a7e | 2271 | struct add_path_state p = |
45e4762c | 2272 | { |
b8c80a7e KS |
2273 | .counting = counting, |
2274 | .idx = 0, | |
2275 | .si = si, | |
2276 | .allocptr = (char *) &si->dls_serpath[si->dls_cnt] | |
2277 | }; | |
2278 | ||
2279 | # define add_path(p, sps, flags) add_path(p, sps, 0) /* XXX */ | |
45e4762c RM |
2280 | |
2281 | /* When the object has the RUNPATH information we don't use any RPATHs. */ | |
2282 | if (loader->l_info[DT_RUNPATH] == NULL) | |
2283 | { | |
2284 | /* First try the DT_RPATH of the dependent object that caused NAME | |
2285 | to be loaded. Then that object's dependent, and on up. */ | |
2286 | ||
2287 | struct link_map *l = loader; | |
2288 | do | |
2289 | { | |
2290 | if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH")) | |
b8c80a7e | 2291 | add_path (&p, &l->l_rpath_dirs, XXX_RPATH); |
45e4762c RM |
2292 | l = l->l_loader; |
2293 | } | |
2294 | while (l != NULL); | |
2295 | ||
2296 | /* If dynamically linked, try the DT_RPATH of the executable itself. */ | |
c0f62c56 UD |
2297 | if (loader->l_ns == LM_ID_BASE) |
2298 | { | |
2299 | l = GL(dl_ns)[LM_ID_BASE]._ns_loaded; | |
2300 | if (l != NULL && l->l_type != lt_loaded && l != loader) | |
2301 | if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH")) | |
b8c80a7e | 2302 | add_path (&p, &l->l_rpath_dirs, XXX_RPATH); |
c0f62c56 | 2303 | } |
45e4762c RM |
2304 | } |
2305 | ||
2306 | /* Try the LD_LIBRARY_PATH environment variable. */ | |
b8c80a7e | 2307 | add_path (&p, &env_path_list, XXX_ENV); |
45e4762c RM |
2308 | |
2309 | /* Look at the RUNPATH information for this binary. */ | |
2310 | if (cache_rpath (loader, &loader->l_runpath_dirs, DT_RUNPATH, "RUNPATH")) | |
b8c80a7e | 2311 | add_path (&p, &loader->l_runpath_dirs, XXX_RUNPATH); |
45e4762c RM |
2312 | |
2313 | /* XXX | |
2314 | Here is where ld.so.cache gets checked, but we don't have | |
2315 | a way to indicate that in the results for Dl_serinfo. */ | |
2316 | ||
2317 | /* Finally, try the default path. */ | |
2318 | if (!(loader->l_flags_1 & DF_1_NODEFLIB)) | |
b8c80a7e | 2319 | add_path (&p, &rtld_search_dirs, XXX_default); |
45e4762c RM |
2320 | |
2321 | if (counting) | |
2322 | /* Count the struct size before the string area, which we didn't | |
2323 | know before we completed dls_cnt. */ | |
2324 | si->dls_size += (char *) &si->dls_serpath[si->dls_cnt] - (char *) si; | |
2325 | } |