1 /*- 2 * Copyright (c) 2000 David O'Brien 3 * Copyright (c) 1995-1996 S�ren Schmidt 4 * Copyright (c) 1996 Peter Wemm 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 * in this position and unchanged. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * 30 * $FreeBSD$ 31 */ 32 33 #include <sys/param.h> 34 #include <sys/exec.h> 35 #include <sys/fcntl.h> 36 #include <sys/imgact.h> 37 #include <sys/imgact_elf.h> 38 #include <sys/kernel.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/mutex.h> 42 #include <sys/mman.h> 43 #include <sys/namei.h> 44 #include <sys/pioctl.h> 45 #include <sys/proc.h> 46 #include <sys/procfs.h> 47 #include <sys/resourcevar.h> 48 #include <sys/systm.h> 49 #include <sys/signalvar.h> 50 #include <sys/stat.h> 51 #include <sys/sx.h> 52 #include <sys/syscall.h> 53 #include <sys/sysctl.h> 54 #include <sys/sysent.h> 55 #include <sys/vnode.h> 56 57 #include <vm/vm.h> 58 #include <vm/vm_kern.h> 59 #include <vm/vm_param.h> 60 #include <vm/pmap.h> 61 #include <vm/vm_map.h> 62 #include <vm/vm_object.h> 63 #include <vm/vm_extern.h> 64 65 #include <machine/elf.h> 66 #include <machine/md_var.h> 67 68 #define OLD_EI_BRAND 8 69 70 static int __elfN(check_header)(const Elf_Ehdr *hdr); 71 static Elf_Brandinfo *__elfN(get_brandinfo)(const Elf_Ehdr *hdr, 72 const char *interp); 73 static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr, 74 u_long *entry, size_t pagesize); 75 static int __elfN(load_section)(struct proc *p, 76 struct vmspace *vmspace, struct vnode *vp, vm_object_t object, 77 vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, 78 vm_prot_t prot, size_t pagesize); 79 static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp); 80 81 SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0, 82 ""); 83 84 int __elfN(fallback_brand) = -1; 85 SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, 86 fallback_brand, CTLFLAG_RW, &__elfN(fallback_brand), 0, 87 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort"); 88 TUNABLE_INT("kern.elf" __XSTRING(__ELF_WORD_SIZE) ".fallback_brand", 89 &__elfN(fallback_brand)); 90 91 static int elf_trace = 0; 92 SYSCTL_INT(_debug, OID_AUTO, __elfN(trace), CTLFLAG_RW, &elf_trace, 0, ""); 93 94 static int elf_legacy_coredump = 0; 95 SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW, 96 &elf_legacy_coredump, 0, ""); 97 98 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS]; 99 100 int 101 __elfN(insert_brand_entry)(Elf_Brandinfo *entry) 102 { 103 int i; 104 105 for (i = 0; i < MAX_BRANDS; i++) { 106 if (elf_brand_list[i] == NULL) { 107 elf_brand_list[i] = entry; 108 break; 109 } 110 } 111 if (i == MAX_BRANDS) 112 return (-1); 113 return (0); 114 } 115 116 int 117 __elfN(remove_brand_entry)(Elf_Brandinfo *entry) 118 { 119 int i; 120 121 for (i = 0; i < MAX_BRANDS; i++) { 122 if (elf_brand_list[i] == entry) { 123 elf_brand_list[i] = NULL; 124 break; 125 } 126 } 127 if (i == MAX_BRANDS) 128 return (-1); 129 return (0); 130 } 131 132 int 133 __elfN(brand_inuse)(Elf_Brandinfo *entry) 134 { 135 struct proc *p; 136 int rval = FALSE; 137 138 sx_slock(&allproc_lock); 139 LIST_FOREACH(p, &allproc, p_list) { 140 if (p->p_sysent == entry->sysvec) { 141 rval = TRUE; 142 break; 143 } 144 } 145 sx_sunlock(&allproc_lock); 146 147 return (rval); 148 } 149 150 static Elf_Brandinfo * 151 __elfN(get_brandinfo)(const Elf_Ehdr *hdr, const char *interp) 152 { 153 Elf_Brandinfo *bi; 154 int i; 155 156 /* 157 * We support three types of branding -- (1) the ELF EI_OSABI field 158 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string 159 * branding w/in the ELF header, and (3) path of the `interp_path' 160 * field. We should also look for an ".note.ABI-tag" ELF section now 161 * in all Linux ELF binaries, FreeBSD 4.1+, and some NetBSD ones. 162 */ 163 164 /* If the executable has a brand, search for it in the brand list. */ 165 for (i = 0; i < MAX_BRANDS; i++) { 166 bi = elf_brand_list[i]; 167 if (bi != NULL && hdr->e_machine == bi->machine && 168 (hdr->e_ident[EI_OSABI] == bi->brand || 169 strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND], 170 bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0)) 171 return (bi); 172 } 173 174 /* Lacking a known brand, search for a recognized interpreter. */ 175 if (interp != NULL) { 176 for (i = 0; i < MAX_BRANDS; i++) { 177 bi = elf_brand_list[i]; 178 if (bi != NULL && hdr->e_machine == bi->machine && 179 strcmp(interp, bi->interp_path) == 0) 180 return (bi); 181 } 182 } 183 184 /* Lacking a recognized interpreter, try the default brand */ 185 for (i = 0; i < MAX_BRANDS; i++) { 186 bi = elf_brand_list[i]; 187 if (bi != NULL && hdr->e_machine == bi->machine && 188 __elfN(fallback_brand) == bi->brand) 189 return (bi); 190 } 191 return (NULL); 192 } 193 194 static int 195 __elfN(check_header)(const Elf_Ehdr *hdr) 196 { 197 Elf_Brandinfo *bi; 198 int i; 199 200 if (!IS_ELF(*hdr) || 201 hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 202 hdr->e_ident[EI_DATA] != ELF_TARG_DATA || 203 hdr->e_ident[EI_VERSION] != EV_CURRENT) 204 return (ENOEXEC); 205 206 /* 207 * Make sure we have at least one brand for this machine. 208 */ 209 210 for (i = 0; i < MAX_BRANDS; i++) { 211 bi = elf_brand_list[i]; 212 if (bi != NULL && bi->machine == hdr->e_machine) 213 break; 214 } 215 if (i == MAX_BRANDS) 216 return (ENOEXEC); 217 218 if (hdr->e_version != ELF_TARG_VER) 219 return (ENOEXEC); 220 221 return (0); 222 } 223 224 static int 225 __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 226 vm_offset_t start, vm_offset_t end, vm_prot_t prot, 227 vm_prot_t max) 228 { 229 int error, rv; 230 vm_offset_t off; 231 vm_offset_t data_buf = 0; 232 233 /* 234 * Create the page if it doesn't exist yet. Ignore errors. 235 */ 236 vm_map_lock(map); 237 vm_map_insert(map, NULL, 0, trunc_page(start), round_page(end), max, 238 max, 0); 239 vm_map_unlock(map); 240 241 /* 242 * Find the page from the underlying object. 243 */ 244 if (object) { 245 vm_object_reference(object); 246 rv = vm_map_find(exec_map, 247 object, 248 trunc_page(offset), 249 &data_buf, 250 PAGE_SIZE, 251 TRUE, 252 VM_PROT_READ, 253 VM_PROT_ALL, 254 MAP_COPY_ON_WRITE | MAP_PREFAULT_PARTIAL); 255 if (rv != KERN_SUCCESS) { 256 vm_object_deallocate(object); 257 return (rv); 258 } 259 260 off = offset - trunc_page(offset); 261 error = copyout((caddr_t)data_buf + off, (caddr_t)start, 262 end - start); 263 vm_map_remove(exec_map, data_buf, data_buf + PAGE_SIZE); 264 if (error) { 265 return (KERN_FAILURE); 266 } 267 } 268 269 return (KERN_SUCCESS); 270 } 271 272 static int 273 __elfN(map_insert)(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 274 vm_offset_t start, vm_offset_t end, vm_prot_t prot, 275 vm_prot_t max, int cow) 276 { 277 vm_offset_t data_buf, off; 278 vm_size_t sz; 279 int error, rv; 280 281 if (start != trunc_page(start)) { 282 rv = __elfN(map_partial)(map, object, offset, start, 283 round_page(start), prot, max); 284 if (rv) 285 return (rv); 286 offset += round_page(start) - start; 287 start = round_page(start); 288 } 289 if (end != round_page(end)) { 290 rv = __elfN(map_partial)(map, object, offset + 291 trunc_page(end) - start, trunc_page(end), end, prot, max); 292 if (rv) 293 return (rv); 294 end = trunc_page(end); 295 } 296 if (end > start) { 297 if (offset & PAGE_MASK) { 298 /* 299 * The mapping is not page aligned. This means we have 300 * to copy the data. Sigh. 301 */ 302 rv = vm_map_find(map, 0, 0, &start, end - start, 303 FALSE, prot, max, 0); 304 if (rv) 305 return (rv); 306 data_buf = 0; 307 while (start < end) { 308 vm_object_reference(object); 309 rv = vm_map_find(exec_map, 310 object, 311 trunc_page(offset), 312 &data_buf, 313 2 * PAGE_SIZE, 314 TRUE, 315 VM_PROT_READ, 316 VM_PROT_ALL, 317 (MAP_COPY_ON_WRITE 318 | MAP_PREFAULT_PARTIAL)); 319 if (rv != KERN_SUCCESS) { 320 vm_object_deallocate(object); 321 return (rv); 322 } 323 off = offset - trunc_page(offset); 324 sz = end - start; 325 if (sz > PAGE_SIZE) 326 sz = PAGE_SIZE; 327 error = copyout((caddr_t)data_buf + off, 328 (caddr_t)start, sz); 329 vm_map_remove(exec_map, data_buf, 330 data_buf + 2 * PAGE_SIZE); 331 if (error) { 332 return (KERN_FAILURE); 333 } 334 start += sz; 335 } 336 rv = KERN_SUCCESS; 337 } else { 338 vm_map_lock(map); 339 rv = vm_map_insert(map, object, offset, start, end, 340 prot, max, cow); 341 vm_map_unlock(map); 342 } 343 return (rv); 344 } else { 345 return (KERN_SUCCESS); 346 } 347 } 348 349 static int 350 __elfN(load_section)(struct proc *p, struct vmspace *vmspace, 351 struct vnode *vp, vm_object_t object, vm_offset_t offset, 352 caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot, 353 size_t pagesize) 354 { 355 size_t map_len; 356 vm_offset_t map_addr; 357 int error, rv, cow; 358 size_t copy_len; 359 vm_offset_t file_addr; 360 vm_offset_t data_buf = 0; 361 362 GIANT_REQUIRED; 363 364 error = 0; 365 366 /* 367 * It's necessary to fail if the filsz + offset taken from the 368 * header is greater than the actual file pager object's size. 369 * If we were to allow this, then the vm_map_find() below would 370 * walk right off the end of the file object and into the ether. 371 * 372 * While I'm here, might as well check for something else that 373 * is invalid: filsz cannot be greater than memsz. 374 */ 375 if ((off_t)filsz + offset > object->un_pager.vnp.vnp_size || 376 filsz > memsz) { 377 uprintf("elf_load_section: truncated ELF file\n"); 378 return (ENOEXEC); 379 } 380 381 #define trunc_page_ps(va, ps) ((va) & ~(ps - 1)) 382 #define round_page_ps(va, ps) (((va) + (ps - 1)) & ~(ps - 1)) 383 384 map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize); 385 file_addr = trunc_page_ps(offset, pagesize); 386 387 /* 388 * We have two choices. We can either clear the data in the last page 389 * of an oversized mapping, or we can start the anon mapping a page 390 * early and copy the initialized data into that first page. We 391 * choose the second.. 392 */ 393 if (memsz > filsz) 394 map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr; 395 else 396 map_len = round_page_ps(offset + filsz, pagesize) - file_addr; 397 398 if (map_len != 0) { 399 vm_object_reference(object); 400 401 /* cow flags: don't dump readonly sections in core */ 402 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT | 403 (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP); 404 405 rv = __elfN(map_insert)(&vmspace->vm_map, 406 object, 407 file_addr, /* file offset */ 408 map_addr, /* virtual start */ 409 map_addr + map_len,/* virtual end */ 410 prot, 411 VM_PROT_ALL, 412 cow); 413 if (rv != KERN_SUCCESS) { 414 vm_object_deallocate(object); 415 return (EINVAL); 416 } 417 418 /* we can stop now if we've covered it all */ 419 if (memsz == filsz) { 420 return (0); 421 } 422 } 423 424 425 /* 426 * We have to get the remaining bit of the file into the first part 427 * of the oversized map segment. This is normally because the .data 428 * segment in the file is extended to provide bss. It's a neat idea 429 * to try and save a page, but it's a pain in the behind to implement. 430 */ 431 copy_len = (offset + filsz) - trunc_page_ps(offset + filsz, pagesize); 432 map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize); 433 map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) - 434 map_addr; 435 436 /* This had damn well better be true! */ 437 if (map_len != 0) { 438 rv = __elfN(map_insert)(&vmspace->vm_map, NULL, 0, map_addr, 439 map_addr + map_len, VM_PROT_ALL, VM_PROT_ALL, 0); 440 if (rv != KERN_SUCCESS) { 441 return (EINVAL); 442 } 443 } 444 445 if (copy_len != 0) { 446 vm_offset_t off; 447 vm_object_reference(object); 448 rv = vm_map_find(exec_map, 449 object, 450 trunc_page(offset + filsz), 451 &data_buf, 452 PAGE_SIZE, 453 TRUE, 454 VM_PROT_READ, 455 VM_PROT_ALL, 456 MAP_COPY_ON_WRITE | MAP_PREFAULT_PARTIAL); 457 if (rv != KERN_SUCCESS) { 458 vm_object_deallocate(object); 459 return (EINVAL); 460 } 461 462 /* send the page fragment to user space */ 463 off = trunc_page_ps(offset + filsz, pagesize) - 464 trunc_page(offset + filsz); 465 error = copyout((caddr_t)data_buf + off, (caddr_t)map_addr, 466 copy_len); 467 vm_map_remove(exec_map, data_buf, data_buf + PAGE_SIZE); 468 if (error) { 469 return (error); 470 } 471 } 472 473 /* 474 * set it to the specified protection. 475 * XXX had better undo the damage from pasting over the cracks here! 476 */ 477 vm_map_protect(&vmspace->vm_map, trunc_page(map_addr), 478 round_page(map_addr + map_len), prot, FALSE); 479 480 return (error); 481 } 482 483 /* 484 * Load the file "file" into memory. It may be either a shared object 485 * or an executable. 486 * 487 * The "addr" reference parameter is in/out. On entry, it specifies 488 * the address where a shared object should be loaded. If the file is 489 * an executable, this value is ignored. On exit, "addr" specifies 490 * where the file was actually loaded. 491 * 492 * The "entry" reference parameter is out only. On exit, it specifies 493 * the entry point for the loaded file. 494 */ 495 static int 496 __elfN(load_file)(struct proc *p, const char *file, u_long *addr, 497 u_long *entry, size_t pagesize) 498 { 499 struct { 500 struct nameidata nd; 501 struct vattr attr; 502 struct image_params image_params; 503 } *tempdata; 504 const Elf_Ehdr *hdr = NULL; 505 const Elf_Phdr *phdr = NULL; 506 struct nameidata *nd; 507 struct vmspace *vmspace = p->p_vmspace; 508 struct vattr *attr; 509 struct image_params *imgp; 510 vm_prot_t prot; 511 u_long rbase; 512 u_long base_addr = 0; 513 int error, i, numsegs; 514 515 if (curthread->td_proc != p) 516 panic("elf_load_file - thread"); /* XXXKSE DIAGNOSTIC */ 517 518 tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK); 519 nd = &tempdata->nd; 520 attr = &tempdata->attr; 521 imgp = &tempdata->image_params; 522 523 /* 524 * Initialize part of the common data 525 */ 526 imgp->proc = p; 527 imgp->userspace_argv = NULL; 528 imgp->userspace_envv = NULL; 529 imgp->attr = attr; 530 imgp->firstpage = NULL; 531 imgp->image_header = (char *)kmem_alloc_wait(exec_map, PAGE_SIZE); 532 imgp->object = NULL; 533 imgp->execlabel = NULL; 534 535 if (imgp->image_header == NULL) { 536 nd->ni_vp = NULL; 537 error = ENOMEM; 538 goto fail; 539 } 540 541 /* XXXKSE */ 542 NDINIT(nd, LOOKUP, LOCKLEAF|FOLLOW, UIO_SYSSPACE, file, curthread); 543 544 if ((error = namei(nd)) != 0) { 545 nd->ni_vp = NULL; 546 goto fail; 547 } 548 NDFREE(nd, NDF_ONLY_PNBUF); 549 imgp->vp = nd->ni_vp; 550 551 /* 552 * Check permissions, modes, uid, etc on the file, and "open" it. 553 */ 554 error = exec_check_permissions(imgp); 555 if (error) { 556 VOP_UNLOCK(nd->ni_vp, 0, curthread); /* XXXKSE */ 557 goto fail; 558 } 559 560 error = exec_map_first_page(imgp); 561 /* 562 * Also make certain that the interpreter stays the same, so set 563 * its VV_TEXT flag, too. 564 */ 565 if (error == 0) 566 nd->ni_vp->v_vflag |= VV_TEXT; 567 568 VOP_GETVOBJECT(nd->ni_vp, &imgp->object); 569 vm_object_reference(imgp->object); 570 571 VOP_UNLOCK(nd->ni_vp, 0, curthread); /* XXXKSE */ 572 if (error) 573 goto fail; 574 575 hdr = (const Elf_Ehdr *)imgp->image_header; 576 if ((error = __elfN(check_header)(hdr)) != 0) 577 goto fail; 578 if (hdr->e_type == ET_DYN) 579 rbase = *addr; 580 else if (hdr->e_type == ET_EXEC) 581 rbase = 0; 582 else { 583 error = ENOEXEC; 584 goto fail; 585 } 586 587 /* Only support headers that fit within first page for now */ 588 if ((hdr->e_phoff > PAGE_SIZE) || 589 (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) { 590 error = ENOEXEC; 591 goto fail; 592 } 593 594 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 595 596 for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) { 597 if (phdr[i].p_type == PT_LOAD) { /* Loadable segment */ 598 prot = 0; 599 if (phdr[i].p_flags & PF_X) 600 prot |= VM_PROT_EXECUTE; 601 if (phdr[i].p_flags & PF_W) 602 prot |= VM_PROT_WRITE; 603 if (phdr[i].p_flags & PF_R) 604 prot |= VM_PROT_READ; 605 606 if ((error = __elfN(load_section)(p, vmspace, 607 nd->ni_vp, imgp->object, phdr[i].p_offset, 608 (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase, 609 phdr[i].p_memsz, phdr[i].p_filesz, prot, 610 pagesize)) != 0) 611 goto fail; 612 /* 613 * Establish the base address if this is the 614 * first segment. 615 */ 616 if (numsegs == 0) 617 base_addr = trunc_page(phdr[i].p_vaddr + 618 rbase); 619 numsegs++; 620 } 621 } 622 *addr = base_addr; 623 *entry = (unsigned long)hdr->e_entry + rbase; 624 625 fail: 626 if (imgp->firstpage) 627 exec_unmap_first_page(imgp); 628 if (imgp->image_header) 629 kmem_free_wakeup(exec_map, (vm_offset_t)imgp->image_header, 630 PAGE_SIZE); 631 if (imgp->object) 632 vm_object_deallocate(imgp->object); 633 634 if (nd->ni_vp) 635 vrele(nd->ni_vp); 636 637 free(tempdata, M_TEMP); 638 639 return (error); 640 } 641 642 static int 643 __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp) 644 { 645 const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header; 646 const Elf_Phdr *phdr; 647 Elf_Auxargs *elf_auxargs = NULL; 648 struct vmspace *vmspace; 649 vm_prot_t prot; 650 u_long text_size = 0, data_size = 0, total_size = 0; 651 u_long text_addr = 0, data_addr = 0; 652 u_long seg_size, seg_addr; 653 u_long addr, entry = 0, proghdr = 0; 654 int error, i; 655 const char *interp = NULL; 656 Elf_Brandinfo *brand_info; 657 char *path; 658 struct thread *td = curthread; 659 struct sysentvec *sv; 660 661 GIANT_REQUIRED; 662 663 /* 664 * Do we have a valid ELF header ? 665 */ 666 if (__elfN(check_header)(hdr) != 0 || hdr->e_type != ET_EXEC) 667 return (-1); 668 669 /* 670 * From here on down, we return an errno, not -1, as we've 671 * detected an ELF file. 672 */ 673 674 if ((hdr->e_phoff > PAGE_SIZE) || 675 (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) { 676 /* Only support headers in first page for now */ 677 return (ENOEXEC); 678 } 679 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 680 681 /* 682 * From this point on, we may have resources that need to be freed. 683 */ 684 685 VOP_UNLOCK(imgp->vp, 0, td); 686 687 for (i = 0; i < hdr->e_phnum; i++) { 688 switch (phdr[i].p_type) { 689 case PT_INTERP: /* Path to interpreter */ 690 if (phdr[i].p_filesz > MAXPATHLEN || 691 phdr[i].p_offset + phdr[i].p_filesz > PAGE_SIZE) { 692 error = ENOEXEC; 693 goto fail; 694 } 695 interp = imgp->image_header + phdr[i].p_offset; 696 break; 697 default: 698 break; 699 } 700 } 701 702 brand_info = __elfN(get_brandinfo)(hdr, interp); 703 if (brand_info == NULL) { 704 uprintf("ELF binary type \"%u\" not known.\n", 705 hdr->e_ident[EI_OSABI]); 706 error = ENOEXEC; 707 goto fail; 708 } 709 sv = brand_info->sysvec; 710 711 if ((error = exec_extract_strings(imgp)) != 0) 712 goto fail; 713 714 exec_new_vmspace(imgp, sv); 715 716 vmspace = imgp->proc->p_vmspace; 717 718 for (i = 0; i < hdr->e_phnum; i++) { 719 switch (phdr[i].p_type) { 720 case PT_LOAD: /* Loadable segment */ 721 prot = 0; 722 if (phdr[i].p_flags & PF_X) 723 prot |= VM_PROT_EXECUTE; 724 if (phdr[i].p_flags & PF_W) 725 prot |= VM_PROT_WRITE; 726 if (phdr[i].p_flags & PF_R) 727 prot |= VM_PROT_READ; 728 729 #if defined(__ia64__) && __ELF_WORD_SIZE == 32 && defined(IA32_ME_HARDER) 730 /* 731 * Some x86 binaries assume read == executable, 732 * notably the M3 runtime and therefore cvsup 733 */ 734 if (prot & VM_PROT_READ) 735 prot |= VM_PROT_EXECUTE; 736 #endif 737 738 if ((error = __elfN(load_section)(imgp->proc, vmspace, 739 imgp->vp, imgp->object, phdr[i].p_offset, 740 (caddr_t)(uintptr_t)phdr[i].p_vaddr, 741 phdr[i].p_memsz, phdr[i].p_filesz, prot, 742 sv->sv_pagesize)) != 0) 743 goto fail; 744 745 seg_addr = trunc_page(phdr[i].p_vaddr); 746 seg_size = round_page(phdr[i].p_memsz + 747 phdr[i].p_vaddr - seg_addr); 748 749 /* 750 * Is this .text or .data? We can't use 751 * VM_PROT_WRITE or VM_PROT_EXEC, it breaks the 752 * alpha terribly and possibly does other bad 753 * things so we stick to the old way of figuring 754 * it out: If the segment contains the program 755 * entry point, it's a text segment, otherwise it 756 * is a data segment. 757 * 758 * Note that obreak() assumes that data_addr + 759 * data_size == end of data load area, and the ELF 760 * file format expects segments to be sorted by 761 * address. If multiple data segments exist, the 762 * last one will be used. 763 */ 764 if (hdr->e_entry >= phdr[i].p_vaddr && 765 hdr->e_entry < (phdr[i].p_vaddr + 766 phdr[i].p_memsz)) { 767 text_size = seg_size; 768 text_addr = seg_addr; 769 entry = (u_long)hdr->e_entry; 770 } else { 771 data_size = seg_size; 772 data_addr = seg_addr; 773 } 774 total_size += seg_size; 775 break; 776 case PT_PHDR: /* Program header table info */ 777 proghdr = phdr[i].p_vaddr; 778 break; 779 default: 780 break; 781 } 782 } 783 784 if (data_addr == 0 && data_size == 0) { 785 data_addr = text_addr; 786 data_size = text_size; 787 } 788 789 /* 790 * Check limits. It should be safe to check the 791 * limits after loading the segments since we do 792 * not actually fault in all the segments pages. 793 */ 794 if (data_size > 795 imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur || 796 text_size > maxtsiz || 797 total_size > 798 imgp->proc->p_rlimit[RLIMIT_VMEM].rlim_cur) { 799 error = ENOMEM; 800 goto fail; 801 } 802 803 vmspace->vm_tsize = text_size >> PAGE_SHIFT; 804 vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr; 805 vmspace->vm_dsize = data_size >> PAGE_SHIFT; 806 vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr; 807 808 addr = ELF_RTLD_ADDR(vmspace); 809 810 imgp->entry_addr = entry; 811 812 imgp->proc->p_sysent = sv; 813 if (interp != NULL) { 814 path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 815 snprintf(path, MAXPATHLEN, "%s%s", brand_info->emul_path, 816 interp); 817 if ((error = __elfN(load_file)(imgp->proc, path, &addr, 818 &imgp->entry_addr, sv->sv_pagesize)) != 0) { 819 if ((error = __elfN(load_file)(imgp->proc, interp, 820 &addr, &imgp->entry_addr, sv->sv_pagesize)) != 0) { 821 uprintf("ELF interpreter %s not found\n", 822 path); 823 free(path, M_TEMP); 824 goto fail; 825 } 826 } 827 free(path, M_TEMP); 828 } 829 830 /* 831 * Construct auxargs table (used by the fixup routine) 832 */ 833 elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK); 834 elf_auxargs->execfd = -1; 835 elf_auxargs->phdr = proghdr; 836 elf_auxargs->phent = hdr->e_phentsize; 837 elf_auxargs->phnum = hdr->e_phnum; 838 elf_auxargs->pagesz = PAGE_SIZE; 839 elf_auxargs->base = addr; 840 elf_auxargs->flags = 0; 841 elf_auxargs->entry = entry; 842 elf_auxargs->trace = elf_trace; 843 844 imgp->auxargs = elf_auxargs; 845 imgp->interpreted = 0; 846 847 fail: 848 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td); 849 return (error); 850 } 851 852 #define suword __CONCAT(suword, __ELF_WORD_SIZE) 853 854 int 855 __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp) 856 { 857 Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs; 858 Elf_Addr *base; 859 Elf_Addr *pos; 860 861 base = (Elf_Addr *)*stack_base; 862 pos = base + (imgp->argc + imgp->envc + 2); 863 864 if (args->trace) { 865 AUXARGS_ENTRY(pos, AT_DEBUG, 1); 866 } 867 if (args->execfd != -1) { 868 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 869 } 870 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 871 AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 872 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 873 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 874 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 875 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 876 AUXARGS_ENTRY(pos, AT_BASE, args->base); 877 AUXARGS_ENTRY(pos, AT_NULL, 0); 878 879 free(imgp->auxargs, M_TEMP); 880 imgp->auxargs = NULL; 881 882 base--; 883 suword(base, (long)imgp->argc); 884 *stack_base = (register_t *)base; 885 return (0); 886 } 887 888 /* 889 * Code for generating ELF core dumps. 890 */ 891 892 typedef void (*segment_callback)(vm_map_entry_t, void *); 893 894 /* Closure for cb_put_phdr(). */ 895 struct phdr_closure { 896 Elf_Phdr *phdr; /* Program header to fill in */ 897 Elf_Off offset; /* Offset of segment in core file */ 898 }; 899 900 /* Closure for cb_size_segment(). */ 901 struct sseg_closure { 902 int count; /* Count of writable segments. */ 903 size_t size; /* Total size of all writable segments. */ 904 }; 905 906 static void cb_put_phdr(vm_map_entry_t, void *); 907 static void cb_size_segment(vm_map_entry_t, void *); 908 static void each_writable_segment(struct proc *, segment_callback, void *); 909 static int __elfN(corehdr)(struct thread *, struct vnode *, struct ucred *, 910 int, void *, size_t); 911 static void __elfN(puthdr)(struct proc *, void *, size_t *, 912 const prstatus_t *, const prfpregset_t *, const prpsinfo_t *, int); 913 static void __elfN(putnote)(void *, size_t *, const char *, int, 914 const void *, size_t); 915 916 extern int osreldate; 917 918 int 919 __elfN(coredump)(td, vp, limit) 920 struct thread *td; 921 register struct vnode *vp; 922 off_t limit; 923 { 924 register struct proc *p = td->td_proc; 925 register struct ucred *cred = td->td_ucred; 926 int error = 0; 927 struct sseg_closure seginfo; 928 void *hdr; 929 size_t hdrsize; 930 931 /* Size the program segments. */ 932 seginfo.count = 0; 933 seginfo.size = 0; 934 each_writable_segment(p, cb_size_segment, &seginfo); 935 936 /* 937 * Calculate the size of the core file header area by making 938 * a dry run of generating it. Nothing is written, but the 939 * size is calculated. 940 */ 941 hdrsize = 0; 942 __elfN(puthdr)((struct proc *)NULL, (void *)NULL, &hdrsize, 943 (const prstatus_t *)NULL, (const prfpregset_t *)NULL, 944 (const prpsinfo_t *)NULL, seginfo.count); 945 946 if (hdrsize + seginfo.size >= limit) 947 return (EFAULT); 948 949 /* 950 * Allocate memory for building the header, fill it up, 951 * and write it out. 952 */ 953 hdr = malloc(hdrsize, M_TEMP, M_WAITOK); 954 if (hdr == NULL) { 955 return (EINVAL); 956 } 957 error = __elfN(corehdr)(td, vp, cred, seginfo.count, hdr, hdrsize); 958 959 /* Write the contents of all of the writable segments. */ 960 if (error == 0) { 961 Elf_Phdr *php; 962 off_t offset; 963 int i; 964 965 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1; 966 offset = hdrsize; 967 for (i = 0; i < seginfo.count; i++) { 968 error = vn_rdwr_inchunks(UIO_WRITE, vp, 969 (caddr_t)(uintptr_t)php->p_vaddr, 970 php->p_filesz, offset, UIO_USERSPACE, 971 IO_UNIT | IO_DIRECT, cred, NOCRED, (int *)NULL, 972 curthread); /* XXXKSE */ 973 if (error != 0) 974 break; 975 offset += php->p_filesz; 976 php++; 977 } 978 } 979 free(hdr, M_TEMP); 980 981 return (error); 982 } 983 984 /* 985 * A callback for each_writable_segment() to write out the segment's 986 * program header entry. 987 */ 988 static void 989 cb_put_phdr(entry, closure) 990 vm_map_entry_t entry; 991 void *closure; 992 { 993 struct phdr_closure *phc = (struct phdr_closure *)closure; 994 Elf_Phdr *phdr = phc->phdr; 995 996 phc->offset = round_page(phc->offset); 997 998 phdr->p_type = PT_LOAD; 999 phdr->p_offset = phc->offset; 1000 phdr->p_vaddr = entry->start; 1001 phdr->p_paddr = 0; 1002 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start; 1003 phdr->p_align = PAGE_SIZE; 1004 phdr->p_flags = 0; 1005 if (entry->protection & VM_PROT_READ) 1006 phdr->p_flags |= PF_R; 1007 if (entry->protection & VM_PROT_WRITE) 1008 phdr->p_flags |= PF_W; 1009 if (entry->protection & VM_PROT_EXECUTE) 1010 phdr->p_flags |= PF_X; 1011 1012 phc->offset += phdr->p_filesz; 1013 phc->phdr++; 1014 } 1015 1016 /* 1017 * A callback for each_writable_segment() to gather information about 1018 * the number of segments and their total size. 1019 */ 1020 static void 1021 cb_size_segment(entry, closure) 1022 vm_map_entry_t entry; 1023 void *closure; 1024 { 1025 struct sseg_closure *ssc = (struct sseg_closure *)closure; 1026 1027 ssc->count++; 1028 ssc->size += entry->end - entry->start; 1029 } 1030 1031 /* 1032 * For each writable segment in the process's memory map, call the given 1033 * function with a pointer to the map entry and some arbitrary 1034 * caller-supplied data. 1035 */ 1036 static void 1037 each_writable_segment(p, func, closure) 1038 struct proc *p; 1039 segment_callback func; 1040 void *closure; 1041 { 1042 vm_map_t map = &p->p_vmspace->vm_map; 1043 vm_map_entry_t entry; 1044 1045 for (entry = map->header.next; entry != &map->header; 1046 entry = entry->next) { 1047 vm_object_t obj; 1048 1049 /* 1050 * Don't dump inaccessible mappings, deal with legacy 1051 * coredump mode. 1052 * 1053 * Note that read-only segments related to the elf binary 1054 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer 1055 * need to arbitrarily ignore such segments. 1056 */ 1057 if (elf_legacy_coredump) { 1058 if ((entry->protection & VM_PROT_RW) != VM_PROT_RW) 1059 continue; 1060 } else { 1061 if ((entry->protection & VM_PROT_ALL) == 0) 1062 continue; 1063 } 1064 1065 /* 1066 * Dont include memory segment in the coredump if 1067 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in 1068 * madvise(2). Do not dump submaps (i.e. parts of the 1069 * kernel map). 1070 */ 1071 if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP)) 1072 continue; 1073 1074 if ((obj = entry->object.vm_object) == NULL) 1075 continue; 1076 1077 /* Find the deepest backing object. */ 1078 while (obj->backing_object != NULL) 1079 obj = obj->backing_object; 1080 1081 /* Ignore memory-mapped devices and such things. */ 1082 if (obj->type != OBJT_DEFAULT && 1083 obj->type != OBJT_SWAP && 1084 obj->type != OBJT_VNODE) 1085 continue; 1086 1087 (*func)(entry, closure); 1088 } 1089 } 1090 1091 /* 1092 * Write the core file header to the file, including padding up to 1093 * the page boundary. 1094 */ 1095 static int 1096 __elfN(corehdr)(td, vp, cred, numsegs, hdr, hdrsize) 1097 struct thread *td; 1098 struct vnode *vp; 1099 struct ucred *cred; 1100 int numsegs; 1101 size_t hdrsize; 1102 void *hdr; 1103 { 1104 struct { 1105 prstatus_t status; 1106 prfpregset_t fpregset; 1107 prpsinfo_t psinfo; 1108 } *tempdata; 1109 struct proc *p = td->td_proc; 1110 size_t off; 1111 prstatus_t *status; 1112 prfpregset_t *fpregset; 1113 prpsinfo_t *psinfo; 1114 1115 tempdata = malloc(sizeof(*tempdata), M_TEMP, M_ZERO | M_WAITOK); 1116 status = &tempdata->status; 1117 fpregset = &tempdata->fpregset; 1118 psinfo = &tempdata->psinfo; 1119 1120 /* Gather the information for the header. */ 1121 status->pr_version = PRSTATUS_VERSION; 1122 status->pr_statussz = sizeof(prstatus_t); 1123 status->pr_gregsetsz = sizeof(gregset_t); 1124 status->pr_fpregsetsz = sizeof(fpregset_t); 1125 status->pr_osreldate = osreldate; 1126 status->pr_cursig = p->p_sig; 1127 status->pr_pid = p->p_pid; 1128 fill_regs(td, &status->pr_reg); 1129 1130 fill_fpregs(td, fpregset); 1131 1132 psinfo->pr_version = PRPSINFO_VERSION; 1133 psinfo->pr_psinfosz = sizeof(prpsinfo_t); 1134 strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname)); 1135 1136 /* XXX - We don't fill in the command line arguments properly yet. */ 1137 strlcpy(psinfo->pr_psargs, p->p_comm, sizeof(psinfo->pr_psargs)); 1138 1139 /* Fill in the header. */ 1140 bzero(hdr, hdrsize); 1141 off = 0; 1142 __elfN(puthdr)(p, hdr, &off, status, fpregset, psinfo, numsegs); 1143 1144 free(tempdata, M_TEMP); 1145 1146 /* Write it to the core file. */ 1147 return (vn_rdwr_inchunks(UIO_WRITE, vp, hdr, hdrsize, (off_t)0, 1148 UIO_SYSSPACE, IO_UNIT | IO_DIRECT, cred, NOCRED, NULL, 1149 td)); /* XXXKSE */ 1150 } 1151 1152 static void 1153 __elfN(puthdr)(struct proc *p, void *dst, size_t *off, const prstatus_t *status, 1154 const prfpregset_t *fpregset, const prpsinfo_t *psinfo, int numsegs) 1155 { 1156 size_t ehoff; 1157 size_t phoff; 1158 size_t noteoff; 1159 size_t notesz; 1160 1161 ehoff = *off; 1162 *off += sizeof(Elf_Ehdr); 1163 1164 phoff = *off; 1165 *off += (numsegs + 1) * sizeof(Elf_Phdr); 1166 1167 noteoff = *off; 1168 __elfN(putnote)(dst, off, "FreeBSD", NT_PRSTATUS, status, 1169 sizeof *status); 1170 __elfN(putnote)(dst, off, "FreeBSD", NT_FPREGSET, fpregset, 1171 sizeof *fpregset); 1172 __elfN(putnote)(dst, off, "FreeBSD", NT_PRPSINFO, psinfo, 1173 sizeof *psinfo); 1174 notesz = *off - noteoff; 1175 1176 /* Align up to a page boundary for the program segments. */ 1177 *off = round_page(*off); 1178 1179 if (dst != NULL) { 1180 Elf_Ehdr *ehdr; 1181 Elf_Phdr *phdr; 1182 struct phdr_closure phc; 1183 1184 /* 1185 * Fill in the ELF header. 1186 */ 1187 ehdr = (Elf_Ehdr *)((char *)dst + ehoff); 1188 ehdr->e_ident[EI_MAG0] = ELFMAG0; 1189 ehdr->e_ident[EI_MAG1] = ELFMAG1; 1190 ehdr->e_ident[EI_MAG2] = ELFMAG2; 1191 ehdr->e_ident[EI_MAG3] = ELFMAG3; 1192 ehdr->e_ident[EI_CLASS] = ELF_CLASS; 1193 ehdr->e_ident[EI_DATA] = ELF_DATA; 1194 ehdr->e_ident[EI_VERSION] = EV_CURRENT; 1195 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD; 1196 ehdr->e_ident[EI_ABIVERSION] = 0; 1197 ehdr->e_ident[EI_PAD] = 0; 1198 ehdr->e_type = ET_CORE; 1199 ehdr->e_machine = ELF_ARCH; 1200 ehdr->e_version = EV_CURRENT; 1201 ehdr->e_entry = 0; 1202 ehdr->e_phoff = phoff; 1203 ehdr->e_flags = 0; 1204 ehdr->e_ehsize = sizeof(Elf_Ehdr); 1205 ehdr->e_phentsize = sizeof(Elf_Phdr); 1206 ehdr->e_phnum = numsegs + 1; 1207 ehdr->e_shentsize = sizeof(Elf_Shdr); 1208 ehdr->e_shnum = 0; 1209 ehdr->e_shstrndx = SHN_UNDEF; 1210 1211 /* 1212 * Fill in the program header entries. 1213 */ 1214 phdr = (Elf_Phdr *)((char *)dst + phoff); 1215 1216 /* The note segement. */ 1217 phdr->p_type = PT_NOTE; 1218 phdr->p_offset = noteoff; 1219 phdr->p_vaddr = 0; 1220 phdr->p_paddr = 0; 1221 phdr->p_filesz = notesz; 1222 phdr->p_memsz = 0; 1223 phdr->p_flags = 0; 1224 phdr->p_align = 0; 1225 phdr++; 1226 1227 /* All the writable segments from the program. */ 1228 phc.phdr = phdr; 1229 phc.offset = *off; 1230 each_writable_segment(p, cb_put_phdr, &phc); 1231 } 1232 } 1233 1234 static void 1235 __elfN(putnote)(void *dst, size_t *off, const char *name, int type, 1236 const void *desc, size_t descsz) 1237 { 1238 Elf_Note note; 1239 1240 note.n_namesz = strlen(name) + 1; 1241 note.n_descsz = descsz; 1242 note.n_type = type; 1243 if (dst != NULL) 1244 bcopy(¬e, (char *)dst + *off, sizeof note); 1245 *off += sizeof note; 1246 if (dst != NULL) 1247 bcopy(name, (char *)dst + *off, note.n_namesz); 1248 *off += roundup2(note.n_namesz, sizeof(Elf_Size)); 1249 if (dst != NULL) 1250 bcopy(desc, (char *)dst + *off, note.n_descsz); 1251 *off += roundup2(note.n_descsz, sizeof(Elf_Size)); 1252 } 1253 1254 /* 1255 * Tell kern_execve.c about it, with a little help from the linker. 1256 */ 1257 static struct execsw __elfN(execsw) = { 1258 __CONCAT(exec_, __elfN(imgact)), 1259 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) 1260 }; 1261 EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw)); 1262