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