Home | History | Annotate | Line # | Download | only in kern
exec_subr.c revision 1.77
      1 /*	$NetBSD: exec_subr.c,v 1.77 2017/05/06 21:34:51 joerg 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.77 2017/05/06 21:34:51 joerg 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 /*
     71  * new_vmcmd():
     72  *	create a new vmcmd structure and fill in its fields based
     73  *	on function call arguments.  make sure objects ref'd by
     74  *	the vmcmd are 'held'.
     75  */
     76 
     77 void
     78 new_vmcmd(struct exec_vmcmd_set *evsp,
     79     int (*proc)(struct lwp * l, struct exec_vmcmd *),
     80     vsize_t len, vaddr_t addr, struct vnode *vp, u_long offset,
     81     u_int prot, int flags)
     82 {
     83 	struct exec_vmcmd *vcp;
     84 
     85 	VMCMD_EVCNT_INCR(calls);
     86 	KASSERT(proc != vmcmd_map_pagedvn || (vp->v_iflag & VI_TEXT));
     87 	KASSERT(vp == NULL || vp->v_usecount > 0);
     88 
     89 	if (evsp->evs_used >= evsp->evs_cnt)
     90 		vmcmdset_extend(evsp);
     91 	vcp = &evsp->evs_cmds[evsp->evs_used++];
     92 	vcp->ev_proc = proc;
     93 	vcp->ev_len = len;
     94 	vcp->ev_addr = addr;
     95 	if ((vcp->ev_vp = vp) != NULL)
     96 		vref(vp);
     97 	vcp->ev_offset = offset;
     98 	vcp->ev_prot = prot;
     99 	vcp->ev_flags = flags;
    100 }
    101 
    102 void
    103 vmcmdset_extend(struct exec_vmcmd_set *evsp)
    104 {
    105 	struct exec_vmcmd *nvcp;
    106 	u_int ocnt;
    107 
    108 #ifdef DIAGNOSTIC
    109 	if (evsp->evs_used < evsp->evs_cnt)
    110 		panic("vmcmdset_extend: not necessary");
    111 #endif
    112 
    113 	/* figure out number of entries in new set */
    114 	if ((ocnt = evsp->evs_cnt) != 0) {
    115 		evsp->evs_cnt += ocnt;
    116 		VMCMD_EVCNT_INCR(extends);
    117 	} else
    118 		evsp->evs_cnt = EXEC_DEFAULT_VMCMD_SETSIZE;
    119 
    120 	/* allocate it */
    121 	nvcp = kmem_alloc(evsp->evs_cnt * sizeof(struct exec_vmcmd), KM_SLEEP);
    122 
    123 	/* free the old struct, if there was one, and record the new one */
    124 	if (ocnt) {
    125 		memcpy(nvcp, evsp->evs_cmds,
    126 		    (ocnt * sizeof(struct exec_vmcmd)));
    127 		kmem_free(evsp->evs_cmds, ocnt * sizeof(struct exec_vmcmd));
    128 	}
    129 	evsp->evs_cmds = nvcp;
    130 }
    131 
    132 void
    133 kill_vmcmds(struct exec_vmcmd_set *evsp)
    134 {
    135 	struct exec_vmcmd *vcp;
    136 	u_int i;
    137 
    138 	VMCMD_EVCNT_INCR(kills);
    139 
    140 	if (evsp->evs_cnt == 0)
    141 		return;
    142 
    143 	for (i = 0; i < evsp->evs_used; i++) {
    144 		vcp = &evsp->evs_cmds[i];
    145 		if (vcp->ev_vp != NULL)
    146 			vrele(vcp->ev_vp);
    147 	}
    148 	kmem_free(evsp->evs_cmds, evsp->evs_cnt * sizeof(struct exec_vmcmd));
    149 	evsp->evs_used = evsp->evs_cnt = 0;
    150 }
    151 
    152 /*
    153  * vmcmd_map_pagedvn():
    154  *	handle vmcmd which specifies that a vnode should be mmap'd.
    155  *	appropriate for handling demand-paged text and data segments.
    156  */
    157 
    158 int
    159 vmcmd_map_pagedvn(struct lwp *l, struct exec_vmcmd *cmd)
    160 {
    161 	struct uvm_object *uobj;
    162 	struct vnode *vp = cmd->ev_vp;
    163 	struct proc *p = l->l_proc;
    164 	int error;
    165 	vm_prot_t prot, maxprot;
    166 
    167 	KASSERT(vp->v_iflag & VI_TEXT);
    168 
    169 	/*
    170 	 * map the vnode in using uvm_map.
    171 	 */
    172 
    173 	if (cmd->ev_len == 0)
    174 		return 0;
    175 	if (cmd->ev_offset & PAGE_MASK)
    176 		return EINVAL;
    177 	if (cmd->ev_addr & PAGE_MASK)
    178 		return EINVAL;
    179 	if (cmd->ev_len & PAGE_MASK)
    180 		return EINVAL;
    181 
    182 	prot = cmd->ev_prot;
    183 	maxprot = PAX_MPROTECT_MAXPROTECT(l, prot, 0, UVM_PROT_ALL);
    184 	if ((prot & maxprot) != prot)
    185 		return EACCES;
    186 	if ((error = PAX_MPROTECT_VALIDATE(l, prot)))
    187 		return error;
    188 
    189 	/*
    190 	 * check the file system's opinion about mmapping the file
    191 	 */
    192 
    193 	error = VOP_MMAP(vp, prot, l->l_cred);
    194 	if (error)
    195 		return error;
    196 
    197 	if ((vp->v_vflag & VV_MAPPED) == 0) {
    198 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    199 		vp->v_vflag |= VV_MAPPED;
    200 		VOP_UNLOCK(vp);
    201 	}
    202 
    203 	/*
    204 	 * do the map, reference the object for this map entry
    205 	 */
    206 	uobj = &vp->v_uobj;
    207 	vref(vp);
    208 
    209 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
    210 		uobj, cmd->ev_offset, 0,
    211 		UVM_MAPFLAG(prot, maxprot, UVM_INH_COPY,
    212 			UVM_ADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED));
    213 	if (error) {
    214 		uobj->pgops->pgo_detach(uobj);
    215 	}
    216 	return error;
    217 }
    218 
    219 /*
    220  * vmcmd_map_readvn():
    221  *	handle vmcmd which specifies that a vnode should be read from.
    222  *	appropriate for non-demand-paged text/data segments, i.e. impure
    223  *	objects (a la OMAGIC and NMAGIC).
    224  */
    225 int
    226 vmcmd_map_readvn(struct lwp *l, struct exec_vmcmd *cmd)
    227 {
    228 	struct proc *p = l->l_proc;
    229 	int error;
    230 	long diff;
    231 
    232 	if (cmd->ev_len == 0)
    233 		return 0;
    234 
    235 	diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
    236 	cmd->ev_addr -= diff;			/* required by uvm_map */
    237 	cmd->ev_offset -= diff;
    238 	cmd->ev_len += diff;
    239 
    240 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
    241 			round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
    242 			UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
    243 			UVM_ADV_NORMAL,
    244 			UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
    245 
    246 	if (error)
    247 		return error;
    248 
    249 	return vmcmd_readvn(l, cmd);
    250 }
    251 
    252 int
    253 vmcmd_readvn(struct lwp *l, struct exec_vmcmd *cmd)
    254 {
    255 	struct proc *p = l->l_proc;
    256 	int error;
    257 	vm_prot_t prot, maxprot;
    258 
    259 	error = vn_rdwr(UIO_READ, cmd->ev_vp, (void *)cmd->ev_addr,
    260 	    cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT,
    261 	    l->l_cred, NULL, l);
    262 	if (error)
    263 		return error;
    264 
    265 	prot = cmd->ev_prot;
    266 	maxprot = PAX_MPROTECT_MAXPROTECT(l, prot, 0, UVM_PROT_ALL);
    267 	if ((prot & maxprot) != prot)
    268 		return EACCES;
    269 	if ((error = PAX_MPROTECT_VALIDATE(l, prot)))
    270 		return error;
    271 
    272 #ifdef PMAP_NEED_PROCWR
    273 	/*
    274 	 * we had to write the process, make sure the pages are synched
    275 	 * with the instruction cache.
    276 	 */
    277 	if (prot & VM_PROT_EXECUTE)
    278 		pmap_procwr(p, cmd->ev_addr, cmd->ev_len);
    279 #endif
    280 
    281 	/*
    282 	 * we had to map in the area at PROT_ALL so that vn_rdwr()
    283 	 * could write to it.   however, the caller seems to want
    284 	 * it mapped read-only, so now we are going to have to call
    285 	 * uvm_map_protect() to fix up the protection.  ICK.
    286 	 */
    287 	if (maxprot != VM_PROT_ALL) {
    288 		error = uvm_map_protect(&p->p_vmspace->vm_map,
    289 				trunc_page(cmd->ev_addr),
    290 				round_page(cmd->ev_addr + cmd->ev_len),
    291 				maxprot, true);
    292 		if (error)
    293 			return error;
    294 	}
    295 
    296 	if (prot != maxprot) {
    297 		error = uvm_map_protect(&p->p_vmspace->vm_map,
    298 				trunc_page(cmd->ev_addr),
    299 				round_page(cmd->ev_addr + cmd->ev_len),
    300 				prot, false);
    301 		if (error)
    302 			return error;
    303 	}
    304 
    305 	return 0;
    306 }
    307 
    308 /*
    309  * vmcmd_map_zero():
    310  *	handle vmcmd which specifies a zero-filled address space region.  The
    311  *	address range must be first allocated, then protected appropriately.
    312  */
    313 
    314 int
    315 vmcmd_map_zero(struct lwp *l, struct exec_vmcmd *cmd)
    316 {
    317 	struct proc *p = l->l_proc;
    318 	int error;
    319 	long diff;
    320 	vm_prot_t prot, maxprot;
    321 
    322 	diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
    323 	cmd->ev_addr -= diff;			/* required by uvm_map */
    324 	cmd->ev_len += diff;
    325 
    326 	prot = cmd->ev_prot;
    327 	maxprot = PAX_MPROTECT_MAXPROTECT(l, prot, 0, UVM_PROT_ALL);
    328 	if ((prot & maxprot) != prot)
    329 		return EACCES;
    330 	if ((error = PAX_MPROTECT_VALIDATE(l, prot)))
    331 		return error;
    332 
    333 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
    334 			round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
    335 			UVM_MAPFLAG(prot, maxprot, UVM_INH_COPY,
    336 			UVM_ADV_NORMAL,
    337 			UVM_FLAG_FIXED|UVM_FLAG_COPYONW));
    338 	if (cmd->ev_flags & VMCMD_STACK)
    339 		curproc->p_vmspace->vm_issize += atop(round_page(cmd->ev_len));
    340 	return error;
    341 }
    342 
    343 /*
    344  * exec_read_from():
    345  *
    346  *	Read from vnode into buffer at offset.
    347  */
    348 int
    349 exec_read_from(struct lwp *l, struct vnode *vp, u_long off, void *bf,
    350     size_t size)
    351 {
    352 	int error;
    353 	size_t resid;
    354 
    355 	if ((error = vn_rdwr(UIO_READ, vp, bf, size, off, UIO_SYSSPACE,
    356 	    0, l->l_cred, &resid, NULL)) != 0)
    357 		return error;
    358 	/*
    359 	 * See if we got all of it
    360 	 */
    361 	if (resid != 0)
    362 		return ENOEXEC;
    363 	return 0;
    364 }
    365 
    366 /*
    367  * exec_setup_stack(): Set up the stack segment for an elf
    368  * executable.
    369  *
    370  * Note that the ep_ssize parameter must be set to be the current stack
    371  * limit; this is adjusted in the body of execve() to yield the
    372  * appropriate stack segment usage once the argument length is
    373  * calculated.
    374  *
    375  * This function returns an int for uniformity with other (future) formats'
    376  * stack setup functions.  They might have errors to return.
    377  */
    378 
    379 int
    380 exec_setup_stack(struct lwp *l, struct exec_package *epp)
    381 {
    382 	vsize_t max_stack_size;
    383 	vaddr_t access_linear_min;
    384 	vsize_t access_size;
    385 	vaddr_t noaccess_linear_min;
    386 	vsize_t noaccess_size;
    387 
    388 #ifndef	USRSTACK32
    389 #define USRSTACK32	(0x00000000ffffffffL&~PGOFSET)
    390 #endif
    391 #ifndef MAXSSIZ32
    392 #define MAXSSIZ32	(MAXSSIZ >> 2)
    393 #endif
    394 
    395 	if (epp->ep_flags & EXEC_32) {
    396 		epp->ep_minsaddr = USRSTACK32;
    397 		max_stack_size = MAXSSIZ32;
    398 	} else {
    399 		epp->ep_minsaddr = USRSTACK;
    400 		max_stack_size = MAXSSIZ;
    401 	}
    402 
    403 	DPRINTF(("ep_minsaddr=%#jx max_stack_size=%#jx\n",
    404 	    (uintmax_t)epp->ep_minsaddr, (uintmax_t)max_stack_size));
    405 
    406 	pax_aslr_stack(epp, &max_stack_size);
    407 
    408 	DPRINTF(("[RLIMIT_STACK].lim_cur=%#jx max_stack_size=%#jx\n",
    409 	    (uintmax_t)l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur,
    410 	    (uintmax_t)max_stack_size));
    411 	epp->ep_ssize = MIN(l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur,
    412 	    max_stack_size);
    413 
    414 	l->l_proc->p_stackbase = epp->ep_minsaddr;
    415 
    416 	epp->ep_maxsaddr = (vaddr_t)STACK_GROW(epp->ep_minsaddr,
    417 	    max_stack_size);
    418 
    419 	DPRINTF(("ep_ssize=%#jx ep_minsaddr=%#jx ep_maxsaddr=%#jx\n",
    420 	    (uintmax_t)epp->ep_ssize, (uintmax_t)epp->ep_minsaddr,
    421 	    (uintmax_t)epp->ep_maxsaddr));
    422 
    423 	/*
    424 	 * set up commands for stack.  note that this takes *two*, one to
    425 	 * map the part of the stack which we can access, and one to map
    426 	 * the part which we can't.
    427 	 *
    428 	 * arguably, it could be made into one, but that would require the
    429 	 * addition of another mapping proc, which is unnecessary
    430 	 */
    431 	access_size = epp->ep_ssize;
    432 	access_linear_min = (vaddr_t)STACK_ALLOC(epp->ep_minsaddr, access_size);
    433 	noaccess_size = max_stack_size - access_size;
    434 	noaccess_linear_min = (vaddr_t)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr,
    435 	    access_size), noaccess_size);
    436 
    437 	DPRINTF(("access_size=%#jx, access_linear_min=%#jx, "
    438 	    "noaccess_size=%#jx, noaccess_linear_min=%#jx\n",
    439 	    (uintmax_t)access_size, (uintmax_t)access_linear_min,
    440 	    (uintmax_t)noaccess_size, (uintmax_t)noaccess_linear_min));
    441 
    442 	if (noaccess_size > 0 && noaccess_size <= MAXSSIZ) {
    443 		NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size,
    444 		    noaccess_linear_min, NULL, 0, VM_PROT_NONE, VMCMD_STACK);
    445 	}
    446 	KASSERT(access_size > 0 && access_size <= MAXSSIZ);
    447 	NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, access_size,
    448 	    access_linear_min, NULL, 0, VM_PROT_READ | VM_PROT_WRITE,
    449 	    VMCMD_STACK);
    450 
    451 	return 0;
    452 }
    453