xref: /freebsd/sys/kern/kern_exec.c (revision 2b743a9e9ddc6736208dc8ca1ce06ce64ad20a19)
1 /*-
2  * Copyright (c) 1993, David Greenman
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_hwpmc_hooks.h"
31 #include "opt_ktrace.h"
32 #include "opt_mac.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/eventhandler.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/sysproto.h>
40 #include <sys/signalvar.h>
41 #include <sys/kernel.h>
42 #include <sys/mount.h>
43 #include <sys/filedesc.h>
44 #include <sys/fcntl.h>
45 #include <sys/acct.h>
46 #include <sys/exec.h>
47 #include <sys/imgact.h>
48 #include <sys/imgact_elf.h>
49 #include <sys/wait.h>
50 #include <sys/malloc.h>
51 #include <sys/priv.h>
52 #include <sys/proc.h>
53 #include <sys/pioctl.h>
54 #include <sys/namei.h>
55 #include <sys/resourcevar.h>
56 #include <sys/sf_buf.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysent.h>
59 #include <sys/shm.h>
60 #include <sys/sysctl.h>
61 #include <sys/vnode.h>
62 #ifdef KTRACE
63 #include <sys/ktrace.h>
64 #endif
65 
66 #include <vm/vm.h>
67 #include <vm/vm_param.h>
68 #include <vm/pmap.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_extern.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_pager.h>
75 
76 #ifdef	HWPMC_HOOKS
77 #include <sys/pmckern.h>
78 #endif
79 
80 #include <machine/reg.h>
81 
82 #include <security/audit/audit.h>
83 #include <security/mac/mac_framework.h>
84 
85 MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
86 
87 static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS);
88 static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS);
89 static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS);
90 static int do_execve(struct thread *td, struct image_args *args,
91     struct mac *mac_p);
92 static void exec_free_args(struct image_args *);
93 
94 /* XXX This should be vm_size_t. */
95 SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD,
96     NULL, 0, sysctl_kern_ps_strings, "LU", "");
97 
98 /* XXX This should be vm_size_t. */
99 SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD,
100     NULL, 0, sysctl_kern_usrstack, "LU", "");
101 
102 SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD,
103     NULL, 0, sysctl_kern_stackprot, "I", "");
104 
105 u_long ps_arg_cache_limit = PAGE_SIZE / 16;
106 SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
107     &ps_arg_cache_limit, 0, "");
108 
109 static int
110 sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS)
111 {
112 	struct proc *p;
113 	int error;
114 
115 	p = curproc;
116 #ifdef SCTL_MASK32
117 	if (req->flags & SCTL_MASK32) {
118 		unsigned int val;
119 		val = (unsigned int)p->p_sysent->sv_psstrings;
120 		error = SYSCTL_OUT(req, &val, sizeof(val));
121 	} else
122 #endif
123 		error = SYSCTL_OUT(req, &p->p_sysent->sv_psstrings,
124 		   sizeof(p->p_sysent->sv_psstrings));
125 	return error;
126 }
127 
128 static int
129 sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS)
130 {
131 	struct proc *p;
132 	int error;
133 
134 	p = curproc;
135 #ifdef SCTL_MASK32
136 	if (req->flags & SCTL_MASK32) {
137 		unsigned int val;
138 		val = (unsigned int)p->p_sysent->sv_usrstack;
139 		error = SYSCTL_OUT(req, &val, sizeof(val));
140 	} else
141 #endif
142 		error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack,
143 		    sizeof(p->p_sysent->sv_usrstack));
144 	return error;
145 }
146 
147 static int
148 sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS)
149 {
150 	struct proc *p;
151 
152 	p = curproc;
153 	return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot,
154 	    sizeof(p->p_sysent->sv_stackprot)));
155 }
156 
157 /*
158  * Each of the items is a pointer to a `const struct execsw', hence the
159  * double pointer here.
160  */
161 static const struct execsw **execsw;
162 
163 #ifndef _SYS_SYSPROTO_H_
164 struct execve_args {
165 	char    *fname;
166 	char    **argv;
167 	char    **envv;
168 };
169 #endif
170 
171 /*
172  * MPSAFE
173  */
174 int
175 execve(td, uap)
176 	struct thread *td;
177 	struct execve_args /* {
178 		char *fname;
179 		char **argv;
180 		char **envv;
181 	} */ *uap;
182 {
183 	int error;
184 	struct image_args args;
185 
186 	error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
187 	    uap->argv, uap->envv);
188 	if (error == 0)
189 		error = kern_execve(td, &args, NULL);
190 	return (error);
191 }
192 
193 #ifndef _SYS_SYSPROTO_H_
194 struct __mac_execve_args {
195 	char	*fname;
196 	char	**argv;
197 	char	**envv;
198 	struct mac	*mac_p;
199 };
200 #endif
201 
202 /*
203  * MPSAFE
204  */
205 int
206 __mac_execve(td, uap)
207 	struct thread *td;
208 	struct __mac_execve_args /* {
209 		char *fname;
210 		char **argv;
211 		char **envv;
212 		struct mac *mac_p;
213 	} */ *uap;
214 {
215 #ifdef MAC
216 	int error;
217 	struct image_args args;
218 
219 	error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
220 	    uap->argv, uap->envv);
221 	if (error == 0)
222 		error = kern_execve(td, &args, uap->mac_p);
223 	return (error);
224 #else
225 	return (ENOSYS);
226 #endif
227 }
228 
229 /*
230  * XXX: kern_execve has the astonishing property of not always
231  * returning to the caller.  If sufficiently bad things happen during
232  * the call to do_execve(), it can end up calling exit1(); as a result,
233  * callers must avoid doing anything which they might need to undo
234  * (e.g., allocating memory).
235  */
236 int
237 kern_execve(td, args, mac_p)
238 	struct thread *td;
239 	struct image_args *args;
240 	struct mac *mac_p;
241 {
242 	struct proc *p = td->td_proc;
243 	int error;
244 
245 	AUDIT_ARG(argv, args->begin_argv, args->argc,
246 	    args->begin_envv - args->begin_argv);
247 	AUDIT_ARG(envv, args->begin_envv, args->envc,
248 	    args->endp - args->begin_envv);
249 	if (p->p_flag & P_HADTHREADS) {
250 		PROC_LOCK(p);
251 		if (thread_single(SINGLE_BOUNDARY)) {
252 			PROC_UNLOCK(p);
253 	       		exec_free_args(args);
254 			return (ERESTART);	/* Try again later. */
255 		}
256 		PROC_UNLOCK(p);
257 	}
258 
259 	error = do_execve(td, args, mac_p);
260 
261 	if (p->p_flag & P_HADTHREADS) {
262 		PROC_LOCK(p);
263 		/*
264 		 * If success, we upgrade to SINGLE_EXIT state to
265 		 * force other threads to suicide.
266 		 */
267 		if (error == 0)
268 			thread_single(SINGLE_EXIT);
269 		else
270 			thread_single_end();
271 		PROC_UNLOCK(p);
272 	}
273 
274 	return (error);
275 }
276 
277 /*
278  * In-kernel implementation of execve().  All arguments are assumed to be
279  * userspace pointers from the passed thread.
280  *
281  * MPSAFE
282  */
283 static int
284 do_execve(td, args, mac_p)
285 	struct thread *td;
286 	struct image_args *args;
287 	struct mac *mac_p;
288 {
289 	struct proc *p = td->td_proc;
290 	struct nameidata nd, *ndp;
291 	struct ucred *newcred = NULL, *oldcred;
292 	struct uidinfo *euip;
293 	register_t *stack_base;
294 	int error, len, i;
295 	struct image_params image_params, *imgp;
296 	struct vattr attr;
297 	int (*img_first)(struct image_params *);
298 	struct pargs *oldargs = NULL, *newargs = NULL;
299 	struct sigacts *oldsigacts, *newsigacts;
300 #ifdef KTRACE
301 	struct vnode *tracevp = NULL;
302 	struct ucred *tracecred = NULL;
303 #endif
304 	struct vnode *textvp = NULL;
305 	int credential_changing;
306 	int vfslocked;
307 	int textset;
308 #ifdef MAC
309 	struct label *interplabel = NULL;
310 	int will_transition;
311 #endif
312 #ifdef HWPMC_HOOKS
313 	struct pmckern_procexec pe;
314 #endif
315 
316 	vfslocked = 0;
317 	imgp = &image_params;
318 
319 	/*
320 	 * Lock the process and set the P_INEXEC flag to indicate that
321 	 * it should be left alone until we're done here.  This is
322 	 * necessary to avoid race conditions - e.g. in ptrace() -
323 	 * that might allow a local user to illicitly obtain elevated
324 	 * privileges.
325 	 */
326 	PROC_LOCK(p);
327 	KASSERT((p->p_flag & P_INEXEC) == 0,
328 	    ("%s(): process already has P_INEXEC flag", __func__));
329 	p->p_flag |= P_INEXEC;
330 	PROC_UNLOCK(p);
331 
332 	/*
333 	 * Initialize part of the common data
334 	 */
335 	imgp->proc = p;
336 	imgp->execlabel = NULL;
337 	imgp->attr = &attr;
338 	imgp->entry_addr = 0;
339 	imgp->vmspace_destroyed = 0;
340 	imgp->interpreted = 0;
341 	imgp->interpreter_name = args->buf + PATH_MAX + ARG_MAX;
342 	imgp->auxargs = NULL;
343 	imgp->vp = NULL;
344 	imgp->object = NULL;
345 	imgp->firstpage = NULL;
346 	imgp->ps_strings = 0;
347 	imgp->auxarg_size = 0;
348 	imgp->args = args;
349 
350 #ifdef MAC
351 	error = mac_execve_enter(imgp, mac_p);
352 	if (error)
353 		goto exec_fail;
354 #endif
355 
356 	imgp->image_header = NULL;
357 
358 	/*
359 	 * Translate the file name. namei() returns a vnode pointer
360 	 *	in ni_vp amoung other things.
361 	 *
362 	 * XXXAUDIT: It would be desirable to also audit the name of the
363 	 * interpreter if this is an interpreted binary.
364 	 */
365 	ndp = &nd;
366 	NDINIT(ndp, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME | MPSAFE |
367 	    AUDITVNODE1, UIO_SYSSPACE, args->fname, td);
368 
369 interpret:
370 	error = namei(ndp);
371 	if (error)
372 		goto exec_fail;
373 
374 	vfslocked = NDHASGIANT(ndp);
375 	imgp->vp = ndp->ni_vp;
376 
377 	/*
378 	 * Check file permissions (also 'opens' file)
379 	 */
380 	error = exec_check_permissions(imgp);
381 	if (error)
382 		goto exec_fail_dealloc;
383 
384 	imgp->object = imgp->vp->v_object;
385 	if (imgp->object != NULL)
386 		vm_object_reference(imgp->object);
387 
388 	/*
389 	 * Set VV_TEXT now so no one can write to the executable while we're
390 	 * activating it.
391 	 *
392 	 * Remember if this was set before and unset it in case this is not
393 	 * actually an executable image.
394 	 */
395 	textset = imgp->vp->v_vflag & VV_TEXT;
396 	imgp->vp->v_vflag |= VV_TEXT;
397 
398 	error = exec_map_first_page(imgp);
399 	if (error)
400 		goto exec_fail_dealloc;
401 
402 	/*
403 	 *	If the current process has a special image activator it
404 	 *	wants to try first, call it.   For example, emulating shell
405 	 *	scripts differently.
406 	 */
407 	error = -1;
408 	if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
409 		error = img_first(imgp);
410 
411 	/*
412 	 *	Loop through the list of image activators, calling each one.
413 	 *	An activator returns -1 if there is no match, 0 on success,
414 	 *	and an error otherwise.
415 	 */
416 	for (i = 0; error == -1 && execsw[i]; ++i) {
417 		if (execsw[i]->ex_imgact == NULL ||
418 		    execsw[i]->ex_imgact == img_first) {
419 			continue;
420 		}
421 		error = (*execsw[i]->ex_imgact)(imgp);
422 	}
423 
424 	if (error) {
425 		if (error == -1) {
426 			if (textset == 0)
427 				imgp->vp->v_vflag &= ~VV_TEXT;
428 			error = ENOEXEC;
429 		}
430 		goto exec_fail_dealloc;
431 	}
432 
433 	/*
434 	 * Special interpreter operation, cleanup and loop up to try to
435 	 * activate the interpreter.
436 	 */
437 	if (imgp->interpreted) {
438 		exec_unmap_first_page(imgp);
439 		/*
440 		 * VV_TEXT needs to be unset for scripts.  There is a short
441 		 * period before we determine that something is a script where
442 		 * VV_TEXT will be set. The vnode lock is held over this
443 		 * entire period so nothing should illegitimately be blocked.
444 		 */
445 		imgp->vp->v_vflag &= ~VV_TEXT;
446 		/* free name buffer and old vnode */
447 		NDFREE(ndp, NDF_ONLY_PNBUF);
448 #ifdef MAC
449 		interplabel = mac_vnode_label_alloc();
450 		mac_copy_vnode_label(ndp->ni_vp->v_label, interplabel);
451 #endif
452 		vput(ndp->ni_vp);
453 		vm_object_deallocate(imgp->object);
454 		imgp->object = NULL;
455 		VFS_UNLOCK_GIANT(vfslocked);
456 		vfslocked = 0;
457 		/* set new name to that of the interpreter */
458 		NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME | MPSAFE,
459 		    UIO_SYSSPACE, imgp->interpreter_name, td);
460 		goto interpret;
461 	}
462 
463 	/*
464 	 * Copy out strings (args and env) and initialize stack base
465 	 */
466 	if (p->p_sysent->sv_copyout_strings)
467 		stack_base = (*p->p_sysent->sv_copyout_strings)(imgp);
468 	else
469 		stack_base = exec_copyout_strings(imgp);
470 
471 	/*
472 	 * If custom stack fixup routine present for this process
473 	 * let it do the stack setup.
474 	 * Else stuff argument count as first item on stack
475 	 */
476 	if (p->p_sysent->sv_fixup != NULL)
477 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
478 	else
479 		suword(--stack_base, imgp->args->argc);
480 
481 	/*
482 	 * For security and other reasons, the file descriptor table cannot
483 	 * be shared after an exec.
484 	 */
485 	fdunshare(p, td);
486 
487 	/*
488 	 * Malloc things before we need locks.
489 	 */
490 	newcred = crget();
491 	euip = uifind(attr.va_uid);
492 	i = imgp->args->begin_envv - imgp->args->begin_argv;
493 	/* Cache arguments if they fit inside our allowance */
494 	if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
495 		newargs = pargs_alloc(i);
496 		bcopy(imgp->args->begin_argv, newargs->ar_args, i);
497 	}
498 
499 	/* close files on exec */
500 	VOP_UNLOCK(imgp->vp, 0, td);
501 	fdcloseexec(td);
502 	vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
503 
504 	/* Get a reference to the vnode prior to locking the proc */
505 	VREF(ndp->ni_vp);
506 
507 	/*
508 	 * For security and other reasons, signal handlers cannot
509 	 * be shared after an exec. The new process gets a copy of the old
510 	 * handlers. In execsigs(), the new process will have its signals
511 	 * reset.
512 	 */
513 	PROC_LOCK(p);
514 	if (sigacts_shared(p->p_sigacts)) {
515 		oldsigacts = p->p_sigacts;
516 		PROC_UNLOCK(p);
517 		newsigacts = sigacts_alloc();
518 		sigacts_copy(newsigacts, oldsigacts);
519 		PROC_LOCK(p);
520 		p->p_sigacts = newsigacts;
521 	} else
522 		oldsigacts = NULL;
523 
524 	/* Stop profiling */
525 	stopprofclock(p);
526 
527 	/* reset caught signals */
528 	execsigs(p);
529 
530 	/* name this process - nameiexec(p, ndp) */
531 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
532 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
533 	p->p_comm[len] = 0;
534 
535 	/*
536 	 * mark as execed, wakeup the process that vforked (if any) and tell
537 	 * it that it now has its own resources back
538 	 */
539 	p->p_flag |= P_EXEC;
540 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
541 		p->p_flag &= ~P_PPWAIT;
542 		wakeup(p->p_pptr);
543 	}
544 
545 	/*
546 	 * Implement image setuid/setgid.
547 	 *
548 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
549 	 * the process is being traced.
550 	 *
551 	 * XXXMAC: For the time being, use NOSUID to also prohibit
552 	 * transitions on the file system.
553 	 */
554 	oldcred = p->p_ucred;
555 	credential_changing = 0;
556 	credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid !=
557 	    attr.va_uid;
558 	credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid !=
559 	    attr.va_gid;
560 #ifdef MAC
561 	will_transition = mac_execve_will_transition(oldcred, imgp->vp,
562 	    interplabel, imgp);
563 	credential_changing |= will_transition;
564 #endif
565 
566 	if (credential_changing &&
567 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
568 	    (p->p_flag & P_TRACED) == 0) {
569 		/*
570 		 * Turn off syscall tracing for set-id programs, except for
571 		 * root.  Record any set-id flags first to make sure that
572 		 * we do not regain any tracing during a possible block.
573 		 */
574 		setsugid(p);
575 
576 #ifdef KTRACE
577 		if (p->p_tracevp != NULL &&
578 		    priv_check_cred(oldcred, PRIV_DEBUG_DIFFCRED,
579 		    SUSER_ALLOWJAIL)) {
580 			mtx_lock(&ktrace_mtx);
581 			p->p_traceflag = 0;
582 			tracevp = p->p_tracevp;
583 			p->p_tracevp = NULL;
584 			tracecred = p->p_tracecred;
585 			p->p_tracecred = NULL;
586 			mtx_unlock(&ktrace_mtx);
587 		}
588 #endif
589 		/*
590 		 * Close any file descriptors 0..2 that reference procfs,
591 		 * then make sure file descriptors 0..2 are in use.
592 		 *
593 		 * setugidsafety() may call closef() and then pfind()
594 		 * which may grab the process lock.
595 		 * fdcheckstd() may call falloc() which may block to
596 		 * allocate memory, so temporarily drop the process lock.
597 		 */
598 		PROC_UNLOCK(p);
599 		setugidsafety(td);
600 		VOP_UNLOCK(imgp->vp, 0, td);
601 		error = fdcheckstd(td);
602 		vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
603 		if (error != 0)
604 			goto done1;
605 		PROC_LOCK(p);
606 		/*
607 		 * Set the new credentials.
608 		 */
609 		crcopy(newcred, oldcred);
610 		if (attr.va_mode & VSUID)
611 			change_euid(newcred, euip);
612 		if (attr.va_mode & VSGID)
613 			change_egid(newcred, attr.va_gid);
614 #ifdef MAC
615 		if (will_transition) {
616 			mac_execve_transition(oldcred, newcred, imgp->vp,
617 			    interplabel, imgp);
618 		}
619 #endif
620 		/*
621 		 * Implement correct POSIX saved-id behavior.
622 		 *
623 		 * XXXMAC: Note that the current logic will save the
624 		 * uid and gid if a MAC domain transition occurs, even
625 		 * though maybe it shouldn't.
626 		 */
627 		change_svuid(newcred, newcred->cr_uid);
628 		change_svgid(newcred, newcred->cr_gid);
629 		p->p_ucred = newcred;
630 		newcred = NULL;
631 	} else {
632 		if (oldcred->cr_uid == oldcred->cr_ruid &&
633 		    oldcred->cr_gid == oldcred->cr_rgid)
634 			p->p_flag &= ~P_SUGID;
635 		/*
636 		 * Implement correct POSIX saved-id behavior.
637 		 *
638 		 * XXX: It's not clear that the existing behavior is
639 		 * POSIX-compliant.  A number of sources indicate that the
640 		 * saved uid/gid should only be updated if the new ruid is
641 		 * not equal to the old ruid, or the new euid is not equal
642 		 * to the old euid and the new euid is not equal to the old
643 		 * ruid.  The FreeBSD code always updates the saved uid/gid.
644 		 * Also, this code uses the new (replaced) euid and egid as
645 		 * the source, which may or may not be the right ones to use.
646 		 */
647 		if (oldcred->cr_svuid != oldcred->cr_uid ||
648 		    oldcred->cr_svgid != oldcred->cr_gid) {
649 			crcopy(newcred, oldcred);
650 			change_svuid(newcred, newcred->cr_uid);
651 			change_svgid(newcred, newcred->cr_gid);
652 			p->p_ucred = newcred;
653 			newcred = NULL;
654 		}
655 	}
656 
657 	/*
658 	 * Store the vp for use in procfs.  This vnode was referenced prior
659 	 * to locking the proc lock.
660 	 */
661 	textvp = p->p_textvp;
662 	p->p_textvp = ndp->ni_vp;
663 
664 	/*
665 	 * Notify others that we exec'd, and clear the P_INEXEC flag
666 	 * as we're now a bona fide freshly-execed process.
667 	 */
668 	KNOTE_LOCKED(&p->p_klist, NOTE_EXEC);
669 	p->p_flag &= ~P_INEXEC;
670 
671 	/*
672 	 * If tracing the process, trap to debugger so breakpoints
673 	 * can be set before the program executes.
674 	 * Use tdsignal to deliver signal to current thread, use
675 	 * psignal may cause the signal to be delivered to wrong thread
676 	 * because that thread will exit, remember we are going to enter
677 	 * single thread mode.
678 	 */
679 	if (p->p_flag & P_TRACED)
680 		tdsignal(p, td, SIGTRAP, NULL);
681 
682 	/* clear "fork but no exec" flag, as we _are_ execing */
683 	p->p_acflag &= ~AFORK;
684 
685 	/*
686 	 * Free any previous argument cache and replace it with
687 	 * the new argument cache, if any.
688 	 */
689 	oldargs = p->p_args;
690 	p->p_args = newargs;
691 	newargs = NULL;
692 
693 #ifdef	HWPMC_HOOKS
694 	/*
695 	 * Check if system-wide sampling is in effect or if the
696 	 * current process is using PMCs.  If so, do exec() time
697 	 * processing.  This processing needs to happen AFTER the
698 	 * P_INEXEC flag is cleared.
699 	 *
700 	 * The proc lock needs to be released before taking the PMC
701 	 * SX.
702 	 */
703 	if (PMC_SYSTEM_SAMPLING_ACTIVE() || PMC_PROC_IS_USING_PMCS(p)) {
704 		PROC_UNLOCK(p);
705 		pe.pm_credentialschanged = credential_changing;
706 		pe.pm_entryaddr = imgp->entry_addr;
707 
708 		PMC_CALL_HOOK_X(td, PMC_FN_PROCESS_EXEC, (void *) &pe);
709 	} else
710 		PROC_UNLOCK(p);
711 #else  /* !HWPMC_HOOKS */
712 	PROC_UNLOCK(p);
713 #endif
714 
715 	/* Set values passed into the program in registers. */
716 	if (p->p_sysent->sv_setregs)
717 		(*p->p_sysent->sv_setregs)(td, imgp->entry_addr,
718 		    (u_long)(uintptr_t)stack_base, imgp->ps_strings);
719 	else
720 		exec_setregs(td, imgp->entry_addr,
721 		    (u_long)(uintptr_t)stack_base, imgp->ps_strings);
722 
723 	vfs_mark_atime(imgp->vp, td);
724 
725 done1:
726 	/*
727 	 * Free any resources malloc'd earlier that we didn't use.
728 	 */
729 	uifree(euip);
730 	if (newcred == NULL)
731 		crfree(oldcred);
732 	else
733 		crfree(newcred);
734 	VOP_UNLOCK(imgp->vp, 0, td);
735 	/*
736 	 * Handle deferred decrement of ref counts.
737 	 */
738 	if (textvp != NULL) {
739 		int tvfslocked;
740 
741 		tvfslocked = VFS_LOCK_GIANT(textvp->v_mount);
742 		vrele(textvp);
743 		VFS_UNLOCK_GIANT(tvfslocked);
744 	}
745 	if (ndp->ni_vp && error != 0)
746 		vrele(ndp->ni_vp);
747 #ifdef KTRACE
748 	if (tracevp != NULL)
749 		vrele(tracevp);
750 	if (tracecred != NULL)
751 		crfree(tracecred);
752 #endif
753 	vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
754 	if (oldargs != NULL)
755 		pargs_drop(oldargs);
756 	if (newargs != NULL)
757 		pargs_drop(newargs);
758 	if (oldsigacts != NULL)
759 		sigacts_free(oldsigacts);
760 
761 exec_fail_dealloc:
762 
763 	/*
764 	 * free various allocated resources
765 	 */
766 	if (imgp->firstpage != NULL)
767 		exec_unmap_first_page(imgp);
768 
769 	if (imgp->vp != NULL) {
770 		NDFREE(ndp, NDF_ONLY_PNBUF);
771 		vput(imgp->vp);
772 	}
773 
774 	if (imgp->object != NULL)
775 		vm_object_deallocate(imgp->object);
776 
777 	if (error == 0) {
778 		/*
779 		 * Stop the process here if its stop event mask has
780 		 * the S_EXEC bit set.
781 		 */
782 		STOPEVENT(p, S_EXEC, 0);
783 		goto done2;
784 	}
785 
786 exec_fail:
787 	/* we're done here, clear P_INEXEC */
788 	PROC_LOCK(p);
789 	p->p_flag &= ~P_INEXEC;
790 	PROC_UNLOCK(p);
791 
792 done2:
793 #ifdef MAC
794 	mac_execve_exit(imgp);
795 	if (interplabel != NULL)
796 		mac_vnode_label_free(interplabel);
797 #endif
798 	VFS_UNLOCK_GIANT(vfslocked);
799 	exec_free_args(args);
800 
801 	if (error && imgp->vmspace_destroyed) {
802 		/* sorry, no more process anymore. exit gracefully */
803 		exit1(td, W_EXITCODE(0, SIGABRT));
804 		/* NOT REACHED */
805 	}
806 	return (error);
807 }
808 
809 int
810 exec_map_first_page(imgp)
811 	struct image_params *imgp;
812 {
813 	int rv, i;
814 	int initial_pagein;
815 	vm_page_t ma[VM_INITIAL_PAGEIN];
816 	vm_object_t object;
817 
818 	if (imgp->firstpage != NULL)
819 		exec_unmap_first_page(imgp);
820 
821 	object = imgp->vp->v_object;
822 	if (object == NULL)
823 		return (EACCES);
824 	VM_OBJECT_LOCK(object);
825 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
826 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
827 		initial_pagein = VM_INITIAL_PAGEIN;
828 		if (initial_pagein > object->size)
829 			initial_pagein = object->size;
830 		for (i = 1; i < initial_pagein; i++) {
831 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
832 				if (ma[i]->valid)
833 					break;
834 				if ((ma[i]->oflags & VPO_BUSY) || ma[i]->busy)
835 					break;
836 				vm_page_busy(ma[i]);
837 			} else {
838 				ma[i] = vm_page_alloc(object, i,
839 				    VM_ALLOC_NORMAL);
840 				if (ma[i] == NULL)
841 					break;
842 			}
843 		}
844 		initial_pagein = i;
845 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
846 		ma[0] = vm_page_lookup(object, 0);
847 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) ||
848 		    (ma[0]->valid == 0)) {
849 			if (ma[0]) {
850 				vm_page_lock_queues();
851 				vm_page_free(ma[0]);
852 				vm_page_unlock_queues();
853 			}
854 			VM_OBJECT_UNLOCK(object);
855 			return (EIO);
856 		}
857 	}
858 	vm_page_lock_queues();
859 	vm_page_hold(ma[0]);
860 	vm_page_unlock_queues();
861 	vm_page_wakeup(ma[0]);
862 	VM_OBJECT_UNLOCK(object);
863 
864 	imgp->firstpage = sf_buf_alloc(ma[0], 0);
865 	imgp->image_header = (char *)sf_buf_kva(imgp->firstpage);
866 
867 	return (0);
868 }
869 
870 void
871 exec_unmap_first_page(imgp)
872 	struct image_params *imgp;
873 {
874 	vm_page_t m;
875 
876 	if (imgp->firstpage != NULL) {
877 		m = sf_buf_page(imgp->firstpage);
878 		sf_buf_free(imgp->firstpage);
879 		imgp->firstpage = NULL;
880 		vm_page_lock_queues();
881 		vm_page_unhold(m);
882 		vm_page_unlock_queues();
883 	}
884 }
885 
886 /*
887  * Destroy old address space, and allocate a new stack
888  *	The new stack is only SGROWSIZ large because it is grown
889  *	automatically in trap.c.
890  */
891 int
892 exec_new_vmspace(imgp, sv)
893 	struct image_params *imgp;
894 	struct sysentvec *sv;
895 {
896 	int error;
897 	struct proc *p = imgp->proc;
898 	struct vmspace *vmspace = p->p_vmspace;
899 	vm_offset_t stack_addr;
900 	vm_map_t map;
901 
902 	imgp->vmspace_destroyed = 1;
903 	imgp->sysent = sv;
904 
905 	/* Called with Giant held, do not depend on it! */
906 	EVENTHANDLER_INVOKE(process_exec, p, imgp);
907 
908 	/*
909 	 * Here is as good a place as any to do any resource limit cleanups.
910 	 * This is needed if a 64 bit binary exec's a 32 bit binary - the
911 	 * data size limit may need to be changed to a value that makes
912 	 * sense for the 32 bit binary.
913 	 */
914 	if (sv->sv_fixlimits != NULL)
915 		sv->sv_fixlimits(p);
916 
917 	/*
918 	 * Blow away entire process VM, if address space not shared,
919 	 * otherwise, create a new VM space so that other threads are
920 	 * not disrupted
921 	 */
922 	map = &vmspace->vm_map;
923 	if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser &&
924 	    vm_map_max(map) == sv->sv_maxuser) {
925 		shmexit(vmspace);
926 		pmap_remove_pages(vmspace_pmap(vmspace));
927 		vm_map_remove(map, vm_map_min(map), vm_map_max(map));
928 	} else {
929 		vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser);
930 		vmspace = p->p_vmspace;
931 		map = &vmspace->vm_map;
932 	}
933 
934 	/* Allocate a new stack */
935 	stack_addr = sv->sv_usrstack - maxssiz;
936 	error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
937 	    sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN);
938 	if (error)
939 		return (error);
940 
941 #ifdef __ia64__
942 	/* Allocate a new register stack */
943 	stack_addr = IA64_BACKINGSTORE;
944 	error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
945 	    sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP);
946 	if (error)
947 		return (error);
948 #endif
949 
950 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
951 	 * VM_STACK case, but they are still used to monitor the size of the
952 	 * process stack so we can check the stack rlimit.
953 	 */
954 	vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
955 	vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz;
956 
957 	return (0);
958 }
959 
960 /*
961  * Copy out argument and environment strings from the old process
962  *	address space into the temporary string buffer.
963  */
964 int
965 exec_copyin_args(struct image_args *args, char *fname,
966     enum uio_seg segflg, char **argv, char **envv)
967 {
968 	char *argp, *envp;
969 	int error;
970 	size_t length;
971 
972 	error = 0;
973 
974 	bzero(args, sizeof(*args));
975 	if (argv == NULL)
976 		return (EFAULT);
977 	/*
978 	 * Allocate temporary demand zeroed space for argument and
979 	 *	environment strings:
980 	 *
981 	 * o ARG_MAX for argument and environment;
982 	 * o MAXSHELLCMDLEN for the name of interpreters.
983 	 */
984 	args->buf = (char *) kmem_alloc_wait(exec_map,
985 	    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
986 	if (args->buf == NULL)
987 		return (ENOMEM);
988 	args->begin_argv = args->buf;
989 	args->endp = args->begin_argv;
990 	args->stringspace = ARG_MAX;
991 
992 	args->fname = args->buf + ARG_MAX;
993 
994 	/*
995 	 * Copy the file name.
996 	 */
997 	error = (segflg == UIO_SYSSPACE) ?
998 	    copystr(fname, args->fname, PATH_MAX, &length) :
999 	    copyinstr(fname, args->fname, PATH_MAX, &length);
1000 	if (error != 0)
1001 		goto err_exit;
1002 
1003 	/*
1004 	 * extract arguments first
1005 	 */
1006 	while ((argp = (caddr_t) (intptr_t) fuword(argv++))) {
1007 		if (argp == (caddr_t) -1) {
1008 			error = EFAULT;
1009 			goto err_exit;
1010 		}
1011 		if ((error = copyinstr(argp, args->endp,
1012 		    args->stringspace, &length))) {
1013 			if (error == ENAMETOOLONG)
1014 				error = E2BIG;
1015 			goto err_exit;
1016 		}
1017 		args->stringspace -= length;
1018 		args->endp += length;
1019 		args->argc++;
1020 	}
1021 
1022 	args->begin_envv = args->endp;
1023 
1024 	/*
1025 	 * extract environment strings
1026 	 */
1027 	if (envv) {
1028 		while ((envp = (caddr_t)(intptr_t)fuword(envv++))) {
1029 			if (envp == (caddr_t)-1) {
1030 				error = EFAULT;
1031 				goto err_exit;
1032 			}
1033 			if ((error = copyinstr(envp, args->endp,
1034 			    args->stringspace, &length))) {
1035 				if (error == ENAMETOOLONG)
1036 					error = E2BIG;
1037 				goto err_exit;
1038 			}
1039 			args->stringspace -= length;
1040 			args->endp += length;
1041 			args->envc++;
1042 		}
1043 	}
1044 
1045 	return (0);
1046 
1047 err_exit:
1048 	exec_free_args(args);
1049 	return (error);
1050 }
1051 
1052 static void
1053 exec_free_args(struct image_args *args)
1054 {
1055 
1056 	if (args->buf) {
1057 		kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
1058 		    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
1059 		args->buf = NULL;
1060 	}
1061 }
1062 
1063 /*
1064  * Copy strings out to the new process address space, constructing
1065  *	new arg and env vector tables. Return a pointer to the base
1066  *	so that it can be used as the initial stack pointer.
1067  */
1068 register_t *
1069 exec_copyout_strings(imgp)
1070 	struct image_params *imgp;
1071 {
1072 	int argc, envc;
1073 	char **vectp;
1074 	char *stringp, *destp;
1075 	register_t *stack_base;
1076 	struct ps_strings *arginfo;
1077 	struct proc *p;
1078 	int szsigcode;
1079 
1080 	/*
1081 	 * Calculate string base and vector table pointers.
1082 	 * Also deal with signal trampoline code for this exec type.
1083 	 */
1084 	p = imgp->proc;
1085 	szsigcode = 0;
1086 	arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
1087 	if (p->p_sysent->sv_szsigcode != NULL)
1088 		szsigcode = *(p->p_sysent->sv_szsigcode);
1089 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
1090 	    roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
1091 
1092 	/*
1093 	 * install sigcode
1094 	 */
1095 	if (szsigcode)
1096 		copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo -
1097 		    szsigcode), szsigcode);
1098 
1099 	/*
1100 	 * If we have a valid auxargs ptr, prepare some room
1101 	 * on the stack.
1102 	 */
1103 	if (imgp->auxargs) {
1104 		/*
1105 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
1106 		 * lower compatibility.
1107 		 */
1108 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
1109 		    (AT_COUNT * 2);
1110 		/*
1111 		 * The '+ 2' is for the null pointers at the end of each of
1112 		 * the arg and env vector sets,and imgp->auxarg_size is room
1113 		 * for argument of Runtime loader.
1114 		 */
1115 		vectp = (char **)(destp - (imgp->args->argc +
1116 		    imgp->args->envc + 2 + imgp->auxarg_size) *
1117 		    sizeof(char *));
1118 
1119 	} else {
1120 		/*
1121 		 * The '+ 2' is for the null pointers at the end of each of
1122 		 * the arg and env vector sets
1123 		 */
1124 		vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc + 2) *
1125 		    sizeof(char *));
1126 	}
1127 
1128 	/*
1129 	 * vectp also becomes our initial stack base
1130 	 */
1131 	stack_base = (register_t *)vectp;
1132 
1133 	stringp = imgp->args->begin_argv;
1134 	argc = imgp->args->argc;
1135 	envc = imgp->args->envc;
1136 
1137 	/*
1138 	 * Copy out strings - arguments and environment.
1139 	 */
1140 	copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
1141 
1142 	/*
1143 	 * Fill in "ps_strings" struct for ps, w, etc.
1144 	 */
1145 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
1146 	suword(&arginfo->ps_nargvstr, argc);
1147 
1148 	/*
1149 	 * Fill in argument portion of vector table.
1150 	 */
1151 	for (; argc > 0; --argc) {
1152 		suword(vectp++, (long)(intptr_t)destp);
1153 		while (*stringp++ != 0)
1154 			destp++;
1155 		destp++;
1156 	}
1157 
1158 	/* a null vector table pointer separates the argp's from the envp's */
1159 	suword(vectp++, 0);
1160 
1161 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
1162 	suword(&arginfo->ps_nenvstr, envc);
1163 
1164 	/*
1165 	 * Fill in environment portion of vector table.
1166 	 */
1167 	for (; envc > 0; --envc) {
1168 		suword(vectp++, (long)(intptr_t)destp);
1169 		while (*stringp++ != 0)
1170 			destp++;
1171 		destp++;
1172 	}
1173 
1174 	/* end of vector table is a null pointer */
1175 	suword(vectp, 0);
1176 
1177 	return (stack_base);
1178 }
1179 
1180 /*
1181  * Check permissions of file to execute.
1182  *	Called with imgp->vp locked.
1183  *	Return 0 for success or error code on failure.
1184  */
1185 int
1186 exec_check_permissions(imgp)
1187 	struct image_params *imgp;
1188 {
1189 	struct vnode *vp = imgp->vp;
1190 	struct vattr *attr = imgp->attr;
1191 	struct thread *td;
1192 	int error;
1193 
1194 	td = curthread;			/* XXXKSE */
1195 
1196 	/* Get file attributes */
1197 	error = VOP_GETATTR(vp, attr, td->td_ucred, td);
1198 	if (error)
1199 		return (error);
1200 
1201 #ifdef MAC
1202 	error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp);
1203 	if (error)
1204 		return (error);
1205 #endif
1206 
1207 	/*
1208 	 * 1) Check if file execution is disabled for the filesystem that this
1209 	 *	file resides on.
1210 	 * 2) Insure that at least one execute bit is on - otherwise root
1211 	 *	will always succeed, and we don't want to happen unless the
1212 	 *	file really is executable.
1213 	 * 3) Insure that the file is a regular file.
1214 	 */
1215 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
1216 	    ((attr->va_mode & 0111) == 0) ||
1217 	    (attr->va_type != VREG))
1218 		return (EACCES);
1219 
1220 	/*
1221 	 * Zero length files can't be exec'd
1222 	 */
1223 	if (attr->va_size == 0)
1224 		return (ENOEXEC);
1225 
1226 	/*
1227 	 *  Check for execute permission to file based on current credentials.
1228 	 */
1229 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
1230 	if (error)
1231 		return (error);
1232 
1233 	/*
1234 	 * Check number of open-for-writes on the file and deny execution
1235 	 * if there are any.
1236 	 */
1237 	if (vp->v_writecount)
1238 		return (ETXTBSY);
1239 
1240 	/*
1241 	 * Call filesystem specific open routine (which does nothing in the
1242 	 * general case).
1243 	 */
1244 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1);
1245 	return (error);
1246 }
1247 
1248 /*
1249  * Exec handler registration
1250  */
1251 int
1252 exec_register(execsw_arg)
1253 	const struct execsw *execsw_arg;
1254 {
1255 	const struct execsw **es, **xs, **newexecsw;
1256 	int count = 2;	/* New slot and trailing NULL */
1257 
1258 	if (execsw)
1259 		for (es = execsw; *es; es++)
1260 			count++;
1261 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1262 	if (newexecsw == NULL)
1263 		return (ENOMEM);
1264 	xs = newexecsw;
1265 	if (execsw)
1266 		for (es = execsw; *es; es++)
1267 			*xs++ = *es;
1268 	*xs++ = execsw_arg;
1269 	*xs = NULL;
1270 	if (execsw)
1271 		free(execsw, M_TEMP);
1272 	execsw = newexecsw;
1273 	return (0);
1274 }
1275 
1276 int
1277 exec_unregister(execsw_arg)
1278 	const struct execsw *execsw_arg;
1279 {
1280 	const struct execsw **es, **xs, **newexecsw;
1281 	int count = 1;
1282 
1283 	if (execsw == NULL)
1284 		panic("unregister with no handlers left?\n");
1285 
1286 	for (es = execsw; *es; es++) {
1287 		if (*es == execsw_arg)
1288 			break;
1289 	}
1290 	if (*es == NULL)
1291 		return (ENOENT);
1292 	for (es = execsw; *es; es++)
1293 		if (*es != execsw_arg)
1294 			count++;
1295 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1296 	if (newexecsw == NULL)
1297 		return (ENOMEM);
1298 	xs = newexecsw;
1299 	for (es = execsw; *es; es++)
1300 		if (*es != execsw_arg)
1301 			*xs++ = *es;
1302 	*xs = NULL;
1303 	if (execsw)
1304 		free(execsw, M_TEMP);
1305 	execsw = newexecsw;
1306 	return (0);
1307 }
1308