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
26 * $FreeBSD: src/sys/kern/kern_exec.c,v 1.107.2.15 2002/07/30 15:40:46 nectar Exp $
27 * $DragonFly: src/sys/kern/kern_exec.c,v 1.13 2003/11/05 23:26:20 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sysproto.h>
33 #include <sys/kernel.h>
34 #include <sys/mount.h>
35 #include <sys/filedesc.h>
36 #include <sys/fcntl.h>
39 #include <sys/imgact.h>
40 #include <sys/imgact_elf.h>
42 #include <sys/malloc.h>
44 #include <sys/signalvar.h>
45 #include <sys/pioctl.h>
46 #include <sys/namei.h>
47 #include <sys/sysent.h>
49 #include <sys/sysctl.h>
50 #include <sys/vnode.h>
54 #include <vm/vm_param.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_kern.h>
60 #include <vm/vm_extern.h>
61 #include <vm/vm_object.h>
62 #include <vm/vm_pager.h>
65 #include <machine/reg.h>
67 MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
69 static register_t *exec_copyout_strings (struct image_params *);
71 /* XXX This should be vm_size_t. */
72 static u_long ps_strings = PS_STRINGS;
73 SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, 0, "");
75 /* XXX This should be vm_size_t. */
76 static u_long usrstack = USRSTACK;
77 SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, "");
79 u_long ps_arg_cache_limit = PAGE_SIZE / 16;
80 SYSCTL_LONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
81 &ps_arg_cache_limit, 0, "");
84 SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, "");
87 * Each of the items is a pointer to a `const struct execsw', hence the
88 * double pointer here.
90 static const struct execsw **execsw;
93 * execve() system call.
96 execve(struct execve_args *uap)
98 struct thread *td = curthread;
99 struct proc *p = td->td_proc;
100 struct nameidata nd, *ndp;
101 register_t *stack_base;
103 struct image_params image_params, *imgp;
105 int (*img_first) (struct image_params *);
108 imgp = &image_params;
111 * Lock the process and set the P_INEXEC flag to indicate that
112 * it should be left alone until we're done here. This is
113 * necessary to avoid race conditions - e.g. in ptrace() -
114 * that might allow a local user to illicitly obtain elevated
117 p->p_flag |= P_INEXEC;
120 * Initialize part of the common data
125 imgp->argc = imgp->envc = 0;
127 imgp->entry_addr = 0;
128 imgp->vmspace_destroyed = 0;
129 imgp->interpreted = 0;
130 imgp->interpreter_name[0] = '\0';
131 imgp->auxargs = NULL;
133 imgp->firstpage = NULL;
134 imgp->ps_strings = 0;
137 * Allocate temporary demand zeroed space for argument and
138 * environment strings
140 imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE);
141 if (imgp->stringbase == NULL) {
145 imgp->stringp = imgp->stringbase;
146 imgp->stringspace = ARG_MAX;
147 imgp->image_header = imgp->stringbase + ARG_MAX;
150 * Translate the file name. namei() returns a vnode pointer
151 * in ni_vp amoung other things.
154 NDINIT(ndp, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_FOLLOW | CNP_SAVENAME,
155 UIO_USERSPACE, uap->fname, td);
161 kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
162 ARG_MAX + PAGE_SIZE);
166 imgp->vp = ndp->ni_vp;
167 imgp->fname = uap->fname;
170 * Check file permissions (also 'opens' file)
172 error = exec_check_permissions(imgp);
174 VOP_UNLOCK(imgp->vp, 0, td);
175 goto exec_fail_dealloc;
178 error = exec_map_first_page(imgp);
179 VOP_UNLOCK(imgp->vp, 0, td);
181 goto exec_fail_dealloc;
184 * If the current process has a special image activator it
185 * wants to try first, call it. For example, emulating shell
186 * scripts differently.
189 if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
190 error = img_first(imgp);
193 * Loop through the list of image activators, calling each one.
194 * An activator returns -1 if there is no match, 0 on success,
195 * and an error otherwise.
197 for (i = 0; error == -1 && execsw[i]; ++i) {
198 if (execsw[i]->ex_imgact == NULL ||
199 execsw[i]->ex_imgact == img_first) {
202 error = (*execsw[i]->ex_imgact)(imgp);
208 goto exec_fail_dealloc;
212 * Special interpreter operation, cleanup and loop up to try to
213 * activate the interpreter.
215 if (imgp->interpreted) {
216 exec_unmap_first_page(imgp);
217 /* free name buffer and old vnode */
218 NDFREE(ndp, NDF_ONLY_PNBUF);
220 /* set new name to that of the interpreter */
221 NDINIT(ndp, NAMEI_LOOKUP,
222 CNP_LOCKLEAF | CNP_FOLLOW | CNP_SAVENAME,
223 UIO_SYSSPACE, imgp->interpreter_name, td);
228 * Copy out strings (args and env) and initialize stack base
230 stack_base = exec_copyout_strings(imgp);
231 p->p_vmspace->vm_minsaddr = (char *)stack_base;
234 * If custom stack fixup routine present for this process
235 * let it do the stack setup.
236 * Else stuff argument count as first item on stack
238 if (p->p_sysent->sv_fixup)
239 (*p->p_sysent->sv_fixup)(&stack_base, imgp);
241 suword(--stack_base, imgp->argc);
244 * For security and other reasons, the file descriptor table cannot
245 * be shared after an exec.
247 if (p->p_fd->fd_refcnt > 1) {
248 struct filedesc *tmp;
256 * For security and other reasons, signal handlers cannot
257 * be shared after an exec. The new proces gets a copy of the old
258 * handlers. In execsigs(), the new process will have its signals
261 if (p->p_procsig->ps_refcnt > 1) {
262 struct procsig *newprocsig;
264 MALLOC(newprocsig, struct procsig *, sizeof(struct procsig),
265 M_SUBPROC, M_WAITOK);
266 bcopy(p->p_procsig, newprocsig, sizeof(*newprocsig));
267 p->p_procsig->ps_refcnt--;
268 p->p_procsig = newprocsig;
269 p->p_procsig->ps_refcnt = 1;
270 if (p->p_sigacts == &p->p_addr->u_sigacts)
271 panic("shared procsig but private sigacts?");
273 p->p_addr->u_sigacts = *p->p_sigacts;
274 p->p_sigacts = &p->p_addr->u_sigacts;
280 /* close files on exec */
283 /* reset caught signals */
286 /* name this process - nameiexec(p, ndp) */
287 len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
288 bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
292 * mark as execed, wakeup the process that vforked (if any) and tell
293 * it that it now has its own resources back
296 if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
297 p->p_flag &= ~P_PPWAIT;
298 wakeup((caddr_t)p->p_pptr);
302 * Implement image setuid/setgid.
304 * Don't honor setuid/setgid if the filesystem prohibits it or if
305 * the process is being traced.
307 if ((((attr.va_mode & VSUID) && p->p_ucred->cr_uid != attr.va_uid) ||
308 ((attr.va_mode & VSGID) && p->p_ucred->cr_gid != attr.va_gid)) &&
309 (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
310 (p->p_flag & P_TRACED) == 0) {
312 * Turn off syscall tracing for set-id programs, except for
313 * root. Record any set-id flags first to make sure that
314 * we do not regain any tracing during a possible block.
317 if (p->p_tracep && suser(td)) {
320 if ((vtmp = p->p_tracep) != NULL) {
326 /* Close any file descriptors 0..2 that reference procfs */
328 /* Make sure file descriptors 0..2 are in use. */
329 error = fdcheckstd(p);
331 goto exec_fail_dealloc;
333 * Set the new credentials.
336 if (attr.va_mode & VSUID)
337 change_euid(attr.va_uid);
338 if (attr.va_mode & VSGID)
339 p->p_ucred->cr_gid = attr.va_gid;
342 * Clear local varsym variables
344 varsymset_clean(&p->p_varsymset);
346 if (p->p_ucred->cr_uid == p->p_ucred->cr_ruid &&
347 p->p_ucred->cr_gid == p->p_ucred->cr_rgid)
348 p->p_flag &= ~P_SUGID;
352 * Implement correct POSIX saved-id behavior.
354 if (p->p_ucred->cr_svuid != p->p_ucred->cr_uid ||
355 p->p_ucred->cr_svgid != p->p_ucred->cr_gid) {
357 p->p_ucred->cr_svuid = p->p_ucred->cr_uid;
358 p->p_ucred->cr_svgid = p->p_ucred->cr_gid;
362 * Store the vp for use in procfs
364 if (p->p_textvp) /* release old reference */
367 p->p_textvp = ndp->ni_vp;
370 * Notify others that we exec'd, and clear the P_INEXEC flag
371 * as we're now a bona fide freshly-execed process.
373 KNOTE(&p->p_klist, NOTE_EXEC);
374 p->p_flag &= ~P_INEXEC;
377 * If tracing the process, trap to debugger so breakpoints
378 * can be set before the program executes.
380 STOPEVENT(p, S_EXEC, 0);
382 if (p->p_flag & P_TRACED)
385 /* clear "fork but no exec" flag, as we _are_ execing */
386 p->p_acflag &= ~AFORK;
388 /* Set values passed into the program in registers. */
389 setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base,
393 * The syscall result is returned in registers to the new program.
394 * Linux will register %edx as an atexit function and we must be
395 * sure to set it to 0. XXX
397 uap->sysmsg_result64 = 0;
399 /* Free any previous argument cache */
400 if (p->p_args && --p->p_args->ar_ref == 0)
401 FREE(p->p_args, M_PARGS);
404 /* Cache arguments if they fit inside our allowance */
405 i = imgp->endargs - imgp->stringbase;
406 if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
407 MALLOC(p->p_args, struct pargs *, sizeof(struct pargs) + i,
409 p->p_args->ar_ref = 1;
410 p->p_args->ar_length = i;
411 bcopy(imgp->stringbase, p->p_args->ar_args, i);
417 * free various allocated resources
420 exec_unmap_first_page(imgp);
422 if (imgp->stringbase != NULL)
423 kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
424 ARG_MAX + PAGE_SIZE);
427 NDFREE(ndp, NDF_ONLY_PNBUF);
435 /* we're done here, clear P_INEXEC */
436 p->p_flag &= ~P_INEXEC;
437 if (imgp->vmspace_destroyed) {
438 /* sorry, no more process anymore. exit gracefully */
439 exit1(W_EXITCODE(0, SIGABRT));
448 exec_map_first_page(struct image_params *imgp)
452 vm_page_t ma[VM_INITIAL_PAGEIN];
456 if (imgp->firstpage) {
457 exec_unmap_first_page(imgp);
460 VOP_GETVOBJECT(imgp->vp, &object);
463 ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
465 if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
466 initial_pagein = VM_INITIAL_PAGEIN;
467 if (initial_pagein > object->size)
468 initial_pagein = object->size;
469 for (i = 1; i < initial_pagein; i++) {
470 if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
471 if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
477 ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
484 rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
485 ma[0] = vm_page_lookup(object, 0);
487 if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) {
489 vm_page_protect(ma[0], VM_PROT_NONE);
498 vm_page_wakeup(ma[0]);
501 pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0]));
502 imgp->firstpage = ma[0];
508 exec_unmap_first_page(imgp)
509 struct image_params *imgp;
511 if (imgp->firstpage) {
512 pmap_kremove((vm_offset_t) imgp->image_header);
513 vm_page_unwire(imgp->firstpage, 1);
514 imgp->firstpage = NULL;
519 * Destroy old address space, and allocate a new stack
520 * The new stack is only SGROWSIZ large because it is grown
521 * automatically in trap.c.
524 exec_new_vmspace(imgp)
525 struct image_params *imgp;
528 struct vmspace *vmspace = imgp->proc->p_vmspace;
529 vm_offset_t stack_addr = USRSTACK - maxssiz;
530 vm_map_t map = &vmspace->vm_map;
532 imgp->vmspace_destroyed = 1;
535 * Prevent a pending AIO from modifying the new address space.
537 aio_proc_rundown(imgp->proc);
540 * Blow away entire process VM, if address space not shared,
541 * otherwise, create a new VM space so that other threads are
544 if (vmspace->vm_refcnt == 1) {
546 pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS);
547 vm_map_remove(map, 0, VM_MAXUSER_ADDRESS);
549 vmspace_exec(imgp->proc);
550 vmspace = imgp->proc->p_vmspace;
551 map = &vmspace->vm_map;
554 /* Allocate a new stack */
555 error = vm_map_stack(&vmspace->vm_map, stack_addr, (vm_size_t)maxssiz,
556 VM_PROT_ALL, VM_PROT_ALL, 0);
560 /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
561 * VM_STACK case, but they are still used to monitor the size of the
562 * process stack so we can check the stack rlimit.
564 vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
565 vmspace->vm_maxsaddr = (char *)USRSTACK - maxssiz;
571 * Copy out argument and environment strings from the old process
572 * address space into the temporary string buffer.
575 exec_extract_strings(imgp)
576 struct image_params *imgp;
584 * extract arguments first
587 argv = imgp->uap->argv;
590 argp = (caddr_t) (intptr_t) fuword(argv);
591 if (argp == (caddr_t) -1)
599 if (argp == (caddr_t) -1)
601 if ((error = copyinstr(argp, imgp->stringp,
602 imgp->stringspace, &length))) {
603 if (error == ENAMETOOLONG)
607 imgp->stringspace -= length;
608 imgp->stringp += length;
610 } while ((argp = (caddr_t) (intptr_t) fuword(argv++)));
614 imgp->endargs = imgp->stringp;
617 * extract environment strings
620 envv = imgp->uap->envv;
623 while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
624 if (envp == (caddr_t) -1)
626 if ((error = copyinstr(envp, imgp->stringp,
627 imgp->stringspace, &length))) {
628 if (error == ENAMETOOLONG)
632 imgp->stringspace -= length;
633 imgp->stringp += length;
642 * Copy strings out to the new process address space, constructing
643 * new arg and env vector tables. Return a pointer to the base
644 * so that it can be used as the initial stack pointer.
647 exec_copyout_strings(imgp)
648 struct image_params *imgp;
652 char *stringp, *destp;
653 register_t *stack_base;
654 struct ps_strings *arginfo;
658 * Calculate string base and vector table pointers.
659 * Also deal with signal trampoline code for this exec type.
661 arginfo = (struct ps_strings *)PS_STRINGS;
662 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
663 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
664 roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
670 copyout(imgp->proc->p_sysent->sv_sigcode,
671 ((caddr_t)arginfo - szsigcode), szsigcode);
674 * If we have a valid auxargs ptr, prepare some room
679 * The '+ 2' is for the null pointers at the end of each of the
680 * arg and env vector sets, and 'AT_COUNT*2' is room for the
683 vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 +
684 AT_COUNT*2) * sizeof(char*));
687 * The '+ 2' is for the null pointers at the end of each of the
688 * arg and env vector sets
691 (destp - (imgp->argc + imgp->envc + 2) * sizeof(char*));
694 * vectp also becomes our initial stack base
696 stack_base = (register_t *)vectp;
698 stringp = imgp->stringbase;
703 * Copy out strings - arguments and environment.
705 copyout(stringp, destp, ARG_MAX - imgp->stringspace);
708 * Fill in "ps_strings" struct for ps, w, etc.
710 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
711 suword(&arginfo->ps_nargvstr, argc);
714 * Fill in argument portion of vector table.
716 for (; argc > 0; --argc) {
717 suword(vectp++, (long)(intptr_t)destp);
718 while (*stringp++ != 0)
723 /* a null vector table pointer seperates the argp's from the envp's */
726 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
727 suword(&arginfo->ps_nenvstr, envc);
730 * Fill in environment portion of vector table.
732 for (; envc > 0; --envc) {
733 suword(vectp++, (long)(intptr_t)destp);
734 while (*stringp++ != 0)
739 /* end of vector table is a null pointer */
746 * Check permissions of file to execute.
747 * Return 0 for success or error code on failure.
750 exec_check_permissions(imgp)
751 struct image_params *imgp;
753 struct proc *p = imgp->proc;
754 struct vnode *vp = imgp->vp;
755 struct vattr *attr = imgp->attr;
756 struct thread *td = p->p_thread;
759 /* Get file attributes */
760 error = VOP_GETATTR(vp, attr, td);
765 * 1) Check if file execution is disabled for the filesystem that this
767 * 2) Insure that at least one execute bit is on - otherwise root
768 * will always succeed, and we don't want to happen unless the
769 * file really is executable.
770 * 3) Insure that the file is a regular file.
772 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
773 ((attr->va_mode & 0111) == 0) ||
774 (attr->va_type != VREG)) {
779 * Zero length files can't be exec'd
781 if (attr->va_size == 0)
785 * Check for execute permission to file based on current credentials.
787 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
792 * Check number of open-for-writes on the file and deny execution
795 if (vp->v_writecount)
799 * Call filesystem specific open routine (which does nothing in the
802 error = VOP_OPEN(vp, FREAD, p->p_ucred, td);
810 * Exec handler registration
813 exec_register(execsw_arg)
814 const struct execsw *execsw_arg;
816 const struct execsw **es, **xs, **newexecsw;
817 int count = 2; /* New slot and trailing NULL */
820 for (es = execsw; *es; es++)
822 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
823 if (newexecsw == NULL)
827 for (es = execsw; *es; es++)
832 free(execsw, M_TEMP);
838 exec_unregister(execsw_arg)
839 const struct execsw *execsw_arg;
841 const struct execsw **es, **xs, **newexecsw;
845 panic("unregister with no handlers left?\n");
847 for (es = execsw; *es; es++) {
848 if (*es == execsw_arg)
853 for (es = execsw; *es; es++)
854 if (*es != execsw_arg)
856 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
857 if (newexecsw == NULL)
860 for (es = execsw; *es; es++)
861 if (*es != execsw_arg)
865 free(execsw, M_TEMP);