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