blob: ac76faf8fe1390984feab361ea2012fb00a866d7 [file] [log] [blame]
Jorge Lucangeli Obesd613ab22015-03-03 14:22:50 -08001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04002 * Use of this source code is governed by a BSD-style license that can be
Will Drewry32ac9f52011-08-18 21:36:27 -05003 * found in the LICENSE file.
4 */
Elly Jonescd7a9042011-07-22 13:56:51 -04005
6#define _BSD_SOURCE
Arthur Gautier7a569072016-04-23 17:25:20 +00007#define _DEFAULT_SOURCE
Elly Jonescd7a9042011-07-22 13:56:51 -04008#define _GNU_SOURCE
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07009
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080010#include <asm/unistd.h>
Luis Hector Chavez43ff0802016-10-07 12:21:07 -070011#include <dirent.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040012#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070013#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040014#include <grp.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040015#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040016#include <pwd.h>
17#include <sched.h>
18#include <signal.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070019#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080020#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040024#include <sys/capability.h>
25#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050026#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040027#include <sys/prctl.h>
Dylan Reid0f72ef42017-06-06 15:42:49 -070028#include <sys/resource.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070029#include <sys/stat.h>
30#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080031#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040032#include <sys/wait.h>
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -070033#include <syscall.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040034#include <unistd.h>
35
36#include "libminijail.h"
37#include "libminijail-private.h"
38
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070039#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080040#include "syscall_filter.h"
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040041#include "syscall_wrapper.h"
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040042#include "system.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070043#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080044
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070045/* Until these are reliably available in linux/prctl.h. */
Andrew Brestickereac28942015-11-11 16:04:46 -080046#ifndef PR_ALT_SYSCALL
47# define PR_ALT_SYSCALL 0x43724f53
48#endif
49
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040050/* Seccomp filter related flags. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080051#ifndef PR_SET_NO_NEW_PRIVS
52# define PR_SET_NO_NEW_PRIVS 38
53#endif
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040054
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080055#ifndef SECCOMP_MODE_FILTER
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040056#define SECCOMP_MODE_FILTER 2 /* Uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050057#endif
58
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040059#ifndef SECCOMP_SET_MODE_STRICT
60# define SECCOMP_SET_MODE_STRICT 0
61#endif
62#ifndef SECCOMP_SET_MODE_FILTER
63# define SECCOMP_SET_MODE_FILTER 1
64#endif
65
66#ifndef SECCOMP_FILTER_FLAG_TSYNC
67# define SECCOMP_FILTER_FLAG_TSYNC 1
68#endif
69/* End seccomp filter related flags. */
70
Dylan Reid4cbc2a52016-06-17 19:06:07 -070071/* New cgroup namespace might not be in linux-headers yet. */
72#ifndef CLONE_NEWCGROUP
73# define CLONE_NEWCGROUP 0x02000000
74#endif
75
Dylan Reid605ce7f2016-01-19 19:21:00 -080076#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
77
Dylan Reid0f72ef42017-06-06 15:42:49 -070078#define MAX_RLIMITS 32 /* Currently there are 15 supported by Linux. */
79
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -080080/* Keyctl commands. */
81#define KEYCTL_JOIN_SESSION_KEYRING 1
82
Dylan Reid0f72ef42017-06-06 15:42:49 -070083struct minijail_rlimit {
84 int type;
85 uint32_t cur;
86 uint32_t max;
87};
88
Dylan Reid648b2202015-10-23 00:50:00 -070089struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040090 char *src;
91 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070092 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -070093 char *data;
94 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -070095 unsigned long flags;
96 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040097};
98
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -070099struct hook {
100 minijail_hook_t hook;
101 void *payload;
102 minijail_hook_event_t event;
103 struct hook *next;
104};
105
Will Drewryf89aef52011-09-16 16:48:57 -0500106struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700107 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700108 * WARNING: if you add a flag here you need to make sure it's
109 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700110 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400111 struct {
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700112 int uid : 1;
113 int gid : 1;
Lutz Justen13807cb2017-01-03 17:11:55 +0100114 int inherit_suppl_gids : 1;
115 int set_suppl_gids : 1;
116 int keep_suppl_gids : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700117 int use_caps : 1;
118 int capbset_drop : 1;
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400119 int set_ambient_caps : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700120 int vfs : 1;
121 int enter_vfs : 1;
122 int skip_remount_private : 1;
123 int pids : 1;
124 int ipc : 1;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400125 int uts : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700126 int net : 1;
127 int enter_net : 1;
128 int ns_cgroups : 1;
129 int userns : 1;
130 int disable_setgroups : 1;
131 int seccomp : 1;
132 int remount_proc_ro : 1;
133 int no_new_privs : 1;
134 int seccomp_filter : 1;
135 int seccomp_filter_tsync : 1;
136 int seccomp_filter_logging : 1;
137 int chroot : 1;
138 int pivot_root : 1;
139 int mount_tmp : 1;
140 int do_init : 1;
141 int pid_file : 1;
142 int cgroups : 1;
143 int alt_syscall : 1;
144 int reset_signal_mask : 1;
145 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800146 int new_session_keyring : 1;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400147 int forward_signals : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400148 } flags;
149 uid_t uid;
150 gid_t gid;
151 gid_t usergid;
152 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800153 size_t suppl_gid_count;
154 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400155 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800156 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400157 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700158 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700159 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400160 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800161 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800162 char *uidmap;
163 char *gidmap;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400164 char *hostname;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800165 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800166 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800167 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700168 struct mountpoint *mounts_head;
169 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800170 size_t mounts_count;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100171 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800172 char *cgroups[MAX_CGROUPS];
173 size_t cgroup_count;
Dylan Reid0f72ef42017-06-06 15:42:49 -0700174 struct minijail_rlimit rlimits[MAX_RLIMITS];
175 size_t rlimit_count;
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700176 uint64_t securebits_skip_mask;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700177 struct hook *hooks_head;
178 struct hook *hooks_tail;
Will Drewryf89aef52011-09-16 16:48:57 -0500179};
180
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700181/*
182 * Strip out flags meant for the parent.
183 * We keep things that are not inherited across execve(2) (e.g. capabilities),
184 * or are easier to set after execve(2) (e.g. seccomp filters).
185 */
186void minijail_preenter(struct minijail *j)
187{
188 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700189 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900190 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700191 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700192 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800193 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800194 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800195 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400196 j->flags.forward_signals = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700197}
198
199/*
200 * Strip out flags meant for the child.
201 * We keep things that are inherited across execve(2).
202 */
203void minijail_preexec(struct minijail *j)
204{
205 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700206 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800207 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700208 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800209 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700210 if (j->user)
211 free(j->user);
212 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800213 if (j->suppl_gid_list)
214 free(j->suppl_gid_list);
215 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700216 memset(&j->flags, 0, sizeof(j->flags));
217 /* Now restore anything we meant to keep. */
218 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700219 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800220 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700221 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800222 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700223 /* Note, |pids| will already have been used before this call. */
224}
225
226/* Minijail API. */
227
Will Drewry6ac91122011-10-21 16:38:58 -0500228struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400229{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400230 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400231}
232
Will Drewry6ac91122011-10-21 16:38:58 -0500233void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400234{
235 if (uid == 0)
236 die("useless change to uid 0");
237 j->uid = uid;
238 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400239}
240
Will Drewry6ac91122011-10-21 16:38:58 -0500241void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400242{
243 if (gid == 0)
244 die("useless change to gid 0");
245 j->gid = gid;
246 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400247}
248
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800249void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
250 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800251{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800252 size_t i;
253
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500254 if (j->flags.inherit_suppl_gids)
255 die("cannot inherit *and* set supplementary groups");
256 if (j->flags.keep_suppl_gids)
257 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800258
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800259 if (size == 0) {
260 /* Clear supplementary groups. */
261 j->suppl_gid_list = NULL;
262 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100263 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800264 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800265 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800266
267 /* Copy the gid_t array. */
268 j->suppl_gid_list = calloc(size, sizeof(gid_t));
269 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800270 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800271 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800272 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800273 j->suppl_gid_list[i] = list[i];
274 }
275 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100276 j->flags.set_suppl_gids = 1;
277}
278
279void API minijail_keep_supplementary_gids(struct minijail *j) {
280 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800281}
282
Will Drewry6ac91122011-10-21 16:38:58 -0500283int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400284{
285 char *buf = NULL;
286 struct passwd pw;
287 struct passwd *ppw = NULL;
288 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
289 if (sz == -1)
290 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400291
Elly Jonesdd3e8512012-01-23 15:13:38 -0500292 /*
293 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400294 * the maximum needed size of the buffer, so we don't have to search.
295 */
296 buf = malloc(sz);
297 if (!buf)
298 return -ENOMEM;
299 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500300 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800301 * We're safe to free the buffer here. The strings inside |pw| point
302 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800303 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
304 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500305 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400306 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700307 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400308 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700309 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400310 minijail_change_uid(j, ppw->pw_uid);
311 j->user = strdup(user);
312 if (!j->user)
313 return -ENOMEM;
314 j->usergid = ppw->pw_gid;
315 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400316}
317
Will Drewry6ac91122011-10-21 16:38:58 -0500318int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400319{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700320 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700321 struct group gr;
322 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400323 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
324 if (sz == -1)
325 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400326
Elly Jonesdd3e8512012-01-23 15:13:38 -0500327 /*
328 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400329 * the maximum needed size of the buffer, so we don't have to search.
330 */
331 buf = malloc(sz);
332 if (!buf)
333 return -ENOMEM;
334 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500335 /*
336 * We're safe to free the buffer here. The strings inside gr point
337 * inside buf, but we don't use any of them; this leaves the pointers
338 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
339 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400340 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700341 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400342 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700343 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400344 minijail_change_gid(j, pgr->gr_gid);
345 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400346}
347
Will Drewry6ac91122011-10-21 16:38:58 -0500348void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400349{
350 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400351}
352
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700353void API minijail_no_new_privs(struct minijail *j)
354{
355 j->flags.no_new_privs = 1;
356}
357
Will Drewry6ac91122011-10-21 16:38:58 -0500358void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400359{
360 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500361}
362
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400363void API minijail_set_seccomp_filter_tsync(struct minijail *j)
364{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400365 if (j->filter_len > 0 && j->filter_prog != NULL) {
366 die("minijail_set_seccomp_filter_tsync() must be called "
367 "before minijail_parse_seccomp_filters()");
368 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400369 j->flags.seccomp_filter_tsync = 1;
370}
371
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700372void API minijail_log_seccomp_filter_failures(struct minijail *j)
373{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400374 if (j->filter_len > 0 && j->filter_prog != NULL) {
375 die("minijail_log_seccomp_filter_failures() must be called "
376 "before minijail_parse_seccomp_filters()");
377 }
378 j->flags.seccomp_filter_logging = 1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700379}
380
Will Drewry6ac91122011-10-21 16:38:58 -0500381void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400382{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800383 /*
384 * 'minijail_use_caps' configures a runtime-capabilities-only
385 * environment, including a bounding set matching the thread's runtime
386 * (permitted|inheritable|effective) sets.
387 * Therefore, it will override any existing bounding set configurations
388 * since the latter would allow gaining extra runtime capabilities from
389 * file capabilities.
390 */
391 if (j->flags.capbset_drop) {
392 warn("overriding bounding set configuration");
393 j->cap_bset = 0;
394 j->flags.capbset_drop = 0;
395 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400396 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800397 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400398}
399
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800400void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
401{
402 if (j->flags.use_caps) {
403 /*
404 * 'minijail_use_caps' will have already configured a capability
405 * bounding set matching the (permitted|inheritable|effective)
406 * sets. Abort if the user tries to configure a separate
407 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
408 * are mutually exclusive.
409 */
410 die("runtime capabilities already configured, can't drop "
411 "bounding set separately");
412 }
413 j->cap_bset = capmask;
414 j->flags.capbset_drop = 1;
415}
416
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400417void API minijail_set_ambient_caps(struct minijail *j)
418{
419 j->flags.set_ambient_caps = 1;
420}
421
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800422void API minijail_reset_signal_mask(struct minijail *j)
423{
Peter Qiu2860c462015-12-16 15:13:06 -0800424 j->flags.reset_signal_mask = 1;
425}
426
Will Drewry6ac91122011-10-21 16:38:58 -0500427void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400428{
429 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400430}
431
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700432void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
433{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800434 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700435 if (ns_fd < 0) {
436 pdie("failed to open namespace '%s'", ns_path);
437 }
438 j->mountns_fd = ns_fd;
439 j->flags.enter_vfs = 1;
440}
441
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800442void API minijail_new_session_keyring(struct minijail *j)
443{
444 j->flags.new_session_keyring = 1;
445}
446
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700447void API minijail_skip_setting_securebits(struct minijail *j,
448 uint64_t securebits_skip_mask)
449{
450 j->securebits_skip_mask = securebits_skip_mask;
451}
452
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800453void API minijail_skip_remount_private(struct minijail *j)
454{
455 j->flags.skip_remount_private = 1;
456}
457
Will Drewry6ac91122011-10-21 16:38:58 -0500458void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400459{
Elly Jonese58176c2012-01-23 11:46:17 -0500460 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700461 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400462 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800463 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400464}
465
Dylan Reidf7942472015-11-18 17:55:26 -0800466void API minijail_namespace_ipc(struct minijail *j)
467{
468 j->flags.ipc = 1;
469}
470
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400471void API minijail_namespace_uts(struct minijail *j)
472{
473 j->flags.uts = 1;
474}
475
476int API minijail_namespace_set_hostname(struct minijail *j, const char *name)
477{
478 if (j->hostname)
479 return -EINVAL;
480 minijail_namespace_uts(j);
481 j->hostname = strdup(name);
482 if (!j->hostname)
483 return -ENOMEM;
484 return 0;
485}
486
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400487void API minijail_namespace_net(struct minijail *j)
488{
489 j->flags.net = 1;
490}
491
Dylan Reid1102f5a2015-09-15 11:52:20 -0700492void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
493{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800494 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700495 if (ns_fd < 0) {
496 pdie("failed to open namespace '%s'", ns_path);
497 }
498 j->netns_fd = ns_fd;
499 j->flags.enter_net = 1;
500}
501
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700502void API minijail_namespace_cgroups(struct minijail *j)
503{
504 j->flags.ns_cgroups = 1;
505}
506
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700507void API minijail_close_open_fds(struct minijail *j)
508{
509 j->flags.close_open_fds = 1;
510}
511
Dylan Reid791f5772015-09-14 20:02:42 -0700512void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400513{
514 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700515 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400516}
517
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800518void API minijail_namespace_user(struct minijail *j)
519{
520 j->flags.userns = 1;
521}
522
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400523void API minijail_namespace_user_disable_setgroups(struct minijail *j)
524{
525 j->flags.disable_setgroups = 1;
526}
527
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800528int API minijail_uidmap(struct minijail *j, const char *uidmap)
529{
530 j->uidmap = strdup(uidmap);
531 if (!j->uidmap)
532 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800533 char *ch;
534 for (ch = j->uidmap; *ch; ch++) {
535 if (*ch == ',')
536 *ch = '\n';
537 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800538 return 0;
539}
540
541int API minijail_gidmap(struct minijail *j, const char *gidmap)
542{
543 j->gidmap = strdup(gidmap);
544 if (!j->gidmap)
545 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800546 char *ch;
547 for (ch = j->gidmap; *ch; ch++) {
548 if (*ch == ',')
549 *ch = '\n';
550 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800551 return 0;
552}
553
Will Drewry6ac91122011-10-21 16:38:58 -0500554void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400555{
Lutz Justen13807cb2017-01-03 17:11:55 +0100556 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400557}
558
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800559void API minijail_run_as_init(struct minijail *j)
560{
561 /*
562 * Since the jailed program will become 'init' in the new PID namespace,
563 * Minijail does not need to fork an 'init' process.
564 */
565 j->flags.do_init = 0;
566}
567
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700568int API minijail_enter_chroot(struct minijail *j, const char *dir)
569{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400570 if (j->chrootdir)
571 return -EINVAL;
572 j->chrootdir = strdup(dir);
573 if (!j->chrootdir)
574 return -ENOMEM;
575 j->flags.chroot = 1;
576 return 0;
577}
578
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800579int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
580{
581 if (j->chrootdir)
582 return -EINVAL;
583 j->chrootdir = strdup(dir);
584 if (!j->chrootdir)
585 return -ENOMEM;
586 j->flags.pivot_root = 1;
587 return 0;
588}
589
Dylan Reida14e08d2015-10-22 21:05:29 -0700590char API *minijail_get_original_path(struct minijail *j,
591 const char *path_inside_chroot)
592{
Dylan Reid648b2202015-10-23 00:50:00 -0700593 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700594
Dylan Reid648b2202015-10-23 00:50:00 -0700595 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700596 while (b) {
597 /*
598 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700599 * mount, then the original path is exactly the source of
600 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700601 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700602 * mount source = /some/path/exe, mount dest =
603 * /chroot/path/exe Then when getting the original path of
604 * "/chroot/path/exe", the source of that mount,
605 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700606 */
607 if (!strcmp(b->dest, path_inside_chroot))
608 return strdup(b->src);
609
610 /*
611 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700612 * mount, take the suffix of the chroot path relative to the
613 * mount destination path, and append it to the mount source
614 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700615 */
616 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
617 const char *relative_path =
618 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400619 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700620 }
621 b = b->next;
622 }
623
624 /* If there is a chroot path, append |path_inside_chroot| to that. */
625 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400626 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700627
628 /* No chroot, so the path outside is the same as it is inside. */
629 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700630}
631
Martin Pelikánab9eb442017-01-25 11:53:58 +1100632size_t minijail_get_tmpfs_size(const struct minijail *j)
633{
634 return j->tmpfs_size;
635}
636
Lee Campbell11af0622014-05-22 12:36:04 -0700637void API minijail_mount_tmp(struct minijail *j)
638{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100639 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
640}
641
642void API minijail_mount_tmp_size(struct minijail *j, size_t size)
643{
644 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700645 j->flags.mount_tmp = 1;
646}
647
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800648int API minijail_write_pid_file(struct minijail *j, const char *path)
649{
650 j->pid_file_path = strdup(path);
651 if (!j->pid_file_path)
652 return -ENOMEM;
653 j->flags.pid_file = 1;
654 return 0;
655}
656
Dylan Reid605ce7f2016-01-19 19:21:00 -0800657int API minijail_add_to_cgroup(struct minijail *j, const char *path)
658{
659 if (j->cgroup_count >= MAX_CGROUPS)
660 return -ENOMEM;
661 j->cgroups[j->cgroup_count] = strdup(path);
662 if (!j->cgroups[j->cgroup_count])
663 return -ENOMEM;
664 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800665 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800666 return 0;
667}
668
Dylan Reid0f72ef42017-06-06 15:42:49 -0700669int API minijail_rlimit(struct minijail *j, int type, uint32_t cur,
670 uint32_t max)
671{
672 size_t i;
673
674 if (j->rlimit_count >= MAX_RLIMITS)
675 return -ENOMEM;
676 /* It's an error if the caller sets the same rlimit multiple times. */
677 for (i = 0; i < j->rlimit_count; i++) {
678 if (j->rlimits[i].type == type)
679 return -EEXIST;
680 }
681
682 j->rlimits[j->rlimit_count].type = type;
683 j->rlimits[j->rlimit_count].cur = cur;
684 j->rlimits[j->rlimit_count].max = max;
685 j->rlimit_count++;
686 return 0;
687}
688
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400689int API minijail_forward_signals(struct minijail *j)
690{
691 j->flags.forward_signals = 1;
692 return 0;
693}
694
Dylan Reid81e23972016-05-18 14:06:35 -0700695int API minijail_mount_with_data(struct minijail *j, const char *src,
696 const char *dest, const char *type,
697 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700698{
Dylan Reid648b2202015-10-23 00:50:00 -0700699 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400700
701 if (*dest != '/')
702 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700703 m = calloc(1, sizeof(*m));
704 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400705 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700706 m->dest = strdup(dest);
707 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400708 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700709 m->src = strdup(src);
710 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400711 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700712 m->type = strdup(type);
713 if (!m->type)
714 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700715 if (data) {
716 m->data = strdup(data);
717 if (!m->data)
718 goto error;
719 m->has_data = 1;
720 }
Dylan Reid648b2202015-10-23 00:50:00 -0700721 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400722
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800723 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400724
Elly Jonesdd3e8512012-01-23 15:13:38 -0500725 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700726 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400727 * containing vfs namespace.
728 */
729 minijail_namespace_vfs(j);
730
Dylan Reid648b2202015-10-23 00:50:00 -0700731 if (j->mounts_tail)
732 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400733 else
Dylan Reid648b2202015-10-23 00:50:00 -0700734 j->mounts_head = m;
735 j->mounts_tail = m;
736 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400737
738 return 0;
739
740error:
Dylan Reid81e23972016-05-18 14:06:35 -0700741 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700742 free(m->src);
743 free(m->dest);
744 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400745 return -ENOMEM;
746}
747
Dylan Reid81e23972016-05-18 14:06:35 -0700748int API minijail_mount(struct minijail *j, const char *src, const char *dest,
749 const char *type, unsigned long flags)
750{
751 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
752}
753
Dylan Reid648b2202015-10-23 00:50:00 -0700754int API minijail_bind(struct minijail *j, const char *src, const char *dest,
755 int writeable)
756{
757 unsigned long flags = MS_BIND;
758
759 if (!writeable)
760 flags |= MS_RDONLY;
761
762 return minijail_mount(j, src, dest, "", flags);
763}
764
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700765int API minijail_add_hook(struct minijail *j, minijail_hook_t hook,
766 void *payload, minijail_hook_event_t event)
767{
768 struct hook *c;
769
770 if (hook == NULL)
771 return -EINVAL;
772 if (event >= MINIJAIL_HOOK_EVENT_MAX)
773 return -EINVAL;
774 c = calloc(1, sizeof(*c));
775 if (!c)
776 return -ENOMEM;
777
778 c->hook = hook;
779 c->payload = payload;
780 c->event = event;
781
782 if (j->hooks_tail)
783 j->hooks_tail->next = c;
784 else
785 j->hooks_head = c;
786 j->hooks_tail = c;
787
788 return 0;
789}
790
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400791static void clear_seccomp_options(struct minijail *j)
792{
793 j->flags.seccomp_filter = 0;
794 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400795 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400796 j->filter_len = 0;
797 j->filter_prog = NULL;
798 j->flags.no_new_privs = 0;
799}
800
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400801static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400802{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400803 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400804 /*
805 * |errno| will be set to EINVAL when seccomp has not been
806 * compiled into the kernel. On certain platforms and kernel
807 * versions this is not a fatal failure. In that case, and only
808 * in that case, disable seccomp and skip loading the filters.
809 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400810 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400811 warn("not loading seccomp filters, seccomp filter not "
812 "supported");
813 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400814 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700815 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400816 /*
817 * If |errno| != EINVAL or seccomp_can_softfail() is false,
818 * we can proceed. Worst case scenario minijail_enter() will
819 * abort() if seccomp fails.
820 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700821 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400822 if (j->flags.seccomp_filter_tsync) {
823 /* Are the seccomp(2) syscall and the TSYNC option supported? */
824 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
825 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
826 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400827 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
828 warn("seccomp(2) syscall not supported");
829 clear_seccomp_options(j);
830 return 0;
831 } else if (saved_errno == EINVAL &&
832 seccomp_can_softfail()) {
833 warn(
834 "seccomp filter thread sync not supported");
835 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400836 return 0;
837 }
838 /*
839 * Similar logic here. If seccomp_can_softfail() is
840 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
841 * we can proceed. Worst case scenario minijail_enter()
842 * will abort() if seccomp or TSYNC fail.
843 */
844 }
845 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400846 return 1;
847}
848
849static int parse_seccomp_filters(struct minijail *j, FILE *policy_file)
850{
851 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400852 int use_ret_trap =
853 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
854 int allow_logging = j->flags.seccomp_filter_logging;
855
856 if (compile_filter(policy_file, fprog, use_ret_trap, allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400857 free(fprog);
858 return -1;
859 }
860
861 j->filter_len = fprog->len;
862 j->filter_prog = fprog;
863 return 0;
864}
865
866void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
867{
868 if (!seccomp_should_parse_filters(j))
869 return;
870
Elly Jonese1749eb2011-10-07 13:54:59 -0400871 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800872 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700873 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400874 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800875
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400876 if (parse_seccomp_filters(j, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700877 die("failed to compile seccomp filter BPF program in '%s'",
878 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800879 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400880 fclose(file);
881}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800882
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400883void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
884{
885 if (!seccomp_should_parse_filters(j))
886 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800887
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400888 FILE *file = fdopen(fd, "r");
889 if (!file) {
890 pdie("failed to associate stream with fd %d", fd);
891 }
892
893 if (parse_seccomp_filters(j, file) != 0) {
894 die("failed to compile seccomp filter BPF program from fd %d",
895 fd);
896 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400897 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500898}
899
Andrew Brestickereac28942015-11-11 16:04:46 -0800900int API minijail_use_alt_syscall(struct minijail *j, const char *table)
901{
902 j->alt_syscall_table = strdup(table);
903 if (!j->alt_syscall_table)
904 return -ENOMEM;
905 j->flags.alt_syscall = 1;
906 return 0;
907}
908
Will Drewryf89aef52011-09-16 16:48:57 -0500909struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400910 size_t available;
911 size_t total;
912 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500913};
914
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800915void marshal_state_init(struct marshal_state *state, char *buf,
916 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400917{
918 state->available = available;
919 state->buf = buf;
920 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500921}
922
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800923void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400924{
925 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500926
Elly Jonese1749eb2011-10-07 13:54:59 -0400927 /* Up to |available| will be written. */
928 if (copy_len) {
929 memcpy(state->buf, src, copy_len);
930 state->buf += copy_len;
931 state->available -= copy_len;
932 }
933 /* |total| will contain the expected length. */
934 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500935}
936
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400937void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700938{
939 marshal_append(state, m->src, strlen(m->src) + 1);
940 marshal_append(state, m->dest, strlen(m->dest) + 1);
941 marshal_append(state, m->type, strlen(m->type) + 1);
942 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
943 if (m->has_data)
944 marshal_append(state, m->data, strlen(m->data) + 1);
945 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
946}
947
Will Drewry6ac91122011-10-21 16:38:58 -0500948void minijail_marshal_helper(struct marshal_state *state,
949 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400950{
Dylan Reid648b2202015-10-23 00:50:00 -0700951 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800952 size_t i;
953
Elly Jonese1749eb2011-10-07 13:54:59 -0400954 marshal_append(state, (char *)j, sizeof(*j));
955 if (j->user)
956 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800957 if (j->suppl_gid_list) {
958 marshal_append(state, j->suppl_gid_list,
959 j->suppl_gid_count * sizeof(gid_t));
960 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400961 if (j->chrootdir)
962 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400963 if (j->hostname)
964 marshal_append(state, j->hostname, strlen(j->hostname) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800965 if (j->alt_syscall_table) {
966 marshal_append(state, j->alt_syscall_table,
967 strlen(j->alt_syscall_table) + 1);
968 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800969 if (j->flags.seccomp_filter && j->filter_prog) {
970 struct sock_fprog *fp = j->filter_prog;
971 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800972 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400973 }
Dylan Reid648b2202015-10-23 00:50:00 -0700974 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400975 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400976 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800977 for (i = 0; i < j->cgroup_count; ++i)
978 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500979}
980
Will Drewry6ac91122011-10-21 16:38:58 -0500981size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400982{
983 struct marshal_state state;
984 marshal_state_init(&state, NULL, 0);
985 minijail_marshal_helper(&state, j);
986 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500987}
988
Elly Jonese1749eb2011-10-07 13:54:59 -0400989int minijail_marshal(const struct minijail *j, char *buf, size_t available)
990{
991 struct marshal_state state;
992 marshal_state_init(&state, buf, available);
993 minijail_marshal_helper(&state, j);
994 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500995}
996
Elly Jonese1749eb2011-10-07 13:54:59 -0400997int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
998{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800999 size_t i;
1000 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -05001001 int ret = -EINVAL;
1002
Elly Jonese1749eb2011-10-07 13:54:59 -04001003 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001004 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001005 memcpy((void *)j, serialized, sizeof(*j));
1006 serialized += sizeof(*j);
1007 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001008
Will Drewrybee7ba72011-10-21 20:47:01 -05001009 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001010 j->pid_file_path = NULL;
1011 j->uidmap = NULL;
1012 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001013 j->mounts_head = NULL;
1014 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001015 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001016 j->hooks_head = NULL;
1017 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001018
Elly Jonese1749eb2011-10-07 13:54:59 -04001019 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001020 char *user = consumestr(&serialized, &length);
1021 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001022 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001023 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001024 if (!j->user)
1025 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001026 }
Will Drewryf89aef52011-09-16 16:48:57 -05001027
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001028 if (j->suppl_gid_list) { /* stale pointer */
1029 if (j->suppl_gid_count > NGROUPS_MAX) {
1030 goto bad_gid_list;
1031 }
1032 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1033 void *gid_list_bytes =
1034 consumebytes(gid_list_size, &serialized, &length);
1035 if (!gid_list_bytes)
1036 goto bad_gid_list;
1037
1038 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1039 if (!j->suppl_gid_list)
1040 goto bad_gid_list;
1041
1042 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1043 }
1044
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001045 if (j->chrootdir) { /* stale pointer */
1046 char *chrootdir = consumestr(&serialized, &length);
1047 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001048 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001049 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001050 if (!j->chrootdir)
1051 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001052 }
1053
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001054 if (j->hostname) { /* stale pointer */
1055 char *hostname = consumestr(&serialized, &length);
1056 if (!hostname)
1057 goto bad_hostname;
1058 j->hostname = strdup(hostname);
1059 if (!j->hostname)
1060 goto bad_hostname;
1061 }
1062
Andrew Brestickereac28942015-11-11 16:04:46 -08001063 if (j->alt_syscall_table) { /* stale pointer */
1064 char *alt_syscall_table = consumestr(&serialized, &length);
1065 if (!alt_syscall_table)
1066 goto bad_syscall_table;
1067 j->alt_syscall_table = strdup(alt_syscall_table);
1068 if (!j->alt_syscall_table)
1069 goto bad_syscall_table;
1070 }
1071
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001072 if (j->flags.seccomp_filter && j->filter_len > 0) {
1073 size_t ninstrs = j->filter_len;
1074 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1075 ninstrs > USHRT_MAX)
1076 goto bad_filters;
1077
1078 size_t program_len = ninstrs * sizeof(struct sock_filter);
1079 void *program = consumebytes(program_len, &serialized, &length);
1080 if (!program)
1081 goto bad_filters;
1082
1083 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001084 if (!j->filter_prog)
1085 goto bad_filters;
1086
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001087 j->filter_prog->len = ninstrs;
1088 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001089 if (!j->filter_prog->filter)
1090 goto bad_filter_prog_instrs;
1091
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001092 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001093 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001094
Dylan Reid648b2202015-10-23 00:50:00 -07001095 count = j->mounts_count;
1096 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001097 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001098 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001099 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001100 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001101 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001102 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001103 const char *src = consumestr(&serialized, &length);
1104 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001105 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001106 dest = consumestr(&serialized, &length);
1107 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001108 goto bad_mounts;
1109 type = consumestr(&serialized, &length);
1110 if (!type)
1111 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001112 has_data = consumebytes(sizeof(*has_data), &serialized,
1113 &length);
1114 if (!has_data)
1115 goto bad_mounts;
1116 if (*has_data) {
1117 data = consumestr(&serialized, &length);
1118 if (!data)
1119 goto bad_mounts;
1120 }
Dylan Reid648b2202015-10-23 00:50:00 -07001121 flags = consumebytes(sizeof(*flags), &serialized, &length);
1122 if (!flags)
1123 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001124 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001125 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001126 }
1127
Dylan Reid605ce7f2016-01-19 19:21:00 -08001128 count = j->cgroup_count;
1129 j->cgroup_count = 0;
1130 for (i = 0; i < count; ++i) {
1131 char *cgroup = consumestr(&serialized, &length);
1132 if (!cgroup)
1133 goto bad_cgroups;
1134 j->cgroups[i] = strdup(cgroup);
1135 if (!j->cgroups[i])
1136 goto bad_cgroups;
1137 ++j->cgroup_count;
1138 }
1139
Elly Jonese1749eb2011-10-07 13:54:59 -04001140 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001141
Dylan Reid605ce7f2016-01-19 19:21:00 -08001142bad_cgroups:
1143 while (j->mounts_head) {
1144 struct mountpoint *m = j->mounts_head;
1145 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07001146 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001147 free(m->type);
1148 free(m->dest);
1149 free(m->src);
1150 free(m);
1151 }
1152 for (i = 0; i < j->cgroup_count; ++i)
1153 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001154bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001155 if (j->flags.seccomp_filter && j->filter_len > 0) {
1156 free(j->filter_prog->filter);
1157 free(j->filter_prog);
1158 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001159bad_filter_prog_instrs:
1160 if (j->filter_prog)
1161 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001162bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001163 if (j->alt_syscall_table)
1164 free(j->alt_syscall_table);
1165bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001166 if (j->chrootdir)
1167 free(j->chrootdir);
1168bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001169 if (j->hostname)
1170 free(j->hostname);
1171bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001172 if (j->suppl_gid_list)
1173 free(j->suppl_gid_list);
1174bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001175 if (j->user)
1176 free(j->user);
1177clear_pointers:
1178 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001179 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001180 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001181 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001182 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001183 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001184out:
1185 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001186}
1187
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001188/*
1189 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001190 * @j Minijail these mounts are for
1191 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001192 *
1193 * Returns 0 for success.
1194 */
Dylan Reid648b2202015-10-23 00:50:00 -07001195static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001196{
Dylan Reid648b2202015-10-23 00:50:00 -07001197 int ret;
1198 char *dest;
1199 int remount_ro = 0;
1200
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001201 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001202 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001203 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001204
Dylan Reideec77962016-06-30 19:35:10 -07001205 if (setup_mount_destination(m->src, dest, j->uid, j->gid))
1206 pdie("creating mount target '%s' failed", dest);
1207
Dylan Reid648b2202015-10-23 00:50:00 -07001208 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001209 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1210 * can't both be specified in the original bind mount.
1211 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001212 */
1213 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1214 remount_ro = 1;
1215 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001216 }
Dylan Reid648b2202015-10-23 00:50:00 -07001217
Dylan Reid81e23972016-05-18 14:06:35 -07001218 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001219 if (ret)
1220 pdie("mount: %s -> %s", m->src, dest);
1221
1222 if (remount_ro) {
1223 m->flags |= MS_RDONLY;
1224 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001225 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001226 if (ret)
1227 pdie("bind ro: %s -> %s", m->src, dest);
1228 }
1229
Elly Jones51a5b6c2011-10-12 19:09:26 -04001230 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001231 if (m->next)
1232 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001233 return ret;
1234}
1235
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001236static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001237{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001238 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001239
1240 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001241 return ret;
1242
1243 if (chroot(j->chrootdir))
1244 return -errno;
1245
1246 if (chdir("/"))
1247 return -errno;
1248
1249 return 0;
1250}
1251
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001252static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001253{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001254 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001255
1256 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001257 return ret;
1258
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001259 /*
1260 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001261 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001262 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001263 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001264 if (oldroot < 0)
1265 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001266 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001267 if (newroot < 0)
1268 pdie("failed to open %s for fchdir", j->chrootdir);
1269
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001270 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001271 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001272 * do a self bind mount.
1273 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001274 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1275 pdie("failed to bind mount '%s'", j->chrootdir);
1276 if (chdir(j->chrootdir))
1277 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001278 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001279 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001280
1281 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001282 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001283 * change to the old root and unmount it.
1284 */
1285 if (fchdir(oldroot))
1286 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001287
1288 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001289 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1290 * there could be a shared mount point under |oldroot|. In that case,
1291 * mounts under this shared mount point will be unmounted below, and
1292 * this unmounting will propagate to the original mount namespace
1293 * (because the mount point is shared). To prevent this unexpected
1294 * unmounting, remove these mounts from their peer groups by recursively
1295 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001296 */
1297 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001298 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001299 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001300 if (umount2(".", MNT_DETACH))
1301 pdie("umount(/)");
1302 /* Change back to the new root. */
1303 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001304 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001305 if (close(oldroot))
1306 return -errno;
1307 if (close(newroot))
1308 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001309 if (chroot("/"))
1310 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001311 /* Set correct CWD for getcwd(3). */
1312 if (chdir("/"))
1313 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001314
1315 return 0;
1316}
1317
Martin Pelikánab9eb442017-01-25 11:53:58 +11001318static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001319{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001320 const char fmt[] = "size=%zu,mode=1777";
1321 /* Count for the user storing ULLONG_MAX literally + extra space. */
1322 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1323 int ret;
1324
1325 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1326
1327 if (ret <= 0)
1328 pdie("tmpfs size spec error");
1329 else if ((size_t)ret >= sizeof(data))
1330 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001331 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001332 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001333}
1334
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001335static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001336{
1337 const char *kProcPath = "/proc";
1338 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001339 /*
1340 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001341 * /proc in our namespace, which means using MS_REMOUNT here would
1342 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001343 * namespace (!). Instead, remove their mount from our namespace lazily
1344 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001345 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001346 if (umount2(kProcPath, MNT_DETACH)) {
1347 /*
1348 * If we are in a new user namespace, umount(2) will fail.
1349 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1350 */
1351 if (j->flags.userns) {
1352 info("umount(/proc, MNT_DETACH) failed, "
1353 "this is expected when using user namespaces");
1354 } else {
1355 return -errno;
1356 }
1357 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001358 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001359 return -errno;
1360 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001361}
1362
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001363static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001364{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001365 kill(j->initpid, SIGKILL);
1366 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001367}
1368
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001369static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001370{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001371 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001372 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001373}
1374
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001375static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001376{
1377 size_t i;
1378
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001379 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001380 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001381 kill_child_and_die(j, "failed to add to cgroups");
1382 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001383}
1384
Dylan Reid0f72ef42017-06-06 15:42:49 -07001385static void set_rlimits_or_die(const struct minijail *j)
1386{
1387 size_t i;
1388
1389 for (i = 0; i < j->rlimit_count; ++i) {
1390 struct rlimit limit;
1391 limit.rlim_cur = j->rlimits[i].cur;
1392 limit.rlim_max = j->rlimits[i].max;
1393 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1394 kill_child_and_die(j, "failed to set rlimit");
1395 }
1396}
1397
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001398static void write_ugid_maps_or_die(const struct minijail *j)
1399{
1400 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1401 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001402 if (j->gidmap && j->flags.disable_setgroups) {
1403 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1404 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001405 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001406 if (ret == -ENOENT) {
1407 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1408 warn("could not disable setgroups(2)");
1409 } else
1410 kill_child_and_die(j, "failed to disable setgroups(2)");
1411 }
1412 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001413 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1414 kill_child_and_die(j, "failed to write gid_map");
1415}
1416
1417static void enter_user_namespace(const struct minijail *j)
1418{
1419 if (j->uidmap && setresuid(0, 0, 0))
1420 pdie("user_namespaces: setresuid(0, 0, 0) failed");
1421 if (j->gidmap && setresgid(0, 0, 0))
1422 pdie("user_namespaces: setresgid(0, 0, 0) failed");
1423}
1424
1425static void parent_setup_complete(int *pipe_fds)
1426{
1427 close(pipe_fds[0]);
1428 close(pipe_fds[1]);
1429}
1430
1431/*
1432 * wait_for_parent_setup: Called by the child process to wait for any
1433 * further parent-side setup to complete before continuing.
1434 */
1435static void wait_for_parent_setup(int *pipe_fds)
1436{
1437 char buf;
1438
1439 close(pipe_fds[1]);
1440
1441 /* Wait for parent to complete setup and close the pipe. */
1442 if (read(pipe_fds[0], &buf, 1) != 0)
1443 die("failed to sync with parent");
1444 close(pipe_fds[0]);
1445}
1446
1447static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001448{
Lutz Justen13807cb2017-01-03 17:11:55 +01001449 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1450 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001451 die("can only do one of inherit, keep, or set supplementary "
1452 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001453 }
1454
Lutz Justen13807cb2017-01-03 17:11:55 +01001455 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001456 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001457 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001458 } else if (j->flags.set_suppl_gids) {
1459 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001460 pdie("setgroups(suppl_gids) failed");
Lutz Justen13807cb2017-01-03 17:11:55 +01001461 } else if (!j->flags.keep_suppl_gids) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001462 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001463 * Only attempt to clear supplementary groups if we are changing
Lutz Justen13807cb2017-01-03 17:11:55 +01001464 * users or groups.
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001465 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001466 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001467 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001468 }
1469
1470 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001471 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001472
1473 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001474 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001475}
1476
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001477static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1478{
1479 const uint64_t one = 1;
1480 unsigned int i;
1481 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1482 if (keep_mask & (one << i))
1483 continue;
1484 if (prctl(PR_CAPBSET_DROP, i))
1485 pdie("could not drop capability from bounding set");
1486 }
1487}
1488
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001489static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001490{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001491 if (!j->flags.use_caps)
1492 return;
1493
Elly Jonese1749eb2011-10-07 13:54:59 -04001494 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001495 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001496 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001497 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001498 unsigned int i;
1499 if (!caps)
1500 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001501 if (cap_clear(caps))
1502 die("can't clear caps");
1503
1504 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001505 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001506 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001507 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001508 flag[0] = i;
1509 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001510 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001511 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001512 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001513 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001514 die("can't add inheritable cap");
1515 }
1516 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001517 die("can't apply initial cleaned capset");
1518
1519 /*
1520 * Instead of dropping bounding set first, do it here in case
1521 * the caller had a more permissive bounding set which could
1522 * have been used above to raise a capability that wasn't already
1523 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1524 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001525 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001526
1527 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001528 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001529 flag[0] = CAP_SETPCAP;
1530 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1531 die("can't clear effective cap");
1532 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1533 die("can't clear permitted cap");
1534 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1535 die("can't clear inheritable cap");
1536 }
1537
1538 if (cap_set_proc(caps))
1539 die("can't apply final cleaned capset");
1540
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001541 /*
1542 * If ambient capabilities are supported, clear all capabilities first,
1543 * then raise the requested ones.
1544 */
1545 if (j->flags.set_ambient_caps) {
1546 if (!cap_ambient_supported()) {
1547 pdie("ambient capabilities not supported");
1548 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001549 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1550 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001551 pdie("can't clear ambient capabilities");
1552 }
1553
1554 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1555 if (!(j->caps & (one << i)))
1556 continue;
1557
1558 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1559 0) != 0) {
1560 pdie("prctl(PR_CAP_AMBIENT, "
1561 "PR_CAP_AMBIENT_RAISE, %u) failed",
1562 i);
1563 }
1564 }
1565 }
1566
Kees Cook323878a2013-02-05 15:35:24 -08001567 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001568}
1569
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001570static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001571{
1572 /*
1573 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1574 * in the kernel source tree for an explanation of the parameters.
1575 */
1576 if (j->flags.no_new_privs) {
1577 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1578 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1579 }
1580
1581 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001582 * Code running with ASan
1583 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1584 * will make system calls not included in the syscall filter policy,
1585 * which will likely crash the program. Skip setting seccomp filter in
1586 * that case.
1587 * 'running_with_asan()' has no inputs and is completely defined at
1588 * build time, so this cannot be used by an attacker to skip setting
1589 * seccomp filter.
1590 */
1591 if (j->flags.seccomp_filter && running_with_asan()) {
1592 warn("running with ASan, not setting seccomp filter");
1593 return;
1594 }
1595
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001596 if (j->flags.seccomp_filter) {
1597 if (j->flags.seccomp_filter_logging) {
1598 /*
1599 * If logging seccomp filter failures,
1600 * install the SIGSYS handler first.
1601 */
1602 if (install_sigsys_handler())
1603 pdie("failed to install SIGSYS handler");
1604 warn("logging seccomp filter failures");
1605 } else if (j->flags.seccomp_filter_tsync) {
1606 /*
1607 * If setting thread sync,
1608 * reset the SIGSYS signal handler so that
1609 * the entire thread group is killed.
1610 */
1611 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1612 pdie("failed to reset SIGSYS disposition");
1613 info("reset SIGSYS disposition");
1614 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001615 }
1616
1617 /*
1618 * Install the syscall filter.
1619 */
1620 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001621 if (j->flags.seccomp_filter_tsync) {
1622 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1623 SECCOMP_FILTER_FLAG_TSYNC,
1624 j->filter_prog)) {
1625 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001626 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001627 } else {
1628 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1629 j->filter_prog)) {
1630 pdie("prctl(seccomp_filter) failed");
1631 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001632 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001633 }
1634}
1635
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001636static pid_t forward_pid = -1;
1637
1638static void forward_signal(__attribute__((unused)) int nr,
1639 __attribute__((unused)) siginfo_t *siginfo,
1640 __attribute__((unused)) void *void_context)
1641{
1642 if (forward_pid != -1) {
1643 kill(forward_pid, nr);
1644 }
1645}
1646
1647static void install_signal_handlers(void)
1648{
1649 struct sigaction act;
1650
1651 memset(&act, 0, sizeof(act));
1652 act.sa_sigaction = &forward_signal;
1653 act.sa_flags = SA_SIGINFO | SA_RESTART;
1654
1655 /* Handle all signals, except SIGCHLD. */
1656 for (int nr = 1; nr < NSIG; nr++) {
1657 /*
1658 * We don't care if we get EINVAL: that just means that we
1659 * can't handle this signal, so let's skip it and continue.
1660 */
1661 sigaction(nr, &act, NULL);
1662 }
1663 /* Reset SIGCHLD's handler. */
1664 signal(SIGCHLD, SIG_DFL);
1665
1666 /* Handle real-time signals. */
1667 for (int nr = SIGRTMIN; nr <= SIGRTMAX; nr++) {
1668 sigaction(nr, &act, NULL);
1669 }
1670}
1671
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001672static const char *lookup_hook_name(minijail_hook_event_t event)
1673{
1674 switch (event) {
1675 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
1676 return "pre-drop-caps";
1677 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
1678 return "pre-execve";
1679 case MINIJAIL_HOOK_EVENT_MAX:
1680 /*
1681 * Adding this in favor of a default case to force the
1682 * compiler to error out if a new enum value is added.
1683 */
1684 break;
1685 }
1686 return "unknown";
1687}
1688
1689static void run_hooks_or_die(const struct minijail *j,
1690 minijail_hook_event_t event)
1691{
1692 int rc;
1693 int hook_index = 0;
1694 for (struct hook *c = j->hooks_head; c; c = c->next) {
1695 if (c->event != event)
1696 continue;
1697 rc = c->hook(c->payload);
1698 if (rc != 0) {
1699 errno = -rc;
1700 pdie("%s hook (index %d) failed",
1701 lookup_hook_name(event), hook_index);
1702 }
1703 /* Only increase the index within the same hook event type. */
1704 ++hook_index;
1705 }
1706}
1707
Will Drewry6ac91122011-10-21 16:38:58 -05001708void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001709{
Dylan Reidf682d472015-09-17 21:39:07 -07001710 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001711 * If we're dropping caps, get the last valid cap from /proc now,
1712 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001713 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001714 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001715 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001716 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001717
Elly Jonese1749eb2011-10-07 13:54:59 -04001718 if (j->flags.pids)
1719 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001720 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001721
Lutz Justen13807cb2017-01-03 17:11:55 +01001722 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001723 die("cannot inherit supplementary groups without setting a "
1724 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001725
Elly Jonesdd3e8512012-01-23 15:13:38 -05001726 /*
1727 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001728 * so we don't even try. If any of our operations fail, we abort() the
1729 * entire process.
1730 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001731 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001732 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001733
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001734 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001735 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001736 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001737 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001738 * Unless asked not to, remount all filesystems as private.
1739 * If they are shared, new bind mounts will creep out of our
1740 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001741 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1742 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001743 if (!j->flags.skip_remount_private) {
1744 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001745 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1746 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001747 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001748 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001749
Dylan Reidf7942472015-11-18 17:55:26 -08001750 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001751 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001752 }
1753
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001754 if (j->flags.uts) {
1755 if (unshare(CLONE_NEWUTS))
1756 pdie("unshare(CLONE_NEWUTS) failed");
1757
1758 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
1759 pdie("sethostname(%s) failed", j->hostname);
1760 }
1761
Dylan Reid1102f5a2015-09-15 11:52:20 -07001762 if (j->flags.enter_net) {
1763 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001764 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001765 } else if (j->flags.net) {
1766 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001767 pdie("unshare(CLONE_NEWNET) failed");
1768 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001769 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001770
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001771 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001772 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001773
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08001774 if (j->flags.new_session_keyring) {
1775 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
1776 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
1777 }
1778
Elly Jones51a5b6c2011-10-12 19:09:26 -04001779 if (j->flags.chroot && enter_chroot(j))
1780 pdie("chroot");
1781
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001782 if (j->flags.pivot_root && enter_pivot_root(j))
1783 pdie("pivot_root");
1784
Martin Pelikánab9eb442017-01-25 11:53:58 +11001785 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07001786 pdie("mount_tmp");
1787
Dylan Reid791f5772015-09-14 20:02:42 -07001788 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001789 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001790
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001791 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
1792
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001793 /*
1794 * If we're only dropping capabilities from the bounding set, but not
1795 * from the thread's (permitted|inheritable|effective) sets, do it now.
1796 */
1797 if (j->flags.capbset_drop) {
1798 drop_capbset(j->cap_bset, last_valid_cap);
1799 }
1800
1801 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001802 /*
1803 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001804 * capability to change uids, our attempt to use setuid()
1805 * below will fail. Hang on to root caps across setuid(), then
1806 * lock securebits.
1807 */
1808 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001809 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001810
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -07001811 if (lock_securebits(j->securebits_skip_mask) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001812 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001813 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001814 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001815
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001816 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001817 /*
1818 * If we're setting no_new_privs, we can drop privileges
1819 * before setting seccomp filter. This way filter policies
1820 * don't need to allow privilege-dropping syscalls.
1821 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001822 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001823 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001824 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001825 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001826 /*
1827 * If we're not setting no_new_privs,
1828 * we need to set seccomp filter *before* dropping privileges.
1829 * WARNING: this means that filter policies *must* allow
1830 * setgroups()/setresgid()/setresuid() for dropping root and
1831 * capget()/capset()/prctl() for dropping caps.
1832 */
1833 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001834 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001835 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001836 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001837
Elly Jonesdd3e8512012-01-23 15:13:38 -05001838 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001839 * Select the specified alternate syscall table. The table must not
1840 * block prctl(2) if we're using seccomp as well.
1841 */
1842 if (j->flags.alt_syscall) {
1843 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001844 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08001845 }
1846
1847 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001848 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001849 * privilege-dropping syscalls :)
1850 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001851 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001852 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001853 warn("seccomp not supported");
1854 return;
1855 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001856 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001857 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001858}
1859
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001860/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001861static int init_exitstatus = 0;
1862
Will Drewry6ac91122011-10-21 16:38:58 -05001863void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001864{
1865 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001866}
1867
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001868void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001869{
1870 pid_t pid;
1871 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001872 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001873 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001874 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001875 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001876 /*
1877 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001878 * left inside our pid namespace or we get a signal.
1879 */
1880 if (pid == rootpid)
1881 init_exitstatus = status;
1882 }
1883 if (!WIFEXITED(init_exitstatus))
1884 _exit(MINIJAIL_ERR_INIT);
1885 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001886}
1887
Will Drewry6ac91122011-10-21 16:38:58 -05001888int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001889{
1890 size_t sz = 0;
1891 size_t bytes = read(fd, &sz, sizeof(sz));
1892 char *buf;
1893 int r;
1894 if (sizeof(sz) != bytes)
1895 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001896 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001897 return -E2BIG;
1898 buf = malloc(sz);
1899 if (!buf)
1900 return -ENOMEM;
1901 bytes = read(fd, buf, sz);
1902 if (bytes != sz) {
1903 free(buf);
1904 return -EINVAL;
1905 }
1906 r = minijail_unmarshal(j, buf, sz);
1907 free(buf);
1908 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001909}
1910
Will Drewry6ac91122011-10-21 16:38:58 -05001911int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001912{
1913 char *buf;
1914 size_t sz = minijail_size(j);
1915 ssize_t written;
1916 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001917
Elly Jonese1749eb2011-10-07 13:54:59 -04001918 if (!sz)
1919 return -EINVAL;
1920 buf = malloc(sz);
1921 r = minijail_marshal(j, buf, sz);
1922 if (r) {
1923 free(buf);
1924 return r;
1925 }
1926 /* Sends [size][minijail]. */
1927 written = write(fd, &sz, sizeof(sz));
1928 if (written != sizeof(sz)) {
1929 free(buf);
1930 return -EFAULT;
1931 }
1932 written = write(fd, buf, sz);
1933 if (written < 0 || (size_t) written != sz) {
1934 free(buf);
1935 return -EFAULT;
1936 }
1937 free(buf);
1938 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001939}
Elly Jonescd7a9042011-07-22 13:56:51 -04001940
Will Drewry6ac91122011-10-21 16:38:58 -05001941int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001942{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001943#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001944 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001945 return 0;
1946#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001947 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1948 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1949 if (!newenv)
1950 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001951
Elly Jonese1749eb2011-10-07 13:54:59 -04001952 /* Only insert a separating space if we have something to separate... */
1953 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1954 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001955
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001956 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001957 setenv(kLdPreloadEnvVar, newenv, 1);
1958 free(newenv);
1959 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001960#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001961}
1962
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001963static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001964{
1965 int r = pipe(fds);
1966 char fd_buf[11];
1967 if (r)
1968 return r;
1969 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1970 if (r <= 0)
1971 return -EINVAL;
1972 setenv(kFdEnvVar, fd_buf, 1);
1973 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001974}
1975
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001976static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07001977{
1978 const char *kFdPath = "/proc/self/fd";
1979
1980 DIR *d = opendir(kFdPath);
1981 struct dirent *dir_entry;
1982
1983 if (d == NULL)
1984 return -1;
1985 int dir_fd = dirfd(d);
1986 while ((dir_entry = readdir(d)) != NULL) {
1987 size_t i;
1988 char *end;
1989 bool should_close = true;
1990 const int fd = strtol(dir_entry->d_name, &end, 10);
1991
1992 if ((*end) != '\0') {
1993 continue;
1994 }
1995 /*
1996 * We might have set up some pipes that we want to share with
1997 * the parent process, and should not be closed.
1998 */
1999 for (i = 0; i < size; ++i) {
2000 if (fd == inheritable_fds[i]) {
2001 should_close = false;
2002 break;
2003 }
2004 }
2005 /* Also avoid closing the directory fd. */
2006 if (should_close && fd != dir_fd)
2007 close(fd);
2008 }
2009 closedir(d);
2010 return 0;
2011}
2012
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002013int minijail_run_internal(struct minijail *j, const char *filename,
2014 char *const argv[], pid_t *pchild_pid,
2015 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
2016 int use_preload);
2017
Will Drewry6ac91122011-10-21 16:38:58 -05002018int API minijail_run(struct minijail *j, const char *filename,
2019 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002020{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002021 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
2022 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002023}
2024
2025int API minijail_run_pid(struct minijail *j, const char *filename,
2026 char *const argv[], pid_t *pchild_pid)
2027{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002028 return minijail_run_internal(j, filename, argv, pchild_pid,
2029 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002030}
2031
2032int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002033 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002034{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002035 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
2036 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002037}
2038
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002039int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002040 char *const argv[], pid_t *pchild_pid,
2041 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002042{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002043 return minijail_run_internal(j, filename, argv, pchild_pid,
2044 pstdin_fd, pstdout_fd, pstderr_fd, true);
2045}
2046
2047int API minijail_run_no_preload(struct minijail *j, const char *filename,
2048 char *const argv[])
2049{
2050 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
2051 false);
2052}
2053
Samuel Tan63187f42015-10-16 13:01:53 -07002054int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002055 const char *filename,
2056 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002057 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002058 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002059 int *pstderr_fd)
2060{
Samuel Tan63187f42015-10-16 13:01:53 -07002061 return minijail_run_internal(j, filename, argv, pchild_pid,
2062 pstdin_fd, pstdout_fd, pstderr_fd, false);
2063}
2064
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002065int minijail_run_internal(struct minijail *j, const char *filename,
2066 char *const argv[], pid_t *pchild_pid,
2067 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
2068 int use_preload)
2069{
Elly Jonese1749eb2011-10-07 13:54:59 -04002070 char *oldenv, *oldenv_copy = NULL;
2071 pid_t child_pid;
2072 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002073 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002074 int stdout_fds[2];
2075 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08002076 int child_sync_pipe_fds[2];
2077 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002078 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002079 /* We need to remember this across the minijail_preexec() call. */
2080 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002081 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07002082
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002083 if (use_preload) {
2084 oldenv = getenv(kLdPreloadEnvVar);
2085 if (oldenv) {
2086 oldenv_copy = strdup(oldenv);
2087 if (!oldenv_copy)
2088 return -ENOMEM;
2089 }
2090
2091 if (setup_preload())
2092 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002093 }
Will Drewryf89aef52011-09-16 16:48:57 -05002094
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002095 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002096 if (j->flags.use_caps && j->caps != 0 &&
2097 !j->flags.set_ambient_caps) {
2098 die("non-empty, non-ambient capabilities are not "
2099 "supported without LD_PRELOAD");
2100 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002101 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002102
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002103 if (use_preload && j->hooks_head != NULL) {
2104 die("Minijail hooks are not supported with LD_PRELOAD");
2105 }
2106
Elly Jonesdd3e8512012-01-23 15:13:38 -05002107 /*
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002108 * Make the process group ID of this process equal to its PID.
2109 * In the non-interactive case (e.g. when the parent process is started
2110 * from init) this ensures the parent process and the jailed process
2111 * can be killed together.
2112 * When the parent process is started from the console this ensures
2113 * the call to setsid(2) in the jailed process succeeds.
2114 *
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002115 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
2116 * the process is already a process group leader.
2117 */
2118 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
2119 if (errno != EPERM) {
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002120 pdie("setpgid(0, 0) failed");
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002121 }
2122 }
2123
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002124 if (use_preload) {
2125 /*
2126 * Before we fork(2) and execve(2) the child process, we need
2127 * to open a pipe(2) to send the minijail configuration over.
2128 */
2129 if (setup_pipe(pipe_fds))
2130 return -EFAULT;
2131 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002132
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002133 /*
2134 * If we want to write to the child process' standard input,
2135 * create the pipe(2) now.
2136 */
2137 if (pstdin_fd) {
2138 if (pipe(stdin_fds))
2139 return -EFAULT;
2140 }
2141
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002142 /*
2143 * If we want to read from the child process' standard output,
2144 * create the pipe(2) now.
2145 */
2146 if (pstdout_fd) {
2147 if (pipe(stdout_fds))
2148 return -EFAULT;
2149 }
2150
2151 /*
2152 * If we want to read from the child process' standard error,
2153 * create the pipe(2) now.
2154 */
2155 if (pstderr_fd) {
2156 if (pipe(stderr_fds))
2157 return -EFAULT;
2158 }
2159
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002160 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002161 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002162 * or if we need to add the child process to cgroups, create the pipe(2)
2163 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002164 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002165 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002166 sync_child = 1;
2167 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002168 return -EFAULT;
2169 }
2170
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002171 /*
2172 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002173 *
2174 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2175 *
2176 * In multithreaded programs, there are a bunch of locks inside libc,
2177 * some of which may be held by other threads at the time that we call
2178 * minijail_run_pid(). If we call fork(), glibc does its level best to
2179 * ensure that we hold all of these locks before it calls clone()
2180 * internally and drop them after clone() returns, but when we call
2181 * sys_clone(2) directly, all that gets bypassed and we end up with a
2182 * child address space where some of libc's important locks are held by
2183 * other threads (which did not get cloned, and hence will never release
2184 * those locks). This is okay so long as we call exec() immediately
2185 * after, but a bunch of seemingly-innocent libc functions like setenv()
2186 * take locks.
2187 *
2188 * Hence, only call sys_clone() if we need to, in order to get at pid
2189 * namespacing. If we follow this path, the child's address space might
2190 * have broken locks; you may only call functions that do not acquire
2191 * any locks.
2192 *
2193 * Unfortunately, fork() acquires every lock it can get its hands on, as
2194 * previously detailed, so this function is highly likely to deadlock
2195 * later on (see "deadlock here") if we're multithreaded.
2196 *
2197 * We might hack around this by having the clone()d child (init of the
2198 * pid namespace) return directly, rather than leaving the clone()d
2199 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002200 * its fork()ed child return in turn), but that process would be
2201 * crippled with its libc locks potentially broken. We might try
2202 * fork()ing in the parent before we clone() to ensure that we own all
2203 * the locks, but then we have to have the forked child hanging around
2204 * consuming resources (and possibly having file descriptors / shared
2205 * memory regions / etc attached). We'd need to keep the child around to
2206 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002207 *
2208 * TODO(ellyjones): figure out if the "forked child hanging around"
2209 * problem is fixable or not. It would be nice if we worked in this
2210 * case.
2211 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002212 if (pid_namespace) {
2213 int clone_flags = CLONE_NEWPID | SIGCHLD;
2214 if (j->flags.userns)
2215 clone_flags |= CLONE_NEWUSER;
2216 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002217 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002218 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002219 }
Elly Jones761b7412012-06-13 15:49:52 -04002220
Elly Jonese1749eb2011-10-07 13:54:59 -04002221 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002222 if (use_preload) {
2223 free(oldenv_copy);
2224 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002225 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002226 }
Will Drewryf89aef52011-09-16 16:48:57 -05002227
Elly Jonese1749eb2011-10-07 13:54:59 -04002228 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002229 if (use_preload) {
2230 /* Restore parent's LD_PRELOAD. */
2231 if (oldenv_copy) {
2232 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2233 free(oldenv_copy);
2234 } else {
2235 unsetenv(kLdPreloadEnvVar);
2236 }
2237 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002238 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002239
Elly Jonese1749eb2011-10-07 13:54:59 -04002240 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002241
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002242 if (j->flags.forward_signals) {
2243 forward_pid = child_pid;
2244 install_signal_handlers();
2245 }
2246
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002247 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002248 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002249
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002250 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002251 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002252
Dylan Reid0f72ef42017-06-06 15:42:49 -07002253 if (j->rlimit_count)
2254 set_rlimits_or_die(j);
2255
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002256 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002257 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002258
2259 if (sync_child)
2260 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002261
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002262 if (use_preload) {
2263 /* Send marshalled minijail. */
2264 close(pipe_fds[0]); /* read endpoint */
2265 ret = minijail_to_fd(j, pipe_fds[1]);
2266 close(pipe_fds[1]); /* write endpoint */
2267 if (ret) {
2268 kill(j->initpid, SIGKILL);
2269 die("failed to send marshalled minijail");
2270 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002271 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002272
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002273 if (pchild_pid)
2274 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002275
2276 /*
2277 * If we want to write to the child process' standard input,
2278 * set up the write end of the pipe.
2279 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002280 if (pstdin_fd)
2281 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002282 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002283
2284 /*
2285 * If we want to read from the child process' standard output,
2286 * set up the read end of the pipe.
2287 */
2288 if (pstdout_fd)
2289 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002290 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002291
2292 /*
2293 * If we want to read from the child process' standard error,
2294 * set up the read end of the pipe.
2295 */
2296 if (pstderr_fd)
2297 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002298 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002299
Elly Jonese1749eb2011-10-07 13:54:59 -04002300 return 0;
2301 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002302 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002303 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002304
Peter Qiu2860c462015-12-16 15:13:06 -08002305 if (j->flags.reset_signal_mask) {
2306 sigset_t signal_mask;
2307 if (sigemptyset(&signal_mask) != 0)
2308 pdie("sigemptyset failed");
2309 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2310 pdie("sigprocmask failed");
2311 }
2312
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002313 if (j->flags.close_open_fds) {
2314 const size_t kMaxInheritableFdsSize = 10;
2315 int inheritable_fds[kMaxInheritableFdsSize];
2316 size_t size = 0;
2317 if (use_preload) {
2318 inheritable_fds[size++] = pipe_fds[0];
2319 inheritable_fds[size++] = pipe_fds[1];
2320 }
2321 if (sync_child) {
2322 inheritable_fds[size++] = child_sync_pipe_fds[0];
2323 inheritable_fds[size++] = child_sync_pipe_fds[1];
2324 }
2325 if (pstdin_fd) {
2326 inheritable_fds[size++] = stdin_fds[0];
2327 inheritable_fds[size++] = stdin_fds[1];
2328 }
2329 if (pstdout_fd) {
2330 inheritable_fds[size++] = stdout_fds[0];
2331 inheritable_fds[size++] = stdout_fds[1];
2332 }
2333 if (pstderr_fd) {
2334 inheritable_fds[size++] = stderr_fds[0];
2335 inheritable_fds[size++] = stderr_fds[1];
2336 }
2337
2338 if (close_open_fds(inheritable_fds, size) < 0)
2339 die("failed to close open file descriptors");
2340 }
2341
Dylan Reidce5b55e2016-01-13 11:04:16 -08002342 if (sync_child)
2343 wait_for_parent_setup(child_sync_pipe_fds);
2344
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002345 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002346 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002347
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002348 /*
2349 * If we want to write to the jailed process' standard input,
2350 * set up the read end of the pipe.
2351 */
2352 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002353 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2354 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002355 die("failed to set up stdin pipe");
2356 }
2357
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002358 /*
2359 * If we want to read from the jailed process' standard output,
2360 * set up the write end of the pipe.
2361 */
2362 if (pstdout_fd) {
2363 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2364 STDOUT_FILENO) < 0)
2365 die("failed to set up stdout pipe");
2366 }
2367
2368 /*
2369 * If we want to read from the jailed process' standard error,
2370 * set up the write end of the pipe.
2371 */
2372 if (pstderr_fd) {
2373 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2374 STDERR_FILENO) < 0)
2375 die("failed to set up stderr pipe");
2376 }
2377
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002378 /*
2379 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2380 * This prevents the jailed process from using the TIOCSTI ioctl
2381 * to push characters into the parent process terminal's input buffer,
2382 * therefore escaping the jail.
2383 */
2384 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2385 isatty(STDERR_FILENO)) {
2386 if (setsid() < 0) {
2387 pdie("setsid() failed");
2388 }
2389 }
2390
Dylan Reid791f5772015-09-14 20:02:42 -07002391 /* If running an init program, let it decide when/how to mount /proc. */
2392 if (pid_namespace && !do_init)
2393 j->flags.remount_proc_ro = 0;
2394
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002395 if (use_preload) {
2396 /* Strip out flags that cannot be inherited across execve(2). */
2397 minijail_preexec(j);
2398 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002399 /*
2400 * If not using LD_PRELOAD, do all jailing before execve(2).
2401 * Note that PID namespaces can only be entered on fork(2),
2402 * so that flag is still cleared.
2403 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002404 j->flags.pids = 0;
2405 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002406 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002407 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002408
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002409 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002410 /*
2411 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002412 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002413 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002414 * a child to actually run the program. If |do_init == 0|, we
2415 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002416 *
2417 * If we're multithreaded, we'll probably deadlock here. See
2418 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002419 */
2420 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002421 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002422 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002423 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002424 /*
2425 * Best effort. Don't bother checking the return value.
2426 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002427 prctl(PR_SET_NAME, "minijail-init");
2428 init(child_pid); /* Never returns. */
2429 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002430 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002431
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002432 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
2433
Elly Jonesdd3e8512012-01-23 15:13:38 -05002434 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002435 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002436 * calling process
2437 * -> execve()-ing process
2438 * If we are:
2439 * calling process
2440 * -> init()-ing process
2441 * -> execve()-ing process
2442 */
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002443 ret = execve(filename, argv, environ);
2444 if (ret == -1) {
2445 pwarn("execve(%s) failed", filename);
2446 }
2447 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002448}
2449
Will Drewry6ac91122011-10-21 16:38:58 -05002450int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002451{
2452 int st;
2453 if (kill(j->initpid, SIGTERM))
2454 return -errno;
2455 if (waitpid(j->initpid, &st, 0) < 0)
2456 return -errno;
2457 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002458}
2459
Will Drewry6ac91122011-10-21 16:38:58 -05002460int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002461{
2462 int st;
2463 if (waitpid(j->initpid, &st, 0) < 0)
2464 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002465
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002466 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002467 int error_status = st;
2468 if (WIFSIGNALED(st)) {
2469 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002470 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002471 j->initpid, signum);
2472 /*
2473 * We return MINIJAIL_ERR_JAIL if the process received
2474 * SIGSYS, which happens when a syscall is blocked by
2475 * seccomp filters.
2476 * If not, we do what bash(1) does:
2477 * $? = 128 + signum
2478 */
2479 if (signum == SIGSYS) {
2480 error_status = MINIJAIL_ERR_JAIL;
2481 } else {
2482 error_status = 128 + signum;
2483 }
2484 }
2485 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002486 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002487
2488 int exit_status = WEXITSTATUS(st);
2489 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002490 info("child process %d exited with status %d",
2491 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002492
2493 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002494}
2495
Will Drewry6ac91122011-10-21 16:38:58 -05002496void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002497{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002498 size_t i;
2499
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002500 if (j->flags.seccomp_filter && j->filter_prog) {
2501 free(j->filter_prog->filter);
2502 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002503 }
Dylan Reid648b2202015-10-23 00:50:00 -07002504 while (j->mounts_head) {
2505 struct mountpoint *m = j->mounts_head;
2506 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002507 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002508 free(m->type);
2509 free(m->dest);
2510 free(m->src);
2511 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002512 }
Dylan Reid648b2202015-10-23 00:50:00 -07002513 j->mounts_tail = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002514 while (j->hooks_head) {
2515 struct hook *c = j->hooks_head;
2516 j->hooks_head = c->next;
2517 free(c);
2518 }
2519 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002520 if (j->user)
2521 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002522 if (j->suppl_gid_list)
2523 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002524 if (j->chrootdir)
2525 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002526 if (j->pid_file_path)
2527 free(j->pid_file_path);
2528 if (j->uidmap)
2529 free(j->uidmap);
2530 if (j->gidmap)
2531 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002532 if (j->hostname)
2533 free(j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08002534 if (j->alt_syscall_table)
2535 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002536 for (i = 0; i < j->cgroup_count; ++i)
2537 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002538 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002539}