Annotation of src/sys/kern/exec_script.c, revision 1.46

1.46    ! christos    1: /*     $NetBSD: exec_script.c,v 1.45 2005/12/11 12:24:29 christos Exp $        */
1.8       cgd         2: 
1.1       cgd         3: /*
1.14      cgd         4:  * Copyright (c) 1993, 1994, 1996 Christopher G. Demetriou
1.1       cgd         5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *      This product includes software developed by Christopher G. Demetriou.
                     18:  * 4. The name of the author may not be used to endorse or promote products
1.3       jtc        19:  *    derived from this software without specific prior written permission
1.1       cgd        20:  *
                     21:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     22:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     23:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     24:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     25:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     26:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     27:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     28:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     29:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     30:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     31:  */
1.30      lukem      32: 
                     33: #include <sys/cdefs.h>
1.46    ! christos   34: __KERNEL_RCSID(0, "$NetBSD: exec_script.c,v 1.45 2005/12/11 12:24:29 christos Exp $");
1.1       cgd        35: 
                     36: #if defined(SETUIDSCRIPTS) && !defined(FDSCRIPTS)
                     37: #define FDSCRIPTS              /* Need this for safe set-id scripts. */
                     38: #endif
                     39: 
1.42      christos   40: #include "opt_verified_exec.h"
                     41: 
1.1       cgd        42: #include <sys/param.h>
                     43: #include <sys/systm.h>
                     44: #include <sys/proc.h>
                     45: #include <sys/malloc.h>
                     46: #include <sys/vnode.h>
                     47: #include <sys/namei.h>
                     48: #include <sys/file.h>
1.18      christos   49: #ifdef SETUIDSCRIPTS
                     50: #include <sys/stat.h>
                     51: #endif
1.1       cgd        52: #include <sys/filedesc.h>
                     53: #include <sys/exec.h>
                     54: #include <sys/resourcevar.h>
                     55: 
                     56: #include <sys/exec_script.h>
1.38      matt       57: #include <sys/exec_elf.h>
1.1       cgd        58: 
1.44      elad       59: #ifdef VERIFIED_EXEC
1.39      elad       60: #include <sys/verified_exec.h>
1.44      elad       61: #endif /* VERIFIED_EXEC */
1.39      elad       62: 
1.40      elad       63: #ifdef SYSTRACE
                     64: #include <sys/systrace.h>
                     65: #endif /* SYSTRACE */
                     66: 
1.1       cgd        67: /*
                     68:  * exec_script_makecmds(): Check if it's an executable shell script.
                     69:  *
                     70:  * Given a proc pointer and an exec package pointer, see if the referent
                     71:  * of the epp is in shell script.  If it is, then set thing up so that
                     72:  * the script can be run.  This involves preparing the address space
                     73:  * and arguments for the shell which will run the script.
                     74:  *
                     75:  * This function is ultimately responsible for creating a set of vmcmds
                     76:  * which can be used to build the process's vm space and inserting them
                     77:  * into the exec package.
                     78:  */
                     79: int
1.45      christos   80: exec_script_makecmds(struct lwp *l, struct exec_package *epp)
1.1       cgd        81: {
                     82:        int error, hdrlinelen, shellnamelen, shellarglen;
                     83:        char *hdrstr = epp->ep_hdr;
                     84:        char *cp, *shellname, *shellarg, *oldpnbuf;
                     85:        char **shellargp, **tmpsap;
                     86:        struct vnode *scriptvp;
1.45      christos   87:        struct proc *p = l->l_proc;
1.1       cgd        88: #ifdef SETUIDSCRIPTS
1.18      christos   89:        /* Gcc needs those initialized for spurious uninitialized warning */
                     90:        uid_t script_uid = (uid_t) -1;
                     91:        gid_t script_gid = NOGROUP;
1.1       cgd        92:        u_short script_sbits;
                     93: #endif
                     94: 
                     95:        /*
                     96:         * if the magic isn't that of a shell script, or we've already
                     97:         * done shell script processing for this exec, punt on it.
                     98:         */
                     99:        if ((epp->ep_flags & EXEC_INDIR) != 0 ||
                    100:            epp->ep_hdrvalid < EXEC_SCRIPT_MAGICLEN ||
                    101:            strncmp(hdrstr, EXEC_SCRIPT_MAGIC, EXEC_SCRIPT_MAGICLEN))
                    102:                return ENOEXEC;
                    103: 
                    104:        /*
                    105:         * check that the shell spec is terminated by a newline,
                    106:         * and that it isn't too large.  Don't modify the
                    107:         * buffer unless we're ready to commit to handling it.
                    108:         * (The latter requirement means that we have to check
                    109:         * for both spaces and tabs later on.)
                    110:         */
1.33      perry     111:        hdrlinelen = min(epp->ep_hdrvalid, SCRIPT_HDR_SIZE);
1.1       cgd       112:        for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; cp < hdrstr + hdrlinelen;
                    113:            cp++) {
                    114:                if (*cp == '\n') {
                    115:                        *cp = '\0';
                    116:                        break;
                    117:                }
                    118:        }
                    119:        if (cp >= hdrstr + hdrlinelen)
                    120:                return ENOEXEC;
                    121: 
