blob: 2c3d5b3e59677c29948e4856d349550371e39b15 [file] [log] [blame]
Elly Jonesdd3e8512012-01-23 15:13:38 -05001/*
2 * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04003 * Use of this source code is governed by a BSD-style license that can be
Will Drewry32ac9f52011-08-18 21:36:27 -05004 * found in the LICENSE file.
5 */
Elly Jonescd7a9042011-07-22 13:56:51 -04006
7#define _BSD_SOURCE
8#define _GNU_SOURCE
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07009
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080010#include <asm/unistd.h>
Will Drewry32ac9f52011-08-18 21:36:27 -050011#include <ctype.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040012#include <errno.h>
13#include <grp.h>
14#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050015#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040016#include <linux/capability.h>
17#include <linux/securebits.h>
18#include <pwd.h>
19#include <sched.h>
20#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050021#include <stdarg.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080022#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <syscall.h>
27#include <sys/capability.h>
28#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050029#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040030#include <sys/prctl.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080031#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040032#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040033#include <unistd.h>
34
35#include "libminijail.h"
36#include "libminijail-private.h"
37
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080038#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070039#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080040
Will Drewry32ac9f52011-08-18 21:36:27 -050041/* Until these are reliably available in linux/prctl.h */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080042#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070043# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080044#endif
45
46/* For seccomp_filter using BPF. */
47#ifndef PR_SET_NO_NEW_PRIVS
48# define PR_SET_NO_NEW_PRIVS 38
49#endif
50#ifndef SECCOMP_MODE_FILTER
51# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050052#endif
53
Elly Jones51a5b6c2011-10-12 19:09:26 -040054struct binding {
55 char *src;
56 char *dest;
57 int writeable;
58 struct binding *next;
59};
60
Will Drewryf89aef52011-09-16 16:48:57 -050061struct minijail {
Elly Jonese1749eb2011-10-07 13:54:59 -040062 struct {
63 int uid:1;
64 int gid:1;
65 int caps:1;
66 int vfs:1;
67 int pids:1;
68 int seccomp:1;
69 int readonly:1;
70 int usergroups:1;
71 int ptrace:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070072 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040073 int seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -040074 int chroot:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040075 } flags;
76 uid_t uid;
77 gid_t gid;
78 gid_t usergid;
79 char *user;
80 uint64_t caps;
81 pid_t initpid;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080082 int filter_len;
Elly Jones51a5b6c2011-10-12 19:09:26 -040083 int binding_count;
84 char *chrootdir;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080085 struct sock_fprog *filter_prog;
Elly Jones51a5b6c2011-10-12 19:09:26 -040086 struct binding *bindings_head;
87 struct binding *bindings_tail;
Will Drewryf89aef52011-09-16 16:48:57 -050088};
89
Will Drewry6ac91122011-10-21 16:38:58 -050090struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -040091{
Elly Jones51a5b6c2011-10-12 19:09:26 -040092 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -040093}
94
Will Drewry6ac91122011-10-21 16:38:58 -050095void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -040096{
97 if (uid == 0)
98 die("useless change to uid 0");
99 j->uid = uid;
100 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400101}
102
Will Drewry6ac91122011-10-21 16:38:58 -0500103void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400104{
105 if (gid == 0)
106 die("useless change to gid 0");
107 j->gid = gid;
108 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400109}
110
Will Drewry6ac91122011-10-21 16:38:58 -0500111int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400112{
113 char *buf = NULL;
114 struct passwd pw;
115 struct passwd *ppw = NULL;
116 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
117 if (sz == -1)
118 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400119
Elly Jonesdd3e8512012-01-23 15:13:38 -0500120 /*
121 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400122 * the maximum needed size of the buffer, so we don't have to search.
123 */
124 buf = malloc(sz);
125 if (!buf)
126 return -ENOMEM;
127 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500128 /*
129 * We're safe to free the buffer here. The strings inside pw point
130 * inside buf, but we don't use any of them; this leaves the pointers
131 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
132 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400133 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 Jonesdd3e8512012-01-23 15:13:38 -0500153 /*
154 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400155 * the maximum needed size of the buffer, so we don't have to search.
156 */
157 buf = malloc(sz);
158 if (!buf)
159 return -ENOMEM;
160 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500161 /*
162 * We're safe to free the buffer here. The strings inside gr point
163 * inside buf, but we don't use any of them; this leaves the pointers
164 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
165 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400166 free(buf);
167 if (!pgr)
168 return -errno;
169 minijail_change_gid(j, pgr->gr_gid);
170 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400171}
172
Will Drewry6ac91122011-10-21 16:38:58 -0500173void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400174{
175 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400176}
177
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700178void API minijail_no_new_privs(struct minijail *j)
179{
180 j->flags.no_new_privs = 1;
181}
182
Will Drewry6ac91122011-10-21 16:38:58 -0500183void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400184{
185 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500186}
187
Will Drewry6ac91122011-10-21 16:38:58 -0500188void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400189{
190 j->caps = capmask;
191 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400192}
193
Will Drewry6ac91122011-10-21 16:38:58 -0500194void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400195{
196 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400197}
198
Will Drewry6ac91122011-10-21 16:38:58 -0500199void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400200{
Elly Jonese58176c2012-01-23 11:46:17 -0500201 j->flags.vfs = 1;
202 j->flags.readonly = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400203 j->flags.pids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400204}
205
Will Drewry6ac91122011-10-21 16:38:58 -0500206void API minijail_remount_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400207{
208 j->flags.vfs = 1;
209 j->flags.readonly = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400210}
211
Will Drewry6ac91122011-10-21 16:38:58 -0500212void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400213{
214 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400215}
216
Will Drewry6ac91122011-10-21 16:38:58 -0500217void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400218{
219 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400220}
221
Will Drewry6ac91122011-10-21 16:38:58 -0500222int API minijail_enter_chroot(struct minijail *j, const char *dir) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400223 if (j->chrootdir)
224 return -EINVAL;
225 j->chrootdir = strdup(dir);
226 if (!j->chrootdir)
227 return -ENOMEM;
228 j->flags.chroot = 1;
229 return 0;
230}
231
Will Drewry6ac91122011-10-21 16:38:58 -0500232int API minijail_bind(struct minijail *j, const char *src, const char *dest,
233 int writeable) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400234 struct binding *b;
235
236 if (*dest != '/')
237 return -EINVAL;
238 b = calloc(1, sizeof(*b));
239 if (!b)
240 return -ENOMEM;
241 b->dest = strdup(dest);
242 if (!b->dest)
243 goto error;
244 b->src = strdup(src);
245 if (!b->src)
246 goto error;
247 b->writeable = writeable;
248
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700249 info("bind %s -> %s", src, dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400250
Elly Jonesdd3e8512012-01-23 15:13:38 -0500251 /*
252 * Force vfs namespacing so the bind mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400253 * containing vfs namespace.
254 */
255 minijail_namespace_vfs(j);
256
257 if (j->bindings_tail)
258 j->bindings_tail->next = b;
259 else
260 j->bindings_head = b;
261 j->bindings_tail = b;
262 j->binding_count++;
263
264 return 0;
265
266error:
267 free(b->src);
268 free(b->dest);
269 free(b);
270 return -ENOMEM;
271}
272
Will Drewry6ac91122011-10-21 16:38:58 -0500273void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400274{
275 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800276 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700277 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400278 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800279
280 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
281 if (compile_filter(file, fprog)) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700282 die("failed to compile seccomp filter BPF program in '%s'", path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800283 }
284
285 j->filter_len = fprog->len;
286 j->filter_prog = fprog;
287
Elly Jonese1749eb2011-10-07 13:54:59 -0400288 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500289}
290
Will Drewryf89aef52011-09-16 16:48:57 -0500291struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400292 size_t available;
293 size_t total;
294 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500295};
296
Will Drewry6ac91122011-10-21 16:38:58 -0500297void marshal_state_init(struct marshal_state *state,
298 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400299{
300 state->available = available;
301 state->buf = buf;
302 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500303}
304
Will Drewry6ac91122011-10-21 16:38:58 -0500305void marshal_append(struct marshal_state *state,
306 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400307{
308 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500309
Elly Jonese1749eb2011-10-07 13:54:59 -0400310 /* Up to |available| will be written. */
311 if (copy_len) {
312 memcpy(state->buf, src, copy_len);
313 state->buf += copy_len;
314 state->available -= copy_len;
315 }
316 /* |total| will contain the expected length. */
317 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500318}
319
Will Drewry6ac91122011-10-21 16:38:58 -0500320void minijail_marshal_helper(struct marshal_state *state,
321 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400322{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400323 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400324 marshal_append(state, (char *)j, sizeof(*j));
325 if (j->user)
326 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400327 if (j->chrootdir)
328 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800329 if (j->flags.seccomp_filter && j->filter_prog) {
330 struct sock_fprog *fp = j->filter_prog;
331 marshal_append(state, (char *)fp->filter,
332 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400333 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400334 for (b = j->bindings_head; b; b = b->next) {
335 marshal_append(state, b->src, strlen(b->src) + 1);
336 marshal_append(state, b->dest, strlen(b->dest) + 1);
337 marshal_append(state, (char *)&b->writeable, sizeof(b->writeable));
338 }
Will Drewryf89aef52011-09-16 16:48:57 -0500339}
340
Will Drewry6ac91122011-10-21 16:38:58 -0500341size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400342{
343 struct marshal_state state;
344 marshal_state_init(&state, NULL, 0);
345 minijail_marshal_helper(&state, j);
346 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500347}
348
Elly Jonese1749eb2011-10-07 13:54:59 -0400349int minijail_marshal(const struct minijail *j, char *buf, size_t available)
350{
351 struct marshal_state state;
352 marshal_state_init(&state, buf, available);
353 minijail_marshal_helper(&state, j);
354 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500355}
356
Elly Jones51a5b6c2011-10-12 19:09:26 -0400357/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
358 * @length Number of bytes to consume
359 * @buf Buffer to consume from
360 * @buflength Size of @buf
361 *
362 * Returns a pointer to the base of the bytes, or NULL for errors.
363 */
Will Drewry6ac91122011-10-21 16:38:58 -0500364void *consumebytes(size_t length, char **buf, size_t *buflength) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400365 char *p = *buf;
366 if (length > *buflength)
367 return NULL;
368 *buf += length;
369 *buflength -= length;
370 return p;
371}
372
373/* consumestr: consumes a C string from a buffer @buf of length @length
374 * @buf Buffer to consume
375 * @length Length of buffer
376 *
377 * Returns a pointer to the base of the string, or NULL for errors.
378 */
Will Drewry6ac91122011-10-21 16:38:58 -0500379char *consumestr(char **buf, size_t *buflength) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400380 size_t len = strnlen(*buf, *buflength);
381 if (len == *buflength)
382 /* There's no null-terminator */
383 return NULL;
384 return consumebytes(len + 1, buf, buflength);
385}
386
Elly Jonese1749eb2011-10-07 13:54:59 -0400387int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
388{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400389 int i;
390 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500391 int ret = -EINVAL;
392
Elly Jonese1749eb2011-10-07 13:54:59 -0400393 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500394 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400395 memcpy((void *)j, serialized, sizeof(*j));
396 serialized += sizeof(*j);
397 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500398
Will Drewrybee7ba72011-10-21 20:47:01 -0500399 /* Potentially stale pointers not used as signals. */
400 j->bindings_head = NULL;
401 j->bindings_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800402 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500403
Elly Jonese1749eb2011-10-07 13:54:59 -0400404 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400405 char *user = consumestr(&serialized, &length);
406 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500407 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400408 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500409 if (!j->user)
410 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400411 }
Will Drewryf89aef52011-09-16 16:48:57 -0500412
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400413 if (j->chrootdir) { /* stale pointer */
414 char *chrootdir = consumestr(&serialized, &length);
415 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500416 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400417 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500418 if (!j->chrootdir)
419 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400420 }
421
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800422 if (j->flags.seccomp_filter && j->filter_len > 0) {
423 size_t ninstrs = j->filter_len;
424 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
425 ninstrs > USHRT_MAX)
426 goto bad_filters;
427
428 size_t program_len = ninstrs * sizeof(struct sock_filter);
429 void *program = consumebytes(program_len, &serialized, &length);
430 if (!program)
431 goto bad_filters;
432
433 j->filter_prog = malloc(sizeof(struct sock_fprog));
434 j->filter_prog->len = ninstrs;
435 j->filter_prog->filter = malloc(program_len);
436 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400437 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400438
439 count = j->binding_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400440 j->binding_count = 0;
441 for (i = 0; i < count; ++i) {
442 int *writeable;
443 const char *dest;
444 const char *src = consumestr(&serialized, &length);
445 if (!src)
Will Drewrybee7ba72011-10-21 20:47:01 -0500446 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400447 dest = consumestr(&serialized, &length);
448 if (!dest)
Will Drewrybee7ba72011-10-21 20:47:01 -0500449 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400450 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
451 if (!writeable)
Will Drewrybee7ba72011-10-21 20:47:01 -0500452 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400453 if (minijail_bind(j, src, dest, *writeable))
Will Drewrybee7ba72011-10-21 20:47:01 -0500454 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400455 }
456
Elly Jonese1749eb2011-10-07 13:54:59 -0400457 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500458
459bad_bindings:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800460 if (j->flags.seccomp_filter && j->filter_len > 0) {
461 free(j->filter_prog->filter);
462 free(j->filter_prog);
463 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500464bad_filters:
465 if (j->chrootdir)
466 free(j->chrootdir);
467bad_chrootdir:
468 if (j->user)
469 free(j->user);
470clear_pointers:
471 j->user = NULL;
472 j->chrootdir = NULL;
473out:
474 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500475}
476
Elly Jonese1749eb2011-10-07 13:54:59 -0400477void minijail_preenter(struct minijail *j)
478{
479 /* Strip out options which are minijail_run() only. */
480 j->flags.vfs = 0;
481 j->flags.readonly = 0;
482 j->flags.pids = 0;
Will Drewryfe4a3722011-09-16 14:50:50 -0500483}
484
Elly Jonese1749eb2011-10-07 13:54:59 -0400485void minijail_preexec(struct minijail *j)
486{
487 int vfs = j->flags.vfs;
488 int readonly = j->flags.readonly;
489 if (j->user)
490 free(j->user);
491 j->user = NULL;
492 memset(&j->flags, 0, sizeof(j->flags));
493 /* Now restore anything we meant to keep. */
494 j->flags.vfs = vfs;
495 j->flags.readonly = readonly;
496 /* Note, pidns will already have been used before this call. */
Will Drewry2ddaad02011-09-16 11:36:08 -0500497}
498
Elly Jones51a5b6c2011-10-12 19:09:26 -0400499/* bind_one: Applies bindings from @b for @j, recursing as needed.
500 * @j Minijail these bindings are for
501 * @b Head of list of bindings
502 *
503 * Returns 0 for success.
504 */
Will Drewry6ac91122011-10-21 16:38:58 -0500505int bind_one(const struct minijail *j, struct binding *b) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400506 int ret = 0;
507 char *dest = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400508 if (ret)
509 return ret;
510 /* dest has a leading "/" */
511 if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0)
512 return -ENOMEM;
Elly Jonesa1059632011-12-15 15:17:07 -0500513 ret = mount(b->src, dest, NULL, MS_BIND, NULL);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400514 if (ret)
515 pdie("bind: %s -> %s", b->src, dest);
Elly Jonesa1059632011-12-15 15:17:07 -0500516 if (!b->writeable) {
517 ret = mount(b->src, dest, NULL,
518 MS_BIND | MS_REMOUNT | MS_RDONLY, NULL);
519 if (ret)
520 pdie("bind ro: %s -> %s", b->src, dest);
521 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400522 free(dest);
523 if (b->next)
524 return bind_one(j, b->next);
525 return ret;
526}
527
Will Drewry6ac91122011-10-21 16:38:58 -0500528int enter_chroot(const struct minijail *j) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400529 int ret;
530 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
531 return ret;
532
533 if (chroot(j->chrootdir))
534 return -errno;
535
536 if (chdir("/"))
537 return -errno;
538
539 return 0;
540}
541
Will Drewry6ac91122011-10-21 16:38:58 -0500542int remount_readonly(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400543{
544 const char *kProcPath = "/proc";
545 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500546 /*
547 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400548 * /proc in our namespace, which means using MS_REMOUNT here would
549 * mutate our parent's mount as well, even though we're in a VFS
550 * namespace (!). Instead, remove their mount from our namespace
551 * and make our own.
552 */
553 if (umount(kProcPath))
554 return -errno;
555 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
556 return -errno;
557 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400558}
559
Will Drewry6ac91122011-10-21 16:38:58 -0500560void drop_caps(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400561{
562 cap_t caps = cap_get_proc();
563 cap_value_t raise_flag[1];
564 unsigned int i;
565 if (!caps)
566 die("can't get process caps");
567 if (cap_clear_flag(caps, CAP_INHERITABLE))
568 die("can't clear inheritable caps");
569 if (cap_clear_flag(caps, CAP_EFFECTIVE))
570 die("can't clear effective caps");
571 if (cap_clear_flag(caps, CAP_PERMITTED))
572 die("can't clear permitted caps");
573 for (i = 0; i < sizeof(j->caps) * 8 && cap_valid((int)i); ++i) {
574 if (i != CAP_SETPCAP && !(j->caps & (1 << i)))
575 continue;
576 raise_flag[0] = i;
577 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, raise_flag, CAP_SET))
578 die("can't add effective cap");
579 if (cap_set_flag(caps, CAP_PERMITTED, 1, raise_flag, CAP_SET))
580 die("can't add permitted cap");
581 if (cap_set_flag(caps, CAP_INHERITABLE, 1, raise_flag, CAP_SET))
582 die("can't add inheritable cap");
583 }
584 if (cap_set_proc(caps))
585 die("can't apply cleaned capset");
586 cap_free(caps);
587 for (i = 0; i < sizeof(j->caps) * 8 && cap_valid((int)i); ++i) {
588 if (j->caps & (1 << i))
589 continue;
590 if (prctl(PR_CAPBSET_DROP, i))
591 pdie("prctl(PR_CAPBSET_DROP)");
592 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400593}
594
Will Drewry6ac91122011-10-21 16:38:58 -0500595void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400596{
597 if (j->flags.pids)
598 die("tried to enter a pid-namespaced jail;"
599 "try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400600
Elly Jonese1749eb2011-10-07 13:54:59 -0400601 if (j->flags.usergroups && !j->user)
602 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400603
Elly Jonesdd3e8512012-01-23 15:13:38 -0500604 /*
605 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -0400606 * so we don't even try. If any of our operations fail, we abort() the
607 * entire process.
608 */
609 if (j->flags.vfs && unshare(CLONE_NEWNS))
610 pdie("unshare");
Elly Jonescd7a9042011-07-22 13:56:51 -0400611
Elly Jones51a5b6c2011-10-12 19:09:26 -0400612 if (j->flags.chroot && enter_chroot(j))
613 pdie("chroot");
614
Elly Jonese1749eb2011-10-07 13:54:59 -0400615 if (j->flags.readonly && remount_readonly())
616 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -0400617
Elly Jonese1749eb2011-10-07 13:54:59 -0400618 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500619 /*
620 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -0400621 * capability to change uids, our attempt to use setuid()
622 * below will fail. Hang on to root caps across setuid(), then
623 * lock securebits.
624 */
625 if (prctl(PR_SET_KEEPCAPS, 1))
626 pdie("prctl(PR_SET_KEEPCAPS)");
627 if (prctl
628 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
629 pdie("prctl(PR_SET_SECUREBITS)");
630 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400631
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700632 /*
Elly Jones1c888ae2012-07-31 12:23:47 -0400633 * Set no_new_privs before installing seccomp filter. See
634 * </kernel/seccomp.c> and </kernel/sys.c> in the kernel source tree for
635 * an explanation of the parameters.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700636 */
637 if (j->flags.no_new_privs) {
638 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
639 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
640 }
641
642 /*
643 * Install seccomp filter before dropping root and caps.
644 * WARNING: this means that filter policies *must* allow
645 * setgroups()/setresgid()/setresuid() for dropping root and
646 * capget()/capset()/prctl() for dropping caps.
647 */
648 if (j->flags.seccomp_filter) {
649 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog))
650 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
651 }
652
Elly Jonese1749eb2011-10-07 13:54:59 -0400653 if (j->flags.usergroups) {
654 if (initgroups(j->user, j->usergid))
655 pdie("initgroups");
656 } else {
657 /* Only attempt to clear supplemental groups if we are changing
658 * users. */
659 if ((j->uid || j->gid) && setgroups(0, NULL))
660 pdie("setgroups");
661 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400662
Elly Jonese1749eb2011-10-07 13:54:59 -0400663 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
664 pdie("setresgid");
Elly Jonescd7a9042011-07-22 13:56:51 -0400665
Elly Jonese1749eb2011-10-07 13:54:59 -0400666 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
667 pdie("setresuid");
Elly Jonescd7a9042011-07-22 13:56:51 -0400668
Elly Jonese1749eb2011-10-07 13:54:59 -0400669 if (j->flags.caps)
670 drop_caps(j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400671
Elly Jonesdd3e8512012-01-23 15:13:38 -0500672 /*
673 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -0400674 * privilege-dropping syscalls :)
675 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400676 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1))
677 pdie("prctl(PR_SET_SECCOMP)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400678}
679
Will Drewry6ac91122011-10-21 16:38:58 -0500680/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -0400681static int init_exitstatus = 0;
682
Will Drewry6ac91122011-10-21 16:38:58 -0500683void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -0400684{
685 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -0400686}
687
Will Drewry6ac91122011-10-21 16:38:58 -0500688int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400689{
690 pid_t pid;
691 int status;
692 /* so that we exit with the right status */
693 signal(SIGTERM, init_term);
694 /* TODO(wad) self jail with seccomp_filters here. */
695 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500696 /*
697 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -0400698 * left inside our pid namespace or we get a signal.
699 */
700 if (pid == rootpid)
701 init_exitstatus = status;
702 }
703 if (!WIFEXITED(init_exitstatus))
704 _exit(MINIJAIL_ERR_INIT);
705 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -0400706}
707
Will Drewry6ac91122011-10-21 16:38:58 -0500708int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400709{
710 size_t sz = 0;
711 size_t bytes = read(fd, &sz, sizeof(sz));
712 char *buf;
713 int r;
714 if (sizeof(sz) != bytes)
715 return -EINVAL;
716 if (sz > USHRT_MAX) /* Arbitrary sanity check */
717 return -E2BIG;
718 buf = malloc(sz);
719 if (!buf)
720 return -ENOMEM;
721 bytes = read(fd, buf, sz);
722 if (bytes != sz) {
723 free(buf);
724 return -EINVAL;
725 }
726 r = minijail_unmarshal(j, buf, sz);
727 free(buf);
728 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500729}
730
Will Drewry6ac91122011-10-21 16:38:58 -0500731int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -0400732{
733 char *buf;
734 size_t sz = minijail_size(j);
735 ssize_t written;
736 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -0400737
Elly Jonese1749eb2011-10-07 13:54:59 -0400738 if (!sz)
739 return -EINVAL;
740 buf = malloc(sz);
741 r = minijail_marshal(j, buf, sz);
742 if (r) {
743 free(buf);
744 return r;
745 }
746 /* Sends [size][minijail]. */
747 written = write(fd, &sz, sizeof(sz));
748 if (written != sizeof(sz)) {
749 free(buf);
750 return -EFAULT;
751 }
752 written = write(fd, buf, sz);
753 if (written < 0 || (size_t) written != sz) {
754 free(buf);
755 return -EFAULT;
756 }
757 free(buf);
758 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500759}
Elly Jonescd7a9042011-07-22 13:56:51 -0400760
Will Drewry6ac91122011-10-21 16:38:58 -0500761int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400762{
763 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
764 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
765 if (!newenv)
766 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -0400767
Elly Jonese1749eb2011-10-07 13:54:59 -0400768 /* Only insert a separating space if we have something to separate... */
769 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
770 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -0400771
Elly Jonese1749eb2011-10-07 13:54:59 -0400772 /* setenv() makes a copy of the string we give it */
773 setenv(kLdPreloadEnvVar, newenv, 1);
774 free(newenv);
775 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400776}
777
Will Drewry6ac91122011-10-21 16:38:58 -0500778int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -0400779{
780 int r = pipe(fds);
781 char fd_buf[11];
782 if (r)
783 return r;
784 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
785 if (r <= 0)
786 return -EINVAL;
787 setenv(kFdEnvVar, fd_buf, 1);
788 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500789}
790
Will Drewry6ac91122011-10-21 16:38:58 -0500791int API minijail_run(struct minijail *j, const char *filename,
792 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -0400793{
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -0700794 return minijail_run_pid(j, filename, argv, NULL);
795}
796
797int API minijail_run_pid(struct minijail *j, const char *filename,
798 char *const argv[], pid_t *pchild_pid)
799{
Elly Jonese1749eb2011-10-07 13:54:59 -0400800 char *oldenv, *oldenv_copy = NULL;
801 pid_t child_pid;
802 int pipe_fds[2];
803 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -0400804 /* We need to remember this across the minijail_preexec() call. */
805 int pid_namespace = j->flags.pids;
Ben Chan541c7e52011-08-26 14:55:53 -0700806
Elly Jonese1749eb2011-10-07 13:54:59 -0400807 oldenv = getenv(kLdPreloadEnvVar);
808 if (oldenv) {
809 oldenv_copy = strdup(oldenv);
810 if (!oldenv_copy)
811 return -ENOMEM;
812 }
Will Drewryf89aef52011-09-16 16:48:57 -0500813
Elly Jonese1749eb2011-10-07 13:54:59 -0400814 if (setup_preload())
815 return -EFAULT;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500816
Elly Jonesdd3e8512012-01-23 15:13:38 -0500817 /*
818 * Before we fork(2) and execve(2) the child process, we need to open
Elly Jonese1749eb2011-10-07 13:54:59 -0400819 * a pipe(2) to send the minijail configuration over.
820 */
821 if (setup_pipe(pipe_fds))
822 return -EFAULT;
Elly Jonescd7a9042011-07-22 13:56:51 -0400823
Elly Jones761b7412012-06-13 15:49:52 -0400824 /* Use sys_clone() if and only if we're creating a pid namespace.
825 *
826 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
827 *
828 * In multithreaded programs, there are a bunch of locks inside libc,
829 * some of which may be held by other threads at the time that we call
830 * minijail_run_pid(). If we call fork(), glibc does its level best to
831 * ensure that we hold all of these locks before it calls clone()
832 * internally and drop them after clone() returns, but when we call
833 * sys_clone(2) directly, all that gets bypassed and we end up with a
834 * child address space where some of libc's important locks are held by
835 * other threads (which did not get cloned, and hence will never release
836 * those locks). This is okay so long as we call exec() immediately
837 * after, but a bunch of seemingly-innocent libc functions like setenv()
838 * take locks.
839 *
840 * Hence, only call sys_clone() if we need to, in order to get at pid
841 * namespacing. If we follow this path, the child's address space might
842 * have broken locks; you may only call functions that do not acquire
843 * any locks.
844 *
845 * Unfortunately, fork() acquires every lock it can get its hands on, as
846 * previously detailed, so this function is highly likely to deadlock
847 * later on (see "deadlock here") if we're multithreaded.
848 *
849 * We might hack around this by having the clone()d child (init of the
850 * pid namespace) return directly, rather than leaving the clone()d
851 * process hanging around to be init for the new namespace (and having
852 * its fork()ed child return in turn), but that process would be crippled
853 * with its libc locks potentially broken. We might try fork()ing in the
854 * parent before we clone() to ensure that we own all the locks, but
855 * then we have to have the forked child hanging around consuming
856 * resources (and possibly having file descriptors / shared memory
857 * regions / etc attached). We'd need to keep the child around to avoid
858 * having its children get reparented to init.
859 *
860 * TODO(ellyjones): figure out if the "forked child hanging around"
861 * problem is fixable or not. It would be nice if we worked in this
862 * case.
863 */
Elly Jonesa05d7bb2012-06-14 14:09:27 -0400864 if (pid_namespace)
Elly Jones761b7412012-06-13 15:49:52 -0400865 child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL);
866 else
867 child_pid = fork();
868
Elly Jonese1749eb2011-10-07 13:54:59 -0400869 if (child_pid < 0) {
870 free(oldenv_copy);
871 return child_pid;
872 }
Will Drewryf89aef52011-09-16 16:48:57 -0500873
Elly Jonese1749eb2011-10-07 13:54:59 -0400874 if (child_pid) {
875 /* Restore parent's LD_PRELOAD. */
876 if (oldenv_copy) {
877 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
878 free(oldenv_copy);
879 } else {
880 unsetenv(kLdPreloadEnvVar);
881 }
882 unsetenv(kFdEnvVar);
883 j->initpid = child_pid;
884 close(pipe_fds[0]); /* read endpoint */
885 ret = minijail_to_fd(j, pipe_fds[1]);
886 close(pipe_fds[1]); /* write endpoint */
887 if (ret) {
888 kill(j->initpid, SIGKILL);
889 die("failed to send marshalled minijail");
890 }
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -0700891 if (pchild_pid)
892 *pchild_pid = child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -0400893 return 0;
894 }
895 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -0700896
Elly Jonese1749eb2011-10-07 13:54:59 -0400897 /* Drop everything that cannot be inherited across execve. */
898 minijail_preexec(j);
899 /* Jail this process and its descendants... */
900 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400901
Elly Jonesa05d7bb2012-06-14 14:09:27 -0400902 if (pid_namespace) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500903 /*
904 * pid namespace: this process will become init inside the new
Elly Jonese1749eb2011-10-07 13:54:59 -0400905 * namespace, so fork off a child to actually run the program
906 * (we don't want all programs we might exec to have to know
907 * how to be init).
Elly Jones761b7412012-06-13 15:49:52 -0400908 *
909 * If we're multithreaded, we'll probably deadlock here. See
910 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -0400911 */
912 child_pid = fork();
913 if (child_pid < 0)
914 _exit(child_pid);
915 else if (child_pid > 0)
916 init(child_pid); /* never returns */
917 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400918
Elly Jonesdd3e8512012-01-23 15:13:38 -0500919 /*
920 * If we aren't pid-namespaced:
Elly Jonese1749eb2011-10-07 13:54:59 -0400921 * calling process
922 * -> execve()-ing process
923 * If we are:
924 * calling process
925 * -> init()-ing process
926 * -> execve()-ing process
927 */
928 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -0400929}
930
Will Drewry6ac91122011-10-21 16:38:58 -0500931int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400932{
933 int st;
934 if (kill(j->initpid, SIGTERM))
935 return -errno;
936 if (waitpid(j->initpid, &st, 0) < 0)
937 return -errno;
938 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -0400939}
940
Will Drewry6ac91122011-10-21 16:38:58 -0500941int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400942{
943 int st;
944 if (waitpid(j->initpid, &st, 0) < 0)
945 return -errno;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700946 if (!WIFEXITED(st)) {
947 if (WIFSIGNALED(st))
948 warn("child process received signal %d", WTERMSIG(st));
Elly Jonese1749eb2011-10-07 13:54:59 -0400949 return MINIJAIL_ERR_JAIL;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700950 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400951 return WEXITSTATUS(st);
Elly Jonescd7a9042011-07-22 13:56:51 -0400952}
953
Will Drewry6ac91122011-10-21 16:38:58 -0500954void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400955{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800956 if (j->flags.seccomp_filter && j->filter_prog) {
957 free(j->filter_prog->filter);
958 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -0400959 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400960 while (j->bindings_head) {
961 struct binding *b = j->bindings_head;
962 j->bindings_head = j->bindings_head->next;
963 free(b->dest);
964 free(b->src);
965 free(b);
966 }
967 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400968 if (j->user)
969 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500970 if (j->chrootdir)
971 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -0400972 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400973}