blob: 9ae1c876a068dda9b6f09d6d73154f5018f95a98 [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);
1375 return ret;
1376 }
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);
1391 return ret;
1392 }
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);
1400 return ret;
1401 }
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);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001407 return ret;
1408}
1409
Mike Frysingerac08a682017-10-10 02:04:50 -04001410static void process_mounts_or_die(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001411{
Mike Frysingerac08a682017-10-10 02:04:50 -04001412 /*
1413 * We have to mount /dev first in case there are bind mounts from
1414 * the original /dev into the new unique tmpfs one.
1415 */
1416 char *dev_path = NULL;
1417 if (j->flags.mount_dev && mount_dev(&dev_path))
1418 pdie("mount_dev failed");
Dylan Reid648b2202015-10-23 00:50:00 -07001419
Mike Frysingerac08a682017-10-10 02:04:50 -04001420 if (j->mounts_head && mount_one(j, j->mounts_head, dev_path)) {
1421 if (dev_path) {
1422 int saved_errno = errno;
1423 mount_dev_cleanup(dev_path);
1424 errno = saved_errno;
1425 }
1426 pdie("mount_one failed");
1427 }
Mike Frysinger33ffef32017-01-13 19:53:19 -05001428
1429 /*
Mike Frysingerac08a682017-10-10 02:04:50 -04001430 * Once all bind mounts have been processed, move the temp dev to
1431 * its final /dev home.
Mike Frysinger33ffef32017-01-13 19:53:19 -05001432 */
1433 if (j->flags.mount_dev && mount_dev_finalize(j, dev_path))
Mike Frysingerac08a682017-10-10 02:04:50 -04001434 pdie("mount_dev_finalize failed");
1435}
Elly Jones51a5b6c2011-10-12 19:09:26 -04001436
Mike Frysingerac08a682017-10-10 02:04:50 -04001437static int enter_chroot(const struct minijail *j)
1438{
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001439 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1440
Elly Jones51a5b6c2011-10-12 19:09:26 -04001441 if (chroot(j->chrootdir))
1442 return -errno;
1443
1444 if (chdir("/"))
1445 return -errno;
1446
1447 return 0;
1448}
1449
Mike Frysingerac08a682017-10-10 02:04:50 -04001450static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001451{
Mike Frysingerac08a682017-10-10 02:04:50 -04001452 int oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001453
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001454 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1455
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001456 /*
1457 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001458 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001459 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001460 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001461 if (oldroot < 0)
1462 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001463 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001464 if (newroot < 0)
1465 pdie("failed to open %s for fchdir", j->chrootdir);
1466
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001467 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001468 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001469 * do a self bind mount.
1470 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001471 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1472 pdie("failed to bind mount '%s'", j->chrootdir);
1473 if (chdir(j->chrootdir))
1474 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001475 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001476 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001477
1478 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001479 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001480 * change to the old root and unmount it.
1481 */
1482 if (fchdir(oldroot))
1483 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001484
1485 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001486 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1487 * there could be a shared mount point under |oldroot|. In that case,
1488 * mounts under this shared mount point will be unmounted below, and
1489 * this unmounting will propagate to the original mount namespace
1490 * (because the mount point is shared). To prevent this unexpected
1491 * unmounting, remove these mounts from their peer groups by recursively
1492 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001493 */
1494 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001495 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001496 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001497 if (umount2(".", MNT_DETACH))
1498 pdie("umount(/)");
1499 /* Change back to the new root. */
1500 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001501 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001502 if (close(oldroot))
1503 return -errno;
1504 if (close(newroot))
1505 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001506 if (chroot("/"))
1507 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001508 /* Set correct CWD for getcwd(3). */
1509 if (chdir("/"))
1510 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001511
1512 return 0;
1513}
1514
Martin Pelikánab9eb442017-01-25 11:53:58 +11001515static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001516{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001517 const char fmt[] = "size=%zu,mode=1777";
1518 /* Count for the user storing ULLONG_MAX literally + extra space. */
1519 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1520 int ret;
1521
1522 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1523
1524 if (ret <= 0)
1525 pdie("tmpfs size spec error");
1526 else if ((size_t)ret >= sizeof(data))
1527 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001528 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001529 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001530}
1531
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001532static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001533{
1534 const char *kProcPath = "/proc";
1535 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001536 /*
1537 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001538 * /proc in our namespace, which means using MS_REMOUNT here would
1539 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001540 * namespace (!). Instead, remove their mount from our namespace lazily
1541 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001542 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001543 if (umount2(kProcPath, MNT_DETACH)) {
1544 /*
1545 * If we are in a new user namespace, umount(2) will fail.
1546 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1547 */
1548 if (j->flags.userns) {
1549 info("umount(/proc, MNT_DETACH) failed, "
1550 "this is expected when using user namespaces");
1551 } else {
1552 return -errno;
1553 }
1554 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001555 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001556 return -errno;
1557 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001558}
1559
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001560static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001561{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001562 kill(j->initpid, SIGKILL);
1563 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001564}
1565
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001566static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001567{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001568 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001569 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001570}
1571
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001572static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001573{
1574 size_t i;
1575
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001576 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001577 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001578 kill_child_and_die(j, "failed to add to cgroups");
1579 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001580}
1581
Dylan Reid0f72ef42017-06-06 15:42:49 -07001582static void set_rlimits_or_die(const struct minijail *j)
1583{
1584 size_t i;
1585
1586 for (i = 0; i < j->rlimit_count; ++i) {
1587 struct rlimit limit;
1588 limit.rlim_cur = j->rlimits[i].cur;
1589 limit.rlim_max = j->rlimits[i].max;
1590 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1591 kill_child_and_die(j, "failed to set rlimit");
1592 }
1593}
1594
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001595static void write_ugid_maps_or_die(const struct minijail *j)
1596{
1597 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1598 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001599 if (j->gidmap && j->flags.disable_setgroups) {
1600 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1601 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001602 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001603 if (ret == -ENOENT) {
1604 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1605 warn("could not disable setgroups(2)");
1606 } else
1607 kill_child_and_die(j, "failed to disable setgroups(2)");
1608 }
1609 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001610 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1611 kill_child_and_die(j, "failed to write gid_map");
1612}
1613
1614static void enter_user_namespace(const struct minijail *j)
1615{
Luis Hector Chavez71323552017-09-05 09:17:22 -07001616 int uid = j->flags.uid ? j->uid : 0;
1617 int gid = j->flags.gid ? j->gid : 0;
1618 if (j->gidmap && setresgid(gid, gid, gid)) {
1619 pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid,
1620 gid);
1621 }
1622 if (j->uidmap && setresuid(uid, uid, uid)) {
1623 pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid,
1624 uid);
1625 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001626}
1627
1628static void parent_setup_complete(int *pipe_fds)
1629{
1630 close(pipe_fds[0]);
1631 close(pipe_fds[1]);
1632}
1633
1634/*
1635 * wait_for_parent_setup: Called by the child process to wait for any
1636 * further parent-side setup to complete before continuing.
1637 */
1638static void wait_for_parent_setup(int *pipe_fds)
1639{
1640 char buf;
1641
1642 close(pipe_fds[1]);
1643
1644 /* Wait for parent to complete setup and close the pipe. */
1645 if (read(pipe_fds[0], &buf, 1) != 0)
1646 die("failed to sync with parent");
1647 close(pipe_fds[0]);
1648}
1649
1650static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001651{
Lutz Justen13807cb2017-01-03 17:11:55 +01001652 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1653 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001654 die("can only do one of inherit, keep, or set supplementary "
1655 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001656 }
1657
Lutz Justen13807cb2017-01-03 17:11:55 +01001658 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001659 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001660 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001661 } else if (j->flags.set_suppl_gids) {
1662 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001663 pdie("setgroups(suppl_gids) failed");
Luis Hector Chavez71323552017-09-05 09:17:22 -07001664 } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001665 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001666 * Only attempt to clear supplementary groups if we are changing
Luis Hector Chavez71323552017-09-05 09:17:22 -07001667 * users or groups, and if the caller did not request to disable
1668 * setgroups (used when entering a user namespace as a
1669 * non-privileged user).
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001670 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001671 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001672 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001673 }
1674
1675 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001676 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001677
1678 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001679 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001680}
1681
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001682static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1683{
1684 const uint64_t one = 1;
1685 unsigned int i;
1686 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1687 if (keep_mask & (one << i))
1688 continue;
1689 if (prctl(PR_CAPBSET_DROP, i))
1690 pdie("could not drop capability from bounding set");
1691 }
1692}
1693
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001694static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001695{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001696 if (!j->flags.use_caps)
1697 return;
1698
Elly Jonese1749eb2011-10-07 13:54:59 -04001699 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001700 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001701 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001702 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001703 unsigned int i;
1704 if (!caps)
1705 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001706 if (cap_clear(caps))
1707 die("can't clear caps");
1708
1709 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001710 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001711 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001712 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001713 flag[0] = i;
1714 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001715 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001716 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001717 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001718 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001719 die("can't add inheritable cap");
1720 }
1721 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001722 die("can't apply initial cleaned capset");
1723
1724 /*
1725 * Instead of dropping bounding set first, do it here in case
1726 * the caller had a more permissive bounding set which could
1727 * have been used above to raise a capability that wasn't already
1728 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1729 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001730 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001731
1732 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001733 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001734 flag[0] = CAP_SETPCAP;
1735 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1736 die("can't clear effective cap");
1737 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1738 die("can't clear permitted cap");
1739 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1740 die("can't clear inheritable cap");
1741 }
1742
1743 if (cap_set_proc(caps))
1744 die("can't apply final cleaned capset");
1745
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001746 /*
1747 * If ambient capabilities are supported, clear all capabilities first,
1748 * then raise the requested ones.
1749 */
1750 if (j->flags.set_ambient_caps) {
1751 if (!cap_ambient_supported()) {
1752 pdie("ambient capabilities not supported");
1753 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001754 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1755 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001756 pdie("can't clear ambient capabilities");
1757 }
1758
1759 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1760 if (!(j->caps & (one << i)))
1761 continue;
1762
1763 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1764 0) != 0) {
1765 pdie("prctl(PR_CAP_AMBIENT, "
1766 "PR_CAP_AMBIENT_RAISE, %u) failed",
1767 i);
1768 }
1769 }
1770 }
1771
Kees Cook323878a2013-02-05 15:35:24 -08001772 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001773}
1774
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001775static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001776{
1777 /*
1778 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1779 * in the kernel source tree for an explanation of the parameters.
1780 */
1781 if (j->flags.no_new_privs) {
1782 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1783 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1784 }
1785
1786 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001787 * Code running with ASan
1788 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1789 * will make system calls not included in the syscall filter policy,
1790 * which will likely crash the program. Skip setting seccomp filter in
1791 * that case.
1792 * 'running_with_asan()' has no inputs and is completely defined at
1793 * build time, so this cannot be used by an attacker to skip setting
1794 * seccomp filter.
1795 */
1796 if (j->flags.seccomp_filter && running_with_asan()) {
1797 warn("running with ASan, not setting seccomp filter");
1798 return;
1799 }
1800
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001801 if (j->flags.seccomp_filter) {
1802 if (j->flags.seccomp_filter_logging) {
1803 /*
1804 * If logging seccomp filter failures,
1805 * install the SIGSYS handler first.
1806 */
1807 if (install_sigsys_handler())
1808 pdie("failed to install SIGSYS handler");
1809 warn("logging seccomp filter failures");
1810 } else if (j->flags.seccomp_filter_tsync) {
1811 /*
1812 * If setting thread sync,
1813 * reset the SIGSYS signal handler so that
1814 * the entire thread group is killed.
1815 */
1816 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1817 pdie("failed to reset SIGSYS disposition");
1818 info("reset SIGSYS disposition");
1819 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001820 }
1821
1822 /*
1823 * Install the syscall filter.
1824 */
1825 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001826 if (j->flags.seccomp_filter_tsync) {
1827 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1828 SECCOMP_FILTER_FLAG_TSYNC,
1829 j->filter_prog)) {
1830 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001831 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001832 } else {
1833 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1834 j->filter_prog)) {
1835 pdie("prctl(seccomp_filter) failed");
1836 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001837 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001838 }
1839}
1840
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001841static pid_t forward_pid = -1;
1842
1843static void forward_signal(__attribute__((unused)) int nr,
1844 __attribute__((unused)) siginfo_t *siginfo,
1845 __attribute__((unused)) void *void_context)
1846{
1847 if (forward_pid != -1) {
1848 kill(forward_pid, nr);
1849 }
1850}
1851
1852static void install_signal_handlers(void)
1853{
1854 struct sigaction act;
1855
1856 memset(&act, 0, sizeof(act));
1857 act.sa_sigaction = &forward_signal;
1858 act.sa_flags = SA_SIGINFO | SA_RESTART;
1859
1860 /* Handle all signals, except SIGCHLD. */
1861 for (int nr = 1; nr < NSIG; nr++) {
1862 /*
1863 * We don't care if we get EINVAL: that just means that we
1864 * can't handle this signal, so let's skip it and continue.
1865 */
1866 sigaction(nr, &act, NULL);
1867 }
1868 /* Reset SIGCHLD's handler. */
1869 signal(SIGCHLD, SIG_DFL);
1870
1871 /* Handle real-time signals. */
1872 for (int nr = SIGRTMIN; nr <= SIGRTMAX; nr++) {
1873 sigaction(nr, &act, NULL);
1874 }
1875}
1876
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001877static const char *lookup_hook_name(minijail_hook_event_t event)
1878{
1879 switch (event) {
1880 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
1881 return "pre-drop-caps";
1882 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
1883 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001884 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
1885 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001886 case MINIJAIL_HOOK_EVENT_MAX:
1887 /*
1888 * Adding this in favor of a default case to force the
1889 * compiler to error out if a new enum value is added.
1890 */
1891 break;
1892 }
1893 return "unknown";
1894}
1895
1896static void run_hooks_or_die(const struct minijail *j,
1897 minijail_hook_event_t event)
1898{
1899 int rc;
1900 int hook_index = 0;
1901 for (struct hook *c = j->hooks_head; c; c = c->next) {
1902 if (c->event != event)
1903 continue;
1904 rc = c->hook(c->payload);
1905 if (rc != 0) {
1906 errno = -rc;
1907 pdie("%s hook (index %d) failed",
1908 lookup_hook_name(event), hook_index);
1909 }
1910 /* Only increase the index within the same hook event type. */
1911 ++hook_index;
1912 }
1913}
1914
Will Drewry6ac91122011-10-21 16:38:58 -05001915void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001916{
Dylan Reidf682d472015-09-17 21:39:07 -07001917 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001918 * If we're dropping caps, get the last valid cap from /proc now,
1919 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001920 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001921 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001922 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001923 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001924
Elly Jonese1749eb2011-10-07 13:54:59 -04001925 if (j->flags.pids)
1926 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001927 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001928
Lutz Justen13807cb2017-01-03 17:11:55 +01001929 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001930 die("cannot inherit supplementary groups without setting a "
1931 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001932
Elly Jonesdd3e8512012-01-23 15:13:38 -05001933 /*
1934 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001935 * so we don't even try. If any of our operations fail, we abort() the
1936 * entire process.
1937 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001938 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001939 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001940
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001941 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001942 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001943 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001944 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001945 * Unless asked not to, remount all filesystems as private.
1946 * If they are shared, new bind mounts will creep out of our
1947 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001948 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1949 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001950 if (!j->flags.skip_remount_private) {
1951 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001952 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1953 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001954 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001955 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001956
Dylan Reidf7942472015-11-18 17:55:26 -08001957 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001958 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001959 }
1960
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001961 if (j->flags.uts) {
1962 if (unshare(CLONE_NEWUTS))
1963 pdie("unshare(CLONE_NEWUTS) failed");
1964
1965 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
1966 pdie("sethostname(%s) failed", j->hostname);
1967 }
1968
Dylan Reid1102f5a2015-09-15 11:52:20 -07001969 if (j->flags.enter_net) {
1970 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001971 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001972 } else if (j->flags.net) {
1973 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001974 pdie("unshare(CLONE_NEWNET) failed");
1975 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001976 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001977
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001978 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001979 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001980
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08001981 if (j->flags.new_session_keyring) {
1982 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
1983 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
1984 }
1985
Mike Frysingerac08a682017-10-10 02:04:50 -04001986 /* We have to process all the mounts before we chroot/pivot_root. */
1987 process_mounts_or_die(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001988
Mike Frysingerac08a682017-10-10 02:04:50 -04001989 if (j->flags.chroot && enter_chroot(j))
Mike Frysinger33ffef32017-01-13 19:53:19 -05001990 pdie("chroot");
Mike Frysinger33ffef32017-01-13 19:53:19 -05001991
Mike Frysingerac08a682017-10-10 02:04:50 -04001992 if (j->flags.pivot_root && enter_pivot_root(j))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001993 pdie("pivot_root");
1994
Martin Pelikánab9eb442017-01-25 11:53:58 +11001995 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07001996 pdie("mount_tmp");
1997
Dylan Reid791f5772015-09-14 20:02:42 -07001998 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001999 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04002000
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002001 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
2002
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002003 /*
2004 * If we're only dropping capabilities from the bounding set, but not
2005 * from the thread's (permitted|inheritable|effective) sets, do it now.
2006 */
2007 if (j->flags.capbset_drop) {
2008 drop_capbset(j->cap_bset, last_valid_cap);
2009 }
2010
2011 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002012 /*
2013 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04002014 * capability to change uids, our attempt to use setuid()
2015 * below will fail. Hang on to root caps across setuid(), then
2016 * lock securebits.
2017 */
2018 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002019 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002020
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -07002021 if (lock_securebits(j->securebits_skip_mask) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002022 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002023 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002024 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002025
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002026 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002027 /*
2028 * If we're setting no_new_privs, we can drop privileges
2029 * before setting seccomp filter. This way filter policies
2030 * don't need to allow privilege-dropping syscalls.
2031 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002032 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002033 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002034 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002035 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002036 /*
2037 * If we're not setting no_new_privs,
2038 * we need to set seccomp filter *before* dropping privileges.
2039 * WARNING: this means that filter policies *must* allow
2040 * setgroups()/setresgid()/setresuid() for dropping root and
2041 * capget()/capset()/prctl() for dropping caps.
2042 */
2043 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002044 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002045 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04002046 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002047
Elly Jonesdd3e8512012-01-23 15:13:38 -05002048 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08002049 * Select the specified alternate syscall table. The table must not
2050 * block prctl(2) if we're using seccomp as well.
2051 */
2052 if (j->flags.alt_syscall) {
2053 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002054 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08002055 }
2056
2057 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05002058 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04002059 * privilege-dropping syscalls :)
2060 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002061 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04002062 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002063 warn("seccomp not supported");
2064 return;
2065 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002066 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002067 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002068}
2069
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002070/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04002071static int init_exitstatus = 0;
2072
Will Drewry6ac91122011-10-21 16:38:58 -05002073void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04002074{
2075 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04002076}
2077
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002078void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04002079{
2080 pid_t pid;
2081 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002082 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002083 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002084 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002085 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002086 /*
2087 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04002088 * left inside our pid namespace or we get a signal.
2089 */
2090 if (pid == rootpid)
2091 init_exitstatus = status;
2092 }
2093 if (!WIFEXITED(init_exitstatus))
2094 _exit(MINIJAIL_ERR_INIT);
2095 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04002096}
2097
Will Drewry6ac91122011-10-21 16:38:58 -05002098int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002099{
2100 size_t sz = 0;
2101 size_t bytes = read(fd, &sz, sizeof(sz));
2102 char *buf;
2103 int r;
2104 if (sizeof(sz) != bytes)
2105 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002106 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04002107 return -E2BIG;
2108 buf = malloc(sz);
2109 if (!buf)
2110 return -ENOMEM;
2111 bytes = read(fd, buf, sz);
2112 if (bytes != sz) {
2113 free(buf);
2114 return -EINVAL;
2115 }
2116 r = minijail_unmarshal(j, buf, sz);
2117 free(buf);
2118 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002119}
2120
Will Drewry6ac91122011-10-21 16:38:58 -05002121int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04002122{
2123 char *buf;
2124 size_t sz = minijail_size(j);
2125 ssize_t written;
2126 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04002127
Elly Jonese1749eb2011-10-07 13:54:59 -04002128 if (!sz)
2129 return -EINVAL;
2130 buf = malloc(sz);
2131 r = minijail_marshal(j, buf, sz);
2132 if (r) {
2133 free(buf);
2134 return r;
2135 }
2136 /* Sends [size][minijail]. */
2137 written = write(fd, &sz, sizeof(sz));
2138 if (written != sizeof(sz)) {
2139 free(buf);
2140 return -EFAULT;
2141 }
2142 written = write(fd, buf, sz);
2143 if (written < 0 || (size_t) written != sz) {
2144 free(buf);
2145 return -EFAULT;
2146 }
2147 free(buf);
2148 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002149}
Elly Jonescd7a9042011-07-22 13:56:51 -04002150
Will Drewry6ac91122011-10-21 16:38:58 -05002151int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04002152{
Daniel Erat5b7a3182015-08-19 16:06:22 -06002153#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002154 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002155 return 0;
2156#else
Elly Jonese1749eb2011-10-07 13:54:59 -04002157 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
2158 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
2159 if (!newenv)
2160 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04002161
Elly Jonese1749eb2011-10-07 13:54:59 -04002162 /* Only insert a separating space if we have something to separate... */
2163 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
2164 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04002165
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002166 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002167 setenv(kLdPreloadEnvVar, newenv, 1);
2168 free(newenv);
2169 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002170#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04002171}
2172
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002173static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04002174{
2175 int r = pipe(fds);
2176 char fd_buf[11];
2177 if (r)
2178 return r;
2179 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2180 if (r <= 0)
2181 return -EINVAL;
2182 setenv(kFdEnvVar, fd_buf, 1);
2183 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05002184}
2185
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002186static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002187{
2188 const char *kFdPath = "/proc/self/fd";
2189
2190 DIR *d = opendir(kFdPath);
2191 struct dirent *dir_entry;
2192
2193 if (d == NULL)
2194 return -1;
2195 int dir_fd = dirfd(d);
2196 while ((dir_entry = readdir(d)) != NULL) {
2197 size_t i;
2198 char *end;
2199 bool should_close = true;
2200 const int fd = strtol(dir_entry->d_name, &end, 10);
2201
2202 if ((*end) != '\0') {
2203 continue;
2204 }
2205 /*
2206 * We might have set up some pipes that we want to share with
2207 * the parent process, and should not be closed.
2208 */
2209 for (i = 0; i < size; ++i) {
2210 if (fd == inheritable_fds[i]) {
2211 should_close = false;
2212 break;
2213 }
2214 }
2215 /* Also avoid closing the directory fd. */
2216 if (should_close && fd != dir_fd)
2217 close(fd);
2218 }
2219 closedir(d);
2220 return 0;
2221}
2222
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002223static int redirect_fds(struct minijail *j)
2224{
2225 size_t i, i2;
2226 int closeable;
2227 for (i = 0; i < j->preserved_fd_count; i++) {
2228 if (dup2(j->preserved_fds[i].parent_fd,
2229 j->preserved_fds[i].child_fd) == -1) {
2230 return -1;
2231 }
2232 }
2233 /*
2234 * After all fds have been duped, we are now free to close all parent
2235 * fds that are *not* child fds.
2236 */
2237 for (i = 0; i < j->preserved_fd_count; i++) {
2238 closeable = true;
2239 for (i2 = 0; i2 < j->preserved_fd_count; i2++) {
2240 closeable &= j->preserved_fds[i].parent_fd !=
2241 j->preserved_fds[i2].child_fd;
2242 }
2243 if (closeable)
2244 close(j->preserved_fds[i].parent_fd);
2245 }
2246 return 0;
2247}
2248
Dylan Reidacfb8be2017-08-25 12:56:51 -07002249/*
2250 * Structure that specifies how to start a minijail.
2251 *
Dylan Reid0412dcc2017-08-24 11:33:15 -07002252 * filename - The program to exec in the child. Required if `exec_in_child` = 1.
2253 * argv - Arguments for the child program. Required if `exec_in_child` = 1.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002254 * use_preload - If true use LD_PRELOAD.
Dylan Reid0412dcc2017-08-24 11:33:15 -07002255 * exec_in_child - If true, run `filename`. Otherwise, the child will return to
2256 * the caller.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002257 */
2258struct minijail_run_config {
2259 const char *filename;
2260 char *const *argv;
2261 int use_preload;
Dylan Reid0412dcc2017-08-24 11:33:15 -07002262 int exec_in_child;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002263};
2264
2265/*
2266 * Set of pointers to fill with values from minijail_run.
2267 * All arguments are allowed to be NULL if unused.
2268 *
2269 * pstdin_fd - Filled with stdin pipe if non-NULL.
2270 * pstdout_fd - Filled with stdout pipe if non-NULL.
2271 * pstderr_fd - Filled with stderr pipe if non-NULL.
2272 * pchild_pid - Filled with the pid of the child process if non-NULL.
2273 */
2274struct minijail_run_status {
2275 int *pstdin_fd;
2276 int *pstdout_fd;
2277 int *pstderr_fd;
2278 pid_t *pchild_pid;
2279};
2280
Dylan Reid18c49c82017-08-25 14:52:27 -07002281static int minijail_run_internal(struct minijail *j,
2282 const struct minijail_run_config *config,
2283 struct minijail_run_status *status_out);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002284
Will Drewry6ac91122011-10-21 16:38:58 -05002285int API minijail_run(struct minijail *j, const char *filename,
2286 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002287{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002288 struct minijail_run_config config = {
2289 .filename = filename,
2290 .argv = argv,
2291 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002292 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002293 };
2294 struct minijail_run_status status = {};
2295 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002296}
2297
2298int API minijail_run_pid(struct minijail *j, const char *filename,
2299 char *const argv[], pid_t *pchild_pid)
2300{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002301 struct minijail_run_config config = {
2302 .filename = filename,
2303 .argv = argv,
2304 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002305 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002306 };
2307 struct minijail_run_status status = {
2308 .pchild_pid = pchild_pid,
2309 };
2310 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002311}
2312
2313int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002314 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002315{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002316 struct minijail_run_config config = {
2317 .filename = filename,
2318 .argv = argv,
2319 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002320 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002321 };
2322 struct minijail_run_status status = {
2323 .pstdin_fd = pstdin_fd,
2324 };
2325 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002326}
2327
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002328int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002329 char *const argv[], pid_t *pchild_pid,
2330 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002331{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002332 struct minijail_run_config config = {
2333 .filename = filename,
2334 .argv = argv,
2335 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002336 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002337 };
2338 struct minijail_run_status status = {
2339 .pstdin_fd = pstdin_fd,
2340 .pstdout_fd = pstdout_fd,
2341 .pstderr_fd = pstderr_fd,
2342 .pchild_pid = pchild_pid,
2343 };
2344 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002345}
2346
2347int API minijail_run_no_preload(struct minijail *j, const char *filename,
2348 char *const argv[])
2349{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002350 struct minijail_run_config config = {
2351 .filename = filename,
2352 .argv = argv,
2353 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002354 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002355 };
2356 struct minijail_run_status status = {};
2357 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002358}
2359
Samuel Tan63187f42015-10-16 13:01:53 -07002360int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002361 const char *filename,
2362 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002363 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002364 int *pstdin_fd,
2365 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002366 int *pstderr_fd)
2367{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002368 struct minijail_run_config config = {
2369 .filename = filename,
2370 .argv = argv,
2371 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002372 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002373 };
2374 struct minijail_run_status status = {
2375 .pstdin_fd = pstdin_fd,
2376 .pstdout_fd = pstdout_fd,
2377 .pstderr_fd = pstderr_fd,
2378 .pchild_pid = pchild_pid,
2379 };
2380 return minijail_run_internal(j, &config, &status);
Samuel Tan63187f42015-10-16 13:01:53 -07002381}
2382
Dylan Reid0412dcc2017-08-24 11:33:15 -07002383pid_t API minijail_fork(struct minijail *j)
2384{
2385 struct minijail_run_config config = {};
2386 struct minijail_run_status status = {};
2387 return minijail_run_internal(j, &config, &status);
2388}
2389
Dylan Reid18c49c82017-08-25 14:52:27 -07002390static int minijail_run_internal(struct minijail *j,
2391 const struct minijail_run_config *config,
2392 struct minijail_run_status *status_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002393{
Elly Jonese1749eb2011-10-07 13:54:59 -04002394 char *oldenv, *oldenv_copy = NULL;
2395 pid_t child_pid;
2396 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002397 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002398 int stdout_fds[2];
2399 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08002400 int child_sync_pipe_fds[2];
2401 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002402 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002403 /* We need to remember this across the minijail_preexec() call. */
2404 int pid_namespace = j->flags.pids;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -07002405 /*
2406 * Create an init process if we are entering a pid namespace, unless the
2407 * user has explicitly opted out by calling minijail_run_as_init().
2408 */
2409 int do_init = j->flags.do_init && !j->flags.run_as_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002410 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002411
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002412 if (use_preload) {
Dylan Reid0412dcc2017-08-24 11:33:15 -07002413 if (j->hooks_head != NULL)
2414 die("Minijail hooks are not supported with LD_PRELOAD");
2415 if (!config->exec_in_child)
2416 die("minijail_fork is not supported with LD_PRELOAD");
2417
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002418 oldenv = getenv(kLdPreloadEnvVar);
2419 if (oldenv) {
2420 oldenv_copy = strdup(oldenv);
2421 if (!oldenv_copy)
2422 return -ENOMEM;
2423 }
2424
2425 if (setup_preload())
2426 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002427 }
Will Drewryf89aef52011-09-16 16:48:57 -05002428
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002429 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002430 if (j->flags.use_caps && j->caps != 0 &&
2431 !j->flags.set_ambient_caps) {
2432 die("non-empty, non-ambient capabilities are not "
2433 "supported without LD_PRELOAD");
2434 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002435 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002436
Elly Jonesdd3e8512012-01-23 15:13:38 -05002437 /*
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002438 * Make the process group ID of this process equal to its PID.
2439 * In the non-interactive case (e.g. when the parent process is started
2440 * from init) this ensures the parent process and the jailed process
2441 * can be killed together.
2442 * When the parent process is started from the console this ensures
2443 * the call to setsid(2) in the jailed process succeeds.
2444 *
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002445 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
2446 * the process is already a process group leader.
2447 */
2448 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
2449 if (errno != EPERM) {
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002450 pdie("setpgid(0, 0) failed");
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002451 }
2452 }
2453
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002454 if (use_preload) {
2455 /*
2456 * Before we fork(2) and execve(2) the child process, we need
2457 * to open a pipe(2) to send the minijail configuration over.
2458 */
2459 if (setup_pipe(pipe_fds))
2460 return -EFAULT;
2461 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002462
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002463 /*
2464 * If we want to write to the child process' standard input,
2465 * create the pipe(2) now.
2466 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002467 if (status_out->pstdin_fd) {
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002468 if (pipe(stdin_fds))
2469 return -EFAULT;
2470 }
2471
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002472 /*
2473 * If we want to read from the child process' standard output,
2474 * create the pipe(2) now.
2475 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002476 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002477 if (pipe(stdout_fds))
2478 return -EFAULT;
2479 }
2480
2481 /*
2482 * If we want to read from the child process' standard error,
2483 * create the pipe(2) now.
2484 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002485 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002486 if (pipe(stderr_fds))
2487 return -EFAULT;
2488 }
2489
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002490 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002491 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002492 * or if we need to add the child process to cgroups, create the pipe(2)
2493 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002494 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002495 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002496 sync_child = 1;
2497 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002498 return -EFAULT;
2499 }
2500
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002501 /*
2502 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002503 *
2504 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2505 *
2506 * In multithreaded programs, there are a bunch of locks inside libc,
2507 * some of which may be held by other threads at the time that we call
2508 * minijail_run_pid(). If we call fork(), glibc does its level best to
2509 * ensure that we hold all of these locks before it calls clone()
2510 * internally and drop them after clone() returns, but when we call
2511 * sys_clone(2) directly, all that gets bypassed and we end up with a
2512 * child address space where some of libc's important locks are held by
2513 * other threads (which did not get cloned, and hence will never release
2514 * those locks). This is okay so long as we call exec() immediately
2515 * after, but a bunch of seemingly-innocent libc functions like setenv()
2516 * take locks.
2517 *
2518 * Hence, only call sys_clone() if we need to, in order to get at pid
2519 * namespacing. If we follow this path, the child's address space might
2520 * have broken locks; you may only call functions that do not acquire
2521 * any locks.
2522 *
2523 * Unfortunately, fork() acquires every lock it can get its hands on, as
2524 * previously detailed, so this function is highly likely to deadlock
2525 * later on (see "deadlock here") if we're multithreaded.
2526 *
2527 * We might hack around this by having the clone()d child (init of the
2528 * pid namespace) return directly, rather than leaving the clone()d
2529 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002530 * its fork()ed child return in turn), but that process would be
2531 * crippled with its libc locks potentially broken. We might try
2532 * fork()ing in the parent before we clone() to ensure that we own all
2533 * the locks, but then we have to have the forked child hanging around
2534 * consuming resources (and possibly having file descriptors / shared
2535 * memory regions / etc attached). We'd need to keep the child around to
2536 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002537 *
2538 * TODO(ellyjones): figure out if the "forked child hanging around"
2539 * problem is fixable or not. It would be nice if we worked in this
2540 * case.
2541 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002542 if (pid_namespace) {
2543 int clone_flags = CLONE_NEWPID | SIGCHLD;
2544 if (j->flags.userns)
2545 clone_flags |= CLONE_NEWUSER;
2546 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002547 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002548 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002549 }
Elly Jones761b7412012-06-13 15:49:52 -04002550
Elly Jonese1749eb2011-10-07 13:54:59 -04002551 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002552 if (use_preload) {
2553 free(oldenv_copy);
2554 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002555 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002556 }
Will Drewryf89aef52011-09-16 16:48:57 -05002557
Elly Jonese1749eb2011-10-07 13:54:59 -04002558 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002559 if (use_preload) {
2560 /* Restore parent's LD_PRELOAD. */
2561 if (oldenv_copy) {
2562 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2563 free(oldenv_copy);
2564 } else {
2565 unsetenv(kLdPreloadEnvVar);
2566 }
2567 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002568 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002569
Elly Jonese1749eb2011-10-07 13:54:59 -04002570 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002571
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002572 if (j->flags.forward_signals) {
2573 forward_pid = child_pid;
2574 install_signal_handlers();
2575 }
2576
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002577 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002578 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002579
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002580 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002581 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002582
Dylan Reid0f72ef42017-06-06 15:42:49 -07002583 if (j->rlimit_count)
2584 set_rlimits_or_die(j);
2585
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002586 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002587 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002588
2589 if (sync_child)
2590 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002591
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002592 if (use_preload) {
2593 /* Send marshalled minijail. */
2594 close(pipe_fds[0]); /* read endpoint */
2595 ret = minijail_to_fd(j, pipe_fds[1]);
2596 close(pipe_fds[1]); /* write endpoint */
2597 if (ret) {
2598 kill(j->initpid, SIGKILL);
2599 die("failed to send marshalled minijail");
2600 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002601 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002602
Dylan Reidacfb8be2017-08-25 12:56:51 -07002603 if (status_out->pchild_pid)
2604 *status_out->pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002605
2606 /*
2607 * If we want to write to the child process' standard input,
2608 * set up the write end of the pipe.
2609 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002610 if (status_out->pstdin_fd)
2611 *status_out->pstdin_fd =
2612 setup_pipe_end(stdin_fds, 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002613
2614 /*
2615 * If we want to read from the child process' standard output,
2616 * set up the read end of the pipe.
2617 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002618 if (status_out->pstdout_fd)
2619 *status_out->pstdout_fd =
2620 setup_pipe_end(stdout_fds, 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002621
2622 /*
2623 * If we want to read from the child process' standard error,
2624 * set up the read end of the pipe.
2625 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002626 if (status_out->pstderr_fd)
2627 *status_out->pstderr_fd =
2628 setup_pipe_end(stderr_fds, 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002629
Dylan Reid0412dcc2017-08-24 11:33:15 -07002630 /*
2631 * If forking return the child pid, in the normal exec case
2632 * return 0 for success.
2633 */
2634 if (!config->exec_in_child)
2635 return child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04002636 return 0;
2637 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002638 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002639 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002640
Peter Qiu2860c462015-12-16 15:13:06 -08002641 if (j->flags.reset_signal_mask) {
2642 sigset_t signal_mask;
2643 if (sigemptyset(&signal_mask) != 0)
2644 pdie("sigemptyset failed");
2645 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2646 pdie("sigprocmask failed");
2647 }
2648
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002649 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002650 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002651 int inheritable_fds[kMaxInheritableFdsSize];
2652 size_t size = 0;
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002653 size_t i;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002654 if (use_preload) {
2655 inheritable_fds[size++] = pipe_fds[0];
2656 inheritable_fds[size++] = pipe_fds[1];
2657 }
2658 if (sync_child) {
2659 inheritable_fds[size++] = child_sync_pipe_fds[0];
2660 inheritable_fds[size++] = child_sync_pipe_fds[1];
2661 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002662 if (status_out->pstdin_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002663 inheritable_fds[size++] = stdin_fds[0];
2664 inheritable_fds[size++] = stdin_fds[1];
2665 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002666 if (status_out->pstdout_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002667 inheritable_fds[size++] = stdout_fds[0];
2668 inheritable_fds[size++] = stdout_fds[1];
2669 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002670 if (status_out->pstderr_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002671 inheritable_fds[size++] = stderr_fds[0];
2672 inheritable_fds[size++] = stderr_fds[1];
2673 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002674 for (i = 0; i < j->preserved_fd_count; i++) {
2675 /*
2676 * Preserve all parent_fds. They will be dup2(2)-ed in
2677 * the child later.
2678 */
2679 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
2680 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002681
2682 if (close_open_fds(inheritable_fds, size) < 0)
2683 die("failed to close open file descriptors");
2684 }
2685
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002686 if (redirect_fds(j))
2687 die("failed to set up fd redirections");
2688
Dylan Reidce5b55e2016-01-13 11:04:16 -08002689 if (sync_child)
2690 wait_for_parent_setup(child_sync_pipe_fds);
2691
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002692 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002693 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002694
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002695 /*
2696 * If we want to write to the jailed process' standard input,
2697 * set up the read end of the pipe.
2698 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002699 if (status_out->pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002700 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2701 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002702 die("failed to set up stdin pipe");
2703 }
2704
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002705 /*
2706 * If we want to read from the jailed process' standard output,
2707 * set up the write end of the pipe.
2708 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002709 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002710 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2711 STDOUT_FILENO) < 0)
2712 die("failed to set up stdout pipe");
2713 }
2714
2715 /*
2716 * If we want to read from the jailed process' standard error,
2717 * set up the write end of the pipe.
2718 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002719 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002720 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2721 STDERR_FILENO) < 0)
2722 die("failed to set up stderr pipe");
2723 }
2724
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002725 /*
2726 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2727 * This prevents the jailed process from using the TIOCSTI ioctl
2728 * to push characters into the parent process terminal's input buffer,
2729 * therefore escaping the jail.
2730 */
2731 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2732 isatty(STDERR_FILENO)) {
2733 if (setsid() < 0) {
2734 pdie("setsid() failed");
2735 }
2736 }
2737
Dylan Reid791f5772015-09-14 20:02:42 -07002738 /* If running an init program, let it decide when/how to mount /proc. */
2739 if (pid_namespace && !do_init)
2740 j->flags.remount_proc_ro = 0;
2741
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002742 if (use_preload) {
2743 /* Strip out flags that cannot be inherited across execve(2). */
2744 minijail_preexec(j);
2745 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002746 /*
2747 * If not using LD_PRELOAD, do all jailing before execve(2).
2748 * Note that PID namespaces can only be entered on fork(2),
2749 * so that flag is still cleared.
2750 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002751 j->flags.pids = 0;
2752 }
Dylan Reid0412dcc2017-08-24 11:33:15 -07002753
2754 /*
2755 * Jail this process.
2756 * If forking, return.
2757 * If not, execve(2) the target.
2758 */
Elly Jonese1749eb2011-10-07 13:54:59 -04002759 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002760
Dylan Reid0412dcc2017-08-24 11:33:15 -07002761 if (config->exec_in_child && pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002762 /*
2763 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002764 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002765 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002766 * a child to actually run the program. If |do_init == 0|, we
2767 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002768 *
2769 * If we're multithreaded, we'll probably deadlock here. See
2770 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002771 */
2772 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002773 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002774 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002775 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002776 /*
2777 * Best effort. Don't bother checking the return value.
2778 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002779 prctl(PR_SET_NAME, "minijail-init");
2780 init(child_pid); /* Never returns. */
2781 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002782 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002783
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002784 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
2785
Dylan Reid0412dcc2017-08-24 11:33:15 -07002786 if (!config->exec_in_child)
2787 return 0;
2788
Elly Jonesdd3e8512012-01-23 15:13:38 -05002789 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002790 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002791 * calling process
2792 * -> execve()-ing process
2793 * If we are:
2794 * calling process
2795 * -> init()-ing process
2796 * -> execve()-ing process
2797 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002798 ret = execve(config->filename, config->argv, environ);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002799 if (ret == -1) {
Dylan Reidacfb8be2017-08-25 12:56:51 -07002800 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002801 }
2802 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002803}
2804
Will Drewry6ac91122011-10-21 16:38:58 -05002805int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002806{
2807 int st;
2808 if (kill(j->initpid, SIGTERM))
2809 return -errno;
2810 if (waitpid(j->initpid, &st, 0) < 0)
2811 return -errno;
2812 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002813}
2814
Will Drewry6ac91122011-10-21 16:38:58 -05002815int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002816{
2817 int st;
2818 if (waitpid(j->initpid, &st, 0) < 0)
2819 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002820
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002821 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002822 int error_status = st;
2823 if (WIFSIGNALED(st)) {
2824 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002825 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002826 j->initpid, signum);
2827 /*
2828 * We return MINIJAIL_ERR_JAIL if the process received
2829 * SIGSYS, which happens when a syscall is blocked by
2830 * seccomp filters.
2831 * If not, we do what bash(1) does:
2832 * $? = 128 + signum
2833 */
2834 if (signum == SIGSYS) {
2835 error_status = MINIJAIL_ERR_JAIL;
2836 } else {
2837 error_status = 128 + signum;
2838 }
2839 }
2840 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002841 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002842
2843 int exit_status = WEXITSTATUS(st);
2844 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002845 info("child process %d exited with status %d",
2846 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002847
2848 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002849}
2850
Will Drewry6ac91122011-10-21 16:38:58 -05002851void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002852{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002853 size_t i;
2854
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002855 if (j->flags.seccomp_filter && j->filter_prog) {
2856 free(j->filter_prog->filter);
2857 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002858 }
Mike Frysingerac08a682017-10-10 02:04:50 -04002859 free_mounts_list(j);
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002860 while (j->hooks_head) {
2861 struct hook *c = j->hooks_head;
2862 j->hooks_head = c->next;
2863 free(c);
2864 }
2865 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002866 if (j->user)
2867 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002868 if (j->suppl_gid_list)
2869 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002870 if (j->chrootdir)
2871 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002872 if (j->pid_file_path)
2873 free(j->pid_file_path);
2874 if (j->uidmap)
2875 free(j->uidmap);
2876 if (j->gidmap)
2877 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002878 if (j->hostname)
2879 free(j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08002880 if (j->alt_syscall_table)
2881 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002882 for (i = 0; i < j->cgroup_count; ++i)
2883 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002884 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002885}
Luis Hector Chavez114a9302017-09-05 20:36:58 -07002886
2887void API minijail_log_to_fd(int fd, int min_priority)
2888{
2889 init_logging(LOG_TO_FD, fd, min_priority);
2890}