1 /* $NetBSD: exec_script.c,v 1.80 2019/09/15 20:21:12 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1993, 1994, 1996 Christopher G. Demetriou 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 19 * derived from this software without specific prior written permission 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 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: exec_script.c,v 1.80 2019/09/15 20:21:12 christos Exp $"); 35 36 #ifdef _KERNEL_OPT 37 #include "opt_script.h" 38 #endif 39 40 #ifdef _KERNEL_OPT 41 #include "opt_script.h" 42 #endif 43 44 #if defined(SETUIDSCRIPTS) && !defined(FDSCRIPTS) 45 #define FDSCRIPTS /* Need this for safe set-id scripts. */ 46 #endif 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/proc.h> 51 #include <sys/kmem.h> 52 #include <sys/vnode.h> 53 #include <sys/namei.h> 54 #include <sys/file.h> 55 #ifdef SETUIDSCRIPTS 56 #include <sys/stat.h> 57 #endif 58 #include <sys/filedesc.h> 59 #include <sys/exec.h> 60 #include <sys/resourcevar.h> 61 #include <sys/module.h> 62 #include <sys/exec_script.h> 63 #include <sys/exec_elf.h> 64 65 MODULE(MODULE_CLASS_EXEC, exec_script, NULL); 66 67 static struct execsw exec_script_execsw = { 68 .es_hdrsz = SCRIPT_HDR_SIZE, 69 .es_makecmds = exec_script_makecmds, 70 .u = { 71 .elf_probe_func = NULL, 72 }, 73 .es_emul = NULL, 74 .es_prio = EXECSW_PRIO_ANY, 75 .es_arglen = 0, 76 .es_copyargs = NULL, 77 .es_setregs = NULL, 78 .es_coredump = NULL, 79 .es_setup_stack = exec_setup_stack, 80 }; 81 82 static int 83 exec_script_modcmd(modcmd_t cmd, void *arg) 84 { 85 86 switch (cmd) { 87 case MODULE_CMD_INIT: 88 return exec_add(&exec_script_execsw, 1); 89 90 case MODULE_CMD_FINI: 91 return exec_remove(&exec_script_execsw, 1); 92 93 case MODULE_CMD_AUTOUNLOAD: 94 /* 95 * We don't want to be autounloaded because our use is 96 * transient: no executables with p_execsw equal to 97 * exec_script_execsw will exist, so FINI will never 98 * return EBUSY. However, the system will run scripts 99 * often. Return EBUSY here to prevent this module from 100 * ping-ponging in and out of the kernel. 101 */ 102 return EBUSY; 103 104 default: 105 return ENOTTY; 106 } 107 } 108 109 /* 110 * exec_script_makecmds(): Check if it's an executable shell script. 111 * 112 * Given a proc pointer and an exec package pointer, see if the referent 113 * of the epp is in shell script. If it is, then set thing up so that 114 * the script can be run. This involves preparing the address space 115 * and arguments for the shell which will run the script. 116 * 117 * This function is ultimately responsible for creating a set of vmcmds 118 * which can be used to build the process's vm space and inserting them 119 * into the exec package. 120 */ 121 int 122 exec_script_makecmds(struct lwp *l, struct exec_package *epp) 123 { 124 int error, hdrlinelen, shellnamelen, shellarglen; 125 char *hdrstr = epp->ep_hdr; 126 char *cp, *shellname, *shellarg; 127 size_t shellargp_len; 128 struct exec_fakearg *shellargp; 129 struct exec_fakearg *tmpsap; 130 struct pathbuf *shell_pathbuf; 131 struct vnode *scriptvp; 132 #ifdef SETUIDSCRIPTS 133 /* Gcc needs those initialized for spurious uninitialized warning */ 134 uid_t script_uid = (uid_t) -1; 135 gid_t script_gid = NOGROUP; 136 u_short script_sbits; 137 #endif 138 139 /* 140 * if the magic isn't that of a shell script, or we've already 141 * done shell script processing for this exec, punt on it. 142 */ 143 if ((epp->ep_flags & EXEC_INDIR) != 0 || 144 epp->ep_hdrvalid < EXEC_SCRIPT_MAGICLEN || 145 strncmp(hdrstr, EXEC_SCRIPT_MAGIC, EXEC_SCRIPT_MAGICLEN)) 146 return ENOEXEC; 147 148 /* 149 * Check that the shell spec is terminated by a newline, and that 150 * it isn't too large. 151 */ 152 hdrlinelen = uimin(epp->ep_hdrvalid, SCRIPT_HDR_SIZE); 153 for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; cp < hdrstr + hdrlinelen; 154 cp++) { 155 if (*cp == '\n') { 156 *cp = '\0'; 157 break; 158 } 159 } 160 if (cp >= hdrstr + hdrlinelen) 161 return ENOEXEC; 162 163 /* strip spaces before the shell name */ 164 for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; *cp == ' ' || *cp == '\t'; 165 cp++) 166 ; 167 if (*cp == '\0') 168 return ENOEXEC; 169 170 shellarg = NULL; 171 shellarglen = 0; 172 173 /* collect the shell name; remember its length for later */ 174 shellname = cp; 175 shellnamelen = 0; 176 for ( /* cp = cp */ ; *cp != '\0' && *cp != ' ' && *cp != '\t'; cp++) 177 shellnamelen++; 178 if (*cp == '\0') 179 goto check_shell; 180 *cp++ = '\0'; 181 182 /* skip spaces before any argument */ 183 for ( /* cp = cp */ ; *cp == ' ' || *cp == '\t'; cp++) 184 ; 185 if (*cp == '\0') 186 goto check_shell; 187 188 /* 189 * collect the shell argument. everything after the shell name 190 * is passed as ONE argument; that's the correct (historical) 191 * behaviour. 192 */ 193 shellarg = cp; 194 for ( /* cp = cp */ ; *cp != '\0'; cp++) 195 shellarglen++; 196 *cp++ = '\0'; 197 198 check_shell: 199 #ifdef SETUIDSCRIPTS 200 /* 201 * MNT_NOSUID has already taken care of by check_exec, 202 * so we don't need to worry about it now or later. We 203 * will need to check PSL_TRACED later, however. 204 */ 205 script_sbits = epp->ep_vap->va_mode & (S_ISUID | S_ISGID); 206 if (script_sbits != 0) { 207 script_uid = epp->ep_vap->va_uid; 208 script_gid = epp->ep_vap->va_gid; 209 } 210 #endif 211 #ifdef FDSCRIPTS 212 /* 213 * if the script isn't readable, or it's set-id, then we've 214 * gotta supply a "/dev/fd/..." for the shell to read. 215 * Note that stupid shells (csh) do the wrong thing, and 216 * close all open fd's when the start. That kills this 217 * method of implementing "safe" set-id and x-only scripts. 218 */ 219 vn_lock(epp->ep_vp, LK_EXCLUSIVE | LK_RETRY); 220 error = VOP_ACCESS(epp->ep_vp, VREAD, l->l_cred); 221 VOP_UNLOCK(epp->ep_vp); 222 if (error == EACCES 223 #ifdef SETUIDSCRIPTS 224 || script_sbits 225 #endif 226 ) { 227 struct file *fp; 228 229 KASSERT(!(epp->ep_flags & EXEC_HASFD)); 230 231 if ((error = fd_allocfile(&fp, &epp->ep_fd)) != 0) { 232 scriptvp = NULL; 233 shellargp = NULL; 234 goto fail; 235 } 236 epp->ep_flags |= EXEC_HASFD; 237 fp->f_type = DTYPE_VNODE; 238 fp->f_ops = &vnops; 239 fp->f_vnode = epp->ep_vp; 240 fp->f_flag = FREAD; 241 fd_affix(curproc, fp, epp->ep_fd); 242 } 243 #endif 244 245 /* set up the fake args list */ 246 shellargp_len = 4 * sizeof(*shellargp); 247 shellargp = kmem_alloc(shellargp_len, KM_SLEEP); 248 tmpsap = shellargp; 249 tmpsap->fa_len = shellnamelen + 1; 250 tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP); 251 strlcpy(tmpsap->fa_arg, shellname, tmpsap->fa_len); 252 tmpsap++; 253 if (shellarg != NULL) { 254 tmpsap->fa_len = shellarglen + 1; 255 tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP); 256 strlcpy(tmpsap->fa_arg, shellarg, tmpsap->fa_len); 257 tmpsap++; 258 } 259 tmpsap->fa_len = MAXPATHLEN; 260 tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP); 261 #ifdef FDSCRIPTS 262 if ((epp->ep_flags & EXEC_HASFD) == 0) { 263 #endif 264 /* normally can't fail, but check for it if diagnostic */ 265 error = copystr(epp->ep_kname, tmpsap->fa_arg, MAXPATHLEN, 266 NULL); 267 KASSERT(error == 0); 268 tmpsap++; 269 #ifdef FDSCRIPTS 270 } else { 271 snprintf(tmpsap->fa_arg, MAXPATHLEN, "/dev/fd/%d", epp->ep_fd); 272 tmpsap++; 273 } 274 #endif 275 tmpsap->fa_arg = NULL; 276 277 /* Save the old vnode so we can clean it up later. */ 278 scriptvp = epp->ep_vp; 279 epp->ep_vp = NULL; 280 281 /* Note that we're trying recursively. */ 282 epp->ep_flags |= EXEC_INDIR; 283 284 /* 285 * mark the header we have as invalid; check_exec will read 286 * the header from the new executable 287 */ 288 epp->ep_hdrvalid = 0; 289 290 /* try loading the interpreter */ 291 if ((error = exec_makepathbuf(l, shellname, UIO_SYSSPACE, 292 &shell_pathbuf, NULL)) == 0) { 293 error = check_exec(l, epp, shell_pathbuf, NULL); 294 pathbuf_destroy(shell_pathbuf); 295 } 296 297 /* note that we've clobbered the header */ 298 epp->ep_flags |= EXEC_DESTR; 299 300 if (error == 0) { 301 /* 302 * It succeeded. Unlock the script and 303 * close it if we aren't using it any more. 304 * Also, set things up so that the fake args 305 * list will be used. 306 */ 307 if ((epp->ep_flags & EXEC_HASFD) == 0) { 308 vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY); 309 VOP_CLOSE(scriptvp, FREAD, l->l_cred); 310 vput(scriptvp); 311 } 312 313 epp->ep_flags |= (EXEC_HASARGL | EXEC_SKIPARG); 314 epp->ep_fa = shellargp; 315 epp->ep_fa_len = shellargp_len; 316 #ifdef SETUIDSCRIPTS 317 /* 318 * set thing up so that set-id scripts will be 319 * handled appropriately. PSL_TRACED will be 320 * checked later when the shell is actually 321 * exec'd. 322 */ 323 epp->ep_vap->va_mode |= script_sbits; 324 if (script_sbits & S_ISUID) 325 epp->ep_vap->va_uid = script_uid; 326 if (script_sbits & S_ISGID) 327 epp->ep_vap->va_gid = script_gid; 328 #endif 329 return (0); 330 } 331 332 #ifdef FDSCRIPTS 333 fail: 334 #endif 335 336 /* kill the opened file descriptor, else close the file */ 337 if (epp->ep_flags & EXEC_HASFD) { 338 epp->ep_flags &= ~EXEC_HASFD; 339 fd_close(epp->ep_fd); 340 } else if (scriptvp) { 341 vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY); 342 VOP_CLOSE(scriptvp, FREAD, l->l_cred); 343 vput(scriptvp); 344 } 345 346 /* free the fake arg list, because we're not returning it */ 347 if ((tmpsap = shellargp) != NULL) { 348 while (tmpsap->fa_arg != NULL) { 349 kmem_free(tmpsap->fa_arg, tmpsap->fa_len); 350 tmpsap++; 351 } 352 kmem_free(shellargp, shellargp_len); 353 } 354 355 /* 356 * free any vmspace-creation commands, 357 * and release their references 358 */ 359 kill_vmcmds(&epp->ep_vmcmds); 360 361 return error; 362 } 363