imgact_shell.c revision 210545
1139804Simp/*- 2885Swollman * Copyright (c) 1993, David Greenman 3885Swollman * All rights reserved. 4885Swollman * 5885Swollman * Redistribution and use in source and binary forms, with or without 6885Swollman * modification, are permitted provided that the following conditions 7885Swollman * are met: 8885Swollman * 1. Redistributions of source code must retain the above copyright 9885Swollman * notice, this list of conditions and the following disclaimer. 10885Swollman * 2. Redistributions in binary form must reproduce the above copyright 11885Swollman * notice, this list of conditions and the following disclaimer in the 12885Swollman * documentation and/or other materials provided with the distribution. 13885Swollman * 14885Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15885Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16885Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1710625Sdg * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18885Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19885Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20885Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21885Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22885Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23885Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24885Swollman * SUCH DAMAGE. 25885Swollman */ 26885Swollman 27116182Sobrien#include <sys/cdefs.h> 28116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/imgact_shell.c 210545 2010-07-27 17:31:03Z alc $"); 29116182Sobrien 302056Swollman#include <sys/param.h> 31142448Ssobomax#include <sys/vnode.h> 32142448Ssobomax#include <sys/proc.h> 33182191Skib#include <sys/sbuf.h> 3440435Speter#include <sys/systm.h> 3511332Sswallace#include <sys/sysproto.h> 361549Srgrimes#include <sys/exec.h> 372056Swollman#include <sys/imgact.h> 382056Swollman#include <sys/kernel.h> 39885Swollman 40886Swollman#if BYTE_ORDER == LITTLE_ENDIAN 41885Swollman#define SHELLMAGIC 0x2123 /* #! */ 42886Swollman#else 43886Swollman#define SHELLMAGIC 0x2321 44886Swollman#endif 45886Swollman 46885Swollman/* 47146731Sgad * At the time of this writing, MAXSHELLCMDLEN == PAGE_SIZE. This is 48146731Sgad * significant because the caller has only mapped in one page of the 49146731Sgad * file we're reading. This code should be changed to know how to 50146731Sgad * read in the second page, but I'm not doing that just yet... 51885Swollman */ 52146731Sgad#if MAXSHELLCMDLEN > PAGE_SIZE 53146731Sgad#error "MAXSHELLCMDLEN is larger than a single page!" 54146731Sgad#endif 55146731Sgad 56146731Sgad/** 57146731Sgad * Shell interpreter image activator. An interpreter name beginning at 58146731Sgad * imgp->args->begin_argv is the minimal successful exit requirement. 59146731Sgad * 60146731Sgad * If the given file is a shell-script, then the first line will start 61146731Sgad * with the two characters `#!' (aka SHELLMAGIC), followed by the name 62146731Sgad * of the shell-interpreter to run, followed by zero or more tokens. 63146731Sgad * 64146731Sgad * The interpreter is then started up such that it will see: 65146731Sgad * arg[0] -> The name of interpreter as specified after `#!' in the 66146731Sgad * first line of the script. The interpreter name must 67146731Sgad * not be longer than MAXSHELLCMDLEN bytes. 68146731Sgad * arg[1] -> *If* there are any additional tokens on the first line, 69146731Sgad * then we add a new arg[1], which is a copy of the rest of 70146731Sgad * that line. The copy starts at the first token after the 71146731Sgad * interpreter name. We leave it to the interpreter to 72146731Sgad * parse the tokens in that value. 73146731Sgad * arg[x] -> the full pathname of the script. This will either be 74146731Sgad * arg[2] or arg[1], depending on whether or not tokens 75146731Sgad * were found after the interpreter name. 76146731Sgad * arg[x+1] -> all the arguments that were specified on the original 77146731Sgad * command line. 78146731Sgad * 79146731Sgad * This processing is described in the execve(2) man page. 80146731Sgad */ 81146731Sgad 82146731Sgad/* 83146731Sgad * HISTORICAL NOTE: From 1993 to mid-2005, FreeBSD parsed out the tokens as 84146731Sgad * found on the first line of the script, and setup each token as a separate 85146731Sgad * value in arg[]. This extra processing did not match the behavior of other 86146731Sgad * OS's, and caused a few subtle problems. For one, it meant the kernel was 87146731Sgad * deciding how those values should be parsed (wrt characters for quoting or 88146731Sgad * comments, etc), while the interpreter might have other rules for parsing. 89146731Sgad * It also meant the interpreter had no way of knowing which arguments came 90146731Sgad * from the first line of the shell script, and which arguments were specified 91147151Sgad * by the user on the command line. That extra processing was dropped in the 92147151Sgad * 6.x branch on May 28, 2005 (matching __FreeBSD_version 600029). 93146731Sgad */ 9459663Sdillonint 9512130Sdgexec_shell_imgact(imgp) 9612130Sdg struct image_params *imgp; 97885Swollman{ 9812130Sdg const char *image_header = imgp->image_header; 99182191Skib const char *ihp, *interpb, *interpe, *maxp, *optb, *opte, *fname; 100141028Ssobomax int error, offset; 101142448Ssobomax size_t length, clength; 102142448Ssobomax struct vattr vattr; 103182191Skib struct sbuf *sname; 104885Swollman 105885Swollman /* a shell script? */ 10617974Sbde if (((const short *) image_header)[0] != SHELLMAGIC) 107885Swollman return(-1); 108885Swollman 109885Swollman /* 110885Swollman * Don't allow a shell script to be the shell for a shell 111885Swollman * script. :-) 112885Swollman */ 11312130Sdg if (imgp->interpreted) 114885Swollman return(ENOEXEC); 115885Swollman 11612130Sdg imgp->interpreted = 1; 117885Swollman 118885Swollman /* 119142448Ssobomax * At this point we have the first page of the file mapped. 120142448Ssobomax * However, we don't know how far into the page the contents are 121142448Ssobomax * valid -- the actual file might be much shorter than the page. 122142448Ssobomax * So find out the file size. 123142448Ssobomax */ 124182371Sattilio error = VOP_GETATTR(imgp->vp, &vattr, imgp->proc->p_ucred); 125142448Ssobomax if (error) 126142448Ssobomax return (error); 127142448Ssobomax 128142448Ssobomax /* 129146731Sgad * Copy shell name and arguments from image_header into a string 130146731Sgad * buffer. Remember that the caller has mapped only the 131146731Sgad * first page of the file into memory. 132146731Sgad */ 133146731Sgad clength = (vattr.va_size > PAGE_SIZE) ? PAGE_SIZE : vattr.va_size; 134146731Sgad 135146731Sgad maxp = &image_header[clength]; 136146731Sgad ihp = &image_header[2]; 137146731Sgad 138146731Sgad /* 139146731Sgad * Find the beginning and end of the interpreter_name. If the 140146731Sgad * line does not include any interpreter, or if the name which 141146731Sgad * was found is too long, we bail out. 142146731Sgad */ 143146731Sgad while (ihp < maxp && ((*ihp == ' ') || (*ihp == '\t'))) 144146731Sgad ihp++; 145146731Sgad interpb = ihp; 146146731Sgad while (ihp < maxp && ((*ihp != ' ') && (*ihp != '\t') && (*ihp != '\n') 147146731Sgad && (*ihp != '\0'))) 148146731Sgad ihp++; 149146731Sgad interpe = ihp; 150146731Sgad if (interpb == interpe) 151146731Sgad return (ENOEXEC); 152146731Sgad if ((interpe - interpb) >= MAXSHELLCMDLEN) 153146731Sgad return (ENAMETOOLONG); 154885Swollman 155140992Ssobomax /* 156146731Sgad * Find the beginning of the options (if any), and the end-of-line. 157146731Sgad * Then trim the trailing blanks off the value. Note that some 158146731Sgad * other operating systems do *not* trim the trailing whitespace... 159146731Sgad */ 160146731Sgad while (ihp < maxp && ((*ihp == ' ') || (*ihp == '\t'))) 161146731Sgad ihp++; 162146731Sgad optb = ihp; 163146731Sgad while (ihp < maxp && ((*ihp != '\n') && (*ihp != '\0'))) 164146731Sgad ihp++; 165146731Sgad opte = ihp; 166147479Sgad while (--ihp > optb && ((*ihp == ' ') || (*ihp == '\t'))) 167146731Sgad opte = ihp; 168146731Sgad 169182191Skib if (imgp->args->fname != NULL) { 170182191Skib fname = imgp->args->fname; 171182191Skib sname = NULL; 172182191Skib } else { 173182191Skib sname = sbuf_new_auto(); 174182191Skib sbuf_printf(sname, "/dev/fd/%d", imgp->args->fd); 175182191Skib sbuf_finish(sname); 176182191Skib fname = sbuf_data(sname); 177182191Skib } 178182191Skib 179146731Sgad /* 180146731Sgad * We need to "pop" (remove) the present value of arg[0], and "push" 181146731Sgad * either two or three new values in the arg[] list. To do this, 182146731Sgad * we first shift all the other values in the `begin_argv' area to 183146731Sgad * provide the exact amount of room for the values added. Set up 184146731Sgad * `offset' as the number of bytes to be added to the `begin_argv' 185146731Sgad * area, and 'length' as the number of bytes being removed. 186146731Sgad */ 187146731Sgad offset = interpe - interpb + 1; /* interpreter */ 188147479Sgad if (opte > optb) /* options (if any) */ 189146731Sgad offset += opte - optb + 1; 190182191Skib offset += strlen(fname) + 1; /* fname of script */ 191146731Sgad length = (imgp->args->argc == 0) ? 0 : 192146731Sgad strlen(imgp->args->begin_argv) + 1; /* bytes to delete */ 193146731Sgad 194182191Skib if (offset - length > imgp->args->stringspace) { 195182191Skib if (sname != NULL) 196182191Skib sbuf_delete(sname); 197146731Sgad return (E2BIG); 198182191Skib } 199146731Sgad 200146731Sgad bcopy(imgp->args->begin_argv + length, imgp->args->begin_argv + offset, 201146731Sgad imgp->args->endp - (imgp->args->begin_argv + length)); 202146731Sgad 203146731Sgad offset -= length; /* calculate actual adjustment */ 204146731Sgad imgp->args->begin_envv += offset; 205146731Sgad imgp->args->endp += offset; 206146731Sgad imgp->args->stringspace -= offset; 207146731Sgad 208146731Sgad /* 209146731Sgad * If there was no arg[0] when we started, then the interpreter_name 210146731Sgad * is adding an argument (instead of replacing the arg[0] we started 211146731Sgad * with). And we're always adding an argument when we include the 212146731Sgad * full pathname of the original script. 213146731Sgad */ 214146731Sgad if (imgp->args->argc == 0) 215146731Sgad imgp->args->argc = 1; 216146731Sgad imgp->args->argc++; 217146731Sgad 218146731Sgad /* 219146731Sgad * The original arg[] list has been shifted appropriately. Copy in 220146731Sgad * the interpreter name and options-string. 221146731Sgad */ 222146731Sgad length = interpe - interpb; 223210475Salc bcopy(interpb, imgp->args->begin_argv, length); 224210475Salc *(imgp->args->begin_argv + length) = '\0'; 225146731Sgad offset = length + 1; 226147479Sgad if (opte > optb) { 227146731Sgad length = opte - optb; 228210475Salc bcopy(optb, imgp->args->begin_argv + offset, length); 229210475Salc *(imgp->args->begin_argv + offset + length) = '\0'; 230146731Sgad offset += length + 1; 231146731Sgad imgp->args->argc++; 232146731Sgad } 233146731Sgad 234146731Sgad /* 235140992Ssobomax * Finally, add the filename onto the end for the interpreter to 236140992Ssobomax * use and copy the interpreter's name to imgp->interpreter_name 237140992Ssobomax * for exec to use. 238140992Ssobomax */ 239210475Salc error = copystr(fname, imgp->args->begin_argv + offset, 240210475Salc imgp->args->stringspace, NULL); 241885Swollman 242140992Ssobomax if (error == 0) 243210545Salc imgp->interpreter_name = imgp->args->begin_argv; 244140992Ssobomax 245182191Skib if (sname != NULL) 246182191Skib sbuf_delete(sname); 247140992Ssobomax return (error); 248885Swollman} 249886Swollman 250886Swollman/* 251886Swollman * Tell kern_execve.c about it, with a little help from the linker. 252886Swollman */ 25343402Sdillonstatic struct execsw shell_execsw = { exec_shell_imgact, "#!" }; 25440435SpeterEXEC_SET(shell, shell_execsw); 255