1.38      matt      122:        /*
                    123:         * If the script has an ELF header, don't exec it.
                    124:         */
                    125:        if (epp->ep_hdrvalid >= sizeof(ELFMAG)-1 &&
                    126:            memcmp(hdrstr, ELFMAG, sizeof(ELFMAG)-1) == 0)
                    127:                return ENOEXEC;
                    128: 
1.1       cgd       129:        shellname = NULL;
                    130:        shellarg = NULL;
1.13      christos  131:        shellarglen = 0;
1.1       cgd       132: 
                    133:        /* strip spaces before the shell name */
                    134:        for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; *cp == ' ' || *cp == '\t';
                    135:            cp++)
                    136:                ;
                    137: 
                    138:        /* collect the shell name; remember it's length for later */
                    139:        shellname = cp;
                    140:        shellnamelen = 0;
                    141:        if (*cp == '\0')
                    142:                goto check_shell;
                    143:        for ( /* cp = cp */ ; *cp != '\0' && *cp != ' ' && *cp != '\t'; cp++)
                    144:                shellnamelen++;
                    145:        if (*cp == '\0')
                    146:                goto check_shell;
                    147:        *cp++ = '\0';
                    148: 
                    149:        /* skip spaces before any argument */
                    150:        for ( /* cp = cp */ ; *cp == ' ' || *cp == '\t'; cp++)
                    151:                ;
                    152:        if (*cp == '\0')
                    153:                goto check_shell;
                    154: 
                    155:        /*
                    156:         * collect the shell argument.  everything after the shell name
                    157:         * is passed as ONE argument; that's the correct (historical)
                    158:         * behaviour.
                    159:         */
                    160:        shellarg = cp;
                    161:        for ( /* cp = cp */ ; *cp != '\0'; cp++)
                    162:                shellarglen++;
                    163:        *cp++ = '\0';
                    164: 
                    165: check_shell:
                    166: #ifdef SETUIDSCRIPTS
                    167:        /*
1.29      thorpej   168:         * MNT_NOSUID has already taken care of by check_exec,
                    169:         * so we don't need to worry about it now or later.  We
                    170:         * will need to check P_TRACED later, however.
1.1       cgd       171:         */
1.17      mycroft   172:        script_sbits = epp->ep_vap->va_mode & (S_ISUID | S_ISGID);
1.1       cgd       173:        if (script_sbits != 0) {
                    174:                script_uid = epp->ep_vap->va_uid;
                    175:                script_gid = epp->ep_vap->va_gid;
                    176:        }
                    177: #endif
                    178: #ifdef FDSCRIPTS
                    179:        /*
                    180:         * if the script isn't readable, or it's set-id, then we've
                    181:         * gotta supply a "/dev/fd/..." for the shell to read.
                    182:         * Note that stupid shells (csh) do the wrong thing, and
                    183:         * close all open fd's when the start.  That kills this
                    184:         * method of implementing "safe" set-id and x-only scripts.
                    185:         */
