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

Cache object: d2c9da958e37c9cb693496ae9a70a4c2


[ 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.