Home | History | Annotate | Line # | Download | only in kern
kern_exec.c revision 1.78
      1 /*	$NetBSD: kern_exec.c,v 1.78 1996/12/22 10:21:07 cgd Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1993, 1994, 1996 Christopher G. Demetriou
      5  * Copyright (C) 1992 Wolfgang Solfrank.
      6  * Copyright (C) 1992 TooLs GmbH.
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by TooLs GmbH.
     20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/filedesc.h>
     38 #include <sys/kernel.h>
     39 #include <sys/proc.h>
     40 #include <sys/mount.h>
     41 #include <sys/malloc.h>
     42 #include <sys/namei.h>
     43 #include <sys/vnode.h>
     44 #include <sys/file.h>
     45 #include <sys/acct.h>
     46 #include <sys/exec.h>
     47 #include <sys/ktrace.h>
     48 #include <sys/resourcevar.h>
     49 #include <sys/wait.h>
     50 #include <sys/mman.h>
     51 #include <sys/signalvar.h>
     52 #include <sys/stat.h>
     53 #ifdef SYSVSHM
     54 #include <sys/shm.h>
     55 #endif
     56 
     57 #include <sys/syscallargs.h>
     58 
     59 #include <vm/vm.h>
     60 #include <vm/vm_kern.h>
     61 
     62 #include <machine/cpu.h>
     63 #include <machine/reg.h>
     64 
     65 /*
     66  * check exec:
     67  * given an "executable" described in the exec package's namei info,
     68  * see what we can do with it.
     69  *
     70  * ON ENTRY:
     71  *	exec package with appropriate namei info
     72  *	proc pointer of exec'ing proc
     73  *	NO SELF-LOCKED VNODES
     74  *
     75  * ON EXIT:
     76  *	error:	nothing held, etc.  exec header still allocated.
     77  *	ok:	filled exec package, executable's vnode (unlocked).
     78  *
     79  * EXEC SWITCH ENTRY:
     80  * 	Locked vnode to check, exec package, proc.
     81  *
     82  * EXEC SWITCH EXIT:
     83  *	ok:	return 0, filled exec package, executable's vnode (unlocked).
     84  *	error:	destructive:
     85  *			everything deallocated execept exec header.
     86  *		non-destructive:
     87  *			error code, executable's vnode (unlocked),
     88  *			exec header unmodified.
     89  */
     90 int
     91 check_exec(p, epp)
     92 	struct proc *p;
     93 	struct exec_package *epp;
     94 {
     95 	int error, i;
     96 	struct vnode *vp;
     97 	struct nameidata *ndp;
     98 	int resid;
     99 
    100 	ndp = epp->ep_ndp;
    101 	ndp->ni_cnd.cn_nameiop = LOOKUP;
    102 	ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME;
    103 	/* first get the vnode */
    104 	if ((error = namei(ndp)) != 0)
    105 		return error;
    106 	epp->ep_vp = vp = ndp->ni_vp;
    107 
    108 	/* check for regular file */
    109 	if (vp->v_type != VREG) {
    110 		error = EACCES;
    111 		goto bad1;
    112 	}
    113 
    114 	/* get attributes */
    115 	if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0)
    116 		goto bad1;
    117 
    118 	/* Check mount point */
    119 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
    120 		error = EACCES;
    121 		goto bad1;
    122 	}
    123 	if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED))
    124 		epp->ep_vap->va_mode &= ~(VSUID | VSGID);
    125 
    126 	/* check access.  for root we have to see if any exec bit on */
    127 	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
    128 		goto bad1;
    129 	if ((epp->ep_vap->va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
    130 		error = EACCES;
    131 		goto bad1;
    132 	}
    133 
    134 	/* try to open it */
    135 	if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0)
    136 		goto bad1;
    137 
    138 	/* unlock vp, since we don't need it locked from here on out. */
    139 	VOP_UNLOCK(vp);
    140 
    141 	/* now we have the file, get the exec header */
    142 	error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
    143 			UIO_SYSSPACE, 0, p->p_ucred, &resid, p);
    144 	if (error)
    145 		goto bad2;
    146 	epp->ep_hdrvalid = epp->ep_hdrlen - resid;
    147 
    148 	/*
    149 	 * set up the vmcmds for creation of the process
    150 	 * address space
    151 	 */
    152 	error = ENOEXEC;
    153 	for (i = 0; i < nexecs && error != 0; i++) {
    154 		int newerror;
    155 
    156 		if (execsw[i].es_check == NULL)
    157 			continue;
    158 
    159 		newerror = (*execsw[i].es_check)(p, epp);
    160 		/* make sure the first "interesting" error code is saved. */
    161 		if (!newerror || error == ENOEXEC)
    162 			error = newerror;
    163 		if (epp->ep_flags & EXEC_DESTR && error != 0)
    164 			return error;
    165 	}
    166 	if (!error) {
    167 		/* check that entry point is sane */
    168 		if (epp->ep_entry > VM_MAXUSER_ADDRESS)
    169 			error = ENOEXEC;
    170 
    171 		/* check limits */
    172 		if ((epp->ep_tsize > MAXTSIZ) ||
    173 		    (epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur))
    174 			error = ENOMEM;
    175 
    176 		if (!error)
    177 			return (0);
    178 	}
    179 
    180 	/*
    181 	 * free any vmspace-creation commands,
    182 	 * and release their references
    183 	 */
    184 	kill_vmcmds(&epp->ep_vmcmds);
    185 
    186 bad2:
    187 	/*
    188 	 * unlock and close the vnode, restore the old one, free the
    189 	 * pathname buf, and punt.
    190 	 */
    191 	VOP_CLOSE(vp, FREAD, p->p_ucred, p);
    192 	vrele(vp);
    193 	FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
    194 	return error;
    195 
    196 bad1:
    197 	/*
    198 	 * free the namei pathname buffer, and put the vnode
    199 	 * (which we don't yet have open).
    200 	 */
    201 	vput(vp);				/* was still locked */
    202 	FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
    203 	return error;
    204 }
    205 
    206 /*
    207  * exec system call
    208  */
    209 /* ARGSUSED */
    210 int
    211 sys_execve(p, v, retval)
    212 	register struct proc *p;
    213 	void *v;
    214 	register_t *retval;
    215 {
    216 	register struct sys_execve_args /* {
    217 		syscallarg(const char *) path;
    218 		syscallarg(char * const *) argp;
    219 		syscallarg(char * const *) envp;
    220 	} */ *uap = v;
    221 	int error, i;
    222 	struct exec_package pack;
    223 	struct nameidata nid;
    224 	struct vattr attr;
    225 	struct ucred *cred = p->p_ucred;
    226 	char *argp;
    227 	char * const *cpp;
    228 	char *dp, *sp;
    229 	long argc, envc;
    230 	size_t len;
    231 	char *stack;
    232 	struct ps_strings arginfo;
    233 	struct vmspace *vm = p->p_vmspace;
    234 	char **tmpfap;
    235 	int szsigcode;
    236 	extern struct emul emul_netbsd;
    237 
    238 	/*
    239 	 * figure out the maximum size of an exec header, if necessary.
    240 	 * XXX should be able to keep LKM code from modifying exec switch
    241 	 * when we're still using it, but...
    242 	 */
    243 	if (exec_maxhdrsz == 0) {
    244 		for (i = 0; i < nexecs; i++)
    245 			if (execsw[i].es_check != NULL
    246 			    && execsw[i].es_hdrsz > exec_maxhdrsz)
    247 				exec_maxhdrsz = execsw[i].es_hdrsz;
    248 	}
    249 
    250 	/* init the namei data to point the file user's program name */
    251 	/* XXX cgd 960926: why do this here?  most will be clobbered. */
    252 	NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    253 
    254 	/*
    255 	 * initialize the fields of the exec package.
    256 	 */
    257 	pack.ep_name = SCARG(uap, path);
    258 	MALLOC(pack.ep_hdr, void *, exec_maxhdrsz, M_EXEC, M_WAITOK);
    259 	pack.ep_hdrlen = exec_maxhdrsz;
    260 	pack.ep_hdrvalid = 0;
    261 	pack.ep_ndp = &nid;
    262 	pack.ep_emul_arg = NULL;
    263 	pack.ep_vmcmds.evs_cnt = 0;
    264 	pack.ep_vmcmds.evs_used = 0;
    265 	pack.ep_vap = &attr;
    266 	pack.ep_emul = &emul_netbsd;
    267 	pack.ep_flags = 0;
    268 
    269 	/* see if we can run it. */
    270 	if ((error = check_exec(p, &pack)) != 0)
    271 		goto freehdr;
    272 
    273 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
    274 
    275 	/* allocate an argument buffer */
    276 	argp = (char *) kmem_alloc_wait(exec_map, NCARGS);
    277 #ifdef DIAGNOSTIC
    278 	if (argp == (vm_offset_t) 0)
    279 		panic("execve: argp == NULL");
    280 #endif
    281 	dp = argp;
    282 	argc = 0;
    283 
    284 	/* copy the fake args list, if there's one, freeing it as we go */
    285 	if (pack.ep_flags & EXEC_HASARGL) {
    286 		tmpfap = pack.ep_fa;
    287 		while (*tmpfap != NULL) {
    288 			char *cp;
    289 
    290 			cp = *tmpfap;
    291 			while (*cp)
    292 				*dp++ = *cp++;
    293 			dp++;
    294 
    295 			FREE(*tmpfap, M_EXEC);
    296 			tmpfap++; argc++;
    297 		}
    298 		FREE(pack.ep_fa, M_EXEC);
    299 		pack.ep_flags &= ~EXEC_HASARGL;
    300 	}
    301 
    302 	/* Now get argv & environment */
    303 	if (!(cpp = SCARG(uap, argp))) {
    304 		error = EINVAL;
    305 		goto bad;
    306 	}
    307 
    308 	if (pack.ep_flags & EXEC_SKIPARG)
    309 		cpp++;
    310 
    311 	while (1) {
    312 		len = argp + ARG_MAX - dp;
    313 		if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
    314 			goto bad;
    315 		if (!sp)
    316 			break;
    317 		if ((error = copyinstr(sp, dp, len, &len)) != 0) {
    318 			if (error == ENAMETOOLONG)
    319 				error = E2BIG;
    320 			goto bad;
    321 		}
    322 		dp += len;
    323 		cpp++;
    324 		argc++;
    325 	}
    326 
    327 	envc = 0;
    328 	/* environment need not be there */
    329 	if ((cpp = SCARG(uap, envp)) != NULL ) {
    330 		while (1) {
    331 			len = argp + ARG_MAX - dp;
    332 			if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
    333 				goto bad;
    334 			if (!sp)
    335 				break;
    336 			if ((error = copyinstr(sp, dp, len, &len)) != 0) {
    337 				if (error == ENAMETOOLONG)
    338 					error = E2BIG;
    339 				goto bad;
    340 			}
    341 			dp += len;
    342 			cpp++;
    343 			envc++;
    344 		}
    345 	}
    346 
    347 	dp = (char *) ALIGN(dp);
    348 
    349 	szsigcode = pack.ep_emul->e_esigcode - pack.ep_emul->e_sigcode;
    350 
    351 	/* Now check if args & environ fit into new stack */
    352 	len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(char *) +
    353 	    sizeof(long) + dp + STACKGAPLEN + szsigcode +
    354 	    sizeof(struct ps_strings)) - argp;
    355 
    356 	len = ALIGN(len);	/* make the stack "safely" aligned */
    357 
    358 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
    359 		error = ENOMEM;
    360 		goto bad;
    361 	}
    362 
    363 	/* adjust "active stack depth" for process VSZ */
    364 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
    365 
    366 	/* Unmap old program */
    367 	/* XXX cgd 960926: the sparc #ifdef should be a MD hook */
    368 #ifdef sparc
    369 	kill_user_windows(p);		/* before stack addresses go away */
    370 #endif
    371 	/* Kill shared memory and unmap old program */
    372 #ifdef SYSVSHM
    373 	if (vm->vm_shm)
    374 		shmexit(p);
    375 #endif
    376 	vm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
    377 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
    378 
    379 	/* Now map address space */
    380 	vm->vm_taddr = (char *) pack.ep_taddr;
    381 	vm->vm_tsize = btoc(pack.ep_tsize);
    382 	vm->vm_daddr = (char *) pack.ep_daddr;
    383 	vm->vm_dsize = btoc(pack.ep_dsize);
    384 	vm->vm_ssize = btoc(pack.ep_ssize);
    385 	vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
    386 
    387 	/* create the new process's VM space by running the vmcmds */
    388 #ifdef DIAGNOSTIC
    389 	if (pack.ep_vmcmds.evs_used == 0)
    390 		panic("execve: no vmcmds");
    391 #endif
    392 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
    393 		struct exec_vmcmd *vcp;
    394 
    395 		vcp = &pack.ep_vmcmds.evs_cmds[i];
    396 		error = (*vcp->ev_proc)(p, vcp);
    397 	}
    398 
    399 	/* free the vmspace-creation commands, and release their references */
    400 	kill_vmcmds(&pack.ep_vmcmds);
    401 
    402 	/* if an error happened, deallocate and punt */
    403 	if (error)
    404 		goto exec_abort;
    405 
    406 	/* remember information about the process */
    407 	arginfo.ps_nargvstr = argc;
    408 	arginfo.ps_nenvstr = envc;
    409 
    410 	stack = (char *) (USRSTACK - len);
    411 	/* Now copy argc, args & environ to new stack */
    412 	if (!(*pack.ep_emul->e_copyargs)(&pack, &arginfo, stack, argp))
    413 		goto exec_abort;
    414 
    415 	/* copy out the process's ps_strings structure */
    416 	if (copyout(&arginfo, (char *) PS_STRINGS, sizeof(arginfo)))
    417 		goto exec_abort;
    418 
    419 	/* copy out the process's signal trapoline code */
    420 	if (szsigcode && copyout((char *) pack.ep_emul->e_sigcode,
    421 				 ((char *) PS_STRINGS) - szsigcode,
    422 				 szsigcode))
    423 		goto exec_abort;
    424 
    425 	fdcloseexec(p);		/* handle close on exec */
    426 	execsigs(p);		/* reset catched signals */
    427 
    428 	/* set command name & other accounting info */
    429 	len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
    430 	bcopy(nid.ni_cnd.cn_nameptr, p->p_comm, len);
    431 	p->p_comm[len] = 0;
    432 	p->p_acflag &= ~AFORK;
    433 
    434 	/* record proc's vnode, for use by procfs and others */
    435         if (p->p_textvp)
    436                 vrele(p->p_textvp);
    437 	VREF(pack.ep_vp);
    438 	p->p_textvp = pack.ep_vp;
    439 
    440 	p->p_flag |= P_EXEC;
    441 	if (p->p_flag & P_PPWAIT) {
    442 		p->p_flag &= ~P_PPWAIT;
    443 		wakeup((caddr_t) p->p_pptr);
    444 	}
    445 
    446 	/*
    447 	 * deal with set[ug]id.
    448 	 * MNT_NOEXEC and P_TRACED have already been used to disable s[ug]id.
    449 	 */
    450 	p->p_flag &= ~P_SUGID;
    451 	if (((attr.va_mode & VSUID) != 0 && p->p_ucred->cr_uid != attr.va_uid)
    452 	 || ((attr.va_mode & VSGID) != 0 && p->p_ucred->cr_gid != attr.va_gid)){
    453 		p->p_ucred = crcopy(cred);
    454 #ifdef KTRACE
    455 		/*
    456 		 * If process is being ktraced, turn off - unless
    457 		 * root set it.
    458 		 */
    459 		if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) {
    460 			vrele(p->p_tracep);
    461 			p->p_tracep = NULL;
    462 			p->p_traceflag = 0;
    463 		}
    464 #endif
    465 		if (attr.va_mode & VSUID)
    466 			p->p_ucred->cr_uid = attr.va_uid;
    467 		if (attr.va_mode & VSGID)
    468 			p->p_ucred->cr_gid = attr.va_gid;
    469 		p->p_flag |= P_SUGID;
    470 	}
    471 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
    472 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
    473 
    474 	kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
    475 
    476 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
    477 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    478 	vrele(pack.ep_vp);
    479 
    480 	/* setup new registers and do misc. setup. */
    481 	(*pack.ep_emul->e_setregs)(p, &pack, (u_long) stack, retval);
    482 
    483 	if (p->p_flag & P_TRACED)
    484 		psignal(p, SIGTRAP);
    485 
    486 	p->p_emul = pack.ep_emul;
    487 	FREE(pack.ep_hdr, M_EXEC);
    488 
    489 #ifdef KTRACE
    490 	if (KTRPOINT(p, KTR_EMUL))
    491 		ktremul(p->p_tracep, p->p_emul->e_name);
    492 #endif
    493 	return 0;
    494 
    495 bad:
    496 	/* free the vmspace-creation commands, and release their references */
    497 	kill_vmcmds(&pack.ep_vmcmds);
    498 	/* kill any opened file descriptor, if necessary */
    499 	if (pack.ep_flags & EXEC_HASFD) {
    500 		pack.ep_flags &= ~EXEC_HASFD;
    501 		(void) fdrelease(p, pack.ep_fd);
    502 	}
    503 	/* close and put the exec'd file */
    504 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    505 	vrele(pack.ep_vp);
    506 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
    507 	kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
    508 
    509 freehdr:
    510 	FREE(pack.ep_hdr, M_EXEC);
    511 	return error;
    512 
    513 exec_abort:
    514 	/*
    515 	 * the old process doesn't exist anymore.  exit gracefully.
    516 	 * get rid of the (new) address space we have created, if any, get rid
    517 	 * of our namei data and vnode, and exit noting failure
    518 	 */
    519 	vm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
    520 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
    521 	if (pack.ep_emul_arg)
    522 		FREE(pack.ep_emul_arg, M_TEMP);
    523 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
    524 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    525 	vrele(pack.ep_vp);
    526 	kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
    527 	FREE(pack.ep_hdr, M_EXEC);
    528 	exit1(p, W_EXITCODE(0, SIGABRT));
    529 	exit1(p, -1);
    530 
    531 	/* NOTREACHED */
    532 	return 0;
    533 }
    534 
    535 
    536 void *
    537 copyargs(pack, arginfo, stack, argp)
    538 	struct exec_package *pack;
    539 	struct ps_strings *arginfo;
    540 	void *stack;
    541 	void *argp;
    542 {
    543 	char **cpp = stack;
    544 	char *dp, *sp;
    545 	size_t len;
    546 	void *nullp = NULL;
    547 	int argc = arginfo->ps_nargvstr;
    548 	int envc = arginfo->ps_nenvstr;
    549 
    550 	if (copyout(&argc, cpp++, sizeof(argc)))
    551 		return NULL;
    552 
    553 	dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen);
    554 	sp = argp;
    555 
    556 	/* XXX don't copy them out, remap them! */
    557 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
    558 
    559 	for (; --argc >= 0; sp += len, dp += len)
    560 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    561 		    copyoutstr(sp, dp, ARG_MAX, &len))
    562 			return NULL;
    563 
    564 	if (copyout(&nullp, cpp++, sizeof(nullp)))
    565 		return NULL;
    566 
    567 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
    568 
    569 	for (; --envc >= 0; sp += len, dp += len)
    570 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    571 		    copyoutstr(sp, dp, ARG_MAX, &len))
    572 			return NULL;
    573 
    574 	if (copyout(&nullp, cpp++, sizeof(nullp)))
    575 		return NULL;
    576 
    577 	return cpp;
    578 }
    579