1.19      fvdl      186:        vn_lock(epp->ep_vp, LK_EXCLUSIVE | LK_RETRY);
1.45      christos  187:        error = VOP_ACCESS(epp->ep_vp, VREAD, l->l_proc->p_ucred, l);
1.19      fvdl      188:        VOP_UNLOCK(epp->ep_vp, 0);
1.14      cgd       189:        if (error == EACCES
1.9       cgd       190: #ifdef SETUIDSCRIPTS
                    191:            || script_sbits
                    192: #endif
                    193:            ) {
1.1       cgd       194:                struct file *fp;
                    195: 
                    196: #if defined(DIAGNOSTIC) && defined(FDSCRIPTS)
                    197:                if (epp->ep_flags & EXEC_HASFD)
                    198:                        panic("exec_script_makecmds: epp already has a fd");
                    199: #endif
                    200: 
1.21      thorpej   201:                /* falloc() will use the descriptor for us */
1.18      christos  202:                if ((error = falloc(p, &fp, &epp->ep_fd)) != 0) {
                    203:                        scriptvp = NULL;
                    204:                        shellargp = NULL;
1.4       cgd       205:                        goto fail;
1.18      christos  206:                }
1.1       cgd       207: 
                    208:                epp->ep_flags |= EXEC_HASFD;
                    209:                fp->f_type = DTYPE_VNODE;
                    210:                fp->f_ops = &vnops;
                    211:                fp->f_data = (caddr_t) epp->ep_vp;
                    212:                fp->f_flag = FREAD;
1.28      thorpej   213:                FILE_SET_MATURE(fp);
1.45      christos  214:                FILE_UNUSE(fp, l);
1.1       cgd       215:        }
                    216: #endif
                    217: 
                    218:        /* set up the parameters for the recursive check_exec() call */
                    219:        epp->ep_ndp->ni_dirp = shellname;
                    220:        epp->ep_ndp->ni_segflg = UIO_SYSSPACE;
                    221:        epp->ep_flags |= EXEC_INDIR;
                    222: 
                    223:        /* and set up the fake args list, for later */
                    224:        MALLOC(shellargp, char **, 4 * sizeof(char *), M_EXEC, M_WAITOK);
                    225:        tmpsap = shellargp;
1.46    ! christos  226:        *tmpsap = malloc(shellnamelen + 1, M_EXEC, M_WAITOK);
1.34      itojun    227:        strlcpy(*tmpsap++, shellname, shellnamelen + 1);
1.1       cgd       228:        if (shellarg != NULL) {
1.46    ! christos  229:                *tmpsap = malloc(shellarglen + 1, M_EXEC, M_WAITOK);
1.34      itojun    230:                strlcpy(*tmpsap++, shellarg, shellarglen + 1);
1.1       cgd       231:        }
                    232:        MALLOC(*tmpsap, char *, MAXPATHLEN, M_EXEC, M_WAITOK);
                    233: #ifdef FDSCRIPTS
                    234:        if ((epp->ep_flags & EXEC_HASFD) == 0) {
                    235: #endif
                    236:                /* normally can't fail, but check for it if diagnostic */
1.40      elad      237: #ifdef SYSTRACE
1.41      elad      238:                error = 1;
                    239:                if (ISSET(p->p_flag, P_SYSTRACE)) {
                    240:                        error = systrace_scriptname(p, *tmpsap);
                    241:                        if (error == 0)
                    242:                                tmpsap++;
                    243:                }
                    244:                if (error) {
                    245:                        /*
                    246:                         * Since systrace_scriptname() provides a
                    247:                         * convenience, not a security issue, we are
                    248:                         * safe to do this.
                    249:                         */
                    250:                        error = copystr(epp->ep_name, *tmpsap++, MAXPATHLEN,
                    251:                                        NULL);
                    252:                }
1.40      elad      253: #else
1.11      mycroft   254:                error = copyinstr(epp->ep_name, *tmpsap++, MAXPATHLEN,
                    255:                    (size_t *)0);
1.40      elad      256: #endif /* SYSTRACE */
1.1       cgd       257: #ifdef DIAGNOSTIC
                    258:                if (error != 0)
1.31      provos    259:                        panic("exec_script: copyinstr couldn't fail");
1.1       cgd       260: #endif
                    261: #ifdef FDSCRIPTS
                    262:        } else
1.37      itojun    263:                snprintf(*tmpsap++, MAXPATHLEN, "/dev/fd/%d", epp->ep_fd);
1.1       cgd       264: #endif
                    265:        *tmpsap = NULL;
                    266: 
                    267:        /*
                    268:         * mark the header we have as invalid; check_exec will read
                    269:         * the header from the new executable
                    270:         */
                    271:        epp->ep_hdrvalid = 0;
                    272: 
                    273:        /*
                    274:         * remember the old vp and pnbuf for later, so we can restore
                    275:         * them if check_exec() fails.
                    276:         */
                    277:        scriptvp = epp->ep_vp;
