forked from thestinger/playpen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandbox.c
311 lines (265 loc) · 8.08 KB
/
sandbox.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
#include "openjail.h"
#include "helpers.h"
#include "trace.h"
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/ptrace.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <syscall.h>
#define SYSCALL_NAME_MAX 30
static int get_syscall_nr(const char *name)
{
int result = seccomp_syscall_resolve_name(name);
if (result == __NR_SCMP_ERROR)
{
errx(EXIT_FAILURE, "non-existent syscall: %s", name);
}
return result;
}
static void allow_syscall(const oj_args *args, scmp_filter_ctx ctx, int syscall)
{
if (args->hardened)
{
switch(syscall)
{
case SYS_mount:
case SYS_ptrace:
case SYS_seccomp:
errx(EXIT_FAILURE, "On hardened mode, neither mount, ptrace or seccomp syscalls are allowed");
}
}
unsigned int act = SCMP_ACT_ALLOW;
switch(syscall)
{
case SYS_clone:
case SYS_unshare:
if (!args->allow_ns)
act = SCMP_ACT_TRACE(SYSCALL_CLONE_ARG);
break;
}
CHECK(seccomp_rule_add(ctx, act, syscall, 0));
}
static void set_rlimit(int resource, long value)
{
struct rlimit rlim;
if (value < 0)
return;
rlim.rlim_cur = (rlim_t) value;
rlim.rlim_max = (rlim_t) value;
CHECK_POSIX_ARGS(setrlimit((unsigned int) resource, &rlim), "set_rlimit %d", resource);
}
static void drop_capabilities()
{
// once we drop the privileges, we should never regain them
// by e.g. executing a suid-root binary
CHECK_POSIX(prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0));
for (int i = 0; i <= 63; i++)
{
int cur = prctl(PR_CAPBSET_READ, i, 0, 0, 0);
if (cur < 0)
{
if (errno == EINVAL)
continue;
else
err(EXIT_FAILURE,"prctl(PR_CAPBSET_READ, %d, 0, 0, 0)", i);
}
if (cur != 0)
{
int code = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
if (code < 0 && errno != EINVAL)
err(EXIT_FAILURE, "prctl(PR_CAPBSET_DROP, %d, 0, 0, 0)",i);
}
}
}
static void bind_list_apply(const char *root, struct bind_list *list)
{
for (; list; list = list->next)
{
char *dst = join_path(root, list->dest);
// Only use MS_REC with writable mounts to work around a kernel bug:
// https://bugzilla.kernel.org/show_bug.cgi?id=24912
MOUNTX(list->origin, dst, "bind", MS_BIND | (list->read_only ? 0 : MS_REC), NULL);
if (list->read_only)
MOUNTX(list->origin, dst, "bind", MS_BIND|MS_REMOUNT|MS_RDONLY, NULL);
free(dst);
}
}
static char *my_strdup(char *val)
{
if (val)
return strdup(val);
return NULL;
}
static bool get_pw(const oj_args *args, struct passwd *out)
{
if (args->is_root)
{
// no namespace mangling was made
errno = 0;
struct passwd *pw = getpwuid(getuid());
if (!pw && errno)
err(EXIT_FAILURE, "getpwuid");
if (pw)
{
out->pw_name = my_strdup(pw->pw_name);
out->pw_uid = pw->pw_uid;
out->pw_gid = pw->pw_gid;
out->pw_dir = my_strdup(pw->pw_dir);
out->pw_shell = my_strdup(pw->pw_shell);
return true;
}
}
// if failed, use current environment info
// this will pick up the parent namespace information
// from the current user. If you want to override this behaviour,
// provide a /etc/passwd with the correct information
out->pw_name = my_strdup(getenv("USER"));
out->pw_uid = getuid();
out->pw_gid = getgid();
out->pw_dir = my_strdup(getenv("HOME"));
out->pw_shell = my_strdup(getenv("SHELL"));
return false;
}
int sandbox(const oj_args *args)
{
// Kill this process if the parent dies. This is not a replacement for killing the sandboxed
// processes via a PID namespace as it is not inherited by child processes, but is more
// robust when the sandboxed process is not allowed to fork.
CHECK_POSIX(prctl(PR_SET_PDEATHSIG, SIGKILL));
scmp_filter_ctx ctx = seccomp_init(args->learn_name || args->syscall_reporting ? SCMP_ACT_TRACE(GENERIC_SYSCALL) : SCMP_ACT_KILL);
if (!ctx) errx(EXIT_FAILURE, "seccomp_init");
if (args->syscalls_file)
{
char name[SYSCALL_NAME_MAX];
FILE *file = fopen(args->syscalls_file, "r");
if (!file) err(EXIT_FAILURE, "failed to open syscalls file: %s", args->syscalls_file);
while (fgets(name, sizeof name, file))
{
char *pos;
if ((pos = strchr(name, '\n'))) *pos = '\0';
allow_syscall(args, ctx, get_syscall_nr(name));
}
fclose(file);
}
CHECK(seccomp_rule_add(ctx, SCMP_ACT_ALLOW, __NR_execve, 0));
if (args->syscalls)
{
for (char *s_ptr = args->syscalls, *saveptr; ; s_ptr = NULL)
{
const char *syscall = strtok_r(s_ptr, ",", &saveptr);
if (!syscall) break;
allow_syscall(args, ctx, get_syscall_nr(syscall));
}
}
// Wait until the scope unit is set up before moving on. This also ensures that the parent
// didn't die before `prctl` was called.
uint8_t ready;
CHECK_POSIX(read(STDIN_FILENO, &ready, sizeof ready));
CHECK_POSIX(sethostname(args->hostname, strlen(args->hostname)));
CHECK_POSIX(setdomainname(args->hostname, strlen(args->hostname)));
// avoid propagating mounts to or from the parent's mount namespace
MOUNTX(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL);
// turn directory into a bind mount
MOUNTX(args->root, args->root, "bind", MS_BIND|MS_REC, NULL);
// re-mount as read-only
if (!args->chroot_rw)
MOUNTX(args->root, args->root, "bind", MS_BIND|MS_REMOUNT|MS_RDONLY|MS_REC, NULL);
if (args->mount_proc)
{
char *mnt = join_path(args->root, "proc");
MOUNTX(NULL, mnt, "proc", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL);
free(mnt);
}
if (args->mount_dev)
{
char *mnt = join_path(args->root, "dev");
MOUNTX(NULL, mnt, "devtmpfs", MS_NOSUID|MS_NOEXEC, NULL);
free(mnt);
}
if (args->mount_minimal_dev)
{
char *devices[] = { "/dev/null", "/dev/zero", "/dev/random", "/dev/urandom", NULL };
for (int i = 0; devices[i] != NULL; i++)
{
char *mnt = join_path(args->root, devices[i]);
if (access(mnt, F_OK) < 0)
{
errx(EXIT_FAILURE,"The file '%s' was not found inside the chroot jail", mnt);
}
CHECK_POSIX(mount(devices[i], mnt, NULL, MS_BIND, NULL));
free(mnt);
}
}
if (args->mount_tmpfs)
{
char *dir = join_path(args->root, "dev/shm/tmp");
CHECK_POSIX(mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO));
char *tmp = join_path(args->root, "tmp");
if (mount(dir, tmp, "bind", MS_BIND, NULL) == -1)
{
if (errno != ENOENT)
{
err(EXIT_FAILURE, "mounting /tmp failed");
}
}
free(tmp);
free(dir);
}
set_rlimit(RLIMIT_AS, args->rlimit_as);
set_rlimit(RLIMIT_FSIZE, args->rlimit_fsize);
set_rlimit(RLIMIT_NOFILE, args->rlimit_nofile);
set_rlimit(RLIMIT_NPROC, args->rlimit_nproc);
set_rlimit(RLIMIT_NICE, args->rlimit_nice);
set_rlimit(RLIMIT_CPU, args->rlimit_cpu);
bind_list_apply(args->root, args->binds);
// preserve a reference to the target directory
CHECK_POSIX(chdir(args->root));
// make the working directory into the root of the mount namespace
MOUNTX(".", "/", NULL, MS_MOVE, NULL);
// chroot into the root of the mount namespace
CHECK_POSIX_ARGS(chroot("."), "chroot into `%s` failed", args->root);
CHECK_POSIX_ARGS(chdir("/"), "entering chroot `%s` failed", args->root);
errno = 0;
struct passwd pw;
bool did_found = get_pw(args, &pw);
// check if exists
if (access(pw.pw_dir, F_OK) >= 0)
{
if (args->mount_tmpfs)
{
CHECK_POSIX(mkdir("/dev/shm/home", S_IRWXU | S_IRWXG |S_IRWXO));
MOUNTX("/dev/shm/home", pw.pw_dir, "bind", MS_BIND, NULL);
}
// switch to the user's home directory as a login shell would
CHECK_POSIX(chdir(pw.pw_dir));
} else {
CHECK_POSIX(chdir("/"));
}
drop_capabilities();
// create a new session
CHECK_POSIX(setsid());
if (did_found)
CHECK_POSIX(initgroups(pw.pw_name, pw.pw_gid));
CHECK_POSIX(setresuid(pw.pw_uid, pw.pw_uid, pw.pw_uid));
CHECK_POSIX(setresgid(pw.pw_gid, pw.pw_gid, pw.pw_gid));
char path[] = "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:";
char *env[] = {path, NULL, NULL, NULL, NULL};
if ((asprintf(env + 1, "HOME=%s", pw.pw_dir) < 0 ||
asprintf(env + 2, "USER=%s", pw.pw_name) < 0 ||
asprintf(env + 3, "LOGNAME=%s", pw.pw_name) < 0))
{
errx(EXIT_FAILURE, "asprintf");
}
if (pw.pw_name) free(pw.pw_name);
if (pw.pw_dir) free(pw.pw_dir);
if (pw.pw_shell) free(pw.pw_shell);
CHECK(seccomp_load(ctx));
CHECK_POSIX(execvpe(args->cmd[0], args->cmd, env));
errx(EXIT_FAILURE, "Control reached after excve");
}