1e1743d02SSøren Schmidt /*- 221a3ee0eSDavid E. O'Brien * Copyright (c) 2000 David O'Brien 39a14aa01SUlrich Spörlein * Copyright (c) 1995-1996 Søren Schmidt 4e1743d02SSøren Schmidt * Copyright (c) 1996 Peter Wemm 5e1743d02SSøren Schmidt * All rights reserved. 6e1743d02SSøren Schmidt * 7e1743d02SSøren Schmidt * Redistribution and use in source and binary forms, with or without 8e1743d02SSøren Schmidt * modification, are permitted provided that the following conditions 9e1743d02SSøren Schmidt * are met: 10e1743d02SSøren Schmidt * 1. Redistributions of source code must retain the above copyright 11e1743d02SSøren Schmidt * notice, this list of conditions and the following disclaimer 12e1743d02SSøren Schmidt * in this position and unchanged. 13e1743d02SSøren Schmidt * 2. Redistributions in binary form must reproduce the above copyright 14e1743d02SSøren Schmidt * notice, this list of conditions and the following disclaimer in the 15e1743d02SSøren Schmidt * documentation and/or other materials provided with the distribution. 16e1743d02SSøren Schmidt * 3. The name of the author may not be used to endorse or promote products 1721dc7d4fSJens Schweikhardt * derived from this software without specific prior written permission 18e1743d02SSøren Schmidt * 19e1743d02SSøren Schmidt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20e1743d02SSøren Schmidt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21e1743d02SSøren Schmidt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22e1743d02SSøren Schmidt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23e1743d02SSøren Schmidt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24e1743d02SSøren Schmidt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25e1743d02SSøren Schmidt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26e1743d02SSøren Schmidt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27e1743d02SSøren Schmidt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28e1743d02SSøren Schmidt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29e1743d02SSøren Schmidt */ 30e1743d02SSøren Schmidt 31677b542eSDavid E. O'Brien #include <sys/cdefs.h> 32677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 33677b542eSDavid E. O'Brien 3412bc222eSJonathan Anderson #include "opt_capsicum.h" 3562919d78SPeter Wemm #include "opt_compat.h" 36aa14e9b7SMark Johnston #include "opt_gzio.h" 3762919d78SPeter Wemm 38e1743d02SSøren Schmidt #include <sys/param.h> 394a144410SRobert Watson #include <sys/capsicum.h> 40e1743d02SSøren Schmidt #include <sys/exec.h> 418c64af4fSJohn Polstra #include <sys/fcntl.h> 42aa14e9b7SMark Johnston #include <sys/gzio.h> 43e1743d02SSøren Schmidt #include <sys/imgact.h> 44e1743d02SSøren Schmidt #include <sys/imgact_elf.h> 45b96bd95bSIan Lepore #include <sys/jail.h> 46e1743d02SSøren Schmidt #include <sys/kernel.h> 47f34fa851SJohn Baldwin #include <sys/lock.h> 48e1743d02SSøren Schmidt #include <sys/malloc.h> 4968ff2a43SChristian S.J. Peron #include <sys/mount.h> 508c64af4fSJohn Polstra #include <sys/mman.h> 51a794e791SBruce Evans #include <sys/namei.h> 528c64af4fSJohn Polstra #include <sys/pioctl.h> 53a794e791SBruce Evans #include <sys/proc.h> 548c64af4fSJohn Polstra #include <sys/procfs.h> 551ba5ad42SEdward Tomasz Napierala #include <sys/racct.h> 568c64af4fSJohn Polstra #include <sys/resourcevar.h> 5789f6b863SAttilio Rao #include <sys/rwlock.h> 58bd390213SMikolaj Golub #include <sys/sbuf.h> 59da61b9a6SAlan Cox #include <sys/sf_buf.h> 60ee235befSKonstantin Belousov #include <sys/smp.h> 6136240ea5SDoug Rabson #include <sys/systm.h> 62e1743d02SSøren Schmidt #include <sys/signalvar.h> 638c64af4fSJohn Polstra #include <sys/stat.h> 641005a129SJohn Baldwin #include <sys/sx.h> 658c64af4fSJohn Polstra #include <sys/syscall.h> 66e1743d02SSøren Schmidt #include <sys/sysctl.h> 678c64af4fSJohn Polstra #include <sys/sysent.h> 68a794e791SBruce Evans #include <sys/vnode.h> 69e7228204SAlfred Perlstein #include <sys/syslog.h> 70e7228204SAlfred Perlstein #include <sys/eventhandler.h> 71f1fca82eSMikolaj Golub #include <sys/user.h> 72e7228204SAlfred Perlstein 73e1743d02SSøren Schmidt #include <vm/vm.h> 74e1743d02SSøren Schmidt #include <vm/vm_kern.h> 75e1743d02SSøren Schmidt #include <vm/vm_param.h> 76e1743d02SSøren Schmidt #include <vm/pmap.h> 77e1743d02SSøren Schmidt #include <vm/vm_map.h> 780ff27d31SJohn Polstra #include <vm/vm_object.h> 79e1743d02SSøren Schmidt #include <vm/vm_extern.h> 80e1743d02SSøren Schmidt 8152c24af7SPeter Wemm #include <machine/elf.h> 82e1743d02SSøren Schmidt #include <machine/md_var.h> 834d22d07aSKonstantin Belousov #ifdef __arm__ 844d22d07aSKonstantin Belousov #include <machine/acle-compat.h> 854d22d07aSKonstantin Belousov #endif 86e1743d02SSøren Schmidt 871b8388cdSMikolaj Golub #define ELF_NOTE_ROUNDSIZE 4 88c815a20cSDavid E. O'Brien #define OLD_EI_BRAND 8 89c815a20cSDavid E. O'Brien 903ebc1248SPeter Wemm static int __elfN(check_header)(const Elf_Ehdr *hdr); 9132c01de2SDmitry Chagin static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp, 92d1ae5c83SKonstantin Belousov const char *interp, int interp_name_len, int32_t *osrel); 933ebc1248SPeter Wemm static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr, 943ebc1248SPeter Wemm u_long *entry, size_t pagesize); 95292177e6SAlan Cox static int __elfN(load_section)(struct image_params *imgp, vm_offset_t offset, 96292177e6SAlan Cox caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot, 97292177e6SAlan Cox size_t pagesize); 983ebc1248SPeter Wemm static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp); 9989ffc202SBjoern A. Zeeb static boolean_t __elfN(freebsd_trans_osrel)(const Elf_Note *note, 10089ffc202SBjoern A. Zeeb int32_t *osrel); 10189ffc202SBjoern A. Zeeb static boolean_t kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel); 10232c01de2SDmitry Chagin static boolean_t __elfN(check_note)(struct image_params *imgp, 10332c01de2SDmitry Chagin Elf_Brandnote *checknote, int32_t *osrel); 104ed167eaaSKonstantin Belousov static vm_prot_t __elfN(trans_prot)(Elf_Word); 105ed167eaaSKonstantin Belousov static Elf_Word __elfN(untrans_prot)(vm_prot_t); 106e1743d02SSøren Schmidt 107a360a43dSJake Burkholder SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0, 108a360a43dSJake Burkholder ""); 109a360a43dSJake Burkholder 110bd390213SMikolaj Golub #define CORE_BUF_SIZE (16 * 1024) 111e7228204SAlfred Perlstein 112e548a1d4SJake Burkholder int __elfN(fallback_brand) = -1; 113e548a1d4SJake Burkholder SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, 114af3b2549SHans Petter Selasky fallback_brand, CTLFLAG_RWTUN, &__elfN(fallback_brand), 0, 115a360a43dSJake Burkholder __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort"); 116a360a43dSJake Burkholder 117551d79e1SMarcel Moolenaar static int elf_legacy_coredump = 0; 118a360a43dSJake Burkholder SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW, 119551d79e1SMarcel Moolenaar &elf_legacy_coredump, 0, ""); 120e1743d02SSøren Schmidt 12162c625fdSKonstantin Belousov int __elfN(nxstack) = 1224d22d07aSKonstantin Belousov #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */ || \ 1234d22d07aSKonstantin Belousov (defined(__arm__) && __ARM_ARCH >= 7) || defined(__aarch64__) 12462c625fdSKonstantin Belousov 1; 12562c625fdSKonstantin Belousov #else 12662c625fdSKonstantin Belousov 0; 12762c625fdSKonstantin Belousov #endif 128291c06a1SKonstantin Belousov SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, 129291c06a1SKonstantin Belousov nxstack, CTLFLAG_RW, &__elfN(nxstack), 0, 130291c06a1SKonstantin Belousov __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack"); 131291c06a1SKonstantin Belousov 132126b36a2SKonstantin Belousov #if __ELF_WORD_SIZE == 32 133e7d939bdSMarcel Moolenaar #if defined(__amd64__) 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 #endif 139126b36a2SKonstantin Belousov 1403ebc1248SPeter Wemm static Elf_Brandinfo *elf_brand_list[MAX_BRANDS]; 141e1743d02SSøren Schmidt 14293d1c728SKonstantin Belousov #define trunc_page_ps(va, ps) ((va) & ~(ps - 1)) 14393d1c728SKonstantin Belousov #define round_page_ps(va, ps) (((va) + (ps - 1)) & ~(ps - 1)) 14493d1c728SKonstantin Belousov #define aligned(a, t) (trunc_page_ps((u_long)(a), sizeof(t)) == (u_long)(a)) 14593d1c728SKonstantin Belousov 14632c01de2SDmitry Chagin static const char FREEBSD_ABI_VENDOR[] = "FreeBSD"; 14732c01de2SDmitry Chagin 14832c01de2SDmitry Chagin Elf_Brandnote __elfN(freebsd_brandnote) = { 14932c01de2SDmitry Chagin .hdr.n_namesz = sizeof(FREEBSD_ABI_VENDOR), 15032c01de2SDmitry Chagin .hdr.n_descsz = sizeof(int32_t), 1514c22b468SEd Maste .hdr.n_type = NT_FREEBSD_ABI_TAG, 15232c01de2SDmitry Chagin .vendor = FREEBSD_ABI_VENDOR, 15389ffc202SBjoern A. Zeeb .flags = BN_TRANSLATE_OSREL, 15489ffc202SBjoern A. Zeeb .trans_osrel = __elfN(freebsd_trans_osrel) 15532c01de2SDmitry Chagin }; 15632c01de2SDmitry Chagin 15789ffc202SBjoern A. Zeeb static boolean_t 15889ffc202SBjoern A. Zeeb __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel) 15989ffc202SBjoern A. Zeeb { 16089ffc202SBjoern A. Zeeb uintptr_t p; 16189ffc202SBjoern A. Zeeb 16289ffc202SBjoern A. Zeeb p = (uintptr_t)(note + 1); 1631b8388cdSMikolaj Golub p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE); 16489ffc202SBjoern A. Zeeb *osrel = *(const int32_t *)(p); 16589ffc202SBjoern A. Zeeb 16689ffc202SBjoern A. Zeeb return (TRUE); 16789ffc202SBjoern A. Zeeb } 16889ffc202SBjoern A. Zeeb 16989ffc202SBjoern A. Zeeb static const char GNU_ABI_VENDOR[] = "GNU"; 17089ffc202SBjoern A. Zeeb static int GNU_KFREEBSD_ABI_DESC = 3; 17189ffc202SBjoern A. Zeeb 17289ffc202SBjoern A. Zeeb Elf_Brandnote __elfN(kfreebsd_brandnote) = { 17389ffc202SBjoern A. Zeeb .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 17489ffc202SBjoern A. Zeeb .hdr.n_descsz = 16, /* XXX at least 16 */ 17589ffc202SBjoern A. Zeeb .hdr.n_type = 1, 17689ffc202SBjoern A. Zeeb .vendor = GNU_ABI_VENDOR, 17789ffc202SBjoern A. Zeeb .flags = BN_TRANSLATE_OSREL, 17889ffc202SBjoern A. Zeeb .trans_osrel = kfreebsd_trans_osrel 17989ffc202SBjoern A. Zeeb }; 18089ffc202SBjoern A. Zeeb 18189ffc202SBjoern A. Zeeb static boolean_t 18289ffc202SBjoern A. Zeeb kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel) 18389ffc202SBjoern A. Zeeb { 18489ffc202SBjoern A. Zeeb const Elf32_Word *desc; 18589ffc202SBjoern A. Zeeb uintptr_t p; 18689ffc202SBjoern A. Zeeb 18789ffc202SBjoern A. Zeeb p = (uintptr_t)(note + 1); 1881b8388cdSMikolaj Golub p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE); 18989ffc202SBjoern A. Zeeb 19089ffc202SBjoern A. Zeeb desc = (const Elf32_Word *)p; 19189ffc202SBjoern A. Zeeb if (desc[0] != GNU_KFREEBSD_ABI_DESC) 19289ffc202SBjoern A. Zeeb return (FALSE); 19389ffc202SBjoern A. Zeeb 19489ffc202SBjoern A. Zeeb /* 19589ffc202SBjoern A. Zeeb * Debian GNU/kFreeBSD embed the earliest compatible kernel version 19689ffc202SBjoern A. Zeeb * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way. 19789ffc202SBjoern A. Zeeb */ 19889ffc202SBjoern A. Zeeb *osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3]; 19989ffc202SBjoern A. Zeeb 20089ffc202SBjoern A. Zeeb return (TRUE); 20189ffc202SBjoern A. Zeeb } 20289ffc202SBjoern A. Zeeb 203e1743d02SSøren Schmidt int 2043ebc1248SPeter Wemm __elfN(insert_brand_entry)(Elf_Brandinfo *entry) 205e1743d02SSøren Schmidt { 206e1743d02SSøren Schmidt int i; 207e1743d02SSøren Schmidt 2083ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 209ea5a2b2eSSøren Schmidt if (elf_brand_list[i] == NULL) { 210ea5a2b2eSSøren Schmidt elf_brand_list[i] = entry; 211e1743d02SSøren Schmidt break; 212e1743d02SSøren Schmidt } 213e1743d02SSøren Schmidt } 214925c8b5bSBjoern A. Zeeb if (i == MAX_BRANDS) { 215925c8b5bSBjoern A. Zeeb printf("WARNING: %s: could not insert brandinfo entry: %p\n", 216925c8b5bSBjoern A. Zeeb __func__, entry); 217a7cddfedSJake Burkholder return (-1); 218925c8b5bSBjoern A. Zeeb } 219a7cddfedSJake Burkholder return (0); 220e1743d02SSøren Schmidt } 221e1743d02SSøren Schmidt 222e1743d02SSøren Schmidt int 2233ebc1248SPeter Wemm __elfN(remove_brand_entry)(Elf_Brandinfo *entry) 224e1743d02SSøren Schmidt { 225e1743d02SSøren Schmidt int i; 226e1743d02SSøren Schmidt 2273ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 228ea5a2b2eSSøren Schmidt if (elf_brand_list[i] == entry) { 229ea5a2b2eSSøren Schmidt elf_brand_list[i] = NULL; 230e1743d02SSøren Schmidt break; 231e1743d02SSøren Schmidt } 232e1743d02SSøren Schmidt } 233ea5a2b2eSSøren Schmidt if (i == MAX_BRANDS) 234a7cddfedSJake Burkholder return (-1); 235a7cddfedSJake Burkholder return (0); 236e1743d02SSøren Schmidt } 237e1743d02SSøren Schmidt 238096977faSMark Newton int 2393ebc1248SPeter Wemm __elfN(brand_inuse)(Elf_Brandinfo *entry) 240096977faSMark Newton { 241096977faSMark Newton struct proc *p; 242553629ebSJake Burkholder int rval = FALSE; 243096977faSMark Newton 2441005a129SJohn Baldwin sx_slock(&allproc_lock); 2454f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) { 246553629ebSJake Burkholder if (p->p_sysent == entry->sysvec) { 247553629ebSJake Burkholder rval = TRUE; 248553629ebSJake Burkholder break; 249096977faSMark Newton } 250553629ebSJake Burkholder } 2511005a129SJohn Baldwin sx_sunlock(&allproc_lock); 252096977faSMark Newton 253553629ebSJake Burkholder return (rval); 254096977faSMark Newton } 255096977faSMark Newton 2565fe3ed62SJake Burkholder static Elf_Brandinfo * 25732c01de2SDmitry Chagin __elfN(get_brandinfo)(struct image_params *imgp, const char *interp, 258d1ae5c83SKonstantin Belousov int interp_name_len, int32_t *osrel) 2595fe3ed62SJake Burkholder { 26032c01de2SDmitry Chagin const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header; 2615fe3ed62SJake Burkholder Elf_Brandinfo *bi; 26232c01de2SDmitry Chagin boolean_t ret; 2635fe3ed62SJake Burkholder int i; 2645fe3ed62SJake Burkholder 2655fe3ed62SJake Burkholder /* 26632c01de2SDmitry Chagin * We support four types of branding -- (1) the ELF EI_OSABI field 2675fe3ed62SJake Burkholder * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string 26832c01de2SDmitry Chagin * branding w/in the ELF header, (3) path of the `interp_path' 26932c01de2SDmitry Chagin * field, and (4) the ".note.ABI-tag" ELF section. 2705fe3ed62SJake Burkholder */ 2715fe3ed62SJake Burkholder 27232c01de2SDmitry Chagin /* Look for an ".note.ABI-tag" ELF section */ 27332c01de2SDmitry Chagin for (i = 0; i < MAX_BRANDS; i++) { 27432c01de2SDmitry Chagin bi = elf_brand_list[i]; 275ecc2fda8SBjoern A. Zeeb if (bi == NULL) 276ecc2fda8SBjoern A. Zeeb continue; 277ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && (bi->flags & 278ecc2fda8SBjoern A. Zeeb (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) { 27932c01de2SDmitry Chagin ret = __elfN(check_note)(imgp, bi->brand_note, osrel); 280f19d421aSNathan Whitehorn /* Give brand a chance to veto check_note's guess */ 281f19d421aSNathan Whitehorn if (ret && bi->header_supported) 282f19d421aSNathan Whitehorn ret = bi->header_supported(imgp); 28332c01de2SDmitry Chagin if (ret) 28432c01de2SDmitry Chagin return (bi); 28532c01de2SDmitry Chagin } 28632c01de2SDmitry Chagin } 28732c01de2SDmitry Chagin 2885fe3ed62SJake Burkholder /* If the executable has a brand, search for it in the brand list. */ 2895fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 2905fe3ed62SJake Burkholder bi = elf_brand_list[i]; 291ecc2fda8SBjoern A. Zeeb if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) 292ecc2fda8SBjoern A. Zeeb continue; 293ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 2945fe3ed62SJake Burkholder (hdr->e_ident[EI_OSABI] == bi->brand || 2955fe3ed62SJake Burkholder strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND], 296686d2f31SNathan Whitehorn bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0)) { 297686d2f31SNathan Whitehorn /* Looks good, but give brand a chance to veto */ 298686d2f31SNathan Whitehorn if (!bi->header_supported || bi->header_supported(imgp)) 2995fe3ed62SJake Burkholder return (bi); 3005fe3ed62SJake Burkholder } 301686d2f31SNathan Whitehorn } 3025fe3ed62SJake Burkholder 303817dc004SWarner Losh /* No known brand, see if the header is recognized by any brand */ 304817dc004SWarner Losh for (i = 0; i < MAX_BRANDS; i++) { 305817dc004SWarner Losh bi = elf_brand_list[i]; 306817dc004SWarner Losh if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY || 307817dc004SWarner Losh bi->header_supported == NULL) 308817dc004SWarner Losh continue; 309817dc004SWarner Losh if (hdr->e_machine == bi->machine) { 310817dc004SWarner Losh ret = bi->header_supported(imgp); 311817dc004SWarner Losh if (ret) 312817dc004SWarner Losh return (bi); 313817dc004SWarner Losh } 314817dc004SWarner Losh } 315817dc004SWarner Losh 3165fe3ed62SJake Burkholder /* Lacking a known brand, search for a recognized interpreter. */ 3175fe3ed62SJake Burkholder if (interp != NULL) { 3185fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 3195fe3ed62SJake Burkholder bi = elf_brand_list[i]; 320ecc2fda8SBjoern A. Zeeb if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) 321ecc2fda8SBjoern A. Zeeb continue; 322ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 323d1ae5c83SKonstantin Belousov /* ELF image p_filesz includes terminating zero */ 324d1ae5c83SKonstantin Belousov strlen(bi->interp_path) + 1 == interp_name_len && 325d1ae5c83SKonstantin Belousov strncmp(interp, bi->interp_path, interp_name_len) 326d1ae5c83SKonstantin Belousov == 0) 3275fe3ed62SJake Burkholder return (bi); 3285fe3ed62SJake Burkholder } 3295fe3ed62SJake Burkholder } 3305fe3ed62SJake Burkholder 3315fe3ed62SJake Burkholder /* Lacking a recognized interpreter, try the default brand */ 3325fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 3335fe3ed62SJake Burkholder bi = elf_brand_list[i]; 334ecc2fda8SBjoern A. Zeeb if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) 335ecc2fda8SBjoern A. Zeeb continue; 336ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 337e548a1d4SJake Burkholder __elfN(fallback_brand) == bi->brand) 3385fe3ed62SJake Burkholder return (bi); 3395fe3ed62SJake Burkholder } 3405fe3ed62SJake Burkholder return (NULL); 3415fe3ed62SJake Burkholder } 3425fe3ed62SJake Burkholder 343e1743d02SSøren Schmidt static int 3443ebc1248SPeter Wemm __elfN(check_header)(const Elf_Ehdr *hdr) 345e1743d02SSøren Schmidt { 346d0ca7c29SPeter Wemm Elf_Brandinfo *bi; 3473ebc1248SPeter Wemm int i; 3483ebc1248SPeter Wemm 34952c24af7SPeter Wemm if (!IS_ELF(*hdr) || 35052c24af7SPeter Wemm hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 35152c24af7SPeter Wemm hdr->e_ident[EI_DATA] != ELF_TARG_DATA || 3523dc19c46SJacques Vidrine hdr->e_ident[EI_VERSION] != EV_CURRENT || 3533dc19c46SJacques Vidrine hdr->e_phentsize != sizeof(Elf_Phdr) || 3543dc19c46SJacques Vidrine hdr->e_version != ELF_TARG_VER) 355a7cddfedSJake Burkholder return (ENOEXEC); 356e1743d02SSøren Schmidt 3573ebc1248SPeter Wemm /* 3583ebc1248SPeter Wemm * Make sure we have at least one brand for this machine. 3593ebc1248SPeter Wemm */ 3603ebc1248SPeter Wemm 3613ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 362d0ca7c29SPeter Wemm bi = elf_brand_list[i]; 363d0ca7c29SPeter Wemm if (bi != NULL && bi->machine == hdr->e_machine) 3643ebc1248SPeter Wemm break; 3653ebc1248SPeter Wemm } 3663ebc1248SPeter Wemm if (i == MAX_BRANDS) 367a7cddfedSJake Burkholder return (ENOEXEC); 368e1743d02SSøren Schmidt 369a7cddfedSJake Burkholder return (0); 370e1743d02SSøren Schmidt } 371e1743d02SSøren Schmidt 372e1743d02SSøren Schmidt static int 3733ebc1248SPeter Wemm __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 374ff6f03c7SAlan Cox vm_offset_t start, vm_offset_t end, vm_prot_t prot) 3753ebc1248SPeter Wemm { 376da61b9a6SAlan Cox struct sf_buf *sf; 377da61b9a6SAlan Cox int error; 3783ebc1248SPeter Wemm vm_offset_t off; 3793ebc1248SPeter Wemm 3803ebc1248SPeter Wemm /* 3813ebc1248SPeter Wemm * Create the page if it doesn't exist yet. Ignore errors. 3823ebc1248SPeter Wemm */ 3833ebc1248SPeter Wemm vm_map_lock(map); 384ff6f03c7SAlan Cox vm_map_insert(map, NULL, 0, trunc_page(start), round_page(end), 385ff6f03c7SAlan Cox VM_PROT_ALL, VM_PROT_ALL, 0); 3863ebc1248SPeter Wemm vm_map_unlock(map); 3873ebc1248SPeter Wemm 3883ebc1248SPeter Wemm /* 3893ebc1248SPeter Wemm * Find the page from the underlying object. 3903ebc1248SPeter Wemm */ 3913ebc1248SPeter Wemm if (object) { 392da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset); 393da61b9a6SAlan Cox if (sf == NULL) 394da61b9a6SAlan Cox return (KERN_FAILURE); 3953ebc1248SPeter Wemm off = offset - trunc_page(offset); 396da61b9a6SAlan Cox error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start, 397ca0387efSJake Burkholder end - start); 398be996836SAttilio Rao vm_imgact_unmap_page(sf); 3993ebc1248SPeter Wemm if (error) { 400a7cddfedSJake Burkholder return (KERN_FAILURE); 4013ebc1248SPeter Wemm } 4023ebc1248SPeter Wemm } 4033ebc1248SPeter Wemm 404a7cddfedSJake Burkholder return (KERN_SUCCESS); 4053ebc1248SPeter Wemm } 4063ebc1248SPeter Wemm 4073ebc1248SPeter Wemm static int 4083ebc1248SPeter Wemm __elfN(map_insert)(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 409ff6f03c7SAlan Cox vm_offset_t start, vm_offset_t end, vm_prot_t prot, int cow) 4103ebc1248SPeter Wemm { 411da61b9a6SAlan Cox struct sf_buf *sf; 412da61b9a6SAlan Cox vm_offset_t off; 413a063facbSMarcel Moolenaar vm_size_t sz; 414a063facbSMarcel Moolenaar int error, rv; 4153ebc1248SPeter Wemm 4163ebc1248SPeter Wemm if (start != trunc_page(start)) { 41781f223caSJake Burkholder rv = __elfN(map_partial)(map, object, offset, start, 418ff6f03c7SAlan Cox round_page(start), prot); 4193ebc1248SPeter Wemm if (rv) 420a7cddfedSJake Burkholder return (rv); 4213ebc1248SPeter Wemm offset += round_page(start) - start; 4223ebc1248SPeter Wemm start = round_page(start); 4233ebc1248SPeter Wemm } 4243ebc1248SPeter Wemm if (end != round_page(end)) { 42581f223caSJake Burkholder rv = __elfN(map_partial)(map, object, offset + 426ff6f03c7SAlan Cox trunc_page(end) - start, trunc_page(end), end, prot); 4273ebc1248SPeter Wemm if (rv) 428a7cddfedSJake Burkholder return (rv); 4293ebc1248SPeter Wemm end = trunc_page(end); 4303ebc1248SPeter Wemm } 4313ebc1248SPeter Wemm if (end > start) { 4323ebc1248SPeter Wemm if (offset & PAGE_MASK) { 4333ebc1248SPeter Wemm /* 4343ebc1248SPeter Wemm * The mapping is not page aligned. This means we have 4353ebc1248SPeter Wemm * to copy the data. Sigh. 4363ebc1248SPeter Wemm */ 437edb572a3SJohn Baldwin rv = vm_map_find(map, NULL, 0, &start, end - start, 0, 438edb572a3SJohn Baldwin VMFS_NO_SPACE, prot | VM_PROT_WRITE, VM_PROT_ALL, 439edb572a3SJohn Baldwin 0); 4403ebc1248SPeter Wemm if (rv) 441a7cddfedSJake Burkholder return (rv); 442da61b9a6SAlan Cox if (object == NULL) 443da61b9a6SAlan Cox return (KERN_SUCCESS); 444da61b9a6SAlan Cox for (; start < end; start += sz) { 445da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset); 446da61b9a6SAlan Cox if (sf == NULL) 447da61b9a6SAlan Cox return (KERN_FAILURE); 4483ebc1248SPeter Wemm off = offset - trunc_page(offset); 4493ebc1248SPeter Wemm sz = end - start; 450da61b9a6SAlan Cox if (sz > PAGE_SIZE - off) 451da61b9a6SAlan Cox sz = PAGE_SIZE - off; 452da61b9a6SAlan Cox error = copyout((caddr_t)sf_buf_kva(sf) + off, 4533ebc1248SPeter Wemm (caddr_t)start, sz); 454be996836SAttilio Rao vm_imgact_unmap_page(sf); 4553ebc1248SPeter Wemm if (error) { 456a7cddfedSJake Burkholder return (KERN_FAILURE); 4573ebc1248SPeter Wemm } 458da61b9a6SAlan Cox offset += sz; 4593ebc1248SPeter Wemm } 4603ebc1248SPeter Wemm rv = KERN_SUCCESS; 4613ebc1248SPeter Wemm } else { 462e5e6093bSAlan Cox vm_object_reference(object); 4633ebc1248SPeter Wemm vm_map_lock(map); 4643ebc1248SPeter Wemm rv = vm_map_insert(map, object, offset, start, end, 465ff6f03c7SAlan Cox prot, VM_PROT_ALL, cow); 4663ebc1248SPeter Wemm vm_map_unlock(map); 467e5e6093bSAlan Cox if (rv != KERN_SUCCESS) 468e5e6093bSAlan Cox vm_object_deallocate(object); 4693ebc1248SPeter Wemm } 470a7cddfedSJake Burkholder return (rv); 4713ebc1248SPeter Wemm } else { 472a7cddfedSJake Burkholder return (KERN_SUCCESS); 4733ebc1248SPeter Wemm } 4743ebc1248SPeter Wemm } 4753ebc1248SPeter Wemm 4763ebc1248SPeter Wemm static int 477292177e6SAlan Cox __elfN(load_section)(struct image_params *imgp, vm_offset_t offset, 4783ebc1248SPeter Wemm caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot, 4793ebc1248SPeter Wemm size_t pagesize) 480e1743d02SSøren Schmidt { 481da61b9a6SAlan Cox struct sf_buf *sf; 482e1743d02SSøren Schmidt size_t map_len; 483292177e6SAlan Cox vm_map_t map; 484292177e6SAlan Cox vm_object_t object; 485e1743d02SSøren Schmidt vm_offset_t map_addr; 486fa7dd9c5SMatthew Dillon int error, rv, cow; 487e1743d02SSøren Schmidt size_t copy_len; 48852c24af7SPeter Wemm vm_offset_t file_addr; 48952c24af7SPeter Wemm 49025ead034SBrian Feldman /* 49125ead034SBrian Feldman * It's necessary to fail if the filsz + offset taken from the 49225ead034SBrian Feldman * header is greater than the actual file pager object's size. 49325ead034SBrian Feldman * If we were to allow this, then the vm_map_find() below would 49425ead034SBrian Feldman * walk right off the end of the file object and into the ether. 49525ead034SBrian Feldman * 49625ead034SBrian Feldman * While I'm here, might as well check for something else that 49725ead034SBrian Feldman * is invalid: filsz cannot be greater than memsz. 49825ead034SBrian Feldman */ 499292177e6SAlan Cox if ((off_t)filsz + offset > imgp->attr->va_size || filsz > memsz) { 50025ead034SBrian Feldman uprintf("elf_load_section: truncated ELF file\n"); 50125ead034SBrian Feldman return (ENOEXEC); 50225ead034SBrian Feldman } 50325ead034SBrian Feldman 504292177e6SAlan Cox object = imgp->object; 505292177e6SAlan Cox map = &imgp->proc->p_vmspace->vm_map; 5063ebc1248SPeter Wemm map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize); 5073ebc1248SPeter Wemm file_addr = trunc_page_ps(offset, pagesize); 508e1743d02SSøren Schmidt 509e1743d02SSøren Schmidt /* 51052c24af7SPeter Wemm * We have two choices. We can either clear the data in the last page 51152c24af7SPeter Wemm * of an oversized mapping, or we can start the anon mapping a page 51252c24af7SPeter Wemm * early and copy the initialized data into that first page. We 51352c24af7SPeter Wemm * choose the second.. 51452c24af7SPeter Wemm */ 51552c24af7SPeter Wemm if (memsz > filsz) 5163ebc1248SPeter Wemm map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr; 51752c24af7SPeter Wemm else 5183ebc1248SPeter Wemm map_len = round_page_ps(offset + filsz, pagesize) - file_addr; 51952c24af7SPeter Wemm 52052c24af7SPeter Wemm if (map_len != 0) { 521fa7dd9c5SMatthew Dillon /* cow flags: don't dump readonly sections in core */ 522fa7dd9c5SMatthew Dillon cow = MAP_COPY_ON_WRITE | MAP_PREFAULT | 523fa7dd9c5SMatthew Dillon (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP); 524fa7dd9c5SMatthew Dillon 525292177e6SAlan Cox rv = __elfN(map_insert)(map, 52652c24af7SPeter Wemm object, 52752c24af7SPeter Wemm file_addr, /* file offset */ 52852c24af7SPeter Wemm map_addr, /* virtual start */ 52952c24af7SPeter Wemm map_addr + map_len,/* virtual end */ 53052c24af7SPeter Wemm prot, 531fa7dd9c5SMatthew Dillon cow); 532e5e6093bSAlan Cox if (rv != KERN_SUCCESS) 533a7cddfedSJake Burkholder return (EINVAL); 53452c24af7SPeter Wemm 53552c24af7SPeter Wemm /* we can stop now if we've covered it all */ 53623955314SAlfred Perlstein if (memsz == filsz) { 537a7cddfedSJake Burkholder return (0); 53852c24af7SPeter Wemm } 53923955314SAlfred Perlstein } 54052c24af7SPeter Wemm 54152c24af7SPeter Wemm 54252c24af7SPeter Wemm /* 54352c24af7SPeter Wemm * We have to get the remaining bit of the file into the first part 54452c24af7SPeter Wemm * of the oversized map segment. This is normally because the .data 54552c24af7SPeter Wemm * segment in the file is extended to provide bss. It's a neat idea 54652c24af7SPeter Wemm * to try and save a page, but it's a pain in the behind to implement. 547e1743d02SSøren Schmidt */ 5483ebc1248SPeter Wemm copy_len = (offset + filsz) - trunc_page_ps(offset + filsz, pagesize); 5493ebc1248SPeter Wemm map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize); 550ca0387efSJake Burkholder map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) - 551ca0387efSJake Burkholder map_addr; 552e1743d02SSøren Schmidt 55352c24af7SPeter Wemm /* This had damn well better be true! */ 5548191d577SPeter Wemm if (map_len != 0) { 555292177e6SAlan Cox rv = __elfN(map_insert)(map, NULL, 0, map_addr, map_addr + 556292177e6SAlan Cox map_len, VM_PROT_ALL, 0); 55723955314SAlfred Perlstein if (rv != KERN_SUCCESS) { 558a7cddfedSJake Burkholder return (EINVAL); 5598191d577SPeter Wemm } 56023955314SAlfred Perlstein } 561e1743d02SSøren Schmidt 56252c24af7SPeter Wemm if (copy_len != 0) { 5633ebc1248SPeter Wemm vm_offset_t off; 564da61b9a6SAlan Cox 565da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset + filsz); 566da61b9a6SAlan Cox if (sf == NULL) 567da61b9a6SAlan Cox return (EIO); 568e1743d02SSøren Schmidt 56952c24af7SPeter Wemm /* send the page fragment to user space */ 57081f223caSJake Burkholder off = trunc_page_ps(offset + filsz, pagesize) - 57181f223caSJake Burkholder trunc_page(offset + filsz); 572da61b9a6SAlan Cox error = copyout((caddr_t)sf_buf_kva(sf) + off, 573da61b9a6SAlan Cox (caddr_t)map_addr, copy_len); 574be996836SAttilio Rao vm_imgact_unmap_page(sf); 57523955314SAlfred Perlstein if (error) { 57652c24af7SPeter Wemm return (error); 57752c24af7SPeter Wemm } 57823955314SAlfred Perlstein } 579e1743d02SSøren Schmidt 580e1743d02SSøren Schmidt /* 5813ebc1248SPeter Wemm * set it to the specified protection. 5823ebc1248SPeter Wemm * XXX had better undo the damage from pasting over the cracks here! 583e1743d02SSøren Schmidt */ 584292177e6SAlan Cox vm_map_protect(map, trunc_page(map_addr), round_page(map_addr + 585292177e6SAlan Cox map_len), prot, FALSE); 5868191d577SPeter Wemm 587ff6f03c7SAlan Cox return (0); 588e1743d02SSøren Schmidt } 589e1743d02SSøren Schmidt 590c33fe779SJohn Polstra /* 591c33fe779SJohn Polstra * Load the file "file" into memory. It may be either a shared object 592c33fe779SJohn Polstra * or an executable. 593c33fe779SJohn Polstra * 594c33fe779SJohn Polstra * The "addr" reference parameter is in/out. On entry, it specifies 595c33fe779SJohn Polstra * the address where a shared object should be loaded. If the file is 596c33fe779SJohn Polstra * an executable, this value is ignored. On exit, "addr" specifies 597c33fe779SJohn Polstra * where the file was actually loaded. 598c33fe779SJohn Polstra * 599c33fe779SJohn Polstra * The "entry" reference parameter is out only. On exit, it specifies 600c33fe779SJohn Polstra * the entry point for the loaded file. 601c33fe779SJohn Polstra */ 602e1743d02SSøren Schmidt static int 6033ebc1248SPeter Wemm __elfN(load_file)(struct proc *p, const char *file, u_long *addr, 6043ebc1248SPeter Wemm u_long *entry, size_t pagesize) 605e1743d02SSøren Schmidt { 606911c2be0SMark Peek struct { 607911c2be0SMark Peek struct nameidata nd; 608911c2be0SMark Peek struct vattr attr; 609911c2be0SMark Peek struct image_params image_params; 610911c2be0SMark Peek } *tempdata; 611d254af07SMatthew Dillon const Elf_Ehdr *hdr = NULL; 612d254af07SMatthew Dillon const Elf_Phdr *phdr = NULL; 613911c2be0SMark Peek struct nameidata *nd; 614911c2be0SMark Peek struct vattr *attr; 615911c2be0SMark Peek struct image_params *imgp; 61652c24af7SPeter Wemm vm_prot_t prot; 617c33fe779SJohn Polstra u_long rbase; 618c33fe779SJohn Polstra u_long base_addr = 0; 6195050aa86SKonstantin Belousov int error, i, numsegs; 620e1743d02SSøren Schmidt 62112bc222eSJonathan Anderson #ifdef CAPABILITY_MODE 62212bc222eSJonathan Anderson /* 62312bc222eSJonathan Anderson * XXXJA: This check can go away once we are sufficiently confident 62412bc222eSJonathan Anderson * that the checks in namei() are correct. 62512bc222eSJonathan Anderson */ 62612bc222eSJonathan Anderson if (IN_CAPABILITY_MODE(curthread)) 62712bc222eSJonathan Anderson return (ECAPMODE); 62812bc222eSJonathan Anderson #endif 62912bc222eSJonathan Anderson 630a163d034SWarner Losh tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK); 631911c2be0SMark Peek nd = &tempdata->nd; 632911c2be0SMark Peek attr = &tempdata->attr; 633911c2be0SMark Peek imgp = &tempdata->image_params; 634911c2be0SMark Peek 635c8a79999SPeter Wemm /* 636c8a79999SPeter Wemm * Initialize part of the common data 637c8a79999SPeter Wemm */ 638c8a79999SPeter Wemm imgp->proc = p; 639911c2be0SMark Peek imgp->attr = attr; 640c8a79999SPeter Wemm imgp->firstpage = NULL; 64159c8bc40SAlan Cox imgp->image_header = NULL; 6420b2ed1aeSJeff Roberson imgp->object = NULL; 6436d7bdc8dSRobert Watson imgp->execlabel = NULL; 644c8a79999SPeter Wemm 6455050aa86SKonstantin Belousov NDINIT(nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE, file, curthread); 646911c2be0SMark Peek if ((error = namei(nd)) != 0) { 647911c2be0SMark Peek nd->ni_vp = NULL; 648e1743d02SSøren Schmidt goto fail; 649e1743d02SSøren Schmidt } 650911c2be0SMark Peek NDFREE(nd, NDF_ONLY_PNBUF); 651911c2be0SMark Peek imgp->vp = nd->ni_vp; 652c8a79999SPeter Wemm 653e1743d02SSøren Schmidt /* 654e1743d02SSøren Schmidt * Check permissions, modes, uid, etc on the file, and "open" it. 655e1743d02SSøren Schmidt */ 656c8a79999SPeter Wemm error = exec_check_permissions(imgp); 657373d1a3fSAlan Cox if (error) 658c8a79999SPeter Wemm goto fail; 659e1743d02SSøren Schmidt 660c8a79999SPeter Wemm error = exec_map_first_page(imgp); 661373d1a3fSAlan Cox if (error) 662373d1a3fSAlan Cox goto fail; 663373d1a3fSAlan Cox 66425ead034SBrian Feldman /* 66525ead034SBrian Feldman * Also make certain that the interpreter stays the same, so set 666e6e370a7SJeff Roberson * its VV_TEXT flag, too. 66725ead034SBrian Feldman */ 668877d24acSKonstantin Belousov VOP_SET_TEXT(nd->ni_vp); 669e6e370a7SJeff Roberson 6708516dd18SPoul-Henning Kamp imgp->object = nd->ni_vp->v_object; 671e1743d02SSøren Schmidt 672d254af07SMatthew Dillon hdr = (const Elf_Ehdr *)imgp->image_header; 6733ebc1248SPeter Wemm if ((error = __elfN(check_header)(hdr)) != 0) 674e1743d02SSøren Schmidt goto fail; 675c33fe779SJohn Polstra if (hdr->e_type == ET_DYN) 676c33fe779SJohn Polstra rbase = *addr; 677c33fe779SJohn Polstra else if (hdr->e_type == ET_EXEC) 678c33fe779SJohn Polstra rbase = 0; 679c33fe779SJohn Polstra else { 680c33fe779SJohn Polstra error = ENOEXEC; 681c33fe779SJohn Polstra goto fail; 682c33fe779SJohn Polstra } 683e1743d02SSøren Schmidt 684c8a79999SPeter Wemm /* Only support headers that fit within first page for now */ 68552c24af7SPeter Wemm if ((hdr->e_phoff > PAGE_SIZE) || 686d19d5bf4STijl Coosemans (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) { 687c8a79999SPeter Wemm error = ENOEXEC; 688e1743d02SSøren Schmidt goto fail; 689c8a79999SPeter Wemm } 690c8a79999SPeter Wemm 691d254af07SMatthew Dillon phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 69293d1c728SKonstantin Belousov if (!aligned(phdr, Elf_Addr)) { 69393d1c728SKonstantin Belousov error = ENOEXEC; 69493d1c728SKonstantin Belousov goto fail; 69593d1c728SKonstantin Belousov } 696e1743d02SSøren Schmidt 697c33fe779SJohn Polstra for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) { 6985b33842aSKonstantin Belousov if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) { 6995b33842aSKonstantin Belousov /* Loadable segment */ 700ed167eaaSKonstantin Belousov prot = __elfN(trans_prot)(phdr[i].p_flags); 701292177e6SAlan Cox error = __elfN(load_section)(imgp, phdr[i].p_offset, 70281f223caSJake Burkholder (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase, 703292177e6SAlan Cox phdr[i].p_memsz, phdr[i].p_filesz, prot, pagesize); 704292177e6SAlan Cox if (error != 0) 705e1743d02SSøren Schmidt goto fail; 706e1743d02SSøren Schmidt /* 707c33fe779SJohn Polstra * Establish the base address if this is the 708c33fe779SJohn Polstra * first segment. 709e1743d02SSøren Schmidt */ 710c33fe779SJohn Polstra if (numsegs == 0) 711ca0387efSJake Burkholder base_addr = trunc_page(phdr[i].p_vaddr + 712ca0387efSJake Burkholder rbase); 713c33fe779SJohn Polstra numsegs++; 714e1743d02SSøren Schmidt } 715e1743d02SSøren Schmidt } 716c33fe779SJohn Polstra *addr = base_addr; 717c33fe779SJohn Polstra *entry = (unsigned long)hdr->e_entry + rbase; 718e1743d02SSøren Schmidt 719e1743d02SSøren Schmidt fail: 720c8a79999SPeter Wemm if (imgp->firstpage) 721c8a79999SPeter Wemm exec_unmap_first_page(imgp); 7220b2ed1aeSJeff Roberson 723911c2be0SMark Peek if (nd->ni_vp) 724373d1a3fSAlan Cox vput(nd->ni_vp); 725911c2be0SMark Peek 726911c2be0SMark Peek free(tempdata, M_TEMP); 727e1743d02SSøren Schmidt 728a7cddfedSJake Burkholder return (error); 729e1743d02SSøren Schmidt } 730e1743d02SSøren Schmidt 731303b270bSEivind Eklund static int 7323ebc1248SPeter Wemm __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp) 733e1743d02SSøren Schmidt { 7346c775eb6SKonstantin Belousov struct thread *td; 7356c775eb6SKonstantin Belousov const Elf_Ehdr *hdr; 73632c01de2SDmitry Chagin const Elf_Phdr *phdr; 737e5e6093bSAlan Cox Elf_Auxargs *elf_auxargs; 7385856e12eSJohn Dyson struct vmspace *vmspace; 7396c775eb6SKonstantin Belousov const char *err_str, *newinterp; 7406c775eb6SKonstantin Belousov char *interp, *interp_buf, *path; 741d1dbc694SJohn Polstra Elf_Brandinfo *brand_info; 7425fe3ed62SJake Burkholder struct sysentvec *sv; 7436c775eb6SKonstantin Belousov vm_prot_t prot; 7446c775eb6SKonstantin Belousov u_long text_size, data_size, total_size, text_addr, data_addr; 7456c775eb6SKonstantin Belousov u_long seg_size, seg_addr, addr, baddr, et_dyn_addr, entry, proghdr; 7466c775eb6SKonstantin Belousov int32_t osrel; 7476c775eb6SKonstantin Belousov int error, i, n, interp_name_len, have_interp; 7486c775eb6SKonstantin Belousov 7496c775eb6SKonstantin Belousov hdr = (const Elf_Ehdr *)imgp->image_header; 750e1743d02SSøren Schmidt 751e1743d02SSøren Schmidt /* 752e1743d02SSøren Schmidt * Do we have a valid ELF header ? 753900b28f9SMaxim Sobolev * 754900b28f9SMaxim Sobolev * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later 755900b28f9SMaxim Sobolev * if particular brand doesn't support it. 756e1743d02SSøren Schmidt */ 757900b28f9SMaxim Sobolev if (__elfN(check_header)(hdr) != 0 || 758900b28f9SMaxim Sobolev (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)) 759a7cddfedSJake Burkholder return (-1); 760e1743d02SSøren Schmidt 761e1743d02SSøren Schmidt /* 762e1743d02SSøren Schmidt * From here on down, we return an errno, not -1, as we've 763e1743d02SSøren Schmidt * detected an ELF file. 764e1743d02SSøren Schmidt */ 765e1743d02SSøren Schmidt 766e1743d02SSøren Schmidt if ((hdr->e_phoff > PAGE_SIZE) || 767d19d5bf4STijl Coosemans (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) { 768c8a79999SPeter Wemm /* Only support headers in first page for now */ 7696b16d664SEd Maste uprintf("Program headers not in the first page\n"); 770a7cddfedSJake Burkholder return (ENOEXEC); 771e1743d02SSøren Schmidt } 77252c24af7SPeter Wemm phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 7736b16d664SEd Maste if (!aligned(phdr, Elf_Addr)) { 7746b16d664SEd Maste uprintf("Unaligned program headers\n"); 77593d1c728SKonstantin Belousov return (ENOEXEC); 7766b16d664SEd Maste } 7776c775eb6SKonstantin Belousov 7786c775eb6SKonstantin Belousov n = error = 0; 7797564c4adSKonstantin Belousov baddr = 0; 7806c775eb6SKonstantin Belousov osrel = 0; 7816c775eb6SKonstantin Belousov text_size = data_size = total_size = text_addr = data_addr = 0; 7826c775eb6SKonstantin Belousov entry = proghdr = 0; 7836c775eb6SKonstantin Belousov interp_name_len = 0; 7846c775eb6SKonstantin Belousov err_str = newinterp = NULL; 7856c775eb6SKonstantin Belousov interp = interp_buf = NULL; 7866c775eb6SKonstantin Belousov td = curthread; 7876c775eb6SKonstantin Belousov 7885fe3ed62SJake Burkholder for (i = 0; i < hdr->e_phnum; i++) { 789291c06a1SKonstantin Belousov switch (phdr[i].p_type) { 790291c06a1SKonstantin Belousov case PT_LOAD: 7917564c4adSKonstantin Belousov if (n == 0) 7927564c4adSKonstantin Belousov baddr = phdr[i].p_vaddr; 7937564c4adSKonstantin Belousov n++; 794291c06a1SKonstantin Belousov break; 795291c06a1SKonstantin Belousov case PT_INTERP: 796e5e6093bSAlan Cox /* Path to interpreter */ 7976c775eb6SKonstantin Belousov if (phdr[i].p_filesz > MAXPATHLEN) { 7986b16d664SEd Maste uprintf("Invalid PT_INTERP\n"); 7996c775eb6SKonstantin Belousov error = ENOEXEC; 8006c775eb6SKonstantin Belousov goto ret; 8016b16d664SEd Maste } 802*d3ee0a15SJonathan T. Looney if (interp != NULL) { 803*d3ee0a15SJonathan T. Looney uprintf("Multiple PT_INTERP headers\n"); 804*d3ee0a15SJonathan T. Looney error = ENOEXEC; 805*d3ee0a15SJonathan T. Looney goto ret; 806*d3ee0a15SJonathan T. Looney } 807d1ae5c83SKonstantin Belousov interp_name_len = phdr[i].p_filesz; 8086c775eb6SKonstantin Belousov if (phdr[i].p_offset > PAGE_SIZE || 8096c775eb6SKonstantin Belousov interp_name_len > PAGE_SIZE - phdr[i].p_offset) { 8106c775eb6SKonstantin Belousov VOP_UNLOCK(imgp->vp, 0); 8116c775eb6SKonstantin Belousov interp_buf = malloc(interp_name_len + 1, M_TEMP, 8126c775eb6SKonstantin Belousov M_WAITOK); 8136c775eb6SKonstantin Belousov vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 8146c775eb6SKonstantin Belousov error = vn_rdwr(UIO_READ, imgp->vp, interp_buf, 8156c775eb6SKonstantin Belousov interp_name_len, phdr[i].p_offset, 8166c775eb6SKonstantin Belousov UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, 8176c775eb6SKonstantin Belousov NOCRED, NULL, td); 8186c775eb6SKonstantin Belousov if (error != 0) { 8196c775eb6SKonstantin Belousov uprintf("i/o error PT_INTERP\n"); 8206c775eb6SKonstantin Belousov goto ret; 8216c775eb6SKonstantin Belousov } 8226c775eb6SKonstantin Belousov interp_buf[interp_name_len] = '\0'; 8236c775eb6SKonstantin Belousov interp = interp_buf; 8246c775eb6SKonstantin Belousov } else { 8256c775eb6SKonstantin Belousov interp = __DECONST(char *, imgp->image_header) + 8266c775eb6SKonstantin Belousov phdr[i].p_offset; 8276c775eb6SKonstantin Belousov } 828291c06a1SKonstantin Belousov break; 829291c06a1SKonstantin Belousov case PT_GNU_STACK: 830291c06a1SKonstantin Belousov if (__elfN(nxstack)) 831291c06a1SKonstantin Belousov imgp->stack_prot = 832291c06a1SKonstantin Belousov __elfN(trans_prot)(phdr[i].p_flags); 833316b3843SKonstantin Belousov imgp->stack_sz = phdr[i].p_memsz; 834291c06a1SKonstantin Belousov break; 8353ebc1248SPeter Wemm } 8363ebc1248SPeter Wemm } 8373ebc1248SPeter Wemm 838d1ae5c83SKonstantin Belousov brand_info = __elfN(get_brandinfo)(imgp, interp, interp_name_len, 839d1ae5c83SKonstantin Belousov &osrel); 8405fe3ed62SJake Burkholder if (brand_info == NULL) { 8415fe3ed62SJake Burkholder uprintf("ELF binary type \"%u\" not known.\n", 8425fe3ed62SJake Burkholder hdr->e_ident[EI_OSABI]); 8436c775eb6SKonstantin Belousov error = ENOEXEC; 8446c775eb6SKonstantin Belousov goto ret; 8453ebc1248SPeter Wemm } 846ab02d85fSKonstantin Belousov if (hdr->e_type == ET_DYN) { 8476b16d664SEd Maste if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) { 8486b16d664SEd Maste uprintf("Cannot execute shared object\n"); 8496c775eb6SKonstantin Belousov error = ENOEXEC; 8506c775eb6SKonstantin Belousov goto ret; 8516b16d664SEd Maste } 8527564c4adSKonstantin Belousov /* 8537564c4adSKonstantin Belousov * Honour the base load address from the dso if it is 8547564c4adSKonstantin Belousov * non-zero for some reason. 8557564c4adSKonstantin Belousov */ 8567564c4adSKonstantin Belousov if (baddr == 0) 857ab02d85fSKonstantin Belousov et_dyn_addr = ET_DYN_LOAD_ADDR; 8587564c4adSKonstantin Belousov else 8597564c4adSKonstantin Belousov et_dyn_addr = 0; 860ab02d85fSKonstantin Belousov } else 861ab02d85fSKonstantin Belousov et_dyn_addr = 0; 8625fe3ed62SJake Burkholder sv = brand_info->sysvec; 8639b68618dSPeter Wemm if (interp != NULL && brand_info->interp_newpath != NULL) 8644113f8d7SPeter Wemm newinterp = brand_info->interp_newpath; 8653ebc1248SPeter Wemm 86660bb3943SAlan Cox /* 86760bb3943SAlan Cox * Avoid a possible deadlock if the current address space is destroyed 86860bb3943SAlan Cox * and that address space maps the locked vnode. In the common case, 86960bb3943SAlan Cox * the locked vnode's v_usecount is decremented but remains greater 87060bb3943SAlan Cox * than zero. Consequently, the vnode lock is not needed by vrele(). 87160bb3943SAlan Cox * However, in cases where the vnode lock is external, such as nullfs, 87260bb3943SAlan Cox * v_usecount may become zero. 8731dfab802SAlan Cox * 8741dfab802SAlan Cox * The VV_TEXT flag prevents modifications to the executable while 8751dfab802SAlan Cox * the vnode is unlocked. 87660bb3943SAlan Cox */ 87722db15c0SAttilio Rao VOP_UNLOCK(imgp->vp, 0); 87860bb3943SAlan Cox 87989b57fcfSKonstantin Belousov error = exec_new_vmspace(imgp, sv); 88019059a13SJohn Baldwin imgp->proc->p_sysent = sv; 881e1743d02SSøren Schmidt 882cb05b60aSAttilio Rao vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 8836c775eb6SKonstantin Belousov if (error != 0) 8846c775eb6SKonstantin Belousov goto ret; 88560bb3943SAlan Cox 886e1743d02SSøren Schmidt for (i = 0; i < hdr->e_phnum; i++) { 887e1743d02SSøren Schmidt switch (phdr[i].p_type) { 888e1743d02SSøren Schmidt case PT_LOAD: /* Loadable segment */ 8895b33842aSKonstantin Belousov if (phdr[i].p_memsz == 0) 8905b33842aSKonstantin Belousov break; 891ed167eaaSKonstantin Belousov prot = __elfN(trans_prot)(phdr[i].p_flags); 892292177e6SAlan Cox error = __elfN(load_section)(imgp, phdr[i].p_offset, 893ab02d85fSKonstantin Belousov (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr, 89481f223caSJake Burkholder phdr[i].p_memsz, phdr[i].p_filesz, prot, 895292177e6SAlan Cox sv->sv_pagesize); 896292177e6SAlan Cox if (error != 0) 8976c775eb6SKonstantin Belousov goto ret; 898e1743d02SSøren Schmidt 899cfaf7e60SDoug Rabson /* 900cfaf7e60SDoug Rabson * If this segment contains the program headers, 901cfaf7e60SDoug Rabson * remember their virtual address for the AT_PHDR 902cfaf7e60SDoug Rabson * aux entry. Static binaries don't usually include 903cfaf7e60SDoug Rabson * a PT_PHDR entry. 904cfaf7e60SDoug Rabson */ 905cfaf7e60SDoug Rabson if (phdr[i].p_offset == 0 && 906cfaf7e60SDoug Rabson hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize 907cfaf7e60SDoug Rabson <= phdr[i].p_filesz) 908ab02d85fSKonstantin Belousov proghdr = phdr[i].p_vaddr + hdr->e_phoff + 909ab02d85fSKonstantin Belousov et_dyn_addr; 910cfaf7e60SDoug Rabson 911ab02d85fSKonstantin Belousov seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr); 912cac45152SMatthew Dillon seg_size = round_page(phdr[i].p_memsz + 913ab02d85fSKonstantin Belousov phdr[i].p_vaddr + et_dyn_addr - seg_addr); 914cac45152SMatthew Dillon 915e1743d02SSøren Schmidt /* 916920acedbSNathan Whitehorn * Make the largest executable segment the official 917920acedbSNathan Whitehorn * text segment and all others data. 91821c2d047SMatthew Dillon * 91921c2d047SMatthew Dillon * Note that obreak() assumes that data_addr + 92021c2d047SMatthew Dillon * data_size == end of data load area, and the ELF 92121c2d047SMatthew Dillon * file format expects segments to be sorted by 92221c2d047SMatthew Dillon * address. If multiple data segments exist, the 92321c2d047SMatthew Dillon * last one will be used. 924e1743d02SSøren Schmidt */ 925920acedbSNathan Whitehorn 926920acedbSNathan Whitehorn if (phdr[i].p_flags & PF_X && text_size < seg_size) { 9279782ecbaSPeter Wemm text_size = seg_size; 9289782ecbaSPeter Wemm text_addr = seg_addr; 9299782ecbaSPeter Wemm } else { 93021c2d047SMatthew Dillon data_size = seg_size; 931cac45152SMatthew Dillon data_addr = seg_addr; 932cac45152SMatthew Dillon } 93321c2d047SMatthew Dillon total_size += seg_size; 93496725dd0SAlexander Kabaev break; 93596725dd0SAlexander Kabaev case PT_PHDR: /* Program header table info */ 936ab02d85fSKonstantin Belousov proghdr = phdr[i].p_vaddr + et_dyn_addr; 93796725dd0SAlexander Kabaev break; 93896725dd0SAlexander Kabaev default: 93996725dd0SAlexander Kabaev break; 94096725dd0SAlexander Kabaev } 94196725dd0SAlexander Kabaev } 94296725dd0SAlexander Kabaev 94396725dd0SAlexander Kabaev if (data_addr == 0 && data_size == 0) { 94496725dd0SAlexander Kabaev data_addr = text_addr; 94596725dd0SAlexander Kabaev data_size = text_size; 94696725dd0SAlexander Kabaev } 947cac45152SMatthew Dillon 948920acedbSNathan Whitehorn entry = (u_long)hdr->e_entry + et_dyn_addr; 949920acedbSNathan Whitehorn 950cac45152SMatthew Dillon /* 951cac45152SMatthew Dillon * Check limits. It should be safe to check the 95296725dd0SAlexander Kabaev * limits after loading the segments since we do 95396725dd0SAlexander Kabaev * not actually fault in all the segments pages. 954cac45152SMatthew Dillon */ 95591d5354aSJohn Baldwin PROC_LOCK(imgp->proc); 956f6f6d240SMateusz Guzik if (data_size > lim_cur_proc(imgp->proc, RLIMIT_DATA)) 9576b16d664SEd Maste err_str = "Data segment size exceeds process limit"; 9586b16d664SEd Maste else if (text_size > maxtsiz) 9596b16d664SEd Maste err_str = "Text segment size exceeds system limit"; 960f6f6d240SMateusz Guzik else if (total_size > lim_cur_proc(imgp->proc, RLIMIT_VMEM)) 9616b16d664SEd Maste err_str = "Total segment size exceeds process limit"; 9626b16d664SEd Maste else if (racct_set(imgp->proc, RACCT_DATA, data_size) != 0) 9636b16d664SEd Maste err_str = "Data segment size exceeds resource limit"; 9646b16d664SEd Maste else if (racct_set(imgp->proc, RACCT_VMEM, total_size) != 0) 9656b16d664SEd Maste err_str = "Total segment size exceeds resource limit"; 9666b16d664SEd Maste if (err_str != NULL) { 96791d5354aSJohn Baldwin PROC_UNLOCK(imgp->proc); 9686b16d664SEd Maste uprintf("%s\n", err_str); 9696c775eb6SKonstantin Belousov error = ENOMEM; 9706c775eb6SKonstantin Belousov goto ret; 971cac45152SMatthew Dillon } 972e1743d02SSøren Schmidt 973292177e6SAlan Cox vmspace = imgp->proc->p_vmspace; 974e1743d02SSøren Schmidt vmspace->vm_tsize = text_size >> PAGE_SHIFT; 9757cd99438SBruce Evans vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr; 976e1743d02SSøren Schmidt vmspace->vm_dsize = data_size >> PAGE_SHIFT; 9777cd99438SBruce Evans vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr; 978e1743d02SSøren Schmidt 979c460ac3aSPeter Wemm /* 980c460ac3aSPeter Wemm * We load the dynamic linker where a userland call 981c460ac3aSPeter Wemm * to mmap(0, ...) would put it. The rationale behind this 982c460ac3aSPeter Wemm * calculation is that it leaves room for the heap to grow to 983c460ac3aSPeter Wemm * its maximum allowed size. 984c460ac3aSPeter Wemm */ 9856c775eb6SKonstantin Belousov addr = round_page((vm_offset_t)vmspace->vm_daddr + lim_max(td, 986292177e6SAlan Cox RLIMIT_DATA)); 98791d5354aSJohn Baldwin PROC_UNLOCK(imgp->proc); 988e1743d02SSøren Schmidt 989ea5a2b2eSSøren Schmidt imgp->entry_addr = entry; 990ea5a2b2eSSøren Schmidt 99160bb3943SAlan Cox if (interp != NULL) { 9926c775eb6SKonstantin Belousov have_interp = FALSE; 99322db15c0SAttilio Rao VOP_UNLOCK(imgp->vp, 0); 99460bb3943SAlan Cox if (brand_info->emul_path != NULL && 9959b68618dSPeter Wemm brand_info->emul_path[0] != '\0') { 996a163d034SWarner Losh path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 99760bb3943SAlan Cox snprintf(path, MAXPATHLEN, "%s%s", 99860bb3943SAlan Cox brand_info->emul_path, interp); 9999b68618dSPeter Wemm error = __elfN(load_file)(imgp->proc, path, &addr, 10009b68618dSPeter Wemm &imgp->entry_addr, sv->sv_pagesize); 1001911c2be0SMark Peek free(path, M_TEMP); 10029b68618dSPeter Wemm if (error == 0) 10034113f8d7SPeter Wemm have_interp = TRUE; 10049b68618dSPeter Wemm } 10054113f8d7SPeter Wemm if (!have_interp && newinterp != NULL) { 10064113f8d7SPeter Wemm error = __elfN(load_file)(imgp->proc, newinterp, &addr, 10074113f8d7SPeter Wemm &imgp->entry_addr, sv->sv_pagesize); 1008387ad998SKonstantin Belousov if (error == 0) 10094113f8d7SPeter Wemm have_interp = TRUE; 10104113f8d7SPeter Wemm } 10114113f8d7SPeter Wemm if (!have_interp) { 10129b68618dSPeter Wemm error = __elfN(load_file)(imgp->proc, interp, &addr, 10139b68618dSPeter Wemm &imgp->entry_addr, sv->sv_pagesize); 101460bb3943SAlan Cox } 1015cb05b60aSAttilio Rao vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 10169b68618dSPeter Wemm if (error != 0) { 1017d943fa35SKonstantin Belousov uprintf("ELF interpreter %s not found, error %d\n", 1018d943fa35SKonstantin Belousov interp, error); 10196c775eb6SKonstantin Belousov goto ret; 1020e1743d02SSøren Schmidt } 102195c807cfSRobert Watson } else 10227564c4adSKonstantin Belousov addr = et_dyn_addr; 1023ea5a2b2eSSøren Schmidt 1024e1743d02SSøren Schmidt /* 1025e1743d02SSøren Schmidt * Construct auxargs table (used by the fixup routine) 1026e1743d02SSøren Schmidt */ 1027a163d034SWarner Losh elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK); 1028e1743d02SSøren Schmidt elf_auxargs->execfd = -1; 1029e1743d02SSøren Schmidt elf_auxargs->phdr = proghdr; 1030e1743d02SSøren Schmidt elf_auxargs->phent = hdr->e_phentsize; 1031e1743d02SSøren Schmidt elf_auxargs->phnum = hdr->e_phnum; 1032e1743d02SSøren Schmidt elf_auxargs->pagesz = PAGE_SIZE; 1033e1743d02SSøren Schmidt elf_auxargs->base = addr; 1034e1743d02SSøren Schmidt elf_auxargs->flags = 0; 1035e1743d02SSøren Schmidt elf_auxargs->entry = entry; 1036d36eec69SWarner Losh elf_auxargs->hdr_eflags = hdr->e_flags; 1037e1743d02SSøren Schmidt 1038e1743d02SSøren Schmidt imgp->auxargs = elf_auxargs; 1039e1743d02SSøren Schmidt imgp->interpreted = 0; 1040a0ea661fSNathan Whitehorn imgp->reloc_base = addr; 104132c01de2SDmitry Chagin imgp->proc->p_osrel = osrel; 1042f231de47SKonstantin Belousov 10436c775eb6SKonstantin Belousov ret: 10446c775eb6SKonstantin Belousov free(interp_buf, M_TEMP); 1045a7cddfedSJake Burkholder return (error); 1046e1743d02SSøren Schmidt } 1047e1743d02SSøren Schmidt 1048a360a43dSJake Burkholder #define suword __CONCAT(suword, __ELF_WORD_SIZE) 10493ebc1248SPeter Wemm 10503ebc1248SPeter Wemm int 10513ebc1248SPeter Wemm __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp) 1052e1743d02SSøren Schmidt { 1053ecbb00a2SDoug Rabson Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs; 1054a360a43dSJake Burkholder Elf_Addr *base; 1055a360a43dSJake Burkholder Elf_Addr *pos; 1056e1743d02SSøren Schmidt 1057a360a43dSJake Burkholder base = (Elf_Addr *)*stack_base; 1058610ecfe0SMaxim Sobolev pos = base + (imgp->args->argc + imgp->args->envc + 2); 1059e1743d02SSøren Schmidt 106035c2a5a8SWarner Losh if (args->execfd != -1) 1061e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 1062e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 1063e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 1064e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 1065e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 1066e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 1067e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 1068e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_BASE, args->base); 1069ee960398SWarner Losh #ifdef AT_EHDRFLAGS 1070d36eec69SWarner Losh AUXARGS_ENTRY(pos, AT_EHDRFLAGS, args->hdr_eflags); 1071d36eec69SWarner Losh #endif 10723ff06357SKonstantin Belousov if (imgp->execpathp != 0) 10733ff06357SKonstantin Belousov AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp); 1074b96bd95bSIan Lepore AUXARGS_ENTRY(pos, AT_OSRELDATE, 1075b96bd95bSIan Lepore imgp->proc->p_ucred->cr_prison->pr_osreldate); 1076ee235befSKonstantin Belousov if (imgp->canary != 0) { 1077ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary); 1078ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen); 1079ee235befSKonstantin Belousov } 1080ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus); 1081ee235befSKonstantin Belousov if (imgp->pagesizes != 0) { 1082ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes); 1083ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen); 1084ee235befSKonstantin Belousov } 1085aea81038SKonstantin Belousov if (imgp->sysent->sv_timekeep_base != 0) { 1086aea81038SKonstantin Belousov AUXARGS_ENTRY(pos, AT_TIMEKEEP, 1087aea81038SKonstantin Belousov imgp->sysent->sv_timekeep_base); 1088aea81038SKonstantin Belousov } 108926d8f3e1SKonstantin Belousov AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj 109026d8f3e1SKonstantin Belousov != NULL && imgp->stack_prot != 0 ? imgp->stack_prot : 109126d8f3e1SKonstantin Belousov imgp->sysent->sv_stackprot); 1092e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_NULL, 0); 1093e1743d02SSøren Schmidt 1094e1743d02SSøren Schmidt free(imgp->auxargs, M_TEMP); 1095e1743d02SSøren Schmidt imgp->auxargs = NULL; 1096e1743d02SSøren Schmidt 10973ebc1248SPeter Wemm base--; 1098610ecfe0SMaxim Sobolev suword(base, (long)imgp->args->argc); 10993ebc1248SPeter Wemm *stack_base = (register_t *)base; 1100a7cddfedSJake Burkholder return (0); 1101e1743d02SSøren Schmidt } 1102e1743d02SSøren Schmidt 1103e1743d02SSøren Schmidt /* 11048c64af4fSJohn Polstra * Code for generating ELF core dumps. 11058c64af4fSJohn Polstra */ 11068c64af4fSJohn Polstra 11074d77a549SAlfred Perlstein typedef void (*segment_callback)(vm_map_entry_t, void *); 11080ff27d31SJohn Polstra 11090ff27d31SJohn Polstra /* Closure for cb_put_phdr(). */ 11100ff27d31SJohn Polstra struct phdr_closure { 11110ff27d31SJohn Polstra Elf_Phdr *phdr; /* Program header to fill in */ 11120ff27d31SJohn Polstra Elf_Off offset; /* Offset of segment in core file */ 11130ff27d31SJohn Polstra }; 11140ff27d31SJohn Polstra 11150ff27d31SJohn Polstra /* Closure for cb_size_segment(). */ 11160ff27d31SJohn Polstra struct sseg_closure { 11170ff27d31SJohn Polstra int count; /* Count of writable segments. */ 11180ff27d31SJohn Polstra size_t size; /* Total size of all writable segments. */ 11190ff27d31SJohn Polstra }; 11200ff27d31SJohn Polstra 1121bd390213SMikolaj Golub typedef void (*outfunc_t)(void *, struct sbuf *, size_t *); 1122bd390213SMikolaj Golub 1123bd390213SMikolaj Golub struct note_info { 1124bd390213SMikolaj Golub int type; /* Note type. */ 1125bd390213SMikolaj Golub outfunc_t outfunc; /* Output function. */ 1126bd390213SMikolaj Golub void *outarg; /* Argument for the output function. */ 1127bd390213SMikolaj Golub size_t outsize; /* Output size. */ 1128bd390213SMikolaj Golub TAILQ_ENTRY(note_info) link; /* Link to the next note info. */ 1129bd390213SMikolaj Golub }; 1130bd390213SMikolaj Golub 1131bd390213SMikolaj Golub TAILQ_HEAD(note_info_list, note_info); 1132bd390213SMikolaj Golub 1133aa14e9b7SMark Johnston /* Coredump output parameters. */ 1134aa14e9b7SMark Johnston struct coredump_params { 1135aa14e9b7SMark Johnston off_t offset; 1136aa14e9b7SMark Johnston struct ucred *active_cred; 1137aa14e9b7SMark Johnston struct ucred *file_cred; 1138aa14e9b7SMark Johnston struct thread *td; 1139aa14e9b7SMark Johnston struct vnode *vp; 1140aa14e9b7SMark Johnston struct gzio_stream *gzs; 1141aa14e9b7SMark Johnston }; 1142aa14e9b7SMark Johnston 11434d77a549SAlfred Perlstein static void cb_put_phdr(vm_map_entry_t, void *); 11444d77a549SAlfred Perlstein static void cb_size_segment(vm_map_entry_t, void *); 1145aa14e9b7SMark Johnston static int core_write(struct coredump_params *, void *, size_t, off_t, 1146aa14e9b7SMark Johnston enum uio_seg); 1147247aba24SMarcel Moolenaar static void each_writable_segment(struct thread *, segment_callback, void *); 1148aa14e9b7SMark Johnston static int __elfN(corehdr)(struct coredump_params *, int, void *, size_t, 1149aa14e9b7SMark Johnston struct note_info_list *, size_t); 1150bd390213SMikolaj Golub static void __elfN(prepare_notes)(struct thread *, struct note_info_list *, 1151bd390213SMikolaj Golub size_t *); 1152bd390213SMikolaj Golub static void __elfN(puthdr)(struct thread *, void *, size_t, int, size_t); 1153bd390213SMikolaj Golub static void __elfN(putnote)(struct note_info *, struct sbuf *); 1154bd390213SMikolaj Golub static size_t register_note(struct note_info_list *, int, outfunc_t, void *); 1155bd390213SMikolaj Golub static int sbuf_drain_core_output(void *, const char *, int); 1156f1fca82eSMikolaj Golub static int sbuf_drain_count(void *arg, const char *data, int len); 1157bd390213SMikolaj Golub 1158bd390213SMikolaj Golub static void __elfN(note_fpregset)(void *, struct sbuf *, size_t *); 1159bd390213SMikolaj Golub static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *); 1160bd390213SMikolaj Golub static void __elfN(note_prstatus)(void *, struct sbuf *, size_t *); 1161bd390213SMikolaj Golub static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *); 1162bd390213SMikolaj Golub static void __elfN(note_thrmisc)(void *, struct sbuf *, size_t *); 1163f1fca82eSMikolaj Golub static void __elfN(note_procstat_auxv)(void *, struct sbuf *, size_t *); 1164f1fca82eSMikolaj Golub static void __elfN(note_procstat_proc)(void *, struct sbuf *, size_t *); 1165f1fca82eSMikolaj Golub static void __elfN(note_procstat_psstrings)(void *, struct sbuf *, size_t *); 1166f1fca82eSMikolaj Golub static void note_procstat_files(void *, struct sbuf *, size_t *); 1167f1fca82eSMikolaj Golub static void note_procstat_groups(void *, struct sbuf *, size_t *); 1168f1fca82eSMikolaj Golub static void note_procstat_osrel(void *, struct sbuf *, size_t *); 1169f1fca82eSMikolaj Golub static void note_procstat_rlimit(void *, struct sbuf *, size_t *); 1170f1fca82eSMikolaj Golub static void note_procstat_umask(void *, struct sbuf *, size_t *); 1171f1fca82eSMikolaj Golub static void note_procstat_vmmap(void *, struct sbuf *, size_t *); 11728c64af4fSJohn Polstra 1173aa14e9b7SMark Johnston #ifdef GZIO 1174e7228204SAlfred Perlstein extern int compress_user_cores_gzlevel; 1175e7228204SAlfred Perlstein 1176aa14e9b7SMark Johnston /* 1177aa14e9b7SMark Johnston * Write out a core segment to the compression stream. 1178aa14e9b7SMark Johnston */ 1179e7228204SAlfred Perlstein static int 1180aa14e9b7SMark Johnston compress_chunk(struct coredump_params *p, char *base, char *buf, u_int len) 1181aa14e9b7SMark Johnston { 1182aa14e9b7SMark Johnston u_int chunk_len; 1183e7228204SAlfred Perlstein int error; 1184aa14e9b7SMark Johnston 1185aa14e9b7SMark Johnston while (len > 0) { 1186aa14e9b7SMark Johnston chunk_len = MIN(len, CORE_BUF_SIZE); 1187aa14e9b7SMark Johnston copyin(base, buf, chunk_len); 1188aa14e9b7SMark Johnston error = gzio_write(p->gzs, buf, chunk_len); 1189aa14e9b7SMark Johnston if (error != 0) 1190aa14e9b7SMark Johnston break; 1191aa14e9b7SMark Johnston base += chunk_len; 1192aa14e9b7SMark Johnston len -= chunk_len; 1193e7228204SAlfred Perlstein } 1194e7228204SAlfred Perlstein return (error); 1195e7228204SAlfred Perlstein } 1196e7228204SAlfred Perlstein 1197aa14e9b7SMark Johnston static int 1198aa14e9b7SMark Johnston core_gz_write(void *base, size_t len, off_t offset, void *arg) 1199aa14e9b7SMark Johnston { 1200aa14e9b7SMark Johnston 1201aa14e9b7SMark Johnston return (core_write((struct coredump_params *)arg, base, len, offset, 1202aa14e9b7SMark Johnston UIO_SYSSPACE)); 1203aa14e9b7SMark Johnston } 1204aa14e9b7SMark Johnston #endif /* GZIO */ 1205aa14e9b7SMark Johnston 1206aa14e9b7SMark Johnston static int 1207aa14e9b7SMark Johnston core_write(struct coredump_params *p, void *base, size_t len, off_t offset, 1208aa14e9b7SMark Johnston enum uio_seg seg) 1209aa14e9b7SMark Johnston { 1210aa14e9b7SMark Johnston 1211aa14e9b7SMark Johnston return (vn_rdwr_inchunks(UIO_WRITE, p->vp, base, len, offset, 1212aa14e9b7SMark Johnston seg, IO_UNIT | IO_DIRECT | IO_RANGELOCKED, 1213aa14e9b7SMark Johnston p->active_cred, p->file_cred, NULL, p->td)); 1214aa14e9b7SMark Johnston } 1215aa14e9b7SMark Johnston 1216aa14e9b7SMark Johnston static int 1217aa14e9b7SMark Johnston core_output(void *base, size_t len, off_t offset, struct coredump_params *p, 1218aa14e9b7SMark Johnston void *tmpbuf) 1219aa14e9b7SMark Johnston { 1220aa14e9b7SMark Johnston 1221aa14e9b7SMark Johnston #ifdef GZIO 1222aa14e9b7SMark Johnston if (p->gzs != NULL) 1223aa14e9b7SMark Johnston return (compress_chunk(p, base, tmpbuf, len)); 1224bd390213SMikolaj Golub #endif 1225aa14e9b7SMark Johnston return (core_write(p, base, len, offset, UIO_USERSPACE)); 1226aa14e9b7SMark Johnston } 1227bd390213SMikolaj Golub 1228bd390213SMikolaj Golub /* 1229bd390213SMikolaj Golub * Drain into a core file. 1230bd390213SMikolaj Golub */ 1231bd390213SMikolaj Golub static int 1232bd390213SMikolaj Golub sbuf_drain_core_output(void *arg, const char *data, int len) 1233bd390213SMikolaj Golub { 1234aa14e9b7SMark Johnston struct coredump_params *p; 1235f1fca82eSMikolaj Golub int error, locked; 1236bd390213SMikolaj Golub 1237aa14e9b7SMark Johnston p = (struct coredump_params *)arg; 1238f1fca82eSMikolaj Golub 1239f1fca82eSMikolaj Golub /* 1240f1fca82eSMikolaj Golub * Some kern_proc out routines that print to this sbuf may 1241f1fca82eSMikolaj Golub * call us with the process lock held. Draining with the 1242f1fca82eSMikolaj Golub * non-sleepable lock held is unsafe. The lock is needed for 1243f1fca82eSMikolaj Golub * those routines when dumping a live process. In our case we 1244f1fca82eSMikolaj Golub * can safely release the lock before draining and acquire 1245f1fca82eSMikolaj Golub * again after. 1246f1fca82eSMikolaj Golub */ 1247f1fca82eSMikolaj Golub locked = PROC_LOCKED(p->td->td_proc); 1248f1fca82eSMikolaj Golub if (locked) 1249f1fca82eSMikolaj Golub PROC_UNLOCK(p->td->td_proc); 1250aa14e9b7SMark Johnston #ifdef GZIO 1251aa14e9b7SMark Johnston if (p->gzs != NULL) 1252aa14e9b7SMark Johnston error = gzio_write(p->gzs, __DECONST(char *, data), len); 1253bd390213SMikolaj Golub else 1254bd390213SMikolaj Golub #endif 1255aa14e9b7SMark Johnston error = core_write(p, __DECONST(void *, data), len, p->offset, 1256aa14e9b7SMark Johnston UIO_SYSSPACE); 1257f1fca82eSMikolaj Golub if (locked) 1258f1fca82eSMikolaj Golub PROC_LOCK(p->td->td_proc); 1259bd390213SMikolaj Golub if (error != 0) 1260bd390213SMikolaj Golub return (-error); 1261bd390213SMikolaj Golub p->offset += len; 1262bd390213SMikolaj Golub return (len); 1263bd390213SMikolaj Golub } 1264bd390213SMikolaj Golub 1265f1fca82eSMikolaj Golub /* 1266f1fca82eSMikolaj Golub * Drain into a counter. 1267f1fca82eSMikolaj Golub */ 1268f1fca82eSMikolaj Golub static int 1269f1fca82eSMikolaj Golub sbuf_drain_count(void *arg, const char *data __unused, int len) 1270f1fca82eSMikolaj Golub { 1271f1fca82eSMikolaj Golub size_t *sizep; 1272f1fca82eSMikolaj Golub 1273f1fca82eSMikolaj Golub sizep = (size_t *)arg; 1274f1fca82eSMikolaj Golub *sizep += len; 1275f1fca82eSMikolaj Golub return (len); 1276f1fca82eSMikolaj Golub } 1277f1fca82eSMikolaj Golub 12788c64af4fSJohn Polstra int 1279e7228204SAlfred Perlstein __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags) 1280fca666a1SJulian Elischer { 1281247aba24SMarcel Moolenaar struct ucred *cred = td->td_ucred; 1282fca666a1SJulian Elischer int error = 0; 12830ff27d31SJohn Polstra struct sseg_closure seginfo; 1284bd390213SMikolaj Golub struct note_info_list notelst; 1285aa14e9b7SMark Johnston struct coredump_params params; 1286bd390213SMikolaj Golub struct note_info *ninfo; 1287aa14e9b7SMark Johnston void *hdr, *tmpbuf; 1288bd390213SMikolaj Golub size_t hdrsize, notesz, coresize; 12899a12e282SEnji Cooper #ifdef GZIO 1290aa14e9b7SMark Johnston boolean_t compress; 12918c64af4fSJohn Polstra 1292aa14e9b7SMark Johnston compress = (flags & IMGACT_CORE_COMPRESS) != 0; 12939a12e282SEnji Cooper #endif 1294e7228204SAlfred Perlstein hdr = NULL; 129502d131adSMark Johnston tmpbuf = NULL; 1296bd390213SMikolaj Golub TAILQ_INIT(¬elst); 1297e7228204SAlfred Perlstein 12980ff27d31SJohn Polstra /* Size the program segments. */ 12990ff27d31SJohn Polstra seginfo.count = 0; 13000ff27d31SJohn Polstra seginfo.size = 0; 1301247aba24SMarcel Moolenaar each_writable_segment(td, cb_size_segment, &seginfo); 13020ff27d31SJohn Polstra 13030ff27d31SJohn Polstra /* 1304bd390213SMikolaj Golub * Collect info about the core file header area. 13050ff27d31SJohn Polstra */ 1306bd390213SMikolaj Golub hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count); 1307bd390213SMikolaj Golub __elfN(prepare_notes)(td, ¬elst, ¬esz); 1308bd390213SMikolaj Golub coresize = round_page(hdrsize + notesz) + seginfo.size; 13090ff27d31SJohn Polstra 131002d131adSMark Johnston /* Set up core dump parameters. */ 131102d131adSMark Johnston params.offset = 0; 131202d131adSMark Johnston params.active_cred = cred; 131302d131adSMark Johnston params.file_cred = NOCRED; 131402d131adSMark Johnston params.td = td; 131502d131adSMark Johnston params.vp = vp; 131602d131adSMark Johnston params.gzs = NULL; 131702d131adSMark Johnston 1318afcc55f3SEdward Tomasz Napierala #ifdef RACCT 13194b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 13201ba5ad42SEdward Tomasz Napierala PROC_LOCK(td->td_proc); 1321bd390213SMikolaj Golub error = racct_add(td->td_proc, RACCT_CORE, coresize); 13221ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 13231ba5ad42SEdward Tomasz Napierala if (error != 0) { 13241ba5ad42SEdward Tomasz Napierala error = EFAULT; 13251ba5ad42SEdward Tomasz Napierala goto done; 13261ba5ad42SEdward Tomasz Napierala } 13274b5c9cf6SEdward Tomasz Napierala } 1328afcc55f3SEdward Tomasz Napierala #endif 1329bd390213SMikolaj Golub if (coresize >= limit) { 1330fba6b1afSAlfred Perlstein error = EFAULT; 1331fba6b1afSAlfred Perlstein goto done; 1332fba6b1afSAlfred Perlstein } 13330ff27d31SJohn Polstra 1334aa14e9b7SMark Johnston #ifdef GZIO 1335aa14e9b7SMark Johnston /* Create a compression stream if necessary. */ 1336aa14e9b7SMark Johnston if (compress) { 1337aa14e9b7SMark Johnston params.gzs = gzio_init(core_gz_write, GZIO_DEFLATE, 1338aa14e9b7SMark Johnston CORE_BUF_SIZE, compress_user_cores_gzlevel, ¶ms); 1339aa14e9b7SMark Johnston if (params.gzs == NULL) { 1340aa14e9b7SMark Johnston error = EFAULT; 1341aa14e9b7SMark Johnston goto done; 1342aa14e9b7SMark Johnston } 1343aa14e9b7SMark Johnston tmpbuf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO); 1344aa14e9b7SMark Johnston } 1345aa14e9b7SMark Johnston #endif 1346aa14e9b7SMark Johnston 13470ff27d31SJohn Polstra /* 13480ff27d31SJohn Polstra * Allocate memory for building the header, fill it up, 1349bd390213SMikolaj Golub * and write it out following the notes. 13500ff27d31SJohn Polstra */ 1351a163d034SWarner Losh hdr = malloc(hdrsize, M_TEMP, M_WAITOK); 13520ff27d31SJohn Polstra if (hdr == NULL) { 1353fba6b1afSAlfred Perlstein error = EINVAL; 1354fba6b1afSAlfred Perlstein goto done; 13550ff27d31SJohn Polstra } 1356aa14e9b7SMark Johnston error = __elfN(corehdr)(¶ms, seginfo.count, hdr, hdrsize, ¬elst, 1357aa14e9b7SMark Johnston notesz); 13580ff27d31SJohn Polstra 13590ff27d31SJohn Polstra /* Write the contents of all of the writable segments. */ 13600ff27d31SJohn Polstra if (error == 0) { 13610ff27d31SJohn Polstra Elf_Phdr *php; 13622b471bc6STim J. Robbins off_t offset; 13630ff27d31SJohn Polstra int i; 13640ff27d31SJohn Polstra 13650ff27d31SJohn Polstra php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1; 1366bd390213SMikolaj Golub offset = round_page(hdrsize + notesz); 13670ff27d31SJohn Polstra for (i = 0; i < seginfo.count; i++) { 1368aa14e9b7SMark Johnston error = core_output((caddr_t)(uintptr_t)php->p_vaddr, 1369aa14e9b7SMark Johnston php->p_filesz, offset, ¶ms, tmpbuf); 13700ff27d31SJohn Polstra if (error != 0) 13712b471bc6STim J. Robbins break; 13720ff27d31SJohn Polstra offset += php->p_filesz; 13730ff27d31SJohn Polstra php++; 13740ff27d31SJohn Polstra } 1375aa14e9b7SMark Johnston #ifdef GZIO 1376aa14e9b7SMark Johnston if (error == 0 && compress) 1377aa14e9b7SMark Johnston error = gzio_flush(params.gzs); 1378aa14e9b7SMark Johnston #endif 13790ff27d31SJohn Polstra } 1380e7228204SAlfred Perlstein if (error) { 1381e7228204SAlfred Perlstein log(LOG_WARNING, 1382e7228204SAlfred Perlstein "Failed to write core file for process %s (error %d)\n", 1383e7228204SAlfred Perlstein curproc->p_comm, error); 1384e7228204SAlfred Perlstein } 1385e7228204SAlfred Perlstein 1386e7228204SAlfred Perlstein done: 1387aa14e9b7SMark Johnston #ifdef GZIO 1388aa14e9b7SMark Johnston if (compress) { 1389aa14e9b7SMark Johnston free(tmpbuf, M_TEMP); 139002d131adSMark Johnston if (params.gzs != NULL) 1391aa14e9b7SMark Johnston gzio_fini(params.gzs); 1392aa14e9b7SMark Johnston } 13938b325009SAlfred Perlstein #endif 1394bd390213SMikolaj Golub while ((ninfo = TAILQ_FIRST(¬elst)) != NULL) { 1395bd390213SMikolaj Golub TAILQ_REMOVE(¬elst, ninfo, link); 1396bd390213SMikolaj Golub free(ninfo, M_TEMP); 1397bd390213SMikolaj Golub } 1398bd390213SMikolaj Golub if (hdr != NULL) 13990ff27d31SJohn Polstra free(hdr, M_TEMP); 14000ff27d31SJohn Polstra 1401a7cddfedSJake Burkholder return (error); 14028c64af4fSJohn Polstra } 14038c64af4fSJohn Polstra 14040ff27d31SJohn Polstra /* 14050ff27d31SJohn Polstra * A callback for each_writable_segment() to write out the segment's 14060ff27d31SJohn Polstra * program header entry. 14070ff27d31SJohn Polstra */ 14080ff27d31SJohn Polstra static void 14090ff27d31SJohn Polstra cb_put_phdr(entry, closure) 14100ff27d31SJohn Polstra vm_map_entry_t entry; 14110ff27d31SJohn Polstra void *closure; 14120ff27d31SJohn Polstra { 14130ff27d31SJohn Polstra struct phdr_closure *phc = (struct phdr_closure *)closure; 14140ff27d31SJohn Polstra Elf_Phdr *phdr = phc->phdr; 14150ff27d31SJohn Polstra 14160ff27d31SJohn Polstra phc->offset = round_page(phc->offset); 14170ff27d31SJohn Polstra 14180ff27d31SJohn Polstra phdr->p_type = PT_LOAD; 14190ff27d31SJohn Polstra phdr->p_offset = phc->offset; 14200ff27d31SJohn Polstra phdr->p_vaddr = entry->start; 14210ff27d31SJohn Polstra phdr->p_paddr = 0; 14220ff27d31SJohn Polstra phdr->p_filesz = phdr->p_memsz = entry->end - entry->start; 14230ff27d31SJohn Polstra phdr->p_align = PAGE_SIZE; 1424ed167eaaSKonstantin Belousov phdr->p_flags = __elfN(untrans_prot)(entry->protection); 14250ff27d31SJohn Polstra 14260ff27d31SJohn Polstra phc->offset += phdr->p_filesz; 14270ff27d31SJohn Polstra phc->phdr++; 14280ff27d31SJohn Polstra } 14290ff27d31SJohn Polstra 14300ff27d31SJohn Polstra /* 14310ff27d31SJohn Polstra * A callback for each_writable_segment() to gather information about 14320ff27d31SJohn Polstra * the number of segments and their total size. 14330ff27d31SJohn Polstra */ 14340ff27d31SJohn Polstra static void 14350ff27d31SJohn Polstra cb_size_segment(entry, closure) 14360ff27d31SJohn Polstra vm_map_entry_t entry; 14370ff27d31SJohn Polstra void *closure; 14380ff27d31SJohn Polstra { 14390ff27d31SJohn Polstra struct sseg_closure *ssc = (struct sseg_closure *)closure; 14400ff27d31SJohn Polstra 14410ff27d31SJohn Polstra ssc->count++; 14420ff27d31SJohn Polstra ssc->size += entry->end - entry->start; 14430ff27d31SJohn Polstra } 14440ff27d31SJohn Polstra 14450ff27d31SJohn Polstra /* 14460ff27d31SJohn Polstra * For each writable segment in the process's memory map, call the given 14470ff27d31SJohn Polstra * function with a pointer to the map entry and some arbitrary 14480ff27d31SJohn Polstra * caller-supplied data. 14490ff27d31SJohn Polstra */ 14500ff27d31SJohn Polstra static void 1451247aba24SMarcel Moolenaar each_writable_segment(td, func, closure) 1452247aba24SMarcel Moolenaar struct thread *td; 14530ff27d31SJohn Polstra segment_callback func; 14540ff27d31SJohn Polstra void *closure; 14550ff27d31SJohn Polstra { 1456247aba24SMarcel Moolenaar struct proc *p = td->td_proc; 14570ff27d31SJohn Polstra vm_map_t map = &p->p_vmspace->vm_map; 14580ff27d31SJohn Polstra vm_map_entry_t entry; 1459976a87a2SAlan Cox vm_object_t backing_object, object; 1460976a87a2SAlan Cox boolean_t ignore_entry; 14610ff27d31SJohn Polstra 1462976a87a2SAlan Cox vm_map_lock_read(map); 14630ff27d31SJohn Polstra for (entry = map->header.next; entry != &map->header; 14640ff27d31SJohn Polstra entry = entry->next) { 1465fa7dd9c5SMatthew Dillon /* 1466fa7dd9c5SMatthew Dillon * Don't dump inaccessible mappings, deal with legacy 1467fa7dd9c5SMatthew Dillon * coredump mode. 1468fa7dd9c5SMatthew Dillon * 1469fa7dd9c5SMatthew Dillon * Note that read-only segments related to the elf binary 1470fa7dd9c5SMatthew Dillon * are marked MAP_ENTRY_NOCOREDUMP now so we no longer 1471fa7dd9c5SMatthew Dillon * need to arbitrarily ignore such segments. 1472fa7dd9c5SMatthew Dillon */ 1473fa7dd9c5SMatthew Dillon if (elf_legacy_coredump) { 1474fa7dd9c5SMatthew Dillon if ((entry->protection & VM_PROT_RW) != VM_PROT_RW) 14750ff27d31SJohn Polstra continue; 1476fa7dd9c5SMatthew Dillon } else { 1477fa7dd9c5SMatthew Dillon if ((entry->protection & VM_PROT_ALL) == 0) 1478fa7dd9c5SMatthew Dillon continue; 1479fa7dd9c5SMatthew Dillon } 14800ff27d31SJohn Polstra 14819730a5daSPaul Saab /* 1482fa7dd9c5SMatthew Dillon * Dont include memory segment in the coredump if 1483fa7dd9c5SMatthew Dillon * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in 1484fa7dd9c5SMatthew Dillon * madvise(2). Do not dump submaps (i.e. parts of the 1485fa7dd9c5SMatthew Dillon * kernel map). 14869730a5daSPaul Saab */ 1487fa7dd9c5SMatthew Dillon if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP)) 14889730a5daSPaul Saab continue; 14899730a5daSPaul Saab 1490976a87a2SAlan Cox if ((object = entry->object.vm_object) == NULL) 14910ff27d31SJohn Polstra continue; 14920ff27d31SJohn Polstra 14930ff27d31SJohn Polstra /* Ignore memory-mapped devices and such things. */ 1494bc403f03SAttilio Rao VM_OBJECT_RLOCK(object); 1495976a87a2SAlan Cox while ((backing_object = object->backing_object) != NULL) { 1496bc403f03SAttilio Rao VM_OBJECT_RLOCK(backing_object); 1497bc403f03SAttilio Rao VM_OBJECT_RUNLOCK(object); 1498976a87a2SAlan Cox object = backing_object; 1499976a87a2SAlan Cox } 1500976a87a2SAlan Cox ignore_entry = object->type != OBJT_DEFAULT && 1501bc411bc2SJohn Baldwin object->type != OBJT_SWAP && object->type != OBJT_VNODE && 1502bc411bc2SJohn Baldwin object->type != OBJT_PHYS; 1503bc403f03SAttilio Rao VM_OBJECT_RUNLOCK(object); 1504976a87a2SAlan Cox if (ignore_entry) 15050ff27d31SJohn Polstra continue; 15060ff27d31SJohn Polstra 15070ff27d31SJohn Polstra (*func)(entry, closure); 15080ff27d31SJohn Polstra } 1509976a87a2SAlan Cox vm_map_unlock_read(map); 15100ff27d31SJohn Polstra } 15110ff27d31SJohn Polstra 15120ff27d31SJohn Polstra /* 15130ff27d31SJohn Polstra * Write the core file header to the file, including padding up to 15140ff27d31SJohn Polstra * the page boundary. 15150ff27d31SJohn Polstra */ 15168c64af4fSJohn Polstra static int 1517aa14e9b7SMark Johnston __elfN(corehdr)(struct coredump_params *p, int numsegs, void *hdr, 1518aa14e9b7SMark Johnston size_t hdrsize, struct note_info_list *notelst, size_t notesz) 15198c64af4fSJohn Polstra { 1520bd390213SMikolaj Golub struct note_info *ninfo; 1521bd390213SMikolaj Golub struct sbuf *sb; 1522bd390213SMikolaj Golub int error; 15238c64af4fSJohn Polstra 15248c64af4fSJohn Polstra /* Fill in the header. */ 15250ff27d31SJohn Polstra bzero(hdr, hdrsize); 1526aa14e9b7SMark Johnston __elfN(puthdr)(p->td, hdr, hdrsize, numsegs, notesz); 15278c64af4fSJohn Polstra 1528bd390213SMikolaj Golub sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN); 1529aa14e9b7SMark Johnston sbuf_set_drain(sb, sbuf_drain_core_output, p); 1530bd390213SMikolaj Golub sbuf_start_section(sb, NULL); 1531bd390213SMikolaj Golub sbuf_bcat(sb, hdr, hdrsize); 1532bd390213SMikolaj Golub TAILQ_FOREACH(ninfo, notelst, link) 1533bd390213SMikolaj Golub __elfN(putnote)(ninfo, sb); 1534bd390213SMikolaj Golub /* Align up to a page boundary for the program segments. */ 1535bd390213SMikolaj Golub sbuf_end_section(sb, -1, PAGE_SIZE, 0); 1536bd390213SMikolaj Golub error = sbuf_finish(sb); 1537bd390213SMikolaj Golub sbuf_delete(sb); 1538bd390213SMikolaj Golub 1539bd390213SMikolaj Golub return (error); 1540e7228204SAlfred Perlstein } 1541bd390213SMikolaj Golub 1542bd390213SMikolaj Golub static void 1543bd390213SMikolaj Golub __elfN(prepare_notes)(struct thread *td, struct note_info_list *list, 1544bd390213SMikolaj Golub size_t *sizep) 1545bd390213SMikolaj Golub { 1546bd390213SMikolaj Golub struct proc *p; 1547bd390213SMikolaj Golub struct thread *thr; 1548bd390213SMikolaj Golub size_t size; 1549bd390213SMikolaj Golub 1550bd390213SMikolaj Golub p = td->td_proc; 1551bd390213SMikolaj Golub size = 0; 1552bd390213SMikolaj Golub 1553bd390213SMikolaj Golub size += register_note(list, NT_PRPSINFO, __elfN(note_prpsinfo), p); 1554bd390213SMikolaj Golub 1555bd390213SMikolaj Golub /* 1556bd390213SMikolaj Golub * To have the debugger select the right thread (LWP) as the initial 1557bd390213SMikolaj Golub * thread, we dump the state of the thread passed to us in td first. 1558bd390213SMikolaj Golub * This is the thread that causes the core dump and thus likely to 1559bd390213SMikolaj Golub * be the right thread one wants to have selected in the debugger. 1560bd390213SMikolaj Golub */ 1561bd390213SMikolaj Golub thr = td; 1562bd390213SMikolaj Golub while (thr != NULL) { 1563bd390213SMikolaj Golub size += register_note(list, NT_PRSTATUS, 1564bd390213SMikolaj Golub __elfN(note_prstatus), thr); 1565bd390213SMikolaj Golub size += register_note(list, NT_FPREGSET, 1566bd390213SMikolaj Golub __elfN(note_fpregset), thr); 1567bd390213SMikolaj Golub size += register_note(list, NT_THRMISC, 1568bd390213SMikolaj Golub __elfN(note_thrmisc), thr); 1569bd390213SMikolaj Golub size += register_note(list, -1, 1570bd390213SMikolaj Golub __elfN(note_threadmd), thr); 1571bd390213SMikolaj Golub 1572bd390213SMikolaj Golub thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) : 1573bd390213SMikolaj Golub TAILQ_NEXT(thr, td_plist); 1574bd390213SMikolaj Golub if (thr == td) 1575bd390213SMikolaj Golub thr = TAILQ_NEXT(thr, td_plist); 1576dada0278SJohn Polstra } 1577dada0278SJohn Polstra 1578f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_PROC, 1579f1fca82eSMikolaj Golub __elfN(note_procstat_proc), p); 1580f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_FILES, 1581f1fca82eSMikolaj Golub note_procstat_files, p); 1582f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_VMMAP, 1583f1fca82eSMikolaj Golub note_procstat_vmmap, p); 1584f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_GROUPS, 1585f1fca82eSMikolaj Golub note_procstat_groups, p); 1586f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_UMASK, 1587f1fca82eSMikolaj Golub note_procstat_umask, p); 1588f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_RLIMIT, 1589f1fca82eSMikolaj Golub note_procstat_rlimit, p); 1590f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_OSREL, 1591f1fca82eSMikolaj Golub note_procstat_osrel, p); 1592f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_PSSTRINGS, 1593f1fca82eSMikolaj Golub __elfN(note_procstat_psstrings), p); 1594f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_AUXV, 1595f1fca82eSMikolaj Golub __elfN(note_procstat_auxv), p); 1596f1fca82eSMikolaj Golub 1597bd390213SMikolaj Golub *sizep = size; 1598bd390213SMikolaj Golub } 1599bd390213SMikolaj Golub 1600bd390213SMikolaj Golub static void 1601bd390213SMikolaj Golub __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs, 1602bd390213SMikolaj Golub size_t notesz) 1603bd390213SMikolaj Golub { 1604bd390213SMikolaj Golub Elf_Ehdr *ehdr; 1605bd390213SMikolaj Golub Elf_Phdr *phdr; 1606bd390213SMikolaj Golub struct phdr_closure phc; 1607bd390213SMikolaj Golub 1608bd390213SMikolaj Golub ehdr = (Elf_Ehdr *)hdr; 1609bd390213SMikolaj Golub phdr = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)); 1610bd390213SMikolaj Golub 1611bd390213SMikolaj Golub ehdr->e_ident[EI_MAG0] = ELFMAG0; 1612bd390213SMikolaj Golub ehdr->e_ident[EI_MAG1] = ELFMAG1; 1613bd390213SMikolaj Golub ehdr->e_ident[EI_MAG2] = ELFMAG2; 1614bd390213SMikolaj Golub ehdr->e_ident[EI_MAG3] = ELFMAG3; 1615bd390213SMikolaj Golub ehdr->e_ident[EI_CLASS] = ELF_CLASS; 1616bd390213SMikolaj Golub ehdr->e_ident[EI_DATA] = ELF_DATA; 1617bd390213SMikolaj Golub ehdr->e_ident[EI_VERSION] = EV_CURRENT; 1618bd390213SMikolaj Golub ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD; 1619bd390213SMikolaj Golub ehdr->e_ident[EI_ABIVERSION] = 0; 1620bd390213SMikolaj Golub ehdr->e_ident[EI_PAD] = 0; 1621bd390213SMikolaj Golub ehdr->e_type = ET_CORE; 1622bd390213SMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 1623bd390213SMikolaj Golub ehdr->e_machine = ELF_ARCH32; 1624bd390213SMikolaj Golub #else 1625bd390213SMikolaj Golub ehdr->e_machine = ELF_ARCH; 1626bd390213SMikolaj Golub #endif 1627bd390213SMikolaj Golub ehdr->e_version = EV_CURRENT; 1628bd390213SMikolaj Golub ehdr->e_entry = 0; 1629bd390213SMikolaj Golub ehdr->e_phoff = sizeof(Elf_Ehdr); 1630bd390213SMikolaj Golub ehdr->e_flags = 0; 1631bd390213SMikolaj Golub ehdr->e_ehsize = sizeof(Elf_Ehdr); 1632bd390213SMikolaj Golub ehdr->e_phentsize = sizeof(Elf_Phdr); 1633bd390213SMikolaj Golub ehdr->e_phnum = numsegs + 1; 1634bd390213SMikolaj Golub ehdr->e_shentsize = sizeof(Elf_Shdr); 1635bd390213SMikolaj Golub ehdr->e_shnum = 0; 1636bd390213SMikolaj Golub ehdr->e_shstrndx = SHN_UNDEF; 1637bd390213SMikolaj Golub 1638bd390213SMikolaj Golub /* 1639bd390213SMikolaj Golub * Fill in the program header entries. 1640bd390213SMikolaj Golub */ 1641bd390213SMikolaj Golub 1642bd390213SMikolaj Golub /* The note segement. */ 1643bd390213SMikolaj Golub phdr->p_type = PT_NOTE; 1644bd390213SMikolaj Golub phdr->p_offset = hdrsize; 1645bd390213SMikolaj Golub phdr->p_vaddr = 0; 1646bd390213SMikolaj Golub phdr->p_paddr = 0; 1647bd390213SMikolaj Golub phdr->p_filesz = notesz; 1648bd390213SMikolaj Golub phdr->p_memsz = 0; 1649bd390213SMikolaj Golub phdr->p_flags = PF_R; 16501b8388cdSMikolaj Golub phdr->p_align = ELF_NOTE_ROUNDSIZE; 1651bd390213SMikolaj Golub phdr++; 1652bd390213SMikolaj Golub 1653bd390213SMikolaj Golub /* All the writable segments from the program. */ 1654bd390213SMikolaj Golub phc.phdr = phdr; 1655bd390213SMikolaj Golub phc.offset = round_page(hdrsize + notesz); 1656bd390213SMikolaj Golub each_writable_segment(td, cb_put_phdr, &phc); 1657bd390213SMikolaj Golub } 1658bd390213SMikolaj Golub 1659bd390213SMikolaj Golub static size_t 1660bd390213SMikolaj Golub register_note(struct note_info_list *list, int type, outfunc_t out, void *arg) 1661bd390213SMikolaj Golub { 1662bd390213SMikolaj Golub struct note_info *ninfo; 1663bd390213SMikolaj Golub size_t size, notesize; 1664bd390213SMikolaj Golub 1665bd390213SMikolaj Golub size = 0; 1666bd390213SMikolaj Golub out(arg, NULL, &size); 1667bd390213SMikolaj Golub ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK); 1668bd390213SMikolaj Golub ninfo->type = type; 1669bd390213SMikolaj Golub ninfo->outfunc = out; 1670bd390213SMikolaj Golub ninfo->outarg = arg; 1671bd390213SMikolaj Golub ninfo->outsize = size; 1672bd390213SMikolaj Golub TAILQ_INSERT_TAIL(list, ninfo, link); 1673bd390213SMikolaj Golub 1674bd390213SMikolaj Golub if (type == -1) 1675bd390213SMikolaj Golub return (size); 1676bd390213SMikolaj Golub 1677bd390213SMikolaj Golub notesize = sizeof(Elf_Note) + /* note header */ 1678180e57e5SJohn Baldwin roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) + 1679180e57e5SJohn Baldwin /* note name */ 1680180e57e5SJohn Baldwin roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */ 1681180e57e5SJohn Baldwin 1682180e57e5SJohn Baldwin return (notesize); 1683180e57e5SJohn Baldwin } 1684180e57e5SJohn Baldwin 1685180e57e5SJohn Baldwin static size_t 1686180e57e5SJohn Baldwin append_note_data(const void *src, void *dst, size_t len) 1687180e57e5SJohn Baldwin { 1688180e57e5SJohn Baldwin size_t padded_len; 1689180e57e5SJohn Baldwin 1690180e57e5SJohn Baldwin padded_len = roundup2(len, ELF_NOTE_ROUNDSIZE); 1691180e57e5SJohn Baldwin if (dst != NULL) { 1692180e57e5SJohn Baldwin bcopy(src, dst, len); 1693180e57e5SJohn Baldwin bzero((char *)dst + len, padded_len - len); 1694180e57e5SJohn Baldwin } 1695180e57e5SJohn Baldwin return (padded_len); 1696180e57e5SJohn Baldwin } 1697180e57e5SJohn Baldwin 1698180e57e5SJohn Baldwin size_t 1699180e57e5SJohn Baldwin __elfN(populate_note)(int type, void *src, void *dst, size_t size, void **descp) 1700180e57e5SJohn Baldwin { 1701180e57e5SJohn Baldwin Elf_Note *note; 1702180e57e5SJohn Baldwin char *buf; 1703180e57e5SJohn Baldwin size_t notesize; 1704180e57e5SJohn Baldwin 1705180e57e5SJohn Baldwin buf = dst; 1706180e57e5SJohn Baldwin if (buf != NULL) { 1707180e57e5SJohn Baldwin note = (Elf_Note *)buf; 1708180e57e5SJohn Baldwin note->n_namesz = sizeof(FREEBSD_ABI_VENDOR); 1709180e57e5SJohn Baldwin note->n_descsz = size; 1710180e57e5SJohn Baldwin note->n_type = type; 1711180e57e5SJohn Baldwin buf += sizeof(*note); 1712180e57e5SJohn Baldwin buf += append_note_data(FREEBSD_ABI_VENDOR, buf, 1713180e57e5SJohn Baldwin sizeof(FREEBSD_ABI_VENDOR)); 1714180e57e5SJohn Baldwin append_note_data(src, buf, size); 1715180e57e5SJohn Baldwin if (descp != NULL) 1716180e57e5SJohn Baldwin *descp = buf; 1717180e57e5SJohn Baldwin } 1718180e57e5SJohn Baldwin 1719180e57e5SJohn Baldwin notesize = sizeof(Elf_Note) + /* note header */ 1720180e57e5SJohn Baldwin roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) + 1721180e57e5SJohn Baldwin /* note name */ 17221b8388cdSMikolaj Golub roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */ 1723bd390213SMikolaj Golub 1724bd390213SMikolaj Golub return (notesize); 1725bd390213SMikolaj Golub } 1726bd390213SMikolaj Golub 1727bd390213SMikolaj Golub static void 1728bd390213SMikolaj Golub __elfN(putnote)(struct note_info *ninfo, struct sbuf *sb) 1729bd390213SMikolaj Golub { 1730bd390213SMikolaj Golub Elf_Note note; 173114bdbaf2SConrad Meyer ssize_t old_len, sect_len; 173214bdbaf2SConrad Meyer size_t new_len, descsz, i; 1733bd390213SMikolaj Golub 1734bd390213SMikolaj Golub if (ninfo->type == -1) { 1735bd390213SMikolaj Golub ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize); 1736bd390213SMikolaj Golub return; 1737bd390213SMikolaj Golub } 1738bd390213SMikolaj Golub 1739180e57e5SJohn Baldwin note.n_namesz = sizeof(FREEBSD_ABI_VENDOR); 1740bd390213SMikolaj Golub note.n_descsz = ninfo->outsize; 1741bd390213SMikolaj Golub note.n_type = ninfo->type; 1742bd390213SMikolaj Golub 1743bd390213SMikolaj Golub sbuf_bcat(sb, ¬e, sizeof(note)); 1744bd390213SMikolaj Golub sbuf_start_section(sb, &old_len); 1745180e57e5SJohn Baldwin sbuf_bcat(sb, FREEBSD_ABI_VENDOR, sizeof(FREEBSD_ABI_VENDOR)); 17461b8388cdSMikolaj Golub sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0); 1747bd390213SMikolaj Golub if (note.n_descsz == 0) 1748bd390213SMikolaj Golub return; 1749bd390213SMikolaj Golub sbuf_start_section(sb, &old_len); 1750bd390213SMikolaj Golub ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize); 175114bdbaf2SConrad Meyer sect_len = sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0); 175214bdbaf2SConrad Meyer if (sect_len < 0) 175314bdbaf2SConrad Meyer return; 175414bdbaf2SConrad Meyer 175514bdbaf2SConrad Meyer new_len = (size_t)sect_len; 175614bdbaf2SConrad Meyer descsz = roundup(note.n_descsz, ELF_NOTE_ROUNDSIZE); 175714bdbaf2SConrad Meyer if (new_len < descsz) { 175814bdbaf2SConrad Meyer /* 175914bdbaf2SConrad Meyer * It is expected that individual note emitters will correctly 176014bdbaf2SConrad Meyer * predict their expected output size and fill up to that size 176114bdbaf2SConrad Meyer * themselves, padding in a format-specific way if needed. 176214bdbaf2SConrad Meyer * However, in case they don't, just do it here with zeros. 176314bdbaf2SConrad Meyer */ 176414bdbaf2SConrad Meyer for (i = 0; i < descsz - new_len; i++) 176514bdbaf2SConrad Meyer sbuf_putc(sb, 0); 176614bdbaf2SConrad Meyer } else if (new_len > descsz) { 176714bdbaf2SConrad Meyer /* 176814bdbaf2SConrad Meyer * We can't always truncate sb -- we may have drained some 176914bdbaf2SConrad Meyer * of it already. 177014bdbaf2SConrad Meyer */ 177114bdbaf2SConrad Meyer KASSERT(new_len == descsz, ("%s: Note type %u changed as we " 177214bdbaf2SConrad Meyer "read it (%zu > %zu). Since it is longer than " 177314bdbaf2SConrad Meyer "expected, this coredump's notes are corrupt. THIS " 177414bdbaf2SConrad Meyer "IS A BUG in the note_procstat routine for type %u.\n", 177514bdbaf2SConrad Meyer __func__, (unsigned)note.n_type, new_len, descsz, 177614bdbaf2SConrad Meyer (unsigned)note.n_type)); 177714bdbaf2SConrad Meyer } 1778bd390213SMikolaj Golub } 1779bd390213SMikolaj Golub 1780bd390213SMikolaj Golub /* 1781bd390213SMikolaj Golub * Miscellaneous note out functions. 1782bd390213SMikolaj Golub */ 1783bd390213SMikolaj Golub 1784841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 1785841c0c7eSNathan Whitehorn #include <compat/freebsd32/freebsd32.h> 1786841c0c7eSNathan Whitehorn 178762919d78SPeter Wemm typedef struct prstatus32 elf_prstatus_t; 178862919d78SPeter Wemm typedef struct prpsinfo32 elf_prpsinfo_t; 178962919d78SPeter Wemm typedef struct fpreg32 elf_prfpregset_t; 179062919d78SPeter Wemm typedef struct fpreg32 elf_fpregset_t; 179162919d78SPeter Wemm typedef struct reg32 elf_gregset_t; 17927f08176eSAttilio Rao typedef struct thrmisc32 elf_thrmisc_t; 1793f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK KERN_PROC_MASK32 1794f1fca82eSMikolaj Golub typedef struct kinfo_proc32 elf_kinfo_proc_t; 1795f1fca82eSMikolaj Golub typedef uint32_t elf_ps_strings_t; 179662919d78SPeter Wemm #else 179762919d78SPeter Wemm typedef prstatus_t elf_prstatus_t; 179862919d78SPeter Wemm typedef prpsinfo_t elf_prpsinfo_t; 179962919d78SPeter Wemm typedef prfpregset_t elf_prfpregset_t; 180062919d78SPeter Wemm typedef prfpregset_t elf_fpregset_t; 180162919d78SPeter Wemm typedef gregset_t elf_gregset_t; 18027f08176eSAttilio Rao typedef thrmisc_t elf_thrmisc_t; 1803f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK 0 1804f1fca82eSMikolaj Golub typedef struct kinfo_proc elf_kinfo_proc_t; 1805f1fca82eSMikolaj Golub typedef vm_offset_t elf_ps_strings_t; 180662919d78SPeter Wemm #endif 180762919d78SPeter Wemm 18088c64af4fSJohn Polstra static void 1809bd390213SMikolaj Golub __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep) 18108c64af4fSJohn Polstra { 1811247aba24SMarcel Moolenaar struct proc *p; 1812bd390213SMikolaj Golub elf_prpsinfo_t *psinfo; 18138c64af4fSJohn Polstra 1814bd390213SMikolaj Golub p = (struct proc *)arg; 1815bd390213SMikolaj Golub if (sb != NULL) { 1816bd390213SMikolaj Golub KASSERT(*sizep == sizeof(*psinfo), ("invalid size")); 1817bd390213SMikolaj Golub psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK); 18188c9b7b2cSMarcel Moolenaar psinfo->pr_version = PRPSINFO_VERSION; 181962919d78SPeter Wemm psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t); 1820ccd3953eSJohn Baldwin strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname)); 18218c9b7b2cSMarcel Moolenaar /* 18228c9b7b2cSMarcel Moolenaar * XXX - We don't fill in the command line arguments properly 18238c9b7b2cSMarcel Moolenaar * yet. 18248c9b7b2cSMarcel Moolenaar */ 1825ccd3953eSJohn Baldwin strlcpy(psinfo->pr_psargs, p->p_comm, 18268c9b7b2cSMarcel Moolenaar sizeof(psinfo->pr_psargs)); 18278c9b7b2cSMarcel Moolenaar 1828bd390213SMikolaj Golub sbuf_bcat(sb, psinfo, sizeof(*psinfo)); 1829bd390213SMikolaj Golub free(psinfo, M_TEMP); 1830bd390213SMikolaj Golub } 1831bd390213SMikolaj Golub *sizep = sizeof(*psinfo); 1832bd390213SMikolaj Golub } 1833bd390213SMikolaj Golub 1834bd390213SMikolaj Golub static void 1835bd390213SMikolaj Golub __elfN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep) 1836bd390213SMikolaj Golub { 1837bd390213SMikolaj Golub struct thread *td; 1838bd390213SMikolaj Golub elf_prstatus_t *status; 1839bd390213SMikolaj Golub 1840bd390213SMikolaj Golub td = (struct thread *)arg; 1841bd390213SMikolaj Golub if (sb != NULL) { 1842bd390213SMikolaj Golub KASSERT(*sizep == sizeof(*status), ("invalid size")); 1843bd390213SMikolaj Golub status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK); 18448c9b7b2cSMarcel Moolenaar status->pr_version = PRSTATUS_VERSION; 184562919d78SPeter Wemm status->pr_statussz = sizeof(elf_prstatus_t); 184662919d78SPeter Wemm status->pr_gregsetsz = sizeof(elf_gregset_t); 184762919d78SPeter Wemm status->pr_fpregsetsz = sizeof(elf_fpregset_t); 18488c9b7b2cSMarcel Moolenaar status->pr_osreldate = osreldate; 1849bd390213SMikolaj Golub status->pr_cursig = td->td_proc->p_sig; 1850bd390213SMikolaj Golub status->pr_pid = td->td_tid; 1851841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 1852bd390213SMikolaj Golub fill_regs32(td, &status->pr_reg); 185362919d78SPeter Wemm #else 1854bd390213SMikolaj Golub fill_regs(td, &status->pr_reg); 185562919d78SPeter Wemm #endif 1856bd390213SMikolaj Golub sbuf_bcat(sb, status, sizeof(*status)); 1857bd390213SMikolaj Golub free(status, M_TEMP); 18588c9b7b2cSMarcel Moolenaar } 1859bd390213SMikolaj Golub *sizep = sizeof(*status); 1860bd390213SMikolaj Golub } 1861bd390213SMikolaj Golub 1862bd390213SMikolaj Golub static void 1863bd390213SMikolaj Golub __elfN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep) 1864bd390213SMikolaj Golub { 1865bd390213SMikolaj Golub struct thread *td; 1866bd390213SMikolaj Golub elf_prfpregset_t *fpregset; 1867bd390213SMikolaj Golub 1868bd390213SMikolaj Golub td = (struct thread *)arg; 1869bd390213SMikolaj Golub if (sb != NULL) { 1870bd390213SMikolaj Golub KASSERT(*sizep == sizeof(*fpregset), ("invalid size")); 1871bd390213SMikolaj Golub fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK); 1872bd390213SMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 1873bd390213SMikolaj Golub fill_fpregs32(td, fpregset); 1874bd390213SMikolaj Golub #else 1875bd390213SMikolaj Golub fill_fpregs(td, fpregset); 1876bd390213SMikolaj Golub #endif 1877bd390213SMikolaj Golub sbuf_bcat(sb, fpregset, sizeof(*fpregset)); 1878bd390213SMikolaj Golub free(fpregset, M_TEMP); 1879bd390213SMikolaj Golub } 1880bd390213SMikolaj Golub *sizep = sizeof(*fpregset); 1881bd390213SMikolaj Golub } 1882bd390213SMikolaj Golub 1883bd390213SMikolaj Golub static void 1884bd390213SMikolaj Golub __elfN(note_thrmisc)(void *arg, struct sbuf *sb, size_t *sizep) 1885bd390213SMikolaj Golub { 1886bd390213SMikolaj Golub struct thread *td; 1887bd390213SMikolaj Golub elf_thrmisc_t thrmisc; 1888bd390213SMikolaj Golub 1889bd390213SMikolaj Golub td = (struct thread *)arg; 1890bd390213SMikolaj Golub if (sb != NULL) { 1891bd390213SMikolaj Golub KASSERT(*sizep == sizeof(thrmisc), ("invalid size")); 1892bd390213SMikolaj Golub bzero(&thrmisc._pad, sizeof(thrmisc._pad)); 1893bd390213SMikolaj Golub strcpy(thrmisc.pr_tname, td->td_name); 1894bd390213SMikolaj Golub sbuf_bcat(sb, &thrmisc, sizeof(thrmisc)); 1895bd390213SMikolaj Golub } 1896bd390213SMikolaj Golub *sizep = sizeof(thrmisc); 1897bd390213SMikolaj Golub } 1898bd390213SMikolaj Golub 18994da47b2fSMarcel Moolenaar /* 19004da47b2fSMarcel Moolenaar * Allow for MD specific notes, as well as any MD 19014da47b2fSMarcel Moolenaar * specific preparations for writing MI notes. 19024da47b2fSMarcel Moolenaar */ 19038c64af4fSJohn Polstra static void 1904bd390213SMikolaj Golub __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep) 19058c64af4fSJohn Polstra { 1906bd390213SMikolaj Golub struct thread *td; 1907bd390213SMikolaj Golub void *buf; 1908bd390213SMikolaj Golub size_t size; 19098c64af4fSJohn Polstra 1910bd390213SMikolaj Golub td = (struct thread *)arg; 1911bd390213SMikolaj Golub size = *sizep; 1912bd390213SMikolaj Golub if (size != 0 && sb != NULL) 1913bd390213SMikolaj Golub buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK); 191483a396ceSChristian Brueffer else 191583a396ceSChristian Brueffer buf = NULL; 1916bd390213SMikolaj Golub size = 0; 1917bd390213SMikolaj Golub __elfN(dump_thread)(td, buf, &size); 191864779280SKonstantin Belousov KASSERT(sb == NULL || *sizep == size, ("invalid size")); 191983a396ceSChristian Brueffer if (size != 0 && sb != NULL) 1920bd390213SMikolaj Golub sbuf_bcat(sb, buf, size); 1921a1761d73SChristian Brueffer free(buf, M_TEMP); 1922bd390213SMikolaj Golub *sizep = size; 19238c64af4fSJohn Polstra } 19248c64af4fSJohn Polstra 1925f1fca82eSMikolaj Golub #ifdef KINFO_PROC_SIZE 1926f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 1927f1fca82eSMikolaj Golub #endif 1928f1fca82eSMikolaj Golub 1929f1fca82eSMikolaj Golub static void 1930f1fca82eSMikolaj Golub __elfN(note_procstat_proc)(void *arg, struct sbuf *sb, size_t *sizep) 1931f1fca82eSMikolaj Golub { 1932f1fca82eSMikolaj Golub struct proc *p; 1933f1fca82eSMikolaj Golub size_t size; 1934f1fca82eSMikolaj Golub int structsize; 1935f1fca82eSMikolaj Golub 1936f1fca82eSMikolaj Golub p = (struct proc *)arg; 1937f1fca82eSMikolaj Golub size = sizeof(structsize) + p->p_numthreads * 1938f1fca82eSMikolaj Golub sizeof(elf_kinfo_proc_t); 1939f1fca82eSMikolaj Golub 1940f1fca82eSMikolaj Golub if (sb != NULL) { 1941f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 1942f1fca82eSMikolaj Golub structsize = sizeof(elf_kinfo_proc_t); 1943f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 19446662ce5aSMateusz Guzik sx_slock(&proctree_lock); 1945f1fca82eSMikolaj Golub PROC_LOCK(p); 1946f1fca82eSMikolaj Golub kern_proc_out(p, sb, ELF_KERN_PROC_MASK); 19476662ce5aSMateusz Guzik sx_sunlock(&proctree_lock); 1948f1fca82eSMikolaj Golub } 1949f1fca82eSMikolaj Golub *sizep = size; 1950f1fca82eSMikolaj Golub } 1951f1fca82eSMikolaj Golub 1952f1fca82eSMikolaj Golub #ifdef KINFO_FILE_SIZE 1953f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE); 1954f1fca82eSMikolaj Golub #endif 1955f1fca82eSMikolaj Golub 1956f1fca82eSMikolaj Golub static void 1957f1fca82eSMikolaj Golub note_procstat_files(void *arg, struct sbuf *sb, size_t *sizep) 1958f1fca82eSMikolaj Golub { 1959f1fca82eSMikolaj Golub struct proc *p; 196014bdbaf2SConrad Meyer size_t size, sect_sz, i; 196114bdbaf2SConrad Meyer ssize_t start_len, sect_len; 196214bdbaf2SConrad Meyer int structsize, filedesc_flags; 196314bdbaf2SConrad Meyer 1964bcb60d52SConrad Meyer if (coredump_pack_fileinfo) 196514bdbaf2SConrad Meyer filedesc_flags = KERN_FILEDESC_PACK_KINFO; 196614bdbaf2SConrad Meyer else 196714bdbaf2SConrad Meyer filedesc_flags = 0; 1968f1fca82eSMikolaj Golub 1969f1fca82eSMikolaj Golub p = (struct proc *)arg; 197014bdbaf2SConrad Meyer structsize = sizeof(struct kinfo_file); 1971f1fca82eSMikolaj Golub if (sb == NULL) { 1972f1fca82eSMikolaj Golub size = 0; 1973f1fca82eSMikolaj Golub sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN); 1974f1fca82eSMikolaj Golub sbuf_set_drain(sb, sbuf_drain_count, &size); 1975f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 1976f1fca82eSMikolaj Golub PROC_LOCK(p); 197714bdbaf2SConrad Meyer kern_proc_filedesc_out(p, sb, -1, filedesc_flags); 1978f1fca82eSMikolaj Golub sbuf_finish(sb); 1979f1fca82eSMikolaj Golub sbuf_delete(sb); 1980f1fca82eSMikolaj Golub *sizep = size; 1981f1fca82eSMikolaj Golub } else { 198214bdbaf2SConrad Meyer sbuf_start_section(sb, &start_len); 198314bdbaf2SConrad Meyer 1984f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 1985f1fca82eSMikolaj Golub PROC_LOCK(p); 198614bdbaf2SConrad Meyer kern_proc_filedesc_out(p, sb, *sizep - sizeof(structsize), 198714bdbaf2SConrad Meyer filedesc_flags); 198814bdbaf2SConrad Meyer 198914bdbaf2SConrad Meyer sect_len = sbuf_end_section(sb, start_len, 0, 0); 199014bdbaf2SConrad Meyer if (sect_len < 0) 199114bdbaf2SConrad Meyer return; 199214bdbaf2SConrad Meyer sect_sz = sect_len; 199314bdbaf2SConrad Meyer 199414bdbaf2SConrad Meyer KASSERT(sect_sz <= *sizep, 199514bdbaf2SConrad Meyer ("kern_proc_filedesc_out did not respect maxlen; " 199614bdbaf2SConrad Meyer "requested %zu, got %zu", *sizep - sizeof(structsize), 199714bdbaf2SConrad Meyer sect_sz - sizeof(structsize))); 199814bdbaf2SConrad Meyer 199914bdbaf2SConrad Meyer for (i = 0; i < *sizep - sect_sz && sb->s_error == 0; i++) 200014bdbaf2SConrad Meyer sbuf_putc(sb, 0); 2001f1fca82eSMikolaj Golub } 2002f1fca82eSMikolaj Golub } 2003f1fca82eSMikolaj Golub 2004f1fca82eSMikolaj Golub #ifdef KINFO_VMENTRY_SIZE 2005f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE); 2006f1fca82eSMikolaj Golub #endif 2007f1fca82eSMikolaj Golub 2008f1fca82eSMikolaj Golub static void 2009f1fca82eSMikolaj Golub note_procstat_vmmap(void *arg, struct sbuf *sb, size_t *sizep) 2010f1fca82eSMikolaj Golub { 2011f1fca82eSMikolaj Golub struct proc *p; 2012f1fca82eSMikolaj Golub size_t size; 2013e6b95927SConrad Meyer int structsize, vmmap_flags; 2014e6b95927SConrad Meyer 2015e6b95927SConrad Meyer if (coredump_pack_vmmapinfo) 2016e6b95927SConrad Meyer vmmap_flags = KERN_VMMAP_PACK_KINFO; 2017e6b95927SConrad Meyer else 2018e6b95927SConrad Meyer vmmap_flags = 0; 2019f1fca82eSMikolaj Golub 2020f1fca82eSMikolaj Golub p = (struct proc *)arg; 2021e6b95927SConrad Meyer structsize = sizeof(struct kinfo_vmentry); 2022f1fca82eSMikolaj Golub if (sb == NULL) { 2023f1fca82eSMikolaj Golub size = 0; 2024f1fca82eSMikolaj Golub sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN); 2025f1fca82eSMikolaj Golub sbuf_set_drain(sb, sbuf_drain_count, &size); 2026f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2027f1fca82eSMikolaj Golub PROC_LOCK(p); 2028e6b95927SConrad Meyer kern_proc_vmmap_out(p, sb, -1, vmmap_flags); 2029f1fca82eSMikolaj Golub sbuf_finish(sb); 2030f1fca82eSMikolaj Golub sbuf_delete(sb); 2031f1fca82eSMikolaj Golub *sizep = size; 2032f1fca82eSMikolaj Golub } else { 2033f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2034f1fca82eSMikolaj Golub PROC_LOCK(p); 2035e6b95927SConrad Meyer kern_proc_vmmap_out(p, sb, *sizep - sizeof(structsize), 2036e6b95927SConrad Meyer vmmap_flags); 2037f1fca82eSMikolaj Golub } 2038f1fca82eSMikolaj Golub } 2039f1fca82eSMikolaj Golub 2040f1fca82eSMikolaj Golub static void 2041f1fca82eSMikolaj Golub note_procstat_groups(void *arg, struct sbuf *sb, size_t *sizep) 2042f1fca82eSMikolaj Golub { 2043f1fca82eSMikolaj Golub struct proc *p; 2044f1fca82eSMikolaj Golub size_t size; 2045f1fca82eSMikolaj Golub int structsize; 2046f1fca82eSMikolaj Golub 2047f1fca82eSMikolaj Golub p = (struct proc *)arg; 2048f1fca82eSMikolaj Golub size = sizeof(structsize) + p->p_ucred->cr_ngroups * sizeof(gid_t); 2049f1fca82eSMikolaj Golub if (sb != NULL) { 2050f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2051f1fca82eSMikolaj Golub structsize = sizeof(gid_t); 2052f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2053f1fca82eSMikolaj Golub sbuf_bcat(sb, p->p_ucred->cr_groups, p->p_ucred->cr_ngroups * 2054f1fca82eSMikolaj Golub sizeof(gid_t)); 2055f1fca82eSMikolaj Golub } 2056f1fca82eSMikolaj Golub *sizep = size; 2057f1fca82eSMikolaj Golub } 2058f1fca82eSMikolaj Golub 2059f1fca82eSMikolaj Golub static void 2060f1fca82eSMikolaj Golub note_procstat_umask(void *arg, struct sbuf *sb, size_t *sizep) 2061f1fca82eSMikolaj Golub { 2062f1fca82eSMikolaj Golub struct proc *p; 2063f1fca82eSMikolaj Golub size_t size; 2064f1fca82eSMikolaj Golub int structsize; 2065f1fca82eSMikolaj Golub 2066f1fca82eSMikolaj Golub p = (struct proc *)arg; 2067f1fca82eSMikolaj Golub size = sizeof(structsize) + sizeof(p->p_fd->fd_cmask); 2068f1fca82eSMikolaj Golub if (sb != NULL) { 2069f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2070f1fca82eSMikolaj Golub structsize = sizeof(p->p_fd->fd_cmask); 2071f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2072f1fca82eSMikolaj Golub sbuf_bcat(sb, &p->p_fd->fd_cmask, sizeof(p->p_fd->fd_cmask)); 2073f1fca82eSMikolaj Golub } 2074f1fca82eSMikolaj Golub *sizep = size; 2075f1fca82eSMikolaj Golub } 2076f1fca82eSMikolaj Golub 2077f1fca82eSMikolaj Golub static void 2078f1fca82eSMikolaj Golub note_procstat_rlimit(void *arg, struct sbuf *sb, size_t *sizep) 2079f1fca82eSMikolaj Golub { 2080f1fca82eSMikolaj Golub struct proc *p; 2081f1fca82eSMikolaj Golub struct rlimit rlim[RLIM_NLIMITS]; 2082f1fca82eSMikolaj Golub size_t size; 2083f1fca82eSMikolaj Golub int structsize, i; 2084f1fca82eSMikolaj Golub 2085f1fca82eSMikolaj Golub p = (struct proc *)arg; 2086f1fca82eSMikolaj Golub size = sizeof(structsize) + sizeof(rlim); 2087f1fca82eSMikolaj Golub if (sb != NULL) { 2088f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2089f1fca82eSMikolaj Golub structsize = sizeof(rlim); 2090f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2091f1fca82eSMikolaj Golub PROC_LOCK(p); 2092f1fca82eSMikolaj Golub for (i = 0; i < RLIM_NLIMITS; i++) 2093f6f6d240SMateusz Guzik lim_rlimit_proc(p, i, &rlim[i]); 2094f1fca82eSMikolaj Golub PROC_UNLOCK(p); 2095f1fca82eSMikolaj Golub sbuf_bcat(sb, rlim, sizeof(rlim)); 2096f1fca82eSMikolaj Golub } 2097f1fca82eSMikolaj Golub *sizep = size; 2098f1fca82eSMikolaj Golub } 2099f1fca82eSMikolaj Golub 2100f1fca82eSMikolaj Golub static void 2101f1fca82eSMikolaj Golub note_procstat_osrel(void *arg, struct sbuf *sb, size_t *sizep) 2102f1fca82eSMikolaj Golub { 2103f1fca82eSMikolaj Golub struct proc *p; 2104f1fca82eSMikolaj Golub size_t size; 2105f1fca82eSMikolaj Golub int structsize; 2106f1fca82eSMikolaj Golub 2107f1fca82eSMikolaj Golub p = (struct proc *)arg; 2108f1fca82eSMikolaj Golub size = sizeof(structsize) + sizeof(p->p_osrel); 2109f1fca82eSMikolaj Golub if (sb != NULL) { 2110f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2111f1fca82eSMikolaj Golub structsize = sizeof(p->p_osrel); 2112f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2113f1fca82eSMikolaj Golub sbuf_bcat(sb, &p->p_osrel, sizeof(p->p_osrel)); 2114f1fca82eSMikolaj Golub } 2115f1fca82eSMikolaj Golub *sizep = size; 2116f1fca82eSMikolaj Golub } 2117f1fca82eSMikolaj Golub 2118f1fca82eSMikolaj Golub static void 2119f1fca82eSMikolaj Golub __elfN(note_procstat_psstrings)(void *arg, struct sbuf *sb, size_t *sizep) 2120f1fca82eSMikolaj Golub { 2121f1fca82eSMikolaj Golub struct proc *p; 2122f1fca82eSMikolaj Golub elf_ps_strings_t ps_strings; 2123f1fca82eSMikolaj Golub size_t size; 2124f1fca82eSMikolaj Golub int structsize; 2125f1fca82eSMikolaj Golub 2126f1fca82eSMikolaj Golub p = (struct proc *)arg; 2127f1fca82eSMikolaj Golub size = sizeof(structsize) + sizeof(ps_strings); 2128f1fca82eSMikolaj Golub if (sb != NULL) { 2129f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2130f1fca82eSMikolaj Golub structsize = sizeof(ps_strings); 2131f1fca82eSMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 2132f1fca82eSMikolaj Golub ps_strings = PTROUT(p->p_sysent->sv_psstrings); 2133f1fca82eSMikolaj Golub #else 2134f1fca82eSMikolaj Golub ps_strings = p->p_sysent->sv_psstrings; 2135f1fca82eSMikolaj Golub #endif 2136f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2137f1fca82eSMikolaj Golub sbuf_bcat(sb, &ps_strings, sizeof(ps_strings)); 2138f1fca82eSMikolaj Golub } 2139f1fca82eSMikolaj Golub *sizep = size; 2140f1fca82eSMikolaj Golub } 2141f1fca82eSMikolaj Golub 2142f1fca82eSMikolaj Golub static void 2143f1fca82eSMikolaj Golub __elfN(note_procstat_auxv)(void *arg, struct sbuf *sb, size_t *sizep) 2144f1fca82eSMikolaj Golub { 2145f1fca82eSMikolaj Golub struct proc *p; 2146f1fca82eSMikolaj Golub size_t size; 2147f1fca82eSMikolaj Golub int structsize; 2148f1fca82eSMikolaj Golub 2149f1fca82eSMikolaj Golub p = (struct proc *)arg; 2150f1fca82eSMikolaj Golub if (sb == NULL) { 2151f1fca82eSMikolaj Golub size = 0; 2152f1fca82eSMikolaj Golub sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN); 2153f1fca82eSMikolaj Golub sbuf_set_drain(sb, sbuf_drain_count, &size); 2154f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2155f1fca82eSMikolaj Golub PHOLD(p); 2156f1fca82eSMikolaj Golub proc_getauxv(curthread, p, sb); 2157f1fca82eSMikolaj Golub PRELE(p); 2158f1fca82eSMikolaj Golub sbuf_finish(sb); 2159f1fca82eSMikolaj Golub sbuf_delete(sb); 2160f1fca82eSMikolaj Golub *sizep = size; 2161f1fca82eSMikolaj Golub } else { 2162f1fca82eSMikolaj Golub structsize = sizeof(Elf_Auxinfo); 2163f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2164f1fca82eSMikolaj Golub PHOLD(p); 2165f1fca82eSMikolaj Golub proc_getauxv(curthread, p, sb); 2166f1fca82eSMikolaj Golub PRELE(p); 2167f1fca82eSMikolaj Golub } 2168f1fca82eSMikolaj Golub } 2169f1fca82eSMikolaj Golub 217032c01de2SDmitry Chagin static boolean_t 21711a9c7decSKonstantin Belousov __elfN(parse_notes)(struct image_params *imgp, Elf_Brandnote *checknote, 21721a9c7decSKonstantin Belousov int32_t *osrel, const Elf_Phdr *pnote) 217332c01de2SDmitry Chagin { 2174267c52fcSKonstantin Belousov const Elf_Note *note, *note0, *note_end; 217532c01de2SDmitry Chagin const char *note_name; 21766c775eb6SKonstantin Belousov char *buf; 21776c775eb6SKonstantin Belousov int i, error; 21786c775eb6SKonstantin Belousov boolean_t res; 217932c01de2SDmitry Chagin 21806c775eb6SKonstantin Belousov /* We need some limit, might as well use PAGE_SIZE. */ 21816c775eb6SKonstantin Belousov if (pnote == NULL || pnote->p_filesz > PAGE_SIZE) 218232c01de2SDmitry Chagin return (FALSE); 21836c775eb6SKonstantin Belousov ASSERT_VOP_LOCKED(imgp->vp, "parse_notes"); 21846c775eb6SKonstantin Belousov if (pnote->p_offset > PAGE_SIZE || 21856c775eb6SKonstantin Belousov pnote->p_filesz > PAGE_SIZE - pnote->p_offset) { 21866c775eb6SKonstantin Belousov VOP_UNLOCK(imgp->vp, 0); 21876c775eb6SKonstantin Belousov buf = malloc(pnote->p_filesz, M_TEMP, M_WAITOK); 21886c775eb6SKonstantin Belousov vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 21896c775eb6SKonstantin Belousov error = vn_rdwr(UIO_READ, imgp->vp, buf, pnote->p_filesz, 21906c775eb6SKonstantin Belousov pnote->p_offset, UIO_SYSSPACE, IO_NODELOCKED, 21916c775eb6SKonstantin Belousov curthread->td_ucred, NOCRED, NULL, curthread); 21926c775eb6SKonstantin Belousov if (error != 0) { 21936c775eb6SKonstantin Belousov uprintf("i/o error PT_NOTE\n"); 21946c775eb6SKonstantin Belousov res = FALSE; 21956c775eb6SKonstantin Belousov goto ret; 21966c775eb6SKonstantin Belousov } 21976c775eb6SKonstantin Belousov note = note0 = (const Elf_Note *)buf; 21986c775eb6SKonstantin Belousov note_end = (const Elf_Note *)(buf + pnote->p_filesz); 21996c775eb6SKonstantin Belousov } else { 22006c775eb6SKonstantin Belousov note = note0 = (const Elf_Note *)(imgp->image_header + 22016c775eb6SKonstantin Belousov pnote->p_offset); 220232c01de2SDmitry Chagin note_end = (const Elf_Note *)(imgp->image_header + 220332c01de2SDmitry Chagin pnote->p_offset + pnote->p_filesz); 22046c775eb6SKonstantin Belousov buf = NULL; 22056c775eb6SKonstantin Belousov } 2206267c52fcSKonstantin Belousov for (i = 0; i < 100 && note >= note0 && note < note_end; i++) { 2207d1ae5c83SKonstantin Belousov if (!aligned(note, Elf32_Addr) || (const char *)note_end - 22086c775eb6SKonstantin Belousov (const char *)note < sizeof(Elf_Note)) { 22096c775eb6SKonstantin Belousov res = FALSE; 22106c775eb6SKonstantin Belousov goto ret; 22116c775eb6SKonstantin Belousov } 221232c01de2SDmitry Chagin if (note->n_namesz != checknote->hdr.n_namesz || 221332c01de2SDmitry Chagin note->n_descsz != checknote->hdr.n_descsz || 221432c01de2SDmitry Chagin note->n_type != checknote->hdr.n_type) 221532c01de2SDmitry Chagin goto nextnote; 221632c01de2SDmitry Chagin note_name = (const char *)(note + 1); 2217d1ae5c83SKonstantin Belousov if (note_name + checknote->hdr.n_namesz >= 2218d1ae5c83SKonstantin Belousov (const char *)note_end || strncmp(checknote->vendor, 2219d1ae5c83SKonstantin Belousov note_name, checknote->hdr.n_namesz) != 0) 222032c01de2SDmitry Chagin goto nextnote; 222132c01de2SDmitry Chagin 222232c01de2SDmitry Chagin /* 222332c01de2SDmitry Chagin * Fetch the osreldate for binary 222432c01de2SDmitry Chagin * from the ELF OSABI-note if necessary. 222532c01de2SDmitry Chagin */ 222689ffc202SBjoern A. Zeeb if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 && 22276c775eb6SKonstantin Belousov checknote->trans_osrel != NULL) { 22286c775eb6SKonstantin Belousov res = checknote->trans_osrel(note, osrel); 22296c775eb6SKonstantin Belousov goto ret; 22306c775eb6SKonstantin Belousov } 22316c775eb6SKonstantin Belousov res = TRUE; 22326c775eb6SKonstantin Belousov goto ret; 223332c01de2SDmitry Chagin nextnote: 223432c01de2SDmitry Chagin note = (const Elf_Note *)((const char *)(note + 1) + 22351b8388cdSMikolaj Golub roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) + 22361b8388cdSMikolaj Golub roundup2(note->n_descsz, ELF_NOTE_ROUNDSIZE)); 223732c01de2SDmitry Chagin } 22386c775eb6SKonstantin Belousov res = FALSE; 22396c775eb6SKonstantin Belousov ret: 22406c775eb6SKonstantin Belousov free(buf, M_TEMP); 22416c775eb6SKonstantin Belousov return (res); 224232c01de2SDmitry Chagin } 224332c01de2SDmitry Chagin 224432c01de2SDmitry Chagin /* 22451a9c7decSKonstantin Belousov * Try to find the appropriate ABI-note section for checknote, 22461a9c7decSKonstantin Belousov * fetch the osreldate for binary from the ELF OSABI-note. Only the 22471a9c7decSKonstantin Belousov * first page of the image is searched, the same as for headers. 22481a9c7decSKonstantin Belousov */ 22491a9c7decSKonstantin Belousov static boolean_t 22501a9c7decSKonstantin Belousov __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote, 22511a9c7decSKonstantin Belousov int32_t *osrel) 22521a9c7decSKonstantin Belousov { 22531a9c7decSKonstantin Belousov const Elf_Phdr *phdr; 22541a9c7decSKonstantin Belousov const Elf_Ehdr *hdr; 22551a9c7decSKonstantin Belousov int i; 22561a9c7decSKonstantin Belousov 22571a9c7decSKonstantin Belousov hdr = (const Elf_Ehdr *)imgp->image_header; 22581a9c7decSKonstantin Belousov phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 22591a9c7decSKonstantin Belousov 22601a9c7decSKonstantin Belousov for (i = 0; i < hdr->e_phnum; i++) { 22611a9c7decSKonstantin Belousov if (phdr[i].p_type == PT_NOTE && 22621a9c7decSKonstantin Belousov __elfN(parse_notes)(imgp, checknote, osrel, &phdr[i])) 22631a9c7decSKonstantin Belousov return (TRUE); 22641a9c7decSKonstantin Belousov } 22651a9c7decSKonstantin Belousov return (FALSE); 22661a9c7decSKonstantin Belousov 22671a9c7decSKonstantin Belousov } 22681a9c7decSKonstantin Belousov 22691a9c7decSKonstantin Belousov /* 2270e1743d02SSøren Schmidt * Tell kern_execve.c about it, with a little help from the linker. 2271e1743d02SSøren Schmidt */ 2272a360a43dSJake Burkholder static struct execsw __elfN(execsw) = { 2273a360a43dSJake Burkholder __CONCAT(exec_, __elfN(imgact)), 2274a360a43dSJake Burkholder __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) 2275a360a43dSJake Burkholder }; 2276a360a43dSJake Burkholder EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw)); 2277e7228204SAlfred Perlstein 2278ed167eaaSKonstantin Belousov static vm_prot_t 2279ed167eaaSKonstantin Belousov __elfN(trans_prot)(Elf_Word flags) 2280ed167eaaSKonstantin Belousov { 2281ed167eaaSKonstantin Belousov vm_prot_t prot; 2282ed167eaaSKonstantin Belousov 2283ed167eaaSKonstantin Belousov prot = 0; 2284ed167eaaSKonstantin Belousov if (flags & PF_X) 2285ed167eaaSKonstantin Belousov prot |= VM_PROT_EXECUTE; 2286ed167eaaSKonstantin Belousov if (flags & PF_W) 2287ed167eaaSKonstantin Belousov prot |= VM_PROT_WRITE; 2288ed167eaaSKonstantin Belousov if (flags & PF_R) 2289ed167eaaSKonstantin Belousov prot |= VM_PROT_READ; 2290676eda08SMarcel Moolenaar #if __ELF_WORD_SIZE == 32 2291e7d939bdSMarcel Moolenaar #if defined(__amd64__) 2292126b36a2SKonstantin Belousov if (i386_read_exec && (flags & PF_R)) 2293676eda08SMarcel Moolenaar prot |= VM_PROT_EXECUTE; 2294676eda08SMarcel Moolenaar #endif 2295676eda08SMarcel Moolenaar #endif 2296ed167eaaSKonstantin Belousov return (prot); 2297ed167eaaSKonstantin Belousov } 2298ed167eaaSKonstantin Belousov 2299ed167eaaSKonstantin Belousov static Elf_Word 2300ed167eaaSKonstantin Belousov __elfN(untrans_prot)(vm_prot_t prot) 2301ed167eaaSKonstantin Belousov { 2302ed167eaaSKonstantin Belousov Elf_Word flags; 2303ed167eaaSKonstantin Belousov 2304ed167eaaSKonstantin Belousov flags = 0; 2305ed167eaaSKonstantin Belousov if (prot & VM_PROT_EXECUTE) 2306ed167eaaSKonstantin Belousov flags |= PF_X; 2307ed167eaaSKonstantin Belousov if (prot & VM_PROT_READ) 2308ed167eaaSKonstantin Belousov flags |= PF_R; 2309ed167eaaSKonstantin Belousov if (prot & VM_PROT_WRITE) 2310ed167eaaSKonstantin Belousov flags |= PF_W; 2311ed167eaaSKonstantin Belousov return (flags); 2312ed167eaaSKonstantin Belousov } 2313