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