2 * Copyright (c) 1993, David Greenman
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_hwpmc_hooks.h"
31 #include "opt_kdtrace.h"
32 #include "opt_ktrace.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/eventhandler.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>
48 #include <sys/imgact.h>
49 #include <sys/imgact_elf.h>
51 #include <sys/malloc.h>
54 #include <sys/pioctl.h>
55 #include <sys/namei.h>
56 #include <sys/resourcevar.h>
57 #include <sys/sched.h>
59 #include <sys/sf_buf.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/sysent.h>
63 #include <sys/sysctl.h>
64 #include <sys/vnode.h>
67 #include <sys/ktrace.h>
71 #include <vm/vm_param.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_kern.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_pager.h>
81 #include <sys/pmckern.h>
84 #include <machine/reg.h>
86 #include <security/audit/audit.h>
87 #include <security/mac/mac_framework.h>
90 #include <sys/dtrace_bsd.h>
91 dtrace_execexit_func_t dtrace_fasttrap_exec;
94 SDT_PROVIDER_DECLARE(proc);
95 SDT_PROBE_DEFINE(proc, kernel, , exec, exec);
96 SDT_PROBE_ARGTYPE(proc, kernel, , exec, 0, "char *");
97 SDT_PROBE_DEFINE(proc, kernel, , exec_failure, exec-failure);
98 SDT_PROBE_ARGTYPE(proc, kernel, , exec_failure, 0, "int");
99 SDT_PROBE_DEFINE(proc, kernel, , exec_success, exec-success);
100 SDT_PROBE_ARGTYPE(proc, kernel, , exec_success, 0, "char *");
102 MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
104 static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS);
105 static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS);
106 static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS);
107 static int do_execve(struct thread *td, struct image_args *args,
109 static void exec_free_args(struct image_args *);
111 /* XXX This should be vm_size_t. */
112 SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD,
113 NULL, 0, sysctl_kern_ps_strings, "LU", "");
115 /* XXX This should be vm_size_t. */
116 SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD,
117 NULL, 0, sysctl_kern_usrstack, "LU", "");
119 SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD,
120 NULL, 0, sysctl_kern_stackprot, "I", "");
122 u_long ps_arg_cache_limit = PAGE_SIZE / 16;
123 SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
124 &ps_arg_cache_limit, 0, "");
126 static int map_at_zero = 0;
127 TUNABLE_INT("security.bsd.map_at_zero", &map_at_zero);
128 SYSCTL_INT(_security_bsd, OID_AUTO, map_at_zero, CTLFLAG_RW, &map_at_zero, 0,
129 "Permit processes to map an object at virtual address 0.");
132 sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS)
139 if (req->flags & SCTL_MASK32) {
141 val = (unsigned int)p->p_sysent->sv_psstrings;
142 error = SYSCTL_OUT(req, &val, sizeof(val));
145 error = SYSCTL_OUT(req, &p->p_sysent->sv_psstrings,
146 sizeof(p->p_sysent->sv_psstrings));
151 sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS)
158 if (req->flags & SCTL_MASK32) {
160 val = (unsigned int)p->p_sysent->sv_usrstack;
161 error = SYSCTL_OUT(req, &val, sizeof(val));
164 error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack,
165 sizeof(p->p_sysent->sv_usrstack));
170 sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS)
175 return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot,
176 sizeof(p->p_sysent->sv_stackprot)));
180 * Each of the items is a pointer to a `const struct execsw', hence the
181 * double pointer here.
183 static const struct execsw **execsw;
185 #ifndef _SYS_SYSPROTO_H_
196 struct execve_args /* {
203 struct image_args args;
205 error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
206 uap->argv, uap->envv);
208 error = kern_execve(td, &args, NULL);
212 #ifndef _SYS_SYSPROTO_H_
213 struct fexecve_args {
220 fexecve(struct thread *td, struct fexecve_args *uap)
223 struct image_args args;
225 error = exec_copyin_args(&args, NULL, UIO_SYSSPACE,
226 uap->argv, uap->envv);
229 error = kern_execve(td, &args, NULL);
234 #ifndef _SYS_SYSPROTO_H_
235 struct __mac_execve_args {
244 __mac_execve(td, uap)
246 struct __mac_execve_args /* {
255 struct image_args args;
257 error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
258 uap->argv, uap->envv);
260 error = kern_execve(td, &args, uap->mac_p);
268 * XXX: kern_execve has the astonishing property of not always returning to
269 * the caller. If sufficiently bad things happen during the call to
270 * do_execve(), it can end up calling exit1(); as a result, callers must
271 * avoid doing anything which they might need to undo (e.g., allocating
275 kern_execve(td, args, mac_p)
277 struct image_args *args;
280 struct proc *p = td->td_proc;
281 struct vmspace *oldvmspace;
284 AUDIT_ARG_ARGV(args->begin_argv, args->argc,
285 args->begin_envv - args->begin_argv);
286 AUDIT_ARG_ENVV(args->begin_envv, args->envc,
287 args->endp - args->begin_envv);
288 if (p->p_flag & P_HADTHREADS) {
290 if (thread_single(SINGLE_BOUNDARY)) {
292 exec_free_args(args);
293 return (ERESTART); /* Try again later. */
298 KASSERT((td->td_pflags & TDP_EXECVMSPC) == 0, ("nested execve"));
299 oldvmspace = td->td_proc->p_vmspace;
300 error = do_execve(td, args, mac_p);
302 if (p->p_flag & P_HADTHREADS) {
305 * If success, we upgrade to SINGLE_EXIT state to
306 * force other threads to suicide.
309 thread_single(SINGLE_EXIT);
314 if ((td->td_pflags & TDP_EXECVMSPC) != 0) {
315 KASSERT(td->td_proc->p_vmspace != oldvmspace,
316 ("oldvmspace still used"));
317 vmspace_free(oldvmspace);
318 td->td_pflags &= ~TDP_EXECVMSPC;
325 * In-kernel implementation of execve(). All arguments are assumed to be
326 * userspace pointers from the passed thread.
329 do_execve(td, args, mac_p)
331 struct image_args *args;
334 struct proc *p = td->td_proc;
336 struct ucred *newcred = NULL, *oldcred;
337 struct uidinfo *euip;
338 register_t *stack_base;
340 struct image_params image_params, *imgp;
342 int (*img_first)(struct image_params *);
343 struct pargs *oldargs = NULL, *newargs = NULL;
344 struct sigacts *oldsigacts, *newsigacts;
346 struct vnode *tracevp = NULL;
347 struct ucred *tracecred = NULL;
349 struct vnode *textvp = NULL, *binvp = NULL;
350 int credential_changing;
354 struct label *interpvplabel = NULL;
358 struct pmckern_procexec pe;
360 static const char fexecv_proc_title[] = "(fexecv)";
363 imgp = &image_params;
366 * Lock the process and set the P_INEXEC flag to indicate that
367 * it should be left alone until we're done here. This is
368 * necessary to avoid race conditions - e.g. in ptrace() -
369 * that might allow a local user to illicitly obtain elevated
373 KASSERT((p->p_flag & P_INEXEC) == 0,
374 ("%s(): process already has P_INEXEC flag", __func__));
375 p->p_flag |= P_INEXEC;
379 * Initialize part of the common data
382 imgp->execlabel = NULL;
384 imgp->entry_addr = 0;
385 imgp->vmspace_destroyed = 0;
386 imgp->interpreted = 0;
388 imgp->interpreter_name = args->buf + PATH_MAX + ARG_MAX;
389 imgp->auxargs = NULL;
392 imgp->firstpage = NULL;
393 imgp->ps_strings = 0;
394 imgp->auxarg_size = 0;
396 imgp->execpath = imgp->freepath = NULL;
400 error = mac_execve_enter(imgp, mac_p);
405 imgp->image_header = NULL;
408 * Translate the file name. namei() returns a vnode pointer
409 * in ni_vp amoung other things.
411 * XXXAUDIT: It would be desirable to also audit the name of the
412 * interpreter if this is an interpreted binary.
414 if (args->fname != NULL) {
415 NDINIT(&nd, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME
416 | MPSAFE | AUDITVNODE1, UIO_SYSSPACE, args->fname, td);
419 SDT_PROBE(proc, kernel, , exec, args->fname, 0, 0, 0, 0 );
422 if (args->fname != NULL) {
427 vfslocked = NDHASGIANT(&nd);
431 AUDIT_ARG_FD(args->fd);
432 error = fgetvp(td, args->fd, &binvp);
435 vfslocked = VFS_LOCK_GIANT(binvp->v_mount);
436 vn_lock(binvp, LK_EXCLUSIVE | LK_RETRY);
437 AUDIT_ARG_VNODE1(binvp);
442 * Check file permissions (also 'opens' file)
444 error = exec_check_permissions(imgp);
446 goto exec_fail_dealloc;
448 imgp->object = imgp->vp->v_object;
449 if (imgp->object != NULL)
450 vm_object_reference(imgp->object);
453 * Set VV_TEXT now so no one can write to the executable while we're
456 * Remember if this was set before and unset it in case this is not
457 * actually an executable image.
459 textset = imgp->vp->v_vflag & VV_TEXT;
460 imgp->vp->v_vflag |= VV_TEXT;
462 error = exec_map_first_page(imgp);
464 goto exec_fail_dealloc;
466 imgp->proc->p_osrel = 0;
468 * If the current process has a special image activator it
469 * wants to try first, call it. For example, emulating shell
470 * scripts differently.
473 if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
474 error = img_first(imgp);
477 * Loop through the list of image activators, calling each one.
478 * An activator returns -1 if there is no match, 0 on success,
479 * and an error otherwise.
481 for (i = 0; error == -1 && execsw[i]; ++i) {
482 if (execsw[i]->ex_imgact == NULL ||
483 execsw[i]->ex_imgact == img_first) {
486 error = (*execsw[i]->ex_imgact)(imgp);
492 imgp->vp->v_vflag &= ~VV_TEXT;
495 goto exec_fail_dealloc;
499 * Special interpreter operation, cleanup and loop up to try to
500 * activate the interpreter.
502 if (imgp->interpreted) {
503 exec_unmap_first_page(imgp);
505 * VV_TEXT needs to be unset for scripts. There is a short
506 * period before we determine that something is a script where
507 * VV_TEXT will be set. The vnode lock is held over this
508 * entire period so nothing should illegitimately be blocked.
510 imgp->vp->v_vflag &= ~VV_TEXT;
511 /* free name buffer and old vnode */
512 if (args->fname != NULL)
513 NDFREE(&nd, NDF_ONLY_PNBUF);
515 mac_execve_interpreter_enter(binvp, &interpvplabel);
518 VOP_CLOSE(binvp, FREAD, td->td_ucred, td);
522 vm_object_deallocate(imgp->object);
524 VFS_UNLOCK_GIANT(vfslocked);
526 /* set new name to that of the interpreter */
527 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME | MPSAFE,
528 UIO_SYSSPACE, imgp->interpreter_name, td);
529 args->fname = imgp->interpreter_name;
534 * NB: We unlock the vnode here because it is believed that none
535 * of the sv_copyout_strings/sv_fixup operations require the vnode.
537 VOP_UNLOCK(imgp->vp, 0);
540 * Do the best to calculate the full path to the image file.
542 if (imgp->auxargs != NULL &&
543 ((args->fname != NULL && args->fname[0] == '/') ||
544 vn_fullpath(td, imgp->vp, &imgp->execpath, &imgp->freepath) != 0))
545 imgp->execpath = args->fname;
548 * Copy out strings (args and env) and initialize stack base
550 if (p->p_sysent->sv_copyout_strings)
551 stack_base = (*p->p_sysent->sv_copyout_strings)(imgp);
553 stack_base = exec_copyout_strings(imgp);
556 * If custom stack fixup routine present for this process
557 * let it do the stack setup.
558 * Else stuff argument count as first item on stack
560 if (p->p_sysent->sv_fixup != NULL)
561 (*p->p_sysent->sv_fixup)(&stack_base, imgp);
563 suword(--stack_base, imgp->args->argc);
566 * For security and other reasons, the file descriptor table cannot
567 * be shared after an exec.
572 * Malloc things before we need locks.
575 euip = uifind(attr.va_uid);
576 i = imgp->args->begin_envv - imgp->args->begin_argv;
577 /* Cache arguments if they fit inside our allowance */
578 if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
579 newargs = pargs_alloc(i);
580 bcopy(imgp->args->begin_argv, newargs->ar_args, i);
583 /* close files on exec */
585 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
587 /* Get a reference to the vnode prior to locking the proc */
591 * For security and other reasons, signal handlers cannot
592 * be shared after an exec. The new process gets a copy of the old
593 * handlers. In execsigs(), the new process will have its signals
597 oldcred = crcopysafe(p, newcred);
598 if (sigacts_shared(p->p_sigacts)) {
599 oldsigacts = p->p_sigacts;
601 newsigacts = sigacts_alloc();
602 sigacts_copy(newsigacts, oldsigacts);
604 p->p_sigacts = newsigacts;
611 /* reset caught signals */
614 /* name this process - nameiexec(p, ndp) */
615 bzero(p->p_comm, sizeof(p->p_comm));
617 bcopy(nd.ni_cnd.cn_nameptr, p->p_comm,
618 min(nd.ni_cnd.cn_namelen, MAXCOMLEN));
619 else if (vn_commname(binvp, p->p_comm, sizeof(p->p_comm)) != 0)
620 bcopy(fexecv_proc_title, p->p_comm, sizeof(fexecv_proc_title));
621 bcopy(p->p_comm, td->td_name, sizeof(td->td_name));
623 sched_clear_tdname(td);
627 * mark as execed, wakeup the process that vforked (if any) and tell
628 * it that it now has its own resources back
631 if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
632 p->p_flag &= ~P_PPWAIT;
633 cv_broadcast(&p->p_pwait);
637 * Implement image setuid/setgid.
639 * Don't honor setuid/setgid if the filesystem prohibits it or if
640 * the process is being traced.
642 * XXXMAC: For the time being, use NOSUID to also prohibit
643 * transitions on the file system.
645 credential_changing = 0;
646 credential_changing |= (attr.va_mode & S_ISUID) && oldcred->cr_uid !=
648 credential_changing |= (attr.va_mode & S_ISGID) && oldcred->cr_gid !=
651 will_transition = mac_vnode_execve_will_transition(oldcred, imgp->vp,
652 interpvplabel, imgp);
653 credential_changing |= will_transition;
656 if (credential_changing &&
657 (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
658 (p->p_flag & P_TRACED) == 0) {
660 * Turn off syscall tracing for set-id programs, except for
661 * root. Record any set-id flags first to make sure that
662 * we do not regain any tracing during a possible block.
667 if (p->p_tracecred != NULL &&
668 priv_check_cred(p->p_tracecred, PRIV_DEBUG_DIFFCRED, 0))
669 ktrprocexec(p, &tracecred, &tracevp);
672 * Close any file descriptors 0..2 that reference procfs,
673 * then make sure file descriptors 0..2 are in use.
675 * setugidsafety() may call closef() and then pfind()
676 * which may grab the process lock.
677 * fdcheckstd() may call falloc() which may block to
678 * allocate memory, so temporarily drop the process lock.
681 VOP_UNLOCK(imgp->vp, 0);
683 error = fdcheckstd(td);
684 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
689 * Set the new credentials.
691 if (attr.va_mode & S_ISUID)
692 change_euid(newcred, euip);
693 if (attr.va_mode & S_ISGID)
694 change_egid(newcred, attr.va_gid);
696 if (will_transition) {
697 mac_vnode_execve_transition(oldcred, newcred, imgp->vp,
698 interpvplabel, imgp);
702 * Implement correct POSIX saved-id behavior.
704 * XXXMAC: Note that the current logic will save the
705 * uid and gid if a MAC domain transition occurs, even
706 * though maybe it shouldn't.
708 change_svuid(newcred, newcred->cr_uid);
709 change_svgid(newcred, newcred->cr_gid);
710 p->p_ucred = newcred;
713 if (oldcred->cr_uid == oldcred->cr_ruid &&
714 oldcred->cr_gid == oldcred->cr_rgid)
715 p->p_flag &= ~P_SUGID;
717 * Implement correct POSIX saved-id behavior.
719 * XXX: It's not clear that the existing behavior is
720 * POSIX-compliant. A number of sources indicate that the
721 * saved uid/gid should only be updated if the new ruid is
722 * not equal to the old ruid, or the new euid is not equal
723 * to the old euid and the new euid is not equal to the old
724 * ruid. The FreeBSD code always updates the saved uid/gid.
725 * Also, this code uses the new (replaced) euid and egid as
726 * the source, which may or may not be the right ones to use.
728 if (oldcred->cr_svuid != oldcred->cr_uid ||
729 oldcred->cr_svgid != oldcred->cr_gid) {
730 change_svuid(newcred, newcred->cr_uid);
731 change_svgid(newcred, newcred->cr_gid);
732 p->p_ucred = newcred;
738 * Store the vp for use in procfs. This vnode was referenced prior
739 * to locking the proc lock.
741 textvp = p->p_textvp;
746 * Tell the DTrace fasttrap provider about the exec if it
747 * has declared an interest.
749 if (dtrace_fasttrap_exec)
750 dtrace_fasttrap_exec(p);
754 * Notify others that we exec'd, and clear the P_INEXEC flag
755 * as we're now a bona fide freshly-execed process.
757 KNOTE_LOCKED(&p->p_klist, NOTE_EXEC);
758 p->p_flag &= ~P_INEXEC;
760 /* clear "fork but no exec" flag, as we _are_ execing */
761 p->p_acflag &= ~AFORK;
764 * Free any previous argument cache and replace it with
765 * the new argument cache, if any.
773 * Check if system-wide sampling is in effect or if the
774 * current process is using PMCs. If so, do exec() time
775 * processing. This processing needs to happen AFTER the
776 * P_INEXEC flag is cleared.
778 * The proc lock needs to be released before taking the PMC
781 if (PMC_SYSTEM_SAMPLING_ACTIVE() || PMC_PROC_IS_USING_PMCS(p)) {
783 VOP_UNLOCK(imgp->vp, 0);
784 pe.pm_credentialschanged = credential_changing;
785 pe.pm_entryaddr = imgp->entry_addr;
787 PMC_CALL_HOOK_X(td, PMC_FN_PROCESS_EXEC, (void *) &pe);
788 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
791 #else /* !HWPMC_HOOKS */
795 /* Set values passed into the program in registers. */
796 if (p->p_sysent->sv_setregs)
797 (*p->p_sysent->sv_setregs)(td, imgp->entry_addr,
798 (u_long)(uintptr_t)stack_base, imgp->ps_strings);
800 exec_setregs(td, imgp->entry_addr,
801 (u_long)(uintptr_t)stack_base, imgp->ps_strings);
803 vfs_mark_atime(imgp->vp, td->td_ucred);
805 SDT_PROBE(proc, kernel, , exec_success, args->fname, 0, 0, 0, 0);
809 * Free any resources malloc'd earlier that we didn't use.
816 VOP_UNLOCK(imgp->vp, 0);
819 * Handle deferred decrement of ref counts.
821 if (textvp != NULL) {
824 tvfslocked = VFS_LOCK_GIANT(textvp->v_mount);
826 VFS_UNLOCK_GIANT(tvfslocked);
828 if (binvp && error != 0)
831 if (tracevp != NULL) {
834 tvfslocked = VFS_LOCK_GIANT(tracevp->v_mount);
836 VFS_UNLOCK_GIANT(tvfslocked);
838 if (tracecred != NULL)
841 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
844 if (oldsigacts != NULL)
845 sigacts_free(oldsigacts);
850 * free various allocated resources
852 if (imgp->firstpage != NULL)
853 exec_unmap_first_page(imgp);
855 if (imgp->vp != NULL) {
857 NDFREE(&nd, NDF_ONLY_PNBUF);
859 VOP_CLOSE(imgp->vp, FREAD, td->td_ucred, td);
863 if (imgp->object != NULL)
864 vm_object_deallocate(imgp->object);
866 free(imgp->freepath, M_TEMP);
870 td->td_dbgflags |= TDB_EXEC;
874 * Stop the process here if its stop event mask has
875 * the S_EXEC bit set.
877 STOPEVENT(p, S_EXEC, 0);
882 /* we're done here, clear P_INEXEC */
884 p->p_flag &= ~P_INEXEC;
887 SDT_PROBE(proc, kernel, , exec_failure, error, 0, 0, 0, 0);
891 mac_execve_exit(imgp);
892 mac_execve_interpreter_exit(interpvplabel);
894 VFS_UNLOCK_GIANT(vfslocked);
895 exec_free_args(args);
897 if (error && imgp->vmspace_destroyed) {
898 /* sorry, no more process anymore. exit gracefully */
899 exit1(td, W_EXITCODE(0, SIGABRT));
912 exec_map_first_page(imgp)
913 struct image_params *imgp;
917 vm_page_t ma[VM_INITIAL_PAGEIN];
920 if (imgp->firstpage != NULL)
921 exec_unmap_first_page(imgp);
923 object = imgp->vp->v_object;
926 VM_OBJECT_LOCK(object);
927 #if VM_NRESERVLEVEL > 0
928 if ((object->flags & OBJ_COLORED) == 0) {
929 object->flags |= OBJ_COLORED;
930 object->pg_color = 0;
933 ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
934 if (ma[0]->valid != VM_PAGE_BITS_ALL) {
935 initial_pagein = VM_INITIAL_PAGEIN;
936 if (initial_pagein > object->size)
937 initial_pagein = object->size;
938 for (i = 1; i < initial_pagein; i++) {
939 if ((ma[i] = vm_page_next(ma[i - 1])) != NULL) {
942 if ((ma[i]->oflags & VPO_BUSY) || ma[i]->busy)
946 ma[i] = vm_page_alloc(object, i,
947 VM_ALLOC_NORMAL | VM_ALLOC_IFNOTCACHED);
953 rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
954 ma[0] = vm_page_lookup(object, 0);
955 if ((rv != VM_PAGER_OK) || (ma[0] == NULL)) {
957 vm_page_lock_queues();
959 vm_page_unlock_queues();
961 VM_OBJECT_UNLOCK(object);
965 vm_page_lock_queues();
967 vm_page_unlock_queues();
968 vm_page_wakeup(ma[0]);
969 VM_OBJECT_UNLOCK(object);
971 imgp->firstpage = sf_buf_alloc(ma[0], 0);
972 imgp->image_header = (char *)sf_buf_kva(imgp->firstpage);
978 exec_unmap_first_page(imgp)
979 struct image_params *imgp;
983 if (imgp->firstpage != NULL) {
984 m = sf_buf_page(imgp->firstpage);
985 sf_buf_free(imgp->firstpage);
986 imgp->firstpage = NULL;
987 vm_page_lock_queues();
989 vm_page_unlock_queues();
994 * Destroy old address space, and allocate a new stack
995 * The new stack is only SGROWSIZ large because it is grown
996 * automatically in trap.c.
999 exec_new_vmspace(imgp, sv)
1000 struct image_params *imgp;
1001 struct sysentvec *sv;
1004 struct proc *p = imgp->proc;
1005 struct vmspace *vmspace = p->p_vmspace;
1006 vm_offset_t sv_minuser, stack_addr;
1010 imgp->vmspace_destroyed = 1;
1013 /* May be called with Giant held */
1014 EVENTHANDLER_INVOKE(process_exec, p, imgp);
1017 * Blow away entire process VM, if address space not shared,
1018 * otherwise, create a new VM space so that other threads are
1021 map = &vmspace->vm_map;
1023 sv_minuser = sv->sv_minuser;
1025 sv_minuser = MAX(sv->sv_minuser, PAGE_SIZE);
1026 if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv_minuser &&
1027 vm_map_max(map) == sv->sv_maxuser) {
1029 pmap_remove_pages(vmspace_pmap(vmspace));
1030 vm_map_remove(map, vm_map_min(map), vm_map_max(map));
1032 error = vmspace_exec(p, sv_minuser, sv->sv_maxuser);
1035 vmspace = p->p_vmspace;
1036 map = &vmspace->vm_map;
1039 /* Allocate a new stack */
1040 if (sv->sv_maxssiz != NULL)
1041 ssiz = *sv->sv_maxssiz;
1044 stack_addr = sv->sv_usrstack - ssiz;
1045 error = vm_map_stack(map, stack_addr, (vm_size_t)ssiz,
1046 sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN);
1051 /* Allocate a new register stack */
1052 stack_addr = IA64_BACKINGSTORE;
1053 error = vm_map_stack(map, stack_addr, (vm_size_t)ssiz,
1054 sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP);
1059 /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
1060 * VM_STACK case, but they are still used to monitor the size of the
1061 * process stack so we can check the stack rlimit.
1063 vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
1064 vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - ssiz;
1070 * Copy out argument and environment strings from the old process address
1071 * space into the temporary string buffer.
1074 exec_copyin_args(struct image_args *args, char *fname,
1075 enum uio_seg segflg, char **argv, char **envv)
1081 bzero(args, sizeof(*args));
1085 * Allocate temporary demand zeroed space for argument and
1086 * environment strings:
1088 * o ARG_MAX for argument and environment;
1089 * o MAXSHELLCMDLEN for the name of interpreters.
1091 args->buf = (char *) kmem_alloc_wait(exec_map,
1092 PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
1093 if (args->buf == NULL)
1095 args->begin_argv = args->buf;
1096 args->endp = args->begin_argv;
1097 args->stringspace = ARG_MAX;
1099 * Copy the file name.
1101 if (fname != NULL) {
1102 args->fname = args->buf + ARG_MAX;
1103 error = (segflg == UIO_SYSSPACE) ?
1104 copystr(fname, args->fname, PATH_MAX, &length) :
1105 copyinstr(fname, args->fname, PATH_MAX, &length);
1112 * extract arguments first
1114 while ((argp = (caddr_t) (intptr_t) fuword(argv++))) {
1115 if (argp == (caddr_t) -1) {
1119 if ((error = copyinstr(argp, args->endp,
1120 args->stringspace, &length))) {
1121 if (error == ENAMETOOLONG)
1125 args->stringspace -= length;
1126 args->endp += length;
1130 args->begin_envv = args->endp;
1133 * extract environment strings
1136 while ((envp = (caddr_t)(intptr_t)fuword(envv++))) {
1137 if (envp == (caddr_t)-1) {
1141 if ((error = copyinstr(envp, args->endp,
1142 args->stringspace, &length))) {
1143 if (error == ENAMETOOLONG)
1147 args->stringspace -= length;
1148 args->endp += length;
1156 exec_free_args(args);
1161 exec_free_args(struct image_args *args)
1165 kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
1166 PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
1172 * Copy strings out to the new process address space, constructing new arg
1173 * and env vector tables. Return a pointer to the base so that it can be used
1174 * as the initial stack pointer.
1177 exec_copyout_strings(imgp)
1178 struct image_params *imgp;
1182 char *stringp, *destp;
1183 register_t *stack_base;
1184 struct ps_strings *arginfo;
1186 size_t execpath_len;
1190 * Calculate string base and vector table pointers.
1191 * Also deal with signal trampoline code for this exec type.
1193 if (imgp->execpath != NULL && imgp->auxargs != NULL)
1194 execpath_len = strlen(imgp->execpath) + 1;
1199 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
1200 if (p->p_sysent->sv_szsigcode != NULL)
1201 szsigcode = *(p->p_sysent->sv_szsigcode);
1202 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
1203 roundup(execpath_len, sizeof(char *)) -
1204 roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
1210 copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo -
1211 szsigcode), szsigcode);
1214 * Copy the image path for the rtld.
1216 if (execpath_len != 0) {
1217 imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len;
1218 copyout(imgp->execpath, (void *)imgp->execpathp,
1223 * If we have a valid auxargs ptr, prepare some room
1226 if (imgp->auxargs) {
1228 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
1229 * lower compatibility.
1231 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
1234 * The '+ 2' is for the null pointers at the end of each of
1235 * the arg and env vector sets,and imgp->auxarg_size is room
1236 * for argument of Runtime loader.
1238 vectp = (char **)(destp - (imgp->args->argc +
1239 imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
1243 * The '+ 2' is for the null pointers at the end of each of
1244 * the arg and env vector sets
1246 vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc + 2) *
1251 * vectp also becomes our initial stack base
1253 stack_base = (register_t *)vectp;
1255 stringp = imgp->args->begin_argv;
1256 argc = imgp->args->argc;
1257 envc = imgp->args->envc;
1260 * Copy out strings - arguments and environment.
1262 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
1265 * Fill in "ps_strings" struct for ps, w, etc.
1267 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
1268 suword(&arginfo->ps_nargvstr, argc);
1271 * Fill in argument portion of vector table.
1273 for (; argc > 0; --argc) {
1274 suword(vectp++, (long)(intptr_t)destp);
1275 while (*stringp++ != 0)
1280 /* a null vector table pointer separates the argp's from the envp's */
1283 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
1284 suword(&arginfo->ps_nenvstr, envc);
1287 * Fill in environment portion of vector table.
1289 for (; envc > 0; --envc) {
1290 suword(vectp++, (long)(intptr_t)destp);
1291 while (*stringp++ != 0)
1296 /* end of vector table is a null pointer */
1299 return (stack_base);
1303 * Check permissions of file to execute.
1304 * Called with imgp->vp locked.
1305 * Return 0 for success or error code on failure.
1308 exec_check_permissions(imgp)
1309 struct image_params *imgp;
1311 struct vnode *vp = imgp->vp;
1312 struct vattr *attr = imgp->attr;
1318 /* Get file attributes */
1319 error = VOP_GETATTR(vp, attr, td->td_ucred);
1324 error = mac_vnode_check_exec(td->td_ucred, imgp->vp, imgp);
1330 * 1) Check if file execution is disabled for the filesystem that this
1332 * 2) Insure that at least one execute bit is on - otherwise root
1333 * will always succeed, and we don't want to happen unless the
1334 * file really is executable.
1335 * 3) Insure that the file is a regular file.
1337 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
1338 ((attr->va_mode & 0111) == 0) ||
1339 (attr->va_type != VREG))
1343 * Zero length files can't be exec'd
1345 if (attr->va_size == 0)
1349 * Check for execute permission to file based on current credentials.
1351 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
1356 * Check number of open-for-writes on the file and deny execution
1359 if (vp->v_writecount)
1363 * Call filesystem specific open routine (which does nothing in the
1366 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
1373 * Exec handler registration
1376 exec_register(execsw_arg)
1377 const struct execsw *execsw_arg;
1379 const struct execsw **es, **xs, **newexecsw;
1380 int count = 2; /* New slot and trailing NULL */
1383 for (es = execsw; *es; es++)
1385 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1386 if (newexecsw == NULL)
1390 for (es = execsw; *es; es++)
1395 free(execsw, M_TEMP);
1401 exec_unregister(execsw_arg)
1402 const struct execsw *execsw_arg;
1404 const struct execsw **es, **xs, **newexecsw;
1408 panic("unregister with no handlers left?\n");
1410 for (es = execsw; *es; es++) {
1411 if (*es == execsw_arg)
1416 for (es = execsw; *es; es++)
1417 if (*es != execsw_arg)
1419 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1420 if (newexecsw == NULL)
1423 for (es = execsw; *es; es++)
1424 if (*es != execsw_arg)
1428 free(execsw, M_TEMP);