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