1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright 1996-1998 John D. Polstra.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/param.h>
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31
32 #include <errno.h>
33 #include <stddef.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include "debug.h"
39 #include "rtld.h"
40
41 static Elf_Ehdr *get_elf_header(int, const char *, const struct stat *,
42 Elf_Phdr **phdr);
43 static int convert_flags(int); /* Elf flags -> mmap flags */
44
45 static bool
phdr_in_zero_page(const Elf_Ehdr * hdr)46 phdr_in_zero_page(const Elf_Ehdr *hdr)
47 {
48 return (hdr->e_phoff + hdr->e_phnum * sizeof(Elf_Phdr) <= page_size);
49 }
50
51 /*
52 * Map a shared object into memory. The "fd" argument is a file descriptor,
53 * which must be open on the object and positioned at its beginning.
54 * The "path" argument is a pathname that is used only for error messages.
55 *
56 * The return value is a pointer to a newly-allocated Obj_Entry structure
57 * for the shared object. Returns NULL on failure.
58 */
59 Obj_Entry *
map_object(int fd,const char * path,const struct stat * sb,bool ismain)60 map_object(int fd, const char *path, const struct stat *sb, bool ismain)
61 {
62 Obj_Entry *obj;
63 Elf_Ehdr *hdr;
64 int i;
65 Elf_Phdr *phdr;
66 Elf_Phdr *phlimit;
67 Elf_Phdr **segs;
68 int nsegs;
69 Elf_Phdr *phdyn;
70 Elf_Phdr *phinterp;
71 Elf_Phdr *phtls;
72 caddr_t mapbase;
73 size_t mapsize;
74 Elf_Addr base_vaddr;
75 Elf_Addr base_vlimit;
76 caddr_t base_addr;
77 int base_flags;
78 Elf_Off data_offset;
79 Elf_Addr data_vaddr;
80 Elf_Addr data_vlimit;
81 caddr_t data_addr;
82 int data_prot;
83 int data_flags;
84 Elf_Addr clear_vaddr;
85 caddr_t clear_addr;
86 caddr_t clear_page;
87 Elf_Addr phdr_vaddr;
88 size_t nclear, phsize;
89 Elf_Addr bss_vaddr;
90 Elf_Addr bss_vlimit;
91 caddr_t bss_addr;
92 Elf_Word stack_flags;
93 Elf_Addr note_start;
94 Elf_Addr note_end;
95 char *note_map;
96 size_t note_map_len;
97
98 hdr = get_elf_header(fd, path, sb, &phdr);
99 if (hdr == NULL)
100 return (NULL);
101
102 /*
103 * Scan the program header entries, and save key information.
104 * We expect that the loadable segments are ordered by load address.
105 */
106 phsize = hdr->e_phnum * sizeof(phdr[0]);
107 phlimit = phdr + hdr->e_phnum;
108 nsegs = -1;
109 phdyn = phinterp = phtls = NULL;
110 phdr_vaddr = 0;
111 note_start = 0;
112 note_end = 0;
113 note_map = NULL;
114 note_map_len = 0;
115 segs = xcalloc(hdr->e_phnum, sizeof(segs[0]));
116 stack_flags = PF_X | PF_R | PF_W;
117 while (phdr < phlimit) {
118 switch (phdr->p_type) {
119 case PT_INTERP:
120 phinterp = phdr;
121 break;
122
123 case PT_LOAD:
124 if (phdr->p_memsz < phdr->p_filesz) {
125 _rtld_error("%s: invalid PT_LOAD segment",
126 path);
127 goto error;
128 }
129
130 segs[++nsegs] = phdr;
131 if ((segs[nsegs]->p_align & (page_size - 1)) != 0) {
132 _rtld_error(
133 "%s: PT_LOAD segment %d not page-aligned",
134 path, nsegs);
135 goto error;
136 }
137 break;
138
139 case PT_PHDR:
140 phdr_vaddr = phdr->p_vaddr;
141 phsize = phdr->p_memsz;
142 break;
143
144 case PT_DYNAMIC:
145 phdyn = phdr;
146 break;
147
148 case PT_TLS:
149 if (phdr->p_memsz < phdr->p_filesz) {
150 _rtld_error("%s: invalid PT_TLS segment",
151 path);
152 goto error;
153 }
154
155 phtls = phdr;
156 break;
157
158 case PT_GNU_STACK:
159 stack_flags = phdr->p_flags;
160 break;
161
162 case PT_NOTE:
163 if (phdr->p_offset > page_size ||
164 phdr->p_offset + phdr->p_filesz > page_size) {
165 note_map_len = rtld_round_page(phdr->p_offset +
166 phdr->p_filesz) -
167 rtld_trunc_page(phdr->p_offset);
168 note_map = mmap(NULL, note_map_len, PROT_READ,
169 MAP_PRIVATE, fd,
170 rtld_trunc_page(phdr->p_offset));
171 if (note_map == MAP_FAILED) {
172 _rtld_error(
173 "%s: error mapping PT_NOTE (%d)",
174 path, errno);
175 goto error;
176 }
177 note_start = (Elf_Addr)(note_map +
178 phdr->p_offset -
179 rtld_trunc_page(phdr->p_offset));
180 } else {
181 note_start = (Elf_Addr)(char *)hdr +
182 phdr->p_offset;
183 }
184 note_end = note_start + phdr->p_filesz;
185 break;
186 }
187
188 ++phdr;
189 }
190 if (phdyn == NULL) {
191 _rtld_error("%s: object is not dynamically-linked", path);
192 goto error;
193 }
194
195 if (nsegs < 0) {
196 _rtld_error("%s: too few PT_LOAD segments", path);
197 goto error;
198 }
199
200 /*
201 * Map the entire address space of the object, to stake out our
202 * contiguous region, and to establish the base address for relocation.
203 */
204 base_vaddr = rtld_trunc_page(segs[0]->p_vaddr);
205 base_vlimit = rtld_round_page(segs[nsegs]->p_vaddr +
206 segs[nsegs]->p_memsz);
207 mapsize = base_vlimit - base_vaddr;
208 base_addr = (caddr_t)base_vaddr;
209 base_flags = MAP_GUARD;
210 if (npagesizes > 1 && rtld_round_page(segs[0]->p_filesz) >=
211 pagesizes[1])
212 base_flags |= MAP_ALIGNED_SUPER;
213 if (base_vaddr != 0)
214 base_flags |= MAP_FIXED | MAP_EXCL;
215
216 mapbase = mmap(base_addr, mapsize, PROT_NONE, base_flags, -1, 0);
217 if (mapbase == MAP_FAILED) {
218 _rtld_error("%s: mmap of entire address space failed: %s",
219 path, rtld_strerror(errno));
220 goto error;
221 }
222 if (base_addr != NULL && mapbase != base_addr) {
223 _rtld_error(
224 "%s: mmap returned wrong address: wanted %p, got %p",
225 path, base_addr, mapbase);
226 goto error1;
227 }
228
229 for (i = 0; i <= nsegs; i++) {
230 /* Overlay the segment onto the proper region. */
231 data_offset = rtld_trunc_page(segs[i]->p_offset);
232 data_vaddr = rtld_trunc_page(segs[i]->p_vaddr);
233 data_vlimit = rtld_round_page(segs[i]->p_vaddr +
234 segs[i]->p_filesz);
235 data_addr = mapbase + (data_vaddr - base_vaddr);
236 data_prot = convert_prot(segs[i]->p_flags);
237 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
238 if (data_vlimit != data_vaddr && mmap(data_addr,
239 data_vlimit - data_vaddr, data_prot, data_flags |
240 MAP_PREFAULT_READ, fd, data_offset) == MAP_FAILED) {
241 _rtld_error("%s: mmap of data failed: %s",
242 path, rtld_strerror(errno));
243 goto error1;
244 }
245
246 /* Do BSS setup */
247 if (segs[i]->p_filesz != segs[i]->p_memsz) {
248 /* Clear any BSS in the last page of the segment. */
249 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
250 clear_addr = mapbase + (clear_vaddr - base_vaddr);
251 clear_page = mapbase + (rtld_trunc_page(clear_vaddr) -
252 base_vaddr);
253
254 if ((nclear = data_vlimit - clear_vaddr) > 0) {
255 /*
256 * Make sure the end of the segment is
257 * writable.
258 */
259 if ((data_prot & PROT_WRITE) == 0 &&
260 mprotect(clear_page, page_size,
261 data_prot | PROT_WRITE) == -1) {
262 _rtld_error("%s: mprotect failed: %s",
263 path, rtld_strerror(errno));
264 goto error1;
265 }
266
267 memset(clear_addr, 0, nclear);
268
269 /* Reset the data protection back */
270 if ((data_prot & PROT_WRITE) == 0)
271 mprotect(clear_page, page_size,
272 data_prot);
273 }
274
275 /* Overlay the BSS segment onto the proper region. */
276 bss_vaddr = data_vlimit;
277 bss_vlimit = rtld_round_page(segs[i]->p_vaddr +
278 segs[i]->p_memsz);
279 bss_addr = mapbase + (bss_vaddr - base_vaddr);
280 if (bss_vlimit > bss_vaddr) {
281 /* There is something to do */
282 if (mmap(bss_addr, bss_vlimit - bss_vaddr,
283 data_prot, data_flags | MAP_ANON, -1,
284 0) == MAP_FAILED) {
285 _rtld_error(
286 "%s: mmap of bss failed: %s",
287 path, rtld_strerror(errno));
288 goto error1;
289 }
290 }
291 }
292
293 if (phdr_vaddr == 0 && data_offset <= hdr->e_phoff &&
294 data_vlimit - data_vaddr + data_offset >=
295 hdr->e_phoff + hdr->e_phnum * sizeof(Elf_Phdr)) {
296 phdr_vaddr = data_vaddr + hdr->e_phoff - data_offset;
297 }
298 }
299
300 obj = obj_new();
301 if (sb != NULL) {
302 obj->dev = sb->st_dev;
303 obj->ino = sb->st_ino;
304 }
305 obj->mapbase = mapbase;
306 obj->mapsize = mapsize;
307 obj->vaddrbase = base_vaddr;
308 obj->relocbase = mapbase - base_vaddr;
309 obj->dynamic = (const Elf_Dyn *)(obj->relocbase + phdyn->p_vaddr);
310 if (hdr->e_entry != 0)
311 obj->entry = (caddr_t)(obj->relocbase + hdr->e_entry);
312 if (phdr_vaddr != 0) {
313 obj->phdr = (const Elf_Phdr *)(obj->relocbase + phdr_vaddr);
314 } else {
315 obj->phdr = malloc(phsize);
316 if (obj->phdr == NULL) {
317 obj_free(obj);
318 _rtld_error("%s: cannot allocate program header",
319 path);
320 goto error1;
321 }
322 memcpy(__DECONST(char *, obj->phdr), (char *)hdr + hdr->e_phoff,
323 phsize);
324 obj->phdr_alloc = true;
325 }
326 obj->phnum = phsize / sizeof(*phdr);
327 if (phinterp != NULL)
328 obj->interp = (const char *)(obj->relocbase +
329 phinterp->p_vaddr);
330 if (phtls != NULL) {
331 if (ismain)
332 obj->tlsindex = 1;
333 else {
334 tls_dtv_generation++;
335 obj->tlsindex = ++tls_max_index;
336 }
337 obj->tlssize = phtls->p_memsz;
338 obj->tlsalign = phtls->p_align;
339 obj->tlspoffset = phtls->p_offset;
340 obj->tlsinitsize = phtls->p_filesz;
341 obj->tlsinit = obj->relocbase + phtls->p_vaddr;
342 }
343 obj->stack_flags = stack_flags;
344 if (note_start < note_end)
345 digest_notes(obj, note_start, note_end);
346 finish:
347 if (note_map != NULL && note_map != MAP_FAILED)
348 munmap(note_map, note_map_len);
349 munmap(hdr, page_size);
350 free(segs);
351 return (obj);
352
353 error1:
354 munmap(mapbase, mapsize);
355 error:
356 if (!phdr_in_zero_page(hdr))
357 munmap(phdr, hdr->e_phnum * sizeof(phdr[0]));
358 obj = NULL;
359 goto finish;
360 }
361
362 bool
check_elf_headers(const Elf_Ehdr * hdr,const char * path)363 check_elf_headers(const Elf_Ehdr *hdr, const char *path)
364 {
365 if (!IS_ELF(*hdr)) {
366 _rtld_error("%s: invalid file format", path);
367 return (false);
368 }
369 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
370 hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
371 _rtld_error("%s: unsupported file layout", path);
372 return (false);
373 }
374 if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
375 hdr->e_version != EV_CURRENT) {
376 _rtld_error("%s: unsupported file version", path);
377 return (false);
378 }
379 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
380 _rtld_error("%s: unsupported file type", path);
381 return (false);
382 }
383 if (hdr->e_machine != ELF_TARG_MACH) {
384 _rtld_error("%s: unsupported machine", path);
385 return (false);
386 }
387 if (hdr->e_phentsize != sizeof(Elf_Phdr)) {
388 _rtld_error(
389 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)",
390 path);
391 return (false);
392 }
393 return (true);
394 }
395
396 static Elf_Ehdr *
get_elf_header(int fd,const char * path,const struct stat * sbp,Elf_Phdr ** phdr_p)397 get_elf_header(int fd, const char *path, const struct stat *sbp,
398 Elf_Phdr **phdr_p)
399 {
400 Elf_Ehdr *hdr;
401 Elf_Phdr *phdr;
402
403 /* Make sure file has enough data for the ELF header */
404 if (sbp != NULL && sbp->st_size < (off_t)sizeof(Elf_Ehdr)) {
405 _rtld_error("%s: invalid file format", path);
406 return (NULL);
407 }
408
409 hdr = mmap(NULL, page_size, PROT_READ, MAP_PRIVATE | MAP_PREFAULT_READ,
410 fd, 0);
411 if (hdr == MAP_FAILED) {
412 _rtld_error("%s: read error: %s", path, rtld_strerror(errno));
413 return (NULL);
414 }
415
416 /* Make sure the file is valid */
417 if (!check_elf_headers(hdr, path))
418 goto error;
419
420 /*
421 * We rely on the program header being in the first page. This is
422 * not strictly required by the ABI specification, but it seems to
423 * always true in practice. And, it simplifies things considerably.
424 */
425 if (phdr_in_zero_page(hdr)) {
426 phdr = (Elf_Phdr *)((char *)hdr + hdr->e_phoff);
427 } else {
428 phdr = mmap(NULL, hdr->e_phnum * sizeof(phdr[0]), PROT_READ,
429 MAP_PRIVATE | MAP_PREFAULT_READ, fd, hdr->e_phoff);
430 if (phdr == MAP_FAILED) {
431 _rtld_error("%s: error mapping phdr: %s", path,
432 rtld_strerror(errno));
433 goto error;
434 }
435 }
436 *phdr_p = phdr;
437 return (hdr);
438
439 error:
440 munmap(hdr, page_size);
441 return (NULL);
442 }
443
444 void
obj_free(Obj_Entry * obj)445 obj_free(Obj_Entry *obj)
446 {
447 Objlist_Entry *elm;
448
449 if (obj->tls_static)
450 free_tls_offset(obj);
451 while (obj->needed != NULL) {
452 Needed_Entry *needed = obj->needed;
453
454 obj->needed = needed->next;
455 free(needed);
456 }
457 while (!STAILQ_EMPTY(&obj->names)) {
458 Name_Entry *entry = STAILQ_FIRST(&obj->names);
459
460 STAILQ_REMOVE_HEAD(&obj->names, link);
461 free(entry);
462 }
463 while (!STAILQ_EMPTY(&obj->dldags)) {
464 elm = STAILQ_FIRST(&obj->dldags);
465 STAILQ_REMOVE_HEAD(&obj->dldags, link);
466 free(elm);
467 }
468 while (!STAILQ_EMPTY(&obj->dagmembers)) {
469 elm = STAILQ_FIRST(&obj->dagmembers);
470 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
471 free(elm);
472 }
473 if (obj->vertab)
474 free(obj->vertab);
475 if (obj->origin_path)
476 free(obj->origin_path);
477 if (obj->z_origin)
478 free(__DECONST(void *, obj->rpath));
479 if (obj->priv)
480 free(obj->priv);
481 if (obj->path)
482 free(obj->path);
483 if (obj->phdr_alloc)
484 free(__DECONST(void *, obj->phdr));
485 free(obj);
486 }
487
488 Obj_Entry *
obj_new(void)489 obj_new(void)
490 {
491 Obj_Entry *obj;
492
493 obj = CNEW(Obj_Entry);
494 STAILQ_INIT(&obj->dldags);
495 STAILQ_INIT(&obj->dagmembers);
496 STAILQ_INIT(&obj->names);
497 return (obj);
498 }
499
500 /*
501 * Given a set of ELF protection flags, return the corresponding protection
502 * flags for MMAP.
503 */
504 int
convert_prot(int elfflags)505 convert_prot(int elfflags)
506 {
507 int prot = 0;
508
509 if ((elfflags & PF_R) != 0)
510 prot |= PROT_READ;
511 if ((elfflags & PF_W) != 0)
512 prot |= PROT_WRITE;
513 if ((elfflags & PF_X) != 0)
514 prot |= PROT_EXEC;
515 return (prot);
516 }
517
518 static int
convert_flags(int elfflags)519 convert_flags(int elfflags)
520 {
521 int flags = MAP_PRIVATE; /* All mappings are private */
522
523 /*
524 * Readonly mappings are marked "MAP_NOCORE", because they can be
525 * reconstructed by a debugger.
526 */
527 if ((elfflags & PF_W) == 0)
528 flags |= MAP_NOCORE;
529 return (flags);
530 }
531