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