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