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