xref: /freebsd/sys/kern/kern_exec.c (revision 1f467eaaf82a9b863fc6264c7c8e6b40fffdb357)
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/mac.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/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/timers.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 MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
84 
85 static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS);
86 static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS);
87 static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS);
88 static int do_execve(struct thread *td, struct image_args *args,
89     struct mac *mac_p);
90 
91 /* XXX This should be vm_size_t. */
92 SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD,
93     NULL, 0, sysctl_kern_ps_strings, "LU", "");
94 
95 /* XXX This should be vm_size_t. */
96 SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD,
97     NULL, 0, sysctl_kern_usrstack, "LU", "");
98 
99 SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD,
100     NULL, 0, sysctl_kern_stackprot, "I", "");
101 
102 u_long ps_arg_cache_limit = PAGE_SIZE / 16;
103 SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
104     &ps_arg_cache_limit, 0, "");
105 
106 static int
107 sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS)
108 {
109 	struct proc *p;
110 	int error;
111 
112 	p = curproc;
113 #ifdef SCTL_MASK32
114 	if (req->flags & SCTL_MASK32) {
115 		unsigned int val;
116 		val = (unsigned int)p->p_sysent->sv_psstrings;
117 		error = SYSCTL_OUT(req, &val, sizeof(val));
118 	} else
119 #endif
120 		error = SYSCTL_OUT(req, &p->p_sysent->sv_psstrings,
121 		   sizeof(p->p_sysent->sv_psstrings));
122 	return error;
123 }
124 
125 static int
126 sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS)
127 {
128 	struct proc *p;
129 	int error;
130 
131 	p = curproc;
132 #ifdef SCTL_MASK32
133 	if (req->flags & SCTL_MASK32) {
134 		unsigned int val;
135 		val = (unsigned int)p->p_sysent->sv_usrstack;
136 		error = SYSCTL_OUT(req, &val, sizeof(val));
137 	} else
138 #endif
139 		error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack,
140 		    sizeof(p->p_sysent->sv_usrstack));
141 	return error;
142 }
143 
144 static int
145 sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS)
146 {
147 	struct proc *p;
148 
149 	p = curproc;
150 	return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot,
151 	    sizeof(p->p_sysent->sv_stackprot)));
152 }
153 
154 /*
155  * Each of the items is a pointer to a `const struct execsw', hence the
156  * double pointer here.
157  */
158 static const struct execsw **execsw;
159 
160 #ifndef _SYS_SYSPROTO_H_
161 struct execve_args {
162 	char    *fname;
163 	char    **argv;
164 	char    **envv;
165 };
166 #endif
167 
168 /*
169  * MPSAFE
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 
186 	if (error == 0)
187 		error = kern_execve(td, &args, NULL);
188 
189 	exec_free_args(&args);
190 
191 	return (error);
192 }
193 
194 #ifndef _SYS_SYSPROTO_H_
195 struct __mac_execve_args {
196 	char	*fname;
197 	char	**argv;
198 	char	**envv;
199 	struct mac	*mac_p;
200 };
201 #endif
202 
203 /*
204  * MPSAFE
205  */
206 int
207 __mac_execve(td, uap)
208 	struct thread *td;
209 	struct __mac_execve_args /* {
210 		char *fname;
211 		char **argv;
212 		char **envv;
213 		struct mac *mac_p;
214 	} */ *uap;
215 {
216 #ifdef MAC
217 	int error;
218 	struct image_args args;
219 
220 	error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
221 	    uap->argv, uap->envv);
222 
223 	if (error == 0)
224 		error = kern_execve(td, &args, uap->mac_p);
225 
226 	exec_free_args(&args);
227 
228 	return (error);
229 #else
230 	return (ENOSYS);
231 #endif
232 }
233 
234 /*
235  * XXX: kern_execve has the astonishing property of not always
236  * returning to the caller.  If sufficiently bad things happen during
237  * the call to do_execve(), it can end up calling exit1(); as a result,
238  * callers must avoid doing anything which they might need to undo
239  * (e.g., allocating memory).
240  */
241 int
242 kern_execve(td, args, mac_p)
243 	struct thread *td;
244 	struct image_args *args;
245 	struct mac *mac_p;
246 {
247 	struct proc *p = td->td_proc;
248 	int error;
249 
250 	if (p->p_flag & P_HADTHREADS) {
251 		PROC_LOCK(p);
252 		if (thread_single(SINGLE_BOUNDARY)) {
253 			PROC_UNLOCK(p);
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 	ndp = &nd;
363 	NDINIT(ndp, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME | MPSAFE,
364 	    UIO_SYSSPACE, args->fname, td);
365 
366 interpret:
367 	error = namei(ndp);
368 	if (error)
369 		goto exec_fail;
370 
371 	vfslocked = NDHASGIANT(ndp);
372 	imgp->vp = ndp->ni_vp;
373 
374 	/*
375 	 * Check file permissions (also 'opens' file)
376 	 */
377 	error = exec_check_permissions(imgp);
378 	if (error)
379 		goto exec_fail_dealloc;
380 
381 	imgp->object = imgp->vp->v_object;
382 	if (imgp->object != NULL)
383 		vm_object_reference(imgp->object);
384 
385 	/*
386 	 * Set VV_TEXT now so no one can write to the executable while we're
387 	 * activating it.
388 	 *
389 	 * Remember if this was set before and unset it in case this is not
390 	 * actually an executable image.
391 	 */
392 	textset = imgp->vp->v_vflag & VV_TEXT;
393 	imgp->vp->v_vflag |= VV_TEXT;
394 
395 	error = exec_map_first_page(imgp);
396 	if (error)
397 		goto exec_fail_dealloc;
398 
399 	/*
400 	 *	If the current process has a special image activator it
401 	 *	wants to try first, call it.   For example, emulating shell
402 	 *	scripts differently.
403 	 */
404 	error = -1;
405 	if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
406 		error = img_first(imgp);
407 
408 	/*
409 	 *	Loop through the list of image activators, calling each one.
410 	 *	An activator returns -1 if there is no match, 0 on success,
411 	 *	and an error otherwise.
412 	 */
413 	for (i = 0; error == -1 && execsw[i]; ++i) {
414 		if (execsw[i]->ex_imgact == NULL ||
415 		    execsw[i]->ex_imgact == img_first) {
416 			continue;
417 		}
418 		error = (*execsw[i]->ex_imgact)(imgp);
419 	}
420 
421 	if (error) {
422 		if (error == -1) {
423 			if (textset == 0)
424 				imgp->vp->v_vflag &= ~VV_TEXT;
425 			error = ENOEXEC;
426 		}
427 		goto exec_fail_dealloc;
428 	}
429 
430 	/*
431 	 * Special interpreter operation, cleanup and loop up to try to
432 	 * activate the interpreter.
433 	 */
434 	if (imgp->interpreted) {
435 		exec_unmap_first_page(imgp);
436 		/*
437 		 * VV_TEXT needs to be unset for scripts.  There is a short
438 		 * period before we determine that something is a script where
439 		 * VV_TEXT will be set. The vnode lock is held over this
440 		 * entire period so nothing should illegitimately be blocked.
441 		 */
442 		imgp->vp->v_vflag &= ~VV_TEXT;
443 		/* free name buffer and old vnode */
444 		NDFREE(ndp, NDF_ONLY_PNBUF);
445 #ifdef MAC
446 		interplabel = mac_vnode_label_alloc();
447 		mac_copy_vnode_label(ndp->ni_vp->v_label, interplabel);
448 #endif
449 		vput(ndp->ni_vp);
450 		vm_object_deallocate(imgp->object);
451 		imgp->object = NULL;
452 		VFS_UNLOCK_GIANT(vfslocked);
453 		vfslocked = 0;
454 		/* set new name to that of the interpreter */
455 		NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME | MPSAFE,
456 		    UIO_SYSSPACE, imgp->interpreter_name, td);
457 		goto interpret;
458 	}
459 
460 	/*
461 	 * Copy out strings (args and env) and initialize stack base
462 	 */
463 	if (p->p_sysent->sv_copyout_strings)
464 		stack_base = (*p->p_sysent->sv_copyout_strings)(imgp);
465 	else
466 		stack_base = exec_copyout_strings(imgp);
467 
468 	/*
469 	 * If custom stack fixup routine present for this process
470 	 * let it do the stack setup.
471 	 * Else stuff argument count as first item on stack
472 	 */
473 	if (p->p_sysent->sv_fixup != NULL)
474 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
475 	else
476 		suword(--stack_base, imgp->args->argc);
477 
478 	/*
479 	 * For security and other reasons, the file descriptor table cannot
480 	 * be shared after an exec.
481 	 */
482 	fdunshare(p, td);
483 
484 	/* Clear POSIX timers */
485 	itimers_event_hook(p, ITIMER_EV_EXEC);
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 	fdcloseexec(td);
501 
502 	/* Get a reference to the vnode prior to locking the proc */
503 	VREF(ndp->ni_vp);
504 
505 	/*
506 	 * For security and other reasons, signal handlers cannot
507 	 * be shared after an exec. The new process gets a copy of the old
508 	 * handlers. In execsigs(), the new process will have its signals
509 	 * reset.
510 	 */
511 	PROC_LOCK(p);
512 	if (sigacts_shared(p->p_sigacts)) {
513 		oldsigacts = p->p_sigacts;
514 		PROC_UNLOCK(p);
515 		newsigacts = sigacts_alloc();
516 		sigacts_copy(newsigacts, oldsigacts);
517 		PROC_LOCK(p);
518 		p->p_sigacts = newsigacts;
519 	} else
520 		oldsigacts = NULL;
521 
522 	/* Stop profiling */
523 	stopprofclock(p);
524 
525 	/* reset caught signals */
526 	execsigs(p);
527 
528 	/* name this process - nameiexec(p, ndp) */
529 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
530 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
531 	p->p_comm[len] = 0;
532 
533 	/*
534 	 * mark as execed, wakeup the process that vforked (if any) and tell
535 	 * it that it now has its own resources back
536 	 */
537 	p->p_flag |= P_EXEC;
538 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
539 		p->p_flag &= ~P_PPWAIT;
540 		wakeup(p->p_pptr);
541 	}
542 
543 	/*
544 	 * Implement image setuid/setgid.
545 	 *
546 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
547 	 * the process is being traced.
548 	 *
549 	 * XXXMAC: For the time being, use NOSUID to also prohibit
550 	 * transitions on the file system.
551 	 */
552 	oldcred = p->p_ucred;
553 	credential_changing = 0;
554 	credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid !=
555 	    attr.va_uid;
556 	credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid !=
557 	    attr.va_gid;
558 #ifdef MAC
559 	will_transition = mac_execve_will_transition(oldcred, imgp->vp,
560 	    interplabel, imgp);
561 	credential_changing |= will_transition;
562 #endif
563 
564 	if (credential_changing &&
565 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
566 	    (p->p_flag & P_TRACED) == 0) {
567 		/*
568 		 * Turn off syscall tracing for set-id programs, except for
569 		 * root.  Record any set-id flags first to make sure that
570 		 * we do not regain any tracing during a possible block.
571 		 */
572 		setsugid(p);
573 #ifdef KTRACE
574 		if (p->p_tracevp != NULL && suser_cred(oldcred, SUSER_ALLOWJAIL)) {
575 			mtx_lock(&ktrace_mtx);
576 			p->p_traceflag = 0;
577 			tracevp = p->p_tracevp;
578 			p->p_tracevp = NULL;
579 			tracecred = p->p_tracecred;
580 			p->p_tracecred = NULL;
581 			mtx_unlock(&ktrace_mtx);
582 		}
583 #endif
584 		/*
585 		 * Close any file descriptors 0..2 that reference procfs,
586 		 * then make sure file descriptors 0..2 are in use.
587 		 *
588 		 * setugidsafety() may call closef() and then pfind()
589 		 * which may grab the process lock.
590 		 * fdcheckstd() may call falloc() which may block to
591 		 * allocate memory, so temporarily drop the process lock.
592 		 */
593 		PROC_UNLOCK(p);
594 		setugidsafety(td);
595 		error = fdcheckstd(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_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(td, SIGTRAP, NULL, SIGTARGET_TD);
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 	/*
728 	 * Handle deferred decrement of ref counts.
729 	 */
730 	if (textvp != NULL)
731 		vrele(textvp);
732 	if (ndp->ni_vp && error != 0)
733 		vrele(ndp->ni_vp);
734 #ifdef KTRACE
735 	if (tracevp != NULL)
736 		vrele(tracevp);
737 	if (tracecred != NULL)
738 		crfree(tracecred);
739 #endif
740 	if (oldargs != NULL)
741 		pargs_drop(oldargs);
742 	if (newargs != NULL)
743 		pargs_drop(newargs);
744 	if (oldsigacts != NULL)
745 		sigacts_free(oldsigacts);
746 
747 exec_fail_dealloc:
748 
749 	/*
750 	 * free various allocated resources
751 	 */
752 	if (imgp->firstpage != NULL)
753 		exec_unmap_first_page(imgp);
754 
755 	if (imgp->vp != NULL) {
756 		NDFREE(ndp, NDF_ONLY_PNBUF);
757 		vput(imgp->vp);
758 	}
759 
760 	if (imgp->object != NULL)
761 		vm_object_deallocate(imgp->object);
762 
763 	if (error == 0) {
764 		/*
765 		 * Stop the process here if its stop event mask has
766 		 * the S_EXEC bit set.
767 		 */
768 		STOPEVENT(p, S_EXEC, 0);
769 		goto done2;
770 	}
771 
772 exec_fail:
773 	/* we're done here, clear P_INEXEC */
774 	PROC_LOCK(p);
775 	p->p_flag &= ~P_INEXEC;
776 	PROC_UNLOCK(p);
777 
778 	if (imgp->vmspace_destroyed) {
779 		/* sorry, no more process anymore. exit gracefully */
780 #ifdef MAC
781 		mac_execve_exit(imgp);
782 		if (interplabel != NULL)
783 			mac_vnode_label_free(interplabel);
784 #endif
785 		VFS_UNLOCK_GIANT(vfslocked);
786 		exec_free_args(args);
787 		exit1(td, W_EXITCODE(0, SIGABRT));
788 		/* NOT REACHED */
789 		error = 0;
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 	return (error);
799 }
800 
801 int
802 exec_map_first_page(imgp)
803 	struct image_params *imgp;
804 {
805 	int rv, i;
806 	int initial_pagein;
807 	vm_page_t ma[VM_INITIAL_PAGEIN];
808 	vm_object_t object;
809 
810 	if (imgp->firstpage != NULL)
811 		exec_unmap_first_page(imgp);
812 
813 	object = imgp->vp->v_object;
814 	if (object == NULL)
815 		return (EACCES);
816 	VM_OBJECT_LOCK(object);
817 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
818 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
819 		initial_pagein = VM_INITIAL_PAGEIN;
820 		if (initial_pagein > object->size)
821 			initial_pagein = object->size;
822 		for (i = 1; i < initial_pagein; i++) {
823 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
824 				if (ma[i]->valid)
825 					break;
826 				vm_page_lock_queues();
827 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) {
828 					vm_page_unlock_queues();
829 					break;
830 				}
831 				vm_page_busy(ma[i]);
832 				vm_page_unlock_queues();
833 			} else {
834 				ma[i] = vm_page_alloc(object, i,
835 				    VM_ALLOC_NORMAL);
836 				if (ma[i] == NULL)
837 					break;
838 			}
839 		}
840 		initial_pagein = i;
841 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
842 		ma[0] = vm_page_lookup(object, 0);
843 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) ||
844 		    (ma[0]->valid == 0)) {
845 			if (ma[0]) {
846 				vm_page_lock_queues();
847 				pmap_remove_all(ma[0]);
848 				vm_page_free(ma[0]);
849 				vm_page_unlock_queues();
850 			}
851 			VM_OBJECT_UNLOCK(object);
852 			return (EIO);
853 		}
854 	}
855 	vm_page_lock_queues();
856 	vm_page_hold(ma[0]);
857 	vm_page_wakeup(ma[0]);
858 	vm_page_unlock_queues();
859 	VM_OBJECT_UNLOCK(object);
860 
861 	imgp->firstpage = sf_buf_alloc(ma[0], 0);
862 	imgp->image_header = (char *)sf_buf_kva(imgp->firstpage);
863 
864 	return (0);
865 }
866 
867 void
868 exec_unmap_first_page(imgp)
869 	struct image_params *imgp;
870 {
871 	vm_page_t m;
872 
873 	if (imgp->firstpage != NULL) {
874 		m = sf_buf_page(imgp->firstpage);
875 		sf_buf_free(imgp->firstpage);
876 		imgp->firstpage = NULL;
877 		vm_page_lock_queues();
878 		vm_page_unhold(m);
879 		vm_page_unlock_queues();
880 	}
881 }
882 
883 /*
884  * Destroy old address space, and allocate a new stack
885  *	The new stack is only SGROWSIZ large because it is grown
886  *	automatically in trap.c.
887  */
888 int
889 exec_new_vmspace(imgp, sv)
890 	struct image_params *imgp;
891 	struct sysentvec *sv;
892 {
893 	int error;
894 	struct proc *p = imgp->proc;
895 	struct vmspace *vmspace = p->p_vmspace;
896 	vm_offset_t stack_addr;
897 	vm_map_t map;
898 
899 	imgp->vmspace_destroyed = 1;
900 
901 	/* Called with Giant held, do not depend on it! */
902 	EVENTHANDLER_INVOKE(process_exec, p);
903 
904 	/*
905 	 * Here is as good a place as any to do any resource limit cleanups.
906 	 * This is needed if a 64 bit binary exec's a 32 bit binary - the
907 	 * data size limit may need to be changed to a value that makes
908 	 * sense for the 32 bit binary.
909 	 */
910 	if (sv->sv_fixlimits != NULL)
911 		sv->sv_fixlimits(imgp);
912 
913 	/*
914 	 * Blow away entire process VM, if address space not shared,
915 	 * otherwise, create a new VM space so that other threads are
916 	 * not disrupted
917 	 */
918 	map = &vmspace->vm_map;
919 	if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser &&
920 	    vm_map_max(map) == sv->sv_maxuser) {
921 		shmexit(vmspace);
922 		pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map),
923 		    vm_map_max(map));
924 		vm_map_remove(map, vm_map_min(map), vm_map_max(map));
925 	} else {
926 		vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser);
927 		vmspace = p->p_vmspace;
928 		map = &vmspace->vm_map;
929 	}
930 
931 	/* Allocate a new stack */
932 	stack_addr = sv->sv_usrstack - maxssiz;
933 	error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
934 	    sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN);
935 	if (error)
936 		return (error);
937 
938 #ifdef __ia64__
939 	/* Allocate a new register stack */
940 	stack_addr = IA64_BACKINGSTORE;
941 	error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
942 	    sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP);
943 	if (error)
944 		return (error);
945 #endif
946 
947 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
948 	 * VM_STACK case, but they are still used to monitor the size of the
949 	 * process stack so we can check the stack rlimit.
950 	 */
951 	vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
952 	vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz;
953 
954 	return (0);
955 }
956 
957 /*
958  * Copy out argument and environment strings from the old process
959  *	address space into the temporary string buffer.
960  */
961 int
962 exec_copyin_args(struct image_args *args, char *fname,
963     enum uio_seg segflg, char **argv, char **envv)
964 {
965 	char *argp, *envp;
966 	int error;
967 	size_t length;
968 
969 	error = 0;
970 
971 	bzero(args, sizeof(*args));
972 	if (argv == NULL)
973 		return (EFAULT);
974 	/*
975 	 * Allocate temporary demand zeroed space for argument and
976 	 *	environment strings:
977 	 *
978 	 * o ARG_MAX for argument and environment;
979 	 * o MAXSHELLCMDLEN for the name of interpreters.
980 	 */
981 	args->buf = (char *) kmem_alloc_wait(exec_map,
982 	    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
983 	if (args->buf == NULL)
984 		return (ENOMEM);
985 	args->begin_argv = args->buf;
986 	args->endp = args->begin_argv;
987 	args->stringspace = ARG_MAX;
988 
989 	args->fname = args->buf + ARG_MAX;
990 
991 	/*
992 	 * Copy the file name.
993 	 */
994 	error = (segflg == UIO_SYSSPACE) ?
995 	    copystr(fname, args->fname, PATH_MAX, &length) :
996 	    copyinstr(fname, args->fname, PATH_MAX, &length);
997 	if (error != 0)
998 		return (error);
999 
1000 	/*
1001 	 * extract arguments first
1002 	 */
1003 	while ((argp = (caddr_t) (intptr_t) fuword(argv++))) {
1004 		if (argp == (caddr_t) -1)
1005 			return (EFAULT);
1006 		if ((error = copyinstr(argp, args->endp,
1007 		    args->stringspace, &length))) {
1008 			if (error == ENAMETOOLONG)
1009 				return (E2BIG);
1010 			return (error);
1011 		}
1012 		args->stringspace -= length;
1013 		args->endp += length;
1014 		args->argc++;
1015 	}
1016 
1017 	args->begin_envv = args->endp;
1018 
1019 	/*
1020 	 * extract environment strings
1021 	 */
1022 	if (envv) {
1023 		while ((envp = (caddr_t)(intptr_t)fuword(envv++))) {
1024 			if (envp == (caddr_t)-1)
1025 				return (EFAULT);
1026 			if ((error = copyinstr(envp, args->endp,
1027 			    args->stringspace, &length))) {
1028 				if (error == ENAMETOOLONG)
1029 					return (E2BIG);
1030 				return (error);
1031 			}
1032 			args->stringspace -= length;
1033 			args->endp += length;
1034 			args->envc++;
1035 		}
1036 	}
1037 
1038 	return (0);
1039 }
1040 
1041 void
1042 exec_free_args(struct image_args *args)
1043 {
1044 
1045 	if (args->buf) {
1046 		kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
1047 		    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
1048 		args->buf = NULL;
1049 	}
1050 }
1051 
1052 /*
1053  * Copy strings out to the new process address space, constructing
1054  *	new arg and env vector tables. Return a pointer to the base
1055  *	so that it can be used as the initial stack pointer.
1056  */
1057 register_t *
1058 exec_copyout_strings(imgp)
1059 	struct image_params *imgp;
1060 {
1061 	int argc, envc;
1062 	char **vectp;
1063 	char *stringp, *destp;
1064 	register_t *stack_base;
1065 	struct ps_strings *arginfo;
1066 	struct proc *p;
1067 	int szsigcode;
1068 
1069 	/*
1070 	 * Calculate string base and vector table pointers.
1071 	 * Also deal with signal trampoline code for this exec type.
1072 	 */
1073 	p = imgp->proc;
1074 	szsigcode = 0;
1075 	arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
1076 	if (p->p_sysent->sv_szsigcode != NULL)
1077 		szsigcode = *(p->p_sysent->sv_szsigcode);
1078 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
1079 	    roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
1080 
1081 	/*
1082 	 * install sigcode
1083 	 */
1084 	if (szsigcode)
1085 		copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo -
1086 		    szsigcode), szsigcode);
1087 
1088 	/*
1089 	 * If we have a valid auxargs ptr, prepare some room
1090 	 * on the stack.
1091 	 */
1092 	if (imgp->auxargs) {
1093 		/*
1094 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
1095 		 * lower compatibility.
1096 		 */
1097 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
1098 		    (AT_COUNT * 2);
1099 		/*
1100 		 * The '+ 2' is for the null pointers at the end of each of
1101 		 * the arg and env vector sets,and imgp->auxarg_size is room
1102 		 * for argument of Runtime loader.
1103 		 */
1104 		vectp = (char **)(destp - (imgp->args->argc +
1105 		    imgp->args->envc + 2 + imgp->auxarg_size) *
1106 		    sizeof(char *));
1107 
1108 	} else {
1109 		/*
1110 		 * The '+ 2' is for the null pointers at the end of each of
1111 		 * the arg and env vector sets
1112 		 */
1113 		vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc + 2) *
1114 		    sizeof(char *));
1115 	}
1116 
1117 	/*
1118 	 * vectp also becomes our initial stack base
1119 	 */
1120 	stack_base = (register_t *)vectp;
1121 
1122 	stringp = imgp->args->begin_argv;
1123 	argc = imgp->args->argc;
1124 	envc = imgp->args->envc;
1125 
1126 	/*
1127 	 * Copy out strings - arguments and environment.
1128 	 */
1129 	copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
1130 
1131 	/*
1132 	 * Fill in "ps_strings" struct for ps, w, etc.
1133 	 */
1134 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
1135 	suword(&arginfo->ps_nargvstr, argc);
1136 
1137 	/*
1138 	 * Fill in argument portion of vector table.
1139 	 */
1140 	for (; argc > 0; --argc) {
1141 		suword(vectp++, (long)(intptr_t)destp);
1142 		while (*stringp++ != 0)
1143 			destp++;
1144 		destp++;
1145 	}
1146 
1147 	/* a null vector table pointer separates the argp's from the envp's */
1148 	suword(vectp++, 0);
1149 
1150 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
1151 	suword(&arginfo->ps_nenvstr, envc);
1152 
1153 	/*
1154 	 * Fill in environment portion of vector table.
1155 	 */
1156 	for (; envc > 0; --envc) {
1157 		suword(vectp++, (long)(intptr_t)destp);
1158 		while (*stringp++ != 0)
1159 			destp++;
1160 		destp++;
1161 	}
1162 
1163 	/* end of vector table is a null pointer */
1164 	suword(vectp, 0);
1165 
1166 	return (stack_base);
1167 }
1168 
1169 /*
1170  * Check permissions of file to execute.
1171  *	Called with imgp->vp locked.
1172  *	Return 0 for success or error code on failure.
1173  */
1174 int
1175 exec_check_permissions(imgp)
1176 	struct image_params *imgp;
1177 {
1178 	struct vnode *vp = imgp->vp;
1179 	struct vattr *attr = imgp->attr;
1180 	struct thread *td;
1181 	int error;
1182 
1183 	td = curthread;			/* XXXKSE */
1184 
1185 	/* Get file attributes */
1186 	error = VOP_GETATTR(vp, attr, td->td_ucred, td);
1187 	if (error)
1188 		return (error);
1189 
1190 #ifdef MAC
1191 	error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp);
1192 	if (error)
1193 		return (error);
1194 #endif
1195 
1196 	/*
1197 	 * 1) Check if file execution is disabled for the filesystem that this
1198 	 *	file resides on.
1199 	 * 2) Insure that at least one execute bit is on - otherwise root
1200 	 *	will always succeed, and we don't want to happen unless the
1201 	 *	file really is executable.
1202 	 * 3) Insure that the file is a regular file.
1203 	 */
1204 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
1205 	    ((attr->va_mode & 0111) == 0) ||
1206 	    (attr->va_type != VREG))
1207 		return (EACCES);
1208 
1209 	/*
1210 	 * Zero length files can't be exec'd
1211 	 */
1212 	if (attr->va_size == 0)
1213 		return (ENOEXEC);
1214 
1215 	/*
1216 	 *  Check for execute permission to file based on current credentials.
1217 	 */
1218 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
1219 	if (error)
1220 		return (error);
1221 
1222 	/*
1223 	 * Check number of open-for-writes on the file and deny execution
1224 	 * if there are any.
1225 	 */
1226 	if (vp->v_writecount)
1227 		return (ETXTBSY);
1228 
1229 	/*
1230 	 * Call filesystem specific open routine (which does nothing in the
1231 	 * general case).
1232 	 */
1233 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1);
1234 	return (error);
1235 }
1236 
1237 /*
1238  * Exec handler registration
1239  */
1240 int
1241 exec_register(execsw_arg)
1242 	const struct execsw *execsw_arg;
1243 {
1244 	const struct execsw **es, **xs, **newexecsw;
1245 	int count = 2;	/* New slot and trailing NULL */
1246 
1247 	if (execsw)
1248 		for (es = execsw; *es; es++)
1249 			count++;
1250 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1251 	if (newexecsw == NULL)
1252 		return (ENOMEM);
1253 	xs = newexecsw;
1254 	if (execsw)
1255 		for (es = execsw; *es; es++)
1256 			*xs++ = *es;
1257 	*xs++ = execsw_arg;
1258 	*xs = NULL;
1259 	if (execsw)
1260 		free(execsw, M_TEMP);
1261 	execsw = newexecsw;
1262 	return (0);
1263 }
1264 
1265 int
1266 exec_unregister(execsw_arg)
1267 	const struct execsw *execsw_arg;
1268 {
1269 	const struct execsw **es, **xs, **newexecsw;
1270 	int count = 1;
1271 
1272 	if (execsw == NULL)
1273 		panic("unregister with no handlers left?\n");
1274 
1275 	for (es = execsw; *es; es++) {
1276 		if (*es == execsw_arg)
1277 			break;
1278 	}
1279 	if (*es == NULL)
1280 		return (ENOENT);
1281 	for (es = execsw; *es; es++)
1282 		if (*es != execsw_arg)
1283 			count++;
1284 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1285 	if (newexecsw == NULL)
1286 		return (ENOMEM);
1287 	xs = newexecsw;
1288 	for (es = execsw; *es; es++)
1289 		if (*es != execsw_arg)
1290 			*xs++ = *es;
1291 	*xs = NULL;
1292 	if (execsw)
1293 		free(execsw, M_TEMP);
1294 	execsw = newexecsw;
1295 	return (0);
1296 }
1297