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

1.76    ! christos    1: /*     $NetBSD: exec_script.c,v 1.75 2018/04/27 18:33:24 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.76    ! christos   34: __KERNEL_RCSID(0, "$NetBSD: exec_script.c,v 1.75 2018/04/27 18:33:24 christos Exp $");
        !            35: 
        !            36: #include "opt_script.h"
1.1       cgd        37: 
                     38: #if defined(SETUIDSCRIPTS) && !defined(FDSCRIPTS)
                     39: #define FDSCRIPTS              /* Need this for safe set-id scripts. */
                     40: #endif
                     41: 
                     42: #include <sys/param.h>
                     43: #include <sys/systm.h>
                     44: #include <sys/proc.h>
1.61      yamt       45: #include <sys/kmem.h>
1.1       cgd        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>
1.63      ad         55: #include <sys/module.h>
1.1       cgd        56: #include <sys/exec_script.h>
1.38      matt       57: #include <sys/exec_elf.h>
1.1       cgd        58: 
1.67      christos   59: MODULE(MODULE_CLASS_EXEC, exec_script, NULL);
1.63      ad         60: 
1.70      christos   61: static struct execsw exec_script_execsw = {
                     62:        .es_hdrsz = SCRIPT_HDR_SIZE,
                     63:        .es_makecmds = exec_script_makecmds,
                     64:        .u = {
                     65:                .elf_probe_func = NULL,
                     66:        },
                     67:        .es_emul = NULL,
                     68:        .es_prio = EXECSW_PRIO_ANY,
                     69:        .es_arglen = 0,
                     70:        .es_copyargs = NULL,
                     71:        .es_setregs = NULL,
                     72:        .es_coredump = NULL,
                     73:        .es_setup_stack = exec_setup_stack,
1.63      ad         74: };
                     75: 
                     76: static int
                     77: exec_script_modcmd(modcmd_t cmd, void *arg)
                     78: {
                     79: 
                     80:        switch (cmd) {
                     81:        case MODULE_CMD_INIT:
1.70      christos   82:                return exec_add(&exec_script_execsw, 1);
1.63      ad         83: 
                     84:        case MODULE_CMD_FINI:
1.70      christos   85:                return exec_remove(&exec_script_execsw, 1);
1.63      ad         86: 
                     87:        case MODULE_CMD_AUTOUNLOAD:
                     88:                /*
                     89:                 * We don't want to be autounloaded because our use is
                     90:                 * transient: no executables with p_execsw equal to
                     91:                 * exec_script_execsw will exist, so FINI will never
                     92:                 * return EBUSY.  However, the system will run scripts
                     93:                 * often.  Return EBUSY here to prevent this module from
                     94:                 * ping-ponging in and out of the kernel.
                     95:                 */
                     96:                return EBUSY;
                     97: 
                     98:        default:
                     99:                return ENOTTY;
1.68      maxv      100:        }
1.63      ad        101: }
                    102: 
1.1       cgd       103: /*
                    104:  * exec_script_makecmds(): Check if it's an executable shell script.
                    105:  *
                    106:  * Given a proc pointer and an exec package pointer, see if the referent
                    107:  * of the epp is in shell script.  If it is, then set thing up so that
                    108:  * the script can be run.  This involves preparing the address space
                    109:  * and arguments for the shell which will run the script.
                    110:  *
                    111:  * This function is ultimately responsible for creating a set of vmcmds
                    112:  * which can be used to build the process's vm space and inserting them
                    113:  * into the exec package.
                    114:  */
                    115: int
