blob: 89fb6096ee20da5b473bd9d2a0c520e516746cfb [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
Luis Hector Chavez1617f632017-08-01 18:32:30 -070080#define MAX_PRESERVED_FDS 10
81
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -080082/* Keyctl commands. */
83#define KEYCTL_JOIN_SESSION_KEYRING 1
84
Dylan Reid0f72ef42017-06-06 15:42:49 -070085struct minijail_rlimit {
86 int type;
87 uint32_t cur;
88 uint32_t max;
89};
90
Dylan Reid648b2202015-10-23 00:50:00 -070091struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040092 char *src;
93 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070094 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -070095 char *data;
96 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -070097 unsigned long flags;
98 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040099};
100
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700101struct hook {
102 minijail_hook_t hook;
103 void *payload;
104 minijail_hook_event_t event;
105 struct hook *next;
106};
107
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700108struct preserved_fd {
109 int parent_fd;
110 int child_fd;
111};
112
Will Drewryf89aef52011-09-16 16:48:57 -0500113struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700114 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700115 * WARNING: if you add a flag here you need to make sure it's
116 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700117 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400118 struct {
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700119 int uid : 1;
120 int gid : 1;
Lutz Justen13807cb2017-01-03 17:11:55 +0100121 int inherit_suppl_gids : 1;
122 int set_suppl_gids : 1;
123 int keep_suppl_gids : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700124 int use_caps : 1;
125 int capbset_drop : 1;
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400126 int set_ambient_caps : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700127 int vfs : 1;
128 int enter_vfs : 1;
129 int skip_remount_private : 1;
130 int pids : 1;
131 int ipc : 1;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400132 int uts : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700133 int net : 1;
134 int enter_net : 1;
135 int ns_cgroups : 1;
136 int userns : 1;
137 int disable_setgroups : 1;
138 int seccomp : 1;
139 int remount_proc_ro : 1;
140 int no_new_privs : 1;
141 int seccomp_filter : 1;
142 int seccomp_filter_tsync : 1;
143 int seccomp_filter_logging : 1;
144 int chroot : 1;
145 int pivot_root : 1;
146 int mount_tmp : 1;
147 int do_init : 1;
148 int pid_file : 1;
149 int cgroups : 1;
150 int alt_syscall : 1;
151 int reset_signal_mask : 1;
152 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800153 int new_session_keyring : 1;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400154 int forward_signals : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400155 } flags;
156 uid_t uid;
157 gid_t gid;
158 gid_t usergid;
159 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800160 size_t suppl_gid_count;
161 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400162 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800163 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400164 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700165 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700166 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400167 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800168 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800169 char *uidmap;
170 char *gidmap;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400171 char *hostname;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800172 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800173 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800174 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700175 struct mountpoint *mounts_head;
176 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800177 size_t mounts_count;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100178 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800179 char *cgroups[MAX_CGROUPS];
180 size_t cgroup_count;
Dylan Reid0f72ef42017-06-06 15:42:49 -0700181 struct minijail_rlimit rlimits[MAX_RLIMITS];
182 size_t rlimit_count;
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700183 uint64_t securebits_skip_mask;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700184 struct hook *hooks_head;
185 struct hook *hooks_tail;
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700186 struct preserved_fd preserved_fds[MAX_PRESERVED_FDS];
187 size_t preserved_fd_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500188};
189
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700190/*
191 * Strip out flags meant for the parent.
192 * We keep things that are not inherited across execve(2) (e.g. capabilities),
193 * or are easier to set after execve(2) (e.g. seccomp filters).
194 */
195void minijail_preenter(struct minijail *j)
196{
197 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700198 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900199 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700200 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700201 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800202 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800203 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800204 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400205 j->flags.forward_signals = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700206}
207
208/*
209 * Strip out flags meant for the child.
210 * We keep things that are inherited across execve(2).
211 */
212void minijail_preexec(struct minijail *j)
213{
214 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700215 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800216 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700217 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800218 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700219 if (j->user)
220 free(j->user);
221 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800222 if (j->suppl_gid_list)
223 free(j->suppl_gid_list);
224 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700225 memset(&j->flags, 0, sizeof(j->flags));
226 /* Now restore anything we meant to keep. */
227 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700228 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800229 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700230 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800231 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700232 /* Note, |pids| will already have been used before this call. */
233}
234
235/* Minijail API. */
236
Will Drewry6ac91122011-10-21 16:38:58 -0500237struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400238{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400239 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400240}
241
Will Drewry6ac91122011-10-21 16:38:58 -0500242void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400243{
244 if (uid == 0)
245 die("useless change to uid 0");
246 j->uid = uid;
247 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400248}
249
Will Drewry6ac91122011-10-21 16:38:58 -0500250void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400251{
252 if (gid == 0)
253 die("useless change to gid 0");
254 j->gid = gid;
255 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400256}
257
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800258void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
259 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800260{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800261 size_t i;
262
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500263 if (j->flags.inherit_suppl_gids)
264 die("cannot inherit *and* set supplementary groups");
265 if (j->flags.keep_suppl_gids)
266 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800267
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800268 if (size == 0) {
269 /* Clear supplementary groups. */
270 j->suppl_gid_list = NULL;
271 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100272 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800273 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800274 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800275
276 /* Copy the gid_t array. */
277 j->suppl_gid_list = calloc(size, sizeof(gid_t));
278 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800279 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800280 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800281 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800282 j->suppl_gid_list[i] = list[i];
283 }
284 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100285 j->flags.set_suppl_gids = 1;
286}
287
288void API minijail_keep_supplementary_gids(struct minijail *j) {
289 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800290}
291
Will Drewry6ac91122011-10-21 16:38:58 -0500292int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400293{
294 char *buf = NULL;
295 struct passwd pw;
296 struct passwd *ppw = NULL;
297 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
298 if (sz == -1)
299 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400300
Elly Jonesdd3e8512012-01-23 15:13:38 -0500301 /*
302 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400303 * the maximum needed size of the buffer, so we don't have to search.
304 */
305 buf = malloc(sz);
306 if (!buf)
307 return -ENOMEM;
308 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500309 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800310 * We're safe to free the buffer here. The strings inside |pw| point
311 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800312 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
313 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500314 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400315 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700316 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400317 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700318 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400319 minijail_change_uid(j, ppw->pw_uid);
320 j->user = strdup(user);
321 if (!j->user)
322 return -ENOMEM;
323 j->usergid = ppw->pw_gid;
324 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400325}
326
Will Drewry6ac91122011-10-21 16:38:58 -0500327int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400328{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700329 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700330 struct group gr;
331 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400332 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
333 if (sz == -1)
334 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400335
Elly Jonesdd3e8512012-01-23 15:13:38 -0500336 /*
337 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400338 * the maximum needed size of the buffer, so we don't have to search.
339 */
340 buf = malloc(sz);
341 if (!buf)
342 return -ENOMEM;
343 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500344 /*
345 * We're safe to free the buffer here. The strings inside gr point
346 * inside buf, but we don't use any of them; this leaves the pointers
347 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
348 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400349 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700350 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400351 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700352 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400353 minijail_change_gid(j, pgr->gr_gid);
354 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400355}
356
Will Drewry6ac91122011-10-21 16:38:58 -0500357void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400358{
359 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400360}
361
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700362void API minijail_no_new_privs(struct minijail *j)
363{
364 j->flags.no_new_privs = 1;
365}
366
Will Drewry6ac91122011-10-21 16:38:58 -0500367void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400368{
369 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500370}
371
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400372void API minijail_set_seccomp_filter_tsync(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_set_seccomp_filter_tsync() must be called "
376 "before minijail_parse_seccomp_filters()");
377 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400378 j->flags.seccomp_filter_tsync = 1;
379}
380
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700381void API minijail_log_seccomp_filter_failures(struct minijail *j)
382{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400383 if (j->filter_len > 0 && j->filter_prog != NULL) {
384 die("minijail_log_seccomp_filter_failures() must be called "
385 "before minijail_parse_seccomp_filters()");
386 }
387 j->flags.seccomp_filter_logging = 1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700388}
389
Will Drewry6ac91122011-10-21 16:38:58 -0500390void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400391{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800392 /*
393 * 'minijail_use_caps' configures a runtime-capabilities-only
394 * environment, including a bounding set matching the thread's runtime
395 * (permitted|inheritable|effective) sets.
396 * Therefore, it will override any existing bounding set configurations
397 * since the latter would allow gaining extra runtime capabilities from
398 * file capabilities.
399 */
400 if (j->flags.capbset_drop) {
401 warn("overriding bounding set configuration");
402 j->cap_bset = 0;
403 j->flags.capbset_drop = 0;
404 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400405 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800406 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400407}
408
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800409void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
410{
411 if (j->flags.use_caps) {
412 /*
413 * 'minijail_use_caps' will have already configured a capability
414 * bounding set matching the (permitted|inheritable|effective)
415 * sets. Abort if the user tries to configure a separate
416 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
417 * are mutually exclusive.
418 */
419 die("runtime capabilities already configured, can't drop "
420 "bounding set separately");
421 }
422 j->cap_bset = capmask;
423 j->flags.capbset_drop = 1;
424}
425
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400426void API minijail_set_ambient_caps(struct minijail *j)
427{
428 j->flags.set_ambient_caps = 1;
429}
430
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800431void API minijail_reset_signal_mask(struct minijail *j)
432{
Peter Qiu2860c462015-12-16 15:13:06 -0800433 j->flags.reset_signal_mask = 1;
434}
435
Will Drewry6ac91122011-10-21 16:38:58 -0500436void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400437{
438 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400439}
440
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700441void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
442{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800443 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700444 if (ns_fd < 0) {
445 pdie("failed to open namespace '%s'", ns_path);
446 }
447 j->mountns_fd = ns_fd;
448 j->flags.enter_vfs = 1;
449}
450
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800451void API minijail_new_session_keyring(struct minijail *j)
452{
453 j->flags.new_session_keyring = 1;
454}
455
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700456void API minijail_skip_setting_securebits(struct minijail *j,
457 uint64_t securebits_skip_mask)
458{
459 j->securebits_skip_mask = securebits_skip_mask;
460}
461
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800462void API minijail_skip_remount_private(struct minijail *j)
463{
464 j->flags.skip_remount_private = 1;
465}
466
Will Drewry6ac91122011-10-21 16:38:58 -0500467void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400468{
Elly Jonese58176c2012-01-23 11:46:17 -0500469 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700470 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400471 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800472 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400473}
474
Dylan Reidf7942472015-11-18 17:55:26 -0800475void API minijail_namespace_ipc(struct minijail *j)
476{
477 j->flags.ipc = 1;
478}
479
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400480void API minijail_namespace_uts(struct minijail *j)
481{
482 j->flags.uts = 1;
483}
484
485int API minijail_namespace_set_hostname(struct minijail *j, const char *name)
486{
487 if (j->hostname)
488 return -EINVAL;
489 minijail_namespace_uts(j);
490 j->hostname = strdup(name);
491 if (!j->hostname)
492 return -ENOMEM;
493 return 0;
494}
495
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400496void API minijail_namespace_net(struct minijail *j)
497{
498 j->flags.net = 1;
499}
500
Dylan Reid1102f5a2015-09-15 11:52:20 -0700501void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
502{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800503 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700504 if (ns_fd < 0) {
505 pdie("failed to open namespace '%s'", ns_path);
506 }
507 j->netns_fd = ns_fd;
508 j->flags.enter_net = 1;
509}
510
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700511void API minijail_namespace_cgroups(struct minijail *j)
512{
513 j->flags.ns_cgroups = 1;
514}
515
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700516void API minijail_close_open_fds(struct minijail *j)
517{
518 j->flags.close_open_fds = 1;
519}
520
Dylan Reid791f5772015-09-14 20:02:42 -0700521void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400522{
523 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700524 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400525}
526
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800527void API minijail_namespace_user(struct minijail *j)
528{
529 j->flags.userns = 1;
530}
531
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400532void API minijail_namespace_user_disable_setgroups(struct minijail *j)
533{
534 j->flags.disable_setgroups = 1;
535}
536
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800537int API minijail_uidmap(struct minijail *j, const char *uidmap)
538{
539 j->uidmap = strdup(uidmap);
540 if (!j->uidmap)
541 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800542 char *ch;
543 for (ch = j->uidmap; *ch; ch++) {
544 if (*ch == ',')
545 *ch = '\n';
546 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800547 return 0;
548}
549
550int API minijail_gidmap(struct minijail *j, const char *gidmap)
551{
552 j->gidmap = strdup(gidmap);
553 if (!j->gidmap)
554 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800555 char *ch;
556 for (ch = j->gidmap; *ch; ch++) {
557 if (*ch == ',')
558 *ch = '\n';
559 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800560 return 0;
561}
562
Will Drewry6ac91122011-10-21 16:38:58 -0500563void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400564{
Lutz Justen13807cb2017-01-03 17:11:55 +0100565 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400566}
567
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800568void API minijail_run_as_init(struct minijail *j)
569{
570 /*
571 * Since the jailed program will become 'init' in the new PID namespace,
572 * Minijail does not need to fork an 'init' process.
573 */
574 j->flags.do_init = 0;
575}
576
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700577int API minijail_enter_chroot(struct minijail *j, const char *dir)
578{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400579 if (j->chrootdir)
580 return -EINVAL;
581 j->chrootdir = strdup(dir);
582 if (!j->chrootdir)
583 return -ENOMEM;
584 j->flags.chroot = 1;
585 return 0;
586}
587
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800588int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
589{
590 if (j->chrootdir)
591 return -EINVAL;
592 j->chrootdir = strdup(dir);
593 if (!j->chrootdir)
594 return -ENOMEM;
595 j->flags.pivot_root = 1;
596 return 0;
597}
598
Dylan Reida14e08d2015-10-22 21:05:29 -0700599char API *minijail_get_original_path(struct minijail *j,
600 const char *path_inside_chroot)
601{
Dylan Reid648b2202015-10-23 00:50:00 -0700602 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700603
Dylan Reid648b2202015-10-23 00:50:00 -0700604 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700605 while (b) {
606 /*
607 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700608 * mount, then the original path is exactly the source of
609 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700610 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700611 * mount source = /some/path/exe, mount dest =
612 * /chroot/path/exe Then when getting the original path of
613 * "/chroot/path/exe", the source of that mount,
614 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700615 */
616 if (!strcmp(b->dest, path_inside_chroot))
617 return strdup(b->src);
618
619 /*
620 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700621 * mount, take the suffix of the chroot path relative to the
622 * mount destination path, and append it to the mount source
623 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700624 */
625 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
626 const char *relative_path =
627 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400628 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700629 }
630 b = b->next;
631 }
632
633 /* If there is a chroot path, append |path_inside_chroot| to that. */
634 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400635 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700636
637 /* No chroot, so the path outside is the same as it is inside. */
638 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700639}
640
Martin Pelikánab9eb442017-01-25 11:53:58 +1100641size_t minijail_get_tmpfs_size(const struct minijail *j)
642{
643 return j->tmpfs_size;
644}
645
Lee Campbell11af0622014-05-22 12:36:04 -0700646void API minijail_mount_tmp(struct minijail *j)
647{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100648 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
649}
650
651void API minijail_mount_tmp_size(struct minijail *j, size_t size)
652{
653 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700654 j->flags.mount_tmp = 1;
655}
656
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800657int API minijail_write_pid_file(struct minijail *j, const char *path)
658{
659 j->pid_file_path = strdup(path);
660 if (!j->pid_file_path)
661 return -ENOMEM;
662 j->flags.pid_file = 1;
663 return 0;
664}
665
Dylan Reid605ce7f2016-01-19 19:21:00 -0800666int API minijail_add_to_cgroup(struct minijail *j, const char *path)
667{
668 if (j->cgroup_count >= MAX_CGROUPS)
669 return -ENOMEM;
670 j->cgroups[j->cgroup_count] = strdup(path);
671 if (!j->cgroups[j->cgroup_count])
672 return -ENOMEM;
673 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800674 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800675 return 0;
676}
677
Dylan Reid0f72ef42017-06-06 15:42:49 -0700678int API minijail_rlimit(struct minijail *j, int type, uint32_t cur,
679 uint32_t max)
680{
681 size_t i;
682
683 if (j->rlimit_count >= MAX_RLIMITS)
684 return -ENOMEM;
685 /* It's an error if the caller sets the same rlimit multiple times. */
686 for (i = 0; i < j->rlimit_count; i++) {
687 if (j->rlimits[i].type == type)
688 return -EEXIST;
689 }
690
691 j->rlimits[j->rlimit_count].type = type;
692 j->rlimits[j->rlimit_count].cur = cur;
693 j->rlimits[j->rlimit_count].max = max;
694 j->rlimit_count++;
695 return 0;
696}
697
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400698int API minijail_forward_signals(struct minijail *j)
699{
700 j->flags.forward_signals = 1;
701 return 0;
702}
703
Dylan Reid81e23972016-05-18 14:06:35 -0700704int API minijail_mount_with_data(struct minijail *j, const char *src,
705 const char *dest, const char *type,
706 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700707{
Dylan Reid648b2202015-10-23 00:50:00 -0700708 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400709
710 if (*dest != '/')
711 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700712 m = calloc(1, sizeof(*m));
713 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400714 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700715 m->dest = strdup(dest);
716 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400717 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700718 m->src = strdup(src);
719 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400720 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700721 m->type = strdup(type);
722 if (!m->type)
723 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700724 if (data) {
725 m->data = strdup(data);
726 if (!m->data)
727 goto error;
728 m->has_data = 1;
729 }
Dylan Reid648b2202015-10-23 00:50:00 -0700730 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400731
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800732 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400733
Elly Jonesdd3e8512012-01-23 15:13:38 -0500734 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700735 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400736 * containing vfs namespace.
737 */
738 minijail_namespace_vfs(j);
739
Dylan Reid648b2202015-10-23 00:50:00 -0700740 if (j->mounts_tail)
741 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400742 else
Dylan Reid648b2202015-10-23 00:50:00 -0700743 j->mounts_head = m;
744 j->mounts_tail = m;
745 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400746
747 return 0;
748
749error:
Dylan Reid81e23972016-05-18 14:06:35 -0700750 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700751 free(m->src);
752 free(m->dest);
753 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400754 return -ENOMEM;
755}
756
Dylan Reid81e23972016-05-18 14:06:35 -0700757int API minijail_mount(struct minijail *j, const char *src, const char *dest,
758 const char *type, unsigned long flags)
759{
760 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
761}
762
Dylan Reid648b2202015-10-23 00:50:00 -0700763int API minijail_bind(struct minijail *j, const char *src, const char *dest,
764 int writeable)
765{
766 unsigned long flags = MS_BIND;
767
768 if (!writeable)
769 flags |= MS_RDONLY;
770
771 return minijail_mount(j, src, dest, "", flags);
772}
773
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700774int API minijail_add_hook(struct minijail *j, minijail_hook_t hook,
775 void *payload, minijail_hook_event_t event)
776{
777 struct hook *c;
778
779 if (hook == NULL)
780 return -EINVAL;
781 if (event >= MINIJAIL_HOOK_EVENT_MAX)
782 return -EINVAL;
783 c = calloc(1, sizeof(*c));
784 if (!c)
785 return -ENOMEM;
786
787 c->hook = hook;
788 c->payload = payload;
789 c->event = event;
790
791 if (j->hooks_tail)
792 j->hooks_tail->next = c;
793 else
794 j->hooks_head = c;
795 j->hooks_tail = c;
796
797 return 0;
798}
799
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700800int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd)
801{
802 if (parent_fd < 0 || child_fd < 0)
803 return -EINVAL;
804 if (j->preserved_fd_count >= MAX_PRESERVED_FDS)
805 return -ENOMEM;
806 j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd;
807 j->preserved_fds[j->preserved_fd_count].child_fd = child_fd;
808 j->preserved_fd_count++;
809 return 0;
810}
811
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400812static void clear_seccomp_options(struct minijail *j)
813{
814 j->flags.seccomp_filter = 0;
815 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400816 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400817 j->filter_len = 0;
818 j->filter_prog = NULL;
819 j->flags.no_new_privs = 0;
820}
821
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400822static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400823{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400824 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400825 /*
826 * |errno| will be set to EINVAL when seccomp has not been
827 * compiled into the kernel. On certain platforms and kernel
828 * versions this is not a fatal failure. In that case, and only
829 * in that case, disable seccomp and skip loading the filters.
830 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400831 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400832 warn("not loading seccomp filters, seccomp filter not "
833 "supported");
834 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400835 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700836 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400837 /*
838 * If |errno| != EINVAL or seccomp_can_softfail() is false,
839 * we can proceed. Worst case scenario minijail_enter() will
840 * abort() if seccomp fails.
841 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700842 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400843 if (j->flags.seccomp_filter_tsync) {
844 /* Are the seccomp(2) syscall and the TSYNC option supported? */
845 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
846 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
847 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400848 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
849 warn("seccomp(2) syscall not supported");
850 clear_seccomp_options(j);
851 return 0;
852 } else if (saved_errno == EINVAL &&
853 seccomp_can_softfail()) {
854 warn(
855 "seccomp filter thread sync not supported");
856 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400857 return 0;
858 }
859 /*
860 * Similar logic here. If seccomp_can_softfail() is
861 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
862 * we can proceed. Worst case scenario minijail_enter()
863 * will abort() if seccomp or TSYNC fail.
864 */
865 }
866 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400867 return 1;
868}
869
870static int parse_seccomp_filters(struct minijail *j, FILE *policy_file)
871{
872 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400873 int use_ret_trap =
874 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
875 int allow_logging = j->flags.seccomp_filter_logging;
876
877 if (compile_filter(policy_file, fprog, use_ret_trap, allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400878 free(fprog);
879 return -1;
880 }
881
882 j->filter_len = fprog->len;
883 j->filter_prog = fprog;
884 return 0;
885}
886
887void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
888{
889 if (!seccomp_should_parse_filters(j))
890 return;
891
Elly Jonese1749eb2011-10-07 13:54:59 -0400892 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800893 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700894 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400895 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800896
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400897 if (parse_seccomp_filters(j, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700898 die("failed to compile seccomp filter BPF program in '%s'",
899 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800900 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400901 fclose(file);
902}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800903
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400904void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
905{
906 if (!seccomp_should_parse_filters(j))
907 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800908
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400909 FILE *file = fdopen(fd, "r");
910 if (!file) {
911 pdie("failed to associate stream with fd %d", fd);
912 }
913
914 if (parse_seccomp_filters(j, file) != 0) {
915 die("failed to compile seccomp filter BPF program from fd %d",
916 fd);
917 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400918 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500919}
920
Andrew Brestickereac28942015-11-11 16:04:46 -0800921int API minijail_use_alt_syscall(struct minijail *j, const char *table)
922{
923 j->alt_syscall_table = strdup(table);
924 if (!j->alt_syscall_table)
925 return -ENOMEM;
926 j->flags.alt_syscall = 1;
927 return 0;
928}
929
Will Drewryf89aef52011-09-16 16:48:57 -0500930struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400931 size_t available;
932 size_t total;
933 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500934};
935
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800936void marshal_state_init(struct marshal_state *state, char *buf,
937 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400938{
939 state->available = available;
940 state->buf = buf;
941 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500942}
943
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800944void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400945{
946 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500947
Elly Jonese1749eb2011-10-07 13:54:59 -0400948 /* Up to |available| will be written. */
949 if (copy_len) {
950 memcpy(state->buf, src, copy_len);
951 state->buf += copy_len;
952 state->available -= copy_len;
953 }
954 /* |total| will contain the expected length. */
955 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500956}
957
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400958void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700959{
960 marshal_append(state, m->src, strlen(m->src) + 1);
961 marshal_append(state, m->dest, strlen(m->dest) + 1);
962 marshal_append(state, m->type, strlen(m->type) + 1);
963 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
964 if (m->has_data)
965 marshal_append(state, m->data, strlen(m->data) + 1);
966 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
967}
968
Will Drewry6ac91122011-10-21 16:38:58 -0500969void minijail_marshal_helper(struct marshal_state *state,
970 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400971{
Dylan Reid648b2202015-10-23 00:50:00 -0700972 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800973 size_t i;
974
Elly Jonese1749eb2011-10-07 13:54:59 -0400975 marshal_append(state, (char *)j, sizeof(*j));
976 if (j->user)
977 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800978 if (j->suppl_gid_list) {
979 marshal_append(state, j->suppl_gid_list,
980 j->suppl_gid_count * sizeof(gid_t));
981 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400982 if (j->chrootdir)
983 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400984 if (j->hostname)
985 marshal_append(state, j->hostname, strlen(j->hostname) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800986 if (j->alt_syscall_table) {
987 marshal_append(state, j->alt_syscall_table,
988 strlen(j->alt_syscall_table) + 1);
989 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800990 if (j->flags.seccomp_filter && j->filter_prog) {
991 struct sock_fprog *fp = j->filter_prog;
992 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800993 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400994 }
Dylan Reid648b2202015-10-23 00:50:00 -0700995 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400996 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400997 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800998 for (i = 0; i < j->cgroup_count; ++i)
999 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -05001000}
1001
Will Drewry6ac91122011-10-21 16:38:58 -05001002size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001003{
1004 struct marshal_state state;
1005 marshal_state_init(&state, NULL, 0);
1006 minijail_marshal_helper(&state, j);
1007 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -05001008}
1009
Elly Jonese1749eb2011-10-07 13:54:59 -04001010int minijail_marshal(const struct minijail *j, char *buf, size_t available)
1011{
1012 struct marshal_state state;
1013 marshal_state_init(&state, buf, available);
1014 minijail_marshal_helper(&state, j);
1015 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -05001016}
1017
Elly Jonese1749eb2011-10-07 13:54:59 -04001018int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
1019{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -08001020 size_t i;
1021 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -05001022 int ret = -EINVAL;
1023
Elly Jonese1749eb2011-10-07 13:54:59 -04001024 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001025 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001026 memcpy((void *)j, serialized, sizeof(*j));
1027 serialized += sizeof(*j);
1028 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001029
Will Drewrybee7ba72011-10-21 20:47:01 -05001030 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001031 j->pid_file_path = NULL;
1032 j->uidmap = NULL;
1033 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001034 j->mounts_head = NULL;
1035 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001036 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001037 j->hooks_head = NULL;
1038 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001039
Elly Jonese1749eb2011-10-07 13:54:59 -04001040 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001041 char *user = consumestr(&serialized, &length);
1042 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001043 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001044 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001045 if (!j->user)
1046 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001047 }
Will Drewryf89aef52011-09-16 16:48:57 -05001048
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001049 if (j->suppl_gid_list) { /* stale pointer */
1050 if (j->suppl_gid_count > NGROUPS_MAX) {
1051 goto bad_gid_list;
1052 }
1053 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1054 void *gid_list_bytes =
1055 consumebytes(gid_list_size, &serialized, &length);
1056 if (!gid_list_bytes)
1057 goto bad_gid_list;
1058
1059 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1060 if (!j->suppl_gid_list)
1061 goto bad_gid_list;
1062
1063 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1064 }
1065
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001066 if (j->chrootdir) { /* stale pointer */
1067 char *chrootdir = consumestr(&serialized, &length);
1068 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001069 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001070 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001071 if (!j->chrootdir)
1072 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001073 }
1074
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001075 if (j->hostname) { /* stale pointer */
1076 char *hostname = consumestr(&serialized, &length);
1077 if (!hostname)
1078 goto bad_hostname;
1079 j->hostname = strdup(hostname);
1080 if (!j->hostname)
1081 goto bad_hostname;
1082 }
1083
Andrew Brestickereac28942015-11-11 16:04:46 -08001084 if (j->alt_syscall_table) { /* stale pointer */
1085 char *alt_syscall_table = consumestr(&serialized, &length);
1086 if (!alt_syscall_table)
1087 goto bad_syscall_table;
1088 j->alt_syscall_table = strdup(alt_syscall_table);
1089 if (!j->alt_syscall_table)
1090 goto bad_syscall_table;
1091 }
1092
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001093 if (j->flags.seccomp_filter && j->filter_len > 0) {
1094 size_t ninstrs = j->filter_len;
1095 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1096 ninstrs > USHRT_MAX)
1097 goto bad_filters;
1098
1099 size_t program_len = ninstrs * sizeof(struct sock_filter);
1100 void *program = consumebytes(program_len, &serialized, &length);
1101 if (!program)
1102 goto bad_filters;
1103
1104 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001105 if (!j->filter_prog)
1106 goto bad_filters;
1107
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001108 j->filter_prog->len = ninstrs;
1109 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001110 if (!j->filter_prog->filter)
1111 goto bad_filter_prog_instrs;
1112
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001113 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001114 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001115
Dylan Reid648b2202015-10-23 00:50:00 -07001116 count = j->mounts_count;
1117 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001118 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001119 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001120 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001121 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001122 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001123 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001124 const char *src = consumestr(&serialized, &length);
1125 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001126 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001127 dest = consumestr(&serialized, &length);
1128 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001129 goto bad_mounts;
1130 type = consumestr(&serialized, &length);
1131 if (!type)
1132 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001133 has_data = consumebytes(sizeof(*has_data), &serialized,
1134 &length);
1135 if (!has_data)
1136 goto bad_mounts;
1137 if (*has_data) {
1138 data = consumestr(&serialized, &length);
1139 if (!data)
1140 goto bad_mounts;
1141 }
Dylan Reid648b2202015-10-23 00:50:00 -07001142 flags = consumebytes(sizeof(*flags), &serialized, &length);
1143 if (!flags)
1144 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001145 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001146 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001147 }
1148
Dylan Reid605ce7f2016-01-19 19:21:00 -08001149 count = j->cgroup_count;
1150 j->cgroup_count = 0;
1151 for (i = 0; i < count; ++i) {
1152 char *cgroup = consumestr(&serialized, &length);
1153 if (!cgroup)
1154 goto bad_cgroups;
1155 j->cgroups[i] = strdup(cgroup);
1156 if (!j->cgroups[i])
1157 goto bad_cgroups;
1158 ++j->cgroup_count;
1159 }
1160
Elly Jonese1749eb2011-10-07 13:54:59 -04001161 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001162
Dylan Reid605ce7f2016-01-19 19:21:00 -08001163bad_cgroups:
1164 while (j->mounts_head) {
1165 struct mountpoint *m = j->mounts_head;
1166 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07001167 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001168 free(m->type);
1169 free(m->dest);
1170 free(m->src);
1171 free(m);
1172 }
1173 for (i = 0; i < j->cgroup_count; ++i)
1174 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001175bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001176 if (j->flags.seccomp_filter && j->filter_len > 0) {
1177 free(j->filter_prog->filter);
1178 free(j->filter_prog);
1179 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001180bad_filter_prog_instrs:
1181 if (j->filter_prog)
1182 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001183bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001184 if (j->alt_syscall_table)
1185 free(j->alt_syscall_table);
1186bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001187 if (j->chrootdir)
1188 free(j->chrootdir);
1189bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001190 if (j->hostname)
1191 free(j->hostname);
1192bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001193 if (j->suppl_gid_list)
1194 free(j->suppl_gid_list);
1195bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001196 if (j->user)
1197 free(j->user);
1198clear_pointers:
1199 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001200 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001201 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001202 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001203 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001204 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001205out:
1206 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001207}
1208
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001209/*
1210 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001211 * @j Minijail these mounts are for
1212 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001213 *
1214 * Returns 0 for success.
1215 */
Dylan Reid648b2202015-10-23 00:50:00 -07001216static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001217{
Dylan Reid648b2202015-10-23 00:50:00 -07001218 int ret;
1219 char *dest;
1220 int remount_ro = 0;
1221
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001222 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001223 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001224 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001225
Dylan Reideec77962016-06-30 19:35:10 -07001226 if (setup_mount_destination(m->src, dest, j->uid, j->gid))
1227 pdie("creating mount target '%s' failed", dest);
1228
Dylan Reid648b2202015-10-23 00:50:00 -07001229 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001230 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1231 * can't both be specified in the original bind mount.
1232 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001233 */
1234 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1235 remount_ro = 1;
1236 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001237 }
Dylan Reid648b2202015-10-23 00:50:00 -07001238
Dylan Reid81e23972016-05-18 14:06:35 -07001239 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001240 if (ret)
1241 pdie("mount: %s -> %s", m->src, dest);
1242
1243 if (remount_ro) {
1244 m->flags |= MS_RDONLY;
1245 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001246 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001247 if (ret)
1248 pdie("bind ro: %s -> %s", m->src, dest);
1249 }
1250
Elly Jones51a5b6c2011-10-12 19:09:26 -04001251 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001252 if (m->next)
1253 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001254 return ret;
1255}
1256
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001257static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001258{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001259 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001260
1261 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001262 return ret;
1263
1264 if (chroot(j->chrootdir))
1265 return -errno;
1266
1267 if (chdir("/"))
1268 return -errno;
1269
1270 return 0;
1271}
1272
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001273static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001274{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001275 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001276
1277 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001278 return ret;
1279
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001280 /*
1281 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001282 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001283 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001284 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001285 if (oldroot < 0)
1286 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001287 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001288 if (newroot < 0)
1289 pdie("failed to open %s for fchdir", j->chrootdir);
1290
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001291 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001292 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001293 * do a self bind mount.
1294 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001295 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1296 pdie("failed to bind mount '%s'", j->chrootdir);
1297 if (chdir(j->chrootdir))
1298 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001299 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001300 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001301
1302 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001303 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001304 * change to the old root and unmount it.
1305 */
1306 if (fchdir(oldroot))
1307 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001308
1309 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001310 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1311 * there could be a shared mount point under |oldroot|. In that case,
1312 * mounts under this shared mount point will be unmounted below, and
1313 * this unmounting will propagate to the original mount namespace
1314 * (because the mount point is shared). To prevent this unexpected
1315 * unmounting, remove these mounts from their peer groups by recursively
1316 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001317 */
1318 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001319 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001320 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001321 if (umount2(".", MNT_DETACH))
1322 pdie("umount(/)");
1323 /* Change back to the new root. */
1324 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001325 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001326 if (close(oldroot))
1327 return -errno;
1328 if (close(newroot))
1329 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001330 if (chroot("/"))
1331 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001332 /* Set correct CWD for getcwd(3). */
1333 if (chdir("/"))
1334 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001335
1336 return 0;
1337}
1338
Martin Pelikánab9eb442017-01-25 11:53:58 +11001339static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001340{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001341 const char fmt[] = "size=%zu,mode=1777";
1342 /* Count for the user storing ULLONG_MAX literally + extra space. */
1343 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1344 int ret;
1345
1346 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1347
1348 if (ret <= 0)
1349 pdie("tmpfs size spec error");
1350 else if ((size_t)ret >= sizeof(data))
1351 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001352 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001353 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001354}
1355
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001356static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001357{
1358 const char *kProcPath = "/proc";
1359 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001360 /*
1361 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001362 * /proc in our namespace, which means using MS_REMOUNT here would
1363 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001364 * namespace (!). Instead, remove their mount from our namespace lazily
1365 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001366 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001367 if (umount2(kProcPath, MNT_DETACH)) {
1368 /*
1369 * If we are in a new user namespace, umount(2) will fail.
1370 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1371 */
1372 if (j->flags.userns) {
1373 info("umount(/proc, MNT_DETACH) failed, "
1374 "this is expected when using user namespaces");
1375 } else {
1376 return -errno;
1377 }
1378 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001379 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001380 return -errno;
1381 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001382}
1383
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001384static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001385{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001386 kill(j->initpid, SIGKILL);
1387 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001388}
1389
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001390static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001391{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001392 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001393 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001394}
1395
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001396static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001397{
1398 size_t i;
1399
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001400 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001401 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001402 kill_child_and_die(j, "failed to add to cgroups");
1403 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001404}
1405
Dylan Reid0f72ef42017-06-06 15:42:49 -07001406static void set_rlimits_or_die(const struct minijail *j)
1407{
1408 size_t i;
1409
1410 for (i = 0; i < j->rlimit_count; ++i) {
1411 struct rlimit limit;
1412 limit.rlim_cur = j->rlimits[i].cur;
1413 limit.rlim_max = j->rlimits[i].max;
1414 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1415 kill_child_and_die(j, "failed to set rlimit");
1416 }
1417}
1418
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001419static void write_ugid_maps_or_die(const struct minijail *j)
1420{
1421 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1422 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001423 if (j->gidmap && j->flags.disable_setgroups) {
1424 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1425 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001426 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001427 if (ret == -ENOENT) {
1428 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1429 warn("could not disable setgroups(2)");
1430 } else
1431 kill_child_and_die(j, "failed to disable setgroups(2)");
1432 }
1433 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001434 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1435 kill_child_and_die(j, "failed to write gid_map");
1436}
1437
1438static void enter_user_namespace(const struct minijail *j)
1439{
1440 if (j->uidmap && setresuid(0, 0, 0))
1441 pdie("user_namespaces: setresuid(0, 0, 0) failed");
1442 if (j->gidmap && setresgid(0, 0, 0))
1443 pdie("user_namespaces: setresgid(0, 0, 0) failed");
1444}
1445
1446static void parent_setup_complete(int *pipe_fds)
1447{
1448 close(pipe_fds[0]);
1449 close(pipe_fds[1]);
1450}
1451
1452/*
1453 * wait_for_parent_setup: Called by the child process to wait for any
1454 * further parent-side setup to complete before continuing.
1455 */
1456static void wait_for_parent_setup(int *pipe_fds)
1457{
1458 char buf;
1459
1460 close(pipe_fds[1]);
1461
1462 /* Wait for parent to complete setup and close the pipe. */
1463 if (read(pipe_fds[0], &buf, 1) != 0)
1464 die("failed to sync with parent");
1465 close(pipe_fds[0]);
1466}
1467
1468static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001469{
Lutz Justen13807cb2017-01-03 17:11:55 +01001470 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1471 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001472 die("can only do one of inherit, keep, or set supplementary "
1473 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001474 }
1475
Lutz Justen13807cb2017-01-03 17:11:55 +01001476 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001477 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001478 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001479 } else if (j->flags.set_suppl_gids) {
1480 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001481 pdie("setgroups(suppl_gids) failed");
Lutz Justen13807cb2017-01-03 17:11:55 +01001482 } else if (!j->flags.keep_suppl_gids) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001483 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001484 * Only attempt to clear supplementary groups if we are changing
Lutz Justen13807cb2017-01-03 17:11:55 +01001485 * users or groups.
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001486 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001487 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001488 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001489 }
1490
1491 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001492 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001493
1494 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001495 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001496}
1497
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001498static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1499{
1500 const uint64_t one = 1;
1501 unsigned int i;
1502 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1503 if (keep_mask & (one << i))
1504 continue;
1505 if (prctl(PR_CAPBSET_DROP, i))
1506 pdie("could not drop capability from bounding set");
1507 }
1508}
1509
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001510static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001511{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001512 if (!j->flags.use_caps)
1513 return;
1514
Elly Jonese1749eb2011-10-07 13:54:59 -04001515 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001516 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001517 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001518 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001519 unsigned int i;
1520 if (!caps)
1521 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001522 if (cap_clear(caps))
1523 die("can't clear caps");
1524
1525 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001526 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001527 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001528 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001529 flag[0] = i;
1530 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001531 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001532 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001533 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001534 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001535 die("can't add inheritable cap");
1536 }
1537 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001538 die("can't apply initial cleaned capset");
1539
1540 /*
1541 * Instead of dropping bounding set first, do it here in case
1542 * the caller had a more permissive bounding set which could
1543 * have been used above to raise a capability that wasn't already
1544 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1545 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001546 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001547
1548 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001549 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001550 flag[0] = CAP_SETPCAP;
1551 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1552 die("can't clear effective cap");
1553 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1554 die("can't clear permitted cap");
1555 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1556 die("can't clear inheritable cap");
1557 }
1558
1559 if (cap_set_proc(caps))
1560 die("can't apply final cleaned capset");
1561
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001562 /*
1563 * If ambient capabilities are supported, clear all capabilities first,
1564 * then raise the requested ones.
1565 */
1566 if (j->flags.set_ambient_caps) {
1567 if (!cap_ambient_supported()) {
1568 pdie("ambient capabilities not supported");
1569 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001570 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1571 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001572 pdie("can't clear ambient capabilities");
1573 }
1574
1575 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1576 if (!(j->caps & (one << i)))
1577 continue;
1578
1579 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1580 0) != 0) {
1581 pdie("prctl(PR_CAP_AMBIENT, "
1582 "PR_CAP_AMBIENT_RAISE, %u) failed",
1583 i);
1584 }
1585 }
1586 }
1587
Kees Cook323878a2013-02-05 15:35:24 -08001588 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001589}
1590
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001591static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001592{
1593 /*
1594 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1595 * in the kernel source tree for an explanation of the parameters.
1596 */
1597 if (j->flags.no_new_privs) {
1598 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1599 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1600 }
1601
1602 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001603 * Code running with ASan
1604 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1605 * will make system calls not included in the syscall filter policy,
1606 * which will likely crash the program. Skip setting seccomp filter in
1607 * that case.
1608 * 'running_with_asan()' has no inputs and is completely defined at
1609 * build time, so this cannot be used by an attacker to skip setting
1610 * seccomp filter.
1611 */
1612 if (j->flags.seccomp_filter && running_with_asan()) {
1613 warn("running with ASan, not setting seccomp filter");
1614 return;
1615 }
1616
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001617 if (j->flags.seccomp_filter) {
1618 if (j->flags.seccomp_filter_logging) {
1619 /*
1620 * If logging seccomp filter failures,
1621 * install the SIGSYS handler first.
1622 */
1623 if (install_sigsys_handler())
1624 pdie("failed to install SIGSYS handler");
1625 warn("logging seccomp filter failures");
1626 } else if (j->flags.seccomp_filter_tsync) {
1627 /*
1628 * If setting thread sync,
1629 * reset the SIGSYS signal handler so that
1630 * the entire thread group is killed.
1631 */
1632 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1633 pdie("failed to reset SIGSYS disposition");
1634 info("reset SIGSYS disposition");
1635 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001636 }
1637
1638 /*
1639 * Install the syscall filter.
1640 */
1641 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001642 if (j->flags.seccomp_filter_tsync) {
1643 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1644 SECCOMP_FILTER_FLAG_TSYNC,
1645 j->filter_prog)) {
1646 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001647 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001648 } else {
1649 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1650 j->filter_prog)) {
1651 pdie("prctl(seccomp_filter) failed");
1652 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001653 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001654 }
1655}
1656
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001657static pid_t forward_pid = -1;
1658
1659static void forward_signal(__attribute__((unused)) int nr,
1660 __attribute__((unused)) siginfo_t *siginfo,
1661 __attribute__((unused)) void *void_context)
1662{
1663 if (forward_pid != -1) {
1664 kill(forward_pid, nr);
1665 }
1666}
1667
1668static void install_signal_handlers(void)
1669{
1670 struct sigaction act;
1671
1672 memset(&act, 0, sizeof(act));
1673 act.sa_sigaction = &forward_signal;
1674 act.sa_flags = SA_SIGINFO | SA_RESTART;
1675
1676 /* Handle all signals, except SIGCHLD. */
1677 for (int nr = 1; nr < NSIG; nr++) {
1678 /*
1679 * We don't care if we get EINVAL: that just means that we
1680 * can't handle this signal, so let's skip it and continue.
1681 */
1682 sigaction(nr, &act, NULL);
1683 }
1684 /* Reset SIGCHLD's handler. */
1685 signal(SIGCHLD, SIG_DFL);
1686
1687 /* Handle real-time signals. */
1688 for (int nr = SIGRTMIN; nr <= SIGRTMAX; nr++) {
1689 sigaction(nr, &act, NULL);
1690 }
1691}
1692
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001693static const char *lookup_hook_name(minijail_hook_event_t event)
1694{
1695 switch (event) {
1696 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
1697 return "pre-drop-caps";
1698 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
1699 return "pre-execve";
1700 case MINIJAIL_HOOK_EVENT_MAX:
1701 /*
1702 * Adding this in favor of a default case to force the
1703 * compiler to error out if a new enum value is added.
1704 */
1705 break;
1706 }
1707 return "unknown";
1708}
1709
1710static void run_hooks_or_die(const struct minijail *j,
1711 minijail_hook_event_t event)
1712{
1713 int rc;
1714 int hook_index = 0;
1715 for (struct hook *c = j->hooks_head; c; c = c->next) {
1716 if (c->event != event)
1717 continue;
1718 rc = c->hook(c->payload);
1719 if (rc != 0) {
1720 errno = -rc;
1721 pdie("%s hook (index %d) failed",
1722 lookup_hook_name(event), hook_index);
1723 }
1724 /* Only increase the index within the same hook event type. */
1725 ++hook_index;
1726 }
1727}
1728
Will Drewry6ac91122011-10-21 16:38:58 -05001729void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001730{
Dylan Reidf682d472015-09-17 21:39:07 -07001731 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001732 * If we're dropping caps, get the last valid cap from /proc now,
1733 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001734 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001735 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001736 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001737 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001738
Elly Jonese1749eb2011-10-07 13:54:59 -04001739 if (j->flags.pids)
1740 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001741 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001742
Lutz Justen13807cb2017-01-03 17:11:55 +01001743 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001744 die("cannot inherit supplementary groups without setting a "
1745 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001746
Elly Jonesdd3e8512012-01-23 15:13:38 -05001747 /*
1748 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001749 * so we don't even try. If any of our operations fail, we abort() the
1750 * entire process.
1751 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001752 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001753 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001754
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001755 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001756 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001757 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001758 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001759 * Unless asked not to, remount all filesystems as private.
1760 * If they are shared, new bind mounts will creep out of our
1761 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001762 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1763 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001764 if (!j->flags.skip_remount_private) {
1765 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001766 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1767 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001768 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001769 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001770
Dylan Reidf7942472015-11-18 17:55:26 -08001771 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001772 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001773 }
1774
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001775 if (j->flags.uts) {
1776 if (unshare(CLONE_NEWUTS))
1777 pdie("unshare(CLONE_NEWUTS) failed");
1778
1779 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
1780 pdie("sethostname(%s) failed", j->hostname);
1781 }
1782
Dylan Reid1102f5a2015-09-15 11:52:20 -07001783 if (j->flags.enter_net) {
1784 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001785 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001786 } else if (j->flags.net) {
1787 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001788 pdie("unshare(CLONE_NEWNET) failed");
1789 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001790 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001791
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001792 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001793 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001794
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08001795 if (j->flags.new_session_keyring) {
1796 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
1797 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
1798 }
1799
Elly Jones51a5b6c2011-10-12 19:09:26 -04001800 if (j->flags.chroot && enter_chroot(j))
1801 pdie("chroot");
1802
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001803 if (j->flags.pivot_root && enter_pivot_root(j))
1804 pdie("pivot_root");
1805
Martin Pelikánab9eb442017-01-25 11:53:58 +11001806 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07001807 pdie("mount_tmp");
1808
Dylan Reid791f5772015-09-14 20:02:42 -07001809 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001810 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001811
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001812 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
1813
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001814 /*
1815 * If we're only dropping capabilities from the bounding set, but not
1816 * from the thread's (permitted|inheritable|effective) sets, do it now.
1817 */
1818 if (j->flags.capbset_drop) {
1819 drop_capbset(j->cap_bset, last_valid_cap);
1820 }
1821
1822 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001823 /*
1824 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001825 * capability to change uids, our attempt to use setuid()
1826 * below will fail. Hang on to root caps across setuid(), then
1827 * lock securebits.
1828 */
1829 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001830 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001831
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -07001832 if (lock_securebits(j->securebits_skip_mask) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001833 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001834 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001835 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001836
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001837 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001838 /*
1839 * If we're setting no_new_privs, we can drop privileges
1840 * before setting seccomp filter. This way filter policies
1841 * don't need to allow privilege-dropping syscalls.
1842 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001843 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001844 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001845 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001846 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001847 /*
1848 * If we're not setting no_new_privs,
1849 * we need to set seccomp filter *before* dropping privileges.
1850 * WARNING: this means that filter policies *must* allow
1851 * setgroups()/setresgid()/setresuid() for dropping root and
1852 * capget()/capset()/prctl() for dropping caps.
1853 */
1854 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001855 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001856 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001857 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001858
Elly Jonesdd3e8512012-01-23 15:13:38 -05001859 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001860 * Select the specified alternate syscall table. The table must not
1861 * block prctl(2) if we're using seccomp as well.
1862 */
1863 if (j->flags.alt_syscall) {
1864 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001865 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08001866 }
1867
1868 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001869 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001870 * privilege-dropping syscalls :)
1871 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001872 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001873 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001874 warn("seccomp not supported");
1875 return;
1876 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001877 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001878 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001879}
1880
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001881/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001882static int init_exitstatus = 0;
1883
Will Drewry6ac91122011-10-21 16:38:58 -05001884void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001885{
1886 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001887}
1888
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001889void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001890{
1891 pid_t pid;
1892 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001893 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001894 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001895 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001896 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001897 /*
1898 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001899 * left inside our pid namespace or we get a signal.
1900 */
1901 if (pid == rootpid)
1902 init_exitstatus = status;
1903 }
1904 if (!WIFEXITED(init_exitstatus))
1905 _exit(MINIJAIL_ERR_INIT);
1906 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001907}
1908
Will Drewry6ac91122011-10-21 16:38:58 -05001909int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001910{
1911 size_t sz = 0;
1912 size_t bytes = read(fd, &sz, sizeof(sz));
1913 char *buf;
1914 int r;
1915 if (sizeof(sz) != bytes)
1916 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001917 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001918 return -E2BIG;
1919 buf = malloc(sz);
1920 if (!buf)
1921 return -ENOMEM;
1922 bytes = read(fd, buf, sz);
1923 if (bytes != sz) {
1924 free(buf);
1925 return -EINVAL;
1926 }
1927 r = minijail_unmarshal(j, buf, sz);
1928 free(buf);
1929 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001930}
1931
Will Drewry6ac91122011-10-21 16:38:58 -05001932int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001933{
1934 char *buf;
1935 size_t sz = minijail_size(j);
1936 ssize_t written;
1937 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001938
Elly Jonese1749eb2011-10-07 13:54:59 -04001939 if (!sz)
1940 return -EINVAL;
1941 buf = malloc(sz);
1942 r = minijail_marshal(j, buf, sz);
1943 if (r) {
1944 free(buf);
1945 return r;
1946 }
1947 /* Sends [size][minijail]. */
1948 written = write(fd, &sz, sizeof(sz));
1949 if (written != sizeof(sz)) {
1950 free(buf);
1951 return -EFAULT;
1952 }
1953 written = write(fd, buf, sz);
1954 if (written < 0 || (size_t) written != sz) {
1955 free(buf);
1956 return -EFAULT;
1957 }
1958 free(buf);
1959 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001960}
Elly Jonescd7a9042011-07-22 13:56:51 -04001961
Will Drewry6ac91122011-10-21 16:38:58 -05001962int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001963{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001964#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001965 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001966 return 0;
1967#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001968 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1969 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1970 if (!newenv)
1971 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001972
Elly Jonese1749eb2011-10-07 13:54:59 -04001973 /* Only insert a separating space if we have something to separate... */
1974 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1975 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001976
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001977 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001978 setenv(kLdPreloadEnvVar, newenv, 1);
1979 free(newenv);
1980 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001981#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001982}
1983
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001984static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001985{
1986 int r = pipe(fds);
1987 char fd_buf[11];
1988 if (r)
1989 return r;
1990 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1991 if (r <= 0)
1992 return -EINVAL;
1993 setenv(kFdEnvVar, fd_buf, 1);
1994 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001995}
1996
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001997static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07001998{
1999 const char *kFdPath = "/proc/self/fd";
2000
2001 DIR *d = opendir(kFdPath);
2002 struct dirent *dir_entry;
2003
2004 if (d == NULL)
2005 return -1;
2006 int dir_fd = dirfd(d);
2007 while ((dir_entry = readdir(d)) != NULL) {
2008 size_t i;
2009 char *end;
2010 bool should_close = true;
2011 const int fd = strtol(dir_entry->d_name, &end, 10);
2012
2013 if ((*end) != '\0') {
2014 continue;
2015 }
2016 /*
2017 * We might have set up some pipes that we want to share with
2018 * the parent process, and should not be closed.
2019 */
2020 for (i = 0; i < size; ++i) {
2021 if (fd == inheritable_fds[i]) {
2022 should_close = false;
2023 break;
2024 }
2025 }
2026 /* Also avoid closing the directory fd. */
2027 if (should_close && fd != dir_fd)
2028 close(fd);
2029 }
2030 closedir(d);
2031 return 0;
2032}
2033
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002034static int redirect_fds(struct minijail *j)
2035{
2036 size_t i, i2;
2037 int closeable;
2038 for (i = 0; i < j->preserved_fd_count; i++) {
2039 if (dup2(j->preserved_fds[i].parent_fd,
2040 j->preserved_fds[i].child_fd) == -1) {
2041 return -1;
2042 }
2043 }
2044 /*
2045 * After all fds have been duped, we are now free to close all parent
2046 * fds that are *not* child fds.
2047 */
2048 for (i = 0; i < j->preserved_fd_count; i++) {
2049 closeable = true;
2050 for (i2 = 0; i2 < j->preserved_fd_count; i2++) {
2051 closeable &= j->preserved_fds[i].parent_fd !=
2052 j->preserved_fds[i2].child_fd;
2053 }
2054 if (closeable)
2055 close(j->preserved_fds[i].parent_fd);
2056 }
2057 return 0;
2058}
2059
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002060int minijail_run_internal(struct minijail *j, const char *filename,
2061 char *const argv[], pid_t *pchild_pid,
2062 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
2063 int use_preload);
2064
Will Drewry6ac91122011-10-21 16:38:58 -05002065int API minijail_run(struct minijail *j, const char *filename,
2066 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002067{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002068 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
2069 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002070}
2071
2072int API minijail_run_pid(struct minijail *j, const char *filename,
2073 char *const argv[], pid_t *pchild_pid)
2074{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002075 return minijail_run_internal(j, filename, argv, pchild_pid,
2076 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002077}
2078
2079int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002080 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002081{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002082 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
2083 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002084}
2085
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002086int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002087 char *const argv[], pid_t *pchild_pid,
2088 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002089{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002090 return minijail_run_internal(j, filename, argv, pchild_pid,
2091 pstdin_fd, pstdout_fd, pstderr_fd, true);
2092}
2093
2094int API minijail_run_no_preload(struct minijail *j, const char *filename,
2095 char *const argv[])
2096{
2097 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
2098 false);
2099}
2100
Samuel Tan63187f42015-10-16 13:01:53 -07002101int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002102 const char *filename,
2103 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002104 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002105 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002106 int *pstderr_fd)
2107{
Samuel Tan63187f42015-10-16 13:01:53 -07002108 return minijail_run_internal(j, filename, argv, pchild_pid,
2109 pstdin_fd, pstdout_fd, pstderr_fd, false);
2110}
2111
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002112int minijail_run_internal(struct minijail *j, const char *filename,
2113 char *const argv[], pid_t *pchild_pid,
2114 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
2115 int use_preload)
2116{
Elly Jonese1749eb2011-10-07 13:54:59 -04002117 char *oldenv, *oldenv_copy = NULL;
2118 pid_t child_pid;
2119 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002120 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002121 int stdout_fds[2];
2122 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08002123 int child_sync_pipe_fds[2];
2124 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002125 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002126 /* We need to remember this across the minijail_preexec() call. */
2127 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002128 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07002129
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002130 if (use_preload) {
2131 oldenv = getenv(kLdPreloadEnvVar);
2132 if (oldenv) {
2133 oldenv_copy = strdup(oldenv);
2134 if (!oldenv_copy)
2135 return -ENOMEM;
2136 }
2137
2138 if (setup_preload())
2139 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002140 }
Will Drewryf89aef52011-09-16 16:48:57 -05002141
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002142 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002143 if (j->flags.use_caps && j->caps != 0 &&
2144 !j->flags.set_ambient_caps) {
2145 die("non-empty, non-ambient capabilities are not "
2146 "supported without LD_PRELOAD");
2147 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002148 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002149
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002150 if (use_preload && j->hooks_head != NULL) {
2151 die("Minijail hooks are not supported with LD_PRELOAD");
2152 }
2153
Elly Jonesdd3e8512012-01-23 15:13:38 -05002154 /*
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002155 * Make the process group ID of this process equal to its PID.
2156 * In the non-interactive case (e.g. when the parent process is started
2157 * from init) this ensures the parent process and the jailed process
2158 * can be killed together.
2159 * When the parent process is started from the console this ensures
2160 * the call to setsid(2) in the jailed process succeeds.
2161 *
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002162 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
2163 * the process is already a process group leader.
2164 */
2165 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
2166 if (errno != EPERM) {
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002167 pdie("setpgid(0, 0) failed");
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002168 }
2169 }
2170
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002171 if (use_preload) {
2172 /*
2173 * Before we fork(2) and execve(2) the child process, we need
2174 * to open a pipe(2) to send the minijail configuration over.
2175 */
2176 if (setup_pipe(pipe_fds))
2177 return -EFAULT;
2178 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002179
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002180 /*
2181 * If we want to write to the child process' standard input,
2182 * create the pipe(2) now.
2183 */
2184 if (pstdin_fd) {
2185 if (pipe(stdin_fds))
2186 return -EFAULT;
2187 }
2188
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002189 /*
2190 * If we want to read from the child process' standard output,
2191 * create the pipe(2) now.
2192 */
2193 if (pstdout_fd) {
2194 if (pipe(stdout_fds))
2195 return -EFAULT;
2196 }
2197
2198 /*
2199 * If we want to read from the child process' standard error,
2200 * create the pipe(2) now.
2201 */
2202 if (pstderr_fd) {
2203 if (pipe(stderr_fds))
2204 return -EFAULT;
2205 }
2206
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002207 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002208 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002209 * or if we need to add the child process to cgroups, create the pipe(2)
2210 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002211 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002212 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002213 sync_child = 1;
2214 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002215 return -EFAULT;
2216 }
2217
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002218 /*
2219 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002220 *
2221 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2222 *
2223 * In multithreaded programs, there are a bunch of locks inside libc,
2224 * some of which may be held by other threads at the time that we call
2225 * minijail_run_pid(). If we call fork(), glibc does its level best to
2226 * ensure that we hold all of these locks before it calls clone()
2227 * internally and drop them after clone() returns, but when we call
2228 * sys_clone(2) directly, all that gets bypassed and we end up with a
2229 * child address space where some of libc's important locks are held by
2230 * other threads (which did not get cloned, and hence will never release
2231 * those locks). This is okay so long as we call exec() immediately
2232 * after, but a bunch of seemingly-innocent libc functions like setenv()
2233 * take locks.
2234 *
2235 * Hence, only call sys_clone() if we need to, in order to get at pid
2236 * namespacing. If we follow this path, the child's address space might
2237 * have broken locks; you may only call functions that do not acquire
2238 * any locks.
2239 *
2240 * Unfortunately, fork() acquires every lock it can get its hands on, as
2241 * previously detailed, so this function is highly likely to deadlock
2242 * later on (see "deadlock here") if we're multithreaded.
2243 *
2244 * We might hack around this by having the clone()d child (init of the
2245 * pid namespace) return directly, rather than leaving the clone()d
2246 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002247 * its fork()ed child return in turn), but that process would be
2248 * crippled with its libc locks potentially broken. We might try
2249 * fork()ing in the parent before we clone() to ensure that we own all
2250 * the locks, but then we have to have the forked child hanging around
2251 * consuming resources (and possibly having file descriptors / shared
2252 * memory regions / etc attached). We'd need to keep the child around to
2253 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002254 *
2255 * TODO(ellyjones): figure out if the "forked child hanging around"
2256 * problem is fixable or not. It would be nice if we worked in this
2257 * case.
2258 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002259 if (pid_namespace) {
2260 int clone_flags = CLONE_NEWPID | SIGCHLD;
2261 if (j->flags.userns)
2262 clone_flags |= CLONE_NEWUSER;
2263 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002264 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002265 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002266 }
Elly Jones761b7412012-06-13 15:49:52 -04002267
Elly Jonese1749eb2011-10-07 13:54:59 -04002268 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002269 if (use_preload) {
2270 free(oldenv_copy);
2271 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002272 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002273 }
Will Drewryf89aef52011-09-16 16:48:57 -05002274
Elly Jonese1749eb2011-10-07 13:54:59 -04002275 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002276 if (use_preload) {
2277 /* Restore parent's LD_PRELOAD. */
2278 if (oldenv_copy) {
2279 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2280 free(oldenv_copy);
2281 } else {
2282 unsetenv(kLdPreloadEnvVar);
2283 }
2284 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002285 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002286
Elly Jonese1749eb2011-10-07 13:54:59 -04002287 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002288
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002289 if (j->flags.forward_signals) {
2290 forward_pid = child_pid;
2291 install_signal_handlers();
2292 }
2293
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002294 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002295 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002296
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002297 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002298 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002299
Dylan Reid0f72ef42017-06-06 15:42:49 -07002300 if (j->rlimit_count)
2301 set_rlimits_or_die(j);
2302
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002303 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002304 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002305
2306 if (sync_child)
2307 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002308
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002309 if (use_preload) {
2310 /* Send marshalled minijail. */
2311 close(pipe_fds[0]); /* read endpoint */
2312 ret = minijail_to_fd(j, pipe_fds[1]);
2313 close(pipe_fds[1]); /* write endpoint */
2314 if (ret) {
2315 kill(j->initpid, SIGKILL);
2316 die("failed to send marshalled minijail");
2317 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002318 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002319
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002320 if (pchild_pid)
2321 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002322
2323 /*
2324 * If we want to write to the child process' standard input,
2325 * set up the write end of the pipe.
2326 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002327 if (pstdin_fd)
2328 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002329 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002330
2331 /*
2332 * If we want to read from the child process' standard output,
2333 * set up the read end of the pipe.
2334 */
2335 if (pstdout_fd)
2336 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002337 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002338
2339 /*
2340 * If we want to read from the child process' standard error,
2341 * set up the read end of the pipe.
2342 */
2343 if (pstderr_fd)
2344 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002345 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002346
Elly Jonese1749eb2011-10-07 13:54:59 -04002347 return 0;
2348 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002349 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002350 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002351
Peter Qiu2860c462015-12-16 15:13:06 -08002352 if (j->flags.reset_signal_mask) {
2353 sigset_t signal_mask;
2354 if (sigemptyset(&signal_mask) != 0)
2355 pdie("sigemptyset failed");
2356 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2357 pdie("sigprocmask failed");
2358 }
2359
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002360 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002361 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002362 int inheritable_fds[kMaxInheritableFdsSize];
2363 size_t size = 0;
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002364 size_t i;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002365 if (use_preload) {
2366 inheritable_fds[size++] = pipe_fds[0];
2367 inheritable_fds[size++] = pipe_fds[1];
2368 }
2369 if (sync_child) {
2370 inheritable_fds[size++] = child_sync_pipe_fds[0];
2371 inheritable_fds[size++] = child_sync_pipe_fds[1];
2372 }
2373 if (pstdin_fd) {
2374 inheritable_fds[size++] = stdin_fds[0];
2375 inheritable_fds[size++] = stdin_fds[1];
2376 }
2377 if (pstdout_fd) {
2378 inheritable_fds[size++] = stdout_fds[0];
2379 inheritable_fds[size++] = stdout_fds[1];
2380 }
2381 if (pstderr_fd) {
2382 inheritable_fds[size++] = stderr_fds[0];
2383 inheritable_fds[size++] = stderr_fds[1];
2384 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002385 for (i = 0; i < j->preserved_fd_count; i++) {
2386 /*
2387 * Preserve all parent_fds. They will be dup2(2)-ed in
2388 * the child later.
2389 */
2390 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
2391 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002392
2393 if (close_open_fds(inheritable_fds, size) < 0)
2394 die("failed to close open file descriptors");
2395 }
2396
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002397 if (redirect_fds(j))
2398 die("failed to set up fd redirections");
2399
Dylan Reidce5b55e2016-01-13 11:04:16 -08002400 if (sync_child)
2401 wait_for_parent_setup(child_sync_pipe_fds);
2402
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002403 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002404 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002405
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002406 /*
2407 * If we want to write to the jailed process' standard input,
2408 * set up the read end of the pipe.
2409 */
2410 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002411 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2412 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002413 die("failed to set up stdin pipe");
2414 }
2415
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002416 /*
2417 * If we want to read from the jailed process' standard output,
2418 * set up the write end of the pipe.
2419 */
2420 if (pstdout_fd) {
2421 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2422 STDOUT_FILENO) < 0)
2423 die("failed to set up stdout pipe");
2424 }
2425
2426 /*
2427 * If we want to read from the jailed process' standard error,
2428 * set up the write end of the pipe.
2429 */
2430 if (pstderr_fd) {
2431 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2432 STDERR_FILENO) < 0)
2433 die("failed to set up stderr pipe");
2434 }
2435
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002436 /*
2437 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2438 * This prevents the jailed process from using the TIOCSTI ioctl
2439 * to push characters into the parent process terminal's input buffer,
2440 * therefore escaping the jail.
2441 */
2442 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2443 isatty(STDERR_FILENO)) {
2444 if (setsid() < 0) {
2445 pdie("setsid() failed");
2446 }
2447 }
2448
Dylan Reid791f5772015-09-14 20:02:42 -07002449 /* If running an init program, let it decide when/how to mount /proc. */
2450 if (pid_namespace && !do_init)
2451 j->flags.remount_proc_ro = 0;
2452
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002453 if (use_preload) {
2454 /* Strip out flags that cannot be inherited across execve(2). */
2455 minijail_preexec(j);
2456 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002457 /*
2458 * If not using LD_PRELOAD, do all jailing before execve(2).
2459 * Note that PID namespaces can only be entered on fork(2),
2460 * so that flag is still cleared.
2461 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002462 j->flags.pids = 0;
2463 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002464 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002465 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002466
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002467 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002468 /*
2469 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002470 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002471 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002472 * a child to actually run the program. If |do_init == 0|, we
2473 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002474 *
2475 * If we're multithreaded, we'll probably deadlock here. See
2476 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002477 */
2478 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002479 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002480 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002481 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002482 /*
2483 * Best effort. Don't bother checking the return value.
2484 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002485 prctl(PR_SET_NAME, "minijail-init");
2486 init(child_pid); /* Never returns. */
2487 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002488 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002489
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002490 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
2491
Elly Jonesdd3e8512012-01-23 15:13:38 -05002492 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002493 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002494 * calling process
2495 * -> execve()-ing process
2496 * If we are:
2497 * calling process
2498 * -> init()-ing process
2499 * -> execve()-ing process
2500 */
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002501 ret = execve(filename, argv, environ);
2502 if (ret == -1) {
2503 pwarn("execve(%s) failed", filename);
2504 }
2505 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002506}
2507
Will Drewry6ac91122011-10-21 16:38:58 -05002508int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002509{
2510 int st;
2511 if (kill(j->initpid, SIGTERM))
2512 return -errno;
2513 if (waitpid(j->initpid, &st, 0) < 0)
2514 return -errno;
2515 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002516}
2517
Will Drewry6ac91122011-10-21 16:38:58 -05002518int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002519{
2520 int st;
2521 if (waitpid(j->initpid, &st, 0) < 0)
2522 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002523
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002524 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002525 int error_status = st;
2526 if (WIFSIGNALED(st)) {
2527 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002528 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002529 j->initpid, signum);
2530 /*
2531 * We return MINIJAIL_ERR_JAIL if the process received
2532 * SIGSYS, which happens when a syscall is blocked by
2533 * seccomp filters.
2534 * If not, we do what bash(1) does:
2535 * $? = 128 + signum
2536 */
2537 if (signum == SIGSYS) {
2538 error_status = MINIJAIL_ERR_JAIL;
2539 } else {
2540 error_status = 128 + signum;
2541 }
2542 }
2543 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002544 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002545
2546 int exit_status = WEXITSTATUS(st);
2547 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002548 info("child process %d exited with status %d",
2549 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002550
2551 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002552}
2553
Will Drewry6ac91122011-10-21 16:38:58 -05002554void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002555{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002556 size_t i;
2557
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002558 if (j->flags.seccomp_filter && j->filter_prog) {
2559 free(j->filter_prog->filter);
2560 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002561 }
Dylan Reid648b2202015-10-23 00:50:00 -07002562 while (j->mounts_head) {
2563 struct mountpoint *m = j->mounts_head;
2564 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002565 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002566 free(m->type);
2567 free(m->dest);
2568 free(m->src);
2569 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002570 }
Dylan Reid648b2202015-10-23 00:50:00 -07002571 j->mounts_tail = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002572 while (j->hooks_head) {
2573 struct hook *c = j->hooks_head;
2574 j->hooks_head = c->next;
2575 free(c);
2576 }
2577 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002578 if (j->user)
2579 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002580 if (j->suppl_gid_list)
2581 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002582 if (j->chrootdir)
2583 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002584 if (j->pid_file_path)
2585 free(j->pid_file_path);
2586 if (j->uidmap)
2587 free(j->uidmap);
2588 if (j->gidmap)
2589 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002590 if (j->hostname)
2591 free(j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08002592 if (j->alt_syscall_table)
2593 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002594 for (i = 0; i < j->cgroup_count; ++i)
2595 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002596 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002597}