Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_execve.c revision 1.19.2.1
      1 /*	$NetBSD: netbsd32_execve.c,v 1.19.2.1 2005/06/10 15:10:38 tron Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2001 Matthew R. Green
      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. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_execve.c,v 1.19.2.1 2005/06/10 15:10:38 tron Exp $");
     33 
     34 #if defined(_KERNEL_OPT)
     35 #include "opt_ktrace.h"
     36 #endif
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/malloc.h>
     41 #include <sys/mount.h>
     42 #include <sys/stat.h>
     43 #include <sys/wait.h>
     44 #include <sys/ktrace.h>
     45 #include <sys/vnode.h>
     46 #include <sys/file.h>
     47 #include <sys/filedesc.h>
     48 #include <sys/namei.h>
     49 
     50 #include <uvm/uvm_extern.h>
     51 
     52 #include <sys/sa.h>
     53 #include <sys/syscallargs.h>
     54 #include <sys/proc.h>
     55 #include <sys/acct.h>
     56 #include <sys/exec.h>
     57 
     58 #include <compat/netbsd32/netbsd32.h>
     59 #include <compat/netbsd32/netbsd32_syscall.h>
     60 #include <compat/netbsd32/netbsd32_syscallargs.h>
     61 
     62 #include <sys/verified_exec.h>
     63 
     64 /* this is provided by kern/kern_exec.c */
     65 extern u_int exec_maxhdrsz;
     66 #if defined(LKM) || defined(_LKM)
     67 extern struct lock exec_lock;
     68 #endif
     69 
     70 /*
     71  * Need to completly reimplement this syscall due to argument copying.
     72  */
     73 /* ARGSUSED */
     74 int
     75 netbsd32_execve(l, v, retval)
     76 	struct lwp *l;
     77 	void *v;
     78 	register_t *retval;
     79 {
     80 	struct netbsd32_execve_args /* {
     81 		syscallarg(const netbsd32_charp) path;
     82 		syscallarg(netbsd32_charpp) argp;
     83 		syscallarg(netbsd32_charpp) envp;
     84 	} */ *uap = v;
     85 	struct sys_execve_args ua;
     86 	caddr_t sg;
     87 	struct proc *p = l->l_proc;
     88 
     89 	NETBSD32TOP_UAP(path, const char);
     90 	NETBSD32TOP_UAP(argp, char *);
     91 	NETBSD32TOP_UAP(envp, char *);
     92 	sg = stackgap_init(p, 0);
     93 	CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
     94 
     95 	return netbsd32_execve2(l, &ua, retval);
     96 }
     97 
     98 int
     99 netbsd32_execve2(l, uap, retval)
    100 	struct lwp *l;
    101 	struct sys_execve_args *uap;
    102 	register_t *retval;
    103 {
    104 	/* Function args */
    105 	struct proc *p = l->l_proc;
    106 	int error, i;
    107 	struct exec_package pack;
    108 	struct nameidata nid;
    109 	struct vattr attr;
    110 	struct ucred *cred = p->p_ucred;
    111 	char *argp;
    112 	netbsd32_charp const *cpp;
    113 	char *dp;
    114 	netbsd32_charp sp;
    115 	long argc, envc;
    116 	size_t len;
    117 	char *stack;
    118 	struct ps_strings arginfo;
    119 	struct vmspace *vm;
    120 	char **tmpfap;
    121 	int szsigcode;
    122 	struct exec_vmcmd *base_vcp = NULL;
    123 
    124 	/*
    125 	 * Init the namei data to point the file user's program name.
    126 	 * This is done here rather than in check_exec(), so that it's
    127 	 * possible to override this settings if any of makecmd/probe
    128 	 * functions call check_exec() recursively - for example,
    129 	 * see exec_script_makecmds().
    130 	 */
    131 	NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    132 
    133 	/*
    134 	 * initialize the fields of the exec package.
    135 	 */
    136 	pack.ep_name = SCARG(uap, path);
    137 	pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK);
    138 	pack.ep_hdrlen = exec_maxhdrsz;
    139 	pack.ep_hdrvalid = 0;
    140 	pack.ep_ndp = &nid;
    141 	pack.ep_emul_arg = NULL;
    142 	pack.ep_vmcmds.evs_cnt = 0;
    143 	pack.ep_vmcmds.evs_used = 0;
    144 	pack.ep_vap = &attr;
    145 	pack.ep_flags = 0;
    146 
    147 #if defined(LKM) || defined(_LKM)
    148 	lockmgr(&exec_lock, LK_SHARED, NULL);
    149 #endif
    150 
    151 	/* see if we can run it. */
    152 #ifdef VERIFIED_EXEC
    153 	if ((error = check_exec(p, &pack, VERIEXEC_DIRECT)) != 0)
    154 #else
    155 	if ((error = check_exec(p, &pack)) != 0)
    156 #endif
    157 		goto freehdr;
    158 
    159 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
    160 
    161 	/* allocate an argument buffer */
    162 	argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
    163 #ifdef DIAGNOSTIC
    164 	if (argp == (vaddr_t) 0)
    165 		panic("netbsd32_execve: argp == NULL");
    166 #endif
    167 	dp = argp;
    168 	argc = 0;
    169 
    170 	/* copy the fake args list, if there's one, freeing it as we go */
    171 	if (pack.ep_flags & EXEC_HASARGL) {
    172 		tmpfap = pack.ep_fa;
    173 		while (*tmpfap != NULL) {
    174 			char *cp;
    175 
    176 			cp = *tmpfap;
    177 			while (*cp)
    178 				*dp++ = *cp++;
    179 			dp++;
    180 
    181 			FREE(*tmpfap, M_EXEC);
    182 			tmpfap++; argc++;
    183 		}
    184 		FREE(pack.ep_fa, M_EXEC);
    185 		pack.ep_flags &= ~EXEC_HASARGL;
    186 	}
    187 
    188 	/* Now get argv & environment */
    189 	if (!(cpp = (netbsd32_charp *)SCARG(uap, argp))) {
    190 		error = EINVAL;
    191 		goto bad;
    192 	}
    193 
    194 	if (pack.ep_flags & EXEC_SKIPARG)
    195 		cpp++;
    196 
    197 	while (1) {
    198 		len = argp + ARG_MAX - dp;
    199 		if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
    200 			goto bad;
    201 		if (!sp)
    202 			break;
    203 		if ((error = copyinstr((char *)(u_long)sp, dp,
    204 				       len, &len)) != 0) {
    205 			if (error == ENAMETOOLONG)
    206 				error = E2BIG;
    207 			goto bad;
    208 		}
    209 		dp += len;
    210 		cpp++;
    211 		argc++;
    212 	}
    213 
    214 	envc = 0;
    215 	/* environment need not be there */
    216 	if ((cpp = (netbsd32_charp *)SCARG(uap, envp)) != NULL ) {
    217 		while (1) {
    218 			len = argp + ARG_MAX - dp;
    219 			if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
    220 				goto bad;
    221 			if (!sp)
    222 				break;
    223 			if ((error = copyinstr((char *)(u_long)sp,
    224 					       dp, len, &len)) != 0) {
    225 				if (error == ENAMETOOLONG)
    226 					error = E2BIG;
    227 				goto bad;
    228 			}
    229 			dp += len;
    230 			cpp++;
    231 			envc++;
    232 		}
    233 	}
    234 
    235 	dp = (char *) ALIGN(dp);
    236 
    237 	szsigcode = pack.ep_es->es_emul->e_esigcode -
    238 	    pack.ep_es->es_emul->e_sigcode;
    239 
    240 	/* Now check if args & environ fit into new stack */
    241 	if (pack.ep_flags & EXEC_32)
    242 		len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
    243 		    sizeof(int) + sizeof(int) + dp + STACKGAPLEN +
    244 		    szsigcode + sizeof(struct ps_strings)) - argp;
    245 	else
    246 		len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
    247 		    sizeof(char *) + sizeof(int) + dp + STACKGAPLEN +
    248 		    szsigcode + sizeof(struct ps_strings)) - argp;
    249 
    250 	len = ALIGN(len);	/* make the stack "safely" aligned */
    251 
    252 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
    253 		error = ENOMEM;
    254 		goto bad;
    255 	}
    256 
    257 	/* adjust "active stack depth" for process VSZ */
    258 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
    259 
    260 	/*
    261 	 * Do whatever is necessary to prepare the address space
    262 	 * for remapping.  Note that this might replace the current
    263 	 * vmspace with another!
    264 	 */
    265 	uvmspace_exec(l, VM_MIN_ADDRESS, (vaddr_t)pack.ep_minsaddr);
    266 
    267 	/* Now map address space */
    268 	vm = p->p_vmspace;
    269 	vm->vm_taddr = (char *) pack.ep_taddr;
    270 	vm->vm_tsize = btoc(pack.ep_tsize);
    271 	vm->vm_daddr = (char *) pack.ep_daddr;
    272 	vm->vm_dsize = btoc(pack.ep_dsize);
    273 	vm->vm_ssize = btoc(pack.ep_ssize);
    274 	vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
    275 	vm->vm_minsaddr = (char *) pack.ep_minsaddr;
    276 
    277 	/* create the new process's VM space by running the vmcmds */
    278 #ifdef DIAGNOSTIC
    279 	if (pack.ep_vmcmds.evs_used == 0)
    280 		panic("netbsd32_execve: no vmcmds");
    281 #endif
    282 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
    283 		struct exec_vmcmd *vcp;
    284 
    285 		vcp = &pack.ep_vmcmds.evs_cmds[i];
    286 		if (vcp->ev_flags & VMCMD_RELATIVE) {
    287 #ifdef DIAGNOSTIC
    288 			if (base_vcp == NULL)
    289 				panic("netbsd32_execve: relative vmcmd with no base");
    290 			if (vcp->ev_flags & VMCMD_BASE)
    291 				panic("netbsd32_execve: illegal base & relative vmcmd");
    292 #endif
    293 			vcp->ev_addr += base_vcp->ev_addr;
    294 		}
    295 		error = (*vcp->ev_proc)(p, vcp);
    296 #ifdef DEBUG
    297 		if (error) {
    298 			int j;
    299 
    300 			for (j = 0; j <= i; j++)
    301 				printf("vmcmd[%d] = %#lx/%#lx @ %#lx\n", j,
    302 				       vcp[j-i].ev_addr, vcp[j-i].ev_len,
    303 				       vcp[j-i].ev_offset);
    304 		}
    305 #endif
    306 		if (vcp->ev_flags & VMCMD_BASE)
    307 			base_vcp = vcp;
    308 	}
    309 
    310 	/* free the vmspace-creation commands, and release their references */
    311 	kill_vmcmds(&pack.ep_vmcmds);
    312 
    313 	/* if an error happened, deallocate and punt */
    314 	if (error) {
    315 #ifdef DEBUG
    316 		printf("netbsd32_execve: vmcmd %i failed: %d\n", i-1, error);
    317 #endif
    318 		goto exec_abort;
    319 	}
    320 
    321 	/* remember information about the process */
    322 	arginfo.ps_nargvstr = argc;
    323 	arginfo.ps_nenvstr = envc;
    324 
    325 	stack = (char *) (vm->vm_minsaddr - len);
    326 	/* Now copy argc, args & environ to new stack */
    327 	error = (*pack.ep_es->es_copyargs)(p, &pack, &arginfo,
    328 	    &stack, argp);
    329 	if (error) {
    330 #ifdef DEBUG
    331 		printf("netbsd32_execve: copyargs failed\n");
    332 #endif
    333 		goto exec_abort;
    334 	}
    335 	/* restore the stack back to its original point */
    336 	stack = (char *) (vm->vm_minsaddr - len);
    337 
    338 	/* fill process ps_strings info */
    339 	p->p_psstr = (struct ps_strings *)(vm->vm_minsaddr -
    340 	    sizeof(struct ps_strings));
    341 	p->p_psargv = offsetof(struct ps_strings, ps_argvstr);
    342 	p->p_psnargv = offsetof(struct ps_strings, ps_nargvstr);
    343 	p->p_psenv = offsetof(struct ps_strings, ps_envstr);
    344 	p->p_psnenv = offsetof(struct ps_strings, ps_nenvstr);
    345 
    346 	/* copy out the process's ps_strings structure */
    347 	if (copyout(&arginfo, (char *)p->p_psstr, sizeof(arginfo))) {
    348 #ifdef DEBUG
    349 		printf("netbsd32_execve: ps_strings copyout failed\n");
    350 #endif
    351 		goto exec_abort;
    352 	}
    353 
    354 	/* copy out the process's signal trapoline code */
    355 	if (szsigcode) {
    356 		if (copyout((char *)pack.ep_es->es_emul->e_sigcode,
    357 		    p->p_sigctx.ps_sigcode = (char *)p->p_psstr - szsigcode,
    358 		    szsigcode)) {
    359 #ifdef DEBUG
    360 			printf("netbsd32_execve: sig trampoline copyout failed\n");
    361 #endif
    362 			goto exec_abort;
    363 		}
    364 #ifdef PMAP_NEED_PROCWR
    365 		/* This is code. Let the pmap do what is needed. */
    366 		pmap_procwr(p, (vaddr_t)p->p_sigctx.ps_sigcode, szsigcode);
    367 #endif
    368 	}
    369 
    370 	stopprofclock(p);	/* stop profiling */
    371 	fdcloseexec(p);		/* handle close on exec */
    372 	execsigs(p);		/* reset catched signals */
    373 	l->l_ctxlink = NULL;	/* reset ucontext link */
    374 
    375 	/* set command name & other accounting info */
    376 	len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
    377 	memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len);
    378 	p->p_comm[len] = 0;
    379 	p->p_acflag &= ~AFORK;
    380 
    381 	/* record proc's vnode, for use by procfs and others */
    382         if (p->p_textvp)
    383                 vrele(p->p_textvp);
    384 	VREF(pack.ep_vp);
    385 	p->p_textvp = pack.ep_vp;
    386 
    387 	p->p_flag |= P_EXEC;
    388 	if (p->p_flag & P_PPWAIT) {
    389 		p->p_flag &= ~P_PPWAIT;
    390 		wakeup((caddr_t) p->p_pptr);
    391 	}
    392 
    393 	/*
    394 	 * deal with set[ug]id.
    395 	 * MNT_NOSUID has already been used to disable s[ug]id.
    396 	 */
    397 	if ((p->p_flag & P_TRACED) == 0 &&
    398 
    399 	    (((attr.va_mode & S_ISUID) != 0 &&
    400 	      p->p_ucred->cr_uid != attr.va_uid) ||
    401 
    402 	     ((attr.va_mode & S_ISGID) != 0 &&
    403 	      p->p_ucred->cr_gid != attr.va_gid))) {
    404 		/*
    405 		 * Mark the process as SUGID before we do
    406 		 * anything that might block.
    407 		 */
    408 		p_sugid(p);
    409 
    410 		p->p_ucred = crcopy(cred);
    411 #ifdef KTRACE
    412 		/*
    413 		 * If process is being ktraced, turn off - unless
    414 		 * root set it.
    415 		 */
    416 		if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT))
    417 			ktrderef(p);
    418 #endif
    419 		if (attr.va_mode & S_ISUID)
    420 			p->p_ucred->cr_uid = attr.va_uid;
    421 		if (attr.va_mode & S_ISGID)
    422 			p->p_ucred->cr_gid = attr.va_gid;
    423 	} else
    424 		p->p_flag &= ~P_SUGID;
    425 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
    426 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
    427 
    428 	doexechooks(p);
    429 
    430 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
    431 
    432 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
    433 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
    434 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    435 	vput(pack.ep_vp);
    436 
    437 	/* setup new registers and do misc. setup. */
    438 	(*pack.ep_es->es_emul->e_setregs)(l, &pack, (u_long) stack);
    439 	if (pack.ep_es->es_setregs)
    440 		(*pack.ep_es->es_setregs)(l, &pack, (u_long) stack);
    441 
    442 	if (p->p_flag & P_TRACED)
    443 		psignal(p, SIGTRAP);
    444 
    445 	free(pack.ep_hdr, M_EXEC);
    446 
    447 	/*
    448 	 * Call emulation specific exec hook. This can setup setup per-process
    449 	 * p->p_emuldata or do any other per-process stuff an emulation needs.
    450 	 *
    451 	 * If we are executing process of different emulation than the
    452 	 * original forked process, call e_proc_exit() of the old emulation
    453 	 * first, then e_proc_exec() of new emulation. If the emulation is
    454 	 * same, the exec hook code should deallocate any old emulation
    455 	 * resources held previously by this process.
    456 	 */
    457 	if (p->p_emul && p->p_emul->e_proc_exit
    458 	    && p->p_emul != pack.ep_es->es_emul)
    459 		(*p->p_emul->e_proc_exit)(p);
    460 
    461 	/*
    462 	 * Call exec hook. Emulation code may NOT store reference to anything
    463 	 * from &pack.
    464 	 */
    465         if (pack.ep_es->es_emul->e_proc_exec)
    466                 (*pack.ep_es->es_emul->e_proc_exec)(p, &pack);
    467 
    468 	/* update p_emul, the old value is no longer needed */
    469 	p->p_emul = pack.ep_es->es_emul;
    470 
    471 	/* ...and the same for p_execsw */
    472 	p->p_execsw = pack.ep_es;
    473 
    474 #ifdef __HAVE_SYSCALL_INTERN
    475 	(*p->p_emul->e_syscall_intern)(p);
    476 #endif
    477 #ifdef KTRACE
    478 	if (KTRPOINT(p, KTR_EMUL))
    479 		ktremul(p);
    480 #endif
    481 
    482 #if defined(LKM) || defined(_LKM)
    483 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    484 #endif
    485 
    486 	return (EJUSTRETURN);
    487 
    488 bad:
    489 	/* free the vmspace-creation commands, and release their references */
    490 	kill_vmcmds(&pack.ep_vmcmds);
    491 	/* kill any opened file descriptor, if necessary */
    492 	if (pack.ep_flags & EXEC_HASFD) {
    493 		pack.ep_flags &= ~EXEC_HASFD;
    494 		(void) fdrelease(p, pack.ep_fd);
    495 	}
    496 	/* close and put the exec'd file */
    497 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
    498 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    499 	vput(pack.ep_vp);
    500 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
    501 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
    502 
    503 freehdr:
    504 #if defined(LKM) || defined(_LKM)
    505 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    506 #endif
    507 
    508 	free(pack.ep_hdr, M_EXEC);
    509 	return error;
    510 
    511 exec_abort:
    512 #if defined(LKM) || defined(_LKM)
    513 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    514 #endif
    515 
    516 	/*
    517 	 * the old process doesn't exist anymore.  exit gracefully.
    518 	 * get rid of the (new) address space we have created, if any, get rid
    519 	 * of our namei data and vnode, and exit noting failure
    520 	 */
    521 	uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
    522 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
    523 	if (pack.ep_emul_arg)
    524 		FREE(pack.ep_emul_arg, M_TEMP);
    525 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
    526 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
    527 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    528 	vput(pack.ep_vp);
    529 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
    530 	free(pack.ep_hdr, M_EXEC);
    531 	exit1(l, W_EXITCODE(error, SIGABRT));
    532 
    533 	/* NOTREACHED */
    534 	return 0;
    535 }
    536