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

Cache object: c3bde3ebf717701dae9ee06a0032c55e


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