Home | History | Annotate | Line # | Download | only in kern
kern_exec.c revision 1.60
      1 /*	$NetBSD: kern_exec.c,v 1.60 1995/02/04 14:44:48 mycroft Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1993, 1994 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 
     54 #include <sys/syscallargs.h>
     55 
     56 #include <vm/vm.h>
     57 #include <vm/vm_kern.h>
     58 
     59 #include <machine/cpu.h>
     60 #include <machine/reg.h>
     61 
     62 #ifdef COPY_SIGCODE
     63 extern char sigcode[], esigcode[];
     64 
     65 #define	szsigcode	(esigcode - sigcode)
     66 #else
     67 #define	szsigcode	0
     68 #endif
     69 
     70 /*
     71  * check exec:
     72  * given an "executable" described in the exec package's namei info,
     73  * see what we can do with it.
     74  *
     75  * ON ENTRY:
     76  *	exec package with appropriate namei info
     77  *	proc pointer of exec'ing proc
     78  *	NO SELF-LOCKED VNODES
     79  *
     80  * ON EXIT:
     81  *	error:	nothing held, etc.  exec header still allocated.
     82  *	ok:	filled exec package, one locked vnode.
     83  *
     84  * EXEC SWITCH ENTRY:
     85  * 	Locked vnode to check, exec package, proc.
     86  *
     87  * EXEC SWITCH EXIT:
     88  *	ok:	return 0, filled exec package, one locked vnode.
     89  *	error:	destructive:
     90  *			everything deallocated execept exec header.
     91  *		non-descructive:
     92  *			error code, locked vnode, exec header unmodified
     93  */
     94 int
     95 check_exec(p, epp)
     96 	struct proc *p;
     97 	struct exec_package *epp;
     98 {
     99 	int error, i;
    100 	struct vnode *vp;
    101 	char *cp, *ep, *name;
    102 	struct nameidata *ndp;
    103 	int resid;
    104 
    105 	ndp = epp->ep_ndp;
    106 	ndp->ni_cnd.cn_nameiop = LOOKUP;
    107 	ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME;
    108 	/* first get the vnode */
    109 	if (error = namei(ndp))
    110 		return error;
    111 	epp->ep_vp = vp = ndp->ni_vp;
    112 
    113 	/* check for regular file */
    114 	if (vp->v_type != VREG) {
    115 		error = EACCES;
    116 		goto bad1;
    117 	}
    118 
    119 	/* get attributes */
    120 	if (error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p))
    121 		goto bad1;
    122 
    123 	/* Check mount point */
    124 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
    125 		error = EACCES;
    126 		goto bad1;
    127 	}
    128 	if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED))
    129 		epp->ep_vap->va_mode &= ~(VSUID | VSGID);
    130 
    131 	/* check access.  for root we have to see if any exec bit on */
    132 	if (error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p))
    133 		goto bad1;
    134 	if ((epp->ep_vap->va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
    135 		error = EACCES;
    136 		goto bad1;
    137 	}
    138 
    139 	/* try to open it */
    140 	if (error = VOP_OPEN(vp, FREAD, p->p_ucred, p))
    141 		goto bad1;
    142 
    143 	/* now we have the file, get the exec header */
    144 	if (error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
    145 	    UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p))
    146 		goto bad2;
    147 	epp->ep_hdrvalid = epp->ep_hdrlen - resid;
    148 
    149 	/*
    150 	 * set up the vmcmds for creation of the process
    151 	 * address space
    152 	 */
    153 	error = ENOEXEC;
    154 	for (i = 0; i < nexecs && error != 0; i++) {
    155 		if (execsw[i].es_check != NULL)
    156 			error = (*execsw[i].es_check)(p, epp);
    157 		if (epp->ep_flags & EXEC_DESTR && error != 0)
    158 			return error;
    159 	}
    160 	if (!error) {
    161 		/* check that entry point is sane */
    162 		if (epp->ep_entry > VM_MAXUSER_ADDRESS)
    163 			error = ENOEXEC;
    164 
    165 		/* check limits */
    166 		if ((epp->ep_tsize > MAXTSIZ) ||
    167 		    (epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur))
    168 			error = ENOMEM;
    169 
    170 		if (!error)
    171 			return (0);
    172 	}
    173 
    174 	/*
    175 	 * free any vmspace-creation commands,
    176 	 * and release their references
    177 	 */
    178 	kill_vmcmds(&epp->ep_vmcmds);
    179 
    180 bad2:
    181 	/*
    182 	 * unlock and close the vnode, restore the old one, free the
    183 	 * pathname buf, and punt.
    184 	 */
    185 	VOP_UNLOCK(vp);
    186 	vn_close(vp, FREAD, p->p_ucred, p);
    187 	FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
    188 	return error;
    189 
    190 bad1:
    191 	/*
    192 	 * free the namei pathname buffer, and put the vnode
    193 	 * (which we don't yet have open).
    194 	 */
    195 	FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
    196 	vput(vp);
    197 	return error;
    198 }
    199 
    200 /*
    201  * exec system call
    202  */
    203 /* ARGSUSED */
    204 execve(p, uap, retval)
    205 	register struct proc *p;
    206 	register struct execve_args /* {
    207 		syscallarg(char *) path;
    208 		syscallarg(char * *) argp;
    209 		syscallarg(char * *) envp;
    210 	} */ *uap;
    211 	register_t *retval;
    212 {
    213 	int error, i;
    214 	struct exec_package pack;
    215 	struct nameidata nid;
    216 	struct vattr attr;
    217 	struct ucred *cred = p->p_ucred;
    218 	char *argp;
    219 	char **cpp, *dp, *sp, *np;
    220 	int argc, envc, len;
    221 	char *stack;
    222 	struct ps_strings arginfo;
    223 	struct vmspace *vm = p->p_vmspace;
    224 	char **tmpfap;
    225 
    226 	/*
    227 	 * figure out the maximum size of an exec header, if necessary.
    228 	 * XXX should be able to keep LKM code from modifying exec switch
    229 	 * when we're still using it, but...
    230 	 */
    231 	if (exec_maxhdrsz == 0) {
    232 		for (i = 0; i < nexecs; i++)
    233 			if (execsw[i].es_check != NULL
    234 			    && execsw[i].es_hdrsz > exec_maxhdrsz)
    235 				exec_maxhdrsz = execsw[i].es_hdrsz;
    236 	}
    237 
    238 	/* init the namei data to point the file user's program name */
    239 	NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    240 
    241 	/*
    242 	 * initialize the fields of the exec package.
    243 	 */
    244 	pack.ep_name = SCARG(uap, path);
    245 	MALLOC(pack.ep_hdr, void *, exec_maxhdrsz, M_EXEC, M_WAITOK);
    246 	pack.ep_hdrlen = exec_maxhdrsz;
    247 	pack.ep_hdrvalid = 0;
    248 	pack.ep_ndp = &nid;
    249 	pack.ep_setup = NULL;		/* assume no setup function */
    250 	pack.ep_setup_arg = NULL;
    251 	pack.ep_setup_arglen = 0;
    252 	pack.ep_vmcmds.evs_cnt = 0;
    253 	pack.ep_vmcmds.evs_used = 0;
    254 	pack.ep_vap = &attr;
    255 	pack.ep_emul = EMUL_NETBSD;
    256 	pack.ep_flags = 0;
    257 
    258 	/* see if we can run it. */
    259 	if (error = check_exec(p, &pack))
    260 		goto freehdr;
    261 
    262 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
    263 
    264 	/* allocate an argument buffer */
    265 	argp = (char *) kmem_alloc_wait(exec_map, NCARGS);
    266 #ifdef DIAGNOSTIC
    267 	if (argp == (vm_offset_t) 0)
    268 		panic("execve: argp == NULL");
    269 #endif
    270 	dp = argp;
    271 	argc = 0;
    272 
    273 	/* copy the fake args list, if there's one, freeing it as we go */
    274 	if (pack.ep_flags & EXEC_HASARGL) {
    275 		tmpfap = pack.ep_fa;
    276 		while (*tmpfap != NULL) {
    277 			char *cp;
    278 
    279 			cp = *tmpfap;
    280 			while (*cp)
    281 				*dp++ = *cp++;
    282 			*dp++;
    283 
    284 			FREE(*tmpfap, M_EXEC);
    285 			tmpfap++; argc++;
    286 		}
    287 		FREE(pack.ep_fa, M_EXEC);
    288 		pack.ep_flags &= ~EXEC_HASARGL;
    289 	}
    290 
    291 	/* Now get argv & environment */
    292 	if (!(cpp = SCARG(uap, argp))) {
    293 		error = EINVAL;
    294 		goto bad;
    295 	}
    296 
    297 	if (pack.ep_flags & EXEC_SKIPARG)
    298 		cpp++;
    299 
    300 	while (1) {
    301 		len = argp + ARG_MAX - dp;
    302 		if (error = copyin(cpp, &sp, sizeof(sp)))
    303 			goto bad;
    304 		if (!sp)
    305 			break;
    306 		if (error = copyinstr(sp, dp, len, (u_int *) & len)) {
    307 			if (error == ENAMETOOLONG)
    308 				error = E2BIG;
    309 			goto bad;
    310 		}
    311 		dp += len;
    312 		cpp++;
    313 		argc++;
    314 	}
    315 
    316 	envc = 0;
    317 	if (cpp = SCARG(uap, envp)) {	/* environment need not be there */
    318 		while (1) {
    319 			len = argp + ARG_MAX - dp;
    320 			if (error = copyin(cpp, &sp, sizeof(sp)))
    321 				goto bad;
    322 			if (!sp)
    323 				break;
    324 			if (error = copyinstr(sp, dp, len, (u_int *) & len)) {
    325 				if (error == ENAMETOOLONG)
    326 					error = E2BIG;
    327 				goto bad;
    328 			}
    329 			dp += len;
    330 			cpp++;
    331 			envc++;
    332 		}
    333 		dp = (char *) ALIGN(dp);
    334 	}
    335 
    336 	/* Now check if args & environ fit into new stack */
    337 	len = ((argc + envc + 2 + pack.ep_setup_arglen) * sizeof(char *) +
    338 	    sizeof(int) + dp + STACKGAPLEN + szsigcode +
    339 	    sizeof(struct ps_strings)) - argp;
    340 	len = ALIGN(len);	/* make the stack "safely" aligned */
    341 
    342 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
    343 		error = ENOMEM;
    344 		goto bad;
    345 	}
    346 
    347 	/* adjust "active stack depth" for process VSZ */
    348 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
    349 
    350 	/* Unmap old program */
    351 #ifdef sparc
    352 	kill_user_windows(p);		/* before stack addresses go away */
    353 #endif
    354 	/* Kill shared memory and unmap old program */
    355 #ifdef SYSVSHM
    356 	if (vm->vm_shm)
    357 		shmexit(p);
    358 #endif
    359 	vm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
    360 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
    361 
    362 	/* Now map address space */
    363 	vm->vm_taddr = (char *) pack.ep_taddr;
    364 	vm->vm_tsize = btoc(pack.ep_tsize);
    365 	vm->vm_daddr = (char *) pack.ep_daddr;
    366 	vm->vm_dsize = btoc(pack.ep_dsize);
    367 	vm->vm_ssize = btoc(pack.ep_ssize);
    368 	vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
    369 
    370 	/* create the new process's VM space by running the vmcmds */
    371 #ifdef DIAGNOSTIC
    372 	if (pack.ep_vmcmds.evs_used == 0)
    373 		panic("execve: no vmcmds");
    374 #endif
    375 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
    376 		struct exec_vmcmd *vcp;
    377 
    378 		vcp = &pack.ep_vmcmds.evs_cmds[i];
    379 		error = (*vcp->ev_proc)(p, vcp);
    380 	}
    381 
    382 	/* free the vmspace-creation commands, and release their references */
    383 	kill_vmcmds(&pack.ep_vmcmds);
    384 
    385 	/* if an error happened, deallocate and punt */
    386 	if (error)
    387 		goto exec_abort;
    388 
    389 	/* remember information about the process */
    390 	arginfo.ps_nargvstr = argc;
    391 	arginfo.ps_nenvstr = envc;
    392 
    393 	/* Now copy argc, args & environ to new stack */
    394 	stack = (char *) (USRSTACK - len);
    395 	cpp = (char **) stack;
    396 
    397 	if (copyout(&argc, cpp++, sizeof(argc)))
    398 		goto exec_abort;
    399 	dp = (char *) (cpp + argc + envc + 2 + pack.ep_setup_arglen);
    400 	sp = argp;
    401 	np = 0;
    402 
    403 	/* XXX don't copy them out, remap them! */
    404 	arginfo.ps_argvstr = dp; /* remember location of argv for later */
    405 	for (; --argc >= 0; sp += len, dp += len) {
    406 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    407 		    copyoutstr(sp, dp, ARG_MAX, &len))
    408 			goto exec_abort;
    409 	}
    410 	if (copyout(&np, cpp++, sizeof(np)))
    411 		goto exec_abort;
    412 
    413 	arginfo.ps_envstr = dp;	/* remember location of envp for later */
    414 	for (; --envc >= 0; sp += len, dp += len) {
    415 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    416 		    copyoutstr(sp, dp, ARG_MAX, &len))
    417 			goto exec_abort;
    418 	}
    419 	if (copyout(&np, cpp++, sizeof(np)))
    420 		goto exec_abort;
    421 
    422 	if (pack.ep_setup != NULL)
    423 		(*pack.ep_setup)(EXEC_SETUP_ADDARGS, p, &pack, cpp);
    424 
    425 	/* copy out the process's ps_strings structure */
    426 	if (copyout(&arginfo, (char *) PS_STRINGS, sizeof(arginfo)))
    427 		goto exec_abort;
    428 
    429 #ifdef COPY_SIGCODE
    430 	/* copy out the process's signal trapoline code */
    431 	if (copyout((char *) sigcode, ((char *) PS_STRINGS) - szsigcode,
    432 		szsigcode)) {
    433 		goto exec_abort;
    434 	}
    435 #endif
    436 
    437 	fdcloseexec(p);		/* handle close on exec */
    438 	execsigs(p);		/* reset catched signals */
    439 
    440 	/* set command name & other accounting info */
    441 	len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
    442 	bcopy(nid.ni_cnd.cn_nameptr, p->p_comm, len);
    443 	p->p_comm[len] = 0;
    444 	p->p_acflag &= ~AFORK;
    445 
    446 	/* record proc's vnode, for use by procfs and others */
    447         if (p->p_textvp)
    448                 vrele(p->p_textvp);
    449 	VREF(pack.ep_vp);
    450 	p->p_textvp = pack.ep_vp;
    451 
    452 	p->p_flag |= P_EXEC;
    453 	if (p->p_flag & P_PPWAIT) {
    454 		p->p_flag &= ~P_PPWAIT;
    455 		wakeup((caddr_t) p->p_pptr);
    456 	}
    457 
    458 	/*
    459 	 * deal with set[ug]id.
    460 	 * MNT_NOEXEC and P_TRACED have already been used to disable s[ug]id.
    461 	 */
    462 	p->p_flag &= ~P_SUGID;
    463 	if (((attr.va_mode & VSUID) != 0 &&
    464 	    p->p_ucred->cr_uid != attr.va_uid)
    465 	    || (attr.va_mode & VSGID) != 0 &&
    466 	    p->p_ucred->cr_gid != attr.va_gid) {
    467 		p->p_ucred = crcopy(cred);
    468 #ifdef KTRACE
    469 		/*
    470 		 * If process is being ktraced, turn off - unless
    471 		 * root set it.
    472 		 */
    473 		if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) {
    474 			vrele(p->p_tracep);
    475 			p->p_tracep = NULL;
    476 			p->p_traceflag = 0;
    477 		}
    478 #endif
    479 		if (attr.va_mode & VSUID)
    480 			p->p_ucred->cr_uid = attr.va_uid;
    481 		if (attr.va_mode & VSGID)
    482 			p->p_ucred->cr_gid = attr.va_gid;
    483 		p->p_flag |= P_SUGID;
    484 	}
    485 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
    486 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
    487 
    488 	kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
    489 
    490 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
    491 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    492 	vput(pack.ep_vp);
    493 
    494 	/* setup new registers and do misc. setup. */
    495 	setregs(p, pack.ep_entry, (u_long) stack, retval);
    496 	if (pack.ep_setup != NULL)
    497 		 (*pack.ep_setup)(EXEC_SETUP_FINISH, p, &pack, NULL);
    498 
    499 	if (p->p_flag & P_TRACED)
    500 		psignal(p, SIGTRAP);
    501 
    502 	p->p_emul = pack.ep_emul;
    503 	FREE(pack.ep_hdr, M_EXEC);
    504 	return 0;
    505 
    506 bad:
    507 	if (pack.ep_setup != NULL)
    508 		(*pack.ep_setup)(EXEC_SETUP_CLEANUP, p, &pack, dp);
    509 	/* free the vmspace-creation commands, and release their references */
    510 	kill_vmcmds(&pack.ep_vmcmds);
    511 	/* kill any opened file descriptor, if necessary */
    512 	if (pack.ep_flags & EXEC_HASFD) {
    513 		pack.ep_flags &= ~EXEC_HASFD;
    514 		(void) fdclose(p, pack.ep_fd);
    515 	}
    516 	/* close and put the exec'd file */
    517 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    518 	vput(pack.ep_vp);
    519 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
    520 	kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
    521 
    522 freehdr:
    523 	FREE(pack.ep_hdr, M_EXEC);
    524 	return error;
    525 
    526 exec_abort:
    527 	/*
    528 	 * the old process doesn't exist anymore.  exit gracefully.
    529 	 * get rid of the (new) address space we have created, if any, get rid
    530 	 * of our namei data and vnode, and exit noting failure
    531 	 */
    532 	vm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
    533 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
    534 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
    535 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    536 	vput(pack.ep_vp);
    537 	kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
    538 	FREE(pack.ep_hdr, M_EXEC);
    539 	exit1(p, W_EXITCODE(0, SIGABRT));
    540 	exit1(p, -1);
    541 
    542 	/* NOTREACHED */
    543 	return 0;
    544 }
    545