1.45      christos  116: exec_script_makecmds(struct lwp *l, struct exec_package *epp)
1.1       cgd       117: {
                    118:        int error, hdrlinelen, shellnamelen, shellarglen;
                    119:        char *hdrstr = epp->ep_hdr;
1.64      dholland  120:        char *cp, *shellname, *shellarg;
1.61      yamt      121:        size_t shellargp_len;
                    122:        struct exec_fakearg *shellargp;
                    123:        struct exec_fakearg *tmpsap;
1.66      dholland  124:        struct pathbuf *shell_pathbuf;
1.1       cgd       125:        struct vnode *scriptvp;
                    126: #ifdef SETUIDSCRIPTS
1.18      christos  127:        /* Gcc needs those initialized for spurious uninitialized warning */
                    128:        uid_t script_uid = (uid_t) -1;
                    129:        gid_t script_gid = NOGROUP;
1.1       cgd       130:        u_short script_sbits;
                    131: #endif
                    132: 
                    133:        /*
                    134:         * if the magic isn't that of a shell script, or we've already
                    135:         * done shell script processing for this exec, punt on it.
                    136:         */
                    137:        if ((epp->ep_flags & EXEC_INDIR) != 0 ||
                    138:            epp->ep_hdrvalid < EXEC_SCRIPT_MAGICLEN ||
                    139:            strncmp(hdrstr, EXEC_SCRIPT_MAGIC, EXEC_SCRIPT_MAGICLEN))
                    140:                return ENOEXEC;
                    141: 
                    142:        /*
1.73      maxv      143:         * Check that the shell spec is terminated by a newline, and that
                    144:         * it isn't too large.
1.1       cgd       145:         */
1.33      perry     146:        hdrlinelen = min(epp->ep_hdrvalid, SCRIPT_HDR_SIZE);
1.1       cgd       147:        for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; cp < hdrstr + hdrlinelen;
                    148:            cp++) {
                    149:                if (*cp == '\n') {
                    150:                        *cp = '\0';
                    151:                        break;
                    152:                }
                    153:        }
                    154:        if (cp >= hdrstr + hdrlinelen)
                    155:                return ENOEXEC;
                    156: 
                    157:        /* strip spaces before the shell name */
                    158:        for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; *cp == ' ' || *cp == '\t';
                    159:            cp++)
                    160:                ;
1.72      maxv      161:        if (*cp == '\0')
                    162:                return ENOEXEC;
1.1       cgd       163: 
1.73      maxv      164:        shellarg = NULL;
                    165:        shellarglen = 0;
                    166: 
                    167:        /* collect the shell name; remember its length for later */
1.1       cgd       168:        shellname = cp;
                    169:        shellnamelen = 0;
                    170:        for ( /* cp = cp */ ; *cp != '\0' && *cp != ' ' && *cp != '\t'; cp++)
                    171:                shellnamelen++;
                    172:        if (*cp == '\0')
                    173:                goto check_shell;
                    174:        *cp++ = '\0';
                    175: 
                    176:        /* skip spaces before any argument */
                    177:        for ( /* cp = cp */ ; *cp == ' ' || *cp == '\t'; cp++)
                    178:                ;
                    179:        if (*cp == '\0')
                    180:                goto check_shell;
                    181: 
                    182:        /*
                    183:         * collect the shell argument.  everything after the shell name
                    184:         * is passed as ONE argument; that's the correct (historical)
                    185:         * behaviour.
                    186:         */
                    187:        shellarg = cp;
                    188:        for ( /* cp = cp */ ; *cp != '\0'; cp++)
                    189:                shellarglen++;
                    190:        *cp++ = '\0';
                    191: 
                    192: check_shell:
                    193: #ifdef SETUIDSCRIPTS
                    194:        /*
1.29      thorpej   195:         * MNT_NOSUID has already taken care of by check_exec,
                    196:         * so we don't need to worry about it now or later.  We
1.54      ad        197:         * will need to check PSL_TRACED later, however.
1.1       cgd       198:         */
1.17      mycroft   199:        script_sbits = epp->ep_vap->va_mode & (S_ISUID | S_ISGID);
1.1       cgd       200:        if (script_sbits != 0) {
                    201:                script_uid = epp->ep_vap->va_uid;
                    202:                script_gid = epp->ep_vap->va_gid;
                    203:        }
                    204: #endif
                    205: #ifdef FDSCRIPTS
                    206:        /*
                    207:         * if the script isn't readable, or it's set-id, then we've
                    208:         * gotta supply a "/dev/fd/..." for the shell to read.
                    209:         * Note that stupid shells (csh) do the wrong thing, and
                    210:         * close all open fd's when the start.  That kills this
                    211:         * method of implementing "safe" set-id and x-only scripts.
                    212:         */
