1e1743d02SSøren Schmidt /*- 28a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a36da99SPedro F. Giffuni * 486be94fcSTycho Nightingale * Copyright (c) 2017 Dell EMC 5455d3589SDavid E. O'Brien * Copyright (c) 2000-2001, 2003 David O'Brien 69a14aa01SUlrich Spörlein * Copyright (c) 1995-1996 Søren Schmidt 7e1743d02SSøren Schmidt * Copyright (c) 1996 Peter Wemm 8e1743d02SSøren Schmidt * All rights reserved. 9e1743d02SSøren Schmidt * 10e1743d02SSøren Schmidt * Redistribution and use in source and binary forms, with or without 11e1743d02SSøren Schmidt * modification, are permitted provided that the following conditions 12e1743d02SSøren Schmidt * are met: 13e1743d02SSøren Schmidt * 1. Redistributions of source code must retain the above copyright 14e1743d02SSøren Schmidt * notice, this list of conditions and the following disclaimer 15e1743d02SSøren Schmidt * in this position and unchanged. 16e1743d02SSøren Schmidt * 2. Redistributions in binary form must reproduce the above copyright 17e1743d02SSøren Schmidt * notice, this list of conditions and the following disclaimer in the 18e1743d02SSøren Schmidt * documentation and/or other materials provided with the distribution. 19e1743d02SSøren Schmidt * 3. The name of the author may not be used to endorse or promote products 2021dc7d4fSJens Schweikhardt * derived from this software without specific prior written permission 21e1743d02SSøren Schmidt * 22e1743d02SSøren Schmidt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23e1743d02SSøren Schmidt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24e1743d02SSøren Schmidt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25e1743d02SSøren Schmidt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26e1743d02SSøren Schmidt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27e1743d02SSøren Schmidt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28e1743d02SSøren Schmidt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29e1743d02SSøren Schmidt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30e1743d02SSøren Schmidt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31e1743d02SSøren Schmidt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32e1743d02SSøren Schmidt */ 33e1743d02SSøren Schmidt 34677b542eSDavid E. O'Brien #include <sys/cdefs.h> 35677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 36677b542eSDavid E. O'Brien 3712bc222eSJonathan Anderson #include "opt_capsicum.h" 3862919d78SPeter Wemm 39e1743d02SSøren Schmidt #include <sys/param.h> 404a144410SRobert Watson #include <sys/capsicum.h> 4178f57a9cSMark Johnston #include <sys/compressor.h> 42e1743d02SSøren Schmidt #include <sys/exec.h> 438c64af4fSJohn Polstra #include <sys/fcntl.h> 44e1743d02SSøren Schmidt #include <sys/imgact.h> 45e1743d02SSøren Schmidt #include <sys/imgact_elf.h> 46b96bd95bSIan Lepore #include <sys/jail.h> 47e1743d02SSøren Schmidt #include <sys/kernel.h> 48f34fa851SJohn Baldwin #include <sys/lock.h> 49e1743d02SSøren Schmidt #include <sys/malloc.h> 5068ff2a43SChristian S.J. Peron #include <sys/mount.h> 518c64af4fSJohn Polstra #include <sys/mman.h> 52a794e791SBruce Evans #include <sys/namei.h> 538c64af4fSJohn Polstra #include <sys/pioctl.h> 54a794e791SBruce Evans #include <sys/proc.h> 558c64af4fSJohn Polstra #include <sys/procfs.h> 5686be94fcSTycho Nightingale #include <sys/ptrace.h> 571ba5ad42SEdward Tomasz Napierala #include <sys/racct.h> 588c64af4fSJohn Polstra #include <sys/resourcevar.h> 5989f6b863SAttilio Rao #include <sys/rwlock.h> 60bd390213SMikolaj Golub #include <sys/sbuf.h> 61da61b9a6SAlan Cox #include <sys/sf_buf.h> 62ee235befSKonstantin Belousov #include <sys/smp.h> 6336240ea5SDoug Rabson #include <sys/systm.h> 64e1743d02SSøren Schmidt #include <sys/signalvar.h> 658c64af4fSJohn Polstra #include <sys/stat.h> 661005a129SJohn Baldwin #include <sys/sx.h> 678c64af4fSJohn Polstra #include <sys/syscall.h> 68e1743d02SSøren Schmidt #include <sys/sysctl.h> 698c64af4fSJohn Polstra #include <sys/sysent.h> 70a794e791SBruce Evans #include <sys/vnode.h> 71e7228204SAlfred Perlstein #include <sys/syslog.h> 72e7228204SAlfred Perlstein #include <sys/eventhandler.h> 73f1fca82eSMikolaj Golub #include <sys/user.h> 74e7228204SAlfred Perlstein 75e1743d02SSøren Schmidt #include <vm/vm.h> 76e1743d02SSøren Schmidt #include <vm/vm_kern.h> 77e1743d02SSøren Schmidt #include <vm/vm_param.h> 78e1743d02SSøren Schmidt #include <vm/pmap.h> 79e1743d02SSøren Schmidt #include <vm/vm_map.h> 800ff27d31SJohn Polstra #include <vm/vm_object.h> 81e1743d02SSøren Schmidt #include <vm/vm_extern.h> 82e1743d02SSøren Schmidt 8352c24af7SPeter Wemm #include <machine/elf.h> 84e1743d02SSøren Schmidt #include <machine/md_var.h> 85e1743d02SSøren Schmidt 861b8388cdSMikolaj Golub #define ELF_NOTE_ROUNDSIZE 4 87c815a20cSDavid E. O'Brien #define OLD_EI_BRAND 8 88c815a20cSDavid E. O'Brien 893ebc1248SPeter Wemm static int __elfN(check_header)(const Elf_Ehdr *hdr); 9032c01de2SDmitry Chagin static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp, 91*09c78d53SEdward Tomasz Napierala const char *interp, int32_t *osrel, uint32_t *fctl0); 923ebc1248SPeter Wemm static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr, 931699546dSEdward Tomasz Napierala u_long *entry); 940bbee4cdSKonstantin Belousov static int __elfN(load_section)(struct image_params *imgp, vm_ooffset_t offset, 951699546dSEdward Tomasz Napierala caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot); 963ebc1248SPeter Wemm static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp); 97a95659f7SEd Maste static bool __elfN(freebsd_trans_osrel)(const Elf_Note *note, 9889ffc202SBjoern A. Zeeb int32_t *osrel); 99a95659f7SEd Maste static bool kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel); 10032c01de2SDmitry Chagin static boolean_t __elfN(check_note)(struct image_params *imgp, 101cefb93f2SKonstantin Belousov Elf_Brandnote *checknote, int32_t *osrel, uint32_t *fctl0); 102ed167eaaSKonstantin Belousov static vm_prot_t __elfN(trans_prot)(Elf_Word); 103ed167eaaSKonstantin Belousov static Elf_Word __elfN(untrans_prot)(vm_prot_t); 104e1743d02SSøren Schmidt 105a360a43dSJake Burkholder SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0, 106a360a43dSJake Burkholder ""); 107a360a43dSJake Burkholder 108bd390213SMikolaj Golub #define CORE_BUF_SIZE (16 * 1024) 109e7228204SAlfred Perlstein 110e548a1d4SJake Burkholder int __elfN(fallback_brand) = -1; 111e548a1d4SJake Burkholder SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, 112af3b2549SHans Petter Selasky fallback_brand, CTLFLAG_RWTUN, &__elfN(fallback_brand), 0, 113a360a43dSJake Burkholder __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort"); 114a360a43dSJake Burkholder 115551d79e1SMarcel Moolenaar static int elf_legacy_coredump = 0; 116a360a43dSJake Burkholder SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW, 1171cbb879dSEd Maste &elf_legacy_coredump, 0, 1181cbb879dSEd Maste "include all and only RW pages in core dumps"); 119e1743d02SSøren Schmidt 12062c625fdSKonstantin Belousov int __elfN(nxstack) = 1214d22d07aSKonstantin Belousov #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */ || \ 1224bf4b0f1SJohn Baldwin (defined(__arm__) && __ARM_ARCH >= 7) || defined(__aarch64__) || \ 1234bf4b0f1SJohn Baldwin defined(__riscv) 12462c625fdSKonstantin Belousov 1; 12562c625fdSKonstantin Belousov #else 12662c625fdSKonstantin Belousov 0; 12762c625fdSKonstantin Belousov #endif 128291c06a1SKonstantin Belousov SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, 129291c06a1SKonstantin Belousov nxstack, CTLFLAG_RW, &__elfN(nxstack), 0, 130291c06a1SKonstantin Belousov __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack"); 131291c06a1SKonstantin Belousov 132eb785fabSKonstantin Belousov #if __ELF_WORD_SIZE == 32 && (defined(__amd64__) || defined(__i386__)) 133126b36a2SKonstantin Belousov int i386_read_exec = 0; 134126b36a2SKonstantin Belousov SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0, 135126b36a2SKonstantin Belousov "enable execution from readable segments"); 136126b36a2SKonstantin Belousov #endif 137126b36a2SKonstantin Belousov 138fa50a355SKonstantin Belousov SYSCTL_NODE(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, aslr, CTLFLAG_RW, 0, 139fa50a355SKonstantin Belousov ""); 140fa50a355SKonstantin Belousov #define ASLR_NODE_OID __CONCAT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), _aslr) 141fa50a355SKonstantin Belousov 142fa50a355SKonstantin Belousov static int __elfN(aslr_enabled) = 0; 143fa50a355SKonstantin Belousov SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, enable, CTLFLAG_RWTUN, 144fa50a355SKonstantin Belousov &__elfN(aslr_enabled), 0, 145fa50a355SKonstantin Belousov __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) 146fa50a355SKonstantin Belousov ": enable address map randomization"); 147fa50a355SKonstantin Belousov 148fa50a355SKonstantin Belousov static int __elfN(pie_aslr_enabled) = 0; 149fa50a355SKonstantin Belousov SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, pie_enable, CTLFLAG_RWTUN, 150fa50a355SKonstantin Belousov &__elfN(pie_aslr_enabled), 0, 151fa50a355SKonstantin Belousov __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) 152fa50a355SKonstantin Belousov ": enable address map randomization for PIE binaries"); 153fa50a355SKonstantin Belousov 154fa50a355SKonstantin Belousov static int __elfN(aslr_honor_sbrk) = 1; 155fa50a355SKonstantin Belousov SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, honor_sbrk, CTLFLAG_RW, 156fa50a355SKonstantin Belousov &__elfN(aslr_honor_sbrk), 0, 157fa50a355SKonstantin Belousov __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": assume sbrk is used"); 158fa50a355SKonstantin Belousov 1593ebc1248SPeter Wemm static Elf_Brandinfo *elf_brand_list[MAX_BRANDS]; 160e1743d02SSøren Schmidt 161545517f1SEdward Tomasz Napierala #define aligned(a, t) (rounddown2((u_long)(a), sizeof(t)) == (u_long)(a)) 16293d1c728SKonstantin Belousov 16332c01de2SDmitry Chagin static const char FREEBSD_ABI_VENDOR[] = "FreeBSD"; 16432c01de2SDmitry Chagin 16532c01de2SDmitry Chagin Elf_Brandnote __elfN(freebsd_brandnote) = { 16632c01de2SDmitry Chagin .hdr.n_namesz = sizeof(FREEBSD_ABI_VENDOR), 16732c01de2SDmitry Chagin .hdr.n_descsz = sizeof(int32_t), 1684c22b468SEd Maste .hdr.n_type = NT_FREEBSD_ABI_TAG, 16932c01de2SDmitry Chagin .vendor = FREEBSD_ABI_VENDOR, 17089ffc202SBjoern A. Zeeb .flags = BN_TRANSLATE_OSREL, 17189ffc202SBjoern A. Zeeb .trans_osrel = __elfN(freebsd_trans_osrel) 17232c01de2SDmitry Chagin }; 17332c01de2SDmitry Chagin 174a95659f7SEd Maste static bool 17589ffc202SBjoern A. Zeeb __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel) 17689ffc202SBjoern A. Zeeb { 17789ffc202SBjoern A. Zeeb uintptr_t p; 17889ffc202SBjoern A. Zeeb 17989ffc202SBjoern A. Zeeb p = (uintptr_t)(note + 1); 1801b8388cdSMikolaj Golub p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE); 18189ffc202SBjoern A. Zeeb *osrel = *(const int32_t *)(p); 18289ffc202SBjoern A. Zeeb 183a95659f7SEd Maste return (true); 18489ffc202SBjoern A. Zeeb } 18589ffc202SBjoern A. Zeeb 18689ffc202SBjoern A. Zeeb static const char GNU_ABI_VENDOR[] = "GNU"; 18789ffc202SBjoern A. Zeeb static int GNU_KFREEBSD_ABI_DESC = 3; 18889ffc202SBjoern A. Zeeb 18989ffc202SBjoern A. Zeeb Elf_Brandnote __elfN(kfreebsd_brandnote) = { 19089ffc202SBjoern A. Zeeb .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 19189ffc202SBjoern A. Zeeb .hdr.n_descsz = 16, /* XXX at least 16 */ 19289ffc202SBjoern A. Zeeb .hdr.n_type = 1, 19389ffc202SBjoern A. Zeeb .vendor = GNU_ABI_VENDOR, 19489ffc202SBjoern A. Zeeb .flags = BN_TRANSLATE_OSREL, 19589ffc202SBjoern A. Zeeb .trans_osrel = kfreebsd_trans_osrel 19689ffc202SBjoern A. Zeeb }; 19789ffc202SBjoern A. Zeeb 198a95659f7SEd Maste static bool 19989ffc202SBjoern A. Zeeb kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel) 20089ffc202SBjoern A. Zeeb { 20189ffc202SBjoern A. Zeeb const Elf32_Word *desc; 20289ffc202SBjoern A. Zeeb uintptr_t p; 20389ffc202SBjoern A. Zeeb 20489ffc202SBjoern A. Zeeb p = (uintptr_t)(note + 1); 2051b8388cdSMikolaj Golub p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE); 20689ffc202SBjoern A. Zeeb 20789ffc202SBjoern A. Zeeb desc = (const Elf32_Word *)p; 20889ffc202SBjoern A. Zeeb if (desc[0] != GNU_KFREEBSD_ABI_DESC) 209a95659f7SEd Maste return (false); 21089ffc202SBjoern A. Zeeb 21189ffc202SBjoern A. Zeeb /* 21289ffc202SBjoern A. Zeeb * Debian GNU/kFreeBSD embed the earliest compatible kernel version 21389ffc202SBjoern A. Zeeb * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way. 21489ffc202SBjoern A. Zeeb */ 21589ffc202SBjoern A. Zeeb *osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3]; 21689ffc202SBjoern A. Zeeb 217a95659f7SEd Maste return (true); 21889ffc202SBjoern A. Zeeb } 21989ffc202SBjoern A. Zeeb 220e1743d02SSøren Schmidt int 2213ebc1248SPeter Wemm __elfN(insert_brand_entry)(Elf_Brandinfo *entry) 222e1743d02SSøren Schmidt { 223e1743d02SSøren Schmidt int i; 224e1743d02SSøren Schmidt 2253ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 226ea5a2b2eSSøren Schmidt if (elf_brand_list[i] == NULL) { 227ea5a2b2eSSøren Schmidt elf_brand_list[i] = entry; 228e1743d02SSøren Schmidt break; 229e1743d02SSøren Schmidt } 230e1743d02SSøren Schmidt } 231925c8b5bSBjoern A. Zeeb if (i == MAX_BRANDS) { 232925c8b5bSBjoern A. Zeeb printf("WARNING: %s: could not insert brandinfo entry: %p\n", 233925c8b5bSBjoern A. Zeeb __func__, entry); 234a7cddfedSJake Burkholder return (-1); 235925c8b5bSBjoern A. Zeeb } 236a7cddfedSJake Burkholder return (0); 237e1743d02SSøren Schmidt } 238e1743d02SSøren Schmidt 239e1743d02SSøren Schmidt int 2403ebc1248SPeter Wemm __elfN(remove_brand_entry)(Elf_Brandinfo *entry) 241e1743d02SSøren Schmidt { 242e1743d02SSøren Schmidt int i; 243e1743d02SSøren Schmidt 2443ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 245ea5a2b2eSSøren Schmidt if (elf_brand_list[i] == entry) { 246ea5a2b2eSSøren Schmidt elf_brand_list[i] = NULL; 247e1743d02SSøren Schmidt break; 248e1743d02SSøren Schmidt } 249e1743d02SSøren Schmidt } 250ea5a2b2eSSøren Schmidt if (i == MAX_BRANDS) 251a7cddfedSJake Burkholder return (-1); 252a7cddfedSJake Burkholder return (0); 253e1743d02SSøren Schmidt } 254e1743d02SSøren Schmidt 255096977faSMark Newton int 2563ebc1248SPeter Wemm __elfN(brand_inuse)(Elf_Brandinfo *entry) 257096977faSMark Newton { 258096977faSMark Newton struct proc *p; 259553629ebSJake Burkholder int rval = FALSE; 260096977faSMark Newton 2611005a129SJohn Baldwin sx_slock(&allproc_lock); 2624f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) { 263553629ebSJake Burkholder if (p->p_sysent == entry->sysvec) { 264553629ebSJake Burkholder rval = TRUE; 265553629ebSJake Burkholder break; 266096977faSMark Newton } 267553629ebSJake Burkholder } 2681005a129SJohn Baldwin sx_sunlock(&allproc_lock); 269096977faSMark Newton 270553629ebSJake Burkholder return (rval); 271096977faSMark Newton } 272096977faSMark Newton 2735fe3ed62SJake Burkholder static Elf_Brandinfo * 27432c01de2SDmitry Chagin __elfN(get_brandinfo)(struct image_params *imgp, const char *interp, 275*09c78d53SEdward Tomasz Napierala int32_t *osrel, uint32_t *fctl0) 2765fe3ed62SJake Burkholder { 27732c01de2SDmitry Chagin const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header; 278af582aaeSKonstantin Belousov Elf_Brandinfo *bi, *bi_m; 27932c01de2SDmitry Chagin boolean_t ret; 280*09c78d53SEdward Tomasz Napierala int i, interp_name_len; 281*09c78d53SEdward Tomasz Napierala 282*09c78d53SEdward Tomasz Napierala interp_name_len = interp != NULL ? strlen(interp) : 0; 2835fe3ed62SJake Burkholder 2845fe3ed62SJake Burkholder /* 28532c01de2SDmitry Chagin * We support four types of branding -- (1) the ELF EI_OSABI field 2865fe3ed62SJake Burkholder * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string 28732c01de2SDmitry Chagin * branding w/in the ELF header, (3) path of the `interp_path' 28832c01de2SDmitry Chagin * field, and (4) the ".note.ABI-tag" ELF section. 2895fe3ed62SJake Burkholder */ 2905fe3ed62SJake Burkholder 29132c01de2SDmitry Chagin /* Look for an ".note.ABI-tag" ELF section */ 292af582aaeSKonstantin Belousov bi_m = NULL; 29332c01de2SDmitry Chagin for (i = 0; i < MAX_BRANDS; i++) { 29432c01de2SDmitry Chagin bi = elf_brand_list[i]; 295ecc2fda8SBjoern A. Zeeb if (bi == NULL) 296ecc2fda8SBjoern A. Zeeb continue; 2972274ab3dSKonstantin Belousov if (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0) 2981438fe3cSKonstantin Belousov continue; 299ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && (bi->flags & 300ecc2fda8SBjoern A. Zeeb (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) { 301cefb93f2SKonstantin Belousov ret = __elfN(check_note)(imgp, bi->brand_note, osrel, 302cefb93f2SKonstantin Belousov fctl0); 303f19d421aSNathan Whitehorn /* Give brand a chance to veto check_note's guess */ 304f19d421aSNathan Whitehorn if (ret && bi->header_supported) 305f19d421aSNathan Whitehorn ret = bi->header_supported(imgp); 306af582aaeSKonstantin Belousov /* 307af582aaeSKonstantin Belousov * If note checker claimed the binary, but the 308af582aaeSKonstantin Belousov * interpreter path in the image does not 309af582aaeSKonstantin Belousov * match default one for the brand, try to 310af582aaeSKonstantin Belousov * search for other brands with the same 311af582aaeSKonstantin Belousov * interpreter. Either there is better brand 312af582aaeSKonstantin Belousov * with the right interpreter, or, failing 313af582aaeSKonstantin Belousov * this, we return first brand which accepted 314af582aaeSKonstantin Belousov * our note and, optionally, header. 315af582aaeSKonstantin Belousov */ 3163aeacc55SKonstantin Belousov if (ret && bi_m == NULL && interp != NULL && 3173aeacc55SKonstantin Belousov (bi->interp_path == NULL || 3183aeacc55SKonstantin Belousov (strlen(bi->interp_path) + 1 != interp_name_len || 3193aeacc55SKonstantin Belousov strncmp(interp, bi->interp_path, interp_name_len) 3203aeacc55SKonstantin Belousov != 0))) { 321af582aaeSKonstantin Belousov bi_m = bi; 322af582aaeSKonstantin Belousov ret = 0; 323af582aaeSKonstantin Belousov } 32432c01de2SDmitry Chagin if (ret) 32532c01de2SDmitry Chagin return (bi); 32632c01de2SDmitry Chagin } 32732c01de2SDmitry Chagin } 328af582aaeSKonstantin Belousov if (bi_m != NULL) 329af582aaeSKonstantin Belousov return (bi_m); 33032c01de2SDmitry Chagin 3315fe3ed62SJake Burkholder /* If the executable has a brand, search for it in the brand list. */ 3325fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 3335fe3ed62SJake Burkholder bi = elf_brand_list[i]; 3341438fe3cSKonstantin Belousov if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 || 3352274ab3dSKonstantin Belousov (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0)) 336ecc2fda8SBjoern A. Zeeb continue; 337ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 3385fe3ed62SJake Burkholder (hdr->e_ident[EI_OSABI] == bi->brand || 3390fe98320SEd Schouten (bi->compat_3_brand != NULL && 3403d560b4bSKonstantin Belousov strcmp((const char *)&hdr->e_ident[OLD_EI_BRAND], 3410fe98320SEd Schouten bi->compat_3_brand) == 0))) { 342686d2f31SNathan Whitehorn /* Looks good, but give brand a chance to veto */ 343d722231bSJohn Baldwin if (bi->header_supported == NULL || 34415a9aedfSKonstantin Belousov bi->header_supported(imgp)) { 34515a9aedfSKonstantin Belousov /* 34615a9aedfSKonstantin Belousov * Again, prefer strictly matching 34715a9aedfSKonstantin Belousov * interpreter path. 34815a9aedfSKonstantin Belousov */ 3497aab7a80SKonstantin Belousov if (interp_name_len == 0 && 3507aab7a80SKonstantin Belousov bi->interp_path == NULL) 3517aab7a80SKonstantin Belousov return (bi); 3527aab7a80SKonstantin Belousov if (bi->interp_path != NULL && 3537aab7a80SKonstantin Belousov strlen(bi->interp_path) + 1 == 35415a9aedfSKonstantin Belousov interp_name_len && strncmp(interp, 35515a9aedfSKonstantin Belousov bi->interp_path, interp_name_len) == 0) 3565fe3ed62SJake Burkholder return (bi); 35715a9aedfSKonstantin Belousov if (bi_m == NULL) 35815a9aedfSKonstantin Belousov bi_m = bi; 3595fe3ed62SJake Burkholder } 360686d2f31SNathan Whitehorn } 36115a9aedfSKonstantin Belousov } 36215a9aedfSKonstantin Belousov if (bi_m != NULL) 36315a9aedfSKonstantin Belousov return (bi_m); 3645fe3ed62SJake Burkholder 365817dc004SWarner Losh /* No known brand, see if the header is recognized by any brand */ 366817dc004SWarner Losh for (i = 0; i < MAX_BRANDS; i++) { 367817dc004SWarner Losh bi = elf_brand_list[i]; 368817dc004SWarner Losh if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY || 369817dc004SWarner Losh bi->header_supported == NULL) 370817dc004SWarner Losh continue; 371817dc004SWarner Losh if (hdr->e_machine == bi->machine) { 372817dc004SWarner Losh ret = bi->header_supported(imgp); 373817dc004SWarner Losh if (ret) 374817dc004SWarner Losh return (bi); 375817dc004SWarner Losh } 376817dc004SWarner Losh } 377817dc004SWarner Losh 3785fe3ed62SJake Burkholder /* Lacking a known brand, search for a recognized interpreter. */ 3795fe3ed62SJake Burkholder if (interp != NULL) { 3805fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 3815fe3ed62SJake Burkholder bi = elf_brand_list[i]; 3822274ab3dSKonstantin Belousov if (bi == NULL || (bi->flags & 3832274ab3dSKonstantin Belousov (BI_BRAND_NOTE_MANDATORY | BI_BRAND_ONLY_STATIC)) 3842274ab3dSKonstantin Belousov != 0) 385ecc2fda8SBjoern A. Zeeb continue; 386ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 3873aeacc55SKonstantin Belousov bi->interp_path != NULL && 388d1ae5c83SKonstantin Belousov /* ELF image p_filesz includes terminating zero */ 389d1ae5c83SKonstantin Belousov strlen(bi->interp_path) + 1 == interp_name_len && 390d1ae5c83SKonstantin Belousov strncmp(interp, bi->interp_path, interp_name_len) 391d722231bSJohn Baldwin == 0 && (bi->header_supported == NULL || 392d722231bSJohn Baldwin bi->header_supported(imgp))) 3935fe3ed62SJake Burkholder return (bi); 3945fe3ed62SJake Burkholder } 3955fe3ed62SJake Burkholder } 3965fe3ed62SJake Burkholder 3975fe3ed62SJake Burkholder /* Lacking a recognized interpreter, try the default brand */ 3985fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 3995fe3ed62SJake Burkholder bi = elf_brand_list[i]; 4001438fe3cSKonstantin Belousov if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 || 4012274ab3dSKonstantin Belousov (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0)) 402ecc2fda8SBjoern A. Zeeb continue; 403ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 404d722231bSJohn Baldwin __elfN(fallback_brand) == bi->brand && 405d722231bSJohn Baldwin (bi->header_supported == NULL || 406d722231bSJohn Baldwin bi->header_supported(imgp))) 4075fe3ed62SJake Burkholder return (bi); 4085fe3ed62SJake Burkholder } 4095fe3ed62SJake Burkholder return (NULL); 4105fe3ed62SJake Burkholder } 4115fe3ed62SJake Burkholder 412e1743d02SSøren Schmidt static int 4133ebc1248SPeter Wemm __elfN(check_header)(const Elf_Ehdr *hdr) 414e1743d02SSøren Schmidt { 415d0ca7c29SPeter Wemm Elf_Brandinfo *bi; 4163ebc1248SPeter Wemm int i; 4173ebc1248SPeter Wemm 41852c24af7SPeter Wemm if (!IS_ELF(*hdr) || 41952c24af7SPeter Wemm hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 42052c24af7SPeter Wemm hdr->e_ident[EI_DATA] != ELF_TARG_DATA || 4213dc19c46SJacques Vidrine hdr->e_ident[EI_VERSION] != EV_CURRENT || 4223dc19c46SJacques Vidrine hdr->e_phentsize != sizeof(Elf_Phdr) || 4233dc19c46SJacques Vidrine hdr->e_version != ELF_TARG_VER) 424a7cddfedSJake Burkholder return (ENOEXEC); 425e1743d02SSøren Schmidt 4263ebc1248SPeter Wemm /* 4273ebc1248SPeter Wemm * Make sure we have at least one brand for this machine. 4283ebc1248SPeter Wemm */ 4293ebc1248SPeter Wemm 4303ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 431d0ca7c29SPeter Wemm bi = elf_brand_list[i]; 432d0ca7c29SPeter Wemm if (bi != NULL && bi->machine == hdr->e_machine) 4333ebc1248SPeter Wemm break; 4343ebc1248SPeter Wemm } 4353ebc1248SPeter Wemm if (i == MAX_BRANDS) 436a7cddfedSJake Burkholder return (ENOEXEC); 437e1743d02SSøren Schmidt 438a7cddfedSJake Burkholder return (0); 439e1743d02SSøren Schmidt } 440e1743d02SSøren Schmidt 441e1743d02SSøren Schmidt static int 4423ebc1248SPeter Wemm __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 443ff6f03c7SAlan Cox vm_offset_t start, vm_offset_t end, vm_prot_t prot) 4443ebc1248SPeter Wemm { 445da61b9a6SAlan Cox struct sf_buf *sf; 446da61b9a6SAlan Cox int error; 4473ebc1248SPeter Wemm vm_offset_t off; 4483ebc1248SPeter Wemm 4493ebc1248SPeter Wemm /* 4503ebc1248SPeter Wemm * Create the page if it doesn't exist yet. Ignore errors. 4513ebc1248SPeter Wemm */ 452aaadc41fSKonstantin Belousov vm_map_fixed(map, NULL, 0, trunc_page(start), round_page(end) - 453aaadc41fSKonstantin Belousov trunc_page(start), VM_PROT_ALL, VM_PROT_ALL, MAP_CHECK_EXCL); 4543ebc1248SPeter Wemm 4553ebc1248SPeter Wemm /* 4563ebc1248SPeter Wemm * Find the page from the underlying object. 4573ebc1248SPeter Wemm */ 45828e8da65SAlan Cox if (object != NULL) { 459da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset); 460da61b9a6SAlan Cox if (sf == NULL) 461da61b9a6SAlan Cox return (KERN_FAILURE); 4623ebc1248SPeter Wemm off = offset - trunc_page(offset); 463da61b9a6SAlan Cox error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start, 464ca0387efSJake Burkholder end - start); 465be996836SAttilio Rao vm_imgact_unmap_page(sf); 466fe0a8a39SKonstantin Belousov if (error != 0) 467a7cddfedSJake Burkholder return (KERN_FAILURE); 4683ebc1248SPeter Wemm } 4693ebc1248SPeter Wemm 470a7cddfedSJake Burkholder return (KERN_SUCCESS); 4713ebc1248SPeter Wemm } 4723ebc1248SPeter Wemm 4733ebc1248SPeter Wemm static int 474e3d8f8feSKonstantin Belousov __elfN(map_insert)(struct image_params *imgp, vm_map_t map, vm_object_t object, 475e3d8f8feSKonstantin Belousov vm_ooffset_t offset, vm_offset_t start, vm_offset_t end, vm_prot_t prot, 476e3d8f8feSKonstantin Belousov int cow) 4773ebc1248SPeter Wemm { 478da61b9a6SAlan Cox struct sf_buf *sf; 479da61b9a6SAlan Cox vm_offset_t off; 480a063facbSMarcel Moolenaar vm_size_t sz; 481e3d8f8feSKonstantin Belousov int error, locked, rv; 4823ebc1248SPeter Wemm 4833ebc1248SPeter Wemm if (start != trunc_page(start)) { 48481f223caSJake Burkholder rv = __elfN(map_partial)(map, object, offset, start, 485ff6f03c7SAlan Cox round_page(start), prot); 48628e8da65SAlan Cox if (rv != KERN_SUCCESS) 487a7cddfedSJake Burkholder return (rv); 4883ebc1248SPeter Wemm offset += round_page(start) - start; 4893ebc1248SPeter Wemm start = round_page(start); 4903ebc1248SPeter Wemm } 4913ebc1248SPeter Wemm if (end != round_page(end)) { 49281f223caSJake Burkholder rv = __elfN(map_partial)(map, object, offset + 493ff6f03c7SAlan Cox trunc_page(end) - start, trunc_page(end), end, prot); 49428e8da65SAlan Cox if (rv != KERN_SUCCESS) 495a7cddfedSJake Burkholder return (rv); 4963ebc1248SPeter Wemm end = trunc_page(end); 4973ebc1248SPeter Wemm } 498e383e820SAlan Cox if (start >= end) 499e383e820SAlan Cox return (KERN_SUCCESS); 500e383e820SAlan Cox if ((offset & PAGE_MASK) != 0) { 5013ebc1248SPeter Wemm /* 502e383e820SAlan Cox * The mapping is not page aligned. This means that we have 503e383e820SAlan Cox * to copy the data. 5043ebc1248SPeter Wemm */ 505aaadc41fSKonstantin Belousov rv = vm_map_fixed(map, NULL, 0, start, end - start, 506aaadc41fSKonstantin Belousov prot | VM_PROT_WRITE, VM_PROT_ALL, MAP_CHECK_EXCL); 5075420f76bSKonstantin Belousov if (rv != KERN_SUCCESS) 508a7cddfedSJake Burkholder return (rv); 509da61b9a6SAlan Cox if (object == NULL) 510da61b9a6SAlan Cox return (KERN_SUCCESS); 511da61b9a6SAlan Cox for (; start < end; start += sz) { 512da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset); 513da61b9a6SAlan Cox if (sf == NULL) 514da61b9a6SAlan Cox return (KERN_FAILURE); 5153ebc1248SPeter Wemm off = offset - trunc_page(offset); 5163ebc1248SPeter Wemm sz = end - start; 517da61b9a6SAlan Cox if (sz > PAGE_SIZE - off) 518da61b9a6SAlan Cox sz = PAGE_SIZE - off; 519da61b9a6SAlan Cox error = copyout((caddr_t)sf_buf_kva(sf) + off, 5203ebc1248SPeter Wemm (caddr_t)start, sz); 521be996836SAttilio Rao vm_imgact_unmap_page(sf); 5225420f76bSKonstantin Belousov if (error != 0) 523a7cddfedSJake Burkholder return (KERN_FAILURE); 524da61b9a6SAlan Cox offset += sz; 5253ebc1248SPeter Wemm } 5263ebc1248SPeter Wemm } else { 527e5e6093bSAlan Cox vm_object_reference(object); 528e383e820SAlan Cox rv = vm_map_fixed(map, object, offset, start, end - start, 529e383e820SAlan Cox prot, VM_PROT_ALL, cow | MAP_CHECK_EXCL); 530e3d8f8feSKonstantin Belousov if (rv != KERN_SUCCESS) { 531e3d8f8feSKonstantin Belousov locked = VOP_ISLOCKED(imgp->vp); 532e3d8f8feSKonstantin Belousov VOP_UNLOCK(imgp->vp, 0); 533e5e6093bSAlan Cox vm_object_deallocate(object); 534e3d8f8feSKonstantin Belousov vn_lock(imgp->vp, locked | LK_RETRY); 535a7cddfedSJake Burkholder return (rv); 5363ebc1248SPeter Wemm } 5373ebc1248SPeter Wemm } 538e383e820SAlan Cox return (KERN_SUCCESS); 539e383e820SAlan Cox } 5403ebc1248SPeter Wemm 5413ebc1248SPeter Wemm static int 5420bbee4cdSKonstantin Belousov __elfN(load_section)(struct image_params *imgp, vm_ooffset_t offset, 5431699546dSEdward Tomasz Napierala caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot) 544e1743d02SSøren Schmidt { 545da61b9a6SAlan Cox struct sf_buf *sf; 546e1743d02SSøren Schmidt size_t map_len; 547292177e6SAlan Cox vm_map_t map; 548292177e6SAlan Cox vm_object_t object; 549973d67c4SKonstantin Belousov vm_offset_t off, map_addr; 550fa7dd9c5SMatthew Dillon int error, rv, cow; 551e1743d02SSøren Schmidt size_t copy_len; 5520bbee4cdSKonstantin Belousov vm_ooffset_t file_addr; 55352c24af7SPeter Wemm 55425ead034SBrian Feldman /* 55525ead034SBrian Feldman * It's necessary to fail if the filsz + offset taken from the 55625ead034SBrian Feldman * header is greater than the actual file pager object's size. 55725ead034SBrian Feldman * If we were to allow this, then the vm_map_find() below would 55825ead034SBrian Feldman * walk right off the end of the file object and into the ether. 55925ead034SBrian Feldman * 56025ead034SBrian Feldman * While I'm here, might as well check for something else that 56125ead034SBrian Feldman * is invalid: filsz cannot be greater than memsz. 56225ead034SBrian Feldman */ 5639bcf2f2dSKonstantin Belousov if ((filsz != 0 && (off_t)filsz + offset > imgp->attr->va_size) || 5649bcf2f2dSKonstantin Belousov filsz > memsz) { 56525ead034SBrian Feldman uprintf("elf_load_section: truncated ELF file\n"); 56625ead034SBrian Feldman return (ENOEXEC); 56725ead034SBrian Feldman } 56825ead034SBrian Feldman 569292177e6SAlan Cox object = imgp->object; 570292177e6SAlan Cox map = &imgp->proc->p_vmspace->vm_map; 571545517f1SEdward Tomasz Napierala map_addr = trunc_page((vm_offset_t)vmaddr); 572545517f1SEdward Tomasz Napierala file_addr = trunc_page(offset); 573e1743d02SSøren Schmidt 574e1743d02SSøren Schmidt /* 57552c24af7SPeter Wemm * We have two choices. We can either clear the data in the last page 57652c24af7SPeter Wemm * of an oversized mapping, or we can start the anon mapping a page 57752c24af7SPeter Wemm * early and copy the initialized data into that first page. We 57828e8da65SAlan Cox * choose the second. 57952c24af7SPeter Wemm */ 5809bcf2f2dSKonstantin Belousov if (filsz == 0) 5819bcf2f2dSKonstantin Belousov map_len = 0; 5829bcf2f2dSKonstantin Belousov else if (memsz > filsz) 583545517f1SEdward Tomasz Napierala map_len = trunc_page(offset + filsz) - file_addr; 58452c24af7SPeter Wemm else 585545517f1SEdward Tomasz Napierala map_len = round_page(offset + filsz) - file_addr; 58652c24af7SPeter Wemm 58752c24af7SPeter Wemm if (map_len != 0) { 588fa7dd9c5SMatthew Dillon /* cow flags: don't dump readonly sections in core */ 589fa7dd9c5SMatthew Dillon cow = MAP_COPY_ON_WRITE | MAP_PREFAULT | 590fa7dd9c5SMatthew Dillon (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP); 591fa7dd9c5SMatthew Dillon 592e3d8f8feSKonstantin Belousov rv = __elfN(map_insert)(imgp, map, 59352c24af7SPeter Wemm object, 59452c24af7SPeter Wemm file_addr, /* file offset */ 59552c24af7SPeter Wemm map_addr, /* virtual start */ 59652c24af7SPeter Wemm map_addr + map_len,/* virtual end */ 59752c24af7SPeter Wemm prot, 598fa7dd9c5SMatthew Dillon cow); 599e5e6093bSAlan Cox if (rv != KERN_SUCCESS) 600a7cddfedSJake Burkholder return (EINVAL); 60152c24af7SPeter Wemm 60252c24af7SPeter Wemm /* we can stop now if we've covered it all */ 603973d67c4SKonstantin Belousov if (memsz == filsz) 604a7cddfedSJake Burkholder return (0); 60552c24af7SPeter Wemm } 60652c24af7SPeter Wemm 60752c24af7SPeter Wemm 60852c24af7SPeter Wemm /* 60952c24af7SPeter Wemm * We have to get the remaining bit of the file into the first part 61052c24af7SPeter Wemm * of the oversized map segment. This is normally because the .data 61152c24af7SPeter Wemm * segment in the file is extended to provide bss. It's a neat idea 61252c24af7SPeter Wemm * to try and save a page, but it's a pain in the behind to implement. 613e1743d02SSøren Schmidt */ 614545517f1SEdward Tomasz Napierala copy_len = filsz == 0 ? 0 : (offset + filsz) - trunc_page(offset + 615545517f1SEdward Tomasz Napierala filsz); 616545517f1SEdward Tomasz Napierala map_addr = trunc_page((vm_offset_t)vmaddr + filsz); 617545517f1SEdward Tomasz Napierala map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr; 618e1743d02SSøren Schmidt 61952c24af7SPeter Wemm /* This had damn well better be true! */ 6208191d577SPeter Wemm if (map_len != 0) { 621e3d8f8feSKonstantin Belousov rv = __elfN(map_insert)(imgp, map, NULL, 0, map_addr, 622c547cbb4SAlan Cox map_addr + map_len, prot, 0); 623973d67c4SKonstantin Belousov if (rv != KERN_SUCCESS) 624a7cddfedSJake Burkholder return (EINVAL); 6258191d577SPeter Wemm } 626e1743d02SSøren Schmidt 62752c24af7SPeter Wemm if (copy_len != 0) { 628da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset + filsz); 629da61b9a6SAlan Cox if (sf == NULL) 630da61b9a6SAlan Cox return (EIO); 631e1743d02SSøren Schmidt 63252c24af7SPeter Wemm /* send the page fragment to user space */ 633545517f1SEdward Tomasz Napierala off = trunc_page(offset + filsz) - trunc_page(offset + filsz); 634da61b9a6SAlan Cox error = copyout((caddr_t)sf_buf_kva(sf) + off, 635da61b9a6SAlan Cox (caddr_t)map_addr, copy_len); 636be996836SAttilio Rao vm_imgact_unmap_page(sf); 637973d67c4SKonstantin Belousov if (error != 0) 63852c24af7SPeter Wemm return (error); 63952c24af7SPeter Wemm } 640e1743d02SSøren Schmidt 641e1743d02SSøren Schmidt /* 642c547cbb4SAlan Cox * Remove write access to the page if it was only granted by map_insert 643c547cbb4SAlan Cox * to allow copyout. 644e1743d02SSøren Schmidt */ 645c547cbb4SAlan Cox if ((prot & VM_PROT_WRITE) == 0) 646292177e6SAlan Cox vm_map_protect(map, trunc_page(map_addr), round_page(map_addr + 647292177e6SAlan Cox map_len), prot, FALSE); 6488191d577SPeter Wemm 649ff6f03c7SAlan Cox return (0); 650e1743d02SSøren Schmidt } 651e1743d02SSøren Schmidt 652c33fe779SJohn Polstra /* 653c33fe779SJohn Polstra * Load the file "file" into memory. It may be either a shared object 654c33fe779SJohn Polstra * or an executable. 655c33fe779SJohn Polstra * 656c33fe779SJohn Polstra * The "addr" reference parameter is in/out. On entry, it specifies 657c33fe779SJohn Polstra * the address where a shared object should be loaded. If the file is 658c33fe779SJohn Polstra * an executable, this value is ignored. On exit, "addr" specifies 659c33fe779SJohn Polstra * where the file was actually loaded. 660c33fe779SJohn Polstra * 661c33fe779SJohn Polstra * The "entry" reference parameter is out only. On exit, it specifies 662c33fe779SJohn Polstra * the entry point for the loaded file. 663c33fe779SJohn Polstra */ 664e1743d02SSøren Schmidt static int 6653ebc1248SPeter Wemm __elfN(load_file)(struct proc *p, const char *file, u_long *addr, 6661699546dSEdward Tomasz Napierala u_long *entry) 667e1743d02SSøren Schmidt { 668911c2be0SMark Peek struct { 669911c2be0SMark Peek struct nameidata nd; 670911c2be0SMark Peek struct vattr attr; 671911c2be0SMark Peek struct image_params image_params; 672911c2be0SMark Peek } *tempdata; 673d254af07SMatthew Dillon const Elf_Ehdr *hdr = NULL; 674d254af07SMatthew Dillon const Elf_Phdr *phdr = NULL; 675911c2be0SMark Peek struct nameidata *nd; 676911c2be0SMark Peek struct vattr *attr; 677911c2be0SMark Peek struct image_params *imgp; 67852c24af7SPeter Wemm vm_prot_t prot; 679c33fe779SJohn Polstra u_long rbase; 680c33fe779SJohn Polstra u_long base_addr = 0; 6815050aa86SKonstantin Belousov int error, i, numsegs; 682e1743d02SSøren Schmidt 68312bc222eSJonathan Anderson #ifdef CAPABILITY_MODE 68412bc222eSJonathan Anderson /* 68512bc222eSJonathan Anderson * XXXJA: This check can go away once we are sufficiently confident 68612bc222eSJonathan Anderson * that the checks in namei() are correct. 68712bc222eSJonathan Anderson */ 68812bc222eSJonathan Anderson if (IN_CAPABILITY_MODE(curthread)) 68912bc222eSJonathan Anderson return (ECAPMODE); 69012bc222eSJonathan Anderson #endif 69112bc222eSJonathan Anderson 692a163d034SWarner Losh tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK); 693911c2be0SMark Peek nd = &tempdata->nd; 694911c2be0SMark Peek attr = &tempdata->attr; 695911c2be0SMark Peek imgp = &tempdata->image_params; 696911c2be0SMark Peek 697c8a79999SPeter Wemm /* 698c8a79999SPeter Wemm * Initialize part of the common data 699c8a79999SPeter Wemm */ 700c8a79999SPeter Wemm imgp->proc = p; 701911c2be0SMark Peek imgp->attr = attr; 702c8a79999SPeter Wemm imgp->firstpage = NULL; 70359c8bc40SAlan Cox imgp->image_header = NULL; 7040b2ed1aeSJeff Roberson imgp->object = NULL; 7056d7bdc8dSRobert Watson imgp->execlabel = NULL; 706c8a79999SPeter Wemm 7075050aa86SKonstantin Belousov NDINIT(nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE, file, curthread); 708911c2be0SMark Peek if ((error = namei(nd)) != 0) { 709911c2be0SMark Peek nd->ni_vp = NULL; 710e1743d02SSøren Schmidt goto fail; 711e1743d02SSøren Schmidt } 712911c2be0SMark Peek NDFREE(nd, NDF_ONLY_PNBUF); 713911c2be0SMark Peek imgp->vp = nd->ni_vp; 714c8a79999SPeter Wemm 715e1743d02SSøren Schmidt /* 716e1743d02SSøren Schmidt * Check permissions, modes, uid, etc on the file, and "open" it. 717e1743d02SSøren Schmidt */ 718c8a79999SPeter Wemm error = exec_check_permissions(imgp); 719373d1a3fSAlan Cox if (error) 720c8a79999SPeter Wemm goto fail; 721e1743d02SSøren Schmidt 722c8a79999SPeter Wemm error = exec_map_first_page(imgp); 723373d1a3fSAlan Cox if (error) 724373d1a3fSAlan Cox goto fail; 725373d1a3fSAlan Cox 72625ead034SBrian Feldman /* 72725ead034SBrian Feldman * Also make certain that the interpreter stays the same, so set 728e6e370a7SJeff Roberson * its VV_TEXT flag, too. 72925ead034SBrian Feldman */ 730877d24acSKonstantin Belousov VOP_SET_TEXT(nd->ni_vp); 731e6e370a7SJeff Roberson 7328516dd18SPoul-Henning Kamp imgp->object = nd->ni_vp->v_object; 733e1743d02SSøren Schmidt 734d254af07SMatthew Dillon hdr = (const Elf_Ehdr *)imgp->image_header; 7353ebc1248SPeter Wemm if ((error = __elfN(check_header)(hdr)) != 0) 736e1743d02SSøren Schmidt goto fail; 737c33fe779SJohn Polstra if (hdr->e_type == ET_DYN) 738c33fe779SJohn Polstra rbase = *addr; 739c33fe779SJohn Polstra else if (hdr->e_type == ET_EXEC) 740c33fe779SJohn Polstra rbase = 0; 741c33fe779SJohn Polstra else { 742c33fe779SJohn Polstra error = ENOEXEC; 743c33fe779SJohn Polstra goto fail; 744c33fe779SJohn Polstra } 745e1743d02SSøren Schmidt 746c8a79999SPeter Wemm /* Only support headers that fit within first page for now */ 74752c24af7SPeter Wemm if ((hdr->e_phoff > PAGE_SIZE) || 748d19d5bf4STijl Coosemans (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) { 749c8a79999SPeter Wemm error = ENOEXEC; 750e1743d02SSøren Schmidt goto fail; 751c8a79999SPeter Wemm } 752c8a79999SPeter Wemm 753d254af07SMatthew Dillon phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 75493d1c728SKonstantin Belousov if (!aligned(phdr, Elf_Addr)) { 75593d1c728SKonstantin Belousov error = ENOEXEC; 75693d1c728SKonstantin Belousov goto fail; 75793d1c728SKonstantin Belousov } 758e1743d02SSøren Schmidt 759c33fe779SJohn Polstra for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) { 7605b33842aSKonstantin Belousov if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) { 7615b33842aSKonstantin Belousov /* Loadable segment */ 762ed167eaaSKonstantin Belousov prot = __elfN(trans_prot)(phdr[i].p_flags); 763292177e6SAlan Cox error = __elfN(load_section)(imgp, phdr[i].p_offset, 76481f223caSJake Burkholder (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase, 7651699546dSEdward Tomasz Napierala phdr[i].p_memsz, phdr[i].p_filesz, prot); 766292177e6SAlan Cox if (error != 0) 767e1743d02SSøren Schmidt goto fail; 768e1743d02SSøren Schmidt /* 769c33fe779SJohn Polstra * Establish the base address if this is the 770c33fe779SJohn Polstra * first segment. 771e1743d02SSøren Schmidt */ 772c33fe779SJohn Polstra if (numsegs == 0) 773ca0387efSJake Burkholder base_addr = trunc_page(phdr[i].p_vaddr + 774ca0387efSJake Burkholder rbase); 775c33fe779SJohn Polstra numsegs++; 776e1743d02SSøren Schmidt } 777e1743d02SSøren Schmidt } 778c33fe779SJohn Polstra *addr = base_addr; 779c33fe779SJohn Polstra *entry = (unsigned long)hdr->e_entry + rbase; 780e1743d02SSøren Schmidt 781e1743d02SSøren Schmidt fail: 782c8a79999SPeter Wemm if (imgp->firstpage) 783c8a79999SPeter Wemm exec_unmap_first_page(imgp); 7840b2ed1aeSJeff Roberson 785911c2be0SMark Peek if (nd->ni_vp) 786373d1a3fSAlan Cox vput(nd->ni_vp); 787911c2be0SMark Peek 788911c2be0SMark Peek free(tempdata, M_TEMP); 789e1743d02SSøren Schmidt 790a7cddfedSJake Burkholder return (error); 791e1743d02SSøren Schmidt } 792e1743d02SSøren Schmidt 793fa50a355SKonstantin Belousov static u_long 794fa50a355SKonstantin Belousov __CONCAT(rnd_, __elfN(base))(vm_map_t map __unused, u_long minv, u_long maxv, 795fa50a355SKonstantin Belousov u_int align) 796fa50a355SKonstantin Belousov { 797fa50a355SKonstantin Belousov u_long rbase, res; 798fa50a355SKonstantin Belousov 799fa50a355SKonstantin Belousov MPASS(vm_map_min(map) <= minv); 800fa50a355SKonstantin Belousov MPASS(maxv <= vm_map_max(map)); 801fa50a355SKonstantin Belousov MPASS(minv < maxv); 802fa50a355SKonstantin Belousov MPASS(minv + align < maxv); 803fa50a355SKonstantin Belousov arc4rand(&rbase, sizeof(rbase), 0); 804fa50a355SKonstantin Belousov res = roundup(minv, (u_long)align) + rbase % (maxv - minv); 805fa50a355SKonstantin Belousov res &= ~((u_long)align - 1); 806fa50a355SKonstantin Belousov if (res >= maxv) 807fa50a355SKonstantin Belousov res -= align; 808fa50a355SKonstantin Belousov KASSERT(res >= minv, 809fa50a355SKonstantin Belousov ("res %#lx < minv %#lx, maxv %#lx rbase %#lx", 810fa50a355SKonstantin Belousov res, minv, maxv, rbase)); 811fa50a355SKonstantin Belousov KASSERT(res < maxv, 812fa50a355SKonstantin Belousov ("res %#lx > maxv %#lx, minv %#lx rbase %#lx", 813fa50a355SKonstantin Belousov res, maxv, minv, rbase)); 814fa50a355SKonstantin Belousov return (res); 815fa50a355SKonstantin Belousov } 816fa50a355SKonstantin Belousov 81720e1174aSEdward Tomasz Napierala static int 81820e1174aSEdward Tomasz Napierala __elfN(enforce_limits)(struct image_params *imgp, const Elf_Ehdr *hdr, 81920e1174aSEdward Tomasz Napierala const Elf_Phdr *phdr, u_long et_dyn_addr) 82020e1174aSEdward Tomasz Napierala { 82120e1174aSEdward Tomasz Napierala struct vmspace *vmspace; 82220e1174aSEdward Tomasz Napierala const char *err_str; 82320e1174aSEdward Tomasz Napierala u_long text_size, data_size, total_size, text_addr, data_addr; 82420e1174aSEdward Tomasz Napierala u_long seg_size, seg_addr; 82520e1174aSEdward Tomasz Napierala int i; 82620e1174aSEdward Tomasz Napierala 82720e1174aSEdward Tomasz Napierala err_str = NULL; 82820e1174aSEdward Tomasz Napierala text_size = data_size = total_size = text_addr = data_addr = 0; 82920e1174aSEdward Tomasz Napierala 83020e1174aSEdward Tomasz Napierala for (i = 0; i < hdr->e_phnum; i++) { 83120e1174aSEdward Tomasz Napierala if (phdr[i].p_type != PT_LOAD || phdr[i].p_memsz == 0) 83220e1174aSEdward Tomasz Napierala continue; 83320e1174aSEdward Tomasz Napierala 83420e1174aSEdward Tomasz Napierala seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr); 83520e1174aSEdward Tomasz Napierala seg_size = round_page(phdr[i].p_memsz + 83620e1174aSEdward Tomasz Napierala phdr[i].p_vaddr + et_dyn_addr - seg_addr); 83720e1174aSEdward Tomasz Napierala 83820e1174aSEdward Tomasz Napierala /* 83920e1174aSEdward Tomasz Napierala * Make the largest executable segment the official 84020e1174aSEdward Tomasz Napierala * text segment and all others data. 84120e1174aSEdward Tomasz Napierala * 84220e1174aSEdward Tomasz Napierala * Note that obreak() assumes that data_addr + data_size == end 84320e1174aSEdward Tomasz Napierala * of data load area, and the ELF file format expects segments 84420e1174aSEdward Tomasz Napierala * to be sorted by address. If multiple data segments exist, 84520e1174aSEdward Tomasz Napierala * the last one will be used. 84620e1174aSEdward Tomasz Napierala */ 84720e1174aSEdward Tomasz Napierala 84820e1174aSEdward Tomasz Napierala if ((phdr[i].p_flags & PF_X) != 0 && text_size < seg_size) { 84920e1174aSEdward Tomasz Napierala text_size = seg_size; 85020e1174aSEdward Tomasz Napierala text_addr = seg_addr; 85120e1174aSEdward Tomasz Napierala } else { 85220e1174aSEdward Tomasz Napierala data_size = seg_size; 85320e1174aSEdward Tomasz Napierala data_addr = seg_addr; 85420e1174aSEdward Tomasz Napierala } 85520e1174aSEdward Tomasz Napierala total_size += seg_size; 85620e1174aSEdward Tomasz Napierala } 85720e1174aSEdward Tomasz Napierala 85820e1174aSEdward Tomasz Napierala if (data_addr == 0 && data_size == 0) { 85920e1174aSEdward Tomasz Napierala data_addr = text_addr; 86020e1174aSEdward Tomasz Napierala data_size = text_size; 86120e1174aSEdward Tomasz Napierala } 86220e1174aSEdward Tomasz Napierala 86320e1174aSEdward Tomasz Napierala /* 86420e1174aSEdward Tomasz Napierala * Check limits. It should be safe to check the 86520e1174aSEdward Tomasz Napierala * limits after loading the segments since we do 86620e1174aSEdward Tomasz Napierala * not actually fault in all the segments pages. 86720e1174aSEdward Tomasz Napierala */ 86820e1174aSEdward Tomasz Napierala PROC_LOCK(imgp->proc); 86920e1174aSEdward Tomasz Napierala if (data_size > lim_cur_proc(imgp->proc, RLIMIT_DATA)) 87020e1174aSEdward Tomasz Napierala err_str = "Data segment size exceeds process limit"; 87120e1174aSEdward Tomasz Napierala else if (text_size > maxtsiz) 87220e1174aSEdward Tomasz Napierala err_str = "Text segment size exceeds system limit"; 87320e1174aSEdward Tomasz Napierala else if (total_size > lim_cur_proc(imgp->proc, RLIMIT_VMEM)) 87420e1174aSEdward Tomasz Napierala err_str = "Total segment size exceeds process limit"; 87520e1174aSEdward Tomasz Napierala else if (racct_set(imgp->proc, RACCT_DATA, data_size) != 0) 87620e1174aSEdward Tomasz Napierala err_str = "Data segment size exceeds resource limit"; 87720e1174aSEdward Tomasz Napierala else if (racct_set(imgp->proc, RACCT_VMEM, total_size) != 0) 87820e1174aSEdward Tomasz Napierala err_str = "Total segment size exceeds resource limit"; 87920e1174aSEdward Tomasz Napierala PROC_UNLOCK(imgp->proc); 88020e1174aSEdward Tomasz Napierala if (err_str != NULL) { 88120e1174aSEdward Tomasz Napierala uprintf("%s\n", err_str); 88220e1174aSEdward Tomasz Napierala return (ENOMEM); 88320e1174aSEdward Tomasz Napierala } 88420e1174aSEdward Tomasz Napierala 88520e1174aSEdward Tomasz Napierala vmspace = imgp->proc->p_vmspace; 88620e1174aSEdward Tomasz Napierala vmspace->vm_tsize = text_size >> PAGE_SHIFT; 88720e1174aSEdward Tomasz Napierala vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr; 88820e1174aSEdward Tomasz Napierala vmspace->vm_dsize = data_size >> PAGE_SHIFT; 88920e1174aSEdward Tomasz Napierala vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr; 89020e1174aSEdward Tomasz Napierala 89120e1174aSEdward Tomasz Napierala return (0); 89220e1174aSEdward Tomasz Napierala } 89320e1174aSEdward Tomasz Napierala 894*09c78d53SEdward Tomasz Napierala static int 895*09c78d53SEdward Tomasz Napierala __elfN(get_interp)(struct image_params *imgp, const Elf_Phdr *phdr, 896*09c78d53SEdward Tomasz Napierala char **interpp, bool *free_interpp) 897*09c78d53SEdward Tomasz Napierala { 898*09c78d53SEdward Tomasz Napierala struct thread *td; 899*09c78d53SEdward Tomasz Napierala char *interp; 900*09c78d53SEdward Tomasz Napierala int error, interp_name_len; 901*09c78d53SEdward Tomasz Napierala 902*09c78d53SEdward Tomasz Napierala KASSERT(phdr->p_type == PT_INTERP, 903*09c78d53SEdward Tomasz Napierala ("%s: p_type %u != PT_INTERP", __func__, phdr->p_type)); 904*09c78d53SEdward Tomasz Napierala KASSERT(VOP_ISLOCKED(imgp->vp), 905*09c78d53SEdward Tomasz Napierala ("%s: vp %p is not locked", __func__, imgp->vp)); 906*09c78d53SEdward Tomasz Napierala 907*09c78d53SEdward Tomasz Napierala td = curthread; 908*09c78d53SEdward Tomasz Napierala 909*09c78d53SEdward Tomasz Napierala /* Path to interpreter */ 910*09c78d53SEdward Tomasz Napierala if (phdr->p_filesz < 2 || phdr->p_filesz > MAXPATHLEN) { 911*09c78d53SEdward Tomasz Napierala uprintf("Invalid PT_INTERP\n"); 912*09c78d53SEdward Tomasz Napierala return (ENOEXEC); 913*09c78d53SEdward Tomasz Napierala } 914*09c78d53SEdward Tomasz Napierala 915*09c78d53SEdward Tomasz Napierala interp_name_len = phdr->p_filesz; 916*09c78d53SEdward Tomasz Napierala if (phdr->p_offset > PAGE_SIZE || 917*09c78d53SEdward Tomasz Napierala interp_name_len > PAGE_SIZE - phdr->p_offset) { 918*09c78d53SEdward Tomasz Napierala VOP_UNLOCK(imgp->vp, 0); 919*09c78d53SEdward Tomasz Napierala interp = malloc(interp_name_len + 1, M_TEMP, M_WAITOK); 920*09c78d53SEdward Tomasz Napierala vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 921*09c78d53SEdward Tomasz Napierala error = vn_rdwr(UIO_READ, imgp->vp, interp, 922*09c78d53SEdward Tomasz Napierala interp_name_len, phdr->p_offset, 923*09c78d53SEdward Tomasz Napierala UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, 924*09c78d53SEdward Tomasz Napierala NOCRED, NULL, td); 925*09c78d53SEdward Tomasz Napierala if (error != 0) { 926*09c78d53SEdward Tomasz Napierala free(interp, M_TEMP); 927*09c78d53SEdward Tomasz Napierala uprintf("i/o error PT_INTERP %d\n", error); 928*09c78d53SEdward Tomasz Napierala return (error); 929*09c78d53SEdward Tomasz Napierala } 930*09c78d53SEdward Tomasz Napierala interp[interp_name_len] = '\0'; 931*09c78d53SEdward Tomasz Napierala 932*09c78d53SEdward Tomasz Napierala *interpp = interp; 933*09c78d53SEdward Tomasz Napierala *free_interpp = true; 934*09c78d53SEdward Tomasz Napierala return (0); 935*09c78d53SEdward Tomasz Napierala } 936*09c78d53SEdward Tomasz Napierala 937*09c78d53SEdward Tomasz Napierala interp = __DECONST(char *, imgp->image_header) + phdr->p_offset; 938*09c78d53SEdward Tomasz Napierala if (interp[interp_name_len - 1] != '\0') { 939*09c78d53SEdward Tomasz Napierala uprintf("Invalid PT_INTERP\n"); 940*09c78d53SEdward Tomasz Napierala return (ENOEXEC); 941*09c78d53SEdward Tomasz Napierala } 942*09c78d53SEdward Tomasz Napierala 943*09c78d53SEdward Tomasz Napierala *interpp = interp; 944*09c78d53SEdward Tomasz Napierala *free_interpp = false; 945*09c78d53SEdward Tomasz Napierala return (0); 946*09c78d53SEdward Tomasz Napierala } 947*09c78d53SEdward Tomasz Napierala 948fa50a355SKonstantin Belousov /* 949fa50a355SKonstantin Belousov * Impossible et_dyn_addr initial value indicating that the real base 950fa50a355SKonstantin Belousov * must be calculated later with some randomization applied. 951fa50a355SKonstantin Belousov */ 952fa50a355SKonstantin Belousov #define ET_DYN_ADDR_RAND 1 953fa50a355SKonstantin Belousov 954303b270bSEivind Eklund static int 9553ebc1248SPeter Wemm __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp) 956e1743d02SSøren Schmidt { 9576c775eb6SKonstantin Belousov struct thread *td; 9586c775eb6SKonstantin Belousov const Elf_Ehdr *hdr; 95932c01de2SDmitry Chagin const Elf_Phdr *phdr; 960e5e6093bSAlan Cox Elf_Auxargs *elf_auxargs; 9615856e12eSJohn Dyson struct vmspace *vmspace; 962fa50a355SKonstantin Belousov vm_map_t map; 96320e1174aSEdward Tomasz Napierala const char *newinterp; 964*09c78d53SEdward Tomasz Napierala char *interp, *path; 965d1dbc694SJohn Polstra Elf_Brandinfo *brand_info; 9665fe3ed62SJake Burkholder struct sysentvec *sv; 9676c775eb6SKonstantin Belousov vm_prot_t prot; 96820e1174aSEdward Tomasz Napierala u_long addr, baddr, et_dyn_addr, entry, proghdr; 969fa50a355SKonstantin Belousov u_long maxalign, mapsz, maxv, maxv1; 970cefb93f2SKonstantin Belousov uint32_t fctl0; 9716c775eb6SKonstantin Belousov int32_t osrel; 972*09c78d53SEdward Tomasz Napierala bool free_interp; 973*09c78d53SEdward Tomasz Napierala int error, i, n, have_interp; 9746c775eb6SKonstantin Belousov 9756c775eb6SKonstantin Belousov hdr = (const Elf_Ehdr *)imgp->image_header; 976e1743d02SSøren Schmidt 977e1743d02SSøren Schmidt /* 978e1743d02SSøren Schmidt * Do we have a valid ELF header ? 979900b28f9SMaxim Sobolev * 980900b28f9SMaxim Sobolev * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later 981900b28f9SMaxim Sobolev * if particular brand doesn't support it. 982e1743d02SSøren Schmidt */ 983900b28f9SMaxim Sobolev if (__elfN(check_header)(hdr) != 0 || 984900b28f9SMaxim Sobolev (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)) 985a7cddfedSJake Burkholder return (-1); 986e1743d02SSøren Schmidt 987e1743d02SSøren Schmidt /* 988e1743d02SSøren Schmidt * From here on down, we return an errno, not -1, as we've 989e1743d02SSøren Schmidt * detected an ELF file. 990e1743d02SSøren Schmidt */ 991e1743d02SSøren Schmidt 992e1743d02SSøren Schmidt if ((hdr->e_phoff > PAGE_SIZE) || 993d19d5bf4STijl Coosemans (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) { 994c8a79999SPeter Wemm /* Only support headers in first page for now */ 9956b16d664SEd Maste uprintf("Program headers not in the first page\n"); 996a7cddfedSJake Burkholder return (ENOEXEC); 997e1743d02SSøren Schmidt } 99852c24af7SPeter Wemm phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 9996b16d664SEd Maste if (!aligned(phdr, Elf_Addr)) { 10006b16d664SEd Maste uprintf("Unaligned program headers\n"); 100193d1c728SKonstantin Belousov return (ENOEXEC); 10026b16d664SEd Maste } 10036c775eb6SKonstantin Belousov 10046c775eb6SKonstantin Belousov n = error = 0; 10057564c4adSKonstantin Belousov baddr = 0; 10066c775eb6SKonstantin Belousov osrel = 0; 1007cefb93f2SKonstantin Belousov fctl0 = 0; 10086c775eb6SKonstantin Belousov entry = proghdr = 0; 1009*09c78d53SEdward Tomasz Napierala newinterp = interp = NULL; 1010*09c78d53SEdward Tomasz Napierala free_interp = false; 10116c775eb6SKonstantin Belousov td = curthread; 1012fa50a355SKonstantin Belousov maxalign = PAGE_SIZE; 1013fa50a355SKonstantin Belousov mapsz = 0; 10146c775eb6SKonstantin Belousov 10155fe3ed62SJake Burkholder for (i = 0; i < hdr->e_phnum; i++) { 1016291c06a1SKonstantin Belousov switch (phdr[i].p_type) { 1017291c06a1SKonstantin Belousov case PT_LOAD: 10187564c4adSKonstantin Belousov if (n == 0) 10197564c4adSKonstantin Belousov baddr = phdr[i].p_vaddr; 1020fa50a355SKonstantin Belousov if (phdr[i].p_align > maxalign) 1021fa50a355SKonstantin Belousov maxalign = phdr[i].p_align; 1022fa50a355SKonstantin Belousov mapsz += phdr[i].p_memsz; 10237564c4adSKonstantin Belousov n++; 1024291c06a1SKonstantin Belousov break; 1025291c06a1SKonstantin Belousov case PT_INTERP: 1026e5e6093bSAlan Cox /* Path to interpreter */ 1027d3ee0a15SJonathan T. Looney if (interp != NULL) { 1028d3ee0a15SJonathan T. Looney uprintf("Multiple PT_INTERP headers\n"); 1029d3ee0a15SJonathan T. Looney error = ENOEXEC; 1030d3ee0a15SJonathan T. Looney goto ret; 1031d3ee0a15SJonathan T. Looney } 1032*09c78d53SEdward Tomasz Napierala error = __elfN(get_interp)(imgp, &phdr[i], &interp, 1033*09c78d53SEdward Tomasz Napierala &free_interp); 1034*09c78d53SEdward Tomasz Napierala if (error != 0) 10356c775eb6SKonstantin Belousov goto ret; 1036291c06a1SKonstantin Belousov break; 1037291c06a1SKonstantin Belousov case PT_GNU_STACK: 1038291c06a1SKonstantin Belousov if (__elfN(nxstack)) 1039291c06a1SKonstantin Belousov imgp->stack_prot = 1040291c06a1SKonstantin Belousov __elfN(trans_prot)(phdr[i].p_flags); 1041316b3843SKonstantin Belousov imgp->stack_sz = phdr[i].p_memsz; 1042291c06a1SKonstantin Belousov break; 10433ebc1248SPeter Wemm } 10443ebc1248SPeter Wemm } 10453ebc1248SPeter Wemm 1046*09c78d53SEdward Tomasz Napierala brand_info = __elfN(get_brandinfo)(imgp, interp, &osrel, &fctl0); 10475fe3ed62SJake Burkholder if (brand_info == NULL) { 10485fe3ed62SJake Burkholder uprintf("ELF binary type \"%u\" not known.\n", 10495fe3ed62SJake Burkholder hdr->e_ident[EI_OSABI]); 10506c775eb6SKonstantin Belousov error = ENOEXEC; 10516c775eb6SKonstantin Belousov goto ret; 10523ebc1248SPeter Wemm } 1053fa50a355SKonstantin Belousov sv = brand_info->sysvec; 105477ebe276SEd Maste et_dyn_addr = 0; 1055ab02d85fSKonstantin Belousov if (hdr->e_type == ET_DYN) { 10566b16d664SEd Maste if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) { 10576b16d664SEd Maste uprintf("Cannot execute shared object\n"); 10586c775eb6SKonstantin Belousov error = ENOEXEC; 10596c775eb6SKonstantin Belousov goto ret; 10606b16d664SEd Maste } 10617564c4adSKonstantin Belousov /* 10627564c4adSKonstantin Belousov * Honour the base load address from the dso if it is 10637564c4adSKonstantin Belousov * non-zero for some reason. 10647564c4adSKonstantin Belousov */ 1065fa50a355SKonstantin Belousov if (baddr == 0) { 1066fa50a355SKonstantin Belousov if ((sv->sv_flags & SV_ASLR) == 0 || 1067fa50a355SKonstantin Belousov (fctl0 & NT_FREEBSD_FCTL_ASLR_DISABLE) != 0) 1068fa50a355SKonstantin Belousov et_dyn_addr = ET_DYN_LOAD_ADDR; 1069fa50a355SKonstantin Belousov else if ((__elfN(pie_aslr_enabled) && 1070fa50a355SKonstantin Belousov (imgp->proc->p_flag2 & P2_ASLR_DISABLE) == 0) || 1071fa50a355SKonstantin Belousov (imgp->proc->p_flag2 & P2_ASLR_ENABLE) != 0) 1072fa50a355SKonstantin Belousov et_dyn_addr = ET_DYN_ADDR_RAND; 1073fa50a355SKonstantin Belousov else 1074ab02d85fSKonstantin Belousov et_dyn_addr = ET_DYN_LOAD_ADDR; 107577ebe276SEd Maste } 1076fa50a355SKonstantin Belousov } 10779b68618dSPeter Wemm if (interp != NULL && brand_info->interp_newpath != NULL) 10784113f8d7SPeter Wemm newinterp = brand_info->interp_newpath; 10793ebc1248SPeter Wemm 108060bb3943SAlan Cox /* 108160bb3943SAlan Cox * Avoid a possible deadlock if the current address space is destroyed 108260bb3943SAlan Cox * and that address space maps the locked vnode. In the common case, 108360bb3943SAlan Cox * the locked vnode's v_usecount is decremented but remains greater 108460bb3943SAlan Cox * than zero. Consequently, the vnode lock is not needed by vrele(). 108560bb3943SAlan Cox * However, in cases where the vnode lock is external, such as nullfs, 108660bb3943SAlan Cox * v_usecount may become zero. 10871dfab802SAlan Cox * 10881dfab802SAlan Cox * The VV_TEXT flag prevents modifications to the executable while 10891dfab802SAlan Cox * the vnode is unlocked. 109060bb3943SAlan Cox */ 109122db15c0SAttilio Rao VOP_UNLOCK(imgp->vp, 0); 109260bb3943SAlan Cox 1093fa50a355SKonstantin Belousov /* 1094fa50a355SKonstantin Belousov * Decide whether to enable randomization of user mappings. 1095fa50a355SKonstantin Belousov * First, reset user preferences for the setid binaries. 1096fa50a355SKonstantin Belousov * Then, account for the support of the randomization by the 1097fa50a355SKonstantin Belousov * ABI, by user preferences, and make special treatment for 1098fa50a355SKonstantin Belousov * PIE binaries. 1099fa50a355SKonstantin Belousov */ 1100fa50a355SKonstantin Belousov if (imgp->credential_setid) { 1101fa50a355SKonstantin Belousov PROC_LOCK(imgp->proc); 1102fa50a355SKonstantin Belousov imgp->proc->p_flag2 &= ~(P2_ASLR_ENABLE | P2_ASLR_DISABLE); 1103fa50a355SKonstantin Belousov PROC_UNLOCK(imgp->proc); 1104fa50a355SKonstantin Belousov } 1105fa50a355SKonstantin Belousov if ((sv->sv_flags & SV_ASLR) == 0 || 1106fa50a355SKonstantin Belousov (imgp->proc->p_flag2 & P2_ASLR_DISABLE) != 0 || 1107fa50a355SKonstantin Belousov (fctl0 & NT_FREEBSD_FCTL_ASLR_DISABLE) != 0) { 1108fa50a355SKonstantin Belousov KASSERT(et_dyn_addr != ET_DYN_ADDR_RAND, 1109fa50a355SKonstantin Belousov ("et_dyn_addr == RAND and !ASLR")); 1110fa50a355SKonstantin Belousov } else if ((imgp->proc->p_flag2 & P2_ASLR_ENABLE) != 0 || 1111fa50a355SKonstantin Belousov (__elfN(aslr_enabled) && hdr->e_type == ET_EXEC) || 1112fa50a355SKonstantin Belousov et_dyn_addr == ET_DYN_ADDR_RAND) { 1113fa50a355SKonstantin Belousov imgp->map_flags |= MAP_ASLR; 1114fa50a355SKonstantin Belousov /* 1115fa50a355SKonstantin Belousov * If user does not care about sbrk, utilize the bss 1116fa50a355SKonstantin Belousov * grow region for mappings as well. We can select 1117fa50a355SKonstantin Belousov * the base for the image anywere and still not suffer 1118fa50a355SKonstantin Belousov * from the fragmentation. 1119fa50a355SKonstantin Belousov */ 1120fa50a355SKonstantin Belousov if (!__elfN(aslr_honor_sbrk) || 1121fa50a355SKonstantin Belousov (imgp->proc->p_flag2 & P2_ASLR_IGNSTART) != 0) 1122fa50a355SKonstantin Belousov imgp->map_flags |= MAP_ASLR_IGNSTART; 1123fa50a355SKonstantin Belousov } 1124fa50a355SKonstantin Belousov 112589b57fcfSKonstantin Belousov error = exec_new_vmspace(imgp, sv); 1126fa50a355SKonstantin Belousov vmspace = imgp->proc->p_vmspace; 1127fa50a355SKonstantin Belousov map = &vmspace->vm_map; 1128fa50a355SKonstantin Belousov 112919059a13SJohn Baldwin imgp->proc->p_sysent = sv; 1130e1743d02SSøren Schmidt 1131fa50a355SKonstantin Belousov maxv = vm_map_max(map) - lim_max(td, RLIMIT_STACK); 1132fa50a355SKonstantin Belousov if (et_dyn_addr == ET_DYN_ADDR_RAND) { 1133fa50a355SKonstantin Belousov KASSERT((map->flags & MAP_ASLR) != 0, 1134fa50a355SKonstantin Belousov ("ET_DYN_ADDR_RAND but !MAP_ASLR")); 1135fa50a355SKonstantin Belousov et_dyn_addr = __CONCAT(rnd_, __elfN(base))(map, 1136fa50a355SKonstantin Belousov vm_map_min(map) + mapsz + lim_max(td, RLIMIT_DATA), 1137fa50a355SKonstantin Belousov /* reserve half of the address space to interpreter */ 1138fa50a355SKonstantin Belousov maxv / 2, 1UL << flsl(maxalign)); 1139fa50a355SKonstantin Belousov } 1140fa50a355SKonstantin Belousov 1141cb05b60aSAttilio Rao vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 11426c775eb6SKonstantin Belousov if (error != 0) 11436c775eb6SKonstantin Belousov goto ret; 114460bb3943SAlan Cox 1145e1743d02SSøren Schmidt for (i = 0; i < hdr->e_phnum; i++) { 1146e1743d02SSøren Schmidt switch (phdr[i].p_type) { 1147e1743d02SSøren Schmidt case PT_LOAD: /* Loadable segment */ 11485b33842aSKonstantin Belousov if (phdr[i].p_memsz == 0) 11495b33842aSKonstantin Belousov break; 1150ed167eaaSKonstantin Belousov prot = __elfN(trans_prot)(phdr[i].p_flags); 1151292177e6SAlan Cox error = __elfN(load_section)(imgp, phdr[i].p_offset, 1152ab02d85fSKonstantin Belousov (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr, 11531699546dSEdward Tomasz Napierala phdr[i].p_memsz, phdr[i].p_filesz, prot); 1154292177e6SAlan Cox if (error != 0) 11556c775eb6SKonstantin Belousov goto ret; 1156e1743d02SSøren Schmidt 1157cfaf7e60SDoug Rabson /* 1158cfaf7e60SDoug Rabson * If this segment contains the program headers, 1159cfaf7e60SDoug Rabson * remember their virtual address for the AT_PHDR 1160cfaf7e60SDoug Rabson * aux entry. Static binaries don't usually include 1161cfaf7e60SDoug Rabson * a PT_PHDR entry. 1162cfaf7e60SDoug Rabson */ 1163cfaf7e60SDoug Rabson if (phdr[i].p_offset == 0 && 1164cfaf7e60SDoug Rabson hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize 1165cfaf7e60SDoug Rabson <= phdr[i].p_filesz) 1166ab02d85fSKonstantin Belousov proghdr = phdr[i].p_vaddr + hdr->e_phoff + 1167ab02d85fSKonstantin Belousov et_dyn_addr; 116896725dd0SAlexander Kabaev break; 116996725dd0SAlexander Kabaev case PT_PHDR: /* Program header table info */ 1170ab02d85fSKonstantin Belousov proghdr = phdr[i].p_vaddr + et_dyn_addr; 117196725dd0SAlexander Kabaev break; 117296725dd0SAlexander Kabaev default: 117396725dd0SAlexander Kabaev break; 117496725dd0SAlexander Kabaev } 117596725dd0SAlexander Kabaev } 117696725dd0SAlexander Kabaev 117720e1174aSEdward Tomasz Napierala error = __elfN(enforce_limits)(imgp, hdr, phdr, et_dyn_addr); 117820e1174aSEdward Tomasz Napierala if (error != 0) 117920e1174aSEdward Tomasz Napierala goto ret; 1180cac45152SMatthew Dillon 1181920acedbSNathan Whitehorn entry = (u_long)hdr->e_entry + et_dyn_addr; 1182920acedbSNathan Whitehorn 1183cac45152SMatthew Dillon /* 1184c460ac3aSPeter Wemm * We load the dynamic linker where a userland call 1185c460ac3aSPeter Wemm * to mmap(0, ...) would put it. The rationale behind this 1186c460ac3aSPeter Wemm * calculation is that it leaves room for the heap to grow to 1187c460ac3aSPeter Wemm * its maximum allowed size. 1188c460ac3aSPeter Wemm */ 11896c775eb6SKonstantin Belousov addr = round_page((vm_offset_t)vmspace->vm_daddr + lim_max(td, 1190292177e6SAlan Cox RLIMIT_DATA)); 1191fa50a355SKonstantin Belousov if ((map->flags & MAP_ASLR) != 0) { 1192fa50a355SKonstantin Belousov maxv1 = maxv / 2 + addr / 2; 1193fa50a355SKonstantin Belousov MPASS(maxv1 >= addr); /* No overflow */ 1194fa50a355SKonstantin Belousov map->anon_loc = __CONCAT(rnd_, __elfN(base))(map, addr, maxv1, 1195fa50a355SKonstantin Belousov MAXPAGESIZES > 1 ? pagesizes[1] : pagesizes[0]); 1196fa50a355SKonstantin Belousov } else { 1197fa50a355SKonstantin Belousov map->anon_loc = addr; 1198fa50a355SKonstantin Belousov } 1199e1743d02SSøren Schmidt 1200ea5a2b2eSSøren Schmidt imgp->entry_addr = entry; 1201ea5a2b2eSSøren Schmidt 120260bb3943SAlan Cox if (interp != NULL) { 12036c775eb6SKonstantin Belousov have_interp = FALSE; 120422db15c0SAttilio Rao VOP_UNLOCK(imgp->vp, 0); 1205fa50a355SKonstantin Belousov if ((map->flags & MAP_ASLR) != 0) { 1206fa50a355SKonstantin Belousov /* Assume that interpeter fits into 1/4 of AS */ 1207fa50a355SKonstantin Belousov maxv1 = maxv / 2 + addr / 2; 1208fa50a355SKonstantin Belousov MPASS(maxv1 >= addr); /* No overflow */ 1209fa50a355SKonstantin Belousov addr = __CONCAT(rnd_, __elfN(base))(map, addr, 1210fa50a355SKonstantin Belousov maxv1, PAGE_SIZE); 1211fa50a355SKonstantin Belousov } 121260bb3943SAlan Cox if (brand_info->emul_path != NULL && 12139b68618dSPeter Wemm brand_info->emul_path[0] != '\0') { 1214a163d034SWarner Losh path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 121560bb3943SAlan Cox snprintf(path, MAXPATHLEN, "%s%s", 121660bb3943SAlan Cox brand_info->emul_path, interp); 12179b68618dSPeter Wemm error = __elfN(load_file)(imgp->proc, path, &addr, 12181699546dSEdward Tomasz Napierala &imgp->entry_addr); 1219911c2be0SMark Peek free(path, M_TEMP); 12209b68618dSPeter Wemm if (error == 0) 12214113f8d7SPeter Wemm have_interp = TRUE; 12229b68618dSPeter Wemm } 122318995077SKonstantin Belousov if (!have_interp && newinterp != NULL && 122418995077SKonstantin Belousov (brand_info->interp_path == NULL || 122518995077SKonstantin Belousov strcmp(interp, brand_info->interp_path) == 0)) { 12264113f8d7SPeter Wemm error = __elfN(load_file)(imgp->proc, newinterp, &addr, 12271699546dSEdward Tomasz Napierala &imgp->entry_addr); 1228387ad998SKonstantin Belousov if (error == 0) 12294113f8d7SPeter Wemm have_interp = TRUE; 12304113f8d7SPeter Wemm } 12314113f8d7SPeter Wemm if (!have_interp) { 12329b68618dSPeter Wemm error = __elfN(load_file)(imgp->proc, interp, &addr, 12331699546dSEdward Tomasz Napierala &imgp->entry_addr); 123460bb3943SAlan Cox } 1235cb05b60aSAttilio Rao vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 12369b68618dSPeter Wemm if (error != 0) { 1237d943fa35SKonstantin Belousov uprintf("ELF interpreter %s not found, error %d\n", 1238d943fa35SKonstantin Belousov interp, error); 12396c775eb6SKonstantin Belousov goto ret; 1240e1743d02SSøren Schmidt } 124195c807cfSRobert Watson } else 12427564c4adSKonstantin Belousov addr = et_dyn_addr; 1243ea5a2b2eSSøren Schmidt 1244e1743d02SSøren Schmidt /* 1245e1743d02SSøren Schmidt * Construct auxargs table (used by the fixup routine) 1246e1743d02SSøren Schmidt */ 1247a163d034SWarner Losh elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK); 1248e1743d02SSøren Schmidt elf_auxargs->execfd = -1; 1249e1743d02SSøren Schmidt elf_auxargs->phdr = proghdr; 1250e1743d02SSøren Schmidt elf_auxargs->phent = hdr->e_phentsize; 1251e1743d02SSøren Schmidt elf_auxargs->phnum = hdr->e_phnum; 1252e1743d02SSøren Schmidt elf_auxargs->pagesz = PAGE_SIZE; 1253e1743d02SSøren Schmidt elf_auxargs->base = addr; 1254e1743d02SSøren Schmidt elf_auxargs->flags = 0; 1255e1743d02SSøren Schmidt elf_auxargs->entry = entry; 1256d36eec69SWarner Losh elf_auxargs->hdr_eflags = hdr->e_flags; 1257e1743d02SSøren Schmidt 1258e1743d02SSøren Schmidt imgp->auxargs = elf_auxargs; 1259e1743d02SSøren Schmidt imgp->interpreted = 0; 1260a0ea661fSNathan Whitehorn imgp->reloc_base = addr; 126132c01de2SDmitry Chagin imgp->proc->p_osrel = osrel; 1262cefb93f2SKonstantin Belousov imgp->proc->p_fctl0 = fctl0; 1263885f13dcSJohn Baldwin imgp->proc->p_elf_machine = hdr->e_machine; 1264885f13dcSJohn Baldwin imgp->proc->p_elf_flags = hdr->e_flags; 1265f231de47SKonstantin Belousov 12666c775eb6SKonstantin Belousov ret: 1267*09c78d53SEdward Tomasz Napierala if (free_interp) 1268*09c78d53SEdward Tomasz Napierala free(interp, M_TEMP); 1269a7cddfedSJake Burkholder return (error); 1270e1743d02SSøren Schmidt } 1271e1743d02SSøren Schmidt 1272a360a43dSJake Burkholder #define suword __CONCAT(suword, __ELF_WORD_SIZE) 12733ebc1248SPeter Wemm 12743ebc1248SPeter Wemm int 12753ebc1248SPeter Wemm __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp) 1276e1743d02SSøren Schmidt { 1277ecbb00a2SDoug Rabson Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs; 12785f77b8a8SBrooks Davis Elf_Auxinfo *argarray, *pos; 12795f77b8a8SBrooks Davis Elf_Addr *base, *auxbase; 12805f77b8a8SBrooks Davis int error; 1281e1743d02SSøren Schmidt 1282a360a43dSJake Burkholder base = (Elf_Addr *)*stack_base; 12835f77b8a8SBrooks Davis auxbase = base + imgp->args->argc + 1 + imgp->args->envc + 1; 12845f77b8a8SBrooks Davis argarray = pos = malloc(AT_COUNT * sizeof(*pos), M_TEMP, 12855f77b8a8SBrooks Davis M_WAITOK | M_ZERO); 1286e1743d02SSøren Schmidt 128735c2a5a8SWarner Losh if (args->execfd != -1) 1288e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 1289e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 1290e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 1291e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 1292e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 1293e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 1294e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 1295e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_BASE, args->base); 1296d36eec69SWarner Losh AUXARGS_ENTRY(pos, AT_EHDRFLAGS, args->hdr_eflags); 12973ff06357SKonstantin Belousov if (imgp->execpathp != 0) 12983ff06357SKonstantin Belousov AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp); 1299b96bd95bSIan Lepore AUXARGS_ENTRY(pos, AT_OSRELDATE, 1300b96bd95bSIan Lepore imgp->proc->p_ucred->cr_prison->pr_osreldate); 1301ee235befSKonstantin Belousov if (imgp->canary != 0) { 1302ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary); 1303ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen); 1304ee235befSKonstantin Belousov } 1305ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus); 1306ee235befSKonstantin Belousov if (imgp->pagesizes != 0) { 1307ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes); 1308ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen); 1309ee235befSKonstantin Belousov } 1310aea81038SKonstantin Belousov if (imgp->sysent->sv_timekeep_base != 0) { 1311aea81038SKonstantin Belousov AUXARGS_ENTRY(pos, AT_TIMEKEEP, 1312aea81038SKonstantin Belousov imgp->sysent->sv_timekeep_base); 1313aea81038SKonstantin Belousov } 131426d8f3e1SKonstantin Belousov AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj 131526d8f3e1SKonstantin Belousov != NULL && imgp->stack_prot != 0 ? imgp->stack_prot : 131626d8f3e1SKonstantin Belousov imgp->sysent->sv_stackprot); 1317c2f37b92SJohn Baldwin if (imgp->sysent->sv_hwcap != NULL) 1318c2f37b92SJohn Baldwin AUXARGS_ENTRY(pos, AT_HWCAP, *imgp->sysent->sv_hwcap); 1319904d8c49SMichal Meloun if (imgp->sysent->sv_hwcap2 != NULL) 1320904d8c49SMichal Meloun AUXARGS_ENTRY(pos, AT_HWCAP2, *imgp->sysent->sv_hwcap2); 1321e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_NULL, 0); 1322e1743d02SSøren Schmidt 1323e1743d02SSøren Schmidt free(imgp->auxargs, M_TEMP); 1324e1743d02SSøren Schmidt imgp->auxargs = NULL; 1325d8b2f079SBrooks Davis KASSERT(pos - argarray <= AT_COUNT, ("Too many auxargs")); 13265f77b8a8SBrooks Davis 13275f77b8a8SBrooks Davis error = copyout(argarray, auxbase, sizeof(*argarray) * AT_COUNT); 13285f77b8a8SBrooks Davis free(argarray, M_TEMP); 13295f77b8a8SBrooks Davis if (error != 0) 13305f77b8a8SBrooks Davis return (error); 1331e1743d02SSøren Schmidt 13323ebc1248SPeter Wemm base--; 13335f77b8a8SBrooks Davis if (suword(base, imgp->args->argc) == -1) 13345f77b8a8SBrooks Davis return (EFAULT); 13353ebc1248SPeter Wemm *stack_base = (register_t *)base; 1336a7cddfedSJake Burkholder return (0); 1337e1743d02SSøren Schmidt } 1338e1743d02SSøren Schmidt 1339e1743d02SSøren Schmidt /* 13408c64af4fSJohn Polstra * Code for generating ELF core dumps. 13418c64af4fSJohn Polstra */ 13428c64af4fSJohn Polstra 13434d77a549SAlfred Perlstein typedef void (*segment_callback)(vm_map_entry_t, void *); 13440ff27d31SJohn Polstra 13450ff27d31SJohn Polstra /* Closure for cb_put_phdr(). */ 13460ff27d31SJohn Polstra struct phdr_closure { 13470ff27d31SJohn Polstra Elf_Phdr *phdr; /* Program header to fill in */ 13480ff27d31SJohn Polstra Elf_Off offset; /* Offset of segment in core file */ 13490ff27d31SJohn Polstra }; 13500ff27d31SJohn Polstra 13510ff27d31SJohn Polstra /* Closure for cb_size_segment(). */ 13520ff27d31SJohn Polstra struct sseg_closure { 13530ff27d31SJohn Polstra int count; /* Count of writable segments. */ 13540ff27d31SJohn Polstra size_t size; /* Total size of all writable segments. */ 13550ff27d31SJohn Polstra }; 13560ff27d31SJohn Polstra 1357bd390213SMikolaj Golub typedef void (*outfunc_t)(void *, struct sbuf *, size_t *); 1358bd390213SMikolaj Golub 1359bd390213SMikolaj Golub struct note_info { 1360bd390213SMikolaj Golub int type; /* Note type. */ 1361bd390213SMikolaj Golub outfunc_t outfunc; /* Output function. */ 1362bd390213SMikolaj Golub void *outarg; /* Argument for the output function. */ 1363bd390213SMikolaj Golub size_t outsize; /* Output size. */ 1364bd390213SMikolaj Golub TAILQ_ENTRY(note_info) link; /* Link to the next note info. */ 1365bd390213SMikolaj Golub }; 1366bd390213SMikolaj Golub 1367bd390213SMikolaj Golub TAILQ_HEAD(note_info_list, note_info); 1368bd390213SMikolaj Golub 1369aa14e9b7SMark Johnston /* Coredump output parameters. */ 1370aa14e9b7SMark Johnston struct coredump_params { 1371aa14e9b7SMark Johnston off_t offset; 1372aa14e9b7SMark Johnston struct ucred *active_cred; 1373aa14e9b7SMark Johnston struct ucred *file_cred; 1374aa14e9b7SMark Johnston struct thread *td; 1375aa14e9b7SMark Johnston struct vnode *vp; 137678f57a9cSMark Johnston struct compressor *comp; 1377aa14e9b7SMark Johnston }; 1378aa14e9b7SMark Johnston 137978f57a9cSMark Johnston extern int compress_user_cores; 138078f57a9cSMark Johnston extern int compress_user_cores_level; 138178f57a9cSMark Johnston 13824d77a549SAlfred Perlstein static void cb_put_phdr(vm_map_entry_t, void *); 13834d77a549SAlfred Perlstein static void cb_size_segment(vm_map_entry_t, void *); 1384c468ff88SAndriy Gapon static int core_write(struct coredump_params *, const void *, size_t, off_t, 1385aa14e9b7SMark Johnston enum uio_seg); 13861005d8afSConrad Meyer static void each_dumpable_segment(struct thread *, segment_callback, void *); 1387aa14e9b7SMark Johnston static int __elfN(corehdr)(struct coredump_params *, int, void *, size_t, 1388aa14e9b7SMark Johnston struct note_info_list *, size_t); 1389bd390213SMikolaj Golub static void __elfN(prepare_notes)(struct thread *, struct note_info_list *, 1390bd390213SMikolaj Golub size_t *); 1391bd390213SMikolaj Golub static void __elfN(puthdr)(struct thread *, void *, size_t, int, size_t); 1392bd390213SMikolaj Golub static void __elfN(putnote)(struct note_info *, struct sbuf *); 1393bd390213SMikolaj Golub static size_t register_note(struct note_info_list *, int, outfunc_t, void *); 1394bd390213SMikolaj Golub static int sbuf_drain_core_output(void *, const char *, int); 1395f1fca82eSMikolaj Golub static int sbuf_drain_count(void *arg, const char *data, int len); 1396bd390213SMikolaj Golub 1397bd390213SMikolaj Golub static void __elfN(note_fpregset)(void *, struct sbuf *, size_t *); 1398bd390213SMikolaj Golub static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *); 1399bd390213SMikolaj Golub static void __elfN(note_prstatus)(void *, struct sbuf *, size_t *); 1400bd390213SMikolaj Golub static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *); 1401bd390213SMikolaj Golub static void __elfN(note_thrmisc)(void *, struct sbuf *, size_t *); 140286be94fcSTycho Nightingale static void __elfN(note_ptlwpinfo)(void *, struct sbuf *, size_t *); 1403f1fca82eSMikolaj Golub static void __elfN(note_procstat_auxv)(void *, struct sbuf *, size_t *); 1404f1fca82eSMikolaj Golub static void __elfN(note_procstat_proc)(void *, struct sbuf *, size_t *); 1405f1fca82eSMikolaj Golub static void __elfN(note_procstat_psstrings)(void *, struct sbuf *, size_t *); 1406f1fca82eSMikolaj Golub static void note_procstat_files(void *, struct sbuf *, size_t *); 1407f1fca82eSMikolaj Golub static void note_procstat_groups(void *, struct sbuf *, size_t *); 1408f1fca82eSMikolaj Golub static void note_procstat_osrel(void *, struct sbuf *, size_t *); 1409f1fca82eSMikolaj Golub static void note_procstat_rlimit(void *, struct sbuf *, size_t *); 1410f1fca82eSMikolaj Golub static void note_procstat_umask(void *, struct sbuf *, size_t *); 1411f1fca82eSMikolaj Golub static void note_procstat_vmmap(void *, struct sbuf *, size_t *); 14128c64af4fSJohn Polstra 1413aa14e9b7SMark Johnston /* 1414aa14e9b7SMark Johnston * Write out a core segment to the compression stream. 1415aa14e9b7SMark Johnston */ 1416e7228204SAlfred Perlstein static int 1417aa14e9b7SMark Johnston compress_chunk(struct coredump_params *p, char *base, char *buf, u_int len) 1418aa14e9b7SMark Johnston { 1419aa14e9b7SMark Johnston u_int chunk_len; 1420e7228204SAlfred Perlstein int error; 1421aa14e9b7SMark Johnston 1422aa14e9b7SMark Johnston while (len > 0) { 1423aa14e9b7SMark Johnston chunk_len = MIN(len, CORE_BUF_SIZE); 1424c468ff88SAndriy Gapon 1425c468ff88SAndriy Gapon /* 1426c468ff88SAndriy Gapon * We can get EFAULT error here. 1427c468ff88SAndriy Gapon * In that case zero out the current chunk of the segment. 1428c468ff88SAndriy Gapon */ 1429c468ff88SAndriy Gapon error = copyin(base, buf, chunk_len); 1430c468ff88SAndriy Gapon if (error != 0) 1431c468ff88SAndriy Gapon bzero(buf, chunk_len); 143278f57a9cSMark Johnston error = compressor_write(p->comp, buf, chunk_len); 1433aa14e9b7SMark Johnston if (error != 0) 1434aa14e9b7SMark Johnston break; 1435aa14e9b7SMark Johnston base += chunk_len; 1436aa14e9b7SMark Johnston len -= chunk_len; 1437e7228204SAlfred Perlstein } 1438e7228204SAlfred Perlstein return (error); 1439e7228204SAlfred Perlstein } 1440e7228204SAlfred Perlstein 1441aa14e9b7SMark Johnston static int 144278f57a9cSMark Johnston core_compressed_write(void *base, size_t len, off_t offset, void *arg) 1443aa14e9b7SMark Johnston { 1444aa14e9b7SMark Johnston 1445aa14e9b7SMark Johnston return (core_write((struct coredump_params *)arg, base, len, offset, 1446aa14e9b7SMark Johnston UIO_SYSSPACE)); 1447aa14e9b7SMark Johnston } 1448aa14e9b7SMark Johnston 1449aa14e9b7SMark Johnston static int 1450c468ff88SAndriy Gapon core_write(struct coredump_params *p, const void *base, size_t len, 1451c468ff88SAndriy Gapon off_t offset, enum uio_seg seg) 1452aa14e9b7SMark Johnston { 1453aa14e9b7SMark Johnston 1454c468ff88SAndriy Gapon return (vn_rdwr_inchunks(UIO_WRITE, p->vp, __DECONST(void *, base), 1455c468ff88SAndriy Gapon len, offset, seg, IO_UNIT | IO_DIRECT | IO_RANGELOCKED, 1456aa14e9b7SMark Johnston p->active_cred, p->file_cred, NULL, p->td)); 1457aa14e9b7SMark Johnston } 1458aa14e9b7SMark Johnston 1459aa14e9b7SMark Johnston static int 1460aa14e9b7SMark Johnston core_output(void *base, size_t len, off_t offset, struct coredump_params *p, 1461aa14e9b7SMark Johnston void *tmpbuf) 1462aa14e9b7SMark Johnston { 1463c468ff88SAndriy Gapon int error; 1464aa14e9b7SMark Johnston 146578f57a9cSMark Johnston if (p->comp != NULL) 1466aa14e9b7SMark Johnston return (compress_chunk(p, base, tmpbuf, len)); 146778f57a9cSMark Johnston 1468c468ff88SAndriy Gapon /* 1469c468ff88SAndriy Gapon * EFAULT is a non-fatal error that we can get, for example, 1470c468ff88SAndriy Gapon * if the segment is backed by a file but extends beyond its 1471c468ff88SAndriy Gapon * end. 1472c468ff88SAndriy Gapon */ 1473c468ff88SAndriy Gapon error = core_write(p, base, len, offset, UIO_USERSPACE); 1474c468ff88SAndriy Gapon if (error == EFAULT) { 1475c468ff88SAndriy Gapon log(LOG_WARNING, "Failed to fully fault in a core file segment " 1476c468ff88SAndriy Gapon "at VA %p with size 0x%zx to be written at offset 0x%jx " 1477c468ff88SAndriy Gapon "for process %s\n", base, len, offset, curproc->p_comm); 1478c468ff88SAndriy Gapon 1479c468ff88SAndriy Gapon /* 1480c468ff88SAndriy Gapon * Write a "real" zero byte at the end of the target region 1481c468ff88SAndriy Gapon * in the case this is the last segment. 1482c468ff88SAndriy Gapon * The intermediate space will be implicitly zero-filled. 1483c468ff88SAndriy Gapon */ 1484c468ff88SAndriy Gapon error = core_write(p, zero_region, 1, offset + len - 1, 1485c468ff88SAndriy Gapon UIO_SYSSPACE); 1486c468ff88SAndriy Gapon } 1487c468ff88SAndriy Gapon return (error); 1488aa14e9b7SMark Johnston } 1489bd390213SMikolaj Golub 1490bd390213SMikolaj Golub /* 1491bd390213SMikolaj Golub * Drain into a core file. 1492bd390213SMikolaj Golub */ 1493bd390213SMikolaj Golub static int 1494bd390213SMikolaj Golub sbuf_drain_core_output(void *arg, const char *data, int len) 1495bd390213SMikolaj Golub { 1496aa14e9b7SMark Johnston struct coredump_params *p; 1497f1fca82eSMikolaj Golub int error, locked; 1498bd390213SMikolaj Golub 1499aa14e9b7SMark Johnston p = (struct coredump_params *)arg; 1500f1fca82eSMikolaj Golub 1501f1fca82eSMikolaj Golub /* 1502f1fca82eSMikolaj Golub * Some kern_proc out routines that print to this sbuf may 1503f1fca82eSMikolaj Golub * call us with the process lock held. Draining with the 1504f1fca82eSMikolaj Golub * non-sleepable lock held is unsafe. The lock is needed for 1505f1fca82eSMikolaj Golub * those routines when dumping a live process. In our case we 1506f1fca82eSMikolaj Golub * can safely release the lock before draining and acquire 1507f1fca82eSMikolaj Golub * again after. 1508f1fca82eSMikolaj Golub */ 1509f1fca82eSMikolaj Golub locked = PROC_LOCKED(p->td->td_proc); 1510f1fca82eSMikolaj Golub if (locked) 1511f1fca82eSMikolaj Golub PROC_UNLOCK(p->td->td_proc); 151278f57a9cSMark Johnston if (p->comp != NULL) 151378f57a9cSMark Johnston error = compressor_write(p->comp, __DECONST(char *, data), len); 1514bd390213SMikolaj Golub else 1515aa14e9b7SMark Johnston error = core_write(p, __DECONST(void *, data), len, p->offset, 1516aa14e9b7SMark Johnston UIO_SYSSPACE); 1517f1fca82eSMikolaj Golub if (locked) 1518f1fca82eSMikolaj Golub PROC_LOCK(p->td->td_proc); 1519bd390213SMikolaj Golub if (error != 0) 1520bd390213SMikolaj Golub return (-error); 1521bd390213SMikolaj Golub p->offset += len; 1522bd390213SMikolaj Golub return (len); 1523bd390213SMikolaj Golub } 1524bd390213SMikolaj Golub 1525f1fca82eSMikolaj Golub /* 1526f1fca82eSMikolaj Golub * Drain into a counter. 1527f1fca82eSMikolaj Golub */ 1528f1fca82eSMikolaj Golub static int 1529f1fca82eSMikolaj Golub sbuf_drain_count(void *arg, const char *data __unused, int len) 1530f1fca82eSMikolaj Golub { 1531f1fca82eSMikolaj Golub size_t *sizep; 1532f1fca82eSMikolaj Golub 1533f1fca82eSMikolaj Golub sizep = (size_t *)arg; 1534f1fca82eSMikolaj Golub *sizep += len; 1535f1fca82eSMikolaj Golub return (len); 1536f1fca82eSMikolaj Golub } 1537f1fca82eSMikolaj Golub 15388c64af4fSJohn Polstra int 1539e7228204SAlfred Perlstein __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags) 1540fca666a1SJulian Elischer { 1541247aba24SMarcel Moolenaar struct ucred *cred = td->td_ucred; 1542fca666a1SJulian Elischer int error = 0; 15430ff27d31SJohn Polstra struct sseg_closure seginfo; 1544bd390213SMikolaj Golub struct note_info_list notelst; 1545aa14e9b7SMark Johnston struct coredump_params params; 1546bd390213SMikolaj Golub struct note_info *ninfo; 1547aa14e9b7SMark Johnston void *hdr, *tmpbuf; 1548bd390213SMikolaj Golub size_t hdrsize, notesz, coresize; 15498c64af4fSJohn Polstra 1550e7228204SAlfred Perlstein hdr = NULL; 155102d131adSMark Johnston tmpbuf = NULL; 1552bd390213SMikolaj Golub TAILQ_INIT(¬elst); 1553e7228204SAlfred Perlstein 15540ff27d31SJohn Polstra /* Size the program segments. */ 15550ff27d31SJohn Polstra seginfo.count = 0; 15560ff27d31SJohn Polstra seginfo.size = 0; 15571005d8afSConrad Meyer each_dumpable_segment(td, cb_size_segment, &seginfo); 15580ff27d31SJohn Polstra 15590ff27d31SJohn Polstra /* 1560bd390213SMikolaj Golub * Collect info about the core file header area. 15610ff27d31SJohn Polstra */ 1562bd390213SMikolaj Golub hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count); 1563c17b0bd2SConrad Meyer if (seginfo.count + 1 >= PN_XNUM) 1564c17b0bd2SConrad Meyer hdrsize += sizeof(Elf_Shdr); 1565bd390213SMikolaj Golub __elfN(prepare_notes)(td, ¬elst, ¬esz); 1566bd390213SMikolaj Golub coresize = round_page(hdrsize + notesz) + seginfo.size; 15670ff27d31SJohn Polstra 156802d131adSMark Johnston /* Set up core dump parameters. */ 156902d131adSMark Johnston params.offset = 0; 157002d131adSMark Johnston params.active_cred = cred; 157102d131adSMark Johnston params.file_cred = NOCRED; 157202d131adSMark Johnston params.td = td; 157302d131adSMark Johnston params.vp = vp; 157478f57a9cSMark Johnston params.comp = NULL; 157502d131adSMark Johnston 1576afcc55f3SEdward Tomasz Napierala #ifdef RACCT 15774b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 15781ba5ad42SEdward Tomasz Napierala PROC_LOCK(td->td_proc); 1579bd390213SMikolaj Golub error = racct_add(td->td_proc, RACCT_CORE, coresize); 15801ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 15811ba5ad42SEdward Tomasz Napierala if (error != 0) { 15821ba5ad42SEdward Tomasz Napierala error = EFAULT; 15831ba5ad42SEdward Tomasz Napierala goto done; 15841ba5ad42SEdward Tomasz Napierala } 15854b5c9cf6SEdward Tomasz Napierala } 1586afcc55f3SEdward Tomasz Napierala #endif 1587bd390213SMikolaj Golub if (coresize >= limit) { 1588fba6b1afSAlfred Perlstein error = EFAULT; 1589fba6b1afSAlfred Perlstein goto done; 1590fba6b1afSAlfred Perlstein } 15910ff27d31SJohn Polstra 1592aa14e9b7SMark Johnston /* Create a compression stream if necessary. */ 159378f57a9cSMark Johnston if (compress_user_cores != 0) { 159478f57a9cSMark Johnston params.comp = compressor_init(core_compressed_write, 159578f57a9cSMark Johnston compress_user_cores, CORE_BUF_SIZE, 159678f57a9cSMark Johnston compress_user_cores_level, ¶ms); 159778f57a9cSMark Johnston if (params.comp == NULL) { 1598aa14e9b7SMark Johnston error = EFAULT; 1599aa14e9b7SMark Johnston goto done; 1600aa14e9b7SMark Johnston } 1601aa14e9b7SMark Johnston tmpbuf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO); 1602aa14e9b7SMark Johnston } 1603aa14e9b7SMark Johnston 16040ff27d31SJohn Polstra /* 16050ff27d31SJohn Polstra * Allocate memory for building the header, fill it up, 1606bd390213SMikolaj Golub * and write it out following the notes. 16070ff27d31SJohn Polstra */ 1608a163d034SWarner Losh hdr = malloc(hdrsize, M_TEMP, M_WAITOK); 1609aa14e9b7SMark Johnston error = __elfN(corehdr)(¶ms, seginfo.count, hdr, hdrsize, ¬elst, 1610aa14e9b7SMark Johnston notesz); 16110ff27d31SJohn Polstra 16120ff27d31SJohn Polstra /* Write the contents of all of the writable segments. */ 16130ff27d31SJohn Polstra if (error == 0) { 16140ff27d31SJohn Polstra Elf_Phdr *php; 16152b471bc6STim J. Robbins off_t offset; 16160ff27d31SJohn Polstra int i; 16170ff27d31SJohn Polstra 16180ff27d31SJohn Polstra php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1; 1619bd390213SMikolaj Golub offset = round_page(hdrsize + notesz); 16200ff27d31SJohn Polstra for (i = 0; i < seginfo.count; i++) { 1621aa14e9b7SMark Johnston error = core_output((caddr_t)(uintptr_t)php->p_vaddr, 1622aa14e9b7SMark Johnston php->p_filesz, offset, ¶ms, tmpbuf); 16230ff27d31SJohn Polstra if (error != 0) 16242b471bc6STim J. Robbins break; 16250ff27d31SJohn Polstra offset += php->p_filesz; 16260ff27d31SJohn Polstra php++; 16270ff27d31SJohn Polstra } 162878f57a9cSMark Johnston if (error == 0 && params.comp != NULL) 162978f57a9cSMark Johnston error = compressor_flush(params.comp); 16300ff27d31SJohn Polstra } 1631e7228204SAlfred Perlstein if (error) { 1632e7228204SAlfred Perlstein log(LOG_WARNING, 1633e7228204SAlfred Perlstein "Failed to write core file for process %s (error %d)\n", 1634e7228204SAlfred Perlstein curproc->p_comm, error); 1635e7228204SAlfred Perlstein } 1636e7228204SAlfred Perlstein 1637e7228204SAlfred Perlstein done: 1638aa14e9b7SMark Johnston free(tmpbuf, M_TEMP); 163978f57a9cSMark Johnston if (params.comp != NULL) 164078f57a9cSMark Johnston compressor_fini(params.comp); 1641bd390213SMikolaj Golub while ((ninfo = TAILQ_FIRST(¬elst)) != NULL) { 1642bd390213SMikolaj Golub TAILQ_REMOVE(¬elst, ninfo, link); 1643bd390213SMikolaj Golub free(ninfo, M_TEMP); 1644bd390213SMikolaj Golub } 1645bd390213SMikolaj Golub if (hdr != NULL) 16460ff27d31SJohn Polstra free(hdr, M_TEMP); 16470ff27d31SJohn Polstra 1648a7cddfedSJake Burkholder return (error); 16498c64af4fSJohn Polstra } 16508c64af4fSJohn Polstra 16510ff27d31SJohn Polstra /* 16521005d8afSConrad Meyer * A callback for each_dumpable_segment() to write out the segment's 16530ff27d31SJohn Polstra * program header entry. 16540ff27d31SJohn Polstra */ 16550ff27d31SJohn Polstra static void 16565cc6d253SEd Maste cb_put_phdr(vm_map_entry_t entry, void *closure) 16570ff27d31SJohn Polstra { 16580ff27d31SJohn Polstra struct phdr_closure *phc = (struct phdr_closure *)closure; 16590ff27d31SJohn Polstra Elf_Phdr *phdr = phc->phdr; 16600ff27d31SJohn Polstra 16610ff27d31SJohn Polstra phc->offset = round_page(phc->offset); 16620ff27d31SJohn Polstra 16630ff27d31SJohn Polstra phdr->p_type = PT_LOAD; 16640ff27d31SJohn Polstra phdr->p_offset = phc->offset; 16650ff27d31SJohn Polstra phdr->p_vaddr = entry->start; 16660ff27d31SJohn Polstra phdr->p_paddr = 0; 16670ff27d31SJohn Polstra phdr->p_filesz = phdr->p_memsz = entry->end - entry->start; 16680ff27d31SJohn Polstra phdr->p_align = PAGE_SIZE; 1669ed167eaaSKonstantin Belousov phdr->p_flags = __elfN(untrans_prot)(entry->protection); 16700ff27d31SJohn Polstra 16710ff27d31SJohn Polstra phc->offset += phdr->p_filesz; 16720ff27d31SJohn Polstra phc->phdr++; 16730ff27d31SJohn Polstra } 16740ff27d31SJohn Polstra 16750ff27d31SJohn Polstra /* 16761005d8afSConrad Meyer * A callback for each_dumpable_segment() to gather information about 16770ff27d31SJohn Polstra * the number of segments and their total size. 16780ff27d31SJohn Polstra */ 16790ff27d31SJohn Polstra static void 1680f3325003SConrad Meyer cb_size_segment(vm_map_entry_t entry, void *closure) 16810ff27d31SJohn Polstra { 16820ff27d31SJohn Polstra struct sseg_closure *ssc = (struct sseg_closure *)closure; 16830ff27d31SJohn Polstra 16840ff27d31SJohn Polstra ssc->count++; 16850ff27d31SJohn Polstra ssc->size += entry->end - entry->start; 16860ff27d31SJohn Polstra } 16870ff27d31SJohn Polstra 16880ff27d31SJohn Polstra /* 16890ff27d31SJohn Polstra * For each writable segment in the process's memory map, call the given 16900ff27d31SJohn Polstra * function with a pointer to the map entry and some arbitrary 16910ff27d31SJohn Polstra * caller-supplied data. 16920ff27d31SJohn Polstra */ 16930ff27d31SJohn Polstra static void 16941005d8afSConrad Meyer each_dumpable_segment(struct thread *td, segment_callback func, void *closure) 16950ff27d31SJohn Polstra { 1696247aba24SMarcel Moolenaar struct proc *p = td->td_proc; 16970ff27d31SJohn Polstra vm_map_t map = &p->p_vmspace->vm_map; 16980ff27d31SJohn Polstra vm_map_entry_t entry; 1699976a87a2SAlan Cox vm_object_t backing_object, object; 1700976a87a2SAlan Cox boolean_t ignore_entry; 17010ff27d31SJohn Polstra 1702976a87a2SAlan Cox vm_map_lock_read(map); 17030ff27d31SJohn Polstra for (entry = map->header.next; entry != &map->header; 17040ff27d31SJohn Polstra entry = entry->next) { 1705fa7dd9c5SMatthew Dillon /* 1706fa7dd9c5SMatthew Dillon * Don't dump inaccessible mappings, deal with legacy 1707fa7dd9c5SMatthew Dillon * coredump mode. 1708fa7dd9c5SMatthew Dillon * 1709fa7dd9c5SMatthew Dillon * Note that read-only segments related to the elf binary 1710fa7dd9c5SMatthew Dillon * are marked MAP_ENTRY_NOCOREDUMP now so we no longer 1711fa7dd9c5SMatthew Dillon * need to arbitrarily ignore such segments. 1712fa7dd9c5SMatthew Dillon */ 1713fa7dd9c5SMatthew Dillon if (elf_legacy_coredump) { 1714fa7dd9c5SMatthew Dillon if ((entry->protection & VM_PROT_RW) != VM_PROT_RW) 17150ff27d31SJohn Polstra continue; 1716fa7dd9c5SMatthew Dillon } else { 1717fa7dd9c5SMatthew Dillon if ((entry->protection & VM_PROT_ALL) == 0) 1718fa7dd9c5SMatthew Dillon continue; 1719fa7dd9c5SMatthew Dillon } 17200ff27d31SJohn Polstra 17219730a5daSPaul Saab /* 1722fa7dd9c5SMatthew Dillon * Dont include memory segment in the coredump if 1723fa7dd9c5SMatthew Dillon * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in 1724fa7dd9c5SMatthew Dillon * madvise(2). Do not dump submaps (i.e. parts of the 1725fa7dd9c5SMatthew Dillon * kernel map). 17269730a5daSPaul Saab */ 1727fa7dd9c5SMatthew Dillon if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP)) 17289730a5daSPaul Saab continue; 17299730a5daSPaul Saab 1730976a87a2SAlan Cox if ((object = entry->object.vm_object) == NULL) 17310ff27d31SJohn Polstra continue; 17320ff27d31SJohn Polstra 17330ff27d31SJohn Polstra /* Ignore memory-mapped devices and such things. */ 1734bc403f03SAttilio Rao VM_OBJECT_RLOCK(object); 1735976a87a2SAlan Cox while ((backing_object = object->backing_object) != NULL) { 1736bc403f03SAttilio Rao VM_OBJECT_RLOCK(backing_object); 1737bc403f03SAttilio Rao VM_OBJECT_RUNLOCK(object); 1738976a87a2SAlan Cox object = backing_object; 1739976a87a2SAlan Cox } 1740976a87a2SAlan Cox ignore_entry = object->type != OBJT_DEFAULT && 1741bc411bc2SJohn Baldwin object->type != OBJT_SWAP && object->type != OBJT_VNODE && 1742bc411bc2SJohn Baldwin object->type != OBJT_PHYS; 1743bc403f03SAttilio Rao VM_OBJECT_RUNLOCK(object); 1744976a87a2SAlan Cox if (ignore_entry) 17450ff27d31SJohn Polstra continue; 17460ff27d31SJohn Polstra 17470ff27d31SJohn Polstra (*func)(entry, closure); 17480ff27d31SJohn Polstra } 1749976a87a2SAlan Cox vm_map_unlock_read(map); 17500ff27d31SJohn Polstra } 17510ff27d31SJohn Polstra 17520ff27d31SJohn Polstra /* 17530ff27d31SJohn Polstra * Write the core file header to the file, including padding up to 17540ff27d31SJohn Polstra * the page boundary. 17550ff27d31SJohn Polstra */ 17568c64af4fSJohn Polstra static int 1757aa14e9b7SMark Johnston __elfN(corehdr)(struct coredump_params *p, int numsegs, void *hdr, 1758aa14e9b7SMark Johnston size_t hdrsize, struct note_info_list *notelst, size_t notesz) 17598c64af4fSJohn Polstra { 1760bd390213SMikolaj Golub struct note_info *ninfo; 1761bd390213SMikolaj Golub struct sbuf *sb; 1762bd390213SMikolaj Golub int error; 17638c64af4fSJohn Polstra 17648c64af4fSJohn Polstra /* Fill in the header. */ 17650ff27d31SJohn Polstra bzero(hdr, hdrsize); 1766aa14e9b7SMark Johnston __elfN(puthdr)(p->td, hdr, hdrsize, numsegs, notesz); 17678c64af4fSJohn Polstra 1768bd390213SMikolaj Golub sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN); 1769aa14e9b7SMark Johnston sbuf_set_drain(sb, sbuf_drain_core_output, p); 1770bd390213SMikolaj Golub sbuf_start_section(sb, NULL); 1771bd390213SMikolaj Golub sbuf_bcat(sb, hdr, hdrsize); 1772bd390213SMikolaj Golub TAILQ_FOREACH(ninfo, notelst, link) 1773bd390213SMikolaj Golub __elfN(putnote)(ninfo, sb); 1774bd390213SMikolaj Golub /* Align up to a page boundary for the program segments. */ 1775bd390213SMikolaj Golub sbuf_end_section(sb, -1, PAGE_SIZE, 0); 1776bd390213SMikolaj Golub error = sbuf_finish(sb); 1777bd390213SMikolaj Golub sbuf_delete(sb); 1778bd390213SMikolaj Golub 1779bd390213SMikolaj Golub return (error); 1780e7228204SAlfred Perlstein } 1781bd390213SMikolaj Golub 1782bd390213SMikolaj Golub static void 1783bd390213SMikolaj Golub __elfN(prepare_notes)(struct thread *td, struct note_info_list *list, 1784bd390213SMikolaj Golub size_t *sizep) 1785bd390213SMikolaj Golub { 1786bd390213SMikolaj Golub struct proc *p; 1787bd390213SMikolaj Golub struct thread *thr; 1788bd390213SMikolaj Golub size_t size; 1789bd390213SMikolaj Golub 1790bd390213SMikolaj Golub p = td->td_proc; 1791bd390213SMikolaj Golub size = 0; 1792bd390213SMikolaj Golub 1793bd390213SMikolaj Golub size += register_note(list, NT_PRPSINFO, __elfN(note_prpsinfo), p); 1794bd390213SMikolaj Golub 1795bd390213SMikolaj Golub /* 1796bd390213SMikolaj Golub * To have the debugger select the right thread (LWP) as the initial 1797bd390213SMikolaj Golub * thread, we dump the state of the thread passed to us in td first. 1798bd390213SMikolaj Golub * This is the thread that causes the core dump and thus likely to 1799bd390213SMikolaj Golub * be the right thread one wants to have selected in the debugger. 1800bd390213SMikolaj Golub */ 1801bd390213SMikolaj Golub thr = td; 1802bd390213SMikolaj Golub while (thr != NULL) { 1803bd390213SMikolaj Golub size += register_note(list, NT_PRSTATUS, 1804bd390213SMikolaj Golub __elfN(note_prstatus), thr); 1805bd390213SMikolaj Golub size += register_note(list, NT_FPREGSET, 1806bd390213SMikolaj Golub __elfN(note_fpregset), thr); 1807bd390213SMikolaj Golub size += register_note(list, NT_THRMISC, 1808bd390213SMikolaj Golub __elfN(note_thrmisc), thr); 180986be94fcSTycho Nightingale size += register_note(list, NT_PTLWPINFO, 181086be94fcSTycho Nightingale __elfN(note_ptlwpinfo), thr); 1811bd390213SMikolaj Golub size += register_note(list, -1, 1812bd390213SMikolaj Golub __elfN(note_threadmd), thr); 1813bd390213SMikolaj Golub 1814bd390213SMikolaj Golub thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) : 1815bd390213SMikolaj Golub TAILQ_NEXT(thr, td_plist); 1816bd390213SMikolaj Golub if (thr == td) 1817bd390213SMikolaj Golub thr = TAILQ_NEXT(thr, td_plist); 1818dada0278SJohn Polstra } 1819dada0278SJohn Polstra 1820f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_PROC, 1821f1fca82eSMikolaj Golub __elfN(note_procstat_proc), p); 1822f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_FILES, 1823f1fca82eSMikolaj Golub note_procstat_files, p); 1824f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_VMMAP, 1825f1fca82eSMikolaj Golub note_procstat_vmmap, p); 1826f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_GROUPS, 1827f1fca82eSMikolaj Golub note_procstat_groups, p); 1828f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_UMASK, 1829f1fca82eSMikolaj Golub note_procstat_umask, p); 1830f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_RLIMIT, 1831f1fca82eSMikolaj Golub note_procstat_rlimit, p); 1832f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_OSREL, 1833f1fca82eSMikolaj Golub note_procstat_osrel, p); 1834f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_PSSTRINGS, 1835f1fca82eSMikolaj Golub __elfN(note_procstat_psstrings), p); 1836f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_AUXV, 1837f1fca82eSMikolaj Golub __elfN(note_procstat_auxv), p); 1838f1fca82eSMikolaj Golub 1839bd390213SMikolaj Golub *sizep = size; 1840bd390213SMikolaj Golub } 1841bd390213SMikolaj Golub 1842bd390213SMikolaj Golub static void 1843bd390213SMikolaj Golub __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs, 1844bd390213SMikolaj Golub size_t notesz) 1845bd390213SMikolaj Golub { 1846bd390213SMikolaj Golub Elf_Ehdr *ehdr; 1847bd390213SMikolaj Golub Elf_Phdr *phdr; 1848c17b0bd2SConrad Meyer Elf_Shdr *shdr; 1849bd390213SMikolaj Golub struct phdr_closure phc; 1850bd390213SMikolaj Golub 1851bd390213SMikolaj Golub ehdr = (Elf_Ehdr *)hdr; 1852bd390213SMikolaj Golub 1853bd390213SMikolaj Golub ehdr->e_ident[EI_MAG0] = ELFMAG0; 1854bd390213SMikolaj Golub ehdr->e_ident[EI_MAG1] = ELFMAG1; 1855bd390213SMikolaj Golub ehdr->e_ident[EI_MAG2] = ELFMAG2; 1856bd390213SMikolaj Golub ehdr->e_ident[EI_MAG3] = ELFMAG3; 1857bd390213SMikolaj Golub ehdr->e_ident[EI_CLASS] = ELF_CLASS; 1858bd390213SMikolaj Golub ehdr->e_ident[EI_DATA] = ELF_DATA; 1859bd390213SMikolaj Golub ehdr->e_ident[EI_VERSION] = EV_CURRENT; 1860bd390213SMikolaj Golub ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD; 1861bd390213SMikolaj Golub ehdr->e_ident[EI_ABIVERSION] = 0; 1862bd390213SMikolaj Golub ehdr->e_ident[EI_PAD] = 0; 1863bd390213SMikolaj Golub ehdr->e_type = ET_CORE; 1864885f13dcSJohn Baldwin ehdr->e_machine = td->td_proc->p_elf_machine; 1865bd390213SMikolaj Golub ehdr->e_version = EV_CURRENT; 1866bd390213SMikolaj Golub ehdr->e_entry = 0; 1867bd390213SMikolaj Golub ehdr->e_phoff = sizeof(Elf_Ehdr); 1868885f13dcSJohn Baldwin ehdr->e_flags = td->td_proc->p_elf_flags; 1869bd390213SMikolaj Golub ehdr->e_ehsize = sizeof(Elf_Ehdr); 1870bd390213SMikolaj Golub ehdr->e_phentsize = sizeof(Elf_Phdr); 1871bd390213SMikolaj Golub ehdr->e_shentsize = sizeof(Elf_Shdr); 1872bd390213SMikolaj Golub ehdr->e_shstrndx = SHN_UNDEF; 1873c17b0bd2SConrad Meyer if (numsegs + 1 < PN_XNUM) { 1874c17b0bd2SConrad Meyer ehdr->e_phnum = numsegs + 1; 1875c17b0bd2SConrad Meyer ehdr->e_shnum = 0; 1876c17b0bd2SConrad Meyer } else { 1877c17b0bd2SConrad Meyer ehdr->e_phnum = PN_XNUM; 1878c17b0bd2SConrad Meyer ehdr->e_shnum = 1; 1879c17b0bd2SConrad Meyer 1880c17b0bd2SConrad Meyer ehdr->e_shoff = ehdr->e_phoff + 1881c17b0bd2SConrad Meyer (numsegs + 1) * ehdr->e_phentsize; 1882c17b0bd2SConrad Meyer KASSERT(ehdr->e_shoff == hdrsize - sizeof(Elf_Shdr), 1883c17b0bd2SConrad Meyer ("e_shoff: %zu, hdrsize - shdr: %zu", 188407f825e8SConrad Meyer (size_t)ehdr->e_shoff, hdrsize - sizeof(Elf_Shdr))); 1885c17b0bd2SConrad Meyer 1886c17b0bd2SConrad Meyer shdr = (Elf_Shdr *)((char *)hdr + ehdr->e_shoff); 1887c17b0bd2SConrad Meyer memset(shdr, 0, sizeof(*shdr)); 1888c17b0bd2SConrad Meyer /* 1889c17b0bd2SConrad Meyer * A special first section is used to hold large segment and 1890c17b0bd2SConrad Meyer * section counts. This was proposed by Sun Microsystems in 1891c17b0bd2SConrad Meyer * Solaris and has been adopted by Linux; the standard ELF 1892c17b0bd2SConrad Meyer * tools are already familiar with the technique. 1893c17b0bd2SConrad Meyer * 1894c17b0bd2SConrad Meyer * See table 7-7 of the Solaris "Linker and Libraries Guide" 1895c17b0bd2SConrad Meyer * (or 12-7 depending on the version of the document) for more 1896c17b0bd2SConrad Meyer * details. 1897c17b0bd2SConrad Meyer */ 1898c17b0bd2SConrad Meyer shdr->sh_type = SHT_NULL; 1899c17b0bd2SConrad Meyer shdr->sh_size = ehdr->e_shnum; 1900c17b0bd2SConrad Meyer shdr->sh_link = ehdr->e_shstrndx; 1901c17b0bd2SConrad Meyer shdr->sh_info = numsegs + 1; 1902c17b0bd2SConrad Meyer } 1903bd390213SMikolaj Golub 1904bd390213SMikolaj Golub /* 1905bd390213SMikolaj Golub * Fill in the program header entries. 1906bd390213SMikolaj Golub */ 1907c17b0bd2SConrad Meyer phdr = (Elf_Phdr *)((char *)hdr + ehdr->e_phoff); 1908bd390213SMikolaj Golub 1909bd390213SMikolaj Golub /* The note segement. */ 1910bd390213SMikolaj Golub phdr->p_type = PT_NOTE; 1911bd390213SMikolaj Golub phdr->p_offset = hdrsize; 1912bd390213SMikolaj Golub phdr->p_vaddr = 0; 1913bd390213SMikolaj Golub phdr->p_paddr = 0; 1914bd390213SMikolaj Golub phdr->p_filesz = notesz; 1915bd390213SMikolaj Golub phdr->p_memsz = 0; 1916bd390213SMikolaj Golub phdr->p_flags = PF_R; 19171b8388cdSMikolaj Golub phdr->p_align = ELF_NOTE_ROUNDSIZE; 1918bd390213SMikolaj Golub phdr++; 1919bd390213SMikolaj Golub 1920bd390213SMikolaj Golub /* All the writable segments from the program. */ 1921bd390213SMikolaj Golub phc.phdr = phdr; 1922bd390213SMikolaj Golub phc.offset = round_page(hdrsize + notesz); 19231005d8afSConrad Meyer each_dumpable_segment(td, cb_put_phdr, &phc); 1924bd390213SMikolaj Golub } 1925bd390213SMikolaj Golub 1926bd390213SMikolaj Golub static size_t 1927bd390213SMikolaj Golub register_note(struct note_info_list *list, int type, outfunc_t out, void *arg) 1928bd390213SMikolaj Golub { 1929bd390213SMikolaj Golub struct note_info *ninfo; 1930bd390213SMikolaj Golub size_t size, notesize; 1931bd390213SMikolaj Golub 1932bd390213SMikolaj Golub size = 0; 1933bd390213SMikolaj Golub out(arg, NULL, &size); 1934bd390213SMikolaj Golub ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK); 1935bd390213SMikolaj Golub ninfo->type = type; 1936bd390213SMikolaj Golub ninfo->outfunc = out; 1937bd390213SMikolaj Golub ninfo->outarg = arg; 1938bd390213SMikolaj Golub ninfo->outsize = size; 1939bd390213SMikolaj Golub TAILQ_INSERT_TAIL(list, ninfo, link); 1940bd390213SMikolaj Golub 1941bd390213SMikolaj Golub if (type == -1) 1942bd390213SMikolaj Golub return (size); 1943bd390213SMikolaj Golub 1944bd390213SMikolaj Golub notesize = sizeof(Elf_Note) + /* note header */ 1945180e57e5SJohn Baldwin roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) + 1946180e57e5SJohn Baldwin /* note name */ 1947180e57e5SJohn Baldwin roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */ 1948180e57e5SJohn Baldwin 1949180e57e5SJohn Baldwin return (notesize); 1950180e57e5SJohn Baldwin } 1951180e57e5SJohn Baldwin 1952180e57e5SJohn Baldwin static size_t 1953180e57e5SJohn Baldwin append_note_data(const void *src, void *dst, size_t len) 1954180e57e5SJohn Baldwin { 1955180e57e5SJohn Baldwin size_t padded_len; 1956180e57e5SJohn Baldwin 1957180e57e5SJohn Baldwin padded_len = roundup2(len, ELF_NOTE_ROUNDSIZE); 1958180e57e5SJohn Baldwin if (dst != NULL) { 1959180e57e5SJohn Baldwin bcopy(src, dst, len); 1960180e57e5SJohn Baldwin bzero((char *)dst + len, padded_len - len); 1961180e57e5SJohn Baldwin } 1962180e57e5SJohn Baldwin return (padded_len); 1963180e57e5SJohn Baldwin } 1964180e57e5SJohn Baldwin 1965180e57e5SJohn Baldwin size_t 1966180e57e5SJohn Baldwin __elfN(populate_note)(int type, void *src, void *dst, size_t size, void **descp) 1967180e57e5SJohn Baldwin { 1968180e57e5SJohn Baldwin Elf_Note *note; 1969180e57e5SJohn Baldwin char *buf; 1970180e57e5SJohn Baldwin size_t notesize; 1971180e57e5SJohn Baldwin 1972180e57e5SJohn Baldwin buf = dst; 1973180e57e5SJohn Baldwin if (buf != NULL) { 1974180e57e5SJohn Baldwin note = (Elf_Note *)buf; 1975180e57e5SJohn Baldwin note->n_namesz = sizeof(FREEBSD_ABI_VENDOR); 1976180e57e5SJohn Baldwin note->n_descsz = size; 1977180e57e5SJohn Baldwin note->n_type = type; 1978180e57e5SJohn Baldwin buf += sizeof(*note); 1979180e57e5SJohn Baldwin buf += append_note_data(FREEBSD_ABI_VENDOR, buf, 1980180e57e5SJohn Baldwin sizeof(FREEBSD_ABI_VENDOR)); 1981180e57e5SJohn Baldwin append_note_data(src, buf, size); 1982180e57e5SJohn Baldwin if (descp != NULL) 1983180e57e5SJohn Baldwin *descp = buf; 1984180e57e5SJohn Baldwin } 1985180e57e5SJohn Baldwin 1986180e57e5SJohn Baldwin notesize = sizeof(Elf_Note) + /* note header */ 1987180e57e5SJohn Baldwin roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) + 1988180e57e5SJohn Baldwin /* note name */ 19891b8388cdSMikolaj Golub roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */ 1990bd390213SMikolaj Golub 1991bd390213SMikolaj Golub return (notesize); 1992bd390213SMikolaj Golub } 1993bd390213SMikolaj Golub 1994bd390213SMikolaj Golub static void 1995bd390213SMikolaj Golub __elfN(putnote)(struct note_info *ninfo, struct sbuf *sb) 1996bd390213SMikolaj Golub { 1997bd390213SMikolaj Golub Elf_Note note; 199814bdbaf2SConrad Meyer ssize_t old_len, sect_len; 199914bdbaf2SConrad Meyer size_t new_len, descsz, i; 2000bd390213SMikolaj Golub 2001bd390213SMikolaj Golub if (ninfo->type == -1) { 2002bd390213SMikolaj Golub ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize); 2003bd390213SMikolaj Golub return; 2004bd390213SMikolaj Golub } 2005bd390213SMikolaj Golub 2006180e57e5SJohn Baldwin note.n_namesz = sizeof(FREEBSD_ABI_VENDOR); 2007bd390213SMikolaj Golub note.n_descsz = ninfo->outsize; 2008bd390213SMikolaj Golub note.n_type = ninfo->type; 2009bd390213SMikolaj Golub 2010bd390213SMikolaj Golub sbuf_bcat(sb, ¬e, sizeof(note)); 2011bd390213SMikolaj Golub sbuf_start_section(sb, &old_len); 2012180e57e5SJohn Baldwin sbuf_bcat(sb, FREEBSD_ABI_VENDOR, sizeof(FREEBSD_ABI_VENDOR)); 20131b8388cdSMikolaj Golub sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0); 2014bd390213SMikolaj Golub if (note.n_descsz == 0) 2015bd390213SMikolaj Golub return; 2016bd390213SMikolaj Golub sbuf_start_section(sb, &old_len); 2017bd390213SMikolaj Golub ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize); 201814bdbaf2SConrad Meyer sect_len = sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0); 201914bdbaf2SConrad Meyer if (sect_len < 0) 202014bdbaf2SConrad Meyer return; 202114bdbaf2SConrad Meyer 202214bdbaf2SConrad Meyer new_len = (size_t)sect_len; 202314bdbaf2SConrad Meyer descsz = roundup(note.n_descsz, ELF_NOTE_ROUNDSIZE); 202414bdbaf2SConrad Meyer if (new_len < descsz) { 202514bdbaf2SConrad Meyer /* 202614bdbaf2SConrad Meyer * It is expected that individual note emitters will correctly 202714bdbaf2SConrad Meyer * predict their expected output size and fill up to that size 202814bdbaf2SConrad Meyer * themselves, padding in a format-specific way if needed. 202914bdbaf2SConrad Meyer * However, in case they don't, just do it here with zeros. 203014bdbaf2SConrad Meyer */ 203114bdbaf2SConrad Meyer for (i = 0; i < descsz - new_len; i++) 203214bdbaf2SConrad Meyer sbuf_putc(sb, 0); 203314bdbaf2SConrad Meyer } else if (new_len > descsz) { 203414bdbaf2SConrad Meyer /* 203514bdbaf2SConrad Meyer * We can't always truncate sb -- we may have drained some 203614bdbaf2SConrad Meyer * of it already. 203714bdbaf2SConrad Meyer */ 203814bdbaf2SConrad Meyer KASSERT(new_len == descsz, ("%s: Note type %u changed as we " 203914bdbaf2SConrad Meyer "read it (%zu > %zu). Since it is longer than " 204014bdbaf2SConrad Meyer "expected, this coredump's notes are corrupt. THIS " 204114bdbaf2SConrad Meyer "IS A BUG in the note_procstat routine for type %u.\n", 204214bdbaf2SConrad Meyer __func__, (unsigned)note.n_type, new_len, descsz, 204314bdbaf2SConrad Meyer (unsigned)note.n_type)); 204414bdbaf2SConrad Meyer } 2045bd390213SMikolaj Golub } 2046bd390213SMikolaj Golub 2047bd390213SMikolaj Golub /* 2048bd390213SMikolaj Golub * Miscellaneous note out functions. 2049bd390213SMikolaj Golub */ 2050bd390213SMikolaj Golub 2051841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 2052841c0c7eSNathan Whitehorn #include <compat/freebsd32/freebsd32.h> 205351645e83SJohn Baldwin #include <compat/freebsd32/freebsd32_signal.h> 2054841c0c7eSNathan Whitehorn 205562919d78SPeter Wemm typedef struct prstatus32 elf_prstatus_t; 205662919d78SPeter Wemm typedef struct prpsinfo32 elf_prpsinfo_t; 205762919d78SPeter Wemm typedef struct fpreg32 elf_prfpregset_t; 205862919d78SPeter Wemm typedef struct fpreg32 elf_fpregset_t; 205962919d78SPeter Wemm typedef struct reg32 elf_gregset_t; 20607f08176eSAttilio Rao typedef struct thrmisc32 elf_thrmisc_t; 2061f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK KERN_PROC_MASK32 2062f1fca82eSMikolaj Golub typedef struct kinfo_proc32 elf_kinfo_proc_t; 2063f1fca82eSMikolaj Golub typedef uint32_t elf_ps_strings_t; 206462919d78SPeter Wemm #else 206562919d78SPeter Wemm typedef prstatus_t elf_prstatus_t; 206662919d78SPeter Wemm typedef prpsinfo_t elf_prpsinfo_t; 206762919d78SPeter Wemm typedef prfpregset_t elf_prfpregset_t; 206862919d78SPeter Wemm typedef prfpregset_t elf_fpregset_t; 206962919d78SPeter Wemm typedef gregset_t elf_gregset_t; 20707f08176eSAttilio Rao typedef thrmisc_t elf_thrmisc_t; 2071f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK 0 2072f1fca82eSMikolaj Golub typedef struct kinfo_proc elf_kinfo_proc_t; 2073f1fca82eSMikolaj Golub typedef vm_offset_t elf_ps_strings_t; 207462919d78SPeter Wemm #endif 207562919d78SPeter Wemm 20768c64af4fSJohn Polstra static void 2077bd390213SMikolaj Golub __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep) 20788c64af4fSJohn Polstra { 2079c77547d2SJohn Baldwin struct sbuf sbarg; 2080c77547d2SJohn Baldwin size_t len; 2081c77547d2SJohn Baldwin char *cp, *end; 2082247aba24SMarcel Moolenaar struct proc *p; 2083bd390213SMikolaj Golub elf_prpsinfo_t *psinfo; 2084c77547d2SJohn Baldwin int error; 20858c64af4fSJohn Polstra 2086bd390213SMikolaj Golub p = (struct proc *)arg; 2087bd390213SMikolaj Golub if (sb != NULL) { 2088bd390213SMikolaj Golub KASSERT(*sizep == sizeof(*psinfo), ("invalid size")); 2089bd390213SMikolaj Golub psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK); 20908c9b7b2cSMarcel Moolenaar psinfo->pr_version = PRPSINFO_VERSION; 209162919d78SPeter Wemm psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t); 2092ccd3953eSJohn Baldwin strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname)); 2093c77547d2SJohn Baldwin PROC_LOCK(p); 2094c77547d2SJohn Baldwin if (p->p_args != NULL) { 2095c77547d2SJohn Baldwin len = sizeof(psinfo->pr_psargs) - 1; 2096c77547d2SJohn Baldwin if (len > p->p_args->ar_length) 2097c77547d2SJohn Baldwin len = p->p_args->ar_length; 2098c77547d2SJohn Baldwin memcpy(psinfo->pr_psargs, p->p_args->ar_args, len); 2099c77547d2SJohn Baldwin PROC_UNLOCK(p); 2100c77547d2SJohn Baldwin error = 0; 2101c77547d2SJohn Baldwin } else { 2102c77547d2SJohn Baldwin _PHOLD(p); 2103c77547d2SJohn Baldwin PROC_UNLOCK(p); 2104c77547d2SJohn Baldwin sbuf_new(&sbarg, psinfo->pr_psargs, 2105c77547d2SJohn Baldwin sizeof(psinfo->pr_psargs), SBUF_FIXEDLEN); 2106c77547d2SJohn Baldwin error = proc_getargv(curthread, p, &sbarg); 2107c77547d2SJohn Baldwin PRELE(p); 2108c77547d2SJohn Baldwin if (sbuf_finish(&sbarg) == 0) 2109c77547d2SJohn Baldwin len = sbuf_len(&sbarg) - 1; 2110c77547d2SJohn Baldwin else 2111c77547d2SJohn Baldwin len = sizeof(psinfo->pr_psargs) - 1; 2112c77547d2SJohn Baldwin sbuf_delete(&sbarg); 2113c77547d2SJohn Baldwin } 2114c77547d2SJohn Baldwin if (error || len == 0) 2115ccd3953eSJohn Baldwin strlcpy(psinfo->pr_psargs, p->p_comm, 21168c9b7b2cSMarcel Moolenaar sizeof(psinfo->pr_psargs)); 2117c77547d2SJohn Baldwin else { 2118c77547d2SJohn Baldwin KASSERT(len < sizeof(psinfo->pr_psargs), 2119c77547d2SJohn Baldwin ("len is too long: %zu vs %zu", len, 2120c77547d2SJohn Baldwin sizeof(psinfo->pr_psargs))); 2121c77547d2SJohn Baldwin cp = psinfo->pr_psargs; 2122c77547d2SJohn Baldwin end = cp + len - 1; 2123c77547d2SJohn Baldwin for (;;) { 2124c77547d2SJohn Baldwin cp = memchr(cp, '\0', end - cp); 2125c77547d2SJohn Baldwin if (cp == NULL) 2126c77547d2SJohn Baldwin break; 2127c77547d2SJohn Baldwin *cp = ' '; 2128c77547d2SJohn Baldwin } 2129c77547d2SJohn Baldwin } 2130ccb83afdSJohn Baldwin psinfo->pr_pid = p->p_pid; 2131bd390213SMikolaj Golub sbuf_bcat(sb, psinfo, sizeof(*psinfo)); 2132bd390213SMikolaj Golub free(psinfo, M_TEMP); 2133bd390213SMikolaj Golub } 2134bd390213SMikolaj Golub *sizep = sizeof(*psinfo); 2135bd390213SMikolaj Golub } 2136bd390213SMikolaj Golub 2137bd390213SMikolaj Golub static void 2138bd390213SMikolaj Golub __elfN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep) 2139bd390213SMikolaj Golub { 2140bd390213SMikolaj Golub struct thread *td; 2141bd390213SMikolaj Golub elf_prstatus_t *status; 2142bd390213SMikolaj Golub 2143bd390213SMikolaj Golub td = (struct thread *)arg; 2144bd390213SMikolaj Golub if (sb != NULL) { 2145bd390213SMikolaj Golub KASSERT(*sizep == sizeof(*status), ("invalid size")); 2146bd390213SMikolaj Golub status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK); 21478c9b7b2cSMarcel Moolenaar status->pr_version = PRSTATUS_VERSION; 214862919d78SPeter Wemm status->pr_statussz = sizeof(elf_prstatus_t); 214962919d78SPeter Wemm status->pr_gregsetsz = sizeof(elf_gregset_t); 215062919d78SPeter Wemm status->pr_fpregsetsz = sizeof(elf_fpregset_t); 21518c9b7b2cSMarcel Moolenaar status->pr_osreldate = osreldate; 2152bd390213SMikolaj Golub status->pr_cursig = td->td_proc->p_sig; 2153bd390213SMikolaj Golub status->pr_pid = td->td_tid; 2154841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 2155bd390213SMikolaj Golub fill_regs32(td, &status->pr_reg); 215662919d78SPeter Wemm #else 2157bd390213SMikolaj Golub fill_regs(td, &status->pr_reg); 215862919d78SPeter Wemm #endif 2159bd390213SMikolaj Golub sbuf_bcat(sb, status, sizeof(*status)); 2160bd390213SMikolaj Golub free(status, M_TEMP); 21618c9b7b2cSMarcel Moolenaar } 2162bd390213SMikolaj Golub *sizep = sizeof(*status); 2163bd390213SMikolaj Golub } 2164bd390213SMikolaj Golub 2165bd390213SMikolaj Golub static void 2166bd390213SMikolaj Golub __elfN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep) 2167bd390213SMikolaj Golub { 2168bd390213SMikolaj Golub struct thread *td; 2169bd390213SMikolaj Golub elf_prfpregset_t *fpregset; 2170bd390213SMikolaj Golub 2171bd390213SMikolaj Golub td = (struct thread *)arg; 2172bd390213SMikolaj Golub if (sb != NULL) { 2173bd390213SMikolaj Golub KASSERT(*sizep == sizeof(*fpregset), ("invalid size")); 2174bd390213SMikolaj Golub fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK); 2175bd390213SMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 2176bd390213SMikolaj Golub fill_fpregs32(td, fpregset); 2177bd390213SMikolaj Golub #else 2178bd390213SMikolaj Golub fill_fpregs(td, fpregset); 2179bd390213SMikolaj Golub #endif 2180bd390213SMikolaj Golub sbuf_bcat(sb, fpregset, sizeof(*fpregset)); 2181bd390213SMikolaj Golub free(fpregset, M_TEMP); 2182bd390213SMikolaj Golub } 2183bd390213SMikolaj Golub *sizep = sizeof(*fpregset); 2184bd390213SMikolaj Golub } 2185bd390213SMikolaj Golub 2186bd390213SMikolaj Golub static void 2187bd390213SMikolaj Golub __elfN(note_thrmisc)(void *arg, struct sbuf *sb, size_t *sizep) 2188bd390213SMikolaj Golub { 2189bd390213SMikolaj Golub struct thread *td; 2190bd390213SMikolaj Golub elf_thrmisc_t thrmisc; 2191bd390213SMikolaj Golub 2192bd390213SMikolaj Golub td = (struct thread *)arg; 2193bd390213SMikolaj Golub if (sb != NULL) { 2194bd390213SMikolaj Golub KASSERT(*sizep == sizeof(thrmisc), ("invalid size")); 2195bd390213SMikolaj Golub bzero(&thrmisc._pad, sizeof(thrmisc._pad)); 2196bd390213SMikolaj Golub strcpy(thrmisc.pr_tname, td->td_name); 2197bd390213SMikolaj Golub sbuf_bcat(sb, &thrmisc, sizeof(thrmisc)); 2198bd390213SMikolaj Golub } 2199bd390213SMikolaj Golub *sizep = sizeof(thrmisc); 2200bd390213SMikolaj Golub } 2201bd390213SMikolaj Golub 220286be94fcSTycho Nightingale static void 220386be94fcSTycho Nightingale __elfN(note_ptlwpinfo)(void *arg, struct sbuf *sb, size_t *sizep) 220486be94fcSTycho Nightingale { 220586be94fcSTycho Nightingale struct thread *td; 220686be94fcSTycho Nightingale size_t size; 220786be94fcSTycho Nightingale int structsize; 220851645e83SJohn Baldwin #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 220951645e83SJohn Baldwin struct ptrace_lwpinfo32 pl; 221051645e83SJohn Baldwin #else 221186be94fcSTycho Nightingale struct ptrace_lwpinfo pl; 221251645e83SJohn Baldwin #endif 221386be94fcSTycho Nightingale 221486be94fcSTycho Nightingale td = (struct thread *)arg; 221551645e83SJohn Baldwin size = sizeof(structsize) + sizeof(pl); 221686be94fcSTycho Nightingale if (sb != NULL) { 221786be94fcSTycho Nightingale KASSERT(*sizep == size, ("invalid size")); 221851645e83SJohn Baldwin structsize = sizeof(pl); 221986be94fcSTycho Nightingale sbuf_bcat(sb, &structsize, sizeof(structsize)); 222086be94fcSTycho Nightingale bzero(&pl, sizeof(pl)); 222186be94fcSTycho Nightingale pl.pl_lwpid = td->td_tid; 222286be94fcSTycho Nightingale pl.pl_event = PL_EVENT_NONE; 222386be94fcSTycho Nightingale pl.pl_sigmask = td->td_sigmask; 222486be94fcSTycho Nightingale pl.pl_siglist = td->td_siglist; 222586be94fcSTycho Nightingale if (td->td_si.si_signo != 0) { 222686be94fcSTycho Nightingale pl.pl_event = PL_EVENT_SIGNAL; 222786be94fcSTycho Nightingale pl.pl_flags |= PL_FLAG_SI; 222851645e83SJohn Baldwin #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 222951645e83SJohn Baldwin siginfo_to_siginfo32(&td->td_si, &pl.pl_siginfo); 223051645e83SJohn Baldwin #else 223186be94fcSTycho Nightingale pl.pl_siginfo = td->td_si; 223251645e83SJohn Baldwin #endif 223386be94fcSTycho Nightingale } 223486be94fcSTycho Nightingale strcpy(pl.pl_tdname, td->td_name); 223586be94fcSTycho Nightingale /* XXX TODO: supply more information in struct ptrace_lwpinfo*/ 223651645e83SJohn Baldwin sbuf_bcat(sb, &pl, sizeof(pl)); 223786be94fcSTycho Nightingale } 223886be94fcSTycho Nightingale *sizep = size; 223986be94fcSTycho Nightingale } 224086be94fcSTycho Nightingale 22414da47b2fSMarcel Moolenaar /* 22424da47b2fSMarcel Moolenaar * Allow for MD specific notes, as well as any MD 22434da47b2fSMarcel Moolenaar * specific preparations for writing MI notes. 22444da47b2fSMarcel Moolenaar */ 22458c64af4fSJohn Polstra static void 2246bd390213SMikolaj Golub __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep) 22478c64af4fSJohn Polstra { 2248bd390213SMikolaj Golub struct thread *td; 2249bd390213SMikolaj Golub void *buf; 2250bd390213SMikolaj Golub size_t size; 22518c64af4fSJohn Polstra 2252bd390213SMikolaj Golub td = (struct thread *)arg; 2253bd390213SMikolaj Golub size = *sizep; 2254bd390213SMikolaj Golub if (size != 0 && sb != NULL) 2255bd390213SMikolaj Golub buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK); 225683a396ceSChristian Brueffer else 225783a396ceSChristian Brueffer buf = NULL; 2258bd390213SMikolaj Golub size = 0; 2259bd390213SMikolaj Golub __elfN(dump_thread)(td, buf, &size); 226064779280SKonstantin Belousov KASSERT(sb == NULL || *sizep == size, ("invalid size")); 226183a396ceSChristian Brueffer if (size != 0 && sb != NULL) 2262bd390213SMikolaj Golub sbuf_bcat(sb, buf, size); 2263a1761d73SChristian Brueffer free(buf, M_TEMP); 2264bd390213SMikolaj Golub *sizep = size; 22658c64af4fSJohn Polstra } 22668c64af4fSJohn Polstra 2267f1fca82eSMikolaj Golub #ifdef KINFO_PROC_SIZE 2268f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 2269f1fca82eSMikolaj Golub #endif 2270f1fca82eSMikolaj Golub 2271f1fca82eSMikolaj Golub static void 2272f1fca82eSMikolaj Golub __elfN(note_procstat_proc)(void *arg, struct sbuf *sb, size_t *sizep) 2273f1fca82eSMikolaj Golub { 2274f1fca82eSMikolaj Golub struct proc *p; 2275f1fca82eSMikolaj Golub size_t size; 2276f1fca82eSMikolaj Golub int structsize; 2277f1fca82eSMikolaj Golub 2278f1fca82eSMikolaj Golub p = (struct proc *)arg; 2279f1fca82eSMikolaj Golub size = sizeof(structsize) + p->p_numthreads * 2280f1fca82eSMikolaj Golub sizeof(elf_kinfo_proc_t); 2281f1fca82eSMikolaj Golub 2282f1fca82eSMikolaj Golub if (sb != NULL) { 2283f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2284f1fca82eSMikolaj Golub structsize = sizeof(elf_kinfo_proc_t); 2285f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2286f1fca82eSMikolaj Golub PROC_LOCK(p); 2287f1fca82eSMikolaj Golub kern_proc_out(p, sb, ELF_KERN_PROC_MASK); 2288f1fca82eSMikolaj Golub } 2289f1fca82eSMikolaj Golub *sizep = size; 2290f1fca82eSMikolaj Golub } 2291f1fca82eSMikolaj Golub 2292f1fca82eSMikolaj Golub #ifdef KINFO_FILE_SIZE 2293f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE); 2294f1fca82eSMikolaj Golub #endif 2295f1fca82eSMikolaj Golub 2296f1fca82eSMikolaj Golub static void 2297f1fca82eSMikolaj Golub note_procstat_files(void *arg, struct sbuf *sb, size_t *sizep) 2298f1fca82eSMikolaj Golub { 2299f1fca82eSMikolaj Golub struct proc *p; 230014bdbaf2SConrad Meyer size_t size, sect_sz, i; 230114bdbaf2SConrad Meyer ssize_t start_len, sect_len; 230214bdbaf2SConrad Meyer int structsize, filedesc_flags; 230314bdbaf2SConrad Meyer 2304bcb60d52SConrad Meyer if (coredump_pack_fileinfo) 230514bdbaf2SConrad Meyer filedesc_flags = KERN_FILEDESC_PACK_KINFO; 230614bdbaf2SConrad Meyer else 230714bdbaf2SConrad Meyer filedesc_flags = 0; 2308f1fca82eSMikolaj Golub 2309f1fca82eSMikolaj Golub p = (struct proc *)arg; 231014bdbaf2SConrad Meyer structsize = sizeof(struct kinfo_file); 2311f1fca82eSMikolaj Golub if (sb == NULL) { 2312f1fca82eSMikolaj Golub size = 0; 2313f1fca82eSMikolaj Golub sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN); 2314f1fca82eSMikolaj Golub sbuf_set_drain(sb, sbuf_drain_count, &size); 2315f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2316f1fca82eSMikolaj Golub PROC_LOCK(p); 231714bdbaf2SConrad Meyer kern_proc_filedesc_out(p, sb, -1, filedesc_flags); 2318f1fca82eSMikolaj Golub sbuf_finish(sb); 2319f1fca82eSMikolaj Golub sbuf_delete(sb); 2320f1fca82eSMikolaj Golub *sizep = size; 2321f1fca82eSMikolaj Golub } else { 232214bdbaf2SConrad Meyer sbuf_start_section(sb, &start_len); 232314bdbaf2SConrad Meyer 2324f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2325f1fca82eSMikolaj Golub PROC_LOCK(p); 232614bdbaf2SConrad Meyer kern_proc_filedesc_out(p, sb, *sizep - sizeof(structsize), 232714bdbaf2SConrad Meyer filedesc_flags); 232814bdbaf2SConrad Meyer 232914bdbaf2SConrad Meyer sect_len = sbuf_end_section(sb, start_len, 0, 0); 233014bdbaf2SConrad Meyer if (sect_len < 0) 233114bdbaf2SConrad Meyer return; 233214bdbaf2SConrad Meyer sect_sz = sect_len; 233314bdbaf2SConrad Meyer 233414bdbaf2SConrad Meyer KASSERT(sect_sz <= *sizep, 233514bdbaf2SConrad Meyer ("kern_proc_filedesc_out did not respect maxlen; " 233614bdbaf2SConrad Meyer "requested %zu, got %zu", *sizep - sizeof(structsize), 233714bdbaf2SConrad Meyer sect_sz - sizeof(structsize))); 233814bdbaf2SConrad Meyer 233914bdbaf2SConrad Meyer for (i = 0; i < *sizep - sect_sz && sb->s_error == 0; i++) 234014bdbaf2SConrad Meyer sbuf_putc(sb, 0); 2341f1fca82eSMikolaj Golub } 2342f1fca82eSMikolaj Golub } 2343f1fca82eSMikolaj Golub 2344f1fca82eSMikolaj Golub #ifdef KINFO_VMENTRY_SIZE 2345f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE); 2346f1fca82eSMikolaj Golub #endif 2347f1fca82eSMikolaj Golub 2348f1fca82eSMikolaj Golub static void 2349f1fca82eSMikolaj Golub note_procstat_vmmap(void *arg, struct sbuf *sb, size_t *sizep) 2350f1fca82eSMikolaj Golub { 2351f1fca82eSMikolaj Golub struct proc *p; 2352f1fca82eSMikolaj Golub size_t size; 2353e6b95927SConrad Meyer int structsize, vmmap_flags; 2354e6b95927SConrad Meyer 2355e6b95927SConrad Meyer if (coredump_pack_vmmapinfo) 2356e6b95927SConrad Meyer vmmap_flags = KERN_VMMAP_PACK_KINFO; 2357e6b95927SConrad Meyer else 2358e6b95927SConrad Meyer vmmap_flags = 0; 2359f1fca82eSMikolaj Golub 2360f1fca82eSMikolaj Golub p = (struct proc *)arg; 2361e6b95927SConrad Meyer structsize = sizeof(struct kinfo_vmentry); 2362f1fca82eSMikolaj Golub if (sb == NULL) { 2363f1fca82eSMikolaj Golub size = 0; 2364f1fca82eSMikolaj Golub sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN); 2365f1fca82eSMikolaj Golub sbuf_set_drain(sb, sbuf_drain_count, &size); 2366f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2367f1fca82eSMikolaj Golub PROC_LOCK(p); 2368e6b95927SConrad Meyer kern_proc_vmmap_out(p, sb, -1, vmmap_flags); 2369f1fca82eSMikolaj Golub sbuf_finish(sb); 2370f1fca82eSMikolaj Golub sbuf_delete(sb); 2371f1fca82eSMikolaj Golub *sizep = size; 2372f1fca82eSMikolaj Golub } else { 2373f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2374f1fca82eSMikolaj Golub PROC_LOCK(p); 2375e6b95927SConrad Meyer kern_proc_vmmap_out(p, sb, *sizep - sizeof(structsize), 2376e6b95927SConrad Meyer vmmap_flags); 2377f1fca82eSMikolaj Golub } 2378f1fca82eSMikolaj Golub } 2379f1fca82eSMikolaj Golub 2380f1fca82eSMikolaj Golub static void 2381f1fca82eSMikolaj Golub note_procstat_groups(void *arg, struct sbuf *sb, size_t *sizep) 2382f1fca82eSMikolaj Golub { 2383f1fca82eSMikolaj Golub struct proc *p; 2384f1fca82eSMikolaj Golub size_t size; 2385f1fca82eSMikolaj Golub int structsize; 2386f1fca82eSMikolaj Golub 2387f1fca82eSMikolaj Golub p = (struct proc *)arg; 2388f1fca82eSMikolaj Golub size = sizeof(structsize) + p->p_ucred->cr_ngroups * sizeof(gid_t); 2389f1fca82eSMikolaj Golub if (sb != NULL) { 2390f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2391f1fca82eSMikolaj Golub structsize = sizeof(gid_t); 2392f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2393f1fca82eSMikolaj Golub sbuf_bcat(sb, p->p_ucred->cr_groups, p->p_ucred->cr_ngroups * 2394f1fca82eSMikolaj Golub sizeof(gid_t)); 2395f1fca82eSMikolaj Golub } 2396f1fca82eSMikolaj Golub *sizep = size; 2397f1fca82eSMikolaj Golub } 2398f1fca82eSMikolaj Golub 2399f1fca82eSMikolaj Golub static void 2400f1fca82eSMikolaj Golub note_procstat_umask(void *arg, struct sbuf *sb, size_t *sizep) 2401f1fca82eSMikolaj Golub { 2402f1fca82eSMikolaj Golub struct proc *p; 2403f1fca82eSMikolaj Golub size_t size; 2404f1fca82eSMikolaj Golub int structsize; 2405f1fca82eSMikolaj Golub 2406f1fca82eSMikolaj Golub p = (struct proc *)arg; 2407f1fca82eSMikolaj Golub size = sizeof(structsize) + sizeof(p->p_fd->fd_cmask); 2408f1fca82eSMikolaj Golub if (sb != NULL) { 2409f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2410f1fca82eSMikolaj Golub structsize = sizeof(p->p_fd->fd_cmask); 2411f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2412f1fca82eSMikolaj Golub sbuf_bcat(sb, &p->p_fd->fd_cmask, sizeof(p->p_fd->fd_cmask)); 2413f1fca82eSMikolaj Golub } 2414f1fca82eSMikolaj Golub *sizep = size; 2415f1fca82eSMikolaj Golub } 2416f1fca82eSMikolaj Golub 2417f1fca82eSMikolaj Golub static void 2418f1fca82eSMikolaj Golub note_procstat_rlimit(void *arg, struct sbuf *sb, size_t *sizep) 2419f1fca82eSMikolaj Golub { 2420f1fca82eSMikolaj Golub struct proc *p; 2421f1fca82eSMikolaj Golub struct rlimit rlim[RLIM_NLIMITS]; 2422f1fca82eSMikolaj Golub size_t size; 2423f1fca82eSMikolaj Golub int structsize, i; 2424f1fca82eSMikolaj Golub 2425f1fca82eSMikolaj Golub p = (struct proc *)arg; 2426f1fca82eSMikolaj Golub size = sizeof(structsize) + sizeof(rlim); 2427f1fca82eSMikolaj Golub if (sb != NULL) { 2428f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2429f1fca82eSMikolaj Golub structsize = sizeof(rlim); 2430f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2431f1fca82eSMikolaj Golub PROC_LOCK(p); 2432f1fca82eSMikolaj Golub for (i = 0; i < RLIM_NLIMITS; i++) 2433f6f6d240SMateusz Guzik lim_rlimit_proc(p, i, &rlim[i]); 2434f1fca82eSMikolaj Golub PROC_UNLOCK(p); 2435f1fca82eSMikolaj Golub sbuf_bcat(sb, rlim, sizeof(rlim)); 2436f1fca82eSMikolaj Golub } 2437f1fca82eSMikolaj Golub *sizep = size; 2438f1fca82eSMikolaj Golub } 2439f1fca82eSMikolaj Golub 2440f1fca82eSMikolaj Golub static void 2441f1fca82eSMikolaj Golub note_procstat_osrel(void *arg, struct sbuf *sb, size_t *sizep) 2442f1fca82eSMikolaj Golub { 2443f1fca82eSMikolaj Golub struct proc *p; 2444f1fca82eSMikolaj Golub size_t size; 2445f1fca82eSMikolaj Golub int structsize; 2446f1fca82eSMikolaj Golub 2447f1fca82eSMikolaj Golub p = (struct proc *)arg; 2448f1fca82eSMikolaj Golub size = sizeof(structsize) + sizeof(p->p_osrel); 2449f1fca82eSMikolaj Golub if (sb != NULL) { 2450f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2451f1fca82eSMikolaj Golub structsize = sizeof(p->p_osrel); 2452f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2453f1fca82eSMikolaj Golub sbuf_bcat(sb, &p->p_osrel, sizeof(p->p_osrel)); 2454f1fca82eSMikolaj Golub } 2455f1fca82eSMikolaj Golub *sizep = size; 2456f1fca82eSMikolaj Golub } 2457f1fca82eSMikolaj Golub 2458f1fca82eSMikolaj Golub static void 2459f1fca82eSMikolaj Golub __elfN(note_procstat_psstrings)(void *arg, struct sbuf *sb, size_t *sizep) 2460f1fca82eSMikolaj Golub { 2461f1fca82eSMikolaj Golub struct proc *p; 2462f1fca82eSMikolaj Golub elf_ps_strings_t ps_strings; 2463f1fca82eSMikolaj Golub size_t size; 2464f1fca82eSMikolaj Golub int structsize; 2465f1fca82eSMikolaj Golub 2466f1fca82eSMikolaj Golub p = (struct proc *)arg; 2467f1fca82eSMikolaj Golub size = sizeof(structsize) + sizeof(ps_strings); 2468f1fca82eSMikolaj Golub if (sb != NULL) { 2469f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2470f1fca82eSMikolaj Golub structsize = sizeof(ps_strings); 2471f1fca82eSMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 2472f1fca82eSMikolaj Golub ps_strings = PTROUT(p->p_sysent->sv_psstrings); 2473f1fca82eSMikolaj Golub #else 2474f1fca82eSMikolaj Golub ps_strings = p->p_sysent->sv_psstrings; 2475f1fca82eSMikolaj Golub #endif 2476f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2477f1fca82eSMikolaj Golub sbuf_bcat(sb, &ps_strings, sizeof(ps_strings)); 2478f1fca82eSMikolaj Golub } 2479f1fca82eSMikolaj Golub *sizep = size; 2480f1fca82eSMikolaj Golub } 2481f1fca82eSMikolaj Golub 2482f1fca82eSMikolaj Golub static void 2483f1fca82eSMikolaj Golub __elfN(note_procstat_auxv)(void *arg, struct sbuf *sb, size_t *sizep) 2484f1fca82eSMikolaj Golub { 2485f1fca82eSMikolaj Golub struct proc *p; 2486f1fca82eSMikolaj Golub size_t size; 2487f1fca82eSMikolaj Golub int structsize; 2488f1fca82eSMikolaj Golub 2489f1fca82eSMikolaj Golub p = (struct proc *)arg; 2490f1fca82eSMikolaj Golub if (sb == NULL) { 2491f1fca82eSMikolaj Golub size = 0; 2492f1fca82eSMikolaj Golub sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN); 2493f1fca82eSMikolaj Golub sbuf_set_drain(sb, sbuf_drain_count, &size); 2494f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2495f1fca82eSMikolaj Golub PHOLD(p); 2496f1fca82eSMikolaj Golub proc_getauxv(curthread, p, sb); 2497f1fca82eSMikolaj Golub PRELE(p); 2498f1fca82eSMikolaj Golub sbuf_finish(sb); 2499f1fca82eSMikolaj Golub sbuf_delete(sb); 2500f1fca82eSMikolaj Golub *sizep = size; 2501f1fca82eSMikolaj Golub } else { 2502f1fca82eSMikolaj Golub structsize = sizeof(Elf_Auxinfo); 2503f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2504f1fca82eSMikolaj Golub PHOLD(p); 2505f1fca82eSMikolaj Golub proc_getauxv(curthread, p, sb); 2506f1fca82eSMikolaj Golub PRELE(p); 2507f1fca82eSMikolaj Golub } 2508f1fca82eSMikolaj Golub } 2509f1fca82eSMikolaj Golub 251032c01de2SDmitry Chagin static boolean_t 251192328a32SKonstantin Belousov __elfN(parse_notes)(struct image_params *imgp, Elf_Note *checknote, 251292328a32SKonstantin Belousov const char *note_vendor, const Elf_Phdr *pnote, 251392328a32SKonstantin Belousov boolean_t (*cb)(const Elf_Note *, void *, boolean_t *), void *cb_arg) 251432c01de2SDmitry Chagin { 2515267c52fcSKonstantin Belousov const Elf_Note *note, *note0, *note_end; 251632c01de2SDmitry Chagin const char *note_name; 25176c775eb6SKonstantin Belousov char *buf; 25186c775eb6SKonstantin Belousov int i, error; 25196c775eb6SKonstantin Belousov boolean_t res; 252032c01de2SDmitry Chagin 25216c775eb6SKonstantin Belousov /* We need some limit, might as well use PAGE_SIZE. */ 25226c775eb6SKonstantin Belousov if (pnote == NULL || pnote->p_filesz > PAGE_SIZE) 252332c01de2SDmitry Chagin return (FALSE); 25246c775eb6SKonstantin Belousov ASSERT_VOP_LOCKED(imgp->vp, "parse_notes"); 25256c775eb6SKonstantin Belousov if (pnote->p_offset > PAGE_SIZE || 25266c775eb6SKonstantin Belousov pnote->p_filesz > PAGE_SIZE - pnote->p_offset) { 25276c775eb6SKonstantin Belousov VOP_UNLOCK(imgp->vp, 0); 25286c775eb6SKonstantin Belousov buf = malloc(pnote->p_filesz, M_TEMP, M_WAITOK); 25296c775eb6SKonstantin Belousov vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 25306c775eb6SKonstantin Belousov error = vn_rdwr(UIO_READ, imgp->vp, buf, pnote->p_filesz, 25316c775eb6SKonstantin Belousov pnote->p_offset, UIO_SYSSPACE, IO_NODELOCKED, 25326c775eb6SKonstantin Belousov curthread->td_ucred, NOCRED, NULL, curthread); 25336c775eb6SKonstantin Belousov if (error != 0) { 25346c775eb6SKonstantin Belousov uprintf("i/o error PT_NOTE\n"); 2535eda8fe63SKonstantin Belousov goto retf; 25366c775eb6SKonstantin Belousov } 25376c775eb6SKonstantin Belousov note = note0 = (const Elf_Note *)buf; 25386c775eb6SKonstantin Belousov note_end = (const Elf_Note *)(buf + pnote->p_filesz); 25396c775eb6SKonstantin Belousov } else { 25406c775eb6SKonstantin Belousov note = note0 = (const Elf_Note *)(imgp->image_header + 25416c775eb6SKonstantin Belousov pnote->p_offset); 254232c01de2SDmitry Chagin note_end = (const Elf_Note *)(imgp->image_header + 254332c01de2SDmitry Chagin pnote->p_offset + pnote->p_filesz); 25446c775eb6SKonstantin Belousov buf = NULL; 25456c775eb6SKonstantin Belousov } 2546267c52fcSKonstantin Belousov for (i = 0; i < 100 && note >= note0 && note < note_end; i++) { 2547d1ae5c83SKonstantin Belousov if (!aligned(note, Elf32_Addr) || (const char *)note_end - 25486c775eb6SKonstantin Belousov (const char *)note < sizeof(Elf_Note)) { 2549eda8fe63SKonstantin Belousov goto retf; 25506c775eb6SKonstantin Belousov } 255192328a32SKonstantin Belousov if (note->n_namesz != checknote->n_namesz || 255292328a32SKonstantin Belousov note->n_descsz != checknote->n_descsz || 255392328a32SKonstantin Belousov note->n_type != checknote->n_type) 255432c01de2SDmitry Chagin goto nextnote; 255532c01de2SDmitry Chagin note_name = (const char *)(note + 1); 255692328a32SKonstantin Belousov if (note_name + checknote->n_namesz >= 255792328a32SKonstantin Belousov (const char *)note_end || strncmp(note_vendor, 255892328a32SKonstantin Belousov note_name, checknote->n_namesz) != 0) 255932c01de2SDmitry Chagin goto nextnote; 256032c01de2SDmitry Chagin 256192328a32SKonstantin Belousov if (cb(note, cb_arg, &res)) 25626c775eb6SKonstantin Belousov goto ret; 256332c01de2SDmitry Chagin nextnote: 256432c01de2SDmitry Chagin note = (const Elf_Note *)((const char *)(note + 1) + 25651b8388cdSMikolaj Golub roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) + 25661b8388cdSMikolaj Golub roundup2(note->n_descsz, ELF_NOTE_ROUNDSIZE)); 256732c01de2SDmitry Chagin } 2568eda8fe63SKonstantin Belousov retf: 25696c775eb6SKonstantin Belousov res = FALSE; 25706c775eb6SKonstantin Belousov ret: 25716c775eb6SKonstantin Belousov free(buf, M_TEMP); 25726c775eb6SKonstantin Belousov return (res); 257332c01de2SDmitry Chagin } 257432c01de2SDmitry Chagin 257592328a32SKonstantin Belousov struct brandnote_cb_arg { 257692328a32SKonstantin Belousov Elf_Brandnote *brandnote; 257792328a32SKonstantin Belousov int32_t *osrel; 257892328a32SKonstantin Belousov }; 257992328a32SKonstantin Belousov 258092328a32SKonstantin Belousov static boolean_t 258192328a32SKonstantin Belousov brandnote_cb(const Elf_Note *note, void *arg0, boolean_t *res) 258292328a32SKonstantin Belousov { 258392328a32SKonstantin Belousov struct brandnote_cb_arg *arg; 258492328a32SKonstantin Belousov 258592328a32SKonstantin Belousov arg = arg0; 258692328a32SKonstantin Belousov 258792328a32SKonstantin Belousov /* 258892328a32SKonstantin Belousov * Fetch the osreldate for binary from the ELF OSABI-note if 258992328a32SKonstantin Belousov * necessary. 259092328a32SKonstantin Belousov */ 259192328a32SKonstantin Belousov *res = (arg->brandnote->flags & BN_TRANSLATE_OSREL) != 0 && 259292328a32SKonstantin Belousov arg->brandnote->trans_osrel != NULL ? 259392328a32SKonstantin Belousov arg->brandnote->trans_osrel(note, arg->osrel) : TRUE; 259492328a32SKonstantin Belousov 259592328a32SKonstantin Belousov return (TRUE); 259692328a32SKonstantin Belousov } 259792328a32SKonstantin Belousov 2598cefb93f2SKonstantin Belousov static Elf_Note fctl_note = { 2599cefb93f2SKonstantin Belousov .n_namesz = sizeof(FREEBSD_ABI_VENDOR), 2600cefb93f2SKonstantin Belousov .n_descsz = sizeof(uint32_t), 2601cefb93f2SKonstantin Belousov .n_type = NT_FREEBSD_FEATURE_CTL, 2602cefb93f2SKonstantin Belousov }; 2603cefb93f2SKonstantin Belousov 2604cefb93f2SKonstantin Belousov struct fctl_cb_arg { 2605cefb93f2SKonstantin Belousov uint32_t *fctl0; 2606cefb93f2SKonstantin Belousov }; 2607cefb93f2SKonstantin Belousov 2608cefb93f2SKonstantin Belousov static boolean_t 2609cefb93f2SKonstantin Belousov note_fctl_cb(const Elf_Note *note, void *arg0, boolean_t *res) 2610cefb93f2SKonstantin Belousov { 2611cefb93f2SKonstantin Belousov struct fctl_cb_arg *arg; 2612cefb93f2SKonstantin Belousov const Elf32_Word *desc; 2613cefb93f2SKonstantin Belousov uintptr_t p; 2614cefb93f2SKonstantin Belousov 2615cefb93f2SKonstantin Belousov arg = arg0; 2616cefb93f2SKonstantin Belousov p = (uintptr_t)(note + 1); 2617cefb93f2SKonstantin Belousov p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE); 2618cefb93f2SKonstantin Belousov desc = (const Elf32_Word *)p; 2619cefb93f2SKonstantin Belousov *arg->fctl0 = desc[0]; 2620cefb93f2SKonstantin Belousov return (TRUE); 2621cefb93f2SKonstantin Belousov } 2622cefb93f2SKonstantin Belousov 262332c01de2SDmitry Chagin /* 2624cefb93f2SKonstantin Belousov * Try to find the appropriate ABI-note section for checknote, fetch 2625cefb93f2SKonstantin Belousov * the osreldate and feature control flags for binary from the ELF 2626cefb93f2SKonstantin Belousov * OSABI-note. Only the first page of the image is searched, the same 2627cefb93f2SKonstantin Belousov * as for headers. 26281a9c7decSKonstantin Belousov */ 26291a9c7decSKonstantin Belousov static boolean_t 263092328a32SKonstantin Belousov __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *brandnote, 2631cefb93f2SKonstantin Belousov int32_t *osrel, uint32_t *fctl0) 26321a9c7decSKonstantin Belousov { 26331a9c7decSKonstantin Belousov const Elf_Phdr *phdr; 26341a9c7decSKonstantin Belousov const Elf_Ehdr *hdr; 263592328a32SKonstantin Belousov struct brandnote_cb_arg b_arg; 2636cefb93f2SKonstantin Belousov struct fctl_cb_arg f_arg; 2637cefb93f2SKonstantin Belousov int i, j; 26381a9c7decSKonstantin Belousov 26391a9c7decSKonstantin Belousov hdr = (const Elf_Ehdr *)imgp->image_header; 26401a9c7decSKonstantin Belousov phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 264192328a32SKonstantin Belousov b_arg.brandnote = brandnote; 264292328a32SKonstantin Belousov b_arg.osrel = osrel; 2643cefb93f2SKonstantin Belousov f_arg.fctl0 = fctl0; 26441a9c7decSKonstantin Belousov 26451a9c7decSKonstantin Belousov for (i = 0; i < hdr->e_phnum; i++) { 264692328a32SKonstantin Belousov if (phdr[i].p_type == PT_NOTE && __elfN(parse_notes)(imgp, 264792328a32SKonstantin Belousov &brandnote->hdr, brandnote->vendor, &phdr[i], brandnote_cb, 264892328a32SKonstantin Belousov &b_arg)) { 2649cefb93f2SKonstantin Belousov for (j = 0; j < hdr->e_phnum; j++) { 2650cefb93f2SKonstantin Belousov if (phdr[j].p_type == PT_NOTE && 2651cefb93f2SKonstantin Belousov __elfN(parse_notes)(imgp, &fctl_note, 2652cefb93f2SKonstantin Belousov FREEBSD_ABI_VENDOR, &phdr[j], 2653cefb93f2SKonstantin Belousov note_fctl_cb, &f_arg)) 2654cefb93f2SKonstantin Belousov break; 2655cefb93f2SKonstantin Belousov } 26561a9c7decSKonstantin Belousov return (TRUE); 26571a9c7decSKonstantin Belousov } 265892328a32SKonstantin Belousov } 26591a9c7decSKonstantin Belousov return (FALSE); 26601a9c7decSKonstantin Belousov 26611a9c7decSKonstantin Belousov } 26621a9c7decSKonstantin Belousov 26631a9c7decSKonstantin Belousov /* 2664e1743d02SSøren Schmidt * Tell kern_execve.c about it, with a little help from the linker. 2665e1743d02SSøren Schmidt */ 2666a360a43dSJake Burkholder static struct execsw __elfN(execsw) = { 2667b7feabf9SEd Maste .ex_imgact = __CONCAT(exec_, __elfN(imgact)), 2668b7feabf9SEd Maste .ex_name = __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) 2669a360a43dSJake Burkholder }; 2670a360a43dSJake Burkholder EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw)); 2671e7228204SAlfred Perlstein 2672ed167eaaSKonstantin Belousov static vm_prot_t 2673ed167eaaSKonstantin Belousov __elfN(trans_prot)(Elf_Word flags) 2674ed167eaaSKonstantin Belousov { 2675ed167eaaSKonstantin Belousov vm_prot_t prot; 2676ed167eaaSKonstantin Belousov 2677ed167eaaSKonstantin Belousov prot = 0; 2678ed167eaaSKonstantin Belousov if (flags & PF_X) 2679ed167eaaSKonstantin Belousov prot |= VM_PROT_EXECUTE; 2680ed167eaaSKonstantin Belousov if (flags & PF_W) 2681ed167eaaSKonstantin Belousov prot |= VM_PROT_WRITE; 2682ed167eaaSKonstantin Belousov if (flags & PF_R) 2683ed167eaaSKonstantin Belousov prot |= VM_PROT_READ; 2684eb785fabSKonstantin Belousov #if __ELF_WORD_SIZE == 32 && (defined(__amd64__) || defined(__i386__)) 2685126b36a2SKonstantin Belousov if (i386_read_exec && (flags & PF_R)) 2686676eda08SMarcel Moolenaar prot |= VM_PROT_EXECUTE; 2687676eda08SMarcel Moolenaar #endif 2688ed167eaaSKonstantin Belousov return (prot); 2689ed167eaaSKonstantin Belousov } 2690ed167eaaSKonstantin Belousov 2691ed167eaaSKonstantin Belousov static Elf_Word 2692ed167eaaSKonstantin Belousov __elfN(untrans_prot)(vm_prot_t prot) 2693ed167eaaSKonstantin Belousov { 2694ed167eaaSKonstantin Belousov Elf_Word flags; 2695ed167eaaSKonstantin Belousov 2696ed167eaaSKonstantin Belousov flags = 0; 2697ed167eaaSKonstantin Belousov if (prot & VM_PROT_EXECUTE) 2698ed167eaaSKonstantin Belousov flags |= PF_X; 2699ed167eaaSKonstantin Belousov if (prot & VM_PROT_READ) 2700ed167eaaSKonstantin Belousov flags |= PF_R; 2701ed167eaaSKonstantin Belousov if (prot & VM_PROT_WRITE) 2702ed167eaaSKonstantin Belousov flags |= PF_W; 2703ed167eaaSKonstantin Belousov return (flags); 2704ed167eaaSKonstantin Belousov } 2705