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