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