Annotation of src/sys/kern/exec_script.c, revision 1.84
1.84 ! riastrad 1: /* $NetBSD: exec_script.c,v 1.83 2021/05/03 10:25:14 fcambus Exp $ */
1.8 cgd 2:
1.1 cgd 3: /*
1.14 cgd 4: * Copyright (c) 1993, 1994, 1996 Christopher G. Demetriou
1.1 cgd 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
1.3 jtc 19: * derived from this software without specific prior written permission
1.1 cgd 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: */
1.30 lukem 32:
33: #include <sys/cdefs.h>
1.84 ! riastrad 34: __KERNEL_RCSID(0, "$NetBSD: exec_script.c,v 1.83 2021/05/03 10:25:14 fcambus Exp $");
1.76 christos 35:
1.77 kre 36: #ifdef _KERNEL_OPT
1.76 christos 37: #include "opt_script.h"
1.77 kre 38: #endif
1.1 cgd 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>
1.84 ! riastrad 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>
1.61 yamt 52: #include <sys/kmem.h>
1.84 ! riastrad 53: #include <sys/module.h>
1.1 cgd 54: #include <sys/namei.h>
1.84 ! riastrad 55: #include <sys/proc.h>
! 56: #include <sys/resourcevar.h>
1.18 christos 57: #ifdef SETUIDSCRIPTS
58: #include <sys/stat.h>
59: #endif
1.84 ! riastrad 60: #include <sys/systm.h>
! 61: #include <sys/vnode.h>
1.1 cgd 62:
1.67 christos 63: MODULE(MODULE_CLASS_EXEC, exec_script, NULL);
1.63 ad 64:
1.70 christos 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,
1.63 ad 78: };
79:
80: static int
81: exec_script_modcmd(modcmd_t cmd, void *arg)
82: {
83:
84: switch (cmd) {
85: case MODULE_CMD_INIT:
1.70 christos 86: return exec_add(&exec_script_execsw, 1);
1.63 ad 87:
88: case MODULE_CMD_FINI:
1.70 christos 89: return exec_remove(&exec_script_execsw, 1);
1.63 ad 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;
1.68 maxv 104: }
1.63 ad 105: }
106:
1.1 cgd 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
1.45 christos 120: exec_script_makecmds(struct lwp *l, struct exec_package *epp)
1.1 cgd 121: {
122: int error, hdrlinelen, shellnamelen, shellarglen;
123: char *hdrstr = epp->ep_hdr;
1.64 dholland 124: char *cp, *shellname, *shellarg;
1.61 yamt 125: size_t shellargp_len;
126: struct exec_fakearg *shellargp;
127: struct exec_fakearg *tmpsap;
1.66 dholland 128: struct pathbuf *shell_pathbuf;
1.1 cgd 129: struct vnode *scriptvp;
130: #ifdef SETUIDSCRIPTS
1.18 christos 131: /* Gcc needs those initialized for spurious uninitialized warning */
132: uid_t script_uid = (uid_t) -1;
133: gid_t script_gid = NOGROUP;
1.1 cgd 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: /*
1.73 maxv 147: * Check that the shell spec is terminated by a newline, and that
148: * it isn't too large.
1.1 cgd 149: */
1.78 riastrad 150: hdrlinelen = uimin(epp->ep_hdrvalid, SCRIPT_HDR_SIZE);
1.1 cgd 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: ;
1.72 maxv 165: if (*cp == '\0')
166: return ENOEXEC;
1.1 cgd 167:
1.73 maxv 168: shellarg = NULL;
169: shellarglen = 0;
170:
171: /* collect the shell name; remember its length for later */
1.1 cgd 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: /*
1.29 thorpej 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
1.54 ad 201: * will need to check PSL_TRACED later, however.
1.1 cgd 202: */
1.17 mycroft 203: script_sbits = epp->ep_vap->va_mode & (S_ISUID | S_ISGID);
1.1 cgd 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
1.82 pgoyette 214: * close all open fd's when they start. That kills this
1.1 cgd 215: * method of implementing "safe" set-id and x-only scripts.
216: */
1.81 ad 217: vn_lock(epp->ep_vp, LK_SHARED | LK_RETRY);
1.59 pooka 218: error = VOP_ACCESS(epp->ep_vp, VREAD, l->l_cred);
1.65 hannken 219: VOP_UNLOCK(epp->ep_vp);
1.14 cgd 220: if (error == EACCES
1.9 cgd 221: #ifdef SETUIDSCRIPTS
222: || script_sbits
223: #endif
224: ) {
1.1 cgd 225: struct file *fp;
226:
1.71 maxv 227: KASSERT(!(epp->ep_flags & EXEC_HASFD));
1.1 cgd 228:
1.62 ad 229: if ((error = fd_allocfile(&fp, &epp->ep_fd)) != 0) {
1.18 christos 230: scriptvp = NULL;
231: shellargp = NULL;
1.4 cgd 232: goto fail;
1.18 christos 233: }
1.1 cgd 234: epp->ep_flags |= EXEC_HASFD;
235: fp->f_type = DTYPE_VNODE;
236: fp->f_ops = &vnops;
1.74 matt 237: fp->f_vnode = epp->ep_vp;
1.1 cgd 238: fp->f_flag = FREAD;
1.62 ad 239: fd_affix(curproc, fp, epp->ep_fd);
1.1 cgd 240: }
241: #endif
242:
1.64 dholland 243: /* set up the fake args list */
1.61 yamt 244: shellargp_len = 4 * sizeof(*shellargp);
245: shellargp = kmem_alloc(shellargp_len, KM_SLEEP);
1.1 cgd 246: tmpsap = shellargp;
1.61 yamt 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++;
1.1 cgd 251: if (shellarg != NULL) {
1.61 yamt 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++;
1.1 cgd 256: }
1.61 yamt 257: tmpsap->fa_len = MAXPATHLEN;
258: tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP);
1.1 cgd 259: #ifdef FDSCRIPTS
260: if ((epp->ep_flags & EXEC_HASFD) == 0) {
261: #endif
262: /* normally can't fail, but check for it if diagnostic */
1.64 dholland 263: error = copystr(epp->ep_kname, tmpsap->fa_arg, MAXPATHLEN,
1.71 maxv 264: NULL);
265: KASSERT(error == 0);
1.61 yamt 266: tmpsap++;
1.1 cgd 267: #ifdef FDSCRIPTS
1.61 yamt 268: } else {
269: snprintf(tmpsap->fa_arg, MAXPATHLEN, "/dev/fd/%d", epp->ep_fd);
270: tmpsap++;
271: }
1.1 cgd 272: #endif
1.61 yamt 273: tmpsap->fa_arg = NULL;
1.1 cgd 274:
1.64 dholland 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:
1.1 cgd 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:
1.64 dholland 288: /* try loading the interpreter */
1.75 christos 289: if ((error = exec_makepathbuf(l, shellname, UIO_SYSSPACE,
290: &shell_pathbuf, NULL)) == 0) {
1.80 christos 291: error = check_exec(l, epp, shell_pathbuf, NULL);
1.66 dholland 292: pathbuf_destroy(shell_pathbuf);
293: }
1.1 cgd 294:
1.57 dsl 295: /* note that we've clobbered the header */
296: epp->ep_flags |= EXEC_DESTR;
1.64 dholland 297:
1.57 dsl 298: if (error == 0) {
1.1 cgd 299: /*
300: * It succeeded. Unlock the script and
301: * close it if we aren't using it any more.
1.4 cgd 302: * Also, set things up so that the fake args
1.1 cgd 303: * list will be used.
304: */
1.14 cgd 305: if ((epp->ep_flags & EXEC_HASFD) == 0) {
1.20 wrstuden 306: vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY);
1.58 pooka 307: VOP_CLOSE(scriptvp, FREAD, l->l_cred);
1.20 wrstuden 308: vput(scriptvp);
1.14 cgd 309: }
1.2 cgd 310:
1.1 cgd 311: epp->ep_flags |= (EXEC_HASARGL | EXEC_SKIPARG);
312: epp->ep_fa = shellargp;
1.61 yamt 313: epp->ep_fa_len = shellargp_len;
1.1 cgd 314: #ifdef SETUIDSCRIPTS
315: /*
316: * set thing up so that set-id scripts will be
1.54 ad 317: * handled appropriately. PSL_TRACED will be
1.29 thorpej 318: * checked later when the shell is actually
319: * exec'd.
1.1 cgd 320: */
321: epp->ep_vap->va_mode |= script_sbits;
1.17 mycroft 322: if (script_sbits & S_ISUID)
1.1 cgd 323: epp->ep_vap->va_uid = script_uid;
1.17 mycroft 324: if (script_sbits & S_ISGID)
1.1 cgd 325: epp->ep_vap->va_gid = script_gid;
326: #endif
1.4 cgd 327: return (0);
328: }
329:
1.13 christos 330: #ifdef FDSCRIPTS
1.4 cgd 331: fail:
1.13 christos 332: #endif
1.4 cgd 333:
334: /* kill the opened file descriptor, else close the file */
1.68 maxv 335: if (epp->ep_flags & EXEC_HASFD) {
336: epp->ep_flags &= ~EXEC_HASFD;
337: fd_close(epp->ep_fd);
338: } else if (scriptvp) {
1.20 wrstuden 339: vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY);
1.58 pooka 340: VOP_CLOSE(scriptvp, FREAD, l->l_cred);
1.20 wrstuden 341: vput(scriptvp);
1.14 cgd 342: }
1.4 cgd 343:
344: /* free the fake arg list, because we're not returning it */
1.18 christos 345: if ((tmpsap = shellargp) != NULL) {
1.61 yamt 346: while (tmpsap->fa_arg != NULL) {
347: kmem_free(tmpsap->fa_arg, tmpsap->fa_len);
1.18 christos 348: tmpsap++;
349: }
1.61 yamt 350: kmem_free(shellargp, shellargp_len);
1.1 cgd 351: }
1.4 cgd 352:
1.68 maxv 353: /*
354: * free any vmspace-creation commands,
355: * and release their references
356: */
357: kill_vmcmds(&epp->ep_vmcmds);
1.1 cgd 358:
1.68 maxv 359: return error;
1.1 cgd 360: }
CVSweb <webmaster@jp.NetBSD.org>