17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
59acbbeafSnn35248 * Common Development and Distribution License (the "License").
69acbbeafSnn35248 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22a4aeef46SDonghai Qiao * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
24*b97d6ca7SMilan Jurik * Copyright 2012 Milan Jurik. All rights reserved.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate
31a4aeef46SDonghai Qiao /* from S5R4 1.6 */
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
367c478bd9Sstevel@tonic-gate #include <sys/signal.h>
377c478bd9Sstevel@tonic-gate #include <sys/cred.h>
387c478bd9Sstevel@tonic-gate #include <sys/user.h>
397c478bd9Sstevel@tonic-gate #include <sys/errno.h>
407c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
417c478bd9Sstevel@tonic-gate #include <sys/proc.h>
427c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
437c478bd9Sstevel@tonic-gate #include <sys/debug.h>
447c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
457c478bd9Sstevel@tonic-gate #include <sys/disp.h>
467c478bd9Sstevel@tonic-gate #include <sys/exec.h>
477c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
489acbbeafSnn35248 #include <sys/note.h>
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate * This is the loadable module wrapper.
527c478bd9Sstevel@tonic-gate */
537c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
547c478bd9Sstevel@tonic-gate
55*b97d6ca7SMilan Jurik extern int intpexec(struct vnode *, struct execa *, struct uarg *,
56*b97d6ca7SMilan Jurik struct intpdata *, int, long *, int, caddr_t, struct cred *, int);
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate static struct execsw esw = {
597c478bd9Sstevel@tonic-gate intpmagicstr,
607c478bd9Sstevel@tonic-gate 0,
617c478bd9Sstevel@tonic-gate 2,
627c478bd9Sstevel@tonic-gate intpexec,
637c478bd9Sstevel@tonic-gate NULL
647c478bd9Sstevel@tonic-gate };
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate * Module linkage information for the kernel.
687c478bd9Sstevel@tonic-gate */
697c478bd9Sstevel@tonic-gate extern struct mod_ops mod_execops;
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate static struct modlexec modlexec = {
727c478bd9Sstevel@tonic-gate &mod_execops, "exec mod for interp", &esw
737c478bd9Sstevel@tonic-gate };
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
767c478bd9Sstevel@tonic-gate MODREV_1, (void *)&modlexec, NULL
777c478bd9Sstevel@tonic-gate };
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate int
_init()807c478bd9Sstevel@tonic-gate _init()
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage));
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate int
_fini()867c478bd9Sstevel@tonic-gate _fini()
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage));
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)927c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * Crack open a '#!' line.
1007c478bd9Sstevel@tonic-gate */
1017c478bd9Sstevel@tonic-gate static int
getintphead(struct vnode * vp,struct intpdata * idatap)1027c478bd9Sstevel@tonic-gate getintphead(struct vnode *vp, struct intpdata *idatap)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate int error;
1057c478bd9Sstevel@tonic-gate char *cp, *linep = idatap->intp;
1067c478bd9Sstevel@tonic-gate ssize_t resid;
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate * Read the entire line and confirm that it starts with '#!'.
1107c478bd9Sstevel@tonic-gate */
1117c478bd9Sstevel@tonic-gate if (error = vn_rdwr(UIO_READ, vp, linep, INTPSZ, (offset_t)0,
1127c478bd9Sstevel@tonic-gate UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid))
1137c478bd9Sstevel@tonic-gate return (error);
1147c478bd9Sstevel@tonic-gate if (resid > INTPSZ-2 || linep[0] != '#' || linep[1] != '!')
1157c478bd9Sstevel@tonic-gate return (ENOEXEC);
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate * Blank all white space and find the newline.
1187c478bd9Sstevel@tonic-gate */
1197c478bd9Sstevel@tonic-gate for (cp = &linep[2]; cp < &linep[INTPSZ] && *cp != '\n'; cp++)
1207c478bd9Sstevel@tonic-gate if (*cp == '\t')
1217c478bd9Sstevel@tonic-gate *cp = ' ';
1227c478bd9Sstevel@tonic-gate if (cp >= &linep[INTPSZ])
1237c478bd9Sstevel@tonic-gate return (ENOEXEC);
1247c478bd9Sstevel@tonic-gate ASSERT(*cp == '\n');
1257c478bd9Sstevel@tonic-gate *cp = '\0';
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate * Locate the beginning and end of the interpreter name.
1297c478bd9Sstevel@tonic-gate * In addition to the name, one additional argument may
1307c478bd9Sstevel@tonic-gate * optionally be included here, to be prepended to the
1317c478bd9Sstevel@tonic-gate * arguments provided on the command line. Thus, for
1327c478bd9Sstevel@tonic-gate * example, you can say
1337c478bd9Sstevel@tonic-gate *
1347c478bd9Sstevel@tonic-gate * #! /usr/bin/awk -f
1357c478bd9Sstevel@tonic-gate */
1367c478bd9Sstevel@tonic-gate for (cp = &linep[2]; *cp == ' '; cp++)
1377c478bd9Sstevel@tonic-gate ;
1387c478bd9Sstevel@tonic-gate if (*cp == '\0')
1397c478bd9Sstevel@tonic-gate return (ENOEXEC);
1407c478bd9Sstevel@tonic-gate idatap->intp_name = cp;
1417c478bd9Sstevel@tonic-gate while (*cp && *cp != ' ')
1427c478bd9Sstevel@tonic-gate cp++;
1437c478bd9Sstevel@tonic-gate if (*cp == '\0')
1447c478bd9Sstevel@tonic-gate idatap->intp_arg = NULL;
1457c478bd9Sstevel@tonic-gate else {
1467c478bd9Sstevel@tonic-gate *cp++ = '\0';
1477c478bd9Sstevel@tonic-gate while (*cp == ' ')
1487c478bd9Sstevel@tonic-gate cp++;
1497c478bd9Sstevel@tonic-gate if (*cp == '\0')
1507c478bd9Sstevel@tonic-gate idatap->intp_arg = NULL;
1517c478bd9Sstevel@tonic-gate else {
1527c478bd9Sstevel@tonic-gate idatap->intp_arg = cp;
1537c478bd9Sstevel@tonic-gate while (*cp && *cp != ' ')
1547c478bd9Sstevel@tonic-gate cp++;
1557c478bd9Sstevel@tonic-gate *cp = '\0';
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate return (0);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate int
intpexec(struct vnode * vp,struct execa * uap,struct uarg * args,struct intpdata * idatap,int level,long * execsz,int setid,caddr_t exec_file,struct cred * cred,int brand_action)1627c478bd9Sstevel@tonic-gate intpexec(
1637c478bd9Sstevel@tonic-gate struct vnode *vp,
1647c478bd9Sstevel@tonic-gate struct execa *uap,
1657c478bd9Sstevel@tonic-gate struct uarg *args,
1667c478bd9Sstevel@tonic-gate struct intpdata *idatap,
1677c478bd9Sstevel@tonic-gate int level,
1687c478bd9Sstevel@tonic-gate long *execsz,
1697c478bd9Sstevel@tonic-gate int setid,
1707c478bd9Sstevel@tonic-gate caddr_t exec_file,
1719acbbeafSnn35248 struct cred *cred,
1729acbbeafSnn35248 int brand_action)
1737c478bd9Sstevel@tonic-gate {
1749acbbeafSnn35248 _NOTE(ARGUNUSED(brand_action))
1757c478bd9Sstevel@tonic-gate vnode_t *nvp;
1767c478bd9Sstevel@tonic-gate int error = 0;
1777c478bd9Sstevel@tonic-gate struct intpdata idata;
1787c478bd9Sstevel@tonic-gate struct pathname intppn;
1797c478bd9Sstevel@tonic-gate struct pathname resolvepn;
1807c478bd9Sstevel@tonic-gate char *opath;
181380789fcSethindra char devfd[19]; /* 32-bit int fits in 10 digits + 8 for "/dev/fd/" */
1827c478bd9Sstevel@tonic-gate int fd = -1;
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate if (level) { /* Can't recurse */
1857c478bd9Sstevel@tonic-gate error = ENOEXEC;
1867c478bd9Sstevel@tonic-gate goto bad;
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate ASSERT(idatap == (struct intpdata *)NULL);
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate * Allocate a buffer to read in the interpreter pathname.
1937c478bd9Sstevel@tonic-gate */
1947c478bd9Sstevel@tonic-gate idata.intp = kmem_alloc(INTPSZ, KM_SLEEP);
1957c478bd9Sstevel@tonic-gate if (error = getintphead(vp, &idata))
1967c478bd9Sstevel@tonic-gate goto fail;
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate * Look the new vnode up.
2007c478bd9Sstevel@tonic-gate */
2017c478bd9Sstevel@tonic-gate if (error = pn_get(idata.intp_name, UIO_SYSSPACE, &intppn))
2027c478bd9Sstevel@tonic-gate goto fail;
2037c478bd9Sstevel@tonic-gate pn_alloc(&resolvepn);
2047c478bd9Sstevel@tonic-gate if (error = lookuppn(&intppn, &resolvepn, FOLLOW, NULLVPP, &nvp)) {
2057c478bd9Sstevel@tonic-gate pn_free(&resolvepn);
2067c478bd9Sstevel@tonic-gate pn_free(&intppn);
2077c478bd9Sstevel@tonic-gate goto fail;
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate opath = args->pathname;
2107c478bd9Sstevel@tonic-gate args->pathname = resolvepn.pn_path;
2117c478bd9Sstevel@tonic-gate /* don't free resolvepn until we are done with args */
2127c478bd9Sstevel@tonic-gate pn_free(&intppn);
2137c478bd9Sstevel@tonic-gate
214cc4b03b5Scasper /*
215cc4b03b5Scasper * When we're executing a set-uid script resulting in uids
216cc4b03b5Scasper * mismatching or when we execute with additional privileges,
217cc4b03b5Scasper * we close the "replace script between exec and open by shell"
218cc4b03b5Scasper * hole by passing the script as /dev/fd parameter.
219cc4b03b5Scasper */
220cc4b03b5Scasper if ((setid & EXECSETID_PRIVS) != 0 ||
221cc4b03b5Scasper (setid & (EXECSETID_UGIDS|EXECSETID_SETID)) ==
222cc4b03b5Scasper (EXECSETID_UGIDS|EXECSETID_SETID)) {
2237c478bd9Sstevel@tonic-gate (void) strcpy(devfd, "/dev/fd/");
2247c478bd9Sstevel@tonic-gate if (error = execopen(&vp, &fd))
2257c478bd9Sstevel@tonic-gate goto done;
2267c478bd9Sstevel@tonic-gate numtos(fd, &devfd[8]);
2277c478bd9Sstevel@tonic-gate args->fname = devfd;
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate
2309acbbeafSnn35248 error = gexec(&nvp, uap, args, &idata, ++level, execsz, exec_file, cred,
2319acbbeafSnn35248 EBA_NONE);
232a4aeef46SDonghai Qiao
233a4aeef46SDonghai Qiao if (!error) {
234a4aeef46SDonghai Qiao /*
235a4aeef46SDonghai Qiao * Close this executable as the interpreter
236a4aeef46SDonghai Qiao * will open and close it later on.
237a4aeef46SDonghai Qiao */
238a4aeef46SDonghai Qiao (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, cred, NULL);
239a4aeef46SDonghai Qiao }
2407c478bd9Sstevel@tonic-gate done:
2417c478bd9Sstevel@tonic-gate VN_RELE(nvp);
2427c478bd9Sstevel@tonic-gate args->pathname = opath;
2437c478bd9Sstevel@tonic-gate pn_free(&resolvepn);
2447c478bd9Sstevel@tonic-gate fail:
2457c478bd9Sstevel@tonic-gate kmem_free(idata.intp, INTPSZ);
2467c478bd9Sstevel@tonic-gate if (error && fd != -1)
2477c478bd9Sstevel@tonic-gate (void) execclose(fd);
2487c478bd9Sstevel@tonic-gate bad:
2497c478bd9Sstevel@tonic-gate return (error);
2507c478bd9Sstevel@tonic-gate }
251