1.19      fvdl      213:        vn_lock(epp->ep_vp, LK_EXCLUSIVE | LK_RETRY);
1.59      pooka     214:        error = VOP_ACCESS(epp->ep_vp, VREAD, l->l_cred);
1.65      hannken   215:        VOP_UNLOCK(epp->ep_vp);
1.14      cgd       216:        if (error == EACCES
1.9       cgd       217: #ifdef SETUIDSCRIPTS
                    218:            || script_sbits
                    219: #endif
                    220:            ) {
1.1       cgd       221:                struct file *fp;
                    222: 
1.71      maxv      223:                KASSERT(!(epp->ep_flags & EXEC_HASFD));
1.1       cgd       224: 
1.62      ad        225:                if ((error = fd_allocfile(&fp, &epp->ep_fd)) != 0) {
1.18      christos  226:                        scriptvp = NULL;
                    227:                        shellargp = NULL;
1.4       cgd       228:                        goto fail;
1.18      christos  229:                }
1.1       cgd       230:                epp->ep_flags |= EXEC_HASFD;
                    231:                fp->f_type = DTYPE_VNODE;
                    232:                fp->f_ops = &vnops;
1.74      matt      233:                fp->f_vnode = epp->ep_vp;
1.1       cgd       234:                fp->f_flag = FREAD;
1.62      ad        235:                fd_affix(curproc, fp, epp->ep_fd);
1.1       cgd       236:        }
                    237: #endif
                    238: 
1.64      dholland  239:        /* set up the fake args list */
1.61      yamt      240:        shellargp_len = 4 * sizeof(*shellargp);
                    241:        shellargp = kmem_alloc(shellargp_len, KM_SLEEP);
1.1       cgd       242:        tmpsap = shellargp;
1.61      yamt      243:        tmpsap->fa_len = shellnamelen + 1;
                    244:        tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP);
                    245:        strlcpy(tmpsap->fa_arg, shellname, tmpsap->fa_len);
                    246:        tmpsap++;
1.1       cgd       247:        if (shellarg != NULL) {
1.61      yamt      248:                tmpsap->fa_len = shellarglen + 1;
                    249:                tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP);
                    250:                strlcpy(tmpsap->fa_arg, shellarg, tmpsap->fa_len);
                    251:                tmpsap++;
1.1       cgd       252:        }
1.61      yamt      253:        tmpsap->fa_len = MAXPATHLEN;
                    254:        tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP);
1.1       cgd       255: #ifdef FDSCRIPTS
                    256:        if ((epp->ep_flags & EXEC_HASFD) == 0) {
                    257: #endif
                    258:                /* normally can't fail, but check for it if diagnostic */
1.64      dholland  259:                error = copystr(epp->ep_kname, tmpsap->fa_arg, MAXPATHLEN,
1.71      maxv      260:                    NULL);
                    261:                KASSERT(error == 0);
1.61      yamt      262:                tmpsap++;
1.1       cgd       263: #ifdef FDSCRIPTS
1.61      yamt      264:        } else {
                    265:                snprintf(tmpsap->fa_arg, MAXPATHLEN, "/dev/fd/%d", epp->ep_fd);
                    266:                tmpsap++;
                    267:        }
1.1       cgd       268: #endif
1.61      yamt      269:        tmpsap->fa_arg = NULL;
1.1       cgd       270: 
1.64      dholland  271:        /* Save the old vnode so we can clean it up later. */
                    272:        scriptvp = epp->ep_vp;
                    273:        epp->ep_vp = NULL;
                    274: 
                    275:        /* Note that we're trying recursively. */
                    276:        epp->ep_flags |= EXEC_INDIR;
                    277: 
1.1       cgd       278:        /*
                    279:         * mark the header we have as invalid; check_exec will read
                    280:         * the header from the new executable
                    281:         */
                    282:        epp->ep_hdrvalid = 0;
                    283: 
