xref: /netbsd-src/sys/kern/exec_script.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: exec_script.c,v 1.38 2004/11/04 23:55:28 matt 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.38 2004/11/04 23:55:28 matt 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 #include <sys/exec_elf.h>
56 
57 /*
58  * exec_script_makecmds(): Check if it's an executable shell script.
59  *
60  * Given a proc pointer and an exec package pointer, see if the referent
61  * of the epp is in shell script.  If it is, then set thing up so that
62  * the script can be run.  This involves preparing the address space
63  * and arguments for the shell which will run the script.
64  *
65  * This function is ultimately responsible for creating a set of vmcmds
66  * which can be used to build the process's vm space and inserting them
67  * into the exec package.
68  */
69 int
70 exec_script_makecmds(struct proc *p, struct exec_package *epp)
71 {
72 	int error, hdrlinelen, shellnamelen, shellarglen;
73 	char *hdrstr = epp->ep_hdr;
74 	char *cp, *shellname, *shellarg, *oldpnbuf;
75 	char **shellargp, **tmpsap;
76 	struct vnode *scriptvp;
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 	/*
112 	 * If the script has an ELF header, don't exec it.
113 	 */
114 	if (epp->ep_hdrvalid >= sizeof(ELFMAG)-1 &&
115 	    memcmp(hdrstr, ELFMAG, sizeof(ELFMAG)-1) == 0)
116 		return ENOEXEC;
117 
118 	shellname = NULL;
119 	shellarg = NULL;
120 	shellarglen = 0;
121 
122 	/* strip spaces before the shell name */
123 	for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; *cp == ' ' || *cp == '\t';
124 	    cp++)
125 		;
126 
127 	/* collect the shell name; remember it's length for later */
128 	shellname = cp;
129 	shellnamelen = 0;
130 	if (*cp == '\0')
131 		goto check_shell;
132 	for ( /* cp = cp */ ; *cp != '\0' && *cp != ' ' && *cp != '\t'; cp++)
133 		shellnamelen++;
134 	if (*cp == '\0')
135 		goto check_shell;
136 	*cp++ = '\0';
137 
138 	/* skip spaces before any argument */
139 	for ( /* cp = cp */ ; *cp == ' ' || *cp == '\t'; cp++)
140 		;
141 	if (*cp == '\0')
142 		goto check_shell;
143 
144 	/*
145 	 * collect the shell argument.  everything after the shell name
146 	 * is passed as ONE argument; that's the correct (historical)
147 	 * behaviour.
148 	 */
149 	shellarg = cp;
150 	for ( /* cp = cp */ ; *cp != '\0'; cp++)
151 		shellarglen++;
152 	*cp++ = '\0';
153 
154 check_shell:
155 #ifdef SETUIDSCRIPTS
156 	/*
157 	 * MNT_NOSUID has already taken care of by check_exec,
158 	 * so we don't need to worry about it now or later.  We
159 	 * will need to check P_TRACED later, however.
160 	 */
161 	script_sbits = epp->ep_vap->va_mode & (S_ISUID | S_ISGID);
162 	if (script_sbits != 0) {
163 		script_uid = epp->ep_vap->va_uid;
164 		script_gid = epp->ep_vap->va_gid;
165 	}
166 #endif
167 #ifdef FDSCRIPTS
168 	/*
169 	 * if the script isn't readable, or it's set-id, then we've
170 	 * gotta supply a "/dev/fd/..." for the shell to read.
171 	 * Note that stupid shells (csh) do the wrong thing, and
172 	 * close all open fd's when the start.  That kills this
173 	 * method of implementing "safe" set-id and x-only scripts.
174 	 */
175 	vn_lock(epp->ep_vp, LK_EXCLUSIVE | LK_RETRY);
176 	error = VOP_ACCESS(epp->ep_vp, VREAD, p->p_ucred, p);
177 	VOP_UNLOCK(epp->ep_vp, 0);
178 	if (error == EACCES
179 #ifdef SETUIDSCRIPTS
180 	    || script_sbits
181 #endif
182 	    ) {
183 		struct file *fp;
184 
185 #if defined(DIAGNOSTIC) && defined(FDSCRIPTS)
186 		if (epp->ep_flags & EXEC_HASFD)
187 			panic("exec_script_makecmds: epp already has a fd");
188 #endif
189 
190 		/* falloc() will use the descriptor for us */
191 		if ((error = falloc(p, &fp, &epp->ep_fd)) != 0) {
192 			scriptvp = NULL;
193 			shellargp = NULL;
194 			goto fail;
195 		}
196 
197 		epp->ep_flags |= EXEC_HASFD;
198 		fp->f_type = DTYPE_VNODE;
199 		fp->f_ops = &vnops;
200 		fp->f_data = (caddr_t) epp->ep_vp;
201 		fp->f_flag = FREAD;
202 		FILE_SET_MATURE(fp);
203 		FILE_UNUSE(fp, p);
204 	}
205 #endif
206 
207 	/* set up the parameters for the recursive check_exec() call */
208 	epp->ep_ndp->ni_dirp = shellname;
209 	epp->ep_ndp->ni_segflg = UIO_SYSSPACE;
210 	epp->ep_flags |= EXEC_INDIR;
211 
212 	/* and set up the fake args list, for later */
213 	MALLOC(shellargp, char **, 4 * sizeof(char *), M_EXEC, M_WAITOK);
214 	tmpsap = shellargp;
215 	MALLOC(*tmpsap, char *, shellnamelen + 1, M_EXEC, M_WAITOK);
216 	strlcpy(*tmpsap++, shellname, shellnamelen + 1);
217 	if (shellarg != NULL) {
218 		MALLOC(*tmpsap, char *, shellarglen + 1, M_EXEC, M_WAITOK);
219 		strlcpy(*tmpsap++, shellarg, shellarglen + 1);
220 	}
221 	MALLOC(*tmpsap, char *, MAXPATHLEN, M_EXEC, M_WAITOK);
222 #ifdef FDSCRIPTS
223 	if ((epp->ep_flags & EXEC_HASFD) == 0) {
224 #endif
225 		/* normally can't fail, but check for it if diagnostic */
226 		error = copyinstr(epp->ep_name, *tmpsap++, MAXPATHLEN,
227 		    (size_t *)0);
228 #ifdef DIAGNOSTIC
229 		if (error != 0)
230 			panic("exec_script: copyinstr couldn't fail");
231 #endif
232 #ifdef FDSCRIPTS
233 	} else
234 		snprintf(*tmpsap++, MAXPATHLEN, "/dev/fd/%d", epp->ep_fd);
235 #endif
236 	*tmpsap = NULL;
237 
238 	/*
239 	 * mark the header we have as invalid; check_exec will read
240 	 * the header from the new executable
241 	 */
242 	epp->ep_hdrvalid = 0;
243 
244 	/*
245 	 * remember the old vp and pnbuf for later, so we can restore
246 	 * them if check_exec() fails.
247 	 */
248 	scriptvp = epp->ep_vp;
249 	oldpnbuf = epp->ep_ndp->ni_cnd.cn_pnbuf;
250 
251 #ifdef VERIFIED_EXEC
252 	if ((error = check_exec(p, epp, 0)) == 0) {
253 #else
254 	if ((error = check_exec(p, epp)) == 0) {
255 #endif
256 		/* note that we've clobbered the header */
257 		epp->ep_flags |= EXEC_DESTR|EXEC_HASES;
258 
259 		/*
260 		 * It succeeded.  Unlock the script and
261 		 * close it if we aren't using it any more.
262 		 * Also, set things up so that the fake args
263 		 * list will be used.
264 		 */
265 		if ((epp->ep_flags & EXEC_HASFD) == 0) {
266 			vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY);
267 			VOP_CLOSE(scriptvp, FREAD, p->p_ucred, p);
268 			vput(scriptvp);
269 		}
270 
271 		/* free the old pathname buffer */
272 		PNBUF_PUT(oldpnbuf);
273 
274 		epp->ep_flags |= (EXEC_HASARGL | EXEC_SKIPARG);
275 		epp->ep_fa = shellargp;
276 #ifdef SETUIDSCRIPTS
277 		/*
278 		 * set thing up so that set-id scripts will be
279 		 * handled appropriately.  P_TRACED will be
280 		 * checked later when the shell is actually
281 		 * exec'd.
282 		 */
283 		epp->ep_vap->va_mode |= script_sbits;
284 		if (script_sbits & S_ISUID)
285 			epp->ep_vap->va_uid = script_uid;
286 		if (script_sbits & S_ISGID)
287 			epp->ep_vap->va_gid = script_gid;
288 #endif
289 		return (0);
290 	}
291 
292 	/* XXX oldpnbuf not set for "goto fail" path */
293 	epp->ep_ndp->ni_cnd.cn_pnbuf = oldpnbuf;
294 #ifdef FDSCRIPTS
295 fail:
296 #endif
297 	/* note that we've clobbered the header */
298 	epp->ep_flags |= EXEC_DESTR;
299 
300 	/* kill the opened file descriptor, else close the file */
301         if (epp->ep_flags & EXEC_HASFD) {
302                 epp->ep_flags &= ~EXEC_HASFD;
303                 (void) fdrelease(p, epp->ep_fd);
304         } else if (scriptvp) {
305 		vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY);
306 		VOP_CLOSE(scriptvp, FREAD, p->p_ucred, p);
307 		vput(scriptvp);
308 	}
309 
310         PNBUF_PUT(epp->ep_ndp->ni_cnd.cn_pnbuf);
311 
312 	/* free the fake arg list, because we're not returning it */
313 	if ((tmpsap = shellargp) != NULL) {
314 		while (*tmpsap != NULL) {
315 			FREE(*tmpsap, M_EXEC);
316 			tmpsap++;
317 		}
318 		FREE(shellargp, M_EXEC);
319 	}
320 
321         /*
322          * free any vmspace-creation commands,
323          * and release their references
324          */
325         kill_vmcmds(&epp->ep_vmcmds);
326 
327         return error;
328 }
329