1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1988 AT&T */ 28 /* All Rights Reserved */ 29 30 31 #pragma ident "%Z%%M% %I% %E% SMI" /* from S5R4 1.6 */ 32 33 #include <sys/types.h> 34 #include <sys/param.h> 35 #include <sys/sysmacros.h> 36 #include <sys/signal.h> 37 #include <sys/cred.h> 38 #include <sys/user.h> 39 #include <sys/errno.h> 40 #include <sys/vnode.h> 41 #include <sys/proc.h> 42 #include <sys/cmn_err.h> 43 #include <sys/debug.h> 44 #include <sys/pathname.h> 45 #include <sys/disp.h> 46 #include <sys/exec.h> 47 #include <sys/kmem.h> 48 49 /* 50 * This is the loadable module wrapper. 51 */ 52 #include <sys/modctl.h> 53 54 extern int intpexec(); 55 56 static struct execsw esw = { 57 intpmagicstr, 58 0, 59 2, 60 intpexec, 61 NULL 62 }; 63 64 /* 65 * Module linkage information for the kernel. 66 */ 67 extern struct mod_ops mod_execops; 68 69 static struct modlexec modlexec = { 70 &mod_execops, "exec mod for interp", &esw 71 }; 72 73 static struct modlinkage modlinkage = { 74 MODREV_1, (void *)&modlexec, NULL 75 }; 76 77 int 78 _init() 79 { 80 return (mod_install(&modlinkage)); 81 } 82 83 int 84 _fini() 85 { 86 return (mod_remove(&modlinkage)); 87 } 88 89 int 90 _info(struct modinfo *modinfop) 91 { 92 return (mod_info(&modlinkage, modinfop)); 93 } 94 95 96 /* 97 * Crack open a '#!' line. 98 */ 99 static int 100 getintphead(struct vnode *vp, struct intpdata *idatap) 101 { 102 int error; 103 char *cp, *linep = idatap->intp; 104 ssize_t resid; 105 106 /* 107 * Read the entire line and confirm that it starts with '#!'. 108 */ 109 if (error = vn_rdwr(UIO_READ, vp, linep, INTPSZ, (offset_t)0, 110 UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid)) 111 return (error); 112 if (resid > INTPSZ-2 || linep[0] != '#' || linep[1] != '!') 113 return (ENOEXEC); 114 /* 115 * Blank all white space and find the newline. 116 */ 117 for (cp = &linep[2]; cp < &linep[INTPSZ] && *cp != '\n'; cp++) 118 if (*cp == '\t') 119 *cp = ' '; 120 if (cp >= &linep[INTPSZ]) 121 return (ENOEXEC); 122 ASSERT(*cp == '\n'); 123 *cp = '\0'; 124 125 /* 126 * Locate the beginning and end of the interpreter name. 127 * In addition to the name, one additional argument may 128 * optionally be included here, to be prepended to the 129 * arguments provided on the command line. Thus, for 130 * example, you can say 131 * 132 * #! /usr/bin/awk -f 133 */ 134 for (cp = &linep[2]; *cp == ' '; cp++) 135 ; 136 if (*cp == '\0') 137 return (ENOEXEC); 138 idatap->intp_name = cp; 139 while (*cp && *cp != ' ') 140 cp++; 141 if (*cp == '\0') 142 idatap->intp_arg = NULL; 143 else { 144 *cp++ = '\0'; 145 while (*cp == ' ') 146 cp++; 147 if (*cp == '\0') 148 idatap->intp_arg = NULL; 149 else { 150 idatap->intp_arg = cp; 151 while (*cp && *cp != ' ') 152 cp++; 153 *cp = '\0'; 154 } 155 } 156 return (0); 157 } 158 159 int 160 intpexec( 161 struct vnode *vp, 162 struct execa *uap, 163 struct uarg *args, 164 struct intpdata *idatap, 165 int level, 166 long *execsz, 167 int setid, 168 caddr_t exec_file, 169 struct cred *cred) 170 { 171 vnode_t *nvp; 172 int error = 0; 173 struct intpdata idata; 174 struct pathname intppn; 175 struct pathname resolvepn; 176 char *opath; 177 char devfd[14]; 178 int fd = -1; 179 180 if (level) { /* Can't recurse */ 181 error = ENOEXEC; 182 goto bad; 183 } 184 185 ASSERT(idatap == (struct intpdata *)NULL); 186 187 /* 188 * Allocate a buffer to read in the interpreter pathname. 189 */ 190 idata.intp = kmem_alloc(INTPSZ, KM_SLEEP); 191 if (error = getintphead(vp, &idata)) 192 goto fail; 193 194 /* 195 * Look the new vnode up. 196 */ 197 if (error = pn_get(idata.intp_name, UIO_SYSSPACE, &intppn)) 198 goto fail; 199 pn_alloc(&resolvepn); 200 if (error = lookuppn(&intppn, &resolvepn, FOLLOW, NULLVPP, &nvp)) { 201 pn_free(&resolvepn); 202 pn_free(&intppn); 203 goto fail; 204 } 205 opath = args->pathname; 206 args->pathname = resolvepn.pn_path; 207 /* don't free resolvepn until we are done with args */ 208 pn_free(&intppn); 209 210 /* 211 * When we're executing a set-uid script resulting in uids 212 * mismatching or when we execute with additional privileges, 213 * we close the "replace script between exec and open by shell" 214 * hole by passing the script as /dev/fd parameter. 215 */ 216 if ((setid & EXECSETID_PRIVS) != 0 || 217 (setid & (EXECSETID_UGIDS|EXECSETID_SETID)) == 218 (EXECSETID_UGIDS|EXECSETID_SETID)) { 219 (void) strcpy(devfd, "/dev/fd/"); 220 if (error = execopen(&vp, &fd)) 221 goto done; 222 numtos(fd, &devfd[8]); 223 args->fname = devfd; 224 } 225 226 error = gexec(&nvp, uap, args, &idata, ++level, 227 execsz, exec_file, cred); 228 done: 229 VN_RELE(nvp); 230 args->pathname = opath; 231 pn_free(&resolvepn); 232 fail: 233 kmem_free(idata.intp, INTPSZ); 234 if (error && fd != -1) 235 (void) execclose(fd); 236 bad: 237 return (error); 238 } 239