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: src/sys/kern/imgact_elf.c,v 1.73.2.13 2002/12/28 19:49:41 dillon Exp $ 31 */ 32 33 #include <sys/param.h> 34 #include <sys/exec.h> 35 #include <sys/fcntl.h> 36 #include <sys/file.h> 37 #include <sys/imgact.h> 38 #include <sys/imgact_elf.h> 39 #include <sys/kernel.h> 40 #include <sys/malloc.h> 41 #include <sys/mman.h> 42 #include <sys/systm.h> 43 #include <sys/proc.h> 44 #include <sys/nlookup.h> 45 #include <sys/pioctl.h> 46 #include <sys/procfs.h> 47 #include <sys/resourcevar.h> 48 #include <sys/signalvar.h> 49 #include <sys/stat.h> 50 #include <sys/syscall.h> 51 #include <sys/sysctl.h> 52 #include <sys/sysent.h> 53 #include <sys/vnode.h> 54 #include <sys/eventhandler.h> 55 56 #include <cpu/lwbuf.h> 57 58 #include <vm/vm.h> 59 #include <vm/vm_kern.h> 60 #include <vm/vm_param.h> 61 #include <vm/pmap.h> 62 #include <sys/lock.h> 63 #include <vm/vm_map.h> 64 #include <vm/vm_object.h> 65 #include <vm/vm_extern.h> 66 67 #include <machine/elf.h> 68 #include <machine/md_var.h> 69 #include <sys/mount.h> 70 #include <sys/ckpt.h> 71 72 #define OLD_EI_BRAND 8 73 #define truncps(va,ps) ((va) & ~(ps - 1)) 74 #define aligned(a,t) (truncps((u_long)(a), sizeof(t)) == (u_long)(a)) 75 76 static int __elfN(check_header)(const Elf_Ehdr *hdr); 77 static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp, 78 const char *interp, int32_t *osrel); 79 static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr, 80 u_long *entry); 81 static int __elfN(load_section)(struct proc *p, 82 struct vmspace *vmspace, struct vnode *vp, 83 vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, 84 vm_prot_t prot); 85 static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp); 86 static boolean_t __elfN(bsd_trans_osrel)(const Elf_Note *note, 87 int32_t *osrel); 88 static boolean_t __elfN(check_note)(struct image_params *imgp, 89 Elf_Brandnote *checknote, int32_t *osrel); 90 static vm_prot_t __elfN(trans_prot)(Elf_Word); 91 static Elf_Word __elfN(untrans_prot)(vm_prot_t); 92 static boolean_t check_PT_NOTE(struct image_params *imgp, 93 Elf_Brandnote *checknote, int32_t *osrel, const Elf_Phdr * pnote); 94 static boolean_t extract_interpreter(struct image_params *imgp, 95 const Elf_Phdr *pinterpreter, char *data); 96 static u_long pie_base_hint(struct proc *p); 97 98 static int elf_legacy_coredump = 0; 99 static int __elfN(fallback_brand) = -1; 100 static int elf_pie_base_mmap = 0; 101 #if defined(__x86_64__) 102 SYSCTL_NODE(_kern, OID_AUTO, elf64, CTLFLAG_RW, 0, ""); 103 SYSCTL_INT(_debug, OID_AUTO, elf64_legacy_coredump, CTLFLAG_RW, 104 &elf_legacy_coredump, 0, "legacy coredump mode"); 105 SYSCTL_INT(_kern_elf64, OID_AUTO, fallback_brand, CTLFLAG_RW, 106 &elf64_fallback_brand, 0, "ELF64 brand of last resort"); 107 TUNABLE_INT("kern.elf64.fallback_brand", &elf64_fallback_brand); 108 SYSCTL_INT(_kern_elf64, OID_AUTO, pie_base_mmap, CTLFLAG_RW, 109 &elf_pie_base_mmap, 0, 110 "choose a base address for PIE as if it is mapped with mmap()"); 111 TUNABLE_INT("kern.elf64.pie_base_mmap", &elf_pie_base_mmap); 112 #else /* i386 assumed */ 113 SYSCTL_NODE(_kern, OID_AUTO, elf32, CTLFLAG_RW, 0, ""); 114 SYSCTL_INT(_debug, OID_AUTO, elf32_legacy_coredump, CTLFLAG_RW, 115 &elf_legacy_coredump, 0, "legacy coredump mode"); 116 SYSCTL_INT(_kern_elf32, OID_AUTO, fallback_brand, CTLFLAG_RW, 117 &elf32_fallback_brand, 0, "ELF32 brand of last resort"); 118 TUNABLE_INT("kern.elf32.fallback_brand", &elf32_fallback_brand); 119 SYSCTL_INT(_kern_elf32, OID_AUTO, pie_base_mmap, CTLFLAG_RW, 120 &elf_pie_base_mmap, 0, 121 "choose a base address for PIE as if it is mapped with mmap()"); 122 TUNABLE_INT("kern.elf32.pie_base_mmap", &elf_pie_base_mmap); 123 #endif 124 125 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS]; 126 127 static const char DRAGONFLY_ABI_VENDOR[] = "DragonFly"; 128 static const char FREEBSD_ABI_VENDOR[] = "FreeBSD"; 129 130 Elf_Brandnote __elfN(dragonfly_brandnote) = { 131 .hdr.n_namesz = sizeof(DRAGONFLY_ABI_VENDOR), 132 .hdr.n_descsz = sizeof(int32_t), 133 .hdr.n_type = 1, 134 .vendor = DRAGONFLY_ABI_VENDOR, 135 .flags = BN_TRANSLATE_OSREL, 136 .trans_osrel = __elfN(bsd_trans_osrel), 137 }; 138 139 Elf_Brandnote __elfN(freebsd_brandnote) = { 140 .hdr.n_namesz = sizeof(FREEBSD_ABI_VENDOR), 141 .hdr.n_descsz = sizeof(int32_t), 142 .hdr.n_type = 1, 143 .vendor = FREEBSD_ABI_VENDOR, 144 .flags = BN_TRANSLATE_OSREL, 145 .trans_osrel = __elfN(bsd_trans_osrel), 146 }; 147 148 int 149 __elfN(insert_brand_entry)(Elf_Brandinfo *entry) 150 { 151 int i; 152 153 for (i = 0; i < MAX_BRANDS; i++) { 154 if (elf_brand_list[i] == NULL) { 155 elf_brand_list[i] = entry; 156 break; 157 } 158 } 159 if (i == MAX_BRANDS) { 160 uprintf("WARNING: %s: could not insert brandinfo entry: %p\n", 161 __func__, entry); 162 return (-1); 163 } 164 return (0); 165 } 166 167 int 168 __elfN(remove_brand_entry)(Elf_Brandinfo *entry) 169 { 170 int i; 171 172 for (i = 0; i < MAX_BRANDS; i++) { 173 if (elf_brand_list[i] == entry) { 174 elf_brand_list[i] = NULL; 175 break; 176 } 177 } 178 if (i == MAX_BRANDS) 179 return (-1); 180 return (0); 181 } 182 183 /* 184 * Check if an elf brand is being used anywhere in the system. 185 * 186 * Used by the linux emulation module unloader. This isn't safe from 187 * races. 188 */ 189 struct elf_brand_inuse_info { 190 int rval; 191 Elf_Brandinfo *entry; 192 }; 193 194 static int elf_brand_inuse_callback(struct proc *p, void *data); 195 196 int 197 __elfN(brand_inuse)(Elf_Brandinfo *entry) 198 { 199 struct elf_brand_inuse_info info; 200 201 info.rval = FALSE; 202 info.entry = entry; 203 allproc_scan(elf_brand_inuse_callback, &info); 204 return (info.rval); 205 } 206 207 static 208 int 209 elf_brand_inuse_callback(struct proc *p, void *data) 210 { 211 struct elf_brand_inuse_info *info = data; 212 213 if (p->p_sysent == info->entry->sysvec) { 214 info->rval = TRUE; 215 return (-1); 216 } 217 return (0); 218 } 219 220 static int 221 __elfN(check_header)(const Elf_Ehdr *hdr) 222 { 223 Elf_Brandinfo *bi; 224 int i; 225 226 if (!IS_ELF(*hdr) || 227 hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 228 hdr->e_ident[EI_DATA] != ELF_TARG_DATA || 229 hdr->e_ident[EI_VERSION] != EV_CURRENT || 230 hdr->e_phentsize != sizeof(Elf_Phdr) || 231 hdr->e_ehsize != sizeof(Elf_Ehdr) || 232 hdr->e_version != ELF_TARG_VER) 233 return (ENOEXEC); 234 235 /* 236 * Make sure we have at least one brand for this machine. 237 */ 238 239 for (i = 0; i < MAX_BRANDS; i++) { 240 bi = elf_brand_list[i]; 241 if (bi != NULL && bi->machine == hdr->e_machine) 242 break; 243 } 244 if (i == MAX_BRANDS) 245 return (ENOEXEC); 246 247 return (0); 248 } 249 250 static int 251 __elfN(load_section)(struct proc *p, struct vmspace *vmspace, struct vnode *vp, 252 vm_offset_t offset, caddr_t vmaddr, size_t memsz, 253 size_t filsz, vm_prot_t prot) 254 { 255 size_t map_len; 256 vm_offset_t map_addr; 257 int error, rv, cow; 258 int count; 259 int shared; 260 size_t copy_len; 261 vm_object_t object; 262 vm_offset_t file_addr; 263 264 object = vp->v_object; 265 error = 0; 266 267 /* 268 * In most cases we will be able to use a shared lock on the 269 * object we are inserting into the map. The lock will be 270 * upgraded in situations where new VM pages must be allocated. 271 */ 272 vm_object_hold_shared(object); 273 shared = 1; 274 275 /* 276 * It's necessary to fail if the filsz + offset taken from the 277 * header is greater than the actual file pager object's size. 278 * If we were to allow this, then the vm_map_find() below would 279 * walk right off the end of the file object and into the ether. 280 * 281 * While I'm here, might as well check for something else that 282 * is invalid: filsz cannot be greater than memsz. 283 */ 284 if ((off_t)filsz + offset > vp->v_filesize || filsz > memsz) { 285 uprintf("elf_load_section: truncated ELF file\n"); 286 vm_object_drop(object); 287 return (ENOEXEC); 288 } 289 290 map_addr = trunc_page((vm_offset_t)vmaddr); 291 file_addr = trunc_page(offset); 292 293 /* 294 * We have two choices. We can either clear the data in the last page 295 * of an oversized mapping, or we can start the anon mapping a page 296 * early and copy the initialized data into that first page. We 297 * choose the second.. 298 */ 299 if (memsz > filsz) 300 map_len = trunc_page(offset+filsz) - file_addr; 301 else 302 map_len = round_page(offset+filsz) - file_addr; 303 304 if (map_len != 0) { 305 vm_object_reference_locked(object); 306 307 /* cow flags: don't dump readonly sections in core */ 308 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT; 309 if ((prot & VM_PROT_WRITE) == 0) 310 cow |= MAP_DISABLE_COREDUMP; 311 if (shared == 0) 312 cow |= MAP_PREFAULT_RELOCK; 313 314 count = vm_map_entry_reserve(MAP_RESERVE_COUNT); 315 vm_map_lock(&vmspace->vm_map); 316 rv = vm_map_insert(&vmspace->vm_map, &count, 317 object, NULL, 318 file_addr, /* file offset */ 319 map_addr, /* virtual start */ 320 map_addr + map_len,/* virtual end */ 321 VM_MAPTYPE_NORMAL, 322 prot, VM_PROT_ALL, 323 cow); 324 vm_map_unlock(&vmspace->vm_map); 325 vm_map_entry_release(count); 326 327 /* 328 * NOTE: Object must have a hold ref when calling 329 * vm_object_deallocate(). 330 */ 331 if (rv != KERN_SUCCESS) { 332 vm_object_drop(object); 333 vm_object_deallocate(object); 334 return (EINVAL); 335 } 336 337 /* we can stop now if we've covered it all */ 338 if (memsz == filsz) { 339 vm_object_drop(object); 340 return (0); 341 } 342 } 343 344 /* 345 * We have to get the remaining bit of the file into the first part 346 * of the oversized map segment. This is normally because the .data 347 * segment in the file is extended to provide bss. It's a neat idea 348 * to try and save a page, but it's a pain in the behind to implement. 349 */ 350 copy_len = (offset + filsz) - trunc_page(offset + filsz); 351 map_addr = trunc_page((vm_offset_t)vmaddr + filsz); 352 map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr; 353 354 /* This had damn well better be true! */ 355 if (map_len != 0) { 356 count = vm_map_entry_reserve(MAP_RESERVE_COUNT); 357 vm_map_lock(&vmspace->vm_map); 358 rv = vm_map_insert(&vmspace->vm_map, &count, 359 NULL, NULL, 360 0, 361 map_addr, 362 map_addr + map_len, 363 VM_MAPTYPE_NORMAL, 364 VM_PROT_ALL, VM_PROT_ALL, 365 0); 366 vm_map_unlock(&vmspace->vm_map); 367 vm_map_entry_release(count); 368 if (rv != KERN_SUCCESS) { 369 vm_object_drop(object); 370 return (EINVAL); 371 } 372 } 373 374 if (copy_len != 0) { 375 struct lwbuf *lwb; 376 struct lwbuf lwb_cache; 377 vm_page_t m; 378 379 m = vm_fault_object_page(object, trunc_page(offset + filsz), 380 VM_PROT_READ, 0, &shared, &error); 381 vm_object_drop(object); 382 if (m) { 383 lwb = lwbuf_alloc(m, &lwb_cache); 384 error = copyout((caddr_t)lwbuf_kva(lwb), 385 (caddr_t)map_addr, copy_len); 386 lwbuf_free(lwb); 387 vm_page_unhold(m); 388 } 389 } else { 390 vm_object_drop(object); 391 } 392 393 /* 394 * set it to the specified protection 395 */ 396 if (error == 0) { 397 vm_map_protect(&vmspace->vm_map, 398 map_addr, map_addr + map_len, 399 prot, FALSE); 400 } 401 return (error); 402 } 403 404 /* 405 * Load the file "file" into memory. It may be either a shared object 406 * or an executable. 407 * 408 * The "addr" reference parameter is in/out. On entry, it specifies 409 * the address where a shared object should be loaded. If the file is 410 * an executable, this value is ignored. On exit, "addr" specifies 411 * where the file was actually loaded. 412 * 413 * The "entry" reference parameter is out only. On exit, it specifies 414 * the entry point for the loaded file. 415 */ 416 static int 417 __elfN(load_file)(struct proc *p, const char *file, u_long *addr, u_long *entry) 418 { 419 struct { 420 struct nlookupdata nd; 421 struct vattr attr; 422 struct image_params image_params; 423 } *tempdata; 424 const Elf_Ehdr *hdr = NULL; 425 const Elf_Phdr *phdr = NULL; 426 struct nlookupdata *nd; 427 struct vmspace *vmspace = p->p_vmspace; 428 struct vattr *attr; 429 struct image_params *imgp; 430 struct mount *topmnt; 431 vm_prot_t prot; 432 u_long rbase; 433 u_long base_addr = 0; 434 int error, i, numsegs; 435 436 tempdata = kmalloc(sizeof(*tempdata), M_TEMP, M_WAITOK); 437 nd = &tempdata->nd; 438 attr = &tempdata->attr; 439 imgp = &tempdata->image_params; 440 441 /* 442 * Initialize part of the common data 443 */ 444 imgp->proc = p; 445 imgp->attr = attr; 446 imgp->firstpage = NULL; 447 imgp->image_header = NULL; 448 imgp->vp = NULL; 449 450 error = nlookup_init(nd, file, UIO_SYSSPACE, NLC_FOLLOW); 451 if (error == 0) 452 error = nlookup(nd); 453 if (error == 0) 454 error = cache_vget(&nd->nl_nch, nd->nl_cred, 455 LK_SHARED, &imgp->vp); 456 topmnt = nd->nl_nch.mount; 457 nlookup_done(nd); 458 if (error) 459 goto fail; 460 461 /* 462 * Check permissions, modes, uid, etc on the file, and "open" it. 463 */ 464 error = exec_check_permissions(imgp, topmnt); 465 if (error) { 466 vn_unlock(imgp->vp); 467 goto fail; 468 } 469 470 error = exec_map_first_page(imgp); 471 /* 472 * Also make certain that the interpreter stays the same, so set 473 * its VTEXT flag, too. 474 */ 475 if (error == 0) 476 vsetflags(imgp->vp, VTEXT); 477 vn_unlock(imgp->vp); 478 if (error) 479 goto fail; 480 481 hdr = (const Elf_Ehdr *)imgp->image_header; 482 if ((error = __elfN(check_header)(hdr)) != 0) 483 goto fail; 484 if (hdr->e_type == ET_DYN) 485 rbase = *addr; 486 else if (hdr->e_type == ET_EXEC) 487 rbase = 0; 488 else { 489 error = ENOEXEC; 490 goto fail; 491 } 492 493 /* Only support headers that fit within first page for now */ 494 /* (multiplication of two Elf_Half fields will not overflow) */ 495 if ((hdr->e_phoff > PAGE_SIZE) || 496 (hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE - hdr->e_phoff) { 497 error = ENOEXEC; 498 goto fail; 499 } 500 501 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 502 if (!aligned(phdr, Elf_Addr)) { 503 error = ENOEXEC; 504 goto fail; 505 } 506 507 for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) { 508 if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) { 509 /* Loadable segment */ 510 prot = __elfN(trans_prot)(phdr[i].p_flags); 511 error = __elfN(load_section)( 512 p, vmspace, imgp->vp, 513 phdr[i].p_offset, 514 (caddr_t)phdr[i].p_vaddr + 515 rbase, 516 phdr[i].p_memsz, 517 phdr[i].p_filesz, prot); 518 if (error != 0) 519 goto fail; 520 /* 521 * Establish the base address if this is the 522 * first segment. 523 */ 524 if (numsegs == 0) 525 base_addr = trunc_page(phdr[i].p_vaddr + rbase); 526 numsegs++; 527 } 528 } 529 *addr = base_addr; 530 *entry = (unsigned long)hdr->e_entry + rbase; 531 532 fail: 533 if (imgp->firstpage) 534 exec_unmap_first_page(imgp); 535 if (imgp->vp) { 536 vrele(imgp->vp); 537 imgp->vp = NULL; 538 } 539 kfree(tempdata, M_TEMP); 540 541 return (error); 542 } 543 544 static Elf_Brandinfo * 545 __elfN(get_brandinfo)(struct image_params *imgp, const char *interp, 546 int32_t *osrel) 547 { 548 const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header; 549 Elf_Brandinfo *bi; 550 boolean_t ret; 551 int i; 552 553 /* We support four types of branding -- (1) the ELF EI_OSABI field 554 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string 555 * branding within the ELF header, (3) path of the `interp_path' field, 556 * and (4) the ".note.ABI-tag" ELF section. 557 */ 558 559 /* Look for an ".note.ABI-tag" ELF section */ 560 for (i = 0; i < MAX_BRANDS; i++) { 561 bi = elf_brand_list[i]; 562 563 if (bi == NULL) 564 continue; 565 if (hdr->e_machine == bi->machine && (bi->flags & 566 (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) { 567 ret = __elfN(check_note)(imgp, bi->brand_note, osrel); 568 if (ret) 569 return (bi); 570 } 571 } 572 573 /* If the executable has a brand, search for it in the brand list. */ 574 for (i = 0; i < MAX_BRANDS; i++) { 575 bi = elf_brand_list[i]; 576 577 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) 578 continue; 579 if (hdr->e_machine == bi->machine && 580 (hdr->e_ident[EI_OSABI] == bi->brand || 581 strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND], 582 bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0)) 583 return (bi); 584 } 585 586 /* Lacking a known brand, search for a recognized interpreter. */ 587 if (interp != NULL) { 588 for (i = 0; i < MAX_BRANDS; i++) { 589 bi = elf_brand_list[i]; 590 591 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) 592 continue; 593 if (hdr->e_machine == bi->machine && 594 strcmp(interp, bi->interp_path) == 0) 595 return (bi); 596 } 597 } 598 599 /* Lacking a recognized interpreter, try the default brand */ 600 for (i = 0; i < MAX_BRANDS; i++) { 601 bi = elf_brand_list[i]; 602 603 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) 604 continue; 605 if (hdr->e_machine == bi->machine && 606 __elfN(fallback_brand) == bi->brand) 607 return (bi); 608 } 609 return (NULL); 610 } 611 612 static int 613 __CONCAT(exec_,__elfN(imgact))(struct image_params *imgp) 614 { 615 const Elf_Ehdr *hdr = (const Elf_Ehdr *) imgp->image_header; 616 const Elf_Phdr *phdr; 617 Elf_Auxargs *elf_auxargs; 618 struct vmspace *vmspace; 619 vm_prot_t prot; 620 u_long text_size = 0, data_size = 0, total_size = 0; 621 u_long text_addr = 0, data_addr = 0; 622 u_long seg_size, seg_addr; 623 u_long addr, baddr, et_dyn_addr = 0, entry = 0, proghdr = 0; 624 int32_t osrel = 0; 625 int error = 0, i, n; 626 boolean_t failure; 627 char *interp = NULL; 628 const char *newinterp = NULL; 629 Elf_Brandinfo *brand_info; 630 char *path; 631 632 /* 633 * Do we have a valid ELF header ? 634 * 635 * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later if a particular 636 * brand doesn't support it. Both DragonFly platforms do by default. 637 */ 638 if (__elfN(check_header)(hdr) != 0 || 639 (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)) 640 return (-1); 641 642 /* 643 * From here on down, we return an errno, not -1, as we've 644 * detected an ELF file. 645 */ 646 647 if ((hdr->e_phoff > PAGE_SIZE) || 648 (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) { 649 /* Only support headers in first page for now */ 650 return (ENOEXEC); 651 } 652 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 653 if (!aligned(phdr, Elf_Addr)) 654 return (ENOEXEC); 655 n = 0; 656 baddr = 0; 657 for (i = 0; i < hdr->e_phnum; i++) { 658 if (phdr[i].p_type == PT_LOAD) { 659 if (n == 0) 660 baddr = phdr[i].p_vaddr; 661 n++; 662 continue; 663 } 664 if (phdr[i].p_type == PT_INTERP) { 665 /* 666 * If interp is already defined there are more than 667 * one PT_INTERP program headers present. Take only 668 * the first one and ignore the rest. 669 */ 670 if (interp != NULL) 671 continue; 672 673 if (phdr[i].p_filesz == 0 || 674 phdr[i].p_filesz > PAGE_SIZE || 675 phdr[i].p_filesz > MAXPATHLEN) 676 return (ENOEXEC); 677 678 interp = kmalloc(phdr[i].p_filesz, M_TEMP, M_WAITOK); 679 failure = extract_interpreter(imgp, &phdr[i], interp); 680 if (failure) { 681 kfree(interp, M_TEMP); 682 return (ENOEXEC); 683 } 684 continue; 685 } 686 } 687 688 brand_info = __elfN(get_brandinfo)(imgp, interp, &osrel); 689 if (brand_info == NULL) { 690 uprintf("ELF binary type \"%u\" not known.\n", 691 hdr->e_ident[EI_OSABI]); 692 if (interp != NULL) 693 kfree(interp, M_TEMP); 694 return (ENOEXEC); 695 } 696 if (hdr->e_type == ET_DYN) { 697 if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) { 698 if (interp != NULL) 699 kfree(interp, M_TEMP); 700 return (ENOEXEC); 701 } 702 /* 703 * If p_vaddr field of PT_LOAD program header is zero and type of an 704 * executale is ET_DYN, then it must be a position independent 705 * executable (PIE). In this case the system needs to pick a base 706 * address for us. Set et_dyn_addr to non-zero and choose the actual 707 * address when we are ready. 708 */ 709 if (baddr == 0) 710 et_dyn_addr = 1; 711 } 712 713 if (interp != NULL && brand_info->interp_newpath != NULL) 714 newinterp = brand_info->interp_newpath; 715 716 exec_new_vmspace(imgp, NULL); 717 718 /* 719 * Yeah, I'm paranoid. There is every reason in the world to get 720 * VTEXT now since from here on out, there are places we can have 721 * a context switch. Better safe than sorry; I really don't want 722 * the file to change while it's being loaded. 723 */ 724 vsetflags(imgp->vp, VTEXT); 725 726 vmspace = imgp->proc->p_vmspace; 727 /* Choose the base address for dynamic executables if we need to. */ 728 if (et_dyn_addr) 729 et_dyn_addr = pie_base_hint(imgp->proc); 730 731 for (i = 0; i < hdr->e_phnum; i++) { 732 switch (phdr[i].p_type) { 733 case PT_LOAD: /* Loadable segment */ 734 if (phdr[i].p_memsz == 0) 735 break; 736 prot = __elfN(trans_prot)(phdr[i].p_flags); 737 738 if ((error = __elfN(load_section)( 739 imgp->proc, 740 vmspace, 741 imgp->vp, 742 phdr[i].p_offset, 743 (caddr_t)phdr[i].p_vaddr + et_dyn_addr, 744 phdr[i].p_memsz, 745 phdr[i].p_filesz, 746 prot)) != 0) { 747 if (interp != NULL) 748 kfree (interp, M_TEMP); 749 return (error); 750 } 751 752 /* 753 * If this segment contains the program headers, 754 * remember their virtual address for the AT_PHDR 755 * aux entry. Static binaries don't usually include 756 * a PT_PHDR entry. 757 */ 758 if (phdr[i].p_offset == 0 && 759 hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize 760 <= phdr[i].p_filesz) 761 proghdr = phdr[i].p_vaddr + hdr->e_phoff + 762 et_dyn_addr; 763 764 seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr); 765 seg_size = round_page(phdr[i].p_memsz + 766 phdr[i].p_vaddr + et_dyn_addr - seg_addr); 767 768 /* 769 * Is this .text or .data? We can't use 770 * VM_PROT_WRITE or VM_PROT_EXEC, it breaks the 771 * alpha terribly and possibly does other bad 772 * things so we stick to the old way of figuring 773 * it out: If the segment contains the program 774 * entry point, it's a text segment, otherwise it 775 * is a data segment. 776 * 777 * Note that obreak() assumes that data_addr + 778 * data_size == end of data load area, and the ELF 779 * file format expects segments to be sorted by 780 * address. If multiple data segments exist, the 781 * last one will be used. 782 */ 783 if (hdr->e_entry >= phdr[i].p_vaddr && 784 hdr->e_entry < (phdr[i].p_vaddr + 785 phdr[i].p_memsz)) { 786 text_size = seg_size; 787 text_addr = seg_addr; 788 entry = (u_long)hdr->e_entry + et_dyn_addr; 789 } else { 790 data_size = seg_size; 791 data_addr = seg_addr; 792 } 793 total_size += seg_size; 794 795 /* 796 * Check limits. It should be safe to check the 797 * limits after loading the segment since we do 798 * not actually fault in all the segment's pages. 799 */ 800 if (data_size > 801 imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur || 802 text_size > maxtsiz || 803 total_size > 804 imgp->proc->p_rlimit[RLIMIT_VMEM].rlim_cur) { 805 if (interp != NULL) 806 kfree(interp, M_TEMP); 807 error = ENOMEM; 808 return (error); 809 } 810 break; 811 case PT_PHDR: /* Program header table info */ 812 proghdr = phdr[i].p_vaddr + et_dyn_addr; 813 break; 814 default: 815 break; 816 } 817 } 818 819 vmspace->vm_tsize = text_size >> PAGE_SHIFT; 820 vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr; 821 vmspace->vm_dsize = data_size >> PAGE_SHIFT; 822 vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr; 823 824 addr = ELF_RTLD_ADDR(vmspace); 825 826 imgp->entry_addr = entry; 827 828 imgp->proc->p_sysent = brand_info->sysvec; 829 EVENTHANDLER_INVOKE(process_exec, imgp); 830 831 if (interp != NULL) { 832 int have_interp = FALSE; 833 if (brand_info->emul_path != NULL && 834 brand_info->emul_path[0] != '\0') { 835 path = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK); 836 ksnprintf(path, MAXPATHLEN, "%s%s", 837 brand_info->emul_path, interp); 838 error = __elfN(load_file)(imgp->proc, path, &addr, 839 &imgp->entry_addr); 840 kfree(path, M_TEMP); 841 if (error == 0) 842 have_interp = TRUE; 843 } 844 if (!have_interp && newinterp != NULL) { 845 error = __elfN(load_file)(imgp->proc, newinterp, 846 &addr, &imgp->entry_addr); 847 if (error == 0) 848 have_interp = TRUE; 849 } 850 if (!have_interp) { 851 error = __elfN(load_file)(imgp->proc, interp, &addr, 852 &imgp->entry_addr); 853 } 854 if (error != 0) { 855 uprintf("ELF interpreter %s not found\n", interp); 856 kfree(interp, M_TEMP); 857 return (error); 858 } 859 kfree(interp, M_TEMP); 860 } else 861 addr = et_dyn_addr; 862 863 /* 864 * Construct auxargs table (used by the fixup routine) 865 */ 866 elf_auxargs = kmalloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK); 867 elf_auxargs->execfd = -1; 868 elf_auxargs->phdr = proghdr; 869 elf_auxargs->phent = hdr->e_phentsize; 870 elf_auxargs->phnum = hdr->e_phnum; 871 elf_auxargs->pagesz = PAGE_SIZE; 872 elf_auxargs->base = addr; 873 elf_auxargs->flags = 0; 874 elf_auxargs->entry = entry; 875 876 imgp->auxargs = elf_auxargs; 877 imgp->interpreted = 0; 878 imgp->proc->p_osrel = osrel; 879 880 return (error); 881 } 882 883 int 884 __elfN(dragonfly_fixup)(register_t **stack_base, struct image_params *imgp) 885 { 886 Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs; 887 Elf_Addr *base; 888 Elf_Addr *pos; 889 890 base = (Elf_Addr *)*stack_base; 891 pos = base + (imgp->args->argc + imgp->args->envc + 2); 892 893 if (args->execfd != -1) 894 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 895 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 896 AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 897 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 898 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 899 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 900 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 901 AUXARGS_ENTRY(pos, AT_BASE, args->base); 902 if (imgp->execpathp != 0) 903 AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp); 904 AUXARGS_ENTRY(pos, AT_OSRELDATE, osreldate); 905 AUXARGS_ENTRY(pos, AT_NULL, 0); 906 907 kfree(imgp->auxargs, M_TEMP); 908 imgp->auxargs = NULL; 909 910 base--; 911 suword(base, (long)imgp->args->argc); 912 *stack_base = (register_t *)base; 913 return (0); 914 } 915 916 /* 917 * Code for generating ELF core dumps. 918 */ 919 920 typedef int (*segment_callback)(vm_map_entry_t, void *); 921 922 /* Closure for cb_put_phdr(). */ 923 struct phdr_closure { 924 Elf_Phdr *phdr; /* Program header to fill in (incremented) */ 925 Elf_Phdr *phdr_max; /* Pointer bound for error check */ 926 Elf_Off offset; /* Offset of segment in core file */ 927 }; 928 929 /* Closure for cb_size_segment(). */ 930 struct sseg_closure { 931 int count; /* Count of writable segments. */ 932 size_t vsize; /* Total size of all writable segments. */ 933 }; 934 935 /* Closure for cb_put_fp(). */ 936 struct fp_closure { 937 struct vn_hdr *vnh; 938 struct vn_hdr *vnh_max; 939 int count; 940 struct stat *sb; 941 }; 942 943 typedef struct elf_buf { 944 char *buf; 945 size_t off; 946 size_t off_max; 947 } *elf_buf_t; 948 949 static void *target_reserve(elf_buf_t target, size_t bytes, int *error); 950 951 static int cb_put_phdr (vm_map_entry_t, void *); 952 static int cb_size_segment (vm_map_entry_t, void *); 953 static int cb_fpcount_segment(vm_map_entry_t, void *); 954 static int cb_put_fp(vm_map_entry_t, void *); 955 956 957 static int each_segment (struct proc *, segment_callback, void *, int); 958 static int __elfN(corehdr)(struct lwp *, int, struct file *, struct ucred *, 959 int, elf_buf_t); 960 enum putmode { WRITE, DRYRUN }; 961 static int __elfN(puthdr)(struct lwp *, elf_buf_t, int sig, enum putmode, 962 int, struct file *); 963 static int elf_putallnotes(struct lwp *, elf_buf_t, int, enum putmode); 964 static int __elfN(putnote)(elf_buf_t, const char *, int, const void *, size_t); 965 966 static int elf_putsigs(struct lwp *, elf_buf_t); 967 static int elf_puttextvp(struct proc *, elf_buf_t); 968 static int elf_putfiles(struct proc *, elf_buf_t, struct file *); 969 970 int 971 __elfN(coredump)(struct lwp *lp, int sig, struct vnode *vp, off_t limit) 972 { 973 struct file *fp; 974 int error; 975 976 if ((error = falloc(NULL, &fp, NULL)) != 0) 977 return (error); 978 fsetcred(fp, lp->lwp_proc->p_ucred); 979 980 /* 981 * XXX fixme. 982 */ 983 fp->f_type = DTYPE_VNODE; 984 fp->f_flag = O_CREAT|O_WRONLY|O_NOFOLLOW; 985 fp->f_ops = &vnode_fileops; 986 fp->f_data = vp; 987 988 error = generic_elf_coredump(lp, sig, fp, limit); 989 990 fp->f_type = 0; 991 fp->f_flag = 0; 992 fp->f_ops = &badfileops; 993 fp->f_data = NULL; 994 fdrop(fp); 995 return (error); 996 } 997 998 int 999 generic_elf_coredump(struct lwp *lp, int sig, struct file *fp, off_t limit) 1000 { 1001 struct proc *p = lp->lwp_proc; 1002 struct ucred *cred = p->p_ucred; 1003 int error = 0; 1004 struct sseg_closure seginfo; 1005 struct elf_buf target; 1006 1007 if (!fp) 1008 kprintf("can't dump core - null fp\n"); 1009 1010 /* 1011 * Size the program segments 1012 */ 1013 seginfo.count = 0; 1014 seginfo.vsize = 0; 1015 each_segment(p, cb_size_segment, &seginfo, 1); 1016 1017 /* 1018 * Calculate the size of the core file header area by making 1019 * a dry run of generating it. Nothing is written, but the 1020 * size is calculated. 1021 */ 1022 bzero(&target, sizeof(target)); 1023 __elfN(puthdr)(lp, &target, sig, DRYRUN, seginfo.count, fp); 1024 1025 if (target.off + seginfo.vsize >= limit) 1026 return (EFAULT); 1027 1028 /* 1029 * Allocate memory for building the header, fill it up, 1030 * and write it out. 1031 */ 1032 target.off_max = target.off; 1033 target.off = 0; 1034 target.buf = kmalloc(target.off_max, M_TEMP, M_WAITOK|M_ZERO); 1035 1036 error = __elfN(corehdr)(lp, sig, fp, cred, seginfo.count, &target); 1037 1038 /* Write the contents of all of the writable segments. */ 1039 if (error == 0) { 1040 Elf_Phdr *php; 1041 int i; 1042 ssize_t nbytes; 1043 1044 php = (Elf_Phdr *)(target.buf + sizeof(Elf_Ehdr)) + 1; 1045 for (i = 0; i < seginfo.count; i++) { 1046 error = fp_write(fp, (caddr_t)php->p_vaddr, 1047 php->p_filesz, &nbytes, UIO_USERSPACE); 1048 if (error != 0) 1049 break; 1050 php++; 1051 } 1052 } 1053 kfree(target.buf, M_TEMP); 1054 1055 return (error); 1056 } 1057 1058 /* 1059 * A callback for each_segment() to write out the segment's 1060 * program header entry. 1061 */ 1062 static int 1063 cb_put_phdr(vm_map_entry_t entry, void *closure) 1064 { 1065 struct phdr_closure *phc = closure; 1066 Elf_Phdr *phdr = phc->phdr; 1067 1068 if (phc->phdr == phc->phdr_max) 1069 return (EINVAL); 1070 1071 phc->offset = round_page(phc->offset); 1072 1073 phdr->p_type = PT_LOAD; 1074 phdr->p_offset = phc->offset; 1075 phdr->p_vaddr = entry->start; 1076 phdr->p_paddr = 0; 1077 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start; 1078 phdr->p_align = PAGE_SIZE; 1079 phdr->p_flags = __elfN(untrans_prot)(entry->protection); 1080 1081 phc->offset += phdr->p_filesz; 1082 ++phc->phdr; 1083 return (0); 1084 } 1085 1086 /* 1087 * A callback for each_writable_segment() to gather information about 1088 * the number of segments and their total size. 1089 */ 1090 static int 1091 cb_size_segment(vm_map_entry_t entry, void *closure) 1092 { 1093 struct sseg_closure *ssc = closure; 1094 1095 ++ssc->count; 1096 ssc->vsize += entry->end - entry->start; 1097 return (0); 1098 } 1099 1100 /* 1101 * A callback for each_segment() to gather information about 1102 * the number of text segments. 1103 */ 1104 static int 1105 cb_fpcount_segment(vm_map_entry_t entry, void *closure) 1106 { 1107 int *count = closure; 1108 struct vnode *vp; 1109 1110 if (entry->object.vm_object->type == OBJT_VNODE) { 1111 vp = (struct vnode *)entry->object.vm_object->handle; 1112 if ((vp->v_flag & VCKPT) && curproc->p_textvp == vp) 1113 return (0); 1114 ++*count; 1115 } 1116 return (0); 1117 } 1118 1119 static int 1120 cb_put_fp(vm_map_entry_t entry, void *closure) 1121 { 1122 struct fp_closure *fpc = closure; 1123 struct vn_hdr *vnh = fpc->vnh; 1124 Elf_Phdr *phdr = &vnh->vnh_phdr; 1125 struct vnode *vp; 1126 int error; 1127 1128 /* 1129 * If an entry represents a vnode then write out a file handle. 1130 * 1131 * If we are checkpointing a checkpoint-restored program we do 1132 * NOT record the filehandle for the old checkpoint vnode (which 1133 * is mapped all over the place). Instead we rely on the fact 1134 * that a checkpoint-restored program does not mmap() the checkpt 1135 * vnode NOCORE, so its contents will be written out to the 1136 * new checkpoint file. This is necessary because the 'old' 1137 * checkpoint file is typically destroyed when a new one is created 1138 * and thus cannot be used to restore the new checkpoint. 1139 * 1140 * Theoretically we could create a chain of checkpoint files and 1141 * operate the checkpointing operation kinda like an incremental 1142 * checkpoint, but a checkpoint restore would then likely wind up 1143 * referencing many prior checkpoint files and that is a bit over 1144 * the top for the purpose of the checkpoint API. 1145 */ 1146 if (entry->object.vm_object->type == OBJT_VNODE) { 1147 vp = (struct vnode *)entry->object.vm_object->handle; 1148 if ((vp->v_flag & VCKPT) && curproc->p_textvp == vp) 1149 return (0); 1150 if (vnh == fpc->vnh_max) 1151 return (EINVAL); 1152 1153 if (vp->v_mount) 1154 vnh->vnh_fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid; 1155 error = VFS_VPTOFH(vp, &vnh->vnh_fh.fh_fid); 1156 if (error) { 1157 char *freepath, *fullpath; 1158 1159 /* 1160 * This is actually a relatively common occurance, 1161 * so don't spew on the console by default. 1162 */ 1163 if (vn_fullpath(curproc, vp, &fullpath, &freepath, 0)) { 1164 if (bootverbose) 1165 kprintf("Warning: coredump, error %d: cannot store file handle for vnode %p\n", error, vp); 1166 } else { 1167 if (bootverbose) 1168 kprintf("Warning: coredump, error %d: cannot store file handle for %s\n", error, fullpath); 1169 kfree(freepath, M_TEMP); 1170 } 1171 error = 0; 1172 } 1173 1174 phdr->p_type = PT_LOAD; 1175 phdr->p_offset = 0; /* not written to core */ 1176 phdr->p_vaddr = entry->start; 1177 phdr->p_paddr = 0; 1178 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start; 1179 phdr->p_align = PAGE_SIZE; 1180 phdr->p_flags = 0; 1181 if (entry->protection & VM_PROT_READ) 1182 phdr->p_flags |= PF_R; 1183 if (entry->protection & VM_PROT_WRITE) 1184 phdr->p_flags |= PF_W; 1185 if (entry->protection & VM_PROT_EXECUTE) 1186 phdr->p_flags |= PF_X; 1187 ++fpc->vnh; 1188 ++fpc->count; 1189 } 1190 return (0); 1191 } 1192 1193 /* 1194 * For each writable segment in the process's memory map, call the given 1195 * function with a pointer to the map entry and some arbitrary 1196 * caller-supplied data. 1197 */ 1198 static int 1199 each_segment(struct proc *p, segment_callback func, void *closure, int writable) 1200 { 1201 int error = 0; 1202 vm_map_t map = &p->p_vmspace->vm_map; 1203 vm_map_entry_t entry; 1204 1205 for (entry = map->header.next; error == 0 && entry != &map->header; 1206 entry = entry->next) { 1207 vm_object_t obj; 1208 vm_object_t lobj; 1209 vm_object_t tobj; 1210 1211 /* 1212 * Don't dump inaccessible mappings, deal with legacy 1213 * coredump mode. 1214 * 1215 * Note that read-only segments related to the elf binary 1216 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer 1217 * need to arbitrarily ignore such segments. 1218 */ 1219 if (elf_legacy_coredump) { 1220 if (writable && (entry->protection & VM_PROT_RW) != VM_PROT_RW) 1221 continue; 1222 } else { 1223 if (writable && (entry->protection & VM_PROT_ALL) == 0) 1224 continue; 1225 } 1226 1227 /* 1228 * Dont include memory segment in the coredump if 1229 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in 1230 * madvise(2). 1231 * 1232 * Currently we only dump normal VM object maps. We do 1233 * not dump submaps or virtual page tables. 1234 */ 1235 if (writable && (entry->eflags & MAP_ENTRY_NOCOREDUMP)) 1236 continue; 1237 if (entry->maptype != VM_MAPTYPE_NORMAL) 1238 continue; 1239 if ((obj = entry->object.vm_object) == NULL) 1240 continue; 1241 1242 /* 1243 * Find the bottom-most object, leaving the base object 1244 * and the bottom-most object held (but only one hold 1245 * if they happen to be the same). 1246 */ 1247 vm_object_hold_shared(obj); 1248 1249 lobj = obj; 1250 while (lobj && (tobj = lobj->backing_object) != NULL) { 1251 KKASSERT(tobj != obj); 1252 vm_object_hold_shared(tobj); 1253 if (tobj == lobj->backing_object) { 1254 if (lobj != obj) { 1255 vm_object_lock_swap(); 1256 vm_object_drop(lobj); 1257 } 1258 lobj = tobj; 1259 } else { 1260 vm_object_drop(tobj); 1261 } 1262 } 1263 1264 /* 1265 * The callback only applies to default, swap, or vnode 1266 * objects. Other types of objects such as memory-mapped 1267 * devices are ignored. 1268 */ 1269 if (lobj->type == OBJT_DEFAULT || lobj->type == OBJT_SWAP || 1270 lobj->type == OBJT_VNODE) { 1271 error = (*func)(entry, closure); 1272 } 1273 if (lobj != obj) 1274 vm_object_drop(lobj); 1275 vm_object_drop(obj); 1276 } 1277 return (error); 1278 } 1279 1280 static 1281 void * 1282 target_reserve(elf_buf_t target, size_t bytes, int *error) 1283 { 1284 void *res = NULL; 1285 1286 if (target->buf) { 1287 if (target->off + bytes > target->off_max) 1288 *error = EINVAL; 1289 else 1290 res = target->buf + target->off; 1291 } 1292 target->off += bytes; 1293 return (res); 1294 } 1295 1296 /* 1297 * Write the core file header to the file, including padding up to 1298 * the page boundary. 1299 */ 1300 static int 1301 __elfN(corehdr)(struct lwp *lp, int sig, struct file *fp, struct ucred *cred, 1302 int numsegs, elf_buf_t target) 1303 { 1304 int error; 1305 ssize_t nbytes; 1306 1307 /* 1308 * Fill in the header. The fp is passed so we can detect and flag 1309 * a checkpoint file pointer within the core file itself, because 1310 * it may not be restored from the same file handle. 1311 */ 1312 error = __elfN(puthdr)(lp, target, sig, WRITE, numsegs, fp); 1313 1314 /* Write it to the core file. */ 1315 if (error == 0) { 1316 error = fp_write(fp, target->buf, target->off, &nbytes, 1317 UIO_SYSSPACE); 1318 } 1319 return (error); 1320 } 1321 1322 static int 1323 __elfN(puthdr)(struct lwp *lp, elf_buf_t target, int sig, enum putmode mode, 1324 int numsegs, struct file *fp) 1325 { 1326 struct proc *p = lp->lwp_proc; 1327 int error = 0; 1328 size_t phoff; 1329 size_t noteoff; 1330 size_t notesz; 1331 Elf_Ehdr *ehdr; 1332 Elf_Phdr *phdr; 1333 1334 ehdr = target_reserve(target, sizeof(Elf_Ehdr), &error); 1335 1336 phoff = target->off; 1337 phdr = target_reserve(target, (numsegs + 1) * sizeof(Elf_Phdr), &error); 1338 1339 noteoff = target->off; 1340 if (error == 0) 1341 elf_putallnotes(lp, target, sig, mode); 1342 notesz = target->off - noteoff; 1343 1344 /* 1345 * put extra cruft for dumping process state here 1346 * - we really want it be before all the program 1347 * mappings 1348 * - we just need to update the offset accordingly 1349 * and GDB will be none the wiser. 1350 */ 1351 if (error == 0) 1352 error = elf_puttextvp(p, target); 1353 if (error == 0) 1354 error = elf_putsigs(lp, target); 1355 if (error == 0) 1356 error = elf_putfiles(p, target, fp); 1357 1358 /* 1359 * Align up to a page boundary for the program segments. The 1360 * actual data will be written to the outptu file, not to elf_buf_t, 1361 * so we do not have to do any further bounds checking. 1362 */ 1363 target->off = round_page(target->off); 1364 if (error == 0 && ehdr != NULL) { 1365 /* 1366 * Fill in the ELF header. 1367 */ 1368 ehdr->e_ident[EI_MAG0] = ELFMAG0; 1369 ehdr->e_ident[EI_MAG1] = ELFMAG1; 1370 ehdr->e_ident[EI_MAG2] = ELFMAG2; 1371 ehdr->e_ident[EI_MAG3] = ELFMAG3; 1372 ehdr->e_ident[EI_CLASS] = ELF_CLASS; 1373 ehdr->e_ident[EI_DATA] = ELF_DATA; 1374 ehdr->e_ident[EI_VERSION] = EV_CURRENT; 1375 ehdr->e_ident[EI_OSABI] = ELFOSABI_NONE; 1376 ehdr->e_ident[EI_ABIVERSION] = 0; 1377 ehdr->e_ident[EI_PAD] = 0; 1378 ehdr->e_type = ET_CORE; 1379 ehdr->e_machine = ELF_ARCH; 1380 ehdr->e_version = EV_CURRENT; 1381 ehdr->e_entry = 0; 1382 ehdr->e_phoff = phoff; 1383 ehdr->e_flags = 0; 1384 ehdr->e_ehsize = sizeof(Elf_Ehdr); 1385 ehdr->e_phentsize = sizeof(Elf_Phdr); 1386 ehdr->e_phnum = numsegs + 1; 1387 ehdr->e_shentsize = sizeof(Elf_Shdr); 1388 ehdr->e_shnum = 0; 1389 ehdr->e_shstrndx = SHN_UNDEF; 1390 } 1391 if (error == 0 && phdr != NULL) { 1392 /* 1393 * Fill in the program header entries. 1394 */ 1395 struct phdr_closure phc; 1396 1397 /* The note segement. */ 1398 phdr->p_type = PT_NOTE; 1399 phdr->p_offset = noteoff; 1400 phdr->p_vaddr = 0; 1401 phdr->p_paddr = 0; 1402 phdr->p_filesz = notesz; 1403 phdr->p_memsz = 0; 1404 phdr->p_flags = 0; 1405 phdr->p_align = 0; 1406 ++phdr; 1407 1408 /* All the writable segments from the program. */ 1409 phc.phdr = phdr; 1410 phc.phdr_max = phdr + numsegs; 1411 phc.offset = target->off; 1412 each_segment(p, cb_put_phdr, &phc, 1); 1413 } 1414 return (error); 1415 } 1416 1417 /* 1418 * Append core dump notes to target ELF buffer or simply update target size 1419 * if dryrun selected. 1420 */ 1421 static int 1422 elf_putallnotes(struct lwp *corelp, elf_buf_t target, int sig, 1423 enum putmode mode) 1424 { 1425 struct proc *p = corelp->lwp_proc; 1426 int error; 1427 struct { 1428 prstatus_t status; 1429 prfpregset_t fpregs; 1430 prpsinfo_t psinfo; 1431 } *tmpdata; 1432 prstatus_t *status; 1433 prfpregset_t *fpregs; 1434 prpsinfo_t *psinfo; 1435 struct lwp *lp; 1436 1437 /* 1438 * Allocate temporary storage for notes on heap to avoid stack overflow. 1439 */ 1440 if (mode != DRYRUN) { 1441 tmpdata = kmalloc(sizeof(*tmpdata), M_TEMP, M_ZERO | M_WAITOK); 1442 status = &tmpdata->status; 1443 fpregs = &tmpdata->fpregs; 1444 psinfo = &tmpdata->psinfo; 1445 } else { 1446 tmpdata = NULL; 1447 status = NULL; 1448 fpregs = NULL; 1449 psinfo = NULL; 1450 } 1451 1452 /* 1453 * Append LWP-agnostic note. 1454 */ 1455 if (mode != DRYRUN) { 1456 psinfo->pr_version = PRPSINFO_VERSION; 1457 psinfo->pr_psinfosz = sizeof(prpsinfo_t); 1458 strlcpy(psinfo->pr_fname, p->p_comm, 1459 sizeof(psinfo->pr_fname)); 1460 /* 1461 * XXX - We don't fill in the command line arguments 1462 * properly yet. 1463 */ 1464 strlcpy(psinfo->pr_psargs, p->p_comm, 1465 sizeof(psinfo->pr_psargs)); 1466 } 1467 error = 1468 __elfN(putnote)(target, "CORE", NT_PRPSINFO, psinfo, sizeof *psinfo); 1469 if (error) 1470 goto exit; 1471 1472 /* 1473 * Append first note for LWP that triggered core so that it is 1474 * the selected one when the debugger starts. 1475 */ 1476 if (mode != DRYRUN) { 1477 status->pr_version = PRSTATUS_VERSION; 1478 status->pr_statussz = sizeof(prstatus_t); 1479 status->pr_gregsetsz = sizeof(gregset_t); 1480 status->pr_fpregsetsz = sizeof(fpregset_t); 1481 status->pr_osreldate = osreldate; 1482 status->pr_cursig = sig; 1483 /* 1484 * XXX GDB needs unique pr_pid for each LWP and does not 1485 * not support pr_pid==0 but lwp_tid can be 0, so hack unique 1486 * value. 1487 */ 1488 status->pr_pid = corelp->lwp_tid; 1489 fill_regs(corelp, &status->pr_reg); 1490 fill_fpregs(corelp, fpregs); 1491 } 1492 error = 1493 __elfN(putnote)(target, "CORE", NT_PRSTATUS, status, sizeof *status); 1494 if (error) 1495 goto exit; 1496 error = 1497 __elfN(putnote)(target, "CORE", NT_FPREGSET, fpregs, sizeof *fpregs); 1498 if (error) 1499 goto exit; 1500 1501 /* 1502 * Then append notes for other LWPs. 1503 */ 1504 FOREACH_LWP_IN_PROC(lp, p) { 1505 if (lp == corelp) 1506 continue; 1507 /* skip lwps being created */ 1508 if (lp->lwp_thread == NULL) 1509 continue; 1510 if (mode != DRYRUN) { 1511 status->pr_pid = lp->lwp_tid; 1512 fill_regs(lp, &status->pr_reg); 1513 fill_fpregs(lp, fpregs); 1514 } 1515 error = __elfN(putnote)(target, "CORE", NT_PRSTATUS, 1516 status, sizeof *status); 1517 if (error) 1518 goto exit; 1519 error = __elfN(putnote)(target, "CORE", NT_FPREGSET, 1520 fpregs, sizeof *fpregs); 1521 if (error) 1522 goto exit; 1523 } 1524 1525 exit: 1526 if (tmpdata != NULL) 1527 kfree(tmpdata, M_TEMP); 1528 return (error); 1529 } 1530 1531 /* 1532 * Generate a note sub-structure. 1533 * 1534 * NOTE: 4-byte alignment. 1535 */ 1536 static int 1537 __elfN(putnote)(elf_buf_t target, const char *name, int type, 1538 const void *desc, size_t descsz) 1539 { 1540 int error = 0; 1541 char *dst; 1542 Elf_Note note; 1543 1544 note.n_namesz = strlen(name) + 1; 1545 note.n_descsz = descsz; 1546 note.n_type = type; 1547 dst = target_reserve(target, sizeof(note), &error); 1548 if (dst != NULL) 1549 bcopy(¬e, dst, sizeof note); 1550 dst = target_reserve(target, note.n_namesz, &error); 1551 if (dst != NULL) 1552 bcopy(name, dst, note.n_namesz); 1553 target->off = roundup2(target->off, sizeof(Elf_Word)); 1554 dst = target_reserve(target, note.n_descsz, &error); 1555 if (dst != NULL) 1556 bcopy(desc, dst, note.n_descsz); 1557 target->off = roundup2(target->off, sizeof(Elf_Word)); 1558 return (error); 1559 } 1560 1561 1562 static int 1563 elf_putsigs(struct lwp *lp, elf_buf_t target) 1564 { 1565 /* XXX lwp handle more than one lwp */ 1566 struct proc *p = lp->lwp_proc; 1567 int error = 0; 1568 struct ckpt_siginfo *csi; 1569 1570 csi = target_reserve(target, sizeof(struct ckpt_siginfo), &error); 1571 if (csi) { 1572 csi->csi_ckptpisz = sizeof(struct ckpt_siginfo); 1573 bcopy(p->p_sigacts, &csi->csi_sigacts, sizeof(*p->p_sigacts)); 1574 bcopy(&p->p_realtimer, &csi->csi_itimerval, sizeof(struct itimerval)); 1575 bcopy(&lp->lwp_sigmask, &csi->csi_sigmask, 1576 sizeof(sigset_t)); 1577 csi->csi_sigparent = p->p_sigparent; 1578 } 1579 return (error); 1580 } 1581 1582 static int 1583 elf_putfiles(struct proc *p, elf_buf_t target, struct file *ckfp) 1584 { 1585 int error = 0; 1586 int i; 1587 struct ckpt_filehdr *cfh = NULL; 1588 struct ckpt_fileinfo *cfi; 1589 struct file *fp; 1590 struct vnode *vp; 1591 /* 1592 * the duplicated loop is gross, but it was the only way 1593 * to eliminate uninitialized variable warnings 1594 */ 1595 cfh = target_reserve(target, sizeof(struct ckpt_filehdr), &error); 1596 if (cfh) { 1597 cfh->cfh_nfiles = 0; 1598 } 1599 1600 /* 1601 * ignore STDIN/STDERR/STDOUT. 1602 */ 1603 for (i = 3; error == 0 && i < p->p_fd->fd_nfiles; i++) { 1604 fp = holdfp(p->p_fd, i, -1); 1605 if (fp == NULL) 1606 continue; 1607 /* 1608 * XXX Only checkpoint vnodes for now. 1609 */ 1610 if (fp->f_type != DTYPE_VNODE) { 1611 fdrop(fp); 1612 continue; 1613 } 1614 cfi = target_reserve(target, sizeof(struct ckpt_fileinfo), 1615 &error); 1616 if (cfi == NULL) { 1617 fdrop(fp); 1618 continue; 1619 } 1620 cfi->cfi_index = -1; 1621 cfi->cfi_type = fp->f_type; 1622 cfi->cfi_flags = fp->f_flag; 1623 cfi->cfi_offset = fp->f_offset; 1624 cfi->cfi_ckflags = 0; 1625 1626 if (fp == ckfp) 1627 cfi->cfi_ckflags |= CKFIF_ISCKPTFD; 1628 /* f_count and f_msgcount should not be saved/restored */ 1629 /* XXX save cred info */ 1630 1631 switch(fp->f_type) { 1632 case DTYPE_VNODE: 1633 vp = (struct vnode *)fp->f_data; 1634 /* 1635 * it looks like a bug in ptrace is marking 1636 * a non-vnode as a vnode - until we find the 1637 * root cause this will at least prevent 1638 * further panics from truss 1639 */ 1640 if (vp == NULL || vp->v_mount == NULL) 1641 break; 1642 cfh->cfh_nfiles++; 1643 cfi->cfi_index = i; 1644 cfi->cfi_fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid; 1645 error = VFS_VPTOFH(vp, &cfi->cfi_fh.fh_fid); 1646 break; 1647 default: 1648 break; 1649 } 1650 fdrop(fp); 1651 } 1652 return (error); 1653 } 1654 1655 static int 1656 elf_puttextvp(struct proc *p, elf_buf_t target) 1657 { 1658 int error = 0; 1659 int *vn_count; 1660 struct fp_closure fpc; 1661 struct ckpt_vminfo *vminfo; 1662 1663 vminfo = target_reserve(target, sizeof(struct ckpt_vminfo), &error); 1664 if (vminfo != NULL) { 1665 vminfo->cvm_dsize = p->p_vmspace->vm_dsize; 1666 vminfo->cvm_tsize = p->p_vmspace->vm_tsize; 1667 vminfo->cvm_daddr = p->p_vmspace->vm_daddr; 1668 vminfo->cvm_taddr = p->p_vmspace->vm_taddr; 1669 } 1670 1671 fpc.count = 0; 1672 vn_count = target_reserve(target, sizeof(int), &error); 1673 if (target->buf != NULL) { 1674 fpc.vnh = (struct vn_hdr *)(target->buf + target->off); 1675 fpc.vnh_max = fpc.vnh + 1676 (target->off_max - target->off) / sizeof(struct vn_hdr); 1677 error = each_segment(p, cb_put_fp, &fpc, 0); 1678 if (vn_count) 1679 *vn_count = fpc.count; 1680 } else { 1681 error = each_segment(p, cb_fpcount_segment, &fpc.count, 0); 1682 } 1683 target->off += fpc.count * sizeof(struct vn_hdr); 1684 return (error); 1685 } 1686 1687 /* 1688 * Try to find the appropriate ABI-note section for checknote, 1689 * The entire image is searched if necessary, not only the first page. 1690 */ 1691 static boolean_t 1692 __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote, 1693 int32_t *osrel) 1694 { 1695 boolean_t valid_note_found; 1696 const Elf_Phdr *phdr, *pnote; 1697 const Elf_Ehdr *hdr; 1698 int i; 1699 1700 valid_note_found = FALSE; 1701 hdr = (const Elf_Ehdr *)imgp->image_header; 1702 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 1703 1704 for (i = 0; i < hdr->e_phnum; i++) { 1705 if (phdr[i].p_type == PT_NOTE) { 1706 pnote = &phdr[i]; 1707 valid_note_found = check_PT_NOTE (imgp, checknote, 1708 osrel, pnote); 1709 if (valid_note_found) 1710 break; 1711 } 1712 } 1713 return valid_note_found; 1714 } 1715 1716 /* 1717 * Be careful not to create new overflow conditions when checking 1718 * for overflow. 1719 */ 1720 static boolean_t 1721 note_overflow(const Elf_Note *note, size_t maxsize) 1722 { 1723 if (sizeof(*note) > maxsize) 1724 return TRUE; 1725 if (note->n_namesz > maxsize - sizeof(*note)) 1726 return TRUE; 1727 return FALSE; 1728 } 1729 1730 static boolean_t 1731 hdr_overflow(__ElfN(Off) off_beg, __ElfN(Size) size) 1732 { 1733 __ElfN(Off) off_end; 1734 1735 off_end = off_beg + size; 1736 if (off_end < off_beg) 1737 return TRUE; 1738 return FALSE; 1739 } 1740 1741 static boolean_t 1742 check_PT_NOTE(struct image_params *imgp, Elf_Brandnote *checknote, 1743 int32_t *osrel, const Elf_Phdr * pnote) 1744 { 1745 boolean_t limited_to_first_page; 1746 boolean_t found = FALSE; 1747 const Elf_Note *note, *note0, *note_end; 1748 const char *note_name; 1749 __ElfN(Off) noteloc, firstloc; 1750 __ElfN(Size) notesz, firstlen, endbyte; 1751 struct lwbuf *lwb; 1752 struct lwbuf lwb_cache; 1753 const char *page; 1754 char *data = NULL; 1755 int n; 1756 1757 if (hdr_overflow(pnote->p_offset, pnote->p_filesz)) 1758 return (FALSE); 1759 notesz = pnote->p_filesz; 1760 noteloc = pnote->p_offset; 1761 endbyte = noteloc + notesz; 1762 limited_to_first_page = noteloc < PAGE_SIZE && endbyte < PAGE_SIZE; 1763 1764 if (limited_to_first_page) { 1765 note = (const Elf_Note *)(imgp->image_header + noteloc); 1766 note_end = (const Elf_Note *)(imgp->image_header + endbyte); 1767 note0 = note; 1768 } else { 1769 firstloc = noteloc & PAGE_MASK; 1770 firstlen = PAGE_SIZE - firstloc; 1771 if (notesz < sizeof(Elf_Note) || notesz > PAGE_SIZE) 1772 return (FALSE); 1773 1774 lwb = &lwb_cache; 1775 if (exec_map_page(imgp, noteloc >> PAGE_SHIFT, &lwb, &page)) 1776 return (FALSE); 1777 if (firstlen < notesz) { /* crosses page boundary */ 1778 data = kmalloc(notesz, M_TEMP, M_WAITOK); 1779 bcopy(page + firstloc, data, firstlen); 1780 1781 exec_unmap_page(lwb); 1782 lwb = &lwb_cache; 1783 if (exec_map_page(imgp, (noteloc >> PAGE_SHIFT) + 1, 1784 &lwb, &page)) { 1785 kfree(data, M_TEMP); 1786 return (FALSE); 1787 } 1788 bcopy(page, data + firstlen, notesz - firstlen); 1789 note = note0 = (const Elf_Note *)(data); 1790 note_end = (const Elf_Note *)(data + notesz); 1791 } else { 1792 note = note0 = (const Elf_Note *)(page + firstloc); 1793 note_end = (const Elf_Note *)(page + firstloc + 1794 firstlen); 1795 } 1796 } 1797 1798 for (n = 0; n < 100 && note >= note0 && note < note_end; n++) { 1799 if (!aligned(note, Elf32_Addr)) 1800 break; 1801 if (note_overflow(note, (const char *)note_end - 1802 (const char *)note)) { 1803 break; 1804 } 1805 note_name = (const char *)(note + 1); 1806 1807 if (note->n_namesz == checknote->hdr.n_namesz 1808 && note->n_descsz == checknote->hdr.n_descsz 1809 && note->n_type == checknote->hdr.n_type 1810 && (strncmp(checknote->vendor, note_name, 1811 checknote->hdr.n_namesz) == 0)) { 1812 /* Fetch osreldata from ABI.note-tag */ 1813 if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 && 1814 checknote->trans_osrel != NULL) 1815 checknote->trans_osrel(note, osrel); 1816 found = TRUE; 1817 break; 1818 } 1819 note = (const Elf_Note *)((const char *)(note + 1) + 1820 roundup2(note->n_namesz, sizeof(Elf32_Addr)) + 1821 roundup2(note->n_descsz, sizeof(Elf32_Addr))); 1822 } 1823 1824 if (!limited_to_first_page) { 1825 if (data != NULL) 1826 kfree(data, M_TEMP); 1827 exec_unmap_page(lwb); 1828 } 1829 return (found); 1830 } 1831 1832 /* 1833 * The interpreter program header may be located beyond the first page, so 1834 * regardless of its location, a copy of the interpreter path is created so 1835 * that it may be safely referenced by the calling function in all case. The 1836 * memory is allocated by calling function, and the copying is done here. 1837 */ 1838 static boolean_t 1839 extract_interpreter(struct image_params *imgp, const Elf_Phdr *pinterpreter, 1840 char *data) 1841 { 1842 boolean_t limited_to_first_page; 1843 const boolean_t result_success = FALSE; 1844 const boolean_t result_failure = TRUE; 1845 __ElfN(Off) pathloc, firstloc; 1846 __ElfN(Size) pathsz, firstlen, endbyte; 1847 struct lwbuf *lwb; 1848 struct lwbuf lwb_cache; 1849 const char *page; 1850 1851 if (hdr_overflow(pinterpreter->p_offset, pinterpreter->p_filesz)) 1852 return (result_failure); 1853 pathsz = pinterpreter->p_filesz; 1854 pathloc = pinterpreter->p_offset; 1855 endbyte = pathloc + pathsz; 1856 1857 limited_to_first_page = pathloc < PAGE_SIZE && endbyte < PAGE_SIZE; 1858 if (limited_to_first_page) { 1859 bcopy(imgp->image_header + pathloc, data, pathsz); 1860 return (result_success); 1861 } 1862 1863 firstloc = pathloc & PAGE_MASK; 1864 firstlen = PAGE_SIZE - firstloc; 1865 1866 lwb = &lwb_cache; 1867 if (exec_map_page(imgp, pathloc >> PAGE_SHIFT, &lwb, &page)) 1868 return (result_failure); 1869 1870 if (firstlen < pathsz) { /* crosses page boundary */ 1871 bcopy(page + firstloc, data, firstlen); 1872 1873 exec_unmap_page(lwb); 1874 lwb = &lwb_cache; 1875 if (exec_map_page(imgp, (pathloc >> PAGE_SHIFT) + 1, &lwb, 1876 &page)) 1877 return (result_failure); 1878 bcopy(page, data + firstlen, pathsz - firstlen); 1879 } else 1880 bcopy(page + firstloc, data, pathsz); 1881 1882 exec_unmap_page(lwb); 1883 return (result_success); 1884 } 1885 1886 static boolean_t 1887 __elfN(bsd_trans_osrel)(const Elf_Note *note, int32_t *osrel) 1888 { 1889 uintptr_t p; 1890 1891 p = (uintptr_t)(note + 1); 1892 p += roundup2(note->n_namesz, sizeof(Elf32_Addr)); 1893 *osrel = *(const int32_t *)(p); 1894 1895 return (TRUE); 1896 } 1897 1898 /* 1899 * Tell kern_execve.c about it, with a little help from the linker. 1900 */ 1901 #if defined(__x86_64__) 1902 static struct execsw elf_execsw = {exec_elf64_imgact, "ELF64"}; 1903 EXEC_SET_ORDERED(elf64, elf_execsw, SI_ORDER_FIRST); 1904 #else /* i386 assumed */ 1905 static struct execsw elf_execsw = {exec_elf32_imgact, "ELF32"}; 1906 EXEC_SET_ORDERED(elf32, elf_execsw, SI_ORDER_FIRST); 1907 #endif 1908 1909 static vm_prot_t 1910 __elfN(trans_prot)(Elf_Word flags) 1911 { 1912 vm_prot_t prot; 1913 1914 prot = 0; 1915 if (flags & PF_X) 1916 prot |= VM_PROT_EXECUTE; 1917 if (flags & PF_W) 1918 prot |= VM_PROT_WRITE; 1919 if (flags & PF_R) 1920 prot |= VM_PROT_READ; 1921 return (prot); 1922 } 1923 1924 static Elf_Word 1925 __elfN(untrans_prot)(vm_prot_t prot) 1926 { 1927 Elf_Word flags; 1928 1929 flags = 0; 1930 if (prot & VM_PROT_EXECUTE) 1931 flags |= PF_X; 1932 if (prot & VM_PROT_READ) 1933 flags |= PF_R; 1934 if (prot & VM_PROT_WRITE) 1935 flags |= PF_W; 1936 return (flags); 1937 } 1938 1939 static u_long 1940 pie_base_hint(struct proc *p) 1941 { 1942 u_long base; 1943 1944 if (elf_pie_base_mmap) 1945 base = vm_map_hint(p, 0, VM_PROT_READ | VM_PROT_EXECUTE); 1946 else 1947 base = ET_DYN_LOAD_ADDR; 1948 return base; 1949 } 1950