blob: 708c68c4cfbd5204af04fdd717024ad9b9dc9b75 [file] [log] [blame]
Elly Jonescd7a9042011-07-22 13:56:51 -04001/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
Will Drewry32ac9f52011-08-18 21:36:27 -05003 * found in the LICENSE file.
4 */
Elly Jonescd7a9042011-07-22 13:56:51 -04005
6#define _BSD_SOURCE
7#define _GNU_SOURCE
Will Drewry32ac9f52011-08-18 21:36:27 -05008#include <ctype.h>
Elly Jonescd7a9042011-07-22 13:56:51 -04009#include <errno.h>
10#include <grp.h>
11#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050012#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040013#include <linux/capability.h>
14#include <linux/securebits.h>
15#include <pwd.h>
16#include <sched.h>
17#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050018#include <stdarg.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <syscall.h>
23#include <sys/capability.h>
24#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050025#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040026#include <sys/prctl.h>
27#include <sys/wait.h>
28#include <syslog.h>
29#include <unistd.h>
30
31#include "libminijail.h"
Will Drewry32ac9f52011-08-18 21:36:27 -050032#include "libsyscalls.h"
Elly Jonescd7a9042011-07-22 13:56:51 -040033#include "libminijail-private.h"
34
Will Drewry32ac9f52011-08-18 21:36:27 -050035/* Until these are reliably available in linux/prctl.h */
36#ifndef PR_SET_SECCOMP_FILTER
Elly Jonese1749eb2011-10-07 13:54:59 -040037# define PR_SECCOMP_FILTER_SYSCALL 0
38# define PR_SECCOMP_FILTER_EVENT 1
39# define PR_GET_SECCOMP_FILTER 35
40# define PR_SET_SECCOMP_FILTER 36
41# define PR_CLEAR_SECCOMP_FILTER 37
Will Drewry32ac9f52011-08-18 21:36:27 -050042#endif
43
Will Drewry32ac9f52011-08-18 21:36:27 -050044#define die(_msg, ...) do { \
Elly Jonese1749eb2011-10-07 13:54:59 -040045 syslog(LOG_ERR, "libminijail: " _msg, ## __VA_ARGS__); \
46 abort(); \
Will Drewry32ac9f52011-08-18 21:36:27 -050047} while (0)
Elly Jonescd7a9042011-07-22 13:56:51 -040048
Will Drewry32ac9f52011-08-18 21:36:27 -050049#define pdie(_msg, ...) \
Elly Jonese1749eb2011-10-07 13:54:59 -040050 die(_msg ": %s", ## __VA_ARGS__, strerror(errno))
Will Drewry32ac9f52011-08-18 21:36:27 -050051
52#define warn(_msg, ...) \
Elly Jonese1749eb2011-10-07 13:54:59 -040053 syslog(LOG_WARNING, "libminijail: " _msg, ## __VA_ARGS__)
Elly Jonescd7a9042011-07-22 13:56:51 -040054
Will Drewryf89aef52011-09-16 16:48:57 -050055struct seccomp_filter {
Elly Jonese1749eb2011-10-07 13:54:59 -040056 int nr;
57 char *filter;
58 struct seccomp_filter *next, *prev;
Will Drewryf89aef52011-09-16 16:48:57 -050059};
60
Elly Jones51a5b6c2011-10-12 19:09:26 -040061struct binding {
62 char *src;
63 char *dest;
64 int writeable;
65 struct binding *next;
66};
67
Will Drewryf89aef52011-09-16 16:48:57 -050068struct minijail {
Elly Jonese1749eb2011-10-07 13:54:59 -040069 struct {
70 int uid:1;
71 int gid:1;
72 int caps:1;
73 int vfs:1;
74 int pids:1;
75 int seccomp:1;
76 int readonly:1;
77 int usergroups:1;
78 int ptrace:1;
79 int seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -040080 int chroot:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040081 } flags;
82 uid_t uid;
83 gid_t gid;
84 gid_t usergid;
85 char *user;
86 uint64_t caps;
87 pid_t initpid;
88 int filter_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -040089 int binding_count;
90 char *chrootdir;
Elly Jonese1749eb2011-10-07 13:54:59 -040091 struct seccomp_filter *filters;
Elly Jones51a5b6c2011-10-12 19:09:26 -040092 struct binding *bindings_head;
93 struct binding *bindings_tail;
Will Drewryf89aef52011-09-16 16:48:57 -050094};
95
Will Drewry6ac91122011-10-21 16:38:58 -050096struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -040097{
Elly Jones51a5b6c2011-10-12 19:09:26 -040098 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -040099}
100
Will Drewry6ac91122011-10-21 16:38:58 -0500101void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400102{
103 if (uid == 0)
104 die("useless change to uid 0");
105 j->uid = uid;
106 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400107}
108
Will Drewry6ac91122011-10-21 16:38:58 -0500109void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400110{
111 if (gid == 0)
112 die("useless change to gid 0");
113 j->gid = gid;
114 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400115}
116
Will Drewry6ac91122011-10-21 16:38:58 -0500117int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400118{
119 char *buf = NULL;
120 struct passwd pw;
121 struct passwd *ppw = NULL;
122 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
123 if (sz == -1)
124 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400125
Elly Jonese1749eb2011-10-07 13:54:59 -0400126 /* sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
127 * the maximum needed size of the buffer, so we don't have to search.
128 */
129 buf = malloc(sz);
130 if (!buf)
131 return -ENOMEM;
132 getpwnam_r(user, &pw, buf, sz, &ppw);
133 free(buf);
134 if (!ppw)
135 return -errno;
136 minijail_change_uid(j, ppw->pw_uid);
137 j->user = strdup(user);
138 if (!j->user)
139 return -ENOMEM;
140 j->usergid = ppw->pw_gid;
141 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400142}
143
Will Drewry6ac91122011-10-21 16:38:58 -0500144int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400145{
146 char *buf = NULL;
147 struct group gr;
148 struct group *pgr = NULL;
149 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
150 if (sz == -1)
151 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400152
Elly Jonese1749eb2011-10-07 13:54:59 -0400153 /* sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
154 * the maximum needed size of the buffer, so we don't have to search.
155 */
156 buf = malloc(sz);
157 if (!buf)
158 return -ENOMEM;
159 getgrnam_r(group, &gr, buf, sz, &pgr);
160 free(buf);
161 if (!pgr)
162 return -errno;
163 minijail_change_gid(j, pgr->gr_gid);
164 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400165}
166
Will Drewry6ac91122011-10-21 16:38:58 -0500167void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400168{
169 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400170}
171
Will Drewry6ac91122011-10-21 16:38:58 -0500172void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400173{
174 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500175}
176
Will Drewry6ac91122011-10-21 16:38:58 -0500177void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400178{
179 j->caps = capmask;
180 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400181}
182
Will Drewry6ac91122011-10-21 16:38:58 -0500183void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400184{
185 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400186}
187
Will Drewry6ac91122011-10-21 16:38:58 -0500188void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400189{
190 j->flags.pids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400191}
192
Will Drewry6ac91122011-10-21 16:38:58 -0500193void API minijail_remount_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400194{
195 j->flags.vfs = 1;
196 j->flags.readonly = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400197}
198
Will Drewry6ac91122011-10-21 16:38:58 -0500199void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400200{
201 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400202}
203
Will Drewry6ac91122011-10-21 16:38:58 -0500204void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400205{
206 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400207}
208
Will Drewry6ac91122011-10-21 16:38:58 -0500209int API minijail_enter_chroot(struct minijail *j, const char *dir) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400210 if (j->chrootdir)
211 return -EINVAL;
212 j->chrootdir = strdup(dir);
213 if (!j->chrootdir)
214 return -ENOMEM;
215 j->flags.chroot = 1;
216 return 0;
217}
218
Will Drewry6ac91122011-10-21 16:38:58 -0500219int API minijail_bind(struct minijail *j, const char *src, const char *dest,
220 int writeable) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400221 struct binding *b;
222
223 if (*dest != '/')
224 return -EINVAL;
225 b = calloc(1, sizeof(*b));
226 if (!b)
227 return -ENOMEM;
228 b->dest = strdup(dest);
229 if (!b->dest)
230 goto error;
231 b->src = strdup(src);
232 if (!b->src)
233 goto error;
234 b->writeable = writeable;
235
236 syslog(LOG_INFO, "libminijail: bind %s -> %s", src, dest);
237
238 /* Force vfs namespacing so the bind mounts don't leak out into the
239 * containing vfs namespace.
240 */
241 minijail_namespace_vfs(j);
242
243 if (j->bindings_tail)
244 j->bindings_tail->next = b;
245 else
246 j->bindings_head = b;
247 j->bindings_tail = b;
248 j->binding_count++;
249
250 return 0;
251
252error:
253 free(b->src);
254 free(b->dest);
255 free(b);
256 return -ENOMEM;
257}
258
Will Drewry6ac91122011-10-21 16:38:58 -0500259int API minijail_add_seccomp_filter(struct minijail *j, int nr,
260 const char *filter)
Elly Jonese1749eb2011-10-07 13:54:59 -0400261{
262 struct seccomp_filter *sf;
263 if (!filter || nr < 0)
264 return -EINVAL;
Will Drewry32ac9f52011-08-18 21:36:27 -0500265
Elly Jonese1749eb2011-10-07 13:54:59 -0400266 sf = malloc(sizeof(*sf));
267 if (!sf)
268 return -ENOMEM;
269 sf->nr = nr;
270 sf->filter = strndup(filter, MINIJAIL_MAX_SECCOMP_FILTER_LINE);
271 if (!sf->filter) {
272 free(sf);
273 return -ENOMEM;
274 }
Will Drewry32ac9f52011-08-18 21:36:27 -0500275
Elly Jonese1749eb2011-10-07 13:54:59 -0400276 j->filter_count++;
Will Drewryf89aef52011-09-16 16:48:57 -0500277
Elly Jonese1749eb2011-10-07 13:54:59 -0400278 if (!j->filters) {
279 j->filters = sf;
280 sf->next = sf;
281 sf->prev = sf;
282 return 0;
283 }
284 sf->next = j->filters;
285 sf->prev = j->filters->prev;
286 sf->prev->next = sf;
287 j->filters->prev = sf;
288 return 0;
Will Drewry32ac9f52011-08-18 21:36:27 -0500289}
290
Will Drewry6ac91122011-10-21 16:38:58 -0500291int API minijail_lookup_syscall(const char *name)
Elly Jonese1749eb2011-10-07 13:54:59 -0400292{
293 const struct syscall_entry *entry = syscall_table;
294 for (; entry->name && entry->nr >= 0; ++entry)
295 if (!strcmp(entry->name, name))
296 return entry->nr;
297 return -1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500298}
299
Will Drewry6ac91122011-10-21 16:38:58 -0500300char *strip(char *s)
Elly Jonese1749eb2011-10-07 13:54:59 -0400301{
302 char *end;
303 while (*s && isblank(*s))
304 s++;
305 end = s + strlen(s) - 1;
306 while (*end && (isblank(*end) || *end == '\n'))
307 end--;
308 *(end + 1) = '\0';
309 return s;
Will Drewry32ac9f52011-08-18 21:36:27 -0500310}
311
Will Drewry6ac91122011-10-21 16:38:58 -0500312void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400313{
314 FILE *file = fopen(path, "r");
315 char line[MINIJAIL_MAX_SECCOMP_FILTER_LINE];
Ben Chan1d697932011-10-14 10:53:32 -0700316 int count = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -0400317 if (!file)
318 pdie("failed to open seccomp filters file");
Will Drewry32ac9f52011-08-18 21:36:27 -0500319
Elly Jonese1749eb2011-10-07 13:54:59 -0400320 /* Format is simple:
321 * syscall_name<COLON><FILTER STRING>[\n|EOF]
322 * #...comment...
323 * <empty line?
324 */
325 while (fgets(line, sizeof(line), file)) {
326 char *filter = line;
327 char *name = strsep(&filter, ":");
328 char *name_end = NULL;
329 int nr = -1;
Ben Chan1d697932011-10-14 10:53:32 -0700330 count++;
Will Drewry32ac9f52011-08-18 21:36:27 -0500331
Ben Chan1d697932011-10-14 10:53:32 -0700332 /* Allow comment lines */
333 if (*name == '#')
334 continue;
Will Drewry32ac9f52011-08-18 21:36:27 -0500335
Elly Jonese1749eb2011-10-07 13:54:59 -0400336 name = strip(name);
Will Drewry32ac9f52011-08-18 21:36:27 -0500337
Elly Jonese1749eb2011-10-07 13:54:59 -0400338 if (!filter) {
339 if (strlen(name))
340 die("invalid filter on line %d", count);
341 /* Allow empty lines */
342 continue;
343 }
Will Drewry32ac9f52011-08-18 21:36:27 -0500344
Elly Jonese1749eb2011-10-07 13:54:59 -0400345 filter = strip(filter);
Will Drewry32ac9f52011-08-18 21:36:27 -0500346
Elly Jonese1749eb2011-10-07 13:54:59 -0400347 /* Take direct syscall numbers */
348 nr = strtol(name, &name_end, 0);
349 /* Or fail-over to using names */
350 if (*name_end != '\0')
351 nr = minijail_lookup_syscall(name);
352 if (nr < 0)
353 die("syscall '%s' unknown", name);
Will Drewry32ac9f52011-08-18 21:36:27 -0500354
Elly Jonese1749eb2011-10-07 13:54:59 -0400355 if (minijail_add_seccomp_filter(j, nr, filter))
356 pdie("failed to add filter for syscall '%s'", name);
357 }
358 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500359}
360
Will Drewryf89aef52011-09-16 16:48:57 -0500361struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400362 size_t available;
363 size_t total;
364 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500365};
366
Will Drewry6ac91122011-10-21 16:38:58 -0500367void marshal_state_init(struct marshal_state *state,
368 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400369{
370 state->available = available;
371 state->buf = buf;
372 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500373}
374
Will Drewry6ac91122011-10-21 16:38:58 -0500375void marshal_append(struct marshal_state *state,
376 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400377{
378 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500379
Elly Jonese1749eb2011-10-07 13:54:59 -0400380 /* Up to |available| will be written. */
381 if (copy_len) {
382 memcpy(state->buf, src, copy_len);
383 state->buf += copy_len;
384 state->available -= copy_len;
385 }
386 /* |total| will contain the expected length. */
387 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500388}
389
Will Drewry6ac91122011-10-21 16:38:58 -0500390void minijail_marshal_helper(struct marshal_state *state,
391 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400392{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400393 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400394 marshal_append(state, (char *)j, sizeof(*j));
395 if (j->user)
396 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400397 if (j->chrootdir)
398 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Elly Jonese1749eb2011-10-07 13:54:59 -0400399 if (j->flags.seccomp_filter && j->filters) {
400 struct seccomp_filter *f = j->filters;
401 do {
402 marshal_append(state, (char *)&f->nr, sizeof(f->nr));
403 marshal_append(state, f->filter, strlen(f->filter) + 1);
404 f = f->next;
405 } while (f != j->filters);
406 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400407 for (b = j->bindings_head; b; b = b->next) {
408 marshal_append(state, b->src, strlen(b->src) + 1);
409 marshal_append(state, b->dest, strlen(b->dest) + 1);
410 marshal_append(state, (char *)&b->writeable, sizeof(b->writeable));
411 }
Will Drewryf89aef52011-09-16 16:48:57 -0500412}
413
Will Drewry6ac91122011-10-21 16:38:58 -0500414size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400415{
416 struct marshal_state state;
417 marshal_state_init(&state, NULL, 0);
418 minijail_marshal_helper(&state, j);
419 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500420}
421
Elly Jonese1749eb2011-10-07 13:54:59 -0400422int minijail_marshal(const struct minijail *j, char *buf, size_t available)
423{
424 struct marshal_state state;
425 marshal_state_init(&state, buf, available);
426 minijail_marshal_helper(&state, j);
427 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500428}
429
Elly Jones51a5b6c2011-10-12 19:09:26 -0400430/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
431 * @length Number of bytes to consume
432 * @buf Buffer to consume from
433 * @buflength Size of @buf
434 *
435 * Returns a pointer to the base of the bytes, or NULL for errors.
436 */
Will Drewry6ac91122011-10-21 16:38:58 -0500437void *consumebytes(size_t length, char **buf, size_t *buflength) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400438 char *p = *buf;
439 if (length > *buflength)
440 return NULL;
441 *buf += length;
442 *buflength -= length;
443 return p;
444}
445
446/* consumestr: consumes a C string from a buffer @buf of length @length
447 * @buf Buffer to consume
448 * @length Length of buffer
449 *
450 * Returns a pointer to the base of the string, or NULL for errors.
451 */
Will Drewry6ac91122011-10-21 16:38:58 -0500452char *consumestr(char **buf, size_t *buflength) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400453 size_t len = strnlen(*buf, *buflength);
454 if (len == *buflength)
455 /* There's no null-terminator */
456 return NULL;
457 return consumebytes(len + 1, buf, buflength);
458}
459
Elly Jonese1749eb2011-10-07 13:54:59 -0400460int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
461{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400462 int i;
463 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500464 int ret = -EINVAL;
465
Elly Jonese1749eb2011-10-07 13:54:59 -0400466 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500467 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400468 memcpy((void *)j, serialized, sizeof(*j));
469 serialized += sizeof(*j);
470 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500471
Will Drewrybee7ba72011-10-21 20:47:01 -0500472 /* Potentially stale pointers not used as signals. */
473 j->bindings_head = NULL;
474 j->bindings_tail = NULL;
475 j->filters = NULL;
476
Elly Jonese1749eb2011-10-07 13:54:59 -0400477 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400478 char *user = consumestr(&serialized, &length);
479 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500480 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400481 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500482 if (!j->user)
483 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400484 }
Will Drewryf89aef52011-09-16 16:48:57 -0500485
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400486 if (j->chrootdir) { /* stale pointer */
487 char *chrootdir = consumestr(&serialized, &length);
488 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500489 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400490 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500491 if (!j->chrootdir)
492 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400493 }
494
Elly Jonese1749eb2011-10-07 13:54:59 -0400495 if (j->flags.seccomp_filter && j->filter_count) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400496 count = j->filter_count;
Elly Jonese1749eb2011-10-07 13:54:59 -0400497 /* Let add_seccomp_filter recompute the value. */
498 j->filter_count = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -0400499 for (; count > 0; --count) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400500 int *nr = (int *)consumebytes(sizeof(*nr), &serialized,
501 &length);
Elly Jonese1749eb2011-10-07 13:54:59 -0400502 char *filter;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400503 if (!nr)
Will Drewrybee7ba72011-10-21 20:47:01 -0500504 goto bad_filters;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400505 filter = consumestr(&serialized, &length);
506 if (!filter)
Will Drewrybee7ba72011-10-21 20:47:01 -0500507 goto bad_filters;
Elly Jonese1749eb2011-10-07 13:54:59 -0400508 if (minijail_add_seccomp_filter(j, *nr, filter))
Will Drewrybee7ba72011-10-21 20:47:01 -0500509 goto bad_filters;
Elly Jonese1749eb2011-10-07 13:54:59 -0400510 }
511 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400512
513 count = j->binding_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400514 j->binding_count = 0;
515 for (i = 0; i < count; ++i) {
516 int *writeable;
517 const char *dest;
518 const char *src = consumestr(&serialized, &length);
519 if (!src)
Will Drewrybee7ba72011-10-21 20:47:01 -0500520 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400521 dest = consumestr(&serialized, &length);
522 if (!dest)
Will Drewrybee7ba72011-10-21 20:47:01 -0500523 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400524 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
525 if (!writeable)
Will Drewrybee7ba72011-10-21 20:47:01 -0500526 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400527 if (minijail_bind(j, src, dest, *writeable))
Will Drewrybee7ba72011-10-21 20:47:01 -0500528 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400529 }
530
Elly Jonese1749eb2011-10-07 13:54:59 -0400531 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500532
533bad_bindings:
534bad_filters:
535 if (j->chrootdir)
536 free(j->chrootdir);
537bad_chrootdir:
538 if (j->user)
539 free(j->user);
540clear_pointers:
541 j->user = NULL;
542 j->chrootdir = NULL;
543out:
544 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500545}
546
Elly Jonese1749eb2011-10-07 13:54:59 -0400547void minijail_preenter(struct minijail *j)
548{
549 /* Strip out options which are minijail_run() only. */
550 j->flags.vfs = 0;
551 j->flags.readonly = 0;
552 j->flags.pids = 0;
Will Drewryfe4a3722011-09-16 14:50:50 -0500553}
554
Elly Jonese1749eb2011-10-07 13:54:59 -0400555void minijail_preexec(struct minijail *j)
556{
557 int vfs = j->flags.vfs;
558 int readonly = j->flags.readonly;
559 if (j->user)
560 free(j->user);
561 j->user = NULL;
562 memset(&j->flags, 0, sizeof(j->flags));
563 /* Now restore anything we meant to keep. */
564 j->flags.vfs = vfs;
565 j->flags.readonly = readonly;
566 /* Note, pidns will already have been used before this call. */
Will Drewry2ddaad02011-09-16 11:36:08 -0500567}
568
Elly Jones51a5b6c2011-10-12 19:09:26 -0400569/* bind_one: Applies bindings from @b for @j, recursing as needed.
570 * @j Minijail these bindings are for
571 * @b Head of list of bindings
572 *
573 * Returns 0 for success.
574 */
Will Drewry6ac91122011-10-21 16:38:58 -0500575int bind_one(const struct minijail *j, struct binding *b) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400576 int ret = 0;
577 char *dest = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400578 if (ret)
579 return ret;
580 /* dest has a leading "/" */
581 if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0)
582 return -ENOMEM;
Elly Jonesa1059632011-12-15 15:17:07 -0500583 ret = mount(b->src, dest, NULL, MS_BIND, NULL);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400584 if (ret)
585 pdie("bind: %s -> %s", b->src, dest);
Elly Jonesa1059632011-12-15 15:17:07 -0500586 if (!b->writeable) {
587 ret = mount(b->src, dest, NULL,
588 MS_BIND | MS_REMOUNT | MS_RDONLY, NULL);
589 if (ret)
590 pdie("bind ro: %s -> %s", b->src, dest);
591 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400592 free(dest);
593 if (b->next)
594 return bind_one(j, b->next);
595 return ret;
596}
597
Will Drewry6ac91122011-10-21 16:38:58 -0500598int enter_chroot(const struct minijail *j) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400599 int ret;
600 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
601 return ret;
602
603 if (chroot(j->chrootdir))
604 return -errno;
605
606 if (chdir("/"))
607 return -errno;
608
609 return 0;
610}
611
Will Drewry6ac91122011-10-21 16:38:58 -0500612int remount_readonly(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400613{
614 const char *kProcPath = "/proc";
615 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
616 /* Right now, we're holding a reference to our parent's old mount of
617 * /proc in our namespace, which means using MS_REMOUNT here would
618 * mutate our parent's mount as well, even though we're in a VFS
619 * namespace (!). Instead, remove their mount from our namespace
620 * and make our own.
621 */
622 if (umount(kProcPath))
623 return -errno;
624 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
625 return -errno;
626 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400627}
628
Will Drewry6ac91122011-10-21 16:38:58 -0500629void drop_caps(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400630{
631 cap_t caps = cap_get_proc();
632 cap_value_t raise_flag[1];
633 unsigned int i;
634 if (!caps)
635 die("can't get process caps");
636 if (cap_clear_flag(caps, CAP_INHERITABLE))
637 die("can't clear inheritable caps");
638 if (cap_clear_flag(caps, CAP_EFFECTIVE))
639 die("can't clear effective caps");
640 if (cap_clear_flag(caps, CAP_PERMITTED))
641 die("can't clear permitted caps");
642 for (i = 0; i < sizeof(j->caps) * 8 && cap_valid((int)i); ++i) {
643 if (i != CAP_SETPCAP && !(j->caps & (1 << i)))
644 continue;
645 raise_flag[0] = i;
646 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, raise_flag, CAP_SET))
647 die("can't add effective cap");
648 if (cap_set_flag(caps, CAP_PERMITTED, 1, raise_flag, CAP_SET))
649 die("can't add permitted cap");
650 if (cap_set_flag(caps, CAP_INHERITABLE, 1, raise_flag, CAP_SET))
651 die("can't add inheritable cap");
652 }
653 if (cap_set_proc(caps))
654 die("can't apply cleaned capset");
655 cap_free(caps);
656 for (i = 0; i < sizeof(j->caps) * 8 && cap_valid((int)i); ++i) {
657 if (j->caps & (1 << i))
658 continue;
659 if (prctl(PR_CAPBSET_DROP, i))
660 pdie("prctl(PR_CAPBSET_DROP)");
661 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400662}
663
Will Drewry6ac91122011-10-21 16:38:58 -0500664int setup_seccomp_filters(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400665{
666 const struct seccomp_filter *sf = j->filters;
667 int ret = 0;
668 int broaden = 0;
Will Drewry32ac9f52011-08-18 21:36:27 -0500669
Elly Jonese1749eb2011-10-07 13:54:59 -0400670 /* No filters installed isn't necessarily an error. */
671 if (!sf)
672 return ret;
Will Drewry32ac9f52011-08-18 21:36:27 -0500673
Elly Jonese1749eb2011-10-07 13:54:59 -0400674 do {
675 errno = 0;
676 ret = prctl(PR_SET_SECCOMP_FILTER, PR_SECCOMP_FILTER_SYSCALL,
677 sf->nr, broaden ? "1" : sf->filter);
678 if (ret) {
679 switch (errno) {
680 case ENOSYS:
681 /* TODO(wad) make this a config option */
682 if (broaden)
683 die("CONFIG_SECCOMP_FILTER is not"
684 "supported by your kernel");
685 warn("missing CONFIG_FTRACE_SYSCALLS; relaxing"
686 "the filter for %d", sf->nr);
687 broaden = 1;
688 continue;
689 case E2BIG:
690 warn("seccomp filter too long: %d", sf->nr);
691 pdie("filter too long");
692 case ENOSPC:
693 pdie("too many seccomp filters");
694 case EPERM:
695 warn("syscall filter disallowed for %d",
696 sf->nr);
697 pdie("failed to install seccomp filter");
698 case EINVAL:
699 warn("seccomp filter or call method is"
700 " invalid. %d:'%s'", sf->nr, sf->filter);
701 default:
702 pdie("failed to install seccomp filter");
703 }
704 }
705 sf = sf->next;
706 broaden = 0;
707 } while (sf != j->filters);
708 return ret;
Will Drewry32ac9f52011-08-18 21:36:27 -0500709}
710
Will Drewry6ac91122011-10-21 16:38:58 -0500711void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400712{
713 if (j->flags.pids)
714 die("tried to enter a pid-namespaced jail;"
715 "try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400716
Elly Jonese1749eb2011-10-07 13:54:59 -0400717 if (j->flags.seccomp_filter && setup_seccomp_filters(j))
718 pdie("failed to configure seccomp filters");
Will Drewry32ac9f52011-08-18 21:36:27 -0500719
Elly Jonese1749eb2011-10-07 13:54:59 -0400720 if (j->flags.usergroups && !j->user)
721 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400722
Elly Jonese1749eb2011-10-07 13:54:59 -0400723 /* We can't recover from failures if we've dropped privileges partially,
724 * so we don't even try. If any of our operations fail, we abort() the
725 * entire process.
726 */
727 if (j->flags.vfs && unshare(CLONE_NEWNS))
728 pdie("unshare");
Elly Jonescd7a9042011-07-22 13:56:51 -0400729
Elly Jones51a5b6c2011-10-12 19:09:26 -0400730 if (j->flags.chroot && enter_chroot(j))
731 pdie("chroot");
732
Elly Jonese1749eb2011-10-07 13:54:59 -0400733 if (j->flags.readonly && remount_readonly())
734 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -0400735
Elly Jonese1749eb2011-10-07 13:54:59 -0400736 if (j->flags.caps) {
737 /* POSIX capabilities are a bit tricky. If we drop our
738 * capability to change uids, our attempt to use setuid()
739 * below will fail. Hang on to root caps across setuid(), then
740 * lock securebits.
741 */
742 if (prctl(PR_SET_KEEPCAPS, 1))
743 pdie("prctl(PR_SET_KEEPCAPS)");
744 if (prctl
745 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
746 pdie("prctl(PR_SET_SECUREBITS)");
747 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400748
Elly Jonese1749eb2011-10-07 13:54:59 -0400749 if (j->flags.usergroups) {
750 if (initgroups(j->user, j->usergid))
751 pdie("initgroups");
752 } else {
753 /* Only attempt to clear supplemental groups if we are changing
754 * users. */
755 if ((j->uid || j->gid) && setgroups(0, NULL))
756 pdie("setgroups");
757 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400758
Elly Jonese1749eb2011-10-07 13:54:59 -0400759 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
760 pdie("setresgid");
Elly Jonescd7a9042011-07-22 13:56:51 -0400761
Elly Jonese1749eb2011-10-07 13:54:59 -0400762 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
763 pdie("setresuid");
Elly Jonescd7a9042011-07-22 13:56:51 -0400764
Elly Jonese1749eb2011-10-07 13:54:59 -0400765 if (j->flags.caps)
766 drop_caps(j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400767
Elly Jonese1749eb2011-10-07 13:54:59 -0400768 /* seccomp has to come last since it cuts off all the other
769 * privilege-dropping syscalls :)
770 */
771 if (j->flags.seccomp_filter && prctl(PR_SET_SECCOMP, 13))
772 pdie("prctl(PR_SET_SECCOMP, 13)");
Will Drewry32ac9f52011-08-18 21:36:27 -0500773
Elly Jonese1749eb2011-10-07 13:54:59 -0400774 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1))
775 pdie("prctl(PR_SET_SECCOMP)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400776}
777
Will Drewry6ac91122011-10-21 16:38:58 -0500778/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -0400779static int init_exitstatus = 0;
780
Will Drewry6ac91122011-10-21 16:38:58 -0500781void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -0400782{
783 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -0400784}
785
Will Drewry6ac91122011-10-21 16:38:58 -0500786int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400787{
788 pid_t pid;
789 int status;
790 /* so that we exit with the right status */
791 signal(SIGTERM, init_term);
792 /* TODO(wad) self jail with seccomp_filters here. */
793 while ((pid = wait(&status)) > 0) {
794 /* This loop will only end when either there are no processes
795 * left inside our pid namespace or we get a signal.
796 */
797 if (pid == rootpid)
798 init_exitstatus = status;
799 }
800 if (!WIFEXITED(init_exitstatus))
801 _exit(MINIJAIL_ERR_INIT);
802 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -0400803}
804
Will Drewry6ac91122011-10-21 16:38:58 -0500805int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400806{
807 size_t sz = 0;
808 size_t bytes = read(fd, &sz, sizeof(sz));
809 char *buf;
810 int r;
811 if (sizeof(sz) != bytes)
812 return -EINVAL;
813 if (sz > USHRT_MAX) /* Arbitrary sanity check */
814 return -E2BIG;
815 buf = malloc(sz);
816 if (!buf)
817 return -ENOMEM;
818 bytes = read(fd, buf, sz);
819 if (bytes != sz) {
820 free(buf);
821 return -EINVAL;
822 }
823 r = minijail_unmarshal(j, buf, sz);
824 free(buf);
825 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500826}
827
Will Drewry6ac91122011-10-21 16:38:58 -0500828int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -0400829{
830 char *buf;
831 size_t sz = minijail_size(j);
832 ssize_t written;
833 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -0400834
Elly Jonese1749eb2011-10-07 13:54:59 -0400835 if (!sz)
836 return -EINVAL;
837 buf = malloc(sz);
838 r = minijail_marshal(j, buf, sz);
839 if (r) {
840 free(buf);
841 return r;
842 }
843 /* Sends [size][minijail]. */
844 written = write(fd, &sz, sizeof(sz));
845 if (written != sizeof(sz)) {
846 free(buf);
847 return -EFAULT;
848 }
849 written = write(fd, buf, sz);
850 if (written < 0 || (size_t) written != sz) {
851 free(buf);
852 return -EFAULT;
853 }
854 free(buf);
855 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500856}
Elly Jonescd7a9042011-07-22 13:56:51 -0400857
Will Drewry6ac91122011-10-21 16:38:58 -0500858int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400859{
860 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
861 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
862 if (!newenv)
863 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -0400864
Elly Jonese1749eb2011-10-07 13:54:59 -0400865 /* Only insert a separating space if we have something to separate... */
866 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
867 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -0400868
Elly Jonese1749eb2011-10-07 13:54:59 -0400869 /* setenv() makes a copy of the string we give it */
870 setenv(kLdPreloadEnvVar, newenv, 1);
871 free(newenv);
872 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400873}
874
Will Drewry6ac91122011-10-21 16:38:58 -0500875int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -0400876{
877 int r = pipe(fds);
878 char fd_buf[11];
879 if (r)
880 return r;
881 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
882 if (r <= 0)
883 return -EINVAL;
884 setenv(kFdEnvVar, fd_buf, 1);
885 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500886}
887
Will Drewry6ac91122011-10-21 16:38:58 -0500888int API minijail_run(struct minijail *j, const char *filename,
889 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -0400890{
891 unsigned int pidns = j->flags.pids ? CLONE_NEWPID : 0;
892 char *oldenv, *oldenv_copy = NULL;
893 pid_t child_pid;
894 int pipe_fds[2];
895 int ret;
Ben Chan541c7e52011-08-26 14:55:53 -0700896
Elly Jonese1749eb2011-10-07 13:54:59 -0400897 oldenv = getenv(kLdPreloadEnvVar);
898 if (oldenv) {
899 oldenv_copy = strdup(oldenv);
900 if (!oldenv_copy)
901 return -ENOMEM;
902 }
Will Drewryf89aef52011-09-16 16:48:57 -0500903
Elly Jonese1749eb2011-10-07 13:54:59 -0400904 if (setup_preload())
905 return -EFAULT;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500906
Elly Jonese1749eb2011-10-07 13:54:59 -0400907 /* Before we fork(2) and execve(2) the child process, we need to open
908 * a pipe(2) to send the minijail configuration over.
909 */
910 if (setup_pipe(pipe_fds))
911 return -EFAULT;
Elly Jonescd7a9042011-07-22 13:56:51 -0400912
Elly Jonese1749eb2011-10-07 13:54:59 -0400913 child_pid = syscall(SYS_clone, pidns | SIGCHLD, NULL);
914 if (child_pid < 0) {
915 free(oldenv_copy);
916 return child_pid;
917 }
Will Drewryf89aef52011-09-16 16:48:57 -0500918
Elly Jonese1749eb2011-10-07 13:54:59 -0400919 if (child_pid) {
920 /* Restore parent's LD_PRELOAD. */
921 if (oldenv_copy) {
922 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
923 free(oldenv_copy);
924 } else {
925 unsetenv(kLdPreloadEnvVar);
926 }
927 unsetenv(kFdEnvVar);
928 j->initpid = child_pid;
929 close(pipe_fds[0]); /* read endpoint */
930 ret = minijail_to_fd(j, pipe_fds[1]);
931 close(pipe_fds[1]); /* write endpoint */
932 if (ret) {
933 kill(j->initpid, SIGKILL);
934 die("failed to send marshalled minijail");
935 }
936 return 0;
937 }
938 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -0700939
Elly Jonese1749eb2011-10-07 13:54:59 -0400940 /* Drop everything that cannot be inherited across execve. */
941 minijail_preexec(j);
942 /* Jail this process and its descendants... */
943 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400944
Elly Jonese1749eb2011-10-07 13:54:59 -0400945 if (pidns) {
946 /* pid namespace: this process will become init inside the new
947 * namespace, so fork off a child to actually run the program
948 * (we don't want all programs we might exec to have to know
949 * how to be init).
950 */
951 child_pid = fork();
952 if (child_pid < 0)
953 _exit(child_pid);
954 else if (child_pid > 0)
955 init(child_pid); /* never returns */
956 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400957
Elly Jonese1749eb2011-10-07 13:54:59 -0400958 /* If we aren't pid-namespaced:
959 * calling process
960 * -> execve()-ing process
961 * If we are:
962 * calling process
963 * -> init()-ing process
964 * -> execve()-ing process
965 */
966 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -0400967}
968
Will Drewry6ac91122011-10-21 16:38:58 -0500969int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400970{
971 int st;
972 if (kill(j->initpid, SIGTERM))
973 return -errno;
974 if (waitpid(j->initpid, &st, 0) < 0)
975 return -errno;
976 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -0400977}
978
Will Drewry6ac91122011-10-21 16:38:58 -0500979int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400980{
981 int st;
982 if (waitpid(j->initpid, &st, 0) < 0)
983 return -errno;
984 if (!WIFEXITED(st))
985 return MINIJAIL_ERR_JAIL;
986 return WEXITSTATUS(st);
Elly Jonescd7a9042011-07-22 13:56:51 -0400987}
988
Will Drewry6ac91122011-10-21 16:38:58 -0500989void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400990{
991 struct seccomp_filter *f = j->filters;
992 /* Unlink the tail and head */
993 if (f)
994 f->prev->next = NULL;
995 while (f) {
996 struct seccomp_filter *next = f->next;
997 free(f->filter);
998 free(f);
999 f = next;
1000 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001001 while (j->bindings_head) {
1002 struct binding *b = j->bindings_head;
1003 j->bindings_head = j->bindings_head->next;
1004 free(b->dest);
1005 free(b->src);
1006 free(b);
1007 }
1008 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001009 if (j->user)
1010 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001011 if (j->chrootdir)
1012 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001013 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001014}