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