blob: f9fb0e9903f91e3d6539f806482b3424e5b68f03 [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 <sched.h>
17#include <signal.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070018#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080019#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040023#include <sys/capability.h>
24#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050025#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040026#include <sys/prctl.h>
Dylan Reid0f72ef42017-06-06 15:42:49 -070027#include <sys/resource.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070028#include <sys/stat.h>
Mike Frysinger33ffef32017-01-13 19:53:19 -050029#include <sys/sysmacros.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070030#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
Stephen Barber0d1cbf62017-10-16 22:19:38 -070080#define MAX_PRESERVED_FDS 32U
Luis Hector Chavez1617f632017-08-01 18:32:30 -070081
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;
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -080087 rlim_t cur;
88 rlim_t max;
Dylan Reid0f72ef42017-06-06 15:42:49 -070089};
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;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700129 int pids : 1;
130 int ipc : 1;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400131 int uts : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700132 int net : 1;
133 int enter_net : 1;
134 int ns_cgroups : 1;
135 int userns : 1;
136 int disable_setgroups : 1;
137 int seccomp : 1;
138 int remount_proc_ro : 1;
139 int no_new_privs : 1;
140 int seccomp_filter : 1;
141 int seccomp_filter_tsync : 1;
142 int seccomp_filter_logging : 1;
143 int chroot : 1;
144 int pivot_root : 1;
Mike Frysinger33ffef32017-01-13 19:53:19 -0500145 int mount_dev : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700146 int mount_tmp : 1;
147 int do_init : 1;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700148 int run_as_init : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700149 int pid_file : 1;
150 int cgroups : 1;
151 int alt_syscall : 1;
152 int reset_signal_mask : 1;
153 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800154 int new_session_keyring : 1;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400155 int forward_signals : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400156 } flags;
157 uid_t uid;
158 gid_t gid;
159 gid_t usergid;
160 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800161 size_t suppl_gid_count;
162 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400163 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800164 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400165 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700166 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700167 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400168 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800169 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800170 char *uidmap;
171 char *gidmap;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400172 char *hostname;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800173 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800174 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800175 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700176 struct mountpoint *mounts_head;
177 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800178 size_t mounts_count;
Mike Frysinger785b1c32018-02-23 15:47:24 -0500179 unsigned long remount_mode;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100180 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800181 char *cgroups[MAX_CGROUPS];
182 size_t cgroup_count;
Dylan Reid0f72ef42017-06-06 15:42:49 -0700183 struct minijail_rlimit rlimits[MAX_RLIMITS];
184 size_t rlimit_count;
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700185 uint64_t securebits_skip_mask;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700186 struct hook *hooks_head;
187 struct hook *hooks_tail;
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700188 struct preserved_fd preserved_fds[MAX_PRESERVED_FDS];
189 size_t preserved_fd_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500190};
191
Luis Hector Chavez64730af2017-09-13 13:18:59 -0700192static void run_hooks_or_die(const struct minijail *j,
193 minijail_hook_event_t event);
194
Mike Frysingerac08a682017-10-10 02:04:50 -0400195static void free_mounts_list(struct minijail *j)
196{
197 while (j->mounts_head) {
198 struct mountpoint *m = j->mounts_head;
199 j->mounts_head = j->mounts_head->next;
200 free(m->data);
201 free(m->type);
202 free(m->dest);
203 free(m->src);
204 free(m);
205 }
206 // No need to clear mounts_head as we know it's NULL after the loop.
207 j->mounts_tail = NULL;
208}
209
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700210/*
211 * Strip out flags meant for the parent.
212 * We keep things that are not inherited across execve(2) (e.g. capabilities),
213 * or are easier to set after execve(2) (e.g. seccomp filters).
214 */
215void minijail_preenter(struct minijail *j)
216{
217 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700218 j->flags.enter_vfs = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700219 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700220 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800221 j->flags.do_init = 0;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700222 j->flags.run_as_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800223 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800224 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400225 j->flags.forward_signals = 0;
Mike Frysinger785b1c32018-02-23 15:47:24 -0500226 j->remount_mode = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700227}
228
229/*
230 * Strip out flags meant for the child.
231 * We keep things that are inherited across execve(2).
232 */
233void minijail_preexec(struct minijail *j)
234{
235 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700236 int enter_vfs = j->flags.enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700237 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800238 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700239 if (j->user)
240 free(j->user);
241 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800242 if (j->suppl_gid_list)
243 free(j->suppl_gid_list);
244 j->suppl_gid_list = NULL;
Mike Frysingerac08a682017-10-10 02:04:50 -0400245 free_mounts_list(j);
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700246 memset(&j->flags, 0, sizeof(j->flags));
247 /* Now restore anything we meant to keep. */
248 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700249 j->flags.enter_vfs = enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700250 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800251 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700252 /* Note, |pids| will already have been used before this call. */
253}
254
255/* Minijail API. */
256
Will Drewry6ac91122011-10-21 16:38:58 -0500257struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400258{
Mike Frysinger785b1c32018-02-23 15:47:24 -0500259 struct minijail *j = calloc(1, sizeof(struct minijail));
260 j->remount_mode = MS_PRIVATE;
261 return j;
Elly Jonescd7a9042011-07-22 13:56:51 -0400262}
263
Will Drewry6ac91122011-10-21 16:38:58 -0500264void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400265{
266 if (uid == 0)
267 die("useless change to uid 0");
268 j->uid = uid;
269 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400270}
271
Will Drewry6ac91122011-10-21 16:38:58 -0500272void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400273{
274 if (gid == 0)
275 die("useless change to gid 0");
276 j->gid = gid;
277 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400278}
279
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800280void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
281 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800282{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800283 size_t i;
284
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500285 if (j->flags.inherit_suppl_gids)
286 die("cannot inherit *and* set supplementary groups");
287 if (j->flags.keep_suppl_gids)
288 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800289
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800290 if (size == 0) {
291 /* Clear supplementary groups. */
292 j->suppl_gid_list = NULL;
293 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100294 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800295 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800296 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800297
298 /* Copy the gid_t array. */
299 j->suppl_gid_list = calloc(size, sizeof(gid_t));
300 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800301 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800302 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800303 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800304 j->suppl_gid_list[i] = list[i];
305 }
306 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100307 j->flags.set_suppl_gids = 1;
308}
309
310void API minijail_keep_supplementary_gids(struct minijail *j) {
311 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800312}
313
Will Drewry6ac91122011-10-21 16:38:58 -0500314int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400315{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700316 uid_t uid;
317 gid_t gid;
318 int rc = lookup_user(user, &uid, &gid);
319 if (rc)
320 return rc;
321 minijail_change_uid(j, uid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400322 j->user = strdup(user);
323 if (!j->user)
324 return -ENOMEM;
Luis Hector Chavez71323552017-09-05 09:17:22 -0700325 j->usergid = gid;
Elly Jonese1749eb2011-10-07 13:54:59 -0400326 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400327}
328
Will Drewry6ac91122011-10-21 16:38:58 -0500329int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400330{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700331 gid_t gid;
332 int rc = lookup_group(group, &gid);
333 if (rc)
334 return rc;
335 minijail_change_gid(j, gid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400336 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400337}
338
Will Drewry6ac91122011-10-21 16:38:58 -0500339void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400340{
341 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400342}
343
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700344void API minijail_no_new_privs(struct minijail *j)
345{
346 j->flags.no_new_privs = 1;
347}
348
Will Drewry6ac91122011-10-21 16:38:58 -0500349void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400350{
351 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500352}
353
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400354void API minijail_set_seccomp_filter_tsync(struct minijail *j)
355{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400356 if (j->filter_len > 0 && j->filter_prog != NULL) {
357 die("minijail_set_seccomp_filter_tsync() must be called "
358 "before minijail_parse_seccomp_filters()");
359 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400360 j->flags.seccomp_filter_tsync = 1;
361}
362
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700363void API minijail_log_seccomp_filter_failures(struct minijail *j)
364{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400365 if (j->filter_len > 0 && j->filter_prog != NULL) {
366 die("minijail_log_seccomp_filter_failures() must be called "
367 "before minijail_parse_seccomp_filters()");
368 }
369 j->flags.seccomp_filter_logging = 1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700370}
371
Will Drewry6ac91122011-10-21 16:38:58 -0500372void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400373{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800374 /*
375 * 'minijail_use_caps' configures a runtime-capabilities-only
376 * environment, including a bounding set matching the thread's runtime
377 * (permitted|inheritable|effective) sets.
378 * Therefore, it will override any existing bounding set configurations
379 * since the latter would allow gaining extra runtime capabilities from
380 * file capabilities.
381 */
382 if (j->flags.capbset_drop) {
383 warn("overriding bounding set configuration");
384 j->cap_bset = 0;
385 j->flags.capbset_drop = 0;
386 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400387 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800388 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400389}
390
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800391void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
392{
393 if (j->flags.use_caps) {
394 /*
395 * 'minijail_use_caps' will have already configured a capability
396 * bounding set matching the (permitted|inheritable|effective)
397 * sets. Abort if the user tries to configure a separate
398 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
399 * are mutually exclusive.
400 */
401 die("runtime capabilities already configured, can't drop "
402 "bounding set separately");
403 }
404 j->cap_bset = capmask;
405 j->flags.capbset_drop = 1;
406}
407
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400408void API minijail_set_ambient_caps(struct minijail *j)
409{
410 j->flags.set_ambient_caps = 1;
411}
412
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800413void API minijail_reset_signal_mask(struct minijail *j)
414{
Peter Qiu2860c462015-12-16 15:13:06 -0800415 j->flags.reset_signal_mask = 1;
416}
417
Will Drewry6ac91122011-10-21 16:38:58 -0500418void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400419{
420 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400421}
422
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700423void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
424{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800425 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700426 if (ns_fd < 0) {
427 pdie("failed to open namespace '%s'", ns_path);
428 }
429 j->mountns_fd = ns_fd;
430 j->flags.enter_vfs = 1;
431}
432
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800433void API minijail_new_session_keyring(struct minijail *j)
434{
435 j->flags.new_session_keyring = 1;
436}
437
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700438void API minijail_skip_setting_securebits(struct minijail *j,
439 uint64_t securebits_skip_mask)
440{
441 j->securebits_skip_mask = securebits_skip_mask;
442}
443
Mike Frysinger785b1c32018-02-23 15:47:24 -0500444void API minijail_remount_mode(struct minijail *j, unsigned long mode)
445{
446 j->remount_mode = mode;
447}
448
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800449void API minijail_skip_remount_private(struct minijail *j)
450{
Mike Frysinger785b1c32018-02-23 15:47:24 -0500451 j->remount_mode = 0;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800452}
453
Will Drewry6ac91122011-10-21 16:38:58 -0500454void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400455{
Elly Jonese58176c2012-01-23 11:46:17 -0500456 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700457 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400458 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800459 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400460}
461
Dylan Reidf7942472015-11-18 17:55:26 -0800462void API minijail_namespace_ipc(struct minijail *j)
463{
464 j->flags.ipc = 1;
465}
466
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400467void API minijail_namespace_uts(struct minijail *j)
468{
469 j->flags.uts = 1;
470}
471
472int API minijail_namespace_set_hostname(struct minijail *j, const char *name)
473{
474 if (j->hostname)
475 return -EINVAL;
476 minijail_namespace_uts(j);
477 j->hostname = strdup(name);
478 if (!j->hostname)
479 return -ENOMEM;
480 return 0;
481}
482
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400483void API minijail_namespace_net(struct minijail *j)
484{
485 j->flags.net = 1;
486}
487
Dylan Reid1102f5a2015-09-15 11:52:20 -0700488void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
489{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800490 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700491 if (ns_fd < 0) {
492 pdie("failed to open namespace '%s'", ns_path);
493 }
494 j->netns_fd = ns_fd;
495 j->flags.enter_net = 1;
496}
497
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700498void API minijail_namespace_cgroups(struct minijail *j)
499{
500 j->flags.ns_cgroups = 1;
501}
502
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700503void API minijail_close_open_fds(struct minijail *j)
504{
505 j->flags.close_open_fds = 1;
506}
507
Dylan Reid791f5772015-09-14 20:02:42 -0700508void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400509{
510 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700511 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400512}
513
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800514void API minijail_namespace_user(struct minijail *j)
515{
516 j->flags.userns = 1;
517}
518
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400519void API minijail_namespace_user_disable_setgroups(struct minijail *j)
520{
521 j->flags.disable_setgroups = 1;
522}
523
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800524int API minijail_uidmap(struct minijail *j, const char *uidmap)
525{
526 j->uidmap = strdup(uidmap);
527 if (!j->uidmap)
528 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800529 char *ch;
530 for (ch = j->uidmap; *ch; ch++) {
531 if (*ch == ',')
532 *ch = '\n';
533 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800534 return 0;
535}
536
537int API minijail_gidmap(struct minijail *j, const char *gidmap)
538{
539 j->gidmap = strdup(gidmap);
540 if (!j->gidmap)
541 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800542 char *ch;
543 for (ch = j->gidmap; *ch; ch++) {
544 if (*ch == ',')
545 *ch = '\n';
546 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800547 return 0;
548}
549
Will Drewry6ac91122011-10-21 16:38:58 -0500550void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400551{
Lutz Justen13807cb2017-01-03 17:11:55 +0100552 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400553}
554
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800555void API minijail_run_as_init(struct minijail *j)
556{
557 /*
558 * Since the jailed program will become 'init' in the new PID namespace,
559 * Minijail does not need to fork an 'init' process.
560 */
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700561 j->flags.run_as_init = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800562}
563
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700564int API minijail_enter_chroot(struct minijail *j, const char *dir)
565{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400566 if (j->chrootdir)
567 return -EINVAL;
568 j->chrootdir = strdup(dir);
569 if (!j->chrootdir)
570 return -ENOMEM;
571 j->flags.chroot = 1;
572 return 0;
573}
574
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800575int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
576{
577 if (j->chrootdir)
578 return -EINVAL;
579 j->chrootdir = strdup(dir);
580 if (!j->chrootdir)
581 return -ENOMEM;
582 j->flags.pivot_root = 1;
583 return 0;
584}
585
Dylan Reida14e08d2015-10-22 21:05:29 -0700586char API *minijail_get_original_path(struct minijail *j,
587 const char *path_inside_chroot)
588{
Dylan Reid648b2202015-10-23 00:50:00 -0700589 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700590
Dylan Reid648b2202015-10-23 00:50:00 -0700591 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700592 while (b) {
593 /*
594 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700595 * mount, then the original path is exactly the source of
596 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700597 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700598 * mount source = /some/path/exe, mount dest =
599 * /chroot/path/exe Then when getting the original path of
600 * "/chroot/path/exe", the source of that mount,
601 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700602 */
603 if (!strcmp(b->dest, path_inside_chroot))
604 return strdup(b->src);
605
606 /*
607 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700608 * mount, take the suffix of the chroot path relative to the
609 * mount destination path, and append it to the mount source
610 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700611 */
612 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
613 const char *relative_path =
614 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400615 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700616 }
617 b = b->next;
618 }
619
620 /* If there is a chroot path, append |path_inside_chroot| to that. */
621 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400622 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700623
624 /* No chroot, so the path outside is the same as it is inside. */
625 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700626}
627
Martin Pelikánab9eb442017-01-25 11:53:58 +1100628size_t minijail_get_tmpfs_size(const struct minijail *j)
629{
630 return j->tmpfs_size;
631}
632
Mike Frysinger33ffef32017-01-13 19:53:19 -0500633void API minijail_mount_dev(struct minijail *j)
634{
635 j->flags.mount_dev = 1;
636}
637
Lee Campbell11af0622014-05-22 12:36:04 -0700638void API minijail_mount_tmp(struct minijail *j)
639{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100640 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
641}
642
643void API minijail_mount_tmp_size(struct minijail *j, size_t size)
644{
645 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700646 j->flags.mount_tmp = 1;
647}
648
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800649int API minijail_write_pid_file(struct minijail *j, const char *path)
650{
651 j->pid_file_path = strdup(path);
652 if (!j->pid_file_path)
653 return -ENOMEM;
654 j->flags.pid_file = 1;
655 return 0;
656}
657
Dylan Reid605ce7f2016-01-19 19:21:00 -0800658int API minijail_add_to_cgroup(struct minijail *j, const char *path)
659{
660 if (j->cgroup_count >= MAX_CGROUPS)
661 return -ENOMEM;
662 j->cgroups[j->cgroup_count] = strdup(path);
663 if (!j->cgroups[j->cgroup_count])
664 return -ENOMEM;
665 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800666 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800667 return 0;
668}
669
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800670int API minijail_rlimit(struct minijail *j, int type, rlim_t cur, rlim_t max)
Dylan Reid0f72ef42017-06-06 15:42:49 -0700671{
672 size_t i;
673
674 if (j->rlimit_count >= MAX_RLIMITS)
675 return -ENOMEM;
676 /* It's an error if the caller sets the same rlimit multiple times. */
677 for (i = 0; i < j->rlimit_count; i++) {
678 if (j->rlimits[i].type == type)
679 return -EEXIST;
680 }
681
682 j->rlimits[j->rlimit_count].type = type;
683 j->rlimits[j->rlimit_count].cur = cur;
684 j->rlimits[j->rlimit_count].max = max;
685 j->rlimit_count++;
686 return 0;
687}
688
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400689int API minijail_forward_signals(struct minijail *j)
690{
691 j->flags.forward_signals = 1;
692 return 0;
693}
694
Dylan Reid81e23972016-05-18 14:06:35 -0700695int API minijail_mount_with_data(struct minijail *j, const char *src,
696 const char *dest, const char *type,
697 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700698{
Dylan Reid648b2202015-10-23 00:50:00 -0700699 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400700
701 if (*dest != '/')
702 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700703 m = calloc(1, sizeof(*m));
704 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400705 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700706 m->dest = strdup(dest);
707 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400708 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700709 m->src = strdup(src);
710 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400711 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700712 m->type = strdup(type);
713 if (!m->type)
714 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700715 if (data) {
716 m->data = strdup(data);
717 if (!m->data)
718 goto error;
719 m->has_data = 1;
720 }
Dylan Reid648b2202015-10-23 00:50:00 -0700721 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400722
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800723 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400724
Elly Jonesdd3e8512012-01-23 15:13:38 -0500725 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700726 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400727 * containing vfs namespace.
728 */
729 minijail_namespace_vfs(j);
730
Dylan Reid648b2202015-10-23 00:50:00 -0700731 if (j->mounts_tail)
732 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400733 else
Dylan Reid648b2202015-10-23 00:50:00 -0700734 j->mounts_head = m;
735 j->mounts_tail = m;
736 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400737
738 return 0;
739
740error:
Dylan Reid81e23972016-05-18 14:06:35 -0700741 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700742 free(m->src);
743 free(m->dest);
744 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400745 return -ENOMEM;
746}
747
Dylan Reid81e23972016-05-18 14:06:35 -0700748int API minijail_mount(struct minijail *j, const char *src, const char *dest,
749 const char *type, unsigned long flags)
750{
751 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
752}
753
Dylan Reid648b2202015-10-23 00:50:00 -0700754int API minijail_bind(struct minijail *j, const char *src, const char *dest,
755 int writeable)
756{
757 unsigned long flags = MS_BIND;
758
759 if (!writeable)
760 flags |= MS_RDONLY;
761
762 return minijail_mount(j, src, dest, "", flags);
763}
764
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700765int API minijail_add_hook(struct minijail *j, minijail_hook_t hook,
766 void *payload, minijail_hook_event_t event)
767{
768 struct hook *c;
769
770 if (hook == NULL)
771 return -EINVAL;
772 if (event >= MINIJAIL_HOOK_EVENT_MAX)
773 return -EINVAL;
774 c = calloc(1, sizeof(*c));
775 if (!c)
776 return -ENOMEM;
777
778 c->hook = hook;
779 c->payload = payload;
780 c->event = event;
781
782 if (j->hooks_tail)
783 j->hooks_tail->next = c;
784 else
785 j->hooks_head = c;
786 j->hooks_tail = c;
787
788 return 0;
789}
790
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700791int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd)
792{
793 if (parent_fd < 0 || child_fd < 0)
794 return -EINVAL;
795 if (j->preserved_fd_count >= MAX_PRESERVED_FDS)
796 return -ENOMEM;
797 j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd;
798 j->preserved_fds[j->preserved_fd_count].child_fd = child_fd;
799 j->preserved_fd_count++;
800 return 0;
801}
802
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400803static void clear_seccomp_options(struct minijail *j)
804{
805 j->flags.seccomp_filter = 0;
806 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400807 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400808 j->filter_len = 0;
809 j->filter_prog = NULL;
810 j->flags.no_new_privs = 0;
811}
812
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400813static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400814{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400815 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400816 /*
817 * |errno| will be set to EINVAL when seccomp has not been
818 * compiled into the kernel. On certain platforms and kernel
819 * versions this is not a fatal failure. In that case, and only
820 * in that case, disable seccomp and skip loading the filters.
821 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400822 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400823 warn("not loading seccomp filters, seccomp filter not "
824 "supported");
825 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400826 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700827 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400828 /*
829 * If |errno| != EINVAL or seccomp_can_softfail() is false,
830 * we can proceed. Worst case scenario minijail_enter() will
831 * abort() if seccomp fails.
832 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700833 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400834 if (j->flags.seccomp_filter_tsync) {
835 /* Are the seccomp(2) syscall and the TSYNC option supported? */
836 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
837 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
838 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400839 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
840 warn("seccomp(2) syscall not supported");
841 clear_seccomp_options(j);
842 return 0;
843 } else if (saved_errno == EINVAL &&
844 seccomp_can_softfail()) {
845 warn(
846 "seccomp filter thread sync not supported");
847 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400848 return 0;
849 }
850 /*
851 * Similar logic here. If seccomp_can_softfail() is
852 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
853 * we can proceed. Worst case scenario minijail_enter()
854 * will abort() if seccomp or TSYNC fail.
855 */
856 }
857 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400858 return 1;
859}
860
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700861static int parse_seccomp_filters(struct minijail *j, const char *filename,
862 FILE *policy_file)
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400863{
864 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400865 int use_ret_trap =
866 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
867 int allow_logging = j->flags.seccomp_filter_logging;
868
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700869 if (compile_filter(filename, policy_file, fprog, use_ret_trap,
870 allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400871 free(fprog);
872 return -1;
873 }
874
875 j->filter_len = fprog->len;
876 j->filter_prog = fprog;
877 return 0;
878}
879
880void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
881{
882 if (!seccomp_should_parse_filters(j))
883 return;
884
Elly Jonese1749eb2011-10-07 13:54:59 -0400885 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800886 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700887 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400888 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800889
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700890 if (parse_seccomp_filters(j, path, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700891 die("failed to compile seccomp filter BPF program in '%s'",
892 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800893 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400894 fclose(file);
895}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800896
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400897void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
898{
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700899 char *fd_path, *path;
900 FILE *file;
901
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400902 if (!seccomp_should_parse_filters(j))
903 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800904
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700905 file = fdopen(fd, "r");
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400906 if (!file) {
907 pdie("failed to associate stream with fd %d", fd);
908 }
909
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700910 if (asprintf(&fd_path, "/proc/self/fd/%d", fd) == -1)
911 pdie("failed to create path for fd %d", fd);
912 path = realpath(fd_path, NULL);
913 if (path == NULL)
914 pwarn("failed to get path of fd %d", fd);
915 free(fd_path);
916
917 if (parse_seccomp_filters(j, path ? path : "<fd>", file) != 0) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400918 die("failed to compile seccomp filter BPF program from fd %d",
919 fd);
920 }
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700921 free(path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400922 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500923}
924
Andrew Brestickereac28942015-11-11 16:04:46 -0800925int API minijail_use_alt_syscall(struct minijail *j, const char *table)
926{
927 j->alt_syscall_table = strdup(table);
928 if (!j->alt_syscall_table)
929 return -ENOMEM;
930 j->flags.alt_syscall = 1;
931 return 0;
932}
933
Will Drewryf89aef52011-09-16 16:48:57 -0500934struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400935 size_t available;
936 size_t total;
937 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500938};
939
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800940void marshal_state_init(struct marshal_state *state, char *buf,
941 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400942{
943 state->available = available;
944 state->buf = buf;
945 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500946}
947
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800948void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400949{
950 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500951
Elly Jonese1749eb2011-10-07 13:54:59 -0400952 /* Up to |available| will be written. */
953 if (copy_len) {
954 memcpy(state->buf, src, copy_len);
955 state->buf += copy_len;
956 state->available -= copy_len;
957 }
958 /* |total| will contain the expected length. */
959 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500960}
961
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400962void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700963{
964 marshal_append(state, m->src, strlen(m->src) + 1);
965 marshal_append(state, m->dest, strlen(m->dest) + 1);
966 marshal_append(state, m->type, strlen(m->type) + 1);
967 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
968 if (m->has_data)
969 marshal_append(state, m->data, strlen(m->data) + 1);
970 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
971}
972
Will Drewry6ac91122011-10-21 16:38:58 -0500973void minijail_marshal_helper(struct marshal_state *state,
974 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400975{
Dylan Reid648b2202015-10-23 00:50:00 -0700976 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800977 size_t i;
978
Elly Jonese1749eb2011-10-07 13:54:59 -0400979 marshal_append(state, (char *)j, sizeof(*j));
980 if (j->user)
981 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800982 if (j->suppl_gid_list) {
983 marshal_append(state, j->suppl_gid_list,
984 j->suppl_gid_count * sizeof(gid_t));
985 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400986 if (j->chrootdir)
987 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400988 if (j->hostname)
989 marshal_append(state, j->hostname, strlen(j->hostname) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800990 if (j->alt_syscall_table) {
991 marshal_append(state, j->alt_syscall_table,
992 strlen(j->alt_syscall_table) + 1);
993 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800994 if (j->flags.seccomp_filter && j->filter_prog) {
995 struct sock_fprog *fp = j->filter_prog;
996 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800997 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400998 }
Dylan Reid648b2202015-10-23 00:50:00 -0700999 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001000 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001001 }
Dylan Reid605ce7f2016-01-19 19:21:00 -08001002 for (i = 0; i < j->cgroup_count; ++i)
1003 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -05001004}
1005
Will Drewry6ac91122011-10-21 16:38:58 -05001006size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001007{
1008 struct marshal_state state;
1009 marshal_state_init(&state, NULL, 0);
1010 minijail_marshal_helper(&state, j);
1011 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -05001012}
1013
Elly Jonese1749eb2011-10-07 13:54:59 -04001014int minijail_marshal(const struct minijail *j, char *buf, size_t available)
1015{
1016 struct marshal_state state;
1017 marshal_state_init(&state, buf, available);
1018 minijail_marshal_helper(&state, j);
1019 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -05001020}
1021
Elly Jonese1749eb2011-10-07 13:54:59 -04001022int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
1023{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -08001024 size_t i;
1025 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -05001026 int ret = -EINVAL;
1027
Elly Jonese1749eb2011-10-07 13:54:59 -04001028 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001029 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001030 memcpy((void *)j, serialized, sizeof(*j));
1031 serialized += sizeof(*j);
1032 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001033
Will Drewrybee7ba72011-10-21 20:47:01 -05001034 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001035 j->pid_file_path = NULL;
1036 j->uidmap = NULL;
1037 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001038 j->mounts_head = NULL;
1039 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001040 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001041 j->hooks_head = NULL;
1042 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001043
Elly Jonese1749eb2011-10-07 13:54:59 -04001044 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001045 char *user = consumestr(&serialized, &length);
1046 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001047 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001048 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001049 if (!j->user)
1050 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001051 }
Will Drewryf89aef52011-09-16 16:48:57 -05001052
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001053 if (j->suppl_gid_list) { /* stale pointer */
1054 if (j->suppl_gid_count > NGROUPS_MAX) {
1055 goto bad_gid_list;
1056 }
1057 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1058 void *gid_list_bytes =
1059 consumebytes(gid_list_size, &serialized, &length);
1060 if (!gid_list_bytes)
1061 goto bad_gid_list;
1062
1063 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1064 if (!j->suppl_gid_list)
1065 goto bad_gid_list;
1066
1067 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1068 }
1069
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001070 if (j->chrootdir) { /* stale pointer */
1071 char *chrootdir = consumestr(&serialized, &length);
1072 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001073 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001074 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001075 if (!j->chrootdir)
1076 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001077 }
1078
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001079 if (j->hostname) { /* stale pointer */
1080 char *hostname = consumestr(&serialized, &length);
1081 if (!hostname)
1082 goto bad_hostname;
1083 j->hostname = strdup(hostname);
1084 if (!j->hostname)
1085 goto bad_hostname;
1086 }
1087
Andrew Brestickereac28942015-11-11 16:04:46 -08001088 if (j->alt_syscall_table) { /* stale pointer */
1089 char *alt_syscall_table = consumestr(&serialized, &length);
1090 if (!alt_syscall_table)
1091 goto bad_syscall_table;
1092 j->alt_syscall_table = strdup(alt_syscall_table);
1093 if (!j->alt_syscall_table)
1094 goto bad_syscall_table;
1095 }
1096
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001097 if (j->flags.seccomp_filter && j->filter_len > 0) {
1098 size_t ninstrs = j->filter_len;
1099 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1100 ninstrs > USHRT_MAX)
1101 goto bad_filters;
1102
1103 size_t program_len = ninstrs * sizeof(struct sock_filter);
1104 void *program = consumebytes(program_len, &serialized, &length);
1105 if (!program)
1106 goto bad_filters;
1107
1108 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001109 if (!j->filter_prog)
1110 goto bad_filters;
1111
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001112 j->filter_prog->len = ninstrs;
1113 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001114 if (!j->filter_prog->filter)
1115 goto bad_filter_prog_instrs;
1116
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001117 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001118 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001119
Dylan Reid648b2202015-10-23 00:50:00 -07001120 count = j->mounts_count;
1121 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001122 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001123 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001124 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001125 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001126 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001127 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001128 const char *src = consumestr(&serialized, &length);
1129 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001130 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001131 dest = consumestr(&serialized, &length);
1132 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001133 goto bad_mounts;
1134 type = consumestr(&serialized, &length);
1135 if (!type)
1136 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001137 has_data = consumebytes(sizeof(*has_data), &serialized,
1138 &length);
1139 if (!has_data)
1140 goto bad_mounts;
1141 if (*has_data) {
1142 data = consumestr(&serialized, &length);
1143 if (!data)
1144 goto bad_mounts;
1145 }
Dylan Reid648b2202015-10-23 00:50:00 -07001146 flags = consumebytes(sizeof(*flags), &serialized, &length);
1147 if (!flags)
1148 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001149 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001150 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001151 }
1152
Dylan Reid605ce7f2016-01-19 19:21:00 -08001153 count = j->cgroup_count;
1154 j->cgroup_count = 0;
1155 for (i = 0; i < count; ++i) {
1156 char *cgroup = consumestr(&serialized, &length);
1157 if (!cgroup)
1158 goto bad_cgroups;
1159 j->cgroups[i] = strdup(cgroup);
1160 if (!j->cgroups[i])
1161 goto bad_cgroups;
1162 ++j->cgroup_count;
1163 }
1164
Elly Jonese1749eb2011-10-07 13:54:59 -04001165 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001166
Dylan Reid605ce7f2016-01-19 19:21:00 -08001167bad_cgroups:
Mike Frysingerac08a682017-10-10 02:04:50 -04001168 free_mounts_list(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001169 for (i = 0; i < j->cgroup_count; ++i)
1170 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001171bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001172 if (j->flags.seccomp_filter && j->filter_len > 0) {
1173 free(j->filter_prog->filter);
1174 free(j->filter_prog);
1175 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001176bad_filter_prog_instrs:
1177 if (j->filter_prog)
1178 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001179bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001180 if (j->alt_syscall_table)
1181 free(j->alt_syscall_table);
1182bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001183 if (j->chrootdir)
1184 free(j->chrootdir);
1185bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001186 if (j->hostname)
1187 free(j->hostname);
1188bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001189 if (j->suppl_gid_list)
1190 free(j->suppl_gid_list);
1191bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001192 if (j->user)
1193 free(j->user);
1194clear_pointers:
1195 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001196 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001197 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001198 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001199 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001200 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001201out:
1202 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001203}
1204
Mike Frysinger33ffef32017-01-13 19:53:19 -05001205struct dev_spec {
1206 const char *name;
1207 mode_t mode;
1208 dev_t major, minor;
1209};
1210
1211static const struct dev_spec device_nodes[] = {
1212 {
1213 "null",
1214 S_IFCHR | 0666, 1, 3,
1215 },
1216 {
1217 "zero",
1218 S_IFCHR | 0666, 1, 5,
1219 },
1220 {
1221 "full",
1222 S_IFCHR | 0666, 1, 7,
1223 },
1224 {
1225 "urandom",
1226 S_IFCHR | 0444, 1, 9,
1227 },
1228 {
1229 "tty",
1230 S_IFCHR | 0666, 5, 0,
1231 },
1232};
1233
1234struct dev_sym_spec {
1235 const char *source, *dest;
1236};
1237
1238static const struct dev_sym_spec device_symlinks[] = {
1239 { "ptmx", "pts/ptmx", },
1240 { "fd", "/proc/self/fd", },
1241 { "stdin", "fd/0", },
1242 { "stdout", "fd/1", },
1243 { "stderr", "fd/2", },
1244};
1245
1246/*
1247 * Clean up the temporary dev path we had setup previously. In case of errors,
1248 * we don't want to go leaking empty tempdirs.
1249 */
1250static void mount_dev_cleanup(char *dev_path)
1251{
1252 umount2(dev_path, MNT_DETACH);
1253 rmdir(dev_path);
1254 free(dev_path);
1255}
1256
1257/*
1258 * Set up the pseudo /dev path at the temporary location.
1259 * See mount_dev_finalize for more details.
1260 */
1261static int mount_dev(char **dev_path_ret)
1262{
1263 int ret;
1264 int dev_fd;
1265 size_t i;
1266 mode_t mask;
1267 char *dev_path;
1268
1269 /*
1270 * Create a temp path for the /dev init. We'll relocate this to the
1271 * final location later on in the startup process.
1272 */
1273 dev_path = *dev_path_ret = strdup("/tmp/minijail.dev.XXXXXX");
1274 if (dev_path == NULL || mkdtemp(dev_path) == NULL)
1275 pdie("could not create temp path for /dev");
1276
1277 /* Set up the empty /dev mount point first. */
1278 ret = mount("minijail-devfs", dev_path, "tmpfs",
1279 MS_NOEXEC | MS_NOSUID, "size=5M,mode=755");
1280 if (ret) {
1281 rmdir(dev_path);
1282 return ret;
1283 }
1284
1285 /* We want to set the mode directly from the spec. */
1286 mask = umask(0);
1287
1288 /* Get a handle to the temp dev path for *at funcs below. */
1289 dev_fd = open(dev_path, O_DIRECTORY|O_PATH|O_CLOEXEC);
1290 if (dev_fd < 0) {
1291 ret = 1;
1292 goto done;
1293 }
1294
1295 /* Create all the nodes in /dev. */
1296 for (i = 0; i < ARRAY_SIZE(device_nodes); ++i) {
1297 const struct dev_spec *ds = &device_nodes[i];
1298 ret = mknodat(dev_fd, ds->name, ds->mode,
1299 makedev(ds->major, ds->minor));
1300 if (ret)
1301 goto done;
1302 }
1303
1304 /* Create all the symlinks in /dev. */
1305 for (i = 0; i < ARRAY_SIZE(device_symlinks); ++i) {
1306 const struct dev_sym_spec *ds = &device_symlinks[i];
1307 ret = symlinkat(ds->dest, dev_fd, ds->source);
1308 if (ret)
1309 goto done;
1310 }
1311
1312 /* Restore old mask. */
1313 done:
1314 close(dev_fd);
1315 umask(mask);
1316
1317 if (ret)
1318 mount_dev_cleanup(dev_path);
1319
1320 return ret;
1321}
1322
1323/*
1324 * Relocate the temporary /dev mount to its final /dev place.
1325 * We have to do this two step process so people can bind mount extra
1326 * /dev paths like /dev/log.
1327 */
1328static int mount_dev_finalize(const struct minijail *j, char *dev_path)
1329{
1330 int ret = -1;
1331 char *dest = NULL;
1332
1333 /* Unmount the /dev mount if possible. */
1334 if (umount2("/dev", MNT_DETACH))
1335 goto done;
1336
1337 if (asprintf(&dest, "%s/dev", j->chrootdir ? : "") < 0)
1338 goto done;
1339
1340 if (mount(dev_path, dest, NULL, MS_MOVE, NULL))
1341 goto done;
1342
1343 ret = 0;
1344 done:
1345 free(dest);
1346 mount_dev_cleanup(dev_path);
1347
1348 return ret;
1349}
1350
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001351/*
1352 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001353 * @j Minijail these mounts are for
1354 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001355 *
1356 * Returns 0 for success.
1357 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001358static int mount_one(const struct minijail *j, struct mountpoint *m,
1359 const char *dev_path)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001360{
Dylan Reid648b2202015-10-23 00:50:00 -07001361 int ret;
1362 char *dest;
1363 int remount_ro = 0;
1364
Mike Frysinger33ffef32017-01-13 19:53:19 -05001365 /* We assume |dest| has a leading "/". */
1366 if (dev_path && strncmp("/dev/", m->dest, 5) == 0) {
1367 /* Since the temp path is rooted at /dev, skip that dest part. */
1368 if (asprintf(&dest, "%s%s", dev_path, m->dest + 4) < 0)
1369 return -ENOMEM;
1370 } else {
Mike Frysingerac08a682017-10-10 02:04:50 -04001371 if (asprintf(&dest, "%s%s", j->chrootdir ?: "", m->dest) < 0)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001372 return -ENOMEM;
1373 }
Dylan Reid648b2202015-10-23 00:50:00 -07001374
Mike Frysinger33ffef32017-01-13 19:53:19 -05001375 ret = setup_mount_destination(m->src, dest, j->uid, j->gid,
1376 (m->flags & MS_BIND));
1377 if (ret) {
1378 pwarn("creating mount target '%s' failed", dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001379 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001380 }
Dylan Reideec77962016-06-30 19:35:10 -07001381
Dylan Reid648b2202015-10-23 00:50:00 -07001382 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001383 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1384 * can't both be specified in the original bind mount.
1385 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001386 */
1387 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1388 remount_ro = 1;
1389 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001390 }
Dylan Reid648b2202015-10-23 00:50:00 -07001391
Dylan Reid81e23972016-05-18 14:06:35 -07001392 ret = mount(m->src, dest, m->type, m->flags, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001393 if (ret) {
1394 pwarn("mount: %s -> %s", m->src, dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001395 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001396 }
Dylan Reid648b2202015-10-23 00:50:00 -07001397
1398 if (remount_ro) {
1399 m->flags |= MS_RDONLY;
1400 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001401 m->flags | MS_REMOUNT, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001402 if (ret) {
1403 pwarn("bind ro: %s -> %s", m->src, dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001404 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001405 }
Dylan Reid648b2202015-10-23 00:50:00 -07001406 }
1407
Elly Jones51a5b6c2011-10-12 19:09:26 -04001408 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001409 if (m->next)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001410 return mount_one(j, m->next, dev_path);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001411 return 0;
1412
1413error:
1414 free(dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001415 return ret;
1416}
1417
Mike Frysingerac08a682017-10-10 02:04:50 -04001418static void process_mounts_or_die(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001419{
Mike Frysingerac08a682017-10-10 02:04:50 -04001420 /*
1421 * We have to mount /dev first in case there are bind mounts from
1422 * the original /dev into the new unique tmpfs one.
1423 */
1424 char *dev_path = NULL;
1425 if (j->flags.mount_dev && mount_dev(&dev_path))
1426 pdie("mount_dev failed");
Dylan Reid648b2202015-10-23 00:50:00 -07001427
Mike Frysingerac08a682017-10-10 02:04:50 -04001428 if (j->mounts_head && mount_one(j, j->mounts_head, dev_path)) {
1429 if (dev_path) {
1430 int saved_errno = errno;
1431 mount_dev_cleanup(dev_path);
1432 errno = saved_errno;
1433 }
1434 pdie("mount_one failed");
1435 }
Mike Frysinger33ffef32017-01-13 19:53:19 -05001436
1437 /*
Mike Frysingerac08a682017-10-10 02:04:50 -04001438 * Once all bind mounts have been processed, move the temp dev to
1439 * its final /dev home.
Mike Frysinger33ffef32017-01-13 19:53:19 -05001440 */
1441 if (j->flags.mount_dev && mount_dev_finalize(j, dev_path))
Mike Frysingerac08a682017-10-10 02:04:50 -04001442 pdie("mount_dev_finalize failed");
1443}
Elly Jones51a5b6c2011-10-12 19:09:26 -04001444
Mike Frysingerac08a682017-10-10 02:04:50 -04001445static int enter_chroot(const struct minijail *j)
1446{
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001447 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1448
Elly Jones51a5b6c2011-10-12 19:09:26 -04001449 if (chroot(j->chrootdir))
1450 return -errno;
1451
1452 if (chdir("/"))
1453 return -errno;
1454
1455 return 0;
1456}
1457
Mike Frysingerac08a682017-10-10 02:04:50 -04001458static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001459{
Mike Frysingerac08a682017-10-10 02:04:50 -04001460 int oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001461
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001462 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1463
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001464 /*
1465 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001466 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001467 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001468 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001469 if (oldroot < 0)
1470 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001471 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001472 if (newroot < 0)
1473 pdie("failed to open %s for fchdir", j->chrootdir);
1474
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001475 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001476 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001477 * do a self bind mount.
1478 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001479 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1480 pdie("failed to bind mount '%s'", j->chrootdir);
1481 if (chdir(j->chrootdir))
1482 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001483 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001484 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001485
1486 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001487 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001488 * change to the old root and unmount it.
1489 */
1490 if (fchdir(oldroot))
1491 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001492
1493 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05001494 * If skip_remount_private was enabled for minijail_enter(),
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001495 * there could be a shared mount point under |oldroot|. In that case,
1496 * mounts under this shared mount point will be unmounted below, and
1497 * this unmounting will propagate to the original mount namespace
1498 * (because the mount point is shared). To prevent this unexpected
1499 * unmounting, remove these mounts from their peer groups by recursively
1500 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001501 */
1502 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001503 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001504 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001505 if (umount2(".", MNT_DETACH))
1506 pdie("umount(/)");
1507 /* Change back to the new root. */
1508 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001509 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001510 if (close(oldroot))
1511 return -errno;
1512 if (close(newroot))
1513 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001514 if (chroot("/"))
1515 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001516 /* Set correct CWD for getcwd(3). */
1517 if (chdir("/"))
1518 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001519
1520 return 0;
1521}
1522
Martin Pelikánab9eb442017-01-25 11:53:58 +11001523static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001524{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001525 const char fmt[] = "size=%zu,mode=1777";
1526 /* Count for the user storing ULLONG_MAX literally + extra space. */
1527 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1528 int ret;
1529
1530 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1531
1532 if (ret <= 0)
1533 pdie("tmpfs size spec error");
1534 else if ((size_t)ret >= sizeof(data))
1535 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001536 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001537 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001538}
1539
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001540static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001541{
1542 const char *kProcPath = "/proc";
1543 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001544 /*
1545 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001546 * /proc in our namespace, which means using MS_REMOUNT here would
1547 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001548 * namespace (!). Instead, remove their mount from our namespace lazily
1549 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001550 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001551 if (umount2(kProcPath, MNT_DETACH)) {
1552 /*
1553 * If we are in a new user namespace, umount(2) will fail.
1554 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1555 */
1556 if (j->flags.userns) {
1557 info("umount(/proc, MNT_DETACH) failed, "
1558 "this is expected when using user namespaces");
1559 } else {
1560 return -errno;
1561 }
1562 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001563 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001564 return -errno;
1565 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001566}
1567
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001568static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001569{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001570 kill(j->initpid, SIGKILL);
1571 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001572}
1573
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001574static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001575{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001576 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001577 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001578}
1579
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001580static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001581{
1582 size_t i;
1583
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001584 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001585 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001586 kill_child_and_die(j, "failed to add to cgroups");
1587 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001588}
1589
Dylan Reid0f72ef42017-06-06 15:42:49 -07001590static void set_rlimits_or_die(const struct minijail *j)
1591{
1592 size_t i;
1593
1594 for (i = 0; i < j->rlimit_count; ++i) {
1595 struct rlimit limit;
1596 limit.rlim_cur = j->rlimits[i].cur;
1597 limit.rlim_max = j->rlimits[i].max;
1598 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1599 kill_child_and_die(j, "failed to set rlimit");
1600 }
1601}
1602
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001603static void write_ugid_maps_or_die(const struct minijail *j)
1604{
1605 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1606 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001607 if (j->gidmap && j->flags.disable_setgroups) {
1608 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1609 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001610 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001611 if (ret == -ENOENT) {
1612 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1613 warn("could not disable setgroups(2)");
1614 } else
1615 kill_child_and_die(j, "failed to disable setgroups(2)");
1616 }
1617 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001618 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1619 kill_child_and_die(j, "failed to write gid_map");
1620}
1621
1622static void enter_user_namespace(const struct minijail *j)
1623{
Luis Hector Chavez71323552017-09-05 09:17:22 -07001624 int uid = j->flags.uid ? j->uid : 0;
1625 int gid = j->flags.gid ? j->gid : 0;
1626 if (j->gidmap && setresgid(gid, gid, gid)) {
1627 pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid,
1628 gid);
1629 }
1630 if (j->uidmap && setresuid(uid, uid, uid)) {
1631 pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid,
1632 uid);
1633 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001634}
1635
1636static void parent_setup_complete(int *pipe_fds)
1637{
1638 close(pipe_fds[0]);
1639 close(pipe_fds[1]);
1640}
1641
1642/*
1643 * wait_for_parent_setup: Called by the child process to wait for any
1644 * further parent-side setup to complete before continuing.
1645 */
1646static void wait_for_parent_setup(int *pipe_fds)
1647{
1648 char buf;
1649
1650 close(pipe_fds[1]);
1651
1652 /* Wait for parent to complete setup and close the pipe. */
1653 if (read(pipe_fds[0], &buf, 1) != 0)
1654 die("failed to sync with parent");
1655 close(pipe_fds[0]);
1656}
1657
1658static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001659{
Lutz Justen13807cb2017-01-03 17:11:55 +01001660 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1661 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001662 die("can only do one of inherit, keep, or set supplementary "
1663 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001664 }
1665
Lutz Justen13807cb2017-01-03 17:11:55 +01001666 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001667 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001668 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001669 } else if (j->flags.set_suppl_gids) {
1670 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001671 pdie("setgroups(suppl_gids) failed");
Luis Hector Chavez71323552017-09-05 09:17:22 -07001672 } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001673 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001674 * Only attempt to clear supplementary groups if we are changing
Luis Hector Chavez71323552017-09-05 09:17:22 -07001675 * users or groups, and if the caller did not request to disable
1676 * setgroups (used when entering a user namespace as a
1677 * non-privileged user).
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001678 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001679 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001680 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001681 }
1682
1683 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001684 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001685
1686 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001687 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001688}
1689
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001690static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1691{
1692 const uint64_t one = 1;
1693 unsigned int i;
1694 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1695 if (keep_mask & (one << i))
1696 continue;
1697 if (prctl(PR_CAPBSET_DROP, i))
1698 pdie("could not drop capability from bounding set");
1699 }
1700}
1701
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001702static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001703{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001704 if (!j->flags.use_caps)
1705 return;
1706
Elly Jonese1749eb2011-10-07 13:54:59 -04001707 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001708 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001709 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001710 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001711 unsigned int i;
1712 if (!caps)
1713 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001714 if (cap_clear(caps))
1715 die("can't clear caps");
1716
1717 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001718 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001719 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001720 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001721 flag[0] = i;
1722 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001723 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001724 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001725 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001726 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001727 die("can't add inheritable cap");
1728 }
1729 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001730 die("can't apply initial cleaned capset");
1731
1732 /*
1733 * Instead of dropping bounding set first, do it here in case
1734 * the caller had a more permissive bounding set which could
1735 * have been used above to raise a capability that wasn't already
1736 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1737 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001738 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001739
1740 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001741 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001742 flag[0] = CAP_SETPCAP;
1743 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1744 die("can't clear effective cap");
1745 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1746 die("can't clear permitted cap");
1747 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1748 die("can't clear inheritable cap");
1749 }
1750
1751 if (cap_set_proc(caps))
1752 die("can't apply final cleaned capset");
1753
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001754 /*
1755 * If ambient capabilities are supported, clear all capabilities first,
1756 * then raise the requested ones.
1757 */
1758 if (j->flags.set_ambient_caps) {
1759 if (!cap_ambient_supported()) {
1760 pdie("ambient capabilities not supported");
1761 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001762 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1763 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001764 pdie("can't clear ambient capabilities");
1765 }
1766
1767 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1768 if (!(j->caps & (one << i)))
1769 continue;
1770
1771 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1772 0) != 0) {
1773 pdie("prctl(PR_CAP_AMBIENT, "
1774 "PR_CAP_AMBIENT_RAISE, %u) failed",
1775 i);
1776 }
1777 }
1778 }
1779
Kees Cook323878a2013-02-05 15:35:24 -08001780 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001781}
1782
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001783static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001784{
1785 /*
1786 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1787 * in the kernel source tree for an explanation of the parameters.
1788 */
1789 if (j->flags.no_new_privs) {
1790 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1791 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1792 }
1793
1794 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001795 * Code running with ASan
1796 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1797 * will make system calls not included in the syscall filter policy,
1798 * which will likely crash the program. Skip setting seccomp filter in
1799 * that case.
1800 * 'running_with_asan()' has no inputs and is completely defined at
1801 * build time, so this cannot be used by an attacker to skip setting
1802 * seccomp filter.
1803 */
1804 if (j->flags.seccomp_filter && running_with_asan()) {
1805 warn("running with ASan, not setting seccomp filter");
1806 return;
1807 }
1808
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001809 if (j->flags.seccomp_filter) {
1810 if (j->flags.seccomp_filter_logging) {
1811 /*
1812 * If logging seccomp filter failures,
1813 * install the SIGSYS handler first.
1814 */
1815 if (install_sigsys_handler())
1816 pdie("failed to install SIGSYS handler");
1817 warn("logging seccomp filter failures");
1818 } else if (j->flags.seccomp_filter_tsync) {
1819 /*
1820 * If setting thread sync,
1821 * reset the SIGSYS signal handler so that
1822 * the entire thread group is killed.
1823 */
1824 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1825 pdie("failed to reset SIGSYS disposition");
1826 info("reset SIGSYS disposition");
1827 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001828 }
1829
1830 /*
1831 * Install the syscall filter.
1832 */
1833 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001834 if (j->flags.seccomp_filter_tsync) {
1835 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1836 SECCOMP_FILTER_FLAG_TSYNC,
1837 j->filter_prog)) {
1838 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001839 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001840 } else {
1841 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1842 j->filter_prog)) {
1843 pdie("prctl(seccomp_filter) failed");
1844 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001845 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001846 }
1847}
1848
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001849static pid_t forward_pid = -1;
1850
1851static void forward_signal(__attribute__((unused)) int nr,
1852 __attribute__((unused)) siginfo_t *siginfo,
1853 __attribute__((unused)) void *void_context)
1854{
1855 if (forward_pid != -1) {
1856 kill(forward_pid, nr);
1857 }
1858}
1859
1860static void install_signal_handlers(void)
1861{
1862 struct sigaction act;
1863
1864 memset(&act, 0, sizeof(act));
1865 act.sa_sigaction = &forward_signal;
1866 act.sa_flags = SA_SIGINFO | SA_RESTART;
1867
1868 /* Handle all signals, except SIGCHLD. */
1869 for (int nr = 1; nr < NSIG; nr++) {
1870 /*
1871 * We don't care if we get EINVAL: that just means that we
1872 * can't handle this signal, so let's skip it and continue.
1873 */
1874 sigaction(nr, &act, NULL);
1875 }
1876 /* Reset SIGCHLD's handler. */
1877 signal(SIGCHLD, SIG_DFL);
1878
1879 /* Handle real-time signals. */
1880 for (int nr = SIGRTMIN; nr <= SIGRTMAX; nr++) {
1881 sigaction(nr, &act, NULL);
1882 }
1883}
1884
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001885static const char *lookup_hook_name(minijail_hook_event_t event)
1886{
1887 switch (event) {
1888 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
1889 return "pre-drop-caps";
1890 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
1891 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001892 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
1893 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001894 case MINIJAIL_HOOK_EVENT_MAX:
1895 /*
1896 * Adding this in favor of a default case to force the
1897 * compiler to error out if a new enum value is added.
1898 */
1899 break;
1900 }
1901 return "unknown";
1902}
1903
1904static void run_hooks_or_die(const struct minijail *j,
1905 minijail_hook_event_t event)
1906{
1907 int rc;
1908 int hook_index = 0;
1909 for (struct hook *c = j->hooks_head; c; c = c->next) {
1910 if (c->event != event)
1911 continue;
1912 rc = c->hook(c->payload);
1913 if (rc != 0) {
1914 errno = -rc;
1915 pdie("%s hook (index %d) failed",
1916 lookup_hook_name(event), hook_index);
1917 }
1918 /* Only increase the index within the same hook event type. */
1919 ++hook_index;
1920 }
1921}
1922
Will Drewry6ac91122011-10-21 16:38:58 -05001923void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001924{
Dylan Reidf682d472015-09-17 21:39:07 -07001925 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001926 * If we're dropping caps, get the last valid cap from /proc now,
1927 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001928 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001929 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001930 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001931 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001932
Elly Jonese1749eb2011-10-07 13:54:59 -04001933 if (j->flags.pids)
1934 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001935 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001936
Lutz Justen13807cb2017-01-03 17:11:55 +01001937 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001938 die("cannot inherit supplementary groups without setting a "
1939 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001940
Elly Jonesdd3e8512012-01-23 15:13:38 -05001941 /*
1942 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001943 * so we don't even try. If any of our operations fail, we abort() the
1944 * entire process.
1945 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001946 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001947 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001948
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001949 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001950 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001951 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001952 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05001953 * By default, remount all filesystems as private, unless
1954 * - Passed a specific remount mode, in which case remount with that,
1955 * - Asked not to remount at all, in which case skip the mount(2) call.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001956 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1957 */
Mike Frysinger785b1c32018-02-23 15:47:24 -05001958 if (j->remount_mode) {
1959 if (mount(NULL, "/", NULL, MS_REC | j->remount_mode, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001960 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1961 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001962 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001963 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001964
Dylan Reidf7942472015-11-18 17:55:26 -08001965 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001966 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001967 }
1968
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001969 if (j->flags.uts) {
1970 if (unshare(CLONE_NEWUTS))
1971 pdie("unshare(CLONE_NEWUTS) failed");
1972
1973 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
1974 pdie("sethostname(%s) failed", j->hostname);
1975 }
1976
Dylan Reid1102f5a2015-09-15 11:52:20 -07001977 if (j->flags.enter_net) {
1978 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001979 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001980 } else if (j->flags.net) {
1981 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001982 pdie("unshare(CLONE_NEWNET) failed");
1983 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001984 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001985
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001986 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001987 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001988
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08001989 if (j->flags.new_session_keyring) {
1990 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
1991 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
1992 }
1993
Mike Frysingerac08a682017-10-10 02:04:50 -04001994 /* We have to process all the mounts before we chroot/pivot_root. */
1995 process_mounts_or_die(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001996
Mike Frysingerac08a682017-10-10 02:04:50 -04001997 if (j->flags.chroot && enter_chroot(j))
Mike Frysinger33ffef32017-01-13 19:53:19 -05001998 pdie("chroot");
Mike Frysinger33ffef32017-01-13 19:53:19 -05001999
Mike Frysingerac08a682017-10-10 02:04:50 -04002000 if (j->flags.pivot_root && enter_pivot_root(j))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08002001 pdie("pivot_root");
2002
Martin Pelikánab9eb442017-01-25 11:53:58 +11002003 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07002004 pdie("mount_tmp");
2005
Dylan Reid791f5772015-09-14 20:02:42 -07002006 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04002007 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04002008
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002009 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
2010
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002011 /*
2012 * If we're only dropping capabilities from the bounding set, but not
2013 * from the thread's (permitted|inheritable|effective) sets, do it now.
2014 */
2015 if (j->flags.capbset_drop) {
2016 drop_capbset(j->cap_bset, last_valid_cap);
2017 }
2018
2019 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002020 /*
2021 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04002022 * capability to change uids, our attempt to use setuid()
2023 * below will fail. Hang on to root caps across setuid(), then
2024 * lock securebits.
2025 */
2026 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002027 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002028
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -07002029 if (lock_securebits(j->securebits_skip_mask) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002030 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002031 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002032 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002033
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002034 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002035 /*
2036 * If we're setting no_new_privs, we can drop privileges
2037 * before setting seccomp filter. This way filter policies
2038 * don't need to allow privilege-dropping syscalls.
2039 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002040 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002041 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002042 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002043 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002044 /*
2045 * If we're not setting no_new_privs,
2046 * we need to set seccomp filter *before* dropping privileges.
2047 * WARNING: this means that filter policies *must* allow
2048 * setgroups()/setresgid()/setresuid() for dropping root and
2049 * capget()/capset()/prctl() for dropping caps.
2050 */
2051 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002052 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002053 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04002054 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002055
Elly Jonesdd3e8512012-01-23 15:13:38 -05002056 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08002057 * Select the specified alternate syscall table. The table must not
2058 * block prctl(2) if we're using seccomp as well.
2059 */
2060 if (j->flags.alt_syscall) {
2061 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002062 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08002063 }
2064
2065 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05002066 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04002067 * privilege-dropping syscalls :)
2068 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002069 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04002070 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002071 warn("seccomp not supported");
2072 return;
2073 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002074 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002075 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002076}
2077
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002078/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04002079static int init_exitstatus = 0;
2080
Will Drewry6ac91122011-10-21 16:38:58 -05002081void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04002082{
2083 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04002084}
2085
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002086void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04002087{
2088 pid_t pid;
2089 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002090 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002091 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002092 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002093 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002094 /*
2095 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04002096 * left inside our pid namespace or we get a signal.
2097 */
2098 if (pid == rootpid)
2099 init_exitstatus = status;
2100 }
2101 if (!WIFEXITED(init_exitstatus))
2102 _exit(MINIJAIL_ERR_INIT);
2103 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04002104}
2105
Will Drewry6ac91122011-10-21 16:38:58 -05002106int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002107{
2108 size_t sz = 0;
2109 size_t bytes = read(fd, &sz, sizeof(sz));
2110 char *buf;
2111 int r;
2112 if (sizeof(sz) != bytes)
2113 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002114 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04002115 return -E2BIG;
2116 buf = malloc(sz);
2117 if (!buf)
2118 return -ENOMEM;
2119 bytes = read(fd, buf, sz);
2120 if (bytes != sz) {
2121 free(buf);
2122 return -EINVAL;
2123 }
2124 r = minijail_unmarshal(j, buf, sz);
2125 free(buf);
2126 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002127}
2128
Will Drewry6ac91122011-10-21 16:38:58 -05002129int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04002130{
2131 char *buf;
2132 size_t sz = minijail_size(j);
2133 ssize_t written;
2134 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04002135
Elly Jonese1749eb2011-10-07 13:54:59 -04002136 if (!sz)
2137 return -EINVAL;
2138 buf = malloc(sz);
2139 r = minijail_marshal(j, buf, sz);
2140 if (r) {
2141 free(buf);
2142 return r;
2143 }
2144 /* Sends [size][minijail]. */
2145 written = write(fd, &sz, sizeof(sz));
2146 if (written != sizeof(sz)) {
2147 free(buf);
2148 return -EFAULT;
2149 }
2150 written = write(fd, buf, sz);
2151 if (written < 0 || (size_t) written != sz) {
2152 free(buf);
2153 return -EFAULT;
2154 }
2155 free(buf);
2156 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002157}
Elly Jonescd7a9042011-07-22 13:56:51 -04002158
Will Drewry6ac91122011-10-21 16:38:58 -05002159int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04002160{
Daniel Erat5b7a3182015-08-19 16:06:22 -06002161#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002162 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002163 return 0;
2164#else
Elly Jonese1749eb2011-10-07 13:54:59 -04002165 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
2166 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
2167 if (!newenv)
2168 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04002169
Elly Jonese1749eb2011-10-07 13:54:59 -04002170 /* Only insert a separating space if we have something to separate... */
2171 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
2172 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04002173
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002174 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002175 setenv(kLdPreloadEnvVar, newenv, 1);
2176 free(newenv);
2177 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002178#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04002179}
2180
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002181static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04002182{
2183 int r = pipe(fds);
2184 char fd_buf[11];
2185 if (r)
2186 return r;
2187 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2188 if (r <= 0)
2189 return -EINVAL;
2190 setenv(kFdEnvVar, fd_buf, 1);
2191 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05002192}
2193
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002194static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002195{
2196 const char *kFdPath = "/proc/self/fd";
2197
2198 DIR *d = opendir(kFdPath);
2199 struct dirent *dir_entry;
2200
2201 if (d == NULL)
2202 return -1;
2203 int dir_fd = dirfd(d);
2204 while ((dir_entry = readdir(d)) != NULL) {
2205 size_t i;
2206 char *end;
2207 bool should_close = true;
2208 const int fd = strtol(dir_entry->d_name, &end, 10);
2209
2210 if ((*end) != '\0') {
2211 continue;
2212 }
2213 /*
2214 * We might have set up some pipes that we want to share with
2215 * the parent process, and should not be closed.
2216 */
2217 for (i = 0; i < size; ++i) {
2218 if (fd == inheritable_fds[i]) {
2219 should_close = false;
2220 break;
2221 }
2222 }
2223 /* Also avoid closing the directory fd. */
2224 if (should_close && fd != dir_fd)
2225 close(fd);
2226 }
2227 closedir(d);
2228 return 0;
2229}
2230
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002231static int redirect_fds(struct minijail *j)
2232{
2233 size_t i, i2;
2234 int closeable;
2235 for (i = 0; i < j->preserved_fd_count; i++) {
2236 if (dup2(j->preserved_fds[i].parent_fd,
2237 j->preserved_fds[i].child_fd) == -1) {
2238 return -1;
2239 }
2240 }
2241 /*
2242 * After all fds have been duped, we are now free to close all parent
2243 * fds that are *not* child fds.
2244 */
2245 for (i = 0; i < j->preserved_fd_count; i++) {
2246 closeable = true;
2247 for (i2 = 0; i2 < j->preserved_fd_count; i2++) {
2248 closeable &= j->preserved_fds[i].parent_fd !=
2249 j->preserved_fds[i2].child_fd;
2250 }
2251 if (closeable)
2252 close(j->preserved_fds[i].parent_fd);
2253 }
2254 return 0;
2255}
2256
Dylan Reidacfb8be2017-08-25 12:56:51 -07002257/*
2258 * Structure that specifies how to start a minijail.
2259 *
Dylan Reid0412dcc2017-08-24 11:33:15 -07002260 * filename - The program to exec in the child. Required if `exec_in_child` = 1.
2261 * argv - Arguments for the child program. Required if `exec_in_child` = 1.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002262 * use_preload - If true use LD_PRELOAD.
Dylan Reid0412dcc2017-08-24 11:33:15 -07002263 * exec_in_child - If true, run `filename`. Otherwise, the child will return to
2264 * the caller.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002265 */
2266struct minijail_run_config {
2267 const char *filename;
2268 char *const *argv;
2269 int use_preload;
Dylan Reid0412dcc2017-08-24 11:33:15 -07002270 int exec_in_child;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002271};
2272
2273/*
2274 * Set of pointers to fill with values from minijail_run.
2275 * All arguments are allowed to be NULL if unused.
2276 *
2277 * pstdin_fd - Filled with stdin pipe if non-NULL.
2278 * pstdout_fd - Filled with stdout pipe if non-NULL.
2279 * pstderr_fd - Filled with stderr pipe if non-NULL.
2280 * pchild_pid - Filled with the pid of the child process if non-NULL.
2281 */
2282struct minijail_run_status {
2283 int *pstdin_fd;
2284 int *pstdout_fd;
2285 int *pstderr_fd;
2286 pid_t *pchild_pid;
2287};
2288
Dylan Reid18c49c82017-08-25 14:52:27 -07002289static int minijail_run_internal(struct minijail *j,
2290 const struct minijail_run_config *config,
2291 struct minijail_run_status *status_out);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002292
Will Drewry6ac91122011-10-21 16:38:58 -05002293int API minijail_run(struct minijail *j, const char *filename,
2294 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002295{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002296 struct minijail_run_config config = {
2297 .filename = filename,
2298 .argv = argv,
2299 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002300 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002301 };
2302 struct minijail_run_status status = {};
2303 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002304}
2305
2306int API minijail_run_pid(struct minijail *j, const char *filename,
2307 char *const argv[], pid_t *pchild_pid)
2308{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002309 struct minijail_run_config config = {
2310 .filename = filename,
2311 .argv = argv,
2312 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002313 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002314 };
2315 struct minijail_run_status status = {
2316 .pchild_pid = pchild_pid,
2317 };
2318 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002319}
2320
2321int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002322 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002323{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002324 struct minijail_run_config config = {
2325 .filename = filename,
2326 .argv = argv,
2327 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002328 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002329 };
2330 struct minijail_run_status status = {
2331 .pstdin_fd = pstdin_fd,
2332 };
2333 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002334}
2335
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002336int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002337 char *const argv[], pid_t *pchild_pid,
2338 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002339{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002340 struct minijail_run_config config = {
2341 .filename = filename,
2342 .argv = argv,
2343 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002344 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002345 };
2346 struct minijail_run_status status = {
2347 .pstdin_fd = pstdin_fd,
2348 .pstdout_fd = pstdout_fd,
2349 .pstderr_fd = pstderr_fd,
2350 .pchild_pid = pchild_pid,
2351 };
2352 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002353}
2354
2355int API minijail_run_no_preload(struct minijail *j, const char *filename,
2356 char *const argv[])
2357{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002358 struct minijail_run_config config = {
2359 .filename = filename,
2360 .argv = argv,
2361 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002362 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002363 };
2364 struct minijail_run_status status = {};
2365 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002366}
2367
Samuel Tan63187f42015-10-16 13:01:53 -07002368int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002369 const char *filename,
2370 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002371 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002372 int *pstdin_fd,
2373 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002374 int *pstderr_fd)
2375{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002376 struct minijail_run_config config = {
2377 .filename = filename,
2378 .argv = argv,
2379 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002380 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002381 };
2382 struct minijail_run_status status = {
2383 .pstdin_fd = pstdin_fd,
2384 .pstdout_fd = pstdout_fd,
2385 .pstderr_fd = pstderr_fd,
2386 .pchild_pid = pchild_pid,
2387 };
2388 return minijail_run_internal(j, &config, &status);
Samuel Tan63187f42015-10-16 13:01:53 -07002389}
2390
Dylan Reid0412dcc2017-08-24 11:33:15 -07002391pid_t API minijail_fork(struct minijail *j)
2392{
2393 struct minijail_run_config config = {};
2394 struct minijail_run_status status = {};
2395 return minijail_run_internal(j, &config, &status);
2396}
2397
Dylan Reid18c49c82017-08-25 14:52:27 -07002398static int minijail_run_internal(struct minijail *j,
2399 const struct minijail_run_config *config,
2400 struct minijail_run_status *status_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002401{
Elly Jonese1749eb2011-10-07 13:54:59 -04002402 char *oldenv, *oldenv_copy = NULL;
2403 pid_t child_pid;
2404 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002405 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002406 int stdout_fds[2];
2407 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08002408 int child_sync_pipe_fds[2];
2409 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002410 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002411 /* We need to remember this across the minijail_preexec() call. */
2412 int pid_namespace = j->flags.pids;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -07002413 /*
2414 * Create an init process if we are entering a pid namespace, unless the
2415 * user has explicitly opted out by calling minijail_run_as_init().
2416 */
2417 int do_init = j->flags.do_init && !j->flags.run_as_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002418 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002419
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002420 if (use_preload) {
Dylan Reid0412dcc2017-08-24 11:33:15 -07002421 if (j->hooks_head != NULL)
2422 die("Minijail hooks are not supported with LD_PRELOAD");
2423 if (!config->exec_in_child)
2424 die("minijail_fork is not supported with LD_PRELOAD");
2425
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002426 oldenv = getenv(kLdPreloadEnvVar);
2427 if (oldenv) {
2428 oldenv_copy = strdup(oldenv);
2429 if (!oldenv_copy)
2430 return -ENOMEM;
2431 }
2432
2433 if (setup_preload())
2434 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002435 }
Will Drewryf89aef52011-09-16 16:48:57 -05002436
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002437 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002438 if (j->flags.use_caps && j->caps != 0 &&
2439 !j->flags.set_ambient_caps) {
2440 die("non-empty, non-ambient capabilities are not "
2441 "supported without LD_PRELOAD");
2442 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002443 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002444
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002445 if (use_preload) {
2446 /*
2447 * Before we fork(2) and execve(2) the child process, we need
2448 * to open a pipe(2) to send the minijail configuration over.
2449 */
2450 if (setup_pipe(pipe_fds))
2451 return -EFAULT;
2452 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002453
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002454 /*
2455 * If we want to write to the child process' standard input,
2456 * create the pipe(2) now.
2457 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002458 if (status_out->pstdin_fd) {
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002459 if (pipe(stdin_fds))
2460 return -EFAULT;
2461 }
2462
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002463 /*
2464 * If we want to read from the child process' standard output,
2465 * create the pipe(2) now.
2466 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002467 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002468 if (pipe(stdout_fds))
2469 return -EFAULT;
2470 }
2471
2472 /*
2473 * If we want to read from the child process' standard error,
2474 * create the pipe(2) now.
2475 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002476 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002477 if (pipe(stderr_fds))
2478 return -EFAULT;
2479 }
2480
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002481 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002482 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002483 * or if we need to add the child process to cgroups, create the pipe(2)
2484 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002485 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002486 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002487 sync_child = 1;
2488 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002489 return -EFAULT;
2490 }
2491
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002492 /*
2493 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002494 *
2495 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2496 *
2497 * In multithreaded programs, there are a bunch of locks inside libc,
2498 * some of which may be held by other threads at the time that we call
2499 * minijail_run_pid(). If we call fork(), glibc does its level best to
2500 * ensure that we hold all of these locks before it calls clone()
2501 * internally and drop them after clone() returns, but when we call
2502 * sys_clone(2) directly, all that gets bypassed and we end up with a
2503 * child address space where some of libc's important locks are held by
2504 * other threads (which did not get cloned, and hence will never release
2505 * those locks). This is okay so long as we call exec() immediately
2506 * after, but a bunch of seemingly-innocent libc functions like setenv()
2507 * take locks.
2508 *
2509 * Hence, only call sys_clone() if we need to, in order to get at pid
2510 * namespacing. If we follow this path, the child's address space might
2511 * have broken locks; you may only call functions that do not acquire
2512 * any locks.
2513 *
2514 * Unfortunately, fork() acquires every lock it can get its hands on, as
2515 * previously detailed, so this function is highly likely to deadlock
2516 * later on (see "deadlock here") if we're multithreaded.
2517 *
2518 * We might hack around this by having the clone()d child (init of the
2519 * pid namespace) return directly, rather than leaving the clone()d
2520 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002521 * its fork()ed child return in turn), but that process would be
2522 * crippled with its libc locks potentially broken. We might try
2523 * fork()ing in the parent before we clone() to ensure that we own all
2524 * the locks, but then we have to have the forked child hanging around
2525 * consuming resources (and possibly having file descriptors / shared
2526 * memory regions / etc attached). We'd need to keep the child around to
2527 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002528 *
2529 * TODO(ellyjones): figure out if the "forked child hanging around"
2530 * problem is fixable or not. It would be nice if we worked in this
2531 * case.
2532 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002533 if (pid_namespace) {
2534 int clone_flags = CLONE_NEWPID | SIGCHLD;
2535 if (j->flags.userns)
2536 clone_flags |= CLONE_NEWUSER;
2537 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002538 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002539 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002540 }
Elly Jones761b7412012-06-13 15:49:52 -04002541
Elly Jonese1749eb2011-10-07 13:54:59 -04002542 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002543 if (use_preload) {
2544 free(oldenv_copy);
2545 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002546 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002547 }
Will Drewryf89aef52011-09-16 16:48:57 -05002548
Elly Jonese1749eb2011-10-07 13:54:59 -04002549 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002550 if (use_preload) {
2551 /* Restore parent's LD_PRELOAD. */
2552 if (oldenv_copy) {
2553 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2554 free(oldenv_copy);
2555 } else {
2556 unsetenv(kLdPreloadEnvVar);
2557 }
2558 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002559 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002560
Elly Jonese1749eb2011-10-07 13:54:59 -04002561 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002562
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002563 if (j->flags.forward_signals) {
2564 forward_pid = child_pid;
2565 install_signal_handlers();
2566 }
2567
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002568 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002569 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002570
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002571 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002572 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002573
Dylan Reid0f72ef42017-06-06 15:42:49 -07002574 if (j->rlimit_count)
2575 set_rlimits_or_die(j);
2576
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002577 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002578 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002579
2580 if (sync_child)
2581 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002582
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002583 if (use_preload) {
2584 /* Send marshalled minijail. */
2585 close(pipe_fds[0]); /* read endpoint */
2586 ret = minijail_to_fd(j, pipe_fds[1]);
2587 close(pipe_fds[1]); /* write endpoint */
2588 if (ret) {
2589 kill(j->initpid, SIGKILL);
2590 die("failed to send marshalled minijail");
2591 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002592 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002593
Dylan Reidacfb8be2017-08-25 12:56:51 -07002594 if (status_out->pchild_pid)
2595 *status_out->pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002596
2597 /*
2598 * If we want to write to the child process' standard input,
2599 * set up the write end of the pipe.
2600 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002601 if (status_out->pstdin_fd)
2602 *status_out->pstdin_fd =
2603 setup_pipe_end(stdin_fds, 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002604
2605 /*
2606 * If we want to read from the child process' standard output,
2607 * set up the read end of the pipe.
2608 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002609 if (status_out->pstdout_fd)
2610 *status_out->pstdout_fd =
2611 setup_pipe_end(stdout_fds, 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002612
2613 /*
2614 * If we want to read from the child process' standard error,
2615 * set up the read end of the pipe.
2616 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002617 if (status_out->pstderr_fd)
2618 *status_out->pstderr_fd =
2619 setup_pipe_end(stderr_fds, 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002620
Dylan Reid0412dcc2017-08-24 11:33:15 -07002621 /*
2622 * If forking return the child pid, in the normal exec case
2623 * return 0 for success.
2624 */
2625 if (!config->exec_in_child)
2626 return child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04002627 return 0;
2628 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002629 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002630 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002631
Peter Qiu2860c462015-12-16 15:13:06 -08002632 if (j->flags.reset_signal_mask) {
2633 sigset_t signal_mask;
2634 if (sigemptyset(&signal_mask) != 0)
2635 pdie("sigemptyset failed");
2636 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2637 pdie("sigprocmask failed");
2638 }
2639
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002640 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002641 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002642 int inheritable_fds[kMaxInheritableFdsSize];
2643 size_t size = 0;
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002644 size_t i;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002645 if (use_preload) {
2646 inheritable_fds[size++] = pipe_fds[0];
2647 inheritable_fds[size++] = pipe_fds[1];
2648 }
2649 if (sync_child) {
2650 inheritable_fds[size++] = child_sync_pipe_fds[0];
2651 inheritable_fds[size++] = child_sync_pipe_fds[1];
2652 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002653 if (status_out->pstdin_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002654 inheritable_fds[size++] = stdin_fds[0];
2655 inheritable_fds[size++] = stdin_fds[1];
2656 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002657 if (status_out->pstdout_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002658 inheritable_fds[size++] = stdout_fds[0];
2659 inheritable_fds[size++] = stdout_fds[1];
2660 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002661 if (status_out->pstderr_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002662 inheritable_fds[size++] = stderr_fds[0];
2663 inheritable_fds[size++] = stderr_fds[1];
2664 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002665 for (i = 0; i < j->preserved_fd_count; i++) {
2666 /*
2667 * Preserve all parent_fds. They will be dup2(2)-ed in
2668 * the child later.
2669 */
2670 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
2671 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002672
2673 if (close_open_fds(inheritable_fds, size) < 0)
2674 die("failed to close open file descriptors");
2675 }
2676
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002677 if (redirect_fds(j))
2678 die("failed to set up fd redirections");
2679
Dylan Reidce5b55e2016-01-13 11:04:16 -08002680 if (sync_child)
2681 wait_for_parent_setup(child_sync_pipe_fds);
2682
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002683 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002684 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002685
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002686 /*
2687 * If we want to write to the jailed process' standard input,
2688 * set up the read end of the pipe.
2689 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002690 if (status_out->pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002691 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2692 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002693 die("failed to set up stdin pipe");
2694 }
2695
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002696 /*
2697 * If we want to read from the jailed process' standard output,
2698 * set up the write end of the pipe.
2699 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002700 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002701 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2702 STDOUT_FILENO) < 0)
2703 die("failed to set up stdout pipe");
2704 }
2705
2706 /*
2707 * If we want to read from the jailed process' standard error,
2708 * set up the write end of the pipe.
2709 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002710 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002711 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2712 STDERR_FILENO) < 0)
2713 die("failed to set up stderr pipe");
2714 }
2715
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002716 /*
2717 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2718 * This prevents the jailed process from using the TIOCSTI ioctl
2719 * to push characters into the parent process terminal's input buffer,
2720 * therefore escaping the jail.
Stephen Barber5dd5b1b2017-10-16 23:02:39 -07002721 *
2722 * Since it has just forked, the child will not be a process group
2723 * leader, and this call to setsid() should always succeed.
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002724 */
2725 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2726 isatty(STDERR_FILENO)) {
2727 if (setsid() < 0) {
2728 pdie("setsid() failed");
2729 }
2730 }
2731
Dylan Reid791f5772015-09-14 20:02:42 -07002732 /* If running an init program, let it decide when/how to mount /proc. */
2733 if (pid_namespace && !do_init)
2734 j->flags.remount_proc_ro = 0;
2735
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002736 if (use_preload) {
2737 /* Strip out flags that cannot be inherited across execve(2). */
2738 minijail_preexec(j);
2739 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002740 /*
2741 * If not using LD_PRELOAD, do all jailing before execve(2).
2742 * Note that PID namespaces can only be entered on fork(2),
2743 * so that flag is still cleared.
2744 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002745 j->flags.pids = 0;
2746 }
Dylan Reid0412dcc2017-08-24 11:33:15 -07002747
2748 /*
2749 * Jail this process.
2750 * If forking, return.
2751 * If not, execve(2) the target.
2752 */
Elly Jonese1749eb2011-10-07 13:54:59 -04002753 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002754
Dylan Reid0412dcc2017-08-24 11:33:15 -07002755 if (config->exec_in_child && pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002756 /*
2757 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002758 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002759 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002760 * a child to actually run the program. If |do_init == 0|, we
2761 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002762 *
2763 * If we're multithreaded, we'll probably deadlock here. See
2764 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002765 */
2766 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002767 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002768 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002769 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002770 /*
2771 * Best effort. Don't bother checking the return value.
2772 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002773 prctl(PR_SET_NAME, "minijail-init");
2774 init(child_pid); /* Never returns. */
2775 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002776 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002777
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002778 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
2779
Dylan Reid0412dcc2017-08-24 11:33:15 -07002780 if (!config->exec_in_child)
2781 return 0;
2782
Elly Jonesdd3e8512012-01-23 15:13:38 -05002783 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002784 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002785 * calling process
2786 * -> execve()-ing process
2787 * If we are:
2788 * calling process
2789 * -> init()-ing process
2790 * -> execve()-ing process
2791 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002792 ret = execve(config->filename, config->argv, environ);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002793 if (ret == -1) {
Dylan Reidacfb8be2017-08-25 12:56:51 -07002794 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002795 }
2796 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002797}
2798
Will Drewry6ac91122011-10-21 16:38:58 -05002799int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002800{
2801 int st;
2802 if (kill(j->initpid, SIGTERM))
2803 return -errno;
2804 if (waitpid(j->initpid, &st, 0) < 0)
2805 return -errno;
2806 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002807}
2808
Will Drewry6ac91122011-10-21 16:38:58 -05002809int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002810{
2811 int st;
2812 if (waitpid(j->initpid, &st, 0) < 0)
2813 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002814
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002815 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002816 int error_status = st;
2817 if (WIFSIGNALED(st)) {
2818 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002819 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002820 j->initpid, signum);
2821 /*
2822 * We return MINIJAIL_ERR_JAIL if the process received
2823 * SIGSYS, which happens when a syscall is blocked by
2824 * seccomp filters.
2825 * If not, we do what bash(1) does:
2826 * $? = 128 + signum
2827 */
2828 if (signum == SIGSYS) {
2829 error_status = MINIJAIL_ERR_JAIL;
2830 } else {
2831 error_status = 128 + signum;
2832 }
2833 }
2834 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002835 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002836
2837 int exit_status = WEXITSTATUS(st);
2838 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002839 info("child process %d exited with status %d",
2840 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002841
2842 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002843}
2844
Will Drewry6ac91122011-10-21 16:38:58 -05002845void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002846{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002847 size_t i;
2848
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002849 if (j->flags.seccomp_filter && j->filter_prog) {
2850 free(j->filter_prog->filter);
2851 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002852 }
Mike Frysingerac08a682017-10-10 02:04:50 -04002853 free_mounts_list(j);
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002854 while (j->hooks_head) {
2855 struct hook *c = j->hooks_head;
2856 j->hooks_head = c->next;
2857 free(c);
2858 }
2859 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002860 if (j->user)
2861 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002862 if (j->suppl_gid_list)
2863 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002864 if (j->chrootdir)
2865 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002866 if (j->pid_file_path)
2867 free(j->pid_file_path);
2868 if (j->uidmap)
2869 free(j->uidmap);
2870 if (j->gidmap)
2871 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002872 if (j->hostname)
2873 free(j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08002874 if (j->alt_syscall_table)
2875 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002876 for (i = 0; i < j->cgroup_count; ++i)
2877 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002878 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002879}
Luis Hector Chavez114a9302017-09-05 20:36:58 -07002880
2881void API minijail_log_to_fd(int fd, int min_priority)
2882{
2883 init_logging(LOG_TO_FD, fd, min_priority);
2884}