Home | History | Annotate | Line # | Download | only in kern
exec_subr.c revision 1.78.2.1
      1 /*	$NetBSD: exec_subr.c,v 1.78.2.1 2017/06/21 18:12:40 snj Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993, 1994, 1996 Christopher G. Demetriou
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Christopher G. Demetriou.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: exec_subr.c,v 1.78.2.1 2017/06/21 18:12:40 snj Exp $");
     35 
     36 #include "opt_pax.h"
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/proc.h>
     41 #include <sys/kmem.h>
     42 #include <sys/vnode.h>
     43 #include <sys/filedesc.h>
     44 #include <sys/exec.h>
     45 #include <sys/mman.h>
     46 #include <sys/resourcevar.h>
     47 #include <sys/device.h>
     48 #include <sys/pax.h>
     49 
     50 #include <uvm/uvm_extern.h>
     51 
     52 #define	VMCMD_EVCNT_DECL(name)					\
     53 static struct evcnt vmcmd_ev_##name =				\
     54     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "vmcmd", #name);	\
     55 EVCNT_ATTACH_STATIC(vmcmd_ev_##name)
     56 
     57 #define	VMCMD_EVCNT_INCR(name)					\
     58     vmcmd_ev_##name.ev_count++
     59 
     60 VMCMD_EVCNT_DECL(calls);
     61 VMCMD_EVCNT_DECL(extends);
     62 VMCMD_EVCNT_DECL(kills);
     63 
     64 #ifdef DEBUG_STACK
     65 #define DPRINTF(a) uprintf a
     66 #else
     67 #define DPRINTF(a)
     68 #endif
     69 
     70 uint32_t user_stack_guard_size = 1024 * 1024;
     71 
     72 /*
     73  * new_vmcmd():
     74  *	create a new vmcmd structure and fill in its fields based
     75  *	on function call arguments.  make sure objects ref'd by
     76  *	the vmcmd are 'held'.
     77  */
     78 
     79 void
     80 new_vmcmd(struct exec_vmcmd_set *evsp,
     81     int (*proc)(struct lwp * l, struct exec_vmcmd *),
     82     vsize_t len, vaddr_t addr, struct vnode *vp, u_long offset,
     83     u_int prot, int flags)
     84 {
     85 	struct exec_vmcmd *vcp;
     86 
     87 	VMCMD_EVCNT_INCR(calls);
     88 	KASSERT(proc != vmcmd_map_pagedvn || (vp->v_iflag & VI_TEXT));
     89 	KASSERT(vp == NULL || vp->v_usecount > 0);
     90 
     91 	if (evsp->evs_used >= evsp->evs_cnt)
     92 		vmcmdset_extend(evsp);
     93 	vcp = &evsp->evs_cmds[evsp->evs_used++];
     94 	vcp->ev_proc = proc;
     95 	vcp->ev_len = len;
     96 	vcp->ev_addr = addr;
     97 	if ((vcp->ev_vp = vp) != NULL)
     98 		vref(vp);
     99 	vcp->ev_offset = offset;
    100 	vcp->ev_prot = prot;
    101 	vcp->ev_flags = flags;
    102 }
    103 
    104 void
    105 vmcmdset_extend(struct exec_vmcmd_set *evsp)
    106 {
    107 	struct exec_vmcmd *nvcp;
    108 	u_int ocnt;
    109 
    110 #ifdef DIAGNOSTIC
    111 	if (evsp->evs_used < evsp->evs_cnt)
    112 		panic("vmcmdset_extend: not necessary");
    113 #endif
    114 
    115 	/* figure out number of entries in new set */
    116 	if ((ocnt = evsp->evs_cnt) != 0) {
    117 		evsp->evs_cnt += ocnt;
    118 		VMCMD_EVCNT_INCR(extends);
    119 	} else
    120 		evsp->evs_cnt = EXEC_DEFAULT_VMCMD_SETSIZE;
    121 
    122 	/* allocate it */
    123 	nvcp = kmem_alloc(evsp->evs_cnt * sizeof(struct exec_vmcmd), KM_SLEEP);
    124 
    125 	/* free the old struct, if there was one, and record the new one */
    126 	if (ocnt) {
    127 		memcpy(nvcp, evsp->evs_cmds,
    128 		    (ocnt * sizeof(struct exec_vmcmd)));
    129 		kmem_free(evsp->evs_cmds, ocnt * sizeof(struct exec_vmcmd));
    130 	}
    131 	evsp->evs_cmds = nvcp;
    132 }
    133 
    134 void
    135 kill_vmcmds(struct exec_vmcmd_set *evsp)
    136 {
    137 	struct exec_vmcmd *vcp;
    138 	u_int i;
    139 
    140 	VMCMD_EVCNT_INCR(kills);
    141 
    142 	if (evsp->evs_cnt == 0)
    143 		return;
    144 
    145 	for (i = 0; i < evsp->evs_used; i++) {
    146 		vcp = &evsp->evs_cmds[i];
    147 		if (vcp->ev_vp != NULL)
    148 			vrele(vcp->ev_vp);
    149 	}
    150 	kmem_free(evsp->evs_cmds, evsp->evs_cnt * sizeof(struct exec_vmcmd));
    151 	evsp->evs_used = evsp->evs_cnt = 0;
    152 }
    153 
    154 /*
    155  * vmcmd_map_pagedvn():
    156  *	handle vmcmd which specifies that a vnode should be mmap'd.
    157  *	appropriate for handling demand-paged text and data segments.
    158  */
    159 
    160 static int
    161 vmcmd_get_prot(struct lwp *l, const struct exec_vmcmd *cmd, vm_prot_t *prot,
    162     vm_prot_t *maxprot)
    163 {
    164 
    165 	*prot = cmd->ev_prot;
    166 	*maxprot = PAX_MPROTECT_MAXPROTECT(l, *prot, 0, UVM_PROT_ALL);
    167 
    168 	if ((*prot & *maxprot) != *prot)
    169 		return EACCES;
    170 	return PAX_MPROTECT_VALIDATE(l, *prot);
    171 }
    172 
    173 int
    174 vmcmd_map_pagedvn(struct lwp *l, struct exec_vmcmd *cmd)
    175 {
    176 	struct uvm_object *uobj;
    177 	struct vnode *vp = cmd->ev_vp;
    178 	struct proc *p = l->l_proc;
    179 	int error;
    180 	vm_prot_t prot, maxprot;
    181 
    182 	KASSERT(vp->v_iflag & VI_TEXT);
    183 
    184 	/*
    185 	 * map the vnode in using uvm_map.
    186 	 */
    187 
    188 	if (cmd->ev_len == 0)
    189 		return 0;
    190 	if (cmd->ev_offset & PAGE_MASK)
    191 		return EINVAL;
    192 	if (cmd->ev_addr & PAGE_MASK)
    193 		return EINVAL;
    194 	if (cmd->ev_len & PAGE_MASK)
    195 		return EINVAL;
    196 
    197 	if ((error = vmcmd_get_prot(l, cmd, &prot, &maxprot)) != 0)
    198 		return error;
    199 
    200 	/*
    201 	 * check the file system's opinion about mmapping the file
    202 	 */
    203 
    204 	error = VOP_MMAP(vp, prot, l->l_cred);
    205 	if (error)
    206 		return error;
    207 
    208 	if ((vp->v_vflag & VV_MAPPED) == 0) {
    209 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    210 		vp->v_vflag |= VV_MAPPED;
    211 		VOP_UNLOCK(vp);
    212 	}
    213 
    214 	/*
    215 	 * do the map, reference the object for this map entry
    216 	 */
    217 	uobj = &vp->v_uobj;
    218 	vref(vp);
    219 
    220 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
    221 		uobj, cmd->ev_offset, 0,
    222 		UVM_MAPFLAG(prot, maxprot, UVM_INH_COPY,
    223 			UVM_ADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED));
    224 	if (error) {
    225 		uobj->pgops->pgo_detach(uobj);
    226 	}
    227 	return error;
    228 }
    229 
    230 /*
    231  * vmcmd_map_readvn():
    232  *	handle vmcmd which specifies that a vnode should be read from.
    233  *	appropriate for non-demand-paged text/data segments, i.e. impure
    234  *	objects (a la OMAGIC and NMAGIC).
    235  */
    236 int
    237 vmcmd_map_readvn(struct lwp *l, struct exec_vmcmd *cmd)
    238 {
    239 	struct proc *p = l->l_proc;
    240 	int error;
    241 	long diff;
    242 
    243 	if (cmd->ev_len == 0)
    244 		return 0;
    245 
    246 	diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
    247 	cmd->ev_addr -= diff;			/* required by uvm_map */
    248 	cmd->ev_offset -= diff;
    249 	cmd->ev_len += diff;
    250 
    251 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
    252 			round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
    253 			UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
    254 			UVM_ADV_NORMAL,
    255 			UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
    256 
    257 	if (error)
    258 		return error;
    259 
    260 	return vmcmd_readvn(l, cmd);
    261 }
    262 
    263 int
    264 vmcmd_readvn(struct lwp *l, struct exec_vmcmd *cmd)
    265 {
    266 	struct proc *p = l->l_proc;
    267 	int error;
    268 	vm_prot_t prot, maxprot;
    269 
    270 	error = vn_rdwr(UIO_READ, cmd->ev_vp, (void *)cmd->ev_addr,
    271 	    cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT,
    272 	    l->l_cred, NULL, l);
    273 	if (error)
    274 		return error;
    275 
    276 	if ((error = vmcmd_get_prot(l, cmd, &prot, &maxprot)) != 0)
    277 		return error;
    278 
    279 #ifdef PMAP_NEED_PROCWR
    280 	/*
    281 	 * we had to write the process, make sure the pages are synched
    282 	 * with the instruction cache.
    283 	 */
    284 	if (prot & VM_PROT_EXECUTE)
    285 		pmap_procwr(p, cmd->ev_addr, cmd->ev_len);
    286 #endif
    287 
    288 	/*
    289 	 * we had to map in the area at PROT_ALL so that vn_rdwr()
    290 	 * could write to it.   however, the caller seems to want
    291 	 * it mapped read-only, so now we are going to have to call
    292 	 * uvm_map_protect() to fix up the protection.  ICK.
    293 	 */
    294 	if (maxprot != VM_PROT_ALL) {
    295 		error = uvm_map_protect(&p->p_vmspace->vm_map,
    296 				trunc_page(cmd->ev_addr),
    297 				round_page(cmd->ev_addr + cmd->ev_len),
    298 				maxprot, true);
    299 		if (error)
    300 			return error;
    301 	}
    302 
    303 	if (prot != maxprot) {
    304 		error = uvm_map_protect(&p->p_vmspace->vm_map,
    305 				trunc_page(cmd->ev_addr),
    306 				round_page(cmd->ev_addr + cmd->ev_len),
    307 				prot, false);
    308 		if (error)
    309 			return error;
    310 	}
    311 
    312 	return 0;
    313 }
    314 
    315 /*
    316  * vmcmd_map_zero():
    317  *	handle vmcmd which specifies a zero-filled address space region.  The
    318  *	address range must be first allocated, then protected appropriately.
    319  */
    320 
    321 int
    322 vmcmd_map_zero(struct lwp *l, struct exec_vmcmd *cmd)
    323 {
    324 	struct proc *p = l->l_proc;
    325 	int error;
    326 	long diff;
    327 	vm_prot_t prot, maxprot;
    328 
    329 	diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
    330 	cmd->ev_addr -= diff;			/* required by uvm_map */
    331 	cmd->ev_len += diff;
    332 
    333 	if ((error = vmcmd_get_prot(l, cmd, &prot, &maxprot)) != 0)
    334 		return error;
    335 
    336 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
    337 			round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
    338 			UVM_MAPFLAG(prot, maxprot, UVM_INH_COPY,
    339 			UVM_ADV_NORMAL,
    340 			UVM_FLAG_FIXED|UVM_FLAG_COPYONW));
    341 	if (cmd->ev_flags & VMCMD_STACK)
    342 		curproc->p_vmspace->vm_issize += atop(round_page(cmd->ev_len));
    343 	return error;
    344 }
    345 
    346 /*
    347  * exec_read_from():
    348  *
    349  *	Read from vnode into buffer at offset.
    350  */
    351 int
    352 exec_read_from(struct lwp *l, struct vnode *vp, u_long off, void *bf,
    353     size_t size)
    354 {
    355 	int error;
    356 	size_t resid;
    357 
    358 	if ((error = vn_rdwr(UIO_READ, vp, bf, size, off, UIO_SYSSPACE,
    359 	    0, l->l_cred, &resid, NULL)) != 0)
    360 		return error;
    361 	/*
    362 	 * See if we got all of it
    363 	 */
    364 	if (resid != 0)
    365 		return ENOEXEC;
    366 	return 0;
    367 }
    368 
    369 /*
    370  * exec_setup_stack(): Set up the stack segment for an elf
    371  * executable.
    372  *
    373  * Note that the ep_ssize parameter must be set to be the current stack
    374  * limit; this is adjusted in the body of execve() to yield the
    375  * appropriate stack segment usage once the argument length is
    376  * calculated.
    377  *
    378  * This function returns an int for uniformity with other (future) formats'
    379  * stack setup functions.  They might have errors to return.
    380  */
    381 
    382 int
    383 exec_setup_stack(struct lwp *l, struct exec_package *epp)
    384 {
    385 	vsize_t max_stack_size;
    386 	vaddr_t access_linear_min;
    387 	vsize_t access_size;
    388 	vaddr_t noaccess_linear_min;
    389 	vsize_t noaccess_size;
    390 
    391 #ifndef	USRSTACK32
    392 #define USRSTACK32	(0x00000000ffffffffL&~PGOFSET)
    393 #endif
    394 #ifndef MAXSSIZ32
    395 #define MAXSSIZ32	(MAXSSIZ >> 2)
    396 #endif
    397 
    398 	if (epp->ep_flags & EXEC_32) {
    399 		epp->ep_minsaddr = USRSTACK32;
    400 		max_stack_size = MAXSSIZ32;
    401 	} else {
    402 		epp->ep_minsaddr = USRSTACK;
    403 		max_stack_size = MAXSSIZ;
    404 	}
    405 
    406 	DPRINTF(("ep_minsaddr=%#jx max_stack_size=%#jx\n",
    407 	    (uintmax_t)epp->ep_minsaddr, (uintmax_t)max_stack_size));
    408 
    409 	pax_aslr_stack(epp, &max_stack_size);
    410 
    411 	DPRINTF(("[RLIMIT_STACK].lim_cur=%#jx max_stack_size=%#jx\n",
    412 	    (uintmax_t)l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur,
    413 	    (uintmax_t)max_stack_size));
    414 	epp->ep_ssize = MIN(l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur,
    415 	    max_stack_size);
    416 
    417 	l->l_proc->p_stackbase = epp->ep_minsaddr;
    418 
    419 	epp->ep_maxsaddr = (vaddr_t)STACK_GROW(epp->ep_minsaddr,
    420 	    max_stack_size);
    421 
    422 	DPRINTF(("ep_ssize=%#jx ep_minsaddr=%#jx ep_maxsaddr=%#jx\n",
    423 	    (uintmax_t)epp->ep_ssize, (uintmax_t)epp->ep_minsaddr,
    424 	    (uintmax_t)epp->ep_maxsaddr));
    425 
    426 	/*
    427 	 * set up commands for stack.  note that this takes *two*, one to
    428 	 * map the part of the stack which we can access, and one to map
    429 	 * the part which we can't.
    430 	 *
    431 	 * arguably, it could be made into one, but that would require the
    432 	 * addition of another mapping proc, which is unnecessary
    433 	 */
    434 	access_size = epp->ep_ssize;
    435 	access_linear_min = (vaddr_t)STACK_ALLOC(epp->ep_minsaddr, access_size);
    436 	noaccess_size = max_stack_size - access_size;
    437 	noaccess_linear_min = (vaddr_t)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr,
    438 	    access_size), noaccess_size);
    439 
    440 	DPRINTF(("access_size=%#jx, access_linear_min=%#jx, "
    441 	    "noaccess_size=%#jx, noaccess_linear_min=%#jx\n",
    442 	    (uintmax_t)access_size, (uintmax_t)access_linear_min,
    443 	    (uintmax_t)noaccess_size, (uintmax_t)noaccess_linear_min));
    444 
    445 	if (user_stack_guard_size > 0) {
    446 #ifdef __MACHINE_STACK_GROWS_UP
    447 		vsize_t guard_size = MIN(VM_MAXUSER_ADDRESS - epp->ep_maxsaddr, user_stack_guard_size);
    448 		if (guard_size > 0)
    449 			NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, guard_size,
    450 			    epp->ep_maxsaddr, NULL, 0, VM_PROT_NONE);
    451 #else
    452 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, user_stack_guard_size,
    453 		    epp->ep_maxsaddr - user_stack_guard_size, NULL, 0, VM_PROT_NONE);
    454 #endif
    455 	}
    456 	if (noaccess_size > 0 && noaccess_size <= MAXSSIZ) {
    457 		NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size,
    458 		    noaccess_linear_min, NULL, 0, VM_PROT_NONE, VMCMD_STACK);
    459 	}
    460 	KASSERT(access_size > 0 && access_size <= MAXSSIZ);
    461 	NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, access_size,
    462 	    access_linear_min, NULL, 0, VM_PROT_READ | VM_PROT_WRITE,
    463 	    VMCMD_STACK);
    464 
    465 	return 0;
    466 }
    467