blob: fcfee1eb594b65cf24021129ba8efbf49009a872 [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 /*
1358 * If we're logging seccomp filter failures,
1359 * install the SIGSYS handler first.
1360 */
1361 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1362 if (install_sigsys_handler())
1363 pdie("install SIGSYS handler");
1364 warn("logging seccomp filter failures");
1365 }
1366
1367 /*
1368 * Install the syscall filter.
1369 */
1370 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001371 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1372 j->filter_prog)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001373 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001374 warn("seccomp not supported");
1375 return;
1376 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001377 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001378 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001379 }
1380}
1381
Will Drewry6ac91122011-10-21 16:38:58 -05001382void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001383{
Dylan Reidf682d472015-09-17 21:39:07 -07001384 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001385 * If we're dropping caps, get the last valid cap from /proc now,
1386 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001387 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001388 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001389 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001390 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001391
Elly Jonese1749eb2011-10-07 13:54:59 -04001392 if (j->flags.pids)
1393 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001394 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001395
Elly Jonese1749eb2011-10-07 13:54:59 -04001396 if (j->flags.usergroups && !j->user)
1397 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001398
Elly Jonesdd3e8512012-01-23 15:13:38 -05001399 /*
1400 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001401 * so we don't even try. If any of our operations fail, we abort() the
1402 * entire process.
1403 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001404 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1405 pdie("setns(CLONE_NEWNS)");
1406
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001407 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001408 if (unshare(CLONE_NEWNS))
1409 pdie("unshare(vfs)");
1410 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001411 * Unless asked not to, remount all filesystems as private.
1412 * If they are shared, new bind mounts will creep out of our
1413 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001414 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1415 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001416 if (!j->flags.skip_remount_private) {
1417 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1418 pdie("mount(/, private)");
1419 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001420 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001421
Dylan Reidf7942472015-11-18 17:55:26 -08001422 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1423 pdie("unshare(ipc)");
1424 }
1425
Dylan Reid1102f5a2015-09-15 11:52:20 -07001426 if (j->flags.enter_net) {
1427 if (setns(j->netns_fd, CLONE_NEWNET))
1428 pdie("setns(CLONE_NEWNET)");
1429 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001430 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001431 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001432
Elly Jones51a5b6c2011-10-12 19:09:26 -04001433 if (j->flags.chroot && enter_chroot(j))
1434 pdie("chroot");
1435
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001436 if (j->flags.pivot_root && enter_pivot_root(j))
1437 pdie("pivot_root");
1438
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001439 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001440 pdie("mount_tmp");
1441
Dylan Reid791f5772015-09-14 20:02:42 -07001442 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001443 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001444
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001445 /*
1446 * If we're only dropping capabilities from the bounding set, but not
1447 * from the thread's (permitted|inheritable|effective) sets, do it now.
1448 */
1449 if (j->flags.capbset_drop) {
1450 drop_capbset(j->cap_bset, last_valid_cap);
1451 }
1452
1453 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001454 /*
1455 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001456 * capability to change uids, our attempt to use setuid()
1457 * below will fail. Hang on to root caps across setuid(), then
1458 * lock securebits.
1459 */
1460 if (prctl(PR_SET_KEEPCAPS, 1))
1461 pdie("prctl(PR_SET_KEEPCAPS)");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001462
1463 /*
1464 * Kernels 4.3+ define a new securebit
1465 * (SECURE_NO_CAP_AMBIENT_RAISE), so using the SECURE_ALL_BITS
1466 * and SECURE_ALL_LOCKS masks from newer kernel headers will
1467 * return EPERM on older kernels. Detect this, and retry with
1468 * the right mask for older (2.6.26-4.2) kernels.
1469 */
1470 int securebits_ret = prctl(PR_SET_SECUREBITS,
1471 SECURE_ALL_BITS | SECURE_ALL_LOCKS);
1472 if (securebits_ret < 0) {
1473 if (errno == EPERM) {
1474 /* Possibly running on kernel < 4.3. */
1475 securebits_ret = prctl(
1476 PR_SET_SECUREBITS,
1477 OLD_SECURE_ALL_BITS | OLD_SECURE_ALL_LOCKS);
1478 }
1479 }
1480 if (securebits_ret < 0)
Elly Jonese1749eb2011-10-07 13:54:59 -04001481 pdie("prctl(PR_SET_SECUREBITS)");
1482 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001483
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001484 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001485 /*
1486 * If we're setting no_new_privs, we can drop privileges
1487 * before setting seccomp filter. This way filter policies
1488 * don't need to allow privilege-dropping syscalls.
1489 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001490 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001491 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001492 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001493 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001494 /*
1495 * If we're not setting no_new_privs,
1496 * we need to set seccomp filter *before* dropping privileges.
1497 * WARNING: this means that filter policies *must* allow
1498 * setgroups()/setresgid()/setresuid() for dropping root and
1499 * capget()/capset()/prctl() for dropping caps.
1500 */
1501 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001502 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001503 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001504 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001505
Elly Jonesdd3e8512012-01-23 15:13:38 -05001506 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001507 * Select the specified alternate syscall table. The table must not
1508 * block prctl(2) if we're using seccomp as well.
1509 */
1510 if (j->flags.alt_syscall) {
1511 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1512 pdie("prctl(PR_ALT_SYSCALL)");
1513 }
1514
1515 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001516 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001517 * privilege-dropping syscalls :)
1518 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001519 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001520 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001521 warn("seccomp not supported");
1522 return;
1523 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001524 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001525 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001526}
1527
Will Drewry6ac91122011-10-21 16:38:58 -05001528/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001529static int init_exitstatus = 0;
1530
Will Drewry6ac91122011-10-21 16:38:58 -05001531void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001532{
1533 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001534}
1535
Will Drewry6ac91122011-10-21 16:38:58 -05001536int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001537{
1538 pid_t pid;
1539 int status;
1540 /* so that we exit with the right status */
1541 signal(SIGTERM, init_term);
1542 /* TODO(wad) self jail with seccomp_filters here. */
1543 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001544 /*
1545 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001546 * left inside our pid namespace or we get a signal.
1547 */
1548 if (pid == rootpid)
1549 init_exitstatus = status;
1550 }
1551 if (!WIFEXITED(init_exitstatus))
1552 _exit(MINIJAIL_ERR_INIT);
1553 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001554}
1555
Will Drewry6ac91122011-10-21 16:38:58 -05001556int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001557{
1558 size_t sz = 0;
1559 size_t bytes = read(fd, &sz, sizeof(sz));
1560 char *buf;
1561 int r;
1562 if (sizeof(sz) != bytes)
1563 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001564 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001565 return -E2BIG;
1566 buf = malloc(sz);
1567 if (!buf)
1568 return -ENOMEM;
1569 bytes = read(fd, buf, sz);
1570 if (bytes != sz) {
1571 free(buf);
1572 return -EINVAL;
1573 }
1574 r = minijail_unmarshal(j, buf, sz);
1575 free(buf);
1576 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001577}
1578
Will Drewry6ac91122011-10-21 16:38:58 -05001579int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001580{
1581 char *buf;
1582 size_t sz = minijail_size(j);
1583 ssize_t written;
1584 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001585
Elly Jonese1749eb2011-10-07 13:54:59 -04001586 if (!sz)
1587 return -EINVAL;
1588 buf = malloc(sz);
1589 r = minijail_marshal(j, buf, sz);
1590 if (r) {
1591 free(buf);
1592 return r;
1593 }
1594 /* Sends [size][minijail]. */
1595 written = write(fd, &sz, sizeof(sz));
1596 if (written != sizeof(sz)) {
1597 free(buf);
1598 return -EFAULT;
1599 }
1600 written = write(fd, buf, sz);
1601 if (written < 0 || (size_t) written != sz) {
1602 free(buf);
1603 return -EFAULT;
1604 }
1605 free(buf);
1606 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001607}
Elly Jonescd7a9042011-07-22 13:56:51 -04001608
Will Drewry6ac91122011-10-21 16:38:58 -05001609int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001610{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001611#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001612 /* Don't use LDPRELOAD on Brillo. */
1613 return 0;
1614#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001615 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1616 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1617 if (!newenv)
1618 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001619
Elly Jonese1749eb2011-10-07 13:54:59 -04001620 /* Only insert a separating space if we have something to separate... */
1621 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1622 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001623
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001624 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001625 setenv(kLdPreloadEnvVar, newenv, 1);
1626 free(newenv);
1627 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001628#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001629}
1630
Will Drewry6ac91122011-10-21 16:38:58 -05001631int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001632{
1633 int r = pipe(fds);
1634 char fd_buf[11];
1635 if (r)
1636 return r;
1637 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1638 if (r <= 0)
1639 return -EINVAL;
1640 setenv(kFdEnvVar, fd_buf, 1);
1641 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001642}
1643
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001644int setup_pipe_end(int fds[2], size_t index)
1645{
1646 if (index > 1)
1647 return -1;
1648
1649 close(fds[1 - index]);
1650 return fds[index];
1651}
1652
1653int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1654{
1655 if (index > 1)
1656 return -1;
1657
1658 close(fds[1 - index]);
1659 /* dup2(2) the corresponding end of the pipe into |fd|. */
1660 return dup2(fds[index], fd);
1661}
1662
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001663int minijail_run_internal(struct minijail *j, const char *filename,
1664 char *const argv[], pid_t *pchild_pid,
1665 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1666 int use_preload);
1667
Will Drewry6ac91122011-10-21 16:38:58 -05001668int API minijail_run(struct minijail *j, const char *filename,
1669 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001670{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001671 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1672 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001673}
1674
1675int API minijail_run_pid(struct minijail *j, const char *filename,
1676 char *const argv[], pid_t *pchild_pid)
1677{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001678 return minijail_run_internal(j, filename, argv, pchild_pid,
1679 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001680}
1681
1682int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001683 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001684{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001685 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1686 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001687}
1688
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001689int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001690 char *const argv[], pid_t *pchild_pid,
1691 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001692{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001693 return minijail_run_internal(j, filename, argv, pchild_pid,
1694 pstdin_fd, pstdout_fd, pstderr_fd, true);
1695}
1696
1697int API minijail_run_no_preload(struct minijail *j, const char *filename,
1698 char *const argv[])
1699{
1700 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1701 false);
1702}
1703
Samuel Tan63187f42015-10-16 13:01:53 -07001704int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001705 const char *filename,
1706 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001707 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001708 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001709 int *pstderr_fd)
1710{
Samuel Tan63187f42015-10-16 13:01:53 -07001711 return minijail_run_internal(j, filename, argv, pchild_pid,
1712 pstdin_fd, pstdout_fd, pstderr_fd, false);
1713}
1714
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001715int minijail_run_internal(struct minijail *j, const char *filename,
1716 char *const argv[], pid_t *pchild_pid,
1717 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1718 int use_preload)
1719{
Elly Jonese1749eb2011-10-07 13:54:59 -04001720 char *oldenv, *oldenv_copy = NULL;
1721 pid_t child_pid;
1722 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001723 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001724 int stdout_fds[2];
1725 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001726 int child_sync_pipe_fds[2];
1727 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001728 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001729 /* We need to remember this across the minijail_preexec() call. */
1730 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001731 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001732
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001733 if (use_preload) {
1734 oldenv = getenv(kLdPreloadEnvVar);
1735 if (oldenv) {
1736 oldenv_copy = strdup(oldenv);
1737 if (!oldenv_copy)
1738 return -ENOMEM;
1739 }
1740
1741 if (setup_preload())
1742 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001743 }
Will Drewryf89aef52011-09-16 16:48:57 -05001744
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001745 if (!use_preload) {
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001746 if (j->flags.use_caps)
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001747 die("capabilities are not supported without "
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001748 "LD_PRELOAD");
1749 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001750
Elly Jonesdd3e8512012-01-23 15:13:38 -05001751 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001752 * Make the process group ID of this process equal to its PID, so that
1753 * both the Minijail process and the jailed process can be killed
1754 * together.
1755 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1756 * the process is already a process group leader.
1757 */
1758 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1759 if (errno != EPERM) {
1760 pdie("setpgid(0, 0)");
1761 }
1762 }
1763
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001764 if (use_preload) {
1765 /*
1766 * Before we fork(2) and execve(2) the child process, we need
1767 * to open a pipe(2) to send the minijail configuration over.
1768 */
1769 if (setup_pipe(pipe_fds))
1770 return -EFAULT;
1771 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001772
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001773 /*
1774 * If we want to write to the child process' standard input,
1775 * create the pipe(2) now.
1776 */
1777 if (pstdin_fd) {
1778 if (pipe(stdin_fds))
1779 return -EFAULT;
1780 }
1781
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001782 /*
1783 * If we want to read from the child process' standard output,
1784 * create the pipe(2) now.
1785 */
1786 if (pstdout_fd) {
1787 if (pipe(stdout_fds))
1788 return -EFAULT;
1789 }
1790
1791 /*
1792 * If we want to read from the child process' standard error,
1793 * create the pipe(2) now.
1794 */
1795 if (pstderr_fd) {
1796 if (pipe(stderr_fds))
1797 return -EFAULT;
1798 }
1799
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001800 /*
1801 * If we want to set up a new uid/gid mapping in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001802 * or if we need to add the child process to cgroups, create the pipe(2)
1803 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001804 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001805 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001806 sync_child = 1;
1807 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001808 return -EFAULT;
1809 }
1810
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001811 /*
1812 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001813 *
1814 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1815 *
1816 * In multithreaded programs, there are a bunch of locks inside libc,
1817 * some of which may be held by other threads at the time that we call
1818 * minijail_run_pid(). If we call fork(), glibc does its level best to
1819 * ensure that we hold all of these locks before it calls clone()
1820 * internally and drop them after clone() returns, but when we call
1821 * sys_clone(2) directly, all that gets bypassed and we end up with a
1822 * child address space where some of libc's important locks are held by
1823 * other threads (which did not get cloned, and hence will never release
1824 * those locks). This is okay so long as we call exec() immediately
1825 * after, but a bunch of seemingly-innocent libc functions like setenv()
1826 * take locks.
1827 *
1828 * Hence, only call sys_clone() if we need to, in order to get at pid
1829 * namespacing. If we follow this path, the child's address space might
1830 * have broken locks; you may only call functions that do not acquire
1831 * any locks.
1832 *
1833 * Unfortunately, fork() acquires every lock it can get its hands on, as
1834 * previously detailed, so this function is highly likely to deadlock
1835 * later on (see "deadlock here") if we're multithreaded.
1836 *
1837 * We might hack around this by having the clone()d child (init of the
1838 * pid namespace) return directly, rather than leaving the clone()d
1839 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001840 * its fork()ed child return in turn), but that process would be
1841 * crippled with its libc locks potentially broken. We might try
1842 * fork()ing in the parent before we clone() to ensure that we own all
1843 * the locks, but then we have to have the forked child hanging around
1844 * consuming resources (and possibly having file descriptors / shared
1845 * memory regions / etc attached). We'd need to keep the child around to
1846 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04001847 *
1848 * TODO(ellyjones): figure out if the "forked child hanging around"
1849 * problem is fixable or not. It would be nice if we worked in this
1850 * case.
1851 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001852 if (pid_namespace) {
1853 int clone_flags = CLONE_NEWPID | SIGCHLD;
1854 if (j->flags.userns)
1855 clone_flags |= CLONE_NEWUSER;
1856 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001857 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001858 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001859 }
Elly Jones761b7412012-06-13 15:49:52 -04001860
Elly Jonese1749eb2011-10-07 13:54:59 -04001861 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001862 if (use_preload) {
1863 free(oldenv_copy);
1864 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001865 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001866 }
Will Drewryf89aef52011-09-16 16:48:57 -05001867
Elly Jonese1749eb2011-10-07 13:54:59 -04001868 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001869 if (use_preload) {
1870 /* Restore parent's LD_PRELOAD. */
1871 if (oldenv_copy) {
1872 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1873 free(oldenv_copy);
1874 } else {
1875 unsetenv(kLdPreloadEnvVar);
1876 }
1877 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001878 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001879
Elly Jonese1749eb2011-10-07 13:54:59 -04001880 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001881
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001882 if (j->flags.pid_file)
1883 write_pid_file(j);
1884
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001885 if (j->flags.cgroups)
1886 add_to_cgroups(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001887
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001888 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001889 write_ugid_mappings(j);
1890
1891 if (sync_child)
1892 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001893
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001894 if (use_preload) {
1895 /* Send marshalled minijail. */
1896 close(pipe_fds[0]); /* read endpoint */
1897 ret = minijail_to_fd(j, pipe_fds[1]);
1898 close(pipe_fds[1]); /* write endpoint */
1899 if (ret) {
1900 kill(j->initpid, SIGKILL);
1901 die("failed to send marshalled minijail");
1902 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001903 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001904
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001905 if (pchild_pid)
1906 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001907
1908 /*
1909 * If we want to write to the child process' standard input,
1910 * set up the write end of the pipe.
1911 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001912 if (pstdin_fd)
1913 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001914 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001915
1916 /*
1917 * If we want to read from the child process' standard output,
1918 * set up the read end of the pipe.
1919 */
1920 if (pstdout_fd)
1921 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001922 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001923
1924 /*
1925 * If we want to read from the child process' standard error,
1926 * set up the read end of the pipe.
1927 */
1928 if (pstderr_fd)
1929 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001930 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001931
Elly Jonese1749eb2011-10-07 13:54:59 -04001932 return 0;
1933 }
1934 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001935
Peter Qiu2860c462015-12-16 15:13:06 -08001936 if (j->flags.reset_signal_mask) {
1937 sigset_t signal_mask;
1938 if (sigemptyset(&signal_mask) != 0)
1939 pdie("sigemptyset failed");
1940 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
1941 pdie("sigprocmask failed");
1942 }
1943
Dylan Reidce5b55e2016-01-13 11:04:16 -08001944 if (sync_child)
1945 wait_for_parent_setup(child_sync_pipe_fds);
1946
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001947 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001948 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001949
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001950 /*
1951 * If we want to write to the jailed process' standard input,
1952 * set up the read end of the pipe.
1953 */
1954 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001955 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1956 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001957 die("failed to set up stdin pipe");
1958 }
1959
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001960 /*
1961 * If we want to read from the jailed process' standard output,
1962 * set up the write end of the pipe.
1963 */
1964 if (pstdout_fd) {
1965 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1966 STDOUT_FILENO) < 0)
1967 die("failed to set up stdout pipe");
1968 }
1969
1970 /*
1971 * If we want to read from the jailed process' standard error,
1972 * set up the write end of the pipe.
1973 */
1974 if (pstderr_fd) {
1975 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1976 STDERR_FILENO) < 0)
1977 die("failed to set up stderr pipe");
1978 }
1979
Dylan Reid791f5772015-09-14 20:02:42 -07001980 /* If running an init program, let it decide when/how to mount /proc. */
1981 if (pid_namespace && !do_init)
1982 j->flags.remount_proc_ro = 0;
1983
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001984 if (use_preload) {
1985 /* Strip out flags that cannot be inherited across execve(2). */
1986 minijail_preexec(j);
1987 } else {
1988 j->flags.pids = 0;
1989 }
1990 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001991 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001992
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001993 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001994 /*
1995 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001996 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001997 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001998 * a child to actually run the program. If |do_init == 0|, we
1999 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002000 *
2001 * If we're multithreaded, we'll probably deadlock here. See
2002 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002003 */
2004 child_pid = fork();
2005 if (child_pid < 0)
2006 _exit(child_pid);
2007 else if (child_pid > 0)
2008 init(child_pid); /* never returns */
2009 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002010
Elly Jonesdd3e8512012-01-23 15:13:38 -05002011 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002012 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002013 * calling process
2014 * -> execve()-ing process
2015 * If we are:
2016 * calling process
2017 * -> init()-ing process
2018 * -> execve()-ing process
2019 */
2020 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04002021}
2022
Will Drewry6ac91122011-10-21 16:38:58 -05002023int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002024{
2025 int st;
2026 if (kill(j->initpid, SIGTERM))
2027 return -errno;
2028 if (waitpid(j->initpid, &st, 0) < 0)
2029 return -errno;
2030 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002031}
2032
Will Drewry6ac91122011-10-21 16:38:58 -05002033int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002034{
2035 int st;
2036 if (waitpid(j->initpid, &st, 0) < 0)
2037 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002038
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002039 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002040 int error_status = st;
2041 if (WIFSIGNALED(st)) {
2042 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002043 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002044 j->initpid, signum);
2045 /*
2046 * We return MINIJAIL_ERR_JAIL if the process received
2047 * SIGSYS, which happens when a syscall is blocked by
2048 * seccomp filters.
2049 * If not, we do what bash(1) does:
2050 * $? = 128 + signum
2051 */
2052 if (signum == SIGSYS) {
2053 error_status = MINIJAIL_ERR_JAIL;
2054 } else {
2055 error_status = 128 + signum;
2056 }
2057 }
2058 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002059 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002060
2061 int exit_status = WEXITSTATUS(st);
2062 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002063 info("child process %d exited with status %d",
2064 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002065
2066 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002067}
2068
Will Drewry6ac91122011-10-21 16:38:58 -05002069void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002070{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002071 size_t i;
2072
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002073 if (j->flags.seccomp_filter && j->filter_prog) {
2074 free(j->filter_prog->filter);
2075 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002076 }
Dylan Reid648b2202015-10-23 00:50:00 -07002077 while (j->mounts_head) {
2078 struct mountpoint *m = j->mounts_head;
2079 j->mounts_head = j->mounts_head->next;
2080 free(m->type);
2081 free(m->dest);
2082 free(m->src);
2083 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002084 }
Dylan Reid648b2202015-10-23 00:50:00 -07002085 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002086 if (j->user)
2087 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002088 if (j->suppl_gid_list)
2089 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002090 if (j->chrootdir)
2091 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08002092 if (j->alt_syscall_table)
2093 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002094 for (i = 0; i < j->cgroup_count; ++i)
2095 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002096 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002097}