1.7       mycroft   278:        oldpnbuf = epp->ep_ndp->ni_cnd.cn_pnbuf;
1.1       cgd       279: 
1.32      blymn     280: #ifdef VERIFIED_EXEC
1.45      christos  281:        if ((error = check_exec(l, epp, VERIEXEC_INDIRECT)) == 0) {
1.32      blymn     282: #else
1.45      christos  283:        if ((error = check_exec(l, epp, 0)) == 0) {
1.32      blymn     284: #endif
1.4       cgd       285:                /* note that we've clobbered the header */
1.27      jdolecek  286:                epp->ep_flags |= EXEC_DESTR|EXEC_HASES;
1.4       cgd       287: 
1.1       cgd       288:                /*
                    289:                 * It succeeded.  Unlock the script and
                    290:                 * close it if we aren't using it any more.
1.4       cgd       291:                 * Also, set things up so that the fake args
1.1       cgd       292:                 * list will be used.
                    293:                 */
1.14      cgd       294:                if ((epp->ep_flags & EXEC_HASFD) == 0) {
1.20      wrstuden  295:                        vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY);
1.45      christos  296:                        VOP_CLOSE(scriptvp, FREAD, p->p_ucred, l);
1.20      wrstuden  297:                        vput(scriptvp);
1.14      cgd       298:                }
1.2       cgd       299: 
                    300:                /* free the old pathname buffer */
1.26      thorpej   301:                PNBUF_PUT(oldpnbuf);
1.1       cgd       302: 
                    303:                epp->ep_flags |= (EXEC_HASARGL | EXEC_SKIPARG);
                    304:                epp->ep_fa = shellargp;
                    305: #ifdef SETUIDSCRIPTS
                    306:                /*
                    307:                 * set thing up so that set-id scripts will be
1.29      thorpej   308:                 * handled appropriately.  P_TRACED will be
                    309:                 * checked later when the shell is actually
                    310:                 * exec'd.
1.1       cgd       311:                 */
                    312:                epp->ep_vap->va_mode |= script_sbits;
1.17      mycroft   313:                if (script_sbits & S_ISUID)
1.1       cgd       314:                        epp->ep_vap->va_uid = script_uid;
1.17      mycroft   315:                if (script_sbits & S_ISGID)
1.1       cgd       316:                        epp->ep_vap->va_gid = script_gid;
                    317: #endif
1.4       cgd       318:                return (0);
                    319:        }
                    320: 
1.5       cgd       321:        /* XXX oldpnbuf not set for "goto fail" path */
1.7       mycroft   322:        epp->ep_ndp->ni_cnd.cn_pnbuf = oldpnbuf;
1.13      christos  323: #ifdef FDSCRIPTS
1.4       cgd       324: fail:
1.13      christos  325: #endif
1.4       cgd       326:        /* note that we've clobbered the header */
                    327:        epp->ep_flags |= EXEC_DESTR;
                    328: 
                    329:        /* kill the opened file descriptor, else close the file */
                    330:         if (epp->ep_flags & EXEC_HASFD) {
                    331:                 epp->ep_flags &= ~EXEC_HASFD;
1.45      christos  332:                 (void) fdrelease(l, epp->ep_fd);
1.18      christos  333:         } else if (scriptvp) {
1.20      wrstuden  334:                vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY);
1.45      christos  335:                VOP_CLOSE(scriptvp, FREAD, p->p_ucred, l);
1.20      wrstuden  336:                vput(scriptvp);
1.14      cgd       337:        }
1.4       cgd       338: 
1.26      thorpej   339:         PNBUF_PUT(epp->ep_ndp->ni_cnd.cn_pnbuf);
1.1       cgd       340: 
1.4       cgd       341:        /* free the fake arg list, because we're not returning it */
1.18      christos  342:        if ((tmpsap = shellargp) != NULL) {
                    343:                while (*tmpsap != NULL) {
                    344:                        FREE(*tmpsap, M_EXEC);
                    345:                        tmpsap++;
                    346:                }
                    347:                FREE(shellargp, M_EXEC);
1.1       cgd       348:        }
1.4       cgd       349: 
                    350:         /*
                    351:          * free any vmspace-creation commands,
                    352:          * and release their references
                    353:          */
                    354:         kill_vmcmds(&epp->ep_vmcmds);
1.1       cgd       355: 
1.4       cgd       356:         return error;
1.1       cgd       357: }

CVSweb <webmaster@jp.NetBSD.org>