Home | History | Annotate | Line # | Download | only in kern
exec_script.c revision 1.35
      1 /*	$NetBSD: exec_script.c,v 1.35 2003/06/28 14:21:52 darrenr Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993, 1994, 1996 Christopher G. Demetriou
      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
     19  *    derived from this software without specific prior written permission
     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  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: exec_script.c,v 1.35 2003/06/28 14:21:52 darrenr Exp $");
     35 
     36 #if defined(SETUIDSCRIPTS) && !defined(FDSCRIPTS)
     37 #define FDSCRIPTS		/* Need this for safe set-id scripts. */
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/proc.h>
     43 #include <sys/malloc.h>
     44 #include <sys/vnode.h>
     45 #include <sys/namei.h>
     46 #include <sys/file.h>
     47 #ifdef SETUIDSCRIPTS
     48 #include <sys/stat.h>
     49 #endif
     50 #include <sys/filedesc.h>
     51 #include <sys/exec.h>
     52 #include <sys/resourcevar.h>
     53 
     54 #include <sys/exec_script.h>
     55 
     56 /*
     57  * exec_script_makecmds(): Check if it's an executable shell script.
     58  *
     59  * Given a proc pointer and an exec package pointer, see if the referent
     60  * of the epp is in shell script.  If it is, then set thing up so that
     61  * the script can be run.  This involves preparing the address space
     62  * and arguments for the shell which will run the script.
     63  *
     64  * This function is ultimately responsible for creating a set of vmcmds
     65  * which can be used to build the process's vm space and inserting them
     66  * into the exec package.
     67  */
     68 int
     69 exec_script_makecmds(struct lwp *l, struct exec_package *epp)
     70 {
     71 	int error, hdrlinelen, shellnamelen, shellarglen;
     72 	char *hdrstr = epp->ep_hdr;
     73 	char *cp, *shellname, *shellarg, *oldpnbuf;
     74 	char **shellargp, **tmpsap;
     75 	struct vnode *scriptvp;
     76 	struct proc *p = l->l_proc;
     77 #ifdef SETUIDSCRIPTS
     78 	/* Gcc needs those initialized for spurious uninitialized warning */
     79 	uid_t script_uid = (uid_t) -1;
     80 	gid_t script_gid = NOGROUP;
     81 	u_short script_sbits;
     82 #endif
     83 
     84 	/*
     85 	 * if the magic isn't that of a shell script, or we've already
     86 	 * done shell script processing for this exec, punt on it.
     87 	 */
     88 	if ((epp->ep_flags & EXEC_INDIR) != 0 ||
     89 	    epp->ep_hdrvalid < EXEC_SCRIPT_MAGICLEN ||
     90 	    strncmp(hdrstr, EXEC_SCRIPT_MAGIC, EXEC_SCRIPT_MAGICLEN))
     91 		return ENOEXEC;
     92 
     93 	/*
     94 	 * check that the shell spec is terminated by a newline,
     95 	 * and that it isn't too large.  Don't modify the
     96 	 * buffer unless we're ready to commit to handling it.
     97 	 * (The latter requirement means that we have to check
     98 	 * for both spaces and tabs later on.)
     99 	 */
    100 	hdrlinelen = min(epp->ep_hdrvalid, SCRIPT_HDR_SIZE);
    101 	for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; cp < hdrstr + hdrlinelen;
    102 	    cp++) {
    103 		if (*cp == '\n') {
    104 			*cp = '\0';
    105 			break;
    106 		}
    107 	}
    108 	if (cp >= hdrstr + hdrlinelen)
    109 		return ENOEXEC;
    110 
    111 	shellname = NULL;
    112 	shellarg = NULL;
    113 	shellarglen = 0;
    114 
    115 	/* strip spaces before the shell name */
    116 	for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; *cp == ' ' || *cp == '\t';
    117 	    cp++)
    118 		;
    119 
    120 	/* collect the shell name; remember it's length for later */
    121 	shellname = cp;
    122 	shellnamelen = 0;
    123 	if (*cp == '\0')
    124 		goto check_shell;
    125 	for ( /* cp = cp */ ; *cp != '\0' && *cp != ' ' && *cp != '\t'; cp++)
    126 		shellnamelen++;
    127 	if (*cp == '\0')
    128 		goto check_shell;
    129 	*cp++ = '\0';
    130 
    131 	/* skip spaces before any argument */
    132 	for ( /* cp = cp */ ; *cp == ' ' || *cp == '\t'; cp++)
    133 		;
    134 	if (*cp == '\0')
    135 		goto check_shell;
    136 
    137 	/*
    138 	 * collect the shell argument.  everything after the shell name
    139 	 * is passed as ONE argument; that's the correct (historical)
    140 	 * behaviour.
    141 	 */
    142 	shellarg = cp;
    143 	for ( /* cp = cp */ ; *cp != '\0'; cp++)
    144 		shellarglen++;
    145 	*cp++ = '\0';
    146 
    147 check_shell:
    148 #ifdef SETUIDSCRIPTS
    149 	/*
    150 	 * MNT_NOSUID has already taken care of by check_exec,
    151 	 * so we don't need to worry about it now or later.  We
    152 	 * will need to check P_TRACED later, however.
    153 	 */
    154 	script_sbits = epp->ep_vap->va_mode & (S_ISUID | S_ISGID);
    155 	if (script_sbits != 0) {
    156 		script_uid = epp->ep_vap->va_uid;
    157 		script_gid = epp->ep_vap->va_gid;
    158 	}
    159 #endif
    160 #ifdef FDSCRIPTS
    161 	/*
    162 	 * if the script isn't readable, or it's set-id, then we've
    163 	 * gotta supply a "/dev/fd/..." for the shell to read.
    164 	 * Note that stupid shells (csh) do the wrong thing, and
    165 	 * close all open fd's when the start.  That kills this
    166 	 * method of implementing "safe" set-id and x-only scripts.
    167 	 */
    168 	vn_lock(epp->ep_vp, LK_EXCLUSIVE | LK_RETRY);
    169 	error = VOP_ACCESS(epp->ep_vp, VREAD, p->p_ucred, p);
    170 	VOP_UNLOCK(epp->ep_vp, 0);
    171 	if (error == EACCES
    172 #ifdef SETUIDSCRIPTS
    173 	    || script_sbits
    174 #endif
    175 	    ) {
    176 		struct file *fp;
    177 
    178 #if defined(DIAGNOSTIC) && defined(FDSCRIPTS)
    179 		if (epp->ep_flags & EXEC_HASFD)
    180 			panic("exec_script_makecmds: epp already has a fd");
    181 #endif
    182 
    183 		/* falloc() will use the descriptor for us */
    184 		if ((error = falloc(p, &fp, &epp->ep_fd)) != 0) {
    185 			scriptvp = NULL;
    186 			shellargp = NULL;
    187 			goto fail;
    188 		}
    189 
    190 		epp->ep_flags |= EXEC_HASFD;
    191 		fp->f_type = DTYPE_VNODE;
    192 		fp->f_ops = &vnops;
    193 		fp->f_data = (caddr_t) epp->ep_vp;
    194 		fp->f_flag = FREAD;
    195 		FILE_SET_MATURE(fp);
    196 		FILE_UNUSE(fp, p);
    197 	}
    198 #endif
    199 
    200 	/* set up the parameters for the recursive check_exec() call */
    201 	epp->ep_ndp->ni_dirp = shellname;
    202 	epp->ep_ndp->ni_segflg = UIO_SYSSPACE;
    203 	epp->ep_flags |= EXEC_INDIR;
    204 
    205 	/* and set up the fake args list, for later */
    206 	MALLOC(shellargp, char **, 4 * sizeof(char *), M_EXEC, M_WAITOK);
    207 	tmpsap = shellargp;
    208 	MALLOC(*tmpsap, char *, shellnamelen + 1, M_EXEC, M_WAITOK);
    209 	strlcpy(*tmpsap++, shellname, shellnamelen + 1);
    210 	if (shellarg != NULL) {
    211 		MALLOC(*tmpsap, char *, shellarglen + 1, M_EXEC, M_WAITOK);
    212 		strlcpy(*tmpsap++, shellarg, shellarglen + 1);
    213 	}
    214 	MALLOC(*tmpsap, char *, MAXPATHLEN, M_EXEC, M_WAITOK);
    215 #ifdef FDSCRIPTS
    216 	if ((epp->ep_flags & EXEC_HASFD) == 0) {
    217 #endif
    218 		/* normally can't fail, but check for it if diagnostic */
    219 		error = copyinstr(epp->ep_name, *tmpsap++, MAXPATHLEN,
    220 		    (size_t *)0);
    221 #ifdef DIAGNOSTIC
    222 		if (error != 0)
    223 			panic("exec_script: copyinstr couldn't fail");
    224 #endif
    225 #ifdef FDSCRIPTS
    226 	} else
    227 		sprintf(*tmpsap++, "/dev/fd/%d", epp->ep_fd);
    228 #endif
    229 	*tmpsap = NULL;
    230 
    231 	/*
    232 	 * mark the header we have as invalid; check_exec will read
    233 	 * the header from the new executable
    234 	 */
    235 	epp->ep_hdrvalid = 0;
    236 
    237 	/*
    238 	 * remember the old vp and pnbuf for later, so we can restore
    239 	 * them if check_exec() fails.
    240 	 */
    241 	scriptvp = epp->ep_vp;
    242 	oldpnbuf = epp->ep_ndp->ni_cnd.cn_pnbuf;
    243 
    244 #ifdef VERIFIED_EXEC
    245 	if ((error = check_exec(l, epp, 0)) == 0) {
    246 #else
    247 	if ((error = check_exec(l, epp)) == 0) {
    248 #endif
    249 		/* note that we've clobbered the header */
    250 		epp->ep_flags |= EXEC_DESTR|EXEC_HASES;
    251 
    252 		/*
    253 		 * It succeeded.  Unlock the script and
    254 		 * close it if we aren't using it any more.
    255 		 * Also, set things up so that the fake args
    256 		 * list will be used.
    257 		 */
    258 		if ((epp->ep_flags & EXEC_HASFD) == 0) {
    259 			vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY);
    260 			VOP_CLOSE(scriptvp, FREAD, p->p_ucred, l);
    261 			vput(scriptvp);
    262 		}
    263 
    264 		/* free the old pathname buffer */
    265 		PNBUF_PUT(oldpnbuf);
    266 
    267 		epp->ep_flags |= (EXEC_HASARGL | EXEC_SKIPARG);
    268 		epp->ep_fa = shellargp;
    269 #ifdef SETUIDSCRIPTS
    270 		/*
    271 		 * set thing up so that set-id scripts will be
    272 		 * handled appropriately.  P_TRACED will be
    273 		 * checked later when the shell is actually
    274 		 * exec'd.
    275 		 */
    276 		epp->ep_vap->va_mode |= script_sbits;
    277 		if (script_sbits & S_ISUID)
    278 			epp->ep_vap->va_uid = script_uid;
    279 		if (script_sbits & S_ISGID)
    280 			epp->ep_vap->va_gid = script_gid;
    281 #endif
    282 		return (0);
    283 	}
    284 
    285 	/* XXX oldpnbuf not set for "goto fail" path */
    286 	epp->ep_ndp->ni_cnd.cn_pnbuf = oldpnbuf;
    287 #ifdef FDSCRIPTS
    288 fail:
    289 #endif
    290 	/* note that we've clobbered the header */
    291 	epp->ep_flags |= EXEC_DESTR;
    292 
    293 	/* kill the opened file descriptor, else close the file */
    294         if (epp->ep_flags & EXEC_HASFD) {
    295                 epp->ep_flags &= ~EXEC_HASFD;
    296                 (void) fdrelease(l, epp->ep_fd);
    297         } else if (scriptvp) {
    298 		vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY);
    299 		VOP_CLOSE(scriptvp, FREAD, p->p_ucred, l);
    300 		vput(scriptvp);
    301 	}
    302 
    303         PNBUF_PUT(epp->ep_ndp->ni_cnd.cn_pnbuf);
    304 
    305 	/* free the fake arg list, because we're not returning it */
    306 	if ((tmpsap = shellargp) != NULL) {
    307 		while (*tmpsap != NULL) {
    308 			FREE(*tmpsap, M_EXEC);
    309 			tmpsap++;
    310 		}
    311 		FREE(shellargp, M_EXEC);
    312 	}
    313 
    314         /*
    315          * free any vmspace-creation commands,
    316          * and release their references
    317          */
    318         kill_vmcmds(&epp->ep_vmcmds);
    319 
    320         return error;
    321 }
    322