blob: 2e3e8b6d1532a73fb7a2bc686529675a445ac22b [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 <pwd.h>
17#include <sched.h>
18#include <signal.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070019#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080020#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040024#include <sys/capability.h>
25#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050026#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040027#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070028#include <sys/stat.h>
29#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080030#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040031#include <sys/wait.h>
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -070032#include <syscall.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040033#include <unistd.h>
34
35#include "libminijail.h"
36#include "libminijail-private.h"
37
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070038#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080039#include "syscall_filter.h"
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040040#include "syscall_wrapper.h"
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040041#include "system.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070042#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080043
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070044/* Until these are reliably available in linux/prctl.h. */
Andrew Brestickereac28942015-11-11 16:04:46 -080045#ifndef PR_ALT_SYSCALL
46# define PR_ALT_SYSCALL 0x43724f53
47#endif
48
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040049/* Seccomp filter related flags. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080050#ifndef PR_SET_NO_NEW_PRIVS
51# define PR_SET_NO_NEW_PRIVS 38
52#endif
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040053
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080054#ifndef SECCOMP_MODE_FILTER
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040055#define SECCOMP_MODE_FILTER 2 /* Uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050056#endif
57
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040058#ifndef SECCOMP_SET_MODE_STRICT
59# define SECCOMP_SET_MODE_STRICT 0
60#endif
61#ifndef SECCOMP_SET_MODE_FILTER
62# define SECCOMP_SET_MODE_FILTER 1
63#endif
64
65#ifndef SECCOMP_FILTER_FLAG_TSYNC
66# define SECCOMP_FILTER_FLAG_TSYNC 1
67#endif
68/* End seccomp filter related flags. */
69
Dylan Reid4cbc2a52016-06-17 19:06:07 -070070/* New cgroup namespace might not be in linux-headers yet. */
71#ifndef CLONE_NEWCGROUP
72# define CLONE_NEWCGROUP 0x02000000
73#endif
74
Dylan Reid605ce7f2016-01-19 19:21:00 -080075#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
76
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -080077/* Keyctl commands. */
78#define KEYCTL_JOIN_SESSION_KEYRING 1
79
Dylan Reid648b2202015-10-23 00:50:00 -070080struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040081 char *src;
82 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070083 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -070084 char *data;
85 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -070086 unsigned long flags;
87 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040088};
89
Will Drewryf89aef52011-09-16 16:48:57 -050090struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070091 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070092 * WARNING: if you add a flag here you need to make sure it's
93 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070094 */
Elly Jonese1749eb2011-10-07 13:54:59 -040095 struct {
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -070096 int uid : 1;
97 int gid : 1;
Lutz Justen13807cb2017-01-03 17:11:55 +010098 int inherit_suppl_gids : 1;
99 int set_suppl_gids : 1;
100 int keep_suppl_gids : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700101 int use_caps : 1;
102 int capbset_drop : 1;
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400103 int set_ambient_caps : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700104 int vfs : 1;
105 int enter_vfs : 1;
106 int skip_remount_private : 1;
107 int pids : 1;
108 int ipc : 1;
109 int net : 1;
110 int enter_net : 1;
111 int ns_cgroups : 1;
112 int userns : 1;
113 int disable_setgroups : 1;
114 int seccomp : 1;
115 int remount_proc_ro : 1;
116 int no_new_privs : 1;
117 int seccomp_filter : 1;
118 int seccomp_filter_tsync : 1;
119 int seccomp_filter_logging : 1;
120 int chroot : 1;
121 int pivot_root : 1;
122 int mount_tmp : 1;
123 int do_init : 1;
124 int pid_file : 1;
125 int cgroups : 1;
126 int alt_syscall : 1;
127 int reset_signal_mask : 1;
128 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800129 int new_session_keyring : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400130 } flags;
131 uid_t uid;
132 gid_t gid;
133 gid_t usergid;
134 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800135 size_t suppl_gid_count;
136 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400137 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800138 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400139 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700140 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700141 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400142 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800143 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800144 char *uidmap;
145 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800146 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800147 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800148 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700149 struct mountpoint *mounts_head;
150 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800151 size_t mounts_count;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100152 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800153 char *cgroups[MAX_CGROUPS];
154 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500155};
156
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700157/*
158 * Strip out flags meant for the parent.
159 * We keep things that are not inherited across execve(2) (e.g. capabilities),
160 * or are easier to set after execve(2) (e.g. seccomp filters).
161 */
162void minijail_preenter(struct minijail *j)
163{
164 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700165 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900166 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700167 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700168 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800169 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800170 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800171 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700172}
173
174/*
175 * Strip out flags meant for the child.
176 * We keep things that are inherited across execve(2).
177 */
178void minijail_preexec(struct minijail *j)
179{
180 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700181 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800182 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700183 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800184 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700185 if (j->user)
186 free(j->user);
187 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800188 if (j->suppl_gid_list)
189 free(j->suppl_gid_list);
190 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700191 memset(&j->flags, 0, sizeof(j->flags));
192 /* Now restore anything we meant to keep. */
193 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700194 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800195 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700196 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800197 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700198 /* Note, |pids| will already have been used before this call. */
199}
200
201/* Minijail API. */
202
Will Drewry6ac91122011-10-21 16:38:58 -0500203struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400204{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400205 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400206}
207
Will Drewry6ac91122011-10-21 16:38:58 -0500208void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400209{
210 if (uid == 0)
211 die("useless change to uid 0");
212 j->uid = uid;
213 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400214}
215
Will Drewry6ac91122011-10-21 16:38:58 -0500216void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400217{
218 if (gid == 0)
219 die("useless change to gid 0");
220 j->gid = gid;
221 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400222}
223
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800224void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
225 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800226{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800227 size_t i;
228
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500229 if (j->flags.inherit_suppl_gids)
230 die("cannot inherit *and* set supplementary groups");
231 if (j->flags.keep_suppl_gids)
232 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800233
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800234 if (size == 0) {
235 /* Clear supplementary groups. */
236 j->suppl_gid_list = NULL;
237 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100238 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800239 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800240 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800241
242 /* Copy the gid_t array. */
243 j->suppl_gid_list = calloc(size, sizeof(gid_t));
244 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800245 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800246 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800247 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800248 j->suppl_gid_list[i] = list[i];
249 }
250 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100251 j->flags.set_suppl_gids = 1;
252}
253
254void API minijail_keep_supplementary_gids(struct minijail *j) {
255 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800256}
257
Will Drewry6ac91122011-10-21 16:38:58 -0500258int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400259{
260 char *buf = NULL;
261 struct passwd pw;
262 struct passwd *ppw = NULL;
263 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
264 if (sz == -1)
265 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400266
Elly Jonesdd3e8512012-01-23 15:13:38 -0500267 /*
268 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400269 * the maximum needed size of the buffer, so we don't have to search.
270 */
271 buf = malloc(sz);
272 if (!buf)
273 return -ENOMEM;
274 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500275 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800276 * We're safe to free the buffer here. The strings inside |pw| point
277 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800278 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
279 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500280 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400281 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700282 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400283 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700284 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400285 minijail_change_uid(j, ppw->pw_uid);
286 j->user = strdup(user);
287 if (!j->user)
288 return -ENOMEM;
289 j->usergid = ppw->pw_gid;
290 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400291}
292
Will Drewry6ac91122011-10-21 16:38:58 -0500293int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400294{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700295 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700296 struct group gr;
297 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400298 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
299 if (sz == -1)
300 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400301
Elly Jonesdd3e8512012-01-23 15:13:38 -0500302 /*
303 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400304 * the maximum needed size of the buffer, so we don't have to search.
305 */
306 buf = malloc(sz);
307 if (!buf)
308 return -ENOMEM;
309 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500310 /*
311 * We're safe to free the buffer here. The strings inside gr point
312 * inside buf, but we don't use any of them; this leaves the pointers
313 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
314 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400315 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700316 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400317 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700318 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400319 minijail_change_gid(j, pgr->gr_gid);
320 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
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800422void API minijail_skip_remount_private(struct minijail *j)
423{
424 j->flags.skip_remount_private = 1;
425}
426
Will Drewry6ac91122011-10-21 16:38:58 -0500427void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400428{
Elly Jonese58176c2012-01-23 11:46:17 -0500429 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700430 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400431 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800432 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400433}
434
Dylan Reidf7942472015-11-18 17:55:26 -0800435void API minijail_namespace_ipc(struct minijail *j)
436{
437 j->flags.ipc = 1;
438}
439
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400440void API minijail_namespace_net(struct minijail *j)
441{
442 j->flags.net = 1;
443}
444
Dylan Reid1102f5a2015-09-15 11:52:20 -0700445void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
446{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800447 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700448 if (ns_fd < 0) {
449 pdie("failed to open namespace '%s'", ns_path);
450 }
451 j->netns_fd = ns_fd;
452 j->flags.enter_net = 1;
453}
454
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700455void API minijail_namespace_cgroups(struct minijail *j)
456{
457 j->flags.ns_cgroups = 1;
458}
459
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700460void API minijail_close_open_fds(struct minijail *j)
461{
462 j->flags.close_open_fds = 1;
463}
464
Dylan Reid791f5772015-09-14 20:02:42 -0700465void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400466{
467 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700468 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400469}
470
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800471void API minijail_namespace_user(struct minijail *j)
472{
473 j->flags.userns = 1;
474}
475
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400476void API minijail_namespace_user_disable_setgroups(struct minijail *j)
477{
478 j->flags.disable_setgroups = 1;
479}
480
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800481int API minijail_uidmap(struct minijail *j, const char *uidmap)
482{
483 j->uidmap = strdup(uidmap);
484 if (!j->uidmap)
485 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800486 char *ch;
487 for (ch = j->uidmap; *ch; ch++) {
488 if (*ch == ',')
489 *ch = '\n';
490 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800491 return 0;
492}
493
494int API minijail_gidmap(struct minijail *j, const char *gidmap)
495{
496 j->gidmap = strdup(gidmap);
497 if (!j->gidmap)
498 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800499 char *ch;
500 for (ch = j->gidmap; *ch; ch++) {
501 if (*ch == ',')
502 *ch = '\n';
503 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800504 return 0;
505}
506
Will Drewry6ac91122011-10-21 16:38:58 -0500507void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400508{
Lutz Justen13807cb2017-01-03 17:11:55 +0100509 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400510}
511
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800512void API minijail_run_as_init(struct minijail *j)
513{
514 /*
515 * Since the jailed program will become 'init' in the new PID namespace,
516 * Minijail does not need to fork an 'init' process.
517 */
518 j->flags.do_init = 0;
519}
520
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700521int API minijail_enter_chroot(struct minijail *j, const char *dir)
522{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400523 if (j->chrootdir)
524 return -EINVAL;
525 j->chrootdir = strdup(dir);
526 if (!j->chrootdir)
527 return -ENOMEM;
528 j->flags.chroot = 1;
529 return 0;
530}
531
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800532int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
533{
534 if (j->chrootdir)
535 return -EINVAL;
536 j->chrootdir = strdup(dir);
537 if (!j->chrootdir)
538 return -ENOMEM;
539 j->flags.pivot_root = 1;
540 return 0;
541}
542
Dylan Reida14e08d2015-10-22 21:05:29 -0700543char API *minijail_get_original_path(struct minijail *j,
544 const char *path_inside_chroot)
545{
Dylan Reid648b2202015-10-23 00:50:00 -0700546 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700547
Dylan Reid648b2202015-10-23 00:50:00 -0700548 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700549 while (b) {
550 /*
551 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700552 * mount, then the original path is exactly the source of
553 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700554 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700555 * mount source = /some/path/exe, mount dest =
556 * /chroot/path/exe Then when getting the original path of
557 * "/chroot/path/exe", the source of that mount,
558 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700559 */
560 if (!strcmp(b->dest, path_inside_chroot))
561 return strdup(b->src);
562
563 /*
564 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700565 * mount, take the suffix of the chroot path relative to the
566 * mount destination path, and append it to the mount source
567 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700568 */
569 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
570 const char *relative_path =
571 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400572 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700573 }
574 b = b->next;
575 }
576
577 /* If there is a chroot path, append |path_inside_chroot| to that. */
578 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400579 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700580
581 /* No chroot, so the path outside is the same as it is inside. */
582 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700583}
584
Martin Pelikánab9eb442017-01-25 11:53:58 +1100585size_t minijail_get_tmpfs_size(const struct minijail *j)
586{
587 return j->tmpfs_size;
588}
589
Lee Campbell11af0622014-05-22 12:36:04 -0700590void API minijail_mount_tmp(struct minijail *j)
591{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100592 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
593}
594
595void API minijail_mount_tmp_size(struct minijail *j, size_t size)
596{
597 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700598 j->flags.mount_tmp = 1;
599}
600
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800601int API minijail_write_pid_file(struct minijail *j, const char *path)
602{
603 j->pid_file_path = strdup(path);
604 if (!j->pid_file_path)
605 return -ENOMEM;
606 j->flags.pid_file = 1;
607 return 0;
608}
609
Dylan Reid605ce7f2016-01-19 19:21:00 -0800610int API minijail_add_to_cgroup(struct minijail *j, const char *path)
611{
612 if (j->cgroup_count >= MAX_CGROUPS)
613 return -ENOMEM;
614 j->cgroups[j->cgroup_count] = strdup(path);
615 if (!j->cgroups[j->cgroup_count])
616 return -ENOMEM;
617 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800618 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800619 return 0;
620}
621
Dylan Reid81e23972016-05-18 14:06:35 -0700622int API minijail_mount_with_data(struct minijail *j, const char *src,
623 const char *dest, const char *type,
624 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700625{
Dylan Reid648b2202015-10-23 00:50:00 -0700626 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400627
628 if (*dest != '/')
629 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700630 m = calloc(1, sizeof(*m));
631 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400632 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700633 m->dest = strdup(dest);
634 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400635 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700636 m->src = strdup(src);
637 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400638 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700639 m->type = strdup(type);
640 if (!m->type)
641 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700642 if (data) {
643 m->data = strdup(data);
644 if (!m->data)
645 goto error;
646 m->has_data = 1;
647 }
Dylan Reid648b2202015-10-23 00:50:00 -0700648 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400649
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800650 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400651
Elly Jonesdd3e8512012-01-23 15:13:38 -0500652 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700653 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400654 * containing vfs namespace.
655 */
656 minijail_namespace_vfs(j);
657
Dylan Reid648b2202015-10-23 00:50:00 -0700658 if (j->mounts_tail)
659 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400660 else
Dylan Reid648b2202015-10-23 00:50:00 -0700661 j->mounts_head = m;
662 j->mounts_tail = m;
663 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400664
665 return 0;
666
667error:
Dylan Reid81e23972016-05-18 14:06:35 -0700668 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700669 free(m->src);
670 free(m->dest);
671 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400672 return -ENOMEM;
673}
674
Dylan Reid81e23972016-05-18 14:06:35 -0700675int API minijail_mount(struct minijail *j, const char *src, const char *dest,
676 const char *type, unsigned long flags)
677{
678 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
679}
680
Dylan Reid648b2202015-10-23 00:50:00 -0700681int API minijail_bind(struct minijail *j, const char *src, const char *dest,
682 int writeable)
683{
684 unsigned long flags = MS_BIND;
685
686 if (!writeable)
687 flags |= MS_RDONLY;
688
689 return minijail_mount(j, src, dest, "", flags);
690}
691
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400692static void clear_seccomp_options(struct minijail *j)
693{
694 j->flags.seccomp_filter = 0;
695 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400696 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400697 j->filter_len = 0;
698 j->filter_prog = NULL;
699 j->flags.no_new_privs = 0;
700}
701
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400702static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400703{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400704 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400705 /*
706 * |errno| will be set to EINVAL when seccomp has not been
707 * compiled into the kernel. On certain platforms and kernel
708 * versions this is not a fatal failure. In that case, and only
709 * in that case, disable seccomp and skip loading the filters.
710 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400711 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400712 warn("not loading seccomp filters, seccomp filter not "
713 "supported");
714 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400715 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700716 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400717 /*
718 * If |errno| != EINVAL or seccomp_can_softfail() is false,
719 * we can proceed. Worst case scenario minijail_enter() will
720 * abort() if seccomp fails.
721 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700722 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400723 if (j->flags.seccomp_filter_tsync) {
724 /* Are the seccomp(2) syscall and the TSYNC option supported? */
725 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
726 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
727 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400728 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
729 warn("seccomp(2) syscall not supported");
730 clear_seccomp_options(j);
731 return 0;
732 } else if (saved_errno == EINVAL &&
733 seccomp_can_softfail()) {
734 warn(
735 "seccomp filter thread sync not supported");
736 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400737 return 0;
738 }
739 /*
740 * Similar logic here. If seccomp_can_softfail() is
741 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
742 * we can proceed. Worst case scenario minijail_enter()
743 * will abort() if seccomp or TSYNC fail.
744 */
745 }
746 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400747 return 1;
748}
749
750static int parse_seccomp_filters(struct minijail *j, FILE *policy_file)
751{
752 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400753 int use_ret_trap =
754 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
755 int allow_logging = j->flags.seccomp_filter_logging;
756
757 if (compile_filter(policy_file, fprog, use_ret_trap, allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400758 free(fprog);
759 return -1;
760 }
761
762 j->filter_len = fprog->len;
763 j->filter_prog = fprog;
764 return 0;
765}
766
767void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
768{
769 if (!seccomp_should_parse_filters(j))
770 return;
771
Elly Jonese1749eb2011-10-07 13:54:59 -0400772 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800773 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700774 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400775 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800776
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400777 if (parse_seccomp_filters(j, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700778 die("failed to compile seccomp filter BPF program in '%s'",
779 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800780 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400781 fclose(file);
782}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800783
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400784void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
785{
786 if (!seccomp_should_parse_filters(j))
787 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800788
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400789 FILE *file = fdopen(fd, "r");
790 if (!file) {
791 pdie("failed to associate stream with fd %d", fd);
792 }
793
794 if (parse_seccomp_filters(j, file) != 0) {
795 die("failed to compile seccomp filter BPF program from fd %d",
796 fd);
797 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400798 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500799}
800
Andrew Brestickereac28942015-11-11 16:04:46 -0800801int API minijail_use_alt_syscall(struct minijail *j, const char *table)
802{
803 j->alt_syscall_table = strdup(table);
804 if (!j->alt_syscall_table)
805 return -ENOMEM;
806 j->flags.alt_syscall = 1;
807 return 0;
808}
809
Will Drewryf89aef52011-09-16 16:48:57 -0500810struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400811 size_t available;
812 size_t total;
813 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500814};
815
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800816void marshal_state_init(struct marshal_state *state, char *buf,
817 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400818{
819 state->available = available;
820 state->buf = buf;
821 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500822}
823
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800824void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400825{
826 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500827
Elly Jonese1749eb2011-10-07 13:54:59 -0400828 /* Up to |available| will be written. */
829 if (copy_len) {
830 memcpy(state->buf, src, copy_len);
831 state->buf += copy_len;
832 state->available -= copy_len;
833 }
834 /* |total| will contain the expected length. */
835 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500836}
837
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400838void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700839{
840 marshal_append(state, m->src, strlen(m->src) + 1);
841 marshal_append(state, m->dest, strlen(m->dest) + 1);
842 marshal_append(state, m->type, strlen(m->type) + 1);
843 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
844 if (m->has_data)
845 marshal_append(state, m->data, strlen(m->data) + 1);
846 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
847}
848
Will Drewry6ac91122011-10-21 16:38:58 -0500849void minijail_marshal_helper(struct marshal_state *state,
850 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400851{
Dylan Reid648b2202015-10-23 00:50:00 -0700852 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800853 size_t i;
854
Elly Jonese1749eb2011-10-07 13:54:59 -0400855 marshal_append(state, (char *)j, sizeof(*j));
856 if (j->user)
857 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800858 if (j->suppl_gid_list) {
859 marshal_append(state, j->suppl_gid_list,
860 j->suppl_gid_count * sizeof(gid_t));
861 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400862 if (j->chrootdir)
863 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800864 if (j->alt_syscall_table) {
865 marshal_append(state, j->alt_syscall_table,
866 strlen(j->alt_syscall_table) + 1);
867 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800868 if (j->flags.seccomp_filter && j->filter_prog) {
869 struct sock_fprog *fp = j->filter_prog;
870 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800871 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400872 }
Dylan Reid648b2202015-10-23 00:50:00 -0700873 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400874 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400875 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800876 for (i = 0; i < j->cgroup_count; ++i)
877 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500878}
879
Will Drewry6ac91122011-10-21 16:38:58 -0500880size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400881{
882 struct marshal_state state;
883 marshal_state_init(&state, NULL, 0);
884 minijail_marshal_helper(&state, j);
885 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500886}
887
Elly Jonese1749eb2011-10-07 13:54:59 -0400888int minijail_marshal(const struct minijail *j, char *buf, size_t available)
889{
890 struct marshal_state state;
891 marshal_state_init(&state, buf, available);
892 minijail_marshal_helper(&state, j);
893 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500894}
895
Elly Jonese1749eb2011-10-07 13:54:59 -0400896int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
897{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800898 size_t i;
899 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500900 int ret = -EINVAL;
901
Elly Jonese1749eb2011-10-07 13:54:59 -0400902 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500903 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400904 memcpy((void *)j, serialized, sizeof(*j));
905 serialized += sizeof(*j);
906 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500907
Will Drewrybee7ba72011-10-21 20:47:01 -0500908 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -0400909 j->pid_file_path = NULL;
910 j->uidmap = NULL;
911 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -0700912 j->mounts_head = NULL;
913 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800914 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500915
Elly Jonese1749eb2011-10-07 13:54:59 -0400916 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400917 char *user = consumestr(&serialized, &length);
918 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500919 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400920 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500921 if (!j->user)
922 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400923 }
Will Drewryf89aef52011-09-16 16:48:57 -0500924
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800925 if (j->suppl_gid_list) { /* stale pointer */
926 if (j->suppl_gid_count > NGROUPS_MAX) {
927 goto bad_gid_list;
928 }
929 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
930 void *gid_list_bytes =
931 consumebytes(gid_list_size, &serialized, &length);
932 if (!gid_list_bytes)
933 goto bad_gid_list;
934
935 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
936 if (!j->suppl_gid_list)
937 goto bad_gid_list;
938
939 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
940 }
941
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400942 if (j->chrootdir) { /* stale pointer */
943 char *chrootdir = consumestr(&serialized, &length);
944 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500945 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400946 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500947 if (!j->chrootdir)
948 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400949 }
950
Andrew Brestickereac28942015-11-11 16:04:46 -0800951 if (j->alt_syscall_table) { /* stale pointer */
952 char *alt_syscall_table = consumestr(&serialized, &length);
953 if (!alt_syscall_table)
954 goto bad_syscall_table;
955 j->alt_syscall_table = strdup(alt_syscall_table);
956 if (!j->alt_syscall_table)
957 goto bad_syscall_table;
958 }
959
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800960 if (j->flags.seccomp_filter && j->filter_len > 0) {
961 size_t ninstrs = j->filter_len;
962 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
963 ninstrs > USHRT_MAX)
964 goto bad_filters;
965
966 size_t program_len = ninstrs * sizeof(struct sock_filter);
967 void *program = consumebytes(program_len, &serialized, &length);
968 if (!program)
969 goto bad_filters;
970
971 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800972 if (!j->filter_prog)
973 goto bad_filters;
974
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800975 j->filter_prog->len = ninstrs;
976 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800977 if (!j->filter_prog->filter)
978 goto bad_filter_prog_instrs;
979
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800980 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400981 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400982
Dylan Reid648b2202015-10-23 00:50:00 -0700983 count = j->mounts_count;
984 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400985 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700986 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -0700987 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400988 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700989 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700990 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400991 const char *src = consumestr(&serialized, &length);
992 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700993 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400994 dest = consumestr(&serialized, &length);
995 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700996 goto bad_mounts;
997 type = consumestr(&serialized, &length);
998 if (!type)
999 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001000 has_data = consumebytes(sizeof(*has_data), &serialized,
1001 &length);
1002 if (!has_data)
1003 goto bad_mounts;
1004 if (*has_data) {
1005 data = consumestr(&serialized, &length);
1006 if (!data)
1007 goto bad_mounts;
1008 }
Dylan Reid648b2202015-10-23 00:50:00 -07001009 flags = consumebytes(sizeof(*flags), &serialized, &length);
1010 if (!flags)
1011 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001012 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001013 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001014 }
1015
Dylan Reid605ce7f2016-01-19 19:21:00 -08001016 count = j->cgroup_count;
1017 j->cgroup_count = 0;
1018 for (i = 0; i < count; ++i) {
1019 char *cgroup = consumestr(&serialized, &length);
1020 if (!cgroup)
1021 goto bad_cgroups;
1022 j->cgroups[i] = strdup(cgroup);
1023 if (!j->cgroups[i])
1024 goto bad_cgroups;
1025 ++j->cgroup_count;
1026 }
1027
Elly Jonese1749eb2011-10-07 13:54:59 -04001028 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001029
Dylan Reid605ce7f2016-01-19 19:21:00 -08001030bad_cgroups:
1031 while (j->mounts_head) {
1032 struct mountpoint *m = j->mounts_head;
1033 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07001034 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001035 free(m->type);
1036 free(m->dest);
1037 free(m->src);
1038 free(m);
1039 }
1040 for (i = 0; i < j->cgroup_count; ++i)
1041 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001042bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001043 if (j->flags.seccomp_filter && j->filter_len > 0) {
1044 free(j->filter_prog->filter);
1045 free(j->filter_prog);
1046 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001047bad_filter_prog_instrs:
1048 if (j->filter_prog)
1049 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001050bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001051 if (j->alt_syscall_table)
1052 free(j->alt_syscall_table);
1053bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001054 if (j->chrootdir)
1055 free(j->chrootdir);
1056bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001057 if (j->suppl_gid_list)
1058 free(j->suppl_gid_list);
1059bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001060 if (j->user)
1061 free(j->user);
1062clear_pointers:
1063 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001064 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001065 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001066 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001067 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001068out:
1069 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001070}
1071
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001072/*
1073 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001074 * @j Minijail these mounts are for
1075 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001076 *
1077 * Returns 0 for success.
1078 */
Dylan Reid648b2202015-10-23 00:50:00 -07001079static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001080{
Dylan Reid648b2202015-10-23 00:50:00 -07001081 int ret;
1082 char *dest;
1083 int remount_ro = 0;
1084
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001085 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001086 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001087 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001088
Dylan Reideec77962016-06-30 19:35:10 -07001089 if (setup_mount_destination(m->src, dest, j->uid, j->gid))
1090 pdie("creating mount target '%s' failed", dest);
1091
Dylan Reid648b2202015-10-23 00:50:00 -07001092 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001093 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1094 * can't both be specified in the original bind mount.
1095 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001096 */
1097 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1098 remount_ro = 1;
1099 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001100 }
Dylan Reid648b2202015-10-23 00:50:00 -07001101
Dylan Reid81e23972016-05-18 14:06:35 -07001102 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001103 if (ret)
1104 pdie("mount: %s -> %s", m->src, dest);
1105
1106 if (remount_ro) {
1107 m->flags |= MS_RDONLY;
1108 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001109 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001110 if (ret)
1111 pdie("bind ro: %s -> %s", m->src, dest);
1112 }
1113
Elly Jones51a5b6c2011-10-12 19:09:26 -04001114 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001115 if (m->next)
1116 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001117 return ret;
1118}
1119
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001120static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001121{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001122 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001123
1124 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001125 return ret;
1126
1127 if (chroot(j->chrootdir))
1128 return -errno;
1129
1130 if (chdir("/"))
1131 return -errno;
1132
1133 return 0;
1134}
1135
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001136static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001137{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001138 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001139
1140 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001141 return ret;
1142
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001143 /*
1144 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001145 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001146 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001147 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001148 if (oldroot < 0)
1149 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001150 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001151 if (newroot < 0)
1152 pdie("failed to open %s for fchdir", j->chrootdir);
1153
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001154 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001155 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001156 * do a self bind mount.
1157 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001158 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1159 pdie("failed to bind mount '%s'", j->chrootdir);
1160 if (chdir(j->chrootdir))
1161 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001162 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001163 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001164
1165 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001166 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001167 * change to the old root and unmount it.
1168 */
1169 if (fchdir(oldroot))
1170 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001171
1172 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001173 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1174 * there could be a shared mount point under |oldroot|. In that case,
1175 * mounts under this shared mount point will be unmounted below, and
1176 * this unmounting will propagate to the original mount namespace
1177 * (because the mount point is shared). To prevent this unexpected
1178 * unmounting, remove these mounts from their peer groups by recursively
1179 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001180 */
1181 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001182 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001183 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001184 if (umount2(".", MNT_DETACH))
1185 pdie("umount(/)");
1186 /* Change back to the new root. */
1187 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001188 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001189 if (close(oldroot))
1190 return -errno;
1191 if (close(newroot))
1192 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001193 if (chroot("/"))
1194 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001195 /* Set correct CWD for getcwd(3). */
1196 if (chdir("/"))
1197 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001198
1199 return 0;
1200}
1201
Martin Pelikánab9eb442017-01-25 11:53:58 +11001202static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001203{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001204 const char fmt[] = "size=%zu,mode=1777";
1205 /* Count for the user storing ULLONG_MAX literally + extra space. */
1206 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1207 int ret;
1208
1209 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1210
1211 if (ret <= 0)
1212 pdie("tmpfs size spec error");
1213 else if ((size_t)ret >= sizeof(data))
1214 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001215 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001216 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001217}
1218
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001219static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001220{
1221 const char *kProcPath = "/proc";
1222 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001223 /*
1224 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001225 * /proc in our namespace, which means using MS_REMOUNT here would
1226 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001227 * namespace (!). Instead, remove their mount from our namespace lazily
1228 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001229 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001230 if (umount2(kProcPath, MNT_DETACH)) {
1231 /*
1232 * If we are in a new user namespace, umount(2) will fail.
1233 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1234 */
1235 if (j->flags.userns) {
1236 info("umount(/proc, MNT_DETACH) failed, "
1237 "this is expected when using user namespaces");
1238 } else {
1239 return -errno;
1240 }
1241 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001242 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001243 return -errno;
1244 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001245}
1246
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001247static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001248{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001249 kill(j->initpid, SIGKILL);
1250 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001251}
1252
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001253static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001254{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001255 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001256 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001257}
1258
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001259static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001260{
1261 size_t i;
1262
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001263 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001264 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001265 kill_child_and_die(j, "failed to add to cgroups");
1266 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001267}
1268
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001269static void write_ugid_maps_or_die(const struct minijail *j)
1270{
1271 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1272 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001273 if (j->gidmap && j->flags.disable_setgroups) {
1274 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1275 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001276 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001277 if (ret == -ENOENT) {
1278 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1279 warn("could not disable setgroups(2)");
1280 } else
1281 kill_child_and_die(j, "failed to disable setgroups(2)");
1282 }
1283 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001284 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1285 kill_child_and_die(j, "failed to write gid_map");
1286}
1287
1288static void enter_user_namespace(const struct minijail *j)
1289{
1290 if (j->uidmap && setresuid(0, 0, 0))
1291 pdie("user_namespaces: setresuid(0, 0, 0) failed");
1292 if (j->gidmap && setresgid(0, 0, 0))
1293 pdie("user_namespaces: setresgid(0, 0, 0) failed");
1294}
1295
1296static void parent_setup_complete(int *pipe_fds)
1297{
1298 close(pipe_fds[0]);
1299 close(pipe_fds[1]);
1300}
1301
1302/*
1303 * wait_for_parent_setup: Called by the child process to wait for any
1304 * further parent-side setup to complete before continuing.
1305 */
1306static void wait_for_parent_setup(int *pipe_fds)
1307{
1308 char buf;
1309
1310 close(pipe_fds[1]);
1311
1312 /* Wait for parent to complete setup and close the pipe. */
1313 if (read(pipe_fds[0], &buf, 1) != 0)
1314 die("failed to sync with parent");
1315 close(pipe_fds[0]);
1316}
1317
1318static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001319{
Lutz Justen13807cb2017-01-03 17:11:55 +01001320 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1321 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001322 die("can only do one of inherit, keep, or set supplementary "
1323 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001324 }
1325
Lutz Justen13807cb2017-01-03 17:11:55 +01001326 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001327 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001328 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001329 } else if (j->flags.set_suppl_gids) {
1330 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001331 pdie("setgroups(suppl_gids) failed");
Lutz Justen13807cb2017-01-03 17:11:55 +01001332 } else if (!j->flags.keep_suppl_gids) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001333 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001334 * Only attempt to clear supplementary groups if we are changing
Lutz Justen13807cb2017-01-03 17:11:55 +01001335 * users or groups.
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001336 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001337 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001338 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001339 }
1340
1341 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001342 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001343
1344 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001345 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001346}
1347
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001348static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1349{
1350 const uint64_t one = 1;
1351 unsigned int i;
1352 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1353 if (keep_mask & (one << i))
1354 continue;
1355 if (prctl(PR_CAPBSET_DROP, i))
1356 pdie("could not drop capability from bounding set");
1357 }
1358}
1359
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001360static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001361{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001362 if (!j->flags.use_caps)
1363 return;
1364
Elly Jonese1749eb2011-10-07 13:54:59 -04001365 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001366 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001367 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001368 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001369 unsigned int i;
1370 if (!caps)
1371 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001372 if (cap_clear(caps))
1373 die("can't clear caps");
1374
1375 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001376 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001377 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001378 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001379 flag[0] = i;
1380 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001381 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001382 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001383 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001384 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001385 die("can't add inheritable cap");
1386 }
1387 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001388 die("can't apply initial cleaned capset");
1389
1390 /*
1391 * Instead of dropping bounding set first, do it here in case
1392 * the caller had a more permissive bounding set which could
1393 * have been used above to raise a capability that wasn't already
1394 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1395 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001396 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001397
1398 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001399 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001400 flag[0] = CAP_SETPCAP;
1401 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1402 die("can't clear effective cap");
1403 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1404 die("can't clear permitted cap");
1405 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1406 die("can't clear inheritable cap");
1407 }
1408
1409 if (cap_set_proc(caps))
1410 die("can't apply final cleaned capset");
1411
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001412 /*
1413 * If ambient capabilities are supported, clear all capabilities first,
1414 * then raise the requested ones.
1415 */
1416 if (j->flags.set_ambient_caps) {
1417 if (!cap_ambient_supported()) {
1418 pdie("ambient capabilities not supported");
1419 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001420 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1421 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001422 pdie("can't clear ambient capabilities");
1423 }
1424
1425 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1426 if (!(j->caps & (one << i)))
1427 continue;
1428
1429 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1430 0) != 0) {
1431 pdie("prctl(PR_CAP_AMBIENT, "
1432 "PR_CAP_AMBIENT_RAISE, %u) failed",
1433 i);
1434 }
1435 }
1436 }
1437
Kees Cook323878a2013-02-05 15:35:24 -08001438 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001439}
1440
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001441static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001442{
1443 /*
1444 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1445 * in the kernel source tree for an explanation of the parameters.
1446 */
1447 if (j->flags.no_new_privs) {
1448 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1449 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1450 }
1451
1452 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001453 * Code running with ASan
1454 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1455 * will make system calls not included in the syscall filter policy,
1456 * which will likely crash the program. Skip setting seccomp filter in
1457 * that case.
1458 * 'running_with_asan()' has no inputs and is completely defined at
1459 * build time, so this cannot be used by an attacker to skip setting
1460 * seccomp filter.
1461 */
1462 if (j->flags.seccomp_filter && running_with_asan()) {
1463 warn("running with ASan, not setting seccomp filter");
1464 return;
1465 }
1466
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001467 if (j->flags.seccomp_filter) {
1468 if (j->flags.seccomp_filter_logging) {
1469 /*
1470 * If logging seccomp filter failures,
1471 * install the SIGSYS handler first.
1472 */
1473 if (install_sigsys_handler())
1474 pdie("failed to install SIGSYS handler");
1475 warn("logging seccomp filter failures");
1476 } else if (j->flags.seccomp_filter_tsync) {
1477 /*
1478 * If setting thread sync,
1479 * reset the SIGSYS signal handler so that
1480 * the entire thread group is killed.
1481 */
1482 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1483 pdie("failed to reset SIGSYS disposition");
1484 info("reset SIGSYS disposition");
1485 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001486 }
1487
1488 /*
1489 * Install the syscall filter.
1490 */
1491 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001492 if (j->flags.seccomp_filter_tsync) {
1493 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1494 SECCOMP_FILTER_FLAG_TSYNC,
1495 j->filter_prog)) {
1496 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001497 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001498 } else {
1499 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1500 j->filter_prog)) {
1501 pdie("prctl(seccomp_filter) failed");
1502 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001503 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001504 }
1505}
1506
Will Drewry6ac91122011-10-21 16:38:58 -05001507void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001508{
Dylan Reidf682d472015-09-17 21:39:07 -07001509 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001510 * If we're dropping caps, get the last valid cap from /proc now,
1511 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001512 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001513 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001514 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001515 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001516
Elly Jonese1749eb2011-10-07 13:54:59 -04001517 if (j->flags.pids)
1518 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001519 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001520
Lutz Justen13807cb2017-01-03 17:11:55 +01001521 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001522 die("cannot inherit supplementary groups without setting a "
1523 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001524
Elly Jonesdd3e8512012-01-23 15:13:38 -05001525 /*
1526 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001527 * so we don't even try. If any of our operations fail, we abort() the
1528 * entire process.
1529 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001530 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001531 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001532
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001533 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001534 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001535 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001536 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001537 * Unless asked not to, remount all filesystems as private.
1538 * If they are shared, new bind mounts will creep out of our
1539 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001540 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1541 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001542 if (!j->flags.skip_remount_private) {
1543 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001544 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1545 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001546 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001547 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001548
Dylan Reidf7942472015-11-18 17:55:26 -08001549 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001550 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001551 }
1552
Dylan Reid1102f5a2015-09-15 11:52:20 -07001553 if (j->flags.enter_net) {
1554 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001555 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001556 } else if (j->flags.net) {
1557 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001558 pdie("unshare(CLONE_NEWNET) failed");
1559 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001560 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001561
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001562 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001563 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001564
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08001565 if (j->flags.new_session_keyring) {
1566 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
1567 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
1568 }
1569
Elly Jones51a5b6c2011-10-12 19:09:26 -04001570 if (j->flags.chroot && enter_chroot(j))
1571 pdie("chroot");
1572
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001573 if (j->flags.pivot_root && enter_pivot_root(j))
1574 pdie("pivot_root");
1575
Martin Pelikánab9eb442017-01-25 11:53:58 +11001576 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07001577 pdie("mount_tmp");
1578
Dylan Reid791f5772015-09-14 20:02:42 -07001579 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001580 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001581
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001582 /*
1583 * If we're only dropping capabilities from the bounding set, but not
1584 * from the thread's (permitted|inheritable|effective) sets, do it now.
1585 */
1586 if (j->flags.capbset_drop) {
1587 drop_capbset(j->cap_bset, last_valid_cap);
1588 }
1589
1590 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001591 /*
1592 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001593 * capability to change uids, our attempt to use setuid()
1594 * below will fail. Hang on to root caps across setuid(), then
1595 * lock securebits.
1596 */
1597 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001598 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001599
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001600 if (lock_securebits() < 0) {
1601 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001602 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001603 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001604
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001605 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001606 /*
1607 * If we're setting no_new_privs, we can drop privileges
1608 * before setting seccomp filter. This way filter policies
1609 * don't need to allow privilege-dropping syscalls.
1610 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001611 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001612 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001613 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001614 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001615 /*
1616 * If we're not setting no_new_privs,
1617 * we need to set seccomp filter *before* dropping privileges.
1618 * WARNING: this means that filter policies *must* allow
1619 * setgroups()/setresgid()/setresuid() for dropping root and
1620 * capget()/capset()/prctl() for dropping caps.
1621 */
1622 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001623 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001624 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001625 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001626
Elly Jonesdd3e8512012-01-23 15:13:38 -05001627 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001628 * Select the specified alternate syscall table. The table must not
1629 * block prctl(2) if we're using seccomp as well.
1630 */
1631 if (j->flags.alt_syscall) {
1632 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001633 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08001634 }
1635
1636 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001637 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001638 * privilege-dropping syscalls :)
1639 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001640 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001641 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001642 warn("seccomp not supported");
1643 return;
1644 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001645 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001646 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001647}
1648
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001649/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001650static int init_exitstatus = 0;
1651
Will Drewry6ac91122011-10-21 16:38:58 -05001652void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001653{
1654 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001655}
1656
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001657void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001658{
1659 pid_t pid;
1660 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001661 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001662 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001663 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001664 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001665 /*
1666 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001667 * left inside our pid namespace or we get a signal.
1668 */
1669 if (pid == rootpid)
1670 init_exitstatus = status;
1671 }
1672 if (!WIFEXITED(init_exitstatus))
1673 _exit(MINIJAIL_ERR_INIT);
1674 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001675}
1676
Will Drewry6ac91122011-10-21 16:38:58 -05001677int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001678{
1679 size_t sz = 0;
1680 size_t bytes = read(fd, &sz, sizeof(sz));
1681 char *buf;
1682 int r;
1683 if (sizeof(sz) != bytes)
1684 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001685 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001686 return -E2BIG;
1687 buf = malloc(sz);
1688 if (!buf)
1689 return -ENOMEM;
1690 bytes = read(fd, buf, sz);
1691 if (bytes != sz) {
1692 free(buf);
1693 return -EINVAL;
1694 }
1695 r = minijail_unmarshal(j, buf, sz);
1696 free(buf);
1697 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001698}
1699
Will Drewry6ac91122011-10-21 16:38:58 -05001700int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001701{
1702 char *buf;
1703 size_t sz = minijail_size(j);
1704 ssize_t written;
1705 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001706
Elly Jonese1749eb2011-10-07 13:54:59 -04001707 if (!sz)
1708 return -EINVAL;
1709 buf = malloc(sz);
1710 r = minijail_marshal(j, buf, sz);
1711 if (r) {
1712 free(buf);
1713 return r;
1714 }
1715 /* Sends [size][minijail]. */
1716 written = write(fd, &sz, sizeof(sz));
1717 if (written != sizeof(sz)) {
1718 free(buf);
1719 return -EFAULT;
1720 }
1721 written = write(fd, buf, sz);
1722 if (written < 0 || (size_t) written != sz) {
1723 free(buf);
1724 return -EFAULT;
1725 }
1726 free(buf);
1727 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001728}
Elly Jonescd7a9042011-07-22 13:56:51 -04001729
Will Drewry6ac91122011-10-21 16:38:58 -05001730int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001731{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001732#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001733 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001734 return 0;
1735#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001736 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1737 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1738 if (!newenv)
1739 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001740
Elly Jonese1749eb2011-10-07 13:54:59 -04001741 /* Only insert a separating space if we have something to separate... */
1742 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1743 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001744
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001745 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001746 setenv(kLdPreloadEnvVar, newenv, 1);
1747 free(newenv);
1748 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001749#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001750}
1751
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001752static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001753{
1754 int r = pipe(fds);
1755 char fd_buf[11];
1756 if (r)
1757 return r;
1758 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1759 if (r <= 0)
1760 return -EINVAL;
1761 setenv(kFdEnvVar, fd_buf, 1);
1762 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001763}
1764
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001765static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07001766{
1767 const char *kFdPath = "/proc/self/fd";
1768
1769 DIR *d = opendir(kFdPath);
1770 struct dirent *dir_entry;
1771
1772 if (d == NULL)
1773 return -1;
1774 int dir_fd = dirfd(d);
1775 while ((dir_entry = readdir(d)) != NULL) {
1776 size_t i;
1777 char *end;
1778 bool should_close = true;
1779 const int fd = strtol(dir_entry->d_name, &end, 10);
1780
1781 if ((*end) != '\0') {
1782 continue;
1783 }
1784 /*
1785 * We might have set up some pipes that we want to share with
1786 * the parent process, and should not be closed.
1787 */
1788 for (i = 0; i < size; ++i) {
1789 if (fd == inheritable_fds[i]) {
1790 should_close = false;
1791 break;
1792 }
1793 }
1794 /* Also avoid closing the directory fd. */
1795 if (should_close && fd != dir_fd)
1796 close(fd);
1797 }
1798 closedir(d);
1799 return 0;
1800}
1801
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001802int minijail_run_internal(struct minijail *j, const char *filename,
1803 char *const argv[], pid_t *pchild_pid,
1804 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1805 int use_preload);
1806
Will Drewry6ac91122011-10-21 16:38:58 -05001807int API minijail_run(struct minijail *j, const char *filename,
1808 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001809{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001810 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1811 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001812}
1813
1814int API minijail_run_pid(struct minijail *j, const char *filename,
1815 char *const argv[], pid_t *pchild_pid)
1816{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001817 return minijail_run_internal(j, filename, argv, pchild_pid,
1818 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001819}
1820
1821int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001822 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001823{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001824 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1825 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001826}
1827
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001828int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001829 char *const argv[], pid_t *pchild_pid,
1830 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001831{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001832 return minijail_run_internal(j, filename, argv, pchild_pid,
1833 pstdin_fd, pstdout_fd, pstderr_fd, true);
1834}
1835
1836int API minijail_run_no_preload(struct minijail *j, const char *filename,
1837 char *const argv[])
1838{
1839 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1840 false);
1841}
1842
Samuel Tan63187f42015-10-16 13:01:53 -07001843int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001844 const char *filename,
1845 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001846 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001847 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001848 int *pstderr_fd)
1849{
Samuel Tan63187f42015-10-16 13:01:53 -07001850 return minijail_run_internal(j, filename, argv, pchild_pid,
1851 pstdin_fd, pstdout_fd, pstderr_fd, false);
1852}
1853
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001854int minijail_run_internal(struct minijail *j, const char *filename,
1855 char *const argv[], pid_t *pchild_pid,
1856 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1857 int use_preload)
1858{
Elly Jonese1749eb2011-10-07 13:54:59 -04001859 char *oldenv, *oldenv_copy = NULL;
1860 pid_t child_pid;
1861 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001862 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001863 int stdout_fds[2];
1864 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001865 int child_sync_pipe_fds[2];
1866 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001867 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001868 /* We need to remember this across the minijail_preexec() call. */
1869 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001870 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001871
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001872 if (use_preload) {
1873 oldenv = getenv(kLdPreloadEnvVar);
1874 if (oldenv) {
1875 oldenv_copy = strdup(oldenv);
1876 if (!oldenv_copy)
1877 return -ENOMEM;
1878 }
1879
1880 if (setup_preload())
1881 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001882 }
Will Drewryf89aef52011-09-16 16:48:57 -05001883
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001884 if (!use_preload) {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04001885 if (j->flags.use_caps && j->caps != 0)
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001886 die("non-empty capabilities are not supported without "
1887 "LD_PRELOAD");
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001888 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001889
Elly Jonesdd3e8512012-01-23 15:13:38 -05001890 /*
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05001891 * Make the process group ID of this process equal to its PID.
1892 * In the non-interactive case (e.g. when the parent process is started
1893 * from init) this ensures the parent process and the jailed process
1894 * can be killed together.
1895 * When the parent process is started from the console this ensures
1896 * the call to setsid(2) in the jailed process succeeds.
1897 *
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001898 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1899 * the process is already a process group leader.
1900 */
1901 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1902 if (errno != EPERM) {
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05001903 pdie("setpgid(0, 0) failed");
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001904 }
1905 }
1906
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001907 if (use_preload) {
1908 /*
1909 * Before we fork(2) and execve(2) the child process, we need
1910 * to open a pipe(2) to send the minijail configuration over.
1911 */
1912 if (setup_pipe(pipe_fds))
1913 return -EFAULT;
1914 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001915
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001916 /*
1917 * If we want to write to the child process' standard input,
1918 * create the pipe(2) now.
1919 */
1920 if (pstdin_fd) {
1921 if (pipe(stdin_fds))
1922 return -EFAULT;
1923 }
1924
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001925 /*
1926 * If we want to read from the child process' standard output,
1927 * create the pipe(2) now.
1928 */
1929 if (pstdout_fd) {
1930 if (pipe(stdout_fds))
1931 return -EFAULT;
1932 }
1933
1934 /*
1935 * If we want to read from the child process' standard error,
1936 * create the pipe(2) now.
1937 */
1938 if (pstderr_fd) {
1939 if (pipe(stderr_fds))
1940 return -EFAULT;
1941 }
1942
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001943 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04001944 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001945 * or if we need to add the child process to cgroups, create the pipe(2)
1946 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001947 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001948 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001949 sync_child = 1;
1950 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001951 return -EFAULT;
1952 }
1953
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001954 /*
1955 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001956 *
1957 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1958 *
1959 * In multithreaded programs, there are a bunch of locks inside libc,
1960 * some of which may be held by other threads at the time that we call
1961 * minijail_run_pid(). If we call fork(), glibc does its level best to
1962 * ensure that we hold all of these locks before it calls clone()
1963 * internally and drop them after clone() returns, but when we call
1964 * sys_clone(2) directly, all that gets bypassed and we end up with a
1965 * child address space where some of libc's important locks are held by
1966 * other threads (which did not get cloned, and hence will never release
1967 * those locks). This is okay so long as we call exec() immediately
1968 * after, but a bunch of seemingly-innocent libc functions like setenv()
1969 * take locks.
1970 *
1971 * Hence, only call sys_clone() if we need to, in order to get at pid
1972 * namespacing. If we follow this path, the child's address space might
1973 * have broken locks; you may only call functions that do not acquire
1974 * any locks.
1975 *
1976 * Unfortunately, fork() acquires every lock it can get its hands on, as
1977 * previously detailed, so this function is highly likely to deadlock
1978 * later on (see "deadlock here") if we're multithreaded.
1979 *
1980 * We might hack around this by having the clone()d child (init of the
1981 * pid namespace) return directly, rather than leaving the clone()d
1982 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001983 * its fork()ed child return in turn), but that process would be
1984 * crippled with its libc locks potentially broken. We might try
1985 * fork()ing in the parent before we clone() to ensure that we own all
1986 * the locks, but then we have to have the forked child hanging around
1987 * consuming resources (and possibly having file descriptors / shared
1988 * memory regions / etc attached). We'd need to keep the child around to
1989 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04001990 *
1991 * TODO(ellyjones): figure out if the "forked child hanging around"
1992 * problem is fixable or not. It would be nice if we worked in this
1993 * case.
1994 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001995 if (pid_namespace) {
1996 int clone_flags = CLONE_NEWPID | SIGCHLD;
1997 if (j->flags.userns)
1998 clone_flags |= CLONE_NEWUSER;
1999 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002000 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002001 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002002 }
Elly Jones761b7412012-06-13 15:49:52 -04002003
Elly Jonese1749eb2011-10-07 13:54:59 -04002004 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002005 if (use_preload) {
2006 free(oldenv_copy);
2007 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002008 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002009 }
Will Drewryf89aef52011-09-16 16:48:57 -05002010
Elly Jonese1749eb2011-10-07 13:54:59 -04002011 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002012 if (use_preload) {
2013 /* Restore parent's LD_PRELOAD. */
2014 if (oldenv_copy) {
2015 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2016 free(oldenv_copy);
2017 } else {
2018 unsetenv(kLdPreloadEnvVar);
2019 }
2020 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002021 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002022
Elly Jonese1749eb2011-10-07 13:54:59 -04002023 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002024
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002025 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002026 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002027
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002028 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002029 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002030
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002031 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002032 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002033
2034 if (sync_child)
2035 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002036
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002037 if (use_preload) {
2038 /* Send marshalled minijail. */
2039 close(pipe_fds[0]); /* read endpoint */
2040 ret = minijail_to_fd(j, pipe_fds[1]);
2041 close(pipe_fds[1]); /* write endpoint */
2042 if (ret) {
2043 kill(j->initpid, SIGKILL);
2044 die("failed to send marshalled minijail");
2045 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002046 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002047
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002048 if (pchild_pid)
2049 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002050
2051 /*
2052 * If we want to write to the child process' standard input,
2053 * set up the write end of the pipe.
2054 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002055 if (pstdin_fd)
2056 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002057 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002058
2059 /*
2060 * If we want to read from the child process' standard output,
2061 * set up the read end of the pipe.
2062 */
2063 if (pstdout_fd)
2064 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002065 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002066
2067 /*
2068 * If we want to read from the child process' standard error,
2069 * set up the read end of the pipe.
2070 */
2071 if (pstderr_fd)
2072 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002073 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002074
Elly Jonese1749eb2011-10-07 13:54:59 -04002075 return 0;
2076 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002077 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002078 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002079
Peter Qiu2860c462015-12-16 15:13:06 -08002080 if (j->flags.reset_signal_mask) {
2081 sigset_t signal_mask;
2082 if (sigemptyset(&signal_mask) != 0)
2083 pdie("sigemptyset failed");
2084 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2085 pdie("sigprocmask failed");
2086 }
2087
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002088 if (j->flags.close_open_fds) {
2089 const size_t kMaxInheritableFdsSize = 10;
2090 int inheritable_fds[kMaxInheritableFdsSize];
2091 size_t size = 0;
2092 if (use_preload) {
2093 inheritable_fds[size++] = pipe_fds[0];
2094 inheritable_fds[size++] = pipe_fds[1];
2095 }
2096 if (sync_child) {
2097 inheritable_fds[size++] = child_sync_pipe_fds[0];
2098 inheritable_fds[size++] = child_sync_pipe_fds[1];
2099 }
2100 if (pstdin_fd) {
2101 inheritable_fds[size++] = stdin_fds[0];
2102 inheritable_fds[size++] = stdin_fds[1];
2103 }
2104 if (pstdout_fd) {
2105 inheritable_fds[size++] = stdout_fds[0];
2106 inheritable_fds[size++] = stdout_fds[1];
2107 }
2108 if (pstderr_fd) {
2109 inheritable_fds[size++] = stderr_fds[0];
2110 inheritable_fds[size++] = stderr_fds[1];
2111 }
2112
2113 if (close_open_fds(inheritable_fds, size) < 0)
2114 die("failed to close open file descriptors");
2115 }
2116
Dylan Reidce5b55e2016-01-13 11:04:16 -08002117 if (sync_child)
2118 wait_for_parent_setup(child_sync_pipe_fds);
2119
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002120 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002121 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002122
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002123 /*
2124 * If we want to write to the jailed process' standard input,
2125 * set up the read end of the pipe.
2126 */
2127 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002128 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2129 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002130 die("failed to set up stdin pipe");
2131 }
2132
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002133 /*
2134 * If we want to read from the jailed process' standard output,
2135 * set up the write end of the pipe.
2136 */
2137 if (pstdout_fd) {
2138 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2139 STDOUT_FILENO) < 0)
2140 die("failed to set up stdout pipe");
2141 }
2142
2143 /*
2144 * If we want to read from the jailed process' standard error,
2145 * set up the write end of the pipe.
2146 */
2147 if (pstderr_fd) {
2148 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2149 STDERR_FILENO) < 0)
2150 die("failed to set up stderr pipe");
2151 }
2152
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002153 /*
2154 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2155 * This prevents the jailed process from using the TIOCSTI ioctl
2156 * to push characters into the parent process terminal's input buffer,
2157 * therefore escaping the jail.
2158 */
2159 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2160 isatty(STDERR_FILENO)) {
2161 if (setsid() < 0) {
2162 pdie("setsid() failed");
2163 }
2164 }
2165
Dylan Reid791f5772015-09-14 20:02:42 -07002166 /* If running an init program, let it decide when/how to mount /proc. */
2167 if (pid_namespace && !do_init)
2168 j->flags.remount_proc_ro = 0;
2169
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002170 if (use_preload) {
2171 /* Strip out flags that cannot be inherited across execve(2). */
2172 minijail_preexec(j);
2173 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002174 /*
2175 * If not using LD_PRELOAD, do all jailing before execve(2).
2176 * Note that PID namespaces can only be entered on fork(2),
2177 * so that flag is still cleared.
2178 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002179 j->flags.pids = 0;
2180 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002181 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002182 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002183
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002184 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002185 /*
2186 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002187 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002188 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002189 * a child to actually run the program. If |do_init == 0|, we
2190 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002191 *
2192 * If we're multithreaded, we'll probably deadlock here. See
2193 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002194 */
2195 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002196 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002197 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002198 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002199 /*
2200 * Best effort. Don't bother checking the return value.
2201 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002202 prctl(PR_SET_NAME, "minijail-init");
2203 init(child_pid); /* Never returns. */
2204 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002205 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002206
Elly Jonesdd3e8512012-01-23 15:13:38 -05002207 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002208 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002209 * calling process
2210 * -> execve()-ing process
2211 * If we are:
2212 * calling process
2213 * -> init()-ing process
2214 * -> execve()-ing process
2215 */
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002216 ret = execve(filename, argv, environ);
2217 if (ret == -1) {
2218 pwarn("execve(%s) failed", filename);
2219 }
2220 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002221}
2222
Will Drewry6ac91122011-10-21 16:38:58 -05002223int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002224{
2225 int st;
2226 if (kill(j->initpid, SIGTERM))
2227 return -errno;
2228 if (waitpid(j->initpid, &st, 0) < 0)
2229 return -errno;
2230 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002231}
2232
Will Drewry6ac91122011-10-21 16:38:58 -05002233int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002234{
2235 int st;
2236 if (waitpid(j->initpid, &st, 0) < 0)
2237 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002238
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002239 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002240 int error_status = st;
2241 if (WIFSIGNALED(st)) {
2242 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002243 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002244 j->initpid, signum);
2245 /*
2246 * We return MINIJAIL_ERR_JAIL if the process received
2247 * SIGSYS, which happens when a syscall is blocked by
2248 * seccomp filters.
2249 * If not, we do what bash(1) does:
2250 * $? = 128 + signum
2251 */
2252 if (signum == SIGSYS) {
2253 error_status = MINIJAIL_ERR_JAIL;
2254 } else {
2255 error_status = 128 + signum;
2256 }
2257 }
2258 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002259 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002260
2261 int exit_status = WEXITSTATUS(st);
2262 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002263 info("child process %d exited with status %d",
2264 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002265
2266 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002267}
2268
Will Drewry6ac91122011-10-21 16:38:58 -05002269void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002270{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002271 size_t i;
2272
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002273 if (j->flags.seccomp_filter && j->filter_prog) {
2274 free(j->filter_prog->filter);
2275 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002276 }
Dylan Reid648b2202015-10-23 00:50:00 -07002277 while (j->mounts_head) {
2278 struct mountpoint *m = j->mounts_head;
2279 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002280 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002281 free(m->type);
2282 free(m->dest);
2283 free(m->src);
2284 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002285 }
Dylan Reid648b2202015-10-23 00:50:00 -07002286 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002287 if (j->user)
2288 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002289 if (j->suppl_gid_list)
2290 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002291 if (j->chrootdir)
2292 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002293 if (j->pid_file_path)
2294 free(j->pid_file_path);
2295 if (j->uidmap)
2296 free(j->uidmap);
2297 if (j->gidmap)
2298 free(j->gidmap);
Andrew Brestickereac28942015-11-11 16:04:46 -08002299 if (j->alt_syscall_table)
2300 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002301 for (i = 0; i < j->cgroup_count; ++i)
2302 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002303 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002304}