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