5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2012 Milan Jurik. All rights reserved.
25 */
26
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
29
30
31 /* 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 #include <sys/note.h>
49 #include <sys/sdt.h>
50
51 /*
52 * This is the loadable module wrapper.
53 */
54 #include <sys/modctl.h>
55
56 extern int intpexec(struct vnode *, struct execa *, struct uarg *,
57 struct intpdata *, int, long *, int, caddr_t, struct cred *, int);
58
59 static struct execsw esw = {
60 intpmagicstr,
61 0,
62 2,
63 intpexec,
64 NULL
65 };
66
67 /*
68 * Module linkage information for the kernel.
69 */
70 extern struct mod_ops mod_execops;
71
72 static struct modlexec modlexec = {
73 &mod_execops, "exec mod for interp", &esw
74 };
75
76 static struct modlinkage modlinkage = {
77 MODREV_1, (void *)&modlexec, NULL
109 /*
110 * Read the entire line and confirm that it starts with '#!'.
111 */
112 if (error = vn_rdwr(UIO_READ, vp, linep, INTPSZ, (offset_t)0,
113 UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid))
114 return (error);
115 if (resid > INTPSZ-2 || linep[0] != '#' || linep[1] != '!')
116 return (ENOEXEC);
117 /*
118 * Blank all white space and find the newline.
119 */
120 for (cp = &linep[2]; cp < &linep[INTPSZ] && *cp != '\n'; cp++)
121 if (*cp == '\t')
122 *cp = ' ';
123 if (cp >= &linep[INTPSZ])
124 return (ENOEXEC);
125 ASSERT(*cp == '\n');
126 *cp = '\0';
127
128 /*
129 * Locate the beginning and end of the interpreter name.
130 * In addition to the name, one additional argument may
131 * optionally be included here, to be prepended to the
132 * arguments provided on the command line. Thus, for
133 * example, you can say
134 *
135 * #! /usr/bin/awk -f
136 */
137 for (cp = &linep[2]; *cp == ' '; cp++)
138 ;
139 if (*cp == '\0')
140 return (ENOEXEC);
141 idatap->intp_name[0] = cp;
142 while (*cp && *cp != ' ')
143 cp++;
144 if (*cp == '\0') {
145 idatap->intp_arg[0] = NULL;
146 } else {
147 *cp++ = '\0';
148 while (*cp == ' ')
149 cp++;
150 if (*cp == '\0')
151 idatap->intp_arg[0] = NULL;
152 else {
153 idatap->intp_arg[0] = cp;
154 while (*cp && *cp != ' ')
155 cp++;
156 *cp = '\0';
157 }
158 }
159 return (0);
160 }
161
162 /*
163 * We support nested interpreters up to a depth of INTP_MAXDEPTH (this value
164 * matches the depth on Linux). When a nested interpreter is in use, the
165 * previous name and argument must be passed along. We use the intpdata_t
166 * name and argument arrays for this. In the normal, non-nested case, only the
167 * first element in those arrays will be populated.
168 *
169 * For setid scripts the "script hole" is a security race condition between
170 * when we exec the interpreter and when the interpreter reads the script. We
171 * handle this below for the initial script, but we don't allow setid scripts
172 * when using nested interpreters. Because gexec only modifies the credentials
173 * for a setid script at level 0, then if we come back through for a nested
174 * interpreter we know that args->fname will be set (the first script is setid)
175 * and we can return an error. If an intermediate nested interpreter is setid
176 * then it will not be run with different credentials because of the gexec
177 * handling, so it is effectively no longer setid and we don't have to worry
178 * about the "script hole".
179 */
180 int
181 intpexec(
182 struct vnode *vp,
183 struct execa *uap,
184 struct uarg *args,
185 struct intpdata *idatap,
186 int level,
187 long *execsz,
188 int setid,
189 caddr_t exec_file,
190 struct cred *cred,
191 int brand_action)
192 {
193 _NOTE(ARGUNUSED(brand_action))
194 vnode_t *nvp;
195 int error = 0;
196 struct intpdata idata;
197 struct pathname intppn;
198 struct pathname resolvepn;
199 char *opath;
200 char devfd[19]; /* 32-bit int fits in 10 digits + 8 for "/dev/fd/" */
201 int fd = -1;
202
203 if (level >= INTP_MAXDEPTH) { /* Can't recurse past maxdepth */
204 error = ELOOP;
205 goto bad;
206 }
207
208 if (level == 0)
209 ASSERT(idatap == (struct intpdata *)NULL);
210
211 bzero(&idata, sizeof (intpdata_t));
212
213 /*
264 goto done;
265 }
266
267 /*
268 * When we're executing a set-uid script resulting in uids
269 * mismatching or when we execute with additional privileges,
270 * we close the "replace script between exec and open by shell"
271 * hole by passing the script as /dev/fd parameter.
272 */
273 if ((setid & EXECSETID_PRIVS) != 0 ||
274 (setid & (EXECSETID_UGIDS|EXECSETID_SETID)) ==
275 (EXECSETID_UGIDS|EXECSETID_SETID)) {
276 (void) strcpy(devfd, "/dev/fd/");
277 if (error = execopen(&vp, &fd))
278 goto done;
279 numtos(fd, &devfd[8]);
280 args->fname = devfd;
281 }
282
283 error = gexec(&nvp, uap, args, &idata, ++level, execsz, exec_file, cred,
284 EBA_NONE);
285
286 if (!error) {
287 /*
288 * Close this executable as the interpreter
289 * will open and close it later on.
290 */
291 (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, cred, NULL);
292 }
293 done:
294 VN_RELE(nvp);
295 args->pathname = opath;
296 pn_free(&resolvepn);
297 fail:
298 kmem_free(idata.intp, INTPSZ);
299 if (error && fd != -1)
300 (void) execclose(fd);
301 bad:
302 return (error);
303 }
|
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2012 Milan Jurik. All rights reserved.
25 * Copyright 2016, Joyent, Inc.
26 */
27
28 /* Copyright (c) 1988 AT&T */
29 /* All Rights Reserved */
30
31
32 /* from S5R4 1.6 */
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/sysmacros.h>
37 #include <sys/signal.h>
38 #include <sys/cred.h>
39 #include <sys/user.h>
40 #include <sys/errno.h>
41 #include <sys/vnode.h>
42 #include <sys/proc.h>
43 #include <sys/cmn_err.h>
44 #include <sys/debug.h>
45 #include <sys/pathname.h>
46 #include <sys/disp.h>
47 #include <sys/exec.h>
48 #include <sys/kmem.h>
49 #include <sys/note.h>
50 #include <sys/sdt.h>
51 #include <sys/brand.h>
52
53 /*
54 * This is the loadable module wrapper.
55 */
56 #include <sys/modctl.h>
57
58 extern int intpexec(struct vnode *, struct execa *, struct uarg *,
59 struct intpdata *, int, long *, int, caddr_t, struct cred *, int *);
60
61 static struct execsw esw = {
62 intpmagicstr,
63 0,
64 2,
65 intpexec,
66 NULL
67 };
68
69 /*
70 * Module linkage information for the kernel.
71 */
72 extern struct mod_ops mod_execops;
73
74 static struct modlexec modlexec = {
75 &mod_execops, "exec mod for interp", &esw
76 };
77
78 static struct modlinkage modlinkage = {
79 MODREV_1, (void *)&modlexec, NULL
111 /*
112 * Read the entire line and confirm that it starts with '#!'.
113 */
114 if (error = vn_rdwr(UIO_READ, vp, linep, INTPSZ, (offset_t)0,
115 UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid))
116 return (error);
117 if (resid > INTPSZ-2 || linep[0] != '#' || linep[1] != '!')
118 return (ENOEXEC);
119 /*
120 * Blank all white space and find the newline.
121 */
122 for (cp = &linep[2]; cp < &linep[INTPSZ] && *cp != '\n'; cp++)
123 if (*cp == '\t')
124 *cp = ' ';
125 if (cp >= &linep[INTPSZ])
126 return (ENOEXEC);
127 ASSERT(*cp == '\n');
128 *cp = '\0';
129
130 /*
131 * Locate the beginning and end of the interpreter name. Historically,
132 * for illumos and its predecessors, in addition to the name, one
133 * additional argument may optionally be included here, to be prepended
134 * to the arguments provided on the command line. Thus, for example,
135 * you can say
136 *
137 * #! /usr/bin/awk -f
138 *
139 * However, handling of interpreter arguments varies across operating
140 * systems and other systems allow more than one argument. In
141 * particular, Linux allows more than one and delivers all arguments
142 * as a single string (argv[1] is "-arg1 -arg2 ..."). We support this
143 * style of argument handling as a brand-specific option (setting
144 * b_intp_parse_arg to B_FALSE).
145 */
146 for (cp = &linep[2]; *cp == ' '; cp++)
147 ;
148 if (*cp == '\0')
149 return (ENOEXEC);
150 idatap->intp_name[0] = cp;
151 while (*cp && *cp != ' ')
152 cp++;
153 if (*cp == '\0') {
154 idatap->intp_arg[0] = NULL;
155 } else {
156 *cp++ = '\0';
157 while (*cp == ' ')
158 cp++;
159 if (*cp == '\0')
160 idatap->intp_arg[0] = NULL;
161 else {
162 idatap->intp_arg[0] = cp;
163 if (!PROC_IS_BRANDED(curproc) ||
164 BROP(curproc)->b_intp_parse_arg) {
165 while (*cp && *cp != ' ')
166 cp++;
167 *cp = '\0';
168 }
169 }
170 }
171 return (0);
172 }
173
174 /*
175 * We support nested interpreters up to a depth of INTP_MAXDEPTH (this value
176 * matches the depth on Linux). When a nested interpreter is in use, the
177 * previous name and argument must be passed along. We use the intpdata_t
178 * name and argument arrays for this. In the normal, non-nested case, only the
179 * first element in those arrays will be populated.
180 *
181 * For setid scripts the "script hole" is a security race condition between
182 * when we exec the interpreter and when the interpreter reads the script. We
183 * handle this below for the initial script, but we don't allow setid scripts
184 * when using nested interpreters. Because gexec only modifies the credentials
185 * for a setid script at level 0, then if we come back through for a nested
186 * interpreter we know that args->fname will be set (the first script is setid)
187 * and we can return an error. If an intermediate nested interpreter is setid
188 * then it will not be run with different credentials because of the gexec
189 * handling, so it is effectively no longer setid and we don't have to worry
190 * about the "script hole".
191 */
192 int
193 intpexec(
194 struct vnode *vp,
195 struct execa *uap,
196 struct uarg *args,
197 struct intpdata *idatap,
198 int level,
199 long *execsz,
200 int setid,
201 caddr_t exec_file,
202 struct cred *cred,
203 int *brand_action)
204 {
205 vnode_t *nvp;
206 int error = 0;
207 struct intpdata idata;
208 struct pathname intppn;
209 struct pathname resolvepn;
210 char *opath;
211 char devfd[19]; /* 32-bit int fits in 10 digits + 8 for "/dev/fd/" */
212 int fd = -1;
213
214 if (level >= INTP_MAXDEPTH) { /* Can't recurse past maxdepth */
215 error = ELOOP;
216 goto bad;
217 }
218
219 if (level == 0)
220 ASSERT(idatap == (struct intpdata *)NULL);
221
222 bzero(&idata, sizeof (intpdata_t));
223
224 /*
275 goto done;
276 }
277
278 /*
279 * When we're executing a set-uid script resulting in uids
280 * mismatching or when we execute with additional privileges,
281 * we close the "replace script between exec and open by shell"
282 * hole by passing the script as /dev/fd parameter.
283 */
284 if ((setid & EXECSETID_PRIVS) != 0 ||
285 (setid & (EXECSETID_UGIDS|EXECSETID_SETID)) ==
286 (EXECSETID_UGIDS|EXECSETID_SETID)) {
287 (void) strcpy(devfd, "/dev/fd/");
288 if (error = execopen(&vp, &fd))
289 goto done;
290 numtos(fd, &devfd[8]);
291 args->fname = devfd;
292 }
293
294 error = gexec(&nvp, uap, args, &idata, ++level, execsz, exec_file, cred,
295 brand_action);
296
297 if (!error) {
298 /*
299 * Close this executable as the interpreter
300 * will open and close it later on.
301 */
302 (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, cred, NULL);
303 }
304 done:
305 VN_RELE(nvp);
306 args->pathname = opath;
307 pn_free(&resolvepn);
308 fail:
309 kmem_free(idata.intp, INTPSZ);
310 if (error && fd != -1)
311 (void) execclose(fd);
312 bad:
313 return (error);
314 }
|