-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvroot_exec.c
413 lines (371 loc) · 8.95 KB
/
vroot_exec.c
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <spawn.h>
#include <paths.h>
#include <sys/stat.h>
#include <sys/param.h>
#include "common.h"
#include "roothide.h"
#define VROOT_API_NAME(NAME) vroot_##NAME
#define VROOTAT_API_NAME(NAME) vroot_##NAME
int VROOT_API_NAME(stat)(const char *path, struct stat *sb);
int VROOT_API_NAME(execve)(const char * __file, char * const * __argv, char * const * __envp);
int VROOT_API_NAME(posix_spawn)(pid_t * pid, const char * path, const posix_spawn_file_actions_t *file_actions,
const posix_spawnattr_t * attrp, char *const argv[], char *const envp[]);
#define _write write
#define _execve VROOT_API_NAME(execve)
int _execvpe(const char *name, char * const argv[], char * const envp[]);
EXPORT
int VROOT_API_NAME(execl)(const char *name, const char *arg, ...)
{
VROOT_LOG("@%s %s %s\n",__FUNCTION__, name, arg);
va_list ap;
const char **argv;
int n;
va_start(ap, arg);
n = 1;
while (va_arg(ap, char *) != NULL)
n++;
va_end(ap);
argv = alloca((n + 1) * sizeof(*argv));
if (argv == NULL) {
errno = ENOMEM;
return (-1);
}
va_start(ap, arg);
n = 1;
argv[0] = arg;
while ((argv[n] = va_arg(ap, char *)) != NULL)
n++;
va_end(ap);
return (_execve(name, __DECONST(char **, argv), environ));
}
EXPORT
int VROOT_API_NAME(execle)(const char *name, const char *arg, ...)
{
VROOT_LOG("@%s %s %s\n",__FUNCTION__, name, arg);
va_list ap;
const char **argv;
char **envp;
int n;
va_start(ap, arg);
n = 1;
while (va_arg(ap, char *) != NULL)
n++;
va_end(ap);
argv = alloca((n + 1) * sizeof(*argv));
if (argv == NULL) {
errno = ENOMEM;
return (-1);
}
va_start(ap, arg);
n = 1;
argv[0] = arg;
while ((argv[n] = va_arg(ap, char *)) != NULL)
n++;
envp = va_arg(ap, char **);
va_end(ap);
return (_execve(name, __DECONST(char **, argv), envp));
}
EXPORT
int VROOT_API_NAME(execvp)(const char *name, char * const *argv)
{
VROOT_LOG("@%s %s\n",__FUNCTION__, name);
return (_execvpe(name, argv, environ));
}
EXPORT
int VROOT_API_NAME(execlp)(const char *name, const char *arg, ...)
{
VROOT_LOG("@%s %s %s\n",__FUNCTION__, name, arg);
va_list ap;
const char **argv;
int n;
va_start(ap, arg);
n = 1;
while (va_arg(ap, char *) != NULL)
n++;
va_end(ap);
argv = alloca((n + 1) * sizeof(*argv));
if (argv == NULL) {
errno = ENOMEM;
return (-1);
}
va_start(ap, arg);
n = 1;
argv[0] = arg;
while ((argv[n] = va_arg(ap, char *)) != NULL)
n++;
va_end(ap);
return (VROOT_API_NAME(execvp)(name, __DECONST(char **, argv)));
}
EXPORT
int VROOT_API_NAME(execv)(const char *name, char * const *argv)
{
VROOT_LOG("@%s %s\n",__FUNCTION__, name);
(void)_execve(name, argv, environ);
return (-1);
}
static int
execvPe(const char *name, const char *path, char * const *argv,
char * const *envp)
{
const char **memp;
size_t cnt, lp, ln;
int eacces, save_errno;
char *cur, buf[MAXPATHLEN];
const char *p, *bp;
struct stat sb;
eacces = 0;
/* If it's an absolute or relative path name, it's easy. */
if (strchr(name, '/')) {
bp = name;
cur = NULL;
goto retry;
}
bp = buf;
/* If it's an empty path name, fail in the usual POSIX way. */
if (*name == '\0') {
errno = ENOENT;
return (-1);
}
cur = alloca(strlen(path) + 1);
if (cur == NULL) {
errno = ENOMEM;
return (-1);
}
strcpy(cur, path);
while ((p = strsep(&cur, ":")) != NULL) {
/*
* It's a SHELL path -- double, leading and trailing colons
* mean the current directory.
*/
if (*p == '\0') {
p = ".";
lp = 1;
} else
lp = strlen(p);
ln = strlen(name);
/*
* If the path is too long complain. This is a possible
* security issue; given a way to make the path too long
* the user may execute the wrong program.
*/
if (lp + ln + 2 > sizeof(buf)) {
(void)_write(STDERR_FILENO, "execvP: ", 8);
(void)_write(STDERR_FILENO, p, lp);
(void)_write(STDERR_FILENO, ": path too long\n",
16);
continue;
}
bcopy(p, buf, lp);
buf[lp] = '/';
bcopy(name, buf + lp + 1, ln);
buf[lp + ln + 1] = '\0';
retry: (void)_execve(bp, argv, envp);
switch (errno) {
case E2BIG:
goto done;
case ELOOP:
case ENAMETOOLONG:
case ENOENT:
break;
case ENOEXEC:
for (cnt = 0; argv[cnt]; ++cnt)
;
/*
* cnt may be 0 above; always allocate at least
* 3 entries so that we can at least fit "sh", bp, and
* the NULL terminator. We can rely on cnt to take into
* account the NULL terminator in all other scenarios,
* as we drop argv[0].
*/
memp = alloca(MAX(3, cnt + 2) * sizeof(char *));
if (memp == NULL) {
/* errno = ENOMEM; XXX override ENOEXEC? */
goto done;
}
if (cnt > 0) {
memp[0] = argv[0];
memp[1] = bp;
bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
} else {
memp[0] = "sh";
memp[1] = bp;
memp[2] = NULL;
}
(void)_execve(_PATH_BSHELL,
__DECONST(char **, memp), envp);
goto done;
case ENOMEM:
goto done;
case ENOTDIR:
break;
case ETXTBSY:
/*
* We used to retry here, but sh(1) doesn't.
*/
goto done;
default:
/*
* EACCES may be for an inaccessible directory or
* a non-executable file. Call stat() to decide
* which. This also handles ambiguities for EFAULT
* and EIO, and undocumented errors like ESTALE.
* We hope that the race for a stat() is unimportant.
*/
save_errno = errno;
if (VROOT_API_NAME(stat)(bp, &sb) != 0)
break;
if (save_errno == EACCES) {
eacces = 1;
continue;
}
errno = save_errno;
goto done;
}
}
if (eacces)
errno = EACCES;
else if (cur)
errno = ENOENT;
/* else use existing errno from _execve */
done:
return (-1);
}
EXPORT
int VROOT_API_NAME(execvP)(const char *name, const char *path, char * const argv[])
{
VROOT_LOG("@%s %s %s\n",__FUNCTION__, name, path);
return execvPe(name, path, argv, environ);
}
__private_extern__ int
_execvpe(const char *name, char * const argv[], char * const envp[])
{
const char *path;
/* Get the path we're searching. */
if ((path = getenv("PATH")) == NULL)
path = _PATH_DEFPATH;
return (execvPe(name, path, argv, envp));
}
EXPORT
int VROOT_API_NAME(posix_spawnp)(pid_t * pid, const char * file, const posix_spawn_file_actions_t *file_actions,
const posix_spawnattr_t * attrp, char *const argv[], char *const envp[])
{
VROOT_LOG("@%s %s\n",__FUNCTION__, file);
const char *env_path;
char *bp;
char *cur;
char *p;
char **memp;
int lp;
int ln;
int cnt;
int err = 0;
int eacces = 0;
struct stat sb;
char path_buf[PATH_MAX];
if ((env_path = getenv("PATH")) == NULL)
env_path = _PATH_DEFPATH;
/* If it's an absolute or relative path name, it's easy. */
if (strchr(file, '/')) {
bp = (char *)file;
cur = NULL;
goto retry;
}
bp = path_buf;
/* If it's an empty path name, fail in the usual POSIX way. */
if (*file == '\0')
return (ENOENT);
if ((cur = alloca(strlen(env_path) + 1)) == NULL)
return ENOMEM;
strcpy(cur, env_path);
while ((p = strsep(&cur, ":")) != NULL) {
/*
* It's a SHELL path -- double, leading and trailing colons
* mean the current directory.
*/
if (*p == '\0') {
p = ".";
lp = 1;
} else {
lp = strlen(p);
}
ln = strlen(file);
/*
* If the path is too long complain. This is a possible
* security issue; given a way to make the path too long
* the user may spawn the wrong program.
*/
if (lp + ln + 2 > sizeof(path_buf)) {
err = ENAMETOOLONG;
goto done;
}
bcopy(p, path_buf, lp);
path_buf[lp] = '/';
bcopy(file, path_buf + lp + 1, ln);
path_buf[lp + ln + 1] = '\0';
retry: err = VROOT_API_NAME(posix_spawn)(pid, bp, file_actions, attrp, argv, envp);
switch (err) {
case E2BIG:
case ENOMEM:
case ETXTBSY:
goto done;
case ELOOP:
case ENAMETOOLONG:
case ENOENT:
case ENOTDIR:
break;
case ENOEXEC:
for (cnt = 0; argv[cnt]; ++cnt)
;
/*
* cnt may be 0 above; always allocate at least
* 3 entries so that we can at least fit "sh", bp, and
* the NULL terminator. We can rely on cnt to take into
* account the NULL terminator in all other scenarios,
* as we drop argv[0].
*/
memp = alloca(MAX(3, cnt + 2) * sizeof(char *));
if (memp == NULL) {
/* errno = ENOMEM; XXX override ENOEXEC? */
goto done;
}
if (cnt > 0) {
memp[0] = argv[0];
memp[1] = bp;
bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
} else {
memp[0] = "sh";
memp[1] = bp;
memp[2] = NULL;
}
err = VROOT_API_NAME(posix_spawn)(pid, _PATH_BSHELL, file_actions, attrp, memp, envp);
goto done;
default:
/*
* EACCES may be for an inaccessible directory or
* a non-executable file. Call stat() to decide
* which. This also handles ambiguities for EFAULT
* and EIO, and undocumented errors like ESTALE.
* We hope that the race for a stat() is unimportant.
*/
if (VROOT_API_NAME(stat)(bp, &sb) != 0)
break;
if (err == EACCES) {
eacces = 1;
continue;
}
goto done;
}
}
if (eacces)
err = EACCES;
else
err = ENOENT;
done:
return (err);
}