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>
58 #include <sys/sf_buf.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/sysent.h>
62 #include <sys/sysctl.h>
63 #include <sys/vnode.h>
66 #include <sys/ktrace.h>
70 #include <vm/vm_param.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_map.h>
74 #include <vm/vm_kern.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_object.h>
77 #include <vm/vm_pager.h>
80 #include <sys/pmckern.h>
83 #include <machine/reg.h>
85 #include <security/audit/audit.h>
86 #include <security/mac/mac_framework.h>
89 #include <sys/dtrace_bsd.h>
90 dtrace_execexit_func_t dtrace_fasttrap_exec;
93 SDT_PROVIDER_DECLARE(proc);
94 SDT_PROBE_DEFINE(proc, kernel, , exec);
95 SDT_PROBE_ARGTYPE(proc, kernel, , exec, 0, "char *");
96 SDT_PROBE_DEFINE(proc, kernel, , exec_failure);
97 SDT_PROBE_ARGTYPE(proc, kernel, , exec_failure, 0, "int");
98 SDT_PROBE_DEFINE(proc, kernel, , exec_success);
99 SDT_PROBE_ARGTYPE(proc, kernel, , exec_success, 0, "char *");
101 MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
103 static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS);
104 static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS);
105 static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS);
106 static int do_execve(struct thread *td, struct image_args *args,
108 static void exec_free_args(struct image_args *);
110 /* XXX This should be vm_size_t. */
111 SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD,
112 NULL, 0, sysctl_kern_ps_strings, "LU", "");
114 /* XXX This should be vm_size_t. */
115 SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD,
116 NULL, 0, sysctl_kern_usrstack, "LU", "");
118 SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD,
119 NULL, 0, sysctl_kern_stackprot, "I", "");
121 u_long ps_arg_cache_limit = PAGE_SIZE / 16;
122 SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
123 &ps_arg_cache_limit, 0, "");
125 static int map_at_zero = 0;
126 TUNABLE_INT("security.bsd.map_at_zero", &map_at_zero);
127 SYSCTL_INT(_security_bsd, OID_AUTO, map_at_zero, CTLFLAG_RW, &map_at_zero, 0,
128 "Permit processes to map an object at virtual address 0.");
131 sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS)
138 if (req->flags & SCTL_MASK32) {
140 val = (unsigned int)p->p_sysent->sv_psstrings;
141 error = SYSCTL_OUT(req, &val, sizeof(val));
144 error = SYSCTL_OUT(req, &p->p_sysent->sv_psstrings,
145 sizeof(p->p_sysent->sv_psstrings));
150 sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS)
157 if (req->flags & SCTL_MASK32) {
159 val = (unsigned int)p->p_sysent->sv_usrstack;
160 error = SYSCTL_OUT(req, &val, sizeof(val));
163 error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack,
164 sizeof(p->p_sysent->sv_usrstack));
169 sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS)
174 return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot,
175 sizeof(p->p_sysent->sv_stackprot)));
179 * Each of the items is a pointer to a `const struct execsw', hence the
180 * double pointer here.
182 static const struct execsw **execsw;
184 #ifndef _SYS_SYSPROTO_H_
195 struct execve_args /* {
202 struct image_args args;
204 error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
205 uap->argv, uap->envv);
207 error = kern_execve(td, &args, NULL);
211 #ifndef _SYS_SYSPROTO_H_
212 struct fexecve_args {
219 fexecve(struct thread *td, struct fexecve_args *uap)
222 struct image_args args;
224 error = exec_copyin_args(&args, NULL, UIO_SYSSPACE,
225 uap->argv, uap->envv);
228 error = kern_execve(td, &args, NULL);
233 #ifndef _SYS_SYSPROTO_H_
234 struct __mac_execve_args {
243 __mac_execve(td, uap)
245 struct __mac_execve_args /* {
254 struct image_args args;
256 error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
257 uap->argv, uap->envv);
259 error = kern_execve(td, &args, uap->mac_p);
267 * XXX: kern_execve has the astonishing property of not always returning to
268 * the caller. If sufficiently bad things happen during the call to
269 * do_execve(), it can end up calling exit1(); as a result, callers must
270 * avoid doing anything which they might need to undo (e.g., allocating
274 kern_execve(td, args, mac_p)
276 struct image_args *args;
279 struct proc *p = td->td_proc;
282 AUDIT_ARG_ARGV(args->begin_argv, args->argc,
283 args->begin_envv - args->begin_argv);
284 AUDIT_ARG_ENVV(args->begin_envv, args->envc,
285 args->endp - args->begin_envv);
286 if (p->p_flag & P_HADTHREADS) {
288 if (thread_single(SINGLE_BOUNDARY)) {
290 exec_free_args(args);
291 return (ERESTART); /* Try again later. */
296 error = do_execve(td, args, mac_p);
298 if (p->p_flag & P_HADTHREADS) {
301 * If success, we upgrade to SINGLE_EXIT state to
302 * force other threads to suicide.
305 thread_single(SINGLE_EXIT);
315 * In-kernel implementation of execve(). All arguments are assumed to be
316 * userspace pointers from the passed thread.
319 do_execve(td, args, mac_p)
321 struct image_args *args;
324 struct proc *p = td->td_proc;
326 struct ucred *newcred = NULL, *oldcred;
327 struct uidinfo *euip;
328 register_t *stack_base;
329 int error, len = 0, i;
330 struct image_params image_params, *imgp;
332 int (*img_first)(struct image_params *);
333 struct pargs *oldargs = NULL, *newargs = NULL;
334 struct sigacts *oldsigacts, *newsigacts;
336 struct vnode *tracevp = NULL;
337 struct ucred *tracecred = NULL;
339 struct vnode *textvp = NULL, *binvp = NULL;
340 int credential_changing;
344 struct label *interpvplabel = NULL;
348 struct pmckern_procexec pe;
350 static const char fexecv_proc_title[] = "(fexecv)";
353 imgp = &image_params;
356 * Lock the process and set the P_INEXEC flag to indicate that
357 * it should be left alone until we're done here. This is
358 * necessary to avoid race conditions - e.g. in ptrace() -
359 * that might allow a local user to illicitly obtain elevated
363 KASSERT((p->p_flag & P_INEXEC) == 0,
364 ("%s(): process already has P_INEXEC flag", __func__));
365 p->p_flag |= P_INEXEC;
369 * Initialize part of the common data
372 imgp->execlabel = NULL;
374 imgp->entry_addr = 0;
375 imgp->vmspace_destroyed = 0;
376 imgp->interpreted = 0;
378 imgp->interpreter_name = args->buf + PATH_MAX + ARG_MAX;
379 imgp->auxargs = NULL;
382 imgp->firstpage = NULL;
383 imgp->ps_strings = 0;
384 imgp->auxarg_size = 0;
386 imgp->execpath = imgp->freepath = NULL;
390 error = mac_execve_enter(imgp, mac_p);
395 imgp->image_header = NULL;
398 * Translate the file name. namei() returns a vnode pointer
399 * in ni_vp amoung other things.
401 * XXXAUDIT: It would be desirable to also audit the name of the
402 * interpreter if this is an interpreted binary.
404 if (args->fname != NULL) {
405 NDINIT(&nd, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME
406 | MPSAFE | AUDITVNODE1, UIO_SYSSPACE, args->fname, td);
409 SDT_PROBE(proc, kernel, , exec, args->fname, 0, 0, 0, 0 );
412 if (args->fname != NULL) {
417 vfslocked = NDHASGIANT(&nd);
421 AUDIT_ARG_FD(args->fd);
422 error = fgetvp(td, args->fd, &binvp);
425 vfslocked = VFS_LOCK_GIANT(binvp->v_mount);
426 vn_lock(binvp, LK_EXCLUSIVE | LK_RETRY);
427 AUDIT_ARG_VNODE1(binvp);
432 * Check file permissions (also 'opens' file)
434 error = exec_check_permissions(imgp);
436 goto exec_fail_dealloc;
438 imgp->object = imgp->vp->v_object;
439 if (imgp->object != NULL)
440 vm_object_reference(imgp->object);
443 * Set VV_TEXT now so no one can write to the executable while we're
446 * Remember if this was set before and unset it in case this is not
447 * actually an executable image.
449 textset = imgp->vp->v_vflag & VV_TEXT;
450 imgp->vp->v_vflag |= VV_TEXT;
452 error = exec_map_first_page(imgp);
454 goto exec_fail_dealloc;
456 imgp->proc->p_osrel = 0;
458 * If the current process has a special image activator it
459 * wants to try first, call it. For example, emulating shell
460 * scripts differently.
463 if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
464 error = img_first(imgp);
467 * Loop through the list of image activators, calling each one.
468 * An activator returns -1 if there is no match, 0 on success,
469 * and an error otherwise.
471 for (i = 0; error == -1 && execsw[i]; ++i) {
472 if (execsw[i]->ex_imgact == NULL ||
473 execsw[i]->ex_imgact == img_first) {
476 error = (*execsw[i]->ex_imgact)(imgp);
482 imgp->vp->v_vflag &= ~VV_TEXT;
485 goto exec_fail_dealloc;
489 * Special interpreter operation, cleanup and loop up to try to
490 * activate the interpreter.
492 if (imgp->interpreted) {
493 exec_unmap_first_page(imgp);
495 * VV_TEXT needs to be unset for scripts. There is a short
496 * period before we determine that something is a script where
497 * VV_TEXT will be set. The vnode lock is held over this
498 * entire period so nothing should illegitimately be blocked.
500 imgp->vp->v_vflag &= ~VV_TEXT;
501 /* free name buffer and old vnode */
502 if (args->fname != NULL)
503 NDFREE(&nd, NDF_ONLY_PNBUF);
505 mac_execve_interpreter_enter(binvp, &interpvplabel);
508 VOP_CLOSE(binvp, FREAD, td->td_ucred, td);
512 vm_object_deallocate(imgp->object);
514 VFS_UNLOCK_GIANT(vfslocked);
516 /* set new name to that of the interpreter */
517 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME | MPSAFE,
518 UIO_SYSSPACE, imgp->interpreter_name, td);
519 args->fname = imgp->interpreter_name;
524 * NB: We unlock the vnode here because it is believed that none
525 * of the sv_copyout_strings/sv_fixup operations require the vnode.
527 VOP_UNLOCK(imgp->vp, 0);
530 * Do the best to calculate the full path to the image file.
532 if (imgp->auxargs != NULL &&
533 ((args->fname != NULL && args->fname[0] == '/') ||
534 vn_fullpath(td, imgp->vp, &imgp->execpath, &imgp->freepath) != 0))
535 imgp->execpath = args->fname;
538 * Copy out strings (args and env) and initialize stack base
540 if (p->p_sysent->sv_copyout_strings)
541 stack_base = (*p->p_sysent->sv_copyout_strings)(imgp);
543 stack_base = exec_copyout_strings(imgp);
546 * If custom stack fixup routine present for this process
547 * let it do the stack setup.
548 * Else stuff argument count as first item on stack
550 if (p->p_sysent->sv_fixup != NULL)
551 (*p->p_sysent->sv_fixup)(&stack_base, imgp);
553 suword(--stack_base, imgp->args->argc);
556 * For security and other reasons, the file descriptor table cannot
557 * be shared after an exec.
562 * Malloc things before we need locks.
565 euip = uifind(attr.va_uid);
566 i = imgp->args->begin_envv - imgp->args->begin_argv;
567 /* Cache arguments if they fit inside our allowance */
568 if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
569 newargs = pargs_alloc(i);
570 bcopy(imgp->args->begin_argv, newargs->ar_args, i);
573 /* close files on exec */
575 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
577 /* Get a reference to the vnode prior to locking the proc */
581 * For security and other reasons, signal handlers cannot
582 * be shared after an exec. The new process gets a copy of the old
583 * handlers. In execsigs(), the new process will have its signals
587 oldcred = crcopysafe(p, newcred);
588 if (sigacts_shared(p->p_sigacts)) {
589 oldsigacts = p->p_sigacts;
591 newsigacts = sigacts_alloc();
592 sigacts_copy(newsigacts, oldsigacts);
594 p->p_sigacts = newsigacts;
601 /* reset caught signals */
604 /* name this process - nameiexec(p, ndp) */
606 len = min(nd.ni_cnd.cn_namelen,MAXCOMLEN);
607 bcopy(nd.ni_cnd.cn_nameptr, p->p_comm, len);
609 if (vn_commname(binvp, p->p_comm, MAXCOMLEN + 1) == 0)
612 len = sizeof(fexecv_proc_title);
613 bcopy(fexecv_proc_title, p->p_comm, len);
617 bcopy(p->p_comm, td->td_name, sizeof(td->td_name));
620 * mark as execed, wakeup the process that vforked (if any) and tell
621 * it that it now has its own resources back
624 if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
625 p->p_flag &= ~P_PPWAIT;
626 cv_broadcast(&p->p_pwait);
630 * Implement image setuid/setgid.
632 * Don't honor setuid/setgid if the filesystem prohibits it or if
633 * the process is being traced.
635 * XXXMAC: For the time being, use NOSUID to also prohibit
636 * transitions on the file system.
638 credential_changing = 0;
639 credential_changing |= (attr.va_mode & S_ISUID) && oldcred->cr_uid !=
641 credential_changing |= (attr.va_mode & S_ISGID) && oldcred->cr_gid !=
644 will_transition = mac_vnode_execve_will_transition(oldcred, imgp->vp,
645 interpvplabel, imgp);
646 credential_changing |= will_transition;
649 if (credential_changing &&
650 (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
651 (p->p_flag & P_TRACED) == 0) {
653 * Turn off syscall tracing for set-id programs, except for
654 * root. Record any set-id flags first to make sure that
655 * we do not regain any tracing during a possible block.
660 if (p->p_tracevp != NULL &&
661 priv_check_cred(oldcred, PRIV_DEBUG_DIFFCRED, 0)) {
662 mtx_lock(&ktrace_mtx);
664 tracevp = p->p_tracevp;
666 tracecred = p->p_tracecred;
667 p->p_tracecred = NULL;
668 mtx_unlock(&ktrace_mtx);
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;
761 * If tracing the process, trap to debugger so breakpoints
762 * can be set before the program executes.
763 * Use tdsignal to deliver signal to current thread, use
764 * psignal may cause the signal to be delivered to wrong thread
765 * because that thread will exit, remember we are going to enter
766 * single thread mode.
768 if (p->p_flag & P_TRACED)
769 tdsignal(p, td, SIGTRAP, NULL);
771 /* clear "fork but no exec" flag, as we _are_ execing */
772 p->p_acflag &= ~AFORK;
775 * Free any previous argument cache and replace it with
776 * the new argument cache, if any.
784 * Check if system-wide sampling is in effect or if the
785 * current process is using PMCs. If so, do exec() time
786 * processing. This processing needs to happen AFTER the
787 * P_INEXEC flag is cleared.
789 * The proc lock needs to be released before taking the PMC
792 if (PMC_SYSTEM_SAMPLING_ACTIVE() || PMC_PROC_IS_USING_PMCS(p)) {
794 VOP_UNLOCK(imgp->vp, 0);
795 pe.pm_credentialschanged = credential_changing;
796 pe.pm_entryaddr = imgp->entry_addr;
798 PMC_CALL_HOOK_X(td, PMC_FN_PROCESS_EXEC, (void *) &pe);
799 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
802 #else /* !HWPMC_HOOKS */
806 /* Set values passed into the program in registers. */
807 if (p->p_sysent->sv_setregs)
808 (*p->p_sysent->sv_setregs)(td, imgp->entry_addr,
809 (u_long)(uintptr_t)stack_base, imgp->ps_strings);
811 exec_setregs(td, imgp->entry_addr,
812 (u_long)(uintptr_t)stack_base, imgp->ps_strings);
814 vfs_mark_atime(imgp->vp, td->td_ucred);
816 SDT_PROBE(proc, kernel, , exec_success, args->fname, 0, 0, 0, 0);
820 * Free any resources malloc'd earlier that we didn't use.
827 VOP_UNLOCK(imgp->vp, 0);
830 * Handle deferred decrement of ref counts.
832 if (textvp != NULL) {
835 tvfslocked = VFS_LOCK_GIANT(textvp->v_mount);
837 VFS_UNLOCK_GIANT(tvfslocked);
839 if (binvp && error != 0)
842 if (tracevp != NULL) {
845 tvfslocked = VFS_LOCK_GIANT(tracevp->v_mount);
847 VFS_UNLOCK_GIANT(tvfslocked);
849 if (tracecred != NULL)
852 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
855 if (oldsigacts != NULL)
856 sigacts_free(oldsigacts);
861 * free various allocated resources
863 if (imgp->firstpage != NULL)
864 exec_unmap_first_page(imgp);
866 if (imgp->vp != NULL) {
868 NDFREE(&nd, NDF_ONLY_PNBUF);
870 VOP_CLOSE(imgp->vp, FREAD, td->td_ucred, td);
874 if (imgp->object != NULL)
875 vm_object_deallocate(imgp->object);
877 free(imgp->freepath, M_TEMP);
881 * Stop the process here if its stop event mask has
882 * the S_EXEC bit set.
884 STOPEVENT(p, S_EXEC, 0);
889 /* we're done here, clear P_INEXEC */
891 p->p_flag &= ~P_INEXEC;
894 SDT_PROBE(proc, kernel, , exec_failure, error, 0, 0, 0, 0);
898 mac_execve_exit(imgp);
899 mac_execve_interpreter_exit(interpvplabel);
901 VFS_UNLOCK_GIANT(vfslocked);
902 exec_free_args(args);
904 if (error && imgp->vmspace_destroyed) {
905 /* sorry, no more process anymore. exit gracefully */
906 exit1(td, W_EXITCODE(0, SIGABRT));
913 exec_map_first_page(imgp)
914 struct image_params *imgp;
918 vm_page_t ma[VM_INITIAL_PAGEIN];
921 if (imgp->firstpage != NULL)
922 exec_unmap_first_page(imgp);
924 object = imgp->vp->v_object;
927 VM_OBJECT_LOCK(object);
928 #if VM_NRESERVLEVEL > 0
929 if ((object->flags & OBJ_COLORED) == 0) {
930 object->flags |= OBJ_COLORED;
931 object->pg_color = 0;
934 ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
935 if (ma[0]->valid != VM_PAGE_BITS_ALL) {
936 initial_pagein = VM_INITIAL_PAGEIN;
937 if (initial_pagein > object->size)
938 initial_pagein = object->size;
939 for (i = 1; i < initial_pagein; i++) {
940 if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
943 if ((ma[i]->oflags & VPO_BUSY) || ma[i]->busy)
947 ma[i] = vm_page_alloc(object, i,
948 VM_ALLOC_NORMAL | VM_ALLOC_IFNOTCACHED);
954 rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
955 ma[0] = vm_page_lookup(object, 0);
956 if ((rv != VM_PAGER_OK) || (ma[0] == NULL)) {
958 vm_page_lock_queues();
960 vm_page_unlock_queues();
962 VM_OBJECT_UNLOCK(object);
966 vm_page_lock_queues();
968 vm_page_unlock_queues();
969 vm_page_wakeup(ma[0]);
970 VM_OBJECT_UNLOCK(object);
972 imgp->firstpage = sf_buf_alloc(ma[0], 0);
973 imgp->image_header = (char *)sf_buf_kva(imgp->firstpage);
979 exec_unmap_first_page(imgp)
980 struct image_params *imgp;
984 if (imgp->firstpage != NULL) {
985 m = sf_buf_page(imgp->firstpage);
986 sf_buf_free(imgp->firstpage);
987 imgp->firstpage = NULL;
988 vm_page_lock_queues();
990 vm_page_unlock_queues();
995 * Destroy old address space, and allocate a new stack
996 * The new stack is only SGROWSIZ large because it is grown
997 * automatically in trap.c.
1000 exec_new_vmspace(imgp, sv)
1001 struct image_params *imgp;
1002 struct sysentvec *sv;
1005 struct proc *p = imgp->proc;
1006 struct vmspace *vmspace = p->p_vmspace;
1007 vm_offset_t sv_minuser, stack_addr;
1011 imgp->vmspace_destroyed = 1;
1014 /* May be called with Giant held */
1015 EVENTHANDLER_INVOKE(process_exec, p, imgp);
1018 * Blow away entire process VM, if address space not shared,
1019 * otherwise, create a new VM space so that other threads are
1022 map = &vmspace->vm_map;
1024 sv_minuser = sv->sv_minuser;
1026 sv_minuser = MAX(sv->sv_minuser, PAGE_SIZE);
1027 if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv_minuser &&
1028 vm_map_max(map) == sv->sv_maxuser) {
1030 pmap_remove_pages(vmspace_pmap(vmspace));
1031 vm_map_remove(map, vm_map_min(map), vm_map_max(map));
1033 error = vmspace_exec(p, sv_minuser, sv->sv_maxuser);
1036 vmspace = p->p_vmspace;
1037 map = &vmspace->vm_map;
1040 /* Allocate a new stack */
1041 if (sv->sv_maxssiz != NULL)
1042 ssiz = *sv->sv_maxssiz;
1045 stack_addr = sv->sv_usrstack - ssiz;
1046 error = vm_map_stack(map, stack_addr, (vm_size_t)ssiz,
1047 sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN);
1052 /* Allocate a new register stack */
1053 stack_addr = IA64_BACKINGSTORE;
1054 error = vm_map_stack(map, stack_addr, (vm_size_t)ssiz,
1055 sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP);
1060 /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
1061 * VM_STACK case, but they are still used to monitor the size of the
1062 * process stack so we can check the stack rlimit.
1064 vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
1065 vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - ssiz;
1071 * Copy out argument and environment strings from the old process address
1072 * space into the temporary string buffer.
1075 exec_copyin_args(struct image_args *args, char *fname,
1076 enum uio_seg segflg, char **argv, char **envv)
1082 bzero(args, sizeof(*args));
1086 * Allocate temporary demand zeroed space for argument and
1087 * environment strings:
1089 * o ARG_MAX for argument and environment;
1090 * o MAXSHELLCMDLEN for the name of interpreters.
1092 args->buf = (char *) kmem_alloc_wait(exec_map,
1093 PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
1094 if (args->buf == NULL)
1096 args->begin_argv = args->buf;
1097 args->endp = args->begin_argv;
1098 args->stringspace = ARG_MAX;
1100 * Copy the file name.
1102 if (fname != NULL) {
1103 args->fname = args->buf + ARG_MAX;
1104 error = (segflg == UIO_SYSSPACE) ?
1105 copystr(fname, args->fname, PATH_MAX, &length) :
1106 copyinstr(fname, args->fname, PATH_MAX, &length);
1113 * extract arguments first
1115 while ((argp = (caddr_t) (intptr_t) fuword(argv++))) {
1116 if (argp == (caddr_t) -1) {
1120 if ((error = copyinstr(argp, args->endp,
1121 args->stringspace, &length))) {
1122 if (error == ENAMETOOLONG)
1126 args->stringspace -= length;
1127 args->endp += length;
1131 args->begin_envv = args->endp;
1134 * extract environment strings
1137 while ((envp = (caddr_t)(intptr_t)fuword(envv++))) {
1138 if (envp == (caddr_t)-1) {
1142 if ((error = copyinstr(envp, args->endp,
1143 args->stringspace, &length))) {
1144 if (error == ENAMETOOLONG)
1148 args->stringspace -= length;
1149 args->endp += length;
1157 exec_free_args(args);
1162 exec_free_args(struct image_args *args)
1166 kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
1167 PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
1173 * Copy strings out to the new process address space, constructing new arg
1174 * and env vector tables. Return a pointer to the base so that it can be used
1175 * as the initial stack pointer.
1178 exec_copyout_strings(imgp)
1179 struct image_params *imgp;
1183 char *stringp, *destp;
1184 register_t *stack_base;
1185 struct ps_strings *arginfo;
1187 size_t execpath_len;
1191 * Calculate string base and vector table pointers.
1192 * Also deal with signal trampoline code for this exec type.
1194 if (imgp->execpath != NULL && imgp->auxargs != NULL)
1195 execpath_len = strlen(imgp->execpath) + 1;
1200 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
1201 if (p->p_sysent->sv_szsigcode != NULL)
1202 szsigcode = *(p->p_sysent->sv_szsigcode);
1203 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
1204 roundup(execpath_len, sizeof(char *)) -
1205 roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
1211 copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo -
1212 szsigcode), szsigcode);
1215 * Copy the image path for the rtld.
1217 if (execpath_len != 0) {
1218 imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len;
1219 copyout(imgp->execpath, (void *)imgp->execpathp,
1224 * If we have a valid auxargs ptr, prepare some room
1227 if (imgp->auxargs) {
1229 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
1230 * lower compatibility.
1232 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
1235 * The '+ 2' is for the null pointers at the end of each of
1236 * the arg and env vector sets,and imgp->auxarg_size is room
1237 * for argument of Runtime loader.
1239 vectp = (char **)(destp - (imgp->args->argc +
1240 imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
1244 * The '+ 2' is for the null pointers at the end of each of
1245 * the arg and env vector sets
1247 vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc + 2) *
1252 * vectp also becomes our initial stack base
1254 stack_base = (register_t *)vectp;
1256 stringp = imgp->args->begin_argv;
1257 argc = imgp->args->argc;
1258 envc = imgp->args->envc;
1261 * Copy out strings - arguments and environment.
1263 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
1266 * Fill in "ps_strings" struct for ps, w, etc.
1268 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
1269 suword(&arginfo->ps_nargvstr, argc);
1272 * Fill in argument portion of vector table.
1274 for (; argc > 0; --argc) {
1275 suword(vectp++, (long)(intptr_t)destp);
1276 while (*stringp++ != 0)
1281 /* a null vector table pointer separates the argp's from the envp's */
1284 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
1285 suword(&arginfo->ps_nenvstr, envc);
1288 * Fill in environment portion of vector table.
1290 for (; envc > 0; --envc) {
1291 suword(vectp++, (long)(intptr_t)destp);
1292 while (*stringp++ != 0)
1297 /* end of vector table is a null pointer */
1300 return (stack_base);
1304 * Check permissions of file to execute.
1305 * Called with imgp->vp locked.
1306 * Return 0 for success or error code on failure.
1309 exec_check_permissions(imgp)
1310 struct image_params *imgp;
1312 struct vnode *vp = imgp->vp;
1313 struct vattr *attr = imgp->attr;
1319 /* Get file attributes */
1320 error = VOP_GETATTR(vp, attr, td->td_ucred);
1325 error = mac_vnode_check_exec(td->td_ucred, imgp->vp, imgp);
1331 * 1) Check if file execution is disabled for the filesystem that this
1333 * 2) Insure that at least one execute bit is on - otherwise root
1334 * will always succeed, and we don't want to happen unless the
1335 * file really is executable.
1336 * 3) Insure that the file is a regular file.
1338 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
1339 ((attr->va_mode & 0111) == 0) ||
1340 (attr->va_type != VREG))
1344 * Zero length files can't be exec'd
1346 if (attr->va_size == 0)
1350 * Check for execute permission to file based on current credentials.
1352 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
1357 * Check number of open-for-writes on the file and deny execution
1360 if (vp->v_writecount)
1364 * Call filesystem specific open routine (which does nothing in the
1367 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
1374 * Exec handler registration
1377 exec_register(execsw_arg)
1378 const struct execsw *execsw_arg;
1380 const struct execsw **es, **xs, **newexecsw;
1381 int count = 2; /* New slot and trailing NULL */
1384 for (es = execsw; *es; es++)
1386 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1387 if (newexecsw == NULL)
1391 for (es = execsw; *es; es++)
1396 free(execsw, M_TEMP);
1402 exec_unregister(execsw_arg)
1403 const struct execsw *execsw_arg;
1405 const struct execsw **es, **xs, **newexecsw;
1409 panic("unregister with no handlers left?\n");
1411 for (es = execsw; *es; es++) {
1412 if (*es == execsw_arg)
1417 for (es = execsw; *es; es++)
1418 if (*es != execsw_arg)
1420 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1421 if (newexecsw == NULL)
1424 for (es = execsw; *es; es++)
1425 if (*es != execsw_arg)
1429 free(execsw, M_TEMP);