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

1.1       cgd         1: /*
                      2:  * Copyright (c) 1993, 1994 Christopher G. Demetriou
                      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:  * 3. All advertising materials mentioning features or use of this software
                     14:  *    must display the following acknowledgement:
                     15:  *      This product includes software developed by Christopher G. Demetriou.
                     16:  * 4. The name of the author may not be used to endorse or promote products
1.3       jtc        17:  *    derived from this software without specific prior written permission
1.1       cgd        18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     20:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     21:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     22:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     23:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     24:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     25:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     26:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     27:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     28:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     29:  *
1.5     ! cgd        30:  *     $Id: exec_script.c,v 1.4 1994/02/16 01:21:00 cgd Exp $
1.1       cgd        31:  */
                     32: 
                     33: #if defined(SETUIDSCRIPTS) && !defined(FDSCRIPTS)
                     34: #define FDSCRIPTS              /* Need this for safe set-id scripts. */
                     35: #endif
                     36: 
                     37: #include <sys/param.h>
                     38: #include <sys/systm.h>
                     39: #include <sys/proc.h>
                     40: #include <sys/malloc.h>
                     41: #include <sys/vnode.h>
                     42: #include <sys/namei.h>
                     43: #include <sys/file.h>
                     44: #include <sys/filedesc.h>
                     45: #include <sys/exec.h>
                     46: #include <sys/resourcevar.h>
                     47: #include <vm/vm.h>
                     48: 
                     49: #include <sys/exec_script.h>
                     50: 
                     51: /*
                     52:  * exec_script_makecmds(): Check if it's an executable shell script.
                     53:  *
                     54:  * Given a proc pointer and an exec package pointer, see if the referent
                     55:  * of the epp is in shell script.  If it is, then set thing up so that
                     56:  * the script can be run.  This involves preparing the address space
                     57:  * and arguments for the shell which will run the script.
                     58:  *
                     59:  * This function is ultimately responsible for creating a set of vmcmds
                     60:  * which can be used to build the process's vm space and inserting them
                     61:  * into the exec package.
                     62:  */
                     63: int
                     64: exec_script_makecmds(p, epp)
                     65:        struct proc *p;
                     66:        struct exec_package *epp;
                     67: {
                     68:        int error, hdrlinelen, shellnamelen, shellarglen;
                     69:        char *hdrstr = epp->ep_hdr;
                     70:        char *cp, *shellname, *shellarg, *oldpnbuf;
                     71:        char **shellargp, **tmpsap;
                     72:        struct vnode *scriptvp;
                     73: #ifdef SETUIDSCRIPTS
                     74:        uid_t script_uid;
                     75:        gid_t script_gid;
                     76:        u_short script_sbits;
                     77: #endif
                     78: 
                     79:        /*
                     80:         * if the magic isn't that of a shell script, or we've already
                     81:         * done shell script processing for this exec, punt on it.
                     82:         */
                     83:        if ((epp->ep_flags & EXEC_INDIR) != 0 ||
                     84:            epp->ep_hdrvalid < EXEC_SCRIPT_MAGICLEN ||
                     85:            strncmp(hdrstr, EXEC_SCRIPT_MAGIC, EXEC_SCRIPT_MAGICLEN))
                     86:                return ENOEXEC;
                     87: 
                     88:        /*
                     89:         * check that the shell spec is terminated by a newline,
                     90:         * and that it isn't too large.  Don't modify the
                     91:         * buffer unless we're ready to commit to handling it.
                     92:         * (The latter requirement means that we have to check
                     93:         * for both spaces and tabs later on.)
                     94:         */
                     95:        hdrlinelen = MIN(epp->ep_hdrvalid, MAXINTERP);
                     96:        for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; cp < hdrstr + hdrlinelen;
                     97:            cp++) {
                     98:                if (*cp == '\n') {
                     99:                        *cp = '\0';
                    100:                        break;
                    101:                }
                    102:        }
                    103:        if (cp >= hdrstr + hdrlinelen)
                    104:                return ENOEXEC;
                    105: 
                    106:        shellname = NULL;
                    107:        shellarg = NULL;
                    108: 
                    109:        /* strip spaces before the shell name */
                    110:        for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; *cp == ' ' || *cp == '\t';
                    111:            cp++)
                    112:                ;
                    113: 
                    114:        /* collect the shell name; remember it's length for later */
                    115:        shellname = cp;
                    116:        shellnamelen = 0;
                    117:        if (*cp == '\0')
                    118:                goto check_shell;
                    119:        for ( /* cp = cp */ ; *cp != '\0' && *cp != ' ' && *cp != '\t'; cp++)
                    120:                shellnamelen++;
                    121:        if (*cp == '\0')
                    122:                goto check_shell;
                    123:        *cp++ = '\0';
                    124: 
                    125:        /* skip spaces before any argument */
                    126:        for ( /* cp = cp */ ; *cp == ' ' || *cp == '\t'; cp++)
                    127:                ;
                    128:        if (*cp == '\0')
                    129:                goto check_shell;
                    130: 
                    131:        /*
                    132:         * collect the shell argument.  everything after the shell name
                    133:         * is passed as ONE argument; that's the correct (historical)
                    134:         * behaviour.
                    135:         */
                    136:        shellarg = cp;
                    137:        shellarglen = 0;
                    138:        for ( /* cp = cp */ ; *cp != '\0'; cp++)
                    139:                shellarglen++;
                    140:        *cp++ = '\0';
                    141: 
                    142: check_shell:
                    143: #ifdef SETUIDSCRIPTS
                    144:        /*
                    145:         * MNT_NOSUID and STRC are already taken care of by check_exec,
                    146:         * so we don't need to worry about them now or later.
                    147:         */
                    148:        script_sbits = epp->ep_vap->va_mode & (VSUID | VSGID);
                    149:        if (script_sbits != 0) {
                    150:                script_uid = epp->ep_vap->va_uid;
                    151:                script_gid = epp->ep_vap->va_gid;
                    152:        }
                    153: #endif
                    154: #ifdef FDSCRIPTS
                    155:        /*
                    156:         * if the script isn't readable, or it's set-id, then we've
                    157:         * gotta supply a "/dev/fd/..." for the shell to read.
                    158:         * Note that stupid shells (csh) do the wrong thing, and
                    159:         * close all open fd's when the start.  That kills this
                    160:         * method of implementing "safe" set-id and x-only scripts.
                    161:         */
                    162:        if (VOP_ACCESS(epp->ep_vp, VREAD, p->p_ucred, p) == EACCES ||
                    163:            script_sbits) {
                    164:                struct file *fp;
                    165:                extern struct fileops vnops;
                    166: 
                    167: #if defined(DIAGNOSTIC) && defined(FDSCRIPTS)
                    168:                if (epp->ep_flags & EXEC_HASFD)
                    169:                        panic("exec_script_makecmds: epp already has a fd");
                    170: #endif
                    171: 
                    172:                if (error = falloc(p, &fp, &epp->ep_fd))
1.4       cgd       173:                        goto fail;
1.1       cgd       174: 
                    175:                epp->ep_flags |= EXEC_HASFD;
                    176:                fp->f_type = DTYPE_VNODE;
                    177:                fp->f_ops = &vnops;
                    178:                fp->f_data = (caddr_t) epp->ep_vp;
                    179:                fp->f_flag = FREAD;
                    180:        }
                    181: #endif
                    182: 
                    183:        /* set up the parameters for the recursive check_exec() call */
                    184:        epp->ep_ndp->ni_dirp = shellname;
                    185:        epp->ep_ndp->ni_segflg = UIO_SYSSPACE;
                    186:        epp->ep_flags |= EXEC_INDIR;
                    187: 
                    188:        /* and set up the fake args list, for later */
                    189:        MALLOC(shellargp, char **, 4 * sizeof(char *), M_EXEC, M_WAITOK);
                    190:        tmpsap = shellargp;
                    191:        MALLOC(*tmpsap, char *, shellnamelen + 1, M_EXEC, M_WAITOK);
                    192:        strcpy(*tmpsap++, shellname);
                    193:        if (shellarg != NULL) {
                    194:                MALLOC(*tmpsap, char *, shellarglen + 1, M_EXEC, M_WAITOK);
                    195:                strcpy(*tmpsap++, shellarg);
                    196:        }
                    197:        MALLOC(*tmpsap, char *, MAXPATHLEN, M_EXEC, M_WAITOK);
                    198: #ifdef FDSCRIPTS
                    199:        if ((epp->ep_flags & EXEC_HASFD) == 0) {
                    200: #endif
                    201:                /* normally can't fail, but check for it if diagnostic */
                    202:                error = copyinstr(epp->ep_name, *tmpsap++, MAXPATHLEN, 0);
                    203: #ifdef DIAGNOSTIC
                    204:                if (error != 0)
                    205:                        panic("exec_script: copyinstr couldn't fail\n");
                    206: #endif
                    207: #ifdef FDSCRIPTS
                    208:        } else
                    209:                sprintf(*tmpsap++, "/dev/fd/%d", epp->ep_fd);
                    210: #endif
                    211:        *tmpsap = NULL;
                    212: 
                    213:        /*
                    214:         * mark the header we have as invalid; check_exec will read
                    215:         * the header from the new executable
                    216:         */
                    217:        epp->ep_hdrvalid = 0;
                    218: 
                    219:        /*
                    220:         * remember the old vp and pnbuf for later, so we can restore
                    221:         * them if check_exec() fails.
                    222:         */
                    223:        scriptvp = epp->ep_vp;
                    224:        oldpnbuf = epp->ep_ndp->ni_pnbuf;
                    225: 
