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_ktrace.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/eventhandler.h>
37 #include <sys/mutex.h>
38 #include <sys/sysproto.h>
39 #include <sys/signalvar.h>
40 #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>
52 #include <sys/pioctl.h>
53 #include <sys/namei.h>
54 #include <sys/resourcevar.h>
55 #include <sys/sf_buf.h>
56 #include <sys/syscallsubr.h>
57 #include <sys/sysent.h>
59 #include <sys/sysctl.h>
60 #include <sys/vnode.h>
62 #include <sys/ktrace.h>
66 #include <vm/vm_param.h>
68 #include <vm/vm_page.h>
69 #include <vm/vm_map.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vm_extern.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_pager.h>
76 #include <sys/pmckern.h>
79 #include <machine/reg.h>
81 MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
83 static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS);
84 static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS);
85 static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS);
86 static int do_execve(struct thread *td, struct image_args *args,
89 /* XXX This should be vm_size_t. */
90 SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD,
91 NULL, 0, sysctl_kern_ps_strings, "LU", "");
93 /* XXX This should be vm_size_t. */
94 SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD,
95 NULL, 0, sysctl_kern_usrstack, "LU", "");
97 SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD,
98 NULL, 0, sysctl_kern_stackprot, "I", "");
100 u_long ps_arg_cache_limit = PAGE_SIZE / 16;
101 SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
102 &ps_arg_cache_limit, 0, "");
105 sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS)
112 if (req->flags & SCTL_MASK32) {
114 val = (unsigned int)p->p_sysent->sv_psstrings;
115 error = SYSCTL_OUT(req, &val, sizeof(val));
118 error = SYSCTL_OUT(req, &p->p_sysent->sv_psstrings,
119 sizeof(p->p_sysent->sv_psstrings));
124 sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS)
131 if (req->flags & SCTL_MASK32) {
133 val = (unsigned int)p->p_sysent->sv_usrstack;
134 error = SYSCTL_OUT(req, &val, sizeof(val));
137 error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack,
138 sizeof(p->p_sysent->sv_usrstack));
143 sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS)
148 return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot,
149 sizeof(p->p_sysent->sv_stackprot)));
153 * Each of the items is a pointer to a `const struct execsw', hence the
154 * double pointer here.
156 static const struct execsw **execsw;
158 #ifndef _SYS_SYSPROTO_H_
172 struct execve_args /* {
179 struct image_args args;
181 error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
182 uap->argv, uap->envv);
185 error = kern_execve(td, &args, NULL);
187 exec_free_args(&args);
192 #ifndef _SYS_SYSPROTO_H_
193 struct __mac_execve_args {
205 __mac_execve(td, uap)
207 struct __mac_execve_args /* {
216 struct image_args args;
218 error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
219 uap->argv, uap->envv);
222 error = kern_execve(td, &args, uap->mac_p);
224 exec_free_args(&args);
233 kern_execve(td, args, mac_p)
235 struct image_args *args;
238 struct proc *p = td->td_proc;
241 if (p->p_flag & P_HADTHREADS) {
243 if (thread_single(SINGLE_BOUNDARY)) {
245 return (ERESTART); /* Try again later. */
250 error = do_execve(td, args, mac_p);
252 if (p->p_flag & P_HADTHREADS) {
255 * If success, we upgrade to SINGLE_EXIT state to
256 * force other threads to suicide.
259 thread_single(SINGLE_EXIT);
269 * In-kernel implementation of execve(). All arguments are assumed to be
270 * userspace pointers from the passed thread.
275 do_execve(td, args, mac_p)
277 struct image_args *args;
280 struct proc *p = td->td_proc;
281 struct nameidata nd, *ndp;
282 struct ucred *newcred = NULL, *oldcred;
283 struct uidinfo *euip;
284 register_t *stack_base;
286 struct image_params image_params, *imgp;
287 struct vattr atimeattr, attr;
288 int (*img_first)(struct image_params *);
289 struct pargs *oldargs = NULL, *newargs = NULL;
290 struct sigacts *oldsigacts, *newsigacts;
292 struct vnode *tracevp = NULL;
293 struct ucred *tracecred = NULL;
295 struct vnode *textvp = NULL;
296 int credential_changing;
300 struct label *interplabel = NULL;
305 imgp = &image_params;
308 * Lock the process and set the P_INEXEC flag to indicate that
309 * it should be left alone until we're done here. This is
310 * necessary to avoid race conditions - e.g. in ptrace() -
311 * that might allow a local user to illicitly obtain elevated
315 KASSERT((p->p_flag & P_INEXEC) == 0,
316 ("%s(): process already has P_INEXEC flag", __func__));
317 p->p_flag |= P_INEXEC;
321 * Initialize part of the common data
324 imgp->execlabel = NULL;
326 imgp->entry_addr = 0;
327 imgp->vmspace_destroyed = 0;
328 imgp->interpreted = 0;
329 imgp->interpreter_name = args->buf + PATH_MAX + ARG_MAX;
330 imgp->auxargs = NULL;
333 imgp->firstpage = NULL;
334 imgp->ps_strings = 0;
335 imgp->auxarg_size = 0;
339 error = mac_execve_enter(imgp, mac_p);
344 imgp->image_header = NULL;
347 * Translate the file name. namei() returns a vnode pointer
348 * in ni_vp amoung other things.
351 NDINIT(ndp, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME | MPSAFE,
352 UIO_SYSSPACE, args->fname, td);
359 vfslocked = NDHASGIANT(ndp);
360 imgp->vp = ndp->ni_vp;
363 * Check file permissions (also 'opens' file)
365 error = exec_check_permissions(imgp);
367 goto exec_fail_dealloc;
369 imgp->object = imgp->vp->v_object;
370 if (imgp->object != NULL)
371 vm_object_reference(imgp->object);
374 * Set VV_TEXT now so no one can write to the executable while we're
377 * Remember if this was set before and unset it in case this is not
378 * actually an executable image.
380 textset = imgp->vp->v_vflag & VV_TEXT;
381 imgp->vp->v_vflag |= VV_TEXT;
383 error = exec_map_first_page(imgp);
385 goto exec_fail_dealloc;
388 * If the current process has a special image activator it
389 * wants to try first, call it. For example, emulating shell
390 * scripts differently.
393 if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
394 error = img_first(imgp);
397 * Loop through the list of image activators, calling each one.
398 * An activator returns -1 if there is no match, 0 on success,
399 * and an error otherwise.
401 for (i = 0; error == -1 && execsw[i]; ++i) {
402 if (execsw[i]->ex_imgact == NULL ||
403 execsw[i]->ex_imgact == img_first) {
406 error = (*execsw[i]->ex_imgact)(imgp);
412 imgp->vp->v_vflag &= ~VV_TEXT;
415 goto exec_fail_dealloc;
419 * Special interpreter operation, cleanup and loop up to try to
420 * activate the interpreter.
422 if (imgp->interpreted) {
423 exec_unmap_first_page(imgp);
425 * VV_TEXT needs to be unset for scripts. There is a short
426 * period before we determine that something is a script where
427 * VV_TEXT will be set. The vnode lock is held over this
428 * entire period so nothing should illegitimately be blocked.
430 imgp->vp->v_vflag &= ~VV_TEXT;
431 /* free name buffer and old vnode */
432 NDFREE(ndp, NDF_ONLY_PNBUF);
434 interplabel = mac_vnode_label_alloc();
435 mac_copy_vnode_label(ndp->ni_vp->v_label, interplabel);
438 vm_object_deallocate(imgp->object);
440 VFS_UNLOCK_GIANT(vfslocked);
442 /* set new name to that of the interpreter */
443 NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME | MPSAFE,
444 UIO_SYSSPACE, imgp->interpreter_name, td);
449 * Copy out strings (args and env) and initialize stack base
451 if (p->p_sysent->sv_copyout_strings)
452 stack_base = (*p->p_sysent->sv_copyout_strings)(imgp);
454 stack_base = exec_copyout_strings(imgp);
457 * If custom stack fixup routine present for this process
458 * let it do the stack setup.
459 * Else stuff argument count as first item on stack
461 if (p->p_sysent->sv_fixup != NULL)
462 (*p->p_sysent->sv_fixup)(&stack_base, imgp);
464 suword(--stack_base, imgp->args->argc);
467 * For security and other reasons, the file descriptor table cannot
468 * be shared after an exec.
473 * Malloc things before we need locks.
476 euip = uifind(attr.va_uid);
477 i = imgp->args->begin_envv - imgp->args->begin_argv;
478 if (ps_arg_cache_limit >= i + sizeof(struct pargs))
479 newargs = pargs_alloc(i);
481 /* close files on exec */
484 /* Get a reference to the vnode prior to locking the proc */
488 * For security and other reasons, signal handlers cannot
489 * be shared after an exec. The new process gets a copy of the old
490 * handlers. In execsigs(), the new process will have its signals
494 if (sigacts_shared(p->p_sigacts)) {
495 oldsigacts = p->p_sigacts;
497 newsigacts = sigacts_alloc();
498 sigacts_copy(newsigacts, oldsigacts);
500 p->p_sigacts = newsigacts;
507 /* reset caught signals */
510 /* name this process - nameiexec(p, ndp) */
511 len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
512 bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
516 * mark as execed, wakeup the process that vforked (if any) and tell
517 * it that it now has its own resources back
520 if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
521 p->p_flag &= ~P_PPWAIT;
526 * Implement image setuid/setgid.
528 * Don't honor setuid/setgid if the filesystem prohibits it or if
529 * the process is being traced.
531 * XXXMAC: For the time being, use NOSUID to also prohibit
532 * transitions on the file system.
534 oldcred = p->p_ucred;
535 credential_changing = 0;
536 credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid !=
538 credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid !=
541 will_transition = mac_execve_will_transition(oldcred, imgp->vp,
543 credential_changing |= will_transition;
546 if (credential_changing &&
547 (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
548 (p->p_flag & P_TRACED) == 0) {
550 * Turn off syscall tracing for set-id programs, except for
551 * root. Record any set-id flags first to make sure that
552 * we do not regain any tracing during a possible block.
556 if (p->p_tracevp != NULL && suser_cred(oldcred, SUSER_ALLOWJAIL)) {
557 mtx_lock(&ktrace_mtx);
559 tracevp = p->p_tracevp;
561 tracecred = p->p_tracecred;
562 p->p_tracecred = NULL;
563 mtx_unlock(&ktrace_mtx);
567 * Close any file descriptors 0..2 that reference procfs,
568 * then make sure file descriptors 0..2 are in use.
570 * setugidsafety() may call closef() and then pfind()
571 * which may grab the process lock.
572 * fdcheckstd() may call falloc() which may block to
573 * allocate memory, so temporarily drop the process lock.
577 error = fdcheckstd(td);
582 * Set the new credentials.
584 crcopy(newcred, oldcred);
585 if (attr.va_mode & VSUID)
586 change_euid(newcred, euip);
587 if (attr.va_mode & VSGID)
588 change_egid(newcred, attr.va_gid);
590 if (will_transition) {
591 mac_execve_transition(oldcred, newcred, imgp->vp,
596 * Implement correct POSIX saved-id behavior.
598 * XXXMAC: Note that the current logic will save the
599 * uid and gid if a MAC domain transition occurs, even
600 * though maybe it shouldn't.
602 change_svuid(newcred, newcred->cr_uid);
603 change_svgid(newcred, newcred->cr_gid);
604 p->p_ucred = newcred;
607 if (oldcred->cr_uid == oldcred->cr_ruid &&
608 oldcred->cr_gid == oldcred->cr_rgid)
609 p->p_flag &= ~P_SUGID;
611 * Implement correct POSIX saved-id behavior.
613 * XXX: It's not clear that the existing behavior is
614 * POSIX-compliant. A number of sources indicate that the
615 * saved uid/gid should only be updated if the new ruid is
616 * not equal to the old ruid, or the new euid is not equal
617 * to the old euid and the new euid is not equal to the old
618 * ruid. The FreeBSD code always updates the saved uid/gid.
619 * Also, this code uses the new (replaced) euid and egid as
620 * the source, which may or may not be the right ones to use.
622 if (oldcred->cr_svuid != oldcred->cr_uid ||
623 oldcred->cr_svgid != oldcred->cr_gid) {
624 crcopy(newcred, oldcred);
625 change_svuid(newcred, newcred->cr_uid);
626 change_svgid(newcred, newcred->cr_gid);
627 p->p_ucred = newcred;
633 * Store the vp for use in procfs. This vnode was referenced prior
634 * to locking the proc lock.
636 textvp = p->p_textvp;
637 p->p_textvp = ndp->ni_vp;
640 * Notify others that we exec'd, and clear the P_INEXEC flag
641 * as we're now a bona fide freshly-execed process.
643 KNOTE_LOCKED(&p->p_klist, NOTE_EXEC);
644 p->p_flag &= ~P_INEXEC;
647 * If tracing the process, trap to debugger so breakpoints
648 * can be set before the program executes.
649 * Use tdsignal to deliver signal to current thread, use
650 * psignal may cause the signal to be delivered to wrong thread
651 * because that thread will exit, remember we are going to enter
652 * single thread mode.
654 if (p->p_flag & P_TRACED)
655 tdsignal(td, SIGTRAP, SIGTARGET_TD);
657 /* clear "fork but no exec" flag, as we _are_ execing */
658 p->p_acflag &= ~AFORK;
660 /* Free any previous argument cache */
664 /* Cache arguments if they fit inside our allowance */
665 if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
666 bcopy(imgp->args->begin_argv, newargs->ar_args, i);
673 * Check if system-wide sampling is in effect or if the
674 * current process is using PMCs. If so, do exec() time
675 * processing. This processing needs to happen AFTER the
676 * P_INEXEC flag is cleared.
678 * The proc lock needs to be released before taking the PMC
681 if (PMC_SYSTEM_SAMPLING_ACTIVE() || PMC_PROC_IS_USING_PMCS(p)) {
683 PMC_CALL_HOOK_X(td, PMC_FN_PROCESS_EXEC,
684 (void *) &credential_changing);
687 #else /* !HWPMC_HOOKS */
691 /* Set values passed into the program in registers. */
692 if (p->p_sysent->sv_setregs)
693 (*p->p_sysent->sv_setregs)(td, imgp->entry_addr,
694 (u_long)(uintptr_t)stack_base, imgp->ps_strings);
696 exec_setregs(td, imgp->entry_addr,
697 (u_long)(uintptr_t)stack_base, imgp->ps_strings);
700 * Here we should update the access time of the file. This must
701 * be implemented by the underlying filesystem in the same way as
702 * access timestamps for a VOP_READ() because we want to avoid
703 * blocking and/or I/O, and have not called vn_start_write().
705 if ((imgp->vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
706 VATTR_NULL(&atimeattr);
707 atimeattr.va_vaflags |= VA_EXECVE_ATIME;
708 (void)VOP_SETATTR(imgp->vp, &atimeattr, td->td_ucred, td);
713 * Free any resources malloc'd earlier that we didn't use.
721 * Handle deferred decrement of ref counts.
725 if (ndp->ni_vp && error != 0)
730 if (tracecred != NULL)
737 if (oldsigacts != NULL)
738 sigacts_free(oldsigacts);
743 * free various allocated resources
745 if (imgp->firstpage != NULL)
746 exec_unmap_first_page(imgp);
748 if (imgp->vp != NULL) {
749 NDFREE(ndp, NDF_ONLY_PNBUF);
753 if (imgp->object != NULL)
754 vm_object_deallocate(imgp->object);
758 * Stop the process here if its stop event mask has
759 * the S_EXEC bit set.
761 STOPEVENT(p, S_EXEC, 0);
766 /* we're done here, clear P_INEXEC */
768 p->p_flag &= ~P_INEXEC;
771 if (imgp->vmspace_destroyed) {
772 /* sorry, no more process anymore. exit gracefully */
774 mac_execve_exit(imgp);
775 if (interplabel != NULL)
776 mac_vnode_label_free(interplabel);
778 VFS_UNLOCK_GIANT(vfslocked);
779 exit1(td, W_EXITCODE(0, SIGABRT));
785 mac_execve_exit(imgp);
786 if (interplabel != NULL)
787 mac_vnode_label_free(interplabel);
789 VFS_UNLOCK_GIANT(vfslocked);
794 exec_map_first_page(imgp)
795 struct image_params *imgp;
799 vm_page_t ma[VM_INITIAL_PAGEIN];
802 if (imgp->firstpage != NULL)
803 exec_unmap_first_page(imgp);
805 object = imgp->vp->v_object;
808 VM_OBJECT_LOCK(object);
809 ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
810 if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
811 initial_pagein = VM_INITIAL_PAGEIN;
812 if (initial_pagein > object->size)
813 initial_pagein = object->size;
814 for (i = 1; i < initial_pagein; i++) {
815 if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
818 vm_page_lock_queues();
819 if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) {
820 vm_page_unlock_queues();
824 vm_page_unlock_queues();
826 ma[i] = vm_page_alloc(object, i,
833 rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
834 ma[0] = vm_page_lookup(object, 0);
835 if ((rv != VM_PAGER_OK) || (ma[0] == NULL) ||
836 (ma[0]->valid == 0)) {
838 vm_page_lock_queues();
839 pmap_remove_all(ma[0]);
841 vm_page_unlock_queues();
843 VM_OBJECT_UNLOCK(object);
847 vm_page_lock_queues();
849 vm_page_wakeup(ma[0]);
850 vm_page_unlock_queues();
851 VM_OBJECT_UNLOCK(object);
853 imgp->firstpage = sf_buf_alloc(ma[0], 0);
854 imgp->image_header = (char *)sf_buf_kva(imgp->firstpage);
860 exec_unmap_first_page(imgp)
861 struct image_params *imgp;
865 if (imgp->firstpage != NULL) {
866 m = sf_buf_page(imgp->firstpage);
867 sf_buf_free(imgp->firstpage);
868 imgp->firstpage = NULL;
869 vm_page_lock_queues();
871 vm_page_unlock_queues();
876 * Destroy old address space, and allocate a new stack
877 * The new stack is only SGROWSIZ large because it is grown
878 * automatically in trap.c.
881 exec_new_vmspace(imgp, sv)
882 struct image_params *imgp;
883 struct sysentvec *sv;
886 struct proc *p = imgp->proc;
887 struct vmspace *vmspace = p->p_vmspace;
888 vm_offset_t stack_addr;
891 imgp->vmspace_destroyed = 1;
893 /* Called with Giant held, do not depend on it! */
894 EVENTHANDLER_INVOKE(process_exec, p);
897 * Here is as good a place as any to do any resource limit cleanups.
898 * This is needed if a 64 bit binary exec's a 32 bit binary - the
899 * data size limit may need to be changed to a value that makes
900 * sense for the 32 bit binary.
902 if (sv->sv_fixlimits != NULL)
903 sv->sv_fixlimits(imgp);
906 * Blow away entire process VM, if address space not shared,
907 * otherwise, create a new VM space so that other threads are
910 map = &vmspace->vm_map;
911 if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser &&
912 vm_map_max(map) == sv->sv_maxuser) {
914 pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map),
916 vm_map_remove(map, vm_map_min(map), vm_map_max(map));
918 vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser);
919 vmspace = p->p_vmspace;
920 map = &vmspace->vm_map;
923 /* Allocate a new stack */
924 stack_addr = sv->sv_usrstack - maxssiz;
925 error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
926 sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN);
931 /* Allocate a new register stack */
932 stack_addr = IA64_BACKINGSTORE;
933 error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
934 sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP);
939 /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
940 * VM_STACK case, but they are still used to monitor the size of the
941 * process stack so we can check the stack rlimit.
943 vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
944 vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz;
950 * Copy out argument and environment strings from the old process
951 * address space into the temporary string buffer.
954 exec_copyin_args(struct image_args *args, char *fname,
955 enum uio_seg segflg, char **argv, char **envv)
963 bzero(args, sizeof(*args));
967 * Allocate temporary demand zeroed space for argument and
968 * environment strings:
970 * o ARG_MAX for argument and environment;
971 * o MAXSHELLCMDLEN for the name of interpreters.
973 args->buf = (char *) kmem_alloc_wait(exec_map,
974 PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
975 if (args->buf == NULL)
977 args->begin_argv = args->buf;
978 args->endp = args->begin_argv;
979 args->stringspace = ARG_MAX;
981 args->fname = args->buf + ARG_MAX;
984 * Copy the file name.
986 error = (segflg == UIO_SYSSPACE) ?
987 copystr(fname, args->fname, PATH_MAX, &length) :
988 copyinstr(fname, args->fname, PATH_MAX, &length);
993 * extract arguments first
995 while ((argp = (caddr_t) (intptr_t) fuword(argv++))) {
996 if (argp == (caddr_t) -1)
998 if ((error = copyinstr(argp, args->endp,
999 args->stringspace, &length))) {
1000 if (error == ENAMETOOLONG)
1004 args->stringspace -= length;
1005 args->endp += length;
1009 args->begin_envv = args->endp;
1012 * extract environment strings
1015 while ((envp = (caddr_t)(intptr_t)fuword(envv++))) {
1016 if (envp == (caddr_t)-1)
1018 if ((error = copyinstr(envp, args->endp,
1019 args->stringspace, &length))) {
1020 if (error == ENAMETOOLONG)
1024 args->stringspace -= length;
1025 args->endp += length;
1034 exec_free_args(struct image_args *args)
1038 kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
1039 PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
1045 * Copy strings out to the new process address space, constructing
1046 * new arg and env vector tables. Return a pointer to the base
1047 * so that it can be used as the initial stack pointer.
1050 exec_copyout_strings(imgp)
1051 struct image_params *imgp;
1055 char *stringp, *destp;
1056 register_t *stack_base;
1057 struct ps_strings *arginfo;
1062 * Calculate string base and vector table pointers.
1063 * Also deal with signal trampoline code for this exec type.
1067 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
1068 if (p->p_sysent->sv_szsigcode != NULL)
1069 szsigcode = *(p->p_sysent->sv_szsigcode);
1070 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
1071 roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
1077 copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo -
1078 szsigcode), szsigcode);
1081 * If we have a valid auxargs ptr, prepare some room
1084 if (imgp->auxargs) {
1086 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
1087 * lower compatibility.
1089 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
1092 * The '+ 2' is for the null pointers at the end of each of
1093 * the arg and env vector sets,and imgp->auxarg_size is room
1094 * for argument of Runtime loader.
1096 vectp = (char **)(destp - (imgp->args->argc +
1097 imgp->args->envc + 2 + imgp->auxarg_size) *
1102 * The '+ 2' is for the null pointers at the end of each of
1103 * the arg and env vector sets
1105 vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc + 2) *
1110 * vectp also becomes our initial stack base
1112 stack_base = (register_t *)vectp;
1114 stringp = imgp->args->begin_argv;
1115 argc = imgp->args->argc;
1116 envc = imgp->args->envc;
1119 * Copy out strings - arguments and environment.
1121 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
1124 * Fill in "ps_strings" struct for ps, w, etc.
1126 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
1127 suword(&arginfo->ps_nargvstr, argc);
1130 * Fill in argument portion of vector table.
1132 for (; argc > 0; --argc) {
1133 suword(vectp++, (long)(intptr_t)destp);
1134 while (*stringp++ != 0)
1139 /* a null vector table pointer separates the argp's from the envp's */
1142 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
1143 suword(&arginfo->ps_nenvstr, envc);
1146 * Fill in environment portion of vector table.
1148 for (; envc > 0; --envc) {
1149 suword(vectp++, (long)(intptr_t)destp);
1150 while (*stringp++ != 0)
1155 /* end of vector table is a null pointer */
1158 return (stack_base);
1162 * Check permissions of file to execute.
1163 * Called with imgp->vp locked.
1164 * Return 0 for success or error code on failure.
1167 exec_check_permissions(imgp)
1168 struct image_params *imgp;
1170 struct vnode *vp = imgp->vp;
1171 struct vattr *attr = imgp->attr;
1175 td = curthread; /* XXXKSE */
1177 /* Get file attributes */
1178 error = VOP_GETATTR(vp, attr, td->td_ucred, td);
1183 error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp);
1189 * 1) Check if file execution is disabled for the filesystem that this
1191 * 2) Insure that at least one execute bit is on - otherwise root
1192 * will always succeed, and we don't want to happen unless the
1193 * file really is executable.
1194 * 3) Insure that the file is a regular file.
1196 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
1197 ((attr->va_mode & 0111) == 0) ||
1198 (attr->va_type != VREG))
1202 * Zero length files can't be exec'd
1204 if (attr->va_size == 0)
1208 * Check for execute permission to file based on current credentials.
1210 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
1215 * Check number of open-for-writes on the file and deny execution
1218 if (vp->v_writecount)
1222 * Call filesystem specific open routine (which does nothing in the
1225 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1);
1230 * Exec handler registration
1233 exec_register(execsw_arg)
1234 const struct execsw *execsw_arg;
1236 const struct execsw **es, **xs, **newexecsw;
1237 int count = 2; /* New slot and trailing NULL */
1240 for (es = execsw; *es; es++)
1242 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1243 if (newexecsw == NULL)
1247 for (es = execsw; *es; es++)
1252 free(execsw, M_TEMP);
1258 exec_unregister(execsw_arg)
1259 const struct execsw *execsw_arg;
1261 const struct execsw **es, **xs, **newexecsw;
1265 panic("unregister with no handlers left?\n");
1267 for (es = execsw; *es; es++) {
1268 if (*es == execsw_arg)
1273 for (es = execsw; *es; es++)
1274 if (*es != execsw_arg)
1276 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1277 if (newexecsw == NULL)
1280 for (es = execsw; *es; es++)
1281 if (*es != execsw_arg)
1285 free(execsw, M_TEMP);