blob: 06f8e1a1adeb11cced0fac066c3fb36ffdc8b640 [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
Arthur Gautier7a569072016-04-23 17:25:20 +00007#define _DEFAULT_SOURCE
Elly Jonescd7a9042011-07-22 13:56:51 -04008#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>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070013#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040014#include <grp.h>
15#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050016#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040017#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040018#include <pwd.h>
19#include <sched.h>
20#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050021#include <stdarg.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070022#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080023#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <syscall.h>
28#include <sys/capability.h>
29#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050030#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040031#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070032#include <sys/stat.h>
33#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080034#include <sys/user.h>
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -080035#include <sys/utsname.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040036#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040037#include <unistd.h>
38
39#include "libminijail.h"
40#include "libminijail-private.h"
41
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070042#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080043#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070044#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080045
Lei Zhangeee31552012-10-17 21:27:10 -070046#ifdef HAVE_SECUREBITS_H
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070047# include <linux/securebits.h>
Lei Zhangeee31552012-10-17 21:27:10 -070048#else
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070049# define SECURE_ALL_BITS 0x55
50# define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
51#endif
52/* For kernels < 4.3. */
53#define OLD_SECURE_ALL_BITS 0x15
54#define OLD_SECURE_ALL_LOCKS (OLD_SECURE_ALL_BITS << 1)
55
56/*
57 * Assert the value of SECURE_ALL_BITS at compile-time.
58 * Brillo devices are currently compiled against 4.4 kernel headers. Kernel 4.3
59 * added a new securebit.
60 * When a new securebit is added, the new SECURE_ALL_BITS mask will return EPERM
61 * when used on older kernels. The compile-time assert will catch this situation
62 * at compile time.
63 */
64#ifdef __BRILLO__
65_Static_assert(SECURE_ALL_BITS == 0x55, "SECURE_ALL_BITS == 0x55.");
Lei Zhangeee31552012-10-17 21:27:10 -070066#endif
67
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070068/* Until these are reliably available in linux/prctl.h. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080069#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070070# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080071#endif
72
Andrew Brestickereac28942015-11-11 16:04:46 -080073#ifndef PR_ALT_SYSCALL
74# define PR_ALT_SYSCALL 0x43724f53
75#endif
76
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080077/* For seccomp_filter using BPF. */
78#ifndef PR_SET_NO_NEW_PRIVS
79# define PR_SET_NO_NEW_PRIVS 38
80#endif
81#ifndef SECCOMP_MODE_FILTER
82# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050083#endif
84
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070085#ifdef USE_SECCOMP_SOFTFAIL
86# define SECCOMP_SOFTFAIL 1
87#else
88# define SECCOMP_SOFTFAIL 0
89#endif
90
Dylan Reid605ce7f2016-01-19 19:21:00 -080091#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
92
Dylan Reid648b2202015-10-23 00:50:00 -070093struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040094 char *src;
95 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070096 char *type;
97 unsigned long flags;
98 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040099};
100
Will Drewryf89aef52011-09-16 16:48:57 -0500101struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700102 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700103 * WARNING: if you add a flag here you need to make sure it's
104 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700105 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400106 struct {
107 int uid:1;
108 int gid:1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800109 int usergroups:1;
110 int suppl_gids:1;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800111 int use_caps:1;
112 int capbset_drop:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400113 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700114 int enter_vfs:1;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800115 int skip_remount_private:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400116 int pids:1;
Dylan Reidf7942472015-11-18 17:55:26 -0800117 int ipc:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400118 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700119 int enter_net:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800120 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400121 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -0700122 int remount_proc_ro:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700123 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400124 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700125 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400126 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800127 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700128 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800129 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800130 int pid_file:1;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800131 int cgroups:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800132 int alt_syscall:1;
Peter Qiu2860c462015-12-16 15:13:06 -0800133 int reset_signal_mask:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400134 } flags;
135 uid_t uid;
136 gid_t gid;
137 gid_t usergid;
138 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800139 size_t suppl_gid_count;
140 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400141 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800142 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400143 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700144 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700145 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400146 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800147 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800148 char *uidmap;
149 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800150 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800151 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800152 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700153 struct mountpoint *mounts_head;
154 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800155 size_t mounts_count;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800156 char *cgroups[MAX_CGROUPS];
157 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500158};
159
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700160/*
161 * Strip out flags meant for the parent.
162 * We keep things that are not inherited across execve(2) (e.g. capabilities),
163 * or are easier to set after execve(2) (e.g. seccomp filters).
164 */
165void minijail_preenter(struct minijail *j)
166{
167 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700168 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900169 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700170 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700171 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800172 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800173 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800174 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700175}
176
177/*
178 * Strip out flags meant for the child.
179 * We keep things that are inherited across execve(2).
180 */
181void minijail_preexec(struct minijail *j)
182{
183 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700184 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800185 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700186 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800187 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700188 if (j->user)
189 free(j->user);
190 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800191 if (j->suppl_gid_list)
192 free(j->suppl_gid_list);
193 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700194 memset(&j->flags, 0, sizeof(j->flags));
195 /* Now restore anything we meant to keep. */
196 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700197 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800198 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700199 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800200 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700201 /* Note, |pids| will already have been used before this call. */
202}
203
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800204/* Returns true if the kernel version is less than 3.8. */
205int seccomp_kernel_support_not_required()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800206{
207 int major, minor;
208 struct utsname uts;
209 return (uname(&uts) != -1 &&
210 sscanf(uts.release, "%d.%d", &major, &minor) == 2 &&
211 ((major < 3) || ((major == 3) && (minor < 8))));
212}
213
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800214/* Allow seccomp soft-fail on Android devices with kernel version < 3.8. */
215int can_softfail()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800216{
217#if SECCOMP_SOFTFAIL
218 if (is_android()) {
219 if (seccomp_kernel_support_not_required())
220 return 1;
221 else
222 return 0;
223 } else {
224 return 1;
225 }
226#endif
227 return 0;
228}
229
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700230/* Minijail API. */
231
Will Drewry6ac91122011-10-21 16:38:58 -0500232struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400233{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400234 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400235}
236
Will Drewry6ac91122011-10-21 16:38:58 -0500237void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400238{
239 if (uid == 0)
240 die("useless change to uid 0");
241 j->uid = uid;
242 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400243}
244
Will Drewry6ac91122011-10-21 16:38:58 -0500245void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400246{
247 if (gid == 0)
248 die("useless change to gid 0");
249 j->gid = gid;
250 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400251}
252
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800253void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
254 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800255{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800256 size_t i;
257
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800258 if (j->flags.usergroups)
259 die("cannot inherit *and* set supplementary groups");
260
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800261 if (size == 0) {
262 /* Clear supplementary groups. */
263 j->suppl_gid_list = NULL;
264 j->suppl_gid_count = 0;
265 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800266 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800267 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800268
269 /* Copy the gid_t array. */
270 j->suppl_gid_list = calloc(size, sizeof(gid_t));
271 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800272 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800273 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800274 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800275 j->suppl_gid_list[i] = list[i];
276 }
277 j->suppl_gid_count = size;
278 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800279}
280
Will Drewry6ac91122011-10-21 16:38:58 -0500281int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400282{
283 char *buf = NULL;
284 struct passwd pw;
285 struct passwd *ppw = NULL;
286 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
287 if (sz == -1)
288 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400289
Elly Jonesdd3e8512012-01-23 15:13:38 -0500290 /*
291 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400292 * the maximum needed size of the buffer, so we don't have to search.
293 */
294 buf = malloc(sz);
295 if (!buf)
296 return -ENOMEM;
297 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500298 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800299 * We're safe to free the buffer here. The strings inside |pw| point
300 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800301 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
302 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500303 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400304 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700305 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400306 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700307 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400308 minijail_change_uid(j, ppw->pw_uid);
309 j->user = strdup(user);
310 if (!j->user)
311 return -ENOMEM;
312 j->usergid = ppw->pw_gid;
313 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400314}
315
Will Drewry6ac91122011-10-21 16:38:58 -0500316int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400317{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700318 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700319 struct group gr;
320 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400321 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
322 if (sz == -1)
323 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400324
Elly Jonesdd3e8512012-01-23 15:13:38 -0500325 /*
326 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400327 * the maximum needed size of the buffer, so we don't have to search.
328 */
329 buf = malloc(sz);
330 if (!buf)
331 return -ENOMEM;
332 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500333 /*
334 * We're safe to free the buffer here. The strings inside gr point
335 * inside buf, but we don't use any of them; this leaves the pointers
336 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
337 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400338 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700339 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400340 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700341 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400342 minijail_change_gid(j, pgr->gr_gid);
343 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400344}
345
Will Drewry6ac91122011-10-21 16:38:58 -0500346void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400347{
348 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400349}
350
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700351void API minijail_no_new_privs(struct minijail *j)
352{
353 j->flags.no_new_privs = 1;
354}
355
Will Drewry6ac91122011-10-21 16:38:58 -0500356void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400357{
358 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500359}
360
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700361void API minijail_log_seccomp_filter_failures(struct minijail *j)
362{
363 j->flags.log_seccomp_filter = 1;
364}
365
Will Drewry6ac91122011-10-21 16:38:58 -0500366void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400367{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800368 /*
369 * 'minijail_use_caps' configures a runtime-capabilities-only
370 * environment, including a bounding set matching the thread's runtime
371 * (permitted|inheritable|effective) sets.
372 * Therefore, it will override any existing bounding set configurations
373 * since the latter would allow gaining extra runtime capabilities from
374 * file capabilities.
375 */
376 if (j->flags.capbset_drop) {
377 warn("overriding bounding set configuration");
378 j->cap_bset = 0;
379 j->flags.capbset_drop = 0;
380 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400381 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800382 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400383}
384
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800385void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
386{
387 if (j->flags.use_caps) {
388 /*
389 * 'minijail_use_caps' will have already configured a capability
390 * bounding set matching the (permitted|inheritable|effective)
391 * sets. Abort if the user tries to configure a separate
392 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
393 * are mutually exclusive.
394 */
395 die("runtime capabilities already configured, can't drop "
396 "bounding set separately");
397 }
398 j->cap_bset = capmask;
399 j->flags.capbset_drop = 1;
400}
401
402void API minijail_reset_signal_mask(struct minijail *j)
403{
Peter Qiu2860c462015-12-16 15:13:06 -0800404 j->flags.reset_signal_mask = 1;
405}
406
Will Drewry6ac91122011-10-21 16:38:58 -0500407void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400408{
409 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400410}
411
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700412void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
413{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800414 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700415 if (ns_fd < 0) {
416 pdie("failed to open namespace '%s'", ns_path);
417 }
418 j->mountns_fd = ns_fd;
419 j->flags.enter_vfs = 1;
420}
421
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800422void API minijail_skip_remount_private(struct minijail *j)
423{
424 j->flags.skip_remount_private = 1;
425}
426
Will Drewry6ac91122011-10-21 16:38:58 -0500427void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400428{
Elly Jonese58176c2012-01-23 11:46:17 -0500429 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700430 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400431 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800432 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400433}
434
Dylan Reidf7942472015-11-18 17:55:26 -0800435void API minijail_namespace_ipc(struct minijail *j)
436{
437 j->flags.ipc = 1;
438}
439
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400440void API minijail_namespace_net(struct minijail *j)
441{
442 j->flags.net = 1;
443}
444
Dylan Reid1102f5a2015-09-15 11:52:20 -0700445void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
446{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800447 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700448 if (ns_fd < 0) {
449 pdie("failed to open namespace '%s'", ns_path);
450 }
451 j->netns_fd = ns_fd;
452 j->flags.enter_net = 1;
453}
454
Dylan Reid791f5772015-09-14 20:02:42 -0700455void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400456{
457 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700458 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400459}
460
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800461void API minijail_namespace_user(struct minijail *j)
462{
463 j->flags.userns = 1;
464}
465
466int API minijail_uidmap(struct minijail *j, const char *uidmap)
467{
468 j->uidmap = strdup(uidmap);
469 if (!j->uidmap)
470 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800471 char *ch;
472 for (ch = j->uidmap; *ch; ch++) {
473 if (*ch == ',')
474 *ch = '\n';
475 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800476 return 0;
477}
478
479int API minijail_gidmap(struct minijail *j, const char *gidmap)
480{
481 j->gidmap = strdup(gidmap);
482 if (!j->gidmap)
483 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800484 char *ch;
485 for (ch = j->gidmap; *ch; ch++) {
486 if (*ch == ',')
487 *ch = '\n';
488 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800489 return 0;
490}
491
Will Drewry6ac91122011-10-21 16:38:58 -0500492void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400493{
494 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400495}
496
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800497void API minijail_run_as_init(struct minijail *j)
498{
499 /*
500 * Since the jailed program will become 'init' in the new PID namespace,
501 * Minijail does not need to fork an 'init' process.
502 */
503 j->flags.do_init = 0;
504}
505
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700506int API minijail_enter_chroot(struct minijail *j, const char *dir)
507{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400508 if (j->chrootdir)
509 return -EINVAL;
510 j->chrootdir = strdup(dir);
511 if (!j->chrootdir)
512 return -ENOMEM;
513 j->flags.chroot = 1;
514 return 0;
515}
516
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800517int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
518{
519 if (j->chrootdir)
520 return -EINVAL;
521 j->chrootdir = strdup(dir);
522 if (!j->chrootdir)
523 return -ENOMEM;
524 j->flags.pivot_root = 1;
525 return 0;
526}
527
Dylan Reida14e08d2015-10-22 21:05:29 -0700528static char *append_external_path(const char *external_path,
529 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700530{
Dylan Reida14e08d2015-10-22 21:05:29 -0700531 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700532 size_t pathlen;
533
Dylan Reid08946cc2015-09-16 19:10:57 -0700534 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700535 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
536 path = malloc(pathlen);
537 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700538
Dylan Reida14e08d2015-10-22 21:05:29 -0700539 return path;
540}
541
542char API *minijail_get_original_path(struct minijail *j,
543 const char *path_inside_chroot)
544{
Dylan Reid648b2202015-10-23 00:50:00 -0700545 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700546
Dylan Reid648b2202015-10-23 00:50:00 -0700547 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700548 while (b) {
549 /*
550 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700551 * mount, then the original path is exactly the source of
552 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700553 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700554 * mount source = /some/path/exe, mount dest =
555 * /chroot/path/exe Then when getting the original path of
556 * "/chroot/path/exe", the source of that mount,
557 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700558 */
559 if (!strcmp(b->dest, path_inside_chroot))
560 return strdup(b->src);
561
562 /*
563 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700564 * mount, take the suffix of the chroot path relative to the
565 * mount destination path, and append it to the mount source
566 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700567 */
568 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
569 const char *relative_path =
570 path_inside_chroot + strlen(b->dest);
571 return append_external_path(b->src, relative_path);
572 }
573 b = b->next;
574 }
575
576 /* If there is a chroot path, append |path_inside_chroot| to that. */
577 if (j->chrootdir)
578 return append_external_path(j->chrootdir, path_inside_chroot);
579
580 /* No chroot, so the path outside is the same as it is inside. */
581 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700582}
583
Lee Campbell11af0622014-05-22 12:36:04 -0700584void API minijail_mount_tmp(struct minijail *j)
585{
586 j->flags.mount_tmp = 1;
587}
588
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800589int API minijail_write_pid_file(struct minijail *j, const char *path)
590{
591 j->pid_file_path = strdup(path);
592 if (!j->pid_file_path)
593 return -ENOMEM;
594 j->flags.pid_file = 1;
595 return 0;
596}
597
Dylan Reid605ce7f2016-01-19 19:21:00 -0800598int API minijail_add_to_cgroup(struct minijail *j, const char *path)
599{
600 if (j->cgroup_count >= MAX_CGROUPS)
601 return -ENOMEM;
602 j->cgroups[j->cgroup_count] = strdup(path);
603 if (!j->cgroups[j->cgroup_count])
604 return -ENOMEM;
605 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800606 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800607 return 0;
608}
609
Dylan Reid648b2202015-10-23 00:50:00 -0700610int API minijail_mount(struct minijail *j, const char *src, const char *dest,
611 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700612{
Dylan Reid648b2202015-10-23 00:50:00 -0700613 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400614
615 if (*dest != '/')
616 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700617 m = calloc(1, sizeof(*m));
618 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400619 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700620 m->dest = strdup(dest);
621 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400622 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700623 m->src = strdup(src);
624 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400625 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700626 m->type = strdup(type);
627 if (!m->type)
628 goto error;
629 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400630
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800631 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400632
Elly Jonesdd3e8512012-01-23 15:13:38 -0500633 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700634 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400635 * containing vfs namespace.
636 */
637 minijail_namespace_vfs(j);
638
Dylan Reid648b2202015-10-23 00:50:00 -0700639 if (j->mounts_tail)
640 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400641 else
Dylan Reid648b2202015-10-23 00:50:00 -0700642 j->mounts_head = m;
643 j->mounts_tail = m;
644 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400645
646 return 0;
647
648error:
Dylan Reid648b2202015-10-23 00:50:00 -0700649 free(m->src);
650 free(m->dest);
651 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400652 return -ENOMEM;
653}
654
Dylan Reid648b2202015-10-23 00:50:00 -0700655int API minijail_bind(struct minijail *j, const char *src, const char *dest,
656 int writeable)
657{
658 unsigned long flags = MS_BIND;
659
660 if (!writeable)
661 flags |= MS_RDONLY;
662
663 return minijail_mount(j, src, dest, "", flags);
664}
665
Will Drewry6ac91122011-10-21 16:38:58 -0500666void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400667{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700668 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800669 if ((errno == EINVAL) && can_softfail()) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800670 warn("not loading seccomp filter,"
671 " seccomp not supported");
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800672 j->flags.seccomp_filter = 0;
673 j->flags.log_seccomp_filter = 0;
674 j->filter_len = 0;
675 j->filter_prog = NULL;
676 j->flags.no_new_privs = 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700677 }
678 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400679 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800680 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700681 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400682 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800683
684 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700685 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
686 die("failed to compile seccomp filter BPF program in '%s'",
687 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800688 }
689
690 j->filter_len = fprog->len;
691 j->filter_prog = fprog;
692
Elly Jonese1749eb2011-10-07 13:54:59 -0400693 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500694}
695
Andrew Brestickereac28942015-11-11 16:04:46 -0800696int API minijail_use_alt_syscall(struct minijail *j, const char *table)
697{
698 j->alt_syscall_table = strdup(table);
699 if (!j->alt_syscall_table)
700 return -ENOMEM;
701 j->flags.alt_syscall = 1;
702 return 0;
703}
704
Will Drewryf89aef52011-09-16 16:48:57 -0500705struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400706 size_t available;
707 size_t total;
708 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500709};
710
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800711void marshal_state_init(struct marshal_state *state, char *buf,
712 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400713{
714 state->available = available;
715 state->buf = buf;
716 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500717}
718
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800719void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400720{
721 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500722
Elly Jonese1749eb2011-10-07 13:54:59 -0400723 /* Up to |available| will be written. */
724 if (copy_len) {
725 memcpy(state->buf, src, copy_len);
726 state->buf += copy_len;
727 state->available -= copy_len;
728 }
729 /* |total| will contain the expected length. */
730 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500731}
732
Will Drewry6ac91122011-10-21 16:38:58 -0500733void minijail_marshal_helper(struct marshal_state *state,
734 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400735{
Dylan Reid648b2202015-10-23 00:50:00 -0700736 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800737 size_t i;
738
Elly Jonese1749eb2011-10-07 13:54:59 -0400739 marshal_append(state, (char *)j, sizeof(*j));
740 if (j->user)
741 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800742 if (j->suppl_gid_list) {
743 marshal_append(state, j->suppl_gid_list,
744 j->suppl_gid_count * sizeof(gid_t));
745 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400746 if (j->chrootdir)
747 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800748 if (j->alt_syscall_table) {
749 marshal_append(state, j->alt_syscall_table,
750 strlen(j->alt_syscall_table) + 1);
751 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800752 if (j->flags.seccomp_filter && j->filter_prog) {
753 struct sock_fprog *fp = j->filter_prog;
754 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800755 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400756 }
Dylan Reid648b2202015-10-23 00:50:00 -0700757 for (m = j->mounts_head; m; m = m->next) {
758 marshal_append(state, m->src, strlen(m->src) + 1);
759 marshal_append(state, m->dest, strlen(m->dest) + 1);
760 marshal_append(state, m->type, strlen(m->type) + 1);
761 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400762 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800763 for (i = 0; i < j->cgroup_count; ++i)
764 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500765}
766
Will Drewry6ac91122011-10-21 16:38:58 -0500767size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400768{
769 struct marshal_state state;
770 marshal_state_init(&state, NULL, 0);
771 minijail_marshal_helper(&state, j);
772 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500773}
774
Elly Jonese1749eb2011-10-07 13:54:59 -0400775int minijail_marshal(const struct minijail *j, char *buf, size_t available)
776{
777 struct marshal_state state;
778 marshal_state_init(&state, buf, available);
779 minijail_marshal_helper(&state, j);
780 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500781}
782
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800783/*
784 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
Elly Jones51a5b6c2011-10-12 19:09:26 -0400785 * @length Number of bytes to consume
786 * @buf Buffer to consume from
787 * @buflength Size of @buf
788 *
789 * Returns a pointer to the base of the bytes, or NULL for errors.
790 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700791void *consumebytes(size_t length, char **buf, size_t *buflength)
792{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400793 char *p = *buf;
794 if (length > *buflength)
795 return NULL;
796 *buf += length;
797 *buflength -= length;
798 return p;
799}
800
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800801/*
802 * consumestr: consumes a C string from a buffer @buf of length @length
Elly Jones51a5b6c2011-10-12 19:09:26 -0400803 * @buf Buffer to consume
804 * @length Length of buffer
805 *
806 * Returns a pointer to the base of the string, or NULL for errors.
807 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700808char *consumestr(char **buf, size_t *buflength)
809{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400810 size_t len = strnlen(*buf, *buflength);
811 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700812 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400813 return NULL;
814 return consumebytes(len + 1, buf, buflength);
815}
816
Elly Jonese1749eb2011-10-07 13:54:59 -0400817int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
818{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800819 size_t i;
820 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500821 int ret = -EINVAL;
822
Elly Jonese1749eb2011-10-07 13:54:59 -0400823 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500824 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400825 memcpy((void *)j, serialized, sizeof(*j));
826 serialized += sizeof(*j);
827 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500828
Will Drewrybee7ba72011-10-21 20:47:01 -0500829 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700830 j->mounts_head = NULL;
831 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800832 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500833
Elly Jonese1749eb2011-10-07 13:54:59 -0400834 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400835 char *user = consumestr(&serialized, &length);
836 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500837 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400838 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500839 if (!j->user)
840 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400841 }
Will Drewryf89aef52011-09-16 16:48:57 -0500842
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800843 if (j->suppl_gid_list) { /* stale pointer */
844 if (j->suppl_gid_count > NGROUPS_MAX) {
845 goto bad_gid_list;
846 }
847 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
848 void *gid_list_bytes =
849 consumebytes(gid_list_size, &serialized, &length);
850 if (!gid_list_bytes)
851 goto bad_gid_list;
852
853 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
854 if (!j->suppl_gid_list)
855 goto bad_gid_list;
856
857 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
858 }
859
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400860 if (j->chrootdir) { /* stale pointer */
861 char *chrootdir = consumestr(&serialized, &length);
862 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500863 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400864 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500865 if (!j->chrootdir)
866 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400867 }
868
Andrew Brestickereac28942015-11-11 16:04:46 -0800869 if (j->alt_syscall_table) { /* stale pointer */
870 char *alt_syscall_table = consumestr(&serialized, &length);
871 if (!alt_syscall_table)
872 goto bad_syscall_table;
873 j->alt_syscall_table = strdup(alt_syscall_table);
874 if (!j->alt_syscall_table)
875 goto bad_syscall_table;
876 }
877
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800878 if (j->flags.seccomp_filter && j->filter_len > 0) {
879 size_t ninstrs = j->filter_len;
880 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
881 ninstrs > USHRT_MAX)
882 goto bad_filters;
883
884 size_t program_len = ninstrs * sizeof(struct sock_filter);
885 void *program = consumebytes(program_len, &serialized, &length);
886 if (!program)
887 goto bad_filters;
888
889 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800890 if (!j->filter_prog)
891 goto bad_filters;
892
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800893 j->filter_prog->len = ninstrs;
894 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800895 if (!j->filter_prog->filter)
896 goto bad_filter_prog_instrs;
897
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800898 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400899 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400900
Dylan Reid648b2202015-10-23 00:50:00 -0700901 count = j->mounts_count;
902 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400903 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700904 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400905 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700906 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400907 const char *src = consumestr(&serialized, &length);
908 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700909 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400910 dest = consumestr(&serialized, &length);
911 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700912 goto bad_mounts;
913 type = consumestr(&serialized, &length);
914 if (!type)
915 goto bad_mounts;
916 flags = consumebytes(sizeof(*flags), &serialized, &length);
917 if (!flags)
918 goto bad_mounts;
919 if (minijail_mount(j, src, dest, type, *flags))
920 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400921 }
922
Dylan Reid605ce7f2016-01-19 19:21:00 -0800923 count = j->cgroup_count;
924 j->cgroup_count = 0;
925 for (i = 0; i < count; ++i) {
926 char *cgroup = consumestr(&serialized, &length);
927 if (!cgroup)
928 goto bad_cgroups;
929 j->cgroups[i] = strdup(cgroup);
930 if (!j->cgroups[i])
931 goto bad_cgroups;
932 ++j->cgroup_count;
933 }
934
Elly Jonese1749eb2011-10-07 13:54:59 -0400935 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500936
Dylan Reid605ce7f2016-01-19 19:21:00 -0800937bad_cgroups:
938 while (j->mounts_head) {
939 struct mountpoint *m = j->mounts_head;
940 j->mounts_head = j->mounts_head->next;
941 free(m->type);
942 free(m->dest);
943 free(m->src);
944 free(m);
945 }
946 for (i = 0; i < j->cgroup_count; ++i)
947 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -0700948bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800949 if (j->flags.seccomp_filter && j->filter_len > 0) {
950 free(j->filter_prog->filter);
951 free(j->filter_prog);
952 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800953bad_filter_prog_instrs:
954 if (j->filter_prog)
955 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -0500956bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800957 if (j->alt_syscall_table)
958 free(j->alt_syscall_table);
959bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500960 if (j->chrootdir)
961 free(j->chrootdir);
962bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800963 if (j->suppl_gid_list)
964 free(j->suppl_gid_list);
965bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -0500966 if (j->user)
967 free(j->user);
968clear_pointers:
969 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800970 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500971 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800972 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800973 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500974out:
975 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500976}
977
Dylan Reidce5b55e2016-01-13 11:04:16 -0800978static void write_ugid_mappings(const struct minijail *j)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800979{
980 int fd, ret, len;
981 size_t sz;
982 char fname[32];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800983
984 sz = sizeof(fname);
985 if (j->uidmap) {
986 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700987 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800988 die("failed to write file name of uid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -0800989 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800990 if (fd < 0)
991 pdie("failed to open '%s'", fname);
992 len = strlen(j->uidmap);
993 if (write(fd, j->uidmap, len) < len)
994 die("failed to set uid_map");
995 close(fd);
996 }
997 if (j->gidmap) {
998 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700999 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001000 die("failed to write file name of gid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001001 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001002 if (fd < 0)
1003 pdie("failed to open '%s'", fname);
1004 len = strlen(j->gidmap);
1005 if (write(fd, j->gidmap, len) < len)
1006 die("failed to set gid_map");
1007 close(fd);
1008 }
Dylan Reidce5b55e2016-01-13 11:04:16 -08001009}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001010
Dylan Reidce5b55e2016-01-13 11:04:16 -08001011static void parent_setup_complete(int *pipe_fds)
1012{
1013 close(pipe_fds[0]);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001014 close(pipe_fds[1]);
1015}
1016
Dylan Reidce5b55e2016-01-13 11:04:16 -08001017/*
1018 * wait_for_parent_setup: Called by the child process to wait for any
1019 * further parent-side setup to complete before continuing.
1020 */
1021static void wait_for_parent_setup(int *pipe_fds)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001022{
1023 char buf;
1024
1025 close(pipe_fds[1]);
1026
Dylan Reidce5b55e2016-01-13 11:04:16 -08001027 /* Wait for parent to complete setup and close the pipe. */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001028 if (read(pipe_fds[0], &buf, 1) != 0)
1029 die("failed to sync with parent");
1030 close(pipe_fds[0]);
Dylan Reidce5b55e2016-01-13 11:04:16 -08001031}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001032
Dylan Reidce5b55e2016-01-13 11:04:16 -08001033static void enter_user_namespace(const struct minijail *j)
1034{
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001035 if (j->uidmap && setresuid(0, 0, 0))
1036 pdie("setresuid");
1037 if (j->gidmap && setresgid(0, 0, 0))
1038 pdie("setresgid");
1039}
1040
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001041/*
1042 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001043 * @j Minijail these mounts are for
1044 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001045 *
1046 * Returns 0 for success.
1047 */
Dylan Reid648b2202015-10-23 00:50:00 -07001048static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001049{
Dylan Reid648b2202015-10-23 00:50:00 -07001050 int ret;
1051 char *dest;
1052 int remount_ro = 0;
1053
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001054 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001055 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001056 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001057
1058 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001059 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1060 * can't both be specified in the original bind mount.
1061 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001062 */
1063 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1064 remount_ro = 1;
1065 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001066 }
Dylan Reid648b2202015-10-23 00:50:00 -07001067
1068 ret = mount(m->src, dest, m->type, m->flags, NULL);
1069 if (ret)
1070 pdie("mount: %s -> %s", m->src, dest);
1071
1072 if (remount_ro) {
1073 m->flags |= MS_RDONLY;
1074 ret = mount(m->src, dest, NULL,
1075 m->flags | MS_REMOUNT, NULL);
1076 if (ret)
1077 pdie("bind ro: %s -> %s", m->src, dest);
1078 }
1079
Elly Jones51a5b6c2011-10-12 19:09:26 -04001080 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001081 if (m->next)
1082 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001083 return ret;
1084}
1085
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001086int enter_chroot(const struct minijail *j)
1087{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001088 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001089
1090 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001091 return ret;
1092
1093 if (chroot(j->chrootdir))
1094 return -errno;
1095
1096 if (chdir("/"))
1097 return -errno;
1098
1099 return 0;
1100}
1101
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001102int enter_pivot_root(const struct minijail *j)
1103{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001104 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001105
1106 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001107 return ret;
1108
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001109 /*
1110 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001111 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001112 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001113 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001114 if (oldroot < 0)
1115 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001116 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001117 if (newroot < 0)
1118 pdie("failed to open %s for fchdir", j->chrootdir);
1119
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001120 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001121 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001122 * do a self bind mount.
1123 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001124 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1125 pdie("failed to bind mount '%s'", j->chrootdir);
1126 if (chdir(j->chrootdir))
1127 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001128 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001129 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001130
1131 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001132 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001133 * change to the old root and unmount it.
1134 */
1135 if (fchdir(oldroot))
1136 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001137
1138 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001139 * If j->flags.skip_remount_private was enabled for minijail_enter(), there
1140 * could be a shared mount point under |oldroot|. In that case, mounts
1141 * under this shared mount point will be unmounted below, and this
1142 * unmounting will propagate to the original mount namespace (because the
1143 * mount point is shared). To prevent this unexpected unmounting, remove
1144 * these mounts from their peer groups by recursively remounting them as
1145 * MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001146 */
1147 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001148 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001149 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001150 if (umount2(".", MNT_DETACH))
1151 pdie("umount(/)");
1152 /* Change back to the new root. */
1153 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001154 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001155 if (close(oldroot))
1156 return -errno;
1157 if (close(newroot))
1158 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001159 if (chroot("/"))
1160 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001161 /* Set correct CWD for getcwd(3). */
1162 if (chdir("/"))
1163 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001164
1165 return 0;
1166}
1167
Lee Campbell11af0622014-05-22 12:36:04 -07001168int mount_tmp(void)
1169{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001170 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001171}
1172
Dylan Reid791f5772015-09-14 20:02:42 -07001173int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001174{
1175 const char *kProcPath = "/proc";
1176 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001177 /*
1178 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001179 * /proc in our namespace, which means using MS_REMOUNT here would
1180 * mutate our parent's mount as well, even though we're in a VFS
1181 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001182 * and make our own. However, if we are in a new user namespace, /proc
1183 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -04001184 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001185 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -04001186 return -errno;
1187 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1188 return -errno;
1189 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001190}
1191
Dylan Reid605ce7f2016-01-19 19:21:00 -08001192static void write_pid_to_path(pid_t pid, const char *path)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001193{
Dylan Reid605ce7f2016-01-19 19:21:00 -08001194 FILE *fp = fopen(path, "w");
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001195
1196 if (!fp)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001197 pdie("failed to open '%s'", path);
1198 if (fprintf(fp, "%d\n", (int)pid) < 0)
1199 pdie("fprintf(%s)", path);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001200 if (fclose(fp))
Dylan Reid605ce7f2016-01-19 19:21:00 -08001201 pdie("fclose(%s)", path);
1202}
1203
1204static void write_pid_file(const struct minijail *j)
1205{
1206 write_pid_to_path(j->initpid, j->pid_file_path);
1207}
1208
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001209static void add_to_cgroups(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001210{
1211 size_t i;
1212
1213 for (i = 0; i < j->cgroup_count; ++i)
1214 write_pid_to_path(j->initpid, j->cgroups[i]);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001215}
1216
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001217void drop_ugid(const struct minijail *j)
1218{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001219 if (j->flags.usergroups && j->flags.suppl_gids) {
1220 die("tried to inherit *and* set supplementary groups;"
1221 " can only do one");
1222 }
1223
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001224 if (j->flags.usergroups) {
1225 if (initgroups(j->user, j->usergid))
1226 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001227 } else if (j->flags.suppl_gids) {
1228 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1229 pdie("setgroups");
1230 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001231 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001232 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001233 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001234 * users.
1235 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001236 if ((j->uid || j->gid) && setgroups(0, NULL))
1237 pdie("setgroups");
1238 }
1239
1240 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1241 pdie("setresgid");
1242
1243 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1244 pdie("setresuid");
1245}
1246
Mike Frysinger3adfef72013-05-09 17:19:08 -04001247/*
1248 * We specifically do not use cap_valid() as that only tells us the last
1249 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001250 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001251 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001252 * kernel).
1253 * Normally, we suck up the answer via /proc. On Android, not all processes are
1254 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1255 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001256 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001257static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001258{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001259 unsigned int last_valid_cap = 0;
1260 if (is_android()) {
1261 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1262 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001263
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001264 /* |last_valid_cap| will be the first failing value. */
1265 if (last_valid_cap > 0) {
1266 last_valid_cap--;
1267 }
1268 } else {
1269 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1270 FILE *fp = fopen(cap_file, "re");
1271 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1272 pdie("fscanf(%s)", cap_file);
1273 fclose(fp);
1274 }
Dylan Reidf682d472015-09-17 21:39:07 -07001275 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001276}
1277
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001278static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1279{
1280 const uint64_t one = 1;
1281 unsigned int i;
1282 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1283 if (keep_mask & (one << i))
1284 continue;
1285 if (prctl(PR_CAPBSET_DROP, i))
1286 pdie("could not drop capability from bounding set");
1287 }
1288}
1289
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001290void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001291{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001292 if (!j->flags.use_caps)
1293 return;
1294
Elly Jonese1749eb2011-10-07 13:54:59 -04001295 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001296 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001297 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001298 unsigned int i;
1299 if (!caps)
1300 die("can't get process caps");
1301 if (cap_clear_flag(caps, CAP_INHERITABLE))
1302 die("can't clear inheritable caps");
1303 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1304 die("can't clear effective caps");
1305 if (cap_clear_flag(caps, CAP_PERMITTED))
1306 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001307 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001308 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001309 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001310 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001311 flag[0] = i;
1312 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001313 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001314 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001315 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001316 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001317 die("can't add inheritable cap");
1318 }
1319 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001320 die("can't apply initial cleaned capset");
1321
1322 /*
1323 * Instead of dropping bounding set first, do it here in case
1324 * the caller had a more permissive bounding set which could
1325 * have been used above to raise a capability that wasn't already
1326 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1327 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001328 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001329
1330 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001331 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001332 flag[0] = CAP_SETPCAP;
1333 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1334 die("can't clear effective cap");
1335 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1336 die("can't clear permitted cap");
1337 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1338 die("can't clear inheritable cap");
1339 }
1340
1341 if (cap_set_proc(caps))
1342 die("can't apply final cleaned capset");
1343
1344 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001345}
1346
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001347void set_seccomp_filter(const struct minijail *j)
1348{
1349 /*
1350 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1351 * in the kernel source tree for an explanation of the parameters.
1352 */
1353 if (j->flags.no_new_privs) {
1354 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1355 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1356 }
1357
1358 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001359 * Code running with ASan
1360 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1361 * will make system calls not included in the syscall filter policy,
1362 * which will likely crash the program. Skip setting seccomp filter in
1363 * that case.
1364 * 'running_with_asan()' has no inputs and is completely defined at
1365 * build time, so this cannot be used by an attacker to skip setting
1366 * seccomp filter.
1367 */
1368 if (j->flags.seccomp_filter && running_with_asan()) {
1369 warn("running with ASan, not setting seccomp filter");
1370 return;
1371 }
1372
1373 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001374 * If we're logging seccomp filter failures,
1375 * install the SIGSYS handler first.
1376 */
1377 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1378 if (install_sigsys_handler())
1379 pdie("install SIGSYS handler");
1380 warn("logging seccomp filter failures");
1381 }
1382
1383 /*
1384 * Install the syscall filter.
1385 */
1386 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001387 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1388 j->filter_prog)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001389 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001390 warn("seccomp not supported");
1391 return;
1392 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001393 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001394 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001395 }
1396}
1397
Will Drewry6ac91122011-10-21 16:38:58 -05001398void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001399{
Dylan Reidf682d472015-09-17 21:39:07 -07001400 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001401 * If we're dropping caps, get the last valid cap from /proc now,
1402 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001403 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001404 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001405 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001406 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001407
Elly Jonese1749eb2011-10-07 13:54:59 -04001408 if (j->flags.pids)
1409 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001410 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001411
Elly Jonese1749eb2011-10-07 13:54:59 -04001412 if (j->flags.usergroups && !j->user)
1413 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001414
Elly Jonesdd3e8512012-01-23 15:13:38 -05001415 /*
1416 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001417 * so we don't even try. If any of our operations fail, we abort() the
1418 * entire process.
1419 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001420 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1421 pdie("setns(CLONE_NEWNS)");
1422
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001423 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001424 if (unshare(CLONE_NEWNS))
1425 pdie("unshare(vfs)");
1426 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001427 * Unless asked not to, remount all filesystems as private.
1428 * If they are shared, new bind mounts will creep out of our
1429 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001430 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1431 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001432 if (!j->flags.skip_remount_private) {
1433 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1434 pdie("mount(/, private)");
1435 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001436 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001437
Dylan Reidf7942472015-11-18 17:55:26 -08001438 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1439 pdie("unshare(ipc)");
1440 }
1441
Dylan Reid1102f5a2015-09-15 11:52:20 -07001442 if (j->flags.enter_net) {
1443 if (setns(j->netns_fd, CLONE_NEWNET))
1444 pdie("setns(CLONE_NEWNET)");
1445 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001446 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001447 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001448
Elly Jones51a5b6c2011-10-12 19:09:26 -04001449 if (j->flags.chroot && enter_chroot(j))
1450 pdie("chroot");
1451
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001452 if (j->flags.pivot_root && enter_pivot_root(j))
1453 pdie("pivot_root");
1454
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001455 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001456 pdie("mount_tmp");
1457
Dylan Reid791f5772015-09-14 20:02:42 -07001458 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001459 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001460
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001461 /*
1462 * If we're only dropping capabilities from the bounding set, but not
1463 * from the thread's (permitted|inheritable|effective) sets, do it now.
1464 */
1465 if (j->flags.capbset_drop) {
1466 drop_capbset(j->cap_bset, last_valid_cap);
1467 }
1468
1469 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001470 /*
1471 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001472 * capability to change uids, our attempt to use setuid()
1473 * below will fail. Hang on to root caps across setuid(), then
1474 * lock securebits.
1475 */
1476 if (prctl(PR_SET_KEEPCAPS, 1))
1477 pdie("prctl(PR_SET_KEEPCAPS)");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001478
1479 /*
1480 * Kernels 4.3+ define a new securebit
1481 * (SECURE_NO_CAP_AMBIENT_RAISE), so using the SECURE_ALL_BITS
1482 * and SECURE_ALL_LOCKS masks from newer kernel headers will
1483 * return EPERM on older kernels. Detect this, and retry with
1484 * the right mask for older (2.6.26-4.2) kernels.
1485 */
1486 int securebits_ret = prctl(PR_SET_SECUREBITS,
1487 SECURE_ALL_BITS | SECURE_ALL_LOCKS);
1488 if (securebits_ret < 0) {
1489 if (errno == EPERM) {
1490 /* Possibly running on kernel < 4.3. */
1491 securebits_ret = prctl(
1492 PR_SET_SECUREBITS,
1493 OLD_SECURE_ALL_BITS | OLD_SECURE_ALL_LOCKS);
1494 }
1495 }
1496 if (securebits_ret < 0)
Elly Jonese1749eb2011-10-07 13:54:59 -04001497 pdie("prctl(PR_SET_SECUREBITS)");
1498 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001499
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001500 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001501 /*
1502 * If we're setting no_new_privs, we can drop privileges
1503 * before setting seccomp filter. This way filter policies
1504 * don't need to allow privilege-dropping syscalls.
1505 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001506 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001507 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001508 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001509 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001510 /*
1511 * If we're not setting no_new_privs,
1512 * we need to set seccomp filter *before* dropping privileges.
1513 * WARNING: this means that filter policies *must* allow
1514 * setgroups()/setresgid()/setresuid() for dropping root and
1515 * capget()/capset()/prctl() for dropping caps.
1516 */
1517 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001518 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001519 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001520 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001521
Elly Jonesdd3e8512012-01-23 15:13:38 -05001522 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001523 * Select the specified alternate syscall table. The table must not
1524 * block prctl(2) if we're using seccomp as well.
1525 */
1526 if (j->flags.alt_syscall) {
1527 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1528 pdie("prctl(PR_ALT_SYSCALL)");
1529 }
1530
1531 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001532 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001533 * privilege-dropping syscalls :)
1534 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001535 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001536 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001537 warn("seccomp not supported");
1538 return;
1539 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001540 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001541 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001542}
1543
Will Drewry6ac91122011-10-21 16:38:58 -05001544/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001545static int init_exitstatus = 0;
1546
Will Drewry6ac91122011-10-21 16:38:58 -05001547void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001548{
1549 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001550}
1551
Will Drewry6ac91122011-10-21 16:38:58 -05001552int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001553{
1554 pid_t pid;
1555 int status;
1556 /* so that we exit with the right status */
1557 signal(SIGTERM, init_term);
1558 /* TODO(wad) self jail with seccomp_filters here. */
1559 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001560 /*
1561 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001562 * left inside our pid namespace or we get a signal.
1563 */
1564 if (pid == rootpid)
1565 init_exitstatus = status;
1566 }
1567 if (!WIFEXITED(init_exitstatus))
1568 _exit(MINIJAIL_ERR_INIT);
1569 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001570}
1571
Will Drewry6ac91122011-10-21 16:38:58 -05001572int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001573{
1574 size_t sz = 0;
1575 size_t bytes = read(fd, &sz, sizeof(sz));
1576 char *buf;
1577 int r;
1578 if (sizeof(sz) != bytes)
1579 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001580 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001581 return -E2BIG;
1582 buf = malloc(sz);
1583 if (!buf)
1584 return -ENOMEM;
1585 bytes = read(fd, buf, sz);
1586 if (bytes != sz) {
1587 free(buf);
1588 return -EINVAL;
1589 }
1590 r = minijail_unmarshal(j, buf, sz);
1591 free(buf);
1592 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001593}
1594
Will Drewry6ac91122011-10-21 16:38:58 -05001595int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001596{
1597 char *buf;
1598 size_t sz = minijail_size(j);
1599 ssize_t written;
1600 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001601
Elly Jonese1749eb2011-10-07 13:54:59 -04001602 if (!sz)
1603 return -EINVAL;
1604 buf = malloc(sz);
1605 r = minijail_marshal(j, buf, sz);
1606 if (r) {
1607 free(buf);
1608 return r;
1609 }
1610 /* Sends [size][minijail]. */
1611 written = write(fd, &sz, sizeof(sz));
1612 if (written != sizeof(sz)) {
1613 free(buf);
1614 return -EFAULT;
1615 }
1616 written = write(fd, buf, sz);
1617 if (written < 0 || (size_t) written != sz) {
1618 free(buf);
1619 return -EFAULT;
1620 }
1621 free(buf);
1622 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001623}
Elly Jonescd7a9042011-07-22 13:56:51 -04001624
Will Drewry6ac91122011-10-21 16:38:58 -05001625int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001626{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001627#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001628 /* Don't use LDPRELOAD on Brillo. */
1629 return 0;
1630#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001631 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1632 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1633 if (!newenv)
1634 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001635
Elly Jonese1749eb2011-10-07 13:54:59 -04001636 /* Only insert a separating space if we have something to separate... */
1637 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1638 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001639
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001640 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001641 setenv(kLdPreloadEnvVar, newenv, 1);
1642 free(newenv);
1643 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001644#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001645}
1646
Will Drewry6ac91122011-10-21 16:38:58 -05001647int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001648{
1649 int r = pipe(fds);
1650 char fd_buf[11];
1651 if (r)
1652 return r;
1653 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1654 if (r <= 0)
1655 return -EINVAL;
1656 setenv(kFdEnvVar, fd_buf, 1);
1657 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001658}
1659
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001660int setup_pipe_end(int fds[2], size_t index)
1661{
1662 if (index > 1)
1663 return -1;
1664
1665 close(fds[1 - index]);
1666 return fds[index];
1667}
1668
1669int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1670{
1671 if (index > 1)
1672 return -1;
1673
1674 close(fds[1 - index]);
1675 /* dup2(2) the corresponding end of the pipe into |fd|. */
1676 return dup2(fds[index], fd);
1677}
1678
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001679int minijail_run_internal(struct minijail *j, const char *filename,
1680 char *const argv[], pid_t *pchild_pid,
1681 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1682 int use_preload);
1683
Will Drewry6ac91122011-10-21 16:38:58 -05001684int API minijail_run(struct minijail *j, const char *filename,
1685 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001686{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001687 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1688 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001689}
1690
1691int API minijail_run_pid(struct minijail *j, const char *filename,
1692 char *const argv[], pid_t *pchild_pid)
1693{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001694 return minijail_run_internal(j, filename, argv, pchild_pid,
1695 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001696}
1697
1698int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001699 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001700{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001701 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1702 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001703}
1704
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001705int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001706 char *const argv[], pid_t *pchild_pid,
1707 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001708{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001709 return minijail_run_internal(j, filename, argv, pchild_pid,
1710 pstdin_fd, pstdout_fd, pstderr_fd, true);
1711}
1712
1713int API minijail_run_no_preload(struct minijail *j, const char *filename,
1714 char *const argv[])
1715{
1716 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1717 false);
1718}
1719
Samuel Tan63187f42015-10-16 13:01:53 -07001720int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001721 const char *filename,
1722 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001723 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001724 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001725 int *pstderr_fd)
1726{
Samuel Tan63187f42015-10-16 13:01:53 -07001727 return minijail_run_internal(j, filename, argv, pchild_pid,
1728 pstdin_fd, pstdout_fd, pstderr_fd, false);
1729}
1730
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001731int minijail_run_internal(struct minijail *j, const char *filename,
1732 char *const argv[], pid_t *pchild_pid,
1733 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1734 int use_preload)
1735{
Elly Jonese1749eb2011-10-07 13:54:59 -04001736 char *oldenv, *oldenv_copy = NULL;
1737 pid_t child_pid;
1738 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001739 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001740 int stdout_fds[2];
1741 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001742 int child_sync_pipe_fds[2];
1743 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001744 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001745 /* We need to remember this across the minijail_preexec() call. */
1746 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001747 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001748
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001749 if (use_preload) {
1750 oldenv = getenv(kLdPreloadEnvVar);
1751 if (oldenv) {
1752 oldenv_copy = strdup(oldenv);
1753 if (!oldenv_copy)
1754 return -ENOMEM;
1755 }
1756
1757 if (setup_preload())
1758 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001759 }
Will Drewryf89aef52011-09-16 16:48:57 -05001760
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001761 if (!use_preload) {
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001762 if (j->flags.use_caps)
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001763 die("capabilities are not supported without "
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001764 "LD_PRELOAD");
1765 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001766
Elly Jonesdd3e8512012-01-23 15:13:38 -05001767 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001768 * Make the process group ID of this process equal to its PID, so that
1769 * both the Minijail process and the jailed process can be killed
1770 * together.
1771 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1772 * the process is already a process group leader.
1773 */
1774 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1775 if (errno != EPERM) {
1776 pdie("setpgid(0, 0)");
1777 }
1778 }
1779
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001780 if (use_preload) {
1781 /*
1782 * Before we fork(2) and execve(2) the child process, we need
1783 * to open a pipe(2) to send the minijail configuration over.
1784 */
1785 if (setup_pipe(pipe_fds))
1786 return -EFAULT;
1787 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001788
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001789 /*
1790 * If we want to write to the child process' standard input,
1791 * create the pipe(2) now.
1792 */
1793 if (pstdin_fd) {
1794 if (pipe(stdin_fds))
1795 return -EFAULT;
1796 }
1797
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001798 /*
1799 * If we want to read from the child process' standard output,
1800 * create the pipe(2) now.
1801 */
1802 if (pstdout_fd) {
1803 if (pipe(stdout_fds))
1804 return -EFAULT;
1805 }
1806
1807 /*
1808 * If we want to read from the child process' standard error,
1809 * create the pipe(2) now.
1810 */
1811 if (pstderr_fd) {
1812 if (pipe(stderr_fds))
1813 return -EFAULT;
1814 }
1815
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001816 /*
1817 * If we want to set up a new uid/gid mapping in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001818 * or if we need to add the child process to cgroups, create the pipe(2)
1819 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001820 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001821 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001822 sync_child = 1;
1823 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001824 return -EFAULT;
1825 }
1826
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001827 /*
1828 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001829 *
1830 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1831 *
1832 * In multithreaded programs, there are a bunch of locks inside libc,
1833 * some of which may be held by other threads at the time that we call
1834 * minijail_run_pid(). If we call fork(), glibc does its level best to
1835 * ensure that we hold all of these locks before it calls clone()
1836 * internally and drop them after clone() returns, but when we call
1837 * sys_clone(2) directly, all that gets bypassed and we end up with a
1838 * child address space where some of libc's important locks are held by
1839 * other threads (which did not get cloned, and hence will never release
1840 * those locks). This is okay so long as we call exec() immediately
1841 * after, but a bunch of seemingly-innocent libc functions like setenv()
1842 * take locks.
1843 *
1844 * Hence, only call sys_clone() if we need to, in order to get at pid
1845 * namespacing. If we follow this path, the child's address space might
1846 * have broken locks; you may only call functions that do not acquire
1847 * any locks.
1848 *
1849 * Unfortunately, fork() acquires every lock it can get its hands on, as
1850 * previously detailed, so this function is highly likely to deadlock
1851 * later on (see "deadlock here") if we're multithreaded.
1852 *
1853 * We might hack around this by having the clone()d child (init of the
1854 * pid namespace) return directly, rather than leaving the clone()d
1855 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001856 * its fork()ed child return in turn), but that process would be
1857 * crippled with its libc locks potentially broken. We might try
1858 * fork()ing in the parent before we clone() to ensure that we own all
1859 * the locks, but then we have to have the forked child hanging around
1860 * consuming resources (and possibly having file descriptors / shared
1861 * memory regions / etc attached). We'd need to keep the child around to
1862 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04001863 *
1864 * TODO(ellyjones): figure out if the "forked child hanging around"
1865 * problem is fixable or not. It would be nice if we worked in this
1866 * case.
1867 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001868 if (pid_namespace) {
1869 int clone_flags = CLONE_NEWPID | SIGCHLD;
1870 if (j->flags.userns)
1871 clone_flags |= CLONE_NEWUSER;
1872 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001873 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001874 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001875 }
Elly Jones761b7412012-06-13 15:49:52 -04001876
Elly Jonese1749eb2011-10-07 13:54:59 -04001877 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001878 if (use_preload) {
1879 free(oldenv_copy);
1880 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001881 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001882 }
Will Drewryf89aef52011-09-16 16:48:57 -05001883
Elly Jonese1749eb2011-10-07 13:54:59 -04001884 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001885 if (use_preload) {
1886 /* Restore parent's LD_PRELOAD. */
1887 if (oldenv_copy) {
1888 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1889 free(oldenv_copy);
1890 } else {
1891 unsetenv(kLdPreloadEnvVar);
1892 }
1893 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001894 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001895
Elly Jonese1749eb2011-10-07 13:54:59 -04001896 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001897
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001898 if (j->flags.pid_file)
1899 write_pid_file(j);
1900
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001901 if (j->flags.cgroups)
1902 add_to_cgroups(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001903
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001904 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001905 write_ugid_mappings(j);
1906
1907 if (sync_child)
1908 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001909
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001910 if (use_preload) {
1911 /* Send marshalled minijail. */
1912 close(pipe_fds[0]); /* read endpoint */
1913 ret = minijail_to_fd(j, pipe_fds[1]);
1914 close(pipe_fds[1]); /* write endpoint */
1915 if (ret) {
1916 kill(j->initpid, SIGKILL);
1917 die("failed to send marshalled minijail");
1918 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001919 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001920
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001921 if (pchild_pid)
1922 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001923
1924 /*
1925 * If we want to write to the child process' standard input,
1926 * set up the write end of the pipe.
1927 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001928 if (pstdin_fd)
1929 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001930 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001931
1932 /*
1933 * If we want to read from the child process' standard output,
1934 * set up the read end of the pipe.
1935 */
1936 if (pstdout_fd)
1937 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001938 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001939
1940 /*
1941 * If we want to read from the child process' standard error,
1942 * set up the read end of the pipe.
1943 */
1944 if (pstderr_fd)
1945 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001946 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001947
Elly Jonese1749eb2011-10-07 13:54:59 -04001948 return 0;
1949 }
1950 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001951
Peter Qiu2860c462015-12-16 15:13:06 -08001952 if (j->flags.reset_signal_mask) {
1953 sigset_t signal_mask;
1954 if (sigemptyset(&signal_mask) != 0)
1955 pdie("sigemptyset failed");
1956 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
1957 pdie("sigprocmask failed");
1958 }
1959
Dylan Reidce5b55e2016-01-13 11:04:16 -08001960 if (sync_child)
1961 wait_for_parent_setup(child_sync_pipe_fds);
1962
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001963 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001964 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001965
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001966 /*
1967 * If we want to write to the jailed process' standard input,
1968 * set up the read end of the pipe.
1969 */
1970 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001971 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1972 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001973 die("failed to set up stdin pipe");
1974 }
1975
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001976 /*
1977 * If we want to read from the jailed process' standard output,
1978 * set up the write end of the pipe.
1979 */
1980 if (pstdout_fd) {
1981 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1982 STDOUT_FILENO) < 0)
1983 die("failed to set up stdout pipe");
1984 }
1985
1986 /*
1987 * If we want to read from the jailed process' standard error,
1988 * set up the write end of the pipe.
1989 */
1990 if (pstderr_fd) {
1991 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1992 STDERR_FILENO) < 0)
1993 die("failed to set up stderr pipe");
1994 }
1995
Dylan Reid791f5772015-09-14 20:02:42 -07001996 /* If running an init program, let it decide when/how to mount /proc. */
1997 if (pid_namespace && !do_init)
1998 j->flags.remount_proc_ro = 0;
1999
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002000 if (use_preload) {
2001 /* Strip out flags that cannot be inherited across execve(2). */
2002 minijail_preexec(j);
2003 } else {
2004 j->flags.pids = 0;
2005 }
2006 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002007 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002008
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002009 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002010 /*
2011 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002012 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002013 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002014 * a child to actually run the program. If |do_init == 0|, we
2015 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002016 *
2017 * If we're multithreaded, we'll probably deadlock here. See
2018 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002019 */
2020 child_pid = fork();
2021 if (child_pid < 0)
2022 _exit(child_pid);
2023 else if (child_pid > 0)
2024 init(child_pid); /* never returns */
2025 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002026
Elly Jonesdd3e8512012-01-23 15:13:38 -05002027 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002028 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002029 * calling process
2030 * -> execve()-ing process
2031 * If we are:
2032 * calling process
2033 * -> init()-ing process
2034 * -> execve()-ing process
2035 */
2036 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04002037}
2038
Will Drewry6ac91122011-10-21 16:38:58 -05002039int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002040{
2041 int st;
2042 if (kill(j->initpid, SIGTERM))
2043 return -errno;
2044 if (waitpid(j->initpid, &st, 0) < 0)
2045 return -errno;
2046 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002047}
2048
Will Drewry6ac91122011-10-21 16:38:58 -05002049int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002050{
2051 int st;
2052 if (waitpid(j->initpid, &st, 0) < 0)
2053 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002054
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002055 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002056 int error_status = st;
2057 if (WIFSIGNALED(st)) {
2058 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002059 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002060 j->initpid, signum);
2061 /*
2062 * We return MINIJAIL_ERR_JAIL if the process received
2063 * SIGSYS, which happens when a syscall is blocked by
2064 * seccomp filters.
2065 * If not, we do what bash(1) does:
2066 * $? = 128 + signum
2067 */
2068 if (signum == SIGSYS) {
2069 error_status = MINIJAIL_ERR_JAIL;
2070 } else {
2071 error_status = 128 + signum;
2072 }
2073 }
2074 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002075 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002076
2077 int exit_status = WEXITSTATUS(st);
2078 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002079 info("child process %d exited with status %d",
2080 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002081
2082 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002083}
2084
Will Drewry6ac91122011-10-21 16:38:58 -05002085void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002086{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002087 size_t i;
2088
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002089 if (j->flags.seccomp_filter && j->filter_prog) {
2090 free(j->filter_prog->filter);
2091 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002092 }
Dylan Reid648b2202015-10-23 00:50:00 -07002093 while (j->mounts_head) {
2094 struct mountpoint *m = j->mounts_head;
2095 j->mounts_head = j->mounts_head->next;
2096 free(m->type);
2097 free(m->dest);
2098 free(m->src);
2099 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002100 }
Dylan Reid648b2202015-10-23 00:50:00 -07002101 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002102 if (j->user)
2103 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002104 if (j->suppl_gid_list)
2105 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002106 if (j->chrootdir)
2107 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08002108 if (j->alt_syscall_table)
2109 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002110 for (i = 0; i < j->cgroup_count; ++i)
2111 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002112 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002113}