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, 9109c78d53SEdward 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 105*7029da5cSPawel Biernacki SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), 106*7029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 107a360a43dSJake Burkholder ""); 108a360a43dSJake Burkholder 109bd390213SMikolaj Golub #define CORE_BUF_SIZE (16 * 1024) 110e7228204SAlfred Perlstein 111e548a1d4SJake Burkholder int __elfN(fallback_brand) = -1; 112e548a1d4SJake Burkholder SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, 113af3b2549SHans Petter Selasky fallback_brand, CTLFLAG_RWTUN, &__elfN(fallback_brand), 0, 114a360a43dSJake Burkholder __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort"); 115a360a43dSJake Burkholder 116551d79e1SMarcel Moolenaar static int elf_legacy_coredump = 0; 117a360a43dSJake Burkholder SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW, 1181cbb879dSEd Maste &elf_legacy_coredump, 0, 1191cbb879dSEd Maste "include all and only RW pages in core dumps"); 120e1743d02SSøren Schmidt 12162c625fdSKonstantin Belousov int __elfN(nxstack) = 1224d22d07aSKonstantin Belousov #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */ || \ 1234bf4b0f1SJohn Baldwin (defined(__arm__) && __ARM_ARCH >= 7) || defined(__aarch64__) || \ 1244bf4b0f1SJohn Baldwin defined(__riscv) 12562c625fdSKonstantin Belousov 1; 12662c625fdSKonstantin Belousov #else 12762c625fdSKonstantin Belousov 0; 12862c625fdSKonstantin Belousov #endif 129291c06a1SKonstantin Belousov SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, 130291c06a1SKonstantin Belousov nxstack, CTLFLAG_RW, &__elfN(nxstack), 0, 131291c06a1SKonstantin Belousov __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack"); 132291c06a1SKonstantin Belousov 133eb785fabSKonstantin Belousov #if __ELF_WORD_SIZE == 32 && (defined(__amd64__) || defined(__i386__)) 134126b36a2SKonstantin Belousov int i386_read_exec = 0; 135126b36a2SKonstantin Belousov SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0, 136126b36a2SKonstantin Belousov "enable execution from readable segments"); 137126b36a2SKonstantin Belousov #endif 138126b36a2SKonstantin Belousov 13995aafd69SKonstantin Belousov static u_long __elfN(pie_base) = ET_DYN_LOAD_ADDR; 140f33533daSKonstantin Belousov static int 141f33533daSKonstantin Belousov sysctl_pie_base(SYSCTL_HANDLER_ARGS) 142f33533daSKonstantin Belousov { 143f33533daSKonstantin Belousov u_long val; 144f33533daSKonstantin Belousov int error; 145f33533daSKonstantin Belousov 146f33533daSKonstantin Belousov val = __elfN(pie_base); 147f33533daSKonstantin Belousov error = sysctl_handle_long(oidp, &val, 0, req); 148f33533daSKonstantin Belousov if (error != 0 || req->newptr == NULL) 149f33533daSKonstantin Belousov return (error); 150f33533daSKonstantin Belousov if ((val & PAGE_MASK) != 0) 151f33533daSKonstantin Belousov return (EINVAL); 152f33533daSKonstantin Belousov __elfN(pie_base) = val; 153f33533daSKonstantin Belousov return (0); 154f33533daSKonstantin Belousov } 155f33533daSKonstantin Belousov SYSCTL_PROC(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, pie_base, 156f33533daSKonstantin Belousov CTLTYPE_ULONG | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0, 157f33533daSKonstantin Belousov sysctl_pie_base, "LU", 15895aafd69SKonstantin Belousov "PIE load base without randomization"); 15995aafd69SKonstantin Belousov 160*7029da5cSPawel Biernacki SYSCTL_NODE(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, aslr, 161*7029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 162fa50a355SKonstantin Belousov ""); 163fa50a355SKonstantin Belousov #define ASLR_NODE_OID __CONCAT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), _aslr) 164fa50a355SKonstantin Belousov 165fa50a355SKonstantin Belousov static int __elfN(aslr_enabled) = 0; 166fa50a355SKonstantin Belousov SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, enable, CTLFLAG_RWTUN, 167fa50a355SKonstantin Belousov &__elfN(aslr_enabled), 0, 168fa50a355SKonstantin Belousov __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) 169fa50a355SKonstantin Belousov ": enable address map randomization"); 170fa50a355SKonstantin Belousov 171fa50a355SKonstantin Belousov static int __elfN(pie_aslr_enabled) = 0; 172fa50a355SKonstantin Belousov SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, pie_enable, CTLFLAG_RWTUN, 173fa50a355SKonstantin Belousov &__elfN(pie_aslr_enabled), 0, 174fa50a355SKonstantin Belousov __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) 175fa50a355SKonstantin Belousov ": enable address map randomization for PIE binaries"); 176fa50a355SKonstantin Belousov 177fa50a355SKonstantin Belousov static int __elfN(aslr_honor_sbrk) = 1; 178fa50a355SKonstantin Belousov SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, honor_sbrk, CTLFLAG_RW, 179fa50a355SKonstantin Belousov &__elfN(aslr_honor_sbrk), 0, 180fa50a355SKonstantin Belousov __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": assume sbrk is used"); 181fa50a355SKonstantin Belousov 182fc83c5a7SKonstantin Belousov static int __elfN(aslr_stack_gap) = 3; 183fc83c5a7SKonstantin Belousov SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, stack_gap, CTLFLAG_RW, 184fc83c5a7SKonstantin Belousov &__elfN(aslr_stack_gap), 0, 185fc83c5a7SKonstantin Belousov __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) 186fc83c5a7SKonstantin Belousov ": maximum percentage of main stack to waste on a random gap"); 187fc83c5a7SKonstantin Belousov 188944cf37bSKonstantin Belousov static int __elfN(sigfastblock) = 1; 189944cf37bSKonstantin Belousov SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, sigfastblock, 190944cf37bSKonstantin Belousov CTLFLAG_RWTUN, &__elfN(sigfastblock), 0, 191944cf37bSKonstantin Belousov "enable sigfastblock for new processes"); 192944cf37bSKonstantin Belousov 1933ebc1248SPeter Wemm static Elf_Brandinfo *elf_brand_list[MAX_BRANDS]; 194e1743d02SSøren Schmidt 195545517f1SEdward Tomasz Napierala #define aligned(a, t) (rounddown2((u_long)(a), sizeof(t)) == (u_long)(a)) 19693d1c728SKonstantin Belousov 19732c01de2SDmitry Chagin static const char FREEBSD_ABI_VENDOR[] = "FreeBSD"; 19832c01de2SDmitry Chagin 19932c01de2SDmitry Chagin Elf_Brandnote __elfN(freebsd_brandnote) = { 20032c01de2SDmitry Chagin .hdr.n_namesz = sizeof(FREEBSD_ABI_VENDOR), 20132c01de2SDmitry Chagin .hdr.n_descsz = sizeof(int32_t), 2024c22b468SEd Maste .hdr.n_type = NT_FREEBSD_ABI_TAG, 20332c01de2SDmitry Chagin .vendor = FREEBSD_ABI_VENDOR, 20489ffc202SBjoern A. Zeeb .flags = BN_TRANSLATE_OSREL, 20589ffc202SBjoern A. Zeeb .trans_osrel = __elfN(freebsd_trans_osrel) 20632c01de2SDmitry Chagin }; 20732c01de2SDmitry Chagin 208a95659f7SEd Maste static bool 20989ffc202SBjoern A. Zeeb __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel) 21089ffc202SBjoern A. Zeeb { 21189ffc202SBjoern A. Zeeb uintptr_t p; 21289ffc202SBjoern A. Zeeb 21389ffc202SBjoern A. Zeeb p = (uintptr_t)(note + 1); 2141b8388cdSMikolaj Golub p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE); 21589ffc202SBjoern A. Zeeb *osrel = *(const int32_t *)(p); 21689ffc202SBjoern A. Zeeb 217a95659f7SEd Maste return (true); 21889ffc202SBjoern A. Zeeb } 21989ffc202SBjoern A. Zeeb 22089ffc202SBjoern A. Zeeb static const char GNU_ABI_VENDOR[] = "GNU"; 22189ffc202SBjoern A. Zeeb static int GNU_KFREEBSD_ABI_DESC = 3; 22289ffc202SBjoern A. Zeeb 22389ffc202SBjoern A. Zeeb Elf_Brandnote __elfN(kfreebsd_brandnote) = { 22489ffc202SBjoern A. Zeeb .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 22589ffc202SBjoern A. Zeeb .hdr.n_descsz = 16, /* XXX at least 16 */ 22689ffc202SBjoern A. Zeeb .hdr.n_type = 1, 22789ffc202SBjoern A. Zeeb .vendor = GNU_ABI_VENDOR, 22889ffc202SBjoern A. Zeeb .flags = BN_TRANSLATE_OSREL, 22989ffc202SBjoern A. Zeeb .trans_osrel = kfreebsd_trans_osrel 23089ffc202SBjoern A. Zeeb }; 23189ffc202SBjoern A. Zeeb 232a95659f7SEd Maste static bool 23389ffc202SBjoern A. Zeeb kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel) 23489ffc202SBjoern A. Zeeb { 23589ffc202SBjoern A. Zeeb const Elf32_Word *desc; 23689ffc202SBjoern A. Zeeb uintptr_t p; 23789ffc202SBjoern A. Zeeb 23889ffc202SBjoern A. Zeeb p = (uintptr_t)(note + 1); 2391b8388cdSMikolaj Golub p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE); 24089ffc202SBjoern A. Zeeb 24189ffc202SBjoern A. Zeeb desc = (const Elf32_Word *)p; 24289ffc202SBjoern A. Zeeb if (desc[0] != GNU_KFREEBSD_ABI_DESC) 243a95659f7SEd Maste return (false); 24489ffc202SBjoern A. Zeeb 24589ffc202SBjoern A. Zeeb /* 24689ffc202SBjoern A. Zeeb * Debian GNU/kFreeBSD embed the earliest compatible kernel version 24789ffc202SBjoern A. Zeeb * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way. 24889ffc202SBjoern A. Zeeb */ 24989ffc202SBjoern A. Zeeb *osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3]; 25089ffc202SBjoern A. Zeeb 251a95659f7SEd Maste return (true); 25289ffc202SBjoern A. Zeeb } 25389ffc202SBjoern A. Zeeb 254e1743d02SSøren Schmidt int 2553ebc1248SPeter Wemm __elfN(insert_brand_entry)(Elf_Brandinfo *entry) 256e1743d02SSøren Schmidt { 257e1743d02SSøren Schmidt int i; 258e1743d02SSøren Schmidt 2593ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 260ea5a2b2eSSøren Schmidt if (elf_brand_list[i] == NULL) { 261ea5a2b2eSSøren Schmidt elf_brand_list[i] = entry; 262e1743d02SSøren Schmidt break; 263e1743d02SSøren Schmidt } 264e1743d02SSøren Schmidt } 265925c8b5bSBjoern A. Zeeb if (i == MAX_BRANDS) { 266925c8b5bSBjoern A. Zeeb printf("WARNING: %s: could not insert brandinfo entry: %p\n", 267925c8b5bSBjoern A. Zeeb __func__, entry); 268a7cddfedSJake Burkholder return (-1); 269925c8b5bSBjoern A. Zeeb } 270a7cddfedSJake Burkholder return (0); 271e1743d02SSøren Schmidt } 272e1743d02SSøren Schmidt 273e1743d02SSøren Schmidt int 2743ebc1248SPeter Wemm __elfN(remove_brand_entry)(Elf_Brandinfo *entry) 275e1743d02SSøren Schmidt { 276e1743d02SSøren Schmidt int i; 277e1743d02SSøren Schmidt 2783ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 279ea5a2b2eSSøren Schmidt if (elf_brand_list[i] == entry) { 280ea5a2b2eSSøren Schmidt elf_brand_list[i] = NULL; 281e1743d02SSøren Schmidt break; 282e1743d02SSøren Schmidt } 283e1743d02SSøren Schmidt } 284ea5a2b2eSSøren Schmidt if (i == MAX_BRANDS) 285a7cddfedSJake Burkholder return (-1); 286a7cddfedSJake Burkholder return (0); 287e1743d02SSøren Schmidt } 288e1743d02SSøren Schmidt 289096977faSMark Newton int 2903ebc1248SPeter Wemm __elfN(brand_inuse)(Elf_Brandinfo *entry) 291096977faSMark Newton { 292096977faSMark Newton struct proc *p; 293553629ebSJake Burkholder int rval = FALSE; 294096977faSMark Newton 2951005a129SJohn Baldwin sx_slock(&allproc_lock); 2964f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) { 297553629ebSJake Burkholder if (p->p_sysent == entry->sysvec) { 298553629ebSJake Burkholder rval = TRUE; 299553629ebSJake Burkholder break; 300096977faSMark Newton } 301553629ebSJake Burkholder } 3021005a129SJohn Baldwin sx_sunlock(&allproc_lock); 303096977faSMark Newton 304553629ebSJake Burkholder return (rval); 305096977faSMark Newton } 306096977faSMark Newton 3075fe3ed62SJake Burkholder static Elf_Brandinfo * 30832c01de2SDmitry Chagin __elfN(get_brandinfo)(struct image_params *imgp, const char *interp, 30909c78d53SEdward Tomasz Napierala int32_t *osrel, uint32_t *fctl0) 3105fe3ed62SJake Burkholder { 31132c01de2SDmitry Chagin const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header; 312af582aaeSKonstantin Belousov Elf_Brandinfo *bi, *bi_m; 31332c01de2SDmitry Chagin boolean_t ret; 31409c78d53SEdward Tomasz Napierala int i, interp_name_len; 31509c78d53SEdward Tomasz Napierala 316be7808dcSKonstantin Belousov interp_name_len = interp != NULL ? strlen(interp) + 1 : 0; 3175fe3ed62SJake Burkholder 3185fe3ed62SJake Burkholder /* 31932c01de2SDmitry Chagin * We support four types of branding -- (1) the ELF EI_OSABI field 3205fe3ed62SJake Burkholder * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string 32132c01de2SDmitry Chagin * branding w/in the ELF header, (3) path of the `interp_path' 32232c01de2SDmitry Chagin * field, and (4) the ".note.ABI-tag" ELF section. 3235fe3ed62SJake Burkholder */ 3245fe3ed62SJake Burkholder 32532c01de2SDmitry Chagin /* Look for an ".note.ABI-tag" ELF section */ 326af582aaeSKonstantin Belousov bi_m = NULL; 32732c01de2SDmitry Chagin for (i = 0; i < MAX_BRANDS; i++) { 32832c01de2SDmitry Chagin bi = elf_brand_list[i]; 329ecc2fda8SBjoern A. Zeeb if (bi == NULL) 330ecc2fda8SBjoern A. Zeeb continue; 3312274ab3dSKonstantin Belousov if (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0) 3321438fe3cSKonstantin Belousov continue; 333ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && (bi->flags & 334ecc2fda8SBjoern A. Zeeb (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) { 335cefb93f2SKonstantin Belousov ret = __elfN(check_note)(imgp, bi->brand_note, osrel, 336cefb93f2SKonstantin Belousov fctl0); 337f19d421aSNathan Whitehorn /* Give brand a chance to veto check_note's guess */ 338f19d421aSNathan Whitehorn if (ret && bi->header_supported) 339f19d421aSNathan Whitehorn ret = bi->header_supported(imgp); 340af582aaeSKonstantin Belousov /* 341af582aaeSKonstantin Belousov * If note checker claimed the binary, but the 342af582aaeSKonstantin Belousov * interpreter path in the image does not 343af582aaeSKonstantin Belousov * match default one for the brand, try to 344af582aaeSKonstantin Belousov * search for other brands with the same 345af582aaeSKonstantin Belousov * interpreter. Either there is better brand 346af582aaeSKonstantin Belousov * with the right interpreter, or, failing 347af582aaeSKonstantin Belousov * this, we return first brand which accepted 348af582aaeSKonstantin Belousov * our note and, optionally, header. 349af582aaeSKonstantin Belousov */ 3503aeacc55SKonstantin Belousov if (ret && bi_m == NULL && interp != NULL && 3513aeacc55SKonstantin Belousov (bi->interp_path == NULL || 3523aeacc55SKonstantin Belousov (strlen(bi->interp_path) + 1 != interp_name_len || 3533aeacc55SKonstantin Belousov strncmp(interp, bi->interp_path, interp_name_len) 3543aeacc55SKonstantin Belousov != 0))) { 355af582aaeSKonstantin Belousov bi_m = bi; 356af582aaeSKonstantin Belousov ret = 0; 357af582aaeSKonstantin Belousov } 35832c01de2SDmitry Chagin if (ret) 35932c01de2SDmitry Chagin return (bi); 36032c01de2SDmitry Chagin } 36132c01de2SDmitry Chagin } 362af582aaeSKonstantin Belousov if (bi_m != NULL) 363af582aaeSKonstantin Belousov return (bi_m); 36432c01de2SDmitry Chagin 3655fe3ed62SJake Burkholder /* If the executable has a brand, search for it in the brand list. */ 3665fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 3675fe3ed62SJake Burkholder bi = elf_brand_list[i]; 3681438fe3cSKonstantin Belousov if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 || 3692274ab3dSKonstantin Belousov (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0)) 370ecc2fda8SBjoern A. Zeeb continue; 371ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 3725fe3ed62SJake Burkholder (hdr->e_ident[EI_OSABI] == bi->brand || 3730fe98320SEd Schouten (bi->compat_3_brand != NULL && 3743d560b4bSKonstantin Belousov strcmp((const char *)&hdr->e_ident[OLD_EI_BRAND], 3750fe98320SEd Schouten bi->compat_3_brand) == 0))) { 376686d2f31SNathan Whitehorn /* Looks good, but give brand a chance to veto */ 377d722231bSJohn Baldwin if (bi->header_supported == NULL || 37815a9aedfSKonstantin Belousov bi->header_supported(imgp)) { 37915a9aedfSKonstantin Belousov /* 38015a9aedfSKonstantin Belousov * Again, prefer strictly matching 38115a9aedfSKonstantin Belousov * interpreter path. 38215a9aedfSKonstantin Belousov */ 3837aab7a80SKonstantin Belousov if (interp_name_len == 0 && 3847aab7a80SKonstantin Belousov bi->interp_path == NULL) 3857aab7a80SKonstantin Belousov return (bi); 3867aab7a80SKonstantin Belousov if (bi->interp_path != NULL && 3877aab7a80SKonstantin Belousov strlen(bi->interp_path) + 1 == 38815a9aedfSKonstantin Belousov interp_name_len && strncmp(interp, 38915a9aedfSKonstantin Belousov bi->interp_path, interp_name_len) == 0) 3905fe3ed62SJake Burkholder return (bi); 39115a9aedfSKonstantin Belousov if (bi_m == NULL) 39215a9aedfSKonstantin Belousov bi_m = bi; 3935fe3ed62SJake Burkholder } 394686d2f31SNathan Whitehorn } 39515a9aedfSKonstantin Belousov } 39615a9aedfSKonstantin Belousov if (bi_m != NULL) 39715a9aedfSKonstantin Belousov return (bi_m); 3985fe3ed62SJake Burkholder 399817dc004SWarner Losh /* No known brand, see if the header is recognized by any brand */ 400817dc004SWarner Losh for (i = 0; i < MAX_BRANDS; i++) { 401817dc004SWarner Losh bi = elf_brand_list[i]; 402817dc004SWarner Losh if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY || 403817dc004SWarner Losh bi->header_supported == NULL) 404817dc004SWarner Losh continue; 405817dc004SWarner Losh if (hdr->e_machine == bi->machine) { 406817dc004SWarner Losh ret = bi->header_supported(imgp); 407817dc004SWarner Losh if (ret) 408817dc004SWarner Losh return (bi); 409817dc004SWarner Losh } 410817dc004SWarner Losh } 411817dc004SWarner Losh 4125fe3ed62SJake Burkholder /* Lacking a known brand, search for a recognized interpreter. */ 4135fe3ed62SJake Burkholder if (interp != NULL) { 4145fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 4155fe3ed62SJake Burkholder bi = elf_brand_list[i]; 4162274ab3dSKonstantin Belousov if (bi == NULL || (bi->flags & 4172274ab3dSKonstantin Belousov (BI_BRAND_NOTE_MANDATORY | BI_BRAND_ONLY_STATIC)) 4182274ab3dSKonstantin Belousov != 0) 419ecc2fda8SBjoern A. Zeeb continue; 420ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 4213aeacc55SKonstantin Belousov bi->interp_path != NULL && 422d1ae5c83SKonstantin Belousov /* ELF image p_filesz includes terminating zero */ 423d1ae5c83SKonstantin Belousov strlen(bi->interp_path) + 1 == interp_name_len && 424d1ae5c83SKonstantin Belousov strncmp(interp, bi->interp_path, interp_name_len) 425d722231bSJohn Baldwin == 0 && (bi->header_supported == NULL || 426d722231bSJohn Baldwin bi->header_supported(imgp))) 4275fe3ed62SJake Burkholder return (bi); 4285fe3ed62SJake Burkholder } 4295fe3ed62SJake Burkholder } 4305fe3ed62SJake Burkholder 4315fe3ed62SJake Burkholder /* Lacking a recognized interpreter, try the default brand */ 4325fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 4335fe3ed62SJake Burkholder bi = elf_brand_list[i]; 4341438fe3cSKonstantin Belousov if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 || 4352274ab3dSKonstantin Belousov (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0)) 436ecc2fda8SBjoern A. Zeeb continue; 437ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 438d722231bSJohn Baldwin __elfN(fallback_brand) == bi->brand && 439d722231bSJohn Baldwin (bi->header_supported == NULL || 440d722231bSJohn Baldwin bi->header_supported(imgp))) 4415fe3ed62SJake Burkholder return (bi); 4425fe3ed62SJake Burkholder } 4435fe3ed62SJake Burkholder return (NULL); 4445fe3ed62SJake Burkholder } 4455fe3ed62SJake Burkholder 446e1743d02SSøren Schmidt static int 4473ebc1248SPeter Wemm __elfN(check_header)(const Elf_Ehdr *hdr) 448e1743d02SSøren Schmidt { 449d0ca7c29SPeter Wemm Elf_Brandinfo *bi; 4503ebc1248SPeter Wemm int i; 4513ebc1248SPeter Wemm 45252c24af7SPeter Wemm if (!IS_ELF(*hdr) || 45352c24af7SPeter Wemm hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 45452c24af7SPeter Wemm hdr->e_ident[EI_DATA] != ELF_TARG_DATA || 4553dc19c46SJacques Vidrine hdr->e_ident[EI_VERSION] != EV_CURRENT || 4563dc19c46SJacques Vidrine hdr->e_phentsize != sizeof(Elf_Phdr) || 4573dc19c46SJacques Vidrine hdr->e_version != ELF_TARG_VER) 458a7cddfedSJake Burkholder return (ENOEXEC); 459e1743d02SSøren Schmidt 4603ebc1248SPeter Wemm /* 4613ebc1248SPeter Wemm * Make sure we have at least one brand for this machine. 4623ebc1248SPeter Wemm */ 4633ebc1248SPeter Wemm 4643ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 465d0ca7c29SPeter Wemm bi = elf_brand_list[i]; 466d0ca7c29SPeter Wemm if (bi != NULL && bi->machine == hdr->e_machine) 4673ebc1248SPeter Wemm break; 4683ebc1248SPeter Wemm } 4693ebc1248SPeter Wemm if (i == MAX_BRANDS) 470a7cddfedSJake Burkholder return (ENOEXEC); 471e1743d02SSøren Schmidt 472a7cddfedSJake Burkholder return (0); 473e1743d02SSøren Schmidt } 474e1743d02SSøren Schmidt 475e1743d02SSøren Schmidt static int 4763ebc1248SPeter Wemm __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 477ff6f03c7SAlan Cox vm_offset_t start, vm_offset_t end, vm_prot_t prot) 4783ebc1248SPeter Wemm { 479da61b9a6SAlan Cox struct sf_buf *sf; 480da61b9a6SAlan Cox int error; 4813ebc1248SPeter Wemm vm_offset_t off; 4823ebc1248SPeter Wemm 4833ebc1248SPeter Wemm /* 4843ebc1248SPeter Wemm * Create the page if it doesn't exist yet. Ignore errors. 4853ebc1248SPeter Wemm */ 486aaadc41fSKonstantin Belousov vm_map_fixed(map, NULL, 0, trunc_page(start), round_page(end) - 487aaadc41fSKonstantin Belousov trunc_page(start), VM_PROT_ALL, VM_PROT_ALL, MAP_CHECK_EXCL); 4883ebc1248SPeter Wemm 4893ebc1248SPeter Wemm /* 4903ebc1248SPeter Wemm * Find the page from the underlying object. 4913ebc1248SPeter Wemm */ 49228e8da65SAlan Cox if (object != NULL) { 493da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset); 494da61b9a6SAlan Cox if (sf == NULL) 495da61b9a6SAlan Cox return (KERN_FAILURE); 4963ebc1248SPeter Wemm off = offset - trunc_page(offset); 497da61b9a6SAlan Cox error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start, 498ca0387efSJake Burkholder end - start); 499be996836SAttilio Rao vm_imgact_unmap_page(sf); 500fe0a8a39SKonstantin Belousov if (error != 0) 501a7cddfedSJake Burkholder return (KERN_FAILURE); 5023ebc1248SPeter Wemm } 5033ebc1248SPeter Wemm 504a7cddfedSJake Burkholder return (KERN_SUCCESS); 5053ebc1248SPeter Wemm } 5063ebc1248SPeter Wemm 5073ebc1248SPeter Wemm static int 508e3d8f8feSKonstantin Belousov __elfN(map_insert)(struct image_params *imgp, vm_map_t map, vm_object_t object, 509e3d8f8feSKonstantin Belousov vm_ooffset_t offset, vm_offset_t start, vm_offset_t end, vm_prot_t prot, 510e3d8f8feSKonstantin Belousov int cow) 5113ebc1248SPeter Wemm { 512da61b9a6SAlan Cox struct sf_buf *sf; 513da61b9a6SAlan Cox vm_offset_t off; 514a063facbSMarcel Moolenaar vm_size_t sz; 515e3d8f8feSKonstantin Belousov int error, locked, rv; 5163ebc1248SPeter Wemm 5173ebc1248SPeter Wemm if (start != trunc_page(start)) { 51881f223caSJake Burkholder rv = __elfN(map_partial)(map, object, offset, start, 519ff6f03c7SAlan Cox round_page(start), prot); 52028e8da65SAlan Cox if (rv != KERN_SUCCESS) 521a7cddfedSJake Burkholder return (rv); 5223ebc1248SPeter Wemm offset += round_page(start) - start; 5233ebc1248SPeter Wemm start = round_page(start); 5243ebc1248SPeter Wemm } 5253ebc1248SPeter Wemm if (end != round_page(end)) { 52681f223caSJake Burkholder rv = __elfN(map_partial)(map, object, offset + 527ff6f03c7SAlan Cox trunc_page(end) - start, trunc_page(end), end, prot); 52828e8da65SAlan Cox if (rv != KERN_SUCCESS) 529a7cddfedSJake Burkholder return (rv); 5303ebc1248SPeter Wemm end = trunc_page(end); 5313ebc1248SPeter Wemm } 532e383e820SAlan Cox if (start >= end) 533e383e820SAlan Cox return (KERN_SUCCESS); 534e383e820SAlan Cox if ((offset & PAGE_MASK) != 0) { 5353ebc1248SPeter Wemm /* 536e383e820SAlan Cox * The mapping is not page aligned. This means that we have 537e383e820SAlan Cox * to copy the data. 5383ebc1248SPeter Wemm */ 539aaadc41fSKonstantin Belousov rv = vm_map_fixed(map, NULL, 0, start, end - start, 540aaadc41fSKonstantin Belousov prot | VM_PROT_WRITE, VM_PROT_ALL, MAP_CHECK_EXCL); 5415420f76bSKonstantin Belousov if (rv != KERN_SUCCESS) 542a7cddfedSJake Burkholder return (rv); 543da61b9a6SAlan Cox if (object == NULL) 544da61b9a6SAlan Cox return (KERN_SUCCESS); 545da61b9a6SAlan Cox for (; start < end; start += sz) { 546da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset); 547da61b9a6SAlan Cox if (sf == NULL) 548da61b9a6SAlan Cox return (KERN_FAILURE); 5493ebc1248SPeter Wemm off = offset - trunc_page(offset); 5503ebc1248SPeter Wemm sz = end - start; 551da61b9a6SAlan Cox if (sz > PAGE_SIZE - off) 552da61b9a6SAlan Cox sz = PAGE_SIZE - off; 553da61b9a6SAlan Cox error = copyout((caddr_t)sf_buf_kva(sf) + off, 5543ebc1248SPeter Wemm (caddr_t)start, sz); 555be996836SAttilio Rao vm_imgact_unmap_page(sf); 5565420f76bSKonstantin Belousov if (error != 0) 557a7cddfedSJake Burkholder return (KERN_FAILURE); 558da61b9a6SAlan Cox offset += sz; 5593ebc1248SPeter Wemm } 5603ebc1248SPeter Wemm } else { 561e5e6093bSAlan Cox vm_object_reference(object); 562e383e820SAlan Cox rv = vm_map_fixed(map, object, offset, start, end - start, 56378022527SKonstantin Belousov prot, VM_PROT_ALL, cow | MAP_CHECK_EXCL | 56478022527SKonstantin Belousov (object != NULL ? MAP_VN_EXEC : 0)); 565e3d8f8feSKonstantin Belousov if (rv != KERN_SUCCESS) { 566e3d8f8feSKonstantin Belousov locked = VOP_ISLOCKED(imgp->vp); 567b249ce48SMateusz Guzik VOP_UNLOCK(imgp->vp); 568e5e6093bSAlan Cox vm_object_deallocate(object); 569e3d8f8feSKonstantin Belousov vn_lock(imgp->vp, locked | LK_RETRY); 570a7cddfedSJake Burkholder return (rv); 57178022527SKonstantin Belousov } else if (object != NULL) { 57278022527SKonstantin Belousov MPASS(imgp->vp->v_object == object); 57378022527SKonstantin Belousov VOP_SET_TEXT_CHECKED(imgp->vp); 5743ebc1248SPeter Wemm } 5753ebc1248SPeter Wemm } 576e383e820SAlan Cox return (KERN_SUCCESS); 577e383e820SAlan Cox } 5783ebc1248SPeter Wemm 5793ebc1248SPeter Wemm static int 5800bbee4cdSKonstantin Belousov __elfN(load_section)(struct image_params *imgp, vm_ooffset_t offset, 5811699546dSEdward Tomasz Napierala caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot) 582e1743d02SSøren Schmidt { 583da61b9a6SAlan Cox struct sf_buf *sf; 584e1743d02SSøren Schmidt size_t map_len; 585292177e6SAlan Cox vm_map_t map; 586292177e6SAlan Cox vm_object_t object; 587e020a35fSMark Johnston vm_offset_t map_addr; 588fa7dd9c5SMatthew Dillon int error, rv, cow; 589e1743d02SSøren Schmidt size_t copy_len; 5900bbee4cdSKonstantin Belousov vm_ooffset_t file_addr; 59152c24af7SPeter Wemm 59225ead034SBrian Feldman /* 59325ead034SBrian Feldman * It's necessary to fail if the filsz + offset taken from the 59425ead034SBrian Feldman * header is greater than the actual file pager object's size. 59525ead034SBrian Feldman * If we were to allow this, then the vm_map_find() below would 59625ead034SBrian Feldman * walk right off the end of the file object and into the ether. 59725ead034SBrian Feldman * 59825ead034SBrian Feldman * While I'm here, might as well check for something else that 59925ead034SBrian Feldman * is invalid: filsz cannot be greater than memsz. 60025ead034SBrian Feldman */ 6019bcf2f2dSKonstantin Belousov if ((filsz != 0 && (off_t)filsz + offset > imgp->attr->va_size) || 6029bcf2f2dSKonstantin Belousov filsz > memsz) { 60325ead034SBrian Feldman uprintf("elf_load_section: truncated ELF file\n"); 60425ead034SBrian Feldman return (ENOEXEC); 60525ead034SBrian Feldman } 60625ead034SBrian Feldman 607292177e6SAlan Cox object = imgp->object; 608292177e6SAlan Cox map = &imgp->proc->p_vmspace->vm_map; 609545517f1SEdward Tomasz Napierala map_addr = trunc_page((vm_offset_t)vmaddr); 610545517f1SEdward Tomasz Napierala file_addr = trunc_page(offset); 611e1743d02SSøren Schmidt 612e1743d02SSøren Schmidt /* 61352c24af7SPeter Wemm * We have two choices. We can either clear the data in the last page 61452c24af7SPeter Wemm * of an oversized mapping, or we can start the anon mapping a page 61552c24af7SPeter Wemm * early and copy the initialized data into that first page. We 61628e8da65SAlan Cox * choose the second. 61752c24af7SPeter Wemm */ 6189bcf2f2dSKonstantin Belousov if (filsz == 0) 6199bcf2f2dSKonstantin Belousov map_len = 0; 6209bcf2f2dSKonstantin Belousov else if (memsz > filsz) 621545517f1SEdward Tomasz Napierala map_len = trunc_page(offset + filsz) - file_addr; 62252c24af7SPeter Wemm else 623545517f1SEdward Tomasz Napierala map_len = round_page(offset + filsz) - file_addr; 62452c24af7SPeter Wemm 62552c24af7SPeter Wemm if (map_len != 0) { 626fa7dd9c5SMatthew Dillon /* cow flags: don't dump readonly sections in core */ 627fa7dd9c5SMatthew Dillon cow = MAP_COPY_ON_WRITE | MAP_PREFAULT | 628fa7dd9c5SMatthew Dillon (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP); 629fa7dd9c5SMatthew Dillon 63078022527SKonstantin Belousov rv = __elfN(map_insert)(imgp, map, object, file_addr, 63178022527SKonstantin Belousov map_addr, map_addr + map_len, prot, cow); 632e5e6093bSAlan Cox if (rv != KERN_SUCCESS) 633a7cddfedSJake Burkholder return (EINVAL); 63452c24af7SPeter Wemm 63552c24af7SPeter Wemm /* we can stop now if we've covered it all */ 636973d67c4SKonstantin Belousov if (memsz == filsz) 637a7cddfedSJake Burkholder return (0); 63852c24af7SPeter Wemm } 63952c24af7SPeter Wemm 64052c24af7SPeter Wemm /* 64152c24af7SPeter Wemm * We have to get the remaining bit of the file into the first part 64252c24af7SPeter Wemm * of the oversized map segment. This is normally because the .data 64352c24af7SPeter Wemm * segment in the file is extended to provide bss. It's a neat idea 64452c24af7SPeter Wemm * to try and save a page, but it's a pain in the behind to implement. 645e1743d02SSøren Schmidt */ 646545517f1SEdward Tomasz Napierala copy_len = filsz == 0 ? 0 : (offset + filsz) - trunc_page(offset + 647545517f1SEdward Tomasz Napierala filsz); 648545517f1SEdward Tomasz Napierala map_addr = trunc_page((vm_offset_t)vmaddr + filsz); 649545517f1SEdward Tomasz Napierala map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr; 650e1743d02SSøren Schmidt 65152c24af7SPeter Wemm /* This had damn well better be true! */ 6528191d577SPeter Wemm if (map_len != 0) { 653e3d8f8feSKonstantin Belousov rv = __elfN(map_insert)(imgp, map, NULL, 0, map_addr, 654c547cbb4SAlan Cox map_addr + map_len, prot, 0); 655973d67c4SKonstantin Belousov if (rv != KERN_SUCCESS) 656a7cddfedSJake Burkholder return (EINVAL); 6578191d577SPeter Wemm } 658e1743d02SSøren Schmidt 65952c24af7SPeter Wemm if (copy_len != 0) { 660da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset + filsz); 661da61b9a6SAlan Cox if (sf == NULL) 662da61b9a6SAlan Cox return (EIO); 663e1743d02SSøren Schmidt 66452c24af7SPeter Wemm /* send the page fragment to user space */ 665e020a35fSMark Johnston error = copyout((caddr_t)sf_buf_kva(sf), (caddr_t)map_addr, 666e020a35fSMark Johnston copy_len); 667be996836SAttilio Rao vm_imgact_unmap_page(sf); 668973d67c4SKonstantin Belousov if (error != 0) 66952c24af7SPeter Wemm return (error); 67052c24af7SPeter Wemm } 671e1743d02SSøren Schmidt 672e1743d02SSøren Schmidt /* 673c547cbb4SAlan Cox * Remove write access to the page if it was only granted by map_insert 674c547cbb4SAlan Cox * to allow copyout. 675e1743d02SSøren Schmidt */ 676c547cbb4SAlan Cox if ((prot & VM_PROT_WRITE) == 0) 677292177e6SAlan Cox vm_map_protect(map, trunc_page(map_addr), round_page(map_addr + 678292177e6SAlan Cox map_len), prot, FALSE); 6798191d577SPeter Wemm 680ff6f03c7SAlan Cox return (0); 681e1743d02SSøren Schmidt } 682e1743d02SSøren Schmidt 6839bcd7482SEdward Tomasz Napierala static int 6849bcd7482SEdward Tomasz Napierala __elfN(load_sections)(struct image_params *imgp, const Elf_Ehdr *hdr, 6859bcd7482SEdward Tomasz Napierala const Elf_Phdr *phdr, u_long rbase, u_long *base_addrp) 6869bcd7482SEdward Tomasz Napierala { 6879bcd7482SEdward Tomasz Napierala vm_prot_t prot; 6889bcd7482SEdward Tomasz Napierala u_long base_addr; 6899bcd7482SEdward Tomasz Napierala bool first; 6909bcd7482SEdward Tomasz Napierala int error, i; 6919bcd7482SEdward Tomasz Napierala 692b65ca345SEdward Tomasz Napierala ASSERT_VOP_LOCKED(imgp->vp, __func__); 693b65ca345SEdward Tomasz Napierala 6949bcd7482SEdward Tomasz Napierala base_addr = 0; 6959bcd7482SEdward Tomasz Napierala first = true; 6969bcd7482SEdward Tomasz Napierala 6979bcd7482SEdward Tomasz Napierala for (i = 0; i < hdr->e_phnum; i++) { 6989bcd7482SEdward Tomasz Napierala if (phdr[i].p_type != PT_LOAD || phdr[i].p_memsz == 0) 6999bcd7482SEdward Tomasz Napierala continue; 7009bcd7482SEdward Tomasz Napierala 7019bcd7482SEdward Tomasz Napierala /* Loadable segment */ 7029bcd7482SEdward Tomasz Napierala prot = __elfN(trans_prot)(phdr[i].p_flags); 7039bcd7482SEdward Tomasz Napierala error = __elfN(load_section)(imgp, phdr[i].p_offset, 7049bcd7482SEdward Tomasz Napierala (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase, 7059bcd7482SEdward Tomasz Napierala phdr[i].p_memsz, phdr[i].p_filesz, prot); 7069bcd7482SEdward Tomasz Napierala if (error != 0) 7079bcd7482SEdward Tomasz Napierala return (error); 7089bcd7482SEdward Tomasz Napierala 7099bcd7482SEdward Tomasz Napierala /* 7109bcd7482SEdward Tomasz Napierala * Establish the base address if this is the first segment. 7119bcd7482SEdward Tomasz Napierala */ 7129bcd7482SEdward Tomasz Napierala if (first) { 7139bcd7482SEdward Tomasz Napierala base_addr = trunc_page(phdr[i].p_vaddr + rbase); 7149bcd7482SEdward Tomasz Napierala first = false; 7159bcd7482SEdward Tomasz Napierala } 7169bcd7482SEdward Tomasz Napierala } 7179bcd7482SEdward Tomasz Napierala 7189bcd7482SEdward Tomasz Napierala if (base_addrp != NULL) 7199bcd7482SEdward Tomasz Napierala *base_addrp = base_addr; 7209bcd7482SEdward Tomasz Napierala 7219bcd7482SEdward Tomasz Napierala return (0); 7229bcd7482SEdward Tomasz Napierala } 7239bcd7482SEdward Tomasz Napierala 724c33fe779SJohn Polstra /* 725c33fe779SJohn Polstra * Load the file "file" into memory. It may be either a shared object 726c33fe779SJohn Polstra * or an executable. 727c33fe779SJohn Polstra * 728c33fe779SJohn Polstra * The "addr" reference parameter is in/out. On entry, it specifies 729c33fe779SJohn Polstra * the address where a shared object should be loaded. If the file is 730c33fe779SJohn Polstra * an executable, this value is ignored. On exit, "addr" specifies 731c33fe779SJohn Polstra * where the file was actually loaded. 732c33fe779SJohn Polstra * 733c33fe779SJohn Polstra * The "entry" reference parameter is out only. On exit, it specifies 734c33fe779SJohn Polstra * the entry point for the loaded file. 735c33fe779SJohn Polstra */ 736e1743d02SSøren Schmidt static int 7373ebc1248SPeter Wemm __elfN(load_file)(struct proc *p, const char *file, u_long *addr, 7381699546dSEdward Tomasz Napierala u_long *entry) 739e1743d02SSøren Schmidt { 740911c2be0SMark Peek struct { 741911c2be0SMark Peek struct nameidata nd; 742911c2be0SMark Peek struct vattr attr; 743911c2be0SMark Peek struct image_params image_params; 744911c2be0SMark Peek } *tempdata; 745d254af07SMatthew Dillon const Elf_Ehdr *hdr = NULL; 746d254af07SMatthew Dillon const Elf_Phdr *phdr = NULL; 747911c2be0SMark Peek struct nameidata *nd; 748911c2be0SMark Peek struct vattr *attr; 749911c2be0SMark Peek struct image_params *imgp; 75078022527SKonstantin Belousov u_long rbase; 751c33fe779SJohn Polstra u_long base_addr = 0; 7529bcd7482SEdward Tomasz Napierala int error; 753e1743d02SSøren Schmidt 75412bc222eSJonathan Anderson #ifdef CAPABILITY_MODE 75512bc222eSJonathan Anderson /* 75612bc222eSJonathan Anderson * XXXJA: This check can go away once we are sufficiently confident 75712bc222eSJonathan Anderson * that the checks in namei() are correct. 75812bc222eSJonathan Anderson */ 75912bc222eSJonathan Anderson if (IN_CAPABILITY_MODE(curthread)) 76012bc222eSJonathan Anderson return (ECAPMODE); 76112bc222eSJonathan Anderson #endif 76212bc222eSJonathan Anderson 7631073d17eSKonstantin Belousov tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK | M_ZERO); 764911c2be0SMark Peek nd = &tempdata->nd; 765911c2be0SMark Peek attr = &tempdata->attr; 766911c2be0SMark Peek imgp = &tempdata->image_params; 767911c2be0SMark Peek 768c8a79999SPeter Wemm /* 769c8a79999SPeter Wemm * Initialize part of the common data 770c8a79999SPeter Wemm */ 771c8a79999SPeter Wemm imgp->proc = p; 772911c2be0SMark Peek imgp->attr = attr; 773c8a79999SPeter Wemm 774f422bc30SJohn Baldwin NDINIT(nd, LOOKUP, ISOPEN | FOLLOW | LOCKSHARED | LOCKLEAF, 775f422bc30SJohn Baldwin UIO_SYSSPACE, file, curthread); 776911c2be0SMark Peek if ((error = namei(nd)) != 0) { 777911c2be0SMark Peek nd->ni_vp = NULL; 778e1743d02SSøren Schmidt goto fail; 779e1743d02SSøren Schmidt } 780911c2be0SMark Peek NDFREE(nd, NDF_ONLY_PNBUF); 781911c2be0SMark Peek imgp->vp = nd->ni_vp; 782c8a79999SPeter Wemm 783e1743d02SSøren Schmidt /* 784e1743d02SSøren Schmidt * Check permissions, modes, uid, etc on the file, and "open" it. 785e1743d02SSøren Schmidt */ 786c8a79999SPeter Wemm error = exec_check_permissions(imgp); 787373d1a3fSAlan Cox if (error) 788c8a79999SPeter Wemm goto fail; 789e1743d02SSøren Schmidt 790c8a79999SPeter Wemm error = exec_map_first_page(imgp); 791373d1a3fSAlan Cox if (error) 792373d1a3fSAlan Cox goto fail; 793373d1a3fSAlan Cox 7948516dd18SPoul-Henning Kamp imgp->object = nd->ni_vp->v_object; 795e1743d02SSøren Schmidt 796d254af07SMatthew Dillon hdr = (const Elf_Ehdr *)imgp->image_header; 7973ebc1248SPeter Wemm if ((error = __elfN(check_header)(hdr)) != 0) 798e1743d02SSøren Schmidt goto fail; 799c33fe779SJohn Polstra if (hdr->e_type == ET_DYN) 800c33fe779SJohn Polstra rbase = *addr; 801c33fe779SJohn Polstra else if (hdr->e_type == ET_EXEC) 802c33fe779SJohn Polstra rbase = 0; 803c33fe779SJohn Polstra else { 804c33fe779SJohn Polstra error = ENOEXEC; 805c33fe779SJohn Polstra goto fail; 806c33fe779SJohn Polstra } 807e1743d02SSøren Schmidt 808c8a79999SPeter Wemm /* Only support headers that fit within first page for now */ 80952c24af7SPeter Wemm if ((hdr->e_phoff > PAGE_SIZE) || 810d19d5bf4STijl Coosemans (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) { 811c8a79999SPeter Wemm error = ENOEXEC; 812e1743d02SSøren Schmidt goto fail; 813c8a79999SPeter Wemm } 814c8a79999SPeter Wemm 815d254af07SMatthew Dillon phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 81693d1c728SKonstantin Belousov if (!aligned(phdr, Elf_Addr)) { 81793d1c728SKonstantin Belousov error = ENOEXEC; 81893d1c728SKonstantin Belousov goto fail; 81993d1c728SKonstantin Belousov } 820e1743d02SSøren Schmidt 8219bcd7482SEdward Tomasz Napierala error = __elfN(load_sections)(imgp, hdr, phdr, rbase, &base_addr); 822292177e6SAlan Cox if (error != 0) 823e1743d02SSøren Schmidt goto fail; 8249bcd7482SEdward Tomasz Napierala 825c33fe779SJohn Polstra *addr = base_addr; 826c33fe779SJohn Polstra *entry = (unsigned long)hdr->e_entry + rbase; 827e1743d02SSøren Schmidt 828e1743d02SSøren Schmidt fail: 829c8a79999SPeter Wemm if (imgp->firstpage) 830c8a79999SPeter Wemm exec_unmap_first_page(imgp); 8310b2ed1aeSJeff Roberson 83278022527SKonstantin Belousov if (nd->ni_vp) { 83378022527SKonstantin Belousov if (imgp->textset) 83478022527SKonstantin Belousov VOP_UNSET_TEXT_CHECKED(nd->ni_vp); 835373d1a3fSAlan Cox vput(nd->ni_vp); 83678022527SKonstantin Belousov } 837911c2be0SMark Peek free(tempdata, M_TEMP); 838e1743d02SSøren Schmidt 839a7cddfedSJake Burkholder return (error); 840e1743d02SSøren Schmidt } 841e1743d02SSøren Schmidt 842fa50a355SKonstantin Belousov static u_long 843fa50a355SKonstantin Belousov __CONCAT(rnd_, __elfN(base))(vm_map_t map __unused, u_long minv, u_long maxv, 844fa50a355SKonstantin Belousov u_int align) 845fa50a355SKonstantin Belousov { 846fa50a355SKonstantin Belousov u_long rbase, res; 847fa50a355SKonstantin Belousov 848fa50a355SKonstantin Belousov MPASS(vm_map_min(map) <= minv); 849fa50a355SKonstantin Belousov MPASS(maxv <= vm_map_max(map)); 850fa50a355SKonstantin Belousov MPASS(minv < maxv); 851fa50a355SKonstantin Belousov MPASS(minv + align < maxv); 852fa50a355SKonstantin Belousov arc4rand(&rbase, sizeof(rbase), 0); 853fa50a355SKonstantin Belousov res = roundup(minv, (u_long)align) + rbase % (maxv - minv); 854fa50a355SKonstantin Belousov res &= ~((u_long)align - 1); 855fa50a355SKonstantin Belousov if (res >= maxv) 856fa50a355SKonstantin Belousov res -= align; 857fa50a355SKonstantin Belousov KASSERT(res >= minv, 858fa50a355SKonstantin Belousov ("res %#lx < minv %#lx, maxv %#lx rbase %#lx", 859fa50a355SKonstantin Belousov res, minv, maxv, rbase)); 860fa50a355SKonstantin Belousov KASSERT(res < maxv, 861fa50a355SKonstantin Belousov ("res %#lx > maxv %#lx, minv %#lx rbase %#lx", 862fa50a355SKonstantin Belousov res, maxv, minv, rbase)); 863fa50a355SKonstantin Belousov return (res); 864fa50a355SKonstantin Belousov } 865fa50a355SKonstantin Belousov 86620e1174aSEdward Tomasz Napierala static int 86720e1174aSEdward Tomasz Napierala __elfN(enforce_limits)(struct image_params *imgp, const Elf_Ehdr *hdr, 86820e1174aSEdward Tomasz Napierala const Elf_Phdr *phdr, u_long et_dyn_addr) 86920e1174aSEdward Tomasz Napierala { 87020e1174aSEdward Tomasz Napierala struct vmspace *vmspace; 87120e1174aSEdward Tomasz Napierala const char *err_str; 87220e1174aSEdward Tomasz Napierala u_long text_size, data_size, total_size, text_addr, data_addr; 87320e1174aSEdward Tomasz Napierala u_long seg_size, seg_addr; 87420e1174aSEdward Tomasz Napierala int i; 87520e1174aSEdward Tomasz Napierala 87620e1174aSEdward Tomasz Napierala err_str = NULL; 87720e1174aSEdward Tomasz Napierala text_size = data_size = total_size = text_addr = data_addr = 0; 87820e1174aSEdward Tomasz Napierala 87920e1174aSEdward Tomasz Napierala for (i = 0; i < hdr->e_phnum; i++) { 88020e1174aSEdward Tomasz Napierala if (phdr[i].p_type != PT_LOAD || phdr[i].p_memsz == 0) 88120e1174aSEdward Tomasz Napierala continue; 88220e1174aSEdward Tomasz Napierala 88320e1174aSEdward Tomasz Napierala seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr); 88420e1174aSEdward Tomasz Napierala seg_size = round_page(phdr[i].p_memsz + 88520e1174aSEdward Tomasz Napierala phdr[i].p_vaddr + et_dyn_addr - seg_addr); 88620e1174aSEdward Tomasz Napierala 88720e1174aSEdward Tomasz Napierala /* 88820e1174aSEdward Tomasz Napierala * Make the largest executable segment the official 88920e1174aSEdward Tomasz Napierala * text segment and all others data. 89020e1174aSEdward Tomasz Napierala * 89120e1174aSEdward Tomasz Napierala * Note that obreak() assumes that data_addr + data_size == end 89220e1174aSEdward Tomasz Napierala * of data load area, and the ELF file format expects segments 89320e1174aSEdward Tomasz Napierala * to be sorted by address. If multiple data segments exist, 89420e1174aSEdward Tomasz Napierala * the last one will be used. 89520e1174aSEdward Tomasz Napierala */ 89620e1174aSEdward Tomasz Napierala 89720e1174aSEdward Tomasz Napierala if ((phdr[i].p_flags & PF_X) != 0 && text_size < seg_size) { 89820e1174aSEdward Tomasz Napierala text_size = seg_size; 89920e1174aSEdward Tomasz Napierala text_addr = seg_addr; 90020e1174aSEdward Tomasz Napierala } else { 90120e1174aSEdward Tomasz Napierala data_size = seg_size; 90220e1174aSEdward Tomasz Napierala data_addr = seg_addr; 90320e1174aSEdward Tomasz Napierala } 90420e1174aSEdward Tomasz Napierala total_size += seg_size; 90520e1174aSEdward Tomasz Napierala } 90620e1174aSEdward Tomasz Napierala 90720e1174aSEdward Tomasz Napierala if (data_addr == 0 && data_size == 0) { 90820e1174aSEdward Tomasz Napierala data_addr = text_addr; 90920e1174aSEdward Tomasz Napierala data_size = text_size; 91020e1174aSEdward Tomasz Napierala } 91120e1174aSEdward Tomasz Napierala 91220e1174aSEdward Tomasz Napierala /* 91320e1174aSEdward Tomasz Napierala * Check limits. It should be safe to check the 91420e1174aSEdward Tomasz Napierala * limits after loading the segments since we do 91520e1174aSEdward Tomasz Napierala * not actually fault in all the segments pages. 91620e1174aSEdward Tomasz Napierala */ 91720e1174aSEdward Tomasz Napierala PROC_LOCK(imgp->proc); 91820e1174aSEdward Tomasz Napierala if (data_size > lim_cur_proc(imgp->proc, RLIMIT_DATA)) 91920e1174aSEdward Tomasz Napierala err_str = "Data segment size exceeds process limit"; 92020e1174aSEdward Tomasz Napierala else if (text_size > maxtsiz) 92120e1174aSEdward Tomasz Napierala err_str = "Text segment size exceeds system limit"; 92220e1174aSEdward Tomasz Napierala else if (total_size > lim_cur_proc(imgp->proc, RLIMIT_VMEM)) 92320e1174aSEdward Tomasz Napierala err_str = "Total segment size exceeds process limit"; 92420e1174aSEdward Tomasz Napierala else if (racct_set(imgp->proc, RACCT_DATA, data_size) != 0) 92520e1174aSEdward Tomasz Napierala err_str = "Data segment size exceeds resource limit"; 92620e1174aSEdward Tomasz Napierala else if (racct_set(imgp->proc, RACCT_VMEM, total_size) != 0) 92720e1174aSEdward Tomasz Napierala err_str = "Total segment size exceeds resource limit"; 92820e1174aSEdward Tomasz Napierala PROC_UNLOCK(imgp->proc); 92920e1174aSEdward Tomasz Napierala if (err_str != NULL) { 93020e1174aSEdward Tomasz Napierala uprintf("%s\n", err_str); 93120e1174aSEdward Tomasz Napierala return (ENOMEM); 93220e1174aSEdward Tomasz Napierala } 93320e1174aSEdward Tomasz Napierala 93420e1174aSEdward Tomasz Napierala vmspace = imgp->proc->p_vmspace; 93520e1174aSEdward Tomasz Napierala vmspace->vm_tsize = text_size >> PAGE_SHIFT; 93620e1174aSEdward Tomasz Napierala vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr; 93720e1174aSEdward Tomasz Napierala vmspace->vm_dsize = data_size >> PAGE_SHIFT; 93820e1174aSEdward Tomasz Napierala vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr; 93920e1174aSEdward Tomasz Napierala 94020e1174aSEdward Tomasz Napierala return (0); 94120e1174aSEdward Tomasz Napierala } 94220e1174aSEdward Tomasz Napierala 94309c78d53SEdward Tomasz Napierala static int 94409c78d53SEdward Tomasz Napierala __elfN(get_interp)(struct image_params *imgp, const Elf_Phdr *phdr, 94509c78d53SEdward Tomasz Napierala char **interpp, bool *free_interpp) 94609c78d53SEdward Tomasz Napierala { 94709c78d53SEdward Tomasz Napierala struct thread *td; 94809c78d53SEdward Tomasz Napierala char *interp; 94909c78d53SEdward Tomasz Napierala int error, interp_name_len; 95009c78d53SEdward Tomasz Napierala 95109c78d53SEdward Tomasz Napierala KASSERT(phdr->p_type == PT_INTERP, 95209c78d53SEdward Tomasz Napierala ("%s: p_type %u != PT_INTERP", __func__, phdr->p_type)); 953b65ca345SEdward Tomasz Napierala ASSERT_VOP_LOCKED(imgp->vp, __func__); 95409c78d53SEdward Tomasz Napierala 95509c78d53SEdward Tomasz Napierala td = curthread; 95609c78d53SEdward Tomasz Napierala 95709c78d53SEdward Tomasz Napierala /* Path to interpreter */ 95809c78d53SEdward Tomasz Napierala if (phdr->p_filesz < 2 || phdr->p_filesz > MAXPATHLEN) { 95909c78d53SEdward Tomasz Napierala uprintf("Invalid PT_INTERP\n"); 96009c78d53SEdward Tomasz Napierala return (ENOEXEC); 96109c78d53SEdward Tomasz Napierala } 96209c78d53SEdward Tomasz Napierala 96309c78d53SEdward Tomasz Napierala interp_name_len = phdr->p_filesz; 96409c78d53SEdward Tomasz Napierala if (phdr->p_offset > PAGE_SIZE || 96509c78d53SEdward Tomasz Napierala interp_name_len > PAGE_SIZE - phdr->p_offset) { 9660ddfdc60SKonstantin Belousov /* 967f1f81d3bSKonstantin Belousov * The vnode lock might be needed by the pagedaemon to 9680ddfdc60SKonstantin Belousov * clean pages owned by the vnode. Do not allow sleep 9690ddfdc60SKonstantin Belousov * waiting for memory with the vnode locked, instead 9700ddfdc60SKonstantin Belousov * try non-sleepable allocation first, and if it 9710ddfdc60SKonstantin Belousov * fails, go to the slow path were we drop the lock 972f1f81d3bSKonstantin Belousov * and do M_WAITOK. A text reference prevents 973f1f81d3bSKonstantin Belousov * modifications to the vnode content. 9740ddfdc60SKonstantin Belousov */ 9752d6b8546SKonstantin Belousov interp = malloc(interp_name_len + 1, M_TEMP, M_NOWAIT); 9762d6b8546SKonstantin Belousov if (interp == NULL) { 977b249ce48SMateusz Guzik VOP_UNLOCK(imgp->vp); 97809c78d53SEdward Tomasz Napierala interp = malloc(interp_name_len + 1, M_TEMP, M_WAITOK); 97978022527SKonstantin Belousov vn_lock(imgp->vp, LK_SHARED | LK_RETRY); 9802d6b8546SKonstantin Belousov } 9810ddfdc60SKonstantin Belousov 98209c78d53SEdward Tomasz Napierala error = vn_rdwr(UIO_READ, imgp->vp, interp, 98309c78d53SEdward Tomasz Napierala interp_name_len, phdr->p_offset, 98409c78d53SEdward Tomasz Napierala UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, 98509c78d53SEdward Tomasz Napierala NOCRED, NULL, td); 98609c78d53SEdward Tomasz Napierala if (error != 0) { 98709c78d53SEdward Tomasz Napierala free(interp, M_TEMP); 98809c78d53SEdward Tomasz Napierala uprintf("i/o error PT_INTERP %d\n", error); 98909c78d53SEdward Tomasz Napierala return (error); 99009c78d53SEdward Tomasz Napierala } 99109c78d53SEdward Tomasz Napierala interp[interp_name_len] = '\0'; 99209c78d53SEdward Tomasz Napierala 99309c78d53SEdward Tomasz Napierala *interpp = interp; 99409c78d53SEdward Tomasz Napierala *free_interpp = true; 99509c78d53SEdward Tomasz Napierala return (0); 99609c78d53SEdward Tomasz Napierala } 99709c78d53SEdward Tomasz Napierala 99809c78d53SEdward Tomasz Napierala interp = __DECONST(char *, imgp->image_header) + phdr->p_offset; 99909c78d53SEdward Tomasz Napierala if (interp[interp_name_len - 1] != '\0') { 100009c78d53SEdward Tomasz Napierala uprintf("Invalid PT_INTERP\n"); 100109c78d53SEdward Tomasz Napierala return (ENOEXEC); 100209c78d53SEdward Tomasz Napierala } 100309c78d53SEdward Tomasz Napierala 100409c78d53SEdward Tomasz Napierala *interpp = interp; 100509c78d53SEdward Tomasz Napierala *free_interpp = false; 100609c78d53SEdward Tomasz Napierala return (0); 100709c78d53SEdward Tomasz Napierala } 100809c78d53SEdward Tomasz Napierala 10099274fb35SEdward Tomasz Napierala static int 10109274fb35SEdward Tomasz Napierala __elfN(load_interp)(struct image_params *imgp, const Elf_Brandinfo *brand_info, 10119274fb35SEdward Tomasz Napierala const char *interp, u_long *addr, u_long *entry) 10129274fb35SEdward Tomasz Napierala { 10139274fb35SEdward Tomasz Napierala char *path; 10149274fb35SEdward Tomasz Napierala int error; 10159274fb35SEdward Tomasz Napierala 10169274fb35SEdward Tomasz Napierala if (brand_info->emul_path != NULL && 10179274fb35SEdward Tomasz Napierala brand_info->emul_path[0] != '\0') { 10189274fb35SEdward Tomasz Napierala path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 10199274fb35SEdward Tomasz Napierala snprintf(path, MAXPATHLEN, "%s%s", 10209274fb35SEdward Tomasz Napierala brand_info->emul_path, interp); 10219274fb35SEdward Tomasz Napierala error = __elfN(load_file)(imgp->proc, path, addr, entry); 10229274fb35SEdward Tomasz Napierala free(path, M_TEMP); 10239274fb35SEdward Tomasz Napierala if (error == 0) 10249274fb35SEdward Tomasz Napierala return (0); 10259274fb35SEdward Tomasz Napierala } 10269274fb35SEdward Tomasz Napierala 10279274fb35SEdward Tomasz Napierala if (brand_info->interp_newpath != NULL && 10289274fb35SEdward Tomasz Napierala (brand_info->interp_path == NULL || 10299274fb35SEdward Tomasz Napierala strcmp(interp, brand_info->interp_path) == 0)) { 10309274fb35SEdward Tomasz Napierala error = __elfN(load_file)(imgp->proc, 10319274fb35SEdward Tomasz Napierala brand_info->interp_newpath, addr, entry); 10329274fb35SEdward Tomasz Napierala if (error == 0) 10339274fb35SEdward Tomasz Napierala return (0); 10349274fb35SEdward Tomasz Napierala } 10359274fb35SEdward Tomasz Napierala 10369274fb35SEdward Tomasz Napierala error = __elfN(load_file)(imgp->proc, interp, addr, entry); 10379274fb35SEdward Tomasz Napierala if (error == 0) 10389274fb35SEdward Tomasz Napierala return (0); 10399274fb35SEdward Tomasz Napierala 10409274fb35SEdward Tomasz Napierala uprintf("ELF interpreter %s not found, error %d\n", interp, error); 10419274fb35SEdward Tomasz Napierala return (error); 10429274fb35SEdward Tomasz Napierala } 10439274fb35SEdward Tomasz Napierala 1044fa50a355SKonstantin Belousov /* 1045fa50a355SKonstantin Belousov * Impossible et_dyn_addr initial value indicating that the real base 1046fa50a355SKonstantin Belousov * must be calculated later with some randomization applied. 1047fa50a355SKonstantin Belousov */ 1048fa50a355SKonstantin Belousov #define ET_DYN_ADDR_RAND 1 1049fa50a355SKonstantin Belousov 1050303b270bSEivind Eklund static int 10513ebc1248SPeter Wemm __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp) 1052e1743d02SSøren Schmidt { 10536c775eb6SKonstantin Belousov struct thread *td; 10546c775eb6SKonstantin Belousov const Elf_Ehdr *hdr; 105532c01de2SDmitry Chagin const Elf_Phdr *phdr; 1056e5e6093bSAlan Cox Elf_Auxargs *elf_auxargs; 10575856e12eSJohn Dyson struct vmspace *vmspace; 1058fa50a355SKonstantin Belousov vm_map_t map; 10599274fb35SEdward Tomasz Napierala char *interp; 1060d1dbc694SJohn Polstra Elf_Brandinfo *brand_info; 10615fe3ed62SJake Burkholder struct sysentvec *sv; 106220e1174aSEdward Tomasz Napierala u_long addr, baddr, et_dyn_addr, entry, proghdr; 1063fa50a355SKonstantin Belousov u_long maxalign, mapsz, maxv, maxv1; 1064cefb93f2SKonstantin Belousov uint32_t fctl0; 10656c775eb6SKonstantin Belousov int32_t osrel; 106609c78d53SEdward Tomasz Napierala bool free_interp; 10679274fb35SEdward Tomasz Napierala int error, i, n; 10686c775eb6SKonstantin Belousov 10696c775eb6SKonstantin Belousov hdr = (const Elf_Ehdr *)imgp->image_header; 1070e1743d02SSøren Schmidt 1071e1743d02SSøren Schmidt /* 1072e1743d02SSøren Schmidt * Do we have a valid ELF header ? 1073900b28f9SMaxim Sobolev * 1074900b28f9SMaxim Sobolev * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later 1075900b28f9SMaxim Sobolev * if particular brand doesn't support it. 1076e1743d02SSøren Schmidt */ 1077900b28f9SMaxim Sobolev if (__elfN(check_header)(hdr) != 0 || 1078900b28f9SMaxim Sobolev (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)) 1079a7cddfedSJake Burkholder return (-1); 1080e1743d02SSøren Schmidt 1081e1743d02SSøren Schmidt /* 1082e1743d02SSøren Schmidt * From here on down, we return an errno, not -1, as we've 1083e1743d02SSøren Schmidt * detected an ELF file. 1084e1743d02SSøren Schmidt */ 1085e1743d02SSøren Schmidt 1086e1743d02SSøren Schmidt if ((hdr->e_phoff > PAGE_SIZE) || 1087d19d5bf4STijl Coosemans (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) { 1088c8a79999SPeter Wemm /* Only support headers in first page for now */ 10896b16d664SEd Maste uprintf("Program headers not in the first page\n"); 1090a7cddfedSJake Burkholder return (ENOEXEC); 1091e1743d02SSøren Schmidt } 109252c24af7SPeter Wemm phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 10936b16d664SEd Maste if (!aligned(phdr, Elf_Addr)) { 10946b16d664SEd Maste uprintf("Unaligned program headers\n"); 109593d1c728SKonstantin Belousov return (ENOEXEC); 10966b16d664SEd Maste } 10976c775eb6SKonstantin Belousov 10986c775eb6SKonstantin Belousov n = error = 0; 10997564c4adSKonstantin Belousov baddr = 0; 11006c775eb6SKonstantin Belousov osrel = 0; 1101cefb93f2SKonstantin Belousov fctl0 = 0; 11026c775eb6SKonstantin Belousov entry = proghdr = 0; 11039274fb35SEdward Tomasz Napierala interp = NULL; 110409c78d53SEdward Tomasz Napierala free_interp = false; 11056c775eb6SKonstantin Belousov td = curthread; 1106fa50a355SKonstantin Belousov maxalign = PAGE_SIZE; 1107fa50a355SKonstantin Belousov mapsz = 0; 11086c775eb6SKonstantin Belousov 11095fe3ed62SJake Burkholder for (i = 0; i < hdr->e_phnum; i++) { 1110291c06a1SKonstantin Belousov switch (phdr[i].p_type) { 1111291c06a1SKonstantin Belousov case PT_LOAD: 11127564c4adSKonstantin Belousov if (n == 0) 11137564c4adSKonstantin Belousov baddr = phdr[i].p_vaddr; 1114fa50a355SKonstantin Belousov if (phdr[i].p_align > maxalign) 1115fa50a355SKonstantin Belousov maxalign = phdr[i].p_align; 1116fa50a355SKonstantin Belousov mapsz += phdr[i].p_memsz; 11177564c4adSKonstantin Belousov n++; 11189bcd7482SEdward Tomasz Napierala 11199bcd7482SEdward Tomasz Napierala /* 11209bcd7482SEdward Tomasz Napierala * If this segment contains the program headers, 11219bcd7482SEdward Tomasz Napierala * remember their virtual address for the AT_PHDR 11229bcd7482SEdward Tomasz Napierala * aux entry. Static binaries don't usually include 11239bcd7482SEdward Tomasz Napierala * a PT_PHDR entry. 11249bcd7482SEdward Tomasz Napierala */ 11259bcd7482SEdward Tomasz Napierala if (phdr[i].p_offset == 0 && 11269bcd7482SEdward Tomasz Napierala hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize 11279bcd7482SEdward Tomasz Napierala <= phdr[i].p_filesz) 11289bcd7482SEdward Tomasz Napierala proghdr = phdr[i].p_vaddr + hdr->e_phoff; 1129291c06a1SKonstantin Belousov break; 1130291c06a1SKonstantin Belousov case PT_INTERP: 1131e5e6093bSAlan Cox /* Path to interpreter */ 1132d3ee0a15SJonathan T. Looney if (interp != NULL) { 1133d3ee0a15SJonathan T. Looney uprintf("Multiple PT_INTERP headers\n"); 1134d3ee0a15SJonathan T. Looney error = ENOEXEC; 1135d3ee0a15SJonathan T. Looney goto ret; 1136d3ee0a15SJonathan T. Looney } 113709c78d53SEdward Tomasz Napierala error = __elfN(get_interp)(imgp, &phdr[i], &interp, 113809c78d53SEdward Tomasz Napierala &free_interp); 113909c78d53SEdward Tomasz Napierala if (error != 0) 11406c775eb6SKonstantin Belousov goto ret; 1141291c06a1SKonstantin Belousov break; 1142291c06a1SKonstantin Belousov case PT_GNU_STACK: 1143291c06a1SKonstantin Belousov if (__elfN(nxstack)) 1144291c06a1SKonstantin Belousov imgp->stack_prot = 1145291c06a1SKonstantin Belousov __elfN(trans_prot)(phdr[i].p_flags); 1146316b3843SKonstantin Belousov imgp->stack_sz = phdr[i].p_memsz; 1147291c06a1SKonstantin Belousov break; 11489bcd7482SEdward Tomasz Napierala case PT_PHDR: /* Program header table info */ 11499bcd7482SEdward Tomasz Napierala proghdr = phdr[i].p_vaddr; 11509bcd7482SEdward Tomasz Napierala break; 11513ebc1248SPeter Wemm } 11523ebc1248SPeter Wemm } 11533ebc1248SPeter Wemm 115409c78d53SEdward Tomasz Napierala brand_info = __elfN(get_brandinfo)(imgp, interp, &osrel, &fctl0); 11555fe3ed62SJake Burkholder if (brand_info == NULL) { 11565fe3ed62SJake Burkholder uprintf("ELF binary type \"%u\" not known.\n", 11575fe3ed62SJake Burkholder hdr->e_ident[EI_OSABI]); 11586c775eb6SKonstantin Belousov error = ENOEXEC; 11596c775eb6SKonstantin Belousov goto ret; 11603ebc1248SPeter Wemm } 1161fa50a355SKonstantin Belousov sv = brand_info->sysvec; 116277ebe276SEd Maste et_dyn_addr = 0; 1163ab02d85fSKonstantin Belousov if (hdr->e_type == ET_DYN) { 11646b16d664SEd Maste if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) { 11656b16d664SEd Maste uprintf("Cannot execute shared object\n"); 11666c775eb6SKonstantin Belousov error = ENOEXEC; 11676c775eb6SKonstantin Belousov goto ret; 11686b16d664SEd Maste } 11697564c4adSKonstantin Belousov /* 11707564c4adSKonstantin Belousov * Honour the base load address from the dso if it is 11717564c4adSKonstantin Belousov * non-zero for some reason. 11727564c4adSKonstantin Belousov */ 1173fa50a355SKonstantin Belousov if (baddr == 0) { 1174fa50a355SKonstantin Belousov if ((sv->sv_flags & SV_ASLR) == 0 || 1175fa50a355SKonstantin Belousov (fctl0 & NT_FREEBSD_FCTL_ASLR_DISABLE) != 0) 117695aafd69SKonstantin Belousov et_dyn_addr = __elfN(pie_base); 1177fa50a355SKonstantin Belousov else if ((__elfN(pie_aslr_enabled) && 1178fa50a355SKonstantin Belousov (imgp->proc->p_flag2 & P2_ASLR_DISABLE) == 0) || 1179fa50a355SKonstantin Belousov (imgp->proc->p_flag2 & P2_ASLR_ENABLE) != 0) 1180fa50a355SKonstantin Belousov et_dyn_addr = ET_DYN_ADDR_RAND; 1181fa50a355SKonstantin Belousov else 118295aafd69SKonstantin Belousov et_dyn_addr = __elfN(pie_base); 118377ebe276SEd Maste } 1184fa50a355SKonstantin Belousov } 11853ebc1248SPeter Wemm 118660bb3943SAlan Cox /* 118760bb3943SAlan Cox * Avoid a possible deadlock if the current address space is destroyed 118860bb3943SAlan Cox * and that address space maps the locked vnode. In the common case, 118960bb3943SAlan Cox * the locked vnode's v_usecount is decremented but remains greater 119060bb3943SAlan Cox * than zero. Consequently, the vnode lock is not needed by vrele(). 119160bb3943SAlan Cox * However, in cases where the vnode lock is external, such as nullfs, 119260bb3943SAlan Cox * v_usecount may become zero. 11931dfab802SAlan Cox * 11941dfab802SAlan Cox * The VV_TEXT flag prevents modifications to the executable while 11951dfab802SAlan Cox * the vnode is unlocked. 119660bb3943SAlan Cox */ 1197b249ce48SMateusz Guzik VOP_UNLOCK(imgp->vp); 119860bb3943SAlan Cox 1199fa50a355SKonstantin Belousov /* 1200fa50a355SKonstantin Belousov * Decide whether to enable randomization of user mappings. 1201fa50a355SKonstantin Belousov * First, reset user preferences for the setid binaries. 1202fa50a355SKonstantin Belousov * Then, account for the support of the randomization by the 1203fa50a355SKonstantin Belousov * ABI, by user preferences, and make special treatment for 1204fa50a355SKonstantin Belousov * PIE binaries. 1205fa50a355SKonstantin Belousov */ 1206fa50a355SKonstantin Belousov if (imgp->credential_setid) { 1207fa50a355SKonstantin Belousov PROC_LOCK(imgp->proc); 1208fa50a355SKonstantin Belousov imgp->proc->p_flag2 &= ~(P2_ASLR_ENABLE | P2_ASLR_DISABLE); 1209fa50a355SKonstantin Belousov PROC_UNLOCK(imgp->proc); 1210fa50a355SKonstantin Belousov } 1211fa50a355SKonstantin Belousov if ((sv->sv_flags & SV_ASLR) == 0 || 1212fa50a355SKonstantin Belousov (imgp->proc->p_flag2 & P2_ASLR_DISABLE) != 0 || 1213fa50a355SKonstantin Belousov (fctl0 & NT_FREEBSD_FCTL_ASLR_DISABLE) != 0) { 1214fa50a355SKonstantin Belousov KASSERT(et_dyn_addr != ET_DYN_ADDR_RAND, 1215fa50a355SKonstantin Belousov ("et_dyn_addr == RAND and !ASLR")); 1216fa50a355SKonstantin Belousov } else if ((imgp->proc->p_flag2 & P2_ASLR_ENABLE) != 0 || 1217fa50a355SKonstantin Belousov (__elfN(aslr_enabled) && hdr->e_type == ET_EXEC) || 1218fa50a355SKonstantin Belousov et_dyn_addr == ET_DYN_ADDR_RAND) { 1219fa50a355SKonstantin Belousov imgp->map_flags |= MAP_ASLR; 1220fa50a355SKonstantin Belousov /* 1221fa50a355SKonstantin Belousov * If user does not care about sbrk, utilize the bss 1222fa50a355SKonstantin Belousov * grow region for mappings as well. We can select 1223fa50a355SKonstantin Belousov * the base for the image anywere and still not suffer 1224fa50a355SKonstantin Belousov * from the fragmentation. 1225fa50a355SKonstantin Belousov */ 1226fa50a355SKonstantin Belousov if (!__elfN(aslr_honor_sbrk) || 1227fa50a355SKonstantin Belousov (imgp->proc->p_flag2 & P2_ASLR_IGNSTART) != 0) 1228fa50a355SKonstantin Belousov imgp->map_flags |= MAP_ASLR_IGNSTART; 1229fa50a355SKonstantin Belousov } 1230fa50a355SKonstantin Belousov 123189b57fcfSKonstantin Belousov error = exec_new_vmspace(imgp, sv); 1232fa50a355SKonstantin Belousov vmspace = imgp->proc->p_vmspace; 1233fa50a355SKonstantin Belousov map = &vmspace->vm_map; 1234fa50a355SKonstantin Belousov 123519059a13SJohn Baldwin imgp->proc->p_sysent = sv; 1236e1743d02SSøren Schmidt 1237fa50a355SKonstantin Belousov maxv = vm_map_max(map) - lim_max(td, RLIMIT_STACK); 1238fa50a355SKonstantin Belousov if (et_dyn_addr == ET_DYN_ADDR_RAND) { 1239fa50a355SKonstantin Belousov KASSERT((map->flags & MAP_ASLR) != 0, 1240fa50a355SKonstantin Belousov ("ET_DYN_ADDR_RAND but !MAP_ASLR")); 1241fa50a355SKonstantin Belousov et_dyn_addr = __CONCAT(rnd_, __elfN(base))(map, 1242fa50a355SKonstantin Belousov vm_map_min(map) + mapsz + lim_max(td, RLIMIT_DATA), 1243fa50a355SKonstantin Belousov /* reserve half of the address space to interpreter */ 1244fa50a355SKonstantin Belousov maxv / 2, 1UL << flsl(maxalign)); 1245fa50a355SKonstantin Belousov } 1246fa50a355SKonstantin Belousov 124778022527SKonstantin Belousov vn_lock(imgp->vp, LK_SHARED | LK_RETRY); 12486c775eb6SKonstantin Belousov if (error != 0) 12496c775eb6SKonstantin Belousov goto ret; 125060bb3943SAlan Cox 12519bcd7482SEdward Tomasz Napierala error = __elfN(load_sections)(imgp, hdr, phdr, et_dyn_addr, NULL); 1252292177e6SAlan Cox if (error != 0) 12536c775eb6SKonstantin Belousov goto ret; 1254e1743d02SSøren Schmidt 125520e1174aSEdward Tomasz Napierala error = __elfN(enforce_limits)(imgp, hdr, phdr, et_dyn_addr); 125620e1174aSEdward Tomasz Napierala if (error != 0) 125720e1174aSEdward Tomasz Napierala goto ret; 1258cac45152SMatthew Dillon 1259920acedbSNathan Whitehorn entry = (u_long)hdr->e_entry + et_dyn_addr; 1260920acedbSNathan Whitehorn 1261cac45152SMatthew Dillon /* 1262c460ac3aSPeter Wemm * We load the dynamic linker where a userland call 1263c460ac3aSPeter Wemm * to mmap(0, ...) would put it. The rationale behind this 1264c460ac3aSPeter Wemm * calculation is that it leaves room for the heap to grow to 1265c460ac3aSPeter Wemm * its maximum allowed size. 1266c460ac3aSPeter Wemm */ 12676c775eb6SKonstantin Belousov addr = round_page((vm_offset_t)vmspace->vm_daddr + lim_max(td, 1268292177e6SAlan Cox RLIMIT_DATA)); 1269fa50a355SKonstantin Belousov if ((map->flags & MAP_ASLR) != 0) { 1270fa50a355SKonstantin Belousov maxv1 = maxv / 2 + addr / 2; 1271fa50a355SKonstantin Belousov MPASS(maxv1 >= addr); /* No overflow */ 1272fa50a355SKonstantin Belousov map->anon_loc = __CONCAT(rnd_, __elfN(base))(map, addr, maxv1, 1273fa50a355SKonstantin Belousov MAXPAGESIZES > 1 ? pagesizes[1] : pagesizes[0]); 1274fa50a355SKonstantin Belousov } else { 1275fa50a355SKonstantin Belousov map->anon_loc = addr; 1276fa50a355SKonstantin Belousov } 1277e1743d02SSøren Schmidt 1278ea5a2b2eSSøren Schmidt imgp->entry_addr = entry; 1279ea5a2b2eSSøren Schmidt 128060bb3943SAlan Cox if (interp != NULL) { 1281b249ce48SMateusz Guzik VOP_UNLOCK(imgp->vp); 1282fa50a355SKonstantin Belousov if ((map->flags & MAP_ASLR) != 0) { 1283fa50a355SKonstantin Belousov /* Assume that interpeter fits into 1/4 of AS */ 1284fa50a355SKonstantin Belousov maxv1 = maxv / 2 + addr / 2; 1285fa50a355SKonstantin Belousov MPASS(maxv1 >= addr); /* No overflow */ 1286fa50a355SKonstantin Belousov addr = __CONCAT(rnd_, __elfN(base))(map, addr, 1287fa50a355SKonstantin Belousov maxv1, PAGE_SIZE); 1288fa50a355SKonstantin Belousov } 12899274fb35SEdward Tomasz Napierala error = __elfN(load_interp)(imgp, brand_info, interp, &addr, 12901699546dSEdward Tomasz Napierala &imgp->entry_addr); 129178022527SKonstantin Belousov vn_lock(imgp->vp, LK_SHARED | LK_RETRY); 12929274fb35SEdward Tomasz Napierala if (error != 0) 12936c775eb6SKonstantin Belousov goto ret; 129495c807cfSRobert Watson } else 12957564c4adSKonstantin Belousov addr = et_dyn_addr; 1296ea5a2b2eSSøren Schmidt 1297e1743d02SSøren Schmidt /* 1298e3532331SJohn Baldwin * Construct auxargs table (used by the copyout_auxargs routine) 1299e1743d02SSøren Schmidt */ 13002d6b8546SKonstantin Belousov elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_NOWAIT); 13012d6b8546SKonstantin Belousov if (elf_auxargs == NULL) { 1302b249ce48SMateusz Guzik VOP_UNLOCK(imgp->vp); 1303a163d034SWarner Losh elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK); 130478022527SKonstantin Belousov vn_lock(imgp->vp, LK_SHARED | LK_RETRY); 13052d6b8546SKonstantin Belousov } 1306e1743d02SSøren Schmidt elf_auxargs->execfd = -1; 13079bcd7482SEdward Tomasz Napierala elf_auxargs->phdr = proghdr + et_dyn_addr; 1308e1743d02SSøren Schmidt elf_auxargs->phent = hdr->e_phentsize; 1309e1743d02SSøren Schmidt elf_auxargs->phnum = hdr->e_phnum; 1310e1743d02SSøren Schmidt elf_auxargs->pagesz = PAGE_SIZE; 1311e1743d02SSøren Schmidt elf_auxargs->base = addr; 1312e1743d02SSøren Schmidt elf_auxargs->flags = 0; 1313e1743d02SSøren Schmidt elf_auxargs->entry = entry; 1314d36eec69SWarner Losh elf_auxargs->hdr_eflags = hdr->e_flags; 1315e1743d02SSøren Schmidt 1316e1743d02SSøren Schmidt imgp->auxargs = elf_auxargs; 1317e1743d02SSøren Schmidt imgp->interpreted = 0; 1318a0ea661fSNathan Whitehorn imgp->reloc_base = addr; 131932c01de2SDmitry Chagin imgp->proc->p_osrel = osrel; 1320cefb93f2SKonstantin Belousov imgp->proc->p_fctl0 = fctl0; 1321885f13dcSJohn Baldwin imgp->proc->p_elf_machine = hdr->e_machine; 1322885f13dcSJohn Baldwin imgp->proc->p_elf_flags = hdr->e_flags; 1323f231de47SKonstantin Belousov 13246c775eb6SKonstantin Belousov ret: 132509c78d53SEdward Tomasz Napierala if (free_interp) 132609c78d53SEdward Tomasz Napierala free(interp, M_TEMP); 1327a7cddfedSJake Burkholder return (error); 1328e1743d02SSøren Schmidt } 1329e1743d02SSøren Schmidt 1330a360a43dSJake Burkholder #define suword __CONCAT(suword, __ELF_WORD_SIZE) 13313ebc1248SPeter Wemm 133203b0d68cSJohn Baldwin int 1333d8010b11SJohn Baldwin __elfN(freebsd_copyout_auxargs)(struct image_params *imgp, uintptr_t base) 1334e1743d02SSøren Schmidt { 1335ecbb00a2SDoug Rabson Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs; 13365f77b8a8SBrooks Davis Elf_Auxinfo *argarray, *pos; 133703b0d68cSJohn Baldwin int error; 1338e1743d02SSøren Schmidt 13395f77b8a8SBrooks Davis argarray = pos = malloc(AT_COUNT * sizeof(*pos), M_TEMP, 13405f77b8a8SBrooks Davis M_WAITOK | M_ZERO); 1341e1743d02SSøren Schmidt 134235c2a5a8SWarner Losh if (args->execfd != -1) 1343e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 1344e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 1345e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 1346e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 1347e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 1348e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 1349e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 1350e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_BASE, args->base); 1351d36eec69SWarner Losh AUXARGS_ENTRY(pos, AT_EHDRFLAGS, args->hdr_eflags); 13523ff06357SKonstantin Belousov if (imgp->execpathp != 0) 13533ff06357SKonstantin Belousov AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp); 1354b96bd95bSIan Lepore AUXARGS_ENTRY(pos, AT_OSRELDATE, 1355b96bd95bSIan Lepore imgp->proc->p_ucred->cr_prison->pr_osreldate); 1356ee235befSKonstantin Belousov if (imgp->canary != 0) { 1357ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary); 1358ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen); 1359ee235befSKonstantin Belousov } 1360ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus); 1361ee235befSKonstantin Belousov if (imgp->pagesizes != 0) { 1362ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes); 1363ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen); 1364ee235befSKonstantin Belousov } 1365aea81038SKonstantin Belousov if (imgp->sysent->sv_timekeep_base != 0) { 1366aea81038SKonstantin Belousov AUXARGS_ENTRY(pos, AT_TIMEKEEP, 1367aea81038SKonstantin Belousov imgp->sysent->sv_timekeep_base); 1368aea81038SKonstantin Belousov } 136926d8f3e1SKonstantin Belousov AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj 137026d8f3e1SKonstantin Belousov != NULL && imgp->stack_prot != 0 ? imgp->stack_prot : 137126d8f3e1SKonstantin Belousov imgp->sysent->sv_stackprot); 1372c2f37b92SJohn Baldwin if (imgp->sysent->sv_hwcap != NULL) 1373c2f37b92SJohn Baldwin AUXARGS_ENTRY(pos, AT_HWCAP, *imgp->sysent->sv_hwcap); 1374904d8c49SMichal Meloun if (imgp->sysent->sv_hwcap2 != NULL) 1375904d8c49SMichal Meloun AUXARGS_ENTRY(pos, AT_HWCAP2, *imgp->sysent->sv_hwcap2); 1376944cf37bSKonstantin Belousov AUXARGS_ENTRY(pos, AT_BSDFLAGS, __elfN(sigfastblock) ? 1377944cf37bSKonstantin Belousov ELF_BSDF_SIGFASTBLK : 0); 1378e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_NULL, 0); 1379e1743d02SSøren Schmidt 1380e1743d02SSøren Schmidt free(imgp->auxargs, M_TEMP); 1381e1743d02SSøren Schmidt imgp->auxargs = NULL; 1382d8b2f079SBrooks Davis KASSERT(pos - argarray <= AT_COUNT, ("Too many auxargs")); 13835f77b8a8SBrooks Davis 1384d8010b11SJohn Baldwin error = copyout(argarray, (void *)base, sizeof(*argarray) * AT_COUNT); 13855f77b8a8SBrooks Davis free(argarray, M_TEMP); 138603b0d68cSJohn Baldwin return (error); 1387e3532331SJohn Baldwin } 1388e1743d02SSøren Schmidt 1389e3532331SJohn Baldwin int 139031174518SJohn Baldwin __elfN(freebsd_fixup)(uintptr_t *stack_base, struct image_params *imgp) 1391e3532331SJohn Baldwin { 1392e3532331SJohn Baldwin Elf_Addr *base; 1393e3532331SJohn Baldwin 1394e3532331SJohn Baldwin base = (Elf_Addr *)*stack_base; 13953ebc1248SPeter Wemm base--; 13965f77b8a8SBrooks Davis if (suword(base, imgp->args->argc) == -1) 13975f77b8a8SBrooks Davis return (EFAULT); 139831174518SJohn Baldwin *stack_base = (uintptr_t)base; 1399a7cddfedSJake Burkholder return (0); 1400e1743d02SSøren Schmidt } 1401e1743d02SSøren Schmidt 1402e1743d02SSøren Schmidt /* 14038c64af4fSJohn Polstra * Code for generating ELF core dumps. 14048c64af4fSJohn Polstra */ 14058c64af4fSJohn Polstra 14064d77a549SAlfred Perlstein typedef void (*segment_callback)(vm_map_entry_t, void *); 14070ff27d31SJohn Polstra 14080ff27d31SJohn Polstra /* Closure for cb_put_phdr(). */ 14090ff27d31SJohn Polstra struct phdr_closure { 14100ff27d31SJohn Polstra Elf_Phdr *phdr; /* Program header to fill in */ 14110ff27d31SJohn Polstra Elf_Off offset; /* Offset of segment in core file */ 14120ff27d31SJohn Polstra }; 14130ff27d31SJohn Polstra 14140ff27d31SJohn Polstra /* Closure for cb_size_segment(). */ 14150ff27d31SJohn Polstra struct sseg_closure { 14160ff27d31SJohn Polstra int count; /* Count of writable segments. */ 14170ff27d31SJohn Polstra size_t size; /* Total size of all writable segments. */ 14180ff27d31SJohn Polstra }; 14190ff27d31SJohn Polstra 1420bd390213SMikolaj Golub typedef void (*outfunc_t)(void *, struct sbuf *, size_t *); 1421bd390213SMikolaj Golub 1422bd390213SMikolaj Golub struct note_info { 1423bd390213SMikolaj Golub int type; /* Note type. */ 1424bd390213SMikolaj Golub outfunc_t outfunc; /* Output function. */ 1425bd390213SMikolaj Golub void *outarg; /* Argument for the output function. */ 1426bd390213SMikolaj Golub size_t outsize; /* Output size. */ 1427bd390213SMikolaj Golub TAILQ_ENTRY(note_info) link; /* Link to the next note info. */ 1428bd390213SMikolaj Golub }; 1429bd390213SMikolaj Golub 1430bd390213SMikolaj Golub TAILQ_HEAD(note_info_list, note_info); 1431bd390213SMikolaj Golub 1432aa14e9b7SMark Johnston /* Coredump output parameters. */ 1433aa14e9b7SMark Johnston struct coredump_params { 1434aa14e9b7SMark Johnston off_t offset; 1435aa14e9b7SMark Johnston struct ucred *active_cred; 1436aa14e9b7SMark Johnston struct ucred *file_cred; 1437aa14e9b7SMark Johnston struct thread *td; 1438aa14e9b7SMark Johnston struct vnode *vp; 143978f57a9cSMark Johnston struct compressor *comp; 1440aa14e9b7SMark Johnston }; 1441aa14e9b7SMark Johnston 144278f57a9cSMark Johnston extern int compress_user_cores; 144378f57a9cSMark Johnston extern int compress_user_cores_level; 144478f57a9cSMark Johnston 14454d77a549SAlfred Perlstein static void cb_put_phdr(vm_map_entry_t, void *); 14464d77a549SAlfred Perlstein static void cb_size_segment(vm_map_entry_t, void *); 1447c468ff88SAndriy Gapon static int core_write(struct coredump_params *, const void *, size_t, off_t, 1448aa14e9b7SMark Johnston enum uio_seg); 14491005d8afSConrad Meyer static void each_dumpable_segment(struct thread *, segment_callback, void *); 1450aa14e9b7SMark Johnston static int __elfN(corehdr)(struct coredump_params *, int, void *, size_t, 1451aa14e9b7SMark Johnston struct note_info_list *, size_t); 1452bd390213SMikolaj Golub static void __elfN(prepare_notes)(struct thread *, struct note_info_list *, 1453bd390213SMikolaj Golub size_t *); 1454bd390213SMikolaj Golub static void __elfN(puthdr)(struct thread *, void *, size_t, int, size_t); 1455bd390213SMikolaj Golub static void __elfN(putnote)(struct note_info *, struct sbuf *); 1456bd390213SMikolaj Golub static size_t register_note(struct note_info_list *, int, outfunc_t, void *); 1457bd390213SMikolaj Golub static int sbuf_drain_core_output(void *, const char *, int); 1458bd390213SMikolaj Golub 1459bd390213SMikolaj Golub static void __elfN(note_fpregset)(void *, struct sbuf *, size_t *); 1460bd390213SMikolaj Golub static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *); 1461bd390213SMikolaj Golub static void __elfN(note_prstatus)(void *, struct sbuf *, size_t *); 1462bd390213SMikolaj Golub static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *); 1463bd390213SMikolaj Golub static void __elfN(note_thrmisc)(void *, struct sbuf *, size_t *); 146486be94fcSTycho Nightingale static void __elfN(note_ptlwpinfo)(void *, struct sbuf *, size_t *); 1465f1fca82eSMikolaj Golub static void __elfN(note_procstat_auxv)(void *, struct sbuf *, size_t *); 1466f1fca82eSMikolaj Golub static void __elfN(note_procstat_proc)(void *, struct sbuf *, size_t *); 1467f1fca82eSMikolaj Golub static void __elfN(note_procstat_psstrings)(void *, struct sbuf *, size_t *); 1468f1fca82eSMikolaj Golub static void note_procstat_files(void *, struct sbuf *, size_t *); 1469f1fca82eSMikolaj Golub static void note_procstat_groups(void *, struct sbuf *, size_t *); 1470f1fca82eSMikolaj Golub static void note_procstat_osrel(void *, struct sbuf *, size_t *); 1471f1fca82eSMikolaj Golub static void note_procstat_rlimit(void *, struct sbuf *, size_t *); 1472f1fca82eSMikolaj Golub static void note_procstat_umask(void *, struct sbuf *, size_t *); 1473f1fca82eSMikolaj Golub static void note_procstat_vmmap(void *, struct sbuf *, size_t *); 14748c64af4fSJohn Polstra 1475aa14e9b7SMark Johnston /* 1476aa14e9b7SMark Johnston * Write out a core segment to the compression stream. 1477aa14e9b7SMark Johnston */ 1478e7228204SAlfred Perlstein static int 1479aa14e9b7SMark Johnston compress_chunk(struct coredump_params *p, char *base, char *buf, u_int len) 1480aa14e9b7SMark Johnston { 1481aa14e9b7SMark Johnston u_int chunk_len; 1482e7228204SAlfred Perlstein int error; 1483aa14e9b7SMark Johnston 1484aa14e9b7SMark Johnston while (len > 0) { 1485aa14e9b7SMark Johnston chunk_len = MIN(len, CORE_BUF_SIZE); 1486c468ff88SAndriy Gapon 1487c468ff88SAndriy Gapon /* 1488c468ff88SAndriy Gapon * We can get EFAULT error here. 1489c468ff88SAndriy Gapon * In that case zero out the current chunk of the segment. 1490c468ff88SAndriy Gapon */ 1491c468ff88SAndriy Gapon error = copyin(base, buf, chunk_len); 1492c468ff88SAndriy Gapon if (error != 0) 1493c468ff88SAndriy Gapon bzero(buf, chunk_len); 149478f57a9cSMark Johnston error = compressor_write(p->comp, buf, chunk_len); 1495aa14e9b7SMark Johnston if (error != 0) 1496aa14e9b7SMark Johnston break; 1497aa14e9b7SMark Johnston base += chunk_len; 1498aa14e9b7SMark Johnston len -= chunk_len; 1499e7228204SAlfred Perlstein } 1500e7228204SAlfred Perlstein return (error); 1501e7228204SAlfred Perlstein } 1502e7228204SAlfred Perlstein 1503aa14e9b7SMark Johnston static int 150478f57a9cSMark Johnston core_compressed_write(void *base, size_t len, off_t offset, void *arg) 1505aa14e9b7SMark Johnston { 1506aa14e9b7SMark Johnston 1507aa14e9b7SMark Johnston return (core_write((struct coredump_params *)arg, base, len, offset, 1508aa14e9b7SMark Johnston UIO_SYSSPACE)); 1509aa14e9b7SMark Johnston } 1510aa14e9b7SMark Johnston 1511aa14e9b7SMark Johnston static int 1512c468ff88SAndriy Gapon core_write(struct coredump_params *p, const void *base, size_t len, 1513c468ff88SAndriy Gapon off_t offset, enum uio_seg seg) 1514aa14e9b7SMark Johnston { 1515aa14e9b7SMark Johnston 1516c468ff88SAndriy Gapon return (vn_rdwr_inchunks(UIO_WRITE, p->vp, __DECONST(void *, base), 1517c468ff88SAndriy Gapon len, offset, seg, IO_UNIT | IO_DIRECT | IO_RANGELOCKED, 1518aa14e9b7SMark Johnston p->active_cred, p->file_cred, NULL, p->td)); 1519aa14e9b7SMark Johnston } 1520aa14e9b7SMark Johnston 1521aa14e9b7SMark Johnston static int 1522aa14e9b7SMark Johnston core_output(void *base, size_t len, off_t offset, struct coredump_params *p, 1523aa14e9b7SMark Johnston void *tmpbuf) 1524aa14e9b7SMark Johnston { 1525c468ff88SAndriy Gapon int error; 1526aa14e9b7SMark Johnston 152778f57a9cSMark Johnston if (p->comp != NULL) 1528aa14e9b7SMark Johnston return (compress_chunk(p, base, tmpbuf, len)); 152978f57a9cSMark Johnston 1530c468ff88SAndriy Gapon /* 1531c468ff88SAndriy Gapon * EFAULT is a non-fatal error that we can get, for example, 1532c468ff88SAndriy Gapon * if the segment is backed by a file but extends beyond its 1533c468ff88SAndriy Gapon * end. 1534c468ff88SAndriy Gapon */ 1535c468ff88SAndriy Gapon error = core_write(p, base, len, offset, UIO_USERSPACE); 1536c468ff88SAndriy Gapon if (error == EFAULT) { 1537c468ff88SAndriy Gapon log(LOG_WARNING, "Failed to fully fault in a core file segment " 1538c468ff88SAndriy Gapon "at VA %p with size 0x%zx to be written at offset 0x%jx " 1539c468ff88SAndriy Gapon "for process %s\n", base, len, offset, curproc->p_comm); 1540c468ff88SAndriy Gapon 1541c468ff88SAndriy Gapon /* 1542c468ff88SAndriy Gapon * Write a "real" zero byte at the end of the target region 1543c468ff88SAndriy Gapon * in the case this is the last segment. 1544c468ff88SAndriy Gapon * The intermediate space will be implicitly zero-filled. 1545c468ff88SAndriy Gapon */ 1546c468ff88SAndriy Gapon error = core_write(p, zero_region, 1, offset + len - 1, 1547c468ff88SAndriy Gapon UIO_SYSSPACE); 1548c468ff88SAndriy Gapon } 1549c468ff88SAndriy Gapon return (error); 1550aa14e9b7SMark Johnston } 1551bd390213SMikolaj Golub 1552bd390213SMikolaj Golub /* 1553bd390213SMikolaj Golub * Drain into a core file. 1554bd390213SMikolaj Golub */ 1555bd390213SMikolaj Golub static int 1556bd390213SMikolaj Golub sbuf_drain_core_output(void *arg, const char *data, int len) 1557bd390213SMikolaj Golub { 1558aa14e9b7SMark Johnston struct coredump_params *p; 1559f1fca82eSMikolaj Golub int error, locked; 1560bd390213SMikolaj Golub 1561aa14e9b7SMark Johnston p = (struct coredump_params *)arg; 1562f1fca82eSMikolaj Golub 1563f1fca82eSMikolaj Golub /* 1564f1fca82eSMikolaj Golub * Some kern_proc out routines that print to this sbuf may 1565f1fca82eSMikolaj Golub * call us with the process lock held. Draining with the 1566f1fca82eSMikolaj Golub * non-sleepable lock held is unsafe. The lock is needed for 1567f1fca82eSMikolaj Golub * those routines when dumping a live process. In our case we 1568f1fca82eSMikolaj Golub * can safely release the lock before draining and acquire 1569f1fca82eSMikolaj Golub * again after. 1570f1fca82eSMikolaj Golub */ 1571f1fca82eSMikolaj Golub locked = PROC_LOCKED(p->td->td_proc); 1572f1fca82eSMikolaj Golub if (locked) 1573f1fca82eSMikolaj Golub PROC_UNLOCK(p->td->td_proc); 157478f57a9cSMark Johnston if (p->comp != NULL) 157578f57a9cSMark Johnston error = compressor_write(p->comp, __DECONST(char *, data), len); 1576bd390213SMikolaj Golub else 1577aa14e9b7SMark Johnston error = core_write(p, __DECONST(void *, data), len, p->offset, 1578aa14e9b7SMark Johnston UIO_SYSSPACE); 1579f1fca82eSMikolaj Golub if (locked) 1580f1fca82eSMikolaj Golub PROC_LOCK(p->td->td_proc); 1581bd390213SMikolaj Golub if (error != 0) 1582bd390213SMikolaj Golub return (-error); 1583bd390213SMikolaj Golub p->offset += len; 1584bd390213SMikolaj Golub return (len); 1585bd390213SMikolaj Golub } 1586bd390213SMikolaj Golub 15878c64af4fSJohn Polstra int 1588e7228204SAlfred Perlstein __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags) 1589fca666a1SJulian Elischer { 1590247aba24SMarcel Moolenaar struct ucred *cred = td->td_ucred; 1591fca666a1SJulian Elischer int error = 0; 15920ff27d31SJohn Polstra struct sseg_closure seginfo; 1593bd390213SMikolaj Golub struct note_info_list notelst; 1594aa14e9b7SMark Johnston struct coredump_params params; 1595bd390213SMikolaj Golub struct note_info *ninfo; 1596aa14e9b7SMark Johnston void *hdr, *tmpbuf; 1597bd390213SMikolaj Golub size_t hdrsize, notesz, coresize; 15988c64af4fSJohn Polstra 1599e7228204SAlfred Perlstein hdr = NULL; 160002d131adSMark Johnston tmpbuf = NULL; 1601bd390213SMikolaj Golub TAILQ_INIT(¬elst); 1602e7228204SAlfred Perlstein 16030ff27d31SJohn Polstra /* Size the program segments. */ 16040ff27d31SJohn Polstra seginfo.count = 0; 16050ff27d31SJohn Polstra seginfo.size = 0; 16061005d8afSConrad Meyer each_dumpable_segment(td, cb_size_segment, &seginfo); 16070ff27d31SJohn Polstra 16080ff27d31SJohn Polstra /* 1609bd390213SMikolaj Golub * Collect info about the core file header area. 16100ff27d31SJohn Polstra */ 1611bd390213SMikolaj Golub hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count); 1612c17b0bd2SConrad Meyer if (seginfo.count + 1 >= PN_XNUM) 1613c17b0bd2SConrad Meyer hdrsize += sizeof(Elf_Shdr); 1614bd390213SMikolaj Golub __elfN(prepare_notes)(td, ¬elst, ¬esz); 1615bd390213SMikolaj Golub coresize = round_page(hdrsize + notesz) + seginfo.size; 16160ff27d31SJohn Polstra 161702d131adSMark Johnston /* Set up core dump parameters. */ 161802d131adSMark Johnston params.offset = 0; 161902d131adSMark Johnston params.active_cred = cred; 162002d131adSMark Johnston params.file_cred = NOCRED; 162102d131adSMark Johnston params.td = td; 162202d131adSMark Johnston params.vp = vp; 162378f57a9cSMark Johnston params.comp = NULL; 162402d131adSMark Johnston 1625afcc55f3SEdward Tomasz Napierala #ifdef RACCT 16264b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 16271ba5ad42SEdward Tomasz Napierala PROC_LOCK(td->td_proc); 1628bd390213SMikolaj Golub error = racct_add(td->td_proc, RACCT_CORE, coresize); 16291ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 16301ba5ad42SEdward Tomasz Napierala if (error != 0) { 16311ba5ad42SEdward Tomasz Napierala error = EFAULT; 16321ba5ad42SEdward Tomasz Napierala goto done; 16331ba5ad42SEdward Tomasz Napierala } 16344b5c9cf6SEdward Tomasz Napierala } 1635afcc55f3SEdward Tomasz Napierala #endif 1636bd390213SMikolaj Golub if (coresize >= limit) { 1637fba6b1afSAlfred Perlstein error = EFAULT; 1638fba6b1afSAlfred Perlstein goto done; 1639fba6b1afSAlfred Perlstein } 16400ff27d31SJohn Polstra 1641aa14e9b7SMark Johnston /* Create a compression stream if necessary. */ 164278f57a9cSMark Johnston if (compress_user_cores != 0) { 164378f57a9cSMark Johnston params.comp = compressor_init(core_compressed_write, 164478f57a9cSMark Johnston compress_user_cores, CORE_BUF_SIZE, 164578f57a9cSMark Johnston compress_user_cores_level, ¶ms); 164678f57a9cSMark Johnston if (params.comp == NULL) { 1647aa14e9b7SMark Johnston error = EFAULT; 1648aa14e9b7SMark Johnston goto done; 1649aa14e9b7SMark Johnston } 1650aa14e9b7SMark Johnston tmpbuf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO); 1651aa14e9b7SMark Johnston } 1652aa14e9b7SMark Johnston 16530ff27d31SJohn Polstra /* 16540ff27d31SJohn Polstra * Allocate memory for building the header, fill it up, 1655bd390213SMikolaj Golub * and write it out following the notes. 16560ff27d31SJohn Polstra */ 1657a163d034SWarner Losh hdr = malloc(hdrsize, M_TEMP, M_WAITOK); 1658aa14e9b7SMark Johnston error = __elfN(corehdr)(¶ms, seginfo.count, hdr, hdrsize, ¬elst, 1659aa14e9b7SMark Johnston notesz); 16600ff27d31SJohn Polstra 16610ff27d31SJohn Polstra /* Write the contents of all of the writable segments. */ 16620ff27d31SJohn Polstra if (error == 0) { 16630ff27d31SJohn Polstra Elf_Phdr *php; 16642b471bc6STim J. Robbins off_t offset; 16650ff27d31SJohn Polstra int i; 16660ff27d31SJohn Polstra 16670ff27d31SJohn Polstra php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1; 1668bd390213SMikolaj Golub offset = round_page(hdrsize + notesz); 16690ff27d31SJohn Polstra for (i = 0; i < seginfo.count; i++) { 1670aa14e9b7SMark Johnston error = core_output((caddr_t)(uintptr_t)php->p_vaddr, 1671aa14e9b7SMark Johnston php->p_filesz, offset, ¶ms, tmpbuf); 16720ff27d31SJohn Polstra if (error != 0) 16732b471bc6STim J. Robbins break; 16740ff27d31SJohn Polstra offset += php->p_filesz; 16750ff27d31SJohn Polstra php++; 16760ff27d31SJohn Polstra } 167778f57a9cSMark Johnston if (error == 0 && params.comp != NULL) 167878f57a9cSMark Johnston error = compressor_flush(params.comp); 16790ff27d31SJohn Polstra } 1680e7228204SAlfred Perlstein if (error) { 1681e7228204SAlfred Perlstein log(LOG_WARNING, 1682e7228204SAlfred Perlstein "Failed to write core file for process %s (error %d)\n", 1683e7228204SAlfred Perlstein curproc->p_comm, error); 1684e7228204SAlfred Perlstein } 1685e7228204SAlfred Perlstein 1686e7228204SAlfred Perlstein done: 1687aa14e9b7SMark Johnston free(tmpbuf, M_TEMP); 168878f57a9cSMark Johnston if (params.comp != NULL) 168978f57a9cSMark Johnston compressor_fini(params.comp); 1690bd390213SMikolaj Golub while ((ninfo = TAILQ_FIRST(¬elst)) != NULL) { 1691bd390213SMikolaj Golub TAILQ_REMOVE(¬elst, ninfo, link); 1692bd390213SMikolaj Golub free(ninfo, M_TEMP); 1693bd390213SMikolaj Golub } 1694bd390213SMikolaj Golub if (hdr != NULL) 16950ff27d31SJohn Polstra free(hdr, M_TEMP); 16960ff27d31SJohn Polstra 1697a7cddfedSJake Burkholder return (error); 16988c64af4fSJohn Polstra } 16998c64af4fSJohn Polstra 17000ff27d31SJohn Polstra /* 17011005d8afSConrad Meyer * A callback for each_dumpable_segment() to write out the segment's 17020ff27d31SJohn Polstra * program header entry. 17030ff27d31SJohn Polstra */ 17040ff27d31SJohn Polstra static void 17055cc6d253SEd Maste cb_put_phdr(vm_map_entry_t entry, void *closure) 17060ff27d31SJohn Polstra { 17070ff27d31SJohn Polstra struct phdr_closure *phc = (struct phdr_closure *)closure; 17080ff27d31SJohn Polstra Elf_Phdr *phdr = phc->phdr; 17090ff27d31SJohn Polstra 17100ff27d31SJohn Polstra phc->offset = round_page(phc->offset); 17110ff27d31SJohn Polstra 17120ff27d31SJohn Polstra phdr->p_type = PT_LOAD; 17130ff27d31SJohn Polstra phdr->p_offset = phc->offset; 17140ff27d31SJohn Polstra phdr->p_vaddr = entry->start; 17150ff27d31SJohn Polstra phdr->p_paddr = 0; 17160ff27d31SJohn Polstra phdr->p_filesz = phdr->p_memsz = entry->end - entry->start; 17170ff27d31SJohn Polstra phdr->p_align = PAGE_SIZE; 1718ed167eaaSKonstantin Belousov phdr->p_flags = __elfN(untrans_prot)(entry->protection); 17190ff27d31SJohn Polstra 17200ff27d31SJohn Polstra phc->offset += phdr->p_filesz; 17210ff27d31SJohn Polstra phc->phdr++; 17220ff27d31SJohn Polstra } 17230ff27d31SJohn Polstra 17240ff27d31SJohn Polstra /* 17251005d8afSConrad Meyer * A callback for each_dumpable_segment() to gather information about 17260ff27d31SJohn Polstra * the number of segments and their total size. 17270ff27d31SJohn Polstra */ 17280ff27d31SJohn Polstra static void 1729f3325003SConrad Meyer cb_size_segment(vm_map_entry_t entry, void *closure) 17300ff27d31SJohn Polstra { 17310ff27d31SJohn Polstra struct sseg_closure *ssc = (struct sseg_closure *)closure; 17320ff27d31SJohn Polstra 17330ff27d31SJohn Polstra ssc->count++; 17340ff27d31SJohn Polstra ssc->size += entry->end - entry->start; 17350ff27d31SJohn Polstra } 17360ff27d31SJohn Polstra 17370ff27d31SJohn Polstra /* 17380ff27d31SJohn Polstra * For each writable segment in the process's memory map, call the given 17390ff27d31SJohn Polstra * function with a pointer to the map entry and some arbitrary 17400ff27d31SJohn Polstra * caller-supplied data. 17410ff27d31SJohn Polstra */ 17420ff27d31SJohn Polstra static void 17431005d8afSConrad Meyer each_dumpable_segment(struct thread *td, segment_callback func, void *closure) 17440ff27d31SJohn Polstra { 1745247aba24SMarcel Moolenaar struct proc *p = td->td_proc; 17460ff27d31SJohn Polstra vm_map_t map = &p->p_vmspace->vm_map; 17470ff27d31SJohn Polstra vm_map_entry_t entry; 1748976a87a2SAlan Cox vm_object_t backing_object, object; 1749976a87a2SAlan Cox boolean_t ignore_entry; 17500ff27d31SJohn Polstra 1751976a87a2SAlan Cox vm_map_lock_read(map); 17522288078cSDoug Moore VM_MAP_ENTRY_FOREACH(entry, map) { 1753fa7dd9c5SMatthew Dillon /* 1754fa7dd9c5SMatthew Dillon * Don't dump inaccessible mappings, deal with legacy 1755fa7dd9c5SMatthew Dillon * coredump mode. 1756fa7dd9c5SMatthew Dillon * 1757fa7dd9c5SMatthew Dillon * Note that read-only segments related to the elf binary 1758fa7dd9c5SMatthew Dillon * are marked MAP_ENTRY_NOCOREDUMP now so we no longer 1759fa7dd9c5SMatthew Dillon * need to arbitrarily ignore such segments. 1760fa7dd9c5SMatthew Dillon */ 1761fa7dd9c5SMatthew Dillon if (elf_legacy_coredump) { 1762fa7dd9c5SMatthew Dillon if ((entry->protection & VM_PROT_RW) != VM_PROT_RW) 17630ff27d31SJohn Polstra continue; 1764fa7dd9c5SMatthew Dillon } else { 1765fa7dd9c5SMatthew Dillon if ((entry->protection & VM_PROT_ALL) == 0) 1766fa7dd9c5SMatthew Dillon continue; 1767fa7dd9c5SMatthew Dillon } 17680ff27d31SJohn Polstra 17699730a5daSPaul Saab /* 1770fa7dd9c5SMatthew Dillon * Dont include memory segment in the coredump if 1771fa7dd9c5SMatthew Dillon * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in 1772fa7dd9c5SMatthew Dillon * madvise(2). Do not dump submaps (i.e. parts of the 1773fa7dd9c5SMatthew Dillon * kernel map). 17749730a5daSPaul Saab */ 1775fa7dd9c5SMatthew Dillon if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP)) 17769730a5daSPaul Saab continue; 17779730a5daSPaul Saab 1778976a87a2SAlan Cox if ((object = entry->object.vm_object) == NULL) 17790ff27d31SJohn Polstra continue; 17800ff27d31SJohn Polstra 17810ff27d31SJohn Polstra /* Ignore memory-mapped devices and such things. */ 1782bc403f03SAttilio Rao VM_OBJECT_RLOCK(object); 1783976a87a2SAlan Cox while ((backing_object = object->backing_object) != NULL) { 1784bc403f03SAttilio Rao VM_OBJECT_RLOCK(backing_object); 1785bc403f03SAttilio Rao VM_OBJECT_RUNLOCK(object); 1786976a87a2SAlan Cox object = backing_object; 1787976a87a2SAlan Cox } 1788976a87a2SAlan Cox ignore_entry = object->type != OBJT_DEFAULT && 1789bc411bc2SJohn Baldwin object->type != OBJT_SWAP && object->type != OBJT_VNODE && 1790bc411bc2SJohn Baldwin object->type != OBJT_PHYS; 1791bc403f03SAttilio Rao VM_OBJECT_RUNLOCK(object); 1792976a87a2SAlan Cox if (ignore_entry) 17930ff27d31SJohn Polstra continue; 17940ff27d31SJohn Polstra 17950ff27d31SJohn Polstra (*func)(entry, closure); 17960ff27d31SJohn Polstra } 1797976a87a2SAlan Cox vm_map_unlock_read(map); 17980ff27d31SJohn Polstra } 17990ff27d31SJohn Polstra 18000ff27d31SJohn Polstra /* 18010ff27d31SJohn Polstra * Write the core file header to the file, including padding up to 18020ff27d31SJohn Polstra * the page boundary. 18030ff27d31SJohn Polstra */ 18048c64af4fSJohn Polstra static int 1805aa14e9b7SMark Johnston __elfN(corehdr)(struct coredump_params *p, int numsegs, void *hdr, 1806aa14e9b7SMark Johnston size_t hdrsize, struct note_info_list *notelst, size_t notesz) 18078c64af4fSJohn Polstra { 1808bd390213SMikolaj Golub struct note_info *ninfo; 1809bd390213SMikolaj Golub struct sbuf *sb; 1810bd390213SMikolaj Golub int error; 18118c64af4fSJohn Polstra 18128c64af4fSJohn Polstra /* Fill in the header. */ 18130ff27d31SJohn Polstra bzero(hdr, hdrsize); 1814aa14e9b7SMark Johnston __elfN(puthdr)(p->td, hdr, hdrsize, numsegs, notesz); 18158c64af4fSJohn Polstra 1816bd390213SMikolaj Golub sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN); 1817aa14e9b7SMark Johnston sbuf_set_drain(sb, sbuf_drain_core_output, p); 1818bd390213SMikolaj Golub sbuf_start_section(sb, NULL); 1819bd390213SMikolaj Golub sbuf_bcat(sb, hdr, hdrsize); 1820bd390213SMikolaj Golub TAILQ_FOREACH(ninfo, notelst, link) 1821bd390213SMikolaj Golub __elfN(putnote)(ninfo, sb); 1822bd390213SMikolaj Golub /* Align up to a page boundary for the program segments. */ 1823bd390213SMikolaj Golub sbuf_end_section(sb, -1, PAGE_SIZE, 0); 1824bd390213SMikolaj Golub error = sbuf_finish(sb); 1825bd390213SMikolaj Golub sbuf_delete(sb); 1826bd390213SMikolaj Golub 1827bd390213SMikolaj Golub return (error); 1828e7228204SAlfred Perlstein } 1829bd390213SMikolaj Golub 1830bd390213SMikolaj Golub static void 1831bd390213SMikolaj Golub __elfN(prepare_notes)(struct thread *td, struct note_info_list *list, 1832bd390213SMikolaj Golub size_t *sizep) 1833bd390213SMikolaj Golub { 1834bd390213SMikolaj Golub struct proc *p; 1835bd390213SMikolaj Golub struct thread *thr; 1836bd390213SMikolaj Golub size_t size; 1837bd390213SMikolaj Golub 1838bd390213SMikolaj Golub p = td->td_proc; 1839bd390213SMikolaj Golub size = 0; 1840bd390213SMikolaj Golub 1841bd390213SMikolaj Golub size += register_note(list, NT_PRPSINFO, __elfN(note_prpsinfo), p); 1842bd390213SMikolaj Golub 1843bd390213SMikolaj Golub /* 1844bd390213SMikolaj Golub * To have the debugger select the right thread (LWP) as the initial 1845bd390213SMikolaj Golub * thread, we dump the state of the thread passed to us in td first. 1846bd390213SMikolaj Golub * This is the thread that causes the core dump and thus likely to 1847bd390213SMikolaj Golub * be the right thread one wants to have selected in the debugger. 1848bd390213SMikolaj Golub */ 1849bd390213SMikolaj Golub thr = td; 1850bd390213SMikolaj Golub while (thr != NULL) { 1851bd390213SMikolaj Golub size += register_note(list, NT_PRSTATUS, 1852bd390213SMikolaj Golub __elfN(note_prstatus), thr); 1853bd390213SMikolaj Golub size += register_note(list, NT_FPREGSET, 1854bd390213SMikolaj Golub __elfN(note_fpregset), thr); 1855bd390213SMikolaj Golub size += register_note(list, NT_THRMISC, 1856bd390213SMikolaj Golub __elfN(note_thrmisc), thr); 185786be94fcSTycho Nightingale size += register_note(list, NT_PTLWPINFO, 185886be94fcSTycho Nightingale __elfN(note_ptlwpinfo), thr); 1859bd390213SMikolaj Golub size += register_note(list, -1, 1860bd390213SMikolaj Golub __elfN(note_threadmd), thr); 1861bd390213SMikolaj Golub 1862bd390213SMikolaj Golub thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) : 1863bd390213SMikolaj Golub TAILQ_NEXT(thr, td_plist); 1864bd390213SMikolaj Golub if (thr == td) 1865bd390213SMikolaj Golub thr = TAILQ_NEXT(thr, td_plist); 1866dada0278SJohn Polstra } 1867dada0278SJohn Polstra 1868f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_PROC, 1869f1fca82eSMikolaj Golub __elfN(note_procstat_proc), p); 1870f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_FILES, 1871f1fca82eSMikolaj Golub note_procstat_files, p); 1872f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_VMMAP, 1873f1fca82eSMikolaj Golub note_procstat_vmmap, p); 1874f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_GROUPS, 1875f1fca82eSMikolaj Golub note_procstat_groups, p); 1876f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_UMASK, 1877f1fca82eSMikolaj Golub note_procstat_umask, p); 1878f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_RLIMIT, 1879f1fca82eSMikolaj Golub note_procstat_rlimit, p); 1880f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_OSREL, 1881f1fca82eSMikolaj Golub note_procstat_osrel, p); 1882f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_PSSTRINGS, 1883f1fca82eSMikolaj Golub __elfN(note_procstat_psstrings), p); 1884f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_AUXV, 1885f1fca82eSMikolaj Golub __elfN(note_procstat_auxv), p); 1886f1fca82eSMikolaj Golub 1887bd390213SMikolaj Golub *sizep = size; 1888bd390213SMikolaj Golub } 1889bd390213SMikolaj Golub 1890bd390213SMikolaj Golub static void 1891bd390213SMikolaj Golub __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs, 1892bd390213SMikolaj Golub size_t notesz) 1893bd390213SMikolaj Golub { 1894bd390213SMikolaj Golub Elf_Ehdr *ehdr; 1895bd390213SMikolaj Golub Elf_Phdr *phdr; 1896c17b0bd2SConrad Meyer Elf_Shdr *shdr; 1897bd390213SMikolaj Golub struct phdr_closure phc; 1898bd390213SMikolaj Golub 1899bd390213SMikolaj Golub ehdr = (Elf_Ehdr *)hdr; 1900bd390213SMikolaj Golub 1901bd390213SMikolaj Golub ehdr->e_ident[EI_MAG0] = ELFMAG0; 1902bd390213SMikolaj Golub ehdr->e_ident[EI_MAG1] = ELFMAG1; 1903bd390213SMikolaj Golub ehdr->e_ident[EI_MAG2] = ELFMAG2; 1904bd390213SMikolaj Golub ehdr->e_ident[EI_MAG3] = ELFMAG3; 1905bd390213SMikolaj Golub ehdr->e_ident[EI_CLASS] = ELF_CLASS; 1906bd390213SMikolaj Golub ehdr->e_ident[EI_DATA] = ELF_DATA; 1907bd390213SMikolaj Golub ehdr->e_ident[EI_VERSION] = EV_CURRENT; 1908bd390213SMikolaj Golub ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD; 1909bd390213SMikolaj Golub ehdr->e_ident[EI_ABIVERSION] = 0; 1910bd390213SMikolaj Golub ehdr->e_ident[EI_PAD] = 0; 1911bd390213SMikolaj Golub ehdr->e_type = ET_CORE; 1912885f13dcSJohn Baldwin ehdr->e_machine = td->td_proc->p_elf_machine; 1913bd390213SMikolaj Golub ehdr->e_version = EV_CURRENT; 1914bd390213SMikolaj Golub ehdr->e_entry = 0; 1915bd390213SMikolaj Golub ehdr->e_phoff = sizeof(Elf_Ehdr); 1916885f13dcSJohn Baldwin ehdr->e_flags = td->td_proc->p_elf_flags; 1917bd390213SMikolaj Golub ehdr->e_ehsize = sizeof(Elf_Ehdr); 1918bd390213SMikolaj Golub ehdr->e_phentsize = sizeof(Elf_Phdr); 1919bd390213SMikolaj Golub ehdr->e_shentsize = sizeof(Elf_Shdr); 1920bd390213SMikolaj Golub ehdr->e_shstrndx = SHN_UNDEF; 1921c17b0bd2SConrad Meyer if (numsegs + 1 < PN_XNUM) { 1922c17b0bd2SConrad Meyer ehdr->e_phnum = numsegs + 1; 1923c17b0bd2SConrad Meyer ehdr->e_shnum = 0; 1924c17b0bd2SConrad Meyer } else { 1925c17b0bd2SConrad Meyer ehdr->e_phnum = PN_XNUM; 1926c17b0bd2SConrad Meyer ehdr->e_shnum = 1; 1927c17b0bd2SConrad Meyer 1928c17b0bd2SConrad Meyer ehdr->e_shoff = ehdr->e_phoff + 1929c17b0bd2SConrad Meyer (numsegs + 1) * ehdr->e_phentsize; 1930c17b0bd2SConrad Meyer KASSERT(ehdr->e_shoff == hdrsize - sizeof(Elf_Shdr), 1931c17b0bd2SConrad Meyer ("e_shoff: %zu, hdrsize - shdr: %zu", 193207f825e8SConrad Meyer (size_t)ehdr->e_shoff, hdrsize - sizeof(Elf_Shdr))); 1933c17b0bd2SConrad Meyer 1934c17b0bd2SConrad Meyer shdr = (Elf_Shdr *)((char *)hdr + ehdr->e_shoff); 1935c17b0bd2SConrad Meyer memset(shdr, 0, sizeof(*shdr)); 1936c17b0bd2SConrad Meyer /* 1937c17b0bd2SConrad Meyer * A special first section is used to hold large segment and 1938c17b0bd2SConrad Meyer * section counts. This was proposed by Sun Microsystems in 1939c17b0bd2SConrad Meyer * Solaris and has been adopted by Linux; the standard ELF 1940c17b0bd2SConrad Meyer * tools are already familiar with the technique. 1941c17b0bd2SConrad Meyer * 1942c17b0bd2SConrad Meyer * See table 7-7 of the Solaris "Linker and Libraries Guide" 1943c17b0bd2SConrad Meyer * (or 12-7 depending on the version of the document) for more 1944c17b0bd2SConrad Meyer * details. 1945c17b0bd2SConrad Meyer */ 1946c17b0bd2SConrad Meyer shdr->sh_type = SHT_NULL; 1947c17b0bd2SConrad Meyer shdr->sh_size = ehdr->e_shnum; 1948c17b0bd2SConrad Meyer shdr->sh_link = ehdr->e_shstrndx; 1949c17b0bd2SConrad Meyer shdr->sh_info = numsegs + 1; 1950c17b0bd2SConrad Meyer } 1951bd390213SMikolaj Golub 1952bd390213SMikolaj Golub /* 1953bd390213SMikolaj Golub * Fill in the program header entries. 1954bd390213SMikolaj Golub */ 1955c17b0bd2SConrad Meyer phdr = (Elf_Phdr *)((char *)hdr + ehdr->e_phoff); 1956bd390213SMikolaj Golub 1957bd390213SMikolaj Golub /* The note segement. */ 1958bd390213SMikolaj Golub phdr->p_type = PT_NOTE; 1959bd390213SMikolaj Golub phdr->p_offset = hdrsize; 1960bd390213SMikolaj Golub phdr->p_vaddr = 0; 1961bd390213SMikolaj Golub phdr->p_paddr = 0; 1962bd390213SMikolaj Golub phdr->p_filesz = notesz; 1963bd390213SMikolaj Golub phdr->p_memsz = 0; 1964bd390213SMikolaj Golub phdr->p_flags = PF_R; 19651b8388cdSMikolaj Golub phdr->p_align = ELF_NOTE_ROUNDSIZE; 1966bd390213SMikolaj Golub phdr++; 1967bd390213SMikolaj Golub 1968bd390213SMikolaj Golub /* All the writable segments from the program. */ 1969bd390213SMikolaj Golub phc.phdr = phdr; 1970bd390213SMikolaj Golub phc.offset = round_page(hdrsize + notesz); 19711005d8afSConrad Meyer each_dumpable_segment(td, cb_put_phdr, &phc); 1972bd390213SMikolaj Golub } 1973bd390213SMikolaj Golub 1974bd390213SMikolaj Golub static size_t 1975bd390213SMikolaj Golub register_note(struct note_info_list *list, int type, outfunc_t out, void *arg) 1976bd390213SMikolaj Golub { 1977bd390213SMikolaj Golub struct note_info *ninfo; 1978bd390213SMikolaj Golub size_t size, notesize; 1979bd390213SMikolaj Golub 1980bd390213SMikolaj Golub size = 0; 1981bd390213SMikolaj Golub out(arg, NULL, &size); 1982bd390213SMikolaj Golub ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK); 1983bd390213SMikolaj Golub ninfo->type = type; 1984bd390213SMikolaj Golub ninfo->outfunc = out; 1985bd390213SMikolaj Golub ninfo->outarg = arg; 1986bd390213SMikolaj Golub ninfo->outsize = size; 1987bd390213SMikolaj Golub TAILQ_INSERT_TAIL(list, ninfo, link); 1988bd390213SMikolaj Golub 1989bd390213SMikolaj Golub if (type == -1) 1990bd390213SMikolaj Golub return (size); 1991bd390213SMikolaj Golub 1992bd390213SMikolaj Golub notesize = sizeof(Elf_Note) + /* note header */ 1993180e57e5SJohn Baldwin roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) + 1994180e57e5SJohn Baldwin /* note name */ 1995180e57e5SJohn Baldwin roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */ 1996180e57e5SJohn Baldwin 1997180e57e5SJohn Baldwin return (notesize); 1998180e57e5SJohn Baldwin } 1999180e57e5SJohn Baldwin 2000180e57e5SJohn Baldwin static size_t 2001180e57e5SJohn Baldwin append_note_data(const void *src, void *dst, size_t len) 2002180e57e5SJohn Baldwin { 2003180e57e5SJohn Baldwin size_t padded_len; 2004180e57e5SJohn Baldwin 2005180e57e5SJohn Baldwin padded_len = roundup2(len, ELF_NOTE_ROUNDSIZE); 2006180e57e5SJohn Baldwin if (dst != NULL) { 2007180e57e5SJohn Baldwin bcopy(src, dst, len); 2008180e57e5SJohn Baldwin bzero((char *)dst + len, padded_len - len); 2009180e57e5SJohn Baldwin } 2010180e57e5SJohn Baldwin return (padded_len); 2011180e57e5SJohn Baldwin } 2012180e57e5SJohn Baldwin 2013180e57e5SJohn Baldwin size_t 2014180e57e5SJohn Baldwin __elfN(populate_note)(int type, void *src, void *dst, size_t size, void **descp) 2015180e57e5SJohn Baldwin { 2016180e57e5SJohn Baldwin Elf_Note *note; 2017180e57e5SJohn Baldwin char *buf; 2018180e57e5SJohn Baldwin size_t notesize; 2019180e57e5SJohn Baldwin 2020180e57e5SJohn Baldwin buf = dst; 2021180e57e5SJohn Baldwin if (buf != NULL) { 2022180e57e5SJohn Baldwin note = (Elf_Note *)buf; 2023180e57e5SJohn Baldwin note->n_namesz = sizeof(FREEBSD_ABI_VENDOR); 2024180e57e5SJohn Baldwin note->n_descsz = size; 2025180e57e5SJohn Baldwin note->n_type = type; 2026180e57e5SJohn Baldwin buf += sizeof(*note); 2027180e57e5SJohn Baldwin buf += append_note_data(FREEBSD_ABI_VENDOR, buf, 2028180e57e5SJohn Baldwin sizeof(FREEBSD_ABI_VENDOR)); 2029180e57e5SJohn Baldwin append_note_data(src, buf, size); 2030180e57e5SJohn Baldwin if (descp != NULL) 2031180e57e5SJohn Baldwin *descp = buf; 2032180e57e5SJohn Baldwin } 2033180e57e5SJohn Baldwin 2034180e57e5SJohn Baldwin notesize = sizeof(Elf_Note) + /* note header */ 2035180e57e5SJohn Baldwin roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) + 2036180e57e5SJohn Baldwin /* note name */ 20371b8388cdSMikolaj Golub roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */ 2038bd390213SMikolaj Golub 2039bd390213SMikolaj Golub return (notesize); 2040bd390213SMikolaj Golub } 2041bd390213SMikolaj Golub 2042bd390213SMikolaj Golub static void 2043bd390213SMikolaj Golub __elfN(putnote)(struct note_info *ninfo, struct sbuf *sb) 2044bd390213SMikolaj Golub { 2045bd390213SMikolaj Golub Elf_Note note; 204614bdbaf2SConrad Meyer ssize_t old_len, sect_len; 204714bdbaf2SConrad Meyer size_t new_len, descsz, i; 2048bd390213SMikolaj Golub 2049bd390213SMikolaj Golub if (ninfo->type == -1) { 2050bd390213SMikolaj Golub ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize); 2051bd390213SMikolaj Golub return; 2052bd390213SMikolaj Golub } 2053bd390213SMikolaj Golub 2054180e57e5SJohn Baldwin note.n_namesz = sizeof(FREEBSD_ABI_VENDOR); 2055bd390213SMikolaj Golub note.n_descsz = ninfo->outsize; 2056bd390213SMikolaj Golub note.n_type = ninfo->type; 2057bd390213SMikolaj Golub 2058bd390213SMikolaj Golub sbuf_bcat(sb, ¬e, sizeof(note)); 2059bd390213SMikolaj Golub sbuf_start_section(sb, &old_len); 2060180e57e5SJohn Baldwin sbuf_bcat(sb, FREEBSD_ABI_VENDOR, sizeof(FREEBSD_ABI_VENDOR)); 20611b8388cdSMikolaj Golub sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0); 2062bd390213SMikolaj Golub if (note.n_descsz == 0) 2063bd390213SMikolaj Golub return; 2064bd390213SMikolaj Golub sbuf_start_section(sb, &old_len); 2065bd390213SMikolaj Golub ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize); 206614bdbaf2SConrad Meyer sect_len = sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0); 206714bdbaf2SConrad Meyer if (sect_len < 0) 206814bdbaf2SConrad Meyer return; 206914bdbaf2SConrad Meyer 207014bdbaf2SConrad Meyer new_len = (size_t)sect_len; 207114bdbaf2SConrad Meyer descsz = roundup(note.n_descsz, ELF_NOTE_ROUNDSIZE); 207214bdbaf2SConrad Meyer if (new_len < descsz) { 207314bdbaf2SConrad Meyer /* 207414bdbaf2SConrad Meyer * It is expected that individual note emitters will correctly 207514bdbaf2SConrad Meyer * predict their expected output size and fill up to that size 207614bdbaf2SConrad Meyer * themselves, padding in a format-specific way if needed. 207714bdbaf2SConrad Meyer * However, in case they don't, just do it here with zeros. 207814bdbaf2SConrad Meyer */ 207914bdbaf2SConrad Meyer for (i = 0; i < descsz - new_len; i++) 208014bdbaf2SConrad Meyer sbuf_putc(sb, 0); 208114bdbaf2SConrad Meyer } else if (new_len > descsz) { 208214bdbaf2SConrad Meyer /* 208314bdbaf2SConrad Meyer * We can't always truncate sb -- we may have drained some 208414bdbaf2SConrad Meyer * of it already. 208514bdbaf2SConrad Meyer */ 208614bdbaf2SConrad Meyer KASSERT(new_len == descsz, ("%s: Note type %u changed as we " 208714bdbaf2SConrad Meyer "read it (%zu > %zu). Since it is longer than " 208814bdbaf2SConrad Meyer "expected, this coredump's notes are corrupt. THIS " 208914bdbaf2SConrad Meyer "IS A BUG in the note_procstat routine for type %u.\n", 209014bdbaf2SConrad Meyer __func__, (unsigned)note.n_type, new_len, descsz, 209114bdbaf2SConrad Meyer (unsigned)note.n_type)); 209214bdbaf2SConrad Meyer } 2093bd390213SMikolaj Golub } 2094bd390213SMikolaj Golub 2095bd390213SMikolaj Golub /* 2096bd390213SMikolaj Golub * Miscellaneous note out functions. 2097bd390213SMikolaj Golub */ 2098bd390213SMikolaj Golub 2099841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 2100841c0c7eSNathan Whitehorn #include <compat/freebsd32/freebsd32.h> 210151645e83SJohn Baldwin #include <compat/freebsd32/freebsd32_signal.h> 2102841c0c7eSNathan Whitehorn 210362919d78SPeter Wemm typedef struct prstatus32 elf_prstatus_t; 210462919d78SPeter Wemm typedef struct prpsinfo32 elf_prpsinfo_t; 210562919d78SPeter Wemm typedef struct fpreg32 elf_prfpregset_t; 210662919d78SPeter Wemm typedef struct fpreg32 elf_fpregset_t; 210762919d78SPeter Wemm typedef struct reg32 elf_gregset_t; 21087f08176eSAttilio Rao typedef struct thrmisc32 elf_thrmisc_t; 2109f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK KERN_PROC_MASK32 2110f1fca82eSMikolaj Golub typedef struct kinfo_proc32 elf_kinfo_proc_t; 2111f1fca82eSMikolaj Golub typedef uint32_t elf_ps_strings_t; 211262919d78SPeter Wemm #else 211362919d78SPeter Wemm typedef prstatus_t elf_prstatus_t; 211462919d78SPeter Wemm typedef prpsinfo_t elf_prpsinfo_t; 211562919d78SPeter Wemm typedef prfpregset_t elf_prfpregset_t; 211662919d78SPeter Wemm typedef prfpregset_t elf_fpregset_t; 211762919d78SPeter Wemm typedef gregset_t elf_gregset_t; 21187f08176eSAttilio Rao typedef thrmisc_t elf_thrmisc_t; 2119f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK 0 2120f1fca82eSMikolaj Golub typedef struct kinfo_proc elf_kinfo_proc_t; 2121f1fca82eSMikolaj Golub typedef vm_offset_t elf_ps_strings_t; 212262919d78SPeter Wemm #endif 212362919d78SPeter Wemm 21248c64af4fSJohn Polstra static void 2125bd390213SMikolaj Golub __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep) 21268c64af4fSJohn Polstra { 2127c77547d2SJohn Baldwin struct sbuf sbarg; 2128c77547d2SJohn Baldwin size_t len; 2129c77547d2SJohn Baldwin char *cp, *end; 2130247aba24SMarcel Moolenaar struct proc *p; 2131bd390213SMikolaj Golub elf_prpsinfo_t *psinfo; 2132c77547d2SJohn Baldwin int error; 21338c64af4fSJohn Polstra 2134bd390213SMikolaj Golub p = (struct proc *)arg; 2135bd390213SMikolaj Golub if (sb != NULL) { 2136bd390213SMikolaj Golub KASSERT(*sizep == sizeof(*psinfo), ("invalid size")); 2137bd390213SMikolaj Golub psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK); 21388c9b7b2cSMarcel Moolenaar psinfo->pr_version = PRPSINFO_VERSION; 213962919d78SPeter Wemm psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t); 2140ccd3953eSJohn Baldwin strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname)); 2141c77547d2SJohn Baldwin PROC_LOCK(p); 2142c77547d2SJohn Baldwin if (p->p_args != NULL) { 2143c77547d2SJohn Baldwin len = sizeof(psinfo->pr_psargs) - 1; 2144c77547d2SJohn Baldwin if (len > p->p_args->ar_length) 2145c77547d2SJohn Baldwin len = p->p_args->ar_length; 2146c77547d2SJohn Baldwin memcpy(psinfo->pr_psargs, p->p_args->ar_args, len); 2147c77547d2SJohn Baldwin PROC_UNLOCK(p); 2148c77547d2SJohn Baldwin error = 0; 2149c77547d2SJohn Baldwin } else { 2150c77547d2SJohn Baldwin _PHOLD(p); 2151c77547d2SJohn Baldwin PROC_UNLOCK(p); 2152c77547d2SJohn Baldwin sbuf_new(&sbarg, psinfo->pr_psargs, 2153c77547d2SJohn Baldwin sizeof(psinfo->pr_psargs), SBUF_FIXEDLEN); 2154c77547d2SJohn Baldwin error = proc_getargv(curthread, p, &sbarg); 2155c77547d2SJohn Baldwin PRELE(p); 2156c77547d2SJohn Baldwin if (sbuf_finish(&sbarg) == 0) 2157c77547d2SJohn Baldwin len = sbuf_len(&sbarg) - 1; 2158c77547d2SJohn Baldwin else 2159c77547d2SJohn Baldwin len = sizeof(psinfo->pr_psargs) - 1; 2160c77547d2SJohn Baldwin sbuf_delete(&sbarg); 2161c77547d2SJohn Baldwin } 2162c77547d2SJohn Baldwin if (error || len == 0) 2163ccd3953eSJohn Baldwin strlcpy(psinfo->pr_psargs, p->p_comm, 21648c9b7b2cSMarcel Moolenaar sizeof(psinfo->pr_psargs)); 2165c77547d2SJohn Baldwin else { 2166c77547d2SJohn Baldwin KASSERT(len < sizeof(psinfo->pr_psargs), 2167c77547d2SJohn Baldwin ("len is too long: %zu vs %zu", len, 2168c77547d2SJohn Baldwin sizeof(psinfo->pr_psargs))); 2169c77547d2SJohn Baldwin cp = psinfo->pr_psargs; 2170c77547d2SJohn Baldwin end = cp + len - 1; 2171c77547d2SJohn Baldwin for (;;) { 2172c77547d2SJohn Baldwin cp = memchr(cp, '\0', end - cp); 2173c77547d2SJohn Baldwin if (cp == NULL) 2174c77547d2SJohn Baldwin break; 2175c77547d2SJohn Baldwin *cp = ' '; 2176c77547d2SJohn Baldwin } 2177c77547d2SJohn Baldwin } 2178ccb83afdSJohn Baldwin psinfo->pr_pid = p->p_pid; 2179bd390213SMikolaj Golub sbuf_bcat(sb, psinfo, sizeof(*psinfo)); 2180bd390213SMikolaj Golub free(psinfo, M_TEMP); 2181bd390213SMikolaj Golub } 2182bd390213SMikolaj Golub *sizep = sizeof(*psinfo); 2183bd390213SMikolaj Golub } 2184bd390213SMikolaj Golub 2185bd390213SMikolaj Golub static void 2186bd390213SMikolaj Golub __elfN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep) 2187bd390213SMikolaj Golub { 2188bd390213SMikolaj Golub struct thread *td; 2189bd390213SMikolaj Golub elf_prstatus_t *status; 2190bd390213SMikolaj Golub 2191bd390213SMikolaj Golub td = (struct thread *)arg; 2192bd390213SMikolaj Golub if (sb != NULL) { 2193bd390213SMikolaj Golub KASSERT(*sizep == sizeof(*status), ("invalid size")); 2194bd390213SMikolaj Golub status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK); 21958c9b7b2cSMarcel Moolenaar status->pr_version = PRSTATUS_VERSION; 219662919d78SPeter Wemm status->pr_statussz = sizeof(elf_prstatus_t); 219762919d78SPeter Wemm status->pr_gregsetsz = sizeof(elf_gregset_t); 219862919d78SPeter Wemm status->pr_fpregsetsz = sizeof(elf_fpregset_t); 21998c9b7b2cSMarcel Moolenaar status->pr_osreldate = osreldate; 2200bd390213SMikolaj Golub status->pr_cursig = td->td_proc->p_sig; 2201bd390213SMikolaj Golub status->pr_pid = td->td_tid; 2202841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 2203bd390213SMikolaj Golub fill_regs32(td, &status->pr_reg); 220462919d78SPeter Wemm #else 2205bd390213SMikolaj Golub fill_regs(td, &status->pr_reg); 220662919d78SPeter Wemm #endif 2207bd390213SMikolaj Golub sbuf_bcat(sb, status, sizeof(*status)); 2208bd390213SMikolaj Golub free(status, M_TEMP); 22098c9b7b2cSMarcel Moolenaar } 2210bd390213SMikolaj Golub *sizep = sizeof(*status); 2211bd390213SMikolaj Golub } 2212bd390213SMikolaj Golub 2213bd390213SMikolaj Golub static void 2214bd390213SMikolaj Golub __elfN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep) 2215bd390213SMikolaj Golub { 2216bd390213SMikolaj Golub struct thread *td; 2217bd390213SMikolaj Golub elf_prfpregset_t *fpregset; 2218bd390213SMikolaj Golub 2219bd390213SMikolaj Golub td = (struct thread *)arg; 2220bd390213SMikolaj Golub if (sb != NULL) { 2221bd390213SMikolaj Golub KASSERT(*sizep == sizeof(*fpregset), ("invalid size")); 2222bd390213SMikolaj Golub fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK); 2223bd390213SMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 2224bd390213SMikolaj Golub fill_fpregs32(td, fpregset); 2225bd390213SMikolaj Golub #else 2226bd390213SMikolaj Golub fill_fpregs(td, fpregset); 2227bd390213SMikolaj Golub #endif 2228bd390213SMikolaj Golub sbuf_bcat(sb, fpregset, sizeof(*fpregset)); 2229bd390213SMikolaj Golub free(fpregset, M_TEMP); 2230bd390213SMikolaj Golub } 2231bd390213SMikolaj Golub *sizep = sizeof(*fpregset); 2232bd390213SMikolaj Golub } 2233bd390213SMikolaj Golub 2234bd390213SMikolaj Golub static void 2235bd390213SMikolaj Golub __elfN(note_thrmisc)(void *arg, struct sbuf *sb, size_t *sizep) 2236bd390213SMikolaj Golub { 2237bd390213SMikolaj Golub struct thread *td; 2238bd390213SMikolaj Golub elf_thrmisc_t thrmisc; 2239bd390213SMikolaj Golub 2240bd390213SMikolaj Golub td = (struct thread *)arg; 2241bd390213SMikolaj Golub if (sb != NULL) { 2242bd390213SMikolaj Golub KASSERT(*sizep == sizeof(thrmisc), ("invalid size")); 22432e5f9189SEd Maste bzero(&thrmisc, sizeof(thrmisc)); 2244bd390213SMikolaj Golub strcpy(thrmisc.pr_tname, td->td_name); 2245bd390213SMikolaj Golub sbuf_bcat(sb, &thrmisc, sizeof(thrmisc)); 2246bd390213SMikolaj Golub } 2247bd390213SMikolaj Golub *sizep = sizeof(thrmisc); 2248bd390213SMikolaj Golub } 2249bd390213SMikolaj Golub 225086be94fcSTycho Nightingale static void 225186be94fcSTycho Nightingale __elfN(note_ptlwpinfo)(void *arg, struct sbuf *sb, size_t *sizep) 225286be94fcSTycho Nightingale { 225386be94fcSTycho Nightingale struct thread *td; 225486be94fcSTycho Nightingale size_t size; 225586be94fcSTycho Nightingale int structsize; 225651645e83SJohn Baldwin #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 225751645e83SJohn Baldwin struct ptrace_lwpinfo32 pl; 225851645e83SJohn Baldwin #else 225986be94fcSTycho Nightingale struct ptrace_lwpinfo pl; 226051645e83SJohn Baldwin #endif 226186be94fcSTycho Nightingale 226286be94fcSTycho Nightingale td = (struct thread *)arg; 226351645e83SJohn Baldwin size = sizeof(structsize) + sizeof(pl); 226486be94fcSTycho Nightingale if (sb != NULL) { 226586be94fcSTycho Nightingale KASSERT(*sizep == size, ("invalid size")); 226651645e83SJohn Baldwin structsize = sizeof(pl); 226786be94fcSTycho Nightingale sbuf_bcat(sb, &structsize, sizeof(structsize)); 226886be94fcSTycho Nightingale bzero(&pl, sizeof(pl)); 226986be94fcSTycho Nightingale pl.pl_lwpid = td->td_tid; 227086be94fcSTycho Nightingale pl.pl_event = PL_EVENT_NONE; 227186be94fcSTycho Nightingale pl.pl_sigmask = td->td_sigmask; 227286be94fcSTycho Nightingale pl.pl_siglist = td->td_siglist; 227386be94fcSTycho Nightingale if (td->td_si.si_signo != 0) { 227486be94fcSTycho Nightingale pl.pl_event = PL_EVENT_SIGNAL; 227586be94fcSTycho Nightingale pl.pl_flags |= PL_FLAG_SI; 227651645e83SJohn Baldwin #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 227751645e83SJohn Baldwin siginfo_to_siginfo32(&td->td_si, &pl.pl_siginfo); 227851645e83SJohn Baldwin #else 227986be94fcSTycho Nightingale pl.pl_siginfo = td->td_si; 228051645e83SJohn Baldwin #endif 228186be94fcSTycho Nightingale } 228286be94fcSTycho Nightingale strcpy(pl.pl_tdname, td->td_name); 228386be94fcSTycho Nightingale /* XXX TODO: supply more information in struct ptrace_lwpinfo*/ 228451645e83SJohn Baldwin sbuf_bcat(sb, &pl, sizeof(pl)); 228586be94fcSTycho Nightingale } 228686be94fcSTycho Nightingale *sizep = size; 228786be94fcSTycho Nightingale } 228886be94fcSTycho Nightingale 22894da47b2fSMarcel Moolenaar /* 22904da47b2fSMarcel Moolenaar * Allow for MD specific notes, as well as any MD 22914da47b2fSMarcel Moolenaar * specific preparations for writing MI notes. 22924da47b2fSMarcel Moolenaar */ 22938c64af4fSJohn Polstra static void 2294bd390213SMikolaj Golub __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep) 22958c64af4fSJohn Polstra { 2296bd390213SMikolaj Golub struct thread *td; 2297bd390213SMikolaj Golub void *buf; 2298bd390213SMikolaj Golub size_t size; 22998c64af4fSJohn Polstra 2300bd390213SMikolaj Golub td = (struct thread *)arg; 2301bd390213SMikolaj Golub size = *sizep; 2302bd390213SMikolaj Golub if (size != 0 && sb != NULL) 2303bd390213SMikolaj Golub buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK); 230483a396ceSChristian Brueffer else 230583a396ceSChristian Brueffer buf = NULL; 2306bd390213SMikolaj Golub size = 0; 2307bd390213SMikolaj Golub __elfN(dump_thread)(td, buf, &size); 230864779280SKonstantin Belousov KASSERT(sb == NULL || *sizep == size, ("invalid size")); 230983a396ceSChristian Brueffer if (size != 0 && sb != NULL) 2310bd390213SMikolaj Golub sbuf_bcat(sb, buf, size); 2311a1761d73SChristian Brueffer free(buf, M_TEMP); 2312bd390213SMikolaj Golub *sizep = size; 23138c64af4fSJohn Polstra } 23148c64af4fSJohn Polstra 2315f1fca82eSMikolaj Golub #ifdef KINFO_PROC_SIZE 2316f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 2317f1fca82eSMikolaj Golub #endif 2318f1fca82eSMikolaj Golub 2319f1fca82eSMikolaj Golub static void 2320f1fca82eSMikolaj Golub __elfN(note_procstat_proc)(void *arg, struct sbuf *sb, size_t *sizep) 2321f1fca82eSMikolaj Golub { 2322f1fca82eSMikolaj Golub struct proc *p; 2323f1fca82eSMikolaj Golub size_t size; 2324f1fca82eSMikolaj Golub int structsize; 2325f1fca82eSMikolaj Golub 2326f1fca82eSMikolaj Golub p = (struct proc *)arg; 2327f1fca82eSMikolaj Golub size = sizeof(structsize) + p->p_numthreads * 2328f1fca82eSMikolaj Golub sizeof(elf_kinfo_proc_t); 2329f1fca82eSMikolaj Golub 2330f1fca82eSMikolaj Golub if (sb != NULL) { 2331f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2332f1fca82eSMikolaj Golub structsize = sizeof(elf_kinfo_proc_t); 2333f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2334f1fca82eSMikolaj Golub PROC_LOCK(p); 2335f1fca82eSMikolaj Golub kern_proc_out(p, sb, ELF_KERN_PROC_MASK); 2336f1fca82eSMikolaj Golub } 2337f1fca82eSMikolaj Golub *sizep = size; 2338f1fca82eSMikolaj Golub } 2339f1fca82eSMikolaj Golub 2340f1fca82eSMikolaj Golub #ifdef KINFO_FILE_SIZE 2341f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE); 2342f1fca82eSMikolaj Golub #endif 2343f1fca82eSMikolaj Golub 2344f1fca82eSMikolaj Golub static void 2345f1fca82eSMikolaj Golub note_procstat_files(void *arg, struct sbuf *sb, size_t *sizep) 2346f1fca82eSMikolaj Golub { 2347f1fca82eSMikolaj Golub struct proc *p; 234814bdbaf2SConrad Meyer size_t size, sect_sz, i; 234914bdbaf2SConrad Meyer ssize_t start_len, sect_len; 235014bdbaf2SConrad Meyer int structsize, filedesc_flags; 235114bdbaf2SConrad Meyer 2352bcb60d52SConrad Meyer if (coredump_pack_fileinfo) 235314bdbaf2SConrad Meyer filedesc_flags = KERN_FILEDESC_PACK_KINFO; 235414bdbaf2SConrad Meyer else 235514bdbaf2SConrad Meyer filedesc_flags = 0; 2356f1fca82eSMikolaj Golub 2357f1fca82eSMikolaj Golub p = (struct proc *)arg; 235814bdbaf2SConrad Meyer structsize = sizeof(struct kinfo_file); 2359f1fca82eSMikolaj Golub if (sb == NULL) { 2360f1fca82eSMikolaj Golub size = 0; 2361f1fca82eSMikolaj Golub sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN); 23625c32e9fcSAlexander Motin sbuf_set_drain(sb, sbuf_count_drain, &size); 2363f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2364f1fca82eSMikolaj Golub PROC_LOCK(p); 236514bdbaf2SConrad Meyer kern_proc_filedesc_out(p, sb, -1, filedesc_flags); 2366f1fca82eSMikolaj Golub sbuf_finish(sb); 2367f1fca82eSMikolaj Golub sbuf_delete(sb); 2368f1fca82eSMikolaj Golub *sizep = size; 2369f1fca82eSMikolaj Golub } else { 237014bdbaf2SConrad Meyer sbuf_start_section(sb, &start_len); 237114bdbaf2SConrad Meyer 2372f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2373f1fca82eSMikolaj Golub PROC_LOCK(p); 237414bdbaf2SConrad Meyer kern_proc_filedesc_out(p, sb, *sizep - sizeof(structsize), 237514bdbaf2SConrad Meyer filedesc_flags); 237614bdbaf2SConrad Meyer 237714bdbaf2SConrad Meyer sect_len = sbuf_end_section(sb, start_len, 0, 0); 237814bdbaf2SConrad Meyer if (sect_len < 0) 237914bdbaf2SConrad Meyer return; 238014bdbaf2SConrad Meyer sect_sz = sect_len; 238114bdbaf2SConrad Meyer 238214bdbaf2SConrad Meyer KASSERT(sect_sz <= *sizep, 238314bdbaf2SConrad Meyer ("kern_proc_filedesc_out did not respect maxlen; " 238414bdbaf2SConrad Meyer "requested %zu, got %zu", *sizep - sizeof(structsize), 238514bdbaf2SConrad Meyer sect_sz - sizeof(structsize))); 238614bdbaf2SConrad Meyer 238714bdbaf2SConrad Meyer for (i = 0; i < *sizep - sect_sz && sb->s_error == 0; i++) 238814bdbaf2SConrad Meyer sbuf_putc(sb, 0); 2389f1fca82eSMikolaj Golub } 2390f1fca82eSMikolaj Golub } 2391f1fca82eSMikolaj Golub 2392f1fca82eSMikolaj Golub #ifdef KINFO_VMENTRY_SIZE 2393f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE); 2394f1fca82eSMikolaj Golub #endif 2395f1fca82eSMikolaj Golub 2396f1fca82eSMikolaj Golub static void 2397f1fca82eSMikolaj Golub note_procstat_vmmap(void *arg, struct sbuf *sb, size_t *sizep) 2398f1fca82eSMikolaj Golub { 2399f1fca82eSMikolaj Golub struct proc *p; 2400f1fca82eSMikolaj Golub size_t size; 2401e6b95927SConrad Meyer int structsize, vmmap_flags; 2402e6b95927SConrad Meyer 2403e6b95927SConrad Meyer if (coredump_pack_vmmapinfo) 2404e6b95927SConrad Meyer vmmap_flags = KERN_VMMAP_PACK_KINFO; 2405e6b95927SConrad Meyer else 2406e6b95927SConrad Meyer vmmap_flags = 0; 2407f1fca82eSMikolaj Golub 2408f1fca82eSMikolaj Golub p = (struct proc *)arg; 2409e6b95927SConrad Meyer structsize = sizeof(struct kinfo_vmentry); 2410f1fca82eSMikolaj Golub if (sb == NULL) { 2411f1fca82eSMikolaj Golub size = 0; 2412f1fca82eSMikolaj Golub sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN); 24135c32e9fcSAlexander Motin sbuf_set_drain(sb, sbuf_count_drain, &size); 2414f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2415f1fca82eSMikolaj Golub PROC_LOCK(p); 2416e6b95927SConrad Meyer kern_proc_vmmap_out(p, sb, -1, vmmap_flags); 2417f1fca82eSMikolaj Golub sbuf_finish(sb); 2418f1fca82eSMikolaj Golub sbuf_delete(sb); 2419f1fca82eSMikolaj Golub *sizep = size; 2420f1fca82eSMikolaj Golub } else { 2421f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2422f1fca82eSMikolaj Golub PROC_LOCK(p); 2423e6b95927SConrad Meyer kern_proc_vmmap_out(p, sb, *sizep - sizeof(structsize), 2424e6b95927SConrad Meyer vmmap_flags); 2425f1fca82eSMikolaj Golub } 2426f1fca82eSMikolaj Golub } 2427f1fca82eSMikolaj Golub 2428f1fca82eSMikolaj Golub static void 2429f1fca82eSMikolaj Golub note_procstat_groups(void *arg, struct sbuf *sb, size_t *sizep) 2430f1fca82eSMikolaj Golub { 2431f1fca82eSMikolaj Golub struct proc *p; 2432f1fca82eSMikolaj Golub size_t size; 2433f1fca82eSMikolaj Golub int structsize; 2434f1fca82eSMikolaj Golub 2435f1fca82eSMikolaj Golub p = (struct proc *)arg; 2436f1fca82eSMikolaj Golub size = sizeof(structsize) + p->p_ucred->cr_ngroups * sizeof(gid_t); 2437f1fca82eSMikolaj Golub if (sb != NULL) { 2438f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2439f1fca82eSMikolaj Golub structsize = sizeof(gid_t); 2440f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2441f1fca82eSMikolaj Golub sbuf_bcat(sb, p->p_ucred->cr_groups, p->p_ucred->cr_ngroups * 2442f1fca82eSMikolaj Golub sizeof(gid_t)); 2443f1fca82eSMikolaj Golub } 2444f1fca82eSMikolaj Golub *sizep = size; 2445f1fca82eSMikolaj Golub } 2446f1fca82eSMikolaj Golub 2447f1fca82eSMikolaj Golub static void 2448f1fca82eSMikolaj Golub note_procstat_umask(void *arg, struct sbuf *sb, size_t *sizep) 2449f1fca82eSMikolaj Golub { 2450f1fca82eSMikolaj Golub struct proc *p; 2451f1fca82eSMikolaj Golub size_t size; 2452f1fca82eSMikolaj Golub int structsize; 2453f1fca82eSMikolaj Golub 2454f1fca82eSMikolaj Golub p = (struct proc *)arg; 2455f1fca82eSMikolaj Golub size = sizeof(structsize) + sizeof(p->p_fd->fd_cmask); 2456f1fca82eSMikolaj Golub if (sb != NULL) { 2457f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2458f1fca82eSMikolaj Golub structsize = sizeof(p->p_fd->fd_cmask); 2459f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2460f1fca82eSMikolaj Golub sbuf_bcat(sb, &p->p_fd->fd_cmask, sizeof(p->p_fd->fd_cmask)); 2461f1fca82eSMikolaj Golub } 2462f1fca82eSMikolaj Golub *sizep = size; 2463f1fca82eSMikolaj Golub } 2464f1fca82eSMikolaj Golub 2465f1fca82eSMikolaj Golub static void 2466f1fca82eSMikolaj Golub note_procstat_rlimit(void *arg, struct sbuf *sb, size_t *sizep) 2467f1fca82eSMikolaj Golub { 2468f1fca82eSMikolaj Golub struct proc *p; 2469f1fca82eSMikolaj Golub struct rlimit rlim[RLIM_NLIMITS]; 2470f1fca82eSMikolaj Golub size_t size; 2471f1fca82eSMikolaj Golub int structsize, i; 2472f1fca82eSMikolaj Golub 2473f1fca82eSMikolaj Golub p = (struct proc *)arg; 2474f1fca82eSMikolaj Golub size = sizeof(structsize) + sizeof(rlim); 2475f1fca82eSMikolaj Golub if (sb != NULL) { 2476f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2477f1fca82eSMikolaj Golub structsize = sizeof(rlim); 2478f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2479f1fca82eSMikolaj Golub PROC_LOCK(p); 2480f1fca82eSMikolaj Golub for (i = 0; i < RLIM_NLIMITS; i++) 2481f6f6d240SMateusz Guzik lim_rlimit_proc(p, i, &rlim[i]); 2482f1fca82eSMikolaj Golub PROC_UNLOCK(p); 2483f1fca82eSMikolaj Golub sbuf_bcat(sb, rlim, sizeof(rlim)); 2484f1fca82eSMikolaj Golub } 2485f1fca82eSMikolaj Golub *sizep = size; 2486f1fca82eSMikolaj Golub } 2487f1fca82eSMikolaj Golub 2488f1fca82eSMikolaj Golub static void 2489f1fca82eSMikolaj Golub note_procstat_osrel(void *arg, struct sbuf *sb, size_t *sizep) 2490f1fca82eSMikolaj Golub { 2491f1fca82eSMikolaj Golub struct proc *p; 2492f1fca82eSMikolaj Golub size_t size; 2493f1fca82eSMikolaj Golub int structsize; 2494f1fca82eSMikolaj Golub 2495f1fca82eSMikolaj Golub p = (struct proc *)arg; 2496f1fca82eSMikolaj Golub size = sizeof(structsize) + sizeof(p->p_osrel); 2497f1fca82eSMikolaj Golub if (sb != NULL) { 2498f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2499f1fca82eSMikolaj Golub structsize = sizeof(p->p_osrel); 2500f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2501f1fca82eSMikolaj Golub sbuf_bcat(sb, &p->p_osrel, sizeof(p->p_osrel)); 2502f1fca82eSMikolaj Golub } 2503f1fca82eSMikolaj Golub *sizep = size; 2504f1fca82eSMikolaj Golub } 2505f1fca82eSMikolaj Golub 2506f1fca82eSMikolaj Golub static void 2507f1fca82eSMikolaj Golub __elfN(note_procstat_psstrings)(void *arg, struct sbuf *sb, size_t *sizep) 2508f1fca82eSMikolaj Golub { 2509f1fca82eSMikolaj Golub struct proc *p; 2510f1fca82eSMikolaj Golub elf_ps_strings_t ps_strings; 2511f1fca82eSMikolaj Golub size_t size; 2512f1fca82eSMikolaj Golub int structsize; 2513f1fca82eSMikolaj Golub 2514f1fca82eSMikolaj Golub p = (struct proc *)arg; 2515f1fca82eSMikolaj Golub size = sizeof(structsize) + sizeof(ps_strings); 2516f1fca82eSMikolaj Golub if (sb != NULL) { 2517f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2518f1fca82eSMikolaj Golub structsize = sizeof(ps_strings); 2519f1fca82eSMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 2520f1fca82eSMikolaj Golub ps_strings = PTROUT(p->p_sysent->sv_psstrings); 2521f1fca82eSMikolaj Golub #else 2522f1fca82eSMikolaj Golub ps_strings = p->p_sysent->sv_psstrings; 2523f1fca82eSMikolaj Golub #endif 2524f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2525f1fca82eSMikolaj Golub sbuf_bcat(sb, &ps_strings, sizeof(ps_strings)); 2526f1fca82eSMikolaj Golub } 2527f1fca82eSMikolaj Golub *sizep = size; 2528f1fca82eSMikolaj Golub } 2529f1fca82eSMikolaj Golub 2530f1fca82eSMikolaj Golub static void 2531f1fca82eSMikolaj Golub __elfN(note_procstat_auxv)(void *arg, struct sbuf *sb, size_t *sizep) 2532f1fca82eSMikolaj Golub { 2533f1fca82eSMikolaj Golub struct proc *p; 2534f1fca82eSMikolaj Golub size_t size; 2535f1fca82eSMikolaj Golub int structsize; 2536f1fca82eSMikolaj Golub 2537f1fca82eSMikolaj Golub p = (struct proc *)arg; 2538f1fca82eSMikolaj Golub if (sb == NULL) { 2539f1fca82eSMikolaj Golub size = 0; 2540f1fca82eSMikolaj Golub sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN); 25415c32e9fcSAlexander Motin sbuf_set_drain(sb, sbuf_count_drain, &size); 2542f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2543f1fca82eSMikolaj Golub PHOLD(p); 2544f1fca82eSMikolaj Golub proc_getauxv(curthread, p, sb); 2545f1fca82eSMikolaj Golub PRELE(p); 2546f1fca82eSMikolaj Golub sbuf_finish(sb); 2547f1fca82eSMikolaj Golub sbuf_delete(sb); 2548f1fca82eSMikolaj Golub *sizep = size; 2549f1fca82eSMikolaj Golub } else { 2550f1fca82eSMikolaj Golub structsize = sizeof(Elf_Auxinfo); 2551f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2552f1fca82eSMikolaj Golub PHOLD(p); 2553f1fca82eSMikolaj Golub proc_getauxv(curthread, p, sb); 2554f1fca82eSMikolaj Golub PRELE(p); 2555f1fca82eSMikolaj Golub } 2556f1fca82eSMikolaj Golub } 2557f1fca82eSMikolaj Golub 255832c01de2SDmitry Chagin static boolean_t 255992328a32SKonstantin Belousov __elfN(parse_notes)(struct image_params *imgp, Elf_Note *checknote, 256092328a32SKonstantin Belousov const char *note_vendor, const Elf_Phdr *pnote, 256192328a32SKonstantin Belousov boolean_t (*cb)(const Elf_Note *, void *, boolean_t *), void *cb_arg) 256232c01de2SDmitry Chagin { 2563267c52fcSKonstantin Belousov const Elf_Note *note, *note0, *note_end; 256432c01de2SDmitry Chagin const char *note_name; 25656c775eb6SKonstantin Belousov char *buf; 25666c775eb6SKonstantin Belousov int i, error; 25676c775eb6SKonstantin Belousov boolean_t res; 256832c01de2SDmitry Chagin 25696c775eb6SKonstantin Belousov /* We need some limit, might as well use PAGE_SIZE. */ 25706c775eb6SKonstantin Belousov if (pnote == NULL || pnote->p_filesz > PAGE_SIZE) 257132c01de2SDmitry Chagin return (FALSE); 25726c775eb6SKonstantin Belousov ASSERT_VOP_LOCKED(imgp->vp, "parse_notes"); 25736c775eb6SKonstantin Belousov if (pnote->p_offset > PAGE_SIZE || 25746c775eb6SKonstantin Belousov pnote->p_filesz > PAGE_SIZE - pnote->p_offset) { 25752d6b8546SKonstantin Belousov buf = malloc(pnote->p_filesz, M_TEMP, M_NOWAIT); 25762d6b8546SKonstantin Belousov if (buf == NULL) { 2577b249ce48SMateusz Guzik VOP_UNLOCK(imgp->vp); 25786c775eb6SKonstantin Belousov buf = malloc(pnote->p_filesz, M_TEMP, M_WAITOK); 257978022527SKonstantin Belousov vn_lock(imgp->vp, LK_SHARED | LK_RETRY); 25802d6b8546SKonstantin Belousov } 25816c775eb6SKonstantin Belousov error = vn_rdwr(UIO_READ, imgp->vp, buf, pnote->p_filesz, 25826c775eb6SKonstantin Belousov pnote->p_offset, UIO_SYSSPACE, IO_NODELOCKED, 25836c775eb6SKonstantin Belousov curthread->td_ucred, NOCRED, NULL, curthread); 25846c775eb6SKonstantin Belousov if (error != 0) { 25856c775eb6SKonstantin Belousov uprintf("i/o error PT_NOTE\n"); 2586eda8fe63SKonstantin Belousov goto retf; 25876c775eb6SKonstantin Belousov } 25886c775eb6SKonstantin Belousov note = note0 = (const Elf_Note *)buf; 25896c775eb6SKonstantin Belousov note_end = (const Elf_Note *)(buf + pnote->p_filesz); 25906c775eb6SKonstantin Belousov } else { 25916c775eb6SKonstantin Belousov note = note0 = (const Elf_Note *)(imgp->image_header + 25926c775eb6SKonstantin Belousov pnote->p_offset); 259332c01de2SDmitry Chagin note_end = (const Elf_Note *)(imgp->image_header + 259432c01de2SDmitry Chagin pnote->p_offset + pnote->p_filesz); 25956c775eb6SKonstantin Belousov buf = NULL; 25966c775eb6SKonstantin Belousov } 2597267c52fcSKonstantin Belousov for (i = 0; i < 100 && note >= note0 && note < note_end; i++) { 2598d1ae5c83SKonstantin Belousov if (!aligned(note, Elf32_Addr) || (const char *)note_end - 25996c775eb6SKonstantin Belousov (const char *)note < sizeof(Elf_Note)) { 2600eda8fe63SKonstantin Belousov goto retf; 26016c775eb6SKonstantin Belousov } 260292328a32SKonstantin Belousov if (note->n_namesz != checknote->n_namesz || 260392328a32SKonstantin Belousov note->n_descsz != checknote->n_descsz || 260492328a32SKonstantin Belousov note->n_type != checknote->n_type) 260532c01de2SDmitry Chagin goto nextnote; 260632c01de2SDmitry Chagin note_name = (const char *)(note + 1); 260792328a32SKonstantin Belousov if (note_name + checknote->n_namesz >= 260892328a32SKonstantin Belousov (const char *)note_end || strncmp(note_vendor, 260992328a32SKonstantin Belousov note_name, checknote->n_namesz) != 0) 261032c01de2SDmitry Chagin goto nextnote; 261132c01de2SDmitry Chagin 261292328a32SKonstantin Belousov if (cb(note, cb_arg, &res)) 26136c775eb6SKonstantin Belousov goto ret; 261432c01de2SDmitry Chagin nextnote: 261532c01de2SDmitry Chagin note = (const Elf_Note *)((const char *)(note + 1) + 26161b8388cdSMikolaj Golub roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) + 26171b8388cdSMikolaj Golub roundup2(note->n_descsz, ELF_NOTE_ROUNDSIZE)); 261832c01de2SDmitry Chagin } 2619eda8fe63SKonstantin Belousov retf: 26206c775eb6SKonstantin Belousov res = FALSE; 26216c775eb6SKonstantin Belousov ret: 26226c775eb6SKonstantin Belousov free(buf, M_TEMP); 26236c775eb6SKonstantin Belousov return (res); 262432c01de2SDmitry Chagin } 262532c01de2SDmitry Chagin 262692328a32SKonstantin Belousov struct brandnote_cb_arg { 262792328a32SKonstantin Belousov Elf_Brandnote *brandnote; 262892328a32SKonstantin Belousov int32_t *osrel; 262992328a32SKonstantin Belousov }; 263092328a32SKonstantin Belousov 263192328a32SKonstantin Belousov static boolean_t 263292328a32SKonstantin Belousov brandnote_cb(const Elf_Note *note, void *arg0, boolean_t *res) 263392328a32SKonstantin Belousov { 263492328a32SKonstantin Belousov struct brandnote_cb_arg *arg; 263592328a32SKonstantin Belousov 263692328a32SKonstantin Belousov arg = arg0; 263792328a32SKonstantin Belousov 263892328a32SKonstantin Belousov /* 263992328a32SKonstantin Belousov * Fetch the osreldate for binary from the ELF OSABI-note if 264092328a32SKonstantin Belousov * necessary. 264192328a32SKonstantin Belousov */ 264292328a32SKonstantin Belousov *res = (arg->brandnote->flags & BN_TRANSLATE_OSREL) != 0 && 264392328a32SKonstantin Belousov arg->brandnote->trans_osrel != NULL ? 264492328a32SKonstantin Belousov arg->brandnote->trans_osrel(note, arg->osrel) : TRUE; 264592328a32SKonstantin Belousov 264692328a32SKonstantin Belousov return (TRUE); 264792328a32SKonstantin Belousov } 264892328a32SKonstantin Belousov 2649cefb93f2SKonstantin Belousov static Elf_Note fctl_note = { 2650cefb93f2SKonstantin Belousov .n_namesz = sizeof(FREEBSD_ABI_VENDOR), 2651cefb93f2SKonstantin Belousov .n_descsz = sizeof(uint32_t), 2652cefb93f2SKonstantin Belousov .n_type = NT_FREEBSD_FEATURE_CTL, 2653cefb93f2SKonstantin Belousov }; 2654cefb93f2SKonstantin Belousov 2655cefb93f2SKonstantin Belousov struct fctl_cb_arg { 2656cefb93f2SKonstantin Belousov uint32_t *fctl0; 2657cefb93f2SKonstantin Belousov }; 2658cefb93f2SKonstantin Belousov 2659cefb93f2SKonstantin Belousov static boolean_t 2660cefb93f2SKonstantin Belousov note_fctl_cb(const Elf_Note *note, void *arg0, boolean_t *res) 2661cefb93f2SKonstantin Belousov { 2662cefb93f2SKonstantin Belousov struct fctl_cb_arg *arg; 2663cefb93f2SKonstantin Belousov const Elf32_Word *desc; 2664cefb93f2SKonstantin Belousov uintptr_t p; 2665cefb93f2SKonstantin Belousov 2666cefb93f2SKonstantin Belousov arg = arg0; 2667cefb93f2SKonstantin Belousov p = (uintptr_t)(note + 1); 2668cefb93f2SKonstantin Belousov p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE); 2669cefb93f2SKonstantin Belousov desc = (const Elf32_Word *)p; 2670cefb93f2SKonstantin Belousov *arg->fctl0 = desc[0]; 2671cefb93f2SKonstantin Belousov return (TRUE); 2672cefb93f2SKonstantin Belousov } 2673cefb93f2SKonstantin Belousov 267432c01de2SDmitry Chagin /* 2675cefb93f2SKonstantin Belousov * Try to find the appropriate ABI-note section for checknote, fetch 2676cefb93f2SKonstantin Belousov * the osreldate and feature control flags for binary from the ELF 2677cefb93f2SKonstantin Belousov * OSABI-note. Only the first page of the image is searched, the same 2678cefb93f2SKonstantin Belousov * as for headers. 26791a9c7decSKonstantin Belousov */ 26801a9c7decSKonstantin Belousov static boolean_t 268192328a32SKonstantin Belousov __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *brandnote, 2682cefb93f2SKonstantin Belousov int32_t *osrel, uint32_t *fctl0) 26831a9c7decSKonstantin Belousov { 26841a9c7decSKonstantin Belousov const Elf_Phdr *phdr; 26851a9c7decSKonstantin Belousov const Elf_Ehdr *hdr; 268692328a32SKonstantin Belousov struct brandnote_cb_arg b_arg; 2687cefb93f2SKonstantin Belousov struct fctl_cb_arg f_arg; 2688cefb93f2SKonstantin Belousov int i, j; 26891a9c7decSKonstantin Belousov 26901a9c7decSKonstantin Belousov hdr = (const Elf_Ehdr *)imgp->image_header; 26911a9c7decSKonstantin Belousov phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 269292328a32SKonstantin Belousov b_arg.brandnote = brandnote; 269392328a32SKonstantin Belousov b_arg.osrel = osrel; 2694cefb93f2SKonstantin Belousov f_arg.fctl0 = fctl0; 26951a9c7decSKonstantin Belousov 26961a9c7decSKonstantin Belousov for (i = 0; i < hdr->e_phnum; i++) { 269792328a32SKonstantin Belousov if (phdr[i].p_type == PT_NOTE && __elfN(parse_notes)(imgp, 269892328a32SKonstantin Belousov &brandnote->hdr, brandnote->vendor, &phdr[i], brandnote_cb, 269992328a32SKonstantin Belousov &b_arg)) { 2700cefb93f2SKonstantin Belousov for (j = 0; j < hdr->e_phnum; j++) { 2701cefb93f2SKonstantin Belousov if (phdr[j].p_type == PT_NOTE && 2702cefb93f2SKonstantin Belousov __elfN(parse_notes)(imgp, &fctl_note, 2703cefb93f2SKonstantin Belousov FREEBSD_ABI_VENDOR, &phdr[j], 2704cefb93f2SKonstantin Belousov note_fctl_cb, &f_arg)) 2705cefb93f2SKonstantin Belousov break; 2706cefb93f2SKonstantin Belousov } 27071a9c7decSKonstantin Belousov return (TRUE); 27081a9c7decSKonstantin Belousov } 270992328a32SKonstantin Belousov } 27101a9c7decSKonstantin Belousov return (FALSE); 27111a9c7decSKonstantin Belousov 27121a9c7decSKonstantin Belousov } 27131a9c7decSKonstantin Belousov 27141a9c7decSKonstantin Belousov /* 2715e1743d02SSøren Schmidt * Tell kern_execve.c about it, with a little help from the linker. 2716e1743d02SSøren Schmidt */ 2717a360a43dSJake Burkholder static struct execsw __elfN(execsw) = { 2718b7feabf9SEd Maste .ex_imgact = __CONCAT(exec_, __elfN(imgact)), 2719b7feabf9SEd Maste .ex_name = __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) 2720a360a43dSJake Burkholder }; 2721a360a43dSJake Burkholder EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw)); 2722e7228204SAlfred Perlstein 2723ed167eaaSKonstantin Belousov static vm_prot_t 2724ed167eaaSKonstantin Belousov __elfN(trans_prot)(Elf_Word flags) 2725ed167eaaSKonstantin Belousov { 2726ed167eaaSKonstantin Belousov vm_prot_t prot; 2727ed167eaaSKonstantin Belousov 2728ed167eaaSKonstantin Belousov prot = 0; 2729ed167eaaSKonstantin Belousov if (flags & PF_X) 2730ed167eaaSKonstantin Belousov prot |= VM_PROT_EXECUTE; 2731ed167eaaSKonstantin Belousov if (flags & PF_W) 2732ed167eaaSKonstantin Belousov prot |= VM_PROT_WRITE; 2733ed167eaaSKonstantin Belousov if (flags & PF_R) 2734ed167eaaSKonstantin Belousov prot |= VM_PROT_READ; 2735eb785fabSKonstantin Belousov #if __ELF_WORD_SIZE == 32 && (defined(__amd64__) || defined(__i386__)) 2736126b36a2SKonstantin Belousov if (i386_read_exec && (flags & PF_R)) 2737676eda08SMarcel Moolenaar prot |= VM_PROT_EXECUTE; 2738676eda08SMarcel Moolenaar #endif 2739ed167eaaSKonstantin Belousov return (prot); 2740ed167eaaSKonstantin Belousov } 2741ed167eaaSKonstantin Belousov 2742ed167eaaSKonstantin Belousov static Elf_Word 2743ed167eaaSKonstantin Belousov __elfN(untrans_prot)(vm_prot_t prot) 2744ed167eaaSKonstantin Belousov { 2745ed167eaaSKonstantin Belousov Elf_Word flags; 2746ed167eaaSKonstantin Belousov 2747ed167eaaSKonstantin Belousov flags = 0; 2748ed167eaaSKonstantin Belousov if (prot & VM_PROT_EXECUTE) 2749ed167eaaSKonstantin Belousov flags |= PF_X; 2750ed167eaaSKonstantin Belousov if (prot & VM_PROT_READ) 2751ed167eaaSKonstantin Belousov flags |= PF_R; 2752ed167eaaSKonstantin Belousov if (prot & VM_PROT_WRITE) 2753ed167eaaSKonstantin Belousov flags |= PF_W; 2754ed167eaaSKonstantin Belousov return (flags); 2755ed167eaaSKonstantin Belousov } 2756fc83c5a7SKonstantin Belousov 2757fc83c5a7SKonstantin Belousov void 275831174518SJohn Baldwin __elfN(stackgap)(struct image_params *imgp, uintptr_t *stack_base) 2759fc83c5a7SKonstantin Belousov { 276031174518SJohn Baldwin uintptr_t range, rbase, gap; 2761fc83c5a7SKonstantin Belousov int pct; 2762fc83c5a7SKonstantin Belousov 2763fc83c5a7SKonstantin Belousov if ((imgp->map_flags & MAP_ASLR) == 0) 2764fc83c5a7SKonstantin Belousov return; 2765fc83c5a7SKonstantin Belousov pct = __elfN(aslr_stack_gap); 2766fc83c5a7SKonstantin Belousov if (pct == 0) 2767fc83c5a7SKonstantin Belousov return; 2768fc83c5a7SKonstantin Belousov if (pct > 50) 2769fc83c5a7SKonstantin Belousov pct = 50; 2770fc83c5a7SKonstantin Belousov range = imgp->eff_stack_sz * pct / 100; 2771fc83c5a7SKonstantin Belousov arc4rand(&rbase, sizeof(rbase), 0); 2772fc83c5a7SKonstantin Belousov gap = rbase % range; 2773fc83c5a7SKonstantin Belousov gap &= ~(sizeof(u_long) - 1); 2774fc83c5a7SKonstantin Belousov *stack_base -= gap; 2775fc83c5a7SKonstantin Belousov } 2776