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