xref: /freebsd/sys/kern/imgact_shell.c (revision 2af6e14d39f2e502889671dfaf10a11b663699b8)
19454b2d8SWarner Losh /*-
2cfefd687SGarrett Wollman  * Copyright (c) 1993, David Greenman
3cfefd687SGarrett Wollman  * All rights reserved.
4cfefd687SGarrett Wollman  *
5cfefd687SGarrett Wollman  * Redistribution and use in source and binary forms, with or without
6cfefd687SGarrett Wollman  * modification, are permitted provided that the following conditions
7cfefd687SGarrett Wollman  * are met:
8cfefd687SGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
9cfefd687SGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
10cfefd687SGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
11cfefd687SGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
12cfefd687SGarrett Wollman  *    documentation and/or other materials provided with the distribution.
13cfefd687SGarrett Wollman  *
14cfefd687SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15cfefd687SGarrett Wollman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16cfefd687SGarrett Wollman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171984b014SDavid Greenman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18cfefd687SGarrett Wollman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19cfefd687SGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20cfefd687SGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21cfefd687SGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22cfefd687SGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23cfefd687SGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24cfefd687SGarrett Wollman  * SUCH DAMAGE.
25cfefd687SGarrett Wollman  */
26cfefd687SGarrett Wollman 
27677b542eSDavid E. O'Brien #include <sys/cdefs.h>
28677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
29677b542eSDavid E. O'Brien 
30f540b106SGarrett Wollman #include <sys/param.h>
316916a1daSMaxim Sobolev #include <sys/vnode.h>
326916a1daSMaxim Sobolev #include <sys/proc.h>
33eaad1099SKonstantin Belousov #include <sys/sbuf.h>
34aa855a59SPeter Wemm #include <sys/systm.h>
35ad7507e2SSteven Wallace #include <sys/sysproto.h>
3626f9a767SRodney W. Grimes #include <sys/exec.h>
37f540b106SGarrett Wollman #include <sys/imgact.h>
38f540b106SGarrett Wollman #include <sys/kernel.h>
39cfefd687SGarrett Wollman 
4092d91f76SGarrett Wollman #if BYTE_ORDER == LITTLE_ENDIAN
41cfefd687SGarrett Wollman #define SHELLMAGIC	0x2123 /* #! */
4292d91f76SGarrett Wollman #else
4392d91f76SGarrett Wollman #define SHELLMAGIC	0x2321
4492d91f76SGarrett Wollman #endif
4592d91f76SGarrett Wollman 
46cfefd687SGarrett Wollman /*
475f49915eSGarance A Drosehn  * At the time of this writing, MAXSHELLCMDLEN == PAGE_SIZE.  This is
485f49915eSGarance A Drosehn  * significant because the caller has only mapped in one page of the
495f49915eSGarance A Drosehn  * file we're reading.  This code should be changed to know how to
505f49915eSGarance A Drosehn  * read in the second page, but I'm not doing that just yet...
515f49915eSGarance A Drosehn  */
525f49915eSGarance A Drosehn #if MAXSHELLCMDLEN > PAGE_SIZE
535f49915eSGarance A Drosehn #error "MAXSHELLCMDLEN is larger than a single page!"
545f49915eSGarance A Drosehn #endif
555f49915eSGarance A Drosehn 
565f49915eSGarance A Drosehn /**
575f49915eSGarance A Drosehn  * Shell interpreter image activator. An interpreter name beginning at
585f49915eSGarance A Drosehn  * imgp->args->begin_argv is the minimal successful exit requirement.
595f49915eSGarance A Drosehn  *
605f49915eSGarance A Drosehn  * If the given file is a shell-script, then the first line will start
615f49915eSGarance A Drosehn  * with the two characters `#!' (aka SHELLMAGIC), followed by the name
625f49915eSGarance A Drosehn  * of the shell-interpreter to run, followed by zero or more tokens.
635f49915eSGarance A Drosehn  *
645f49915eSGarance A Drosehn  * The interpreter is then started up such that it will see:
655f49915eSGarance A Drosehn  *    arg[0] -> The name of interpreter as specified after `#!' in the
665f49915eSGarance A Drosehn  *		first line of the script.  The interpreter name must
675f49915eSGarance A Drosehn  *		not be longer than MAXSHELLCMDLEN bytes.
685f49915eSGarance A Drosehn  *    arg[1] -> *If* there are any additional tokens on the first line,
695f49915eSGarance A Drosehn  *		then we add a new arg[1], which is a copy of the rest of
705f49915eSGarance A Drosehn  *		that line.  The copy starts at the first token after the
715f49915eSGarance A Drosehn  *		interpreter name.  We leave it to the interpreter to
725f49915eSGarance A Drosehn  *		parse the tokens in that value.
735f49915eSGarance A Drosehn  *    arg[x] -> the full pathname of the script.  This will either be
745f49915eSGarance A Drosehn  *		arg[2] or arg[1], depending on whether or not tokens
755f49915eSGarance A Drosehn  *		were found after the interpreter name.
765f49915eSGarance A Drosehn  *  arg[x+1] -> all the arguments that were specified on the original
775f49915eSGarance A Drosehn  *		command line.
785f49915eSGarance A Drosehn  *
795f49915eSGarance A Drosehn  * This processing is described in the execve(2) man page.
805f49915eSGarance A Drosehn  */
815f49915eSGarance A Drosehn 
825f49915eSGarance A Drosehn /*
835f49915eSGarance A Drosehn  * HISTORICAL NOTE: From 1993 to mid-2005, FreeBSD parsed out the tokens as
845f49915eSGarance A Drosehn  * found on the first line of the script, and setup each token as a separate
855f49915eSGarance A Drosehn  * value in arg[].  This extra processing did not match the behavior of other
865f49915eSGarance A Drosehn  * OS's, and caused a few subtle problems.  For one, it meant the kernel was
875f49915eSGarance A Drosehn  * deciding how those values should be parsed (wrt characters for quoting or
885f49915eSGarance A Drosehn  * comments, etc), while the interpreter might have other rules for parsing.
895f49915eSGarance A Drosehn  * It also meant the interpreter had no way of knowing which arguments came
905f49915eSGarance A Drosehn  * from the first line of the shell script, and which arguments were specified
91386ea932SGarance A Drosehn  * by the user on the command line.  That extra processing was dropped in the
92386ea932SGarance A Drosehn  * 6.x branch on May 28, 2005 (matching __FreeBSD_version 600029).
93cfefd687SGarrett Wollman  */
94d323ddf3SMatthew Dillon int
95c52007c2SDavid Greenman exec_shell_imgact(imgp)
96c52007c2SDavid Greenman 	struct image_params *imgp;
97cfefd687SGarrett Wollman {
98c52007c2SDavid Greenman 	const char *image_header = imgp->image_header;
99eaad1099SKonstantin Belousov 	const char *ihp, *interpb, *interpe, *maxp, *optb, *opte, *fname;
100ec217396SMaxim Sobolev 	int error, offset;
1016916a1daSMaxim Sobolev 	size_t length, clength;
1026916a1daSMaxim Sobolev 	struct vattr vattr;
103eaad1099SKonstantin Belousov 	struct sbuf *sname;
104cfefd687SGarrett Wollman 
105cfefd687SGarrett Wollman 	/* a shell script? */
106e0c95ed9SBruce Evans 	if (((const short *) image_header)[0] != SHELLMAGIC)
107cfefd687SGarrett Wollman 		return(-1);
108cfefd687SGarrett Wollman 
109cfefd687SGarrett Wollman 	/*
110cfefd687SGarrett Wollman 	 * Don't allow a shell script to be the shell for a shell
111cfefd687SGarrett Wollman 	 *	script. :-)
112cfefd687SGarrett Wollman 	 */
113c52007c2SDavid Greenman 	if (imgp->interpreted)
114cfefd687SGarrett Wollman 		return(ENOEXEC);
115cfefd687SGarrett Wollman 
116c52007c2SDavid Greenman 	imgp->interpreted = 1;
117cfefd687SGarrett Wollman 
118cfefd687SGarrett Wollman 	/*
1196916a1daSMaxim Sobolev 	 * At this point we have the first page of the file mapped.
1206916a1daSMaxim Sobolev 	 * However, we don't know how far into the page the contents are
1216916a1daSMaxim Sobolev 	 * valid -- the actual file might be much shorter than the page.
1226916a1daSMaxim Sobolev 	 * So find out the file size.
1236916a1daSMaxim Sobolev  	 */
1240359a12eSAttilio Rao 	error = VOP_GETATTR(imgp->vp, &vattr, imgp->proc->p_ucred);
1256916a1daSMaxim Sobolev 	if (error)
1266916a1daSMaxim Sobolev 		return (error);
1276916a1daSMaxim Sobolev 
1285f49915eSGarance A Drosehn 	/*
1295f49915eSGarance A Drosehn 	 * Copy shell name and arguments from image_header into a string
1305f49915eSGarance A Drosehn 	 *	buffer.  Remember that the caller has mapped only the
1315f49915eSGarance A Drosehn 	 *	first page of the file into memory.
1325f49915eSGarance A Drosehn 	 */
1335f49915eSGarance A Drosehn 	clength = (vattr.va_size > PAGE_SIZE) ? PAGE_SIZE : vattr.va_size;
1345f49915eSGarance A Drosehn 
1355f49915eSGarance A Drosehn 	maxp = &image_header[clength];
1365f49915eSGarance A Drosehn 	ihp = &image_header[2];
1375f49915eSGarance A Drosehn 
1386916a1daSMaxim Sobolev 	/*
1395f49915eSGarance A Drosehn 	 * Find the beginning and end of the interpreter_name.  If the
1405f49915eSGarance A Drosehn 	 * line does not include any interpreter, or if the name which
1415f49915eSGarance A Drosehn 	 * was found is too long, we bail out.
1425f49915eSGarance A Drosehn 	 */
1435f49915eSGarance A Drosehn 	while (ihp < maxp && ((*ihp == ' ') || (*ihp == '\t')))
1445f49915eSGarance A Drosehn 		ihp++;
1455f49915eSGarance A Drosehn 	interpb = ihp;
1465f49915eSGarance A Drosehn 	while (ihp < maxp && ((*ihp != ' ') && (*ihp != '\t') && (*ihp != '\n')
1475f49915eSGarance A Drosehn 	    && (*ihp != '\0')))
1485f49915eSGarance A Drosehn 		ihp++;
1495f49915eSGarance A Drosehn 	interpe = ihp;
1505f49915eSGarance A Drosehn 	if (interpb == interpe)
1515f49915eSGarance A Drosehn 		return (ENOEXEC);
1525f49915eSGarance A Drosehn 	if ((interpe - interpb) >= MAXSHELLCMDLEN)
1535f49915eSGarance A Drosehn 		return (ENAMETOOLONG);
154cfefd687SGarrett Wollman 
155610ecfe0SMaxim Sobolev 	/*
1565f49915eSGarance A Drosehn 	 * Find the beginning of the options (if any), and the end-of-line.
1575f49915eSGarance A Drosehn 	 * Then trim the trailing blanks off the value.  Note that some
1585f49915eSGarance A Drosehn 	 * other operating systems do *not* trim the trailing whitespace...
1595f49915eSGarance A Drosehn 	 */
1605f49915eSGarance A Drosehn 	while (ihp < maxp && ((*ihp == ' ') || (*ihp == '\t')))
1615f49915eSGarance A Drosehn 		ihp++;
1625f49915eSGarance A Drosehn 	optb = ihp;
1635f49915eSGarance A Drosehn 	while (ihp < maxp && ((*ihp != '\n') && (*ihp != '\0')))
1645f49915eSGarance A Drosehn 		ihp++;
1655f49915eSGarance A Drosehn 	opte = ihp;
166bd3aace7SGarance A Drosehn 	while (--ihp > optb && ((*ihp == ' ') || (*ihp == '\t')))
1675f49915eSGarance A Drosehn 		opte = ihp;
1685f49915eSGarance A Drosehn 
169eaad1099SKonstantin Belousov 	if (imgp->args->fname != NULL) {
170eaad1099SKonstantin Belousov 		fname = imgp->args->fname;
171eaad1099SKonstantin Belousov 		sname = NULL;
172eaad1099SKonstantin Belousov 	} else {
173eaad1099SKonstantin Belousov 		sname = sbuf_new_auto();
174eaad1099SKonstantin Belousov 		sbuf_printf(sname, "/dev/fd/%d", imgp->args->fd);
175eaad1099SKonstantin Belousov 		sbuf_finish(sname);
176eaad1099SKonstantin Belousov 		fname = sbuf_data(sname);
177eaad1099SKonstantin Belousov 	}
178eaad1099SKonstantin Belousov 
1795f49915eSGarance A Drosehn 	/*
1805f49915eSGarance A Drosehn 	 * We need to "pop" (remove) the present value of arg[0], and "push"
1815f49915eSGarance A Drosehn 	 * either two or three new values in the arg[] list.  To do this,
1825f49915eSGarance A Drosehn 	 * we first shift all the other values in the `begin_argv' area to
1835f49915eSGarance A Drosehn 	 * provide the exact amount of room for the values added.  Set up
1845f49915eSGarance A Drosehn 	 * `offset' as the number of bytes to be added to the `begin_argv'
1855f49915eSGarance A Drosehn 	 * area, and 'length' as the number of bytes being removed.
1865f49915eSGarance A Drosehn 	 */
1875f49915eSGarance A Drosehn 	offset = interpe - interpb + 1;			/* interpreter */
188bd3aace7SGarance A Drosehn 	if (opte > optb)				/* options (if any) */
1895f49915eSGarance A Drosehn 		offset += opte - optb + 1;
190eaad1099SKonstantin Belousov 	offset += strlen(fname) + 1;			/* fname of script */
1915f49915eSGarance A Drosehn 	length = (imgp->args->argc == 0) ? 0 :
1925f49915eSGarance A Drosehn 	    strlen(imgp->args->begin_argv) + 1;		/* bytes to delete */
1935f49915eSGarance A Drosehn 
194eaad1099SKonstantin Belousov 	if (offset - length > imgp->args->stringspace) {
195eaad1099SKonstantin Belousov 		if (sname != NULL)
196eaad1099SKonstantin Belousov 			sbuf_delete(sname);
1975f49915eSGarance A Drosehn 		return (E2BIG);
198eaad1099SKonstantin Belousov 	}
1995f49915eSGarance A Drosehn 
2005f49915eSGarance A Drosehn 	bcopy(imgp->args->begin_argv + length, imgp->args->begin_argv + offset,
2015f49915eSGarance A Drosehn 	    imgp->args->endp - (imgp->args->begin_argv + length));
2025f49915eSGarance A Drosehn 
2035f49915eSGarance A Drosehn 	offset -= length;		/* calculate actual adjustment */
2045f49915eSGarance A Drosehn 	imgp->args->begin_envv += offset;
2055f49915eSGarance A Drosehn 	imgp->args->endp += offset;
2065f49915eSGarance A Drosehn 	imgp->args->stringspace -= offset;
2075f49915eSGarance A Drosehn 
2085f49915eSGarance A Drosehn 	/*
2095f49915eSGarance A Drosehn 	 * If there was no arg[0] when we started, then the interpreter_name
2105f49915eSGarance A Drosehn 	 * is adding an argument (instead of replacing the arg[0] we started
2115f49915eSGarance A Drosehn 	 * with).  And we're always adding an argument when we include the
2125f49915eSGarance A Drosehn 	 * full pathname of the original script.
2135f49915eSGarance A Drosehn 	 */
2145f49915eSGarance A Drosehn 	if (imgp->args->argc == 0)
2155f49915eSGarance A Drosehn 		imgp->args->argc = 1;
2165f49915eSGarance A Drosehn 	imgp->args->argc++;
2175f49915eSGarance A Drosehn 
2185f49915eSGarance A Drosehn 	/*
2195f49915eSGarance A Drosehn 	 * The original arg[] list has been shifted appropriately.  Copy in
2205f49915eSGarance A Drosehn 	 * the interpreter name and options-string.
2215f49915eSGarance A Drosehn 	 */
2225f49915eSGarance A Drosehn 	length = interpe - interpb;
2239e4e5114SAlan Cox 	bcopy(interpb, imgp->args->begin_argv, length);
2249e4e5114SAlan Cox 	*(imgp->args->begin_argv + length) = '\0';
2255f49915eSGarance A Drosehn 	offset = length + 1;
226bd3aace7SGarance A Drosehn 	if (opte > optb) {
2275f49915eSGarance A Drosehn 		length = opte - optb;
2289e4e5114SAlan Cox 		bcopy(optb, imgp->args->begin_argv + offset, length);
2299e4e5114SAlan Cox 		*(imgp->args->begin_argv + offset + length) = '\0';
2305f49915eSGarance A Drosehn 		offset += length + 1;
2315f49915eSGarance A Drosehn 		imgp->args->argc++;
2325f49915eSGarance A Drosehn 	}
2335f49915eSGarance A Drosehn 
2345f49915eSGarance A Drosehn 	/*
235610ecfe0SMaxim Sobolev 	 * Finally, add the filename onto the end for the interpreter to
236610ecfe0SMaxim Sobolev 	 * use and copy the interpreter's name to imgp->interpreter_name
237610ecfe0SMaxim Sobolev 	 * for exec to use.
238610ecfe0SMaxim Sobolev 	 */
2399e4e5114SAlan Cox 	error = copystr(fname, imgp->args->begin_argv + offset,
2409e4e5114SAlan Cox 	    imgp->args->stringspace, NULL);
241cfefd687SGarrett Wollman 
242610ecfe0SMaxim Sobolev 	if (error == 0)
243*2af6e14dSAlan Cox 		imgp->interpreter_name = imgp->args->begin_argv;
244610ecfe0SMaxim Sobolev 
245eaad1099SKonstantin Belousov 	if (sname != NULL)
246eaad1099SKonstantin Belousov 		sbuf_delete(sname);
247610ecfe0SMaxim Sobolev 	return (error);
248cfefd687SGarrett Wollman }
24992d91f76SGarrett Wollman 
25092d91f76SGarrett Wollman /*
25192d91f76SGarrett Wollman  * Tell kern_execve.c about it, with a little help from the linker.
25292d91f76SGarrett Wollman  */
253820ca326SMatthew Dillon static struct execsw shell_execsw = { exec_shell_imgact, "#!" };
254aa855a59SPeter Wemm EXEC_SET(shell, shell_execsw);
255