blob: 900acd53a6ba13755c1d3c56cec5df2a09ae2764 [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;
129 int skip_remount_private : 1;
130 int pids : 1;
131 int ipc : 1;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400132 int uts : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700133 int net : 1;
134 int enter_net : 1;
135 int ns_cgroups : 1;
136 int userns : 1;
137 int disable_setgroups : 1;
138 int seccomp : 1;
139 int remount_proc_ro : 1;
140 int no_new_privs : 1;
141 int seccomp_filter : 1;
142 int seccomp_filter_tsync : 1;
143 int seccomp_filter_logging : 1;
144 int chroot : 1;
145 int pivot_root : 1;
Mike Frysinger33ffef32017-01-13 19:53:19 -0500146 int mount_dev : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700147 int mount_tmp : 1;
148 int do_init : 1;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700149 int run_as_init : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700150 int pid_file : 1;
151 int cgroups : 1;
152 int alt_syscall : 1;
153 int reset_signal_mask : 1;
154 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800155 int new_session_keyring : 1;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400156 int forward_signals : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400157 } flags;
158 uid_t uid;
159 gid_t gid;
160 gid_t usergid;
161 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800162 size_t suppl_gid_count;
163 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400164 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800165 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400166 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700167 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700168 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400169 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800170 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800171 char *uidmap;
172 char *gidmap;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400173 char *hostname;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800174 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800175 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800176 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700177 struct mountpoint *mounts_head;
178 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800179 size_t mounts_count;
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;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900219 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700220 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700221 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800222 j->flags.do_init = 0;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700223 j->flags.run_as_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800224 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800225 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400226 j->flags.forward_signals = 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;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800237 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700238 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800239 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700240 if (j->user)
241 free(j->user);
242 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800243 if (j->suppl_gid_list)
244 free(j->suppl_gid_list);
245 j->suppl_gid_list = NULL;
Mike Frysingerac08a682017-10-10 02:04:50 -0400246 free_mounts_list(j);
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700247 memset(&j->flags, 0, sizeof(j->flags));
248 /* Now restore anything we meant to keep. */
249 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700250 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800251 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700252 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800253 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700254 /* Note, |pids| will already have been used before this call. */
255}
256
257/* Minijail API. */
258
Will Drewry6ac91122011-10-21 16:38:58 -0500259struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400260{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400261 return calloc(1, sizeof(struct minijail));
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
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800444void API minijail_skip_remount_private(struct minijail *j)
445{
446 j->flags.skip_remount_private = 1;
447}
448
Will Drewry6ac91122011-10-21 16:38:58 -0500449void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400450{
Elly Jonese58176c2012-01-23 11:46:17 -0500451 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700452 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400453 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800454 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400455}
456
Dylan Reidf7942472015-11-18 17:55:26 -0800457void API minijail_namespace_ipc(struct minijail *j)
458{
459 j->flags.ipc = 1;
460}
461
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400462void API minijail_namespace_uts(struct minijail *j)
463{
464 j->flags.uts = 1;
465}
466
467int API minijail_namespace_set_hostname(struct minijail *j, const char *name)
468{
469 if (j->hostname)
470 return -EINVAL;
471 minijail_namespace_uts(j);
472 j->hostname = strdup(name);
473 if (!j->hostname)
474 return -ENOMEM;
475 return 0;
476}
477
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400478void API minijail_namespace_net(struct minijail *j)
479{
480 j->flags.net = 1;
481}
482
Dylan Reid1102f5a2015-09-15 11:52:20 -0700483void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
484{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800485 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700486 if (ns_fd < 0) {
487 pdie("failed to open namespace '%s'", ns_path);
488 }
489 j->netns_fd = ns_fd;
490 j->flags.enter_net = 1;
491}
492
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700493void API minijail_namespace_cgroups(struct minijail *j)
494{
495 j->flags.ns_cgroups = 1;
496}
497
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700498void API minijail_close_open_fds(struct minijail *j)
499{
500 j->flags.close_open_fds = 1;
501}
502
Dylan Reid791f5772015-09-14 20:02:42 -0700503void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400504{
505 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700506 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400507}
508
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800509void API minijail_namespace_user(struct minijail *j)
510{
511 j->flags.userns = 1;
512}
513
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400514void API minijail_namespace_user_disable_setgroups(struct minijail *j)
515{
516 j->flags.disable_setgroups = 1;
517}
518
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800519int API minijail_uidmap(struct minijail *j, const char *uidmap)
520{
521 j->uidmap = strdup(uidmap);
522 if (!j->uidmap)
523 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800524 char *ch;
525 for (ch = j->uidmap; *ch; ch++) {
526 if (*ch == ',')
527 *ch = '\n';
528 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800529 return 0;
530}
531
532int API minijail_gidmap(struct minijail *j, const char *gidmap)
533{
534 j->gidmap = strdup(gidmap);
535 if (!j->gidmap)
536 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800537 char *ch;
538 for (ch = j->gidmap; *ch; ch++) {
539 if (*ch == ',')
540 *ch = '\n';
541 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800542 return 0;
543}
544
Will Drewry6ac91122011-10-21 16:38:58 -0500545void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400546{
Lutz Justen13807cb2017-01-03 17:11:55 +0100547 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400548}
549
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800550void API minijail_run_as_init(struct minijail *j)
551{
552 /*
553 * Since the jailed program will become 'init' in the new PID namespace,
554 * Minijail does not need to fork an 'init' process.
555 */
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700556 j->flags.run_as_init = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800557}
558
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700559int API minijail_enter_chroot(struct minijail *j, const char *dir)
560{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400561 if (j->chrootdir)
562 return -EINVAL;
563 j->chrootdir = strdup(dir);
564 if (!j->chrootdir)
565 return -ENOMEM;
566 j->flags.chroot = 1;
567 return 0;
568}
569
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800570int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
571{
572 if (j->chrootdir)
573 return -EINVAL;
574 j->chrootdir = strdup(dir);
575 if (!j->chrootdir)
576 return -ENOMEM;
577 j->flags.pivot_root = 1;
578 return 0;
579}
580
Dylan Reida14e08d2015-10-22 21:05:29 -0700581char API *minijail_get_original_path(struct minijail *j,
582 const char *path_inside_chroot)
583{
Dylan Reid648b2202015-10-23 00:50:00 -0700584 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700585
Dylan Reid648b2202015-10-23 00:50:00 -0700586 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700587 while (b) {
588 /*
589 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700590 * mount, then the original path is exactly the source of
591 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700592 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700593 * mount source = /some/path/exe, mount dest =
594 * /chroot/path/exe Then when getting the original path of
595 * "/chroot/path/exe", the source of that mount,
596 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700597 */
598 if (!strcmp(b->dest, path_inside_chroot))
599 return strdup(b->src);
600
601 /*
602 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700603 * mount, take the suffix of the chroot path relative to the
604 * mount destination path, and append it to the mount source
605 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700606 */
607 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
608 const char *relative_path =
609 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400610 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700611 }
612 b = b->next;
613 }
614
615 /* If there is a chroot path, append |path_inside_chroot| to that. */
616 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400617 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700618
619 /* No chroot, so the path outside is the same as it is inside. */
620 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700621}
622
Martin Pelikánab9eb442017-01-25 11:53:58 +1100623size_t minijail_get_tmpfs_size(const struct minijail *j)
624{
625 return j->tmpfs_size;
626}
627
Mike Frysinger33ffef32017-01-13 19:53:19 -0500628void API minijail_mount_dev(struct minijail *j)
629{
630 j->flags.mount_dev = 1;
631}
632
Lee Campbell11af0622014-05-22 12:36:04 -0700633void API minijail_mount_tmp(struct minijail *j)
634{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100635 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
636}
637
638void API minijail_mount_tmp_size(struct minijail *j, size_t size)
639{
640 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700641 j->flags.mount_tmp = 1;
642}
643
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800644int API minijail_write_pid_file(struct minijail *j, const char *path)
645{
646 j->pid_file_path = strdup(path);
647 if (!j->pid_file_path)
648 return -ENOMEM;
649 j->flags.pid_file = 1;
650 return 0;
651}
652
Dylan Reid605ce7f2016-01-19 19:21:00 -0800653int API minijail_add_to_cgroup(struct minijail *j, const char *path)
654{
655 if (j->cgroup_count >= MAX_CGROUPS)
656 return -ENOMEM;
657 j->cgroups[j->cgroup_count] = strdup(path);
658 if (!j->cgroups[j->cgroup_count])
659 return -ENOMEM;
660 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800661 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800662 return 0;
663}
664
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800665int API minijail_rlimit(struct minijail *j, int type, rlim_t cur, rlim_t max)
Dylan Reid0f72ef42017-06-06 15:42:49 -0700666{
667 size_t i;
668
669 if (j->rlimit_count >= MAX_RLIMITS)
670 return -ENOMEM;
671 /* It's an error if the caller sets the same rlimit multiple times. */
672 for (i = 0; i < j->rlimit_count; i++) {
673 if (j->rlimits[i].type == type)
674 return -EEXIST;
675 }
676
677 j->rlimits[j->rlimit_count].type = type;
678 j->rlimits[j->rlimit_count].cur = cur;
679 j->rlimits[j->rlimit_count].max = max;
680 j->rlimit_count++;
681 return 0;
682}
683
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400684int API minijail_forward_signals(struct minijail *j)
685{
686 j->flags.forward_signals = 1;
687 return 0;
688}
689
Dylan Reid81e23972016-05-18 14:06:35 -0700690int API minijail_mount_with_data(struct minijail *j, const char *src,
691 const char *dest, const char *type,
692 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700693{
Dylan Reid648b2202015-10-23 00:50:00 -0700694 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400695
696 if (*dest != '/')
697 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700698 m = calloc(1, sizeof(*m));
699 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400700 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700701 m->dest = strdup(dest);
702 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400703 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700704 m->src = strdup(src);
705 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400706 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700707 m->type = strdup(type);
708 if (!m->type)
709 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700710 if (data) {
711 m->data = strdup(data);
712 if (!m->data)
713 goto error;
714 m->has_data = 1;
715 }
Dylan Reid648b2202015-10-23 00:50:00 -0700716 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400717
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800718 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400719
Elly Jonesdd3e8512012-01-23 15:13:38 -0500720 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700721 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400722 * containing vfs namespace.
723 */
724 minijail_namespace_vfs(j);
725
Dylan Reid648b2202015-10-23 00:50:00 -0700726 if (j->mounts_tail)
727 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400728 else
Dylan Reid648b2202015-10-23 00:50:00 -0700729 j->mounts_head = m;
730 j->mounts_tail = m;
731 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400732
733 return 0;
734
735error:
Dylan Reid81e23972016-05-18 14:06:35 -0700736 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700737 free(m->src);
738 free(m->dest);
739 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400740 return -ENOMEM;
741}
742
Dylan Reid81e23972016-05-18 14:06:35 -0700743int API minijail_mount(struct minijail *j, const char *src, const char *dest,
744 const char *type, unsigned long flags)
745{
746 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
747}
748
Dylan Reid648b2202015-10-23 00:50:00 -0700749int API minijail_bind(struct minijail *j, const char *src, const char *dest,
750 int writeable)
751{
752 unsigned long flags = MS_BIND;
753
754 if (!writeable)
755 flags |= MS_RDONLY;
756
757 return minijail_mount(j, src, dest, "", flags);
758}
759
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700760int API minijail_add_hook(struct minijail *j, minijail_hook_t hook,
761 void *payload, minijail_hook_event_t event)
762{
763 struct hook *c;
764
765 if (hook == NULL)
766 return -EINVAL;
767 if (event >= MINIJAIL_HOOK_EVENT_MAX)
768 return -EINVAL;
769 c = calloc(1, sizeof(*c));
770 if (!c)
771 return -ENOMEM;
772
773 c->hook = hook;
774 c->payload = payload;
775 c->event = event;
776
777 if (j->hooks_tail)
778 j->hooks_tail->next = c;
779 else
780 j->hooks_head = c;
781 j->hooks_tail = c;
782
783 return 0;
784}
785
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700786int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd)
787{
788 if (parent_fd < 0 || child_fd < 0)
789 return -EINVAL;
790 if (j->preserved_fd_count >= MAX_PRESERVED_FDS)
791 return -ENOMEM;
792 j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd;
793 j->preserved_fds[j->preserved_fd_count].child_fd = child_fd;
794 j->preserved_fd_count++;
795 return 0;
796}
797
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400798static void clear_seccomp_options(struct minijail *j)
799{
800 j->flags.seccomp_filter = 0;
801 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400802 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400803 j->filter_len = 0;
804 j->filter_prog = NULL;
805 j->flags.no_new_privs = 0;
806}
807
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400808static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400809{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400810 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400811 /*
812 * |errno| will be set to EINVAL when seccomp has not been
813 * compiled into the kernel. On certain platforms and kernel
814 * versions this is not a fatal failure. In that case, and only
815 * in that case, disable seccomp and skip loading the filters.
816 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400817 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400818 warn("not loading seccomp filters, seccomp filter not "
819 "supported");
820 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400821 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700822 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400823 /*
824 * If |errno| != EINVAL or seccomp_can_softfail() is false,
825 * we can proceed. Worst case scenario minijail_enter() will
826 * abort() if seccomp fails.
827 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700828 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400829 if (j->flags.seccomp_filter_tsync) {
830 /* Are the seccomp(2) syscall and the TSYNC option supported? */
831 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
832 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
833 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400834 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
835 warn("seccomp(2) syscall not supported");
836 clear_seccomp_options(j);
837 return 0;
838 } else if (saved_errno == EINVAL &&
839 seccomp_can_softfail()) {
840 warn(
841 "seccomp filter thread sync not supported");
842 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400843 return 0;
844 }
845 /*
846 * Similar logic here. If seccomp_can_softfail() is
847 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
848 * we can proceed. Worst case scenario minijail_enter()
849 * will abort() if seccomp or TSYNC fail.
850 */
851 }
852 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400853 return 1;
854}
855
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700856static int parse_seccomp_filters(struct minijail *j, const char *filename,
857 FILE *policy_file)
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400858{
859 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400860 int use_ret_trap =
861 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
862 int allow_logging = j->flags.seccomp_filter_logging;
863
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700864 if (compile_filter(filename, policy_file, fprog, use_ret_trap,
865 allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400866 free(fprog);
867 return -1;
868 }
869
870 j->filter_len = fprog->len;
871 j->filter_prog = fprog;
872 return 0;
873}
874
875void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
876{
877 if (!seccomp_should_parse_filters(j))
878 return;
879
Elly Jonese1749eb2011-10-07 13:54:59 -0400880 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800881 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700882 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400883 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800884
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700885 if (parse_seccomp_filters(j, path, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700886 die("failed to compile seccomp filter BPF program in '%s'",
887 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800888 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400889 fclose(file);
890}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800891
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400892void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
893{
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700894 char *fd_path, *path;
895 FILE *file;
896
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400897 if (!seccomp_should_parse_filters(j))
898 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800899
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700900 file = fdopen(fd, "r");
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400901 if (!file) {
902 pdie("failed to associate stream with fd %d", fd);
903 }
904
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700905 if (asprintf(&fd_path, "/proc/self/fd/%d", fd) == -1)
906 pdie("failed to create path for fd %d", fd);
907 path = realpath(fd_path, NULL);
908 if (path == NULL)
909 pwarn("failed to get path of fd %d", fd);
910 free(fd_path);
911
912 if (parse_seccomp_filters(j, path ? path : "<fd>", file) != 0) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400913 die("failed to compile seccomp filter BPF program from fd %d",
914 fd);
915 }
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700916 free(path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400917 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500918}
919
Andrew Brestickereac28942015-11-11 16:04:46 -0800920int API minijail_use_alt_syscall(struct minijail *j, const char *table)
921{
922 j->alt_syscall_table = strdup(table);
923 if (!j->alt_syscall_table)
924 return -ENOMEM;
925 j->flags.alt_syscall = 1;
926 return 0;
927}
928
Will Drewryf89aef52011-09-16 16:48:57 -0500929struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400930 size_t available;
931 size_t total;
932 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500933};
934
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800935void marshal_state_init(struct marshal_state *state, char *buf,
936 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400937{
938 state->available = available;
939 state->buf = buf;
940 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500941}
942
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800943void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400944{
945 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500946
Elly Jonese1749eb2011-10-07 13:54:59 -0400947 /* Up to |available| will be written. */
948 if (copy_len) {
949 memcpy(state->buf, src, copy_len);
950 state->buf += copy_len;
951 state->available -= copy_len;
952 }
953 /* |total| will contain the expected length. */
954 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500955}
956
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400957void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700958{
959 marshal_append(state, m->src, strlen(m->src) + 1);
960 marshal_append(state, m->dest, strlen(m->dest) + 1);
961 marshal_append(state, m->type, strlen(m->type) + 1);
962 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
963 if (m->has_data)
964 marshal_append(state, m->data, strlen(m->data) + 1);
965 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
966}
967
Will Drewry6ac91122011-10-21 16:38:58 -0500968void minijail_marshal_helper(struct marshal_state *state,
969 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400970{
Dylan Reid648b2202015-10-23 00:50:00 -0700971 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800972 size_t i;
973
Elly Jonese1749eb2011-10-07 13:54:59 -0400974 marshal_append(state, (char *)j, sizeof(*j));
975 if (j->user)
976 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800977 if (j->suppl_gid_list) {
978 marshal_append(state, j->suppl_gid_list,
979 j->suppl_gid_count * sizeof(gid_t));
980 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400981 if (j->chrootdir)
982 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400983 if (j->hostname)
984 marshal_append(state, j->hostname, strlen(j->hostname) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800985 if (j->alt_syscall_table) {
986 marshal_append(state, j->alt_syscall_table,
987 strlen(j->alt_syscall_table) + 1);
988 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800989 if (j->flags.seccomp_filter && j->filter_prog) {
990 struct sock_fprog *fp = j->filter_prog;
991 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800992 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400993 }
Dylan Reid648b2202015-10-23 00:50:00 -0700994 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400995 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400996 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800997 for (i = 0; i < j->cgroup_count; ++i)
998 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500999}
1000
Will Drewry6ac91122011-10-21 16:38:58 -05001001size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001002{
1003 struct marshal_state state;
1004 marshal_state_init(&state, NULL, 0);
1005 minijail_marshal_helper(&state, j);
1006 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -05001007}
1008
Elly Jonese1749eb2011-10-07 13:54:59 -04001009int minijail_marshal(const struct minijail *j, char *buf, size_t available)
1010{
1011 struct marshal_state state;
1012 marshal_state_init(&state, buf, available);
1013 minijail_marshal_helper(&state, j);
1014 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -05001015}
1016
Elly Jonese1749eb2011-10-07 13:54:59 -04001017int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
1018{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -08001019 size_t i;
1020 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -05001021 int ret = -EINVAL;
1022
Elly Jonese1749eb2011-10-07 13:54:59 -04001023 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001024 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001025 memcpy((void *)j, serialized, sizeof(*j));
1026 serialized += sizeof(*j);
1027 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001028
Will Drewrybee7ba72011-10-21 20:47:01 -05001029 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001030 j->pid_file_path = NULL;
1031 j->uidmap = NULL;
1032 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001033 j->mounts_head = NULL;
1034 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001035 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001036 j->hooks_head = NULL;
1037 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001038
Elly Jonese1749eb2011-10-07 13:54:59 -04001039 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001040 char *user = consumestr(&serialized, &length);
1041 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001042 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001043 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001044 if (!j->user)
1045 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001046 }
Will Drewryf89aef52011-09-16 16:48:57 -05001047
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001048 if (j->suppl_gid_list) { /* stale pointer */
1049 if (j->suppl_gid_count > NGROUPS_MAX) {
1050 goto bad_gid_list;
1051 }
1052 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1053 void *gid_list_bytes =
1054 consumebytes(gid_list_size, &serialized, &length);
1055 if (!gid_list_bytes)
1056 goto bad_gid_list;
1057
1058 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1059 if (!j->suppl_gid_list)
1060 goto bad_gid_list;
1061
1062 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1063 }
1064
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001065 if (j->chrootdir) { /* stale pointer */
1066 char *chrootdir = consumestr(&serialized, &length);
1067 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001068 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001069 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001070 if (!j->chrootdir)
1071 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001072 }
1073
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001074 if (j->hostname) { /* stale pointer */
1075 char *hostname = consumestr(&serialized, &length);
1076 if (!hostname)
1077 goto bad_hostname;
1078 j->hostname = strdup(hostname);
1079 if (!j->hostname)
1080 goto bad_hostname;
1081 }
1082
Andrew Brestickereac28942015-11-11 16:04:46 -08001083 if (j->alt_syscall_table) { /* stale pointer */
1084 char *alt_syscall_table = consumestr(&serialized, &length);
1085 if (!alt_syscall_table)
1086 goto bad_syscall_table;
1087 j->alt_syscall_table = strdup(alt_syscall_table);
1088 if (!j->alt_syscall_table)
1089 goto bad_syscall_table;
1090 }
1091
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001092 if (j->flags.seccomp_filter && j->filter_len > 0) {
1093 size_t ninstrs = j->filter_len;
1094 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1095 ninstrs > USHRT_MAX)
1096 goto bad_filters;
1097
1098 size_t program_len = ninstrs * sizeof(struct sock_filter);
1099 void *program = consumebytes(program_len, &serialized, &length);
1100 if (!program)
1101 goto bad_filters;
1102
1103 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001104 if (!j->filter_prog)
1105 goto bad_filters;
1106
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001107 j->filter_prog->len = ninstrs;
1108 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001109 if (!j->filter_prog->filter)
1110 goto bad_filter_prog_instrs;
1111
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001112 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001113 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001114
Dylan Reid648b2202015-10-23 00:50:00 -07001115 count = j->mounts_count;
1116 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001117 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001118 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001119 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001120 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001121 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001122 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001123 const char *src = consumestr(&serialized, &length);
1124 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001125 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001126 dest = consumestr(&serialized, &length);
1127 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001128 goto bad_mounts;
1129 type = consumestr(&serialized, &length);
1130 if (!type)
1131 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001132 has_data = consumebytes(sizeof(*has_data), &serialized,
1133 &length);
1134 if (!has_data)
1135 goto bad_mounts;
1136 if (*has_data) {
1137 data = consumestr(&serialized, &length);
1138 if (!data)
1139 goto bad_mounts;
1140 }
Dylan Reid648b2202015-10-23 00:50:00 -07001141 flags = consumebytes(sizeof(*flags), &serialized, &length);
1142 if (!flags)
1143 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001144 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001145 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001146 }
1147
Dylan Reid605ce7f2016-01-19 19:21:00 -08001148 count = j->cgroup_count;
1149 j->cgroup_count = 0;
1150 for (i = 0; i < count; ++i) {
1151 char *cgroup = consumestr(&serialized, &length);
1152 if (!cgroup)
1153 goto bad_cgroups;
1154 j->cgroups[i] = strdup(cgroup);
1155 if (!j->cgroups[i])
1156 goto bad_cgroups;
1157 ++j->cgroup_count;
1158 }
1159
Elly Jonese1749eb2011-10-07 13:54:59 -04001160 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001161
Dylan Reid605ce7f2016-01-19 19:21:00 -08001162bad_cgroups:
Mike Frysingerac08a682017-10-10 02:04:50 -04001163 free_mounts_list(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001164 for (i = 0; i < j->cgroup_count; ++i)
1165 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001166bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001167 if (j->flags.seccomp_filter && j->filter_len > 0) {
1168 free(j->filter_prog->filter);
1169 free(j->filter_prog);
1170 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001171bad_filter_prog_instrs:
1172 if (j->filter_prog)
1173 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001174bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001175 if (j->alt_syscall_table)
1176 free(j->alt_syscall_table);
1177bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001178 if (j->chrootdir)
1179 free(j->chrootdir);
1180bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001181 if (j->hostname)
1182 free(j->hostname);
1183bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001184 if (j->suppl_gid_list)
1185 free(j->suppl_gid_list);
1186bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001187 if (j->user)
1188 free(j->user);
1189clear_pointers:
1190 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001191 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001192 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001193 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001194 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001195 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001196out:
1197 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001198}
1199
Mike Frysinger33ffef32017-01-13 19:53:19 -05001200struct dev_spec {
1201 const char *name;
1202 mode_t mode;
1203 dev_t major, minor;
1204};
1205
1206static const struct dev_spec device_nodes[] = {
1207 {
1208 "null",
1209 S_IFCHR | 0666, 1, 3,
1210 },
1211 {
1212 "zero",
1213 S_IFCHR | 0666, 1, 5,
1214 },
1215 {
1216 "full",
1217 S_IFCHR | 0666, 1, 7,
1218 },
1219 {
1220 "urandom",
1221 S_IFCHR | 0444, 1, 9,
1222 },
1223 {
1224 "tty",
1225 S_IFCHR | 0666, 5, 0,
1226 },
1227};
1228
1229struct dev_sym_spec {
1230 const char *source, *dest;
1231};
1232
1233static const struct dev_sym_spec device_symlinks[] = {
1234 { "ptmx", "pts/ptmx", },
1235 { "fd", "/proc/self/fd", },
1236 { "stdin", "fd/0", },
1237 { "stdout", "fd/1", },
1238 { "stderr", "fd/2", },
1239};
1240
1241/*
1242 * Clean up the temporary dev path we had setup previously. In case of errors,
1243 * we don't want to go leaking empty tempdirs.
1244 */
1245static void mount_dev_cleanup(char *dev_path)
1246{
1247 umount2(dev_path, MNT_DETACH);
1248 rmdir(dev_path);
1249 free(dev_path);
1250}
1251
1252/*
1253 * Set up the pseudo /dev path at the temporary location.
1254 * See mount_dev_finalize for more details.
1255 */
1256static int mount_dev(char **dev_path_ret)
1257{
1258 int ret;
1259 int dev_fd;
1260 size_t i;
1261 mode_t mask;
1262 char *dev_path;
1263
1264 /*
1265 * Create a temp path for the /dev init. We'll relocate this to the
1266 * final location later on in the startup process.
1267 */
1268 dev_path = *dev_path_ret = strdup("/tmp/minijail.dev.XXXXXX");
1269 if (dev_path == NULL || mkdtemp(dev_path) == NULL)
1270 pdie("could not create temp path for /dev");
1271
1272 /* Set up the empty /dev mount point first. */
1273 ret = mount("minijail-devfs", dev_path, "tmpfs",
1274 MS_NOEXEC | MS_NOSUID, "size=5M,mode=755");
1275 if (ret) {
1276 rmdir(dev_path);
1277 return ret;
1278 }
1279
1280 /* We want to set the mode directly from the spec. */
1281 mask = umask(0);
1282
1283 /* Get a handle to the temp dev path for *at funcs below. */
1284 dev_fd = open(dev_path, O_DIRECTORY|O_PATH|O_CLOEXEC);
1285 if (dev_fd < 0) {
1286 ret = 1;
1287 goto done;
1288 }
1289
1290 /* Create all the nodes in /dev. */
1291 for (i = 0; i < ARRAY_SIZE(device_nodes); ++i) {
1292 const struct dev_spec *ds = &device_nodes[i];
1293 ret = mknodat(dev_fd, ds->name, ds->mode,
1294 makedev(ds->major, ds->minor));
1295 if (ret)
1296 goto done;
1297 }
1298
1299 /* Create all the symlinks in /dev. */
1300 for (i = 0; i < ARRAY_SIZE(device_symlinks); ++i) {
1301 const struct dev_sym_spec *ds = &device_symlinks[i];
1302 ret = symlinkat(ds->dest, dev_fd, ds->source);
1303 if (ret)
1304 goto done;
1305 }
1306
1307 /* Restore old mask. */
1308 done:
1309 close(dev_fd);
1310 umask(mask);
1311
1312 if (ret)
1313 mount_dev_cleanup(dev_path);
1314
1315 return ret;
1316}
1317
1318/*
1319 * Relocate the temporary /dev mount to its final /dev place.
1320 * We have to do this two step process so people can bind mount extra
1321 * /dev paths like /dev/log.
1322 */
1323static int mount_dev_finalize(const struct minijail *j, char *dev_path)
1324{
1325 int ret = -1;
1326 char *dest = NULL;
1327
1328 /* Unmount the /dev mount if possible. */
1329 if (umount2("/dev", MNT_DETACH))
1330 goto done;
1331
1332 if (asprintf(&dest, "%s/dev", j->chrootdir ? : "") < 0)
1333 goto done;
1334
1335 if (mount(dev_path, dest, NULL, MS_MOVE, NULL))
1336 goto done;
1337
1338 ret = 0;
1339 done:
1340 free(dest);
1341 mount_dev_cleanup(dev_path);
1342
1343 return ret;
1344}
1345
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001346/*
1347 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001348 * @j Minijail these mounts are for
1349 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001350 *
1351 * Returns 0 for success.
1352 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001353static int mount_one(const struct minijail *j, struct mountpoint *m,
1354 const char *dev_path)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001355{
Dylan Reid648b2202015-10-23 00:50:00 -07001356 int ret;
1357 char *dest;
1358 int remount_ro = 0;
1359
Mike Frysinger33ffef32017-01-13 19:53:19 -05001360 /* We assume |dest| has a leading "/". */
1361 if (dev_path && strncmp("/dev/", m->dest, 5) == 0) {
1362 /* Since the temp path is rooted at /dev, skip that dest part. */
1363 if (asprintf(&dest, "%s%s", dev_path, m->dest + 4) < 0)
1364 return -ENOMEM;
1365 } else {
Mike Frysingerac08a682017-10-10 02:04:50 -04001366 if (asprintf(&dest, "%s%s", j->chrootdir ?: "", m->dest) < 0)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001367 return -ENOMEM;
1368 }
Dylan Reid648b2202015-10-23 00:50:00 -07001369
Mike Frysinger33ffef32017-01-13 19:53:19 -05001370 ret = setup_mount_destination(m->src, dest, j->uid, j->gid,
1371 (m->flags & MS_BIND));
1372 if (ret) {
1373 pwarn("creating mount target '%s' failed", dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001374 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001375 }
Dylan Reideec77962016-06-30 19:35:10 -07001376
Dylan Reid648b2202015-10-23 00:50:00 -07001377 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001378 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1379 * can't both be specified in the original bind mount.
1380 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001381 */
1382 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1383 remount_ro = 1;
1384 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001385 }
Dylan Reid648b2202015-10-23 00:50:00 -07001386
Dylan Reid81e23972016-05-18 14:06:35 -07001387 ret = mount(m->src, dest, m->type, m->flags, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001388 if (ret) {
1389 pwarn("mount: %s -> %s", m->src, dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001390 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001391 }
Dylan Reid648b2202015-10-23 00:50:00 -07001392
1393 if (remount_ro) {
1394 m->flags |= MS_RDONLY;
1395 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001396 m->flags | MS_REMOUNT, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001397 if (ret) {
1398 pwarn("bind ro: %s -> %s", m->src, dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001399 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001400 }
Dylan Reid648b2202015-10-23 00:50:00 -07001401 }
1402
Elly Jones51a5b6c2011-10-12 19:09:26 -04001403 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001404 if (m->next)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001405 return mount_one(j, m->next, dev_path);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001406 return 0;
1407
1408error:
1409 free(dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001410 return ret;
1411}
1412
Mike Frysingerac08a682017-10-10 02:04:50 -04001413static void process_mounts_or_die(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001414{
Mike Frysingerac08a682017-10-10 02:04:50 -04001415 /*
1416 * We have to mount /dev first in case there are bind mounts from
1417 * the original /dev into the new unique tmpfs one.
1418 */
1419 char *dev_path = NULL;
1420 if (j->flags.mount_dev && mount_dev(&dev_path))
1421 pdie("mount_dev failed");
Dylan Reid648b2202015-10-23 00:50:00 -07001422
Mike Frysingerac08a682017-10-10 02:04:50 -04001423 if (j->mounts_head && mount_one(j, j->mounts_head, dev_path)) {
1424 if (dev_path) {
1425 int saved_errno = errno;
1426 mount_dev_cleanup(dev_path);
1427 errno = saved_errno;
1428 }
1429 pdie("mount_one failed");
1430 }
Mike Frysinger33ffef32017-01-13 19:53:19 -05001431
1432 /*
Mike Frysingerac08a682017-10-10 02:04:50 -04001433 * Once all bind mounts have been processed, move the temp dev to
1434 * its final /dev home.
Mike Frysinger33ffef32017-01-13 19:53:19 -05001435 */
1436 if (j->flags.mount_dev && mount_dev_finalize(j, dev_path))
Mike Frysingerac08a682017-10-10 02:04:50 -04001437 pdie("mount_dev_finalize failed");
1438}
Elly Jones51a5b6c2011-10-12 19:09:26 -04001439
Mike Frysingerac08a682017-10-10 02:04:50 -04001440static int enter_chroot(const struct minijail *j)
1441{
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001442 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1443
Elly Jones51a5b6c2011-10-12 19:09:26 -04001444 if (chroot(j->chrootdir))
1445 return -errno;
1446
1447 if (chdir("/"))
1448 return -errno;
1449
1450 return 0;
1451}
1452
Mike Frysingerac08a682017-10-10 02:04:50 -04001453static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001454{
Mike Frysingerac08a682017-10-10 02:04:50 -04001455 int oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001456
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001457 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1458
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001459 /*
1460 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001461 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001462 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001463 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001464 if (oldroot < 0)
1465 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001466 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001467 if (newroot < 0)
1468 pdie("failed to open %s for fchdir", j->chrootdir);
1469
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001470 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001471 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001472 * do a self bind mount.
1473 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001474 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1475 pdie("failed to bind mount '%s'", j->chrootdir);
1476 if (chdir(j->chrootdir))
1477 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001478 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001479 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001480
1481 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001482 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001483 * change to the old root and unmount it.
1484 */
1485 if (fchdir(oldroot))
1486 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001487
1488 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001489 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1490 * there could be a shared mount point under |oldroot|. In that case,
1491 * mounts under this shared mount point will be unmounted below, and
1492 * this unmounting will propagate to the original mount namespace
1493 * (because the mount point is shared). To prevent this unexpected
1494 * unmounting, remove these mounts from their peer groups by recursively
1495 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001496 */
1497 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001498 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001499 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001500 if (umount2(".", MNT_DETACH))
1501 pdie("umount(/)");
1502 /* Change back to the new root. */
1503 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001504 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001505 if (close(oldroot))
1506 return -errno;
1507 if (close(newroot))
1508 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001509 if (chroot("/"))
1510 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001511 /* Set correct CWD for getcwd(3). */
1512 if (chdir("/"))
1513 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001514
1515 return 0;
1516}
1517
Martin Pelikánab9eb442017-01-25 11:53:58 +11001518static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001519{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001520 const char fmt[] = "size=%zu,mode=1777";
1521 /* Count for the user storing ULLONG_MAX literally + extra space. */
1522 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1523 int ret;
1524
1525 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1526
1527 if (ret <= 0)
1528 pdie("tmpfs size spec error");
1529 else if ((size_t)ret >= sizeof(data))
1530 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001531 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001532 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001533}
1534
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001535static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001536{
1537 const char *kProcPath = "/proc";
1538 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001539 /*
1540 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001541 * /proc in our namespace, which means using MS_REMOUNT here would
1542 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001543 * namespace (!). Instead, remove their mount from our namespace lazily
1544 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001545 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001546 if (umount2(kProcPath, MNT_DETACH)) {
1547 /*
1548 * If we are in a new user namespace, umount(2) will fail.
1549 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1550 */
1551 if (j->flags.userns) {
1552 info("umount(/proc, MNT_DETACH) failed, "
1553 "this is expected when using user namespaces");
1554 } else {
1555 return -errno;
1556 }
1557 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001558 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001559 return -errno;
1560 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001561}
1562
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001563static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001564{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001565 kill(j->initpid, SIGKILL);
1566 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001567}
1568
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001569static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001570{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001571 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001572 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001573}
1574
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001575static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001576{
1577 size_t i;
1578
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001579 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001580 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001581 kill_child_and_die(j, "failed to add to cgroups");
1582 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001583}
1584
Dylan Reid0f72ef42017-06-06 15:42:49 -07001585static void set_rlimits_or_die(const struct minijail *j)
1586{
1587 size_t i;
1588
1589 for (i = 0; i < j->rlimit_count; ++i) {
1590 struct rlimit limit;
1591 limit.rlim_cur = j->rlimits[i].cur;
1592 limit.rlim_max = j->rlimits[i].max;
1593 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1594 kill_child_and_die(j, "failed to set rlimit");
1595 }
1596}
1597
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001598static void write_ugid_maps_or_die(const struct minijail *j)
1599{
1600 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1601 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001602 if (j->gidmap && j->flags.disable_setgroups) {
1603 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1604 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001605 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001606 if (ret == -ENOENT) {
1607 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1608 warn("could not disable setgroups(2)");
1609 } else
1610 kill_child_and_die(j, "failed to disable setgroups(2)");
1611 }
1612 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001613 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1614 kill_child_and_die(j, "failed to write gid_map");
1615}
1616
1617static void enter_user_namespace(const struct minijail *j)
1618{
Luis Hector Chavez71323552017-09-05 09:17:22 -07001619 int uid = j->flags.uid ? j->uid : 0;
1620 int gid = j->flags.gid ? j->gid : 0;
1621 if (j->gidmap && setresgid(gid, gid, gid)) {
1622 pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid,
1623 gid);
1624 }
1625 if (j->uidmap && setresuid(uid, uid, uid)) {
1626 pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid,
1627 uid);
1628 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001629}
1630
1631static void parent_setup_complete(int *pipe_fds)
1632{
1633 close(pipe_fds[0]);
1634 close(pipe_fds[1]);
1635}
1636
1637/*
1638 * wait_for_parent_setup: Called by the child process to wait for any
1639 * further parent-side setup to complete before continuing.
1640 */
1641static void wait_for_parent_setup(int *pipe_fds)
1642{
1643 char buf;
1644
1645 close(pipe_fds[1]);
1646
1647 /* Wait for parent to complete setup and close the pipe. */
1648 if (read(pipe_fds[0], &buf, 1) != 0)
1649 die("failed to sync with parent");
1650 close(pipe_fds[0]);
1651}
1652
1653static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001654{
Lutz Justen13807cb2017-01-03 17:11:55 +01001655 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1656 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001657 die("can only do one of inherit, keep, or set supplementary "
1658 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001659 }
1660
Lutz Justen13807cb2017-01-03 17:11:55 +01001661 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001662 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001663 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001664 } else if (j->flags.set_suppl_gids) {
1665 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001666 pdie("setgroups(suppl_gids) failed");
Luis Hector Chavez71323552017-09-05 09:17:22 -07001667 } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001668 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001669 * Only attempt to clear supplementary groups if we are changing
Luis Hector Chavez71323552017-09-05 09:17:22 -07001670 * users or groups, and if the caller did not request to disable
1671 * setgroups (used when entering a user namespace as a
1672 * non-privileged user).
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001673 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001674 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001675 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001676 }
1677
1678 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001679 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001680
1681 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001682 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001683}
1684
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001685static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1686{
1687 const uint64_t one = 1;
1688 unsigned int i;
1689 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1690 if (keep_mask & (one << i))
1691 continue;
1692 if (prctl(PR_CAPBSET_DROP, i))
1693 pdie("could not drop capability from bounding set");
1694 }
1695}
1696
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001697static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001698{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001699 if (!j->flags.use_caps)
1700 return;
1701
Elly Jonese1749eb2011-10-07 13:54:59 -04001702 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001703 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001704 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001705 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001706 unsigned int i;
1707 if (!caps)
1708 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001709 if (cap_clear(caps))
1710 die("can't clear caps");
1711
1712 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001713 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001714 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001715 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001716 flag[0] = i;
1717 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001718 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001719 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001720 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001721 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001722 die("can't add inheritable cap");
1723 }
1724 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001725 die("can't apply initial cleaned capset");
1726
1727 /*
1728 * Instead of dropping bounding set first, do it here in case
1729 * the caller had a more permissive bounding set which could
1730 * have been used above to raise a capability that wasn't already
1731 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1732 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001733 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001734
1735 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001736 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001737 flag[0] = CAP_SETPCAP;
1738 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1739 die("can't clear effective cap");
1740 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1741 die("can't clear permitted cap");
1742 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1743 die("can't clear inheritable cap");
1744 }
1745
1746 if (cap_set_proc(caps))
1747 die("can't apply final cleaned capset");
1748
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001749 /*
1750 * If ambient capabilities are supported, clear all capabilities first,
1751 * then raise the requested ones.
1752 */
1753 if (j->flags.set_ambient_caps) {
1754 if (!cap_ambient_supported()) {
1755 pdie("ambient capabilities not supported");
1756 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001757 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1758 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001759 pdie("can't clear ambient capabilities");
1760 }
1761
1762 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1763 if (!(j->caps & (one << i)))
1764 continue;
1765
1766 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1767 0) != 0) {
1768 pdie("prctl(PR_CAP_AMBIENT, "
1769 "PR_CAP_AMBIENT_RAISE, %u) failed",
1770 i);
1771 }
1772 }
1773 }
1774
Kees Cook323878a2013-02-05 15:35:24 -08001775 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001776}
1777
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001778static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001779{
1780 /*
1781 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1782 * in the kernel source tree for an explanation of the parameters.
1783 */
1784 if (j->flags.no_new_privs) {
1785 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1786 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1787 }
1788
1789 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001790 * Code running with ASan
1791 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1792 * will make system calls not included in the syscall filter policy,
1793 * which will likely crash the program. Skip setting seccomp filter in
1794 * that case.
1795 * 'running_with_asan()' has no inputs and is completely defined at
1796 * build time, so this cannot be used by an attacker to skip setting
1797 * seccomp filter.
1798 */
1799 if (j->flags.seccomp_filter && running_with_asan()) {
1800 warn("running with ASan, not setting seccomp filter");
1801 return;
1802 }
1803
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001804 if (j->flags.seccomp_filter) {
1805 if (j->flags.seccomp_filter_logging) {
1806 /*
1807 * If logging seccomp filter failures,
1808 * install the SIGSYS handler first.
1809 */
1810 if (install_sigsys_handler())
1811 pdie("failed to install SIGSYS handler");
1812 warn("logging seccomp filter failures");
1813 } else if (j->flags.seccomp_filter_tsync) {
1814 /*
1815 * If setting thread sync,
1816 * reset the SIGSYS signal handler so that
1817 * the entire thread group is killed.
1818 */
1819 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1820 pdie("failed to reset SIGSYS disposition");
1821 info("reset SIGSYS disposition");
1822 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001823 }
1824
1825 /*
1826 * Install the syscall filter.
1827 */
1828 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001829 if (j->flags.seccomp_filter_tsync) {
1830 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1831 SECCOMP_FILTER_FLAG_TSYNC,
1832 j->filter_prog)) {
1833 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001834 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001835 } else {
1836 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1837 j->filter_prog)) {
1838 pdie("prctl(seccomp_filter) failed");
1839 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001840 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001841 }
1842}
1843
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001844static pid_t forward_pid = -1;
1845
1846static void forward_signal(__attribute__((unused)) int nr,
1847 __attribute__((unused)) siginfo_t *siginfo,
1848 __attribute__((unused)) void *void_context)
1849{
1850 if (forward_pid != -1) {
1851 kill(forward_pid, nr);
1852 }
1853}
1854
1855static void install_signal_handlers(void)
1856{
1857 struct sigaction act;
1858
1859 memset(&act, 0, sizeof(act));
1860 act.sa_sigaction = &forward_signal;
1861 act.sa_flags = SA_SIGINFO | SA_RESTART;
1862
1863 /* Handle all signals, except SIGCHLD. */
1864 for (int nr = 1; nr < NSIG; nr++) {
1865 /*
1866 * We don't care if we get EINVAL: that just means that we
1867 * can't handle this signal, so let's skip it and continue.
1868 */
1869 sigaction(nr, &act, NULL);
1870 }
1871 /* Reset SIGCHLD's handler. */
1872 signal(SIGCHLD, SIG_DFL);
1873
1874 /* Handle real-time signals. */
1875 for (int nr = SIGRTMIN; nr <= SIGRTMAX; nr++) {
1876 sigaction(nr, &act, NULL);
1877 }
1878}
1879
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001880static const char *lookup_hook_name(minijail_hook_event_t event)
1881{
1882 switch (event) {
1883 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
1884 return "pre-drop-caps";
1885 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
1886 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001887 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
1888 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001889 case MINIJAIL_HOOK_EVENT_MAX:
1890 /*
1891 * Adding this in favor of a default case to force the
1892 * compiler to error out if a new enum value is added.
1893 */
1894 break;
1895 }
1896 return "unknown";
1897}
1898
1899static void run_hooks_or_die(const struct minijail *j,
1900 minijail_hook_event_t event)
1901{
1902 int rc;
1903 int hook_index = 0;
1904 for (struct hook *c = j->hooks_head; c; c = c->next) {
1905 if (c->event != event)
1906 continue;
1907 rc = c->hook(c->payload);
1908 if (rc != 0) {
1909 errno = -rc;
1910 pdie("%s hook (index %d) failed",
1911 lookup_hook_name(event), hook_index);
1912 }
1913 /* Only increase the index within the same hook event type. */
1914 ++hook_index;
1915 }
1916}
1917
Will Drewry6ac91122011-10-21 16:38:58 -05001918void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001919{
Dylan Reidf682d472015-09-17 21:39:07 -07001920 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001921 * If we're dropping caps, get the last valid cap from /proc now,
1922 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001923 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001924 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001925 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001926 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001927
Elly Jonese1749eb2011-10-07 13:54:59 -04001928 if (j->flags.pids)
1929 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001930 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001931
Lutz Justen13807cb2017-01-03 17:11:55 +01001932 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001933 die("cannot inherit supplementary groups without setting a "
1934 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001935
Elly Jonesdd3e8512012-01-23 15:13:38 -05001936 /*
1937 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001938 * so we don't even try. If any of our operations fail, we abort() the
1939 * entire process.
1940 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001941 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001942 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001943
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001944 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001945 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001946 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001947 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001948 * Unless asked not to, remount all filesystems as private.
1949 * If they are shared, new bind mounts will creep out of our
1950 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001951 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1952 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001953 if (!j->flags.skip_remount_private) {
1954 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001955 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1956 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001957 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001958 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001959
Dylan Reidf7942472015-11-18 17:55:26 -08001960 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001961 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001962 }
1963
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001964 if (j->flags.uts) {
1965 if (unshare(CLONE_NEWUTS))
1966 pdie("unshare(CLONE_NEWUTS) failed");
1967
1968 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
1969 pdie("sethostname(%s) failed", j->hostname);
1970 }
1971
Dylan Reid1102f5a2015-09-15 11:52:20 -07001972 if (j->flags.enter_net) {
1973 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001974 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001975 } else if (j->flags.net) {
1976 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001977 pdie("unshare(CLONE_NEWNET) failed");
1978 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001979 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001980
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001981 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001982 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001983
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08001984 if (j->flags.new_session_keyring) {
1985 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
1986 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
1987 }
1988
Mike Frysingerac08a682017-10-10 02:04:50 -04001989 /* We have to process all the mounts before we chroot/pivot_root. */
1990 process_mounts_or_die(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001991
Mike Frysingerac08a682017-10-10 02:04:50 -04001992 if (j->flags.chroot && enter_chroot(j))
Mike Frysinger33ffef32017-01-13 19:53:19 -05001993 pdie("chroot");
Mike Frysinger33ffef32017-01-13 19:53:19 -05001994
Mike Frysingerac08a682017-10-10 02:04:50 -04001995 if (j->flags.pivot_root && enter_pivot_root(j))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001996 pdie("pivot_root");
1997
Martin Pelikánab9eb442017-01-25 11:53:58 +11001998 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07001999 pdie("mount_tmp");
2000
Dylan Reid791f5772015-09-14 20:02:42 -07002001 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04002002 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04002003
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002004 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
2005
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002006 /*
2007 * If we're only dropping capabilities from the bounding set, but not
2008 * from the thread's (permitted|inheritable|effective) sets, do it now.
2009 */
2010 if (j->flags.capbset_drop) {
2011 drop_capbset(j->cap_bset, last_valid_cap);
2012 }
2013
2014 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002015 /*
2016 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04002017 * capability to change uids, our attempt to use setuid()
2018 * below will fail. Hang on to root caps across setuid(), then
2019 * lock securebits.
2020 */
2021 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002022 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002023
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -07002024 if (lock_securebits(j->securebits_skip_mask) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002025 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002026 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002027 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002028
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002029 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002030 /*
2031 * If we're setting no_new_privs, we can drop privileges
2032 * before setting seccomp filter. This way filter policies
2033 * don't need to allow privilege-dropping syscalls.
2034 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002035 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002036 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002037 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002038 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002039 /*
2040 * If we're not setting no_new_privs,
2041 * we need to set seccomp filter *before* dropping privileges.
2042 * WARNING: this means that filter policies *must* allow
2043 * setgroups()/setresgid()/setresuid() for dropping root and
2044 * capget()/capset()/prctl() for dropping caps.
2045 */
2046 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002047 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002048 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04002049 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002050
Elly Jonesdd3e8512012-01-23 15:13:38 -05002051 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08002052 * Select the specified alternate syscall table. The table must not
2053 * block prctl(2) if we're using seccomp as well.
2054 */
2055 if (j->flags.alt_syscall) {
2056 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002057 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08002058 }
2059
2060 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05002061 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04002062 * privilege-dropping syscalls :)
2063 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002064 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04002065 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002066 warn("seccomp not supported");
2067 return;
2068 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002069 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002070 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002071}
2072
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002073/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04002074static int init_exitstatus = 0;
2075
Will Drewry6ac91122011-10-21 16:38:58 -05002076void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04002077{
2078 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04002079}
2080
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002081void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04002082{
2083 pid_t pid;
2084 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002085 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002086 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002087 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002088 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002089 /*
2090 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04002091 * left inside our pid namespace or we get a signal.
2092 */
2093 if (pid == rootpid)
2094 init_exitstatus = status;
2095 }
2096 if (!WIFEXITED(init_exitstatus))
2097 _exit(MINIJAIL_ERR_INIT);
2098 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04002099}
2100
Will Drewry6ac91122011-10-21 16:38:58 -05002101int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002102{
2103 size_t sz = 0;
2104 size_t bytes = read(fd, &sz, sizeof(sz));
2105 char *buf;
2106 int r;
2107 if (sizeof(sz) != bytes)
2108 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002109 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04002110 return -E2BIG;
2111 buf = malloc(sz);
2112 if (!buf)
2113 return -ENOMEM;
2114 bytes = read(fd, buf, sz);
2115 if (bytes != sz) {
2116 free(buf);
2117 return -EINVAL;
2118 }
2119 r = minijail_unmarshal(j, buf, sz);
2120 free(buf);
2121 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002122}
2123
Will Drewry6ac91122011-10-21 16:38:58 -05002124int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04002125{
2126 char *buf;
2127 size_t sz = minijail_size(j);
2128 ssize_t written;
2129 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04002130
Elly Jonese1749eb2011-10-07 13:54:59 -04002131 if (!sz)
2132 return -EINVAL;
2133 buf = malloc(sz);
2134 r = minijail_marshal(j, buf, sz);
2135 if (r) {
2136 free(buf);
2137 return r;
2138 }
2139 /* Sends [size][minijail]. */
2140 written = write(fd, &sz, sizeof(sz));
2141 if (written != sizeof(sz)) {
2142 free(buf);
2143 return -EFAULT;
2144 }
2145 written = write(fd, buf, sz);
2146 if (written < 0 || (size_t) written != sz) {
2147 free(buf);
2148 return -EFAULT;
2149 }
2150 free(buf);
2151 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002152}
Elly Jonescd7a9042011-07-22 13:56:51 -04002153
Will Drewry6ac91122011-10-21 16:38:58 -05002154int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04002155{
Daniel Erat5b7a3182015-08-19 16:06:22 -06002156#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002157 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002158 return 0;
2159#else
Elly Jonese1749eb2011-10-07 13:54:59 -04002160 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
2161 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
2162 if (!newenv)
2163 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04002164
Elly Jonese1749eb2011-10-07 13:54:59 -04002165 /* Only insert a separating space if we have something to separate... */
2166 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
2167 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04002168
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002169 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002170 setenv(kLdPreloadEnvVar, newenv, 1);
2171 free(newenv);
2172 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002173#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04002174}
2175
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002176static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04002177{
2178 int r = pipe(fds);
2179 char fd_buf[11];
2180 if (r)
2181 return r;
2182 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2183 if (r <= 0)
2184 return -EINVAL;
2185 setenv(kFdEnvVar, fd_buf, 1);
2186 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05002187}
2188
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002189static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002190{
2191 const char *kFdPath = "/proc/self/fd";
2192
2193 DIR *d = opendir(kFdPath);
2194 struct dirent *dir_entry;
2195
2196 if (d == NULL)
2197 return -1;
2198 int dir_fd = dirfd(d);
2199 while ((dir_entry = readdir(d)) != NULL) {
2200 size_t i;
2201 char *end;
2202 bool should_close = true;
2203 const int fd = strtol(dir_entry->d_name, &end, 10);
2204
2205 if ((*end) != '\0') {
2206 continue;
2207 }
2208 /*
2209 * We might have set up some pipes that we want to share with
2210 * the parent process, and should not be closed.
2211 */
2212 for (i = 0; i < size; ++i) {
2213 if (fd == inheritable_fds[i]) {
2214 should_close = false;
2215 break;
2216 }
2217 }
2218 /* Also avoid closing the directory fd. */
2219 if (should_close && fd != dir_fd)
2220 close(fd);
2221 }
2222 closedir(d);
2223 return 0;
2224}
2225
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002226static int redirect_fds(struct minijail *j)
2227{
2228 size_t i, i2;
2229 int closeable;
2230 for (i = 0; i < j->preserved_fd_count; i++) {
2231 if (dup2(j->preserved_fds[i].parent_fd,
2232 j->preserved_fds[i].child_fd) == -1) {
2233 return -1;
2234 }
2235 }
2236 /*
2237 * After all fds have been duped, we are now free to close all parent
2238 * fds that are *not* child fds.
2239 */
2240 for (i = 0; i < j->preserved_fd_count; i++) {
2241 closeable = true;
2242 for (i2 = 0; i2 < j->preserved_fd_count; i2++) {
2243 closeable &= j->preserved_fds[i].parent_fd !=
2244 j->preserved_fds[i2].child_fd;
2245 }
2246 if (closeable)
2247 close(j->preserved_fds[i].parent_fd);
2248 }
2249 return 0;
2250}
2251
Dylan Reidacfb8be2017-08-25 12:56:51 -07002252/*
2253 * Structure that specifies how to start a minijail.
2254 *
Dylan Reid0412dcc2017-08-24 11:33:15 -07002255 * filename - The program to exec in the child. Required if `exec_in_child` = 1.
2256 * argv - Arguments for the child program. Required if `exec_in_child` = 1.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002257 * use_preload - If true use LD_PRELOAD.
Dylan Reid0412dcc2017-08-24 11:33:15 -07002258 * exec_in_child - If true, run `filename`. Otherwise, the child will return to
2259 * the caller.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002260 */
2261struct minijail_run_config {
2262 const char *filename;
2263 char *const *argv;
2264 int use_preload;
Dylan Reid0412dcc2017-08-24 11:33:15 -07002265 int exec_in_child;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002266};
2267
2268/*
2269 * Set of pointers to fill with values from minijail_run.
2270 * All arguments are allowed to be NULL if unused.
2271 *
2272 * pstdin_fd - Filled with stdin pipe if non-NULL.
2273 * pstdout_fd - Filled with stdout pipe if non-NULL.
2274 * pstderr_fd - Filled with stderr pipe if non-NULL.
2275 * pchild_pid - Filled with the pid of the child process if non-NULL.
2276 */
2277struct minijail_run_status {
2278 int *pstdin_fd;
2279 int *pstdout_fd;
2280 int *pstderr_fd;
2281 pid_t *pchild_pid;
2282};
2283
Dylan Reid18c49c82017-08-25 14:52:27 -07002284static int minijail_run_internal(struct minijail *j,
2285 const struct minijail_run_config *config,
2286 struct minijail_run_status *status_out);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002287
Will Drewry6ac91122011-10-21 16:38:58 -05002288int API minijail_run(struct minijail *j, const char *filename,
2289 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002290{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002291 struct minijail_run_config config = {
2292 .filename = filename,
2293 .argv = argv,
2294 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002295 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002296 };
2297 struct minijail_run_status status = {};
2298 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002299}
2300
2301int API minijail_run_pid(struct minijail *j, const char *filename,
2302 char *const argv[], pid_t *pchild_pid)
2303{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002304 struct minijail_run_config config = {
2305 .filename = filename,
2306 .argv = argv,
2307 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002308 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002309 };
2310 struct minijail_run_status status = {
2311 .pchild_pid = pchild_pid,
2312 };
2313 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002314}
2315
2316int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002317 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002318{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002319 struct minijail_run_config config = {
2320 .filename = filename,
2321 .argv = argv,
2322 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002323 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002324 };
2325 struct minijail_run_status status = {
2326 .pstdin_fd = pstdin_fd,
2327 };
2328 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002329}
2330
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002331int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002332 char *const argv[], pid_t *pchild_pid,
2333 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002334{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002335 struct minijail_run_config config = {
2336 .filename = filename,
2337 .argv = argv,
2338 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002339 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002340 };
2341 struct minijail_run_status status = {
2342 .pstdin_fd = pstdin_fd,
2343 .pstdout_fd = pstdout_fd,
2344 .pstderr_fd = pstderr_fd,
2345 .pchild_pid = pchild_pid,
2346 };
2347 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002348}
2349
2350int API minijail_run_no_preload(struct minijail *j, const char *filename,
2351 char *const argv[])
2352{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002353 struct minijail_run_config config = {
2354 .filename = filename,
2355 .argv = argv,
2356 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002357 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002358 };
2359 struct minijail_run_status status = {};
2360 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002361}
2362
Samuel Tan63187f42015-10-16 13:01:53 -07002363int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002364 const char *filename,
2365 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002366 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002367 int *pstdin_fd,
2368 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002369 int *pstderr_fd)
2370{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002371 struct minijail_run_config config = {
2372 .filename = filename,
2373 .argv = argv,
2374 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002375 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002376 };
2377 struct minijail_run_status status = {
2378 .pstdin_fd = pstdin_fd,
2379 .pstdout_fd = pstdout_fd,
2380 .pstderr_fd = pstderr_fd,
2381 .pchild_pid = pchild_pid,
2382 };
2383 return minijail_run_internal(j, &config, &status);
Samuel Tan63187f42015-10-16 13:01:53 -07002384}
2385
Dylan Reid0412dcc2017-08-24 11:33:15 -07002386pid_t API minijail_fork(struct minijail *j)
2387{
2388 struct minijail_run_config config = {};
2389 struct minijail_run_status status = {};
2390 return minijail_run_internal(j, &config, &status);
2391}
2392
Dylan Reid18c49c82017-08-25 14:52:27 -07002393static int minijail_run_internal(struct minijail *j,
2394 const struct minijail_run_config *config,
2395 struct minijail_run_status *status_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002396{
Elly Jonese1749eb2011-10-07 13:54:59 -04002397 char *oldenv, *oldenv_copy = NULL;
2398 pid_t child_pid;
2399 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002400 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002401 int stdout_fds[2];
2402 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08002403 int child_sync_pipe_fds[2];
2404 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002405 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002406 /* We need to remember this across the minijail_preexec() call. */
2407 int pid_namespace = j->flags.pids;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -07002408 /*
2409 * Create an init process if we are entering a pid namespace, unless the
2410 * user has explicitly opted out by calling minijail_run_as_init().
2411 */
2412 int do_init = j->flags.do_init && !j->flags.run_as_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002413 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002414
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002415 if (use_preload) {
Dylan Reid0412dcc2017-08-24 11:33:15 -07002416 if (j->hooks_head != NULL)
2417 die("Minijail hooks are not supported with LD_PRELOAD");
2418 if (!config->exec_in_child)
2419 die("minijail_fork is not supported with LD_PRELOAD");
2420
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002421 oldenv = getenv(kLdPreloadEnvVar);
2422 if (oldenv) {
2423 oldenv_copy = strdup(oldenv);
2424 if (!oldenv_copy)
2425 return -ENOMEM;
2426 }
2427
2428 if (setup_preload())
2429 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002430 }
Will Drewryf89aef52011-09-16 16:48:57 -05002431
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002432 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002433 if (j->flags.use_caps && j->caps != 0 &&
2434 !j->flags.set_ambient_caps) {
2435 die("non-empty, non-ambient capabilities are not "
2436 "supported without LD_PRELOAD");
2437 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002438 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002439
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002440 if (use_preload) {
2441 /*
2442 * Before we fork(2) and execve(2) the child process, we need
2443 * to open a pipe(2) to send the minijail configuration over.
2444 */
2445 if (setup_pipe(pipe_fds))
2446 return -EFAULT;
2447 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002448
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002449 /*
2450 * If we want to write to the child process' standard input,
2451 * create the pipe(2) now.
2452 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002453 if (status_out->pstdin_fd) {
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002454 if (pipe(stdin_fds))
2455 return -EFAULT;
2456 }
2457
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002458 /*
2459 * If we want to read from the child process' standard output,
2460 * create the pipe(2) now.
2461 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002462 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002463 if (pipe(stdout_fds))
2464 return -EFAULT;
2465 }
2466
2467 /*
2468 * If we want to read from the child process' standard error,
2469 * create the pipe(2) now.
2470 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002471 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002472 if (pipe(stderr_fds))
2473 return -EFAULT;
2474 }
2475
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002476 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002477 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002478 * or if we need to add the child process to cgroups, create the pipe(2)
2479 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002480 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002481 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002482 sync_child = 1;
2483 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002484 return -EFAULT;
2485 }
2486
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002487 /*
2488 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002489 *
2490 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2491 *
2492 * In multithreaded programs, there are a bunch of locks inside libc,
2493 * some of which may be held by other threads at the time that we call
2494 * minijail_run_pid(). If we call fork(), glibc does its level best to
2495 * ensure that we hold all of these locks before it calls clone()
2496 * internally and drop them after clone() returns, but when we call
2497 * sys_clone(2) directly, all that gets bypassed and we end up with a
2498 * child address space where some of libc's important locks are held by
2499 * other threads (which did not get cloned, and hence will never release
2500 * those locks). This is okay so long as we call exec() immediately
2501 * after, but a bunch of seemingly-innocent libc functions like setenv()
2502 * take locks.
2503 *
2504 * Hence, only call sys_clone() if we need to, in order to get at pid
2505 * namespacing. If we follow this path, the child's address space might
2506 * have broken locks; you may only call functions that do not acquire
2507 * any locks.
2508 *
2509 * Unfortunately, fork() acquires every lock it can get its hands on, as
2510 * previously detailed, so this function is highly likely to deadlock
2511 * later on (see "deadlock here") if we're multithreaded.
2512 *
2513 * We might hack around this by having the clone()d child (init of the
2514 * pid namespace) return directly, rather than leaving the clone()d
2515 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002516 * its fork()ed child return in turn), but that process would be
2517 * crippled with its libc locks potentially broken. We might try
2518 * fork()ing in the parent before we clone() to ensure that we own all
2519 * the locks, but then we have to have the forked child hanging around
2520 * consuming resources (and possibly having file descriptors / shared
2521 * memory regions / etc attached). We'd need to keep the child around to
2522 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002523 *
2524 * TODO(ellyjones): figure out if the "forked child hanging around"
2525 * problem is fixable or not. It would be nice if we worked in this
2526 * case.
2527 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002528 if (pid_namespace) {
2529 int clone_flags = CLONE_NEWPID | SIGCHLD;
2530 if (j->flags.userns)
2531 clone_flags |= CLONE_NEWUSER;
2532 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002533 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002534 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002535 }
Elly Jones761b7412012-06-13 15:49:52 -04002536
Elly Jonese1749eb2011-10-07 13:54:59 -04002537 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002538 if (use_preload) {
2539 free(oldenv_copy);
2540 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002541 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002542 }
Will Drewryf89aef52011-09-16 16:48:57 -05002543
Elly Jonese1749eb2011-10-07 13:54:59 -04002544 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002545 if (use_preload) {
2546 /* Restore parent's LD_PRELOAD. */
2547 if (oldenv_copy) {
2548 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2549 free(oldenv_copy);
2550 } else {
2551 unsetenv(kLdPreloadEnvVar);
2552 }
2553 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002554 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002555
Elly Jonese1749eb2011-10-07 13:54:59 -04002556 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002557
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002558 if (j->flags.forward_signals) {
2559 forward_pid = child_pid;
2560 install_signal_handlers();
2561 }
2562
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002563 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002564 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002565
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002566 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002567 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002568
Dylan Reid0f72ef42017-06-06 15:42:49 -07002569 if (j->rlimit_count)
2570 set_rlimits_or_die(j);
2571
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002572 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002573 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002574
2575 if (sync_child)
2576 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002577
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002578 if (use_preload) {
2579 /* Send marshalled minijail. */
2580 close(pipe_fds[0]); /* read endpoint */
2581 ret = minijail_to_fd(j, pipe_fds[1]);
2582 close(pipe_fds[1]); /* write endpoint */
2583 if (ret) {
2584 kill(j->initpid, SIGKILL);
2585 die("failed to send marshalled minijail");
2586 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002587 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002588
Dylan Reidacfb8be2017-08-25 12:56:51 -07002589 if (status_out->pchild_pid)
2590 *status_out->pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002591
2592 /*
2593 * If we want to write to the child process' standard input,
2594 * set up the write end of the pipe.
2595 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002596 if (status_out->pstdin_fd)
2597 *status_out->pstdin_fd =
2598 setup_pipe_end(stdin_fds, 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002599
2600 /*
2601 * If we want to read from the child process' standard output,
2602 * set up the read end of the pipe.
2603 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002604 if (status_out->pstdout_fd)
2605 *status_out->pstdout_fd =
2606 setup_pipe_end(stdout_fds, 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002607
2608 /*
2609 * If we want to read from the child process' standard error,
2610 * set up the read end of the pipe.
2611 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002612 if (status_out->pstderr_fd)
2613 *status_out->pstderr_fd =
2614 setup_pipe_end(stderr_fds, 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002615
Dylan Reid0412dcc2017-08-24 11:33:15 -07002616 /*
2617 * If forking return the child pid, in the normal exec case
2618 * return 0 for success.
2619 */
2620 if (!config->exec_in_child)
2621 return child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04002622 return 0;
2623 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002624 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002625 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002626
Peter Qiu2860c462015-12-16 15:13:06 -08002627 if (j->flags.reset_signal_mask) {
2628 sigset_t signal_mask;
2629 if (sigemptyset(&signal_mask) != 0)
2630 pdie("sigemptyset failed");
2631 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2632 pdie("sigprocmask failed");
2633 }
2634
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002635 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002636 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002637 int inheritable_fds[kMaxInheritableFdsSize];
2638 size_t size = 0;
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002639 size_t i;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002640 if (use_preload) {
2641 inheritable_fds[size++] = pipe_fds[0];
2642 inheritable_fds[size++] = pipe_fds[1];
2643 }
2644 if (sync_child) {
2645 inheritable_fds[size++] = child_sync_pipe_fds[0];
2646 inheritable_fds[size++] = child_sync_pipe_fds[1];
2647 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002648 if (status_out->pstdin_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002649 inheritable_fds[size++] = stdin_fds[0];
2650 inheritable_fds[size++] = stdin_fds[1];
2651 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002652 if (status_out->pstdout_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002653 inheritable_fds[size++] = stdout_fds[0];
2654 inheritable_fds[size++] = stdout_fds[1];
2655 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002656 if (status_out->pstderr_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002657 inheritable_fds[size++] = stderr_fds[0];
2658 inheritable_fds[size++] = stderr_fds[1];
2659 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002660 for (i = 0; i < j->preserved_fd_count; i++) {
2661 /*
2662 * Preserve all parent_fds. They will be dup2(2)-ed in
2663 * the child later.
2664 */
2665 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
2666 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002667
2668 if (close_open_fds(inheritable_fds, size) < 0)
2669 die("failed to close open file descriptors");
2670 }
2671
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002672 if (redirect_fds(j))
2673 die("failed to set up fd redirections");
2674
Dylan Reidce5b55e2016-01-13 11:04:16 -08002675 if (sync_child)
2676 wait_for_parent_setup(child_sync_pipe_fds);
2677
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002678 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002679 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002680
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002681 /*
2682 * If we want to write to the jailed process' standard input,
2683 * set up the read end of the pipe.
2684 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002685 if (status_out->pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002686 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2687 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002688 die("failed to set up stdin pipe");
2689 }
2690
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002691 /*
2692 * If we want to read from the jailed process' standard output,
2693 * set up the write end of the pipe.
2694 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002695 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002696 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2697 STDOUT_FILENO) < 0)
2698 die("failed to set up stdout pipe");
2699 }
2700
2701 /*
2702 * If we want to read from the jailed process' standard error,
2703 * set up the write end of the pipe.
2704 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002705 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002706 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2707 STDERR_FILENO) < 0)
2708 die("failed to set up stderr pipe");
2709 }
2710
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002711 /*
2712 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2713 * This prevents the jailed process from using the TIOCSTI ioctl
2714 * to push characters into the parent process terminal's input buffer,
2715 * therefore escaping the jail.
Stephen Barber5dd5b1b2017-10-16 23:02:39 -07002716 *
2717 * Since it has just forked, the child will not be a process group
2718 * leader, and this call to setsid() should always succeed.
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002719 */
2720 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2721 isatty(STDERR_FILENO)) {
2722 if (setsid() < 0) {
2723 pdie("setsid() failed");
2724 }
2725 }
2726
Dylan Reid791f5772015-09-14 20:02:42 -07002727 /* If running an init program, let it decide when/how to mount /proc. */
2728 if (pid_namespace && !do_init)
2729 j->flags.remount_proc_ro = 0;
2730
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002731 if (use_preload) {
2732 /* Strip out flags that cannot be inherited across execve(2). */
2733 minijail_preexec(j);
2734 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002735 /*
2736 * If not using LD_PRELOAD, do all jailing before execve(2).
2737 * Note that PID namespaces can only be entered on fork(2),
2738 * so that flag is still cleared.
2739 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002740 j->flags.pids = 0;
2741 }
Dylan Reid0412dcc2017-08-24 11:33:15 -07002742
2743 /*
2744 * Jail this process.
2745 * If forking, return.
2746 * If not, execve(2) the target.
2747 */
Elly Jonese1749eb2011-10-07 13:54:59 -04002748 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002749
Dylan Reid0412dcc2017-08-24 11:33:15 -07002750 if (config->exec_in_child && pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002751 /*
2752 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002753 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002754 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002755 * a child to actually run the program. If |do_init == 0|, we
2756 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002757 *
2758 * If we're multithreaded, we'll probably deadlock here. See
2759 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002760 */
2761 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002762 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002763 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002764 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002765 /*
2766 * Best effort. Don't bother checking the return value.
2767 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002768 prctl(PR_SET_NAME, "minijail-init");
2769 init(child_pid); /* Never returns. */
2770 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002771 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002772
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002773 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
2774
Dylan Reid0412dcc2017-08-24 11:33:15 -07002775 if (!config->exec_in_child)
2776 return 0;
2777
Elly Jonesdd3e8512012-01-23 15:13:38 -05002778 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002779 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002780 * calling process
2781 * -> execve()-ing process
2782 * If we are:
2783 * calling process
2784 * -> init()-ing process
2785 * -> execve()-ing process
2786 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002787 ret = execve(config->filename, config->argv, environ);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002788 if (ret == -1) {
Dylan Reidacfb8be2017-08-25 12:56:51 -07002789 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002790 }
2791 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002792}
2793
Will Drewry6ac91122011-10-21 16:38:58 -05002794int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002795{
2796 int st;
2797 if (kill(j->initpid, SIGTERM))
2798 return -errno;
2799 if (waitpid(j->initpid, &st, 0) < 0)
2800 return -errno;
2801 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002802}
2803
Will Drewry6ac91122011-10-21 16:38:58 -05002804int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002805{
2806 int st;
2807 if (waitpid(j->initpid, &st, 0) < 0)
2808 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002809
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002810 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002811 int error_status = st;
2812 if (WIFSIGNALED(st)) {
2813 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002814 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002815 j->initpid, signum);
2816 /*
2817 * We return MINIJAIL_ERR_JAIL if the process received
2818 * SIGSYS, which happens when a syscall is blocked by
2819 * seccomp filters.
2820 * If not, we do what bash(1) does:
2821 * $? = 128 + signum
2822 */
2823 if (signum == SIGSYS) {
2824 error_status = MINIJAIL_ERR_JAIL;
2825 } else {
2826 error_status = 128 + signum;
2827 }
2828 }
2829 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002830 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002831
2832 int exit_status = WEXITSTATUS(st);
2833 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002834 info("child process %d exited with status %d",
2835 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002836
2837 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002838}
2839
Will Drewry6ac91122011-10-21 16:38:58 -05002840void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002841{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002842 size_t i;
2843
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002844 if (j->flags.seccomp_filter && j->filter_prog) {
2845 free(j->filter_prog->filter);
2846 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002847 }
Mike Frysingerac08a682017-10-10 02:04:50 -04002848 free_mounts_list(j);
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002849 while (j->hooks_head) {
2850 struct hook *c = j->hooks_head;
2851 j->hooks_head = c->next;
2852 free(c);
2853 }
2854 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002855 if (j->user)
2856 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002857 if (j->suppl_gid_list)
2858 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002859 if (j->chrootdir)
2860 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002861 if (j->pid_file_path)
2862 free(j->pid_file_path);
2863 if (j->uidmap)
2864 free(j->uidmap);
2865 if (j->gidmap)
2866 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002867 if (j->hostname)
2868 free(j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08002869 if (j->alt_syscall_table)
2870 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002871 for (i = 0; i < j->cgroup_count; ++i)
2872 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002873 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002874}
Luis Hector Chavez114a9302017-09-05 20:36:58 -07002875
2876void API minijail_log_to_fd(int fd, int min_priority)
2877{
2878 init_logging(LOG_TO_FD, fd, min_priority);
2879}