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