xref: /freebsd/sys/kern/imgact_shell.c (revision 8a36da99deb0e19363ec04e4d3facd869c1028f5)
19454b2d8SWarner Losh /*-
2*8a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*8a36da99SPedro F. Giffuni  *
4cfefd687SGarrett Wollman  * Copyright (c) 1993, David Greenman
5cfefd687SGarrett Wollman  * All rights reserved.
6cfefd687SGarrett Wollman  *
7cfefd687SGarrett Wollman  * Redistribution and use in source and binary forms, with or without
8cfefd687SGarrett Wollman  * modification, are permitted provided that the following conditions
9cfefd687SGarrett Wollman  * are met:
10cfefd687SGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
11cfefd687SGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
12cfefd687SGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
13cfefd687SGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
14cfefd687SGarrett Wollman  *    documentation and/or other materials provided with the distribution.
15cfefd687SGarrett Wollman  *
16cfefd687SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17cfefd687SGarrett Wollman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18cfefd687SGarrett Wollman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191984b014SDavid Greenman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20cfefd687SGarrett Wollman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21cfefd687SGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22cfefd687SGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23cfefd687SGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24cfefd687SGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25cfefd687SGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26cfefd687SGarrett Wollman  * SUCH DAMAGE.
27cfefd687SGarrett Wollman  */
28cfefd687SGarrett Wollman 
29677b542eSDavid E. O'Brien #include <sys/cdefs.h>
30677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
31677b542eSDavid E. O'Brien 
32f540b106SGarrett Wollman #include <sys/param.h>
336916a1daSMaxim Sobolev #include <sys/vnode.h>
346916a1daSMaxim Sobolev #include <sys/proc.h>
35eaad1099SKonstantin Belousov #include <sys/sbuf.h>
36aa855a59SPeter Wemm #include <sys/systm.h>
37ad7507e2SSteven Wallace #include <sys/sysproto.h>
3826f9a767SRodney W. Grimes #include <sys/exec.h>
39f540b106SGarrett Wollman #include <sys/imgact.h>
40f540b106SGarrett Wollman #include <sys/kernel.h>
41cfefd687SGarrett Wollman 
4292d91f76SGarrett Wollman #if BYTE_ORDER == LITTLE_ENDIAN
43cfefd687SGarrett Wollman #define SHELLMAGIC	0x2123 /* #! */
4492d91f76SGarrett Wollman #else
4592d91f76SGarrett Wollman #define SHELLMAGIC	0x2321
4692d91f76SGarrett Wollman #endif
4792d91f76SGarrett Wollman 
48cfefd687SGarrett Wollman /*
495f49915eSGarance A Drosehn  * At the time of this writing, MAXSHELLCMDLEN == PAGE_SIZE.  This is
505f49915eSGarance A Drosehn  * significant because the caller has only mapped in one page of the
518f7f5a7fSAlan Cox  * file we're reading.
525f49915eSGarance A Drosehn  */
535f49915eSGarance A Drosehn #if MAXSHELLCMDLEN > PAGE_SIZE
545f49915eSGarance A Drosehn #error "MAXSHELLCMDLEN is larger than a single page!"
555f49915eSGarance A Drosehn #endif
565f49915eSGarance A Drosehn 
578f7f5a7fSAlan Cox /*
588f7f5a7fSAlan Cox  * MAXSHELLCMDLEN must be at least MAXINTERP plus the size of the `#!'
598f7f5a7fSAlan Cox  * prefix and terminating newline.
608f7f5a7fSAlan Cox  */
618f7f5a7fSAlan Cox CTASSERT(MAXSHELLCMDLEN >= MAXINTERP + 3);
628f7f5a7fSAlan Cox 
635f49915eSGarance A Drosehn /**
645f49915eSGarance A Drosehn  * Shell interpreter image activator. An interpreter name beginning at
655f49915eSGarance A Drosehn  * imgp->args->begin_argv is the minimal successful exit requirement.
665f49915eSGarance A Drosehn  *
675f49915eSGarance A Drosehn  * If the given file is a shell-script, then the first line will start
685f49915eSGarance A Drosehn  * with the two characters `#!' (aka SHELLMAGIC), followed by the name
695f49915eSGarance A Drosehn  * of the shell-interpreter to run, followed by zero or more tokens.
705f49915eSGarance A Drosehn  *
715f49915eSGarance A Drosehn  * The interpreter is then started up such that it will see:
725f49915eSGarance A Drosehn  *    arg[0] -> The name of interpreter as specified after `#!' in the
735f49915eSGarance A Drosehn  *		first line of the script.  The interpreter name must
745f49915eSGarance A Drosehn  *		not be longer than MAXSHELLCMDLEN bytes.
755f49915eSGarance A Drosehn  *    arg[1] -> *If* there are any additional tokens on the first line,
765f49915eSGarance A Drosehn  *		then we add a new arg[1], which is a copy of the rest of
775f49915eSGarance A Drosehn  *		that line.  The copy starts at the first token after the
785f49915eSGarance A Drosehn  *		interpreter name.  We leave it to the interpreter to
795f49915eSGarance A Drosehn  *		parse the tokens in that value.
805f49915eSGarance A Drosehn  *    arg[x] -> the full pathname of the script.  This will either be
815f49915eSGarance A Drosehn  *		arg[2] or arg[1], depending on whether or not tokens
825f49915eSGarance A Drosehn  *		were found after the interpreter name.
835f49915eSGarance A Drosehn  *  arg[x+1] -> all the arguments that were specified on the original
845f49915eSGarance A Drosehn  *		command line.
855f49915eSGarance A Drosehn  *
865f49915eSGarance A Drosehn  * This processing is described in the execve(2) man page.
875f49915eSGarance A Drosehn  */
885f49915eSGarance A Drosehn 
895f49915eSGarance A Drosehn /*
905f49915eSGarance A Drosehn  * HISTORICAL NOTE: From 1993 to mid-2005, FreeBSD parsed out the tokens as
915f49915eSGarance A Drosehn  * found on the first line of the script, and setup each token as a separate
925f49915eSGarance A Drosehn  * value in arg[].  This extra processing did not match the behavior of other
935f49915eSGarance A Drosehn  * OS's, and caused a few subtle problems.  For one, it meant the kernel was
945f49915eSGarance A Drosehn  * deciding how those values should be parsed (wrt characters for quoting or
955f49915eSGarance A Drosehn  * comments, etc), while the interpreter might have other rules for parsing.
965f49915eSGarance A Drosehn  * It also meant the interpreter had no way of knowing which arguments came
975f49915eSGarance A Drosehn  * from the first line of the shell script, and which arguments were specified
98386ea932SGarance A Drosehn  * by the user on the command line.  That extra processing was dropped in the
99386ea932SGarance A Drosehn  * 6.x branch on May 28, 2005 (matching __FreeBSD_version 600029).
100cfefd687SGarrett Wollman  */
101d323ddf3SMatthew Dillon int
1027aa47273SPedro F. Giffuni exec_shell_imgact(struct image_params *imgp)
103cfefd687SGarrett Wollman {
104c52007c2SDavid Greenman 	const char *image_header = imgp->image_header;
105eaad1099SKonstantin Belousov 	const char *ihp, *interpb, *interpe, *maxp, *optb, *opte, *fname;
106ec217396SMaxim Sobolev 	int error, offset;
1078f7f5a7fSAlan Cox 	size_t length;
1086916a1daSMaxim Sobolev 	struct vattr vattr;
109eaad1099SKonstantin Belousov 	struct sbuf *sname;
110cfefd687SGarrett Wollman 
111cfefd687SGarrett Wollman 	/* a shell script? */
112e0c95ed9SBruce Evans 	if (((const short *)image_header)[0] != SHELLMAGIC)
113cfefd687SGarrett Wollman 		return (-1);
114cfefd687SGarrett Wollman 
115cfefd687SGarrett Wollman 	/*
116cfefd687SGarrett Wollman 	 * Don't allow a shell script to be the shell for a shell
117cfefd687SGarrett Wollman 	 *	script. :-)
118cfefd687SGarrett Wollman 	 */
11965f20a89SSean Bruno 	if (imgp->interpreted & IMGACT_SHELL)
120cfefd687SGarrett Wollman 		return (ENOEXEC);
121cfefd687SGarrett Wollman 
12265f20a89SSean Bruno 	imgp->interpreted |= IMGACT_SHELL;
123cfefd687SGarrett Wollman 
124cfefd687SGarrett Wollman 	/*
1256916a1daSMaxim Sobolev 	 * At this point we have the first page of the file mapped.
1266916a1daSMaxim Sobolev 	 * However, we don't know how far into the page the contents are
1276916a1daSMaxim Sobolev 	 * valid -- the actual file might be much shorter than the page.
1286916a1daSMaxim Sobolev 	 * So find out the file size.
1296916a1daSMaxim Sobolev 	 */
1300359a12eSAttilio Rao 	error = VOP_GETATTR(imgp->vp, &vattr, imgp->proc->p_ucred);
1316916a1daSMaxim Sobolev 	if (error)
1326916a1daSMaxim Sobolev 		return (error);
1336916a1daSMaxim Sobolev 
1345f49915eSGarance A Drosehn 	/*
1355f49915eSGarance A Drosehn 	 * Copy shell name and arguments from image_header into a string
1368f7f5a7fSAlan Cox 	 * buffer.
1375f49915eSGarance A Drosehn 	 */
1388f7f5a7fSAlan Cox 	maxp = &image_header[MIN(vattr.va_size, MAXSHELLCMDLEN)];
1395f49915eSGarance A Drosehn 	ihp = &image_header[2];
1405f49915eSGarance A Drosehn 
1416916a1daSMaxim Sobolev 	/*
1425f49915eSGarance A Drosehn 	 * Find the beginning and end of the interpreter_name.  If the
1435f49915eSGarance A Drosehn 	 * line does not include any interpreter, or if the name which
1445f49915eSGarance A Drosehn 	 * was found is too long, we bail out.
1455f49915eSGarance A Drosehn 	 */
1465f49915eSGarance A Drosehn 	while (ihp < maxp && ((*ihp == ' ') || (*ihp == '\t')))
1475f49915eSGarance A Drosehn 		ihp++;
1485f49915eSGarance A Drosehn 	interpb = ihp;
1495f49915eSGarance A Drosehn 	while (ihp < maxp && ((*ihp != ' ') && (*ihp != '\t') && (*ihp != '\n')
1505f49915eSGarance A Drosehn 	    && (*ihp != '\0')))
1515f49915eSGarance A Drosehn 		ihp++;
1525f49915eSGarance A Drosehn 	interpe = ihp;
1535f49915eSGarance A Drosehn 	if (interpb == interpe)
1545f49915eSGarance A Drosehn 		return (ENOEXEC);
1558f7f5a7fSAlan Cox 	if (interpe - interpb >= MAXINTERP)
1565f49915eSGarance A Drosehn 		return (ENAMETOOLONG);
157cfefd687SGarrett Wollman 
158610ecfe0SMaxim Sobolev 	/*
1595f49915eSGarance A Drosehn 	 * Find the beginning of the options (if any), and the end-of-line.
1605f49915eSGarance A Drosehn 	 * Then trim the trailing blanks off the value.  Note that some
1615f49915eSGarance A Drosehn 	 * other operating systems do *not* trim the trailing whitespace...
1625f49915eSGarance A Drosehn 	 */
1635f49915eSGarance A Drosehn 	while (ihp < maxp && ((*ihp == ' ') || (*ihp == '\t')))
1645f49915eSGarance A Drosehn 		ihp++;
1655f49915eSGarance A Drosehn 	optb = ihp;
1665f49915eSGarance A Drosehn 	while (ihp < maxp && ((*ihp != '\n') && (*ihp != '\0')))
1675f49915eSGarance A Drosehn 		ihp++;
1685f49915eSGarance A Drosehn 	opte = ihp;
1698f7f5a7fSAlan Cox 	if (opte == maxp)
1708f7f5a7fSAlan Cox 		return (ENOEXEC);
171bd3aace7SGarance A Drosehn 	while (--ihp > optb && ((*ihp == ' ') || (*ihp == '\t')))
1725f49915eSGarance A Drosehn 		opte = ihp;
1735f49915eSGarance A Drosehn 
174eaad1099SKonstantin Belousov 	if (imgp->args->fname != NULL) {
175eaad1099SKonstantin Belousov 		fname = imgp->args->fname;
176eaad1099SKonstantin Belousov 		sname = NULL;
177eaad1099SKonstantin Belousov 	} else {
178eaad1099SKonstantin Belousov 		sname = sbuf_new_auto();
179eaad1099SKonstantin Belousov 		sbuf_printf(sname, "/dev/fd/%d", imgp->args->fd);
180eaad1099SKonstantin Belousov 		sbuf_finish(sname);
181eaad1099SKonstantin Belousov 		fname = sbuf_data(sname);
182eaad1099SKonstantin Belousov 	}
183eaad1099SKonstantin Belousov 
1845f49915eSGarance A Drosehn 	/*
1855f49915eSGarance A Drosehn 	 * We need to "pop" (remove) the present value of arg[0], and "push"
1865f49915eSGarance A Drosehn 	 * either two or three new values in the arg[] list.  To do this,
1875f49915eSGarance A Drosehn 	 * we first shift all the other values in the `begin_argv' area to
1885f49915eSGarance A Drosehn 	 * provide the exact amount of room for the values added.  Set up
1895f49915eSGarance A Drosehn 	 * `offset' as the number of bytes to be added to the `begin_argv'
1905f49915eSGarance A Drosehn 	 * area, and 'length' as the number of bytes being removed.
1915f49915eSGarance A Drosehn 	 */
1925f49915eSGarance A Drosehn 	offset = interpe - interpb + 1;			/* interpreter */
193bd3aace7SGarance A Drosehn 	if (opte > optb)				/* options (if any) */
1945f49915eSGarance A Drosehn 		offset += opte - optb + 1;
195eaad1099SKonstantin Belousov 	offset += strlen(fname) + 1;			/* fname of script */
1965f49915eSGarance A Drosehn 	length = (imgp->args->argc == 0) ? 0 :
1975f49915eSGarance A Drosehn 	    strlen(imgp->args->begin_argv) + 1;		/* bytes to delete */
1985f49915eSGarance A Drosehn 
1990ad4dd9aSKonstantin Belousov 	if (offset > imgp->args->stringspace + length) {
200eaad1099SKonstantin Belousov 		if (sname != NULL)
201eaad1099SKonstantin Belousov 			sbuf_delete(sname);
2025f49915eSGarance A Drosehn 		return (E2BIG);
203eaad1099SKonstantin Belousov 	}
2045f49915eSGarance A Drosehn 
2055f49915eSGarance A Drosehn 	bcopy(imgp->args->begin_argv + length, imgp->args->begin_argv + offset,
2065f49915eSGarance A Drosehn 	    imgp->args->endp - (imgp->args->begin_argv + length));
2075f49915eSGarance A Drosehn 
2085f49915eSGarance A Drosehn 	offset -= length;		/* calculate actual adjustment */
2095f49915eSGarance A Drosehn 	imgp->args->begin_envv += offset;
2105f49915eSGarance A Drosehn 	imgp->args->endp += offset;
2115f49915eSGarance A Drosehn 	imgp->args->stringspace -= offset;
2125f49915eSGarance A Drosehn 
2135f49915eSGarance A Drosehn 	/*
2145f49915eSGarance A Drosehn 	 * If there was no arg[0] when we started, then the interpreter_name
2155f49915eSGarance A Drosehn 	 * is adding an argument (instead of replacing the arg[0] we started
2165f49915eSGarance A Drosehn 	 * with).  And we're always adding an argument when we include the
2175f49915eSGarance A Drosehn 	 * full pathname of the original script.
2185f49915eSGarance A Drosehn 	 */
2195f49915eSGarance A Drosehn 	if (imgp->args->argc == 0)
2205f49915eSGarance A Drosehn 		imgp->args->argc = 1;
2215f49915eSGarance A Drosehn 	imgp->args->argc++;
2225f49915eSGarance A Drosehn 
2235f49915eSGarance A Drosehn 	/*
2245f49915eSGarance A Drosehn 	 * The original arg[] list has been shifted appropriately.  Copy in
2255f49915eSGarance A Drosehn 	 * the interpreter name and options-string.
2265f49915eSGarance A Drosehn 	 */
2275f49915eSGarance A Drosehn 	length = interpe - interpb;
2289e4e5114SAlan Cox 	bcopy(interpb, imgp->args->begin_argv, length);
2299e4e5114SAlan Cox 	*(imgp->args->begin_argv + length) = '\0';
2305f49915eSGarance A Drosehn 	offset = length + 1;
231bd3aace7SGarance A Drosehn 	if (opte > optb) {
2325f49915eSGarance A Drosehn 		length = opte - optb;
2339e4e5114SAlan Cox 		bcopy(optb, imgp->args->begin_argv + offset, length);
2349e4e5114SAlan Cox 		*(imgp->args->begin_argv + offset + length) = '\0';
2355f49915eSGarance A Drosehn 		offset += length + 1;
2365f49915eSGarance A Drosehn 		imgp->args->argc++;
2375f49915eSGarance A Drosehn 	}
2385f49915eSGarance A Drosehn 
2395f49915eSGarance A Drosehn 	/*
240610ecfe0SMaxim Sobolev 	 * Finally, add the filename onto the end for the interpreter to
241610ecfe0SMaxim Sobolev 	 * use and copy the interpreter's name to imgp->interpreter_name
242610ecfe0SMaxim Sobolev 	 * for exec to use.
243610ecfe0SMaxim Sobolev 	 */
2449e4e5114SAlan Cox 	error = copystr(fname, imgp->args->begin_argv + offset,
2459e4e5114SAlan Cox 	    imgp->args->stringspace, NULL);
246cfefd687SGarrett Wollman 
247610ecfe0SMaxim Sobolev 	if (error == 0)
2482af6e14dSAlan Cox 		imgp->interpreter_name = imgp->args->begin_argv;
249610ecfe0SMaxim Sobolev 
250eaad1099SKonstantin Belousov 	if (sname != NULL)
251eaad1099SKonstantin Belousov 		sbuf_delete(sname);
252610ecfe0SMaxim Sobolev 	return (error);
253cfefd687SGarrett Wollman }
25492d91f76SGarrett Wollman 
25592d91f76SGarrett Wollman /*
25692d91f76SGarrett Wollman  * Tell kern_execve.c about it, with a little help from the linker.
25792d91f76SGarrett Wollman  */
258820ca326SMatthew Dillon static struct execsw shell_execsw = { exec_shell_imgact, "#!" };
259aa855a59SPeter Wemm EXEC_SET(shell, shell_execsw);
260