1.64      dholland  284:        /* try loading the interpreter */
1.75      christos  285:        if ((error = exec_makepathbuf(l, shellname, UIO_SYSSPACE,
                    286:            &shell_pathbuf, NULL)) == 0) {
1.66      dholland  287:                error = check_exec(l, epp, shell_pathbuf);
                    288:                pathbuf_destroy(shell_pathbuf);
                    289:        }
1.1       cgd       290: 
1.57      dsl       291:        /* note that we've clobbered the header */
                    292:        epp->ep_flags |= EXEC_DESTR;
1.64      dholland  293: 
1.57      dsl       294:        if (error == 0) {
1.1       cgd       295:                /*
                    296:                 * It succeeded.  Unlock the script and
                    297:                 * close it if we aren't using it any more.
1.4       cgd       298:                 * Also, set things up so that the fake args
1.1       cgd       299:                 * list will be used.
                    300:                 */
1.14      cgd       301:                if ((epp->ep_flags & EXEC_HASFD) == 0) {
1.20      wrstuden  302:                        vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY);
1.58      pooka     303:                        VOP_CLOSE(scriptvp, FREAD, l->l_cred);
1.20      wrstuden  304:                        vput(scriptvp);
1.14      cgd       305:                }
1.2       cgd       306: 
1.1       cgd       307:                epp->ep_flags |= (EXEC_HASARGL | EXEC_SKIPARG);
                    308:                epp->ep_fa = shellargp;
1.61      yamt      309:                epp->ep_fa_len = shellargp_len;
1.1       cgd       310: #ifdef SETUIDSCRIPTS
                    311:                /*
                    312:                 * set thing up so that set-id scripts will be
1.54      ad        313:                 * handled appropriately.  PSL_TRACED will be
1.29      thorpej   314:                 * checked later when the shell is actually
                    315:                 * exec'd.
1.1       cgd       316:                 */
                    317:                epp->ep_vap->va_mode |= script_sbits;
1.17      mycroft   318:                if (script_sbits & S_ISUID)
1.1       cgd       319:                        epp->ep_vap->va_uid = script_uid;
1.17      mycroft   320:                if (script_sbits & S_ISGID)
1.1       cgd       321:                        epp->ep_vap->va_gid = script_gid;
                    322: #endif
1.4       cgd       323:                return (0);
                    324:        }
                    325: 
1.13      christos  326: #ifdef FDSCRIPTS
1.4       cgd       327: fail:
1.13      christos  328: #endif
1.4       cgd       329: 
                    330:        /* kill the opened file descriptor, else close the file */
1.68      maxv      331:        if (epp->ep_flags & EXEC_HASFD) {
                    332:                epp->ep_flags &= ~EXEC_HASFD;
                    333:                fd_close(epp->ep_fd);
                    334:        } else if (scriptvp) {
1.20      wrstuden  335:                vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY);
1.58      pooka     336:                VOP_CLOSE(scriptvp, FREAD, l->l_cred);
1.20      wrstuden  337:                vput(scriptvp);
1.14      cgd       338:        }
1.4       cgd       339: 
                    340:        /* free the fake arg list, because we're not returning it */
1.18      christos  341:        if ((tmpsap = shellargp) != NULL) {
1.61      yamt      342:                while (tmpsap->fa_arg != NULL) {
                    343:                        kmem_free(tmpsap->fa_arg, tmpsap->fa_len);
1.18      christos  344:                        tmpsap++;
                    345:                }
1.61      yamt      346:                kmem_free(shellargp, shellargp_len);
1.1       cgd       347:        }
1.4       cgd       348: 
1.68      maxv      349:        /*
                    350:         * free any vmspace-creation commands,
                    351:         * and release their references
                    352:         */
                    353:        kill_vmcmds(&epp->ep_vmcmds);
1.1       cgd       354: 
1.68      maxv      355:        return error;
1.1       cgd       356: }

CVSweb <webmaster@jp.NetBSD.org>