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