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