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_ktrace.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/eventhandler.h>
38 #include <sys/mutex.h>
39 #include <sys/sysproto.h>
40 #include <sys/signalvar.h>
41 #include <sys/kernel.h>
42 #include <sys/mount.h>
43 #include <sys/filedesc.h>
44 #include <sys/fcntl.h>
47 #include <sys/imgact.h>
48 #include <sys/imgact_elf.h>
50 #include <sys/malloc.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>
60 #include <sys/sysctl.h>
61 #include <sys/vnode.h>
63 #include <sys/ktrace.h>
67 #include <vm/vm_param.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_extern.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_pager.h>
77 #include <sys/pmckern.h>
80 #include <machine/reg.h>
82 #include <security/audit/audit.h>
83 #include <security/mac/mac_framework.h>
85 MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
87 static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS);
88 static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS);
89 static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS);
90 static int do_execve(struct thread *td, struct image_args *args,
92 static void exec_free_args(struct image_args *);
94 /* XXX This should be vm_size_t. */
95 SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD,
96 NULL, 0, sysctl_kern_ps_strings, "LU", "");
98 /* XXX This should be vm_size_t. */
99 SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD,
100 NULL, 0, sysctl_kern_usrstack, "LU", "");
102 SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD,
103 NULL, 0, sysctl_kern_stackprot, "I", "");
105 u_long ps_arg_cache_limit = PAGE_SIZE / 16;
106 SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
107 &ps_arg_cache_limit, 0, "");
110 sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS)
117 if (req->flags & SCTL_MASK32) {
119 val = (unsigned int)p->p_sysent->sv_psstrings;
120 error = SYSCTL_OUT(req, &val, sizeof(val));
123 error = SYSCTL_OUT(req, &p->p_sysent->sv_psstrings,
124 sizeof(p->p_sysent->sv_psstrings));
129 sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS)
136 if (req->flags & SCTL_MASK32) {
138 val = (unsigned int)p->p_sysent->sv_usrstack;
139 error = SYSCTL_OUT(req, &val, sizeof(val));
142 error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack,
143 sizeof(p->p_sysent->sv_usrstack));
148 sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS)
153 return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot,
154 sizeof(p->p_sysent->sv_stackprot)));
158 * Each of the items is a pointer to a `const struct execsw', hence the
159 * double pointer here.
161 static const struct execsw **execsw;
163 #ifndef _SYS_SYSPROTO_H_
174 struct execve_args /* {
181 struct image_args args;
183 error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
184 uap->argv, uap->envv);
186 error = kern_execve(td, &args, NULL);
190 #ifndef _SYS_SYSPROTO_H_
191 struct __mac_execve_args {
200 __mac_execve(td, uap)
202 struct __mac_execve_args /* {
211 struct image_args args;
213 error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
214 uap->argv, uap->envv);
216 error = kern_execve(td, &args, uap->mac_p);
224 * XXX: kern_execve has the astonishing property of not always returning to
225 * the caller. If sufficiently bad things happen during the call to
226 * do_execve(), it can end up calling exit1(); as a result, callers must
227 * avoid doing anything which they might need to undo (e.g., allocating
231 kern_execve(td, args, mac_p)
233 struct image_args *args;
236 struct proc *p = td->td_proc;
239 AUDIT_ARG(argv, args->begin_argv, args->argc,
240 args->begin_envv - args->begin_argv);
241 AUDIT_ARG(envv, args->begin_envv, args->envc,
242 args->endp - args->begin_envv);
243 if (p->p_flag & P_HADTHREADS) {
245 if (thread_single(SINGLE_BOUNDARY)) {
247 exec_free_args(args);
248 return (ERESTART); /* Try again later. */
253 error = do_execve(td, args, mac_p);
255 if (p->p_flag & P_HADTHREADS) {
258 * If success, we upgrade to SINGLE_EXIT state to
259 * force other threads to suicide.
262 thread_single(SINGLE_EXIT);
272 * In-kernel implementation of execve(). All arguments are assumed to be
273 * userspace pointers from the passed thread.
276 do_execve(td, args, mac_p)
278 struct image_args *args;
281 struct proc *p = td->td_proc;
282 struct nameidata nd, *ndp;
283 struct ucred *newcred = NULL, *oldcred;
284 struct uidinfo *euip;
285 register_t *stack_base;
287 struct image_params image_params, *imgp;
289 int (*img_first)(struct image_params *);
290 struct pargs *oldargs = NULL, *newargs = NULL;
291 struct sigacts *oldsigacts, *newsigacts;
293 struct vnode *tracevp = NULL;
294 struct ucred *tracecred = NULL;
296 struct vnode *textvp = NULL;
297 int credential_changing;
301 struct label *interplabel = NULL;
305 struct pmckern_procexec pe;
309 imgp = &image_params;
312 * Lock the process and set the P_INEXEC flag to indicate that
313 * it should be left alone until we're done here. This is
314 * necessary to avoid race conditions - e.g. in ptrace() -
315 * that might allow a local user to illicitly obtain elevated
319 KASSERT((p->p_flag & P_INEXEC) == 0,
320 ("%s(): process already has P_INEXEC flag", __func__));
321 p->p_flag |= P_INEXEC;
325 * Initialize part of the common data
328 imgp->execlabel = NULL;
330 imgp->entry_addr = 0;
331 imgp->vmspace_destroyed = 0;
332 imgp->interpreted = 0;
333 imgp->interpreter_name = args->buf + PATH_MAX + ARG_MAX;
334 imgp->auxargs = NULL;
337 imgp->firstpage = NULL;
338 imgp->ps_strings = 0;
339 imgp->auxarg_size = 0;
343 error = mac_execve_enter(imgp, mac_p);
348 imgp->image_header = NULL;
351 * Translate the file name. namei() returns a vnode pointer
352 * in ni_vp amoung other things.
354 * XXXAUDIT: It would be desirable to also audit the name of the
355 * interpreter if this is an interpreted binary.
358 NDINIT(ndp, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME | MPSAFE |
359 AUDITVNODE1, UIO_SYSSPACE, args->fname, td);
366 vfslocked = NDHASGIANT(ndp);
367 imgp->vp = ndp->ni_vp;
370 * Check file permissions (also 'opens' file)
372 error = exec_check_permissions(imgp);
374 goto exec_fail_dealloc;
376 imgp->object = imgp->vp->v_object;
377 if (imgp->object != NULL)
378 vm_object_reference(imgp->object);
381 * Set VV_TEXT now so no one can write to the executable while we're
384 * Remember if this was set before and unset it in case this is not
385 * actually an executable image.
387 textset = imgp->vp->v_vflag & VV_TEXT;
388 imgp->vp->v_vflag |= VV_TEXT;
390 error = exec_map_first_page(imgp);
392 goto exec_fail_dealloc;
395 * If the current process has a special image activator it
396 * wants to try first, call it. For example, emulating shell
397 * scripts differently.
400 if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
401 error = img_first(imgp);
404 * Loop through the list of image activators, calling each one.
405 * An activator returns -1 if there is no match, 0 on success,
406 * and an error otherwise.
408 for (i = 0; error == -1 && execsw[i]; ++i) {
409 if (execsw[i]->ex_imgact == NULL ||
410 execsw[i]->ex_imgact == img_first) {
413 error = (*execsw[i]->ex_imgact)(imgp);
419 imgp->vp->v_vflag &= ~VV_TEXT;
422 goto exec_fail_dealloc;
426 * Special interpreter operation, cleanup and loop up to try to
427 * activate the interpreter.
429 if (imgp->interpreted) {
430 exec_unmap_first_page(imgp);
432 * VV_TEXT needs to be unset for scripts. There is a short
433 * period before we determine that something is a script where
434 * VV_TEXT will be set. The vnode lock is held over this
435 * entire period so nothing should illegitimately be blocked.
437 imgp->vp->v_vflag &= ~VV_TEXT;
438 /* free name buffer and old vnode */
439 NDFREE(ndp, NDF_ONLY_PNBUF);
441 interplabel = mac_vnode_label_alloc();
442 mac_copy_vnode_label(ndp->ni_vp->v_label, interplabel);
445 vm_object_deallocate(imgp->object);
447 VFS_UNLOCK_GIANT(vfslocked);
449 /* set new name to that of the interpreter */
450 NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME | MPSAFE,
451 UIO_SYSSPACE, imgp->interpreter_name, td);
456 * Copy out strings (args and env) and initialize stack base
458 if (p->p_sysent->sv_copyout_strings)
459 stack_base = (*p->p_sysent->sv_copyout_strings)(imgp);
461 stack_base = exec_copyout_strings(imgp);
464 * If custom stack fixup routine present for this process
465 * let it do the stack setup.
466 * Else stuff argument count as first item on stack
468 if (p->p_sysent->sv_fixup != NULL)
469 (*p->p_sysent->sv_fixup)(&stack_base, imgp);
471 suword(--stack_base, imgp->args->argc);
474 * For security and other reasons, the file descriptor table cannot
475 * be shared after an exec.
480 * Malloc things before we need locks.
483 euip = uifind(attr.va_uid);
484 i = imgp->args->begin_envv - imgp->args->begin_argv;
485 /* Cache arguments if they fit inside our allowance */
486 if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
487 newargs = pargs_alloc(i);
488 bcopy(imgp->args->begin_argv, newargs->ar_args, i);
491 /* close files on exec */
492 VOP_UNLOCK(imgp->vp, 0, td);
494 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
496 /* Get a reference to the vnode prior to locking the proc */
500 * For security and other reasons, signal handlers cannot
501 * be shared after an exec. The new process gets a copy of the old
502 * handlers. In execsigs(), the new process will have its signals
506 if (sigacts_shared(p->p_sigacts)) {
507 oldsigacts = p->p_sigacts;
509 newsigacts = sigacts_alloc();
510 sigacts_copy(newsigacts, oldsigacts);
512 p->p_sigacts = newsigacts;
519 /* reset caught signals */
522 /* name this process - nameiexec(p, ndp) */
523 len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
524 bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
528 * mark as execed, wakeup the process that vforked (if any) and tell
529 * it that it now has its own resources back
532 if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
533 p->p_flag &= ~P_PPWAIT;
538 * Implement image setuid/setgid.
540 * Don't honor setuid/setgid if the filesystem prohibits it or if
541 * the process is being traced.
543 * XXXMAC: For the time being, use NOSUID to also prohibit
544 * transitions on the file system.
546 oldcred = p->p_ucred;
547 credential_changing = 0;
548 credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid !=
550 credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid !=
553 will_transition = mac_execve_will_transition(oldcred, imgp->vp,
555 credential_changing |= will_transition;
558 if (credential_changing &&
559 (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
560 (p->p_flag & P_TRACED) == 0) {
562 * Turn off syscall tracing for set-id programs, except for
563 * root. Record any set-id flags first to make sure that
564 * we do not regain any tracing during a possible block.
569 if (p->p_tracevp != NULL &&
570 priv_check_cred(oldcred, PRIV_DEBUG_DIFFCRED, 0)) {
571 mtx_lock(&ktrace_mtx);
573 tracevp = p->p_tracevp;
575 tracecred = p->p_tracecred;
576 p->p_tracecred = NULL;
577 mtx_unlock(&ktrace_mtx);
581 * Close any file descriptors 0..2 that reference procfs,
582 * then make sure file descriptors 0..2 are in use.
584 * setugidsafety() may call closef() and then pfind()
585 * which may grab the process lock.
586 * fdcheckstd() may call falloc() which may block to
587 * allocate memory, so temporarily drop the process lock.
591 VOP_UNLOCK(imgp->vp, 0, td);
592 error = fdcheckstd(td);
593 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
598 * Set the new credentials.
600 crcopy(newcred, oldcred);
601 if (attr.va_mode & VSUID)
602 change_euid(newcred, euip);
603 if (attr.va_mode & VSGID)
604 change_egid(newcred, attr.va_gid);
606 if (will_transition) {
607 mac_execve_transition(oldcred, newcred, imgp->vp,
612 * Implement correct POSIX saved-id behavior.
614 * XXXMAC: Note that the current logic will save the
615 * uid and gid if a MAC domain transition occurs, even
616 * though maybe it shouldn't.
618 change_svuid(newcred, newcred->cr_uid);
619 change_svgid(newcred, newcred->cr_gid);
620 p->p_ucred = newcred;
623 if (oldcred->cr_uid == oldcred->cr_ruid &&
624 oldcred->cr_gid == oldcred->cr_rgid)
625 p->p_flag &= ~P_SUGID;
627 * Implement correct POSIX saved-id behavior.
629 * XXX: It's not clear that the existing behavior is
630 * POSIX-compliant. A number of sources indicate that the
631 * saved uid/gid should only be updated if the new ruid is
632 * not equal to the old ruid, or the new euid is not equal
633 * to the old euid and the new euid is not equal to the old
634 * ruid. The FreeBSD code always updates the saved uid/gid.
635 * Also, this code uses the new (replaced) euid and egid as
636 * the source, which may or may not be the right ones to use.
638 if (oldcred->cr_svuid != oldcred->cr_uid ||
639 oldcred->cr_svgid != oldcred->cr_gid) {
640 crcopy(newcred, oldcred);
641 change_svuid(newcred, newcred->cr_uid);
642 change_svgid(newcred, newcred->cr_gid);
643 p->p_ucred = newcred;
649 * Store the vp for use in procfs. This vnode was referenced prior
650 * to locking the proc lock.
652 textvp = p->p_textvp;
653 p->p_textvp = ndp->ni_vp;
656 * Notify others that we exec'd, and clear the P_INEXEC flag
657 * as we're now a bona fide freshly-execed process.
659 KNOTE_LOCKED(&p->p_klist, NOTE_EXEC);
660 p->p_flag &= ~P_INEXEC;
663 * If tracing the process, trap to debugger so breakpoints
664 * can be set before the program executes.
665 * Use tdsignal to deliver signal to current thread, use
666 * psignal may cause the signal to be delivered to wrong thread
667 * because that thread will exit, remember we are going to enter
668 * single thread mode.
670 if (p->p_flag & P_TRACED)
671 tdsignal(p, td, SIGTRAP, NULL);
673 /* clear "fork but no exec" flag, as we _are_ execing */
674 p->p_acflag &= ~AFORK;
677 * Free any previous argument cache and replace it with
678 * the new argument cache, if any.
686 * Check if system-wide sampling is in effect or if the
687 * current process is using PMCs. If so, do exec() time
688 * processing. This processing needs to happen AFTER the
689 * P_INEXEC flag is cleared.
691 * The proc lock needs to be released before taking the PMC
694 if (PMC_SYSTEM_SAMPLING_ACTIVE() || PMC_PROC_IS_USING_PMCS(p)) {
696 pe.pm_credentialschanged = credential_changing;
697 pe.pm_entryaddr = imgp->entry_addr;
699 PMC_CALL_HOOK_X(td, PMC_FN_PROCESS_EXEC, (void *) &pe);
702 #else /* !HWPMC_HOOKS */
706 /* Set values passed into the program in registers. */
707 if (p->p_sysent->sv_setregs)
708 (*p->p_sysent->sv_setregs)(td, imgp->entry_addr,
709 (u_long)(uintptr_t)stack_base, imgp->ps_strings);
711 exec_setregs(td, imgp->entry_addr,
712 (u_long)(uintptr_t)stack_base, imgp->ps_strings);
714 vfs_mark_atime(imgp->vp, td);
718 * Free any resources malloc'd earlier that we didn't use.
725 VOP_UNLOCK(imgp->vp, 0, td);
727 * Handle deferred decrement of ref counts.
729 if (textvp != NULL) {
732 tvfslocked = VFS_LOCK_GIANT(textvp->v_mount);
734 VFS_UNLOCK_GIANT(tvfslocked);
736 if (ndp->ni_vp && error != 0)
741 if (tracecred != NULL)
744 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
749 if (oldsigacts != NULL)
750 sigacts_free(oldsigacts);
755 * free various allocated resources
757 if (imgp->firstpage != NULL)
758 exec_unmap_first_page(imgp);
760 if (imgp->vp != NULL) {
761 NDFREE(ndp, NDF_ONLY_PNBUF);
765 if (imgp->object != NULL)
766 vm_object_deallocate(imgp->object);
770 * Stop the process here if its stop event mask has
771 * the S_EXEC bit set.
773 STOPEVENT(p, S_EXEC, 0);
778 /* we're done here, clear P_INEXEC */
780 p->p_flag &= ~P_INEXEC;
785 mac_execve_exit(imgp);
786 if (interplabel != NULL)
787 mac_vnode_label_free(interplabel);
789 VFS_UNLOCK_GIANT(vfslocked);
790 exec_free_args(args);
792 if (error && imgp->vmspace_destroyed) {
793 /* sorry, no more process anymore. exit gracefully */
794 exit1(td, W_EXITCODE(0, SIGABRT));
801 exec_map_first_page(imgp)
802 struct image_params *imgp;
806 vm_page_t ma[VM_INITIAL_PAGEIN];
809 if (imgp->firstpage != NULL)
810 exec_unmap_first_page(imgp);
812 object = imgp->vp->v_object;
815 VM_OBJECT_LOCK(object);
816 ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
817 if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
818 initial_pagein = VM_INITIAL_PAGEIN;
819 if (initial_pagein > object->size)
820 initial_pagein = object->size;
821 for (i = 1; i < initial_pagein; i++) {
822 if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
825 if ((ma[i]->oflags & VPO_BUSY) || ma[i]->busy)
829 ma[i] = vm_page_alloc(object, i,
836 rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
837 ma[0] = vm_page_lookup(object, 0);
838 if ((rv != VM_PAGER_OK) || (ma[0] == NULL) ||
839 (ma[0]->valid == 0)) {
841 vm_page_lock_queues();
843 vm_page_unlock_queues();
845 VM_OBJECT_UNLOCK(object);
849 vm_page_lock_queues();
851 vm_page_unlock_queues();
852 vm_page_wakeup(ma[0]);
853 VM_OBJECT_UNLOCK(object);
855 imgp->firstpage = sf_buf_alloc(ma[0], 0);
856 imgp->image_header = (char *)sf_buf_kva(imgp->firstpage);
862 exec_unmap_first_page(imgp)
863 struct image_params *imgp;
867 if (imgp->firstpage != NULL) {
868 m = sf_buf_page(imgp->firstpage);
869 sf_buf_free(imgp->firstpage);
870 imgp->firstpage = NULL;
871 vm_page_lock_queues();
873 vm_page_unlock_queues();
878 * Destroy old address space, and allocate a new stack
879 * The new stack is only SGROWSIZ large because it is grown
880 * automatically in trap.c.
883 exec_new_vmspace(imgp, sv)
884 struct image_params *imgp;
885 struct sysentvec *sv;
888 struct proc *p = imgp->proc;
889 struct vmspace *vmspace = p->p_vmspace;
890 vm_offset_t stack_addr;
893 imgp->vmspace_destroyed = 1;
896 /* May be called with Giant held */
897 EVENTHANDLER_INVOKE(process_exec, p, imgp);
900 * Blow away entire process VM, if address space not shared,
901 * otherwise, create a new VM space so that other threads are
904 map = &vmspace->vm_map;
905 if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser &&
906 vm_map_max(map) == sv->sv_maxuser) {
908 pmap_remove_pages(vmspace_pmap(vmspace));
909 vm_map_remove(map, vm_map_min(map), vm_map_max(map));
911 vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser);
912 vmspace = p->p_vmspace;
913 map = &vmspace->vm_map;
916 /* Allocate a new stack */
917 stack_addr = sv->sv_usrstack - maxssiz;
918 error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
919 sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN);
924 /* Allocate a new register stack */
925 stack_addr = IA64_BACKINGSTORE;
926 error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
927 sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP);
932 /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
933 * VM_STACK case, but they are still used to monitor the size of the
934 * process stack so we can check the stack rlimit.
936 vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
937 vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz;
943 * Copy out argument and environment strings from the old process address
944 * space into the temporary string buffer.
947 exec_copyin_args(struct image_args *args, char *fname,
948 enum uio_seg segflg, char **argv, char **envv)
956 bzero(args, sizeof(*args));
960 * Allocate temporary demand zeroed space for argument and
961 * environment strings:
963 * o ARG_MAX for argument and environment;
964 * o MAXSHELLCMDLEN for the name of interpreters.
966 args->buf = (char *) kmem_alloc_wait(exec_map,
967 PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
968 if (args->buf == NULL)
970 args->begin_argv = args->buf;
971 args->endp = args->begin_argv;
972 args->stringspace = ARG_MAX;
974 args->fname = args->buf + ARG_MAX;
977 * Copy the file name.
979 error = (segflg == UIO_SYSSPACE) ?
980 copystr(fname, args->fname, PATH_MAX, &length) :
981 copyinstr(fname, args->fname, PATH_MAX, &length);
986 * extract arguments first
988 while ((argp = (caddr_t) (intptr_t) fuword(argv++))) {
989 if (argp == (caddr_t) -1) {
993 if ((error = copyinstr(argp, args->endp,
994 args->stringspace, &length))) {
995 if (error == ENAMETOOLONG)
999 args->stringspace -= length;
1000 args->endp += length;
1004 args->begin_envv = args->endp;
1007 * extract environment strings
1010 while ((envp = (caddr_t)(intptr_t)fuword(envv++))) {
1011 if (envp == (caddr_t)-1) {
1015 if ((error = copyinstr(envp, args->endp,
1016 args->stringspace, &length))) {
1017 if (error == ENAMETOOLONG)
1021 args->stringspace -= length;
1022 args->endp += length;
1030 exec_free_args(args);
1035 exec_free_args(struct image_args *args)
1039 kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
1040 PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
1046 * Copy strings out to the new process address space, constructing new arg
1047 * and env vector tables. Return a pointer to the base so that it can be used
1048 * as the initial stack pointer.
1051 exec_copyout_strings(imgp)
1052 struct image_params *imgp;
1056 char *stringp, *destp;
1057 register_t *stack_base;
1058 struct ps_strings *arginfo;
1063 * Calculate string base and vector table pointers.
1064 * Also deal with signal trampoline code for this exec type.
1068 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
1069 if (p->p_sysent->sv_szsigcode != NULL)
1070 szsigcode = *(p->p_sysent->sv_szsigcode);
1071 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
1072 roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
1078 copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo -
1079 szsigcode), szsigcode);
1082 * If we have a valid auxargs ptr, prepare some room
1085 if (imgp->auxargs) {
1087 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
1088 * lower compatibility.
1090 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
1093 * The '+ 2' is for the null pointers at the end of each of
1094 * the arg and env vector sets,and imgp->auxarg_size is room
1095 * for argument of Runtime loader.
1097 vectp = (char **)(destp - (imgp->args->argc +
1098 imgp->args->envc + 2 + imgp->auxarg_size) *
1103 * The '+ 2' is for the null pointers at the end of each of
1104 * the arg and env vector sets
1106 vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc + 2) *
1111 * vectp also becomes our initial stack base
1113 stack_base = (register_t *)vectp;
1115 stringp = imgp->args->begin_argv;
1116 argc = imgp->args->argc;
1117 envc = imgp->args->envc;
1120 * Copy out strings - arguments and environment.
1122 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
1125 * Fill in "ps_strings" struct for ps, w, etc.
1127 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
1128 suword(&arginfo->ps_nargvstr, argc);
1131 * Fill in argument portion of vector table.
1133 for (; argc > 0; --argc) {
1134 suword(vectp++, (long)(intptr_t)destp);
1135 while (*stringp++ != 0)
1140 /* a null vector table pointer separates the argp's from the envp's */
1143 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
1144 suword(&arginfo->ps_nenvstr, envc);
1147 * Fill in environment portion of vector table.
1149 for (; envc > 0; --envc) {
1150 suword(vectp++, (long)(intptr_t)destp);
1151 while (*stringp++ != 0)
1156 /* end of vector table is a null pointer */
1159 return (stack_base);
1163 * Check permissions of file to execute.
1164 * Called with imgp->vp locked.
1165 * Return 0 for success or error code on failure.
1168 exec_check_permissions(imgp)
1169 struct image_params *imgp;
1171 struct vnode *vp = imgp->vp;
1172 struct vattr *attr = imgp->attr;
1176 td = curthread; /* XXXKSE */
1178 /* Get file attributes */
1179 error = VOP_GETATTR(vp, attr, td->td_ucred, td);
1184 error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp);
1190 * 1) Check if file execution is disabled for the filesystem that this
1192 * 2) Insure that at least one execute bit is on - otherwise root
1193 * will always succeed, and we don't want to happen unless the
1194 * file really is executable.
1195 * 3) Insure that the file is a regular file.
1197 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
1198 ((attr->va_mode & 0111) == 0) ||
1199 (attr->va_type != VREG))
1203 * Zero length files can't be exec'd
1205 if (attr->va_size == 0)
1209 * Check for execute permission to file based on current credentials.
1211 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
1216 * Check number of open-for-writes on the file and deny execution
1219 if (vp->v_writecount)
1223 * Call filesystem specific open routine (which does nothing in the
1226 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
1231 * Exec handler registration
1234 exec_register(execsw_arg)
1235 const struct execsw *execsw_arg;
1237 const struct execsw **es, **xs, **newexecsw;
1238 int count = 2; /* New slot and trailing NULL */
1241 for (es = execsw; *es; es++)
1243 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1244 if (newexecsw == NULL)
1248 for (es = execsw; *es; es++)
1253 free(execsw, M_TEMP);
1259 exec_unregister(execsw_arg)
1260 const struct execsw *execsw_arg;
1262 const struct execsw **es, **xs, **newexecsw;
1266 panic("unregister with no handlers left?\n");
1268 for (es = execsw; *es; es++) {
1269 if (*es == execsw_arg)
1274 for (es = execsw; *es; es++)
1275 if (*es != execsw_arg)
1277 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1278 if (newexecsw == NULL)
1281 for (es = execsw; *es; es++)
1282 if (*es != execsw_arg)
1286 free(execsw, M_TEMP);