blob: 0d83cb26236e7387748dc13b503de8df3690a5a7 [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
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700195/*
196 * Strip out flags meant for the parent.
197 * We keep things that are not inherited across execve(2) (e.g. capabilities),
198 * or are easier to set after execve(2) (e.g. seccomp filters).
199 */
200void minijail_preenter(struct minijail *j)
201{
202 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700203 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900204 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700205 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700206 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800207 j->flags.do_init = 0;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700208 j->flags.run_as_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800209 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800210 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400211 j->flags.forward_signals = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700212}
213
214/*
215 * Strip out flags meant for the child.
216 * We keep things that are inherited across execve(2).
217 */
218void minijail_preexec(struct minijail *j)
219{
220 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700221 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800222 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700223 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800224 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700225 if (j->user)
226 free(j->user);
227 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800228 if (j->suppl_gid_list)
229 free(j->suppl_gid_list);
230 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700231 memset(&j->flags, 0, sizeof(j->flags));
232 /* Now restore anything we meant to keep. */
233 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700234 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800235 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700236 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800237 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700238 /* Note, |pids| will already have been used before this call. */
239}
240
241/* Minijail API. */
242
Will Drewry6ac91122011-10-21 16:38:58 -0500243struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400244{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400245 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400246}
247
Will Drewry6ac91122011-10-21 16:38:58 -0500248void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400249{
250 if (uid == 0)
251 die("useless change to uid 0");
252 j->uid = uid;
253 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400254}
255
Will Drewry6ac91122011-10-21 16:38:58 -0500256void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400257{
258 if (gid == 0)
259 die("useless change to gid 0");
260 j->gid = gid;
261 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400262}
263
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800264void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
265 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800266{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800267 size_t i;
268
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500269 if (j->flags.inherit_suppl_gids)
270 die("cannot inherit *and* set supplementary groups");
271 if (j->flags.keep_suppl_gids)
272 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800273
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800274 if (size == 0) {
275 /* Clear supplementary groups. */
276 j->suppl_gid_list = NULL;
277 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100278 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800279 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800280 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800281
282 /* Copy the gid_t array. */
283 j->suppl_gid_list = calloc(size, sizeof(gid_t));
284 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800285 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800286 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800287 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800288 j->suppl_gid_list[i] = list[i];
289 }
290 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100291 j->flags.set_suppl_gids = 1;
292}
293
294void API minijail_keep_supplementary_gids(struct minijail *j) {
295 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800296}
297
Will Drewry6ac91122011-10-21 16:38:58 -0500298int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400299{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700300 uid_t uid;
301 gid_t gid;
302 int rc = lookup_user(user, &uid, &gid);
303 if (rc)
304 return rc;
305 minijail_change_uid(j, uid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400306 j->user = strdup(user);
307 if (!j->user)
308 return -ENOMEM;
Luis Hector Chavez71323552017-09-05 09:17:22 -0700309 j->usergid = gid;
Elly Jonese1749eb2011-10-07 13:54:59 -0400310 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400311}
312
Will Drewry6ac91122011-10-21 16:38:58 -0500313int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400314{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700315 gid_t gid;
316 int rc = lookup_group(group, &gid);
317 if (rc)
318 return rc;
319 minijail_change_gid(j, gid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400320 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400321}
322
Will Drewry6ac91122011-10-21 16:38:58 -0500323void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400324{
325 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400326}
327
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700328void API minijail_no_new_privs(struct minijail *j)
329{
330 j->flags.no_new_privs = 1;
331}
332
Will Drewry6ac91122011-10-21 16:38:58 -0500333void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400334{
335 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500336}
337
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400338void API minijail_set_seccomp_filter_tsync(struct minijail *j)
339{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400340 if (j->filter_len > 0 && j->filter_prog != NULL) {
341 die("minijail_set_seccomp_filter_tsync() must be called "
342 "before minijail_parse_seccomp_filters()");
343 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400344 j->flags.seccomp_filter_tsync = 1;
345}
346
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700347void API minijail_log_seccomp_filter_failures(struct minijail *j)
348{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400349 if (j->filter_len > 0 && j->filter_prog != NULL) {
350 die("minijail_log_seccomp_filter_failures() must be called "
351 "before minijail_parse_seccomp_filters()");
352 }
353 j->flags.seccomp_filter_logging = 1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700354}
355
Will Drewry6ac91122011-10-21 16:38:58 -0500356void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400357{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800358 /*
359 * 'minijail_use_caps' configures a runtime-capabilities-only
360 * environment, including a bounding set matching the thread's runtime
361 * (permitted|inheritable|effective) sets.
362 * Therefore, it will override any existing bounding set configurations
363 * since the latter would allow gaining extra runtime capabilities from
364 * file capabilities.
365 */
366 if (j->flags.capbset_drop) {
367 warn("overriding bounding set configuration");
368 j->cap_bset = 0;
369 j->flags.capbset_drop = 0;
370 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400371 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800372 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400373}
374
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800375void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
376{
377 if (j->flags.use_caps) {
378 /*
379 * 'minijail_use_caps' will have already configured a capability
380 * bounding set matching the (permitted|inheritable|effective)
381 * sets. Abort if the user tries to configure a separate
382 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
383 * are mutually exclusive.
384 */
385 die("runtime capabilities already configured, can't drop "
386 "bounding set separately");
387 }
388 j->cap_bset = capmask;
389 j->flags.capbset_drop = 1;
390}
391
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400392void API minijail_set_ambient_caps(struct minijail *j)
393{
394 j->flags.set_ambient_caps = 1;
395}
396
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800397void API minijail_reset_signal_mask(struct minijail *j)
398{
Peter Qiu2860c462015-12-16 15:13:06 -0800399 j->flags.reset_signal_mask = 1;
400}
401
Will Drewry6ac91122011-10-21 16:38:58 -0500402void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400403{
404 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400405}
406
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700407void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
408{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800409 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700410 if (ns_fd < 0) {
411 pdie("failed to open namespace '%s'", ns_path);
412 }
413 j->mountns_fd = ns_fd;
414 j->flags.enter_vfs = 1;
415}
416
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800417void API minijail_new_session_keyring(struct minijail *j)
418{
419 j->flags.new_session_keyring = 1;
420}
421
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700422void API minijail_skip_setting_securebits(struct minijail *j,
423 uint64_t securebits_skip_mask)
424{
425 j->securebits_skip_mask = securebits_skip_mask;
426}
427
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800428void API minijail_skip_remount_private(struct minijail *j)
429{
430 j->flags.skip_remount_private = 1;
431}
432
Will Drewry6ac91122011-10-21 16:38:58 -0500433void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400434{
Elly Jonese58176c2012-01-23 11:46:17 -0500435 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700436 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400437 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800438 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400439}
440
Dylan Reidf7942472015-11-18 17:55:26 -0800441void API minijail_namespace_ipc(struct minijail *j)
442{
443 j->flags.ipc = 1;
444}
445
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400446void API minijail_namespace_uts(struct minijail *j)
447{
448 j->flags.uts = 1;
449}
450
451int API minijail_namespace_set_hostname(struct minijail *j, const char *name)
452{
453 if (j->hostname)
454 return -EINVAL;
455 minijail_namespace_uts(j);
456 j->hostname = strdup(name);
457 if (!j->hostname)
458 return -ENOMEM;
459 return 0;
460}
461
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400462void API minijail_namespace_net(struct minijail *j)
463{
464 j->flags.net = 1;
465}
466
Dylan Reid1102f5a2015-09-15 11:52:20 -0700467void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
468{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800469 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700470 if (ns_fd < 0) {
471 pdie("failed to open namespace '%s'", ns_path);
472 }
473 j->netns_fd = ns_fd;
474 j->flags.enter_net = 1;
475}
476
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700477void API minijail_namespace_cgroups(struct minijail *j)
478{
479 j->flags.ns_cgroups = 1;
480}
481
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700482void API minijail_close_open_fds(struct minijail *j)
483{
484 j->flags.close_open_fds = 1;
485}
486
Dylan Reid791f5772015-09-14 20:02:42 -0700487void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400488{
489 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700490 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400491}
492
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800493void API minijail_namespace_user(struct minijail *j)
494{
495 j->flags.userns = 1;
496}
497
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400498void API minijail_namespace_user_disable_setgroups(struct minijail *j)
499{
500 j->flags.disable_setgroups = 1;
501}
502
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800503int API minijail_uidmap(struct minijail *j, const char *uidmap)
504{
505 j->uidmap = strdup(uidmap);
506 if (!j->uidmap)
507 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800508 char *ch;
509 for (ch = j->uidmap; *ch; ch++) {
510 if (*ch == ',')
511 *ch = '\n';
512 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800513 return 0;
514}
515
516int API minijail_gidmap(struct minijail *j, const char *gidmap)
517{
518 j->gidmap = strdup(gidmap);
519 if (!j->gidmap)
520 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800521 char *ch;
522 for (ch = j->gidmap; *ch; ch++) {
523 if (*ch == ',')
524 *ch = '\n';
525 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800526 return 0;
527}
528
Will Drewry6ac91122011-10-21 16:38:58 -0500529void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400530{
Lutz Justen13807cb2017-01-03 17:11:55 +0100531 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400532}
533
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800534void API minijail_run_as_init(struct minijail *j)
535{
536 /*
537 * Since the jailed program will become 'init' in the new PID namespace,
538 * Minijail does not need to fork an 'init' process.
539 */
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700540 j->flags.run_as_init = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800541}
542
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700543int API minijail_enter_chroot(struct minijail *j, const char *dir)
544{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400545 if (j->chrootdir)
546 return -EINVAL;
547 j->chrootdir = strdup(dir);
548 if (!j->chrootdir)
549 return -ENOMEM;
550 j->flags.chroot = 1;
551 return 0;
552}
553
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800554int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
555{
556 if (j->chrootdir)
557 return -EINVAL;
558 j->chrootdir = strdup(dir);
559 if (!j->chrootdir)
560 return -ENOMEM;
561 j->flags.pivot_root = 1;
562 return 0;
563}
564
Dylan Reida14e08d2015-10-22 21:05:29 -0700565char API *minijail_get_original_path(struct minijail *j,
566 const char *path_inside_chroot)
567{
Dylan Reid648b2202015-10-23 00:50:00 -0700568 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700569
Dylan Reid648b2202015-10-23 00:50:00 -0700570 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700571 while (b) {
572 /*
573 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700574 * mount, then the original path is exactly the source of
575 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700576 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700577 * mount source = /some/path/exe, mount dest =
578 * /chroot/path/exe Then when getting the original path of
579 * "/chroot/path/exe", the source of that mount,
580 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700581 */
582 if (!strcmp(b->dest, path_inside_chroot))
583 return strdup(b->src);
584
585 /*
586 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700587 * mount, take the suffix of the chroot path relative to the
588 * mount destination path, and append it to the mount source
589 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700590 */
591 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
592 const char *relative_path =
593 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400594 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700595 }
596 b = b->next;
597 }
598
599 /* If there is a chroot path, append |path_inside_chroot| to that. */
600 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400601 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700602
603 /* No chroot, so the path outside is the same as it is inside. */
604 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700605}
606
Martin Pelikánab9eb442017-01-25 11:53:58 +1100607size_t minijail_get_tmpfs_size(const struct minijail *j)
608{
609 return j->tmpfs_size;
610}
611
Mike Frysinger33ffef32017-01-13 19:53:19 -0500612void API minijail_mount_dev(struct minijail *j)
613{
614 j->flags.mount_dev = 1;
615}
616
Lee Campbell11af0622014-05-22 12:36:04 -0700617void API minijail_mount_tmp(struct minijail *j)
618{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100619 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
620}
621
622void API minijail_mount_tmp_size(struct minijail *j, size_t size)
623{
624 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700625 j->flags.mount_tmp = 1;
626}
627
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800628int API minijail_write_pid_file(struct minijail *j, const char *path)
629{
630 j->pid_file_path = strdup(path);
631 if (!j->pid_file_path)
632 return -ENOMEM;
633 j->flags.pid_file = 1;
634 return 0;
635}
636
Dylan Reid605ce7f2016-01-19 19:21:00 -0800637int API minijail_add_to_cgroup(struct minijail *j, const char *path)
638{
639 if (j->cgroup_count >= MAX_CGROUPS)
640 return -ENOMEM;
641 j->cgroups[j->cgroup_count] = strdup(path);
642 if (!j->cgroups[j->cgroup_count])
643 return -ENOMEM;
644 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800645 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800646 return 0;
647}
648
Dylan Reid0f72ef42017-06-06 15:42:49 -0700649int API minijail_rlimit(struct minijail *j, int type, uint32_t cur,
650 uint32_t max)
651{
652 size_t i;
653
654 if (j->rlimit_count >= MAX_RLIMITS)
655 return -ENOMEM;
656 /* It's an error if the caller sets the same rlimit multiple times. */
657 for (i = 0; i < j->rlimit_count; i++) {
658 if (j->rlimits[i].type == type)
659 return -EEXIST;
660 }
661
662 j->rlimits[j->rlimit_count].type = type;
663 j->rlimits[j->rlimit_count].cur = cur;
664 j->rlimits[j->rlimit_count].max = max;
665 j->rlimit_count++;
666 return 0;
667}
668
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400669int API minijail_forward_signals(struct minijail *j)
670{
671 j->flags.forward_signals = 1;
672 return 0;
673}
674
Dylan Reid81e23972016-05-18 14:06:35 -0700675int API minijail_mount_with_data(struct minijail *j, const char *src,
676 const char *dest, const char *type,
677 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700678{
Dylan Reid648b2202015-10-23 00:50:00 -0700679 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400680
681 if (*dest != '/')
682 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700683 m = calloc(1, sizeof(*m));
684 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400685 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700686 m->dest = strdup(dest);
687 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400688 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700689 m->src = strdup(src);
690 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400691 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700692 m->type = strdup(type);
693 if (!m->type)
694 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700695 if (data) {
696 m->data = strdup(data);
697 if (!m->data)
698 goto error;
699 m->has_data = 1;
700 }
Dylan Reid648b2202015-10-23 00:50:00 -0700701 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400702
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800703 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400704
Elly Jonesdd3e8512012-01-23 15:13:38 -0500705 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700706 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400707 * containing vfs namespace.
708 */
709 minijail_namespace_vfs(j);
710
Dylan Reid648b2202015-10-23 00:50:00 -0700711 if (j->mounts_tail)
712 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400713 else
Dylan Reid648b2202015-10-23 00:50:00 -0700714 j->mounts_head = m;
715 j->mounts_tail = m;
716 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400717
718 return 0;
719
720error:
Dylan Reid81e23972016-05-18 14:06:35 -0700721 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700722 free(m->src);
723 free(m->dest);
724 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400725 return -ENOMEM;
726}
727
Dylan Reid81e23972016-05-18 14:06:35 -0700728int API minijail_mount(struct minijail *j, const char *src, const char *dest,
729 const char *type, unsigned long flags)
730{
731 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
732}
733
Dylan Reid648b2202015-10-23 00:50:00 -0700734int API minijail_bind(struct minijail *j, const char *src, const char *dest,
735 int writeable)
736{
737 unsigned long flags = MS_BIND;
738
739 if (!writeable)
740 flags |= MS_RDONLY;
741
742 return minijail_mount(j, src, dest, "", flags);
743}
744
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700745int API minijail_add_hook(struct minijail *j, minijail_hook_t hook,
746 void *payload, minijail_hook_event_t event)
747{
748 struct hook *c;
749
750 if (hook == NULL)
751 return -EINVAL;
752 if (event >= MINIJAIL_HOOK_EVENT_MAX)
753 return -EINVAL;
754 c = calloc(1, sizeof(*c));
755 if (!c)
756 return -ENOMEM;
757
758 c->hook = hook;
759 c->payload = payload;
760 c->event = event;
761
762 if (j->hooks_tail)
763 j->hooks_tail->next = c;
764 else
765 j->hooks_head = c;
766 j->hooks_tail = c;
767
768 return 0;
769}
770
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700771int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd)
772{
773 if (parent_fd < 0 || child_fd < 0)
774 return -EINVAL;
775 if (j->preserved_fd_count >= MAX_PRESERVED_FDS)
776 return -ENOMEM;
777 j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd;
778 j->preserved_fds[j->preserved_fd_count].child_fd = child_fd;
779 j->preserved_fd_count++;
780 return 0;
781}
782
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400783static void clear_seccomp_options(struct minijail *j)
784{
785 j->flags.seccomp_filter = 0;
786 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400787 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400788 j->filter_len = 0;
789 j->filter_prog = NULL;
790 j->flags.no_new_privs = 0;
791}
792
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400793static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400794{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400795 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400796 /*
797 * |errno| will be set to EINVAL when seccomp has not been
798 * compiled into the kernel. On certain platforms and kernel
799 * versions this is not a fatal failure. In that case, and only
800 * in that case, disable seccomp and skip loading the filters.
801 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400802 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400803 warn("not loading seccomp filters, seccomp filter not "
804 "supported");
805 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400806 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700807 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400808 /*
809 * If |errno| != EINVAL or seccomp_can_softfail() is false,
810 * we can proceed. Worst case scenario minijail_enter() will
811 * abort() if seccomp fails.
812 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700813 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400814 if (j->flags.seccomp_filter_tsync) {
815 /* Are the seccomp(2) syscall and the TSYNC option supported? */
816 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
817 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
818 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400819 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
820 warn("seccomp(2) syscall not supported");
821 clear_seccomp_options(j);
822 return 0;
823 } else if (saved_errno == EINVAL &&
824 seccomp_can_softfail()) {
825 warn(
826 "seccomp filter thread sync not supported");
827 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400828 return 0;
829 }
830 /*
831 * Similar logic here. If seccomp_can_softfail() is
832 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
833 * we can proceed. Worst case scenario minijail_enter()
834 * will abort() if seccomp or TSYNC fail.
835 */
836 }
837 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400838 return 1;
839}
840
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700841static int parse_seccomp_filters(struct minijail *j, const char *filename,
842 FILE *policy_file)
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400843{
844 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400845 int use_ret_trap =
846 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
847 int allow_logging = j->flags.seccomp_filter_logging;
848
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700849 if (compile_filter(filename, policy_file, fprog, use_ret_trap,
850 allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400851 free(fprog);
852 return -1;
853 }
854
855 j->filter_len = fprog->len;
856 j->filter_prog = fprog;
857 return 0;
858}
859
860void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
861{
862 if (!seccomp_should_parse_filters(j))
863 return;
864
Elly Jonese1749eb2011-10-07 13:54:59 -0400865 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800866 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700867 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400868 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800869
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700870 if (parse_seccomp_filters(j, path, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700871 die("failed to compile seccomp filter BPF program in '%s'",
872 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800873 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400874 fclose(file);
875}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800876
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400877void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
878{
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700879 char *fd_path, *path;
880 FILE *file;
881
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400882 if (!seccomp_should_parse_filters(j))
883 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800884
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700885 file = fdopen(fd, "r");
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400886 if (!file) {
887 pdie("failed to associate stream with fd %d", fd);
888 }
889
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700890 if (asprintf(&fd_path, "/proc/self/fd/%d", fd) == -1)
891 pdie("failed to create path for fd %d", fd);
892 path = realpath(fd_path, NULL);
893 if (path == NULL)
894 pwarn("failed to get path of fd %d", fd);
895 free(fd_path);
896
897 if (parse_seccomp_filters(j, path ? path : "<fd>", file) != 0) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400898 die("failed to compile seccomp filter BPF program from fd %d",
899 fd);
900 }
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700901 free(path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400902 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500903}
904
Andrew Brestickereac28942015-11-11 16:04:46 -0800905int API minijail_use_alt_syscall(struct minijail *j, const char *table)
906{
907 j->alt_syscall_table = strdup(table);
908 if (!j->alt_syscall_table)
909 return -ENOMEM;
910 j->flags.alt_syscall = 1;
911 return 0;
912}
913
Will Drewryf89aef52011-09-16 16:48:57 -0500914struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400915 size_t available;
916 size_t total;
917 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500918};
919
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800920void marshal_state_init(struct marshal_state *state, char *buf,
921 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400922{
923 state->available = available;
924 state->buf = buf;
925 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500926}
927
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800928void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400929{
930 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500931
Elly Jonese1749eb2011-10-07 13:54:59 -0400932 /* Up to |available| will be written. */
933 if (copy_len) {
934 memcpy(state->buf, src, copy_len);
935 state->buf += copy_len;
936 state->available -= copy_len;
937 }
938 /* |total| will contain the expected length. */
939 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500940}
941
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400942void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700943{
944 marshal_append(state, m->src, strlen(m->src) + 1);
945 marshal_append(state, m->dest, strlen(m->dest) + 1);
946 marshal_append(state, m->type, strlen(m->type) + 1);
947 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
948 if (m->has_data)
949 marshal_append(state, m->data, strlen(m->data) + 1);
950 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
951}
952
Will Drewry6ac91122011-10-21 16:38:58 -0500953void minijail_marshal_helper(struct marshal_state *state,
954 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400955{
Dylan Reid648b2202015-10-23 00:50:00 -0700956 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800957 size_t i;
958
Elly Jonese1749eb2011-10-07 13:54:59 -0400959 marshal_append(state, (char *)j, sizeof(*j));
960 if (j->user)
961 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800962 if (j->suppl_gid_list) {
963 marshal_append(state, j->suppl_gid_list,
964 j->suppl_gid_count * sizeof(gid_t));
965 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400966 if (j->chrootdir)
967 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400968 if (j->hostname)
969 marshal_append(state, j->hostname, strlen(j->hostname) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800970 if (j->alt_syscall_table) {
971 marshal_append(state, j->alt_syscall_table,
972 strlen(j->alt_syscall_table) + 1);
973 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800974 if (j->flags.seccomp_filter && j->filter_prog) {
975 struct sock_fprog *fp = j->filter_prog;
976 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800977 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400978 }
Dylan Reid648b2202015-10-23 00:50:00 -0700979 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400980 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400981 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800982 for (i = 0; i < j->cgroup_count; ++i)
983 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500984}
985
Will Drewry6ac91122011-10-21 16:38:58 -0500986size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400987{
988 struct marshal_state state;
989 marshal_state_init(&state, NULL, 0);
990 minijail_marshal_helper(&state, j);
991 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500992}
993
Elly Jonese1749eb2011-10-07 13:54:59 -0400994int minijail_marshal(const struct minijail *j, char *buf, size_t available)
995{
996 struct marshal_state state;
997 marshal_state_init(&state, buf, available);
998 minijail_marshal_helper(&state, j);
999 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -05001000}
1001
Elly Jonese1749eb2011-10-07 13:54:59 -04001002int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
1003{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -08001004 size_t i;
1005 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -05001006 int ret = -EINVAL;
1007
Elly Jonese1749eb2011-10-07 13:54:59 -04001008 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001009 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001010 memcpy((void *)j, serialized, sizeof(*j));
1011 serialized += sizeof(*j);
1012 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001013
Will Drewrybee7ba72011-10-21 20:47:01 -05001014 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001015 j->pid_file_path = NULL;
1016 j->uidmap = NULL;
1017 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001018 j->mounts_head = NULL;
1019 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001020 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001021 j->hooks_head = NULL;
1022 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001023
Elly Jonese1749eb2011-10-07 13:54:59 -04001024 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001025 char *user = consumestr(&serialized, &length);
1026 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001027 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001028 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001029 if (!j->user)
1030 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001031 }
Will Drewryf89aef52011-09-16 16:48:57 -05001032
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001033 if (j->suppl_gid_list) { /* stale pointer */
1034 if (j->suppl_gid_count > NGROUPS_MAX) {
1035 goto bad_gid_list;
1036 }
1037 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1038 void *gid_list_bytes =
1039 consumebytes(gid_list_size, &serialized, &length);
1040 if (!gid_list_bytes)
1041 goto bad_gid_list;
1042
1043 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1044 if (!j->suppl_gid_list)
1045 goto bad_gid_list;
1046
1047 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1048 }
1049
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001050 if (j->chrootdir) { /* stale pointer */
1051 char *chrootdir = consumestr(&serialized, &length);
1052 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001053 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001054 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001055 if (!j->chrootdir)
1056 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001057 }
1058
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001059 if (j->hostname) { /* stale pointer */
1060 char *hostname = consumestr(&serialized, &length);
1061 if (!hostname)
1062 goto bad_hostname;
1063 j->hostname = strdup(hostname);
1064 if (!j->hostname)
1065 goto bad_hostname;
1066 }
1067
Andrew Brestickereac28942015-11-11 16:04:46 -08001068 if (j->alt_syscall_table) { /* stale pointer */
1069 char *alt_syscall_table = consumestr(&serialized, &length);
1070 if (!alt_syscall_table)
1071 goto bad_syscall_table;
1072 j->alt_syscall_table = strdup(alt_syscall_table);
1073 if (!j->alt_syscall_table)
1074 goto bad_syscall_table;
1075 }
1076
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001077 if (j->flags.seccomp_filter && j->filter_len > 0) {
1078 size_t ninstrs = j->filter_len;
1079 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1080 ninstrs > USHRT_MAX)
1081 goto bad_filters;
1082
1083 size_t program_len = ninstrs * sizeof(struct sock_filter);
1084 void *program = consumebytes(program_len, &serialized, &length);
1085 if (!program)
1086 goto bad_filters;
1087
1088 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001089 if (!j->filter_prog)
1090 goto bad_filters;
1091
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001092 j->filter_prog->len = ninstrs;
1093 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001094 if (!j->filter_prog->filter)
1095 goto bad_filter_prog_instrs;
1096
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001097 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001098 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001099
Dylan Reid648b2202015-10-23 00:50:00 -07001100 count = j->mounts_count;
1101 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001102 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001103 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001104 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001105 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001106 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001107 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001108 const char *src = consumestr(&serialized, &length);
1109 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001110 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001111 dest = consumestr(&serialized, &length);
1112 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001113 goto bad_mounts;
1114 type = consumestr(&serialized, &length);
1115 if (!type)
1116 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001117 has_data = consumebytes(sizeof(*has_data), &serialized,
1118 &length);
1119 if (!has_data)
1120 goto bad_mounts;
1121 if (*has_data) {
1122 data = consumestr(&serialized, &length);
1123 if (!data)
1124 goto bad_mounts;
1125 }
Dylan Reid648b2202015-10-23 00:50:00 -07001126 flags = consumebytes(sizeof(*flags), &serialized, &length);
1127 if (!flags)
1128 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001129 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001130 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001131 }
1132
Dylan Reid605ce7f2016-01-19 19:21:00 -08001133 count = j->cgroup_count;
1134 j->cgroup_count = 0;
1135 for (i = 0; i < count; ++i) {
1136 char *cgroup = consumestr(&serialized, &length);
1137 if (!cgroup)
1138 goto bad_cgroups;
1139 j->cgroups[i] = strdup(cgroup);
1140 if (!j->cgroups[i])
1141 goto bad_cgroups;
1142 ++j->cgroup_count;
1143 }
1144
Elly Jonese1749eb2011-10-07 13:54:59 -04001145 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001146
Dylan Reid605ce7f2016-01-19 19:21:00 -08001147bad_cgroups:
1148 while (j->mounts_head) {
1149 struct mountpoint *m = j->mounts_head;
1150 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07001151 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001152 free(m->type);
1153 free(m->dest);
1154 free(m->src);
1155 free(m);
1156 }
1157 for (i = 0; i < j->cgroup_count; ++i)
1158 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001159bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001160 if (j->flags.seccomp_filter && j->filter_len > 0) {
1161 free(j->filter_prog->filter);
1162 free(j->filter_prog);
1163 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001164bad_filter_prog_instrs:
1165 if (j->filter_prog)
1166 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001167bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001168 if (j->alt_syscall_table)
1169 free(j->alt_syscall_table);
1170bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001171 if (j->chrootdir)
1172 free(j->chrootdir);
1173bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001174 if (j->hostname)
1175 free(j->hostname);
1176bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001177 if (j->suppl_gid_list)
1178 free(j->suppl_gid_list);
1179bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001180 if (j->user)
1181 free(j->user);
1182clear_pointers:
1183 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001184 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001185 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001186 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001187 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001188 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001189out:
1190 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001191}
1192
Mike Frysinger33ffef32017-01-13 19:53:19 -05001193struct dev_spec {
1194 const char *name;
1195 mode_t mode;
1196 dev_t major, minor;
1197};
1198
1199static const struct dev_spec device_nodes[] = {
1200 {
1201 "null",
1202 S_IFCHR | 0666, 1, 3,
1203 },
1204 {
1205 "zero",
1206 S_IFCHR | 0666, 1, 5,
1207 },
1208 {
1209 "full",
1210 S_IFCHR | 0666, 1, 7,
1211 },
1212 {
1213 "urandom",
1214 S_IFCHR | 0444, 1, 9,
1215 },
1216 {
1217 "tty",
1218 S_IFCHR | 0666, 5, 0,
1219 },
1220};
1221
1222struct dev_sym_spec {
1223 const char *source, *dest;
1224};
1225
1226static const struct dev_sym_spec device_symlinks[] = {
1227 { "ptmx", "pts/ptmx", },
1228 { "fd", "/proc/self/fd", },
1229 { "stdin", "fd/0", },
1230 { "stdout", "fd/1", },
1231 { "stderr", "fd/2", },
1232};
1233
1234/*
1235 * Clean up the temporary dev path we had setup previously. In case of errors,
1236 * we don't want to go leaking empty tempdirs.
1237 */
1238static void mount_dev_cleanup(char *dev_path)
1239{
1240 umount2(dev_path, MNT_DETACH);
1241 rmdir(dev_path);
1242 free(dev_path);
1243}
1244
1245/*
1246 * Set up the pseudo /dev path at the temporary location.
1247 * See mount_dev_finalize for more details.
1248 */
1249static int mount_dev(char **dev_path_ret)
1250{
1251 int ret;
1252 int dev_fd;
1253 size_t i;
1254 mode_t mask;
1255 char *dev_path;
1256
1257 /*
1258 * Create a temp path for the /dev init. We'll relocate this to the
1259 * final location later on in the startup process.
1260 */
1261 dev_path = *dev_path_ret = strdup("/tmp/minijail.dev.XXXXXX");
1262 if (dev_path == NULL || mkdtemp(dev_path) == NULL)
1263 pdie("could not create temp path for /dev");
1264
1265 /* Set up the empty /dev mount point first. */
1266 ret = mount("minijail-devfs", dev_path, "tmpfs",
1267 MS_NOEXEC | MS_NOSUID, "size=5M,mode=755");
1268 if (ret) {
1269 rmdir(dev_path);
1270 return ret;
1271 }
1272
1273 /* We want to set the mode directly from the spec. */
1274 mask = umask(0);
1275
1276 /* Get a handle to the temp dev path for *at funcs below. */
1277 dev_fd = open(dev_path, O_DIRECTORY|O_PATH|O_CLOEXEC);
1278 if (dev_fd < 0) {
1279 ret = 1;
1280 goto done;
1281 }
1282
1283 /* Create all the nodes in /dev. */
1284 for (i = 0; i < ARRAY_SIZE(device_nodes); ++i) {
1285 const struct dev_spec *ds = &device_nodes[i];
1286 ret = mknodat(dev_fd, ds->name, ds->mode,
1287 makedev(ds->major, ds->minor));
1288 if (ret)
1289 goto done;
1290 }
1291
1292 /* Create all the symlinks in /dev. */
1293 for (i = 0; i < ARRAY_SIZE(device_symlinks); ++i) {
1294 const struct dev_sym_spec *ds = &device_symlinks[i];
1295 ret = symlinkat(ds->dest, dev_fd, ds->source);
1296 if (ret)
1297 goto done;
1298 }
1299
1300 /* Restore old mask. */
1301 done:
1302 close(dev_fd);
1303 umask(mask);
1304
1305 if (ret)
1306 mount_dev_cleanup(dev_path);
1307
1308 return ret;
1309}
1310
1311/*
1312 * Relocate the temporary /dev mount to its final /dev place.
1313 * We have to do this two step process so people can bind mount extra
1314 * /dev paths like /dev/log.
1315 */
1316static int mount_dev_finalize(const struct minijail *j, char *dev_path)
1317{
1318 int ret = -1;
1319 char *dest = NULL;
1320
1321 /* Unmount the /dev mount if possible. */
1322 if (umount2("/dev", MNT_DETACH))
1323 goto done;
1324
1325 if (asprintf(&dest, "%s/dev", j->chrootdir ? : "") < 0)
1326 goto done;
1327
1328 if (mount(dev_path, dest, NULL, MS_MOVE, NULL))
1329 goto done;
1330
1331 ret = 0;
1332 done:
1333 free(dest);
1334 mount_dev_cleanup(dev_path);
1335
1336 return ret;
1337}
1338
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001339/*
1340 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001341 * @j Minijail these mounts are for
1342 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001343 *
1344 * Returns 0 for success.
1345 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001346static int mount_one(const struct minijail *j, struct mountpoint *m,
1347 const char *dev_path)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001348{
Dylan Reid648b2202015-10-23 00:50:00 -07001349 int ret;
1350 char *dest;
1351 int remount_ro = 0;
1352
Mike Frysinger33ffef32017-01-13 19:53:19 -05001353 /* We assume |dest| has a leading "/". */
1354 if (dev_path && strncmp("/dev/", m->dest, 5) == 0) {
1355 /* Since the temp path is rooted at /dev, skip that dest part. */
1356 if (asprintf(&dest, "%s%s", dev_path, m->dest + 4) < 0)
1357 return -ENOMEM;
1358 } else {
1359 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
1360 return -ENOMEM;
1361 }
Dylan Reid648b2202015-10-23 00:50:00 -07001362
Mike Frysinger33ffef32017-01-13 19:53:19 -05001363 ret = setup_mount_destination(m->src, dest, j->uid, j->gid,
1364 (m->flags & MS_BIND));
1365 if (ret) {
1366 pwarn("creating mount target '%s' failed", dest);
1367 return ret;
1368 }
Dylan Reideec77962016-06-30 19:35:10 -07001369
Dylan Reid648b2202015-10-23 00:50:00 -07001370 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001371 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1372 * can't both be specified in the original bind mount.
1373 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001374 */
1375 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1376 remount_ro = 1;
1377 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001378 }
Dylan Reid648b2202015-10-23 00:50:00 -07001379
Dylan Reid81e23972016-05-18 14:06:35 -07001380 ret = mount(m->src, dest, m->type, m->flags, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001381 if (ret) {
1382 pwarn("mount: %s -> %s", m->src, dest);
1383 return ret;
1384 }
Dylan Reid648b2202015-10-23 00:50:00 -07001385
1386 if (remount_ro) {
1387 m->flags |= MS_RDONLY;
1388 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001389 m->flags | MS_REMOUNT, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001390 if (ret) {
1391 pwarn("bind ro: %s -> %s", m->src, dest);
1392 return ret;
1393 }
Dylan Reid648b2202015-10-23 00:50:00 -07001394 }
1395
Elly Jones51a5b6c2011-10-12 19:09:26 -04001396 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001397 if (m->next)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001398 return mount_one(j, m->next, dev_path);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001399 return ret;
1400}
1401
Mike Frysinger33ffef32017-01-13 19:53:19 -05001402static int enter_chroot(const struct minijail *j, char *dev_path)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001403{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001404 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001405
Mike Frysinger33ffef32017-01-13 19:53:19 -05001406 if (j->mounts_head && (ret = mount_one(j, j->mounts_head, dev_path)))
1407 return ret;
1408
1409 /*
1410 * Once all bind mounts have been processed, but before we chroot,
1411 * move the temp dev to its final /dev home.
1412 */
1413 if (j->flags.mount_dev && mount_dev_finalize(j, dev_path))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001414 return ret;
1415
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001416 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1417
Elly Jones51a5b6c2011-10-12 19:09:26 -04001418 if (chroot(j->chrootdir))
1419 return -errno;
1420
1421 if (chdir("/"))
1422 return -errno;
1423
1424 return 0;
1425}
1426
Mike Frysinger33ffef32017-01-13 19:53:19 -05001427static int enter_pivot_root(const struct minijail *j, char *dev_path)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001428{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001429 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001430
Mike Frysinger33ffef32017-01-13 19:53:19 -05001431 if (j->mounts_head && (ret = mount_one(j, j->mounts_head, dev_path)))
1432 return ret;
1433
1434 /*
1435 * Once all bind mounts have been processed, but before we pivot,
1436 * move the temp dev to its final /dev home.
1437 */
1438 if (j->flags.mount_dev && mount_dev_finalize(j, dev_path))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001439 return ret;
1440
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001441 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1442
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001443 /*
1444 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001445 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001446 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001447 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001448 if (oldroot < 0)
1449 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001450 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001451 if (newroot < 0)
1452 pdie("failed to open %s for fchdir", j->chrootdir);
1453
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001454 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001455 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001456 * do a self bind mount.
1457 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001458 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1459 pdie("failed to bind mount '%s'", j->chrootdir);
1460 if (chdir(j->chrootdir))
1461 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001462 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001463 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001464
1465 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001466 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001467 * change to the old root and unmount it.
1468 */
1469 if (fchdir(oldroot))
1470 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001471
1472 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001473 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1474 * there could be a shared mount point under |oldroot|. In that case,
1475 * mounts under this shared mount point will be unmounted below, and
1476 * this unmounting will propagate to the original mount namespace
1477 * (because the mount point is shared). To prevent this unexpected
1478 * unmounting, remove these mounts from their peer groups by recursively
1479 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001480 */
1481 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001482 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001483 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001484 if (umount2(".", MNT_DETACH))
1485 pdie("umount(/)");
1486 /* Change back to the new root. */
1487 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001488 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001489 if (close(oldroot))
1490 return -errno;
1491 if (close(newroot))
1492 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001493 if (chroot("/"))
1494 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001495 /* Set correct CWD for getcwd(3). */
1496 if (chdir("/"))
1497 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001498
1499 return 0;
1500}
1501
Martin Pelikánab9eb442017-01-25 11:53:58 +11001502static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001503{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001504 const char fmt[] = "size=%zu,mode=1777";
1505 /* Count for the user storing ULLONG_MAX literally + extra space. */
1506 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1507 int ret;
1508
1509 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1510
1511 if (ret <= 0)
1512 pdie("tmpfs size spec error");
1513 else if ((size_t)ret >= sizeof(data))
1514 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001515 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001516 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001517}
1518
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001519static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001520{
1521 const char *kProcPath = "/proc";
1522 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001523 /*
1524 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001525 * /proc in our namespace, which means using MS_REMOUNT here would
1526 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001527 * namespace (!). Instead, remove their mount from our namespace lazily
1528 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001529 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001530 if (umount2(kProcPath, MNT_DETACH)) {
1531 /*
1532 * If we are in a new user namespace, umount(2) will fail.
1533 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1534 */
1535 if (j->flags.userns) {
1536 info("umount(/proc, MNT_DETACH) failed, "
1537 "this is expected when using user namespaces");
1538 } else {
1539 return -errno;
1540 }
1541 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001542 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001543 return -errno;
1544 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001545}
1546
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001547static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001548{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001549 kill(j->initpid, SIGKILL);
1550 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001551}
1552
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001553static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001554{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001555 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001556 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001557}
1558
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001559static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001560{
1561 size_t i;
1562
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001563 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001564 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001565 kill_child_and_die(j, "failed to add to cgroups");
1566 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001567}
1568
Dylan Reid0f72ef42017-06-06 15:42:49 -07001569static void set_rlimits_or_die(const struct minijail *j)
1570{
1571 size_t i;
1572
1573 for (i = 0; i < j->rlimit_count; ++i) {
1574 struct rlimit limit;
1575 limit.rlim_cur = j->rlimits[i].cur;
1576 limit.rlim_max = j->rlimits[i].max;
1577 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1578 kill_child_and_die(j, "failed to set rlimit");
1579 }
1580}
1581
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001582static void write_ugid_maps_or_die(const struct minijail *j)
1583{
1584 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1585 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001586 if (j->gidmap && j->flags.disable_setgroups) {
1587 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1588 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001589 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001590 if (ret == -ENOENT) {
1591 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1592 warn("could not disable setgroups(2)");
1593 } else
1594 kill_child_and_die(j, "failed to disable setgroups(2)");
1595 }
1596 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001597 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1598 kill_child_and_die(j, "failed to write gid_map");
1599}
1600
1601static void enter_user_namespace(const struct minijail *j)
1602{
Luis Hector Chavez71323552017-09-05 09:17:22 -07001603 int uid = j->flags.uid ? j->uid : 0;
1604 int gid = j->flags.gid ? j->gid : 0;
1605 if (j->gidmap && setresgid(gid, gid, gid)) {
1606 pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid,
1607 gid);
1608 }
1609 if (j->uidmap && setresuid(uid, uid, uid)) {
1610 pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid,
1611 uid);
1612 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001613}
1614
1615static void parent_setup_complete(int *pipe_fds)
1616{
1617 close(pipe_fds[0]);
1618 close(pipe_fds[1]);
1619}
1620
1621/*
1622 * wait_for_parent_setup: Called by the child process to wait for any
1623 * further parent-side setup to complete before continuing.
1624 */
1625static void wait_for_parent_setup(int *pipe_fds)
1626{
1627 char buf;
1628
1629 close(pipe_fds[1]);
1630
1631 /* Wait for parent to complete setup and close the pipe. */
1632 if (read(pipe_fds[0], &buf, 1) != 0)
1633 die("failed to sync with parent");
1634 close(pipe_fds[0]);
1635}
1636
1637static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001638{
Lutz Justen13807cb2017-01-03 17:11:55 +01001639 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1640 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001641 die("can only do one of inherit, keep, or set supplementary "
1642 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001643 }
1644
Lutz Justen13807cb2017-01-03 17:11:55 +01001645 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001646 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001647 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001648 } else if (j->flags.set_suppl_gids) {
1649 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001650 pdie("setgroups(suppl_gids) failed");
Luis Hector Chavez71323552017-09-05 09:17:22 -07001651 } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001652 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001653 * Only attempt to clear supplementary groups if we are changing
Luis Hector Chavez71323552017-09-05 09:17:22 -07001654 * users or groups, and if the caller did not request to disable
1655 * setgroups (used when entering a user namespace as a
1656 * non-privileged user).
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001657 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001658 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001659 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001660 }
1661
1662 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001663 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001664
1665 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001666 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001667}
1668
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001669static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1670{
1671 const uint64_t one = 1;
1672 unsigned int i;
1673 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1674 if (keep_mask & (one << i))
1675 continue;
1676 if (prctl(PR_CAPBSET_DROP, i))
1677 pdie("could not drop capability from bounding set");
1678 }
1679}
1680
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001681static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001682{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001683 if (!j->flags.use_caps)
1684 return;
1685
Elly Jonese1749eb2011-10-07 13:54:59 -04001686 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001687 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001688 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001689 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001690 unsigned int i;
1691 if (!caps)
1692 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001693 if (cap_clear(caps))
1694 die("can't clear caps");
1695
1696 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001697 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001698 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001699 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001700 flag[0] = i;
1701 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001702 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001703 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001704 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001705 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001706 die("can't add inheritable cap");
1707 }
1708 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001709 die("can't apply initial cleaned capset");
1710
1711 /*
1712 * Instead of dropping bounding set first, do it here in case
1713 * the caller had a more permissive bounding set which could
1714 * have been used above to raise a capability that wasn't already
1715 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1716 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001717 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001718
1719 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001720 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001721 flag[0] = CAP_SETPCAP;
1722 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1723 die("can't clear effective cap");
1724 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1725 die("can't clear permitted cap");
1726 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1727 die("can't clear inheritable cap");
1728 }
1729
1730 if (cap_set_proc(caps))
1731 die("can't apply final cleaned capset");
1732
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001733 /*
1734 * If ambient capabilities are supported, clear all capabilities first,
1735 * then raise the requested ones.
1736 */
1737 if (j->flags.set_ambient_caps) {
1738 if (!cap_ambient_supported()) {
1739 pdie("ambient capabilities not supported");
1740 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001741 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1742 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001743 pdie("can't clear ambient capabilities");
1744 }
1745
1746 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1747 if (!(j->caps & (one << i)))
1748 continue;
1749
1750 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1751 0) != 0) {
1752 pdie("prctl(PR_CAP_AMBIENT, "
1753 "PR_CAP_AMBIENT_RAISE, %u) failed",
1754 i);
1755 }
1756 }
1757 }
1758
Kees Cook323878a2013-02-05 15:35:24 -08001759 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001760}
1761
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001762static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001763{
1764 /*
1765 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1766 * in the kernel source tree for an explanation of the parameters.
1767 */
1768 if (j->flags.no_new_privs) {
1769 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1770 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1771 }
1772
1773 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001774 * Code running with ASan
1775 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1776 * will make system calls not included in the syscall filter policy,
1777 * which will likely crash the program. Skip setting seccomp filter in
1778 * that case.
1779 * 'running_with_asan()' has no inputs and is completely defined at
1780 * build time, so this cannot be used by an attacker to skip setting
1781 * seccomp filter.
1782 */
1783 if (j->flags.seccomp_filter && running_with_asan()) {
1784 warn("running with ASan, not setting seccomp filter");
1785 return;
1786 }
1787
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001788 if (j->flags.seccomp_filter) {
1789 if (j->flags.seccomp_filter_logging) {
1790 /*
1791 * If logging seccomp filter failures,
1792 * install the SIGSYS handler first.
1793 */
1794 if (install_sigsys_handler())
1795 pdie("failed to install SIGSYS handler");
1796 warn("logging seccomp filter failures");
1797 } else if (j->flags.seccomp_filter_tsync) {
1798 /*
1799 * If setting thread sync,
1800 * reset the SIGSYS signal handler so that
1801 * the entire thread group is killed.
1802 */
1803 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1804 pdie("failed to reset SIGSYS disposition");
1805 info("reset SIGSYS disposition");
1806 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001807 }
1808
1809 /*
1810 * Install the syscall filter.
1811 */
1812 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001813 if (j->flags.seccomp_filter_tsync) {
1814 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1815 SECCOMP_FILTER_FLAG_TSYNC,
1816 j->filter_prog)) {
1817 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001818 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001819 } else {
1820 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1821 j->filter_prog)) {
1822 pdie("prctl(seccomp_filter) failed");
1823 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001824 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001825 }
1826}
1827
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001828static pid_t forward_pid = -1;
1829
1830static void forward_signal(__attribute__((unused)) int nr,
1831 __attribute__((unused)) siginfo_t *siginfo,
1832 __attribute__((unused)) void *void_context)
1833{
1834 if (forward_pid != -1) {
1835 kill(forward_pid, nr);
1836 }
1837}
1838
1839static void install_signal_handlers(void)
1840{
1841 struct sigaction act;
1842
1843 memset(&act, 0, sizeof(act));
1844 act.sa_sigaction = &forward_signal;
1845 act.sa_flags = SA_SIGINFO | SA_RESTART;
1846
1847 /* Handle all signals, except SIGCHLD. */
1848 for (int nr = 1; nr < NSIG; nr++) {
1849 /*
1850 * We don't care if we get EINVAL: that just means that we
1851 * can't handle this signal, so let's skip it and continue.
1852 */
1853 sigaction(nr, &act, NULL);
1854 }
1855 /* Reset SIGCHLD's handler. */
1856 signal(SIGCHLD, SIG_DFL);
1857
1858 /* Handle real-time signals. */
1859 for (int nr = SIGRTMIN; nr <= SIGRTMAX; nr++) {
1860 sigaction(nr, &act, NULL);
1861 }
1862}
1863
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001864static const char *lookup_hook_name(minijail_hook_event_t event)
1865{
1866 switch (event) {
1867 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
1868 return "pre-drop-caps";
1869 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
1870 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001871 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
1872 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001873 case MINIJAIL_HOOK_EVENT_MAX:
1874 /*
1875 * Adding this in favor of a default case to force the
1876 * compiler to error out if a new enum value is added.
1877 */
1878 break;
1879 }
1880 return "unknown";
1881}
1882
1883static void run_hooks_or_die(const struct minijail *j,
1884 minijail_hook_event_t event)
1885{
1886 int rc;
1887 int hook_index = 0;
1888 for (struct hook *c = j->hooks_head; c; c = c->next) {
1889 if (c->event != event)
1890 continue;
1891 rc = c->hook(c->payload);
1892 if (rc != 0) {
1893 errno = -rc;
1894 pdie("%s hook (index %d) failed",
1895 lookup_hook_name(event), hook_index);
1896 }
1897 /* Only increase the index within the same hook event type. */
1898 ++hook_index;
1899 }
1900}
1901
Will Drewry6ac91122011-10-21 16:38:58 -05001902void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001903{
Dylan Reidf682d472015-09-17 21:39:07 -07001904 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001905 * If we're dropping caps, get the last valid cap from /proc now,
1906 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001907 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001908 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001909 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001910 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001911
Elly Jonese1749eb2011-10-07 13:54:59 -04001912 if (j->flags.pids)
1913 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001914 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001915
Lutz Justen13807cb2017-01-03 17:11:55 +01001916 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001917 die("cannot inherit supplementary groups without setting a "
1918 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001919
Elly Jonesdd3e8512012-01-23 15:13:38 -05001920 /*
1921 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001922 * so we don't even try. If any of our operations fail, we abort() the
1923 * entire process.
1924 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001925 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001926 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001927
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001928 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001929 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001930 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001931 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001932 * Unless asked not to, remount all filesystems as private.
1933 * If they are shared, new bind mounts will creep out of our
1934 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001935 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1936 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001937 if (!j->flags.skip_remount_private) {
1938 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001939 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1940 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001941 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001942 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001943
Dylan Reidf7942472015-11-18 17:55:26 -08001944 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001945 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001946 }
1947
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001948 if (j->flags.uts) {
1949 if (unshare(CLONE_NEWUTS))
1950 pdie("unshare(CLONE_NEWUTS) failed");
1951
1952 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
1953 pdie("sethostname(%s) failed", j->hostname);
1954 }
1955
Dylan Reid1102f5a2015-09-15 11:52:20 -07001956 if (j->flags.enter_net) {
1957 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001958 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001959 } else if (j->flags.net) {
1960 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001961 pdie("unshare(CLONE_NEWNET) failed");
1962 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001963 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001964
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001965 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001966 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001967
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08001968 if (j->flags.new_session_keyring) {
1969 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
1970 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
1971 }
1972
Mike Frysinger33ffef32017-01-13 19:53:19 -05001973 /*
1974 * This has to come before the chroot/pivot_root in case there are
1975 * bind mounts from /dev into the chroot dev.
1976 */
1977 char *dev_path = NULL;
1978 if (j->flags.mount_dev && mount_dev(&dev_path))
1979 pdie("mount_dev");
Elly Jones51a5b6c2011-10-12 19:09:26 -04001980
Mike Frysinger33ffef32017-01-13 19:53:19 -05001981 if (j->flags.chroot && enter_chroot(j, dev_path)) {
1982 if (dev_path)
1983 mount_dev_cleanup(dev_path);
1984 pdie("chroot");
1985 }
1986
1987 if (j->flags.pivot_root && enter_pivot_root(j, dev_path)) {
1988 if (dev_path)
1989 mount_dev_cleanup(dev_path);
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001990 pdie("pivot_root");
Mike Frysinger33ffef32017-01-13 19:53:19 -05001991 }
1992
1993 /*
1994 * If using a chroot or pivot root, we already finalized /dev at
1995 * the right point. If not, we need to call it ourselves.
1996 */
1997 if (j->flags.mount_dev && !j->flags.chroot && !j->flags.pivot_root &&
1998 mount_dev_finalize(j, dev_path))
1999 pdie("mount_dev_finalize");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08002000
Martin Pelikánab9eb442017-01-25 11:53:58 +11002001 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07002002 pdie("mount_tmp");
2003
Dylan Reid791f5772015-09-14 20:02:42 -07002004 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04002005 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04002006
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002007 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
2008
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002009 /*
2010 * If we're only dropping capabilities from the bounding set, but not
2011 * from the thread's (permitted|inheritable|effective) sets, do it now.
2012 */
2013 if (j->flags.capbset_drop) {
2014 drop_capbset(j->cap_bset, last_valid_cap);
2015 }
2016
2017 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002018 /*
2019 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04002020 * capability to change uids, our attempt to use setuid()
2021 * below will fail. Hang on to root caps across setuid(), then
2022 * lock securebits.
2023 */
2024 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002025 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002026
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -07002027 if (lock_securebits(j->securebits_skip_mask) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002028 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002029 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002030 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002031
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002032 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002033 /*
2034 * If we're setting no_new_privs, we can drop privileges
2035 * before setting seccomp filter. This way filter policies
2036 * don't need to allow privilege-dropping syscalls.
2037 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002038 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002039 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002040 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002041 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002042 /*
2043 * If we're not setting no_new_privs,
2044 * we need to set seccomp filter *before* dropping privileges.
2045 * WARNING: this means that filter policies *must* allow
2046 * setgroups()/setresgid()/setresuid() for dropping root and
2047 * capget()/capset()/prctl() for dropping caps.
2048 */
2049 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002050 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002051 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04002052 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002053
Elly Jonesdd3e8512012-01-23 15:13:38 -05002054 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08002055 * Select the specified alternate syscall table. The table must not
2056 * block prctl(2) if we're using seccomp as well.
2057 */
2058 if (j->flags.alt_syscall) {
2059 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002060 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08002061 }
2062
2063 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05002064 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04002065 * privilege-dropping syscalls :)
2066 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002067 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04002068 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002069 warn("seccomp not supported");
2070 return;
2071 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002072 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002073 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002074}
2075
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002076/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04002077static int init_exitstatus = 0;
2078
Will Drewry6ac91122011-10-21 16:38:58 -05002079void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04002080{
2081 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04002082}
2083
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002084void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04002085{
2086 pid_t pid;
2087 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002088 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002089 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002090 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002091 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002092 /*
2093 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04002094 * left inside our pid namespace or we get a signal.
2095 */
2096 if (pid == rootpid)
2097 init_exitstatus = status;
2098 }
2099 if (!WIFEXITED(init_exitstatus))
2100 _exit(MINIJAIL_ERR_INIT);
2101 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04002102}
2103
Will Drewry6ac91122011-10-21 16:38:58 -05002104int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002105{
2106 size_t sz = 0;
2107 size_t bytes = read(fd, &sz, sizeof(sz));
2108 char *buf;
2109 int r;
2110 if (sizeof(sz) != bytes)
2111 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002112 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04002113 return -E2BIG;
2114 buf = malloc(sz);
2115 if (!buf)
2116 return -ENOMEM;
2117 bytes = read(fd, buf, sz);
2118 if (bytes != sz) {
2119 free(buf);
2120 return -EINVAL;
2121 }
2122 r = minijail_unmarshal(j, buf, sz);
2123 free(buf);
2124 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002125}
2126
Will Drewry6ac91122011-10-21 16:38:58 -05002127int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04002128{
2129 char *buf;
2130 size_t sz = minijail_size(j);
2131 ssize_t written;
2132 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04002133
Elly Jonese1749eb2011-10-07 13:54:59 -04002134 if (!sz)
2135 return -EINVAL;
2136 buf = malloc(sz);
2137 r = minijail_marshal(j, buf, sz);
2138 if (r) {
2139 free(buf);
2140 return r;
2141 }
2142 /* Sends [size][minijail]. */
2143 written = write(fd, &sz, sizeof(sz));
2144 if (written != sizeof(sz)) {
2145 free(buf);
2146 return -EFAULT;
2147 }
2148 written = write(fd, buf, sz);
2149 if (written < 0 || (size_t) written != sz) {
2150 free(buf);
2151 return -EFAULT;
2152 }
2153 free(buf);
2154 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002155}
Elly Jonescd7a9042011-07-22 13:56:51 -04002156
Will Drewry6ac91122011-10-21 16:38:58 -05002157int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04002158{
Daniel Erat5b7a3182015-08-19 16:06:22 -06002159#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002160 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002161 return 0;
2162#else
Elly Jonese1749eb2011-10-07 13:54:59 -04002163 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
2164 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
2165 if (!newenv)
2166 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04002167
Elly Jonese1749eb2011-10-07 13:54:59 -04002168 /* Only insert a separating space if we have something to separate... */
2169 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
2170 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04002171
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002172 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002173 setenv(kLdPreloadEnvVar, newenv, 1);
2174 free(newenv);
2175 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002176#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04002177}
2178
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002179static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04002180{
2181 int r = pipe(fds);
2182 char fd_buf[11];
2183 if (r)
2184 return r;
2185 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2186 if (r <= 0)
2187 return -EINVAL;
2188 setenv(kFdEnvVar, fd_buf, 1);
2189 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05002190}
2191
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002192static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002193{
2194 const char *kFdPath = "/proc/self/fd";
2195
2196 DIR *d = opendir(kFdPath);
2197 struct dirent *dir_entry;
2198
2199 if (d == NULL)
2200 return -1;
2201 int dir_fd = dirfd(d);
2202 while ((dir_entry = readdir(d)) != NULL) {
2203 size_t i;
2204 char *end;
2205 bool should_close = true;
2206 const int fd = strtol(dir_entry->d_name, &end, 10);
2207
2208 if ((*end) != '\0') {
2209 continue;
2210 }
2211 /*
2212 * We might have set up some pipes that we want to share with
2213 * the parent process, and should not be closed.
2214 */
2215 for (i = 0; i < size; ++i) {
2216 if (fd == inheritable_fds[i]) {
2217 should_close = false;
2218 break;
2219 }
2220 }
2221 /* Also avoid closing the directory fd. */
2222 if (should_close && fd != dir_fd)
2223 close(fd);
2224 }
2225 closedir(d);
2226 return 0;
2227}
2228
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002229static int redirect_fds(struct minijail *j)
2230{
2231 size_t i, i2;
2232 int closeable;
2233 for (i = 0; i < j->preserved_fd_count; i++) {
2234 if (dup2(j->preserved_fds[i].parent_fd,
2235 j->preserved_fds[i].child_fd) == -1) {
2236 return -1;
2237 }
2238 }
2239 /*
2240 * After all fds have been duped, we are now free to close all parent
2241 * fds that are *not* child fds.
2242 */
2243 for (i = 0; i < j->preserved_fd_count; i++) {
2244 closeable = true;
2245 for (i2 = 0; i2 < j->preserved_fd_count; i2++) {
2246 closeable &= j->preserved_fds[i].parent_fd !=
2247 j->preserved_fds[i2].child_fd;
2248 }
2249 if (closeable)
2250 close(j->preserved_fds[i].parent_fd);
2251 }
2252 return 0;
2253}
2254
Dylan Reidacfb8be2017-08-25 12:56:51 -07002255/*
2256 * Structure that specifies how to start a minijail.
2257 *
Dylan Reid0412dcc2017-08-24 11:33:15 -07002258 * filename - The program to exec in the child. Required if `exec_in_child` = 1.
2259 * argv - Arguments for the child program. Required if `exec_in_child` = 1.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002260 * use_preload - If true use LD_PRELOAD.
Dylan Reid0412dcc2017-08-24 11:33:15 -07002261 * exec_in_child - If true, run `filename`. Otherwise, the child will return to
2262 * the caller.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002263 */
2264struct minijail_run_config {
2265 const char *filename;
2266 char *const *argv;
2267 int use_preload;
Dylan Reid0412dcc2017-08-24 11:33:15 -07002268 int exec_in_child;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002269};
2270
2271/*
2272 * Set of pointers to fill with values from minijail_run.
2273 * All arguments are allowed to be NULL if unused.
2274 *
2275 * pstdin_fd - Filled with stdin pipe if non-NULL.
2276 * pstdout_fd - Filled with stdout pipe if non-NULL.
2277 * pstderr_fd - Filled with stderr pipe if non-NULL.
2278 * pchild_pid - Filled with the pid of the child process if non-NULL.
2279 */
2280struct minijail_run_status {
2281 int *pstdin_fd;
2282 int *pstdout_fd;
2283 int *pstderr_fd;
2284 pid_t *pchild_pid;
2285};
2286
Dylan Reid18c49c82017-08-25 14:52:27 -07002287static int minijail_run_internal(struct minijail *j,
2288 const struct minijail_run_config *config,
2289 struct minijail_run_status *status_out);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002290
Will Drewry6ac91122011-10-21 16:38:58 -05002291int API minijail_run(struct minijail *j, const char *filename,
2292 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002293{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002294 struct minijail_run_config config = {
2295 .filename = filename,
2296 .argv = argv,
2297 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002298 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002299 };
2300 struct minijail_run_status status = {};
2301 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002302}
2303
2304int API minijail_run_pid(struct minijail *j, const char *filename,
2305 char *const argv[], pid_t *pchild_pid)
2306{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002307 struct minijail_run_config config = {
2308 .filename = filename,
2309 .argv = argv,
2310 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002311 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002312 };
2313 struct minijail_run_status status = {
2314 .pchild_pid = pchild_pid,
2315 };
2316 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002317}
2318
2319int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002320 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002321{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002322 struct minijail_run_config config = {
2323 .filename = filename,
2324 .argv = argv,
2325 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002326 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002327 };
2328 struct minijail_run_status status = {
2329 .pstdin_fd = pstdin_fd,
2330 };
2331 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002332}
2333
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002334int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002335 char *const argv[], pid_t *pchild_pid,
2336 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002337{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002338 struct minijail_run_config config = {
2339 .filename = filename,
2340 .argv = argv,
2341 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002342 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002343 };
2344 struct minijail_run_status status = {
2345 .pstdin_fd = pstdin_fd,
2346 .pstdout_fd = pstdout_fd,
2347 .pstderr_fd = pstderr_fd,
2348 .pchild_pid = pchild_pid,
2349 };
2350 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002351}
2352
2353int API minijail_run_no_preload(struct minijail *j, const char *filename,
2354 char *const argv[])
2355{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002356 struct minijail_run_config config = {
2357 .filename = filename,
2358 .argv = argv,
2359 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002360 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002361 };
2362 struct minijail_run_status status = {};
2363 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002364}
2365
Samuel Tan63187f42015-10-16 13:01:53 -07002366int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002367 const char *filename,
2368 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002369 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002370 int *pstdin_fd,
2371 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002372 int *pstderr_fd)
2373{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002374 struct minijail_run_config config = {
2375 .filename = filename,
2376 .argv = argv,
2377 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002378 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002379 };
2380 struct minijail_run_status status = {
2381 .pstdin_fd = pstdin_fd,
2382 .pstdout_fd = pstdout_fd,
2383 .pstderr_fd = pstderr_fd,
2384 .pchild_pid = pchild_pid,
2385 };
2386 return minijail_run_internal(j, &config, &status);
Samuel Tan63187f42015-10-16 13:01:53 -07002387}
2388
Dylan Reid0412dcc2017-08-24 11:33:15 -07002389pid_t API minijail_fork(struct minijail *j)
2390{
2391 struct minijail_run_config config = {};
2392 struct minijail_run_status status = {};
2393 return minijail_run_internal(j, &config, &status);
2394}
2395
Dylan Reid18c49c82017-08-25 14:52:27 -07002396static int minijail_run_internal(struct minijail *j,
2397 const struct minijail_run_config *config,
2398 struct minijail_run_status *status_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002399{
Elly Jonese1749eb2011-10-07 13:54:59 -04002400 char *oldenv, *oldenv_copy = NULL;
2401 pid_t child_pid;
2402 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002403 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002404 int stdout_fds[2];
2405 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08002406 int child_sync_pipe_fds[2];
2407 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002408 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002409 /* We need to remember this across the minijail_preexec() call. */
2410 int pid_namespace = j->flags.pids;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -07002411 /*
2412 * Create an init process if we are entering a pid namespace, unless the
2413 * user has explicitly opted out by calling minijail_run_as_init().
2414 */
2415 int do_init = j->flags.do_init && !j->flags.run_as_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002416 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002417
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002418 if (use_preload) {
Dylan Reid0412dcc2017-08-24 11:33:15 -07002419 if (j->hooks_head != NULL)
2420 die("Minijail hooks are not supported with LD_PRELOAD");
2421 if (!config->exec_in_child)
2422 die("minijail_fork is not supported with LD_PRELOAD");
2423
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002424 oldenv = getenv(kLdPreloadEnvVar);
2425 if (oldenv) {
2426 oldenv_copy = strdup(oldenv);
2427 if (!oldenv_copy)
2428 return -ENOMEM;
2429 }
2430
2431 if (setup_preload())
2432 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002433 }
Will Drewryf89aef52011-09-16 16:48:57 -05002434
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002435 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002436 if (j->flags.use_caps && j->caps != 0 &&
2437 !j->flags.set_ambient_caps) {
2438 die("non-empty, non-ambient capabilities are not "
2439 "supported without LD_PRELOAD");
2440 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002441 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002442
Elly Jonesdd3e8512012-01-23 15:13:38 -05002443 /*
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002444 * Make the process group ID of this process equal to its PID.
2445 * In the non-interactive case (e.g. when the parent process is started
2446 * from init) this ensures the parent process and the jailed process
2447 * can be killed together.
2448 * When the parent process is started from the console this ensures
2449 * the call to setsid(2) in the jailed process succeeds.
2450 *
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002451 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
2452 * the process is already a process group leader.
2453 */
2454 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
2455 if (errno != EPERM) {
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002456 pdie("setpgid(0, 0) failed");
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002457 }
2458 }
2459
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002460 if (use_preload) {
2461 /*
2462 * Before we fork(2) and execve(2) the child process, we need
2463 * to open a pipe(2) to send the minijail configuration over.
2464 */
2465 if (setup_pipe(pipe_fds))
2466 return -EFAULT;
2467 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002468
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002469 /*
2470 * If we want to write to the child process' standard input,
2471 * create the pipe(2) now.
2472 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002473 if (status_out->pstdin_fd) {
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002474 if (pipe(stdin_fds))
2475 return -EFAULT;
2476 }
2477
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002478 /*
2479 * If we want to read from the child process' standard output,
2480 * create the pipe(2) now.
2481 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002482 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002483 if (pipe(stdout_fds))
2484 return -EFAULT;
2485 }
2486
2487 /*
2488 * If we want to read from the child process' standard error,
2489 * create the pipe(2) now.
2490 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002491 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002492 if (pipe(stderr_fds))
2493 return -EFAULT;
2494 }
2495
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002496 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002497 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002498 * or if we need to add the child process to cgroups, create the pipe(2)
2499 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002500 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002501 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002502 sync_child = 1;
2503 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002504 return -EFAULT;
2505 }
2506
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002507 /*
2508 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002509 *
2510 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2511 *
2512 * In multithreaded programs, there are a bunch of locks inside libc,
2513 * some of which may be held by other threads at the time that we call
2514 * minijail_run_pid(). If we call fork(), glibc does its level best to
2515 * ensure that we hold all of these locks before it calls clone()
2516 * internally and drop them after clone() returns, but when we call
2517 * sys_clone(2) directly, all that gets bypassed and we end up with a
2518 * child address space where some of libc's important locks are held by
2519 * other threads (which did not get cloned, and hence will never release
2520 * those locks). This is okay so long as we call exec() immediately
2521 * after, but a bunch of seemingly-innocent libc functions like setenv()
2522 * take locks.
2523 *
2524 * Hence, only call sys_clone() if we need to, in order to get at pid
2525 * namespacing. If we follow this path, the child's address space might
2526 * have broken locks; you may only call functions that do not acquire
2527 * any locks.
2528 *
2529 * Unfortunately, fork() acquires every lock it can get its hands on, as
2530 * previously detailed, so this function is highly likely to deadlock
2531 * later on (see "deadlock here") if we're multithreaded.
2532 *
2533 * We might hack around this by having the clone()d child (init of the
2534 * pid namespace) return directly, rather than leaving the clone()d
2535 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002536 * its fork()ed child return in turn), but that process would be
2537 * crippled with its libc locks potentially broken. We might try
2538 * fork()ing in the parent before we clone() to ensure that we own all
2539 * the locks, but then we have to have the forked child hanging around
2540 * consuming resources (and possibly having file descriptors / shared
2541 * memory regions / etc attached). We'd need to keep the child around to
2542 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002543 *
2544 * TODO(ellyjones): figure out if the "forked child hanging around"
2545 * problem is fixable or not. It would be nice if we worked in this
2546 * case.
2547 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002548 if (pid_namespace) {
2549 int clone_flags = CLONE_NEWPID | SIGCHLD;
2550 if (j->flags.userns)
2551 clone_flags |= CLONE_NEWUSER;
2552 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002553 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002554 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002555 }
Elly Jones761b7412012-06-13 15:49:52 -04002556
Elly Jonese1749eb2011-10-07 13:54:59 -04002557 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002558 if (use_preload) {
2559 free(oldenv_copy);
2560 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002561 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002562 }
Will Drewryf89aef52011-09-16 16:48:57 -05002563
Elly Jonese1749eb2011-10-07 13:54:59 -04002564 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002565 if (use_preload) {
2566 /* Restore parent's LD_PRELOAD. */
2567 if (oldenv_copy) {
2568 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2569 free(oldenv_copy);
2570 } else {
2571 unsetenv(kLdPreloadEnvVar);
2572 }
2573 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002574 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002575
Elly Jonese1749eb2011-10-07 13:54:59 -04002576 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002577
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002578 if (j->flags.forward_signals) {
2579 forward_pid = child_pid;
2580 install_signal_handlers();
2581 }
2582
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002583 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002584 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002585
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002586 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002587 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002588
Dylan Reid0f72ef42017-06-06 15:42:49 -07002589 if (j->rlimit_count)
2590 set_rlimits_or_die(j);
2591
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002592 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002593 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002594
2595 if (sync_child)
2596 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002597
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002598 if (use_preload) {
2599 /* Send marshalled minijail. */
2600 close(pipe_fds[0]); /* read endpoint */
2601 ret = minijail_to_fd(j, pipe_fds[1]);
2602 close(pipe_fds[1]); /* write endpoint */
2603 if (ret) {
2604 kill(j->initpid, SIGKILL);
2605 die("failed to send marshalled minijail");
2606 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002607 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002608
Dylan Reidacfb8be2017-08-25 12:56:51 -07002609 if (status_out->pchild_pid)
2610 *status_out->pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002611
2612 /*
2613 * If we want to write to the child process' standard input,
2614 * set up the write end of the pipe.
2615 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002616 if (status_out->pstdin_fd)
2617 *status_out->pstdin_fd =
2618 setup_pipe_end(stdin_fds, 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002619
2620 /*
2621 * If we want to read from the child process' standard output,
2622 * set up the read end of the pipe.
2623 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002624 if (status_out->pstdout_fd)
2625 *status_out->pstdout_fd =
2626 setup_pipe_end(stdout_fds, 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002627
2628 /*
2629 * If we want to read from the child process' standard error,
2630 * set up the read end of the pipe.
2631 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002632 if (status_out->pstderr_fd)
2633 *status_out->pstderr_fd =
2634 setup_pipe_end(stderr_fds, 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002635
Dylan Reid0412dcc2017-08-24 11:33:15 -07002636 /*
2637 * If forking return the child pid, in the normal exec case
2638 * return 0 for success.
2639 */
2640 if (!config->exec_in_child)
2641 return child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04002642 return 0;
2643 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002644 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002645 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002646
Peter Qiu2860c462015-12-16 15:13:06 -08002647 if (j->flags.reset_signal_mask) {
2648 sigset_t signal_mask;
2649 if (sigemptyset(&signal_mask) != 0)
2650 pdie("sigemptyset failed");
2651 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2652 pdie("sigprocmask failed");
2653 }
2654
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002655 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002656 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002657 int inheritable_fds[kMaxInheritableFdsSize];
2658 size_t size = 0;
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002659 size_t i;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002660 if (use_preload) {
2661 inheritable_fds[size++] = pipe_fds[0];
2662 inheritable_fds[size++] = pipe_fds[1];
2663 }
2664 if (sync_child) {
2665 inheritable_fds[size++] = child_sync_pipe_fds[0];
2666 inheritable_fds[size++] = child_sync_pipe_fds[1];
2667 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002668 if (status_out->pstdin_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002669 inheritable_fds[size++] = stdin_fds[0];
2670 inheritable_fds[size++] = stdin_fds[1];
2671 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002672 if (status_out->pstdout_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002673 inheritable_fds[size++] = stdout_fds[0];
2674 inheritable_fds[size++] = stdout_fds[1];
2675 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002676 if (status_out->pstderr_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002677 inheritable_fds[size++] = stderr_fds[0];
2678 inheritable_fds[size++] = stderr_fds[1];
2679 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002680 for (i = 0; i < j->preserved_fd_count; i++) {
2681 /*
2682 * Preserve all parent_fds. They will be dup2(2)-ed in
2683 * the child later.
2684 */
2685 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
2686 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002687
2688 if (close_open_fds(inheritable_fds, size) < 0)
2689 die("failed to close open file descriptors");
2690 }
2691
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002692 if (redirect_fds(j))
2693 die("failed to set up fd redirections");
2694
Dylan Reidce5b55e2016-01-13 11:04:16 -08002695 if (sync_child)
2696 wait_for_parent_setup(child_sync_pipe_fds);
2697
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002698 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002699 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002700
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002701 /*
2702 * If we want to write to the jailed process' standard input,
2703 * set up the read end of the pipe.
2704 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002705 if (status_out->pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002706 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2707 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002708 die("failed to set up stdin pipe");
2709 }
2710
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002711 /*
2712 * If we want to read from the jailed process' standard output,
2713 * set up the write end of the pipe.
2714 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002715 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002716 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2717 STDOUT_FILENO) < 0)
2718 die("failed to set up stdout pipe");
2719 }
2720
2721 /*
2722 * If we want to read from the jailed process' standard error,
2723 * set up the write end of the pipe.
2724 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002725 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002726 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2727 STDERR_FILENO) < 0)
2728 die("failed to set up stderr pipe");
2729 }
2730
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002731 /*
2732 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2733 * This prevents the jailed process from using the TIOCSTI ioctl
2734 * to push characters into the parent process terminal's input buffer,
2735 * therefore escaping the jail.
2736 */
2737 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2738 isatty(STDERR_FILENO)) {
2739 if (setsid() < 0) {
2740 pdie("setsid() failed");
2741 }
2742 }
2743
Dylan Reid791f5772015-09-14 20:02:42 -07002744 /* If running an init program, let it decide when/how to mount /proc. */
2745 if (pid_namespace && !do_init)
2746 j->flags.remount_proc_ro = 0;
2747
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002748 if (use_preload) {
2749 /* Strip out flags that cannot be inherited across execve(2). */
2750 minijail_preexec(j);
2751 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002752 /*
2753 * If not using LD_PRELOAD, do all jailing before execve(2).
2754 * Note that PID namespaces can only be entered on fork(2),
2755 * so that flag is still cleared.
2756 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002757 j->flags.pids = 0;
2758 }
Dylan Reid0412dcc2017-08-24 11:33:15 -07002759
2760 /*
2761 * Jail this process.
2762 * If forking, return.
2763 * If not, execve(2) the target.
2764 */
Elly Jonese1749eb2011-10-07 13:54:59 -04002765 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002766
Dylan Reid0412dcc2017-08-24 11:33:15 -07002767 if (config->exec_in_child && pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002768 /*
2769 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002770 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002771 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002772 * a child to actually run the program. If |do_init == 0|, we
2773 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002774 *
2775 * If we're multithreaded, we'll probably deadlock here. See
2776 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002777 */
2778 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002779 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002780 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002781 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002782 /*
2783 * Best effort. Don't bother checking the return value.
2784 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002785 prctl(PR_SET_NAME, "minijail-init");
2786 init(child_pid); /* Never returns. */
2787 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002788 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002789
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002790 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
2791
Dylan Reid0412dcc2017-08-24 11:33:15 -07002792 if (!config->exec_in_child)
2793 return 0;
2794
Elly Jonesdd3e8512012-01-23 15:13:38 -05002795 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002796 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002797 * calling process
2798 * -> execve()-ing process
2799 * If we are:
2800 * calling process
2801 * -> init()-ing process
2802 * -> execve()-ing process
2803 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002804 ret = execve(config->filename, config->argv, environ);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002805 if (ret == -1) {
Dylan Reidacfb8be2017-08-25 12:56:51 -07002806 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002807 }
2808 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002809}
2810
Will Drewry6ac91122011-10-21 16:38:58 -05002811int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002812{
2813 int st;
2814 if (kill(j->initpid, SIGTERM))
2815 return -errno;
2816 if (waitpid(j->initpid, &st, 0) < 0)
2817 return -errno;
2818 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002819}
2820
Will Drewry6ac91122011-10-21 16:38:58 -05002821int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002822{
2823 int st;
2824 if (waitpid(j->initpid, &st, 0) < 0)
2825 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002826
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002827 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002828 int error_status = st;
2829 if (WIFSIGNALED(st)) {
2830 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002831 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002832 j->initpid, signum);
2833 /*
2834 * We return MINIJAIL_ERR_JAIL if the process received
2835 * SIGSYS, which happens when a syscall is blocked by
2836 * seccomp filters.
2837 * If not, we do what bash(1) does:
2838 * $? = 128 + signum
2839 */
2840 if (signum == SIGSYS) {
2841 error_status = MINIJAIL_ERR_JAIL;
2842 } else {
2843 error_status = 128 + signum;
2844 }
2845 }
2846 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002847 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002848
2849 int exit_status = WEXITSTATUS(st);
2850 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002851 info("child process %d exited with status %d",
2852 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002853
2854 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002855}
2856
Will Drewry6ac91122011-10-21 16:38:58 -05002857void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002858{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002859 size_t i;
2860
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002861 if (j->flags.seccomp_filter && j->filter_prog) {
2862 free(j->filter_prog->filter);
2863 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002864 }
Dylan Reid648b2202015-10-23 00:50:00 -07002865 while (j->mounts_head) {
2866 struct mountpoint *m = j->mounts_head;
2867 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002868 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002869 free(m->type);
2870 free(m->dest);
2871 free(m->src);
2872 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002873 }
Dylan Reid648b2202015-10-23 00:50:00 -07002874 j->mounts_tail = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002875 while (j->hooks_head) {
2876 struct hook *c = j->hooks_head;
2877 j->hooks_head = c->next;
2878 free(c);
2879 }
2880 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002881 if (j->user)
2882 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002883 if (j->suppl_gid_list)
2884 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002885 if (j->chrootdir)
2886 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002887 if (j->pid_file_path)
2888 free(j->pid_file_path);
2889 if (j->uidmap)
2890 free(j->uidmap);
2891 if (j->gidmap)
2892 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002893 if (j->hostname)
2894 free(j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08002895 if (j->alt_syscall_table)
2896 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002897 for (i = 0; i < j->cgroup_count; ++i)
2898 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002899 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002900}
Luis Hector Chavez114a9302017-09-05 20:36:58 -07002901
2902void API minijail_log_to_fd(int fd, int min_priority)
2903{
2904 init_logging(LOG_TO_FD, fd, min_priority);
2905}