exec_script.c revision 1.84 1 /* $NetBSD: exec_script.c,v 1.84 2024/12/06 16:18:41 riastradh 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.84 2024/12/06 16:18:41 riastradh Exp $");
35
36 #ifdef _KERNEL_OPT
37 #include "opt_script.h"
38 #endif
39
40 #if defined(SETUIDSCRIPTS) && !defined(FDSCRIPTS)
41 #define FDSCRIPTS /* Need this for safe set-id scripts. */
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/types.h>
46
47 #include <sys/exec.h>
48 #include <sys/exec_elf.h>
49 #include <sys/exec_script.h>
50 #include <sys/file.h>
51 #include <sys/filedesc.h>
52 #include <sys/kmem.h>
53 #include <sys/module.h>
54 #include <sys/namei.h>
55 #include <sys/proc.h>
56 #include <sys/resourcevar.h>
57 #ifdef SETUIDSCRIPTS
58 #include <sys/stat.h>
59 #endif
60 #include <sys/systm.h>
61 #include <sys/vnode.h>
62
63 MODULE(MODULE_CLASS_EXEC, exec_script, NULL);
64
65 static struct execsw exec_script_execsw = {
66 .es_hdrsz = SCRIPT_HDR_SIZE,
67 .es_makecmds = exec_script_makecmds,
68 .u = {
69 .elf_probe_func = NULL,
70 },
71 .es_emul = NULL,
72 .es_prio = EXECSW_PRIO_ANY,
73 .es_arglen = 0,
74 .es_copyargs = NULL,
75 .es_setregs = NULL,
76 .es_coredump = NULL,
77 .es_setup_stack = exec_setup_stack,
78 };
79
80 static int
81 exec_script_modcmd(modcmd_t cmd, void *arg)
82 {
83
84 switch (cmd) {
85 case MODULE_CMD_INIT:
86 return exec_add(&exec_script_execsw, 1);
87
88 case MODULE_CMD_FINI:
89 return exec_remove(&exec_script_execsw, 1);
90
91 case MODULE_CMD_AUTOUNLOAD:
92 /*
93 * We don't want to be autounloaded because our use is
94 * transient: no executables with p_execsw equal to
95 * exec_script_execsw will exist, so FINI will never
96 * return EBUSY. However, the system will run scripts
97 * often. Return EBUSY here to prevent this module from
98 * ping-ponging in and out of the kernel.
99 */
100 return EBUSY;
101
102 default:
103 return ENOTTY;
104 }
105 }
106
107 /*
108 * exec_script_makecmds(): Check if it's an executable shell script.
109 *
110 * Given a proc pointer and an exec package pointer, see if the referent
111 * of the epp is in shell script. If it is, then set thing up so that
112 * the script can be run. This involves preparing the address space
113 * and arguments for the shell which will run the script.
114 *
115 * This function is ultimately responsible for creating a set of vmcmds
116 * which can be used to build the process's vm space and inserting them
117 * into the exec package.
118 */
119 int
120 exec_script_makecmds(struct lwp *l, struct exec_package *epp)
121 {
122 int error, hdrlinelen, shellnamelen, shellarglen;
123 char *hdrstr = epp->ep_hdr;
124 char *cp, *shellname, *shellarg;
125 size_t shellargp_len;
126 struct exec_fakearg *shellargp;
127 struct exec_fakearg *tmpsap;
128 struct pathbuf *shell_pathbuf;
129 struct vnode *scriptvp;
130 #ifdef SETUIDSCRIPTS
131 /* Gcc needs those initialized for spurious uninitialized warning */
132 uid_t script_uid = (uid_t) -1;
133 gid_t script_gid = NOGROUP;
134 u_short script_sbits;
135 #endif
136
137 /*
138 * if the magic isn't that of a shell script, or we've already
139 * done shell script processing for this exec, punt on it.
140 */
141 if ((epp->ep_flags & EXEC_INDIR) != 0 ||
142 epp->ep_hdrvalid < EXEC_SCRIPT_MAGICLEN ||
143 strncmp(hdrstr, EXEC_SCRIPT_MAGIC, EXEC_SCRIPT_MAGICLEN))
144 return ENOEXEC;
145
146 /*
147 * Check that the shell spec is terminated by a newline, and that
148 * it isn't too large.
149 */
150 hdrlinelen = uimin(epp->ep_hdrvalid, SCRIPT_HDR_SIZE);
151 for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; cp < hdrstr + hdrlinelen;
152 cp++) {
153 if (*cp == '\n') {
154 *cp = '\0';
155 break;
156 }
157 }
158 if (cp >= hdrstr + hdrlinelen)
159 return ENOEXEC;
160
161 /* strip spaces before the shell name */
162 for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; *cp == ' ' || *cp == '\t';
163 cp++)
164 ;
165 if (*cp == '\0')
166 return ENOEXEC;
167
168 shellarg = NULL;
169 shellarglen = 0;
170
171 /* collect the shell name; remember its length for later */
172 shellname = cp;
173 shellnamelen = 0;
174 for ( /* cp = cp */ ; *cp != '\0' && *cp != ' ' && *cp != '\t'; cp++)
175 shellnamelen++;
176 if (*cp == '\0')
177 goto check_shell;
178 *cp++ = '\0';
179
180 /* skip spaces before any argument */
181 for ( /* cp = cp */ ; *cp == ' ' || *cp == '\t'; cp++)
182 ;
183 if (*cp == '\0')
184 goto check_shell;
185
186 /*
187 * collect the shell argument. everything after the shell name
188 * is passed as ONE argument; that's the correct (historical)
189 * behaviour.
190 */
191 shellarg = cp;
192 for ( /* cp = cp */ ; *cp != '\0'; cp++)
193 shellarglen++;
194 *cp++ = '\0';
195
196 check_shell:
197 #ifdef SETUIDSCRIPTS
198 /*
199 * MNT_NOSUID has already taken care of by check_exec,
200 * so we don't need to worry about it now or later. We
201 * will need to check PSL_TRACED later, however.
202 */
203 script_sbits = epp->ep_vap->va_mode & (S_ISUID | S_ISGID);
204 if (script_sbits != 0) {
205 script_uid = epp->ep_vap->va_uid;
206 script_gid = epp->ep_vap->va_gid;
207 }
208 #endif
209 #ifdef FDSCRIPTS
210 /*
211 * if the script isn't readable, or it's set-id, then we've
212 * gotta supply a "/dev/fd/..." for the shell to read.
213 * Note that stupid shells (csh) do the wrong thing, and
214 * close all open fd's when they start. That kills this
215 * method of implementing "safe" set-id and x-only scripts.
216 */
217 vn_lock(epp->ep_vp, LK_SHARED | LK_RETRY);
218 error = VOP_ACCESS(epp->ep_vp, VREAD, l->l_cred);
219 VOP_UNLOCK(epp->ep_vp);
220 if (error == EACCES
221 #ifdef SETUIDSCRIPTS
222 || script_sbits
223 #endif
224 ) {
225 struct file *fp;
226
227 KASSERT(!(epp->ep_flags & EXEC_HASFD));
228
229 if ((error = fd_allocfile(&fp, &epp->ep_fd)) != 0) {
230 scriptvp = NULL;
231 shellargp = NULL;
232 goto fail;
233 }
234 epp->ep_flags |= EXEC_HASFD;
235 fp->f_type = DTYPE_VNODE;
236 fp->f_ops = &vnops;
237 fp->f_vnode = epp->ep_vp;
238 fp->f_flag = FREAD;
239 fd_affix(curproc, fp, epp->ep_fd);
240 }
241 #endif
242
243 /* set up the fake args list */
244 shellargp_len = 4 * sizeof(*shellargp);
245 shellargp = kmem_alloc(shellargp_len, KM_SLEEP);
246 tmpsap = shellargp;
247 tmpsap->fa_len = shellnamelen + 1;
248 tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP);
249 strlcpy(tmpsap->fa_arg, shellname, tmpsap->fa_len);
250 tmpsap++;
251 if (shellarg != NULL) {
252 tmpsap->fa_len = shellarglen + 1;
253 tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP);
254 strlcpy(tmpsap->fa_arg, shellarg, tmpsap->fa_len);
255 tmpsap++;
256 }
257 tmpsap->fa_len = MAXPATHLEN;
258 tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP);
259 #ifdef FDSCRIPTS
260 if ((epp->ep_flags & EXEC_HASFD) == 0) {
261 #endif
262 /* normally can't fail, but check for it if diagnostic */
263 error = copystr(epp->ep_kname, tmpsap->fa_arg, MAXPATHLEN,
264 NULL);
265 KASSERT(error == 0);
266 tmpsap++;
267 #ifdef FDSCRIPTS
268 } else {
269 snprintf(tmpsap->fa_arg, MAXPATHLEN, "/dev/fd/%d", epp->ep_fd);
270 tmpsap++;
271 }
272 #endif
273 tmpsap->fa_arg = NULL;
274
275 /* Save the old vnode so we can clean it up later. */
276 scriptvp = epp->ep_vp;
277 epp->ep_vp = NULL;
278
279 /* Note that we're trying recursively. */
280 epp->ep_flags |= EXEC_INDIR;
281
282 /*
283 * mark the header we have as invalid; check_exec will read
284 * the header from the new executable
285 */
286 epp->ep_hdrvalid = 0;
287
288 /* try loading the interpreter */
289 if ((error = exec_makepathbuf(l, shellname, UIO_SYSSPACE,
290 &shell_pathbuf, NULL)) == 0) {
291 error = check_exec(l, epp, shell_pathbuf, NULL);
292 pathbuf_destroy(shell_pathbuf);
293 }
294
295 /* note that we've clobbered the header */
296 epp->ep_flags |= EXEC_DESTR;
297
298 if (error == 0) {
299 /*
300 * It succeeded. Unlock the script and
301 * close it if we aren't using it any more.
302 * Also, set things up so that the fake args
303 * list will be used.
304 */
305 if ((epp->ep_flags & EXEC_HASFD) == 0) {
306 vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY);
307 VOP_CLOSE(scriptvp, FREAD, l->l_cred);
308 vput(scriptvp);
309 }
310
311 epp->ep_flags |= (EXEC_HASARGL | EXEC_SKIPARG);
312 epp->ep_fa = shellargp;
313 epp->ep_fa_len = shellargp_len;
314 #ifdef SETUIDSCRIPTS
315 /*
316 * set thing up so that set-id scripts will be
317 * handled appropriately. PSL_TRACED will be
318 * checked later when the shell is actually
319 * exec'd.
320 */
321 epp->ep_vap->va_mode |= script_sbits;
322 if (script_sbits & S_ISUID)
323 epp->ep_vap->va_uid = script_uid;
324 if (script_sbits & S_ISGID)
325 epp->ep_vap->va_gid = script_gid;
326 #endif
327 return (0);
328 }
329
330 #ifdef FDSCRIPTS
331 fail:
332 #endif
333
334 /* kill the opened file descriptor, else close the file */
335 if (epp->ep_flags & EXEC_HASFD) {
336 epp->ep_flags &= ~EXEC_HASFD;
337 fd_close(epp->ep_fd);
338 } else if (scriptvp) {
339 vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY);
340 VOP_CLOSE(scriptvp, FREAD, l->l_cred);
341 vput(scriptvp);
342 }
343
344 /* free the fake arg list, because we're not returning it */
345 if ((tmpsap = shellargp) != NULL) {
346 while (tmpsap->fa_arg != NULL) {
347 kmem_free(tmpsap->fa_arg, tmpsap->fa_len);
348 tmpsap++;
349 }
350 kmem_free(shellargp, shellargp_len);
351 }
352
353 /*
354 * free any vmspace-creation commands,
355 * and release their references
356 */
357 kill_vmcmds(&epp->ep_vmcmds);
358
359 return error;
360 }
361