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