xref: /freebsd/sys/kern/imgact_shell.c (revision 0f2bd1e89db1a2f09268edea21e0ead329e092df)
1 /*-
2  * Copyright (c) 1993, David Greenman
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/vnode.h>
32 #include <sys/proc.h>
33 #include <sys/sbuf.h>
34 #include <sys/systm.h>
35 #include <sys/sysproto.h>
36 #include <sys/exec.h>
37 #include <sys/imgact.h>
38 #include <sys/kernel.h>
39 
40 #if BYTE_ORDER == LITTLE_ENDIAN
41 #define SHELLMAGIC	0x2123 /* #! */
42 #else
43 #define SHELLMAGIC	0x2321
44 #endif
45 
46 /*
47  * At the time of this writing, MAXSHELLCMDLEN == PAGE_SIZE.  This is
48  * significant because the caller has only mapped in one page of the
49  * file we're reading.  This code should be changed to know how to
50  * read in the second page, but I'm not doing that just yet...
51  */
52 #if MAXSHELLCMDLEN > PAGE_SIZE
53 #error "MAXSHELLCMDLEN is larger than a single page!"
54 #endif
55 
56 /**
57  * Shell interpreter image activator. An interpreter name beginning at
58  * imgp->args->begin_argv is the minimal successful exit requirement.
59  *
60  * If the given file is a shell-script, then the first line will start
61  * with the two characters `#!' (aka SHELLMAGIC), followed by the name
62  * of the shell-interpreter to run, followed by zero or more tokens.
63  *
64  * The interpreter is then started up such that it will see:
65  *    arg[0] -> The name of interpreter as specified after `#!' in the
66  *		first line of the script.  The interpreter name must
67  *		not be longer than MAXSHELLCMDLEN bytes.
68  *    arg[1] -> *If* there are any additional tokens on the first line,
69  *		then we add a new arg[1], which is a copy of the rest of
70  *		that line.  The copy starts at the first token after the
71  *		interpreter name.  We leave it to the interpreter to
72  *		parse the tokens in that value.
73  *    arg[x] -> the full pathname of the script.  This will either be
74  *		arg[2] or arg[1], depending on whether or not tokens
75  *		were found after the interpreter name.
76  *  arg[x+1] -> all the arguments that were specified on the original
77  *		command line.
78  *
79  * This processing is described in the execve(2) man page.
80  */
81 
82 /*
83  * HISTORICAL NOTE: From 1993 to mid-2005, FreeBSD parsed out the tokens as
84  * found on the first line of the script, and setup each token as a separate
85  * value in arg[].  This extra processing did not match the behavior of other
86  * OS's, and caused a few subtle problems.  For one, it meant the kernel was
87  * deciding how those values should be parsed (wrt characters for quoting or
88  * comments, etc), while the interpreter might have other rules for parsing.
89  * It also meant the interpreter had no way of knowing which arguments came
90  * from the first line of the shell script, and which arguments were specified
91  * by the user on the command line.  That extra processing was dropped in the
92  * 6.x branch on May 28, 2005 (matching __FreeBSD_version 600029).
93  */
94 int
95 exec_shell_imgact(imgp)
96 	struct image_params *imgp;
97 {
98 	const char *image_header = imgp->image_header;
99 	const char *ihp, *interpb, *interpe, *maxp, *optb, *opte, *fname;
100 	int error, offset;
101 	size_t length, clength;
102 	struct vattr vattr;
103 	struct sbuf *sname;
104 
105 	/* a shell script? */
106 	if (((const short *) image_header)[0] != SHELLMAGIC)
107 		return(-1);
108 
109 	/*
110 	 * Don't allow a shell script to be the shell for a shell
111 	 *	script. :-)
112 	 */
113 	if (imgp->interpreted)
114 		return(ENOEXEC);
115 
116 	imgp->interpreted = 1;
117 
118 	/*
119 	 * At this point we have the first page of the file mapped.
120 	 * However, we don't know how far into the page the contents are
121 	 * valid -- the actual file might be much shorter than the page.
122 	 * So find out the file size.
123  	 */
124 	error = VOP_GETATTR(imgp->vp, &vattr, imgp->proc->p_ucred);
125 	if (error)
126 		return (error);
127 
128 	/*
129 	 * Copy shell name and arguments from image_header into a string
130 	 *	buffer.  Remember that the caller has mapped only the
131 	 *	first page of the file into memory.
132 	 */
133 	clength = (vattr.va_size > PAGE_SIZE) ? PAGE_SIZE : vattr.va_size;
134 
135 	maxp = &image_header[clength];
136 	ihp = &image_header[2];
137 
138 	/*
139 	 * Find the beginning and end of the interpreter_name.  If the
140 	 * line does not include any interpreter, or if the name which
141 	 * was found is too long, we bail out.
142 	 */
143 	while (ihp < maxp && ((*ihp == ' ') || (*ihp == '\t')))
144 		ihp++;
145 	interpb = ihp;
146 	while (ihp < maxp && ((*ihp != ' ') && (*ihp != '\t') && (*ihp != '\n')
147 	    && (*ihp != '\0')))
148 		ihp++;
149 	interpe = ihp;
150 	if (interpb == interpe)
151 		return (ENOEXEC);
152 	if ((interpe - interpb) >= MAXSHELLCMDLEN)
153 		return (ENAMETOOLONG);
154 
155 	/*
156 	 * Find the beginning of the options (if any), and the end-of-line.
157 	 * Then trim the trailing blanks off the value.  Note that some
158 	 * other operating systems do *not* trim the trailing whitespace...
159 	 */
160 	while (ihp < maxp && ((*ihp == ' ') || (*ihp == '\t')))
161 		ihp++;
162 	optb = ihp;
163 	while (ihp < maxp && ((*ihp != '\n') && (*ihp != '\0')))
164 		ihp++;
165 	opte = ihp;
166 	while (--ihp > optb && ((*ihp == ' ') || (*ihp == '\t')))
167 		opte = ihp;
168 
169 	if (imgp->args->fname != NULL) {
170 		fname = imgp->args->fname;
171 		sname = NULL;
172 	} else {
173 		sname = sbuf_new_auto();
174 		sbuf_printf(sname, "/dev/fd/%d", imgp->args->fd);
175 		sbuf_finish(sname);
176 		fname = sbuf_data(sname);
177 	}
178 
179 	/*
180 	 * We need to "pop" (remove) the present value of arg[0], and "push"
181 	 * either two or three new values in the arg[] list.  To do this,
182 	 * we first shift all the other values in the `begin_argv' area to
183 	 * provide the exact amount of room for the values added.  Set up
184 	 * `offset' as the number of bytes to be added to the `begin_argv'
185 	 * area, and 'length' as the number of bytes being removed.
186 	 */
187 	offset = interpe - interpb + 1;			/* interpreter */
188 	if (opte > optb)				/* options (if any) */
189 		offset += opte - optb + 1;
190 	offset += strlen(fname) + 1;			/* fname of script */
191 	length = (imgp->args->argc == 0) ? 0 :
192 	    strlen(imgp->args->begin_argv) + 1;		/* bytes to delete */
193 
194 	if (offset - length > imgp->args->stringspace) {
195 		if (sname != NULL)
196 			sbuf_delete(sname);
197 		return (E2BIG);
198 	}
199 
200 	bcopy(imgp->args->begin_argv + length, imgp->args->begin_argv + offset,
201 	    imgp->args->endp - (imgp->args->begin_argv + length));
202 
203 	offset -= length;		/* calculate actual adjustment */
204 	imgp->args->begin_envv += offset;
205 	imgp->args->endp += offset;
206 	imgp->args->stringspace -= offset;
207 
208 	/*
209 	 * If there was no arg[0] when we started, then the interpreter_name
210 	 * is adding an argument (instead of replacing the arg[0] we started
211 	 * with).  And we're always adding an argument when we include the
212 	 * full pathname of the original script.
213 	 */
214 	if (imgp->args->argc == 0)
215 		imgp->args->argc = 1;
216 	imgp->args->argc++;
217 
218 	/*
219 	 * The original arg[] list has been shifted appropriately.  Copy in
220 	 * the interpreter name and options-string.
221 	 */
222 	length = interpe - interpb;
223 	bcopy(interpb, imgp->args->begin_argv, length);
224 	*(imgp->args->begin_argv + length) = '\0';
225 	offset = length + 1;
226 	if (opte > optb) {
227 		length = opte - optb;
228 		bcopy(optb, imgp->args->begin_argv + offset, length);
229 		*(imgp->args->begin_argv + offset + length) = '\0';
230 		offset += length + 1;
231 		imgp->args->argc++;
232 	}
233 
234 	/*
235 	 * Finally, add the filename onto the end for the interpreter to
236 	 * use and copy the interpreter's name to imgp->interpreter_name
237 	 * for exec to use.
238 	 */
239 	error = copystr(fname, imgp->args->begin_argv + offset,
240 	    imgp->args->stringspace, NULL);
241 
242 	if (error == 0)
243 		imgp->interpreter_name = imgp->args->begin_argv;
244 
245 	if (sname != NULL)
246 		sbuf_delete(sname);
247 	return (error);
248 }
249 
250 /*
251  * Tell kern_execve.c about it, with a little help from the linker.
252  */
253 static struct execsw shell_execsw = { exec_shell_imgact, "#!" };
254 EXEC_SET(shell, shell_execsw);
255