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