xref: /netbsd/sys/kern/kern_exec.c (revision c4a72b64)
1 /*	$NetBSD: kern_exec.c,v 1.163 2002/11/17 22:53:46 chs 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/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.163 2002/11/17 22:53:46 chs Exp $");
37 
38 #include "opt_ktrace.h"
39 #include "opt_syscall_debug.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/filedesc.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/mount.h>
47 #include <sys/malloc.h>
48 #include <sys/namei.h>
49 #include <sys/vnode.h>
50 #include <sys/file.h>
51 #include <sys/acct.h>
52 #include <sys/exec.h>
53 #include <sys/ktrace.h>
54 #include <sys/resourcevar.h>
55 #include <sys/wait.h>
56 #include <sys/mman.h>
57 #include <sys/ras.h>
58 #include <sys/signalvar.h>
59 #include <sys/stat.h>
60 #include <sys/syscall.h>
61 
62 #include <sys/syscallargs.h>
63 
64 #include <uvm/uvm_extern.h>
65 
66 #include <machine/cpu.h>
67 #include <machine/reg.h>
68 
69 #ifdef DEBUG_EXEC
70 #define DPRINTF(a) uprintf a
71 #else
72 #define DPRINTF(a)
73 #endif /* DEBUG_EXEC */
74 
75 /*
76  * Exec function switch:
77  *
78  * Note that each makecmds function is responsible for loading the
79  * exec package with the necessary functions for any exec-type-specific
80  * handling.
81  *
82  * Functions for specific exec types should be defined in their own
83  * header file.
84  */
85 extern const struct execsw	execsw_builtin[];
86 extern int			nexecs_builtin;
87 static const struct execsw	**execsw = NULL;
88 static int			nexecs;
89 
90 u_int	exec_maxhdrsz;		/* must not be static - netbsd32 needs it */
91 
92 #ifdef LKM
93 /* list of supported emulations */
94 static
95 LIST_HEAD(emlist_head, emul_entry) el_head = LIST_HEAD_INITIALIZER(el_head);
96 struct emul_entry {
97 	LIST_ENTRY(emul_entry)	el_list;
98 	const struct emul	*el_emul;
99 	int			ro_entry;
100 };
101 
102 /* list of dynamically loaded execsw entries */
103 static
104 LIST_HEAD(execlist_head, exec_entry) ex_head = LIST_HEAD_INITIALIZER(ex_head);
105 struct exec_entry {
106 	LIST_ENTRY(exec_entry)	ex_list;
107 	const struct execsw	*es;
108 };
109 
110 /* structure used for building execw[] */
111 struct execsw_entry {
112 	struct execsw_entry	*next;
113 	const struct execsw	*es;
114 };
115 #endif /* LKM */
116 
117 /* NetBSD emul struct */
118 extern char	sigcode[], esigcode[];
119 #ifdef SYSCALL_DEBUG
120 extern const char * const syscallnames[];
121 #endif
122 #ifdef __HAVE_SYSCALL_INTERN
123 void syscall_intern(struct proc *);
124 #else
125 void syscall(void);
126 #endif
127 
128 const struct emul emul_netbsd = {
129 	"netbsd",
130 	NULL,		/* emulation path */
131 #ifndef __HAVE_MINIMAL_EMUL
132 	EMUL_HAS_SYS___syscall,
133 	NULL,
134 	SYS_syscall,
135 	SYS_NSYSENT,
136 #endif
137 	sysent,
138 #ifdef SYSCALL_DEBUG
139 	syscallnames,
140 #else
141 	NULL,
142 #endif
143 	sendsig,
144 	trapsignal,
145 	sigcode,
146 	esigcode,
147 	setregs,
148 	NULL,
149 	NULL,
150 	NULL,
151 #ifdef __HAVE_SYSCALL_INTERN
152 	syscall_intern,
153 #else
154 	syscall,
155 #endif
156 	NULL,
157 	NULL,
158 };
159 
160 #ifdef LKM
161 /*
162  * Exec lock. Used to control access to execsw[] structures.
163  * This must not be static so that netbsd32 can access it, too.
164  */
165 struct lock exec_lock;
166 
167 static void link_es(struct execsw_entry **, const struct execsw *);
168 #endif /* LKM */
169 
170 /*
171  * check exec:
172  * given an "executable" described in the exec package's namei info,
173  * see what we can do with it.
174  *
175  * ON ENTRY:
176  *	exec package with appropriate namei info
177  *	proc pointer of exec'ing proc
178  *      iff verified exec enabled then flag indicating a direct exec or
179  *        an indirect exec (i.e. for a shell script interpreter)
180  *	NO SELF-LOCKED VNODES
181  *
182  * ON EXIT:
183  *	error:	nothing held, etc.  exec header still allocated.
184  *	ok:	filled exec package, executable's vnode (unlocked).
185  *
186  * EXEC SWITCH ENTRY:
187  * 	Locked vnode to check, exec package, proc.
188  *
189  * EXEC SWITCH EXIT:
190  *	ok:	return 0, filled exec package, executable's vnode (unlocked).
191  *	error:	destructive:
192  *			everything deallocated execept exec header.
193  *		non-destructive:
194  *			error code, executable's vnode (unlocked),
195  *			exec header unmodified.
196  */
197 int
198 #ifdef VERIFIED_EXEC
199 check_exec(struct proc *p, struct exec_package *epp, int direct_exec)
200 #else
201 check_exec(struct proc *p, struct exec_package *epp)
202 #endif
203 {
204 	int		error, i;
205 	struct vnode	*vp;
206 	struct nameidata *ndp;
207 	size_t		resid;
208 
209 	ndp = epp->ep_ndp;
210 	ndp->ni_cnd.cn_nameiop = LOOKUP;
211 	ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME;
212 	/* first get the vnode */
213 	if ((error = namei(ndp)) != 0)
214 		return error;
215 	epp->ep_vp = vp = ndp->ni_vp;
216 
217 	/* check access and type */
218 	if (vp->v_type != VREG) {
219 		error = EACCES;
220 		goto bad1;
221 	}
222 	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
223 		goto bad1;
224 
225 	/* get attributes */
226 	if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0)
227 		goto bad1;
228 
229 	/* Check mount point */
230 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
231 		error = EACCES;
232 		goto bad1;
233 	}
234 	if (vp->v_mount->mnt_flag & MNT_NOSUID)
235 		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
236 
237 	/* try to open it */
238 	if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0)
239 		goto bad1;
240 
241 	/* unlock vp, since we need it unlocked from here on out. */
242 	VOP_UNLOCK(vp, 0);
243 
244 
245 #ifdef VERIFIED_EXEC
246         /* Evaluate signature for file... */
247         if ((error = check_veriexec(p, vp, epp, direct_exec)) != 0)
248                 goto bad2;
249 #endif
250 
251 	/* now we have the file, get the exec header */
252 	uvn_attach(vp, VM_PROT_READ);
253 	error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
254 			UIO_SYSSPACE, 0, p->p_ucred, &resid, p);
255 	if (error)
256 		goto bad2;
257 	epp->ep_hdrvalid = epp->ep_hdrlen - resid;
258 
259 	/*
260 	 * Set up default address space limits.  Can be overridden
261 	 * by individual exec packages.
262 	 *
263 	 * XXX probably shoul be all done in the exec pakages.
264 	 */
265 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
266 	epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS;
267 	/*
268 	 * set up the vmcmds for creation of the process
269 	 * address space
270 	 */
271 	error = ENOEXEC;
272 	for (i = 0; i < nexecs && error != 0; i++) {
273 		int newerror;
274 
275 		epp->ep_esch = execsw[i];
276 		newerror = (*execsw[i]->es_check)(p, epp);
277 		/* make sure the first "interesting" error code is saved. */
278 		if (!newerror || error == ENOEXEC)
279 			error = newerror;
280 
281 		/* if es_check call was successful, update epp->ep_es */
282 		if (!newerror && (epp->ep_flags & EXEC_HASES) == 0)
283 			epp->ep_es = execsw[i];
284 
285 		if (epp->ep_flags & EXEC_DESTR && error != 0)
286 			return error;
287 	}
288 	if (!error) {
289 		/* check that entry point is sane */
290 		if (epp->ep_entry > VM_MAXUSER_ADDRESS)
291 			error = ENOEXEC;
292 
293 		/* check limits */
294 		if ((epp->ep_tsize > MAXTSIZ) ||
295 		    (epp->ep_dsize >
296 		     (u_quad_t)p->p_rlimit[RLIMIT_DATA].rlim_cur))
297 			error = ENOMEM;
298 
299 		if (!error)
300 			return (0);
301 	}
302 
303 	/*
304 	 * free any vmspace-creation commands,
305 	 * and release their references
306 	 */
307 	kill_vmcmds(&epp->ep_vmcmds);
308 
309 bad2:
310 	/*
311 	 * close and release the vnode, restore the old one, free the
312 	 * pathname buf, and punt.
313 	 */
314 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
315 	VOP_CLOSE(vp, FREAD, p->p_ucred, p);
316 	vput(vp);
317 	PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
318 	return error;
319 
320 bad1:
321 	/*
322 	 * free the namei pathname buffer, and put the vnode
323 	 * (which we don't yet have open).
324 	 */
325 	vput(vp);				/* was still locked */
326 	PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
327 	return error;
328 }
329 
330 /*
331  * exec system call
332  */
333 /* ARGSUSED */
334 int
335 sys_execve(struct proc *p, void *v, register_t *retval)
336 {
337 	struct sys_execve_args /* {
338 		syscallarg(const char *)	path;
339 		syscallarg(char * const *)	argp;
340 		syscallarg(char * const *)	envp;
341 	} */ *uap = v;
342 	int			error;
343 	u_int			i;
344 	struct exec_package	pack;
345 	struct nameidata	nid;
346 	struct vattr		attr;
347 	struct ucred		*cred;
348 	char			*argp;
349 	char * const		*cpp;
350 	char			*dp, *sp;
351 	long			argc, envc;
352 	size_t			len;
353 	char			*stack;
354 	struct ps_strings	arginfo;
355 	struct vmspace		*vm;
356 	char			**tmpfap;
357 	int			szsigcode;
358 	struct exec_vmcmd	*base_vcp;
359 
360 	/*
361 	 * Lock the process and set the P_INEXEC flag to indicate that
362 	 * it should be left alone until we're done here.  This is
363 	 * necessary to avoid race conditions - e.g. in ptrace() -
364 	 * that might allow a local user to illicitly obtain elevated
365 	 * privileges.
366 	 */
367 	p->p_flag |= P_INEXEC;
368 
369 	cred = p->p_ucred;
370 	base_vcp = NULL;
371 	/*
372 	 * Init the namei data to point the file user's program name.
373 	 * This is done here rather than in check_exec(), so that it's
374 	 * possible to override this settings if any of makecmd/probe
375 	 * functions call check_exec() recursively - for example,
376 	 * see exec_script_makecmds().
377 	 */
378 	NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
379 
380 	/*
381 	 * initialize the fields of the exec package.
382 	 */
383 	pack.ep_name = SCARG(uap, path);
384 	pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK);
385 	pack.ep_hdrlen = exec_maxhdrsz;
386 	pack.ep_hdrvalid = 0;
387 	pack.ep_ndp = &nid;
388 	pack.ep_emul_arg = NULL;
389 	pack.ep_vmcmds.evs_cnt = 0;
390 	pack.ep_vmcmds.evs_used = 0;
391 	pack.ep_vap = &attr;
392 	pack.ep_flags = 0;
393 
394 #ifdef LKM
395 	lockmgr(&exec_lock, LK_SHARED, NULL);
396 #endif
397 
398 	/* see if we can run it. */
399 #ifdef VERIFIED_EXEC
400         if ((error = check_exec(p, &pack, 1)) != 0)
401         //if ((error = check_exec(p, &pack, 0)) != 0)
402 #else
403         if ((error = check_exec(p, &pack)) != 0)
404 #endif
405 		goto freehdr;
406 
407 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
408 
409 	/* allocate an argument buffer */
410 	argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
411 #ifdef DIAGNOSTIC
412 	if (argp == (vaddr_t) 0)
413 		panic("execve: argp == NULL");
414 #endif
415 	dp = argp;
416 	argc = 0;
417 
418 	/* copy the fake args list, if there's one, freeing it as we go */
419 	if (pack.ep_flags & EXEC_HASARGL) {
420 		tmpfap = pack.ep_fa;
421 		while (*tmpfap != NULL) {
422 			char *cp;
423 
424 			cp = *tmpfap;
425 			while (*cp)
426 				*dp++ = *cp++;
427 			dp++;
428 
429 			FREE(*tmpfap, M_EXEC);
430 			tmpfap++; argc++;
431 		}
432 		FREE(pack.ep_fa, M_EXEC);
433 		pack.ep_flags &= ~EXEC_HASARGL;
434 	}
435 
436 	/* Now get argv & environment */
437 	if (!(cpp = SCARG(uap, argp))) {
438 		error = EINVAL;
439 		goto bad;
440 	}
441 
442 	if (pack.ep_flags & EXEC_SKIPARG)
443 		cpp++;
444 
445 	while (1) {
446 		len = argp + ARG_MAX - dp;
447 		if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
448 			goto bad;
449 		if (!sp)
450 			break;
451 		if ((error = copyinstr(sp, dp, len, &len)) != 0) {
452 			if (error == ENAMETOOLONG)
453 				error = E2BIG;
454 			goto bad;
455 		}
456 		dp += len;
457 		cpp++;
458 		argc++;
459 	}
460 
461 	envc = 0;
462 	/* environment need not be there */
463 	if ((cpp = SCARG(uap, envp)) != NULL ) {
464 		while (1) {
465 			len = argp + ARG_MAX - dp;
466 			if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
467 				goto bad;
468 			if (!sp)
469 				break;
470 			if ((error = copyinstr(sp, dp, len, &len)) != 0) {
471 				if (error == ENAMETOOLONG)
472 					error = E2BIG;
473 				goto bad;
474 			}
475 			dp += len;
476 			cpp++;
477 			envc++;
478 		}
479 	}
480 
481 	dp = (char *) ALIGN(dp);
482 
483 	szsigcode = pack.ep_es->es_emul->e_esigcode -
484 	    pack.ep_es->es_emul->e_sigcode;
485 
486 	/* Now check if args & environ fit into new stack */
487 	if (pack.ep_flags & EXEC_32)
488 		len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
489 		    sizeof(int) + sizeof(int) + dp + STACKGAPLEN +
490 		    szsigcode + sizeof(struct ps_strings)) - argp;
491 	else
492 		len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
493 		    sizeof(char *) + sizeof(int) + dp + STACKGAPLEN +
494 		    szsigcode + sizeof(struct ps_strings)) - argp;
495 
496 	len = ALIGN(len);	/* make the stack "safely" aligned */
497 
498 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
499 		error = ENOMEM;
500 		goto bad;
501 	}
502 
503 	/* adjust "active stack depth" for process VSZ */
504 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
505 
506 	/*
507 	 * Do whatever is necessary to prepare the address space
508 	 * for remapping.  Note that this might replace the current
509 	 * vmspace with another!
510 	 */
511 	uvmspace_exec(p, pack.ep_vm_minaddr, pack.ep_vm_maxaddr);
512 
513 	/* Now map address space */
514 	vm = p->p_vmspace;
515 	vm->vm_taddr = (caddr_t) pack.ep_taddr;
516 	vm->vm_tsize = btoc(pack.ep_tsize);
517 	vm->vm_daddr = (caddr_t) pack.ep_daddr;
518 	vm->vm_dsize = btoc(pack.ep_dsize);
519 	vm->vm_ssize = btoc(pack.ep_ssize);
520 	vm->vm_maxsaddr = (caddr_t) pack.ep_maxsaddr;
521 	vm->vm_minsaddr = (caddr_t) pack.ep_minsaddr;
522 
523 	/* create the new process's VM space by running the vmcmds */
524 #ifdef DIAGNOSTIC
525 	if (pack.ep_vmcmds.evs_used == 0)
526 		panic("execve: no vmcmds");
527 #endif
528 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
529 		struct exec_vmcmd *vcp;
530 
531 		vcp = &pack.ep_vmcmds.evs_cmds[i];
532 		if (vcp->ev_flags & VMCMD_RELATIVE) {
533 #ifdef DIAGNOSTIC
534 			if (base_vcp == NULL)
535 				panic("execve: relative vmcmd with no base");
536 			if (vcp->ev_flags & VMCMD_BASE)
537 				panic("execve: illegal base & relative vmcmd");
538 #endif
539 			vcp->ev_addr += base_vcp->ev_addr;
540 		}
541 		error = (*vcp->ev_proc)(p, vcp);
542 #ifdef DEBUG_EXEC
543 		if (error) {
544 			int j;
545 			struct exec_vmcmd *vp = &pack.ep_vmcmds.evs_cmds[0];
546 			for (j = 0; j <= i; j++)
547 				uprintf(
548 			    "vmcmd[%d] = %#lx/%#lx fd@%#lx prot=0%o flags=%d\n",
549 				    j, vp[j].ev_addr, vp[j].ev_len,
550 				    vp[j].ev_offset, vp[j].ev_prot,
551 				    vp[j].ev_flags);
552 		}
553 #endif /* DEBUG_EXEC */
554 		if (vcp->ev_flags & VMCMD_BASE)
555 			base_vcp = vcp;
556 	}
557 
558 	/* free the vmspace-creation commands, and release their references */
559 	kill_vmcmds(&pack.ep_vmcmds);
560 
561 	/* if an error happened, deallocate and punt */
562 	if (error) {
563 		DPRINTF(("execve: vmcmd %i failed: %d\n", i - 1, error));
564 		goto exec_abort;
565 	}
566 
567 	/* remember information about the process */
568 	arginfo.ps_nargvstr = argc;
569 	arginfo.ps_nenvstr = envc;
570 
571 	stack = (char *)STACK_ALLOC(STACK_GROW(vm->vm_minsaddr,
572 		sizeof(struct ps_strings) + szsigcode),
573 		len - (sizeof(struct ps_strings) + szsigcode));
574 #ifdef __MACHINE_STACK_GROWS_UP
575 	/*
576 	 * The copyargs call always copies into lower addresses
577 	 * first, moving towards higher addresses, starting with
578 	 * the stack pointer that we give.  When the stack grows
579 	 * down, this puts argc/argv/envp very shallow on the
580 	 * stack, right at the first user stack pointer, and puts
581 	 * STACKGAPLEN very deep in the stack.  When the stack
582 	 * grows up, the situation is reversed.
583 	 *
584 	 * Normally, this is no big deal.  But the ld_elf.so _rtld()
585 	 * function expects to be called with a single pointer to
586 	 * a region that has a few words it can stash values into,
587 	 * followed by argc/argv/envp.  When the stack grows down,
588 	 * it's easy to decrement the stack pointer a little bit to
589 	 * allocate the space for these few words and pass the new
590 	 * stack pointer to _rtld.  When the stack grows up, however,
591 	 * a few words before argc is part of the signal trampoline,
592 	 * so we have a problem.
593 	 *
594 	 * Instead of changing how _rtld works, we take the easy way
595 	 * out and steal 32 bytes before we call copyargs.  This
596 	 * space is effectively stolen from STACKGAPLEN.
597 	 */
598 	stack += 32;
599 #endif /* __MACHINE_STACK_GROWS_UP */
600 
601 	/* Now copy argc, args & environ to new stack */
602 	error = (*pack.ep_es->es_copyargs)(p, &pack, &arginfo, &stack, argp);
603 	if (error) {
604 		DPRINTF(("execve: copyargs failed %d\n", error));
605 		goto exec_abort;
606 	}
607 	/* Move the stack back to original point */
608 	stack = (char *)STACK_GROW(vm->vm_minsaddr, len);
609 
610 	/* fill process ps_strings info */
611 	p->p_psstr = (struct ps_strings *)STACK_ALLOC(vm->vm_minsaddr,
612 	    sizeof(struct ps_strings));
613 	p->p_psargv = offsetof(struct ps_strings, ps_argvstr);
614 	p->p_psnargv = offsetof(struct ps_strings, ps_nargvstr);
615 	p->p_psenv = offsetof(struct ps_strings, ps_envstr);
616 	p->p_psnenv = offsetof(struct ps_strings, ps_nenvstr);
617 
618 	/* copy out the process's ps_strings structure */
619 	if ((error = copyout(&arginfo, (char *)p->p_psstr,
620 	    sizeof(arginfo))) != 0) {
621 		DPRINTF(("execve: ps_strings copyout %p->%p size %ld failed\n",
622 		       &arginfo, (char *)p->p_psstr, (long)sizeof(arginfo)));
623 		goto exec_abort;
624 	}
625 
626 	/* copy out the process's signal trampoline code */
627 	if (szsigcode) {
628 		p->p_sigctx.ps_sigcode = STACK_ALLOC(STACK_MAX(p->p_psstr,
629 		    sizeof(struct ps_strings)), szsigcode);
630 		if ((error = copyout((char *)pack.ep_es->es_emul->e_sigcode,
631 		    p->p_sigctx.ps_sigcode, szsigcode)) != 0) {
632 			DPRINTF(("execve: sig trampoline copyout failed\n"));
633 			goto exec_abort;
634 		}
635 #ifdef PMAP_NEED_PROCWR
636 		/* This is code. Let the pmap do what is needed. */
637 		pmap_procwr(p, (vaddr_t)p->p_sigctx.ps_sigcode, szsigcode);
638 #endif
639 	}
640 
641 	stopprofclock(p);	/* stop profiling */
642 	fdcloseexec(p);		/* handle close on exec */
643 	execsigs(p);		/* reset catched signals */
644 	p->p_ctxlink = NULL;	/* reset ucontext link */
645 
646 	/* set command name & other accounting info */
647 	len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
648 	memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len);
649 	p->p_comm[len] = 0;
650 	p->p_acflag &= ~AFORK;
651 
652 	/* record proc's vnode, for use by procfs and others */
653         if (p->p_textvp)
654                 vrele(p->p_textvp);
655 	VREF(pack.ep_vp);
656 	p->p_textvp = pack.ep_vp;
657 
658 	p->p_flag |= P_EXEC;
659 	if (p->p_flag & P_PPWAIT) {
660 		p->p_flag &= ~P_PPWAIT;
661 		wakeup((caddr_t) p->p_pptr);
662 	}
663 
664 	/*
665 	 * deal with set[ug]id.
666 	 * MNT_NOSUID has already been used to disable s[ug]id.
667 	 */
668 	if ((p->p_flag & P_TRACED) == 0 &&
669 
670 	    (((attr.va_mode & S_ISUID) != 0 &&
671 	      p->p_ucred->cr_uid != attr.va_uid) ||
672 
673 	     ((attr.va_mode & S_ISGID) != 0 &&
674 	      p->p_ucred->cr_gid != attr.va_gid))) {
675 		/*
676 		 * Mark the process as SUGID before we do
677 		 * anything that might block.
678 		 */
679 		p_sugid(p);
680 
681 		/* Make sure file descriptors 0..2 are in use. */
682 		if ((error = fdcheckstd(p)) != 0)
683 			goto exec_abort;
684 
685 		p->p_ucred = crcopy(cred);
686 #ifdef KTRACE
687 		/*
688 		 * If process is being ktraced, turn off - unless
689 		 * root set it.
690 		 */
691 		if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT))
692 			ktrderef(p);
693 #endif
694 		if (attr.va_mode & S_ISUID)
695 			p->p_ucred->cr_uid = attr.va_uid;
696 		if (attr.va_mode & S_ISGID)
697 			p->p_ucred->cr_gid = attr.va_gid;
698 	} else
699 		p->p_flag &= ~P_SUGID;
700 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
701 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
702 
703 #if defined(__HAVE_RAS)
704 	/*
705 	 * Remove all RASs from the address space.
706 	 */
707 	ras_purgeall(p);
708 #endif
709 
710 	doexechooks(p);
711 
712 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
713 
714 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
715 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
716 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
717 	vput(pack.ep_vp);
718 
719 	/* notify others that we exec'd */
720 	KNOTE(&p->p_klist, NOTE_EXEC);
721 
722 	/* setup new registers and do misc. setup. */
723 	(*pack.ep_es->es_emul->e_setregs)(p, &pack, (u_long) stack);
724 	if (pack.ep_es->es_setregs)
725 		(*pack.ep_es->es_setregs)(p, &pack, (u_long) stack);
726 
727 	if (p->p_flag & P_TRACED)
728 		psignal(p, SIGTRAP);
729 
730 	free(pack.ep_hdr, M_EXEC);
731 
732 	/*
733 	 * Call emulation specific exec hook. This can setup setup per-process
734 	 * p->p_emuldata or do any other per-process stuff an emulation needs.
735 	 *
736 	 * If we are executing process of different emulation than the
737 	 * original forked process, call e_proc_exit() of the old emulation
738 	 * first, then e_proc_exec() of new emulation. If the emulation is
739 	 * same, the exec hook code should deallocate any old emulation
740 	 * resources held previously by this process.
741 	 */
742 	if (p->p_emul && p->p_emul->e_proc_exit
743 	    && p->p_emul != pack.ep_es->es_emul)
744 		(*p->p_emul->e_proc_exit)(p);
745 
746 	/*
747 	 * Call exec hook. Emulation code may NOT store reference to anything
748 	 * from &pack.
749 	 */
750         if (pack.ep_es->es_emul->e_proc_exec)
751                 (*pack.ep_es->es_emul->e_proc_exec)(p, &pack);
752 
753 	/* update p_emul, the old value is no longer needed */
754 	p->p_emul = pack.ep_es->es_emul;
755 
756 	/* ...and the same for p_execsw */
757 	p->p_execsw = pack.ep_es;
758 
759 #ifdef __HAVE_SYSCALL_INTERN
760 	(*p->p_emul->e_syscall_intern)(p);
761 #endif
762 #ifdef KTRACE
763 	if (KTRPOINT(p, KTR_EMUL))
764 		ktremul(p);
765 #endif
766 
767 #ifdef LKM
768 	lockmgr(&exec_lock, LK_RELEASE, NULL);
769 #endif
770 	p->p_flag &= ~P_INEXEC;
771 
772 	if (p->p_flag & P_STOPEXEC) {
773 		int s;
774 
775 		sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
776 		SCHED_LOCK(s);
777 		p->p_stat = SSTOP;
778 		mi_switch(p, NULL);
779 		SCHED_ASSERT_UNLOCKED();
780 		splx(s);
781 	}
782 
783 	return (EJUSTRETURN);
784 
785  bad:
786 	p->p_flag &= ~P_INEXEC;
787 	/* free the vmspace-creation commands, and release their references */
788 	kill_vmcmds(&pack.ep_vmcmds);
789 	/* kill any opened file descriptor, if necessary */
790 	if (pack.ep_flags & EXEC_HASFD) {
791 		pack.ep_flags &= ~EXEC_HASFD;
792 		(void) fdrelease(p, pack.ep_fd);
793 	}
794 	/* close and put the exec'd file */
795 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
796 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
797 	vput(pack.ep_vp);
798 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
799 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
800 
801  freehdr:
802 	p->p_flag &= ~P_INEXEC;
803 #ifdef LKM
804 	lockmgr(&exec_lock, LK_RELEASE, NULL);
805 #endif
806 
807 	free(pack.ep_hdr, M_EXEC);
808 	return error;
809 
810  exec_abort:
811 	p->p_flag &= ~P_INEXEC;
812 #ifdef LKM
813 	lockmgr(&exec_lock, LK_RELEASE, NULL);
814 #endif
815 
816 	/*
817 	 * the old process doesn't exist anymore.  exit gracefully.
818 	 * get rid of the (new) address space we have created, if any, get rid
819 	 * of our namei data and vnode, and exit noting failure
820 	 */
821 	uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
822 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
823 	if (pack.ep_emul_arg)
824 		FREE(pack.ep_emul_arg, M_TEMP);
825 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
826 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
827 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
828 	vput(pack.ep_vp);
829 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
830 	free(pack.ep_hdr, M_EXEC);
831 	exit1(p, W_EXITCODE(error, SIGABRT));
832 
833 	/* NOTREACHED */
834 	return 0;
835 }
836 
837 
838 int
839 copyargs(struct proc *p, struct exec_package *pack, struct ps_strings *arginfo,
840     char **stackp, void *argp)
841 {
842 	char	**cpp, *dp, *sp;
843 	size_t	len;
844 	void	*nullp;
845 	long	argc, envc;
846 	int	error;
847 
848 	cpp = (char **)*stackp;
849 	nullp = NULL;
850 	argc = arginfo->ps_nargvstr;
851 	envc = arginfo->ps_nenvstr;
852 	if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0)
853 		return error;
854 
855 	dp = (char *) (cpp + argc + envc + 2 + pack->ep_es->es_arglen);
856 	sp = argp;
857 
858 	/* XXX don't copy them out, remap them! */
859 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
860 
861 	for (; --argc >= 0; sp += len, dp += len)
862 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
863 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
864 			return error;
865 
866 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
867 		return error;
868 
869 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
870 
871 	for (; --envc >= 0; sp += len, dp += len)
872 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
873 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
874 			return error;
875 
876 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
877 		return error;
878 
879 	*stackp = (char *)cpp;
880 	return 0;
881 }
882 
883 #ifdef LKM
884 /*
885  * Find an emulation of given name in list of emulations.
886  * Needs to be called with the exec_lock held.
887  */
888 const struct emul *
889 emul_search(const char *name)
890 {
891 	struct emul_entry *it;
892 
893 	LIST_FOREACH(it, &el_head, el_list) {
894 		if (strcmp(name, it->el_emul->e_name) == 0)
895 			return it->el_emul;
896 	}
897 
898 	return NULL;
899 }
900 
901 /*
902  * Add an emulation to list, if it's not there already.
903  */
904 int
905 emul_register(const struct emul *emul, int ro_entry)
906 {
907 	struct emul_entry	*ee;
908 	int			error;
909 
910 	error = 0;
911 	lockmgr(&exec_lock, LK_SHARED, NULL);
912 
913 	if (emul_search(emul->e_name)) {
914 		error = EEXIST;
915 		goto out;
916 	}
917 
918 	MALLOC(ee, struct emul_entry *, sizeof(struct emul_entry),
919 		M_EXEC, M_WAITOK);
920 	ee->el_emul = emul;
921 	ee->ro_entry = ro_entry;
922 	LIST_INSERT_HEAD(&el_head, ee, el_list);
923 
924  out:
925 	lockmgr(&exec_lock, LK_RELEASE, NULL);
926 	return error;
927 }
928 
929 /*
930  * Remove emulation with name 'name' from list of supported emulations.
931  */
932 int
933 emul_unregister(const char *name)
934 {
935 	const struct proclist_desc *pd;
936 	struct emul_entry	*it;
937 	int			i, error;
938 	struct proc		*ptmp;
939 
940 	error = 0;
941 	lockmgr(&exec_lock, LK_SHARED, NULL);
942 
943 	LIST_FOREACH(it, &el_head, el_list) {
944 		if (strcmp(it->el_emul->e_name, name) == 0)
945 			break;
946 	}
947 
948 	if (!it) {
949 		error = ENOENT;
950 		goto out;
951 	}
952 
953 	if (it->ro_entry) {
954 		error = EBUSY;
955 		goto out;
956 	}
957 
958 	/* test if any execw[] entry is still using this */
959 	for(i=0; i < nexecs; i++) {
960 		if (execsw[i]->es_emul == it->el_emul) {
961 			error = EBUSY;
962 			goto out;
963 		}
964 	}
965 
966 	/*
967 	 * Test if any process is running under this emulation - since
968 	 * emul_unregister() is running quite sendomly, it's better
969 	 * to do expensive check here than to use any locking.
970 	 */
971 	proclist_lock_read();
972 	for (pd = proclists; pd->pd_list != NULL && !error; pd++) {
973 		LIST_FOREACH(ptmp, pd->pd_list, p_list) {
974 			if (ptmp->p_emul == it->el_emul) {
975 				error = EBUSY;
976 				break;
977 			}
978 		}
979 	}
980 	proclist_unlock_read();
981 
982 	if (error)
983 		goto out;
984 
985 
986 	/* entry is not used, remove it */
987 	LIST_REMOVE(it, el_list);
988 	FREE(it, M_EXEC);
989 
990  out:
991 	lockmgr(&exec_lock, LK_RELEASE, NULL);
992 	return error;
993 }
994 
995 /*
996  * Add execsw[] entry.
997  */
998 int
999 exec_add(struct execsw *esp, const char *e_name)
1000 {
1001 	struct exec_entry	*it;
1002 	int			error;
1003 
1004 	error = 0;
1005 	lockmgr(&exec_lock, LK_EXCLUSIVE, NULL);
1006 
1007 	if (!esp->es_emul) {
1008 		esp->es_emul = emul_search(e_name);
1009 		if (!esp->es_emul) {
1010 			error = ENOENT;
1011 			goto out;
1012 		}
1013 	}
1014 
1015 	LIST_FOREACH(it, &ex_head, ex_list) {
1016 		/* assume tuple (makecmds, probe_func, emulation) is unique */
1017 		if (it->es->es_check == esp->es_check
1018 		    && it->es->u.elf_probe_func == esp->u.elf_probe_func
1019 		    && it->es->es_emul == esp->es_emul) {
1020 			error = EEXIST;
1021 			goto out;
1022 		}
1023 	}
1024 
1025 	/* if we got here, the entry doesn't exist yet */
1026 	MALLOC(it, struct exec_entry *, sizeof(struct exec_entry),
1027 		M_EXEC, M_WAITOK);
1028 	it->es = esp;
1029 	LIST_INSERT_HEAD(&ex_head, it, ex_list);
1030 
1031 	/* update execsw[] */
1032 	exec_init(0);
1033 
1034  out:
1035 	lockmgr(&exec_lock, LK_RELEASE, NULL);
1036 	return error;
1037 }
1038 
1039 /*
1040  * Remove execsw[] entry.
1041  */
1042 int
1043 exec_remove(const struct execsw *esp)
1044 {
1045 	struct exec_entry	*it;
1046 	int			error;
1047 
1048 	error = 0;
1049 	lockmgr(&exec_lock, LK_EXCLUSIVE, NULL);
1050 
1051 	LIST_FOREACH(it, &ex_head, ex_list) {
1052 		/* assume tuple (makecmds, probe_func, emulation) is unique */
1053 		if (it->es->es_check == esp->es_check
1054 		    && it->es->u.elf_probe_func == esp->u.elf_probe_func
1055 		    && it->es->es_emul == esp->es_emul)
1056 			break;
1057 	}
1058 	if (!it) {
1059 		error = ENOENT;
1060 		goto out;
1061 	}
1062 
1063 	/* remove item from list and free resources */
1064 	LIST_REMOVE(it, ex_list);
1065 	FREE(it, M_EXEC);
1066 
1067 	/* update execsw[] */
1068 	exec_init(0);
1069 
1070  out:
1071 	lockmgr(&exec_lock, LK_RELEASE, NULL);
1072 	return error;
1073 }
1074 
1075 static void
1076 link_es(struct execsw_entry **listp, const struct execsw *esp)
1077 {
1078 	struct execsw_entry *et, *e1;
1079 
1080 	MALLOC(et, struct execsw_entry *, sizeof(struct execsw_entry),
1081 			M_TEMP, M_WAITOK);
1082 	et->next = NULL;
1083 	et->es = esp;
1084 	if (*listp == NULL) {
1085 		*listp = et;
1086 		return;
1087 	}
1088 
1089 	switch(et->es->es_prio) {
1090 	case EXECSW_PRIO_FIRST:
1091 		/* put new entry as the first */
1092 		et->next = *listp;
1093 		*listp = et;
1094 		break;
1095 	case EXECSW_PRIO_ANY:
1096 		/* put new entry after all *_FIRST and *_ANY entries */
1097 		for(e1 = *listp; e1->next
1098 			&& e1->next->es->es_prio != EXECSW_PRIO_LAST;
1099 			e1 = e1->next);
1100 		et->next = e1->next;
1101 		e1->next = et;
1102 		break;
1103 	case EXECSW_PRIO_LAST:
1104 		/* put new entry as the last one */
1105 		for(e1 = *listp; e1->next; e1 = e1->next);
1106 		e1->next = et;
1107 		break;
1108 	default:
1109 #ifdef DIAGNOSTIC
1110 		panic("execw[] entry with unknown priority %d found",
1111 			et->es->es_prio);
1112 #endif
1113 		break;
1114 	}
1115 }
1116 
1117 /*
1118  * Initialize exec structures. If init_boot is true, also does necessary
1119  * one-time initialization (it's called from main() that way).
1120  * Once system is multiuser, this should be called with exec_lock held,
1121  * i.e. via exec_{add|remove}().
1122  */
1123 int
1124 exec_init(int init_boot)
1125 {
1126 	const struct execsw	**new_es, * const *old_es;
1127 	struct execsw_entry	*list, *e1;
1128 	struct exec_entry	*e2;
1129 	int			i, es_sz;
1130 
1131 	if (init_boot) {
1132 		/* do one-time initializations */
1133 		lockinit(&exec_lock, PWAIT, "execlck", 0, 0);
1134 
1135 		/* register compiled-in emulations */
1136 		for(i=0; i < nexecs_builtin; i++) {
1137 			if (execsw_builtin[i].es_emul)
1138 				emul_register(execsw_builtin[i].es_emul, 1);
1139 		}
1140 #ifdef DIAGNOSTIC
1141 		if (i == 0)
1142 			panic("no emulations found in execsw_builtin[]");
1143 #endif
1144 	}
1145 
1146 	/*
1147 	 * Build execsw[] array from builtin entries and entries added
1148 	 * at runtime.
1149 	 */
1150 	list = NULL;
1151 	for(i=0; i < nexecs_builtin; i++)
1152 		link_es(&list, &execsw_builtin[i]);
1153 
1154 	/* Add dynamically loaded entries */
1155 	es_sz = nexecs_builtin;
1156 	LIST_FOREACH(e2, &ex_head, ex_list) {
1157 		link_es(&list, e2->es);
1158 		es_sz++;
1159 	}
1160 
1161 	/*
1162 	 * Now that we have sorted all execw entries, create new execsw[]
1163 	 * and free no longer needed memory in the process.
1164 	 */
1165 	new_es = malloc(es_sz * sizeof(struct execsw *), M_EXEC, M_WAITOK);
1166 	for(i=0; list; i++) {
1167 		new_es[i] = list->es;
1168 		e1 = list->next;
1169 		FREE(list, M_TEMP);
1170 		list = e1;
1171 	}
1172 
1173 	/*
1174 	 * New execsw[] array built, now replace old execsw[] and free
1175 	 * used memory.
1176 	 */
1177 	old_es = execsw;
1178 	execsw = new_es;
1179 	nexecs = es_sz;
1180 	if (old_es)
1181 		free((void *)old_es, M_EXEC);
1182 
1183 	/*
1184 	 * Figure out the maximum size of an exec header.
1185 	 */
1186 	exec_maxhdrsz = 0;
1187 	for (i = 0; i < nexecs; i++) {
1188 		if (execsw[i]->es_hdrsz > exec_maxhdrsz)
1189 			exec_maxhdrsz = execsw[i]->es_hdrsz;
1190 	}
1191 
1192 	return 0;
1193 }
1194 #endif
1195 
1196 #ifndef LKM
1197 /*
1198  * Simplified exec_init() for kernels without LKMs. Only initialize
1199  * exec_maxhdrsz and execsw[].
1200  */
1201 int
1202 exec_init(int init_boot)
1203 {
1204 	int i;
1205 
1206 #ifdef DIAGNOSTIC
1207 	if (!init_boot)
1208 		panic("exec_init(): called with init_boot == 0");
1209 #endif
1210 
1211 	/* do one-time initializations */
1212 	nexecs = nexecs_builtin;
1213 	execsw = malloc(nexecs*sizeof(struct execsw *), M_EXEC, M_WAITOK);
1214 
1215 	/*
1216 	 * Fill in execsw[] and figure out the maximum size of an exec header.
1217 	 */
1218 	exec_maxhdrsz = 0;
1219 	for(i=0; i < nexecs; i++) {
1220 		execsw[i] = &execsw_builtin[i];
1221 		if (execsw_builtin[i].es_hdrsz > exec_maxhdrsz)
1222 			exec_maxhdrsz = execsw_builtin[i].es_hdrsz;
1223 	}
1224 
1225 	return 0;
1226 
1227 }
1228 #endif /* !LKM */
1229