blob: a9521b5c0adff87b8749abe664f05d6e98f99367 [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
Mike Frysingereaab4202017-08-14 14:57:21 -04001226 if (setup_mount_destination(m->src, dest, j->uid, j->gid,
1227 (m->flags & MS_BIND)))
Dylan Reideec77962016-06-30 19:35:10 -07001228 pdie("creating mount target '%s' failed", dest);
1229
Dylan Reid648b2202015-10-23 00:50:00 -07001230 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001231 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1232 * can't both be specified in the original bind mount.
1233 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001234 */
1235 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1236 remount_ro = 1;
1237 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001238 }
Dylan Reid648b2202015-10-23 00:50:00 -07001239
Dylan Reid81e23972016-05-18 14:06:35 -07001240 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001241 if (ret)
1242 pdie("mount: %s -> %s", m->src, dest);
1243
1244 if (remount_ro) {
1245 m->flags |= MS_RDONLY;
1246 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001247 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001248 if (ret)
1249 pdie("bind ro: %s -> %s", m->src, dest);
1250 }
1251
Elly Jones51a5b6c2011-10-12 19:09:26 -04001252 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001253 if (m->next)
1254 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001255 return ret;
1256}
1257
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001258static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001259{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001260 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001261
1262 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001263 return ret;
1264
1265 if (chroot(j->chrootdir))
1266 return -errno;
1267
1268 if (chdir("/"))
1269 return -errno;
1270
1271 return 0;
1272}
1273
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001274static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001275{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001276 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001277
1278 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001279 return ret;
1280
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001281 /*
1282 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001283 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001284 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001285 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001286 if (oldroot < 0)
1287 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001288 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001289 if (newroot < 0)
1290 pdie("failed to open %s for fchdir", j->chrootdir);
1291
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001292 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001293 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001294 * do a self bind mount.
1295 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001296 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1297 pdie("failed to bind mount '%s'", j->chrootdir);
1298 if (chdir(j->chrootdir))
1299 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001300 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001301 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001302
1303 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001304 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001305 * change to the old root and unmount it.
1306 */
1307 if (fchdir(oldroot))
1308 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001309
1310 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001311 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1312 * there could be a shared mount point under |oldroot|. In that case,
1313 * mounts under this shared mount point will be unmounted below, and
1314 * this unmounting will propagate to the original mount namespace
1315 * (because the mount point is shared). To prevent this unexpected
1316 * unmounting, remove these mounts from their peer groups by recursively
1317 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001318 */
1319 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001320 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001321 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001322 if (umount2(".", MNT_DETACH))
1323 pdie("umount(/)");
1324 /* Change back to the new root. */
1325 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001326 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001327 if (close(oldroot))
1328 return -errno;
1329 if (close(newroot))
1330 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001331 if (chroot("/"))
1332 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001333 /* Set correct CWD for getcwd(3). */
1334 if (chdir("/"))
1335 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001336
1337 return 0;
1338}
1339
Martin Pelikánab9eb442017-01-25 11:53:58 +11001340static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001341{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001342 const char fmt[] = "size=%zu,mode=1777";
1343 /* Count for the user storing ULLONG_MAX literally + extra space. */
1344 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1345 int ret;
1346
1347 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1348
1349 if (ret <= 0)
1350 pdie("tmpfs size spec error");
1351 else if ((size_t)ret >= sizeof(data))
1352 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001353 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001354 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001355}
1356
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001357static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001358{
1359 const char *kProcPath = "/proc";
1360 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001361 /*
1362 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001363 * /proc in our namespace, which means using MS_REMOUNT here would
1364 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001365 * namespace (!). Instead, remove their mount from our namespace lazily
1366 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001367 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001368 if (umount2(kProcPath, MNT_DETACH)) {
1369 /*
1370 * If we are in a new user namespace, umount(2) will fail.
1371 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1372 */
1373 if (j->flags.userns) {
1374 info("umount(/proc, MNT_DETACH) failed, "
1375 "this is expected when using user namespaces");
1376 } else {
1377 return -errno;
1378 }
1379 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001380 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001381 return -errno;
1382 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001383}
1384
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001385static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001386{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001387 kill(j->initpid, SIGKILL);
1388 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001389}
1390
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001391static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001392{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001393 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001394 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001395}
1396
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001397static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001398{
1399 size_t i;
1400
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001401 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001402 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001403 kill_child_and_die(j, "failed to add to cgroups");
1404 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001405}
1406
Dylan Reid0f72ef42017-06-06 15:42:49 -07001407static void set_rlimits_or_die(const struct minijail *j)
1408{
1409 size_t i;
1410
1411 for (i = 0; i < j->rlimit_count; ++i) {
1412 struct rlimit limit;
1413 limit.rlim_cur = j->rlimits[i].cur;
1414 limit.rlim_max = j->rlimits[i].max;
1415 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1416 kill_child_and_die(j, "failed to set rlimit");
1417 }
1418}
1419
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001420static void write_ugid_maps_or_die(const struct minijail *j)
1421{
1422 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1423 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001424 if (j->gidmap && j->flags.disable_setgroups) {
1425 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1426 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001427 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001428 if (ret == -ENOENT) {
1429 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1430 warn("could not disable setgroups(2)");
1431 } else
1432 kill_child_and_die(j, "failed to disable setgroups(2)");
1433 }
1434 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001435 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1436 kill_child_and_die(j, "failed to write gid_map");
1437}
1438
1439static void enter_user_namespace(const struct minijail *j)
1440{
1441 if (j->uidmap && setresuid(0, 0, 0))
1442 pdie("user_namespaces: setresuid(0, 0, 0) failed");
1443 if (j->gidmap && setresgid(0, 0, 0))
1444 pdie("user_namespaces: setresgid(0, 0, 0) failed");
1445}
1446
1447static void parent_setup_complete(int *pipe_fds)
1448{
1449 close(pipe_fds[0]);
1450 close(pipe_fds[1]);
1451}
1452
1453/*
1454 * wait_for_parent_setup: Called by the child process to wait for any
1455 * further parent-side setup to complete before continuing.
1456 */
1457static void wait_for_parent_setup(int *pipe_fds)
1458{
1459 char buf;
1460
1461 close(pipe_fds[1]);
1462
1463 /* Wait for parent to complete setup and close the pipe. */
1464 if (read(pipe_fds[0], &buf, 1) != 0)
1465 die("failed to sync with parent");
1466 close(pipe_fds[0]);
1467}
1468
1469static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001470{
Lutz Justen13807cb2017-01-03 17:11:55 +01001471 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1472 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001473 die("can only do one of inherit, keep, or set supplementary "
1474 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001475 }
1476
Lutz Justen13807cb2017-01-03 17:11:55 +01001477 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001478 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001479 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001480 } else if (j->flags.set_suppl_gids) {
1481 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001482 pdie("setgroups(suppl_gids) failed");
Lutz Justen13807cb2017-01-03 17:11:55 +01001483 } else if (!j->flags.keep_suppl_gids) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001484 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001485 * Only attempt to clear supplementary groups if we are changing
Lutz Justen13807cb2017-01-03 17:11:55 +01001486 * users or groups.
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001487 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001488 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001489 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001490 }
1491
1492 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001493 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001494
1495 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001496 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001497}
1498
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001499static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1500{
1501 const uint64_t one = 1;
1502 unsigned int i;
1503 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1504 if (keep_mask & (one << i))
1505 continue;
1506 if (prctl(PR_CAPBSET_DROP, i))
1507 pdie("could not drop capability from bounding set");
1508 }
1509}
1510
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001511static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001512{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001513 if (!j->flags.use_caps)
1514 return;
1515
Elly Jonese1749eb2011-10-07 13:54:59 -04001516 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001517 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001518 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001519 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001520 unsigned int i;
1521 if (!caps)
1522 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001523 if (cap_clear(caps))
1524 die("can't clear caps");
1525
1526 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001527 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001528 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001529 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001530 flag[0] = i;
1531 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001532 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001533 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001534 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001535 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001536 die("can't add inheritable cap");
1537 }
1538 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001539 die("can't apply initial cleaned capset");
1540
1541 /*
1542 * Instead of dropping bounding set first, do it here in case
1543 * the caller had a more permissive bounding set which could
1544 * have been used above to raise a capability that wasn't already
1545 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1546 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001547 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001548
1549 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001550 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001551 flag[0] = CAP_SETPCAP;
1552 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1553 die("can't clear effective cap");
1554 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1555 die("can't clear permitted cap");
1556 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1557 die("can't clear inheritable cap");
1558 }
1559
1560 if (cap_set_proc(caps))
1561 die("can't apply final cleaned capset");
1562
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001563 /*
1564 * If ambient capabilities are supported, clear all capabilities first,
1565 * then raise the requested ones.
1566 */
1567 if (j->flags.set_ambient_caps) {
1568 if (!cap_ambient_supported()) {
1569 pdie("ambient capabilities not supported");
1570 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001571 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1572 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001573 pdie("can't clear ambient capabilities");
1574 }
1575
1576 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1577 if (!(j->caps & (one << i)))
1578 continue;
1579
1580 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1581 0) != 0) {
1582 pdie("prctl(PR_CAP_AMBIENT, "
1583 "PR_CAP_AMBIENT_RAISE, %u) failed",
1584 i);
1585 }
1586 }
1587 }
1588
Kees Cook323878a2013-02-05 15:35:24 -08001589 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001590}
1591
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001592static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001593{
1594 /*
1595 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1596 * in the kernel source tree for an explanation of the parameters.
1597 */
1598 if (j->flags.no_new_privs) {
1599 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1600 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1601 }
1602
1603 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001604 * Code running with ASan
1605 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1606 * will make system calls not included in the syscall filter policy,
1607 * which will likely crash the program. Skip setting seccomp filter in
1608 * that case.
1609 * 'running_with_asan()' has no inputs and is completely defined at
1610 * build time, so this cannot be used by an attacker to skip setting
1611 * seccomp filter.
1612 */
1613 if (j->flags.seccomp_filter && running_with_asan()) {
1614 warn("running with ASan, not setting seccomp filter");
1615 return;
1616 }
1617
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001618 if (j->flags.seccomp_filter) {
1619 if (j->flags.seccomp_filter_logging) {
1620 /*
1621 * If logging seccomp filter failures,
1622 * install the SIGSYS handler first.
1623 */
1624 if (install_sigsys_handler())
1625 pdie("failed to install SIGSYS handler");
1626 warn("logging seccomp filter failures");
1627 } else if (j->flags.seccomp_filter_tsync) {
1628 /*
1629 * If setting thread sync,
1630 * reset the SIGSYS signal handler so that
1631 * the entire thread group is killed.
1632 */
1633 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1634 pdie("failed to reset SIGSYS disposition");
1635 info("reset SIGSYS disposition");
1636 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001637 }
1638
1639 /*
1640 * Install the syscall filter.
1641 */
1642 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001643 if (j->flags.seccomp_filter_tsync) {
1644 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1645 SECCOMP_FILTER_FLAG_TSYNC,
1646 j->filter_prog)) {
1647 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001648 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001649 } else {
1650 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1651 j->filter_prog)) {
1652 pdie("prctl(seccomp_filter) failed");
1653 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001654 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001655 }
1656}
1657
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001658static pid_t forward_pid = -1;
1659
1660static void forward_signal(__attribute__((unused)) int nr,
1661 __attribute__((unused)) siginfo_t *siginfo,
1662 __attribute__((unused)) void *void_context)
1663{
1664 if (forward_pid != -1) {
1665 kill(forward_pid, nr);
1666 }
1667}
1668
1669static void install_signal_handlers(void)
1670{
1671 struct sigaction act;
1672
1673 memset(&act, 0, sizeof(act));
1674 act.sa_sigaction = &forward_signal;
1675 act.sa_flags = SA_SIGINFO | SA_RESTART;
1676
1677 /* Handle all signals, except SIGCHLD. */
1678 for (int nr = 1; nr < NSIG; nr++) {
1679 /*
1680 * We don't care if we get EINVAL: that just means that we
1681 * can't handle this signal, so let's skip it and continue.
1682 */
1683 sigaction(nr, &act, NULL);
1684 }
1685 /* Reset SIGCHLD's handler. */
1686 signal(SIGCHLD, SIG_DFL);
1687
1688 /* Handle real-time signals. */
1689 for (int nr = SIGRTMIN; nr <= SIGRTMAX; nr++) {
1690 sigaction(nr, &act, NULL);
1691 }
1692}
1693
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001694static const char *lookup_hook_name(minijail_hook_event_t event)
1695{
1696 switch (event) {
1697 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
1698 return "pre-drop-caps";
1699 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
1700 return "pre-execve";
1701 case MINIJAIL_HOOK_EVENT_MAX:
1702 /*
1703 * Adding this in favor of a default case to force the
1704 * compiler to error out if a new enum value is added.
1705 */
1706 break;
1707 }
1708 return "unknown";
1709}
1710
1711static void run_hooks_or_die(const struct minijail *j,
1712 minijail_hook_event_t event)
1713{
1714 int rc;
1715 int hook_index = 0;
1716 for (struct hook *c = j->hooks_head; c; c = c->next) {
1717 if (c->event != event)
1718 continue;
1719 rc = c->hook(c->payload);
1720 if (rc != 0) {
1721 errno = -rc;
1722 pdie("%s hook (index %d) failed",
1723 lookup_hook_name(event), hook_index);
1724 }
1725 /* Only increase the index within the same hook event type. */
1726 ++hook_index;
1727 }
1728}
1729
Will Drewry6ac91122011-10-21 16:38:58 -05001730void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001731{
Dylan Reidf682d472015-09-17 21:39:07 -07001732 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001733 * If we're dropping caps, get the last valid cap from /proc now,
1734 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001735 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001736 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001737 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001738 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001739
Elly Jonese1749eb2011-10-07 13:54:59 -04001740 if (j->flags.pids)
1741 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001742 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001743
Lutz Justen13807cb2017-01-03 17:11:55 +01001744 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001745 die("cannot inherit supplementary groups without setting a "
1746 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001747
Elly Jonesdd3e8512012-01-23 15:13:38 -05001748 /*
1749 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001750 * so we don't even try. If any of our operations fail, we abort() the
1751 * entire process.
1752 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001753 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001754 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001755
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001756 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001757 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001758 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001759 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001760 * Unless asked not to, remount all filesystems as private.
1761 * If they are shared, new bind mounts will creep out of our
1762 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001763 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1764 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001765 if (!j->flags.skip_remount_private) {
1766 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001767 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1768 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001769 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001770 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001771
Dylan Reidf7942472015-11-18 17:55:26 -08001772 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001773 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001774 }
1775
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001776 if (j->flags.uts) {
1777 if (unshare(CLONE_NEWUTS))
1778 pdie("unshare(CLONE_NEWUTS) failed");
1779
1780 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
1781 pdie("sethostname(%s) failed", j->hostname);
1782 }
1783
Dylan Reid1102f5a2015-09-15 11:52:20 -07001784 if (j->flags.enter_net) {
1785 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001786 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001787 } else if (j->flags.net) {
1788 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001789 pdie("unshare(CLONE_NEWNET) failed");
1790 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001791 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001792
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001793 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001794 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001795
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08001796 if (j->flags.new_session_keyring) {
1797 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
1798 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
1799 }
1800
Elly Jones51a5b6c2011-10-12 19:09:26 -04001801 if (j->flags.chroot && enter_chroot(j))
1802 pdie("chroot");
1803
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001804 if (j->flags.pivot_root && enter_pivot_root(j))
1805 pdie("pivot_root");
1806
Martin Pelikánab9eb442017-01-25 11:53:58 +11001807 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07001808 pdie("mount_tmp");
1809
Dylan Reid791f5772015-09-14 20:02:42 -07001810 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001811 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001812
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001813 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
1814
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001815 /*
1816 * If we're only dropping capabilities from the bounding set, but not
1817 * from the thread's (permitted|inheritable|effective) sets, do it now.
1818 */
1819 if (j->flags.capbset_drop) {
1820 drop_capbset(j->cap_bset, last_valid_cap);
1821 }
1822
1823 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001824 /*
1825 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001826 * capability to change uids, our attempt to use setuid()
1827 * below will fail. Hang on to root caps across setuid(), then
1828 * lock securebits.
1829 */
1830 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001831 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001832
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -07001833 if (lock_securebits(j->securebits_skip_mask) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001834 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001835 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001836 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001837
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001838 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001839 /*
1840 * If we're setting no_new_privs, we can drop privileges
1841 * before setting seccomp filter. This way filter policies
1842 * don't need to allow privilege-dropping syscalls.
1843 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001844 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001845 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001846 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001847 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001848 /*
1849 * If we're not setting no_new_privs,
1850 * we need to set seccomp filter *before* dropping privileges.
1851 * WARNING: this means that filter policies *must* allow
1852 * setgroups()/setresgid()/setresuid() for dropping root and
1853 * capget()/capset()/prctl() for dropping caps.
1854 */
1855 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001856 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001857 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001858 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001859
Elly Jonesdd3e8512012-01-23 15:13:38 -05001860 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001861 * Select the specified alternate syscall table. The table must not
1862 * block prctl(2) if we're using seccomp as well.
1863 */
1864 if (j->flags.alt_syscall) {
1865 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001866 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08001867 }
1868
1869 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001870 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001871 * privilege-dropping syscalls :)
1872 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001873 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001874 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001875 warn("seccomp not supported");
1876 return;
1877 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001878 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001879 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001880}
1881
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001882/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001883static int init_exitstatus = 0;
1884
Will Drewry6ac91122011-10-21 16:38:58 -05001885void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001886{
1887 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001888}
1889
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001890void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001891{
1892 pid_t pid;
1893 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001894 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001895 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001896 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001897 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001898 /*
1899 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001900 * left inside our pid namespace or we get a signal.
1901 */
1902 if (pid == rootpid)
1903 init_exitstatus = status;
1904 }
1905 if (!WIFEXITED(init_exitstatus))
1906 _exit(MINIJAIL_ERR_INIT);
1907 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001908}
1909
Will Drewry6ac91122011-10-21 16:38:58 -05001910int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001911{
1912 size_t sz = 0;
1913 size_t bytes = read(fd, &sz, sizeof(sz));
1914 char *buf;
1915 int r;
1916 if (sizeof(sz) != bytes)
1917 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001918 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001919 return -E2BIG;
1920 buf = malloc(sz);
1921 if (!buf)
1922 return -ENOMEM;
1923 bytes = read(fd, buf, sz);
1924 if (bytes != sz) {
1925 free(buf);
1926 return -EINVAL;
1927 }
1928 r = minijail_unmarshal(j, buf, sz);
1929 free(buf);
1930 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001931}
1932
Will Drewry6ac91122011-10-21 16:38:58 -05001933int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001934{
1935 char *buf;
1936 size_t sz = minijail_size(j);
1937 ssize_t written;
1938 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001939
Elly Jonese1749eb2011-10-07 13:54:59 -04001940 if (!sz)
1941 return -EINVAL;
1942 buf = malloc(sz);
1943 r = minijail_marshal(j, buf, sz);
1944 if (r) {
1945 free(buf);
1946 return r;
1947 }
1948 /* Sends [size][minijail]. */
1949 written = write(fd, &sz, sizeof(sz));
1950 if (written != sizeof(sz)) {
1951 free(buf);
1952 return -EFAULT;
1953 }
1954 written = write(fd, buf, sz);
1955 if (written < 0 || (size_t) written != sz) {
1956 free(buf);
1957 return -EFAULT;
1958 }
1959 free(buf);
1960 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001961}
Elly Jonescd7a9042011-07-22 13:56:51 -04001962
Will Drewry6ac91122011-10-21 16:38:58 -05001963int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001964{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001965#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001966 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001967 return 0;
1968#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001969 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1970 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1971 if (!newenv)
1972 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001973
Elly Jonese1749eb2011-10-07 13:54:59 -04001974 /* Only insert a separating space if we have something to separate... */
1975 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1976 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001977
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001978 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001979 setenv(kLdPreloadEnvVar, newenv, 1);
1980 free(newenv);
1981 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001982#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001983}
1984
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001985static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001986{
1987 int r = pipe(fds);
1988 char fd_buf[11];
1989 if (r)
1990 return r;
1991 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1992 if (r <= 0)
1993 return -EINVAL;
1994 setenv(kFdEnvVar, fd_buf, 1);
1995 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001996}
1997
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001998static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07001999{
2000 const char *kFdPath = "/proc/self/fd";
2001
2002 DIR *d = opendir(kFdPath);
2003 struct dirent *dir_entry;
2004
2005 if (d == NULL)
2006 return -1;
2007 int dir_fd = dirfd(d);
2008 while ((dir_entry = readdir(d)) != NULL) {
2009 size_t i;
2010 char *end;
2011 bool should_close = true;
2012 const int fd = strtol(dir_entry->d_name, &end, 10);
2013
2014 if ((*end) != '\0') {
2015 continue;
2016 }
2017 /*
2018 * We might have set up some pipes that we want to share with
2019 * the parent process, and should not be closed.
2020 */
2021 for (i = 0; i < size; ++i) {
2022 if (fd == inheritable_fds[i]) {
2023 should_close = false;
2024 break;
2025 }
2026 }
2027 /* Also avoid closing the directory fd. */
2028 if (should_close && fd != dir_fd)
2029 close(fd);
2030 }
2031 closedir(d);
2032 return 0;
2033}
2034
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002035static int redirect_fds(struct minijail *j)
2036{
2037 size_t i, i2;
2038 int closeable;
2039 for (i = 0; i < j->preserved_fd_count; i++) {
2040 if (dup2(j->preserved_fds[i].parent_fd,
2041 j->preserved_fds[i].child_fd) == -1) {
2042 return -1;
2043 }
2044 }
2045 /*
2046 * After all fds have been duped, we are now free to close all parent
2047 * fds that are *not* child fds.
2048 */
2049 for (i = 0; i < j->preserved_fd_count; i++) {
2050 closeable = true;
2051 for (i2 = 0; i2 < j->preserved_fd_count; i2++) {
2052 closeable &= j->preserved_fds[i].parent_fd !=
2053 j->preserved_fds[i2].child_fd;
2054 }
2055 if (closeable)
2056 close(j->preserved_fds[i].parent_fd);
2057 }
2058 return 0;
2059}
2060
Dylan Reidacfb8be2017-08-25 12:56:51 -07002061/*
2062 * Structure that specifies how to start a minijail.
2063 *
2064 * filename - The program to exec in the child.
2065 * argv - Arguments for the child program.
2066 * use_preload - If true use LD_PRELOAD.
2067 */
2068struct minijail_run_config {
2069 const char *filename;
2070 char *const *argv;
2071 int use_preload;
2072};
2073
2074/*
2075 * Set of pointers to fill with values from minijail_run.
2076 * All arguments are allowed to be NULL if unused.
2077 *
2078 * pstdin_fd - Filled with stdin pipe if non-NULL.
2079 * pstdout_fd - Filled with stdout pipe if non-NULL.
2080 * pstderr_fd - Filled with stderr pipe if non-NULL.
2081 * pchild_pid - Filled with the pid of the child process if non-NULL.
2082 */
2083struct minijail_run_status {
2084 int *pstdin_fd;
2085 int *pstdout_fd;
2086 int *pstderr_fd;
2087 pid_t *pchild_pid;
2088};
2089
2090int minijail_run_internal(struct minijail *j,
2091 const struct minijail_run_config *config,
2092 struct minijail_run_status *status_out);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002093
Will Drewry6ac91122011-10-21 16:38:58 -05002094int API minijail_run(struct minijail *j, const char *filename,
2095 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002096{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002097 struct minijail_run_config config = {
2098 .filename = filename,
2099 .argv = argv,
2100 .use_preload = true,
2101 };
2102 struct minijail_run_status status = {};
2103 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002104}
2105
2106int API minijail_run_pid(struct minijail *j, const char *filename,
2107 char *const argv[], pid_t *pchild_pid)
2108{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002109 struct minijail_run_config config = {
2110 .filename = filename,
2111 .argv = argv,
2112 .use_preload = true,
2113 };
2114 struct minijail_run_status status = {
2115 .pchild_pid = pchild_pid,
2116 };
2117 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002118}
2119
2120int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002121 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002122{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002123 struct minijail_run_config config = {
2124 .filename = filename,
2125 .argv = argv,
2126 .use_preload = true,
2127 };
2128 struct minijail_run_status status = {
2129 .pstdin_fd = pstdin_fd,
2130 };
2131 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002132}
2133
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002134int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002135 char *const argv[], pid_t *pchild_pid,
2136 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002137{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002138 struct minijail_run_config config = {
2139 .filename = filename,
2140 .argv = argv,
2141 .use_preload = true,
2142 };
2143 struct minijail_run_status status = {
2144 .pstdin_fd = pstdin_fd,
2145 .pstdout_fd = pstdout_fd,
2146 .pstderr_fd = pstderr_fd,
2147 .pchild_pid = pchild_pid,
2148 };
2149 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002150}
2151
2152int API minijail_run_no_preload(struct minijail *j, const char *filename,
2153 char *const argv[])
2154{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002155 struct minijail_run_config config = {
2156 .filename = filename,
2157 .argv = argv,
2158 .use_preload = false,
2159 };
2160 struct minijail_run_status status = {};
2161 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002162}
2163
Samuel Tan63187f42015-10-16 13:01:53 -07002164int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002165 const char *filename,
2166 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002167 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002168 int *pstdin_fd,
2169 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002170 int *pstderr_fd)
2171{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002172 struct minijail_run_config config = {
2173 .filename = filename,
2174 .argv = argv,
2175 .use_preload = false,
2176 };
2177 struct minijail_run_status status = {
2178 .pstdin_fd = pstdin_fd,
2179 .pstdout_fd = pstdout_fd,
2180 .pstderr_fd = pstderr_fd,
2181 .pchild_pid = pchild_pid,
2182 };
2183 return minijail_run_internal(j, &config, &status);
Samuel Tan63187f42015-10-16 13:01:53 -07002184}
2185
Dylan Reidacfb8be2017-08-25 12:56:51 -07002186int minijail_run_internal(struct minijail *j,
2187 const struct minijail_run_config *config,
2188 struct minijail_run_status *status_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002189{
Elly Jonese1749eb2011-10-07 13:54:59 -04002190 char *oldenv, *oldenv_copy = NULL;
2191 pid_t child_pid;
2192 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002193 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002194 int stdout_fds[2];
2195 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08002196 int child_sync_pipe_fds[2];
2197 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002198 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002199 /* We need to remember this across the minijail_preexec() call. */
2200 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002201 int do_init = j->flags.do_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002202 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002203
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002204 if (use_preload) {
2205 oldenv = getenv(kLdPreloadEnvVar);
2206 if (oldenv) {
2207 oldenv_copy = strdup(oldenv);
2208 if (!oldenv_copy)
2209 return -ENOMEM;
2210 }
2211
2212 if (setup_preload())
2213 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002214 }
Will Drewryf89aef52011-09-16 16:48:57 -05002215
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002216 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002217 if (j->flags.use_caps && j->caps != 0 &&
2218 !j->flags.set_ambient_caps) {
2219 die("non-empty, non-ambient capabilities are not "
2220 "supported without LD_PRELOAD");
2221 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002222 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002223
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002224 if (use_preload && j->hooks_head != NULL) {
2225 die("Minijail hooks are not supported with LD_PRELOAD");
2226 }
2227
Elly Jonesdd3e8512012-01-23 15:13:38 -05002228 /*
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002229 * Make the process group ID of this process equal to its PID.
2230 * In the non-interactive case (e.g. when the parent process is started
2231 * from init) this ensures the parent process and the jailed process
2232 * can be killed together.
2233 * When the parent process is started from the console this ensures
2234 * the call to setsid(2) in the jailed process succeeds.
2235 *
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002236 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
2237 * the process is already a process group leader.
2238 */
2239 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
2240 if (errno != EPERM) {
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002241 pdie("setpgid(0, 0) failed");
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002242 }
2243 }
2244
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002245 if (use_preload) {
2246 /*
2247 * Before we fork(2) and execve(2) the child process, we need
2248 * to open a pipe(2) to send the minijail configuration over.
2249 */
2250 if (setup_pipe(pipe_fds))
2251 return -EFAULT;
2252 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002253
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002254 /*
2255 * If we want to write to the child process' standard input,
2256 * create the pipe(2) now.
2257 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002258 if (status_out->pstdin_fd) {
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002259 if (pipe(stdin_fds))
2260 return -EFAULT;
2261 }
2262
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002263 /*
2264 * If we want to read from the child process' standard output,
2265 * create the pipe(2) now.
2266 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002267 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002268 if (pipe(stdout_fds))
2269 return -EFAULT;
2270 }
2271
2272 /*
2273 * If we want to read from the child process' standard error,
2274 * create the pipe(2) now.
2275 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002276 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002277 if (pipe(stderr_fds))
2278 return -EFAULT;
2279 }
2280
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002281 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002282 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002283 * or if we need to add the child process to cgroups, create the pipe(2)
2284 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002285 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002286 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002287 sync_child = 1;
2288 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002289 return -EFAULT;
2290 }
2291
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002292 /*
2293 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002294 *
2295 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2296 *
2297 * In multithreaded programs, there are a bunch of locks inside libc,
2298 * some of which may be held by other threads at the time that we call
2299 * minijail_run_pid(). If we call fork(), glibc does its level best to
2300 * ensure that we hold all of these locks before it calls clone()
2301 * internally and drop them after clone() returns, but when we call
2302 * sys_clone(2) directly, all that gets bypassed and we end up with a
2303 * child address space where some of libc's important locks are held by
2304 * other threads (which did not get cloned, and hence will never release
2305 * those locks). This is okay so long as we call exec() immediately
2306 * after, but a bunch of seemingly-innocent libc functions like setenv()
2307 * take locks.
2308 *
2309 * Hence, only call sys_clone() if we need to, in order to get at pid
2310 * namespacing. If we follow this path, the child's address space might
2311 * have broken locks; you may only call functions that do not acquire
2312 * any locks.
2313 *
2314 * Unfortunately, fork() acquires every lock it can get its hands on, as
2315 * previously detailed, so this function is highly likely to deadlock
2316 * later on (see "deadlock here") if we're multithreaded.
2317 *
2318 * We might hack around this by having the clone()d child (init of the
2319 * pid namespace) return directly, rather than leaving the clone()d
2320 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002321 * its fork()ed child return in turn), but that process would be
2322 * crippled with its libc locks potentially broken. We might try
2323 * fork()ing in the parent before we clone() to ensure that we own all
2324 * the locks, but then we have to have the forked child hanging around
2325 * consuming resources (and possibly having file descriptors / shared
2326 * memory regions / etc attached). We'd need to keep the child around to
2327 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002328 *
2329 * TODO(ellyjones): figure out if the "forked child hanging around"
2330 * problem is fixable or not. It would be nice if we worked in this
2331 * case.
2332 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002333 if (pid_namespace) {
2334 int clone_flags = CLONE_NEWPID | SIGCHLD;
2335 if (j->flags.userns)
2336 clone_flags |= CLONE_NEWUSER;
2337 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002338 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002339 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002340 }
Elly Jones761b7412012-06-13 15:49:52 -04002341
Elly Jonese1749eb2011-10-07 13:54:59 -04002342 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002343 if (use_preload) {
2344 free(oldenv_copy);
2345 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002346 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002347 }
Will Drewryf89aef52011-09-16 16:48:57 -05002348
Elly Jonese1749eb2011-10-07 13:54:59 -04002349 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002350 if (use_preload) {
2351 /* Restore parent's LD_PRELOAD. */
2352 if (oldenv_copy) {
2353 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2354 free(oldenv_copy);
2355 } else {
2356 unsetenv(kLdPreloadEnvVar);
2357 }
2358 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002359 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002360
Elly Jonese1749eb2011-10-07 13:54:59 -04002361 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002362
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002363 if (j->flags.forward_signals) {
2364 forward_pid = child_pid;
2365 install_signal_handlers();
2366 }
2367
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002368 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002369 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002370
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002371 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002372 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002373
Dylan Reid0f72ef42017-06-06 15:42:49 -07002374 if (j->rlimit_count)
2375 set_rlimits_or_die(j);
2376
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002377 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002378 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002379
2380 if (sync_child)
2381 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002382
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002383 if (use_preload) {
2384 /* Send marshalled minijail. */
2385 close(pipe_fds[0]); /* read endpoint */
2386 ret = minijail_to_fd(j, pipe_fds[1]);
2387 close(pipe_fds[1]); /* write endpoint */
2388 if (ret) {
2389 kill(j->initpid, SIGKILL);
2390 die("failed to send marshalled minijail");
2391 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002392 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002393
Dylan Reidacfb8be2017-08-25 12:56:51 -07002394 if (status_out->pchild_pid)
2395 *status_out->pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002396
2397 /*
2398 * If we want to write to the child process' standard input,
2399 * set up the write end of the pipe.
2400 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002401 if (status_out->pstdin_fd)
2402 *status_out->pstdin_fd =
2403 setup_pipe_end(stdin_fds, 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002404
2405 /*
2406 * If we want to read from the child process' standard output,
2407 * set up the read end of the pipe.
2408 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002409 if (status_out->pstdout_fd)
2410 *status_out->pstdout_fd =
2411 setup_pipe_end(stdout_fds, 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002412
2413 /*
2414 * If we want to read from the child process' standard error,
2415 * set up the read end of the pipe.
2416 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002417 if (status_out->pstderr_fd)
2418 *status_out->pstderr_fd =
2419 setup_pipe_end(stderr_fds, 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002420
Elly Jonese1749eb2011-10-07 13:54:59 -04002421 return 0;
2422 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002423 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002424 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002425
Peter Qiu2860c462015-12-16 15:13:06 -08002426 if (j->flags.reset_signal_mask) {
2427 sigset_t signal_mask;
2428 if (sigemptyset(&signal_mask) != 0)
2429 pdie("sigemptyset failed");
2430 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2431 pdie("sigprocmask failed");
2432 }
2433
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002434 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002435 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002436 int inheritable_fds[kMaxInheritableFdsSize];
2437 size_t size = 0;
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002438 size_t i;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002439 if (use_preload) {
2440 inheritable_fds[size++] = pipe_fds[0];
2441 inheritable_fds[size++] = pipe_fds[1];
2442 }
2443 if (sync_child) {
2444 inheritable_fds[size++] = child_sync_pipe_fds[0];
2445 inheritable_fds[size++] = child_sync_pipe_fds[1];
2446 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002447 if (status_out->pstdin_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002448 inheritable_fds[size++] = stdin_fds[0];
2449 inheritable_fds[size++] = stdin_fds[1];
2450 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002451 if (status_out->pstdout_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002452 inheritable_fds[size++] = stdout_fds[0];
2453 inheritable_fds[size++] = stdout_fds[1];
2454 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002455 if (status_out->pstderr_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002456 inheritable_fds[size++] = stderr_fds[0];
2457 inheritable_fds[size++] = stderr_fds[1];
2458 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002459 for (i = 0; i < j->preserved_fd_count; i++) {
2460 /*
2461 * Preserve all parent_fds. They will be dup2(2)-ed in
2462 * the child later.
2463 */
2464 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
2465 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002466
2467 if (close_open_fds(inheritable_fds, size) < 0)
2468 die("failed to close open file descriptors");
2469 }
2470
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002471 if (redirect_fds(j))
2472 die("failed to set up fd redirections");
2473
Dylan Reidce5b55e2016-01-13 11:04:16 -08002474 if (sync_child)
2475 wait_for_parent_setup(child_sync_pipe_fds);
2476
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002477 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002478 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002479
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002480 /*
2481 * If we want to write to the jailed process' standard input,
2482 * set up the read end of the pipe.
2483 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002484 if (status_out->pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002485 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2486 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002487 die("failed to set up stdin pipe");
2488 }
2489
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002490 /*
2491 * If we want to read from the jailed process' standard output,
2492 * set up the write end of the pipe.
2493 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002494 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002495 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2496 STDOUT_FILENO) < 0)
2497 die("failed to set up stdout pipe");
2498 }
2499
2500 /*
2501 * If we want to read from the jailed process' standard error,
2502 * set up the write end of the pipe.
2503 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002504 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002505 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2506 STDERR_FILENO) < 0)
2507 die("failed to set up stderr pipe");
2508 }
2509
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002510 /*
2511 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2512 * This prevents the jailed process from using the TIOCSTI ioctl
2513 * to push characters into the parent process terminal's input buffer,
2514 * therefore escaping the jail.
2515 */
2516 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2517 isatty(STDERR_FILENO)) {
2518 if (setsid() < 0) {
2519 pdie("setsid() failed");
2520 }
2521 }
2522
Dylan Reid791f5772015-09-14 20:02:42 -07002523 /* If running an init program, let it decide when/how to mount /proc. */
2524 if (pid_namespace && !do_init)
2525 j->flags.remount_proc_ro = 0;
2526
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002527 if (use_preload) {
2528 /* Strip out flags that cannot be inherited across execve(2). */
2529 minijail_preexec(j);
2530 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002531 /*
2532 * If not using LD_PRELOAD, do all jailing before execve(2).
2533 * Note that PID namespaces can only be entered on fork(2),
2534 * so that flag is still cleared.
2535 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002536 j->flags.pids = 0;
2537 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002538 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002539 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002540
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002541 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002542 /*
2543 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002544 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002545 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002546 * a child to actually run the program. If |do_init == 0|, we
2547 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002548 *
2549 * If we're multithreaded, we'll probably deadlock here. See
2550 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002551 */
2552 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002553 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002554 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002555 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002556 /*
2557 * Best effort. Don't bother checking the return value.
2558 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002559 prctl(PR_SET_NAME, "minijail-init");
2560 init(child_pid); /* Never returns. */
2561 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002562 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002563
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002564 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
2565
Elly Jonesdd3e8512012-01-23 15:13:38 -05002566 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002567 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002568 * calling process
2569 * -> execve()-ing process
2570 * If we are:
2571 * calling process
2572 * -> init()-ing process
2573 * -> execve()-ing process
2574 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002575 ret = execve(config->filename, config->argv, environ);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002576 if (ret == -1) {
Dylan Reidacfb8be2017-08-25 12:56:51 -07002577 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002578 }
2579 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002580}
2581
Will Drewry6ac91122011-10-21 16:38:58 -05002582int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002583{
2584 int st;
2585 if (kill(j->initpid, SIGTERM))
2586 return -errno;
2587 if (waitpid(j->initpid, &st, 0) < 0)
2588 return -errno;
2589 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002590}
2591
Will Drewry6ac91122011-10-21 16:38:58 -05002592int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002593{
2594 int st;
2595 if (waitpid(j->initpid, &st, 0) < 0)
2596 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002597
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002598 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002599 int error_status = st;
2600 if (WIFSIGNALED(st)) {
2601 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002602 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002603 j->initpid, signum);
2604 /*
2605 * We return MINIJAIL_ERR_JAIL if the process received
2606 * SIGSYS, which happens when a syscall is blocked by
2607 * seccomp filters.
2608 * If not, we do what bash(1) does:
2609 * $? = 128 + signum
2610 */
2611 if (signum == SIGSYS) {
2612 error_status = MINIJAIL_ERR_JAIL;
2613 } else {
2614 error_status = 128 + signum;
2615 }
2616 }
2617 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002618 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002619
2620 int exit_status = WEXITSTATUS(st);
2621 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002622 info("child process %d exited with status %d",
2623 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002624
2625 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002626}
2627
Will Drewry6ac91122011-10-21 16:38:58 -05002628void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002629{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002630 size_t i;
2631
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002632 if (j->flags.seccomp_filter && j->filter_prog) {
2633 free(j->filter_prog->filter);
2634 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002635 }
Dylan Reid648b2202015-10-23 00:50:00 -07002636 while (j->mounts_head) {
2637 struct mountpoint *m = j->mounts_head;
2638 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002639 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002640 free(m->type);
2641 free(m->dest);
2642 free(m->src);
2643 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002644 }
Dylan Reid648b2202015-10-23 00:50:00 -07002645 j->mounts_tail = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002646 while (j->hooks_head) {
2647 struct hook *c = j->hooks_head;
2648 j->hooks_head = c->next;
2649 free(c);
2650 }
2651 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002652 if (j->user)
2653 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002654 if (j->suppl_gid_list)
2655 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002656 if (j->chrootdir)
2657 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002658 if (j->pid_file_path)
2659 free(j->pid_file_path);
2660 if (j->uidmap)
2661 free(j->uidmap);
2662 if (j->gidmap)
2663 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002664 if (j->hostname)
2665 free(j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08002666 if (j->alt_syscall_table)
2667 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002668 for (i = 0; i < j->cgroup_count; ++i)
2669 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002670 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002671}