blob: 2e625d60b138fb2ffe04db6e66dfecb9990caca4 [file] [log] [blame]
Jorge Lucangeli Obesd613ab22015-03-03 14:22:50 -08001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04002 * 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
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07008
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08009#include <asm/unistd.h>
Will Drewry32ac9f52011-08-18 21:36:27 -050010#include <ctype.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040011#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070012#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040013#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>
Elly Jonescd7a9042011-07-22 13:56:51 -040017#include <pwd.h>
18#include <sched.h>
19#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050020#include <stdarg.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080021#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <syscall.h>
26#include <sys/capability.h>
27#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050028#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040029#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070030#include <sys/stat.h>
31#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080032#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040033#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040034#include <unistd.h>
35
36#include "libminijail.h"
37#include "libminijail-private.h"
38
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070039#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080040#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070041#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080042
Lei Zhangeee31552012-10-17 21:27:10 -070043#ifdef HAVE_SECUREBITS_H
44#include <linux/securebits.h>
45#else
46#define SECURE_ALL_BITS 0x15
47#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
48#endif
49
Will Drewry32ac9f52011-08-18 21:36:27 -050050/* Until these are reliably available in linux/prctl.h */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080051#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070052# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080053#endif
54
55/* For seccomp_filter using BPF. */
56#ifndef PR_SET_NO_NEW_PRIVS
57# define PR_SET_NO_NEW_PRIVS 38
58#endif
59#ifndef SECCOMP_MODE_FILTER
60# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050061#endif
62
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070063#ifdef USE_SECCOMP_SOFTFAIL
64# define SECCOMP_SOFTFAIL 1
65#else
66# define SECCOMP_SOFTFAIL 0
67#endif
68
Elly Jones51a5b6c2011-10-12 19:09:26 -040069struct binding {
70 char *src;
71 char *dest;
72 int writeable;
73 struct binding *next;
74};
75
Will Drewryf89aef52011-09-16 16:48:57 -050076struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070077 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070078 * WARNING: if you add a flag here you need to make sure it's
79 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070080 */
Elly Jonese1749eb2011-10-07 13:54:59 -040081 struct {
82 int uid:1;
83 int gid:1;
84 int caps:1;
85 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070086 int enter_vfs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040087 int pids:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -040088 int net:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040089 int seccomp:1;
90 int readonly:1;
91 int usergroups:1;
92 int ptrace:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070093 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040094 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070095 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -040096 int chroot:1;
Lee Campbell11af0622014-05-22 12:36:04 -070097 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +080098 int do_init:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040099 } flags;
100 uid_t uid;
101 gid_t gid;
102 gid_t usergid;
103 char *user;
104 uint64_t caps;
105 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700106 int mountns_fd;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800107 int filter_len;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400108 int binding_count;
109 char *chrootdir;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800110 struct sock_fprog *filter_prog;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400111 struct binding *bindings_head;
112 struct binding *bindings_tail;
Will Drewryf89aef52011-09-16 16:48:57 -0500113};
114
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700115/*
116 * Strip out flags meant for the parent.
117 * We keep things that are not inherited across execve(2) (e.g. capabilities),
118 * or are easier to set after execve(2) (e.g. seccomp filters).
119 */
120void minijail_preenter(struct minijail *j)
121{
122 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700123 j->flags.enter_vfs = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700124 j->flags.readonly = 0;
125 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800126 j->flags.do_init = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700127}
128
129/*
130 * Strip out flags meant for the child.
131 * We keep things that are inherited across execve(2).
132 */
133void minijail_preexec(struct minijail *j)
134{
135 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700136 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700137 int readonly = j->flags.readonly;
138 if (j->user)
139 free(j->user);
140 j->user = NULL;
141 memset(&j->flags, 0, sizeof(j->flags));
142 /* Now restore anything we meant to keep. */
143 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700144 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700145 j->flags.readonly = readonly;
146 /* Note, |pids| will already have been used before this call. */
147}
148
149/* Minijail API. */
150
Will Drewry6ac91122011-10-21 16:38:58 -0500151struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400152{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400153 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400154}
155
Will Drewry6ac91122011-10-21 16:38:58 -0500156void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400157{
158 if (uid == 0)
159 die("useless change to uid 0");
160 j->uid = uid;
161 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400162}
163
Will Drewry6ac91122011-10-21 16:38:58 -0500164void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400165{
166 if (gid == 0)
167 die("useless change to gid 0");
168 j->gid = gid;
169 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400170}
171
Will Drewry6ac91122011-10-21 16:38:58 -0500172int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400173{
174 char *buf = NULL;
175 struct passwd pw;
176 struct passwd *ppw = NULL;
177 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
178 if (sz == -1)
179 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400180
Elly Jonesdd3e8512012-01-23 15:13:38 -0500181 /*
182 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400183 * the maximum needed size of the buffer, so we don't have to search.
184 */
185 buf = malloc(sz);
186 if (!buf)
187 return -ENOMEM;
188 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500189 /*
190 * We're safe to free the buffer here. The strings inside pw point
191 * inside buf, but we don't use any of them; this leaves the pointers
192 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
193 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400194 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700195 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400196 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700197 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400198 minijail_change_uid(j, ppw->pw_uid);
199 j->user = strdup(user);
200 if (!j->user)
201 return -ENOMEM;
202 j->usergid = ppw->pw_gid;
203 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400204}
205
Will Drewry6ac91122011-10-21 16:38:58 -0500206int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400207{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700208 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700209 struct group gr;
210 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400211 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
212 if (sz == -1)
213 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400214
Elly Jonesdd3e8512012-01-23 15:13:38 -0500215 /*
216 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400217 * the maximum needed size of the buffer, so we don't have to search.
218 */
219 buf = malloc(sz);
220 if (!buf)
221 return -ENOMEM;
222 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500223 /*
224 * We're safe to free the buffer here. The strings inside gr point
225 * inside buf, but we don't use any of them; this leaves the pointers
226 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
227 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400228 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700229 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400230 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700231 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400232 minijail_change_gid(j, pgr->gr_gid);
233 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400234}
235
Will Drewry6ac91122011-10-21 16:38:58 -0500236void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400237{
238 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400239}
240
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700241void API minijail_no_new_privs(struct minijail *j)
242{
243 j->flags.no_new_privs = 1;
244}
245
Will Drewry6ac91122011-10-21 16:38:58 -0500246void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400247{
248 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500249}
250
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700251void API minijail_log_seccomp_filter_failures(struct minijail *j)
252{
253 j->flags.log_seccomp_filter = 1;
254}
255
Will Drewry6ac91122011-10-21 16:38:58 -0500256void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400257{
258 j->caps = capmask;
259 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400260}
261
Will Drewry6ac91122011-10-21 16:38:58 -0500262void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400263{
264 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400265}
266
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700267void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
268{
269 int ns_fd = open(ns_path, O_RDONLY);
270 if (ns_fd < 0) {
271 pdie("failed to open namespace '%s'", ns_path);
272 }
273 j->mountns_fd = ns_fd;
274 j->flags.enter_vfs = 1;
275}
276
Will Drewry6ac91122011-10-21 16:38:58 -0500277void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400278{
Elly Jonese58176c2012-01-23 11:46:17 -0500279 j->flags.vfs = 1;
280 j->flags.readonly = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400281 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800282 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400283}
284
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400285void API minijail_namespace_net(struct minijail *j)
286{
287 j->flags.net = 1;
288}
289
Will Drewry6ac91122011-10-21 16:38:58 -0500290void API minijail_remount_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400291{
292 j->flags.vfs = 1;
293 j->flags.readonly = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400294}
295
Will Drewry6ac91122011-10-21 16:38:58 -0500296void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400297{
298 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400299}
300
Will Drewry6ac91122011-10-21 16:38:58 -0500301void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400302{
303 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400304}
305
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800306void API minijail_run_as_init(struct minijail *j)
307{
308 /*
309 * Since the jailed program will become 'init' in the new PID namespace,
310 * Minijail does not need to fork an 'init' process.
311 */
312 j->flags.do_init = 0;
313}
314
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700315int API minijail_enter_chroot(struct minijail *j, const char *dir)
316{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400317 if (j->chrootdir)
318 return -EINVAL;
319 j->chrootdir = strdup(dir);
320 if (!j->chrootdir)
321 return -ENOMEM;
322 j->flags.chroot = 1;
323 return 0;
324}
325
Lee Campbell11af0622014-05-22 12:36:04 -0700326void API minijail_mount_tmp(struct minijail *j)
327{
328 j->flags.mount_tmp = 1;
329}
330
Will Drewry6ac91122011-10-21 16:38:58 -0500331int API minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700332 int writeable)
333{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400334 struct binding *b;
335
336 if (*dest != '/')
337 return -EINVAL;
338 b = calloc(1, sizeof(*b));
339 if (!b)
340 return -ENOMEM;
341 b->dest = strdup(dest);
342 if (!b->dest)
343 goto error;
344 b->src = strdup(src);
345 if (!b->src)
346 goto error;
347 b->writeable = writeable;
348
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700349 info("bind %s -> %s", src, dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400350
Elly Jonesdd3e8512012-01-23 15:13:38 -0500351 /*
352 * Force vfs namespacing so the bind mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400353 * containing vfs namespace.
354 */
355 minijail_namespace_vfs(j);
356
357 if (j->bindings_tail)
358 j->bindings_tail->next = b;
359 else
360 j->bindings_head = b;
361 j->bindings_tail = b;
362 j->binding_count++;
363
364 return 0;
365
366error:
367 free(b->src);
368 free(b->dest);
369 free(b);
370 return -ENOMEM;
371}
372
Will Drewry6ac91122011-10-21 16:38:58 -0500373void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400374{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700375 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
376 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
377 warn("not loading seccomp filter, seccomp not supported");
378 return;
379 }
380 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400381 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800382 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700383 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400384 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800385
386 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700387 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
388 die("failed to compile seccomp filter BPF program in '%s'",
389 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800390 }
391
392 j->filter_len = fprog->len;
393 j->filter_prog = fprog;
394
Elly Jonese1749eb2011-10-07 13:54:59 -0400395 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500396}
397
Will Drewryf89aef52011-09-16 16:48:57 -0500398struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400399 size_t available;
400 size_t total;
401 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500402};
403
Will Drewry6ac91122011-10-21 16:38:58 -0500404void marshal_state_init(struct marshal_state *state,
405 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400406{
407 state->available = available;
408 state->buf = buf;
409 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500410}
411
Will Drewry6ac91122011-10-21 16:38:58 -0500412void marshal_append(struct marshal_state *state,
413 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400414{
415 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500416
Elly Jonese1749eb2011-10-07 13:54:59 -0400417 /* Up to |available| will be written. */
418 if (copy_len) {
419 memcpy(state->buf, src, copy_len);
420 state->buf += copy_len;
421 state->available -= copy_len;
422 }
423 /* |total| will contain the expected length. */
424 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500425}
426
Will Drewry6ac91122011-10-21 16:38:58 -0500427void minijail_marshal_helper(struct marshal_state *state,
428 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400429{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400430 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400431 marshal_append(state, (char *)j, sizeof(*j));
432 if (j->user)
433 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400434 if (j->chrootdir)
435 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800436 if (j->flags.seccomp_filter && j->filter_prog) {
437 struct sock_fprog *fp = j->filter_prog;
438 marshal_append(state, (char *)fp->filter,
439 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400440 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400441 for (b = j->bindings_head; b; b = b->next) {
442 marshal_append(state, b->src, strlen(b->src) + 1);
443 marshal_append(state, b->dest, strlen(b->dest) + 1);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700444 marshal_append(state, (char *)&b->writeable,
445 sizeof(b->writeable));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400446 }
Will Drewryf89aef52011-09-16 16:48:57 -0500447}
448
Will Drewry6ac91122011-10-21 16:38:58 -0500449size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400450{
451 struct marshal_state state;
452 marshal_state_init(&state, NULL, 0);
453 minijail_marshal_helper(&state, j);
454 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500455}
456
Elly Jonese1749eb2011-10-07 13:54:59 -0400457int minijail_marshal(const struct minijail *j, char *buf, size_t available)
458{
459 struct marshal_state state;
460 marshal_state_init(&state, buf, available);
461 minijail_marshal_helper(&state, j);
462 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500463}
464
Elly Jones51a5b6c2011-10-12 19:09:26 -0400465/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
466 * @length Number of bytes to consume
467 * @buf Buffer to consume from
468 * @buflength Size of @buf
469 *
470 * Returns a pointer to the base of the bytes, or NULL for errors.
471 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700472void *consumebytes(size_t length, char **buf, size_t *buflength)
473{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400474 char *p = *buf;
475 if (length > *buflength)
476 return NULL;
477 *buf += length;
478 *buflength -= length;
479 return p;
480}
481
482/* consumestr: consumes a C string from a buffer @buf of length @length
483 * @buf Buffer to consume
484 * @length Length of buffer
485 *
486 * Returns a pointer to the base of the string, or NULL for errors.
487 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700488char *consumestr(char **buf, size_t *buflength)
489{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400490 size_t len = strnlen(*buf, *buflength);
491 if (len == *buflength)
492 /* There's no null-terminator */
493 return NULL;
494 return consumebytes(len + 1, buf, buflength);
495}
496
Elly Jonese1749eb2011-10-07 13:54:59 -0400497int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
498{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400499 int i;
500 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500501 int ret = -EINVAL;
502
Elly Jonese1749eb2011-10-07 13:54:59 -0400503 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500504 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400505 memcpy((void *)j, serialized, sizeof(*j));
506 serialized += sizeof(*j);
507 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500508
Will Drewrybee7ba72011-10-21 20:47:01 -0500509 /* Potentially stale pointers not used as signals. */
510 j->bindings_head = NULL;
511 j->bindings_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800512 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500513
Elly Jonese1749eb2011-10-07 13:54:59 -0400514 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400515 char *user = consumestr(&serialized, &length);
516 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500517 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400518 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500519 if (!j->user)
520 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400521 }
Will Drewryf89aef52011-09-16 16:48:57 -0500522
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400523 if (j->chrootdir) { /* stale pointer */
524 char *chrootdir = consumestr(&serialized, &length);
525 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500526 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400527 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500528 if (!j->chrootdir)
529 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400530 }
531
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800532 if (j->flags.seccomp_filter && j->filter_len > 0) {
533 size_t ninstrs = j->filter_len;
534 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
535 ninstrs > USHRT_MAX)
536 goto bad_filters;
537
538 size_t program_len = ninstrs * sizeof(struct sock_filter);
539 void *program = consumebytes(program_len, &serialized, &length);
540 if (!program)
541 goto bad_filters;
542
543 j->filter_prog = malloc(sizeof(struct sock_fprog));
544 j->filter_prog->len = ninstrs;
545 j->filter_prog->filter = malloc(program_len);
546 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400547 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400548
549 count = j->binding_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400550 j->binding_count = 0;
551 for (i = 0; i < count; ++i) {
552 int *writeable;
553 const char *dest;
554 const char *src = consumestr(&serialized, &length);
555 if (!src)
Will Drewrybee7ba72011-10-21 20:47:01 -0500556 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400557 dest = consumestr(&serialized, &length);
558 if (!dest)
Will Drewrybee7ba72011-10-21 20:47:01 -0500559 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400560 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
561 if (!writeable)
Will Drewrybee7ba72011-10-21 20:47:01 -0500562 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400563 if (minijail_bind(j, src, dest, *writeable))
Will Drewrybee7ba72011-10-21 20:47:01 -0500564 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400565 }
566
Elly Jonese1749eb2011-10-07 13:54:59 -0400567 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500568
569bad_bindings:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800570 if (j->flags.seccomp_filter && j->filter_len > 0) {
571 free(j->filter_prog->filter);
572 free(j->filter_prog);
573 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500574bad_filters:
575 if (j->chrootdir)
576 free(j->chrootdir);
577bad_chrootdir:
578 if (j->user)
579 free(j->user);
580clear_pointers:
581 j->user = NULL;
582 j->chrootdir = NULL;
583out:
584 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500585}
586
Elly Jones51a5b6c2011-10-12 19:09:26 -0400587/* bind_one: Applies bindings from @b for @j, recursing as needed.
588 * @j Minijail these bindings are for
589 * @b Head of list of bindings
590 *
591 * Returns 0 for success.
592 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700593int bind_one(const struct minijail *j, struct binding *b)
594{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400595 int ret = 0;
596 char *dest = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400597 if (ret)
598 return ret;
599 /* dest has a leading "/" */
600 if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0)
601 return -ENOMEM;
Elly Jonesa1059632011-12-15 15:17:07 -0500602 ret = mount(b->src, dest, NULL, MS_BIND, NULL);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400603 if (ret)
604 pdie("bind: %s -> %s", b->src, dest);
Elly Jonesa1059632011-12-15 15:17:07 -0500605 if (!b->writeable) {
606 ret = mount(b->src, dest, NULL,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700607 MS_BIND | MS_REMOUNT | MS_RDONLY, NULL);
Elly Jonesa1059632011-12-15 15:17:07 -0500608 if (ret)
609 pdie("bind ro: %s -> %s", b->src, dest);
610 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400611 free(dest);
612 if (b->next)
613 return bind_one(j, b->next);
614 return ret;
615}
616
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700617int enter_chroot(const struct minijail *j)
618{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400619 int ret;
620 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
621 return ret;
622
623 if (chroot(j->chrootdir))
624 return -errno;
625
626 if (chdir("/"))
627 return -errno;
628
629 return 0;
630}
631
Lee Campbell11af0622014-05-22 12:36:04 -0700632int mount_tmp(void)
633{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800634 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -0700635}
636
Will Drewry6ac91122011-10-21 16:38:58 -0500637int remount_readonly(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400638{
639 const char *kProcPath = "/proc";
640 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500641 /*
642 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400643 * /proc in our namespace, which means using MS_REMOUNT here would
644 * mutate our parent's mount as well, even though we're in a VFS
645 * namespace (!). Instead, remove their mount from our namespace
646 * and make our own.
647 */
648 if (umount(kProcPath))
649 return -errno;
650 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
651 return -errno;
652 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400653}
654
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700655void drop_ugid(const struct minijail *j)
656{
657 if (j->flags.usergroups) {
658 if (initgroups(j->user, j->usergid))
659 pdie("initgroups");
660 } else {
661 /* Only attempt to clear supplemental groups if we are changing
662 * users. */
663 if ((j->uid || j->gid) && setgroups(0, NULL))
664 pdie("setgroups");
665 }
666
667 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
668 pdie("setresgid");
669
670 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
671 pdie("setresuid");
672}
673
Mike Frysinger3adfef72013-05-09 17:19:08 -0400674/*
675 * We specifically do not use cap_valid() as that only tells us the last
676 * valid cap we were *compiled* against (i.e. what the version of kernel
677 * headers says). If we run on a different kernel version, then it's not
678 * uncommon for that to be less (if an older kernel) or more (if a newer
679 * kernel). So suck up the answer via /proc.
680 */
681static int run_cap_valid(unsigned int cap)
682{
683 static unsigned int last_cap;
684
685 if (!last_cap) {
686 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
687 FILE *fp = fopen(cap_file, "re");
688 if (fscanf(fp, "%u", &last_cap) != 1)
689 pdie("fscanf(%s)", cap_file);
690 fclose(fp);
691 }
692
693 return cap <= last_cap;
694}
695
Will Drewry6ac91122011-10-21 16:38:58 -0500696void drop_caps(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400697{
698 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -0800699 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -0800700 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400701 unsigned int i;
702 if (!caps)
703 die("can't get process caps");
704 if (cap_clear_flag(caps, CAP_INHERITABLE))
705 die("can't clear inheritable caps");
706 if (cap_clear_flag(caps, CAP_EFFECTIVE))
707 die("can't clear effective caps");
708 if (cap_clear_flag(caps, CAP_PERMITTED))
709 die("can't clear permitted caps");
Mike Frysinger3adfef72013-05-09 17:19:08 -0400710 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cook323878a2013-02-05 15:35:24 -0800711 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800712 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -0400713 continue;
Kees Cook323878a2013-02-05 15:35:24 -0800714 flag[0] = i;
715 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400716 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -0800717 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400718 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -0800719 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400720 die("can't add inheritable cap");
721 }
722 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -0800723 die("can't apply initial cleaned capset");
724
725 /*
726 * Instead of dropping bounding set first, do it here in case
727 * the caller had a more permissive bounding set which could
728 * have been used above to raise a capability that wasn't already
729 * present. This requires CAP_SETPCAP, so we raised/kept it above.
730 */
Mike Frysinger3adfef72013-05-09 17:19:08 -0400731 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -0800732 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -0400733 continue;
734 if (prctl(PR_CAPBSET_DROP, i))
735 pdie("prctl(PR_CAPBSET_DROP)");
736 }
Kees Cook323878a2013-02-05 15:35:24 -0800737
738 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800739 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -0800740 flag[0] = CAP_SETPCAP;
741 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
742 die("can't clear effective cap");
743 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
744 die("can't clear permitted cap");
745 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
746 die("can't clear inheritable cap");
747 }
748
749 if (cap_set_proc(caps))
750 die("can't apply final cleaned capset");
751
752 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -0400753}
754
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700755void set_seccomp_filter(const struct minijail *j)
756{
757 /*
758 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
759 * in the kernel source tree for an explanation of the parameters.
760 */
761 if (j->flags.no_new_privs) {
762 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
763 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
764 }
765
766 /*
767 * If we're logging seccomp filter failures,
768 * install the SIGSYS handler first.
769 */
770 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
771 if (install_sigsys_handler())
772 pdie("install SIGSYS handler");
773 warn("logging seccomp filter failures");
774 }
775
776 /*
777 * Install the syscall filter.
778 */
779 if (j->flags.seccomp_filter) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700780 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) {
781 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
782 warn("seccomp not supported");
783 return;
784 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700785 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700786 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700787 }
788}
789
Will Drewry6ac91122011-10-21 16:38:58 -0500790void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400791{
792 if (j->flags.pids)
793 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700794 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400795
Elly Jonese1749eb2011-10-07 13:54:59 -0400796 if (j->flags.usergroups && !j->user)
797 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400798
Elly Jonesdd3e8512012-01-23 15:13:38 -0500799 /*
800 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -0400801 * so we don't even try. If any of our operations fail, we abort() the
802 * entire process.
803 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700804 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
805 pdie("setns(CLONE_NEWNS)");
806
Elly Jonese1749eb2011-10-07 13:54:59 -0400807 if (j->flags.vfs && unshare(CLONE_NEWNS))
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400808 pdie("unshare(vfs)");
809
810 if (j->flags.net && unshare(CLONE_NEWNET))
811 pdie("unshare(net)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400812
Elly Jones51a5b6c2011-10-12 19:09:26 -0400813 if (j->flags.chroot && enter_chroot(j))
814 pdie("chroot");
815
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800816 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -0700817 pdie("mount_tmp");
818
Elly Jonese1749eb2011-10-07 13:54:59 -0400819 if (j->flags.readonly && remount_readonly())
820 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -0400821
Elly Jonese1749eb2011-10-07 13:54:59 -0400822 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500823 /*
824 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -0400825 * capability to change uids, our attempt to use setuid()
826 * below will fail. Hang on to root caps across setuid(), then
827 * lock securebits.
828 */
829 if (prctl(PR_SET_KEEPCAPS, 1))
830 pdie("prctl(PR_SET_KEEPCAPS)");
831 if (prctl
832 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
833 pdie("prctl(PR_SET_SECUREBITS)");
834 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400835
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700836 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700837 * If we're setting no_new_privs, we can drop privileges
838 * before setting seccomp filter. This way filter policies
839 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700840 */
841 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700842 drop_ugid(j);
843 if (j->flags.caps)
844 drop_caps(j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700845
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700846 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400847 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700848 /*
849 * If we're not setting no_new_privs,
850 * we need to set seccomp filter *before* dropping privileges.
851 * WARNING: this means that filter policies *must* allow
852 * setgroups()/setresgid()/setresuid() for dropping root and
853 * capget()/capset()/prctl() for dropping caps.
854 */
855 set_seccomp_filter(j);
856
857 drop_ugid(j);
858 if (j->flags.caps)
859 drop_caps(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400860 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400861
Elly Jonesdd3e8512012-01-23 15:13:38 -0500862 /*
863 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -0400864 * privilege-dropping syscalls :)
865 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700866 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
867 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
868 warn("seccomp not supported");
869 return;
870 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400871 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700872 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400873}
874
Will Drewry6ac91122011-10-21 16:38:58 -0500875/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -0400876static int init_exitstatus = 0;
877
Will Drewry6ac91122011-10-21 16:38:58 -0500878void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -0400879{
880 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -0400881}
882
Will Drewry6ac91122011-10-21 16:38:58 -0500883int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400884{
885 pid_t pid;
886 int status;
887 /* so that we exit with the right status */
888 signal(SIGTERM, init_term);
889 /* TODO(wad) self jail with seccomp_filters here. */
890 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500891 /*
892 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -0400893 * left inside our pid namespace or we get a signal.
894 */
895 if (pid == rootpid)
896 init_exitstatus = status;
897 }
898 if (!WIFEXITED(init_exitstatus))
899 _exit(MINIJAIL_ERR_INIT);
900 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -0400901}
902
Will Drewry6ac91122011-10-21 16:38:58 -0500903int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400904{
905 size_t sz = 0;
906 size_t bytes = read(fd, &sz, sizeof(sz));
907 char *buf;
908 int r;
909 if (sizeof(sz) != bytes)
910 return -EINVAL;
911 if (sz > USHRT_MAX) /* Arbitrary sanity check */
912 return -E2BIG;
913 buf = malloc(sz);
914 if (!buf)
915 return -ENOMEM;
916 bytes = read(fd, buf, sz);
917 if (bytes != sz) {
918 free(buf);
919 return -EINVAL;
920 }
921 r = minijail_unmarshal(j, buf, sz);
922 free(buf);
923 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500924}
925
Will Drewry6ac91122011-10-21 16:38:58 -0500926int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -0400927{
928 char *buf;
929 size_t sz = minijail_size(j);
930 ssize_t written;
931 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -0400932
Elly Jonese1749eb2011-10-07 13:54:59 -0400933 if (!sz)
934 return -EINVAL;
935 buf = malloc(sz);
936 r = minijail_marshal(j, buf, sz);
937 if (r) {
938 free(buf);
939 return r;
940 }
941 /* Sends [size][minijail]. */
942 written = write(fd, &sz, sizeof(sz));
943 if (written != sizeof(sz)) {
944 free(buf);
945 return -EFAULT;
946 }
947 written = write(fd, buf, sz);
948 if (written < 0 || (size_t) written != sz) {
949 free(buf);
950 return -EFAULT;
951 }
952 free(buf);
953 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500954}
Elly Jonescd7a9042011-07-22 13:56:51 -0400955
Will Drewry6ac91122011-10-21 16:38:58 -0500956int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400957{
Daniel Erat5b7a3182015-08-19 16:06:22 -0600958#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700959 /* Don't use LDPRELOAD on Brillo. */
960 return 0;
961#else
Elly Jonese1749eb2011-10-07 13:54:59 -0400962 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
963 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
964 if (!newenv)
965 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -0400966
Elly Jonese1749eb2011-10-07 13:54:59 -0400967 /* Only insert a separating space if we have something to separate... */
968 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
969 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -0400970
Elly Jonese1749eb2011-10-07 13:54:59 -0400971 /* setenv() makes a copy of the string we give it */
972 setenv(kLdPreloadEnvVar, newenv, 1);
973 free(newenv);
974 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700975#endif
Elly Jonescd7a9042011-07-22 13:56:51 -0400976}
977
Will Drewry6ac91122011-10-21 16:38:58 -0500978int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -0400979{
980 int r = pipe(fds);
981 char fd_buf[11];
982 if (r)
983 return r;
984 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
985 if (r <= 0)
986 return -EINVAL;
987 setenv(kFdEnvVar, fd_buf, 1);
988 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500989}
990
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800991int setup_pipe_end(int fds[2], size_t index)
992{
993 if (index > 1)
994 return -1;
995
996 close(fds[1 - index]);
997 return fds[index];
998}
999
1000int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1001{
1002 if (index > 1)
1003 return -1;
1004
1005 close(fds[1 - index]);
1006 /* dup2(2) the corresponding end of the pipe into |fd|. */
1007 return dup2(fds[index], fd);
1008}
1009
Will Drewry6ac91122011-10-21 16:38:58 -05001010int API minijail_run(struct minijail *j, const char *filename,
1011 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001012{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001013 return minijail_run_pid_pipes(j, filename, argv,
1014 NULL, NULL, NULL, NULL);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001015}
1016
1017int API minijail_run_pid(struct minijail *j, const char *filename,
1018 char *const argv[], pid_t *pchild_pid)
1019{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001020 return minijail_run_pid_pipes(j, filename, argv, pchild_pid,
1021 NULL, NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001022}
1023
1024int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001025 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001026{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001027 return minijail_run_pid_pipes(j, filename, argv, NULL, pstdin_fd,
1028 NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001029}
1030
1031int API minijail_run_pid_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001032 char *const argv[], pid_t *pchild_pid,
1033 int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001034{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001035 return minijail_run_pid_pipes(j, filename, argv, pchild_pid, pstdin_fd,
1036 NULL, NULL);
1037}
1038
1039int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001040 char *const argv[], pid_t *pchild_pid,
1041 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001042{
Elly Jonese1749eb2011-10-07 13:54:59 -04001043 char *oldenv, *oldenv_copy = NULL;
1044 pid_t child_pid;
1045 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001046 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001047 int stdout_fds[2];
1048 int stderr_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001049 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001050 /* We need to remember this across the minijail_preexec() call. */
1051 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001052 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001053
Elly Jonese1749eb2011-10-07 13:54:59 -04001054 oldenv = getenv(kLdPreloadEnvVar);
1055 if (oldenv) {
1056 oldenv_copy = strdup(oldenv);
1057 if (!oldenv_copy)
1058 return -ENOMEM;
1059 }
Will Drewryf89aef52011-09-16 16:48:57 -05001060
Elly Jonese1749eb2011-10-07 13:54:59 -04001061 if (setup_preload())
1062 return -EFAULT;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001063
Elly Jonesdd3e8512012-01-23 15:13:38 -05001064 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001065 * Make the process group ID of this process equal to its PID, so that
1066 * both the Minijail process and the jailed process can be killed
1067 * together.
1068 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1069 * the process is already a process group leader.
1070 */
1071 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1072 if (errno != EPERM) {
1073 pdie("setpgid(0, 0)");
1074 }
1075 }
1076
1077 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001078 * Before we fork(2) and execve(2) the child process, we need to open
Elly Jonese1749eb2011-10-07 13:54:59 -04001079 * a pipe(2) to send the minijail configuration over.
1080 */
1081 if (setup_pipe(pipe_fds))
1082 return -EFAULT;
Elly Jonescd7a9042011-07-22 13:56:51 -04001083
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001084 /*
1085 * If we want to write to the child process' standard input,
1086 * create the pipe(2) now.
1087 */
1088 if (pstdin_fd) {
1089 if (pipe(stdin_fds))
1090 return -EFAULT;
1091 }
1092
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001093 /*
1094 * If we want to read from the child process' standard output,
1095 * create the pipe(2) now.
1096 */
1097 if (pstdout_fd) {
1098 if (pipe(stdout_fds))
1099 return -EFAULT;
1100 }
1101
1102 /*
1103 * If we want to read from the child process' standard error,
1104 * create the pipe(2) now.
1105 */
1106 if (pstderr_fd) {
1107 if (pipe(stderr_fds))
1108 return -EFAULT;
1109 }
1110
Elly Jones761b7412012-06-13 15:49:52 -04001111 /* Use sys_clone() if and only if we're creating a pid namespace.
1112 *
1113 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1114 *
1115 * In multithreaded programs, there are a bunch of locks inside libc,
1116 * some of which may be held by other threads at the time that we call
1117 * minijail_run_pid(). If we call fork(), glibc does its level best to
1118 * ensure that we hold all of these locks before it calls clone()
1119 * internally and drop them after clone() returns, but when we call
1120 * sys_clone(2) directly, all that gets bypassed and we end up with a
1121 * child address space where some of libc's important locks are held by
1122 * other threads (which did not get cloned, and hence will never release
1123 * those locks). This is okay so long as we call exec() immediately
1124 * after, but a bunch of seemingly-innocent libc functions like setenv()
1125 * take locks.
1126 *
1127 * Hence, only call sys_clone() if we need to, in order to get at pid
1128 * namespacing. If we follow this path, the child's address space might
1129 * have broken locks; you may only call functions that do not acquire
1130 * any locks.
1131 *
1132 * Unfortunately, fork() acquires every lock it can get its hands on, as
1133 * previously detailed, so this function is highly likely to deadlock
1134 * later on (see "deadlock here") if we're multithreaded.
1135 *
1136 * We might hack around this by having the clone()d child (init of the
1137 * pid namespace) return directly, rather than leaving the clone()d
1138 * process hanging around to be init for the new namespace (and having
1139 * its fork()ed child return in turn), but that process would be crippled
1140 * with its libc locks potentially broken. We might try fork()ing in the
1141 * parent before we clone() to ensure that we own all the locks, but
1142 * then we have to have the forked child hanging around consuming
1143 * resources (and possibly having file descriptors / shared memory
1144 * regions / etc attached). We'd need to keep the child around to avoid
1145 * having its children get reparented to init.
1146 *
1147 * TODO(ellyjones): figure out if the "forked child hanging around"
1148 * problem is fixable or not. It would be nice if we worked in this
1149 * case.
1150 */
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001151 if (pid_namespace)
Elly Jones761b7412012-06-13 15:49:52 -04001152 child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL);
1153 else
1154 child_pid = fork();
1155
Elly Jonese1749eb2011-10-07 13:54:59 -04001156 if (child_pid < 0) {
1157 free(oldenv_copy);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001158 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001159 }
Will Drewryf89aef52011-09-16 16:48:57 -05001160
Elly Jonese1749eb2011-10-07 13:54:59 -04001161 if (child_pid) {
1162 /* Restore parent's LD_PRELOAD. */
1163 if (oldenv_copy) {
1164 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1165 free(oldenv_copy);
1166 } else {
1167 unsetenv(kLdPreloadEnvVar);
1168 }
1169 unsetenv(kFdEnvVar);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001170
Elly Jonese1749eb2011-10-07 13:54:59 -04001171 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001172
1173 /* Send marshalled minijail. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001174 close(pipe_fds[0]); /* read endpoint */
1175 ret = minijail_to_fd(j, pipe_fds[1]);
1176 close(pipe_fds[1]); /* write endpoint */
1177 if (ret) {
1178 kill(j->initpid, SIGKILL);
1179 die("failed to send marshalled minijail");
1180 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001181
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001182 if (pchild_pid)
1183 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001184
1185 /*
1186 * If we want to write to the child process' standard input,
1187 * set up the write end of the pipe.
1188 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001189 if (pstdin_fd)
1190 *pstdin_fd = setup_pipe_end(stdin_fds,
1191 1 /* write end */);
1192
1193 /*
1194 * If we want to read from the child process' standard output,
1195 * set up the read end of the pipe.
1196 */
1197 if (pstdout_fd)
1198 *pstdout_fd = setup_pipe_end(stdout_fds,
1199 0 /* read end */);
1200
1201 /*
1202 * If we want to read from the child process' standard error,
1203 * set up the read end of the pipe.
1204 */
1205 if (pstderr_fd)
1206 *pstderr_fd = setup_pipe_end(stderr_fds,
1207 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001208
Elly Jonese1749eb2011-10-07 13:54:59 -04001209 return 0;
1210 }
1211 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001212
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001213 /*
1214 * If we want to write to the jailed process' standard input,
1215 * set up the read end of the pipe.
1216 */
1217 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001218 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1219 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001220 die("failed to set up stdin pipe");
1221 }
1222
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001223 /*
1224 * If we want to read from the jailed process' standard output,
1225 * set up the write end of the pipe.
1226 */
1227 if (pstdout_fd) {
1228 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1229 STDOUT_FILENO) < 0)
1230 die("failed to set up stdout pipe");
1231 }
1232
1233 /*
1234 * If we want to read from the jailed process' standard error,
1235 * set up the write end of the pipe.
1236 */
1237 if (pstderr_fd) {
1238 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1239 STDERR_FILENO) < 0)
1240 die("failed to set up stderr pipe");
1241 }
1242
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001243 /* Strip out flags that cannot be inherited across execve. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001244 minijail_preexec(j);
1245 /* Jail this process and its descendants... */
1246 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001247
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001248 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001249 /*
1250 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001251 * namespace. We don't want all programs we might exec to have
1252 * to know how to be init. Normally |do_init == 1| we fork off
1253 * a child to actually run the program. If |do_init == 0|, we
1254 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001255 *
1256 * If we're multithreaded, we'll probably deadlock here. See
1257 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001258 */
1259 child_pid = fork();
1260 if (child_pid < 0)
1261 _exit(child_pid);
1262 else if (child_pid > 0)
1263 init(child_pid); /* never returns */
1264 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001265
Elly Jonesdd3e8512012-01-23 15:13:38 -05001266 /*
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001267 * If we aren't pid-namespaced, or jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001268 * calling process
1269 * -> execve()-ing process
1270 * If we are:
1271 * calling process
1272 * -> init()-ing process
1273 * -> execve()-ing process
1274 */
1275 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001276}
1277
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001278int API minijail_run_static(struct minijail *j, const char *filename,
1279 char *const argv[])
1280{
1281 pid_t child_pid;
1282 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001283 int do_init = j->flags.do_init;
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001284
1285 if (j->flags.caps)
1286 die("caps not supported with static targets");
1287
1288 if (pid_namespace)
1289 child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL);
1290 else
1291 child_pid = fork();
1292
1293 if (child_pid < 0) {
1294 die("failed to fork child");
1295 }
1296 if (child_pid > 0 ) {
1297 j->initpid = child_pid;
1298 return 0;
1299 }
1300
1301 /*
1302 * We can now drop this child into the sandbox
1303 * then execve the target.
1304 */
1305
1306 j->flags.pids = 0;
1307 minijail_enter(j);
1308
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001309 if (pid_namespace && do_init) {
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001310 /*
1311 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001312 * namespace. We don't want all programs we might exec to have
1313 * to know how to be init. Normally |do_init == 1| we fork off
1314 * a child to actually run the program. If |do_init == 0|, we
1315 * let the program keep pid 1 and be init.
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001316 *
1317 * If we're multithreaded, we'll probably deadlock here. See
1318 * WARNING above.
1319 */
1320 child_pid = fork();
1321 if (child_pid < 0)
1322 _exit(child_pid);
1323 else if (child_pid > 0)
1324 init(child_pid); /* never returns */
1325 }
1326
1327 _exit(execve(filename, argv, environ));
1328}
1329
Will Drewry6ac91122011-10-21 16:38:58 -05001330int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001331{
1332 int st;
1333 if (kill(j->initpid, SIGTERM))
1334 return -errno;
1335 if (waitpid(j->initpid, &st, 0) < 0)
1336 return -errno;
1337 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001338}
1339
Will Drewry6ac91122011-10-21 16:38:58 -05001340int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001341{
1342 int st;
1343 if (waitpid(j->initpid, &st, 0) < 0)
1344 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001345
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001346 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001347 int error_status = st;
1348 if (WIFSIGNALED(st)) {
1349 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001350 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001351 j->initpid, signum);
1352 /*
1353 * We return MINIJAIL_ERR_JAIL if the process received
1354 * SIGSYS, which happens when a syscall is blocked by
1355 * seccomp filters.
1356 * If not, we do what bash(1) does:
1357 * $? = 128 + signum
1358 */
1359 if (signum == SIGSYS) {
1360 error_status = MINIJAIL_ERR_JAIL;
1361 } else {
1362 error_status = 128 + signum;
1363 }
1364 }
1365 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001366 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001367
1368 int exit_status = WEXITSTATUS(st);
1369 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001370 info("child process %d exited with status %d",
1371 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001372
1373 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001374}
1375
Will Drewry6ac91122011-10-21 16:38:58 -05001376void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001377{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001378 if (j->flags.seccomp_filter && j->filter_prog) {
1379 free(j->filter_prog->filter);
1380 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001381 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001382 while (j->bindings_head) {
1383 struct binding *b = j->bindings_head;
1384 j->bindings_head = j->bindings_head->next;
1385 free(b->dest);
1386 free(b->src);
1387 free(b);
1388 }
1389 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001390 if (j->user)
1391 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001392 if (j->chrootdir)
1393 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001394 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001395}