The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_exec.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1993, David Greenman
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    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.
   13  *
   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
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/7.2/sys/kern/kern_exec.c 197715 2009-10-02 18:09:56Z simon $");
   29 
   30 #include "opt_hwpmc_hooks.h"
   31 #include "opt_kdtrace.h"
   32 #include "opt_ktrace.h"
   33 #include "opt_mac.h"
   34 #include "opt_vm.h"
   35 
   36 #include <sys/param.h>
   37 #include <sys/systm.h>
   38 #include <sys/eventhandler.h>
   39 #include <sys/lock.h>
   40 #include <sys/mutex.h>
   41 #include <sys/sysproto.h>
   42 #include <sys/signalvar.h>
   43 #include <sys/kernel.h>
   44 #include <sys/mount.h>
   45 #include <sys/filedesc.h>
   46 #include <sys/fcntl.h>
   47 #include <sys/acct.h>
   48 #include <sys/exec.h>
   49 #include <sys/imgact.h>
   50 #include <sys/imgact_elf.h>
   51 #include <sys/wait.h>
   52 #include <sys/malloc.h>
   53 #include <sys/priv.h>
   54 #include <sys/proc.h>
   55 #include <sys/pioctl.h>
   56 #include <sys/namei.h>
   57 #include <sys/resourcevar.h>
   58 #include <sys/sdt.h>
   59 #include <sys/sf_buf.h>
   60 #include <sys/syscallsubr.h>
   61 #include <sys/sysent.h>
   62 #include <sys/shm.h>
   63 #include <sys/sysctl.h>
   64 #include <sys/vnode.h>
   65 #ifdef KTRACE
   66 #include <sys/ktrace.h>
   67 #endif
   68 
   69 #include <vm/vm.h>
   70 #include <vm/vm_param.h>
   71 #include <vm/pmap.h>
   72 #include <vm/vm_page.h>
   73 #include <vm/vm_map.h>
   74 #include <vm/vm_kern.h>
   75 #include <vm/vm_extern.h>
   76 #include <vm/vm_object.h>
   77 #include <vm/vm_pager.h>
   78 
   79 #ifdef  HWPMC_HOOKS
   80 #include <sys/pmckern.h>
   81 #endif
   82 
   83 #include <machine/reg.h>
   84 
   85 #include <security/audit/audit.h>
   86 #include <security/mac/mac_framework.h>
   87 
   88 #ifdef KDTRACE_HOOKS
   89 #include <sys/dtrace_bsd.h>
   90 dtrace_execexit_func_t  dtrace_fasttrap_exec;
   91 #endif
   92 
   93 SDT_PROVIDER_DECLARE(proc);
   94 SDT_PROBE_DEFINE(proc, kernel, , exec);
   95 SDT_PROBE_ARGTYPE(proc, kernel, , exec, 0, "char *");
   96 SDT_PROBE_DEFINE(proc, kernel, , exec_failure);
   97 SDT_PROBE_ARGTYPE(proc, kernel, , exec_failure, 0, "int");
   98 SDT_PROBE_DEFINE(proc, kernel, , exec_success);
   99 SDT_PROBE_ARGTYPE(proc, kernel, , exec_success, 0, "char *");
  100 
  101 MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
  102 
  103 static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS);
  104 static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS);
  105 static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS);
  106 static int do_execve(struct thread *td, struct image_args *args,
  107     struct mac *mac_p);
  108 static void exec_free_args(struct image_args *);
  109 
  110 /* XXX This should be vm_size_t. */
  111 SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD,
  112     NULL, 0, sysctl_kern_ps_strings, "LU", "");
  113 
  114 /* XXX This should be vm_size_t. */
  115 SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD,
  116     NULL, 0, sysctl_kern_usrstack, "LU", "");
  117 
  118 SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD,
  119     NULL, 0, sysctl_kern_stackprot, "I", "");
  120 
  121 u_long ps_arg_cache_limit = PAGE_SIZE / 16;
  122 SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 
  123     &ps_arg_cache_limit, 0, "");
  124 
  125 static int map_at_zero = 1;
  126 TUNABLE_INT("security.bsd.map_at_zero", &map_at_zero);
  127 SYSCTL_INT(_security_bsd, OID_AUTO, map_at_zero, CTLFLAG_RW, &map_at_zero, 0,
  128     "Permit processes to map an object at virtual address 0.");
  129 
  130 static int
  131 sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS)
  132 {
  133         struct proc *p;
  134         int error;
  135 
  136         p = curproc;
  137 #ifdef SCTL_MASK32
  138         if (req->flags & SCTL_MASK32) {
  139                 unsigned int val;
  140                 val = (unsigned int)p->p_sysent->sv_psstrings;
  141                 error = SYSCTL_OUT(req, &val, sizeof(val));
  142         } else
  143 #endif
  144                 error = SYSCTL_OUT(req, &p->p_sysent->sv_psstrings,
  145                    sizeof(p->p_sysent->sv_psstrings));
  146         return error;
  147 }
  148 
  149 static int
  150 sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS)
  151 {
  152         struct proc *p;
  153         int error;
  154 
  155         p = curproc;
  156 #ifdef SCTL_MASK32
  157         if (req->flags & SCTL_MASK32) {
  158                 unsigned int val;
  159                 val = (unsigned int)p->p_sysent->sv_usrstack;
  160                 error = SYSCTL_OUT(req, &val, sizeof(val));
  161         } else
  162 #endif
  163                 error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack,
  164                     sizeof(p->p_sysent->sv_usrstack));
  165         return error;
  166 }
  167 
  168 static int
  169 sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS)
  170 {
  171         struct proc *p;
  172 
  173         p = curproc;
  174         return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot,
  175             sizeof(p->p_sysent->sv_stackprot)));
  176 }
  177 
  178 /*
  179  * Each of the items is a pointer to a `const struct execsw', hence the
  180  * double pointer here.
  181  */
  182 static const struct execsw **execsw;
  183 
  184 #ifndef _SYS_SYSPROTO_H_
  185 struct execve_args {
  186         char    *fname; 
  187         char    **argv;
  188         char    **envv; 
  189 };
  190 #endif
  191 
  192 int
  193 execve(td, uap)
  194         struct thread *td;
  195         struct execve_args /* {
  196                 char *fname;
  197                 char **argv;
  198                 char **envv;
  199         } */ *uap;
  200 {
  201         int error;
  202         struct image_args args;
  203 
  204         error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
  205             uap->argv, uap->envv);
  206         if (error == 0)
  207                 error = kern_execve(td, &args, NULL);
  208         return (error);
  209 }
  210 
  211 #ifndef _SYS_SYSPROTO_H_
  212 struct __mac_execve_args {
  213         char    *fname;
  214         char    **argv;
  215         char    **envv;
  216         struct mac      *mac_p;
  217 };
  218 #endif
  219 
  220 int
  221 __mac_execve(td, uap)
  222         struct thread *td;
  223         struct __mac_execve_args /* {
  224                 char *fname;
  225                 char **argv;
  226                 char **envv;
  227                 struct mac *mac_p;
  228         } */ *uap;
  229 {
  230 #ifdef MAC
  231         int error;
  232         struct image_args args;
  233 
  234         error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
  235             uap->argv, uap->envv);
  236         if (error == 0)
  237                 error = kern_execve(td, &args, uap->mac_p);
  238         return (error);
  239 #else
  240         return (ENOSYS);
  241 #endif
  242 }
  243 
  244 /*
  245  * XXX: kern_execve has the astonishing property of not always returning to
  246  * the caller.  If sufficiently bad things happen during the call to
  247  * do_execve(), it can end up calling exit1(); as a result, callers must
  248  * avoid doing anything which they might need to undo (e.g., allocating
  249  * memory).
  250  */
  251 int
  252 kern_execve(td, args, mac_p)
  253         struct thread *td;
  254         struct image_args *args;
  255         struct mac *mac_p;
  256 {
  257         struct proc *p = td->td_proc;
  258         int error;
  259 
  260         AUDIT_ARG(argv, args->begin_argv, args->argc,
  261             args->begin_envv - args->begin_argv);
  262         AUDIT_ARG(envv, args->begin_envv, args->envc,
  263             args->endp - args->begin_envv);
  264         if (p->p_flag & P_HADTHREADS) {
  265                 PROC_LOCK(p);
  266                 if (thread_single(SINGLE_BOUNDARY)) {
  267                         PROC_UNLOCK(p);
  268                         exec_free_args(args);
  269                         return (ERESTART);      /* Try again later. */
  270                 }
  271                 PROC_UNLOCK(p);
  272         }
  273 
  274         error = do_execve(td, args, mac_p);
  275 
  276         if (p->p_flag & P_HADTHREADS) {
  277                 PROC_LOCK(p);
  278                 /*
  279                  * If success, we upgrade to SINGLE_EXIT state to
  280                  * force other threads to suicide.
  281                  */
  282                 if (error == 0)
  283                         thread_single(SINGLE_EXIT);
  284                 else
  285                         thread_single_end();
  286                 PROC_UNLOCK(p);
  287         }
  288 
  289         return (error);
  290 }
  291 
  292 /*
  293  * In-kernel implementation of execve().  All arguments are assumed to be
  294  * userspace pointers from the passed thread.
  295  */
  296 static int
  297 do_execve(td, args, mac_p)
  298         struct thread *td;
  299         struct image_args *args;
  300         struct mac *mac_p;
  301 {
  302         struct proc *p = td->td_proc;
  303         struct nameidata nd, *ndp;
  304         struct ucred *newcred = NULL, *oldcred;
  305         struct uidinfo *euip;
  306         register_t *stack_base;
  307         int error, len, i;
  308         struct image_params image_params, *imgp;
  309         struct vattr attr;
  310         int (*img_first)(struct image_params *);
  311         struct pargs *oldargs = NULL, *newargs = NULL;
  312         struct sigacts *oldsigacts, *newsigacts;
  313 #ifdef KTRACE
  314         struct vnode *tracevp = NULL;
  315         struct ucred *tracecred = NULL;
  316 #endif
  317         struct vnode *textvp = NULL;
  318         int credential_changing;
  319         int vfslocked;
  320         int textset;
  321 #ifdef MAC
  322         struct label *interplabel = NULL;
  323         int will_transition;
  324 #endif
  325 #ifdef HWPMC_HOOKS
  326         struct pmckern_procexec pe;
  327 #endif
  328 
  329         vfslocked = 0;
  330         imgp = &image_params;
  331 
  332         /*
  333          * Lock the process and set the P_INEXEC flag to indicate that
  334          * it should be left alone until we're done here.  This is
  335          * necessary to avoid race conditions - e.g. in ptrace() -
  336          * that might allow a local user to illicitly obtain elevated
  337          * privileges.
  338          */
  339         PROC_LOCK(p);
  340         KASSERT((p->p_flag & P_INEXEC) == 0,
  341             ("%s(): process already has P_INEXEC flag", __func__));
  342         p->p_flag |= P_INEXEC;
  343         PROC_UNLOCK(p);
  344 
  345         /*
  346          * Initialize part of the common data
  347          */
  348         imgp->proc = p;
  349         imgp->execlabel = NULL;
  350         imgp->attr = &attr;
  351         imgp->entry_addr = 0;
  352         imgp->vmspace_destroyed = 0;
  353         imgp->interpreted = 0;
  354         imgp->opened = 0;
  355         imgp->interpreter_name = args->buf + PATH_MAX + ARG_MAX;
  356         imgp->auxargs = NULL;
  357         imgp->vp = NULL;
  358         imgp->object = NULL;
  359         imgp->firstpage = NULL;
  360         imgp->ps_strings = 0;
  361         imgp->auxarg_size = 0;
  362         imgp->args = args;
  363 
  364 #ifdef MAC
  365         error = mac_execve_enter(imgp, mac_p);
  366         if (error)
  367                 goto exec_fail;
  368 #endif
  369 
  370         imgp->image_header = NULL;
  371 
  372         /*
  373          * Translate the file name. namei() returns a vnode pointer
  374          *      in ni_vp amoung other things.
  375          *
  376          * XXXAUDIT: It would be desirable to also audit the name of the
  377          * interpreter if this is an interpreted binary.
  378          */
  379         ndp = &nd;
  380         NDINIT(ndp, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME | MPSAFE |
  381             AUDITVNODE1, UIO_SYSSPACE, args->fname, td);
  382 
  383         SDT_PROBE(proc, kernel, , exec, args->fname, 0, 0, 0, 0 );
  384 
  385 interpret:
  386         error = namei(ndp);
  387         if (error)
  388                 goto exec_fail;
  389 
  390         vfslocked = NDHASGIANT(ndp);
  391         imgp->vp = ndp->ni_vp;
  392 
  393         /*
  394          * Check file permissions (also 'opens' file)
  395          */
  396         error = exec_check_permissions(imgp);
  397         if (error)
  398                 goto exec_fail_dealloc;
  399 
  400         imgp->object = imgp->vp->v_object;
  401         if (imgp->object != NULL)
  402                 vm_object_reference(imgp->object);
  403 
  404         /*
  405          * Set VV_TEXT now so no one can write to the executable while we're
  406          * activating it.
  407          *
  408          * Remember if this was set before and unset it in case this is not
  409          * actually an executable image.
  410          */
  411         textset = imgp->vp->v_vflag & VV_TEXT;
  412         imgp->vp->v_vflag |= VV_TEXT;
  413 
  414         error = exec_map_first_page(imgp);
  415         if (error)
  416                 goto exec_fail_dealloc;
  417 
  418         imgp->proc->p_osrel = 0;
  419         /*
  420          *      If the current process has a special image activator it
  421          *      wants to try first, call it.   For example, emulating shell
  422          *      scripts differently.
  423          */
  424         error = -1;
  425         if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
  426                 error = img_first(imgp);
  427 
  428         /*
  429          *      Loop through the list of image activators, calling each one.
  430          *      An activator returns -1 if there is no match, 0 on success,
  431          *      and an error otherwise.
  432          */
  433         for (i = 0; error == -1 && execsw[i]; ++i) {
  434                 if (execsw[i]->ex_imgact == NULL ||
  435                     execsw[i]->ex_imgact == img_first) {
  436                         continue;
  437                 }
  438                 error = (*execsw[i]->ex_imgact)(imgp);
  439         }
  440 
  441         if (error) {
  442                 if (error == -1) {
  443                         if (textset == 0)
  444                                 imgp->vp->v_vflag &= ~VV_TEXT;
  445                         error = ENOEXEC;
  446                 }
  447                 goto exec_fail_dealloc;
  448         }
  449 
  450         /*
  451          * Special interpreter operation, cleanup and loop up to try to
  452          * activate the interpreter.
  453          */
  454         if (imgp->interpreted) {
  455                 exec_unmap_first_page(imgp);
  456                 /*
  457                  * VV_TEXT needs to be unset for scripts.  There is a short
  458                  * period before we determine that something is a script where
  459                  * VV_TEXT will be set. The vnode lock is held over this
  460                  * entire period so nothing should illegitimately be blocked.
  461                  */
  462                 imgp->vp->v_vflag &= ~VV_TEXT;
  463                 /* free name buffer and old vnode */
  464                 NDFREE(ndp, NDF_ONLY_PNBUF);
  465 #ifdef MAC
  466                 interplabel = mac_vnode_label_alloc();
  467                 mac_copy_vnode_label(ndp->ni_vp->v_label, interplabel);
  468 #endif
  469                 if (imgp->opened) {
  470                         VOP_CLOSE(ndp->ni_vp, FREAD, td->td_ucred, td);
  471                         imgp->opened = 0;
  472                 }
  473                 vput(ndp->ni_vp);
  474                 vm_object_deallocate(imgp->object);
  475                 imgp->object = NULL;
  476                 VFS_UNLOCK_GIANT(vfslocked);
  477                 vfslocked = 0;
  478                 /* set new name to that of the interpreter */
  479                 NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME | MPSAFE,
  480                     UIO_SYSSPACE, imgp->interpreter_name, td);
  481                 goto interpret;
  482         }
  483 
  484         /*
  485          * NB: We unlock the vnode here because it is believed that none
  486          * of the sv_copyout_strings/sv_fixup operations require the vnode.
  487          */
  488         VOP_UNLOCK(imgp->vp, 0, td);
  489         /*
  490          * Copy out strings (args and env) and initialize stack base
  491          */
  492         if (p->p_sysent->sv_copyout_strings)
  493                 stack_base = (*p->p_sysent->sv_copyout_strings)(imgp);
  494         else
  495                 stack_base = exec_copyout_strings(imgp);
  496 
  497         /*
  498          * If custom stack fixup routine present for this process
  499          * let it do the stack setup.
  500          * Else stuff argument count as first item on stack
  501          */
  502         if (p->p_sysent->sv_fixup != NULL)
  503                 (*p->p_sysent->sv_fixup)(&stack_base, imgp);
  504         else
  505                 suword(--stack_base, imgp->args->argc);
  506 
  507         /*
  508          * For security and other reasons, the file descriptor table cannot
  509          * be shared after an exec.
  510          */
  511         fdunshare(p, td);
  512 
  513         /*
  514          * Malloc things before we need locks.
  515          */
  516         newcred = crget();
  517         euip = uifind(attr.va_uid);
  518         i = imgp->args->begin_envv - imgp->args->begin_argv;
  519         /* Cache arguments if they fit inside our allowance */
  520         if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
  521                 newargs = pargs_alloc(i);
  522                 bcopy(imgp->args->begin_argv, newargs->ar_args, i);
  523         }
  524 
  525         /* close files on exec */
  526         fdcloseexec(td);
  527         vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
  528 
  529         /* Get a reference to the vnode prior to locking the proc */
  530         VREF(ndp->ni_vp);
  531 
  532         /*
  533          * For security and other reasons, signal handlers cannot
  534          * be shared after an exec. The new process gets a copy of the old
  535          * handlers. In execsigs(), the new process will have its signals
  536          * reset.
  537          */
  538         PROC_LOCK(p);
  539         if (sigacts_shared(p->p_sigacts)) {
  540                 oldsigacts = p->p_sigacts;
  541                 PROC_UNLOCK(p);
  542                 newsigacts = sigacts_alloc();
  543                 sigacts_copy(newsigacts, oldsigacts);
  544                 PROC_LOCK(p);
  545                 p->p_sigacts = newsigacts;
  546         } else
  547                 oldsigacts = NULL;
  548 
  549         /* Stop profiling */
  550         stopprofclock(p);
  551 
  552         /* reset caught signals */
  553         execsigs(p);
  554 
  555         /* name this process - nameiexec(p, ndp) */
  556         len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
  557         bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
  558         p->p_comm[len] = 0;
  559 
  560         /*
  561          * mark as execed, wakeup the process that vforked (if any) and tell
  562          * it that it now has its own resources back
  563          */
  564         p->p_flag |= P_EXEC;
  565         if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
  566                 p->p_flag &= ~P_PPWAIT;
  567                 wakeup(p->p_pptr);
  568         }
  569 
  570         /*
  571          * Implement image setuid/setgid.
  572          *
  573          * Don't honor setuid/setgid if the filesystem prohibits it or if
  574          * the process is being traced.
  575          *
  576          * XXXMAC: For the time being, use NOSUID to also prohibit
  577          * transitions on the file system.
  578          */
  579         oldcred = p->p_ucred;
  580         credential_changing = 0;
  581         credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid !=
  582             attr.va_uid;
  583         credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid !=
  584             attr.va_gid;
  585 #ifdef MAC
  586         will_transition = mac_execve_will_transition(oldcred, imgp->vp,
  587             interplabel, imgp);
  588         credential_changing |= will_transition;
  589 #endif
  590 
  591         if (credential_changing &&
  592             (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
  593             (p->p_flag & P_TRACED) == 0) {
  594                 /*
  595                  * Turn off syscall tracing for set-id programs, except for
  596                  * root.  Record any set-id flags first to make sure that
  597                  * we do not regain any tracing during a possible block.
  598                  */
  599                 setsugid(p);
  600 
  601 #ifdef KTRACE
  602                 if (p->p_tracevp != NULL &&
  603                     priv_check_cred(oldcred, PRIV_DEBUG_DIFFCRED, 0)) {
  604                         mtx_lock(&ktrace_mtx);
  605                         p->p_traceflag = 0;
  606                         tracevp = p->p_tracevp;
  607                         p->p_tracevp = NULL;
  608                         tracecred = p->p_tracecred;
  609                         p->p_tracecred = NULL;
  610                         mtx_unlock(&ktrace_mtx);
  611                 }
  612 #endif
  613                 /*
  614                  * Close any file descriptors 0..2 that reference procfs,
  615                  * then make sure file descriptors 0..2 are in use.
  616                  *
  617                  * setugidsafety() may call closef() and then pfind()
  618                  * which may grab the process lock.
  619                  * fdcheckstd() may call falloc() which may block to
  620                  * allocate memory, so temporarily drop the process lock.
  621                  */
  622                 PROC_UNLOCK(p);
  623                 setugidsafety(td);
  624                 VOP_UNLOCK(imgp->vp, 0, td);
  625                 error = fdcheckstd(td);
  626                 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
  627                 if (error != 0)
  628                         goto done1;
  629                 PROC_LOCK(p);
  630                 /*
  631                  * Set the new credentials.
  632                  */
  633                 crcopy(newcred, oldcred);
  634                 if (attr.va_mode & VSUID)
  635                         change_euid(newcred, euip);
  636                 if (attr.va_mode & VSGID)
  637                         change_egid(newcred, attr.va_gid);
  638 #ifdef MAC
  639                 if (will_transition) {
  640                         mac_execve_transition(oldcred, newcred, imgp->vp,
  641                             interplabel, imgp);
  642                 }
  643 #endif
  644                 /*
  645                  * Implement correct POSIX saved-id behavior.
  646                  *
  647                  * XXXMAC: Note that the current logic will save the
  648                  * uid and gid if a MAC domain transition occurs, even
  649                  * though maybe it shouldn't.
  650                  */
  651                 change_svuid(newcred, newcred->cr_uid);
  652                 change_svgid(newcred, newcred->cr_gid);
  653                 p->p_ucred = newcred;
  654                 newcred = NULL;
  655         } else {
  656                 if (oldcred->cr_uid == oldcred->cr_ruid &&
  657                     oldcred->cr_gid == oldcred->cr_rgid)
  658                         p->p_flag &= ~P_SUGID;
  659                 /*
  660                  * Implement correct POSIX saved-id behavior.
  661                  *
  662                  * XXX: It's not clear that the existing behavior is
  663                  * POSIX-compliant.  A number of sources indicate that the
  664                  * saved uid/gid should only be updated if the new ruid is
  665                  * not equal to the old ruid, or the new euid is not equal
  666                  * to the old euid and the new euid is not equal to the old
  667                  * ruid.  The FreeBSD code always updates the saved uid/gid.
  668                  * Also, this code uses the new (replaced) euid and egid as
  669                  * the source, which may or may not be the right ones to use.
  670                  */
  671                 if (oldcred->cr_svuid != oldcred->cr_uid ||
  672                     oldcred->cr_svgid != oldcred->cr_gid) {
  673                         crcopy(newcred, oldcred);
  674                         change_svuid(newcred, newcred->cr_uid);
  675                         change_svgid(newcred, newcred->cr_gid);
  676                         p->p_ucred = newcred;
  677                         newcred = NULL;
  678                 }
  679         }
  680 
  681         /*
  682          * Store the vp for use in procfs.  This vnode was referenced prior
  683          * to locking the proc lock.
  684          */
  685         textvp = p->p_textvp;
  686         p->p_textvp = ndp->ni_vp;
  687 
  688 #ifdef KDTRACE_HOOKS
  689         /*
  690          * Tell the DTrace fasttrap provider about the exec if it
  691          * has declared an interest.
  692          */
  693         if (dtrace_fasttrap_exec)
  694                 dtrace_fasttrap_exec(p);
  695 #endif
  696 
  697         /*
  698          * Notify others that we exec'd, and clear the P_INEXEC flag
  699          * as we're now a bona fide freshly-execed process.
  700          */
  701         KNOTE_LOCKED(&p->p_klist, NOTE_EXEC);
  702         p->p_flag &= ~P_INEXEC;
  703 
  704         /*
  705          * If tracing the process, trap to debugger so breakpoints
  706          * can be set before the program executes.
  707          * Use tdsignal to deliver signal to current thread, use
  708          * psignal may cause the signal to be delivered to wrong thread
  709          * because that thread will exit, remember we are going to enter
  710          * single thread mode.
  711          */
  712         if (p->p_flag & P_TRACED)
  713                 tdsignal(p, td, SIGTRAP, NULL);
  714 
  715         /* clear "fork but no exec" flag, as we _are_ execing */
  716         p->p_acflag &= ~AFORK;
  717 
  718         /*
  719          * Free any previous argument cache and replace it with
  720          * the new argument cache, if any.
  721          */
  722         oldargs = p->p_args;
  723         p->p_args = newargs;
  724         newargs = NULL;
  725 
  726 #ifdef  HWPMC_HOOKS
  727         /*
  728          * Check if system-wide sampling is in effect or if the
  729          * current process is using PMCs.  If so, do exec() time
  730          * processing.  This processing needs to happen AFTER the
  731          * P_INEXEC flag is cleared.
  732          *
  733          * The proc lock needs to be released before taking the PMC
  734          * SX.
  735          */
  736         if (PMC_SYSTEM_SAMPLING_ACTIVE() || PMC_PROC_IS_USING_PMCS(p)) {
  737                 PROC_UNLOCK(p);
  738                 pe.pm_credentialschanged = credential_changing;
  739                 pe.pm_entryaddr = imgp->entry_addr;
  740 
  741                 PMC_CALL_HOOK_X(td, PMC_FN_PROCESS_EXEC, (void *) &pe);
  742         } else
  743                 PROC_UNLOCK(p);
  744 #else  /* !HWPMC_HOOKS */
  745         PROC_UNLOCK(p);
  746 #endif
  747 
  748         /* Set values passed into the program in registers. */
  749         if (p->p_sysent->sv_setregs)
  750                 (*p->p_sysent->sv_setregs)(td, imgp->entry_addr,
  751                     (u_long)(uintptr_t)stack_base, imgp->ps_strings);
  752         else
  753                 exec_setregs(td, imgp->entry_addr,
  754                     (u_long)(uintptr_t)stack_base, imgp->ps_strings);
  755 
  756         vfs_mark_atime(imgp->vp, td);
  757 
  758         SDT_PROBE(proc, kernel, , exec_success, args->fname, 0, 0, 0, 0);
  759 done1:
  760         /*
  761          * Free any resources malloc'd earlier that we didn't use.
  762          */
  763         uifree(euip);
  764         if (newcred == NULL)
  765                 crfree(oldcred);
  766         else
  767                 crfree(newcred);
  768         VOP_UNLOCK(imgp->vp, 0, td);
  769 
  770         /*
  771          * Handle deferred decrement of ref counts.
  772          */
  773         if (textvp != NULL) {
  774                 int tvfslocked;
  775 
  776                 tvfslocked = VFS_LOCK_GIANT(textvp->v_mount);
  777                 vrele(textvp);
  778                 VFS_UNLOCK_GIANT(tvfslocked);
  779         }
  780         if (ndp->ni_vp && error != 0)
  781                 vrele(ndp->ni_vp);
  782 #ifdef KTRACE
  783         if (tracevp != NULL) {
  784                 int tvfslocked;
  785 
  786                 tvfslocked = VFS_LOCK_GIANT(tracevp->v_mount);
  787                 vrele(tracevp);
  788                 VFS_UNLOCK_GIANT(tvfslocked);
  789         }
  790         if (tracecred != NULL)
  791                 crfree(tracecred);
  792 #endif
  793         vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
  794         pargs_drop(oldargs);
  795         pargs_drop(newargs);
  796         if (oldsigacts != NULL)
  797                 sigacts_free(oldsigacts);
  798 
  799 exec_fail_dealloc:
  800 
  801         /*
  802          * free various allocated resources
  803          */
  804         if (imgp->firstpage != NULL)
  805                 exec_unmap_first_page(imgp);
  806 
  807         if (imgp->vp != NULL) {
  808                 NDFREE(ndp, NDF_ONLY_PNBUF);
  809                 if (imgp->opened)
  810                         VOP_CLOSE(imgp->vp, FREAD, td->td_ucred, td);
  811                 vput(imgp->vp);
  812         }
  813 
  814         if (imgp->object != NULL)
  815                 vm_object_deallocate(imgp->object);
  816 
  817         if (error == 0) {
  818                 /*
  819                  * Stop the process here if its stop event mask has
  820                  * the S_EXEC bit set.
  821                  */
  822                 STOPEVENT(p, S_EXEC, 0);
  823                 goto done2;
  824         }
  825 
  826 exec_fail:
  827         /* we're done here, clear P_INEXEC */
  828         PROC_LOCK(p);
  829         p->p_flag &= ~P_INEXEC;
  830         PROC_UNLOCK(p);
  831 
  832         SDT_PROBE(proc, kernel, , exec_failure, error, 0, 0, 0, 0);
  833 
  834 done2:
  835 #ifdef MAC
  836         mac_execve_exit(imgp);
  837         if (interplabel != NULL)
  838                 mac_vnode_label_free(interplabel);
  839 #endif
  840         VFS_UNLOCK_GIANT(vfslocked);
  841         exec_free_args(args);
  842 
  843         if (error && imgp->vmspace_destroyed) {
  844                 /* sorry, no more process anymore. exit gracefully */
  845                 exit1(td, W_EXITCODE(0, SIGABRT));
  846                 /* NOT REACHED */
  847         }
  848         return (error);
  849 }
  850 
  851 int
  852 exec_map_first_page(imgp)
  853         struct image_params *imgp;
  854 {
  855         int rv, i;
  856         int initial_pagein;
  857         vm_page_t ma[VM_INITIAL_PAGEIN];
  858         vm_object_t object;
  859 
  860         if (imgp->firstpage != NULL)
  861                 exec_unmap_first_page(imgp);
  862 
  863         object = imgp->vp->v_object;
  864         if (object == NULL)
  865                 return (EACCES);
  866         VM_OBJECT_LOCK(object);
  867 #if VM_NRESERVLEVEL > 0
  868         if ((object->flags & OBJ_COLORED) == 0) {
  869                 object->flags |= OBJ_COLORED;
  870                 object->pg_color = 0;
  871         }
  872 #endif
  873         ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
  874         if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
  875                 initial_pagein = VM_INITIAL_PAGEIN;
  876                 if (initial_pagein > object->size)
  877                         initial_pagein = object->size;
  878                 for (i = 1; i < initial_pagein; i++) {
  879                         if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
  880                                 if (ma[i]->valid)
  881                                         break;
  882                                 if ((ma[i]->oflags & VPO_BUSY) || ma[i]->busy)
  883                                         break;
  884                                 vm_page_busy(ma[i]);
  885                         } else {
  886                                 ma[i] = vm_page_alloc(object, i,
  887                                     VM_ALLOC_NORMAL | VM_ALLOC_IFNOTCACHED);
  888                                 if (ma[i] == NULL)
  889                                         break;
  890                         }
  891                 }
  892                 initial_pagein = i;
  893                 rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
  894                 ma[0] = vm_page_lookup(object, 0);
  895                 if ((rv != VM_PAGER_OK) || (ma[0] == NULL) ||
  896                     (ma[0]->valid == 0)) {
  897                         if (ma[0]) {
  898                                 vm_page_lock_queues();
  899                                 vm_page_free(ma[0]);
  900                                 vm_page_unlock_queues();
  901                         }
  902                         VM_OBJECT_UNLOCK(object);
  903                         return (EIO);
  904                 }
  905         }
  906         vm_page_lock_queues();
  907         vm_page_hold(ma[0]);
  908         vm_page_unlock_queues();
  909         vm_page_wakeup(ma[0]);
  910         VM_OBJECT_UNLOCK(object);
  911 
  912         imgp->firstpage = sf_buf_alloc(ma[0], 0);
  913         imgp->image_header = (char *)sf_buf_kva(imgp->firstpage);
  914 
  915         return (0);
  916 }
  917 
  918 void
  919 exec_unmap_first_page(imgp)
  920         struct image_params *imgp;
  921 {
  922         vm_page_t m;
  923 
  924         if (imgp->firstpage != NULL) {
  925                 m = sf_buf_page(imgp->firstpage);
  926                 sf_buf_free(imgp->firstpage);
  927                 imgp->firstpage = NULL;
  928                 vm_page_lock_queues();
  929                 vm_page_unhold(m);
  930                 vm_page_unlock_queues();
  931         }
  932 }
  933 
  934 /*
  935  * Destroy old address space, and allocate a new stack
  936  *      The new stack is only SGROWSIZ large because it is grown
  937  *      automatically in trap.c.
  938  */
  939 int
  940 exec_new_vmspace(imgp, sv)
  941         struct image_params *imgp;
  942         struct sysentvec *sv;
  943 {
  944         int error;
  945         struct proc *p = imgp->proc;
  946         struct vmspace *vmspace = p->p_vmspace;
  947         vm_offset_t sv_minuser, stack_addr;
  948         vm_map_t map;
  949         u_long ssiz;
  950 
  951         imgp->vmspace_destroyed = 1;
  952         imgp->sysent = sv;
  953 
  954         /* May be called with Giant held */
  955         EVENTHANDLER_INVOKE(process_exec, p, imgp);
  956 
  957         /*
  958          * Blow away entire process VM, if address space not shared,
  959          * otherwise, create a new VM space so that other threads are
  960          * not disrupted
  961          */
  962         map = &vmspace->vm_map;
  963         if (map_at_zero)
  964                 sv_minuser = sv->sv_minuser;
  965         else
  966                 sv_minuser = MAX(sv->sv_minuser, PAGE_SIZE);
  967         if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv_minuser &&
  968             vm_map_max(map) == sv->sv_maxuser) {
  969                 shmexit(vmspace);
  970                 pmap_remove_pages(vmspace_pmap(vmspace));
  971                 vm_map_remove(map, vm_map_min(map), vm_map_max(map));
  972         } else {
  973                 error = vmspace_exec(p, sv_minuser, sv->sv_maxuser);
  974                 if (error)
  975                         return (error);
  976                 vmspace = p->p_vmspace;
  977                 map = &vmspace->vm_map;
  978         }
  979 
  980         /* Allocate a new stack */
  981         if (sv->sv_maxssiz != NULL)
  982                 ssiz = *sv->sv_maxssiz;
  983         else
  984                 ssiz = maxssiz;
  985         stack_addr = sv->sv_usrstack - ssiz;
  986         error = vm_map_stack(map, stack_addr, (vm_size_t)ssiz,
  987             sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN);
  988         if (error)
  989                 return (error);
  990 
  991 #ifdef __ia64__
  992         /* Allocate a new register stack */
  993         stack_addr = IA64_BACKINGSTORE;
  994         error = vm_map_stack(map, stack_addr, (vm_size_t)ssiz,
  995             sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP);
  996         if (error)
  997                 return (error);
  998 #endif
  999 
 1000         /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
 1001          * VM_STACK case, but they are still used to monitor the size of the
 1002          * process stack so we can check the stack rlimit.
 1003          */
 1004         vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
 1005         vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - ssiz;
 1006 
 1007         return (0);
 1008 }
 1009 
 1010 /*
 1011  * Copy out argument and environment strings from the old process address
 1012  * space into the temporary string buffer.
 1013  */
 1014 int
 1015 exec_copyin_args(struct image_args *args, char *fname,
 1016     enum uio_seg segflg, char **argv, char **envv)
 1017 {
 1018         char *argp, *envp;
 1019         int error;
 1020         size_t length;
 1021 
 1022         error = 0;
 1023 
 1024         bzero(args, sizeof(*args));
 1025         if (argv == NULL)
 1026                 return (EFAULT);
 1027         /*
 1028          * Allocate temporary demand zeroed space for argument and
 1029          *      environment strings:
 1030          *
 1031          * o ARG_MAX for argument and environment;
 1032          * o MAXSHELLCMDLEN for the name of interpreters.
 1033          */
 1034         args->buf = (char *) kmem_alloc_wait(exec_map,
 1035             PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
 1036         if (args->buf == NULL)
 1037                 return (ENOMEM);
 1038         args->begin_argv = args->buf;
 1039         args->endp = args->begin_argv;
 1040         args->stringspace = ARG_MAX;
 1041 
 1042         args->fname = args->buf + ARG_MAX;
 1043 
 1044         /*
 1045          * Copy the file name.
 1046          */
 1047         error = (segflg == UIO_SYSSPACE) ?
 1048             copystr(fname, args->fname, PATH_MAX, &length) :
 1049             copyinstr(fname, args->fname, PATH_MAX, &length);
 1050         if (error != 0)
 1051                 goto err_exit;
 1052 
 1053         /*
 1054          * extract arguments first
 1055          */
 1056         while ((argp = (caddr_t) (intptr_t) fuword(argv++))) {
 1057                 if (argp == (caddr_t) -1) {
 1058                         error = EFAULT;
 1059                         goto err_exit;
 1060                 }
 1061                 if ((error = copyinstr(argp, args->endp,
 1062                     args->stringspace, &length))) {
 1063                         if (error == ENAMETOOLONG) 
 1064                                 error = E2BIG;
 1065                         goto err_exit;
 1066                 }
 1067                 args->stringspace -= length;
 1068                 args->endp += length;
 1069                 args->argc++;
 1070         }
 1071 
 1072         args->begin_envv = args->endp;
 1073 
 1074         /*
 1075          * extract environment strings
 1076          */
 1077         if (envv) {
 1078                 while ((envp = (caddr_t)(intptr_t)fuword(envv++))) {
 1079                         if (envp == (caddr_t)-1) {
 1080                                 error = EFAULT;
 1081                                 goto err_exit;
 1082                         }
 1083                         if ((error = copyinstr(envp, args->endp,
 1084                             args->stringspace, &length))) {
 1085                                 if (error == ENAMETOOLONG)
 1086                                         error = E2BIG;
 1087                                 goto err_exit;
 1088                         }
 1089                         args->stringspace -= length;
 1090                         args->endp += length;
 1091                         args->envc++;
 1092                 }
 1093         }
 1094 
 1095         return (0);
 1096 
 1097 err_exit:
 1098         exec_free_args(args);
 1099         return (error);
 1100 }
 1101 
 1102 static void
 1103 exec_free_args(struct image_args *args)
 1104 {
 1105 
 1106         if (args->buf) {
 1107                 kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
 1108                     PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
 1109                 args->buf = NULL;
 1110         }
 1111 }
 1112 
 1113 /*
 1114  * Copy strings out to the new process address space, constructing new arg
 1115  * and env vector tables. Return a pointer to the base so that it can be used
 1116  * as the initial stack pointer.
 1117  */
 1118 register_t *
 1119 exec_copyout_strings(imgp)
 1120         struct image_params *imgp;
 1121 {
 1122         int argc, envc;
 1123         char **vectp;
 1124         char *stringp, *destp;
 1125         register_t *stack_base;
 1126         struct ps_strings *arginfo;
 1127         struct proc *p;
 1128         int szsigcode;
 1129 
 1130         /*
 1131          * Calculate string base and vector table pointers.
 1132          * Also deal with signal trampoline code for this exec type.
 1133          */
 1134         p = imgp->proc;
 1135         szsigcode = 0;
 1136         arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
 1137         if (p->p_sysent->sv_szsigcode != NULL)
 1138                 szsigcode = *(p->p_sysent->sv_szsigcode);
 1139         destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
 1140             roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
 1141 
 1142         /*
 1143          * install sigcode
 1144          */
 1145         if (szsigcode)
 1146                 copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo -
 1147                     szsigcode), szsigcode);
 1148 
 1149         /*
 1150          * If we have a valid auxargs ptr, prepare some room
 1151          * on the stack.
 1152          */
 1153         if (imgp->auxargs) {
 1154                 /*
 1155                  * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
 1156                  * lower compatibility.
 1157                  */
 1158                 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
 1159                     (AT_COUNT * 2);
 1160                 /*
 1161                  * The '+ 2' is for the null pointers at the end of each of
 1162                  * the arg and env vector sets,and imgp->auxarg_size is room
 1163                  * for argument of Runtime loader.
 1164                  */
 1165                 vectp = (char **)(destp - (imgp->args->argc +
 1166                     imgp->args->envc + 2 + imgp->auxarg_size) *
 1167                     sizeof(char *));
 1168 
 1169         } else {
 1170                 /*
 1171                  * The '+ 2' is for the null pointers at the end of each of
 1172                  * the arg and env vector sets
 1173                  */
 1174                 vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc + 2) *
 1175                     sizeof(char *));
 1176         }
 1177 
 1178         /*
 1179          * vectp also becomes our initial stack base
 1180          */
 1181         stack_base = (register_t *)vectp;
 1182 
 1183         stringp = imgp->args->begin_argv;
 1184         argc = imgp->args->argc;
 1185         envc = imgp->args->envc;
 1186 
 1187         /*
 1188          * Copy out strings - arguments and environment.
 1189          */
 1190         copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
 1191 
 1192         /*
 1193          * Fill in "ps_strings" struct for ps, w, etc.
 1194          */
 1195         suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
 1196         suword(&arginfo->ps_nargvstr, argc);
 1197 
 1198         /*
 1199          * Fill in argument portion of vector table.
 1200          */
 1201         for (; argc > 0; --argc) {
 1202                 suword(vectp++, (long)(intptr_t)destp);
 1203                 while (*stringp++ != 0)
 1204                         destp++;
 1205                 destp++;
 1206         }
 1207 
 1208         /* a null vector table pointer separates the argp's from the envp's */
 1209         suword(vectp++, 0);
 1210 
 1211         suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
 1212         suword(&arginfo->ps_nenvstr, envc);
 1213 
 1214         /*
 1215          * Fill in environment portion of vector table.
 1216          */
 1217         for (; envc > 0; --envc) {
 1218                 suword(vectp++, (long)(intptr_t)destp);
 1219                 while (*stringp++ != 0)
 1220                         destp++;
 1221                 destp++;
 1222         }
 1223 
 1224         /* end of vector table is a null pointer */
 1225         suword(vectp, 0);
 1226 
 1227         return (stack_base);
 1228 }
 1229 
 1230 /*
 1231  * Check permissions of file to execute.
 1232  *      Called with imgp->vp locked.
 1233  *      Return 0 for success or error code on failure.
 1234  */
 1235 int
 1236 exec_check_permissions(imgp)
 1237         struct image_params *imgp;
 1238 {
 1239         struct vnode *vp = imgp->vp;
 1240         struct vattr *attr = imgp->attr;
 1241         struct thread *td;
 1242         int error;
 1243 
 1244         td = curthread;                 /* XXXKSE */
 1245 
 1246         /* Get file attributes */
 1247         error = VOP_GETATTR(vp, attr, td->td_ucred, td);
 1248         if (error)
 1249                 return (error);
 1250 
 1251 #ifdef MAC
 1252         error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp);
 1253         if (error)
 1254                 return (error);
 1255 #endif
 1256         
 1257         /*
 1258          * 1) Check if file execution is disabled for the filesystem that this
 1259          *      file resides on.
 1260          * 2) Insure that at least one execute bit is on - otherwise root
 1261          *      will always succeed, and we don't want to happen unless the
 1262          *      file really is executable.
 1263          * 3) Insure that the file is a regular file.
 1264          */
 1265         if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
 1266             ((attr->va_mode & 0111) == 0) ||
 1267             (attr->va_type != VREG))
 1268                 return (EACCES);
 1269 
 1270         /*
 1271          * Zero length files can't be exec'd
 1272          */
 1273         if (attr->va_size == 0)
 1274                 return (ENOEXEC);
 1275 
 1276         /*
 1277          *  Check for execute permission to file based on current credentials.
 1278          */
 1279         error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
 1280         if (error)
 1281                 return (error);
 1282 
 1283         /*
 1284          * Check number of open-for-writes on the file and deny execution
 1285          * if there are any.
 1286          */
 1287         if (vp->v_writecount)
 1288                 return (ETXTBSY);
 1289 
 1290         /*
 1291          * Call filesystem specific open routine (which does nothing in the
 1292          * general case).
 1293          */
 1294         error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
 1295         if (error == 0)
 1296                 imgp->opened = 1;
 1297         return (error);
 1298 }
 1299 
 1300 /*
 1301  * Exec handler registration
 1302  */
 1303 int
 1304 exec_register(execsw_arg)
 1305         const struct execsw *execsw_arg;
 1306 {
 1307         const struct execsw **es, **xs, **newexecsw;
 1308         int count = 2;  /* New slot and trailing NULL */
 1309 
 1310         if (execsw)
 1311                 for (es = execsw; *es; es++)
 1312                         count++;
 1313         newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
 1314         if (newexecsw == NULL)
 1315                 return (ENOMEM);
 1316         xs = newexecsw;
 1317         if (execsw)
 1318                 for (es = execsw; *es; es++)
 1319                         *xs++ = *es;
 1320         *xs++ = execsw_arg;
 1321         *xs = NULL;
 1322         if (execsw)
 1323                 free(execsw, M_TEMP);
 1324         execsw = newexecsw;
 1325         return (0);
 1326 }
 1327 
 1328 int
 1329 exec_unregister(execsw_arg)
 1330         const struct execsw *execsw_arg;
 1331 {
 1332         const struct execsw **es, **xs, **newexecsw;
 1333         int count = 1;
 1334 
 1335         if (execsw == NULL)
 1336                 panic("unregister with no handlers left?\n");
 1337 
 1338         for (es = execsw; *es; es++) {
 1339                 if (*es == execsw_arg)
 1340                         break;
 1341         }
 1342         if (*es == NULL)
 1343                 return (ENOENT);
 1344         for (es = execsw; *es; es++)
 1345                 if (*es != execsw_arg)
 1346                         count++;
 1347         newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
 1348         if (newexecsw == NULL)
 1349                 return (ENOMEM);
 1350         xs = newexecsw;
 1351         for (es = execsw; *es; es++)
 1352                 if (*es != execsw_arg)
 1353                         *xs++ = *es;
 1354         *xs = NULL;
 1355         if (execsw)
 1356                 free(execsw, M_TEMP);
 1357         execsw = newexecsw;
 1358         return (0);
 1359 }

Cache object: 31198ad3f7345a0755cce1319d941b9e


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.