blob: 9cf3232345a01884462d7501352fbffa1101782e [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;
87 uint32_t cur;
88 uint32_t max;
89};
90
Dylan Reid648b2202015-10-23 00:50:00 -070091struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040092 char *src;
93 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070094 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -070095 char *data;
96 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -070097 unsigned long flags;
98 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040099};
100
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700101struct hook {
102 minijail_hook_t hook;
103 void *payload;
104 minijail_hook_event_t event;
105 struct hook *next;
106};
107
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700108struct preserved_fd {
109 int parent_fd;
110 int child_fd;
111};
112
Will Drewryf89aef52011-09-16 16:48:57 -0500113struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700114 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700115 * WARNING: if you add a flag here you need to make sure it's
116 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700117 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400118 struct {
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700119 int uid : 1;
120 int gid : 1;
Lutz Justen13807cb2017-01-03 17:11:55 +0100121 int inherit_suppl_gids : 1;
122 int set_suppl_gids : 1;
123 int keep_suppl_gids : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700124 int use_caps : 1;
125 int capbset_drop : 1;
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400126 int set_ambient_caps : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700127 int vfs : 1;
128 int enter_vfs : 1;
129 int skip_remount_private : 1;
130 int pids : 1;
131 int ipc : 1;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400132 int uts : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700133 int net : 1;
134 int enter_net : 1;
135 int ns_cgroups : 1;
136 int userns : 1;
137 int disable_setgroups : 1;
138 int seccomp : 1;
139 int remount_proc_ro : 1;
140 int no_new_privs : 1;
141 int seccomp_filter : 1;
142 int seccomp_filter_tsync : 1;
143 int seccomp_filter_logging : 1;
144 int chroot : 1;
145 int pivot_root : 1;
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
Dylan Reid0f72ef42017-06-06 15:42:49 -0700665int API minijail_rlimit(struct minijail *j, int type, uint32_t cur,
666 uint32_t max)
667{
668 size_t i;
669
670 if (j->rlimit_count >= MAX_RLIMITS)
671 return -ENOMEM;
672 /* It's an error if the caller sets the same rlimit multiple times. */
673 for (i = 0; i < j->rlimit_count; i++) {
674 if (j->rlimits[i].type == type)
675 return -EEXIST;
676 }
677
678 j->rlimits[j->rlimit_count].type = type;
679 j->rlimits[j->rlimit_count].cur = cur;
680 j->rlimits[j->rlimit_count].max = max;
681 j->rlimit_count++;
682 return 0;
683}
684
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400685int API minijail_forward_signals(struct minijail *j)
686{
687 j->flags.forward_signals = 1;
688 return 0;
689}
690
Dylan Reid81e23972016-05-18 14:06:35 -0700691int API minijail_mount_with_data(struct minijail *j, const char *src,
692 const char *dest, const char *type,
693 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700694{
Dylan Reid648b2202015-10-23 00:50:00 -0700695 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400696
697 if (*dest != '/')
698 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700699 m = calloc(1, sizeof(*m));
700 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400701 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700702 m->dest = strdup(dest);
703 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400704 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700705 m->src = strdup(src);
706 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400707 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700708 m->type = strdup(type);
709 if (!m->type)
710 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700711 if (data) {
712 m->data = strdup(data);
713 if (!m->data)
714 goto error;
715 m->has_data = 1;
716 }
Dylan Reid648b2202015-10-23 00:50:00 -0700717 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400718
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800719 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400720
Elly Jonesdd3e8512012-01-23 15:13:38 -0500721 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700722 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400723 * containing vfs namespace.
724 */
725 minijail_namespace_vfs(j);
726
Dylan Reid648b2202015-10-23 00:50:00 -0700727 if (j->mounts_tail)
728 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400729 else
Dylan Reid648b2202015-10-23 00:50:00 -0700730 j->mounts_head = m;
731 j->mounts_tail = m;
732 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400733
734 return 0;
735
736error:
Dylan Reid81e23972016-05-18 14:06:35 -0700737 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700738 free(m->src);
739 free(m->dest);
740 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400741 return -ENOMEM;
742}
743
Dylan Reid81e23972016-05-18 14:06:35 -0700744int API minijail_mount(struct minijail *j, const char *src, const char *dest,
745 const char *type, unsigned long flags)
746{
747 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
748}
749
Dylan Reid648b2202015-10-23 00:50:00 -0700750int API minijail_bind(struct minijail *j, const char *src, const char *dest,
751 int writeable)
752{
753 unsigned long flags = MS_BIND;
754
755 if (!writeable)
756 flags |= MS_RDONLY;
757
758 return minijail_mount(j, src, dest, "", flags);
759}
760
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700761int API minijail_add_hook(struct minijail *j, minijail_hook_t hook,
762 void *payload, minijail_hook_event_t event)
763{
764 struct hook *c;
765
766 if (hook == NULL)
767 return -EINVAL;
768 if (event >= MINIJAIL_HOOK_EVENT_MAX)
769 return -EINVAL;
770 c = calloc(1, sizeof(*c));
771 if (!c)
772 return -ENOMEM;
773
774 c->hook = hook;
775 c->payload = payload;
776 c->event = event;
777
778 if (j->hooks_tail)
779 j->hooks_tail->next = c;
780 else
781 j->hooks_head = c;
782 j->hooks_tail = c;
783
784 return 0;
785}
786
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700787int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd)
788{
789 if (parent_fd < 0 || child_fd < 0)
790 return -EINVAL;
791 if (j->preserved_fd_count >= MAX_PRESERVED_FDS)
792 return -ENOMEM;
793 j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd;
794 j->preserved_fds[j->preserved_fd_count].child_fd = child_fd;
795 j->preserved_fd_count++;
796 return 0;
797}
798
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400799static void clear_seccomp_options(struct minijail *j)
800{
801 j->flags.seccomp_filter = 0;
802 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400803 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400804 j->filter_len = 0;
805 j->filter_prog = NULL;
806 j->flags.no_new_privs = 0;
807}
808
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400809static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400810{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400811 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400812 /*
813 * |errno| will be set to EINVAL when seccomp has not been
814 * compiled into the kernel. On certain platforms and kernel
815 * versions this is not a fatal failure. In that case, and only
816 * in that case, disable seccomp and skip loading the filters.
817 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400818 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400819 warn("not loading seccomp filters, seccomp filter not "
820 "supported");
821 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400822 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700823 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400824 /*
825 * If |errno| != EINVAL or seccomp_can_softfail() is false,
826 * we can proceed. Worst case scenario minijail_enter() will
827 * abort() if seccomp fails.
828 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700829 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400830 if (j->flags.seccomp_filter_tsync) {
831 /* Are the seccomp(2) syscall and the TSYNC option supported? */
832 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
833 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
834 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400835 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
836 warn("seccomp(2) syscall not supported");
837 clear_seccomp_options(j);
838 return 0;
839 } else if (saved_errno == EINVAL &&
840 seccomp_can_softfail()) {
841 warn(
842 "seccomp filter thread sync not supported");
843 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400844 return 0;
845 }
846 /*
847 * Similar logic here. If seccomp_can_softfail() is
848 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
849 * we can proceed. Worst case scenario minijail_enter()
850 * will abort() if seccomp or TSYNC fail.
851 */
852 }
853 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400854 return 1;
855}
856
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700857static int parse_seccomp_filters(struct minijail *j, const char *filename,
858 FILE *policy_file)
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400859{
860 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400861 int use_ret_trap =
862 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
863 int allow_logging = j->flags.seccomp_filter_logging;
864
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700865 if (compile_filter(filename, policy_file, fprog, use_ret_trap,
866 allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400867 free(fprog);
868 return -1;
869 }
870
871 j->filter_len = fprog->len;
872 j->filter_prog = fprog;
873 return 0;
874}
875
876void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
877{
878 if (!seccomp_should_parse_filters(j))
879 return;
880
Elly Jonese1749eb2011-10-07 13:54:59 -0400881 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800882 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700883 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400884 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800885
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700886 if (parse_seccomp_filters(j, path, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700887 die("failed to compile seccomp filter BPF program in '%s'",
888 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800889 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400890 fclose(file);
891}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800892
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400893void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
894{
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700895 char *fd_path, *path;
896 FILE *file;
897
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400898 if (!seccomp_should_parse_filters(j))
899 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800900
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700901 file = fdopen(fd, "r");
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400902 if (!file) {
903 pdie("failed to associate stream with fd %d", fd);
904 }
905
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700906 if (asprintf(&fd_path, "/proc/self/fd/%d", fd) == -1)
907 pdie("failed to create path for fd %d", fd);
908 path = realpath(fd_path, NULL);
909 if (path == NULL)
910 pwarn("failed to get path of fd %d", fd);
911 free(fd_path);
912
913 if (parse_seccomp_filters(j, path ? path : "<fd>", file) != 0) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400914 die("failed to compile seccomp filter BPF program from fd %d",
915 fd);
916 }
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700917 free(path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400918 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500919}
920
Andrew Brestickereac28942015-11-11 16:04:46 -0800921int API minijail_use_alt_syscall(struct minijail *j, const char *table)
922{
923 j->alt_syscall_table = strdup(table);
924 if (!j->alt_syscall_table)
925 return -ENOMEM;
926 j->flags.alt_syscall = 1;
927 return 0;
928}
929
Will Drewryf89aef52011-09-16 16:48:57 -0500930struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400931 size_t available;
932 size_t total;
933 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500934};
935
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800936void marshal_state_init(struct marshal_state *state, char *buf,
937 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400938{
939 state->available = available;
940 state->buf = buf;
941 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500942}
943
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800944void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400945{
946 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500947
Elly Jonese1749eb2011-10-07 13:54:59 -0400948 /* Up to |available| will be written. */
949 if (copy_len) {
950 memcpy(state->buf, src, copy_len);
951 state->buf += copy_len;
952 state->available -= copy_len;
953 }
954 /* |total| will contain the expected length. */
955 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500956}
957
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400958void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700959{
960 marshal_append(state, m->src, strlen(m->src) + 1);
961 marshal_append(state, m->dest, strlen(m->dest) + 1);
962 marshal_append(state, m->type, strlen(m->type) + 1);
963 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
964 if (m->has_data)
965 marshal_append(state, m->data, strlen(m->data) + 1);
966 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
967}
968
Will Drewry6ac91122011-10-21 16:38:58 -0500969void minijail_marshal_helper(struct marshal_state *state,
970 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400971{
Dylan Reid648b2202015-10-23 00:50:00 -0700972 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800973 size_t i;
974
Elly Jonese1749eb2011-10-07 13:54:59 -0400975 marshal_append(state, (char *)j, sizeof(*j));
976 if (j->user)
977 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800978 if (j->suppl_gid_list) {
979 marshal_append(state, j->suppl_gid_list,
980 j->suppl_gid_count * sizeof(gid_t));
981 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400982 if (j->chrootdir)
983 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400984 if (j->hostname)
985 marshal_append(state, j->hostname, strlen(j->hostname) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800986 if (j->alt_syscall_table) {
987 marshal_append(state, j->alt_syscall_table,
988 strlen(j->alt_syscall_table) + 1);
989 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800990 if (j->flags.seccomp_filter && j->filter_prog) {
991 struct sock_fprog *fp = j->filter_prog;
992 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800993 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400994 }
Dylan Reid648b2202015-10-23 00:50:00 -0700995 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400996 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400997 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800998 for (i = 0; i < j->cgroup_count; ++i)
999 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -05001000}
1001
Will Drewry6ac91122011-10-21 16:38:58 -05001002size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001003{
1004 struct marshal_state state;
1005 marshal_state_init(&state, NULL, 0);
1006 minijail_marshal_helper(&state, j);
1007 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -05001008}
1009
Elly Jonese1749eb2011-10-07 13:54:59 -04001010int minijail_marshal(const struct minijail *j, char *buf, size_t available)
1011{
1012 struct marshal_state state;
1013 marshal_state_init(&state, buf, available);
1014 minijail_marshal_helper(&state, j);
1015 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -05001016}
1017
Elly Jonese1749eb2011-10-07 13:54:59 -04001018int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
1019{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -08001020 size_t i;
1021 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -05001022 int ret = -EINVAL;
1023
Elly Jonese1749eb2011-10-07 13:54:59 -04001024 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001025 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001026 memcpy((void *)j, serialized, sizeof(*j));
1027 serialized += sizeof(*j);
1028 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001029
Will Drewrybee7ba72011-10-21 20:47:01 -05001030 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001031 j->pid_file_path = NULL;
1032 j->uidmap = NULL;
1033 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001034 j->mounts_head = NULL;
1035 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001036 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001037 j->hooks_head = NULL;
1038 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001039
Elly Jonese1749eb2011-10-07 13:54:59 -04001040 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001041 char *user = consumestr(&serialized, &length);
1042 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001043 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001044 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001045 if (!j->user)
1046 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001047 }
Will Drewryf89aef52011-09-16 16:48:57 -05001048
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001049 if (j->suppl_gid_list) { /* stale pointer */
1050 if (j->suppl_gid_count > NGROUPS_MAX) {
1051 goto bad_gid_list;
1052 }
1053 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1054 void *gid_list_bytes =
1055 consumebytes(gid_list_size, &serialized, &length);
1056 if (!gid_list_bytes)
1057 goto bad_gid_list;
1058
1059 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1060 if (!j->suppl_gid_list)
1061 goto bad_gid_list;
1062
1063 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1064 }
1065
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001066 if (j->chrootdir) { /* stale pointer */
1067 char *chrootdir = consumestr(&serialized, &length);
1068 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001069 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001070 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001071 if (!j->chrootdir)
1072 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001073 }
1074
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001075 if (j->hostname) { /* stale pointer */
1076 char *hostname = consumestr(&serialized, &length);
1077 if (!hostname)
1078 goto bad_hostname;
1079 j->hostname = strdup(hostname);
1080 if (!j->hostname)
1081 goto bad_hostname;
1082 }
1083
Andrew Brestickereac28942015-11-11 16:04:46 -08001084 if (j->alt_syscall_table) { /* stale pointer */
1085 char *alt_syscall_table = consumestr(&serialized, &length);
1086 if (!alt_syscall_table)
1087 goto bad_syscall_table;
1088 j->alt_syscall_table = strdup(alt_syscall_table);
1089 if (!j->alt_syscall_table)
1090 goto bad_syscall_table;
1091 }
1092
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001093 if (j->flags.seccomp_filter && j->filter_len > 0) {
1094 size_t ninstrs = j->filter_len;
1095 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1096 ninstrs > USHRT_MAX)
1097 goto bad_filters;
1098
1099 size_t program_len = ninstrs * sizeof(struct sock_filter);
1100 void *program = consumebytes(program_len, &serialized, &length);
1101 if (!program)
1102 goto bad_filters;
1103
1104 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001105 if (!j->filter_prog)
1106 goto bad_filters;
1107
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001108 j->filter_prog->len = ninstrs;
1109 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001110 if (!j->filter_prog->filter)
1111 goto bad_filter_prog_instrs;
1112
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001113 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001114 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001115
Dylan Reid648b2202015-10-23 00:50:00 -07001116 count = j->mounts_count;
1117 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001118 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001119 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001120 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001121 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001122 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001123 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001124 const char *src = consumestr(&serialized, &length);
1125 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001126 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001127 dest = consumestr(&serialized, &length);
1128 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001129 goto bad_mounts;
1130 type = consumestr(&serialized, &length);
1131 if (!type)
1132 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001133 has_data = consumebytes(sizeof(*has_data), &serialized,
1134 &length);
1135 if (!has_data)
1136 goto bad_mounts;
1137 if (*has_data) {
1138 data = consumestr(&serialized, &length);
1139 if (!data)
1140 goto bad_mounts;
1141 }
Dylan Reid648b2202015-10-23 00:50:00 -07001142 flags = consumebytes(sizeof(*flags), &serialized, &length);
1143 if (!flags)
1144 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001145 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001146 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001147 }
1148
Dylan Reid605ce7f2016-01-19 19:21:00 -08001149 count = j->cgroup_count;
1150 j->cgroup_count = 0;
1151 for (i = 0; i < count; ++i) {
1152 char *cgroup = consumestr(&serialized, &length);
1153 if (!cgroup)
1154 goto bad_cgroups;
1155 j->cgroups[i] = strdup(cgroup);
1156 if (!j->cgroups[i])
1157 goto bad_cgroups;
1158 ++j->cgroup_count;
1159 }
1160
Elly Jonese1749eb2011-10-07 13:54:59 -04001161 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001162
Dylan Reid605ce7f2016-01-19 19:21:00 -08001163bad_cgroups:
Mike Frysingerac08a682017-10-10 02:04:50 -04001164 free_mounts_list(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001165 for (i = 0; i < j->cgroup_count; ++i)
1166 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001167bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001168 if (j->flags.seccomp_filter && j->filter_len > 0) {
1169 free(j->filter_prog->filter);
1170 free(j->filter_prog);
1171 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001172bad_filter_prog_instrs:
1173 if (j->filter_prog)
1174 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001175bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001176 if (j->alt_syscall_table)
1177 free(j->alt_syscall_table);
1178bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001179 if (j->chrootdir)
1180 free(j->chrootdir);
1181bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001182 if (j->hostname)
1183 free(j->hostname);
1184bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001185 if (j->suppl_gid_list)
1186 free(j->suppl_gid_list);
1187bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001188 if (j->user)
1189 free(j->user);
1190clear_pointers:
1191 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001192 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001193 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001194 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001195 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001196 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001197out:
1198 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001199}
1200
Mike Frysinger33ffef32017-01-13 19:53:19 -05001201struct dev_spec {
1202 const char *name;
1203 mode_t mode;
1204 dev_t major, minor;
1205};
1206
1207static const struct dev_spec device_nodes[] = {
1208 {
1209 "null",
1210 S_IFCHR | 0666, 1, 3,
1211 },
1212 {
1213 "zero",
1214 S_IFCHR | 0666, 1, 5,
1215 },
1216 {
1217 "full",
1218 S_IFCHR | 0666, 1, 7,
1219 },
1220 {
1221 "urandom",
1222 S_IFCHR | 0444, 1, 9,
1223 },
1224 {
1225 "tty",
1226 S_IFCHR | 0666, 5, 0,
1227 },
1228};
1229
1230struct dev_sym_spec {
1231 const char *source, *dest;
1232};
1233
1234static const struct dev_sym_spec device_symlinks[] = {
1235 { "ptmx", "pts/ptmx", },
1236 { "fd", "/proc/self/fd", },
1237 { "stdin", "fd/0", },
1238 { "stdout", "fd/1", },
1239 { "stderr", "fd/2", },
1240};
1241
1242/*
1243 * Clean up the temporary dev path we had setup previously. In case of errors,
1244 * we don't want to go leaking empty tempdirs.
1245 */
1246static void mount_dev_cleanup(char *dev_path)
1247{
1248 umount2(dev_path, MNT_DETACH);
1249 rmdir(dev_path);
1250 free(dev_path);
1251}
1252
1253/*
1254 * Set up the pseudo /dev path at the temporary location.
1255 * See mount_dev_finalize for more details.
1256 */
1257static int mount_dev(char **dev_path_ret)
1258{
1259 int ret;
1260 int dev_fd;
1261 size_t i;
1262 mode_t mask;
1263 char *dev_path;
1264
1265 /*
1266 * Create a temp path for the /dev init. We'll relocate this to the
1267 * final location later on in the startup process.
1268 */
1269 dev_path = *dev_path_ret = strdup("/tmp/minijail.dev.XXXXXX");
1270 if (dev_path == NULL || mkdtemp(dev_path) == NULL)
1271 pdie("could not create temp path for /dev");
1272
1273 /* Set up the empty /dev mount point first. */
1274 ret = mount("minijail-devfs", dev_path, "tmpfs",
1275 MS_NOEXEC | MS_NOSUID, "size=5M,mode=755");
1276 if (ret) {
1277 rmdir(dev_path);
1278 return ret;
1279 }
1280
1281 /* We want to set the mode directly from the spec. */
1282 mask = umask(0);
1283
1284 /* Get a handle to the temp dev path for *at funcs below. */
1285 dev_fd = open(dev_path, O_DIRECTORY|O_PATH|O_CLOEXEC);
1286 if (dev_fd < 0) {
1287 ret = 1;
1288 goto done;
1289 }
1290
1291 /* Create all the nodes in /dev. */
1292 for (i = 0; i < ARRAY_SIZE(device_nodes); ++i) {
1293 const struct dev_spec *ds = &device_nodes[i];
1294 ret = mknodat(dev_fd, ds->name, ds->mode,
1295 makedev(ds->major, ds->minor));
1296 if (ret)
1297 goto done;
1298 }
1299
1300 /* Create all the symlinks in /dev. */
1301 for (i = 0; i < ARRAY_SIZE(device_symlinks); ++i) {
1302 const struct dev_sym_spec *ds = &device_symlinks[i];
1303 ret = symlinkat(ds->dest, dev_fd, ds->source);
1304 if (ret)
1305 goto done;
1306 }
1307
1308 /* Restore old mask. */
1309 done:
1310 close(dev_fd);
1311 umask(mask);
1312
1313 if (ret)
1314 mount_dev_cleanup(dev_path);
1315
1316 return ret;
1317}
1318
1319/*
1320 * Relocate the temporary /dev mount to its final /dev place.
1321 * We have to do this two step process so people can bind mount extra
1322 * /dev paths like /dev/log.
1323 */
1324static int mount_dev_finalize(const struct minijail *j, char *dev_path)
1325{
1326 int ret = -1;
1327 char *dest = NULL;
1328
1329 /* Unmount the /dev mount if possible. */
1330 if (umount2("/dev", MNT_DETACH))
1331 goto done;
1332
1333 if (asprintf(&dest, "%s/dev", j->chrootdir ? : "") < 0)
1334 goto done;
1335
1336 if (mount(dev_path, dest, NULL, MS_MOVE, NULL))
1337 goto done;
1338
1339 ret = 0;
1340 done:
1341 free(dest);
1342 mount_dev_cleanup(dev_path);
1343
1344 return ret;
1345}
1346
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001347/*
1348 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001349 * @j Minijail these mounts are for
1350 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001351 *
1352 * Returns 0 for success.
1353 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001354static int mount_one(const struct minijail *j, struct mountpoint *m,
1355 const char *dev_path)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001356{
Dylan Reid648b2202015-10-23 00:50:00 -07001357 int ret;
1358 char *dest;
1359 int remount_ro = 0;
1360
Mike Frysinger33ffef32017-01-13 19:53:19 -05001361 /* We assume |dest| has a leading "/". */
1362 if (dev_path && strncmp("/dev/", m->dest, 5) == 0) {
1363 /* Since the temp path is rooted at /dev, skip that dest part. */
1364 if (asprintf(&dest, "%s%s", dev_path, m->dest + 4) < 0)
1365 return -ENOMEM;
1366 } else {
Mike Frysingerac08a682017-10-10 02:04:50 -04001367 if (asprintf(&dest, "%s%s", j->chrootdir ?: "", m->dest) < 0)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001368 return -ENOMEM;
1369 }
Dylan Reid648b2202015-10-23 00:50:00 -07001370
Mike Frysinger33ffef32017-01-13 19:53:19 -05001371 ret = setup_mount_destination(m->src, dest, j->uid, j->gid,
1372 (m->flags & MS_BIND));
1373 if (ret) {
1374 pwarn("creating mount target '%s' failed", dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001375 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001376 }
Dylan Reideec77962016-06-30 19:35:10 -07001377
Dylan Reid648b2202015-10-23 00:50:00 -07001378 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001379 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1380 * can't both be specified in the original bind mount.
1381 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001382 */
1383 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1384 remount_ro = 1;
1385 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001386 }
Dylan Reid648b2202015-10-23 00:50:00 -07001387
Dylan Reid81e23972016-05-18 14:06:35 -07001388 ret = mount(m->src, dest, m->type, m->flags, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001389 if (ret) {
1390 pwarn("mount: %s -> %s", m->src, dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001391 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001392 }
Dylan Reid648b2202015-10-23 00:50:00 -07001393
1394 if (remount_ro) {
1395 m->flags |= MS_RDONLY;
1396 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001397 m->flags | MS_REMOUNT, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001398 if (ret) {
1399 pwarn("bind ro: %s -> %s", m->src, dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001400 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001401 }
Dylan Reid648b2202015-10-23 00:50:00 -07001402 }
1403
Elly Jones51a5b6c2011-10-12 19:09:26 -04001404 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001405 if (m->next)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001406 return mount_one(j, m->next, dev_path);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001407 return 0;
1408
1409error:
1410 free(dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001411 return ret;
1412}
1413
Mike Frysingerac08a682017-10-10 02:04:50 -04001414static void process_mounts_or_die(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001415{
Mike Frysingerac08a682017-10-10 02:04:50 -04001416 /*
1417 * We have to mount /dev first in case there are bind mounts from
1418 * the original /dev into the new unique tmpfs one.
1419 */
1420 char *dev_path = NULL;
1421 if (j->flags.mount_dev && mount_dev(&dev_path))
1422 pdie("mount_dev failed");
Dylan Reid648b2202015-10-23 00:50:00 -07001423
Mike Frysingerac08a682017-10-10 02:04:50 -04001424 if (j->mounts_head && mount_one(j, j->mounts_head, dev_path)) {
1425 if (dev_path) {
1426 int saved_errno = errno;
1427 mount_dev_cleanup(dev_path);
1428 errno = saved_errno;
1429 }
1430 pdie("mount_one failed");
1431 }
Mike Frysinger33ffef32017-01-13 19:53:19 -05001432
1433 /*
Mike Frysingerac08a682017-10-10 02:04:50 -04001434 * Once all bind mounts have been processed, move the temp dev to
1435 * its final /dev home.
Mike Frysinger33ffef32017-01-13 19:53:19 -05001436 */
1437 if (j->flags.mount_dev && mount_dev_finalize(j, dev_path))
Mike Frysingerac08a682017-10-10 02:04:50 -04001438 pdie("mount_dev_finalize failed");
1439}
Elly Jones51a5b6c2011-10-12 19:09:26 -04001440
Mike Frysingerac08a682017-10-10 02:04:50 -04001441static int enter_chroot(const struct minijail *j)
1442{
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001443 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1444
Elly Jones51a5b6c2011-10-12 19:09:26 -04001445 if (chroot(j->chrootdir))
1446 return -errno;
1447
1448 if (chdir("/"))
1449 return -errno;
1450
1451 return 0;
1452}
1453
Mike Frysingerac08a682017-10-10 02:04:50 -04001454static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001455{
Mike Frysingerac08a682017-10-10 02:04:50 -04001456 int oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001457
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001458 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1459
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001460 /*
1461 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001462 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001463 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001464 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001465 if (oldroot < 0)
1466 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001467 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001468 if (newroot < 0)
1469 pdie("failed to open %s for fchdir", j->chrootdir);
1470
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001471 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001472 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001473 * do a self bind mount.
1474 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001475 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1476 pdie("failed to bind mount '%s'", j->chrootdir);
1477 if (chdir(j->chrootdir))
1478 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001479 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001480 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001481
1482 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001483 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001484 * change to the old root and unmount it.
1485 */
1486 if (fchdir(oldroot))
1487 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001488
1489 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001490 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1491 * there could be a shared mount point under |oldroot|. In that case,
1492 * mounts under this shared mount point will be unmounted below, and
1493 * this unmounting will propagate to the original mount namespace
1494 * (because the mount point is shared). To prevent this unexpected
1495 * unmounting, remove these mounts from their peer groups by recursively
1496 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001497 */
1498 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001499 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001500 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001501 if (umount2(".", MNT_DETACH))
1502 pdie("umount(/)");
1503 /* Change back to the new root. */
1504 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001505 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001506 if (close(oldroot))
1507 return -errno;
1508 if (close(newroot))
1509 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001510 if (chroot("/"))
1511 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001512 /* Set correct CWD for getcwd(3). */
1513 if (chdir("/"))
1514 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001515
1516 return 0;
1517}
1518
Martin Pelikánab9eb442017-01-25 11:53:58 +11001519static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001520{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001521 const char fmt[] = "size=%zu,mode=1777";
1522 /* Count for the user storing ULLONG_MAX literally + extra space. */
1523 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1524 int ret;
1525
1526 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1527
1528 if (ret <= 0)
1529 pdie("tmpfs size spec error");
1530 else if ((size_t)ret >= sizeof(data))
1531 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001532 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001533 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001534}
1535
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001536static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001537{
1538 const char *kProcPath = "/proc";
1539 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001540 /*
1541 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001542 * /proc in our namespace, which means using MS_REMOUNT here would
1543 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001544 * namespace (!). Instead, remove their mount from our namespace lazily
1545 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001546 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001547 if (umount2(kProcPath, MNT_DETACH)) {
1548 /*
1549 * If we are in a new user namespace, umount(2) will fail.
1550 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1551 */
1552 if (j->flags.userns) {
1553 info("umount(/proc, MNT_DETACH) failed, "
1554 "this is expected when using user namespaces");
1555 } else {
1556 return -errno;
1557 }
1558 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001559 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001560 return -errno;
1561 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001562}
1563
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001564static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001565{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001566 kill(j->initpid, SIGKILL);
1567 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001568}
1569
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001570static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001571{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001572 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001573 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001574}
1575
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001576static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001577{
1578 size_t i;
1579
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001580 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001581 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001582 kill_child_and_die(j, "failed to add to cgroups");
1583 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001584}
1585
Dylan Reid0f72ef42017-06-06 15:42:49 -07001586static void set_rlimits_or_die(const struct minijail *j)
1587{
1588 size_t i;
1589
1590 for (i = 0; i < j->rlimit_count; ++i) {
1591 struct rlimit limit;
1592 limit.rlim_cur = j->rlimits[i].cur;
1593 limit.rlim_max = j->rlimits[i].max;
1594 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1595 kill_child_and_die(j, "failed to set rlimit");
1596 }
1597}
1598
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001599static void write_ugid_maps_or_die(const struct minijail *j)
1600{
1601 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1602 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001603 if (j->gidmap && j->flags.disable_setgroups) {
1604 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1605 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001606 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001607 if (ret == -ENOENT) {
1608 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1609 warn("could not disable setgroups(2)");
1610 } else
1611 kill_child_and_die(j, "failed to disable setgroups(2)");
1612 }
1613 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001614 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1615 kill_child_and_die(j, "failed to write gid_map");
1616}
1617
1618static void enter_user_namespace(const struct minijail *j)
1619{
Luis Hector Chavez71323552017-09-05 09:17:22 -07001620 int uid = j->flags.uid ? j->uid : 0;
1621 int gid = j->flags.gid ? j->gid : 0;
1622 if (j->gidmap && setresgid(gid, gid, gid)) {
1623 pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid,
1624 gid);
1625 }
1626 if (j->uidmap && setresuid(uid, uid, uid)) {
1627 pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid,
1628 uid);
1629 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001630}
1631
1632static void parent_setup_complete(int *pipe_fds)
1633{
1634 close(pipe_fds[0]);
1635 close(pipe_fds[1]);
1636}
1637
1638/*
1639 * wait_for_parent_setup: Called by the child process to wait for any
1640 * further parent-side setup to complete before continuing.
1641 */
1642static void wait_for_parent_setup(int *pipe_fds)
1643{
1644 char buf;
1645
1646 close(pipe_fds[1]);
1647
1648 /* Wait for parent to complete setup and close the pipe. */
1649 if (read(pipe_fds[0], &buf, 1) != 0)
1650 die("failed to sync with parent");
1651 close(pipe_fds[0]);
1652}
1653
1654static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001655{
Lutz Justen13807cb2017-01-03 17:11:55 +01001656 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1657 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001658 die("can only do one of inherit, keep, or set supplementary "
1659 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001660 }
1661
Lutz Justen13807cb2017-01-03 17:11:55 +01001662 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001663 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001664 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001665 } else if (j->flags.set_suppl_gids) {
1666 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001667 pdie("setgroups(suppl_gids) failed");
Luis Hector Chavez71323552017-09-05 09:17:22 -07001668 } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001669 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001670 * Only attempt to clear supplementary groups if we are changing
Luis Hector Chavez71323552017-09-05 09:17:22 -07001671 * users or groups, and if the caller did not request to disable
1672 * setgroups (used when entering a user namespace as a
1673 * non-privileged user).
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001674 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001675 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001676 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001677 }
1678
1679 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001680 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001681
1682 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001683 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001684}
1685
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001686static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1687{
1688 const uint64_t one = 1;
1689 unsigned int i;
1690 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1691 if (keep_mask & (one << i))
1692 continue;
1693 if (prctl(PR_CAPBSET_DROP, i))
1694 pdie("could not drop capability from bounding set");
1695 }
1696}
1697
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001698static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001699{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001700 if (!j->flags.use_caps)
1701 return;
1702
Elly Jonese1749eb2011-10-07 13:54:59 -04001703 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001704 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001705 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001706 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001707 unsigned int i;
1708 if (!caps)
1709 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001710 if (cap_clear(caps))
1711 die("can't clear caps");
1712
1713 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001714 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001715 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001716 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001717 flag[0] = i;
1718 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001719 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001720 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001721 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001722 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001723 die("can't add inheritable cap");
1724 }
1725 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001726 die("can't apply initial cleaned capset");
1727
1728 /*
1729 * Instead of dropping bounding set first, do it here in case
1730 * the caller had a more permissive bounding set which could
1731 * have been used above to raise a capability that wasn't already
1732 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1733 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001734 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001735
1736 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001737 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001738 flag[0] = CAP_SETPCAP;
1739 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1740 die("can't clear effective cap");
1741 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1742 die("can't clear permitted cap");
1743 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1744 die("can't clear inheritable cap");
1745 }
1746
1747 if (cap_set_proc(caps))
1748 die("can't apply final cleaned capset");
1749
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001750 /*
1751 * If ambient capabilities are supported, clear all capabilities first,
1752 * then raise the requested ones.
1753 */
1754 if (j->flags.set_ambient_caps) {
1755 if (!cap_ambient_supported()) {
1756 pdie("ambient capabilities not supported");
1757 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001758 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1759 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001760 pdie("can't clear ambient capabilities");
1761 }
1762
1763 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1764 if (!(j->caps & (one << i)))
1765 continue;
1766
1767 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1768 0) != 0) {
1769 pdie("prctl(PR_CAP_AMBIENT, "
1770 "PR_CAP_AMBIENT_RAISE, %u) failed",
1771 i);
1772 }
1773 }
1774 }
1775
Kees Cook323878a2013-02-05 15:35:24 -08001776 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001777}
1778
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001779static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001780{
1781 /*
1782 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1783 * in the kernel source tree for an explanation of the parameters.
1784 */
1785 if (j->flags.no_new_privs) {
1786 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1787 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1788 }
1789
1790 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001791 * Code running with ASan
1792 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1793 * will make system calls not included in the syscall filter policy,
1794 * which will likely crash the program. Skip setting seccomp filter in
1795 * that case.
1796 * 'running_with_asan()' has no inputs and is completely defined at
1797 * build time, so this cannot be used by an attacker to skip setting
1798 * seccomp filter.
1799 */
1800 if (j->flags.seccomp_filter && running_with_asan()) {
1801 warn("running with ASan, not setting seccomp filter");
1802 return;
1803 }
1804
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001805 if (j->flags.seccomp_filter) {
1806 if (j->flags.seccomp_filter_logging) {
1807 /*
1808 * If logging seccomp filter failures,
1809 * install the SIGSYS handler first.
1810 */
1811 if (install_sigsys_handler())
1812 pdie("failed to install SIGSYS handler");
1813 warn("logging seccomp filter failures");
1814 } else if (j->flags.seccomp_filter_tsync) {
1815 /*
1816 * If setting thread sync,
1817 * reset the SIGSYS signal handler so that
1818 * the entire thread group is killed.
1819 */
1820 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1821 pdie("failed to reset SIGSYS disposition");
1822 info("reset SIGSYS disposition");
1823 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001824 }
1825
1826 /*
1827 * Install the syscall filter.
1828 */
1829 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001830 if (j->flags.seccomp_filter_tsync) {
1831 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1832 SECCOMP_FILTER_FLAG_TSYNC,
1833 j->filter_prog)) {
1834 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001835 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001836 } else {
1837 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1838 j->filter_prog)) {
1839 pdie("prctl(seccomp_filter) failed");
1840 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001841 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001842 }
1843}
1844
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001845static pid_t forward_pid = -1;
1846
1847static void forward_signal(__attribute__((unused)) int nr,
1848 __attribute__((unused)) siginfo_t *siginfo,
1849 __attribute__((unused)) void *void_context)
1850{
1851 if (forward_pid != -1) {
1852 kill(forward_pid, nr);
1853 }
1854}
1855
1856static void install_signal_handlers(void)
1857{
1858 struct sigaction act;
1859
1860 memset(&act, 0, sizeof(act));
1861 act.sa_sigaction = &forward_signal;
1862 act.sa_flags = SA_SIGINFO | SA_RESTART;
1863
1864 /* Handle all signals, except SIGCHLD. */
1865 for (int nr = 1; nr < NSIG; nr++) {
1866 /*
1867 * We don't care if we get EINVAL: that just means that we
1868 * can't handle this signal, so let's skip it and continue.
1869 */
1870 sigaction(nr, &act, NULL);
1871 }
1872 /* Reset SIGCHLD's handler. */
1873 signal(SIGCHLD, SIG_DFL);
1874
1875 /* Handle real-time signals. */
1876 for (int nr = SIGRTMIN; nr <= SIGRTMAX; nr++) {
1877 sigaction(nr, &act, NULL);
1878 }
1879}
1880
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001881static const char *lookup_hook_name(minijail_hook_event_t event)
1882{
1883 switch (event) {
1884 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
1885 return "pre-drop-caps";
1886 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
1887 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001888 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
1889 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001890 case MINIJAIL_HOOK_EVENT_MAX:
1891 /*
1892 * Adding this in favor of a default case to force the
1893 * compiler to error out if a new enum value is added.
1894 */
1895 break;
1896 }
1897 return "unknown";
1898}
1899
1900static void run_hooks_or_die(const struct minijail *j,
1901 minijail_hook_event_t event)
1902{
1903 int rc;
1904 int hook_index = 0;
1905 for (struct hook *c = j->hooks_head; c; c = c->next) {
1906 if (c->event != event)
1907 continue;
1908 rc = c->hook(c->payload);
1909 if (rc != 0) {
1910 errno = -rc;
1911 pdie("%s hook (index %d) failed",
1912 lookup_hook_name(event), hook_index);
1913 }
1914 /* Only increase the index within the same hook event type. */
1915 ++hook_index;
1916 }
1917}
1918
Will Drewry6ac91122011-10-21 16:38:58 -05001919void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001920{
Dylan Reidf682d472015-09-17 21:39:07 -07001921 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001922 * If we're dropping caps, get the last valid cap from /proc now,
1923 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001924 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001925 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001926 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001927 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001928
Elly Jonese1749eb2011-10-07 13:54:59 -04001929 if (j->flags.pids)
1930 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001931 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001932
Lutz Justen13807cb2017-01-03 17:11:55 +01001933 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001934 die("cannot inherit supplementary groups without setting a "
1935 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001936
Elly Jonesdd3e8512012-01-23 15:13:38 -05001937 /*
1938 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001939 * so we don't even try. If any of our operations fail, we abort() the
1940 * entire process.
1941 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001942 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001943 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001944
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001945 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001946 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001947 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001948 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001949 * Unless asked not to, remount all filesystems as private.
1950 * If they are shared, new bind mounts will creep out of our
1951 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001952 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1953 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001954 if (!j->flags.skip_remount_private) {
1955 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001956 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1957 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001958 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001959 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001960
Dylan Reidf7942472015-11-18 17:55:26 -08001961 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001962 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001963 }
1964
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001965 if (j->flags.uts) {
1966 if (unshare(CLONE_NEWUTS))
1967 pdie("unshare(CLONE_NEWUTS) failed");
1968
1969 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
1970 pdie("sethostname(%s) failed", j->hostname);
1971 }
1972
Dylan Reid1102f5a2015-09-15 11:52:20 -07001973 if (j->flags.enter_net) {
1974 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001975 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001976 } else if (j->flags.net) {
1977 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001978 pdie("unshare(CLONE_NEWNET) failed");
1979 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001980 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001981
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001982 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001983 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001984
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08001985 if (j->flags.new_session_keyring) {
1986 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
1987 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
1988 }
1989
Mike Frysingerac08a682017-10-10 02:04:50 -04001990 /* We have to process all the mounts before we chroot/pivot_root. */
1991 process_mounts_or_die(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001992
Mike Frysingerac08a682017-10-10 02:04:50 -04001993 if (j->flags.chroot && enter_chroot(j))
Mike Frysinger33ffef32017-01-13 19:53:19 -05001994 pdie("chroot");
Mike Frysinger33ffef32017-01-13 19:53:19 -05001995
Mike Frysingerac08a682017-10-10 02:04:50 -04001996 if (j->flags.pivot_root && enter_pivot_root(j))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001997 pdie("pivot_root");
1998
Martin Pelikánab9eb442017-01-25 11:53:58 +11001999 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07002000 pdie("mount_tmp");
2001
Dylan Reid791f5772015-09-14 20:02:42 -07002002 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04002003 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04002004
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002005 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
2006
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002007 /*
2008 * If we're only dropping capabilities from the bounding set, but not
2009 * from the thread's (permitted|inheritable|effective) sets, do it now.
2010 */
2011 if (j->flags.capbset_drop) {
2012 drop_capbset(j->cap_bset, last_valid_cap);
2013 }
2014
2015 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002016 /*
2017 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04002018 * capability to change uids, our attempt to use setuid()
2019 * below will fail. Hang on to root caps across setuid(), then
2020 * lock securebits.
2021 */
2022 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002023 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002024
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -07002025 if (lock_securebits(j->securebits_skip_mask) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002026 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002027 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002028 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002029
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002030 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002031 /*
2032 * If we're setting no_new_privs, we can drop privileges
2033 * before setting seccomp filter. This way filter policies
2034 * don't need to allow privilege-dropping syscalls.
2035 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002036 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002037 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002038 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002039 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002040 /*
2041 * If we're not setting no_new_privs,
2042 * we need to set seccomp filter *before* dropping privileges.
2043 * WARNING: this means that filter policies *must* allow
2044 * setgroups()/setresgid()/setresuid() for dropping root and
2045 * capget()/capset()/prctl() for dropping caps.
2046 */
2047 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002048 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002049 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04002050 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002051
Elly Jonesdd3e8512012-01-23 15:13:38 -05002052 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08002053 * Select the specified alternate syscall table. The table must not
2054 * block prctl(2) if we're using seccomp as well.
2055 */
2056 if (j->flags.alt_syscall) {
2057 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002058 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08002059 }
2060
2061 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05002062 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04002063 * privilege-dropping syscalls :)
2064 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002065 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04002066 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002067 warn("seccomp not supported");
2068 return;
2069 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002070 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002071 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002072}
2073
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002074/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04002075static int init_exitstatus = 0;
2076
Will Drewry6ac91122011-10-21 16:38:58 -05002077void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04002078{
2079 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04002080}
2081
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002082void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04002083{
2084 pid_t pid;
2085 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002086 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002087 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002088 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002089 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002090 /*
2091 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04002092 * left inside our pid namespace or we get a signal.
2093 */
2094 if (pid == rootpid)
2095 init_exitstatus = status;
2096 }
2097 if (!WIFEXITED(init_exitstatus))
2098 _exit(MINIJAIL_ERR_INIT);
2099 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04002100}
2101
Will Drewry6ac91122011-10-21 16:38:58 -05002102int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002103{
2104 size_t sz = 0;
2105 size_t bytes = read(fd, &sz, sizeof(sz));
2106 char *buf;
2107 int r;
2108 if (sizeof(sz) != bytes)
2109 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002110 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04002111 return -E2BIG;
2112 buf = malloc(sz);
2113 if (!buf)
2114 return -ENOMEM;
2115 bytes = read(fd, buf, sz);
2116 if (bytes != sz) {
2117 free(buf);
2118 return -EINVAL;
2119 }
2120 r = minijail_unmarshal(j, buf, sz);
2121 free(buf);
2122 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002123}
2124
Will Drewry6ac91122011-10-21 16:38:58 -05002125int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04002126{
2127 char *buf;
2128 size_t sz = minijail_size(j);
2129 ssize_t written;
2130 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04002131
Elly Jonese1749eb2011-10-07 13:54:59 -04002132 if (!sz)
2133 return -EINVAL;
2134 buf = malloc(sz);
2135 r = minijail_marshal(j, buf, sz);
2136 if (r) {
2137 free(buf);
2138 return r;
2139 }
2140 /* Sends [size][minijail]. */
2141 written = write(fd, &sz, sizeof(sz));
2142 if (written != sizeof(sz)) {
2143 free(buf);
2144 return -EFAULT;
2145 }
2146 written = write(fd, buf, sz);
2147 if (written < 0 || (size_t) written != sz) {
2148 free(buf);
2149 return -EFAULT;
2150 }
2151 free(buf);
2152 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002153}
Elly Jonescd7a9042011-07-22 13:56:51 -04002154
Will Drewry6ac91122011-10-21 16:38:58 -05002155int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04002156{
Daniel Erat5b7a3182015-08-19 16:06:22 -06002157#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002158 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002159 return 0;
2160#else
Elly Jonese1749eb2011-10-07 13:54:59 -04002161 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
2162 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
2163 if (!newenv)
2164 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04002165
Elly Jonese1749eb2011-10-07 13:54:59 -04002166 /* Only insert a separating space if we have something to separate... */
2167 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
2168 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04002169
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002170 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002171 setenv(kLdPreloadEnvVar, newenv, 1);
2172 free(newenv);
2173 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002174#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04002175}
2176
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002177static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04002178{
2179 int r = pipe(fds);
2180 char fd_buf[11];
2181 if (r)
2182 return r;
2183 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2184 if (r <= 0)
2185 return -EINVAL;
2186 setenv(kFdEnvVar, fd_buf, 1);
2187 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05002188}
2189
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002190static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002191{
2192 const char *kFdPath = "/proc/self/fd";
2193
2194 DIR *d = opendir(kFdPath);
2195 struct dirent *dir_entry;
2196
2197 if (d == NULL)
2198 return -1;
2199 int dir_fd = dirfd(d);
2200 while ((dir_entry = readdir(d)) != NULL) {
2201 size_t i;
2202 char *end;
2203 bool should_close = true;
2204 const int fd = strtol(dir_entry->d_name, &end, 10);
2205
2206 if ((*end) != '\0') {
2207 continue;
2208 }
2209 /*
2210 * We might have set up some pipes that we want to share with
2211 * the parent process, and should not be closed.
2212 */
2213 for (i = 0; i < size; ++i) {
2214 if (fd == inheritable_fds[i]) {
2215 should_close = false;
2216 break;
2217 }
2218 }
2219 /* Also avoid closing the directory fd. */
2220 if (should_close && fd != dir_fd)
2221 close(fd);
2222 }
2223 closedir(d);
2224 return 0;
2225}
2226
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002227static int redirect_fds(struct minijail *j)
2228{
2229 size_t i, i2;
2230 int closeable;
2231 for (i = 0; i < j->preserved_fd_count; i++) {
2232 if (dup2(j->preserved_fds[i].parent_fd,
2233 j->preserved_fds[i].child_fd) == -1) {
2234 return -1;
2235 }
2236 }
2237 /*
2238 * After all fds have been duped, we are now free to close all parent
2239 * fds that are *not* child fds.
2240 */
2241 for (i = 0; i < j->preserved_fd_count; i++) {
2242 closeable = true;
2243 for (i2 = 0; i2 < j->preserved_fd_count; i2++) {
2244 closeable &= j->preserved_fds[i].parent_fd !=
2245 j->preserved_fds[i2].child_fd;
2246 }
2247 if (closeable)
2248 close(j->preserved_fds[i].parent_fd);
2249 }
2250 return 0;
2251}
2252
Dylan Reidacfb8be2017-08-25 12:56:51 -07002253/*
2254 * Structure that specifies how to start a minijail.
2255 *
Dylan Reid0412dcc2017-08-24 11:33:15 -07002256 * filename - The program to exec in the child. Required if `exec_in_child` = 1.
2257 * argv - Arguments for the child program. Required if `exec_in_child` = 1.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002258 * use_preload - If true use LD_PRELOAD.
Dylan Reid0412dcc2017-08-24 11:33:15 -07002259 * exec_in_child - If true, run `filename`. Otherwise, the child will return to
2260 * the caller.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002261 */
2262struct minijail_run_config {
2263 const char *filename;
2264 char *const *argv;
2265 int use_preload;
Dylan Reid0412dcc2017-08-24 11:33:15 -07002266 int exec_in_child;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002267};
2268
2269/*
2270 * Set of pointers to fill with values from minijail_run.
2271 * All arguments are allowed to be NULL if unused.
2272 *
2273 * pstdin_fd - Filled with stdin pipe if non-NULL.
2274 * pstdout_fd - Filled with stdout pipe if non-NULL.
2275 * pstderr_fd - Filled with stderr pipe if non-NULL.
2276 * pchild_pid - Filled with the pid of the child process if non-NULL.
2277 */
2278struct minijail_run_status {
2279 int *pstdin_fd;
2280 int *pstdout_fd;
2281 int *pstderr_fd;
2282 pid_t *pchild_pid;
2283};
2284
Dylan Reid18c49c82017-08-25 14:52:27 -07002285static int minijail_run_internal(struct minijail *j,
2286 const struct minijail_run_config *config,
2287 struct minijail_run_status *status_out);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002288
Will Drewry6ac91122011-10-21 16:38:58 -05002289int API minijail_run(struct minijail *j, const char *filename,
2290 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002291{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002292 struct minijail_run_config config = {
2293 .filename = filename,
2294 .argv = argv,
2295 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002296 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002297 };
2298 struct minijail_run_status status = {};
2299 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002300}
2301
2302int API minijail_run_pid(struct minijail *j, const char *filename,
2303 char *const argv[], pid_t *pchild_pid)
2304{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002305 struct minijail_run_config config = {
2306 .filename = filename,
2307 .argv = argv,
2308 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002309 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002310 };
2311 struct minijail_run_status status = {
2312 .pchild_pid = pchild_pid,
2313 };
2314 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002315}
2316
2317int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002318 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002319{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002320 struct minijail_run_config config = {
2321 .filename = filename,
2322 .argv = argv,
2323 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002324 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002325 };
2326 struct minijail_run_status status = {
2327 .pstdin_fd = pstdin_fd,
2328 };
2329 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002330}
2331
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002332int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002333 char *const argv[], pid_t *pchild_pid,
2334 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002335{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002336 struct minijail_run_config config = {
2337 .filename = filename,
2338 .argv = argv,
2339 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002340 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002341 };
2342 struct minijail_run_status status = {
2343 .pstdin_fd = pstdin_fd,
2344 .pstdout_fd = pstdout_fd,
2345 .pstderr_fd = pstderr_fd,
2346 .pchild_pid = pchild_pid,
2347 };
2348 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002349}
2350
2351int API minijail_run_no_preload(struct minijail *j, const char *filename,
2352 char *const argv[])
2353{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002354 struct minijail_run_config config = {
2355 .filename = filename,
2356 .argv = argv,
2357 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002358 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002359 };
2360 struct minijail_run_status status = {};
2361 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002362}
2363
Samuel Tan63187f42015-10-16 13:01:53 -07002364int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002365 const char *filename,
2366 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002367 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002368 int *pstdin_fd,
2369 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002370 int *pstderr_fd)
2371{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002372 struct minijail_run_config config = {
2373 .filename = filename,
2374 .argv = argv,
2375 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002376 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002377 };
2378 struct minijail_run_status status = {
2379 .pstdin_fd = pstdin_fd,
2380 .pstdout_fd = pstdout_fd,
2381 .pstderr_fd = pstderr_fd,
2382 .pchild_pid = pchild_pid,
2383 };
2384 return minijail_run_internal(j, &config, &status);
Samuel Tan63187f42015-10-16 13:01:53 -07002385}
2386
Dylan Reid0412dcc2017-08-24 11:33:15 -07002387pid_t API minijail_fork(struct minijail *j)
2388{
2389 struct minijail_run_config config = {};
2390 struct minijail_run_status status = {};
2391 return minijail_run_internal(j, &config, &status);
2392}
2393
Dylan Reid18c49c82017-08-25 14:52:27 -07002394static int minijail_run_internal(struct minijail *j,
2395 const struct minijail_run_config *config,
2396 struct minijail_run_status *status_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002397{
Elly Jonese1749eb2011-10-07 13:54:59 -04002398 char *oldenv, *oldenv_copy = NULL;
2399 pid_t child_pid;
2400 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002401 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002402 int stdout_fds[2];
2403 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08002404 int child_sync_pipe_fds[2];
2405 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002406 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002407 /* We need to remember this across the minijail_preexec() call. */
2408 int pid_namespace = j->flags.pids;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -07002409 /*
2410 * Create an init process if we are entering a pid namespace, unless the
2411 * user has explicitly opted out by calling minijail_run_as_init().
2412 */
2413 int do_init = j->flags.do_init && !j->flags.run_as_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002414 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002415
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002416 if (use_preload) {
Dylan Reid0412dcc2017-08-24 11:33:15 -07002417 if (j->hooks_head != NULL)
2418 die("Minijail hooks are not supported with LD_PRELOAD");
2419 if (!config->exec_in_child)
2420 die("minijail_fork is not supported with LD_PRELOAD");
2421
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002422 oldenv = getenv(kLdPreloadEnvVar);
2423 if (oldenv) {
2424 oldenv_copy = strdup(oldenv);
2425 if (!oldenv_copy)
2426 return -ENOMEM;
2427 }
2428
2429 if (setup_preload())
2430 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002431 }
Will Drewryf89aef52011-09-16 16:48:57 -05002432
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002433 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002434 if (j->flags.use_caps && j->caps != 0 &&
2435 !j->flags.set_ambient_caps) {
2436 die("non-empty, non-ambient capabilities are not "
2437 "supported without LD_PRELOAD");
2438 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002439 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002440
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002441 if (use_preload) {
2442 /*
2443 * Before we fork(2) and execve(2) the child process, we need
2444 * to open a pipe(2) to send the minijail configuration over.
2445 */
2446 if (setup_pipe(pipe_fds))
2447 return -EFAULT;
2448 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002449
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002450 /*
2451 * If we want to write to the child process' standard input,
2452 * create the pipe(2) now.
2453 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002454 if (status_out->pstdin_fd) {
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002455 if (pipe(stdin_fds))
2456 return -EFAULT;
2457 }
2458
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002459 /*
2460 * If we want to read from the child process' standard output,
2461 * create the pipe(2) now.
2462 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002463 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002464 if (pipe(stdout_fds))
2465 return -EFAULT;
2466 }
2467
2468 /*
2469 * If we want to read from the child process' standard error,
2470 * create the pipe(2) now.
2471 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002472 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002473 if (pipe(stderr_fds))
2474 return -EFAULT;
2475 }
2476
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002477 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002478 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002479 * or if we need to add the child process to cgroups, create the pipe(2)
2480 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002481 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002482 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002483 sync_child = 1;
2484 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002485 return -EFAULT;
2486 }
2487
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002488 /*
2489 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002490 *
2491 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2492 *
2493 * In multithreaded programs, there are a bunch of locks inside libc,
2494 * some of which may be held by other threads at the time that we call
2495 * minijail_run_pid(). If we call fork(), glibc does its level best to
2496 * ensure that we hold all of these locks before it calls clone()
2497 * internally and drop them after clone() returns, but when we call
2498 * sys_clone(2) directly, all that gets bypassed and we end up with a
2499 * child address space where some of libc's important locks are held by
2500 * other threads (which did not get cloned, and hence will never release
2501 * those locks). This is okay so long as we call exec() immediately
2502 * after, but a bunch of seemingly-innocent libc functions like setenv()
2503 * take locks.
2504 *
2505 * Hence, only call sys_clone() if we need to, in order to get at pid
2506 * namespacing. If we follow this path, the child's address space might
2507 * have broken locks; you may only call functions that do not acquire
2508 * any locks.
2509 *
2510 * Unfortunately, fork() acquires every lock it can get its hands on, as
2511 * previously detailed, so this function is highly likely to deadlock
2512 * later on (see "deadlock here") if we're multithreaded.
2513 *
2514 * We might hack around this by having the clone()d child (init of the
2515 * pid namespace) return directly, rather than leaving the clone()d
2516 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002517 * its fork()ed child return in turn), but that process would be
2518 * crippled with its libc locks potentially broken. We might try
2519 * fork()ing in the parent before we clone() to ensure that we own all
2520 * the locks, but then we have to have the forked child hanging around
2521 * consuming resources (and possibly having file descriptors / shared
2522 * memory regions / etc attached). We'd need to keep the child around to
2523 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002524 *
2525 * TODO(ellyjones): figure out if the "forked child hanging around"
2526 * problem is fixable or not. It would be nice if we worked in this
2527 * case.
2528 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002529 if (pid_namespace) {
2530 int clone_flags = CLONE_NEWPID | SIGCHLD;
2531 if (j->flags.userns)
2532 clone_flags |= CLONE_NEWUSER;
2533 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002534 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002535 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002536 }
Elly Jones761b7412012-06-13 15:49:52 -04002537
Elly Jonese1749eb2011-10-07 13:54:59 -04002538 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002539 if (use_preload) {
2540 free(oldenv_copy);
2541 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002542 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002543 }
Will Drewryf89aef52011-09-16 16:48:57 -05002544
Elly Jonese1749eb2011-10-07 13:54:59 -04002545 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002546 if (use_preload) {
2547 /* Restore parent's LD_PRELOAD. */
2548 if (oldenv_copy) {
2549 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2550 free(oldenv_copy);
2551 } else {
2552 unsetenv(kLdPreloadEnvVar);
2553 }
2554 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002555 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002556
Elly Jonese1749eb2011-10-07 13:54:59 -04002557 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002558
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002559 if (j->flags.forward_signals) {
2560 forward_pid = child_pid;
2561 install_signal_handlers();
2562 }
2563
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002564 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002565 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002566
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002567 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002568 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002569
Dylan Reid0f72ef42017-06-06 15:42:49 -07002570 if (j->rlimit_count)
2571 set_rlimits_or_die(j);
2572
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002573 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002574 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002575
2576 if (sync_child)
2577 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002578
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002579 if (use_preload) {
2580 /* Send marshalled minijail. */
2581 close(pipe_fds[0]); /* read endpoint */
2582 ret = minijail_to_fd(j, pipe_fds[1]);
2583 close(pipe_fds[1]); /* write endpoint */
2584 if (ret) {
2585 kill(j->initpid, SIGKILL);
2586 die("failed to send marshalled minijail");
2587 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002588 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002589
Dylan Reidacfb8be2017-08-25 12:56:51 -07002590 if (status_out->pchild_pid)
2591 *status_out->pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002592
2593 /*
2594 * If we want to write to the child process' standard input,
2595 * set up the write end of the pipe.
2596 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002597 if (status_out->pstdin_fd)
2598 *status_out->pstdin_fd =
2599 setup_pipe_end(stdin_fds, 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002600
2601 /*
2602 * If we want to read from the child process' standard output,
2603 * set up the read end of the pipe.
2604 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002605 if (status_out->pstdout_fd)
2606 *status_out->pstdout_fd =
2607 setup_pipe_end(stdout_fds, 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002608
2609 /*
2610 * If we want to read from the child process' standard error,
2611 * set up the read end of the pipe.
2612 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002613 if (status_out->pstderr_fd)
2614 *status_out->pstderr_fd =
2615 setup_pipe_end(stderr_fds, 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002616
Dylan Reid0412dcc2017-08-24 11:33:15 -07002617 /*
2618 * If forking return the child pid, in the normal exec case
2619 * return 0 for success.
2620 */
2621 if (!config->exec_in_child)
2622 return child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04002623 return 0;
2624 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002625 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002626 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002627
Peter Qiu2860c462015-12-16 15:13:06 -08002628 if (j->flags.reset_signal_mask) {
2629 sigset_t signal_mask;
2630 if (sigemptyset(&signal_mask) != 0)
2631 pdie("sigemptyset failed");
2632 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2633 pdie("sigprocmask failed");
2634 }
2635
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002636 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002637 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002638 int inheritable_fds[kMaxInheritableFdsSize];
2639 size_t size = 0;
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002640 size_t i;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002641 if (use_preload) {
2642 inheritable_fds[size++] = pipe_fds[0];
2643 inheritable_fds[size++] = pipe_fds[1];
2644 }
2645 if (sync_child) {
2646 inheritable_fds[size++] = child_sync_pipe_fds[0];
2647 inheritable_fds[size++] = child_sync_pipe_fds[1];
2648 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002649 if (status_out->pstdin_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002650 inheritable_fds[size++] = stdin_fds[0];
2651 inheritable_fds[size++] = stdin_fds[1];
2652 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002653 if (status_out->pstdout_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002654 inheritable_fds[size++] = stdout_fds[0];
2655 inheritable_fds[size++] = stdout_fds[1];
2656 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002657 if (status_out->pstderr_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002658 inheritable_fds[size++] = stderr_fds[0];
2659 inheritable_fds[size++] = stderr_fds[1];
2660 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002661 for (i = 0; i < j->preserved_fd_count; i++) {
2662 /*
2663 * Preserve all parent_fds. They will be dup2(2)-ed in
2664 * the child later.
2665 */
2666 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
2667 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002668
2669 if (close_open_fds(inheritable_fds, size) < 0)
2670 die("failed to close open file descriptors");
2671 }
2672
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002673 if (redirect_fds(j))
2674 die("failed to set up fd redirections");
2675
Dylan Reidce5b55e2016-01-13 11:04:16 -08002676 if (sync_child)
2677 wait_for_parent_setup(child_sync_pipe_fds);
2678
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002679 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002680 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002681
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002682 /*
2683 * If we want to write to the jailed process' standard input,
2684 * set up the read end of the pipe.
2685 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002686 if (status_out->pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002687 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2688 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002689 die("failed to set up stdin pipe");
2690 }
2691
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002692 /*
2693 * If we want to read from the jailed process' standard output,
2694 * set up the write end of the pipe.
2695 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002696 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002697 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2698 STDOUT_FILENO) < 0)
2699 die("failed to set up stdout pipe");
2700 }
2701
2702 /*
2703 * If we want to read from the jailed process' standard error,
2704 * set up the write end of the pipe.
2705 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002706 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002707 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2708 STDERR_FILENO) < 0)
2709 die("failed to set up stderr pipe");
2710 }
2711
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002712 /*
2713 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2714 * This prevents the jailed process from using the TIOCSTI ioctl
2715 * to push characters into the parent process terminal's input buffer,
2716 * therefore escaping the jail.
Stephen Barber5dd5b1b2017-10-16 23:02:39 -07002717 *
2718 * Since it has just forked, the child will not be a process group
2719 * leader, and this call to setsid() should always succeed.
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002720 */
2721 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2722 isatty(STDERR_FILENO)) {
2723 if (setsid() < 0) {
2724 pdie("setsid() failed");
2725 }
2726 }
2727
Dylan Reid791f5772015-09-14 20:02:42 -07002728 /* If running an init program, let it decide when/how to mount /proc. */
2729 if (pid_namespace && !do_init)
2730 j->flags.remount_proc_ro = 0;
2731
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002732 if (use_preload) {
2733 /* Strip out flags that cannot be inherited across execve(2). */
2734 minijail_preexec(j);
2735 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002736 /*
2737 * If not using LD_PRELOAD, do all jailing before execve(2).
2738 * Note that PID namespaces can only be entered on fork(2),
2739 * so that flag is still cleared.
2740 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002741 j->flags.pids = 0;
2742 }
Dylan Reid0412dcc2017-08-24 11:33:15 -07002743
2744 /*
2745 * Jail this process.
2746 * If forking, return.
2747 * If not, execve(2) the target.
2748 */
Elly Jonese1749eb2011-10-07 13:54:59 -04002749 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002750
Dylan Reid0412dcc2017-08-24 11:33:15 -07002751 if (config->exec_in_child && pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002752 /*
2753 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002754 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002755 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002756 * a child to actually run the program. If |do_init == 0|, we
2757 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002758 *
2759 * If we're multithreaded, we'll probably deadlock here. See
2760 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002761 */
2762 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002763 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002764 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002765 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002766 /*
2767 * Best effort. Don't bother checking the return value.
2768 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002769 prctl(PR_SET_NAME, "minijail-init");
2770 init(child_pid); /* Never returns. */
2771 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002772 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002773
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002774 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
2775
Dylan Reid0412dcc2017-08-24 11:33:15 -07002776 if (!config->exec_in_child)
2777 return 0;
2778
Elly Jonesdd3e8512012-01-23 15:13:38 -05002779 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002780 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002781 * calling process
2782 * -> execve()-ing process
2783 * If we are:
2784 * calling process
2785 * -> init()-ing process
2786 * -> execve()-ing process
2787 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002788 ret = execve(config->filename, config->argv, environ);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002789 if (ret == -1) {
Dylan Reidacfb8be2017-08-25 12:56:51 -07002790 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002791 }
2792 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002793}
2794
Will Drewry6ac91122011-10-21 16:38:58 -05002795int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002796{
2797 int st;
2798 if (kill(j->initpid, SIGTERM))
2799 return -errno;
2800 if (waitpid(j->initpid, &st, 0) < 0)
2801 return -errno;
2802 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002803}
2804
Will Drewry6ac91122011-10-21 16:38:58 -05002805int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002806{
2807 int st;
2808 if (waitpid(j->initpid, &st, 0) < 0)
2809 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002810
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002811 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002812 int error_status = st;
2813 if (WIFSIGNALED(st)) {
2814 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002815 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002816 j->initpid, signum);
2817 /*
2818 * We return MINIJAIL_ERR_JAIL if the process received
2819 * SIGSYS, which happens when a syscall is blocked by
2820 * seccomp filters.
2821 * If not, we do what bash(1) does:
2822 * $? = 128 + signum
2823 */
2824 if (signum == SIGSYS) {
2825 error_status = MINIJAIL_ERR_JAIL;
2826 } else {
2827 error_status = 128 + signum;
2828 }
2829 }
2830 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002831 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002832
2833 int exit_status = WEXITSTATUS(st);
2834 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002835 info("child process %d exited with status %d",
2836 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002837
2838 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002839}
2840
Will Drewry6ac91122011-10-21 16:38:58 -05002841void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002842{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002843 size_t i;
2844
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002845 if (j->flags.seccomp_filter && j->filter_prog) {
2846 free(j->filter_prog->filter);
2847 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002848 }
Mike Frysingerac08a682017-10-10 02:04:50 -04002849 free_mounts_list(j);
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002850 while (j->hooks_head) {
2851 struct hook *c = j->hooks_head;
2852 j->hooks_head = c->next;
2853 free(c);
2854 }
2855 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002856 if (j->user)
2857 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002858 if (j->suppl_gid_list)
2859 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002860 if (j->chrootdir)
2861 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002862 if (j->pid_file_path)
2863 free(j->pid_file_path);
2864 if (j->uidmap)
2865 free(j->uidmap);
2866 if (j->gidmap)
2867 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002868 if (j->hostname)
2869 free(j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08002870 if (j->alt_syscall_table)
2871 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002872 for (i = 0; i < j->cgroup_count; ++i)
2873 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002874 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002875}
Luis Hector Chavez114a9302017-09-05 20:36:58 -07002876
2877void API minijail_log_to_fd(int fd, int min_priority)
2878{
2879 init_logging(LOG_TO_FD, fd, min_priority);
2880}