1.4       cgd       226:        VOP_UNLOCK(scriptvp);
                    227: 
1.1       cgd       228:        if ((error = check_exec(p, epp)) == 0) {
1.4       cgd       229:                /* note that we've clobbered the header */
                    230:                epp->ep_flags |= EXEC_DESTR;
                    231: 
1.1       cgd       232:                /*
                    233:                 * It succeeded.  Unlock the script and
                    234:                 * close it if we aren't using it any more.
1.4       cgd       235:                 * Also, set things up so that the fake args
1.1       cgd       236:                 * list will be used.
                    237:                 */
                    238:                if ((epp->ep_flags & EXEC_HASFD) == 0)
                    239:                        vn_close(scriptvp, FREAD, p->p_ucred, p);
1.2       cgd       240: 
                    241:                /* free the old pathname buffer */
                    242:                FREE(oldpnbuf, M_NAMEI);
1.1       cgd       243: 
                    244:                epp->ep_flags |= (EXEC_HASARGL | EXEC_SKIPARG);
                    245:                epp->ep_fa = shellargp;
                    246: #ifdef SETUIDSCRIPTS
                    247:                /*
                    248:                 * set thing up so that set-id scripts will be
                    249:                 * handled appropriately
                    250:                 */
                    251:                epp->ep_vap->va_mode |= script_sbits;
                    252:                if (script_sbits & VSUID)
                    253:                        epp->ep_vap->va_uid = script_uid;
                    254:                if (script_sbits & VSGID)
                    255:                        epp->ep_vap->va_gid = script_gid;
                    256: #endif
1.4       cgd       257:                return (0);
                    258:        }
                    259: 
