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