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 Elf_Addr text_end;
98
99 hdr = get_elf_header(fd, path, sb, &phdr);
100 if (hdr == NULL)
101 return (NULL);
102
103 /*
104 * Scan the program header entries, and save key information.
105 * We expect that the loadable segments are ordered by load address.
106 */
107 phsize = hdr->e_phnum * sizeof(phdr[0]);
108 phlimit = phdr + hdr->e_phnum;
109 nsegs = -1;
110 phdyn = phinterp = phtls = NULL;
111 phdr_vaddr = 0;
112 note_start = 0;
113 note_end = 0;
114 note_map = NULL;
115 note_map_len = 0;
116 segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
117 stack_flags = PF_X | PF_R | PF_W;
118 text_end = 0;
119 while (phdr < phlimit) {
120 switch (phdr->p_type) {
121 case PT_INTERP:
122 phinterp = phdr;
123 break;
124
125 case PT_LOAD:
126 segs[++nsegs] = phdr;
127 if ((segs[nsegs]->p_align & (page_size - 1)) != 0) {
128 _rtld_error(
129 "%s: PT_LOAD segment %d not page-aligned",
130 path, nsegs);
131 goto error;
132 }
133 if ((segs[nsegs]->p_flags & PF_X) == PF_X) {
134 text_end = MAX(text_end,
135 rtld_round_page(segs[nsegs]->p_vaddr +
136 segs[nsegs]->p_memsz));
137 }
138 break;
139
140 case PT_PHDR:
141 phdr_vaddr = phdr->p_vaddr;
142 phsize = phdr->p_memsz;
143 break;
144
145 case PT_DYNAMIC:
146 phdyn = phdr;
147 break;
148
149 case PT_TLS:
150 phtls = phdr;
151 break;
152
153 case PT_GNU_STACK:
154 stack_flags = phdr->p_flags;
155 break;
156
157 case PT_NOTE:
158 if (phdr->p_offset > page_size ||
159 phdr->p_offset + phdr->p_filesz > page_size) {
160 note_map_len = rtld_round_page(phdr->p_offset +
161 phdr->p_filesz) -
162 rtld_trunc_page(phdr->p_offset);
163 note_map = mmap(NULL, note_map_len, PROT_READ,
164 MAP_PRIVATE, fd,
165 rtld_trunc_page(phdr->p_offset));
166 if (note_map == MAP_FAILED) {
167 _rtld_error(
168 "%s: error mapping PT_NOTE (%d)",
169 path, errno);
170 goto error;
171 }
172 note_start = (Elf_Addr)(note_map +
173 phdr->p_offset -
174 rtld_trunc_page(phdr->p_offset));
175 } else {
176 note_start = (Elf_Addr)(char *)hdr +
177 phdr->p_offset;
178 }
179 note_end = note_start + phdr->p_filesz;
180 break;
181 }
182
183 ++phdr;
184 }
185 if (phdyn == NULL) {
186 _rtld_error("%s: object is not dynamically-linked", path);
187 goto error;
188 }
189
190 if (nsegs < 0) {
191 _rtld_error("%s: too few PT_LOAD segments", path);
192 goto error;
193 }
194
195 /*
196 * Map the entire address space of the object, to stake out our
197 * contiguous region, and to establish the base address for relocation.
198 */
199 base_vaddr = rtld_trunc_page(segs[0]->p_vaddr);
200 base_vlimit = rtld_round_page(segs[nsegs]->p_vaddr +
201 segs[nsegs]->p_memsz);
202 mapsize = base_vlimit - base_vaddr;
203 base_addr = (caddr_t)base_vaddr;
204 base_flags = MAP_GUARD;
205 if (npagesizes > 1 && rtld_round_page(segs[0]->p_filesz) >=
206 pagesizes[1])
207 base_flags |= MAP_ALIGNED_SUPER;
208 if (base_vaddr != 0)
209 base_flags |= MAP_FIXED | MAP_EXCL;
210
211 mapbase = mmap(base_addr, mapsize, PROT_NONE, base_flags, -1, 0);
212 if (mapbase == MAP_FAILED) {
213 _rtld_error("%s: mmap of entire address space failed: %s",
214 path, rtld_strerror(errno));
215 goto error;
216 }
217 if (base_addr != NULL && mapbase != base_addr) {
218 _rtld_error(
219 "%s: mmap returned wrong address: wanted %p, got %p",
220 path, base_addr, mapbase);
221 goto error1;
222 }
223
224 for (i = 0; i <= nsegs; i++) {
225 /* Overlay the segment onto the proper region. */
226 data_offset = rtld_trunc_page(segs[i]->p_offset);
227 data_vaddr = rtld_trunc_page(segs[i]->p_vaddr);
228 data_vlimit = rtld_round_page(segs[i]->p_vaddr +
229 segs[i]->p_filesz);
230 data_addr = mapbase + (data_vaddr - base_vaddr);
231 data_prot = convert_prot(segs[i]->p_flags);
232 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
233 if (data_vlimit != data_vaddr && mmap(data_addr,
234 data_vlimit - data_vaddr, data_prot, data_flags |
235 MAP_PREFAULT_READ, fd, data_offset) == MAP_FAILED) {
236 _rtld_error("%s: mmap of data failed: %s",
237 path, rtld_strerror(errno));
238 goto error1;
239 }
240
241 /* Do BSS setup */
242 if (segs[i]->p_filesz != segs[i]->p_memsz) {
243 /* Clear any BSS in the last page of the segment. */
244 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
245 clear_addr = mapbase + (clear_vaddr - base_vaddr);
246 clear_page = mapbase + (rtld_trunc_page(clear_vaddr) -
247 base_vaddr);
248
249 if ((nclear = data_vlimit - clear_vaddr) > 0) {
250 /*
251 * Make sure the end of the segment is
252 * writable.
253 */
254 if ((data_prot & PROT_WRITE) == 0 &&
255 mprotect(clear_page, page_size,
256 data_prot | PROT_WRITE) == -1) {
257 _rtld_error("%s: mprotect failed: %s",
258 path, rtld_strerror(errno));
259 goto error1;
260 }
261
262 memset(clear_addr, 0, nclear);
263
264 /* Reset the data protection back */
265 if ((data_prot & PROT_WRITE) == 0)
266 mprotect(clear_page, page_size,
267 data_prot);
268 }
269
270 /* Overlay the BSS segment onto the proper region. */
271 bss_vaddr = data_vlimit;
272 bss_vlimit = rtld_round_page(segs[i]->p_vaddr +
273 segs[i]->p_memsz);
274 bss_addr = mapbase + (bss_vaddr - base_vaddr);
275 if (bss_vlimit > bss_vaddr) {
276 /* There is something to do */
277 if (mmap(bss_addr, bss_vlimit - bss_vaddr,
278 data_prot, data_flags | MAP_ANON, -1,
279 0) == MAP_FAILED) {
280 _rtld_error(
281 "%s: mmap of bss failed: %s",
282 path, rtld_strerror(errno));
283 goto error1;
284 }
285 }
286 }
287
288 if (phdr_vaddr == 0 && data_offset <= hdr->e_phoff &&
289 data_vlimit - data_vaddr + data_offset >=
290 hdr->e_phoff + hdr->e_phnum * sizeof(Elf_Phdr)) {
291 phdr_vaddr = data_vaddr + hdr->e_phoff - data_offset;
292 }
293 }
294
295 obj = obj_new();
296 if (sb != NULL) {
297 obj->dev = sb->st_dev;
298 obj->ino = sb->st_ino;
299 }
300 obj->mapbase = mapbase;
301 obj->mapsize = mapsize;
302 obj->vaddrbase = base_vaddr;
303 obj->relocbase = mapbase - base_vaddr;
304 obj->dynamic = (const Elf_Dyn *)(obj->relocbase + phdyn->p_vaddr);
305 if (hdr->e_entry != 0)
306 obj->entry = (caddr_t)(obj->relocbase + hdr->e_entry);
307 if (phdr_vaddr != 0) {
308 obj->phdr = (const Elf_Phdr *)(obj->relocbase + phdr_vaddr);
309 } else {
310 obj->phdr = malloc(phsize);
311 if (obj->phdr == NULL) {
312 obj_free(obj);
313 _rtld_error("%s: cannot allocate program header",
314 path);
315 goto error1;
316 }
317 memcpy(__DECONST(char *, obj->phdr), (char *)hdr + hdr->e_phoff,
318 phsize);
319 obj->phdr_alloc = true;
320 }
321 obj->phsize = phsize;
322 if (phinterp != NULL)
323 obj->interp = (const char *)(obj->relocbase +
324 phinterp->p_vaddr);
325 if (phtls != NULL) {
326 if (ismain)
327 obj->tlsindex = 1;
328 else {
329 tls_dtv_generation++;
330 obj->tlsindex = ++tls_max_index;
331 }
332 obj->tlssize = phtls->p_memsz;
333 obj->tlsalign = phtls->p_align;
334 obj->tlspoffset = phtls->p_offset;
335 obj->tlsinitsize = phtls->p_filesz;
336 obj->tlsinit = obj->relocbase + phtls->p_vaddr;
337 }
338 obj->stack_flags = stack_flags;
339 if (note_start < note_end)
340 digest_notes(obj, note_start, note_end);
341 if (note_map != NULL)
342 munmap(note_map, note_map_len);
343 munmap(hdr, page_size);
344 return (obj);
345
346 error1:
347 munmap(mapbase, mapsize);
348 error:
349 if (note_map != NULL && note_map != MAP_FAILED)
350 munmap(note_map, note_map_len);
351 if (!phdr_in_zero_page(hdr))
352 munmap(phdr, hdr->e_phnum * sizeof(phdr[0]));
353 munmap(hdr, page_size);
354 return (NULL);
355 }
356
357 bool
check_elf_headers(const Elf_Ehdr * hdr,const char * path)358 check_elf_headers(const Elf_Ehdr *hdr, const char *path)
359 {
360 if (!IS_ELF(*hdr)) {
361 _rtld_error("%s: invalid file format", path);
362 return (false);
363 }
364 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
365 hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
366 _rtld_error("%s: unsupported file layout", path);
367 return (false);
368 }
369 if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
370 hdr->e_version != EV_CURRENT) {
371 _rtld_error("%s: unsupported file version", path);
372 return (false);
373 }
374 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
375 _rtld_error("%s: unsupported file type", path);
376 return (false);
377 }
378 if (hdr->e_machine != ELF_TARG_MACH) {
379 _rtld_error("%s: unsupported machine", path);
380 return (false);
381 }
382 if (hdr->e_phentsize != sizeof(Elf_Phdr)) {
383 _rtld_error(
384 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)",
385 path);
386 return (false);
387 }
388 return (true);
389 }
390
391 static Elf_Ehdr *
get_elf_header(int fd,const char * path,const struct stat * sbp,Elf_Phdr ** phdr_p)392 get_elf_header(int fd, const char *path, const struct stat *sbp,
393 Elf_Phdr **phdr_p)
394 {
395 Elf_Ehdr *hdr;
396 Elf_Phdr *phdr;
397
398 /* Make sure file has enough data for the ELF header */
399 if (sbp != NULL && sbp->st_size < (off_t)sizeof(Elf_Ehdr)) {
400 _rtld_error("%s: invalid file format", path);
401 return (NULL);
402 }
403
404 hdr = mmap(NULL, page_size, PROT_READ, MAP_PRIVATE | MAP_PREFAULT_READ,
405 fd, 0);
406 if (hdr == MAP_FAILED) {
407 _rtld_error("%s: read error: %s", path, rtld_strerror(errno));
408 return (NULL);
409 }
410
411 /* Make sure the file is valid */
412 if (!check_elf_headers(hdr, path))
413 goto error;
414
415 /*
416 * We rely on the program header being in the first page. This is
417 * not strictly required by the ABI specification, but it seems to
418 * always true in practice. And, it simplifies things considerably.
419 */
420 if (phdr_in_zero_page(hdr)) {
421 phdr = (Elf_Phdr *)((char *)hdr + hdr->e_phoff);
422 } else {
423 phdr = mmap(NULL, hdr->e_phnum * sizeof(phdr[0]), PROT_READ,
424 MAP_PRIVATE | MAP_PREFAULT_READ, fd, hdr->e_phoff);
425 if (phdr == MAP_FAILED) {
426 _rtld_error("%s: error mapping phdr: %s", path,
427 rtld_strerror(errno));
428 goto error;
429 }
430 }
431 *phdr_p = phdr;
432 return (hdr);
433
434 error:
435 munmap(hdr, page_size);
436 return (NULL);
437 }
438
439 void
obj_free(Obj_Entry * obj)440 obj_free(Obj_Entry *obj)
441 {
442 Objlist_Entry *elm;
443
444 if (obj->tls_static)
445 free_tls_offset(obj);
446 while (obj->needed != NULL) {
447 Needed_Entry *needed = obj->needed;
448
449 obj->needed = needed->next;
450 free(needed);
451 }
452 while (!STAILQ_EMPTY(&obj->names)) {
453 Name_Entry *entry = STAILQ_FIRST(&obj->names);
454
455 STAILQ_REMOVE_HEAD(&obj->names, link);
456 free(entry);
457 }
458 while (!STAILQ_EMPTY(&obj->dldags)) {
459 elm = STAILQ_FIRST(&obj->dldags);
460 STAILQ_REMOVE_HEAD(&obj->dldags, link);
461 free(elm);
462 }
463 while (!STAILQ_EMPTY(&obj->dagmembers)) {
464 elm = STAILQ_FIRST(&obj->dagmembers);
465 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
466 free(elm);
467 }
468 if (obj->vertab)
469 free(obj->vertab);
470 if (obj->origin_path)
471 free(obj->origin_path);
472 if (obj->z_origin)
473 free(__DECONST(void *, obj->rpath));
474 if (obj->priv)
475 free(obj->priv);
476 if (obj->path)
477 free(obj->path);
478 if (obj->phdr_alloc)
479 free(__DECONST(void *, obj->phdr));
480 free(obj);
481 }
482
483 Obj_Entry *
obj_new(void)484 obj_new(void)
485 {
486 Obj_Entry *obj;
487
488 obj = CNEW(Obj_Entry);
489 STAILQ_INIT(&obj->dldags);
490 STAILQ_INIT(&obj->dagmembers);
491 STAILQ_INIT(&obj->names);
492 return (obj);
493 }
494
495 /*
496 * Given a set of ELF protection flags, return the corresponding protection
497 * flags for MMAP.
498 */
499 int
convert_prot(int elfflags)500 convert_prot(int elfflags)
501 {
502 int prot = 0;
503
504 if ((elfflags & PF_R) != 0)
505 prot |= PROT_READ;
506 if ((elfflags & PF_W) != 0)
507 prot |= PROT_WRITE;
508 if ((elfflags & PF_X) != 0)
509 prot |= PROT_EXEC;
510 return (prot);
511 }
512
513 static int
convert_flags(int elfflags)514 convert_flags(int elfflags)
515 {
516 int flags = MAP_PRIVATE; /* All mappings are private */
517
518 /*
519 * Readonly mappings are marked "MAP_NOCORE", because they can be
520 * reconstructed by a debugger.
521 */
522 if ((elfflags & PF_W) == 0)
523 flags |= MAP_NOCORE;
524 return (flags);
525 }
526