exec_elf32.c revision 1.128
11.128Schristos/* $NetBSD: exec_elf32.c,v 1.128 2007/12/26 22:11:47 christos Exp $ */ 21.36Schristos 31.36Schristos/*- 41.102Smycroft * Copyright (c) 1994, 2000, 2005 The NetBSD Foundation, Inc. 51.36Schristos * All rights reserved. 61.36Schristos * 71.36Schristos * This code is derived from software contributed to The NetBSD Foundation 81.36Schristos * by Christos Zoulas. 91.36Schristos * 101.36Schristos * Redistribution and use in source and binary forms, with or without 111.36Schristos * modification, are permitted provided that the following conditions 121.36Schristos * are met: 131.36Schristos * 1. Redistributions of source code must retain the above copyright 141.36Schristos * notice, this list of conditions and the following disclaimer. 151.36Schristos * 2. Redistributions in binary form must reproduce the above copyright 161.36Schristos * notice, this list of conditions and the following disclaimer in the 171.36Schristos * documentation and/or other materials provided with the distribution. 181.36Schristos * 3. All advertising materials mentioning features or use of this software 191.36Schristos * must display the following acknowledgement: 201.37Schristos * This product includes software developed by the NetBSD 211.37Schristos * Foundation, Inc. and its contributors. 221.36Schristos * 4. Neither the name of The NetBSD Foundation nor the names of its 231.36Schristos * contributors may be used to endorse or promote products derived 241.36Schristos * from this software without specific prior written permission. 251.36Schristos * 261.36Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 271.36Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 281.36Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 291.36Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 301.36Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 311.36Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 321.36Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 331.36Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 341.36Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 351.36Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 361.36Schristos * POSSIBILITY OF SUCH DAMAGE. 371.36Schristos */ 381.1Sfvdl 391.1Sfvdl/* 401.9Scgd * Copyright (c) 1996 Christopher G. Demetriou 411.1Sfvdl * All rights reserved. 421.1Sfvdl * 431.1Sfvdl * Redistribution and use in source and binary forms, with or without 441.1Sfvdl * modification, are permitted provided that the following conditions 451.1Sfvdl * are met: 461.1Sfvdl * 1. Redistributions of source code must retain the above copyright 471.1Sfvdl * notice, this list of conditions and the following disclaimer. 481.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright 491.1Sfvdl * notice, this list of conditions and the following disclaimer in the 501.1Sfvdl * documentation and/or other materials provided with the distribution. 511.1Sfvdl * 3. The name of the author may not be used to endorse or promote products 521.1Sfvdl * derived from this software without specific prior written permission 531.1Sfvdl * 541.1Sfvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 551.1Sfvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 561.1Sfvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 571.1Sfvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 581.1Sfvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 591.1Sfvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 601.1Sfvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 611.1Sfvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 621.1Sfvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 631.1Sfvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 641.1Sfvdl */ 651.69Slukem 661.69Slukem#include <sys/cdefs.h> 671.128Schristos__KERNEL_RCSID(1, "$NetBSD: exec_elf32.c,v 1.128 2007/12/26 22:11:47 christos Exp $"); 681.1Sfvdl 691.9Scgd/* If not included by exec_elf64.c, ELFSIZE won't be defined. */ 701.9Scgd#ifndef ELFSIZE 711.9Scgd#define ELFSIZE 32 721.9Scgd#endif 731.30Sthorpej 741.114Selad#ifdef _KERNEL_OPT 751.113Selad#include "opt_pax.h" 761.114Selad#endif /* _KERNEL_OPT */ 771.113Selad 781.1Sfvdl#include <sys/param.h> 791.1Sfvdl#include <sys/proc.h> 801.1Sfvdl#include <sys/malloc.h> 811.1Sfvdl#include <sys/namei.h> 821.1Sfvdl#include <sys/vnode.h> 831.1Sfvdl#include <sys/exec.h> 841.1Sfvdl#include <sys/exec_elf.h> 851.8Schristos#include <sys/syscall.h> 861.8Schristos#include <sys/signalvar.h> 871.14Scgd#include <sys/mount.h> 881.14Scgd#include <sys/stat.h> 891.112Selad#include <sys/kauth.h> 901.128Schristos#include <sys/bitops.h> 911.1Sfvdl 921.125Sad#include <sys/cpu.h> 931.58Sjdolecek#include <machine/reg.h> 941.57Sthorpej 951.123Sdsl#include <compat/common/compat_util.h> 961.123Sdsl 971.113Selad#include <sys/pax.h> 981.113Selad 991.58Sjdolecekextern const struct emul emul_netbsd; 1001.42Schristos 1011.105Sjunyoung#define elf_check_header ELFNAME(check_header) 1021.105Sjunyoung#define elf_copyargs ELFNAME(copyargs) 1031.105Sjunyoung#define elf_load_file ELFNAME(load_file) 1041.105Sjunyoung#define elf_load_psection ELFNAME(load_psection) 1051.105Sjunyoung#define exec_elf_makecmds ELFNAME2(exec,makecmds) 1061.105Sjunyoung#define netbsd_elf_signature ELFNAME2(netbsd,signature) 1071.105Sjunyoung#define netbsd_elf_probe ELFNAME2(netbsd,probe) 1081.105Sjunyoung 1091.108Schristosint elf_load_file(struct lwp *, struct exec_package *, char *, 1101.54Sthorpej struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *); 1111.105Sjunyoungvoid elf_load_psection(struct exec_vmcmd_set *, struct vnode *, 1121.54Sthorpej const Elf_Phdr *, Elf_Addr *, u_long *, int *, int); 1131.54Sthorpej 1141.108Schristosint netbsd_elf_signature(struct lwp *, struct exec_package *, Elf_Ehdr *); 1151.108Schristosint netbsd_elf_probe(struct lwp *, struct exec_package *, void *, char *, 1161.105Sjunyoung vaddr_t *); 1171.8Schristos 1181.18Scgd/* round up and down to page boundaries. */ 1191.18Scgd#define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1)) 1201.18Scgd#define ELF_TRUNC(a, b) ((a) & ~((b) - 1)) 1211.8Schristos 1221.90Schristos#define MAXPHNUM 50 1231.90Schristos 1241.128Schristos#ifdef PAX_ASLR 1251.128Schristos/* 1261.128Schristos * We don't move this code in kern_pax.c because it is compiled twice. 1271.128Schristos */ 1281.128Schristos#ifndef PAX_ASLR_DELTA_PROG_LEN 1291.128Schristos#define PAX_ASLR_DELTA_PROG_LEN 12 1301.128Schristos#endif 1311.128Schristosstatic void 1321.128Schristospax_aslr_elf(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh, 1331.128Schristos Elf_Phdr *ph) 1341.128Schristos{ 1351.128Schristos size_t pax_align = 0, pax_offset, i; 1361.128Schristos 1371.128Schristos if (!pax_aslr_active(l)) 1381.128Schristos return; 1391.128Schristos 1401.128Schristos /* 1411.128Schristos * find align XXX: not all sections might have the same 1421.128Schristos * alignment 1431.128Schristos */ 1441.128Schristos for (i = 0; i < eh->e_phnum; i++) 1451.128Schristos if (ph[i].p_type == PT_LOAD) { 1461.128Schristos pax_align = ph[i].p_align; 1471.128Schristos break; 1481.128Schristos } 1491.128Schristos 1501.128Schristos if (pax_align == 0) 1511.128Schristos pax_align = PGSHIFT; 1521.128Schristos#ifdef DEBUG_ASLR 1531.128Schristos uprintf("r=0x%x a=0x%x p=0x%x Delta=0x%lx\n", epp->ep_random, 1541.128Schristos ilog2(pax_align), PGSHIFT, PAX_ASLR_DELTA(epp->ep_random, 1551.128Schristos ilog2(pax_align), PAX_ASLR_DELTA_PROG_LEN)); 1561.128Schristos#endif 1571.128Schristos pax_offset = ELF_TRUNC(PAX_ASLR_DELTA(epp->ep_random, 1581.128Schristos ilog2(pax_align), PAX_ASLR_DELTA_PROG_LEN), pax_align); 1591.128Schristos 1601.128Schristos for (i = 0; i < eh->e_phnum; i++) 1611.128Schristos ph[i].p_vaddr += pax_offset; 1621.128Schristos eh->e_entry += pax_offset; 1631.128Schristos#ifdef DEBUG_ASLR 1641.128Schristos uprintf("pax offset=0x%x entry=0x%x\n", 1651.128Schristos pax_offset, eh->e_entry); 1661.128Schristos#endif 1671.128Schristos} 1681.128Schristos#endif /* PAX_ASLR */ 1691.128Schristos 1701.8Schristos/* 1711.1Sfvdl * Copy arguments onto the stack in the normal way, but add some 1721.1Sfvdl * extra information in case of dynamic binding. 1731.1Sfvdl */ 1741.66Schristosint 1751.108Schristoself_copyargs(struct lwp *l, struct exec_package *pack, 1761.72Schristos struct ps_strings *arginfo, char **stackp, void *argp) 1771.1Sfvdl{ 1781.127Schristos size_t len, vlen; 1791.127Schristos AuxInfo ai[ELF_AUX_ENTRIES], *a, *execname; 1801.1Sfvdl struct elf_args *ap; 1811.66Schristos int error; 1821.1Sfvdl 1831.108Schristos if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0) 1841.66Schristos return error; 1851.1Sfvdl 1861.22Scgd a = ai; 1871.127Schristos execname = NULL; 1881.22Scgd 1891.1Sfvdl /* 1901.1Sfvdl * Push extra arguments on the stack needed by dynamically 1911.1Sfvdl * linked binaries 1921.1Sfvdl */ 1931.17Scgd if ((ap = (struct elf_args *)pack->ep_emul_arg)) { 1941.77Sjdolecek struct vattr *vap = pack->ep_vap; 1951.1Sfvdl 1961.46Skleink a->a_type = AT_PHDR; 1971.46Skleink a->a_v = ap->arg_phaddr; 1981.1Sfvdl a++; 1991.1Sfvdl 2001.46Skleink a->a_type = AT_PHENT; 2011.46Skleink a->a_v = ap->arg_phentsize; 2021.1Sfvdl a++; 2031.1Sfvdl 2041.46Skleink a->a_type = AT_PHNUM; 2051.46Skleink a->a_v = ap->arg_phnum; 2061.1Sfvdl a++; 2071.1Sfvdl 2081.46Skleink a->a_type = AT_PAGESZ; 2091.57Sthorpej a->a_v = PAGE_SIZE; 2101.1Sfvdl a++; 2111.1Sfvdl 2121.46Skleink a->a_type = AT_BASE; 2131.46Skleink a->a_v = ap->arg_interp; 2141.1Sfvdl a++; 2151.1Sfvdl 2161.46Skleink a->a_type = AT_FLAGS; 2171.46Skleink a->a_v = 0; 2181.1Sfvdl a++; 2191.1Sfvdl 2201.46Skleink a->a_type = AT_ENTRY; 2211.46Skleink a->a_v = ap->arg_entry; 2221.72Schristos a++; 2231.72Schristos 2241.72Schristos a->a_type = AT_EUID; 2251.77Sjdolecek if (vap->va_mode & S_ISUID) 2261.77Sjdolecek a->a_v = vap->va_uid; 2271.77Sjdolecek else 2281.115Sad a->a_v = kauth_cred_geteuid(l->l_cred); 2291.72Schristos a++; 2301.72Schristos 2311.72Schristos a->a_type = AT_RUID; 2321.115Sad a->a_v = kauth_cred_getuid(l->l_cred); 2331.72Schristos a++; 2341.72Schristos 2351.72Schristos a->a_type = AT_EGID; 2361.77Sjdolecek if (vap->va_mode & S_ISGID) 2371.77Sjdolecek a->a_v = vap->va_gid; 2381.77Sjdolecek else 2391.115Sad a->a_v = kauth_cred_getegid(l->l_cred); 2401.72Schristos a++; 2411.72Schristos 2421.72Schristos a->a_type = AT_RGID; 2431.115Sad a->a_v = kauth_cred_getgid(l->l_cred); 2441.1Sfvdl a++; 2451.1Sfvdl 2461.127Schristos if (pack->ep_path) { 2471.127Schristos execname = a; 2481.127Schristos a->a_type = AT_SUN_EXECNAME; 2491.127Schristos a++; 2501.127Schristos } 2511.127Schristos 2521.76Schs free(ap, M_TEMP); 2531.9Scgd pack->ep_emul_arg = NULL; 2541.1Sfvdl } 2551.22Scgd 2561.46Skleink a->a_type = AT_NULL; 2571.46Skleink a->a_v = 0; 2581.22Scgd a++; 2591.22Scgd 2601.127Schristos vlen = (a - ai) * sizeof(AuxInfo); 2611.127Schristos 2621.127Schristos if (execname) { 2631.127Schristos char *path = pack->ep_path; 2641.127Schristos execname->a_v = (intptr_t)(*stackp + vlen); 2651.127Schristos len = strlen(path) + 1; 2661.127Schristos if ((error = copyout(path, (*stackp + vlen), len)) != 0) 2671.127Schristos return error; 2681.127Schristos len = ALIGN(len); 2691.127Schristos } else 2701.127Schristos len = 0; 2711.127Schristos 2721.127Schristos if ((error = copyout(ai, *stackp, vlen)) != 0) 2731.66Schristos return error; 2741.127Schristos *stackp += vlen + len; 2751.22Scgd 2761.66Schristos return 0; 2771.1Sfvdl} 2781.1Sfvdl 2791.1Sfvdl/* 2801.1Sfvdl * elf_check_header(): 2811.1Sfvdl * 2821.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error 2831.1Sfvdl */ 2841.17Scgdint 2851.105Sjunyoungelf_check_header(Elf_Ehdr *eh, int type) 2861.1Sfvdl{ 2871.3Sthorpej 2881.46Skleink if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 || 2891.46Skleink eh->e_ident[EI_CLASS] != ELFCLASS) 2901.106Sjunyoung return ENOEXEC; 2911.1Sfvdl 2921.1Sfvdl switch (eh->e_machine) { 2931.9Scgd 2941.10Scgd ELFDEFNNAME(MACHDEP_ID_CASES) 2951.1Sfvdl 2961.1Sfvdl default: 2971.106Sjunyoung return ENOEXEC; 2981.1Sfvdl } 2991.70Sthorpej 3001.70Sthorpej if (ELF_EHDR_FLAGS_OK(eh) == 0) 3011.106Sjunyoung return ENOEXEC; 3021.1Sfvdl 3031.1Sfvdl if (eh->e_type != type) 3041.106Sjunyoung return ENOEXEC; 3051.61Smycroft 3061.100Schristos if (eh->e_shnum > 32768 || eh->e_phnum > 128) 3071.106Sjunyoung return ENOEXEC; 3081.1Sfvdl 3091.106Sjunyoung return 0; 3101.1Sfvdl} 3111.1Sfvdl 3121.1Sfvdl/* 3131.1Sfvdl * elf_load_psection(): 3141.17Scgd * 3151.1Sfvdl * Load a psection at the appropriate address 3161.1Sfvdl */ 3171.17Scgdvoid 3181.105Sjunyoungelf_load_psection(struct exec_vmcmd_set *vcset, struct vnode *vp, 3191.54Sthorpej const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int *prot, int flags) 3201.1Sfvdl{ 3211.87Smatt u_long msize, psize, rm, rf; 3221.1Sfvdl long diff, offset; 3231.1Sfvdl 3241.1Sfvdl /* 3251.17Scgd * If the user specified an address, then we load there. 3261.17Scgd */ 3271.87Smatt if (*addr == ELFDEFNNAME(NO_ADDR)) 3281.87Smatt *addr = ph->p_vaddr; 3291.87Smatt 3301.87Smatt if (ph->p_align > 1) { 3311.87Smatt /* 3321.87Smatt * Make sure we are virtually aligned as we are supposed to be. 3331.87Smatt */ 3341.87Smatt diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align); 3351.87Smatt KASSERT(*addr - diff == ELF_TRUNC(*addr, ph->p_align)); 3361.87Smatt /* 3371.87Smatt * But make sure to not map any pages before the start of the 3381.87Smatt * psection by limiting the difference to within a page. 3391.87Smatt */ 3401.101Sperry diff &= PAGE_MASK; 3411.87Smatt } else 3421.87Smatt diff = 0; 3431.1Sfvdl 3441.46Skleink *prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0; 3451.46Skleink *prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0; 3461.46Skleink *prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0; 3471.1Sfvdl 3481.87Smatt /* 3491.87Smatt * Adjust everything so it all starts on a page boundary. 3501.87Smatt */ 3511.87Smatt *addr -= diff; 3521.1Sfvdl offset = ph->p_offset - diff; 3531.1Sfvdl *size = ph->p_filesz + diff; 3541.1Sfvdl msize = ph->p_memsz + diff; 3551.1Sfvdl 3561.65Schristos if (ph->p_align >= PAGE_SIZE) { 3571.65Schristos if ((ph->p_flags & PF_W) != 0) { 3581.65Schristos /* 3591.65Schristos * Because the pagedvn pager can't handle zero fill 3601.65Schristos * of the last data page if it's not page aligned we 3611.65Schristos * map the last page readvn. 3621.65Schristos */ 3631.65Schristos psize = trunc_page(*size); 3641.65Schristos } else { 3651.65Schristos psize = round_page(*size); 3661.65Schristos } 3671.65Schristos } else { 3681.65Schristos psize = *size; 3691.53Smatt } 3701.65Schristos 3711.53Smatt if (psize > 0) { 3721.65Schristos NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ? 3731.65Schristos vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp, 3741.53Smatt offset, *prot, flags); 3751.87Smatt flags &= VMCMD_RELATIVE; 3761.53Smatt } 3771.53Smatt if (psize < *size) { 3781.53Smatt NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize, 3791.87Smatt *addr + psize, vp, offset + psize, *prot, flags); 3801.53Smatt } 3811.1Sfvdl 3821.1Sfvdl /* 3831.87Smatt * Check if we need to extend the size of the segment (does 3841.87Smatt * bss extend page the next page boundary)? 3851.17Scgd */ 3861.1Sfvdl rm = round_page(*addr + msize); 3871.1Sfvdl rf = round_page(*addr + *size); 3881.1Sfvdl 3891.1Sfvdl if (rm != rf) { 3901.53Smatt NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 3911.83Smatt 0, *prot, flags & VMCMD_RELATIVE); 3921.1Sfvdl *size = msize; 3931.1Sfvdl } 3941.1Sfvdl} 3951.1Sfvdl 3961.1Sfvdl/* 3971.1Sfvdl * elf_load_file(): 3981.1Sfvdl * 3991.1Sfvdl * Load a file (interpreter/library) pointed to by path 4001.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic 4011.1Sfvdl * so it might be used externally. 4021.1Sfvdl */ 4031.17Scgdint 4041.108Schristoself_load_file(struct lwp *l, struct exec_package *epp, char *path, 4051.117Syamt struct exec_vmcmd_set *vcset, u_long *entryoff, struct elf_args *ap, 4061.117Syamt Elf_Addr *last) 4071.1Sfvdl{ 4081.83Smatt int error, i; 4091.14Scgd struct vnode *vp; 4101.14Scgd struct vattr attr; 4111.9Scgd Elf_Ehdr eh; 4121.9Scgd Elf_Phdr *ph = NULL; 4131.83Smatt const Elf_Phdr *ph0; 4141.83Smatt const Elf_Phdr *base_ph; 4151.83Smatt const Elf_Phdr *last_ph; 4161.1Sfvdl u_long phsize; 4171.9Scgd Elf_Addr addr = *last; 4181.108Schristos struct proc *p; 4191.108Schristos 4201.108Schristos p = l->l_proc; 4211.1Sfvdl 4221.1Sfvdl /* 4231.17Scgd * 1. open file 4241.17Scgd * 2. read filehdr 4251.17Scgd * 3. map text, data, and bss out of it using VM_* 4261.17Scgd */ 4271.123Sdsl vp = epp->ep_interp; 4281.123Sdsl if (vp == NULL) { 4291.123Sdsl error = emul_find_interp(l, epp, path); 4301.123Sdsl if (error != 0) 4311.123Sdsl return error; 4321.123Sdsl vp = epp->ep_interp; 4331.123Sdsl } 4341.123Sdsl /* We'll tidy this ourselves - otherwise we have locking issues */ 4351.123Sdsl epp->ep_interp = NULL; 4361.123Sdsl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4371.14Scgd 4381.26Smycroft /* 4391.26Smycroft * Similarly, if it's not marked as executable, or it's not a regular 4401.26Smycroft * file, we don't allow it to be used. 4411.26Smycroft */ 4421.14Scgd if (vp->v_type != VREG) { 4431.14Scgd error = EACCES; 4441.14Scgd goto badunlock; 4451.14Scgd } 4461.126Spooka if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0) 4471.26Smycroft goto badunlock; 4481.14Scgd 4491.17Scgd /* get attributes */ 4501.126Spooka if ((error = VOP_GETATTR(vp, &attr, l->l_cred)) != 0) 4511.14Scgd goto badunlock; 4521.14Scgd 4531.14Scgd /* 4541.14Scgd * Check mount point. Though we're not trying to exec this binary, 4551.15Scgd * we will be executing code from it, so if the mount point 4561.15Scgd * disallows execution or set-id-ness, we punt or kill the set-id. 4571.14Scgd */ 4581.14Scgd if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 4591.14Scgd error = EACCES; 4601.14Scgd goto badunlock; 4611.14Scgd } 4621.14Scgd if (vp->v_mount->mnt_flag & MNT_NOSUID) 4631.24Smycroft epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID); 4641.14Scgd 4651.12Scgd#ifdef notyet /* XXX cgd 960926 */ 4661.12Scgd XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?) 4671.12Scgd#endif 4681.76Schs 4691.76Schs error = vn_marktext(vp); 4701.76Schs if (error) 4711.76Schs goto badunlock; 4721.76Schs 4731.28Sfvdl VOP_UNLOCK(vp, 0); 4741.12Scgd 4751.108Schristos if ((error = exec_read_from(l, vp, 0, &eh, sizeof(eh))) != 0) 4761.1Sfvdl goto bad; 4771.1Sfvdl 4781.105Sjunyoung if ((error = elf_check_header(&eh, ET_DYN)) != 0) 4791.1Sfvdl goto bad; 4801.1Sfvdl 4811.90Schristos if (eh.e_phnum > MAXPHNUM) 4821.90Schristos goto bad; 4831.90Schristos 4841.9Scgd phsize = eh.e_phnum * sizeof(Elf_Phdr); 4851.17Scgd ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 4861.1Sfvdl 4871.108Schristos if ((error = exec_read_from(l, vp, eh.e_phoff, ph, phsize)) != 0) 4881.1Sfvdl goto bad; 4891.1Sfvdl 4901.107Ssimonb#ifdef ELF_INTERP_NON_RELOCATABLE 4911.107Ssimonb /* 4921.107Ssimonb * Evil hack: Only MIPS should be non-relocatable, and the 4931.107Ssimonb * psections should have a high address (typically 0x5ffe0000). 4941.107Ssimonb * If it's now relocatable, it should be linked at 0 and the 4951.107Ssimonb * psections should have zeros in the upper part of the address. 4961.107Ssimonb * Otherwise, force the load at the linked address. 4971.107Ssimonb */ 4981.107Ssimonb if (*last == ELF_LINK_ADDR && (ph->p_vaddr & 0xffff0000) == 0) 4991.107Ssimonb *last = ELFDEFNNAME(NO_ADDR); 5001.107Ssimonb#endif 5011.107Ssimonb 5021.1Sfvdl /* 5031.83Smatt * If no position to load the interpreter was set by a probe 5041.83Smatt * function, pick the same address that a non-fixed mmap(0, ..) 5051.83Smatt * would (i.e. something safely out of the way). 5061.83Smatt */ 5071.83Smatt if (*last == ELFDEFNNAME(NO_ADDR)) { 5081.83Smatt u_long limit = 0; 5091.83Smatt /* 5101.83Smatt * Find the start and ending addresses of the psections to 5111.83Smatt * be loaded. This will give us the size. 5121.83Smatt */ 5131.83Smatt for (i = 0, ph0 = ph, base_ph = NULL; i < eh.e_phnum; 5141.83Smatt i++, ph0++) { 5151.83Smatt if (ph0->p_type == PT_LOAD) { 5161.83Smatt u_long psize = ph0->p_vaddr + ph0->p_memsz; 5171.83Smatt if (base_ph == NULL) 5181.83Smatt base_ph = ph0; 5191.83Smatt if (psize > limit) 5201.83Smatt limit = psize; 5211.83Smatt } 5221.83Smatt } 5231.83Smatt 5241.111Sskrll if (base_ph == NULL) { 5251.110Serh error = ENOEXEC; 5261.110Serh goto bad; 5271.110Serh } 5281.110Serh 5291.83Smatt /* 5301.83Smatt * Now compute the size and load address. 5311.83Smatt */ 5321.105Sjunyoung addr = (*epp->ep_esch->es_emul->e_vm_default_addr)(p, 5331.103Sfvdl epp->ep_daddr, 5341.83Smatt round_page(limit) - trunc_page(base_ph->p_vaddr)); 5351.83Smatt } else 5361.95Sdrochner addr = *last; /* may be ELF_LINK_ADDR */ 5371.83Smatt 5381.83Smatt /* 5391.83Smatt * Load all the necessary sections 5401.83Smatt */ 5411.83Smatt for (i = 0, ph0 = ph, base_ph = NULL, last_ph = NULL; 5421.83Smatt i < eh.e_phnum; i++, ph0++) { 5431.83Smatt switch (ph0->p_type) { 5441.83Smatt case PT_LOAD: { 5451.83Smatt u_long size; 5461.83Smatt int prot = 0; 5471.83Smatt int flags; 5481.1Sfvdl 5491.53Smatt if (base_ph == NULL) { 5501.82Smatt /* 5511.82Smatt * First encountered psection is always the 5521.88Smatt * base psection. Make sure it's aligned 5531.89Smatt * properly (align down for topdown and align 5541.89Smatt * upwards for not topdown). 5551.82Smatt */ 5561.83Smatt base_ph = ph0; 5571.53Smatt flags = VMCMD_BASE; 5581.95Sdrochner if (addr == ELF_LINK_ADDR) 5591.95Sdrochner addr = ph0->p_vaddr; 5601.89Smatt if (p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN) 5611.89Smatt addr = ELF_TRUNC(addr, ph0->p_align); 5621.89Smatt else 5631.89Smatt addr = ELF_ROUND(addr, ph0->p_align); 5641.53Smatt } else { 5651.83Smatt u_long limit = round_page(last_ph->p_vaddr 5661.83Smatt + last_ph->p_memsz); 5671.87Smatt u_long base = trunc_page(ph0->p_vaddr); 5681.83Smatt 5691.82Smatt /* 5701.83Smatt * If there is a gap in between the psections, 5711.83Smatt * map it as inaccessible so nothing else 5721.83Smatt * mmap'ed will be placed there. 5731.82Smatt */ 5741.85Smatt if (limit != base) { 5751.83Smatt NEW_VMCMD2(vcset, vmcmd_map_zero, 5761.85Smatt base - limit, 5771.87Smatt limit - base_ph->p_vaddr, NULLVP, 5781.87Smatt 0, VM_PROT_NONE, VMCMD_RELATIVE); 5791.83Smatt } 5801.83Smatt 5811.83Smatt addr = ph0->p_vaddr - base_ph->p_vaddr; 5821.53Smatt flags = VMCMD_RELATIVE; 5831.53Smatt } 5841.83Smatt last_ph = ph0; 5851.105Sjunyoung elf_load_psection(vcset, vp, &ph[i], &addr, 5861.83Smatt &size, &prot, flags); 5871.82Smatt /* 5881.82Smatt * If entry is within this psection then this 5891.82Smatt * must contain the .text section. *entryoff is 5901.83Smatt * relative to the base psection. 5911.82Smatt */ 5921.83Smatt if (eh.e_entry >= ph0->p_vaddr && 5931.83Smatt eh.e_entry < (ph0->p_vaddr + size)) { 5941.83Smatt *entryoff = eh.e_entry - base_ph->p_vaddr; 5951.1Sfvdl } 5961.84Smatt addr += size; 5971.1Sfvdl break; 5981.83Smatt } 5991.1Sfvdl 6001.46Skleink case PT_DYNAMIC: 6011.46Skleink case PT_PHDR: 6021.113Selad break; 6031.113Selad 6041.46Skleink case PT_NOTE: 6051.1Sfvdl break; 6061.1Sfvdl 6071.1Sfvdl default: 6081.1Sfvdl break; 6091.1Sfvdl } 6101.1Sfvdl } 6111.1Sfvdl 6121.76Schs free(ph, M_TEMP); 6131.82Smatt /* 6141.82Smatt * This value is ignored if TOPDOWN. 6151.82Smatt */ 6161.12Scgd *last = addr; 6171.14Scgd vrele(vp); 6181.12Scgd return 0; 6191.12Scgd 6201.14Scgdbadunlock: 6211.28Sfvdl VOP_UNLOCK(vp, 0); 6221.14Scgd 6231.1Sfvdlbad: 6241.1Sfvdl if (ph != NULL) 6251.76Schs free(ph, M_TEMP); 6261.12Scgd#ifdef notyet /* XXX cgd 960926 */ 6271.12Scgd (maybe) VOP_CLOSE it 6281.12Scgd#endif 6291.14Scgd vrele(vp); 6301.1Sfvdl return error; 6311.1Sfvdl} 6321.1Sfvdl 6331.1Sfvdl/* 6341.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package 6351.1Sfvdl * 6361.1Sfvdl * First, set of the various offsets/lengths in the exec package. 6371.1Sfvdl * 6381.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error 6391.1Sfvdl * out if this is not possible. Finally, set up vmcmds for the 6401.1Sfvdl * text, data, bss, and stack segments. 6411.1Sfvdl */ 6421.1Sfvdlint 6431.108Schristosexec_elf_makecmds(struct lwp *l, struct exec_package *epp) 6441.1Sfvdl{ 6451.9Scgd Elf_Ehdr *eh = epp->ep_hdr; 6461.9Scgd Elf_Phdr *ph, *pp; 6471.9Scgd Elf_Addr phdr = 0, pos = 0; 6481.97Sthorpej int error, i, nload; 6491.58Sjdolecek char *interp = NULL; 6501.9Scgd u_long phsize; 6511.108Schristos struct proc *p; 6521.128Schristos bool is_dyn; 6531.1Sfvdl 6541.9Scgd if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) 6551.1Sfvdl return ENOEXEC; 6561.1Sfvdl 6571.128Schristos is_dyn = elf_check_header(eh, ET_DYN) == 0; 6581.45Sfvdl /* 6591.45Sfvdl * XXX allow for executing shared objects. It seems silly 6601.45Sfvdl * but other ELF-based systems allow it as well. 6611.45Sfvdl */ 6621.128Schristos if (elf_check_header(eh, ET_EXEC) != 0 && !is_dyn) 6631.1Sfvdl return ENOEXEC; 6641.1Sfvdl 6651.90Schristos if (eh->e_phnum > MAXPHNUM) 6661.90Schristos return ENOEXEC; 6671.90Schristos 6681.76Schs error = vn_marktext(epp->ep_vp); 6691.76Schs if (error) 6701.106Sjunyoung return error; 6711.76Schs 6721.1Sfvdl /* 6731.17Scgd * Allocate space to hold all the program headers, and read them 6741.17Scgd * from the file 6751.17Scgd */ 6761.108Schristos p = l->l_proc; 6771.9Scgd phsize = eh->e_phnum * sizeof(Elf_Phdr); 6781.17Scgd ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 6791.1Sfvdl 6801.108Schristos if ((error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize)) != 6811.64Schristos 0) 6821.1Sfvdl goto bad; 6831.1Sfvdl 6841.19Scgd epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR); 6851.19Scgd epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR); 6861.1Sfvdl 6871.1Sfvdl for (i = 0; i < eh->e_phnum; i++) { 6881.1Sfvdl pp = &ph[i]; 6891.46Skleink if (pp->p_type == PT_INTERP) { 6901.58Sjdolecek if (pp->p_filesz >= MAXPATHLEN) 6911.1Sfvdl goto bad; 6921.109Syamt interp = PNBUF_GET(); 6931.95Sdrochner interp[0] = '\0'; 6941.108Schristos if ((error = exec_read_from(l, epp->ep_vp, 6951.64Schristos pp->p_offset, interp, pp->p_filesz)) != 0) 6961.1Sfvdl goto bad; 6971.1Sfvdl break; 6981.1Sfvdl } 6991.1Sfvdl } 7001.1Sfvdl 7011.9Scgd /* 7021.1Sfvdl * On the same architecture, we may be emulating different systems. 7031.99Sskrll * See which one will accept this executable. 7041.1Sfvdl * 7051.1Sfvdl * Probe functions would normally see if the interpreter (if any) 7061.1Sfvdl * exists. Emulation packages may possibly replace the interpreter in 7071.58Sjdolecek * interp[] with a changed path (/emul/xxx/<path>). 7081.1Sfvdl */ 7091.95Sdrochner pos = ELFDEFNNAME(NO_ADDR); 7101.95Sdrochner if (epp->ep_esch->u.elf_probe_func) { 7111.95Sdrochner vaddr_t startp = (vaddr_t)pos; 7121.62Seeh 7131.108Schristos error = (*epp->ep_esch->u.elf_probe_func)(l, epp, eh, interp, 7141.62Seeh &startp); 7151.1Sfvdl if (error) 7161.1Sfvdl goto bad; 7171.95Sdrochner pos = (Elf_Addr)startp; 7181.1Sfvdl } 7191.1Sfvdl 7201.128Schristos#if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR) 7211.128Schristos if (epp->ep_pax_flags) 7221.128Schristos pax_adjust(l, epp->ep_pax_flags); 7231.128Schristos#endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */ 7241.128Schristos 7251.128Schristos#ifdef PAX_ASLR 7261.128Schristos if (is_dyn) 7271.128Schristos pax_aslr_elf(l, epp, eh, ph); 7281.128Schristos#endif /* PAX_ASLR */ 7291.128Schristos 7301.1Sfvdl /* 7311.17Scgd * Load all the necessary sections 7321.17Scgd */ 7331.97Sthorpej for (i = nload = 0; i < eh->e_phnum; i++) { 7341.9Scgd Elf_Addr addr = ELFDEFNNAME(NO_ADDR); 7351.9Scgd u_long size = 0; 7361.1Sfvdl int prot = 0; 7371.1Sfvdl 7381.1Sfvdl pp = &ph[i]; 7391.1Sfvdl 7401.1Sfvdl switch (ph[i].p_type) { 7411.46Skleink case PT_LOAD: 7421.4Sfvdl /* 7431.97Sthorpej * XXX 7441.97Sthorpej * Can handle only 2 sections: text and data 7451.4Sfvdl */ 7461.97Sthorpej if (nload++ == 2) 7471.97Sthorpej goto bad; 7481.105Sjunyoung elf_load_psection(&epp->ep_vmcmds, epp->ep_vp, 7491.79Satatat &ph[i], &addr, &size, &prot, VMCMD_FIXED); 7501.17Scgd 7511.4Sfvdl /* 7521.4Sfvdl * Decide whether it's text or data by looking 7531.97Sthorpej * at the entry point. 7541.4Sfvdl */ 7551.97Sthorpej if (eh->e_entry >= addr && 7561.97Sthorpej eh->e_entry < (addr + size)) { 7571.97Sthorpej epp->ep_taddr = addr; 7581.97Sthorpej epp->ep_tsize = size; 7591.97Sthorpej if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) { 7601.19Scgd epp->ep_daddr = addr; 7611.19Scgd epp->ep_dsize = size; 7621.19Scgd } 7631.97Sthorpej } else { 7641.97Sthorpej epp->ep_daddr = addr; 7651.97Sthorpej epp->ep_dsize = size; 7661.4Sfvdl } 7671.1Sfvdl break; 7681.1Sfvdl 7691.46Skleink case PT_SHLIB: 7701.60Smycroft /* SCO has these sections. */ 7711.46Skleink case PT_INTERP: 7721.60Smycroft /* Already did this one. */ 7731.46Skleink case PT_DYNAMIC: 7741.120Selad break; 7751.46Skleink case PT_NOTE: 7761.1Sfvdl break; 7771.46Skleink case PT_PHDR: 7781.4Sfvdl /* Note address of program headers (in text segment) */ 7791.4Sfvdl phdr = pp->p_vaddr; 7801.7Schristos break; 7811.4Sfvdl 7821.1Sfvdl default: 7831.1Sfvdl /* 7841.9Scgd * Not fatal; we don't need to understand everything. 7851.1Sfvdl */ 7861.1Sfvdl break; 7871.1Sfvdl } 7881.1Sfvdl } 7891.1Sfvdl 7901.1Sfvdl /* 7911.17Scgd * Check if we found a dynamically linked binary and arrange to load 7921.79Satatat * its interpreter 7931.17Scgd */ 7941.95Sdrochner if (interp) { 7951.1Sfvdl struct elf_args *ap; 7961.104Schristos int j = epp->ep_vmcmds.evs_used; 7971.79Satatat u_long interp_offset; 7981.1Sfvdl 7991.58Sjdolecek MALLOC(ap, struct elf_args *, sizeof(struct elf_args), 8001.17Scgd M_TEMP, M_WAITOK); 8011.108Schristos if ((error = elf_load_file(l, epp, interp, 8021.79Satatat &epp->ep_vmcmds, &interp_offset, ap, &pos)) != 0) { 8031.76Schs FREE(ap, M_TEMP); 8041.1Sfvdl goto bad; 8051.1Sfvdl } 8061.104Schristos ap->arg_interp = epp->ep_vmcmds.evs_cmds[j].ev_addr; 8071.79Satatat epp->ep_entry = ap->arg_interp + interp_offset; 8081.4Sfvdl ap->arg_phaddr = phdr; 8091.1Sfvdl 8101.1Sfvdl ap->arg_phentsize = eh->e_phentsize; 8111.1Sfvdl ap->arg_phnum = eh->e_phnum; 8121.1Sfvdl ap->arg_entry = eh->e_entry; 8131.1Sfvdl 8141.1Sfvdl epp->ep_emul_arg = ap; 8151.95Sdrochner 8161.109Syamt PNBUF_PUT(interp); 8171.1Sfvdl } else 8181.1Sfvdl epp->ep_entry = eh->e_entry; 8191.1Sfvdl 8201.8Schristos#ifdef ELF_MAP_PAGE_ZERO 8211.8Schristos /* Dell SVR4 maps page zero, yeuch! */ 8221.57Sthorpej NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0, 8231.57Sthorpej epp->ep_vp, 0, VM_PROT_READ); 8241.8Schristos#endif 8251.76Schs free(ph, M_TEMP); 8261.108Schristos return (*epp->ep_esch->es_setup_stack)(l, epp); 8271.1Sfvdl 8281.1Sfvdlbad: 8291.58Sjdolecek if (interp) 8301.109Syamt PNBUF_PUT(interp); 8311.76Schs free(ph, M_TEMP); 8321.1Sfvdl kill_vmcmds(&epp->ep_vmcmds); 8331.1Sfvdl return ENOEXEC; 8341.40Schristos} 8351.40Schristos 8361.59Smrgint 8371.108Schristosnetbsd_elf_signature(struct lwp *l, struct exec_package *epp, 8381.54Sthorpej Elf_Ehdr *eh) 8391.40Schristos{ 8401.61Smycroft size_t i; 8411.61Smycroft Elf_Phdr *ph; 8421.40Schristos size_t phsize; 8431.40Schristos int error; 8441.124Schristos int isnetbsd = 0; 8451.124Schristos char *ndata; 8461.90Schristos 8471.124Schristos epp->ep_pax_flags = 0; 8481.90Schristos if (eh->e_phnum > MAXPHNUM) 8491.90Schristos return ENOEXEC; 8501.40Schristos 8511.40Schristos phsize = eh->e_phnum * sizeof(Elf_Phdr); 8521.61Smycroft ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 8531.108Schristos error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize); 8541.61Smycroft if (error) 8551.61Smycroft goto out; 8561.40Schristos 8571.61Smycroft for (i = 0; i < eh->e_phnum; i++) { 8581.61Smycroft Elf_Phdr *ephp = &ph[i]; 8591.61Smycroft Elf_Nhdr *np; 8601.40Schristos 8611.61Smycroft if (ephp->p_type != PT_NOTE || 8621.61Smycroft ephp->p_filesz > 1024 || 8631.61Smycroft ephp->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ) 8641.40Schristos continue; 8651.40Schristos 8661.61Smycroft np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK); 8671.108Schristos error = exec_read_from(l, epp->ep_vp, ephp->p_offset, np, 8681.64Schristos ephp->p_filesz); 8691.61Smycroft if (error) 8701.61Smycroft goto next; 8711.40Schristos 8721.124Schristos ndata = (char *)(np + 1); 8731.124Schristos switch (np->n_type) { 8741.124Schristos case ELF_NOTE_TYPE_NETBSD_TAG: 8751.124Schristos if (np->n_namesz != ELF_NOTE_NETBSD_NAMESZ || 8761.124Schristos np->n_descsz != ELF_NOTE_NETBSD_DESCSZ || 8771.124Schristos memcmp(ndata, ELF_NOTE_NETBSD_NAME, 8781.124Schristos ELF_NOTE_NETBSD_NAMESZ)) 8791.124Schristos goto next; 8801.124Schristos isnetbsd = 1; 8811.124Schristos break; 8821.124Schristos 8831.124Schristos case ELF_NOTE_TYPE_PAX_TAG: 8841.124Schristos if (np->n_namesz != ELF_NOTE_PAX_NAMESZ || 8851.124Schristos np->n_descsz != ELF_NOTE_PAX_DESCSZ || 8861.124Schristos memcmp(ndata, ELF_NOTE_PAX_NAME, 8871.124Schristos ELF_NOTE_PAX_NAMESZ)) 8881.124Schristos goto next; 8891.124Schristos (void)memcpy(&epp->ep_pax_flags, 8901.124Schristos ndata + ELF_NOTE_PAX_NAMESZ, 8911.124Schristos sizeof(epp->ep_pax_flags)); 8921.124Schristos break; 8931.40Schristos 8941.124Schristos default: 8951.124Schristos break; 8961.124Schristos } 8971.61Smycroft 8981.124Schristosnext: 8991.61Smycroft free(np, M_TEMP); 9001.61Smycroft continue; 9011.40Schristos } 9021.40Schristos 9031.124Schristos error = isnetbsd ? 0 : ENOEXEC; 9041.61Smycroftout: 9051.61Smycroft free(ph, M_TEMP); 9061.106Sjunyoung return error; 9071.40Schristos} 9081.40Schristos 9091.58Sjdolecekint 9101.117Syamtnetbsd_elf_probe(struct lwp *l, struct exec_package *epp, void *eh, char *itp, 9111.117Syamt vaddr_t *pos) 9121.40Schristos{ 9131.40Schristos int error; 9141.40Schristos 9151.108Schristos if ((error = netbsd_elf_signature(l, epp, eh)) != 0) 9161.40Schristos return error; 9171.95Sdrochner#ifdef ELF_INTERP_NON_RELOCATABLE 9181.95Sdrochner *pos = ELF_LINK_ADDR; 9191.95Sdrochner#endif 9201.40Schristos return 0; 9211.1Sfvdl} 922