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 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include "opt_capsicum.h" 35 #include "opt_compat.h" 36 #include "opt_core.h" 37 38 #include <sys/param.h> 39 #include <sys/capsicum.h> 40 #include <sys/exec.h> 41 #include <sys/fcntl.h> 42 #include <sys/imgact.h> 43 #include <sys/imgact_elf.h> 44 #include <sys/jail.h> 45 #include <sys/kernel.h> 46 #include <sys/lock.h> 47 #include <sys/malloc.h> 48 #include <sys/mount.h> 49 #include <sys/mman.h> 50 #include <sys/namei.h> 51 #include <sys/pioctl.h> 52 #include <sys/proc.h> 53 #include <sys/procfs.h> 54 #include <sys/racct.h> 55 #include <sys/resourcevar.h> 56 #include <sys/rwlock.h> 57 #include <sys/sbuf.h> 58 #include <sys/sf_buf.h> 59 #include <sys/smp.h> 60 #include <sys/systm.h> 61 #include <sys/signalvar.h> 62 #include <sys/stat.h> 63 #include <sys/sx.h> 64 #include <sys/syscall.h> 65 #include <sys/sysctl.h> 66 #include <sys/sysent.h> 67 #include <sys/vnode.h> 68 #include <sys/syslog.h> 69 #include <sys/eventhandler.h> 70 #include <sys/user.h> 71 72 #include <net/zlib.h> 73 74 #include <vm/vm.h> 75 #include <vm/vm_kern.h> 76 #include <vm/vm_param.h> 77 #include <vm/pmap.h> 78 #include <vm/vm_map.h> 79 #include <vm/vm_object.h> 80 #include <vm/vm_extern.h> 81 82 #include <machine/elf.h> 83 #include <machine/md_var.h> 84 85 #define ELF_NOTE_ROUNDSIZE 4 86 #define OLD_EI_BRAND 8 87 88 static int __elfN(check_header)(const Elf_Ehdr *hdr); 89 static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp, 90 const char *interp, int interp_name_len, int32_t *osrel); 91 static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr, 92 u_long *entry, size_t pagesize); 93 static int __elfN(load_section)(struct image_params *imgp, vm_offset_t offset, 94 caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot, 95 size_t pagesize); 96 static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp); 97 static boolean_t __elfN(freebsd_trans_osrel)(const Elf_Note *note, 98 int32_t *osrel); 99 static boolean_t kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel); 100 static boolean_t __elfN(check_note)(struct image_params *imgp, 101 Elf_Brandnote *checknote, int32_t *osrel); 102 static vm_prot_t __elfN(trans_prot)(Elf_Word); 103 static Elf_Word __elfN(untrans_prot)(vm_prot_t); 104 105 SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0, 106 ""); 107 108 #ifdef COMPRESS_USER_CORES 109 static int compress_core(gzFile, char *, char *, unsigned int, 110 struct thread * td); 111 #endif 112 #define CORE_BUF_SIZE (16 * 1024) 113 114 int __elfN(fallback_brand) = -1; 115 SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, 116 fallback_brand, CTLFLAG_RWTUN, &__elfN(fallback_brand), 0, 117 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort"); 118 119 static int elf_legacy_coredump = 0; 120 SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW, 121 &elf_legacy_coredump, 0, ""); 122 123 int __elfN(nxstack) = 124 #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */ 125 1; 126 #else 127 0; 128 #endif 129 SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, 130 nxstack, CTLFLAG_RW, &__elfN(nxstack), 0, 131 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack"); 132 133 #if __ELF_WORD_SIZE == 32 134 #if defined(__amd64__) 135 int i386_read_exec = 0; 136 SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0, 137 "enable execution from readable segments"); 138 #endif 139 #endif 140 141 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS]; 142 143 #define trunc_page_ps(va, ps) ((va) & ~(ps - 1)) 144 #define round_page_ps(va, ps) (((va) + (ps - 1)) & ~(ps - 1)) 145 #define aligned(a, t) (trunc_page_ps((u_long)(a), sizeof(t)) == (u_long)(a)) 146 147 static const char FREEBSD_ABI_VENDOR[] = "FreeBSD"; 148 149 Elf_Brandnote __elfN(freebsd_brandnote) = { 150 .hdr.n_namesz = sizeof(FREEBSD_ABI_VENDOR), 151 .hdr.n_descsz = sizeof(int32_t), 152 .hdr.n_type = 1, 153 .vendor = FREEBSD_ABI_VENDOR, 154 .flags = BN_TRANSLATE_OSREL, 155 .trans_osrel = __elfN(freebsd_trans_osrel) 156 }; 157 158 static boolean_t 159 __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel) 160 { 161 uintptr_t p; 162 163 p = (uintptr_t)(note + 1); 164 p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE); 165 *osrel = *(const int32_t *)(p); 166 167 return (TRUE); 168 } 169 170 static const char GNU_ABI_VENDOR[] = "GNU"; 171 static int GNU_KFREEBSD_ABI_DESC = 3; 172 173 Elf_Brandnote __elfN(kfreebsd_brandnote) = { 174 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 175 .hdr.n_descsz = 16, /* XXX at least 16 */ 176 .hdr.n_type = 1, 177 .vendor = GNU_ABI_VENDOR, 178 .flags = BN_TRANSLATE_OSREL, 179 .trans_osrel = kfreebsd_trans_osrel 180 }; 181 182 static boolean_t 183 kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel) 184 { 185 const Elf32_Word *desc; 186 uintptr_t p; 187 188 p = (uintptr_t)(note + 1); 189 p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE); 190 191 desc = (const Elf32_Word *)p; 192 if (desc[0] != GNU_KFREEBSD_ABI_DESC) 193 return (FALSE); 194 195 /* 196 * Debian GNU/kFreeBSD embed the earliest compatible kernel version 197 * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way. 198 */ 199 *osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3]; 200 201 return (TRUE); 202 } 203 204 int 205 __elfN(insert_brand_entry)(Elf_Brandinfo *entry) 206 { 207 int i; 208 209 for (i = 0; i < MAX_BRANDS; i++) { 210 if (elf_brand_list[i] == NULL) { 211 elf_brand_list[i] = entry; 212 break; 213 } 214 } 215 if (i == MAX_BRANDS) { 216 printf("WARNING: %s: could not insert brandinfo entry: %p\n", 217 __func__, entry); 218 return (-1); 219 } 220 return (0); 221 } 222 223 int 224 __elfN(remove_brand_entry)(Elf_Brandinfo *entry) 225 { 226 int i; 227 228 for (i = 0; i < MAX_BRANDS; i++) { 229 if (elf_brand_list[i] == entry) { 230 elf_brand_list[i] = NULL; 231 break; 232 } 233 } 234 if (i == MAX_BRANDS) 235 return (-1); 236 return (0); 237 } 238 239 int 240 __elfN(brand_inuse)(Elf_Brandinfo *entry) 241 { 242 struct proc *p; 243 int rval = FALSE; 244 245 sx_slock(&allproc_lock); 246 FOREACH_PROC_IN_SYSTEM(p) { 247 if (p->p_sysent == entry->sysvec) { 248 rval = TRUE; 249 break; 250 } 251 } 252 sx_sunlock(&allproc_lock); 253 254 return (rval); 255 } 256 257 static Elf_Brandinfo * 258 __elfN(get_brandinfo)(struct image_params *imgp, const char *interp, 259 int interp_name_len, int32_t *osrel) 260 { 261 const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header; 262 Elf_Brandinfo *bi; 263 boolean_t ret; 264 int i; 265 266 /* 267 * We support four types of branding -- (1) the ELF EI_OSABI field 268 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string 269 * branding w/in the ELF header, (3) path of the `interp_path' 270 * field, and (4) the ".note.ABI-tag" ELF section. 271 */ 272 273 /* Look for an ".note.ABI-tag" ELF section */ 274 for (i = 0; i < MAX_BRANDS; i++) { 275 bi = elf_brand_list[i]; 276 if (bi == NULL) 277 continue; 278 if (hdr->e_machine == bi->machine && (bi->flags & 279 (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) { 280 ret = __elfN(check_note)(imgp, bi->brand_note, osrel); 281 if (ret) 282 return (bi); 283 } 284 } 285 286 /* If the executable has a brand, search for it in the brand list. */ 287 for (i = 0; i < MAX_BRANDS; i++) { 288 bi = elf_brand_list[i]; 289 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) 290 continue; 291 if (hdr->e_machine == bi->machine && 292 (hdr->e_ident[EI_OSABI] == bi->brand || 293 strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND], 294 bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0)) 295 return (bi); 296 } 297 298 /* No known brand, see if the header is recognized by any brand */ 299 for (i = 0; i < MAX_BRANDS; i++) { 300 bi = elf_brand_list[i]; 301 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY || 302 bi->header_supported == NULL) 303 continue; 304 if (hdr->e_machine == bi->machine) { 305 ret = bi->header_supported(imgp); 306 if (ret) 307 return (bi); 308 } 309 } 310 311 /* Lacking a known brand, search for a recognized interpreter. */ 312 if (interp != NULL) { 313 for (i = 0; i < MAX_BRANDS; i++) { 314 bi = elf_brand_list[i]; 315 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) 316 continue; 317 if (hdr->e_machine == bi->machine && 318 /* ELF image p_filesz includes terminating zero */ 319 strlen(bi->interp_path) + 1 == interp_name_len && 320 strncmp(interp, bi->interp_path, interp_name_len) 321 == 0) 322 return (bi); 323 } 324 } 325 326 /* Lacking a recognized interpreter, try the default brand */ 327 for (i = 0; i < MAX_BRANDS; i++) { 328 bi = elf_brand_list[i]; 329 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) 330 continue; 331 if (hdr->e_machine == bi->machine && 332 __elfN(fallback_brand) == bi->brand) 333 return (bi); 334 } 335 return (NULL); 336 } 337 338 static int 339 __elfN(check_header)(const Elf_Ehdr *hdr) 340 { 341 Elf_Brandinfo *bi; 342 int i; 343 344 if (!IS_ELF(*hdr) || 345 hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 346 hdr->e_ident[EI_DATA] != ELF_TARG_DATA || 347 hdr->e_ident[EI_VERSION] != EV_CURRENT || 348 hdr->e_phentsize != sizeof(Elf_Phdr) || 349 hdr->e_version != ELF_TARG_VER) 350 return (ENOEXEC); 351 352 /* 353 * Make sure we have at least one brand for this machine. 354 */ 355 356 for (i = 0; i < MAX_BRANDS; i++) { 357 bi = elf_brand_list[i]; 358 if (bi != NULL && bi->machine == hdr->e_machine) 359 break; 360 } 361 if (i == MAX_BRANDS) 362 return (ENOEXEC); 363 364 return (0); 365 } 366 367 static int 368 __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 369 vm_offset_t start, vm_offset_t end, vm_prot_t prot) 370 { 371 struct sf_buf *sf; 372 int error; 373 vm_offset_t off; 374 375 /* 376 * Create the page if it doesn't exist yet. Ignore errors. 377 */ 378 vm_map_lock(map); 379 vm_map_insert(map, NULL, 0, trunc_page(start), round_page(end), 380 VM_PROT_ALL, VM_PROT_ALL, 0); 381 vm_map_unlock(map); 382 383 /* 384 * Find the page from the underlying object. 385 */ 386 if (object) { 387 sf = vm_imgact_map_page(object, offset); 388 if (sf == NULL) 389 return (KERN_FAILURE); 390 off = offset - trunc_page(offset); 391 error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start, 392 end - start); 393 vm_imgact_unmap_page(sf); 394 if (error) { 395 return (KERN_FAILURE); 396 } 397 } 398 399 return (KERN_SUCCESS); 400 } 401 402 static int 403 __elfN(map_insert)(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 404 vm_offset_t start, vm_offset_t end, vm_prot_t prot, int cow) 405 { 406 struct sf_buf *sf; 407 vm_offset_t off; 408 vm_size_t sz; 409 int error, rv; 410 411 if (start != trunc_page(start)) { 412 rv = __elfN(map_partial)(map, object, offset, start, 413 round_page(start), prot); 414 if (rv) 415 return (rv); 416 offset += round_page(start) - start; 417 start = round_page(start); 418 } 419 if (end != round_page(end)) { 420 rv = __elfN(map_partial)(map, object, offset + 421 trunc_page(end) - start, trunc_page(end), end, prot); 422 if (rv) 423 return (rv); 424 end = trunc_page(end); 425 } 426 if (end > start) { 427 if (offset & PAGE_MASK) { 428 /* 429 * The mapping is not page aligned. This means we have 430 * to copy the data. Sigh. 431 */ 432 rv = vm_map_find(map, NULL, 0, &start, end - start, 0, 433 VMFS_NO_SPACE, prot | VM_PROT_WRITE, VM_PROT_ALL, 434 0); 435 if (rv) 436 return (rv); 437 if (object == NULL) 438 return (KERN_SUCCESS); 439 for (; start < end; start += sz) { 440 sf = vm_imgact_map_page(object, offset); 441 if (sf == NULL) 442 return (KERN_FAILURE); 443 off = offset - trunc_page(offset); 444 sz = end - start; 445 if (sz > PAGE_SIZE - off) 446 sz = PAGE_SIZE - off; 447 error = copyout((caddr_t)sf_buf_kva(sf) + off, 448 (caddr_t)start, sz); 449 vm_imgact_unmap_page(sf); 450 if (error) { 451 return (KERN_FAILURE); 452 } 453 offset += sz; 454 } 455 rv = KERN_SUCCESS; 456 } else { 457 vm_object_reference(object); 458 vm_map_lock(map); 459 rv = vm_map_insert(map, object, offset, start, end, 460 prot, VM_PROT_ALL, cow); 461 vm_map_unlock(map); 462 if (rv != KERN_SUCCESS) 463 vm_object_deallocate(object); 464 } 465 return (rv); 466 } else { 467 return (KERN_SUCCESS); 468 } 469 } 470 471 static int 472 __elfN(load_section)(struct image_params *imgp, vm_offset_t offset, 473 caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot, 474 size_t pagesize) 475 { 476 struct sf_buf *sf; 477 size_t map_len; 478 vm_map_t map; 479 vm_object_t object; 480 vm_offset_t map_addr; 481 int error, rv, cow; 482 size_t copy_len; 483 vm_offset_t file_addr; 484 485 /* 486 * It's necessary to fail if the filsz + offset taken from the 487 * header is greater than the actual file pager object's size. 488 * If we were to allow this, then the vm_map_find() below would 489 * walk right off the end of the file object and into the ether. 490 * 491 * While I'm here, might as well check for something else that 492 * is invalid: filsz cannot be greater than memsz. 493 */ 494 if ((off_t)filsz + offset > imgp->attr->va_size || filsz > memsz) { 495 uprintf("elf_load_section: truncated ELF file\n"); 496 return (ENOEXEC); 497 } 498 499 object = imgp->object; 500 map = &imgp->proc->p_vmspace->vm_map; 501 map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize); 502 file_addr = trunc_page_ps(offset, pagesize); 503 504 /* 505 * We have two choices. We can either clear the data in the last page 506 * of an oversized mapping, or we can start the anon mapping a page 507 * early and copy the initialized data into that first page. We 508 * choose the second.. 509 */ 510 if (memsz > filsz) 511 map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr; 512 else 513 map_len = round_page_ps(offset + filsz, pagesize) - file_addr; 514 515 if (map_len != 0) { 516 /* cow flags: don't dump readonly sections in core */ 517 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT | 518 (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP); 519 520 rv = __elfN(map_insert)(map, 521 object, 522 file_addr, /* file offset */ 523 map_addr, /* virtual start */ 524 map_addr + map_len,/* virtual end */ 525 prot, 526 cow); 527 if (rv != KERN_SUCCESS) 528 return (EINVAL); 529 530 /* we can stop now if we've covered it all */ 531 if (memsz == filsz) { 532 return (0); 533 } 534 } 535 536 537 /* 538 * We have to get the remaining bit of the file into the first part 539 * of the oversized map segment. This is normally because the .data 540 * segment in the file is extended to provide bss. It's a neat idea 541 * to try and save a page, but it's a pain in the behind to implement. 542 */ 543 copy_len = (offset + filsz) - trunc_page_ps(offset + filsz, pagesize); 544 map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize); 545 map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) - 546 map_addr; 547 548 /* This had damn well better be true! */ 549 if (map_len != 0) { 550 rv = __elfN(map_insert)(map, NULL, 0, map_addr, map_addr + 551 map_len, VM_PROT_ALL, 0); 552 if (rv != KERN_SUCCESS) { 553 return (EINVAL); 554 } 555 } 556 557 if (copy_len != 0) { 558 vm_offset_t off; 559 560 sf = vm_imgact_map_page(object, offset + filsz); 561 if (sf == NULL) 562 return (EIO); 563 564 /* send the page fragment to user space */ 565 off = trunc_page_ps(offset + filsz, pagesize) - 566 trunc_page(offset + filsz); 567 error = copyout((caddr_t)sf_buf_kva(sf) + off, 568 (caddr_t)map_addr, copy_len); 569 vm_imgact_unmap_page(sf); 570 if (error) { 571 return (error); 572 } 573 } 574 575 /* 576 * set it to the specified protection. 577 * XXX had better undo the damage from pasting over the cracks here! 578 */ 579 vm_map_protect(map, trunc_page(map_addr), round_page(map_addr + 580 map_len), prot, FALSE); 581 582 return (0); 583 } 584 585 /* 586 * Load the file "file" into memory. It may be either a shared object 587 * or an executable. 588 * 589 * The "addr" reference parameter is in/out. On entry, it specifies 590 * the address where a shared object should be loaded. If the file is 591 * an executable, this value is ignored. On exit, "addr" specifies 592 * where the file was actually loaded. 593 * 594 * The "entry" reference parameter is out only. On exit, it specifies 595 * the entry point for the loaded file. 596 */ 597 static int 598 __elfN(load_file)(struct proc *p, const char *file, u_long *addr, 599 u_long *entry, size_t pagesize) 600 { 601 struct { 602 struct nameidata nd; 603 struct vattr attr; 604 struct image_params image_params; 605 } *tempdata; 606 const Elf_Ehdr *hdr = NULL; 607 const Elf_Phdr *phdr = NULL; 608 struct nameidata *nd; 609 struct vattr *attr; 610 struct image_params *imgp; 611 vm_prot_t prot; 612 u_long rbase; 613 u_long base_addr = 0; 614 int error, i, numsegs; 615 616 #ifdef CAPABILITY_MODE 617 /* 618 * XXXJA: This check can go away once we are sufficiently confident 619 * that the checks in namei() are correct. 620 */ 621 if (IN_CAPABILITY_MODE(curthread)) 622 return (ECAPMODE); 623 #endif 624 625 tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK); 626 nd = &tempdata->nd; 627 attr = &tempdata->attr; 628 imgp = &tempdata->image_params; 629 630 /* 631 * Initialize part of the common data 632 */ 633 imgp->proc = p; 634 imgp->attr = attr; 635 imgp->firstpage = NULL; 636 imgp->image_header = NULL; 637 imgp->object = NULL; 638 imgp->execlabel = NULL; 639 640 NDINIT(nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE, file, curthread); 641 if ((error = namei(nd)) != 0) { 642 nd->ni_vp = NULL; 643 goto fail; 644 } 645 NDFREE(nd, NDF_ONLY_PNBUF); 646 imgp->vp = nd->ni_vp; 647 648 /* 649 * Check permissions, modes, uid, etc on the file, and "open" it. 650 */ 651 error = exec_check_permissions(imgp); 652 if (error) 653 goto fail; 654 655 error = exec_map_first_page(imgp); 656 if (error) 657 goto fail; 658 659 /* 660 * Also make certain that the interpreter stays the same, so set 661 * its VV_TEXT flag, too. 662 */ 663 VOP_SET_TEXT(nd->ni_vp); 664 665 imgp->object = nd->ni_vp->v_object; 666 667 hdr = (const Elf_Ehdr *)imgp->image_header; 668 if ((error = __elfN(check_header)(hdr)) != 0) 669 goto fail; 670 if (hdr->e_type == ET_DYN) 671 rbase = *addr; 672 else if (hdr->e_type == ET_EXEC) 673 rbase = 0; 674 else { 675 error = ENOEXEC; 676 goto fail; 677 } 678 679 /* Only support headers that fit within first page for now */ 680 if ((hdr->e_phoff > PAGE_SIZE) || 681 (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) { 682 error = ENOEXEC; 683 goto fail; 684 } 685 686 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 687 if (!aligned(phdr, Elf_Addr)) { 688 error = ENOEXEC; 689 goto fail; 690 } 691 692 for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) { 693 if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) { 694 /* Loadable segment */ 695 prot = __elfN(trans_prot)(phdr[i].p_flags); 696 error = __elfN(load_section)(imgp, phdr[i].p_offset, 697 (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase, 698 phdr[i].p_memsz, phdr[i].p_filesz, prot, pagesize); 699 if (error != 0) 700 goto fail; 701 /* 702 * Establish the base address if this is the 703 * first segment. 704 */ 705 if (numsegs == 0) 706 base_addr = trunc_page(phdr[i].p_vaddr + 707 rbase); 708 numsegs++; 709 } 710 } 711 *addr = base_addr; 712 *entry = (unsigned long)hdr->e_entry + rbase; 713 714 fail: 715 if (imgp->firstpage) 716 exec_unmap_first_page(imgp); 717 718 if (nd->ni_vp) 719 vput(nd->ni_vp); 720 721 free(tempdata, M_TEMP); 722 723 return (error); 724 } 725 726 static int 727 __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp) 728 { 729 const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header; 730 const Elf_Phdr *phdr; 731 Elf_Auxargs *elf_auxargs; 732 struct vmspace *vmspace; 733 vm_prot_t prot; 734 u_long text_size = 0, data_size = 0, total_size = 0; 735 u_long text_addr = 0, data_addr = 0; 736 u_long seg_size, seg_addr; 737 u_long addr, baddr, et_dyn_addr, entry = 0, proghdr = 0; 738 int32_t osrel = 0; 739 int error = 0, i, n, interp_name_len = 0; 740 const char *interp = NULL, *newinterp = NULL; 741 Elf_Brandinfo *brand_info; 742 char *path; 743 struct sysentvec *sv; 744 745 /* 746 * Do we have a valid ELF header ? 747 * 748 * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later 749 * if particular brand doesn't support it. 750 */ 751 if (__elfN(check_header)(hdr) != 0 || 752 (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)) 753 return (-1); 754 755 /* 756 * From here on down, we return an errno, not -1, as we've 757 * detected an ELF file. 758 */ 759 760 if ((hdr->e_phoff > PAGE_SIZE) || 761 (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) { 762 /* Only support headers in first page for now */ 763 return (ENOEXEC); 764 } 765 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 766 if (!aligned(phdr, Elf_Addr)) 767 return (ENOEXEC); 768 n = 0; 769 baddr = 0; 770 for (i = 0; i < hdr->e_phnum; i++) { 771 switch (phdr[i].p_type) { 772 case PT_LOAD: 773 if (n == 0) 774 baddr = phdr[i].p_vaddr; 775 n++; 776 break; 777 case PT_INTERP: 778 /* Path to interpreter */ 779 if (phdr[i].p_filesz > MAXPATHLEN || 780 phdr[i].p_offset > PAGE_SIZE || 781 phdr[i].p_filesz > PAGE_SIZE - phdr[i].p_offset) 782 return (ENOEXEC); 783 interp = imgp->image_header + phdr[i].p_offset; 784 interp_name_len = phdr[i].p_filesz; 785 break; 786 case PT_GNU_STACK: 787 if (__elfN(nxstack)) 788 imgp->stack_prot = 789 __elfN(trans_prot)(phdr[i].p_flags); 790 break; 791 } 792 } 793 794 brand_info = __elfN(get_brandinfo)(imgp, interp, interp_name_len, 795 &osrel); 796 if (brand_info == NULL) { 797 uprintf("ELF binary type \"%u\" not known.\n", 798 hdr->e_ident[EI_OSABI]); 799 return (ENOEXEC); 800 } 801 if (hdr->e_type == ET_DYN) { 802 if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) 803 return (ENOEXEC); 804 /* 805 * Honour the base load address from the dso if it is 806 * non-zero for some reason. 807 */ 808 if (baddr == 0) 809 et_dyn_addr = ET_DYN_LOAD_ADDR; 810 else 811 et_dyn_addr = 0; 812 } else 813 et_dyn_addr = 0; 814 sv = brand_info->sysvec; 815 if (interp != NULL && brand_info->interp_newpath != NULL) 816 newinterp = brand_info->interp_newpath; 817 818 /* 819 * Avoid a possible deadlock if the current address space is destroyed 820 * and that address space maps the locked vnode. In the common case, 821 * the locked vnode's v_usecount is decremented but remains greater 822 * than zero. Consequently, the vnode lock is not needed by vrele(). 823 * However, in cases where the vnode lock is external, such as nullfs, 824 * v_usecount may become zero. 825 * 826 * The VV_TEXT flag prevents modifications to the executable while 827 * the vnode is unlocked. 828 */ 829 VOP_UNLOCK(imgp->vp, 0); 830 831 error = exec_new_vmspace(imgp, sv); 832 imgp->proc->p_sysent = sv; 833 834 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 835 if (error) 836 return (error); 837 838 for (i = 0; i < hdr->e_phnum; i++) { 839 switch (phdr[i].p_type) { 840 case PT_LOAD: /* Loadable segment */ 841 if (phdr[i].p_memsz == 0) 842 break; 843 prot = __elfN(trans_prot)(phdr[i].p_flags); 844 error = __elfN(load_section)(imgp, phdr[i].p_offset, 845 (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr, 846 phdr[i].p_memsz, phdr[i].p_filesz, prot, 847 sv->sv_pagesize); 848 if (error != 0) 849 return (error); 850 851 /* 852 * If this segment contains the program headers, 853 * remember their virtual address for the AT_PHDR 854 * aux entry. Static binaries don't usually include 855 * a PT_PHDR entry. 856 */ 857 if (phdr[i].p_offset == 0 && 858 hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize 859 <= phdr[i].p_filesz) 860 proghdr = phdr[i].p_vaddr + hdr->e_phoff + 861 et_dyn_addr; 862 863 seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr); 864 seg_size = round_page(phdr[i].p_memsz + 865 phdr[i].p_vaddr + et_dyn_addr - seg_addr); 866 867 /* 868 * Make the largest executable segment the official 869 * text segment and all others data. 870 * 871 * Note that obreak() assumes that data_addr + 872 * data_size == end of data load area, and the ELF 873 * file format expects segments to be sorted by 874 * address. If multiple data segments exist, the 875 * last one will be used. 876 */ 877 878 if (phdr[i].p_flags & PF_X && text_size < seg_size) { 879 text_size = seg_size; 880 text_addr = seg_addr; 881 } else { 882 data_size = seg_size; 883 data_addr = seg_addr; 884 } 885 total_size += seg_size; 886 break; 887 case PT_PHDR: /* Program header table info */ 888 proghdr = phdr[i].p_vaddr + et_dyn_addr; 889 break; 890 default: 891 break; 892 } 893 } 894 895 if (data_addr == 0 && data_size == 0) { 896 data_addr = text_addr; 897 data_size = text_size; 898 } 899 900 entry = (u_long)hdr->e_entry + et_dyn_addr; 901 902 /* 903 * Check limits. It should be safe to check the 904 * limits after loading the segments since we do 905 * not actually fault in all the segments pages. 906 */ 907 PROC_LOCK(imgp->proc); 908 if (data_size > lim_cur(imgp->proc, RLIMIT_DATA) || 909 text_size > maxtsiz || 910 total_size > lim_cur(imgp->proc, RLIMIT_VMEM) || 911 racct_set(imgp->proc, RACCT_DATA, data_size) != 0 || 912 racct_set(imgp->proc, RACCT_VMEM, total_size) != 0) { 913 PROC_UNLOCK(imgp->proc); 914 return (ENOMEM); 915 } 916 917 vmspace = imgp->proc->p_vmspace; 918 vmspace->vm_tsize = text_size >> PAGE_SHIFT; 919 vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr; 920 vmspace->vm_dsize = data_size >> PAGE_SHIFT; 921 vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr; 922 923 /* 924 * We load the dynamic linker where a userland call 925 * to mmap(0, ...) would put it. The rationale behind this 926 * calculation is that it leaves room for the heap to grow to 927 * its maximum allowed size. 928 */ 929 addr = round_page((vm_offset_t)vmspace->vm_daddr + lim_max(imgp->proc, 930 RLIMIT_DATA)); 931 PROC_UNLOCK(imgp->proc); 932 933 imgp->entry_addr = entry; 934 935 if (interp != NULL) { 936 int have_interp = FALSE; 937 VOP_UNLOCK(imgp->vp, 0); 938 if (brand_info->emul_path != NULL && 939 brand_info->emul_path[0] != '\0') { 940 path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 941 snprintf(path, MAXPATHLEN, "%s%s", 942 brand_info->emul_path, interp); 943 error = __elfN(load_file)(imgp->proc, path, &addr, 944 &imgp->entry_addr, sv->sv_pagesize); 945 free(path, M_TEMP); 946 if (error == 0) 947 have_interp = TRUE; 948 } 949 if (!have_interp && newinterp != NULL) { 950 error = __elfN(load_file)(imgp->proc, newinterp, &addr, 951 &imgp->entry_addr, sv->sv_pagesize); 952 if (error == 0) 953 have_interp = TRUE; 954 } 955 if (!have_interp) { 956 error = __elfN(load_file)(imgp->proc, interp, &addr, 957 &imgp->entry_addr, sv->sv_pagesize); 958 } 959 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 960 if (error != 0) { 961 uprintf("ELF interpreter %s not found\n", interp); 962 return (error); 963 } 964 } else 965 addr = et_dyn_addr; 966 967 /* 968 * Construct auxargs table (used by the fixup routine) 969 */ 970 elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK); 971 elf_auxargs->execfd = -1; 972 elf_auxargs->phdr = proghdr; 973 elf_auxargs->phent = hdr->e_phentsize; 974 elf_auxargs->phnum = hdr->e_phnum; 975 elf_auxargs->pagesz = PAGE_SIZE; 976 elf_auxargs->base = addr; 977 elf_auxargs->flags = 0; 978 elf_auxargs->entry = entry; 979 980 imgp->auxargs = elf_auxargs; 981 imgp->interpreted = 0; 982 imgp->reloc_base = addr; 983 imgp->proc->p_osrel = osrel; 984 985 return (error); 986 } 987 988 #define suword __CONCAT(suword, __ELF_WORD_SIZE) 989 990 int 991 __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp) 992 { 993 Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs; 994 Elf_Addr *base; 995 Elf_Addr *pos; 996 997 base = (Elf_Addr *)*stack_base; 998 pos = base + (imgp->args->argc + imgp->args->envc + 2); 999 1000 if (args->execfd != -1) 1001 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 1002 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 1003 AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 1004 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 1005 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 1006 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 1007 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 1008 AUXARGS_ENTRY(pos, AT_BASE, args->base); 1009 if (imgp->execpathp != 0) 1010 AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp); 1011 AUXARGS_ENTRY(pos, AT_OSRELDATE, 1012 imgp->proc->p_ucred->cr_prison->pr_osreldate); 1013 if (imgp->canary != 0) { 1014 AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary); 1015 AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen); 1016 } 1017 AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus); 1018 if (imgp->pagesizes != 0) { 1019 AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes); 1020 AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen); 1021 } 1022 if (imgp->sysent->sv_timekeep_base != 0) { 1023 AUXARGS_ENTRY(pos, AT_TIMEKEEP, 1024 imgp->sysent->sv_timekeep_base); 1025 } 1026 AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj 1027 != NULL && imgp->stack_prot != 0 ? imgp->stack_prot : 1028 imgp->sysent->sv_stackprot); 1029 AUXARGS_ENTRY(pos, AT_NULL, 0); 1030 1031 free(imgp->auxargs, M_TEMP); 1032 imgp->auxargs = NULL; 1033 1034 base--; 1035 suword(base, (long)imgp->args->argc); 1036 *stack_base = (register_t *)base; 1037 return (0); 1038 } 1039 1040 /* 1041 * Code for generating ELF core dumps. 1042 */ 1043 1044 typedef void (*segment_callback)(vm_map_entry_t, void *); 1045 1046 /* Closure for cb_put_phdr(). */ 1047 struct phdr_closure { 1048 Elf_Phdr *phdr; /* Program header to fill in */ 1049 Elf_Off offset; /* Offset of segment in core file */ 1050 }; 1051 1052 /* Closure for cb_size_segment(). */ 1053 struct sseg_closure { 1054 int count; /* Count of writable segments. */ 1055 size_t size; /* Total size of all writable segments. */ 1056 }; 1057 1058 typedef void (*outfunc_t)(void *, struct sbuf *, size_t *); 1059 1060 struct note_info { 1061 int type; /* Note type. */ 1062 outfunc_t outfunc; /* Output function. */ 1063 void *outarg; /* Argument for the output function. */ 1064 size_t outsize; /* Output size. */ 1065 TAILQ_ENTRY(note_info) link; /* Link to the next note info. */ 1066 }; 1067 1068 TAILQ_HEAD(note_info_list, note_info); 1069 1070 static void cb_put_phdr(vm_map_entry_t, void *); 1071 static void cb_size_segment(vm_map_entry_t, void *); 1072 static void each_writable_segment(struct thread *, segment_callback, void *); 1073 static int __elfN(corehdr)(struct thread *, struct vnode *, struct ucred *, 1074 int, void *, size_t, struct note_info_list *, size_t, gzFile); 1075 static void __elfN(prepare_notes)(struct thread *, struct note_info_list *, 1076 size_t *); 1077 static void __elfN(puthdr)(struct thread *, void *, size_t, int, size_t); 1078 static void __elfN(putnote)(struct note_info *, struct sbuf *); 1079 static size_t register_note(struct note_info_list *, int, outfunc_t, void *); 1080 static int sbuf_drain_core_output(void *, const char *, int); 1081 static int sbuf_drain_count(void *arg, const char *data, int len); 1082 1083 static void __elfN(note_fpregset)(void *, struct sbuf *, size_t *); 1084 static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *); 1085 static void __elfN(note_prstatus)(void *, struct sbuf *, size_t *); 1086 static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *); 1087 static void __elfN(note_thrmisc)(void *, struct sbuf *, size_t *); 1088 static void __elfN(note_procstat_auxv)(void *, struct sbuf *, size_t *); 1089 static void __elfN(note_procstat_proc)(void *, struct sbuf *, size_t *); 1090 static void __elfN(note_procstat_psstrings)(void *, struct sbuf *, size_t *); 1091 static void note_procstat_files(void *, struct sbuf *, size_t *); 1092 static void note_procstat_groups(void *, struct sbuf *, size_t *); 1093 static void note_procstat_osrel(void *, struct sbuf *, size_t *); 1094 static void note_procstat_rlimit(void *, struct sbuf *, size_t *); 1095 static void note_procstat_umask(void *, struct sbuf *, size_t *); 1096 static void note_procstat_vmmap(void *, struct sbuf *, size_t *); 1097 1098 #ifdef COMPRESS_USER_CORES 1099 extern int compress_user_cores; 1100 extern int compress_user_cores_gzlevel; 1101 #endif 1102 1103 static int 1104 core_output(struct vnode *vp, void *base, size_t len, off_t offset, 1105 struct ucred *active_cred, struct ucred *file_cred, 1106 struct thread *td, char *core_buf, gzFile gzfile) { 1107 1108 int error; 1109 if (gzfile) { 1110 #ifdef COMPRESS_USER_CORES 1111 error = compress_core(gzfile, base, core_buf, len, td); 1112 #else 1113 panic("shouldn't be here"); 1114 #endif 1115 } else { 1116 error = vn_rdwr_inchunks(UIO_WRITE, vp, base, len, offset, 1117 UIO_USERSPACE, IO_UNIT | IO_DIRECT | IO_RANGELOCKED, 1118 active_cred, file_cred, NULL, td); 1119 } 1120 return (error); 1121 } 1122 1123 /* Coredump output parameters for sbuf drain routine. */ 1124 struct sbuf_drain_core_params { 1125 off_t offset; 1126 struct ucred *active_cred; 1127 struct ucred *file_cred; 1128 struct thread *td; 1129 struct vnode *vp; 1130 #ifdef COMPRESS_USER_CORES 1131 gzFile gzfile; 1132 #endif 1133 }; 1134 1135 /* 1136 * Drain into a core file. 1137 */ 1138 static int 1139 sbuf_drain_core_output(void *arg, const char *data, int len) 1140 { 1141 struct sbuf_drain_core_params *p; 1142 int error, locked; 1143 1144 p = (struct sbuf_drain_core_params *)arg; 1145 1146 /* 1147 * Some kern_proc out routines that print to this sbuf may 1148 * call us with the process lock held. Draining with the 1149 * non-sleepable lock held is unsafe. The lock is needed for 1150 * those routines when dumping a live process. In our case we 1151 * can safely release the lock before draining and acquire 1152 * again after. 1153 */ 1154 locked = PROC_LOCKED(p->td->td_proc); 1155 if (locked) 1156 PROC_UNLOCK(p->td->td_proc); 1157 #ifdef COMPRESS_USER_CORES 1158 if (p->gzfile != Z_NULL) 1159 error = compress_core(p->gzfile, NULL, __DECONST(char *, data), 1160 len, p->td); 1161 else 1162 #endif 1163 error = vn_rdwr_inchunks(UIO_WRITE, p->vp, 1164 __DECONST(void *, data), len, p->offset, UIO_SYSSPACE, 1165 IO_UNIT | IO_DIRECT | IO_RANGELOCKED, p->active_cred, 1166 p->file_cred, NULL, p->td); 1167 if (locked) 1168 PROC_LOCK(p->td->td_proc); 1169 if (error != 0) 1170 return (-error); 1171 p->offset += len; 1172 return (len); 1173 } 1174 1175 /* 1176 * Drain into a counter. 1177 */ 1178 static int 1179 sbuf_drain_count(void *arg, const char *data __unused, int len) 1180 { 1181 size_t *sizep; 1182 1183 sizep = (size_t *)arg; 1184 *sizep += len; 1185 return (len); 1186 } 1187 1188 int 1189 __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags) 1190 { 1191 struct ucred *cred = td->td_ucred; 1192 int error = 0; 1193 struct sseg_closure seginfo; 1194 struct note_info_list notelst; 1195 struct note_info *ninfo; 1196 void *hdr; 1197 size_t hdrsize, notesz, coresize; 1198 1199 gzFile gzfile = Z_NULL; 1200 char *core_buf = NULL; 1201 #ifdef COMPRESS_USER_CORES 1202 char gzopen_flags[8]; 1203 char *p; 1204 int doing_compress = flags & IMGACT_CORE_COMPRESS; 1205 #endif 1206 1207 hdr = NULL; 1208 TAILQ_INIT(¬elst); 1209 1210 #ifdef COMPRESS_USER_CORES 1211 if (doing_compress) { 1212 p = gzopen_flags; 1213 *p++ = 'w'; 1214 if (compress_user_cores_gzlevel >= 0 && 1215 compress_user_cores_gzlevel <= 9) 1216 *p++ = '0' + compress_user_cores_gzlevel; 1217 *p = 0; 1218 gzfile = gz_open("", gzopen_flags, vp); 1219 if (gzfile == Z_NULL) { 1220 error = EFAULT; 1221 goto done; 1222 } 1223 core_buf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO); 1224 if (!core_buf) { 1225 error = ENOMEM; 1226 goto done; 1227 } 1228 } 1229 #endif 1230 1231 /* Size the program segments. */ 1232 seginfo.count = 0; 1233 seginfo.size = 0; 1234 each_writable_segment(td, cb_size_segment, &seginfo); 1235 1236 /* 1237 * Collect info about the core file header area. 1238 */ 1239 hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count); 1240 __elfN(prepare_notes)(td, ¬elst, ¬esz); 1241 coresize = round_page(hdrsize + notesz) + seginfo.size; 1242 1243 #ifdef RACCT 1244 PROC_LOCK(td->td_proc); 1245 error = racct_add(td->td_proc, RACCT_CORE, coresize); 1246 PROC_UNLOCK(td->td_proc); 1247 if (error != 0) { 1248 error = EFAULT; 1249 goto done; 1250 } 1251 #endif 1252 if (coresize >= limit) { 1253 error = EFAULT; 1254 goto done; 1255 } 1256 1257 /* 1258 * Allocate memory for building the header, fill it up, 1259 * and write it out following the notes. 1260 */ 1261 hdr = malloc(hdrsize, M_TEMP, M_WAITOK); 1262 if (hdr == NULL) { 1263 error = EINVAL; 1264 goto done; 1265 } 1266 error = __elfN(corehdr)(td, vp, cred, seginfo.count, hdr, hdrsize, 1267 ¬elst, notesz, gzfile); 1268 1269 /* Write the contents of all of the writable segments. */ 1270 if (error == 0) { 1271 Elf_Phdr *php; 1272 off_t offset; 1273 int i; 1274 1275 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1; 1276 offset = round_page(hdrsize + notesz); 1277 for (i = 0; i < seginfo.count; i++) { 1278 error = core_output(vp, (caddr_t)(uintptr_t)php->p_vaddr, 1279 php->p_filesz, offset, cred, NOCRED, curthread, core_buf, gzfile); 1280 if (error != 0) 1281 break; 1282 offset += php->p_filesz; 1283 php++; 1284 } 1285 } 1286 if (error) { 1287 log(LOG_WARNING, 1288 "Failed to write core file for process %s (error %d)\n", 1289 curproc->p_comm, error); 1290 } 1291 1292 done: 1293 #ifdef COMPRESS_USER_CORES 1294 if (core_buf) 1295 free(core_buf, M_TEMP); 1296 if (gzfile) 1297 gzclose(gzfile); 1298 #endif 1299 while ((ninfo = TAILQ_FIRST(¬elst)) != NULL) { 1300 TAILQ_REMOVE(¬elst, ninfo, link); 1301 free(ninfo, M_TEMP); 1302 } 1303 if (hdr != NULL) 1304 free(hdr, M_TEMP); 1305 1306 return (error); 1307 } 1308 1309 /* 1310 * A callback for each_writable_segment() to write out the segment's 1311 * program header entry. 1312 */ 1313 static void 1314 cb_put_phdr(entry, closure) 1315 vm_map_entry_t entry; 1316 void *closure; 1317 { 1318 struct phdr_closure *phc = (struct phdr_closure *)closure; 1319 Elf_Phdr *phdr = phc->phdr; 1320 1321 phc->offset = round_page(phc->offset); 1322 1323 phdr->p_type = PT_LOAD; 1324 phdr->p_offset = phc->offset; 1325 phdr->p_vaddr = entry->start; 1326 phdr->p_paddr = 0; 1327 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start; 1328 phdr->p_align = PAGE_SIZE; 1329 phdr->p_flags = __elfN(untrans_prot)(entry->protection); 1330 1331 phc->offset += phdr->p_filesz; 1332 phc->phdr++; 1333 } 1334 1335 /* 1336 * A callback for each_writable_segment() to gather information about 1337 * the number of segments and their total size. 1338 */ 1339 static void 1340 cb_size_segment(entry, closure) 1341 vm_map_entry_t entry; 1342 void *closure; 1343 { 1344 struct sseg_closure *ssc = (struct sseg_closure *)closure; 1345 1346 ssc->count++; 1347 ssc->size += entry->end - entry->start; 1348 } 1349 1350 /* 1351 * For each writable segment in the process's memory map, call the given 1352 * function with a pointer to the map entry and some arbitrary 1353 * caller-supplied data. 1354 */ 1355 static void 1356 each_writable_segment(td, func, closure) 1357 struct thread *td; 1358 segment_callback func; 1359 void *closure; 1360 { 1361 struct proc *p = td->td_proc; 1362 vm_map_t map = &p->p_vmspace->vm_map; 1363 vm_map_entry_t entry; 1364 vm_object_t backing_object, object; 1365 boolean_t ignore_entry; 1366 1367 vm_map_lock_read(map); 1368 for (entry = map->header.next; entry != &map->header; 1369 entry = entry->next) { 1370 /* 1371 * Don't dump inaccessible mappings, deal with legacy 1372 * coredump mode. 1373 * 1374 * Note that read-only segments related to the elf binary 1375 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer 1376 * need to arbitrarily ignore such segments. 1377 */ 1378 if (elf_legacy_coredump) { 1379 if ((entry->protection & VM_PROT_RW) != VM_PROT_RW) 1380 continue; 1381 } else { 1382 if ((entry->protection & VM_PROT_ALL) == 0) 1383 continue; 1384 } 1385 1386 /* 1387 * Dont include memory segment in the coredump if 1388 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in 1389 * madvise(2). Do not dump submaps (i.e. parts of the 1390 * kernel map). 1391 */ 1392 if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP)) 1393 continue; 1394 1395 if ((object = entry->object.vm_object) == NULL) 1396 continue; 1397 1398 /* Ignore memory-mapped devices and such things. */ 1399 VM_OBJECT_RLOCK(object); 1400 while ((backing_object = object->backing_object) != NULL) { 1401 VM_OBJECT_RLOCK(backing_object); 1402 VM_OBJECT_RUNLOCK(object); 1403 object = backing_object; 1404 } 1405 ignore_entry = object->type != OBJT_DEFAULT && 1406 object->type != OBJT_SWAP && object->type != OBJT_VNODE && 1407 object->type != OBJT_PHYS; 1408 VM_OBJECT_RUNLOCK(object); 1409 if (ignore_entry) 1410 continue; 1411 1412 (*func)(entry, closure); 1413 } 1414 vm_map_unlock_read(map); 1415 } 1416 1417 /* 1418 * Write the core file header to the file, including padding up to 1419 * the page boundary. 1420 */ 1421 static int 1422 __elfN(corehdr)(struct thread *td, struct vnode *vp, struct ucred *cred, 1423 int numsegs, void *hdr, size_t hdrsize, struct note_info_list *notelst, 1424 size_t notesz, gzFile gzfile) 1425 { 1426 struct sbuf_drain_core_params params; 1427 struct note_info *ninfo; 1428 struct sbuf *sb; 1429 int error; 1430 1431 /* Fill in the header. */ 1432 bzero(hdr, hdrsize); 1433 __elfN(puthdr)(td, hdr, hdrsize, numsegs, notesz); 1434 1435 params.offset = 0; 1436 params.active_cred = cred; 1437 params.file_cred = NOCRED; 1438 params.td = td; 1439 params.vp = vp; 1440 #ifdef COMPRESS_USER_CORES 1441 params.gzfile = gzfile; 1442 #endif 1443 sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN); 1444 sbuf_set_drain(sb, sbuf_drain_core_output, ¶ms); 1445 sbuf_start_section(sb, NULL); 1446 sbuf_bcat(sb, hdr, hdrsize); 1447 TAILQ_FOREACH(ninfo, notelst, link) 1448 __elfN(putnote)(ninfo, sb); 1449 /* Align up to a page boundary for the program segments. */ 1450 sbuf_end_section(sb, -1, PAGE_SIZE, 0); 1451 error = sbuf_finish(sb); 1452 sbuf_delete(sb); 1453 1454 return (error); 1455 } 1456 1457 static void 1458 __elfN(prepare_notes)(struct thread *td, struct note_info_list *list, 1459 size_t *sizep) 1460 { 1461 struct proc *p; 1462 struct thread *thr; 1463 size_t size; 1464 1465 p = td->td_proc; 1466 size = 0; 1467 1468 size += register_note(list, NT_PRPSINFO, __elfN(note_prpsinfo), p); 1469 1470 /* 1471 * To have the debugger select the right thread (LWP) as the initial 1472 * thread, we dump the state of the thread passed to us in td first. 1473 * This is the thread that causes the core dump and thus likely to 1474 * be the right thread one wants to have selected in the debugger. 1475 */ 1476 thr = td; 1477 while (thr != NULL) { 1478 size += register_note(list, NT_PRSTATUS, 1479 __elfN(note_prstatus), thr); 1480 size += register_note(list, NT_FPREGSET, 1481 __elfN(note_fpregset), thr); 1482 size += register_note(list, NT_THRMISC, 1483 __elfN(note_thrmisc), thr); 1484 size += register_note(list, -1, 1485 __elfN(note_threadmd), thr); 1486 1487 thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) : 1488 TAILQ_NEXT(thr, td_plist); 1489 if (thr == td) 1490 thr = TAILQ_NEXT(thr, td_plist); 1491 } 1492 1493 size += register_note(list, NT_PROCSTAT_PROC, 1494 __elfN(note_procstat_proc), p); 1495 size += register_note(list, NT_PROCSTAT_FILES, 1496 note_procstat_files, p); 1497 size += register_note(list, NT_PROCSTAT_VMMAP, 1498 note_procstat_vmmap, p); 1499 size += register_note(list, NT_PROCSTAT_GROUPS, 1500 note_procstat_groups, p); 1501 size += register_note(list, NT_PROCSTAT_UMASK, 1502 note_procstat_umask, p); 1503 size += register_note(list, NT_PROCSTAT_RLIMIT, 1504 note_procstat_rlimit, p); 1505 size += register_note(list, NT_PROCSTAT_OSREL, 1506 note_procstat_osrel, p); 1507 size += register_note(list, NT_PROCSTAT_PSSTRINGS, 1508 __elfN(note_procstat_psstrings), p); 1509 size += register_note(list, NT_PROCSTAT_AUXV, 1510 __elfN(note_procstat_auxv), p); 1511 1512 *sizep = size; 1513 } 1514 1515 static void 1516 __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs, 1517 size_t notesz) 1518 { 1519 Elf_Ehdr *ehdr; 1520 Elf_Phdr *phdr; 1521 struct phdr_closure phc; 1522 1523 ehdr = (Elf_Ehdr *)hdr; 1524 phdr = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)); 1525 1526 ehdr->e_ident[EI_MAG0] = ELFMAG0; 1527 ehdr->e_ident[EI_MAG1] = ELFMAG1; 1528 ehdr->e_ident[EI_MAG2] = ELFMAG2; 1529 ehdr->e_ident[EI_MAG3] = ELFMAG3; 1530 ehdr->e_ident[EI_CLASS] = ELF_CLASS; 1531 ehdr->e_ident[EI_DATA] = ELF_DATA; 1532 ehdr->e_ident[EI_VERSION] = EV_CURRENT; 1533 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD; 1534 ehdr->e_ident[EI_ABIVERSION] = 0; 1535 ehdr->e_ident[EI_PAD] = 0; 1536 ehdr->e_type = ET_CORE; 1537 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 1538 ehdr->e_machine = ELF_ARCH32; 1539 #else 1540 ehdr->e_machine = ELF_ARCH; 1541 #endif 1542 ehdr->e_version = EV_CURRENT; 1543 ehdr->e_entry = 0; 1544 ehdr->e_phoff = sizeof(Elf_Ehdr); 1545 ehdr->e_flags = 0; 1546 ehdr->e_ehsize = sizeof(Elf_Ehdr); 1547 ehdr->e_phentsize = sizeof(Elf_Phdr); 1548 ehdr->e_phnum = numsegs + 1; 1549 ehdr->e_shentsize = sizeof(Elf_Shdr); 1550 ehdr->e_shnum = 0; 1551 ehdr->e_shstrndx = SHN_UNDEF; 1552 1553 /* 1554 * Fill in the program header entries. 1555 */ 1556 1557 /* The note segement. */ 1558 phdr->p_type = PT_NOTE; 1559 phdr->p_offset = hdrsize; 1560 phdr->p_vaddr = 0; 1561 phdr->p_paddr = 0; 1562 phdr->p_filesz = notesz; 1563 phdr->p_memsz = 0; 1564 phdr->p_flags = PF_R; 1565 phdr->p_align = ELF_NOTE_ROUNDSIZE; 1566 phdr++; 1567 1568 /* All the writable segments from the program. */ 1569 phc.phdr = phdr; 1570 phc.offset = round_page(hdrsize + notesz); 1571 each_writable_segment(td, cb_put_phdr, &phc); 1572 } 1573 1574 static size_t 1575 register_note(struct note_info_list *list, int type, outfunc_t out, void *arg) 1576 { 1577 struct note_info *ninfo; 1578 size_t size, notesize; 1579 1580 size = 0; 1581 out(arg, NULL, &size); 1582 ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK); 1583 ninfo->type = type; 1584 ninfo->outfunc = out; 1585 ninfo->outarg = arg; 1586 ninfo->outsize = size; 1587 TAILQ_INSERT_TAIL(list, ninfo, link); 1588 1589 if (type == -1) 1590 return (size); 1591 1592 notesize = sizeof(Elf_Note) + /* note header */ 1593 roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) + 1594 /* note name */ 1595 roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */ 1596 1597 return (notesize); 1598 } 1599 1600 static size_t 1601 append_note_data(const void *src, void *dst, size_t len) 1602 { 1603 size_t padded_len; 1604 1605 padded_len = roundup2(len, ELF_NOTE_ROUNDSIZE); 1606 if (dst != NULL) { 1607 bcopy(src, dst, len); 1608 bzero((char *)dst + len, padded_len - len); 1609 } 1610 return (padded_len); 1611 } 1612 1613 size_t 1614 __elfN(populate_note)(int type, void *src, void *dst, size_t size, void **descp) 1615 { 1616 Elf_Note *note; 1617 char *buf; 1618 size_t notesize; 1619 1620 buf = dst; 1621 if (buf != NULL) { 1622 note = (Elf_Note *)buf; 1623 note->n_namesz = sizeof(FREEBSD_ABI_VENDOR); 1624 note->n_descsz = size; 1625 note->n_type = type; 1626 buf += sizeof(*note); 1627 buf += append_note_data(FREEBSD_ABI_VENDOR, buf, 1628 sizeof(FREEBSD_ABI_VENDOR)); 1629 append_note_data(src, buf, size); 1630 if (descp != NULL) 1631 *descp = buf; 1632 } 1633 1634 notesize = sizeof(Elf_Note) + /* note header */ 1635 roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) + 1636 /* note name */ 1637 roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */ 1638 1639 return (notesize); 1640 } 1641 1642 static void 1643 __elfN(putnote)(struct note_info *ninfo, struct sbuf *sb) 1644 { 1645 Elf_Note note; 1646 ssize_t old_len; 1647 1648 if (ninfo->type == -1) { 1649 ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize); 1650 return; 1651 } 1652 1653 note.n_namesz = sizeof(FREEBSD_ABI_VENDOR); 1654 note.n_descsz = ninfo->outsize; 1655 note.n_type = ninfo->type; 1656 1657 sbuf_bcat(sb, ¬e, sizeof(note)); 1658 sbuf_start_section(sb, &old_len); 1659 sbuf_bcat(sb, FREEBSD_ABI_VENDOR, sizeof(FREEBSD_ABI_VENDOR)); 1660 sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0); 1661 if (note.n_descsz == 0) 1662 return; 1663 sbuf_start_section(sb, &old_len); 1664 ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize); 1665 sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0); 1666 } 1667 1668 /* 1669 * Miscellaneous note out functions. 1670 */ 1671 1672 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 1673 #include <compat/freebsd32/freebsd32.h> 1674 1675 typedef struct prstatus32 elf_prstatus_t; 1676 typedef struct prpsinfo32 elf_prpsinfo_t; 1677 typedef struct fpreg32 elf_prfpregset_t; 1678 typedef struct fpreg32 elf_fpregset_t; 1679 typedef struct reg32 elf_gregset_t; 1680 typedef struct thrmisc32 elf_thrmisc_t; 1681 #define ELF_KERN_PROC_MASK KERN_PROC_MASK32 1682 typedef struct kinfo_proc32 elf_kinfo_proc_t; 1683 typedef uint32_t elf_ps_strings_t; 1684 #else 1685 typedef prstatus_t elf_prstatus_t; 1686 typedef prpsinfo_t elf_prpsinfo_t; 1687 typedef prfpregset_t elf_prfpregset_t; 1688 typedef prfpregset_t elf_fpregset_t; 1689 typedef gregset_t elf_gregset_t; 1690 typedef thrmisc_t elf_thrmisc_t; 1691 #define ELF_KERN_PROC_MASK 0 1692 typedef struct kinfo_proc elf_kinfo_proc_t; 1693 typedef vm_offset_t elf_ps_strings_t; 1694 #endif 1695 1696 static void 1697 __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep) 1698 { 1699 struct proc *p; 1700 elf_prpsinfo_t *psinfo; 1701 1702 p = (struct proc *)arg; 1703 if (sb != NULL) { 1704 KASSERT(*sizep == sizeof(*psinfo), ("invalid size")); 1705 psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK); 1706 psinfo->pr_version = PRPSINFO_VERSION; 1707 psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t); 1708 strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname)); 1709 /* 1710 * XXX - We don't fill in the command line arguments properly 1711 * yet. 1712 */ 1713 strlcpy(psinfo->pr_psargs, p->p_comm, 1714 sizeof(psinfo->pr_psargs)); 1715 1716 sbuf_bcat(sb, psinfo, sizeof(*psinfo)); 1717 free(psinfo, M_TEMP); 1718 } 1719 *sizep = sizeof(*psinfo); 1720 } 1721 1722 static void 1723 __elfN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep) 1724 { 1725 struct thread *td; 1726 elf_prstatus_t *status; 1727 1728 td = (struct thread *)arg; 1729 if (sb != NULL) { 1730 KASSERT(*sizep == sizeof(*status), ("invalid size")); 1731 status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK); 1732 status->pr_version = PRSTATUS_VERSION; 1733 status->pr_statussz = sizeof(elf_prstatus_t); 1734 status->pr_gregsetsz = sizeof(elf_gregset_t); 1735 status->pr_fpregsetsz = sizeof(elf_fpregset_t); 1736 status->pr_osreldate = osreldate; 1737 status->pr_cursig = td->td_proc->p_sig; 1738 status->pr_pid = td->td_tid; 1739 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 1740 fill_regs32(td, &status->pr_reg); 1741 #else 1742 fill_regs(td, &status->pr_reg); 1743 #endif 1744 sbuf_bcat(sb, status, sizeof(*status)); 1745 free(status, M_TEMP); 1746 } 1747 *sizep = sizeof(*status); 1748 } 1749 1750 static void 1751 __elfN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep) 1752 { 1753 struct thread *td; 1754 elf_prfpregset_t *fpregset; 1755 1756 td = (struct thread *)arg; 1757 if (sb != NULL) { 1758 KASSERT(*sizep == sizeof(*fpregset), ("invalid size")); 1759 fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK); 1760 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 1761 fill_fpregs32(td, fpregset); 1762 #else 1763 fill_fpregs(td, fpregset); 1764 #endif 1765 sbuf_bcat(sb, fpregset, sizeof(*fpregset)); 1766 free(fpregset, M_TEMP); 1767 } 1768 *sizep = sizeof(*fpregset); 1769 } 1770 1771 static void 1772 __elfN(note_thrmisc)(void *arg, struct sbuf *sb, size_t *sizep) 1773 { 1774 struct thread *td; 1775 elf_thrmisc_t thrmisc; 1776 1777 td = (struct thread *)arg; 1778 if (sb != NULL) { 1779 KASSERT(*sizep == sizeof(thrmisc), ("invalid size")); 1780 bzero(&thrmisc._pad, sizeof(thrmisc._pad)); 1781 strcpy(thrmisc.pr_tname, td->td_name); 1782 sbuf_bcat(sb, &thrmisc, sizeof(thrmisc)); 1783 } 1784 *sizep = sizeof(thrmisc); 1785 } 1786 1787 /* 1788 * Allow for MD specific notes, as well as any MD 1789 * specific preparations for writing MI notes. 1790 */ 1791 static void 1792 __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep) 1793 { 1794 struct thread *td; 1795 void *buf; 1796 size_t size; 1797 1798 td = (struct thread *)arg; 1799 size = *sizep; 1800 if (size != 0 && sb != NULL) 1801 buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK); 1802 else 1803 buf = NULL; 1804 size = 0; 1805 __elfN(dump_thread)(td, buf, &size); 1806 KASSERT(sb == NULL || *sizep == size, ("invalid size")); 1807 if (size != 0 && sb != NULL) 1808 sbuf_bcat(sb, buf, size); 1809 free(buf, M_TEMP); 1810 *sizep = size; 1811 } 1812 1813 #ifdef KINFO_PROC_SIZE 1814 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 1815 #endif 1816 1817 static void 1818 __elfN(note_procstat_proc)(void *arg, struct sbuf *sb, size_t *sizep) 1819 { 1820 struct proc *p; 1821 size_t size; 1822 int structsize; 1823 1824 p = (struct proc *)arg; 1825 size = sizeof(structsize) + p->p_numthreads * 1826 sizeof(elf_kinfo_proc_t); 1827 1828 if (sb != NULL) { 1829 KASSERT(*sizep == size, ("invalid size")); 1830 structsize = sizeof(elf_kinfo_proc_t); 1831 sbuf_bcat(sb, &structsize, sizeof(structsize)); 1832 sx_slock(&proctree_lock); 1833 PROC_LOCK(p); 1834 kern_proc_out(p, sb, ELF_KERN_PROC_MASK); 1835 sx_sunlock(&proctree_lock); 1836 } 1837 *sizep = size; 1838 } 1839 1840 #ifdef KINFO_FILE_SIZE 1841 CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE); 1842 #endif 1843 1844 static void 1845 note_procstat_files(void *arg, struct sbuf *sb, size_t *sizep) 1846 { 1847 struct proc *p; 1848 size_t size; 1849 int structsize; 1850 1851 p = (struct proc *)arg; 1852 if (sb == NULL) { 1853 size = 0; 1854 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN); 1855 sbuf_set_drain(sb, sbuf_drain_count, &size); 1856 sbuf_bcat(sb, &structsize, sizeof(structsize)); 1857 PROC_LOCK(p); 1858 kern_proc_filedesc_out(p, sb, -1); 1859 sbuf_finish(sb); 1860 sbuf_delete(sb); 1861 *sizep = size; 1862 } else { 1863 structsize = sizeof(struct kinfo_file); 1864 sbuf_bcat(sb, &structsize, sizeof(structsize)); 1865 PROC_LOCK(p); 1866 kern_proc_filedesc_out(p, sb, -1); 1867 } 1868 } 1869 1870 #ifdef KINFO_VMENTRY_SIZE 1871 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE); 1872 #endif 1873 1874 static void 1875 note_procstat_vmmap(void *arg, struct sbuf *sb, size_t *sizep) 1876 { 1877 struct proc *p; 1878 size_t size; 1879 int structsize; 1880 1881 p = (struct proc *)arg; 1882 if (sb == NULL) { 1883 size = 0; 1884 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN); 1885 sbuf_set_drain(sb, sbuf_drain_count, &size); 1886 sbuf_bcat(sb, &structsize, sizeof(structsize)); 1887 PROC_LOCK(p); 1888 kern_proc_vmmap_out(p, sb); 1889 sbuf_finish(sb); 1890 sbuf_delete(sb); 1891 *sizep = size; 1892 } else { 1893 structsize = sizeof(struct kinfo_vmentry); 1894 sbuf_bcat(sb, &structsize, sizeof(structsize)); 1895 PROC_LOCK(p); 1896 kern_proc_vmmap_out(p, sb); 1897 } 1898 } 1899 1900 static void 1901 note_procstat_groups(void *arg, struct sbuf *sb, size_t *sizep) 1902 { 1903 struct proc *p; 1904 size_t size; 1905 int structsize; 1906 1907 p = (struct proc *)arg; 1908 size = sizeof(structsize) + p->p_ucred->cr_ngroups * sizeof(gid_t); 1909 if (sb != NULL) { 1910 KASSERT(*sizep == size, ("invalid size")); 1911 structsize = sizeof(gid_t); 1912 sbuf_bcat(sb, &structsize, sizeof(structsize)); 1913 sbuf_bcat(sb, p->p_ucred->cr_groups, p->p_ucred->cr_ngroups * 1914 sizeof(gid_t)); 1915 } 1916 *sizep = size; 1917 } 1918 1919 static void 1920 note_procstat_umask(void *arg, struct sbuf *sb, size_t *sizep) 1921 { 1922 struct proc *p; 1923 size_t size; 1924 int structsize; 1925 1926 p = (struct proc *)arg; 1927 size = sizeof(structsize) + sizeof(p->p_fd->fd_cmask); 1928 if (sb != NULL) { 1929 KASSERT(*sizep == size, ("invalid size")); 1930 structsize = sizeof(p->p_fd->fd_cmask); 1931 sbuf_bcat(sb, &structsize, sizeof(structsize)); 1932 sbuf_bcat(sb, &p->p_fd->fd_cmask, sizeof(p->p_fd->fd_cmask)); 1933 } 1934 *sizep = size; 1935 } 1936 1937 static void 1938 note_procstat_rlimit(void *arg, struct sbuf *sb, size_t *sizep) 1939 { 1940 struct proc *p; 1941 struct rlimit rlim[RLIM_NLIMITS]; 1942 size_t size; 1943 int structsize, i; 1944 1945 p = (struct proc *)arg; 1946 size = sizeof(structsize) + sizeof(rlim); 1947 if (sb != NULL) { 1948 KASSERT(*sizep == size, ("invalid size")); 1949 structsize = sizeof(rlim); 1950 sbuf_bcat(sb, &structsize, sizeof(structsize)); 1951 PROC_LOCK(p); 1952 for (i = 0; i < RLIM_NLIMITS; i++) 1953 lim_rlimit(p, i, &rlim[i]); 1954 PROC_UNLOCK(p); 1955 sbuf_bcat(sb, rlim, sizeof(rlim)); 1956 } 1957 *sizep = size; 1958 } 1959 1960 static void 1961 note_procstat_osrel(void *arg, struct sbuf *sb, size_t *sizep) 1962 { 1963 struct proc *p; 1964 size_t size; 1965 int structsize; 1966 1967 p = (struct proc *)arg; 1968 size = sizeof(structsize) + sizeof(p->p_osrel); 1969 if (sb != NULL) { 1970 KASSERT(*sizep == size, ("invalid size")); 1971 structsize = sizeof(p->p_osrel); 1972 sbuf_bcat(sb, &structsize, sizeof(structsize)); 1973 sbuf_bcat(sb, &p->p_osrel, sizeof(p->p_osrel)); 1974 } 1975 *sizep = size; 1976 } 1977 1978 static void 1979 __elfN(note_procstat_psstrings)(void *arg, struct sbuf *sb, size_t *sizep) 1980 { 1981 struct proc *p; 1982 elf_ps_strings_t ps_strings; 1983 size_t size; 1984 int structsize; 1985 1986 p = (struct proc *)arg; 1987 size = sizeof(structsize) + sizeof(ps_strings); 1988 if (sb != NULL) { 1989 KASSERT(*sizep == size, ("invalid size")); 1990 structsize = sizeof(ps_strings); 1991 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 1992 ps_strings = PTROUT(p->p_sysent->sv_psstrings); 1993 #else 1994 ps_strings = p->p_sysent->sv_psstrings; 1995 #endif 1996 sbuf_bcat(sb, &structsize, sizeof(structsize)); 1997 sbuf_bcat(sb, &ps_strings, sizeof(ps_strings)); 1998 } 1999 *sizep = size; 2000 } 2001 2002 static void 2003 __elfN(note_procstat_auxv)(void *arg, struct sbuf *sb, size_t *sizep) 2004 { 2005 struct proc *p; 2006 size_t size; 2007 int structsize; 2008 2009 p = (struct proc *)arg; 2010 if (sb == NULL) { 2011 size = 0; 2012 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN); 2013 sbuf_set_drain(sb, sbuf_drain_count, &size); 2014 sbuf_bcat(sb, &structsize, sizeof(structsize)); 2015 PHOLD(p); 2016 proc_getauxv(curthread, p, sb); 2017 PRELE(p); 2018 sbuf_finish(sb); 2019 sbuf_delete(sb); 2020 *sizep = size; 2021 } else { 2022 structsize = sizeof(Elf_Auxinfo); 2023 sbuf_bcat(sb, &structsize, sizeof(structsize)); 2024 PHOLD(p); 2025 proc_getauxv(curthread, p, sb); 2026 PRELE(p); 2027 } 2028 } 2029 2030 static boolean_t 2031 __elfN(parse_notes)(struct image_params *imgp, Elf_Brandnote *checknote, 2032 int32_t *osrel, const Elf_Phdr *pnote) 2033 { 2034 const Elf_Note *note, *note0, *note_end; 2035 const char *note_name; 2036 int i; 2037 2038 if (pnote == NULL || pnote->p_offset > PAGE_SIZE || 2039 pnote->p_filesz > PAGE_SIZE - pnote->p_offset) 2040 return (FALSE); 2041 2042 note = note0 = (const Elf_Note *)(imgp->image_header + pnote->p_offset); 2043 note_end = (const Elf_Note *)(imgp->image_header + 2044 pnote->p_offset + pnote->p_filesz); 2045 for (i = 0; i < 100 && note >= note0 && note < note_end; i++) { 2046 if (!aligned(note, Elf32_Addr) || (const char *)note_end - 2047 (const char *)note < sizeof(Elf_Note)) 2048 return (FALSE); 2049 if (note->n_namesz != checknote->hdr.n_namesz || 2050 note->n_descsz != checknote->hdr.n_descsz || 2051 note->n_type != checknote->hdr.n_type) 2052 goto nextnote; 2053 note_name = (const char *)(note + 1); 2054 if (note_name + checknote->hdr.n_namesz >= 2055 (const char *)note_end || strncmp(checknote->vendor, 2056 note_name, checknote->hdr.n_namesz) != 0) 2057 goto nextnote; 2058 2059 /* 2060 * Fetch the osreldate for binary 2061 * from the ELF OSABI-note if necessary. 2062 */ 2063 if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 && 2064 checknote->trans_osrel != NULL) 2065 return (checknote->trans_osrel(note, osrel)); 2066 return (TRUE); 2067 2068 nextnote: 2069 note = (const Elf_Note *)((const char *)(note + 1) + 2070 roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) + 2071 roundup2(note->n_descsz, ELF_NOTE_ROUNDSIZE)); 2072 } 2073 2074 return (FALSE); 2075 } 2076 2077 /* 2078 * Try to find the appropriate ABI-note section for checknote, 2079 * fetch the osreldate for binary from the ELF OSABI-note. Only the 2080 * first page of the image is searched, the same as for headers. 2081 */ 2082 static boolean_t 2083 __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote, 2084 int32_t *osrel) 2085 { 2086 const Elf_Phdr *phdr; 2087 const Elf_Ehdr *hdr; 2088 int i; 2089 2090 hdr = (const Elf_Ehdr *)imgp->image_header; 2091 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 2092 2093 for (i = 0; i < hdr->e_phnum; i++) { 2094 if (phdr[i].p_type == PT_NOTE && 2095 __elfN(parse_notes)(imgp, checknote, osrel, &phdr[i])) 2096 return (TRUE); 2097 } 2098 return (FALSE); 2099 2100 } 2101 2102 /* 2103 * Tell kern_execve.c about it, with a little help from the linker. 2104 */ 2105 static struct execsw __elfN(execsw) = { 2106 __CONCAT(exec_, __elfN(imgact)), 2107 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) 2108 }; 2109 EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw)); 2110 2111 #ifdef COMPRESS_USER_CORES 2112 /* 2113 * Compress and write out a core segment for a user process. 2114 * 2115 * 'inbuf' is the starting address of a VM segment in the process' address 2116 * space that is to be compressed and written out to the core file. 'dest_buf' 2117 * is a buffer in the kernel's address space. The segment is copied from 2118 * 'inbuf' to 'dest_buf' first before being processed by the compression 2119 * routine gzwrite(). This copying is necessary because the content of the VM 2120 * segment may change between the compression pass and the crc-computation pass 2121 * in gzwrite(). This is because realtime threads may preempt the UNIX kernel. 2122 * 2123 * If inbuf is NULL it is assumed that data is already copied to 'dest_buf'. 2124 */ 2125 static int 2126 compress_core (gzFile file, char *inbuf, char *dest_buf, unsigned int len, 2127 struct thread *td) 2128 { 2129 int len_compressed; 2130 int error = 0; 2131 unsigned int chunk_len; 2132 2133 while (len) { 2134 if (inbuf != NULL) { 2135 chunk_len = (len > CORE_BUF_SIZE) ? CORE_BUF_SIZE : len; 2136 copyin(inbuf, dest_buf, chunk_len); 2137 inbuf += chunk_len; 2138 } else { 2139 chunk_len = len; 2140 } 2141 len_compressed = gzwrite(file, dest_buf, chunk_len); 2142 2143 EVENTHANDLER_INVOKE(app_coredump_progress, td, len_compressed); 2144 2145 if ((unsigned int)len_compressed != chunk_len) { 2146 log(LOG_WARNING, 2147 "compress_core: length mismatch (0x%x returned, " 2148 "0x%x expected)\n", len_compressed, chunk_len); 2149 EVENTHANDLER_INVOKE(app_coredump_error, td, 2150 "compress_core: length mismatch %x -> %x", 2151 chunk_len, len_compressed); 2152 error = EFAULT; 2153 break; 2154 } 2155 len -= chunk_len; 2156 maybe_yield(); 2157 } 2158 2159 return (error); 2160 } 2161 #endif /* COMPRESS_USER_CORES */ 2162 2163 static vm_prot_t 2164 __elfN(trans_prot)(Elf_Word flags) 2165 { 2166 vm_prot_t prot; 2167 2168 prot = 0; 2169 if (flags & PF_X) 2170 prot |= VM_PROT_EXECUTE; 2171 if (flags & PF_W) 2172 prot |= VM_PROT_WRITE; 2173 if (flags & PF_R) 2174 prot |= VM_PROT_READ; 2175 #if __ELF_WORD_SIZE == 32 2176 #if defined(__amd64__) 2177 if (i386_read_exec && (flags & PF_R)) 2178 prot |= VM_PROT_EXECUTE; 2179 #endif 2180 #endif 2181 return (prot); 2182 } 2183 2184 static Elf_Word 2185 __elfN(untrans_prot)(vm_prot_t prot) 2186 { 2187 Elf_Word flags; 2188 2189 flags = 0; 2190 if (prot & VM_PROT_EXECUTE) 2191 flags |= PF_X; 2192 if (prot & VM_PROT_READ) 2193 flags |= PF_R; 2194 if (prot & VM_PROT_WRITE) 2195 flags |= PF_W; 2196 return (flags); 2197 } 2198