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.36 2006/03/29 18:44:50 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>
41 #include <sys/kern_syscall.h>
43 #include <sys/malloc.h>
45 #include <sys/signalvar.h>
46 #include <sys/pioctl.h>
47 #include <sys/nlookup.h>
48 #include <sys/sfbuf.h>
49 #include <sys/sysent.h>
51 #include <sys/sysctl.h>
52 #include <sys/vnode.h>
53 #include <sys/vmmeter.h>
55 #include <sys/libkern.h>
58 #include <vm/vm_param.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_map.h>
63 #include <vm/vm_kern.h>
64 #include <vm/vm_extern.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_pager.h>
69 #include <machine/reg.h>
71 #include <sys/thread2.h>
73 MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
75 static register_t *exec_copyout_strings (struct image_params *);
77 /* XXX This should be vm_size_t. */
78 static u_long ps_strings = PS_STRINGS;
79 SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, 0, "");
81 /* XXX This should be vm_size_t. */
82 static u_long usrstack = USRSTACK;
83 SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, "");
85 u_long ps_arg_cache_limit = PAGE_SIZE / 16;
86 SYSCTL_LONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
87 &ps_arg_cache_limit, 0, "");
90 SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, "");
92 void print_execve_args(struct image_args *args);
93 int debug_execve_args = 0;
94 SYSCTL_INT(_kern, OID_AUTO, debug_execve_args, CTLFLAG_RW, &debug_execve_args,
98 * stackgap_random specifies if the stackgap should have a random size added
99 * to it. It must be a power of 2. If non-zero, the stack gap will be
100 * calculated as: ALIGN(arc4random() & (stackgap_random - 1)).
102 static int stackgap_random = 1024;
104 sysctl_kern_stackgap(SYSCTL_HANDLER_ARGS)
107 new_val = stackgap_random;
108 error = sysctl_handle_int(oidp, &new_val, 0, req);
109 if (error != 0 || req->newptr == NULL)
111 if ((new_val < 0) || (new_val > 16 * PAGE_SIZE) || ! powerof2(new_val))
113 stackgap_random = new_val;
118 SYSCTL_PROC(_kern, OID_AUTO, stackgap_random, CTLFLAG_RW|CTLTYPE_UINT,
119 0, 0, sysctl_kern_stackgap, "IU", "Max random stack gap (power of 2)");
122 print_execve_args(struct image_args *args)
127 cp = args->begin_argv;
128 for (ndx = 0; ndx < args->argc; ndx++) {
129 printf("\targv[%d]: %s\n", ndx, cp);
130 while (*cp++ != '\0');
132 for (ndx = 0; ndx < args->envc; ndx++) {
133 printf("\tenvv[%d]: %s\n", ndx, cp);
134 while (*cp++ != '\0');
139 * Each of the items is a pointer to a `const struct execsw', hence the
140 * double pointer here.
142 static const struct execsw **execsw;
145 kern_execve(struct nlookupdata *nd, struct image_args *args)
147 struct thread *td = curthread;
148 struct proc *p = td->td_proc;
149 register_t *stack_base;
151 struct image_params image_params, *imgp;
153 int (*img_first) (struct image_params *);
155 if (debug_execve_args) {
156 printf("%s()\n", __func__);
157 print_execve_args(args);
161 imgp = &image_params;
164 * Lock the process and set the P_INEXEC flag to indicate that
165 * it should be left alone until we're done here. This is
166 * necessary to avoid race conditions - e.g. in ptrace() -
167 * that might allow a local user to illicitly obtain elevated
170 p->p_flag |= P_INEXEC;
173 * Initialize part of the common data
178 imgp->entry_addr = 0;
180 imgp->vmspace_destroyed = 0;
181 imgp->interpreted = 0;
182 imgp->interpreter_name[0] = 0;
183 imgp->auxargs = NULL;
185 imgp->firstpage = NULL;
186 imgp->ps_strings = 0;
187 imgp->image_header = NULL;
192 * Translate the file name to a vnode. Unlock the cache entry to
193 * improve parallelism for programs exec'd in parallel.
195 if ((error = nlookup(nd)) != 0)
197 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &imgp->vp);
198 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
199 nd->nl_flags &= ~NLC_NCPISLOCKED;
200 cache_unlock(nd->nl_ncp);
205 * Check file permissions (also 'opens' file)
207 error = exec_check_permissions(imgp);
209 VOP_UNLOCK(imgp->vp, 0, td);
210 goto exec_fail_dealloc;
213 error = exec_map_first_page(imgp);
214 VOP_UNLOCK(imgp->vp, 0, td);
216 goto exec_fail_dealloc;
218 if (debug_execve_args && imgp->interpreted) {
219 printf(" target is interpreted -- recursive pass\n");
220 printf(" interpreter: %s\n", imgp->interpreter_name);
221 print_execve_args(args);
225 * If the current process has a special image activator it
226 * wants to try first, call it. For example, emulating shell
227 * scripts differently.
230 if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
231 error = img_first(imgp);
234 * If the vnode has a registered vmspace, exec the vmspace
236 if (error == -1 && imgp->vp->v_resident) {
237 error = exec_resident_imgact(imgp);
241 * Loop through the list of image activators, calling each one.
242 * An activator returns -1 if there is no match, 0 on success,
243 * and an error otherwise.
245 for (i = 0; error == -1 && execsw[i]; ++i) {
246 if (execsw[i]->ex_imgact == NULL ||
247 execsw[i]->ex_imgact == img_first) {
250 error = (*execsw[i]->ex_imgact)(imgp);
256 goto exec_fail_dealloc;
260 * Special interpreter operation, cleanup and loop up to try to
261 * activate the interpreter.
263 if (imgp->interpreted) {
264 exec_unmap_first_page(imgp);
268 error = nlookup_init(nd, imgp->interpreter_name, UIO_SYSSPACE,
276 * Copy out strings (args and env) and initialize stack base
278 stack_base = exec_copyout_strings(imgp);
279 p->p_vmspace->vm_minsaddr = (char *)stack_base;
282 * If custom stack fixup routine present for this process
283 * let it do the stack setup. If we are running a resident
284 * image there is no auxinfo or other image activator context
285 * so don't try to add fixups to the stack.
287 * Else stuff argument count as first item on stack
289 if (p->p_sysent->sv_fixup && imgp->resident == 0)
290 (*p->p_sysent->sv_fixup)(&stack_base, imgp);
292 suword(--stack_base, imgp->args->argc);
295 * For security and other reasons, the file descriptor table cannot
296 * be shared after an exec.
298 if (p->p_fd->fd_refcnt > 1) {
299 struct filedesc *tmp;
307 * For security and other reasons, signal handlers cannot
308 * be shared after an exec. The new proces gets a copy of the old
309 * handlers. In execsigs(), the new process will have its signals
312 if (p->p_procsig->ps_refcnt > 1) {
313 struct procsig *newprocsig;
315 MALLOC(newprocsig, struct procsig *, sizeof(struct procsig),
316 M_SUBPROC, M_WAITOK);
317 bcopy(p->p_procsig, newprocsig, sizeof(*newprocsig));
318 p->p_procsig->ps_refcnt--;
319 p->p_procsig = newprocsig;
320 p->p_procsig->ps_refcnt = 1;
321 if (p->p_sigacts == &p->p_addr->u_sigacts)
322 panic("shared procsig but private sigacts?");
324 p->p_addr->u_sigacts = *p->p_sigacts;
325 p->p_sigacts = &p->p_addr->u_sigacts;
331 /* close files on exec */
334 /* reset caught signals */
337 /* name this process - nameiexec(p, ndp) */
338 len = min(nd->nl_ncp->nc_nlen, MAXCOMLEN);
339 bcopy(nd->nl_ncp->nc_name, p->p_comm, len);
341 bcopy(p->p_comm, p->p_lwp.lwp_thread->td_comm, MAXCOMLEN+1);
344 * mark as execed, wakeup the process that vforked (if any) and tell
345 * it that it now has its own resources back
348 if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
349 p->p_flag &= ~P_PPWAIT;
350 wakeup((caddr_t)p->p_pptr);
354 * Implement image setuid/setgid.
356 * Don't honor setuid/setgid if the filesystem prohibits it or if
357 * the process is being traced.
359 if ((((attr.va_mode & VSUID) && p->p_ucred->cr_uid != attr.va_uid) ||
360 ((attr.va_mode & VSGID) && p->p_ucred->cr_gid != attr.va_gid)) &&
361 (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
362 (p->p_flag & P_TRACED) == 0) {
364 * Turn off syscall tracing for set-id programs, except for
365 * root. Record any set-id flags first to make sure that
366 * we do not regain any tracing during a possible block.
369 if (p->p_tracep && suser(td)) {
372 if ((vtmp = p->p_tracep) != NULL) {
378 /* Close any file descriptors 0..2 that reference procfs */
380 /* Make sure file descriptors 0..2 are in use. */
381 error = fdcheckstd(p);
383 goto exec_fail_dealloc;
385 * Set the new credentials.
388 if (attr.va_mode & VSUID)
389 change_euid(attr.va_uid);
390 if (attr.va_mode & VSGID)
391 p->p_ucred->cr_gid = attr.va_gid;
394 * Clear local varsym variables
396 varsymset_clean(&p->p_varsymset);
398 if (p->p_ucred->cr_uid == p->p_ucred->cr_ruid &&
399 p->p_ucred->cr_gid == p->p_ucred->cr_rgid)
400 p->p_flag &= ~P_SUGID;
404 * Implement correct POSIX saved-id behavior.
406 if (p->p_ucred->cr_svuid != p->p_ucred->cr_uid ||
407 p->p_ucred->cr_svgid != p->p_ucred->cr_gid) {
409 p->p_ucred->cr_svuid = p->p_ucred->cr_uid;
410 p->p_ucred->cr_svgid = p->p_ucred->cr_gid;
414 * Store the vp for use in procfs
416 if (p->p_textvp) /* release old reference */
418 p->p_textvp = imgp->vp;
422 * Notify others that we exec'd, and clear the P_INEXEC flag
423 * as we're now a bona fide freshly-execed process.
425 KNOTE(&p->p_klist, NOTE_EXEC);
426 p->p_flag &= ~P_INEXEC;
429 * If tracing the process, trap to debugger so breakpoints
430 * can be set before the program executes.
432 STOPEVENT(p, S_EXEC, 0);
434 if (p->p_flag & P_TRACED)
437 /* clear "fork but no exec" flag, as we _are_ execing */
438 p->p_acflag &= ~AFORK;
440 /* Set values passed into the program in registers. */
441 setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base,
444 /* Free any previous argument cache */
445 if (p->p_args && --p->p_args->ar_ref == 0)
446 FREE(p->p_args, M_PARGS);
449 /* Cache arguments if they fit inside our allowance */
450 i = imgp->args->begin_envv - imgp->args->begin_argv;
451 if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
452 MALLOC(p->p_args, struct pargs *, sizeof(struct pargs) + i,
454 p->p_args->ar_ref = 1;
455 p->p_args->ar_length = i;
456 bcopy(imgp->args->begin_argv, p->p_args->ar_args, i);
462 * free various allocated resources
465 exec_unmap_first_page(imgp);
473 ++mycpu->gd_cnt.v_exec;
478 /* we're done here, clear P_INEXEC */
479 p->p_flag &= ~P_INEXEC;
480 if (imgp->vmspace_destroyed) {
481 /* sorry, no more process anymore. exit gracefully */
482 exit1(W_EXITCODE(0, SIGABRT));
491 * execve() system call.
494 execve(struct execve_args *uap)
496 struct nlookupdata nd;
497 struct image_args args;
500 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
502 error = exec_copyin_args(&args, uap->fname, PATH_USERSPACE,
503 uap->argv, uap->envv);
506 error = kern_execve(&nd, &args);
508 exec_free_args(&args);
511 * The syscall result is returned in registers to the new program.
512 * Linux will register %edx as an atexit function and we must be
513 * sure to set it to 0. XXX
516 uap->sysmsg_result64 = 0;
522 exec_map_first_page(struct image_params *imgp)
526 vm_page_t ma[VM_INITIAL_PAGEIN];
531 exec_unmap_first_page(imgp);
534 * The file has to be mappable.
536 if ((object = imgp->vp->v_object) == NULL)
540 * We shouldn't need protection for vm_page_grab() but we certainly
541 * need it for the lookup loop below (lookup/busy race), since
542 * an interrupt can unbusy and free the page before our busy check.
545 m = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
547 if ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
549 initial_pagein = VM_INITIAL_PAGEIN;
550 if (initial_pagein > object->size)
551 initial_pagein = object->size;
552 for (i = 1; i < initial_pagein; i++) {
553 if ((m = vm_page_lookup(object, i)) != NULL) {
554 if ((m->flags & PG_BUSY) || m->busy)
560 m = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
569 * get_pages unbusies all the requested pages except the
570 * primary page (at index 0 in this case).
572 rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
573 m = vm_page_lookup(object, 0);
575 if (rv != VM_PAGER_OK || m == NULL || m->valid == 0) {
577 vm_page_protect(m, VM_PROT_NONE);
585 vm_page_wakeup(m); /* unbusy the page */
588 imgp->firstpage = sf_buf_alloc(m, SFB_CPUPRIVATE);
589 imgp->image_header = (void *)sf_buf_kva(imgp->firstpage);
595 exec_unmap_first_page(struct image_params *imgp)
600 if (imgp->firstpage != NULL) {
601 m = sf_buf_page(imgp->firstpage);
602 sf_buf_free(imgp->firstpage);
603 imgp->firstpage = NULL;
604 imgp->image_header = NULL;
611 * Destroy old address space, and allocate a new stack
612 * The new stack is only SGROWSIZ large because it is grown
613 * automatically in trap.c.
616 exec_new_vmspace(struct image_params *imgp, struct vmspace *vmcopy)
619 struct vmspace *vmspace = imgp->proc->p_vmspace;
620 vm_offset_t stack_addr = USRSTACK - maxssiz;
623 imgp->vmspace_destroyed = 1;
626 * Prevent a pending AIO from modifying the new address space.
628 aio_proc_rundown(imgp->proc);
631 * Blow away entire process VM, if address space not shared,
632 * otherwise, create a new VM space so that other threads are
633 * not disrupted. If we are execing a resident vmspace we
634 * create a duplicate of it and remap the stack.
636 * The exitingcnt test is not strictly necessary but has been
637 * included for code sanity (to make the code more deterministic).
639 map = &vmspace->vm_map;
641 vmspace_exec(imgp->proc, vmcopy);
642 vmspace = imgp->proc->p_vmspace;
643 pmap_remove_pages(vmspace_pmap(vmspace), stack_addr, USRSTACK);
644 map = &vmspace->vm_map;
645 } else if (vmspace->vm_refcnt == 1 && vmspace->vm_exitingcnt == 0) {
647 if (vmspace->vm_upcalls)
648 upc_release(vmspace, &imgp->proc->p_lwp);
649 pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS);
650 vm_map_remove(map, 0, VM_MAXUSER_ADDRESS);
652 vmspace_exec(imgp->proc, NULL);
653 vmspace = imgp->proc->p_vmspace;
654 map = &vmspace->vm_map;
657 /* Allocate a new stack */
658 error = vm_map_stack(&vmspace->vm_map, stack_addr, (vm_size_t)maxssiz,
659 VM_PROT_ALL, VM_PROT_ALL, 0);
663 /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
664 * VM_STACK case, but they are still used to monitor the size of the
665 * process stack so we can check the stack rlimit.
667 vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
668 vmspace->vm_maxsaddr = (char *)USRSTACK - maxssiz;
674 * Copy out argument and environment strings from the old process
675 * address space into the temporary string buffer.
678 exec_copyin_args(struct image_args *args, char *fname,
679 enum exec_path_segflg segflg, char **argv, char **envv)
685 bzero(args, sizeof(*args));
686 args->buf = (char *) kmem_alloc_wait(exec_map, PATH_MAX + ARG_MAX);
687 if (args->buf == NULL)
689 args->begin_argv = args->buf;
690 args->endp = args->begin_argv;
691 args->space = ARG_MAX;
693 args->fname = args->buf + ARG_MAX;
696 * Copy the file name.
698 if (segflg == PATH_SYSSPACE) {
699 error = copystr(fname, args->fname, PATH_MAX, &length);
700 } else if (segflg == PATH_USERSPACE) {
701 error = copyinstr(fname, args->fname, PATH_MAX, &length);
705 * Extract argument strings. argv may not be NULL. The argv
706 * array is terminated by a NULL entry. We special-case the
707 * situation where argv[0] is NULL by passing { filename, NULL }
708 * to the new program to guarentee that the interpreter knows what
709 * file to open in case we exec an interpreted file. Note that
710 * a NULL argv[0] terminates the argv[] array.
712 * XXX the special-casing of argv[0] is historical and needs to be
718 while ((argp = (caddr_t)(intptr_t)fuword(argv++)) != NULL) {
719 if (argp == (caddr_t)-1) {
723 error = copyinstr(argp, args->endp,
724 args->space, &length);
726 if (error == ENAMETOOLONG)
730 args->space -= length;
731 args->endp += length;
734 if (args->argc == 0 && error == 0) {
735 length = strlen(args->fname) + 1;
736 if (length > args->space) {
739 bcopy(args->fname, args->endp, length);
740 args->space -= length;
741 args->endp += length;
747 args->begin_envv = args->endp;
750 * extract environment strings. envv may be NULL.
752 if (envv && error == 0) {
753 while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
754 if (envp == (caddr_t) -1) {
758 error = copyinstr(envp, args->endp, args->space,
761 if (error == ENAMETOOLONG)
765 args->space -= length;
766 args->endp += length;
774 exec_free_args(struct image_args *args)
777 kmem_free_wakeup(exec_map,
778 (vm_offset_t)args->buf, PATH_MAX + ARG_MAX);
784 * Copy strings out to the new process address space, constructing
785 * new arg and env vector tables. Return a pointer to the base
786 * so that it can be used as the initial stack pointer.
789 exec_copyout_strings(struct image_params *imgp)
791 int argc, envc, sgap;
793 char *stringp, *destp;
794 register_t *stack_base;
795 struct ps_strings *arginfo;
799 * Calculate string base and vector table pointers.
800 * Also deal with signal trampoline code for this exec type.
802 arginfo = (struct ps_strings *)PS_STRINGS;
803 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
804 if (stackgap_random != 0)
805 sgap = ALIGN(arc4random() & (stackgap_random - 1));
808 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - sgap -
809 roundup((ARG_MAX - imgp->args->space), sizeof(char *));
815 copyout(imgp->proc->p_sysent->sv_sigcode,
816 ((caddr_t)arginfo - szsigcode), szsigcode);
819 * If we have a valid auxargs ptr, prepare some room
822 * The '+ 2' is for the null pointers at the end of each of the
823 * arg and env vector sets, and 'AT_COUNT*2' is room for the
827 vectp = (char **)(destp - (imgp->args->argc +
828 imgp->args->envc + 2 + AT_COUNT * 2) * sizeof(char*));
830 vectp = (char **)(destp - (imgp->args->argc +
831 imgp->args->envc + 2) * sizeof(char*));
835 * NOTE: don't bother aligning the stack here for GCC 2.x, it will
836 * be done in crt1.o. Note that GCC 3.x aligns the stack in main.
840 * vectp also becomes our initial stack base
842 stack_base = (register_t *)vectp;
844 stringp = imgp->args->begin_argv;
845 argc = imgp->args->argc;
846 envc = imgp->args->envc;
849 * Copy out strings - arguments and environment.
851 copyout(stringp, destp, ARG_MAX - imgp->args->space);
854 * Fill in "ps_strings" struct for ps, w, etc.
856 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
857 suword(&arginfo->ps_nargvstr, argc);
860 * Fill in argument portion of vector table.
862 for (; argc > 0; --argc) {
863 suword(vectp++, (long)(intptr_t)destp);
864 while (*stringp++ != 0)
869 /* a null vector table pointer separates the argp's from the envp's */
872 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
873 suword(&arginfo->ps_nenvstr, envc);
876 * Fill in environment portion of vector table.
878 for (; envc > 0; --envc) {
879 suword(vectp++, (long)(intptr_t)destp);
880 while (*stringp++ != 0)
885 /* end of vector table is a null pointer */
892 * Check permissions of file to execute.
893 * Return 0 for success or error code on failure.
896 exec_check_permissions(struct image_params *imgp)
898 struct proc *p = imgp->proc;
899 struct vnode *vp = imgp->vp;
900 struct vattr *attr = imgp->attr;
901 struct thread *td = p->p_thread;
904 /* Get file attributes */
905 error = VOP_GETATTR(vp, attr, td);
910 * 1) Check if file execution is disabled for the filesystem that this
912 * 2) Insure that at least one execute bit is on - otherwise root
913 * will always succeed, and we don't want to happen unless the
914 * file really is executable.
915 * 3) Insure that the file is a regular file.
917 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
918 ((attr->va_mode & 0111) == 0) ||
919 (attr->va_type != VREG)) {
924 * Zero length files can't be exec'd
926 if (attr->va_size == 0)
930 * Check for execute permission to file based on current credentials.
932 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
937 * Check number of open-for-writes on the file and deny execution
940 if (vp->v_writecount)
944 * Call filesystem specific open routine, which allows us to read,
945 * write, and mmap the file. Without the VOP_OPEN we can only
948 error = VOP_OPEN(vp, FREAD, p->p_ucred, NULL, td);
956 * Exec handler registration
959 exec_register(const struct execsw *execsw_arg)
961 const struct execsw **es, **xs, **newexecsw;
962 int count = 2; /* New slot and trailing NULL */
965 for (es = execsw; *es; es++)
967 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
968 if (newexecsw == NULL)
972 for (es = execsw; *es; es++)
977 free(execsw, M_TEMP);
983 exec_unregister(const struct execsw *execsw_arg)
985 const struct execsw **es, **xs, **newexecsw;
989 panic("unregister with no handlers left?");
991 for (es = execsw; *es; es++) {
992 if (*es == execsw_arg)
997 for (es = execsw; *es; es++)
998 if (*es != execsw_arg)
1000 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1001 if (newexecsw == NULL)
1004 for (es = execsw; *es; es++)
1005 if (*es != execsw_arg)
1009 free(execsw, M_TEMP);