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