exec_script.c revision 1.43 1 /* $NetBSD: exec_script.c,v 1.43 2005/07/17 05:22:34 christos 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.43 2005/07/17 05:22:34 christos Exp $");
35
36 #if defined(SETUIDSCRIPTS) && !defined(FDSCRIPTS)
37 #define FDSCRIPTS /* Need this for safe set-id scripts. */
38 #endif
39
40 #include "opt_verified_exec.h"
41
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>
49 #ifdef SETUIDSCRIPTS
50 #include <sys/stat.h>
51 #endif
52 #include <sys/filedesc.h>
53 #include <sys/exec.h>
54 #include <sys/resourcevar.h>
55
56 #include <sys/exec_script.h>
57 #include <sys/exec_elf.h>
58
59 #include <sys/verified_exec.h>
60
61 #ifdef SYSTRACE
62 #include <sys/systrace.h>
63 #endif /* SYSTRACE */
64
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
78 exec_script_makecmds(struct proc *p, struct exec_package *epp)
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
86 /* Gcc needs those initialized for spurious uninitialized warning */
87 uid_t script_uid = (uid_t) -1;
88 gid_t script_gid = NOGROUP;
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 */
108 hdrlinelen = min(epp->ep_hdrvalid, SCRIPT_HDR_SIZE);
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
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
126 shellname = NULL;
127 shellarg = NULL;
128 shellarglen = 0;
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 /*
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.
168 */
169 script_sbits = epp->ep_vap->va_mode & (S_ISUID | S_ISGID);
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 */
183 vn_lock(epp->ep_vp, LK_EXCLUSIVE | LK_RETRY);
184 error = VOP_ACCESS(epp->ep_vp, VREAD, p->p_ucred, p);
185 VOP_UNLOCK(epp->ep_vp, 0);
186 if (error == EACCES
187 #ifdef SETUIDSCRIPTS
188 || script_sbits
189 #endif
190 ) {
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
198 /* falloc() will use the descriptor for us */
199 if ((error = falloc(p, &fp, &epp->ep_fd)) != 0) {
200 scriptvp = NULL;
201 shellargp = NULL;
202 goto fail;
203 }
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;
210 FILE_SET_MATURE(fp);
211 FILE_UNUSE(fp, p);
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);
224 strlcpy(*tmpsap++, shellname, shellnamelen + 1);
225 if (shellarg != NULL) {
226 MALLOC(*tmpsap, char *, shellarglen + 1, M_EXEC, M_WAITOK);
227 strlcpy(*tmpsap++, shellarg, shellarglen + 1);
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 */
234 #ifdef SYSTRACE
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 }
250 #else
251 error = copyinstr(epp->ep_name, *tmpsap++, MAXPATHLEN,
252 (size_t *)0);
253 #endif /* SYSTRACE */
254 #ifdef DIAGNOSTIC
255 if (error != 0)
256 panic("exec_script: copyinstr couldn't fail");
257 #endif
258 #ifdef FDSCRIPTS
259 } else
260 snprintf(*tmpsap++, MAXPATHLEN, "/dev/fd/%d", epp->ep_fd);
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;
275 oldpnbuf = epp->ep_ndp->ni_cnd.cn_pnbuf;
276
277 #ifdef VERIFIED_EXEC
278 if ((error = check_exec(p, epp, VERIEXEC_INDIRECT)) == 0) {
279 #else
280 if ((error = check_exec(p, epp, 0)) == 0) {
281 #endif
282 /* note that we've clobbered the header */
283 epp->ep_flags |= EXEC_DESTR|EXEC_HASES;
284
285 /*
286 * It succeeded. Unlock the script and
287 * close it if we aren't using it any more.
288 * Also, set things up so that the fake args
289 * list will be used.
290 */
291 if ((epp->ep_flags & EXEC_HASFD) == 0) {
292 vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY);
293 VOP_CLOSE(scriptvp, FREAD, p->p_ucred, p);
294 vput(scriptvp);
295 }
296
297 /* free the old pathname buffer */
298 PNBUF_PUT(oldpnbuf);
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
305 * handled appropriately. P_TRACED will be
306 * checked later when the shell is actually
307 * exec'd.
308 */
309 epp->ep_vap->va_mode |= script_sbits;
310 if (script_sbits & S_ISUID)
311 epp->ep_vap->va_uid = script_uid;
312 if (script_sbits & S_ISGID)
313 epp->ep_vap->va_gid = script_gid;
314 #endif
315 return (0);
316 }
317
318 /* XXX oldpnbuf not set for "goto fail" path */
319 epp->ep_ndp->ni_cnd.cn_pnbuf = oldpnbuf;
320 #ifdef FDSCRIPTS
321 fail:
322 #endif
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;
329 (void) fdrelease(p, epp->ep_fd);
330 } else if (scriptvp) {
331 vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY);
332 VOP_CLOSE(scriptvp, FREAD, p->p_ucred, p);
333 vput(scriptvp);
334 }
335
336 PNBUF_PUT(epp->ep_ndp->ni_cnd.cn_pnbuf);
337
338 /* free the fake arg list, because we're not returning it */
339 if ((tmpsap = shellargp) != NULL) {
340 while (*tmpsap != NULL) {
341 FREE(*tmpsap, M_EXEC);
342 tmpsap++;
343 }
344 FREE(shellargp, M_EXEC);
345 }
346
347 /*
348 * free any vmspace-creation commands,
349 * and release their references
350 */
351 kill_vmcmds(&epp->ep_vmcmds);
352
353 return error;
354 }
355