10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*2712Snn35248 * Common Development and Distribution License (the "License"). 6*2712Snn35248 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 221335Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 270Sstevel@tonic-gate /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate 300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* from S5R4 1.6 */ 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <sys/param.h> 340Sstevel@tonic-gate #include <sys/sysmacros.h> 350Sstevel@tonic-gate #include <sys/signal.h> 360Sstevel@tonic-gate #include <sys/cred.h> 370Sstevel@tonic-gate #include <sys/user.h> 380Sstevel@tonic-gate #include <sys/errno.h> 390Sstevel@tonic-gate #include <sys/vnode.h> 400Sstevel@tonic-gate #include <sys/proc.h> 410Sstevel@tonic-gate #include <sys/cmn_err.h> 420Sstevel@tonic-gate #include <sys/debug.h> 430Sstevel@tonic-gate #include <sys/pathname.h> 440Sstevel@tonic-gate #include <sys/disp.h> 450Sstevel@tonic-gate #include <sys/exec.h> 460Sstevel@tonic-gate #include <sys/kmem.h> 47*2712Snn35248 #include <sys/note.h> 480Sstevel@tonic-gate 490Sstevel@tonic-gate /* 500Sstevel@tonic-gate * This is the loadable module wrapper. 510Sstevel@tonic-gate */ 520Sstevel@tonic-gate #include <sys/modctl.h> 530Sstevel@tonic-gate 540Sstevel@tonic-gate extern int intpexec(); 550Sstevel@tonic-gate 560Sstevel@tonic-gate static struct execsw esw = { 570Sstevel@tonic-gate intpmagicstr, 580Sstevel@tonic-gate 0, 590Sstevel@tonic-gate 2, 600Sstevel@tonic-gate intpexec, 610Sstevel@tonic-gate NULL 620Sstevel@tonic-gate }; 630Sstevel@tonic-gate 640Sstevel@tonic-gate /* 650Sstevel@tonic-gate * Module linkage information for the kernel. 660Sstevel@tonic-gate */ 670Sstevel@tonic-gate extern struct mod_ops mod_execops; 680Sstevel@tonic-gate 690Sstevel@tonic-gate static struct modlexec modlexec = { 700Sstevel@tonic-gate &mod_execops, "exec mod for interp", &esw 710Sstevel@tonic-gate }; 720Sstevel@tonic-gate 730Sstevel@tonic-gate static struct modlinkage modlinkage = { 740Sstevel@tonic-gate MODREV_1, (void *)&modlexec, NULL 750Sstevel@tonic-gate }; 760Sstevel@tonic-gate 770Sstevel@tonic-gate int 780Sstevel@tonic-gate _init() 790Sstevel@tonic-gate { 800Sstevel@tonic-gate return (mod_install(&modlinkage)); 810Sstevel@tonic-gate } 820Sstevel@tonic-gate 830Sstevel@tonic-gate int 840Sstevel@tonic-gate _fini() 850Sstevel@tonic-gate { 860Sstevel@tonic-gate return (mod_remove(&modlinkage)); 870Sstevel@tonic-gate } 880Sstevel@tonic-gate 890Sstevel@tonic-gate int 900Sstevel@tonic-gate _info(struct modinfo *modinfop) 910Sstevel@tonic-gate { 920Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 930Sstevel@tonic-gate } 940Sstevel@tonic-gate 950Sstevel@tonic-gate 960Sstevel@tonic-gate /* 970Sstevel@tonic-gate * Crack open a '#!' line. 980Sstevel@tonic-gate */ 990Sstevel@tonic-gate static int 1000Sstevel@tonic-gate getintphead(struct vnode *vp, struct intpdata *idatap) 1010Sstevel@tonic-gate { 1020Sstevel@tonic-gate int error; 1030Sstevel@tonic-gate char *cp, *linep = idatap->intp; 1040Sstevel@tonic-gate ssize_t resid; 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate /* 1070Sstevel@tonic-gate * Read the entire line and confirm that it starts with '#!'. 1080Sstevel@tonic-gate */ 1090Sstevel@tonic-gate if (error = vn_rdwr(UIO_READ, vp, linep, INTPSZ, (offset_t)0, 1100Sstevel@tonic-gate UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid)) 1110Sstevel@tonic-gate return (error); 1120Sstevel@tonic-gate if (resid > INTPSZ-2 || linep[0] != '#' || linep[1] != '!') 1130Sstevel@tonic-gate return (ENOEXEC); 1140Sstevel@tonic-gate /* 1150Sstevel@tonic-gate * Blank all white space and find the newline. 1160Sstevel@tonic-gate */ 1170Sstevel@tonic-gate for (cp = &linep[2]; cp < &linep[INTPSZ] && *cp != '\n'; cp++) 1180Sstevel@tonic-gate if (*cp == '\t') 1190Sstevel@tonic-gate *cp = ' '; 1200Sstevel@tonic-gate if (cp >= &linep[INTPSZ]) 1210Sstevel@tonic-gate return (ENOEXEC); 1220Sstevel@tonic-gate ASSERT(*cp == '\n'); 1230Sstevel@tonic-gate *cp = '\0'; 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate /* 1260Sstevel@tonic-gate * Locate the beginning and end of the interpreter name. 1270Sstevel@tonic-gate * In addition to the name, one additional argument may 1280Sstevel@tonic-gate * optionally be included here, to be prepended to the 1290Sstevel@tonic-gate * arguments provided on the command line. Thus, for 1300Sstevel@tonic-gate * example, you can say 1310Sstevel@tonic-gate * 1320Sstevel@tonic-gate * #! /usr/bin/awk -f 1330Sstevel@tonic-gate */ 1340Sstevel@tonic-gate for (cp = &linep[2]; *cp == ' '; cp++) 1350Sstevel@tonic-gate ; 1360Sstevel@tonic-gate if (*cp == '\0') 1370Sstevel@tonic-gate return (ENOEXEC); 1380Sstevel@tonic-gate idatap->intp_name = cp; 1390Sstevel@tonic-gate while (*cp && *cp != ' ') 1400Sstevel@tonic-gate cp++; 1410Sstevel@tonic-gate if (*cp == '\0') 1420Sstevel@tonic-gate idatap->intp_arg = NULL; 1430Sstevel@tonic-gate else { 1440Sstevel@tonic-gate *cp++ = '\0'; 1450Sstevel@tonic-gate while (*cp == ' ') 1460Sstevel@tonic-gate cp++; 1470Sstevel@tonic-gate if (*cp == '\0') 1480Sstevel@tonic-gate idatap->intp_arg = NULL; 1490Sstevel@tonic-gate else { 1500Sstevel@tonic-gate idatap->intp_arg = cp; 1510Sstevel@tonic-gate while (*cp && *cp != ' ') 1520Sstevel@tonic-gate cp++; 1530Sstevel@tonic-gate *cp = '\0'; 1540Sstevel@tonic-gate } 1550Sstevel@tonic-gate } 1560Sstevel@tonic-gate return (0); 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate int 1600Sstevel@tonic-gate intpexec( 1610Sstevel@tonic-gate struct vnode *vp, 1620Sstevel@tonic-gate struct execa *uap, 1630Sstevel@tonic-gate struct uarg *args, 1640Sstevel@tonic-gate struct intpdata *idatap, 1650Sstevel@tonic-gate int level, 1660Sstevel@tonic-gate long *execsz, 1670Sstevel@tonic-gate int setid, 1680Sstevel@tonic-gate caddr_t exec_file, 169*2712Snn35248 struct cred *cred, 170*2712Snn35248 int brand_action) 1710Sstevel@tonic-gate { 172*2712Snn35248 _NOTE(ARGUNUSED(brand_action)) 1730Sstevel@tonic-gate vnode_t *nvp; 1740Sstevel@tonic-gate int error = 0; 1750Sstevel@tonic-gate struct intpdata idata; 1760Sstevel@tonic-gate struct pathname intppn; 1770Sstevel@tonic-gate struct pathname resolvepn; 1780Sstevel@tonic-gate char *opath; 1790Sstevel@tonic-gate char devfd[14]; 1800Sstevel@tonic-gate int fd = -1; 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate if (level) { /* Can't recurse */ 1830Sstevel@tonic-gate error = ENOEXEC; 1840Sstevel@tonic-gate goto bad; 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate ASSERT(idatap == (struct intpdata *)NULL); 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate /* 1900Sstevel@tonic-gate * Allocate a buffer to read in the interpreter pathname. 1910Sstevel@tonic-gate */ 1920Sstevel@tonic-gate idata.intp = kmem_alloc(INTPSZ, KM_SLEEP); 1930Sstevel@tonic-gate if (error = getintphead(vp, &idata)) 1940Sstevel@tonic-gate goto fail; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate /* 1970Sstevel@tonic-gate * Look the new vnode up. 1980Sstevel@tonic-gate */ 1990Sstevel@tonic-gate if (error = pn_get(idata.intp_name, UIO_SYSSPACE, &intppn)) 2000Sstevel@tonic-gate goto fail; 2010Sstevel@tonic-gate pn_alloc(&resolvepn); 2020Sstevel@tonic-gate if (error = lookuppn(&intppn, &resolvepn, FOLLOW, NULLVPP, &nvp)) { 2030Sstevel@tonic-gate pn_free(&resolvepn); 2040Sstevel@tonic-gate pn_free(&intppn); 2050Sstevel@tonic-gate goto fail; 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate opath = args->pathname; 2080Sstevel@tonic-gate args->pathname = resolvepn.pn_path; 2090Sstevel@tonic-gate /* don't free resolvepn until we are done with args */ 2100Sstevel@tonic-gate pn_free(&intppn); 2110Sstevel@tonic-gate 2121335Scasper /* 2131335Scasper * When we're executing a set-uid script resulting in uids 2141335Scasper * mismatching or when we execute with additional privileges, 2151335Scasper * we close the "replace script between exec and open by shell" 2161335Scasper * hole by passing the script as /dev/fd parameter. 2171335Scasper */ 2181335Scasper if ((setid & EXECSETID_PRIVS) != 0 || 2191335Scasper (setid & (EXECSETID_UGIDS|EXECSETID_SETID)) == 2201335Scasper (EXECSETID_UGIDS|EXECSETID_SETID)) { 2210Sstevel@tonic-gate (void) strcpy(devfd, "/dev/fd/"); 2220Sstevel@tonic-gate if (error = execopen(&vp, &fd)) 2230Sstevel@tonic-gate goto done; 2240Sstevel@tonic-gate numtos(fd, &devfd[8]); 2250Sstevel@tonic-gate args->fname = devfd; 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate 228*2712Snn35248 error = gexec(&nvp, uap, args, &idata, ++level, execsz, exec_file, cred, 229*2712Snn35248 EBA_NONE); 2300Sstevel@tonic-gate done: 2310Sstevel@tonic-gate VN_RELE(nvp); 2320Sstevel@tonic-gate args->pathname = opath; 2330Sstevel@tonic-gate pn_free(&resolvepn); 2340Sstevel@tonic-gate fail: 2350Sstevel@tonic-gate kmem_free(idata.intp, INTPSZ); 2360Sstevel@tonic-gate if (error && fd != -1) 2370Sstevel@tonic-gate (void) execclose(fd); 2380Sstevel@tonic-gate bad: 2390Sstevel@tonic-gate return (error); 2400Sstevel@tonic-gate } 241