blob: a0c4c86d722c7cbb441a88fd3ff965b99fc98506 [file] [log] [blame]
Jorge Lucangeli Obesd613ab22015-03-03 14:22:50 -08001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04002 * Use of this source code is governed by a BSD-style license that can be
Will Drewry32ac9f52011-08-18 21:36:27 -05003 * found in the LICENSE file.
4 */
Elly Jonescd7a9042011-07-22 13:56:51 -04005
6#define _BSD_SOURCE
7#define _GNU_SOURCE
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07008
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08009#include <asm/unistd.h>
Will Drewry32ac9f52011-08-18 21:36:27 -050010#include <ctype.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040011#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070012#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040013#include <grp.h>
14#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050015#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040016#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040017#include <pwd.h>
18#include <sched.h>
19#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050020#include <stdarg.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070021#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080022#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <syscall.h>
27#include <sys/capability.h>
28#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050029#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040030#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070031#include <sys/stat.h>
32#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080033#include <sys/user.h>
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -080034#include <sys/utsname.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040035#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040036#include <unistd.h>
37
38#include "libminijail.h"
39#include "libminijail-private.h"
40
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070041#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080042#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070043#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080044
Lei Zhangeee31552012-10-17 21:27:10 -070045#ifdef HAVE_SECUREBITS_H
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070046# include <linux/securebits.h>
Lei Zhangeee31552012-10-17 21:27:10 -070047#else
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070048# define SECURE_ALL_BITS 0x55
49# define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
50#endif
51/* For kernels < 4.3. */
52#define OLD_SECURE_ALL_BITS 0x15
53#define OLD_SECURE_ALL_LOCKS (OLD_SECURE_ALL_BITS << 1)
54
55/*
56 * Assert the value of SECURE_ALL_BITS at compile-time.
57 * Brillo devices are currently compiled against 4.4 kernel headers. Kernel 4.3
58 * added a new securebit.
59 * When a new securebit is added, the new SECURE_ALL_BITS mask will return EPERM
60 * when used on older kernels. The compile-time assert will catch this situation
61 * at compile time.
62 */
63#ifdef __BRILLO__
64_Static_assert(SECURE_ALL_BITS == 0x55, "SECURE_ALL_BITS == 0x55.");
Lei Zhangeee31552012-10-17 21:27:10 -070065#endif
66
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070067/* Until these are reliably available in linux/prctl.h. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080068#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070069# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080070#endif
71
Andrew Brestickereac28942015-11-11 16:04:46 -080072#ifndef PR_ALT_SYSCALL
73# define PR_ALT_SYSCALL 0x43724f53
74#endif
75
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080076/* For seccomp_filter using BPF. */
77#ifndef PR_SET_NO_NEW_PRIVS
78# define PR_SET_NO_NEW_PRIVS 38
79#endif
80#ifndef SECCOMP_MODE_FILTER
81# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050082#endif
83
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070084#ifdef USE_SECCOMP_SOFTFAIL
85# define SECCOMP_SOFTFAIL 1
86#else
87# define SECCOMP_SOFTFAIL 0
88#endif
89
Dylan Reid605ce7f2016-01-19 19:21:00 -080090#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
91
Dylan Reid648b2202015-10-23 00:50:00 -070092struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040093 char *src;
94 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070095 char *type;
96 unsigned long flags;
97 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040098};
99
Will Drewryf89aef52011-09-16 16:48:57 -0500100struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700101 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700102 * WARNING: if you add a flag here you need to make sure it's
103 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700104 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400105 struct {
106 int uid:1;
107 int gid:1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800108 int usergroups:1;
109 int suppl_gids:1;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800110 int use_caps:1;
111 int capbset_drop:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400112 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700113 int enter_vfs:1;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800114 int skip_remount_private:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400115 int pids:1;
Dylan Reidf7942472015-11-18 17:55:26 -0800116 int ipc:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400117 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700118 int enter_net:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800119 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400120 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -0700121 int remount_proc_ro:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700122 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400123 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700124 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400125 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800126 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700127 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800128 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800129 int pid_file:1;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800130 int cgroups:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800131 int alt_syscall:1;
Peter Qiu2860c462015-12-16 15:13:06 -0800132 int reset_signal_mask:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400133 } flags;
134 uid_t uid;
135 gid_t gid;
136 gid_t usergid;
137 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800138 size_t suppl_gid_count;
139 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400140 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800141 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400142 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700143 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700144 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400145 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800146 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800147 char *uidmap;
148 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800149 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800150 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800151 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700152 struct mountpoint *mounts_head;
153 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800154 size_t mounts_count;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800155 char *cgroups[MAX_CGROUPS];
156 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500157};
158
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700159/*
160 * Strip out flags meant for the parent.
161 * We keep things that are not inherited across execve(2) (e.g. capabilities),
162 * or are easier to set after execve(2) (e.g. seccomp filters).
163 */
164void minijail_preenter(struct minijail *j)
165{
166 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700167 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900168 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700169 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700170 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800171 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800172 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800173 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700174}
175
176/*
177 * Strip out flags meant for the child.
178 * We keep things that are inherited across execve(2).
179 */
180void minijail_preexec(struct minijail *j)
181{
182 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700183 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800184 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700185 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800186 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700187 if (j->user)
188 free(j->user);
189 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800190 if (j->suppl_gid_list)
191 free(j->suppl_gid_list);
192 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700193 memset(&j->flags, 0, sizeof(j->flags));
194 /* Now restore anything we meant to keep. */
195 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700196 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800197 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700198 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800199 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700200 /* Note, |pids| will already have been used before this call. */
201}
202
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800203/* Returns true if the kernel version is less than 3.8. */
204int seccomp_kernel_support_not_required()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800205{
206 int major, minor;
207 struct utsname uts;
208 return (uname(&uts) != -1 &&
209 sscanf(uts.release, "%d.%d", &major, &minor) == 2 &&
210 ((major < 3) || ((major == 3) && (minor < 8))));
211}
212
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800213/* Allow seccomp soft-fail on Android devices with kernel version < 3.8. */
214int can_softfail()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800215{
216#if SECCOMP_SOFTFAIL
217 if (is_android()) {
218 if (seccomp_kernel_support_not_required())
219 return 1;
220 else
221 return 0;
222 } else {
223 return 1;
224 }
225#endif
226 return 0;
227}
228
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700229/* Minijail API. */
230
Will Drewry6ac91122011-10-21 16:38:58 -0500231struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400232{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400233 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400234}
235
Will Drewry6ac91122011-10-21 16:38:58 -0500236void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400237{
238 if (uid == 0)
239 die("useless change to uid 0");
240 j->uid = uid;
241 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400242}
243
Will Drewry6ac91122011-10-21 16:38:58 -0500244void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400245{
246 if (gid == 0)
247 die("useless change to gid 0");
248 j->gid = gid;
249 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400250}
251
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800252void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
253 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800254{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800255 size_t i;
256
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800257 if (j->flags.usergroups)
258 die("cannot inherit *and* set supplementary groups");
259
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800260 if (size == 0) {
261 /* Clear supplementary groups. */
262 j->suppl_gid_list = NULL;
263 j->suppl_gid_count = 0;
264 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800265 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800266 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800267
268 /* Copy the gid_t array. */
269 j->suppl_gid_list = calloc(size, sizeof(gid_t));
270 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800271 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800272 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800273 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800274 j->suppl_gid_list[i] = list[i];
275 }
276 j->suppl_gid_count = size;
277 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800278}
279
Will Drewry6ac91122011-10-21 16:38:58 -0500280int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400281{
282 char *buf = NULL;
283 struct passwd pw;
284 struct passwd *ppw = NULL;
285 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
286 if (sz == -1)
287 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400288
Elly Jonesdd3e8512012-01-23 15:13:38 -0500289 /*
290 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400291 * the maximum needed size of the buffer, so we don't have to search.
292 */
293 buf = malloc(sz);
294 if (!buf)
295 return -ENOMEM;
296 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500297 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800298 * We're safe to free the buffer here. The strings inside |pw| point
299 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800300 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
301 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500302 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400303 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700304 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400305 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700306 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400307 minijail_change_uid(j, ppw->pw_uid);
308 j->user = strdup(user);
309 if (!j->user)
310 return -ENOMEM;
311 j->usergid = ppw->pw_gid;
312 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400313}
314
Will Drewry6ac91122011-10-21 16:38:58 -0500315int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400316{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700317 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700318 struct group gr;
319 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400320 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
321 if (sz == -1)
322 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400323
Elly Jonesdd3e8512012-01-23 15:13:38 -0500324 /*
325 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400326 * the maximum needed size of the buffer, so we don't have to search.
327 */
328 buf = malloc(sz);
329 if (!buf)
330 return -ENOMEM;
331 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500332 /*
333 * We're safe to free the buffer here. The strings inside gr point
334 * inside buf, but we don't use any of them; this leaves the pointers
335 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
336 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400337 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700338 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400339 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700340 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400341 minijail_change_gid(j, pgr->gr_gid);
342 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400343}
344
Will Drewry6ac91122011-10-21 16:38:58 -0500345void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400346{
347 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400348}
349
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700350void API minijail_no_new_privs(struct minijail *j)
351{
352 j->flags.no_new_privs = 1;
353}
354
Will Drewry6ac91122011-10-21 16:38:58 -0500355void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400356{
357 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500358}
359
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700360void API minijail_log_seccomp_filter_failures(struct minijail *j)
361{
362 j->flags.log_seccomp_filter = 1;
363}
364
Will Drewry6ac91122011-10-21 16:38:58 -0500365void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400366{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800367 /*
368 * 'minijail_use_caps' configures a runtime-capabilities-only
369 * environment, including a bounding set matching the thread's runtime
370 * (permitted|inheritable|effective) sets.
371 * Therefore, it will override any existing bounding set configurations
372 * since the latter would allow gaining extra runtime capabilities from
373 * file capabilities.
374 */
375 if (j->flags.capbset_drop) {
376 warn("overriding bounding set configuration");
377 j->cap_bset = 0;
378 j->flags.capbset_drop = 0;
379 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400380 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800381 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400382}
383
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800384void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
385{
386 if (j->flags.use_caps) {
387 /*
388 * 'minijail_use_caps' will have already configured a capability
389 * bounding set matching the (permitted|inheritable|effective)
390 * sets. Abort if the user tries to configure a separate
391 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
392 * are mutually exclusive.
393 */
394 die("runtime capabilities already configured, can't drop "
395 "bounding set separately");
396 }
397 j->cap_bset = capmask;
398 j->flags.capbset_drop = 1;
399}
400
401void API minijail_reset_signal_mask(struct minijail *j)
402{
Peter Qiu2860c462015-12-16 15:13:06 -0800403 j->flags.reset_signal_mask = 1;
404}
405
Will Drewry6ac91122011-10-21 16:38:58 -0500406void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400407{
408 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400409}
410
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700411void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
412{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800413 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700414 if (ns_fd < 0) {
415 pdie("failed to open namespace '%s'", ns_path);
416 }
417 j->mountns_fd = ns_fd;
418 j->flags.enter_vfs = 1;
419}
420
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800421void API minijail_skip_remount_private(struct minijail *j)
422{
423 j->flags.skip_remount_private = 1;
424}
425
Will Drewry6ac91122011-10-21 16:38:58 -0500426void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400427{
Elly Jonese58176c2012-01-23 11:46:17 -0500428 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700429 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400430 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800431 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400432}
433
Dylan Reidf7942472015-11-18 17:55:26 -0800434void API minijail_namespace_ipc(struct minijail *j)
435{
436 j->flags.ipc = 1;
437}
438
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400439void API minijail_namespace_net(struct minijail *j)
440{
441 j->flags.net = 1;
442}
443
Dylan Reid1102f5a2015-09-15 11:52:20 -0700444void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
445{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800446 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700447 if (ns_fd < 0) {
448 pdie("failed to open namespace '%s'", ns_path);
449 }
450 j->netns_fd = ns_fd;
451 j->flags.enter_net = 1;
452}
453
Dylan Reid791f5772015-09-14 20:02:42 -0700454void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400455{
456 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700457 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400458}
459
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800460void API minijail_namespace_user(struct minijail *j)
461{
462 j->flags.userns = 1;
463}
464
465int API minijail_uidmap(struct minijail *j, const char *uidmap)
466{
467 j->uidmap = strdup(uidmap);
468 if (!j->uidmap)
469 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800470 char *ch;
471 for (ch = j->uidmap; *ch; ch++) {
472 if (*ch == ',')
473 *ch = '\n';
474 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800475 return 0;
476}
477
478int API minijail_gidmap(struct minijail *j, const char *gidmap)
479{
480 j->gidmap = strdup(gidmap);
481 if (!j->gidmap)
482 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800483 char *ch;
484 for (ch = j->gidmap; *ch; ch++) {
485 if (*ch == ',')
486 *ch = '\n';
487 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800488 return 0;
489}
490
Will Drewry6ac91122011-10-21 16:38:58 -0500491void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400492{
493 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400494}
495
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800496void API minijail_run_as_init(struct minijail *j)
497{
498 /*
499 * Since the jailed program will become 'init' in the new PID namespace,
500 * Minijail does not need to fork an 'init' process.
501 */
502 j->flags.do_init = 0;
503}
504
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700505int API minijail_enter_chroot(struct minijail *j, const char *dir)
506{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400507 if (j->chrootdir)
508 return -EINVAL;
509 j->chrootdir = strdup(dir);
510 if (!j->chrootdir)
511 return -ENOMEM;
512 j->flags.chroot = 1;
513 return 0;
514}
515
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800516int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
517{
518 if (j->chrootdir)
519 return -EINVAL;
520 j->chrootdir = strdup(dir);
521 if (!j->chrootdir)
522 return -ENOMEM;
523 j->flags.pivot_root = 1;
524 return 0;
525}
526
Dylan Reida14e08d2015-10-22 21:05:29 -0700527static char *append_external_path(const char *external_path,
528 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700529{
Dylan Reida14e08d2015-10-22 21:05:29 -0700530 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700531 size_t pathlen;
532
Dylan Reid08946cc2015-09-16 19:10:57 -0700533 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700534 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
535 path = malloc(pathlen);
536 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700537
Dylan Reida14e08d2015-10-22 21:05:29 -0700538 return path;
539}
540
541char API *minijail_get_original_path(struct minijail *j,
542 const char *path_inside_chroot)
543{
Dylan Reid648b2202015-10-23 00:50:00 -0700544 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700545
Dylan Reid648b2202015-10-23 00:50:00 -0700546 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700547 while (b) {
548 /*
549 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700550 * mount, then the original path is exactly the source of
551 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700552 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700553 * mount source = /some/path/exe, mount dest =
554 * /chroot/path/exe Then when getting the original path of
555 * "/chroot/path/exe", the source of that mount,
556 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700557 */
558 if (!strcmp(b->dest, path_inside_chroot))
559 return strdup(b->src);
560
561 /*
562 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700563 * mount, take the suffix of the chroot path relative to the
564 * mount destination path, and append it to the mount source
565 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700566 */
567 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
568 const char *relative_path =
569 path_inside_chroot + strlen(b->dest);
570 return append_external_path(b->src, relative_path);
571 }
572 b = b->next;
573 }
574
575 /* If there is a chroot path, append |path_inside_chroot| to that. */
576 if (j->chrootdir)
577 return append_external_path(j->chrootdir, path_inside_chroot);
578
579 /* No chroot, so the path outside is the same as it is inside. */
580 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700581}
582
Lee Campbell11af0622014-05-22 12:36:04 -0700583void API minijail_mount_tmp(struct minijail *j)
584{
585 j->flags.mount_tmp = 1;
586}
587
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800588int API minijail_write_pid_file(struct minijail *j, const char *path)
589{
590 j->pid_file_path = strdup(path);
591 if (!j->pid_file_path)
592 return -ENOMEM;
593 j->flags.pid_file = 1;
594 return 0;
595}
596
Dylan Reid605ce7f2016-01-19 19:21:00 -0800597int API minijail_add_to_cgroup(struct minijail *j, const char *path)
598{
599 if (j->cgroup_count >= MAX_CGROUPS)
600 return -ENOMEM;
601 j->cgroups[j->cgroup_count] = strdup(path);
602 if (!j->cgroups[j->cgroup_count])
603 return -ENOMEM;
604 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800605 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800606 return 0;
607}
608
Dylan Reid648b2202015-10-23 00:50:00 -0700609int API minijail_mount(struct minijail *j, const char *src, const char *dest,
610 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700611{
Dylan Reid648b2202015-10-23 00:50:00 -0700612 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400613
614 if (*dest != '/')
615 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700616 m = calloc(1, sizeof(*m));
617 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400618 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700619 m->dest = strdup(dest);
620 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400621 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700622 m->src = strdup(src);
623 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400624 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700625 m->type = strdup(type);
626 if (!m->type)
627 goto error;
628 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400629
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800630 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400631
Elly Jonesdd3e8512012-01-23 15:13:38 -0500632 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700633 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400634 * containing vfs namespace.
635 */
636 minijail_namespace_vfs(j);
637
Dylan Reid648b2202015-10-23 00:50:00 -0700638 if (j->mounts_tail)
639 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400640 else
Dylan Reid648b2202015-10-23 00:50:00 -0700641 j->mounts_head = m;
642 j->mounts_tail = m;
643 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400644
645 return 0;
646
647error:
Dylan Reid648b2202015-10-23 00:50:00 -0700648 free(m->src);
649 free(m->dest);
650 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400651 return -ENOMEM;
652}
653
Dylan Reid648b2202015-10-23 00:50:00 -0700654int API minijail_bind(struct minijail *j, const char *src, const char *dest,
655 int writeable)
656{
657 unsigned long flags = MS_BIND;
658
659 if (!writeable)
660 flags |= MS_RDONLY;
661
662 return minijail_mount(j, src, dest, "", flags);
663}
664
Will Drewry6ac91122011-10-21 16:38:58 -0500665void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400666{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700667 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800668 if ((errno == EINVAL) && can_softfail()) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800669 warn("not loading seccomp filter,"
670 " seccomp not supported");
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800671 j->flags.seccomp_filter = 0;
672 j->flags.log_seccomp_filter = 0;
673 j->filter_len = 0;
674 j->filter_prog = NULL;
675 j->flags.no_new_privs = 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700676 }
677 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400678 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800679 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700680 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400681 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800682
683 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700684 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
685 die("failed to compile seccomp filter BPF program in '%s'",
686 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800687 }
688
689 j->filter_len = fprog->len;
690 j->filter_prog = fprog;
691
Elly Jonese1749eb2011-10-07 13:54:59 -0400692 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500693}
694
Andrew Brestickereac28942015-11-11 16:04:46 -0800695int API minijail_use_alt_syscall(struct minijail *j, const char *table)
696{
697 j->alt_syscall_table = strdup(table);
698 if (!j->alt_syscall_table)
699 return -ENOMEM;
700 j->flags.alt_syscall = 1;
701 return 0;
702}
703
Will Drewryf89aef52011-09-16 16:48:57 -0500704struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400705 size_t available;
706 size_t total;
707 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500708};
709
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800710void marshal_state_init(struct marshal_state *state, char *buf,
711 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400712{
713 state->available = available;
714 state->buf = buf;
715 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500716}
717
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800718void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400719{
720 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500721
Elly Jonese1749eb2011-10-07 13:54:59 -0400722 /* Up to |available| will be written. */
723 if (copy_len) {
724 memcpy(state->buf, src, copy_len);
725 state->buf += copy_len;
726 state->available -= copy_len;
727 }
728 /* |total| will contain the expected length. */
729 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500730}
731
Will Drewry6ac91122011-10-21 16:38:58 -0500732void minijail_marshal_helper(struct marshal_state *state,
733 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400734{
Dylan Reid648b2202015-10-23 00:50:00 -0700735 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800736 size_t i;
737
Elly Jonese1749eb2011-10-07 13:54:59 -0400738 marshal_append(state, (char *)j, sizeof(*j));
739 if (j->user)
740 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800741 if (j->suppl_gid_list) {
742 marshal_append(state, j->suppl_gid_list,
743 j->suppl_gid_count * sizeof(gid_t));
744 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400745 if (j->chrootdir)
746 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800747 if (j->alt_syscall_table) {
748 marshal_append(state, j->alt_syscall_table,
749 strlen(j->alt_syscall_table) + 1);
750 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800751 if (j->flags.seccomp_filter && j->filter_prog) {
752 struct sock_fprog *fp = j->filter_prog;
753 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800754 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400755 }
Dylan Reid648b2202015-10-23 00:50:00 -0700756 for (m = j->mounts_head; m; m = m->next) {
757 marshal_append(state, m->src, strlen(m->src) + 1);
758 marshal_append(state, m->dest, strlen(m->dest) + 1);
759 marshal_append(state, m->type, strlen(m->type) + 1);
760 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400761 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800762 for (i = 0; i < j->cgroup_count; ++i)
763 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500764}
765
Will Drewry6ac91122011-10-21 16:38:58 -0500766size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400767{
768 struct marshal_state state;
769 marshal_state_init(&state, NULL, 0);
770 minijail_marshal_helper(&state, j);
771 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500772}
773
Elly Jonese1749eb2011-10-07 13:54:59 -0400774int minijail_marshal(const struct minijail *j, char *buf, size_t available)
775{
776 struct marshal_state state;
777 marshal_state_init(&state, buf, available);
778 minijail_marshal_helper(&state, j);
779 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500780}
781
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800782/*
783 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
Elly Jones51a5b6c2011-10-12 19:09:26 -0400784 * @length Number of bytes to consume
785 * @buf Buffer to consume from
786 * @buflength Size of @buf
787 *
788 * Returns a pointer to the base of the bytes, or NULL for errors.
789 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700790void *consumebytes(size_t length, char **buf, size_t *buflength)
791{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400792 char *p = *buf;
793 if (length > *buflength)
794 return NULL;
795 *buf += length;
796 *buflength -= length;
797 return p;
798}
799
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800800/*
801 * consumestr: consumes a C string from a buffer @buf of length @length
Elly Jones51a5b6c2011-10-12 19:09:26 -0400802 * @buf Buffer to consume
803 * @length Length of buffer
804 *
805 * Returns a pointer to the base of the string, or NULL for errors.
806 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700807char *consumestr(char **buf, size_t *buflength)
808{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400809 size_t len = strnlen(*buf, *buflength);
810 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700811 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400812 return NULL;
813 return consumebytes(len + 1, buf, buflength);
814}
815
Elly Jonese1749eb2011-10-07 13:54:59 -0400816int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
817{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800818 size_t i;
819 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500820 int ret = -EINVAL;
821
Elly Jonese1749eb2011-10-07 13:54:59 -0400822 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500823 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400824 memcpy((void *)j, serialized, sizeof(*j));
825 serialized += sizeof(*j);
826 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500827
Will Drewrybee7ba72011-10-21 20:47:01 -0500828 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700829 j->mounts_head = NULL;
830 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800831 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500832
Elly Jonese1749eb2011-10-07 13:54:59 -0400833 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400834 char *user = consumestr(&serialized, &length);
835 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500836 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400837 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500838 if (!j->user)
839 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400840 }
Will Drewryf89aef52011-09-16 16:48:57 -0500841
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800842 if (j->suppl_gid_list) { /* stale pointer */
843 if (j->suppl_gid_count > NGROUPS_MAX) {
844 goto bad_gid_list;
845 }
846 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
847 void *gid_list_bytes =
848 consumebytes(gid_list_size, &serialized, &length);
849 if (!gid_list_bytes)
850 goto bad_gid_list;
851
852 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
853 if (!j->suppl_gid_list)
854 goto bad_gid_list;
855
856 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
857 }
858
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400859 if (j->chrootdir) { /* stale pointer */
860 char *chrootdir = consumestr(&serialized, &length);
861 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500862 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400863 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500864 if (!j->chrootdir)
865 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400866 }
867
Andrew Brestickereac28942015-11-11 16:04:46 -0800868 if (j->alt_syscall_table) { /* stale pointer */
869 char *alt_syscall_table = consumestr(&serialized, &length);
870 if (!alt_syscall_table)
871 goto bad_syscall_table;
872 j->alt_syscall_table = strdup(alt_syscall_table);
873 if (!j->alt_syscall_table)
874 goto bad_syscall_table;
875 }
876
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800877 if (j->flags.seccomp_filter && j->filter_len > 0) {
878 size_t ninstrs = j->filter_len;
879 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
880 ninstrs > USHRT_MAX)
881 goto bad_filters;
882
883 size_t program_len = ninstrs * sizeof(struct sock_filter);
884 void *program = consumebytes(program_len, &serialized, &length);
885 if (!program)
886 goto bad_filters;
887
888 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800889 if (!j->filter_prog)
890 goto bad_filters;
891
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800892 j->filter_prog->len = ninstrs;
893 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800894 if (!j->filter_prog->filter)
895 goto bad_filter_prog_instrs;
896
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800897 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400898 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400899
Dylan Reid648b2202015-10-23 00:50:00 -0700900 count = j->mounts_count;
901 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400902 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700903 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400904 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700905 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400906 const char *src = consumestr(&serialized, &length);
907 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700908 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400909 dest = consumestr(&serialized, &length);
910 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700911 goto bad_mounts;
912 type = consumestr(&serialized, &length);
913 if (!type)
914 goto bad_mounts;
915 flags = consumebytes(sizeof(*flags), &serialized, &length);
916 if (!flags)
917 goto bad_mounts;
918 if (minijail_mount(j, src, dest, type, *flags))
919 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400920 }
921
Dylan Reid605ce7f2016-01-19 19:21:00 -0800922 count = j->cgroup_count;
923 j->cgroup_count = 0;
924 for (i = 0; i < count; ++i) {
925 char *cgroup = consumestr(&serialized, &length);
926 if (!cgroup)
927 goto bad_cgroups;
928 j->cgroups[i] = strdup(cgroup);
929 if (!j->cgroups[i])
930 goto bad_cgroups;
931 ++j->cgroup_count;
932 }
933
Elly Jonese1749eb2011-10-07 13:54:59 -0400934 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500935
Dylan Reid605ce7f2016-01-19 19:21:00 -0800936bad_cgroups:
937 while (j->mounts_head) {
938 struct mountpoint *m = j->mounts_head;
939 j->mounts_head = j->mounts_head->next;
940 free(m->type);
941 free(m->dest);
942 free(m->src);
943 free(m);
944 }
945 for (i = 0; i < j->cgroup_count; ++i)
946 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -0700947bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800948 if (j->flags.seccomp_filter && j->filter_len > 0) {
949 free(j->filter_prog->filter);
950 free(j->filter_prog);
951 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800952bad_filter_prog_instrs:
953 if (j->filter_prog)
954 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -0500955bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800956 if (j->alt_syscall_table)
957 free(j->alt_syscall_table);
958bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500959 if (j->chrootdir)
960 free(j->chrootdir);
961bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800962 if (j->suppl_gid_list)
963 free(j->suppl_gid_list);
964bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -0500965 if (j->user)
966 free(j->user);
967clear_pointers:
968 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800969 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500970 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800971 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800972 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500973out:
974 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500975}
976
Dylan Reidce5b55e2016-01-13 11:04:16 -0800977static void write_ugid_mappings(const struct minijail *j)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800978{
979 int fd, ret, len;
980 size_t sz;
981 char fname[32];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800982
983 sz = sizeof(fname);
984 if (j->uidmap) {
985 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700986 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800987 die("failed to write file name of uid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -0800988 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800989 if (fd < 0)
990 pdie("failed to open '%s'", fname);
991 len = strlen(j->uidmap);
992 if (write(fd, j->uidmap, len) < len)
993 die("failed to set uid_map");
994 close(fd);
995 }
996 if (j->gidmap) {
997 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700998 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800999 die("failed to write file name of gid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001000 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001001 if (fd < 0)
1002 pdie("failed to open '%s'", fname);
1003 len = strlen(j->gidmap);
1004 if (write(fd, j->gidmap, len) < len)
1005 die("failed to set gid_map");
1006 close(fd);
1007 }
Dylan Reidce5b55e2016-01-13 11:04:16 -08001008}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001009
Dylan Reidce5b55e2016-01-13 11:04:16 -08001010static void parent_setup_complete(int *pipe_fds)
1011{
1012 close(pipe_fds[0]);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001013 close(pipe_fds[1]);
1014}
1015
Dylan Reidce5b55e2016-01-13 11:04:16 -08001016/*
1017 * wait_for_parent_setup: Called by the child process to wait for any
1018 * further parent-side setup to complete before continuing.
1019 */
1020static void wait_for_parent_setup(int *pipe_fds)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001021{
1022 char buf;
1023
1024 close(pipe_fds[1]);
1025
Dylan Reidce5b55e2016-01-13 11:04:16 -08001026 /* Wait for parent to complete setup and close the pipe. */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001027 if (read(pipe_fds[0], &buf, 1) != 0)
1028 die("failed to sync with parent");
1029 close(pipe_fds[0]);
Dylan Reidce5b55e2016-01-13 11:04:16 -08001030}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001031
Dylan Reidce5b55e2016-01-13 11:04:16 -08001032static void enter_user_namespace(const struct minijail *j)
1033{
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001034 if (j->uidmap && setresuid(0, 0, 0))
1035 pdie("setresuid");
1036 if (j->gidmap && setresgid(0, 0, 0))
1037 pdie("setresgid");
1038}
1039
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001040/*
1041 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001042 * @j Minijail these mounts are for
1043 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001044 *
1045 * Returns 0 for success.
1046 */
Dylan Reid648b2202015-10-23 00:50:00 -07001047static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001048{
Dylan Reid648b2202015-10-23 00:50:00 -07001049 int ret;
1050 char *dest;
1051 int remount_ro = 0;
1052
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001053 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001054 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001055 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001056
1057 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001058 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1059 * can't both be specified in the original bind mount.
1060 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001061 */
1062 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1063 remount_ro = 1;
1064 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001065 }
Dylan Reid648b2202015-10-23 00:50:00 -07001066
1067 ret = mount(m->src, dest, m->type, m->flags, NULL);
1068 if (ret)
1069 pdie("mount: %s -> %s", m->src, dest);
1070
1071 if (remount_ro) {
1072 m->flags |= MS_RDONLY;
1073 ret = mount(m->src, dest, NULL,
1074 m->flags | MS_REMOUNT, NULL);
1075 if (ret)
1076 pdie("bind ro: %s -> %s", m->src, dest);
1077 }
1078
Elly Jones51a5b6c2011-10-12 19:09:26 -04001079 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001080 if (m->next)
1081 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001082 return ret;
1083}
1084
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001085int enter_chroot(const struct minijail *j)
1086{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001087 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001088
1089 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001090 return ret;
1091
1092 if (chroot(j->chrootdir))
1093 return -errno;
1094
1095 if (chdir("/"))
1096 return -errno;
1097
1098 return 0;
1099}
1100
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001101int enter_pivot_root(const struct minijail *j)
1102{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001103 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001104
1105 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001106 return ret;
1107
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001108 /*
1109 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001110 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001111 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001112 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001113 if (oldroot < 0)
1114 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001115 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001116 if (newroot < 0)
1117 pdie("failed to open %s for fchdir", j->chrootdir);
1118
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001119 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001120 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001121 * do a self bind mount.
1122 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001123 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1124 pdie("failed to bind mount '%s'", j->chrootdir);
1125 if (chdir(j->chrootdir))
1126 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001127 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001128 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001129
1130 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001131 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001132 * change to the old root and unmount it.
1133 */
1134 if (fchdir(oldroot))
1135 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001136
1137 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001138 * If j->flags.skip_remount_private was enabled for minijail_enter(), there
1139 * could be a shared mount point under |oldroot|. In that case, mounts
1140 * under this shared mount point will be unmounted below, and this
1141 * unmounting will propagate to the original mount namespace (because the
1142 * mount point is shared). To prevent this unexpected unmounting, remove
1143 * these mounts from their peer groups by recursively remounting them as
1144 * MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001145 */
1146 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001147 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001148 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001149 if (umount2(".", MNT_DETACH))
1150 pdie("umount(/)");
1151 /* Change back to the new root. */
1152 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001153 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001154 if (close(oldroot))
1155 return -errno;
1156 if (close(newroot))
1157 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001158 if (chroot("/"))
1159 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001160 /* Set correct CWD for getcwd(3). */
1161 if (chdir("/"))
1162 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001163
1164 return 0;
1165}
1166
Lee Campbell11af0622014-05-22 12:36:04 -07001167int mount_tmp(void)
1168{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001169 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001170}
1171
Dylan Reid791f5772015-09-14 20:02:42 -07001172int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001173{
1174 const char *kProcPath = "/proc";
1175 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001176 /*
1177 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001178 * /proc in our namespace, which means using MS_REMOUNT here would
1179 * mutate our parent's mount as well, even though we're in a VFS
1180 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001181 * and make our own. However, if we are in a new user namespace, /proc
1182 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -04001183 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001184 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -04001185 return -errno;
1186 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1187 return -errno;
1188 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001189}
1190
Dylan Reid605ce7f2016-01-19 19:21:00 -08001191static void write_pid_to_path(pid_t pid, const char *path)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001192{
Dylan Reid605ce7f2016-01-19 19:21:00 -08001193 FILE *fp = fopen(path, "w");
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001194
1195 if (!fp)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001196 pdie("failed to open '%s'", path);
1197 if (fprintf(fp, "%d\n", (int)pid) < 0)
1198 pdie("fprintf(%s)", path);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001199 if (fclose(fp))
Dylan Reid605ce7f2016-01-19 19:21:00 -08001200 pdie("fclose(%s)", path);
1201}
1202
1203static void write_pid_file(const struct minijail *j)
1204{
1205 write_pid_to_path(j->initpid, j->pid_file_path);
1206}
1207
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001208static void add_to_cgroups(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001209{
1210 size_t i;
1211
1212 for (i = 0; i < j->cgroup_count; ++i)
1213 write_pid_to_path(j->initpid, j->cgroups[i]);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001214}
1215
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001216void drop_ugid(const struct minijail *j)
1217{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001218 if (j->flags.usergroups && j->flags.suppl_gids) {
1219 die("tried to inherit *and* set supplementary groups;"
1220 " can only do one");
1221 }
1222
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001223 if (j->flags.usergroups) {
1224 if (initgroups(j->user, j->usergid))
1225 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001226 } else if (j->flags.suppl_gids) {
1227 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1228 pdie("setgroups");
1229 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001230 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001231 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001232 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001233 * users.
1234 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001235 if ((j->uid || j->gid) && setgroups(0, NULL))
1236 pdie("setgroups");
1237 }
1238
1239 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1240 pdie("setresgid");
1241
1242 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1243 pdie("setresuid");
1244}
1245
Mike Frysinger3adfef72013-05-09 17:19:08 -04001246/*
1247 * We specifically do not use cap_valid() as that only tells us the last
1248 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001249 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001250 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001251 * kernel).
1252 * Normally, we suck up the answer via /proc. On Android, not all processes are
1253 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1254 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001255 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001256static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001257{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001258 unsigned int last_valid_cap = 0;
1259 if (is_android()) {
1260 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1261 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001262
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001263 /* |last_valid_cap| will be the first failing value. */
1264 if (last_valid_cap > 0) {
1265 last_valid_cap--;
1266 }
1267 } else {
1268 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1269 FILE *fp = fopen(cap_file, "re");
1270 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1271 pdie("fscanf(%s)", cap_file);
1272 fclose(fp);
1273 }
Dylan Reidf682d472015-09-17 21:39:07 -07001274 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001275}
1276
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001277static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1278{
1279 const uint64_t one = 1;
1280 unsigned int i;
1281 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1282 if (keep_mask & (one << i))
1283 continue;
1284 if (prctl(PR_CAPBSET_DROP, i))
1285 pdie("could not drop capability from bounding set");
1286 }
1287}
1288
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001289void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001290{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001291 if (!j->flags.use_caps)
1292 return;
1293
Elly Jonese1749eb2011-10-07 13:54:59 -04001294 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001295 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001296 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001297 unsigned int i;
1298 if (!caps)
1299 die("can't get process caps");
1300 if (cap_clear_flag(caps, CAP_INHERITABLE))
1301 die("can't clear inheritable caps");
1302 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1303 die("can't clear effective caps");
1304 if (cap_clear_flag(caps, CAP_PERMITTED))
1305 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001306 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001307 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001308 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001309 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001310 flag[0] = i;
1311 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001312 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001313 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001314 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001315 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001316 die("can't add inheritable cap");
1317 }
1318 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001319 die("can't apply initial cleaned capset");
1320
1321 /*
1322 * Instead of dropping bounding set first, do it here in case
1323 * the caller had a more permissive bounding set which could
1324 * have been used above to raise a capability that wasn't already
1325 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1326 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001327 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001328
1329 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001330 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001331 flag[0] = CAP_SETPCAP;
1332 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1333 die("can't clear effective cap");
1334 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1335 die("can't clear permitted cap");
1336 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1337 die("can't clear inheritable cap");
1338 }
1339
1340 if (cap_set_proc(caps))
1341 die("can't apply final cleaned capset");
1342
1343 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001344}
1345
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001346void set_seccomp_filter(const struct minijail *j)
1347{
1348 /*
1349 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1350 * in the kernel source tree for an explanation of the parameters.
1351 */
1352 if (j->flags.no_new_privs) {
1353 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1354 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1355 }
1356
1357 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001358 * Code running with ASan
1359 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1360 * will make system calls not included in the syscall filter policy,
1361 * which will likely crash the program. Skip setting seccomp filter in
1362 * that case.
1363 * 'running_with_asan()' has no inputs and is completely defined at
1364 * build time, so this cannot be used by an attacker to skip setting
1365 * seccomp filter.
1366 */
1367 if (j->flags.seccomp_filter && running_with_asan()) {
1368 warn("running with ASan, not setting seccomp filter");
1369 return;
1370 }
1371
1372 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001373 * If we're logging seccomp filter failures,
1374 * install the SIGSYS handler first.
1375 */
1376 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1377 if (install_sigsys_handler())
1378 pdie("install SIGSYS handler");
1379 warn("logging seccomp filter failures");
1380 }
1381
1382 /*
1383 * Install the syscall filter.
1384 */
1385 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001386 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1387 j->filter_prog)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001388 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001389 warn("seccomp not supported");
1390 return;
1391 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001392 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001393 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001394 }
1395}
1396
Will Drewry6ac91122011-10-21 16:38:58 -05001397void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001398{
Dylan Reidf682d472015-09-17 21:39:07 -07001399 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001400 * If we're dropping caps, get the last valid cap from /proc now,
1401 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001402 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001403 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001404 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001405 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001406
Elly Jonese1749eb2011-10-07 13:54:59 -04001407 if (j->flags.pids)
1408 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001409 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001410
Elly Jonese1749eb2011-10-07 13:54:59 -04001411 if (j->flags.usergroups && !j->user)
1412 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001413
Elly Jonesdd3e8512012-01-23 15:13:38 -05001414 /*
1415 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001416 * so we don't even try. If any of our operations fail, we abort() the
1417 * entire process.
1418 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001419 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1420 pdie("setns(CLONE_NEWNS)");
1421
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001422 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001423 if (unshare(CLONE_NEWNS))
1424 pdie("unshare(vfs)");
1425 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001426 * Unless asked not to, remount all filesystems as private.
1427 * If they are shared, new bind mounts will creep out of our
1428 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001429 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1430 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001431 if (!j->flags.skip_remount_private) {
1432 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1433 pdie("mount(/, private)");
1434 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001435 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001436
Dylan Reidf7942472015-11-18 17:55:26 -08001437 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1438 pdie("unshare(ipc)");
1439 }
1440
Dylan Reid1102f5a2015-09-15 11:52:20 -07001441 if (j->flags.enter_net) {
1442 if (setns(j->netns_fd, CLONE_NEWNET))
1443 pdie("setns(CLONE_NEWNET)");
1444 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001445 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001446 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001447
Elly Jones51a5b6c2011-10-12 19:09:26 -04001448 if (j->flags.chroot && enter_chroot(j))
1449 pdie("chroot");
1450
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001451 if (j->flags.pivot_root && enter_pivot_root(j))
1452 pdie("pivot_root");
1453
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001454 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001455 pdie("mount_tmp");
1456
Dylan Reid791f5772015-09-14 20:02:42 -07001457 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001458 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001459
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001460 /*
1461 * If we're only dropping capabilities from the bounding set, but not
1462 * from the thread's (permitted|inheritable|effective) sets, do it now.
1463 */
1464 if (j->flags.capbset_drop) {
1465 drop_capbset(j->cap_bset, last_valid_cap);
1466 }
1467
1468 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001469 /*
1470 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001471 * capability to change uids, our attempt to use setuid()
1472 * below will fail. Hang on to root caps across setuid(), then
1473 * lock securebits.
1474 */
1475 if (prctl(PR_SET_KEEPCAPS, 1))
1476 pdie("prctl(PR_SET_KEEPCAPS)");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001477
1478 /*
1479 * Kernels 4.3+ define a new securebit
1480 * (SECURE_NO_CAP_AMBIENT_RAISE), so using the SECURE_ALL_BITS
1481 * and SECURE_ALL_LOCKS masks from newer kernel headers will
1482 * return EPERM on older kernels. Detect this, and retry with
1483 * the right mask for older (2.6.26-4.2) kernels.
1484 */
1485 int securebits_ret = prctl(PR_SET_SECUREBITS,
1486 SECURE_ALL_BITS | SECURE_ALL_LOCKS);
1487 if (securebits_ret < 0) {
1488 if (errno == EPERM) {
1489 /* Possibly running on kernel < 4.3. */
1490 securebits_ret = prctl(
1491 PR_SET_SECUREBITS,
1492 OLD_SECURE_ALL_BITS | OLD_SECURE_ALL_LOCKS);
1493 }
1494 }
1495 if (securebits_ret < 0)
Elly Jonese1749eb2011-10-07 13:54:59 -04001496 pdie("prctl(PR_SET_SECUREBITS)");
1497 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001498
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001499 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001500 /*
1501 * If we're setting no_new_privs, we can drop privileges
1502 * before setting seccomp filter. This way filter policies
1503 * don't need to allow privilege-dropping syscalls.
1504 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001505 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001506 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001507 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001508 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001509 /*
1510 * If we're not setting no_new_privs,
1511 * we need to set seccomp filter *before* dropping privileges.
1512 * WARNING: this means that filter policies *must* allow
1513 * setgroups()/setresgid()/setresuid() for dropping root and
1514 * capget()/capset()/prctl() for dropping caps.
1515 */
1516 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001517 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001518 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001519 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001520
Elly Jonesdd3e8512012-01-23 15:13:38 -05001521 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001522 * Select the specified alternate syscall table. The table must not
1523 * block prctl(2) if we're using seccomp as well.
1524 */
1525 if (j->flags.alt_syscall) {
1526 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1527 pdie("prctl(PR_ALT_SYSCALL)");
1528 }
1529
1530 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001531 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001532 * privilege-dropping syscalls :)
1533 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001534 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001535 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001536 warn("seccomp not supported");
1537 return;
1538 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001539 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001540 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001541}
1542
Will Drewry6ac91122011-10-21 16:38:58 -05001543/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001544static int init_exitstatus = 0;
1545
Will Drewry6ac91122011-10-21 16:38:58 -05001546void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001547{
1548 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001549}
1550
Will Drewry6ac91122011-10-21 16:38:58 -05001551int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001552{
1553 pid_t pid;
1554 int status;
1555 /* so that we exit with the right status */
1556 signal(SIGTERM, init_term);
1557 /* TODO(wad) self jail with seccomp_filters here. */
1558 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001559 /*
1560 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001561 * left inside our pid namespace or we get a signal.
1562 */
1563 if (pid == rootpid)
1564 init_exitstatus = status;
1565 }
1566 if (!WIFEXITED(init_exitstatus))
1567 _exit(MINIJAIL_ERR_INIT);
1568 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001569}
1570
Will Drewry6ac91122011-10-21 16:38:58 -05001571int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001572{
1573 size_t sz = 0;
1574 size_t bytes = read(fd, &sz, sizeof(sz));
1575 char *buf;
1576 int r;
1577 if (sizeof(sz) != bytes)
1578 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001579 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001580 return -E2BIG;
1581 buf = malloc(sz);
1582 if (!buf)
1583 return -ENOMEM;
1584 bytes = read(fd, buf, sz);
1585 if (bytes != sz) {
1586 free(buf);
1587 return -EINVAL;
1588 }
1589 r = minijail_unmarshal(j, buf, sz);
1590 free(buf);
1591 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001592}
1593
Will Drewry6ac91122011-10-21 16:38:58 -05001594int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001595{
1596 char *buf;
1597 size_t sz = minijail_size(j);
1598 ssize_t written;
1599 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001600
Elly Jonese1749eb2011-10-07 13:54:59 -04001601 if (!sz)
1602 return -EINVAL;
1603 buf = malloc(sz);
1604 r = minijail_marshal(j, buf, sz);
1605 if (r) {
1606 free(buf);
1607 return r;
1608 }
1609 /* Sends [size][minijail]. */
1610 written = write(fd, &sz, sizeof(sz));
1611 if (written != sizeof(sz)) {
1612 free(buf);
1613 return -EFAULT;
1614 }
1615 written = write(fd, buf, sz);
1616 if (written < 0 || (size_t) written != sz) {
1617 free(buf);
1618 return -EFAULT;
1619 }
1620 free(buf);
1621 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001622}
Elly Jonescd7a9042011-07-22 13:56:51 -04001623
Will Drewry6ac91122011-10-21 16:38:58 -05001624int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001625{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001626#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001627 /* Don't use LDPRELOAD on Brillo. */
1628 return 0;
1629#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001630 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1631 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1632 if (!newenv)
1633 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001634
Elly Jonese1749eb2011-10-07 13:54:59 -04001635 /* Only insert a separating space if we have something to separate... */
1636 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1637 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001638
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001639 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001640 setenv(kLdPreloadEnvVar, newenv, 1);
1641 free(newenv);
1642 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001643#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001644}
1645
Will Drewry6ac91122011-10-21 16:38:58 -05001646int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001647{
1648 int r = pipe(fds);
1649 char fd_buf[11];
1650 if (r)
1651 return r;
1652 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1653 if (r <= 0)
1654 return -EINVAL;
1655 setenv(kFdEnvVar, fd_buf, 1);
1656 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001657}
1658
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001659int setup_pipe_end(int fds[2], size_t index)
1660{
1661 if (index > 1)
1662 return -1;
1663
1664 close(fds[1 - index]);
1665 return fds[index];
1666}
1667
1668int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1669{
1670 if (index > 1)
1671 return -1;
1672
1673 close(fds[1 - index]);
1674 /* dup2(2) the corresponding end of the pipe into |fd|. */
1675 return dup2(fds[index], fd);
1676}
1677
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001678int minijail_run_internal(struct minijail *j, const char *filename,
1679 char *const argv[], pid_t *pchild_pid,
1680 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1681 int use_preload);
1682
Will Drewry6ac91122011-10-21 16:38:58 -05001683int API minijail_run(struct minijail *j, const char *filename,
1684 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001685{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001686 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1687 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001688}
1689
1690int API minijail_run_pid(struct minijail *j, const char *filename,
1691 char *const argv[], pid_t *pchild_pid)
1692{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001693 return minijail_run_internal(j, filename, argv, pchild_pid,
1694 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001695}
1696
1697int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001698 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001699{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001700 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1701 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001702}
1703
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001704int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001705 char *const argv[], pid_t *pchild_pid,
1706 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001707{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001708 return minijail_run_internal(j, filename, argv, pchild_pid,
1709 pstdin_fd, pstdout_fd, pstderr_fd, true);
1710}
1711
1712int API minijail_run_no_preload(struct minijail *j, const char *filename,
1713 char *const argv[])
1714{
1715 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1716 false);
1717}
1718
Samuel Tan63187f42015-10-16 13:01:53 -07001719int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001720 const char *filename,
1721 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001722 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001723 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001724 int *pstderr_fd)
1725{
Samuel Tan63187f42015-10-16 13:01:53 -07001726 return minijail_run_internal(j, filename, argv, pchild_pid,
1727 pstdin_fd, pstdout_fd, pstderr_fd, false);
1728}
1729
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001730int minijail_run_internal(struct minijail *j, const char *filename,
1731 char *const argv[], pid_t *pchild_pid,
1732 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1733 int use_preload)
1734{
Elly Jonese1749eb2011-10-07 13:54:59 -04001735 char *oldenv, *oldenv_copy = NULL;
1736 pid_t child_pid;
1737 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001738 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001739 int stdout_fds[2];
1740 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001741 int child_sync_pipe_fds[2];
1742 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001743 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001744 /* We need to remember this across the minijail_preexec() call. */
1745 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001746 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001747
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001748 if (use_preload) {
1749 oldenv = getenv(kLdPreloadEnvVar);
1750 if (oldenv) {
1751 oldenv_copy = strdup(oldenv);
1752 if (!oldenv_copy)
1753 return -ENOMEM;
1754 }
1755
1756 if (setup_preload())
1757 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001758 }
Will Drewryf89aef52011-09-16 16:48:57 -05001759
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001760 if (!use_preload) {
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001761 if (j->flags.use_caps)
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001762 die("capabilities are not supported without "
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001763 "LD_PRELOAD");
1764 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001765
Elly Jonesdd3e8512012-01-23 15:13:38 -05001766 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001767 * Make the process group ID of this process equal to its PID, so that
1768 * both the Minijail process and the jailed process can be killed
1769 * together.
1770 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1771 * the process is already a process group leader.
1772 */
1773 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1774 if (errno != EPERM) {
1775 pdie("setpgid(0, 0)");
1776 }
1777 }
1778
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001779 if (use_preload) {
1780 /*
1781 * Before we fork(2) and execve(2) the child process, we need
1782 * to open a pipe(2) to send the minijail configuration over.
1783 */
1784 if (setup_pipe(pipe_fds))
1785 return -EFAULT;
1786 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001787
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001788 /*
1789 * If we want to write to the child process' standard input,
1790 * create the pipe(2) now.
1791 */
1792 if (pstdin_fd) {
1793 if (pipe(stdin_fds))
1794 return -EFAULT;
1795 }
1796
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001797 /*
1798 * If we want to read from the child process' standard output,
1799 * create the pipe(2) now.
1800 */
1801 if (pstdout_fd) {
1802 if (pipe(stdout_fds))
1803 return -EFAULT;
1804 }
1805
1806 /*
1807 * If we want to read from the child process' standard error,
1808 * create the pipe(2) now.
1809 */
1810 if (pstderr_fd) {
1811 if (pipe(stderr_fds))
1812 return -EFAULT;
1813 }
1814
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001815 /*
1816 * If we want to set up a new uid/gid mapping in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001817 * or if we need to add the child process to cgroups, create the pipe(2)
1818 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001819 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001820 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001821 sync_child = 1;
1822 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001823 return -EFAULT;
1824 }
1825
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001826 /*
1827 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001828 *
1829 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1830 *
1831 * In multithreaded programs, there are a bunch of locks inside libc,
1832 * some of which may be held by other threads at the time that we call
1833 * minijail_run_pid(). If we call fork(), glibc does its level best to
1834 * ensure that we hold all of these locks before it calls clone()
1835 * internally and drop them after clone() returns, but when we call
1836 * sys_clone(2) directly, all that gets bypassed and we end up with a
1837 * child address space where some of libc's important locks are held by
1838 * other threads (which did not get cloned, and hence will never release
1839 * those locks). This is okay so long as we call exec() immediately
1840 * after, but a bunch of seemingly-innocent libc functions like setenv()
1841 * take locks.
1842 *
1843 * Hence, only call sys_clone() if we need to, in order to get at pid
1844 * namespacing. If we follow this path, the child's address space might
1845 * have broken locks; you may only call functions that do not acquire
1846 * any locks.
1847 *
1848 * Unfortunately, fork() acquires every lock it can get its hands on, as
1849 * previously detailed, so this function is highly likely to deadlock
1850 * later on (see "deadlock here") if we're multithreaded.
1851 *
1852 * We might hack around this by having the clone()d child (init of the
1853 * pid namespace) return directly, rather than leaving the clone()d
1854 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001855 * its fork()ed child return in turn), but that process would be
1856 * crippled with its libc locks potentially broken. We might try
1857 * fork()ing in the parent before we clone() to ensure that we own all
1858 * the locks, but then we have to have the forked child hanging around
1859 * consuming resources (and possibly having file descriptors / shared
1860 * memory regions / etc attached). We'd need to keep the child around to
1861 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04001862 *
1863 * TODO(ellyjones): figure out if the "forked child hanging around"
1864 * problem is fixable or not. It would be nice if we worked in this
1865 * case.
1866 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001867 if (pid_namespace) {
1868 int clone_flags = CLONE_NEWPID | SIGCHLD;
1869 if (j->flags.userns)
1870 clone_flags |= CLONE_NEWUSER;
1871 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001872 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001873 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001874 }
Elly Jones761b7412012-06-13 15:49:52 -04001875
Elly Jonese1749eb2011-10-07 13:54:59 -04001876 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001877 if (use_preload) {
1878 free(oldenv_copy);
1879 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001880 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001881 }
Will Drewryf89aef52011-09-16 16:48:57 -05001882
Elly Jonese1749eb2011-10-07 13:54:59 -04001883 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001884 if (use_preload) {
1885 /* Restore parent's LD_PRELOAD. */
1886 if (oldenv_copy) {
1887 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1888 free(oldenv_copy);
1889 } else {
1890 unsetenv(kLdPreloadEnvVar);
1891 }
1892 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001893 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001894
Elly Jonese1749eb2011-10-07 13:54:59 -04001895 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001896
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001897 if (j->flags.pid_file)
1898 write_pid_file(j);
1899
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001900 if (j->flags.cgroups)
1901 add_to_cgroups(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001902
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001903 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001904 write_ugid_mappings(j);
1905
1906 if (sync_child)
1907 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001908
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001909 if (use_preload) {
1910 /* Send marshalled minijail. */
1911 close(pipe_fds[0]); /* read endpoint */
1912 ret = minijail_to_fd(j, pipe_fds[1]);
1913 close(pipe_fds[1]); /* write endpoint */
1914 if (ret) {
1915 kill(j->initpid, SIGKILL);
1916 die("failed to send marshalled minijail");
1917 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001918 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001919
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001920 if (pchild_pid)
1921 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001922
1923 /*
1924 * If we want to write to the child process' standard input,
1925 * set up the write end of the pipe.
1926 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001927 if (pstdin_fd)
1928 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001929 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001930
1931 /*
1932 * If we want to read from the child process' standard output,
1933 * set up the read end of the pipe.
1934 */
1935 if (pstdout_fd)
1936 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001937 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001938
1939 /*
1940 * If we want to read from the child process' standard error,
1941 * set up the read end of the pipe.
1942 */
1943 if (pstderr_fd)
1944 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001945 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001946
Elly Jonese1749eb2011-10-07 13:54:59 -04001947 return 0;
1948 }
1949 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001950
Peter Qiu2860c462015-12-16 15:13:06 -08001951 if (j->flags.reset_signal_mask) {
1952 sigset_t signal_mask;
1953 if (sigemptyset(&signal_mask) != 0)
1954 pdie("sigemptyset failed");
1955 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
1956 pdie("sigprocmask failed");
1957 }
1958
Dylan Reidce5b55e2016-01-13 11:04:16 -08001959 if (sync_child)
1960 wait_for_parent_setup(child_sync_pipe_fds);
1961
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001962 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001963 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001964
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001965 /*
1966 * If we want to write to the jailed process' standard input,
1967 * set up the read end of the pipe.
1968 */
1969 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001970 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1971 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001972 die("failed to set up stdin pipe");
1973 }
1974
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001975 /*
1976 * If we want to read from the jailed process' standard output,
1977 * set up the write end of the pipe.
1978 */
1979 if (pstdout_fd) {
1980 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1981 STDOUT_FILENO) < 0)
1982 die("failed to set up stdout pipe");
1983 }
1984
1985 /*
1986 * If we want to read from the jailed process' standard error,
1987 * set up the write end of the pipe.
1988 */
1989 if (pstderr_fd) {
1990 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1991 STDERR_FILENO) < 0)
1992 die("failed to set up stderr pipe");
1993 }
1994
Dylan Reid791f5772015-09-14 20:02:42 -07001995 /* If running an init program, let it decide when/how to mount /proc. */
1996 if (pid_namespace && !do_init)
1997 j->flags.remount_proc_ro = 0;
1998
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001999 if (use_preload) {
2000 /* Strip out flags that cannot be inherited across execve(2). */
2001 minijail_preexec(j);
2002 } else {
2003 j->flags.pids = 0;
2004 }
2005 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002006 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002007
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002008 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002009 /*
2010 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002011 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002012 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002013 * a child to actually run the program. If |do_init == 0|, we
2014 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002015 *
2016 * If we're multithreaded, we'll probably deadlock here. See
2017 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002018 */
2019 child_pid = fork();
2020 if (child_pid < 0)
2021 _exit(child_pid);
2022 else if (child_pid > 0)
2023 init(child_pid); /* never returns */
2024 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002025
Elly Jonesdd3e8512012-01-23 15:13:38 -05002026 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002027 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002028 * calling process
2029 * -> execve()-ing process
2030 * If we are:
2031 * calling process
2032 * -> init()-ing process
2033 * -> execve()-ing process
2034 */
2035 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04002036}
2037
Will Drewry6ac91122011-10-21 16:38:58 -05002038int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002039{
2040 int st;
2041 if (kill(j->initpid, SIGTERM))
2042 return -errno;
2043 if (waitpid(j->initpid, &st, 0) < 0)
2044 return -errno;
2045 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002046}
2047
Will Drewry6ac91122011-10-21 16:38:58 -05002048int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002049{
2050 int st;
2051 if (waitpid(j->initpid, &st, 0) < 0)
2052 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002053
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002054 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002055 int error_status = st;
2056 if (WIFSIGNALED(st)) {
2057 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002058 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002059 j->initpid, signum);
2060 /*
2061 * We return MINIJAIL_ERR_JAIL if the process received
2062 * SIGSYS, which happens when a syscall is blocked by
2063 * seccomp filters.
2064 * If not, we do what bash(1) does:
2065 * $? = 128 + signum
2066 */
2067 if (signum == SIGSYS) {
2068 error_status = MINIJAIL_ERR_JAIL;
2069 } else {
2070 error_status = 128 + signum;
2071 }
2072 }
2073 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002074 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002075
2076 int exit_status = WEXITSTATUS(st);
2077 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002078 info("child process %d exited with status %d",
2079 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002080
2081 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002082}
2083
Will Drewry6ac91122011-10-21 16:38:58 -05002084void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002085{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002086 size_t i;
2087
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002088 if (j->flags.seccomp_filter && j->filter_prog) {
2089 free(j->filter_prog->filter);
2090 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002091 }
Dylan Reid648b2202015-10-23 00:50:00 -07002092 while (j->mounts_head) {
2093 struct mountpoint *m = j->mounts_head;
2094 j->mounts_head = j->mounts_head->next;
2095 free(m->type);
2096 free(m->dest);
2097 free(m->src);
2098 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002099 }
Dylan Reid648b2202015-10-23 00:50:00 -07002100 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002101 if (j->user)
2102 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002103 if (j->suppl_gid_list)
2104 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002105 if (j->chrootdir)
2106 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08002107 if (j->alt_syscall_table)
2108 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002109 for (i = 0; i < j->cgroup_count; ++i)
2110 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002111 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002112}