1.5     ! cgd       260:        /* XXX oldpnbuf not set for "goto fail" path */
        !           261:        epp->ep_ndp->ni_pnbuf = oldpnbuf;
1.4       cgd       262: fail:
                    263:        /* note that we've clobbered the header */
                    264:        epp->ep_flags |= EXEC_DESTR;
                    265: 
                    266:        /* kill the opened file descriptor, else close the file */
                    267:         if (epp->ep_flags & EXEC_HASFD) {
                    268:                 epp->ep_flags &= ~EXEC_HASFD;
                    269:                 exec_closefd(p, epp->ep_fd);
                    270:         } else
                    271:                vn_close(scriptvp, FREAD, p->p_ucred, p);
                    272: 
1.5     ! cgd       273:         FREE(epp->ep_ndp->ni_pnbuf, M_NAMEI);
1.1       cgd       274: 
1.4       cgd       275:        /* free the fake arg list, because we're not returning it */
                    276:        tmpsap = shellargp;
                    277:        while (*tmpsap != NULL) {
                    278:                FREE(*tmpsap, M_EXEC);
                    279:                tmpsap++;
1.1       cgd       280:        }
1.4       cgd       281:        FREE(shellargp, M_EXEC);
                    282: 
                    283:         /*
                    284:          * free any vmspace-creation commands,
                    285:          * and release their references
                    286:          */
                    287:         kill_vmcmds(&epp->ep_vmcmds);
1.1       cgd       288: 
1.4       cgd       289:         return error;
1.1       cgd       290: }

CVSweb <webmaster@jp.NetBSD.org>