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

Cache object: 238863ddd6e8c3bfdc1257b4a0ec188b


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