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