blob: 4d61b0d5bd178cb5400fd7e07d1fd96a85895ba9 [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>
Dylan Reid0f72ef42017-06-06 15:42:49 -070028#include <sys/resource.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070029#include <sys/stat.h>
30#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080031#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040032#include <sys/wait.h>
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -070033#include <syscall.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040034#include <unistd.h>
35
36#include "libminijail.h"
37#include "libminijail-private.h"
38
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070039#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080040#include "syscall_filter.h"
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040041#include "syscall_wrapper.h"
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040042#include "system.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070043#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080044
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070045/* Until these are reliably available in linux/prctl.h. */
Andrew Brestickereac28942015-11-11 16:04:46 -080046#ifndef PR_ALT_SYSCALL
47# define PR_ALT_SYSCALL 0x43724f53
48#endif
49
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040050/* Seccomp filter related flags. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080051#ifndef PR_SET_NO_NEW_PRIVS
52# define PR_SET_NO_NEW_PRIVS 38
53#endif
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040054
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080055#ifndef SECCOMP_MODE_FILTER
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040056#define SECCOMP_MODE_FILTER 2 /* Uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050057#endif
58
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040059#ifndef SECCOMP_SET_MODE_STRICT
60# define SECCOMP_SET_MODE_STRICT 0
61#endif
62#ifndef SECCOMP_SET_MODE_FILTER
63# define SECCOMP_SET_MODE_FILTER 1
64#endif
65
66#ifndef SECCOMP_FILTER_FLAG_TSYNC
67# define SECCOMP_FILTER_FLAG_TSYNC 1
68#endif
69/* End seccomp filter related flags. */
70
Dylan Reid4cbc2a52016-06-17 19:06:07 -070071/* New cgroup namespace might not be in linux-headers yet. */
72#ifndef CLONE_NEWCGROUP
73# define CLONE_NEWCGROUP 0x02000000
74#endif
75
Dylan Reid605ce7f2016-01-19 19:21:00 -080076#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
77
Dylan Reid0f72ef42017-06-06 15:42:49 -070078#define MAX_RLIMITS 32 /* Currently there are 15 supported by Linux. */
79
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -080080/* Keyctl commands. */
81#define KEYCTL_JOIN_SESSION_KEYRING 1
82
Dylan Reid0f72ef42017-06-06 15:42:49 -070083struct minijail_rlimit {
84 int type;
85 uint32_t cur;
86 uint32_t max;
87};
88
Dylan Reid648b2202015-10-23 00:50:00 -070089struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040090 char *src;
91 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070092 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -070093 char *data;
94 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -070095 unsigned long flags;
96 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040097};
98
Will Drewryf89aef52011-09-16 16:48:57 -050099struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700100 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700101 * WARNING: if you add a flag here you need to make sure it's
102 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700103 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400104 struct {
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700105 int uid : 1;
106 int gid : 1;
Lutz Justen13807cb2017-01-03 17:11:55 +0100107 int inherit_suppl_gids : 1;
108 int set_suppl_gids : 1;
109 int keep_suppl_gids : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700110 int use_caps : 1;
111 int capbset_drop : 1;
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400112 int set_ambient_caps : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700113 int vfs : 1;
114 int enter_vfs : 1;
115 int skip_remount_private : 1;
116 int pids : 1;
117 int ipc : 1;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400118 int uts : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700119 int net : 1;
120 int enter_net : 1;
121 int ns_cgroups : 1;
122 int userns : 1;
123 int disable_setgroups : 1;
124 int seccomp : 1;
125 int remount_proc_ro : 1;
126 int no_new_privs : 1;
127 int seccomp_filter : 1;
128 int seccomp_filter_tsync : 1;
129 int seccomp_filter_logging : 1;
130 int chroot : 1;
131 int pivot_root : 1;
132 int mount_tmp : 1;
133 int do_init : 1;
134 int pid_file : 1;
135 int cgroups : 1;
136 int alt_syscall : 1;
137 int reset_signal_mask : 1;
138 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800139 int new_session_keyring : 1;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400140 int forward_signals : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400141 } flags;
142 uid_t uid;
143 gid_t gid;
144 gid_t usergid;
145 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800146 size_t suppl_gid_count;
147 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400148 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800149 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400150 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700151 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700152 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400153 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800154 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800155 char *uidmap;
156 char *gidmap;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400157 char *hostname;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800158 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800159 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800160 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700161 struct mountpoint *mounts_head;
162 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800163 size_t mounts_count;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100164 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800165 char *cgroups[MAX_CGROUPS];
166 size_t cgroup_count;
Dylan Reid0f72ef42017-06-06 15:42:49 -0700167 struct minijail_rlimit rlimits[MAX_RLIMITS];
168 size_t rlimit_count;
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700169 uint64_t securebits_skip_mask;
Will Drewryf89aef52011-09-16 16:48:57 -0500170};
171
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700172/*
173 * Strip out flags meant for the parent.
174 * We keep things that are not inherited across execve(2) (e.g. capabilities),
175 * or are easier to set after execve(2) (e.g. seccomp filters).
176 */
177void minijail_preenter(struct minijail *j)
178{
179 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700180 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900181 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700182 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700183 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800184 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800185 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800186 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400187 j->flags.forward_signals = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700188}
189
190/*
191 * Strip out flags meant for the child.
192 * We keep things that are inherited across execve(2).
193 */
194void minijail_preexec(struct minijail *j)
195{
196 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700197 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800198 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700199 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800200 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700201 if (j->user)
202 free(j->user);
203 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800204 if (j->suppl_gid_list)
205 free(j->suppl_gid_list);
206 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700207 memset(&j->flags, 0, sizeof(j->flags));
208 /* Now restore anything we meant to keep. */
209 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700210 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800211 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700212 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800213 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700214 /* Note, |pids| will already have been used before this call. */
215}
216
217/* Minijail API. */
218
Will Drewry6ac91122011-10-21 16:38:58 -0500219struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400220{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400221 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400222}
223
Will Drewry6ac91122011-10-21 16:38:58 -0500224void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400225{
226 if (uid == 0)
227 die("useless change to uid 0");
228 j->uid = uid;
229 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400230}
231
Will Drewry6ac91122011-10-21 16:38:58 -0500232void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400233{
234 if (gid == 0)
235 die("useless change to gid 0");
236 j->gid = gid;
237 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400238}
239
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800240void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
241 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800242{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800243 size_t i;
244
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500245 if (j->flags.inherit_suppl_gids)
246 die("cannot inherit *and* set supplementary groups");
247 if (j->flags.keep_suppl_gids)
248 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800249
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800250 if (size == 0) {
251 /* Clear supplementary groups. */
252 j->suppl_gid_list = NULL;
253 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100254 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800255 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800256 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800257
258 /* Copy the gid_t array. */
259 j->suppl_gid_list = calloc(size, sizeof(gid_t));
260 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800261 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800262 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800263 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800264 j->suppl_gid_list[i] = list[i];
265 }
266 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100267 j->flags.set_suppl_gids = 1;
268}
269
270void API minijail_keep_supplementary_gids(struct minijail *j) {
271 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800272}
273
Will Drewry6ac91122011-10-21 16:38:58 -0500274int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400275{
276 char *buf = NULL;
277 struct passwd pw;
278 struct passwd *ppw = NULL;
279 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
280 if (sz == -1)
281 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400282
Elly Jonesdd3e8512012-01-23 15:13:38 -0500283 /*
284 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400285 * the maximum needed size of the buffer, so we don't have to search.
286 */
287 buf = malloc(sz);
288 if (!buf)
289 return -ENOMEM;
290 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500291 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800292 * We're safe to free the buffer here. The strings inside |pw| point
293 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800294 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
295 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500296 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400297 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700298 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400299 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700300 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400301 minijail_change_uid(j, ppw->pw_uid);
302 j->user = strdup(user);
303 if (!j->user)
304 return -ENOMEM;
305 j->usergid = ppw->pw_gid;
306 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400307}
308
Will Drewry6ac91122011-10-21 16:38:58 -0500309int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400310{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700311 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700312 struct group gr;
313 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400314 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
315 if (sz == -1)
316 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400317
Elly Jonesdd3e8512012-01-23 15:13:38 -0500318 /*
319 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400320 * the maximum needed size of the buffer, so we don't have to search.
321 */
322 buf = malloc(sz);
323 if (!buf)
324 return -ENOMEM;
325 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500326 /*
327 * We're safe to free the buffer here. The strings inside gr point
328 * inside buf, but we don't use any of them; this leaves the pointers
329 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
330 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400331 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700332 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400333 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700334 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400335 minijail_change_gid(j, pgr->gr_gid);
336 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400337}
338
Will Drewry6ac91122011-10-21 16:38:58 -0500339void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400340{
341 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400342}
343
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700344void API minijail_no_new_privs(struct minijail *j)
345{
346 j->flags.no_new_privs = 1;
347}
348
Will Drewry6ac91122011-10-21 16:38:58 -0500349void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400350{
351 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500352}
353
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400354void API minijail_set_seccomp_filter_tsync(struct minijail *j)
355{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400356 if (j->filter_len > 0 && j->filter_prog != NULL) {
357 die("minijail_set_seccomp_filter_tsync() must be called "
358 "before minijail_parse_seccomp_filters()");
359 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400360 j->flags.seccomp_filter_tsync = 1;
361}
362
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700363void API minijail_log_seccomp_filter_failures(struct minijail *j)
364{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400365 if (j->filter_len > 0 && j->filter_prog != NULL) {
366 die("minijail_log_seccomp_filter_failures() must be called "
367 "before minijail_parse_seccomp_filters()");
368 }
369 j->flags.seccomp_filter_logging = 1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700370}
371
Will Drewry6ac91122011-10-21 16:38:58 -0500372void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400373{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800374 /*
375 * 'minijail_use_caps' configures a runtime-capabilities-only
376 * environment, including a bounding set matching the thread's runtime
377 * (permitted|inheritable|effective) sets.
378 * Therefore, it will override any existing bounding set configurations
379 * since the latter would allow gaining extra runtime capabilities from
380 * file capabilities.
381 */
382 if (j->flags.capbset_drop) {
383 warn("overriding bounding set configuration");
384 j->cap_bset = 0;
385 j->flags.capbset_drop = 0;
386 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400387 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800388 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400389}
390
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800391void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
392{
393 if (j->flags.use_caps) {
394 /*
395 * 'minijail_use_caps' will have already configured a capability
396 * bounding set matching the (permitted|inheritable|effective)
397 * sets. Abort if the user tries to configure a separate
398 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
399 * are mutually exclusive.
400 */
401 die("runtime capabilities already configured, can't drop "
402 "bounding set separately");
403 }
404 j->cap_bset = capmask;
405 j->flags.capbset_drop = 1;
406}
407
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400408void API minijail_set_ambient_caps(struct minijail *j)
409{
410 j->flags.set_ambient_caps = 1;
411}
412
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800413void API minijail_reset_signal_mask(struct minijail *j)
414{
Peter Qiu2860c462015-12-16 15:13:06 -0800415 j->flags.reset_signal_mask = 1;
416}
417
Will Drewry6ac91122011-10-21 16:38:58 -0500418void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400419{
420 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400421}
422
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700423void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
424{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800425 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700426 if (ns_fd < 0) {
427 pdie("failed to open namespace '%s'", ns_path);
428 }
429 j->mountns_fd = ns_fd;
430 j->flags.enter_vfs = 1;
431}
432
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800433void API minijail_new_session_keyring(struct minijail *j)
434{
435 j->flags.new_session_keyring = 1;
436}
437
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700438void API minijail_skip_setting_securebits(struct minijail *j,
439 uint64_t securebits_skip_mask)
440{
441 j->securebits_skip_mask = securebits_skip_mask;
442}
443
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800444void API minijail_skip_remount_private(struct minijail *j)
445{
446 j->flags.skip_remount_private = 1;
447}
448
Will Drewry6ac91122011-10-21 16:38:58 -0500449void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400450{
Elly Jonese58176c2012-01-23 11:46:17 -0500451 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700452 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400453 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800454 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400455}
456
Dylan Reidf7942472015-11-18 17:55:26 -0800457void API minijail_namespace_ipc(struct minijail *j)
458{
459 j->flags.ipc = 1;
460}
461
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400462void API minijail_namespace_uts(struct minijail *j)
463{
464 j->flags.uts = 1;
465}
466
467int API minijail_namespace_set_hostname(struct minijail *j, const char *name)
468{
469 if (j->hostname)
470 return -EINVAL;
471 minijail_namespace_uts(j);
472 j->hostname = strdup(name);
473 if (!j->hostname)
474 return -ENOMEM;
475 return 0;
476}
477
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400478void API minijail_namespace_net(struct minijail *j)
479{
480 j->flags.net = 1;
481}
482
Dylan Reid1102f5a2015-09-15 11:52:20 -0700483void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
484{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800485 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700486 if (ns_fd < 0) {
487 pdie("failed to open namespace '%s'", ns_path);
488 }
489 j->netns_fd = ns_fd;
490 j->flags.enter_net = 1;
491}
492
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700493void API minijail_namespace_cgroups(struct minijail *j)
494{
495 j->flags.ns_cgroups = 1;
496}
497
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700498void API minijail_close_open_fds(struct minijail *j)
499{
500 j->flags.close_open_fds = 1;
501}
502
Dylan Reid791f5772015-09-14 20:02:42 -0700503void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400504{
505 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700506 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400507}
508
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800509void API minijail_namespace_user(struct minijail *j)
510{
511 j->flags.userns = 1;
512}
513
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400514void API minijail_namespace_user_disable_setgroups(struct minijail *j)
515{
516 j->flags.disable_setgroups = 1;
517}
518
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800519int API minijail_uidmap(struct minijail *j, const char *uidmap)
520{
521 j->uidmap = strdup(uidmap);
522 if (!j->uidmap)
523 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800524 char *ch;
525 for (ch = j->uidmap; *ch; ch++) {
526 if (*ch == ',')
527 *ch = '\n';
528 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800529 return 0;
530}
531
532int API minijail_gidmap(struct minijail *j, const char *gidmap)
533{
534 j->gidmap = strdup(gidmap);
535 if (!j->gidmap)
536 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800537 char *ch;
538 for (ch = j->gidmap; *ch; ch++) {
539 if (*ch == ',')
540 *ch = '\n';
541 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800542 return 0;
543}
544
Will Drewry6ac91122011-10-21 16:38:58 -0500545void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400546{
Lutz Justen13807cb2017-01-03 17:11:55 +0100547 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400548}
549
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800550void API minijail_run_as_init(struct minijail *j)
551{
552 /*
553 * Since the jailed program will become 'init' in the new PID namespace,
554 * Minijail does not need to fork an 'init' process.
555 */
556 j->flags.do_init = 0;
557}
558
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700559int API minijail_enter_chroot(struct minijail *j, const char *dir)
560{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400561 if (j->chrootdir)
562 return -EINVAL;
563 j->chrootdir = strdup(dir);
564 if (!j->chrootdir)
565 return -ENOMEM;
566 j->flags.chroot = 1;
567 return 0;
568}
569
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800570int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
571{
572 if (j->chrootdir)
573 return -EINVAL;
574 j->chrootdir = strdup(dir);
575 if (!j->chrootdir)
576 return -ENOMEM;
577 j->flags.pivot_root = 1;
578 return 0;
579}
580
Dylan Reida14e08d2015-10-22 21:05:29 -0700581char API *minijail_get_original_path(struct minijail *j,
582 const char *path_inside_chroot)
583{
Dylan Reid648b2202015-10-23 00:50:00 -0700584 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700585
Dylan Reid648b2202015-10-23 00:50:00 -0700586 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700587 while (b) {
588 /*
589 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700590 * mount, then the original path is exactly the source of
591 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700592 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700593 * mount source = /some/path/exe, mount dest =
594 * /chroot/path/exe Then when getting the original path of
595 * "/chroot/path/exe", the source of that mount,
596 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700597 */
598 if (!strcmp(b->dest, path_inside_chroot))
599 return strdup(b->src);
600
601 /*
602 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700603 * mount, take the suffix of the chroot path relative to the
604 * mount destination path, and append it to the mount source
605 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700606 */
607 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
608 const char *relative_path =
609 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400610 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700611 }
612 b = b->next;
613 }
614
615 /* If there is a chroot path, append |path_inside_chroot| to that. */
616 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400617 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700618
619 /* No chroot, so the path outside is the same as it is inside. */
620 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700621}
622
Martin Pelikánab9eb442017-01-25 11:53:58 +1100623size_t minijail_get_tmpfs_size(const struct minijail *j)
624{
625 return j->tmpfs_size;
626}
627
Lee Campbell11af0622014-05-22 12:36:04 -0700628void API minijail_mount_tmp(struct minijail *j)
629{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100630 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
631}
632
633void API minijail_mount_tmp_size(struct minijail *j, size_t size)
634{
635 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700636 j->flags.mount_tmp = 1;
637}
638
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800639int API minijail_write_pid_file(struct minijail *j, const char *path)
640{
641 j->pid_file_path = strdup(path);
642 if (!j->pid_file_path)
643 return -ENOMEM;
644 j->flags.pid_file = 1;
645 return 0;
646}
647
Dylan Reid605ce7f2016-01-19 19:21:00 -0800648int API minijail_add_to_cgroup(struct minijail *j, const char *path)
649{
650 if (j->cgroup_count >= MAX_CGROUPS)
651 return -ENOMEM;
652 j->cgroups[j->cgroup_count] = strdup(path);
653 if (!j->cgroups[j->cgroup_count])
654 return -ENOMEM;
655 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800656 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800657 return 0;
658}
659
Dylan Reid0f72ef42017-06-06 15:42:49 -0700660int API minijail_rlimit(struct minijail *j, int type, uint32_t cur,
661 uint32_t max)
662{
663 size_t i;
664
665 if (j->rlimit_count >= MAX_RLIMITS)
666 return -ENOMEM;
667 /* It's an error if the caller sets the same rlimit multiple times. */
668 for (i = 0; i < j->rlimit_count; i++) {
669 if (j->rlimits[i].type == type)
670 return -EEXIST;
671 }
672
673 j->rlimits[j->rlimit_count].type = type;
674 j->rlimits[j->rlimit_count].cur = cur;
675 j->rlimits[j->rlimit_count].max = max;
676 j->rlimit_count++;
677 return 0;
678}
679
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400680int API minijail_forward_signals(struct minijail *j)
681{
682 j->flags.forward_signals = 1;
683 return 0;
684}
685
Dylan Reid81e23972016-05-18 14:06:35 -0700686int API minijail_mount_with_data(struct minijail *j, const char *src,
687 const char *dest, const char *type,
688 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700689{
Dylan Reid648b2202015-10-23 00:50:00 -0700690 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400691
692 if (*dest != '/')
693 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700694 m = calloc(1, sizeof(*m));
695 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400696 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700697 m->dest = strdup(dest);
698 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400699 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700700 m->src = strdup(src);
701 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400702 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700703 m->type = strdup(type);
704 if (!m->type)
705 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700706 if (data) {
707 m->data = strdup(data);
708 if (!m->data)
709 goto error;
710 m->has_data = 1;
711 }
Dylan Reid648b2202015-10-23 00:50:00 -0700712 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400713
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800714 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400715
Elly Jonesdd3e8512012-01-23 15:13:38 -0500716 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700717 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400718 * containing vfs namespace.
719 */
720 minijail_namespace_vfs(j);
721
Dylan Reid648b2202015-10-23 00:50:00 -0700722 if (j->mounts_tail)
723 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400724 else
Dylan Reid648b2202015-10-23 00:50:00 -0700725 j->mounts_head = m;
726 j->mounts_tail = m;
727 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400728
729 return 0;
730
731error:
Dylan Reid81e23972016-05-18 14:06:35 -0700732 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700733 free(m->src);
734 free(m->dest);
735 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400736 return -ENOMEM;
737}
738
Dylan Reid81e23972016-05-18 14:06:35 -0700739int API minijail_mount(struct minijail *j, const char *src, const char *dest,
740 const char *type, unsigned long flags)
741{
742 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
743}
744
Dylan Reid648b2202015-10-23 00:50:00 -0700745int API minijail_bind(struct minijail *j, const char *src, const char *dest,
746 int writeable)
747{
748 unsigned long flags = MS_BIND;
749
750 if (!writeable)
751 flags |= MS_RDONLY;
752
753 return minijail_mount(j, src, dest, "", flags);
754}
755
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400756static void clear_seccomp_options(struct minijail *j)
757{
758 j->flags.seccomp_filter = 0;
759 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400760 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400761 j->filter_len = 0;
762 j->filter_prog = NULL;
763 j->flags.no_new_privs = 0;
764}
765
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400766static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400767{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400768 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400769 /*
770 * |errno| will be set to EINVAL when seccomp has not been
771 * compiled into the kernel. On certain platforms and kernel
772 * versions this is not a fatal failure. In that case, and only
773 * in that case, disable seccomp and skip loading the filters.
774 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400775 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400776 warn("not loading seccomp filters, seccomp filter not "
777 "supported");
778 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400779 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700780 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400781 /*
782 * If |errno| != EINVAL or seccomp_can_softfail() is false,
783 * we can proceed. Worst case scenario minijail_enter() will
784 * abort() if seccomp fails.
785 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700786 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400787 if (j->flags.seccomp_filter_tsync) {
788 /* Are the seccomp(2) syscall and the TSYNC option supported? */
789 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
790 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
791 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400792 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
793 warn("seccomp(2) syscall not supported");
794 clear_seccomp_options(j);
795 return 0;
796 } else if (saved_errno == EINVAL &&
797 seccomp_can_softfail()) {
798 warn(
799 "seccomp filter thread sync not supported");
800 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400801 return 0;
802 }
803 /*
804 * Similar logic here. If seccomp_can_softfail() is
805 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
806 * we can proceed. Worst case scenario minijail_enter()
807 * will abort() if seccomp or TSYNC fail.
808 */
809 }
810 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400811 return 1;
812}
813
814static int parse_seccomp_filters(struct minijail *j, FILE *policy_file)
815{
816 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400817 int use_ret_trap =
818 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
819 int allow_logging = j->flags.seccomp_filter_logging;
820
821 if (compile_filter(policy_file, fprog, use_ret_trap, allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400822 free(fprog);
823 return -1;
824 }
825
826 j->filter_len = fprog->len;
827 j->filter_prog = fprog;
828 return 0;
829}
830
831void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
832{
833 if (!seccomp_should_parse_filters(j))
834 return;
835
Elly Jonese1749eb2011-10-07 13:54:59 -0400836 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800837 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700838 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400839 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800840
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400841 if (parse_seccomp_filters(j, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700842 die("failed to compile seccomp filter BPF program in '%s'",
843 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800844 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400845 fclose(file);
846}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800847
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400848void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
849{
850 if (!seccomp_should_parse_filters(j))
851 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800852
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400853 FILE *file = fdopen(fd, "r");
854 if (!file) {
855 pdie("failed to associate stream with fd %d", fd);
856 }
857
858 if (parse_seccomp_filters(j, file) != 0) {
859 die("failed to compile seccomp filter BPF program from fd %d",
860 fd);
861 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400862 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500863}
864
Andrew Brestickereac28942015-11-11 16:04:46 -0800865int API minijail_use_alt_syscall(struct minijail *j, const char *table)
866{
867 j->alt_syscall_table = strdup(table);
868 if (!j->alt_syscall_table)
869 return -ENOMEM;
870 j->flags.alt_syscall = 1;
871 return 0;
872}
873
Will Drewryf89aef52011-09-16 16:48:57 -0500874struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400875 size_t available;
876 size_t total;
877 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500878};
879
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800880void marshal_state_init(struct marshal_state *state, char *buf,
881 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400882{
883 state->available = available;
884 state->buf = buf;
885 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500886}
887
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800888void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400889{
890 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500891
Elly Jonese1749eb2011-10-07 13:54:59 -0400892 /* Up to |available| will be written. */
893 if (copy_len) {
894 memcpy(state->buf, src, copy_len);
895 state->buf += copy_len;
896 state->available -= copy_len;
897 }
898 /* |total| will contain the expected length. */
899 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500900}
901
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400902void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700903{
904 marshal_append(state, m->src, strlen(m->src) + 1);
905 marshal_append(state, m->dest, strlen(m->dest) + 1);
906 marshal_append(state, m->type, strlen(m->type) + 1);
907 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
908 if (m->has_data)
909 marshal_append(state, m->data, strlen(m->data) + 1);
910 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
911}
912
Will Drewry6ac91122011-10-21 16:38:58 -0500913void minijail_marshal_helper(struct marshal_state *state,
914 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400915{
Dylan Reid648b2202015-10-23 00:50:00 -0700916 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800917 size_t i;
918
Elly Jonese1749eb2011-10-07 13:54:59 -0400919 marshal_append(state, (char *)j, sizeof(*j));
920 if (j->user)
921 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800922 if (j->suppl_gid_list) {
923 marshal_append(state, j->suppl_gid_list,
924 j->suppl_gid_count * sizeof(gid_t));
925 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400926 if (j->chrootdir)
927 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400928 if (j->hostname)
929 marshal_append(state, j->hostname, strlen(j->hostname) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800930 if (j->alt_syscall_table) {
931 marshal_append(state, j->alt_syscall_table,
932 strlen(j->alt_syscall_table) + 1);
933 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800934 if (j->flags.seccomp_filter && j->filter_prog) {
935 struct sock_fprog *fp = j->filter_prog;
936 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800937 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400938 }
Dylan Reid648b2202015-10-23 00:50:00 -0700939 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400940 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400941 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800942 for (i = 0; i < j->cgroup_count; ++i)
943 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500944}
945
Will Drewry6ac91122011-10-21 16:38:58 -0500946size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400947{
948 struct marshal_state state;
949 marshal_state_init(&state, NULL, 0);
950 minijail_marshal_helper(&state, j);
951 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500952}
953
Elly Jonese1749eb2011-10-07 13:54:59 -0400954int minijail_marshal(const struct minijail *j, char *buf, size_t available)
955{
956 struct marshal_state state;
957 marshal_state_init(&state, buf, available);
958 minijail_marshal_helper(&state, j);
959 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500960}
961
Elly Jonese1749eb2011-10-07 13:54:59 -0400962int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
963{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800964 size_t i;
965 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500966 int ret = -EINVAL;
967
Elly Jonese1749eb2011-10-07 13:54:59 -0400968 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500969 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400970 memcpy((void *)j, serialized, sizeof(*j));
971 serialized += sizeof(*j);
972 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500973
Will Drewrybee7ba72011-10-21 20:47:01 -0500974 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -0400975 j->pid_file_path = NULL;
976 j->uidmap = NULL;
977 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -0700978 j->mounts_head = NULL;
979 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800980 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500981
Elly Jonese1749eb2011-10-07 13:54:59 -0400982 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400983 char *user = consumestr(&serialized, &length);
984 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500985 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400986 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500987 if (!j->user)
988 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400989 }
Will Drewryf89aef52011-09-16 16:48:57 -0500990
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800991 if (j->suppl_gid_list) { /* stale pointer */
992 if (j->suppl_gid_count > NGROUPS_MAX) {
993 goto bad_gid_list;
994 }
995 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
996 void *gid_list_bytes =
997 consumebytes(gid_list_size, &serialized, &length);
998 if (!gid_list_bytes)
999 goto bad_gid_list;
1000
1001 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1002 if (!j->suppl_gid_list)
1003 goto bad_gid_list;
1004
1005 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1006 }
1007
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001008 if (j->chrootdir) { /* stale pointer */
1009 char *chrootdir = consumestr(&serialized, &length);
1010 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001011 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001012 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001013 if (!j->chrootdir)
1014 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001015 }
1016
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001017 if (j->hostname) { /* stale pointer */
1018 char *hostname = consumestr(&serialized, &length);
1019 if (!hostname)
1020 goto bad_hostname;
1021 j->hostname = strdup(hostname);
1022 if (!j->hostname)
1023 goto bad_hostname;
1024 }
1025
Andrew Brestickereac28942015-11-11 16:04:46 -08001026 if (j->alt_syscall_table) { /* stale pointer */
1027 char *alt_syscall_table = consumestr(&serialized, &length);
1028 if (!alt_syscall_table)
1029 goto bad_syscall_table;
1030 j->alt_syscall_table = strdup(alt_syscall_table);
1031 if (!j->alt_syscall_table)
1032 goto bad_syscall_table;
1033 }
1034
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001035 if (j->flags.seccomp_filter && j->filter_len > 0) {
1036 size_t ninstrs = j->filter_len;
1037 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1038 ninstrs > USHRT_MAX)
1039 goto bad_filters;
1040
1041 size_t program_len = ninstrs * sizeof(struct sock_filter);
1042 void *program = consumebytes(program_len, &serialized, &length);
1043 if (!program)
1044 goto bad_filters;
1045
1046 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001047 if (!j->filter_prog)
1048 goto bad_filters;
1049
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001050 j->filter_prog->len = ninstrs;
1051 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001052 if (!j->filter_prog->filter)
1053 goto bad_filter_prog_instrs;
1054
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001055 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001056 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001057
Dylan Reid648b2202015-10-23 00:50:00 -07001058 count = j->mounts_count;
1059 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001060 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001061 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001062 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001063 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001064 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001065 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001066 const char *src = consumestr(&serialized, &length);
1067 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001068 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001069 dest = consumestr(&serialized, &length);
1070 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001071 goto bad_mounts;
1072 type = consumestr(&serialized, &length);
1073 if (!type)
1074 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001075 has_data = consumebytes(sizeof(*has_data), &serialized,
1076 &length);
1077 if (!has_data)
1078 goto bad_mounts;
1079 if (*has_data) {
1080 data = consumestr(&serialized, &length);
1081 if (!data)
1082 goto bad_mounts;
1083 }
Dylan Reid648b2202015-10-23 00:50:00 -07001084 flags = consumebytes(sizeof(*flags), &serialized, &length);
1085 if (!flags)
1086 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001087 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001088 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001089 }
1090
Dylan Reid605ce7f2016-01-19 19:21:00 -08001091 count = j->cgroup_count;
1092 j->cgroup_count = 0;
1093 for (i = 0; i < count; ++i) {
1094 char *cgroup = consumestr(&serialized, &length);
1095 if (!cgroup)
1096 goto bad_cgroups;
1097 j->cgroups[i] = strdup(cgroup);
1098 if (!j->cgroups[i])
1099 goto bad_cgroups;
1100 ++j->cgroup_count;
1101 }
1102
Elly Jonese1749eb2011-10-07 13:54:59 -04001103 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001104
Dylan Reid605ce7f2016-01-19 19:21:00 -08001105bad_cgroups:
1106 while (j->mounts_head) {
1107 struct mountpoint *m = j->mounts_head;
1108 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07001109 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001110 free(m->type);
1111 free(m->dest);
1112 free(m->src);
1113 free(m);
1114 }
1115 for (i = 0; i < j->cgroup_count; ++i)
1116 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001117bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001118 if (j->flags.seccomp_filter && j->filter_len > 0) {
1119 free(j->filter_prog->filter);
1120 free(j->filter_prog);
1121 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001122bad_filter_prog_instrs:
1123 if (j->filter_prog)
1124 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001125bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001126 if (j->alt_syscall_table)
1127 free(j->alt_syscall_table);
1128bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001129 if (j->chrootdir)
1130 free(j->chrootdir);
1131bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001132 if (j->hostname)
1133 free(j->hostname);
1134bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001135 if (j->suppl_gid_list)
1136 free(j->suppl_gid_list);
1137bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001138 if (j->user)
1139 free(j->user);
1140clear_pointers:
1141 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001142 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001143 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001144 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001145 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001146 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001147out:
1148 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001149}
1150
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001151/*
1152 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001153 * @j Minijail these mounts are for
1154 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001155 *
1156 * Returns 0 for success.
1157 */
Dylan Reid648b2202015-10-23 00:50:00 -07001158static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001159{
Dylan Reid648b2202015-10-23 00:50:00 -07001160 int ret;
1161 char *dest;
1162 int remount_ro = 0;
1163
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001164 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001165 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001166 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001167
Dylan Reideec77962016-06-30 19:35:10 -07001168 if (setup_mount_destination(m->src, dest, j->uid, j->gid))
1169 pdie("creating mount target '%s' failed", dest);
1170
Dylan Reid648b2202015-10-23 00:50:00 -07001171 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001172 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1173 * can't both be specified in the original bind mount.
1174 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001175 */
1176 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1177 remount_ro = 1;
1178 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001179 }
Dylan Reid648b2202015-10-23 00:50:00 -07001180
Dylan Reid81e23972016-05-18 14:06:35 -07001181 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001182 if (ret)
1183 pdie("mount: %s -> %s", m->src, dest);
1184
1185 if (remount_ro) {
1186 m->flags |= MS_RDONLY;
1187 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001188 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001189 if (ret)
1190 pdie("bind ro: %s -> %s", m->src, dest);
1191 }
1192
Elly Jones51a5b6c2011-10-12 19:09:26 -04001193 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001194 if (m->next)
1195 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001196 return ret;
1197}
1198
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001199static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001200{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001201 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001202
1203 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001204 return ret;
1205
1206 if (chroot(j->chrootdir))
1207 return -errno;
1208
1209 if (chdir("/"))
1210 return -errno;
1211
1212 return 0;
1213}
1214
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001215static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001216{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001217 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001218
1219 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001220 return ret;
1221
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001222 /*
1223 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001224 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001225 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001226 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001227 if (oldroot < 0)
1228 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001229 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001230 if (newroot < 0)
1231 pdie("failed to open %s for fchdir", j->chrootdir);
1232
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001233 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001234 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001235 * do a self bind mount.
1236 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001237 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1238 pdie("failed to bind mount '%s'", j->chrootdir);
1239 if (chdir(j->chrootdir))
1240 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001241 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001242 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001243
1244 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001245 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001246 * change to the old root and unmount it.
1247 */
1248 if (fchdir(oldroot))
1249 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001250
1251 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001252 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1253 * there could be a shared mount point under |oldroot|. In that case,
1254 * mounts under this shared mount point will be unmounted below, and
1255 * this unmounting will propagate to the original mount namespace
1256 * (because the mount point is shared). To prevent this unexpected
1257 * unmounting, remove these mounts from their peer groups by recursively
1258 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001259 */
1260 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001261 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001262 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001263 if (umount2(".", MNT_DETACH))
1264 pdie("umount(/)");
1265 /* Change back to the new root. */
1266 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001267 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001268 if (close(oldroot))
1269 return -errno;
1270 if (close(newroot))
1271 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001272 if (chroot("/"))
1273 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001274 /* Set correct CWD for getcwd(3). */
1275 if (chdir("/"))
1276 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001277
1278 return 0;
1279}
1280
Martin Pelikánab9eb442017-01-25 11:53:58 +11001281static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001282{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001283 const char fmt[] = "size=%zu,mode=1777";
1284 /* Count for the user storing ULLONG_MAX literally + extra space. */
1285 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1286 int ret;
1287
1288 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1289
1290 if (ret <= 0)
1291 pdie("tmpfs size spec error");
1292 else if ((size_t)ret >= sizeof(data))
1293 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001294 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001295 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001296}
1297
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001298static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001299{
1300 const char *kProcPath = "/proc";
1301 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001302 /*
1303 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001304 * /proc in our namespace, which means using MS_REMOUNT here would
1305 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001306 * namespace (!). Instead, remove their mount from our namespace lazily
1307 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001308 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001309 if (umount2(kProcPath, MNT_DETACH)) {
1310 /*
1311 * If we are in a new user namespace, umount(2) will fail.
1312 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1313 */
1314 if (j->flags.userns) {
1315 info("umount(/proc, MNT_DETACH) failed, "
1316 "this is expected when using user namespaces");
1317 } else {
1318 return -errno;
1319 }
1320 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001321 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001322 return -errno;
1323 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001324}
1325
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001326static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001327{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001328 kill(j->initpid, SIGKILL);
1329 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001330}
1331
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001332static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001333{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001334 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001335 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001336}
1337
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001338static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001339{
1340 size_t i;
1341
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001342 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001343 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001344 kill_child_and_die(j, "failed to add to cgroups");
1345 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001346}
1347
Dylan Reid0f72ef42017-06-06 15:42:49 -07001348static void set_rlimits_or_die(const struct minijail *j)
1349{
1350 size_t i;
1351
1352 for (i = 0; i < j->rlimit_count; ++i) {
1353 struct rlimit limit;
1354 limit.rlim_cur = j->rlimits[i].cur;
1355 limit.rlim_max = j->rlimits[i].max;
1356 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1357 kill_child_and_die(j, "failed to set rlimit");
1358 }
1359}
1360
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001361static void write_ugid_maps_or_die(const struct minijail *j)
1362{
1363 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1364 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001365 if (j->gidmap && j->flags.disable_setgroups) {
1366 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1367 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001368 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001369 if (ret == -ENOENT) {
1370 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1371 warn("could not disable setgroups(2)");
1372 } else
1373 kill_child_and_die(j, "failed to disable setgroups(2)");
1374 }
1375 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001376 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1377 kill_child_and_die(j, "failed to write gid_map");
1378}
1379
1380static void enter_user_namespace(const struct minijail *j)
1381{
1382 if (j->uidmap && setresuid(0, 0, 0))
1383 pdie("user_namespaces: setresuid(0, 0, 0) failed");
1384 if (j->gidmap && setresgid(0, 0, 0))
1385 pdie("user_namespaces: setresgid(0, 0, 0) failed");
1386}
1387
1388static void parent_setup_complete(int *pipe_fds)
1389{
1390 close(pipe_fds[0]);
1391 close(pipe_fds[1]);
1392}
1393
1394/*
1395 * wait_for_parent_setup: Called by the child process to wait for any
1396 * further parent-side setup to complete before continuing.
1397 */
1398static void wait_for_parent_setup(int *pipe_fds)
1399{
1400 char buf;
1401
1402 close(pipe_fds[1]);
1403
1404 /* Wait for parent to complete setup and close the pipe. */
1405 if (read(pipe_fds[0], &buf, 1) != 0)
1406 die("failed to sync with parent");
1407 close(pipe_fds[0]);
1408}
1409
1410static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001411{
Lutz Justen13807cb2017-01-03 17:11:55 +01001412 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1413 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001414 die("can only do one of inherit, keep, or set supplementary "
1415 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001416 }
1417
Lutz Justen13807cb2017-01-03 17:11:55 +01001418 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001419 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001420 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001421 } else if (j->flags.set_suppl_gids) {
1422 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001423 pdie("setgroups(suppl_gids) failed");
Lutz Justen13807cb2017-01-03 17:11:55 +01001424 } else if (!j->flags.keep_suppl_gids) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001425 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001426 * Only attempt to clear supplementary groups if we are changing
Lutz Justen13807cb2017-01-03 17:11:55 +01001427 * users or groups.
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001428 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001429 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001430 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001431 }
1432
1433 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001434 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001435
1436 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001437 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001438}
1439
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001440static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1441{
1442 const uint64_t one = 1;
1443 unsigned int i;
1444 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1445 if (keep_mask & (one << i))
1446 continue;
1447 if (prctl(PR_CAPBSET_DROP, i))
1448 pdie("could not drop capability from bounding set");
1449 }
1450}
1451
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001452static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001453{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001454 if (!j->flags.use_caps)
1455 return;
1456
Elly Jonese1749eb2011-10-07 13:54:59 -04001457 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001458 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001459 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001460 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001461 unsigned int i;
1462 if (!caps)
1463 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001464 if (cap_clear(caps))
1465 die("can't clear caps");
1466
1467 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001468 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001469 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001470 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001471 flag[0] = i;
1472 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001473 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001474 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001475 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001476 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001477 die("can't add inheritable cap");
1478 }
1479 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001480 die("can't apply initial cleaned capset");
1481
1482 /*
1483 * Instead of dropping bounding set first, do it here in case
1484 * the caller had a more permissive bounding set which could
1485 * have been used above to raise a capability that wasn't already
1486 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1487 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001488 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001489
1490 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001491 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001492 flag[0] = CAP_SETPCAP;
1493 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1494 die("can't clear effective cap");
1495 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1496 die("can't clear permitted cap");
1497 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1498 die("can't clear inheritable cap");
1499 }
1500
1501 if (cap_set_proc(caps))
1502 die("can't apply final cleaned capset");
1503
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001504 /*
1505 * If ambient capabilities are supported, clear all capabilities first,
1506 * then raise the requested ones.
1507 */
1508 if (j->flags.set_ambient_caps) {
1509 if (!cap_ambient_supported()) {
1510 pdie("ambient capabilities not supported");
1511 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001512 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1513 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001514 pdie("can't clear ambient capabilities");
1515 }
1516
1517 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1518 if (!(j->caps & (one << i)))
1519 continue;
1520
1521 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1522 0) != 0) {
1523 pdie("prctl(PR_CAP_AMBIENT, "
1524 "PR_CAP_AMBIENT_RAISE, %u) failed",
1525 i);
1526 }
1527 }
1528 }
1529
Kees Cook323878a2013-02-05 15:35:24 -08001530 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001531}
1532
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001533static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001534{
1535 /*
1536 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1537 * in the kernel source tree for an explanation of the parameters.
1538 */
1539 if (j->flags.no_new_privs) {
1540 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1541 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1542 }
1543
1544 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001545 * Code running with ASan
1546 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1547 * will make system calls not included in the syscall filter policy,
1548 * which will likely crash the program. Skip setting seccomp filter in
1549 * that case.
1550 * 'running_with_asan()' has no inputs and is completely defined at
1551 * build time, so this cannot be used by an attacker to skip setting
1552 * seccomp filter.
1553 */
1554 if (j->flags.seccomp_filter && running_with_asan()) {
1555 warn("running with ASan, not setting seccomp filter");
1556 return;
1557 }
1558
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001559 if (j->flags.seccomp_filter) {
1560 if (j->flags.seccomp_filter_logging) {
1561 /*
1562 * If logging seccomp filter failures,
1563 * install the SIGSYS handler first.
1564 */
1565 if (install_sigsys_handler())
1566 pdie("failed to install SIGSYS handler");
1567 warn("logging seccomp filter failures");
1568 } else if (j->flags.seccomp_filter_tsync) {
1569 /*
1570 * If setting thread sync,
1571 * reset the SIGSYS signal handler so that
1572 * the entire thread group is killed.
1573 */
1574 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1575 pdie("failed to reset SIGSYS disposition");
1576 info("reset SIGSYS disposition");
1577 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001578 }
1579
1580 /*
1581 * Install the syscall filter.
1582 */
1583 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001584 if (j->flags.seccomp_filter_tsync) {
1585 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1586 SECCOMP_FILTER_FLAG_TSYNC,
1587 j->filter_prog)) {
1588 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001589 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001590 } else {
1591 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1592 j->filter_prog)) {
1593 pdie("prctl(seccomp_filter) failed");
1594 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001595 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001596 }
1597}
1598
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001599static pid_t forward_pid = -1;
1600
1601static void forward_signal(__attribute__((unused)) int nr,
1602 __attribute__((unused)) siginfo_t *siginfo,
1603 __attribute__((unused)) void *void_context)
1604{
1605 if (forward_pid != -1) {
1606 kill(forward_pid, nr);
1607 }
1608}
1609
1610static void install_signal_handlers(void)
1611{
1612 struct sigaction act;
1613
1614 memset(&act, 0, sizeof(act));
1615 act.sa_sigaction = &forward_signal;
1616 act.sa_flags = SA_SIGINFO | SA_RESTART;
1617
1618 /* Handle all signals, except SIGCHLD. */
1619 for (int nr = 1; nr < NSIG; nr++) {
1620 /*
1621 * We don't care if we get EINVAL: that just means that we
1622 * can't handle this signal, so let's skip it and continue.
1623 */
1624 sigaction(nr, &act, NULL);
1625 }
1626 /* Reset SIGCHLD's handler. */
1627 signal(SIGCHLD, SIG_DFL);
1628
1629 /* Handle real-time signals. */
1630 for (int nr = SIGRTMIN; nr <= SIGRTMAX; nr++) {
1631 sigaction(nr, &act, NULL);
1632 }
1633}
1634
Will Drewry6ac91122011-10-21 16:38:58 -05001635void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001636{
Dylan Reidf682d472015-09-17 21:39:07 -07001637 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001638 * If we're dropping caps, get the last valid cap from /proc now,
1639 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001640 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001641 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001642 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001643 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001644
Elly Jonese1749eb2011-10-07 13:54:59 -04001645 if (j->flags.pids)
1646 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001647 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001648
Lutz Justen13807cb2017-01-03 17:11:55 +01001649 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001650 die("cannot inherit supplementary groups without setting a "
1651 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001652
Elly Jonesdd3e8512012-01-23 15:13:38 -05001653 /*
1654 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001655 * so we don't even try. If any of our operations fail, we abort() the
1656 * entire process.
1657 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001658 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001659 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001660
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001661 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001662 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001663 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001664 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001665 * Unless asked not to, remount all filesystems as private.
1666 * If they are shared, new bind mounts will creep out of our
1667 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001668 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1669 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001670 if (!j->flags.skip_remount_private) {
1671 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001672 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1673 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001674 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001675 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001676
Dylan Reidf7942472015-11-18 17:55:26 -08001677 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001678 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001679 }
1680
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001681 if (j->flags.uts) {
1682 if (unshare(CLONE_NEWUTS))
1683 pdie("unshare(CLONE_NEWUTS) failed");
1684
1685 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
1686 pdie("sethostname(%s) failed", j->hostname);
1687 }
1688
Dylan Reid1102f5a2015-09-15 11:52:20 -07001689 if (j->flags.enter_net) {
1690 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001691 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001692 } else if (j->flags.net) {
1693 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001694 pdie("unshare(CLONE_NEWNET) failed");
1695 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001696 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001697
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001698 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001699 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001700
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08001701 if (j->flags.new_session_keyring) {
1702 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
1703 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
1704 }
1705
Elly Jones51a5b6c2011-10-12 19:09:26 -04001706 if (j->flags.chroot && enter_chroot(j))
1707 pdie("chroot");
1708
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001709 if (j->flags.pivot_root && enter_pivot_root(j))
1710 pdie("pivot_root");
1711
Martin Pelikánab9eb442017-01-25 11:53:58 +11001712 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07001713 pdie("mount_tmp");
1714
Dylan Reid791f5772015-09-14 20:02:42 -07001715 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001716 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001717
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001718 /*
1719 * If we're only dropping capabilities from the bounding set, but not
1720 * from the thread's (permitted|inheritable|effective) sets, do it now.
1721 */
1722 if (j->flags.capbset_drop) {
1723 drop_capbset(j->cap_bset, last_valid_cap);
1724 }
1725
1726 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001727 /*
1728 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001729 * capability to change uids, our attempt to use setuid()
1730 * below will fail. Hang on to root caps across setuid(), then
1731 * lock securebits.
1732 */
1733 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001734 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001735
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -07001736 if (lock_securebits(j->securebits_skip_mask) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001737 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001738 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001739 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001740
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001741 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001742 /*
1743 * If we're setting no_new_privs, we can drop privileges
1744 * before setting seccomp filter. This way filter policies
1745 * don't need to allow privilege-dropping syscalls.
1746 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001747 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001748 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001749 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001750 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001751 /*
1752 * If we're not setting no_new_privs,
1753 * we need to set seccomp filter *before* dropping privileges.
1754 * WARNING: this means that filter policies *must* allow
1755 * setgroups()/setresgid()/setresuid() for dropping root and
1756 * capget()/capset()/prctl() for dropping caps.
1757 */
1758 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001759 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001760 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001761 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001762
Elly Jonesdd3e8512012-01-23 15:13:38 -05001763 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001764 * Select the specified alternate syscall table. The table must not
1765 * block prctl(2) if we're using seccomp as well.
1766 */
1767 if (j->flags.alt_syscall) {
1768 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001769 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08001770 }
1771
1772 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001773 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001774 * privilege-dropping syscalls :)
1775 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001776 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001777 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001778 warn("seccomp not supported");
1779 return;
1780 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001781 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001782 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001783}
1784
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001785/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001786static int init_exitstatus = 0;
1787
Will Drewry6ac91122011-10-21 16:38:58 -05001788void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001789{
1790 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001791}
1792
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001793void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001794{
1795 pid_t pid;
1796 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001797 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001798 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001799 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001800 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001801 /*
1802 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001803 * left inside our pid namespace or we get a signal.
1804 */
1805 if (pid == rootpid)
1806 init_exitstatus = status;
1807 }
1808 if (!WIFEXITED(init_exitstatus))
1809 _exit(MINIJAIL_ERR_INIT);
1810 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001811}
1812
Will Drewry6ac91122011-10-21 16:38:58 -05001813int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001814{
1815 size_t sz = 0;
1816 size_t bytes = read(fd, &sz, sizeof(sz));
1817 char *buf;
1818 int r;
1819 if (sizeof(sz) != bytes)
1820 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001821 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001822 return -E2BIG;
1823 buf = malloc(sz);
1824 if (!buf)
1825 return -ENOMEM;
1826 bytes = read(fd, buf, sz);
1827 if (bytes != sz) {
1828 free(buf);
1829 return -EINVAL;
1830 }
1831 r = minijail_unmarshal(j, buf, sz);
1832 free(buf);
1833 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001834}
1835
Will Drewry6ac91122011-10-21 16:38:58 -05001836int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001837{
1838 char *buf;
1839 size_t sz = minijail_size(j);
1840 ssize_t written;
1841 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001842
Elly Jonese1749eb2011-10-07 13:54:59 -04001843 if (!sz)
1844 return -EINVAL;
1845 buf = malloc(sz);
1846 r = minijail_marshal(j, buf, sz);
1847 if (r) {
1848 free(buf);
1849 return r;
1850 }
1851 /* Sends [size][minijail]. */
1852 written = write(fd, &sz, sizeof(sz));
1853 if (written != sizeof(sz)) {
1854 free(buf);
1855 return -EFAULT;
1856 }
1857 written = write(fd, buf, sz);
1858 if (written < 0 || (size_t) written != sz) {
1859 free(buf);
1860 return -EFAULT;
1861 }
1862 free(buf);
1863 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001864}
Elly Jonescd7a9042011-07-22 13:56:51 -04001865
Will Drewry6ac91122011-10-21 16:38:58 -05001866int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001867{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001868#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001869 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001870 return 0;
1871#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001872 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1873 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1874 if (!newenv)
1875 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001876
Elly Jonese1749eb2011-10-07 13:54:59 -04001877 /* Only insert a separating space if we have something to separate... */
1878 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1879 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001880
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001881 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001882 setenv(kLdPreloadEnvVar, newenv, 1);
1883 free(newenv);
1884 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001885#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001886}
1887
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001888static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001889{
1890 int r = pipe(fds);
1891 char fd_buf[11];
1892 if (r)
1893 return r;
1894 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1895 if (r <= 0)
1896 return -EINVAL;
1897 setenv(kFdEnvVar, fd_buf, 1);
1898 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001899}
1900
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001901static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07001902{
1903 const char *kFdPath = "/proc/self/fd";
1904
1905 DIR *d = opendir(kFdPath);
1906 struct dirent *dir_entry;
1907
1908 if (d == NULL)
1909 return -1;
1910 int dir_fd = dirfd(d);
1911 while ((dir_entry = readdir(d)) != NULL) {
1912 size_t i;
1913 char *end;
1914 bool should_close = true;
1915 const int fd = strtol(dir_entry->d_name, &end, 10);
1916
1917 if ((*end) != '\0') {
1918 continue;
1919 }
1920 /*
1921 * We might have set up some pipes that we want to share with
1922 * the parent process, and should not be closed.
1923 */
1924 for (i = 0; i < size; ++i) {
1925 if (fd == inheritable_fds[i]) {
1926 should_close = false;
1927 break;
1928 }
1929 }
1930 /* Also avoid closing the directory fd. */
1931 if (should_close && fd != dir_fd)
1932 close(fd);
1933 }
1934 closedir(d);
1935 return 0;
1936}
1937
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001938int minijail_run_internal(struct minijail *j, const char *filename,
1939 char *const argv[], pid_t *pchild_pid,
1940 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1941 int use_preload);
1942
Will Drewry6ac91122011-10-21 16:38:58 -05001943int API minijail_run(struct minijail *j, const char *filename,
1944 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001945{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001946 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1947 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001948}
1949
1950int API minijail_run_pid(struct minijail *j, const char *filename,
1951 char *const argv[], pid_t *pchild_pid)
1952{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001953 return minijail_run_internal(j, filename, argv, pchild_pid,
1954 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001955}
1956
1957int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001958 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001959{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001960 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1961 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001962}
1963
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001964int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001965 char *const argv[], pid_t *pchild_pid,
1966 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001967{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001968 return minijail_run_internal(j, filename, argv, pchild_pid,
1969 pstdin_fd, pstdout_fd, pstderr_fd, true);
1970}
1971
1972int API minijail_run_no_preload(struct minijail *j, const char *filename,
1973 char *const argv[])
1974{
1975 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1976 false);
1977}
1978
Samuel Tan63187f42015-10-16 13:01:53 -07001979int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001980 const char *filename,
1981 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001982 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001983 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001984 int *pstderr_fd)
1985{
Samuel Tan63187f42015-10-16 13:01:53 -07001986 return minijail_run_internal(j, filename, argv, pchild_pid,
1987 pstdin_fd, pstdout_fd, pstderr_fd, false);
1988}
1989
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001990int minijail_run_internal(struct minijail *j, const char *filename,
1991 char *const argv[], pid_t *pchild_pid,
1992 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1993 int use_preload)
1994{
Elly Jonese1749eb2011-10-07 13:54:59 -04001995 char *oldenv, *oldenv_copy = NULL;
1996 pid_t child_pid;
1997 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001998 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001999 int stdout_fds[2];
2000 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08002001 int child_sync_pipe_fds[2];
2002 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002003 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002004 /* We need to remember this across the minijail_preexec() call. */
2005 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002006 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07002007
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002008 if (use_preload) {
2009 oldenv = getenv(kLdPreloadEnvVar);
2010 if (oldenv) {
2011 oldenv_copy = strdup(oldenv);
2012 if (!oldenv_copy)
2013 return -ENOMEM;
2014 }
2015
2016 if (setup_preload())
2017 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002018 }
Will Drewryf89aef52011-09-16 16:48:57 -05002019
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002020 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002021 if (j->flags.use_caps && j->caps != 0 &&
2022 !j->flags.set_ambient_caps) {
2023 die("non-empty, non-ambient capabilities are not "
2024 "supported without LD_PRELOAD");
2025 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002026 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002027
Elly Jonesdd3e8512012-01-23 15:13:38 -05002028 /*
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002029 * Make the process group ID of this process equal to its PID.
2030 * In the non-interactive case (e.g. when the parent process is started
2031 * from init) this ensures the parent process and the jailed process
2032 * can be killed together.
2033 * When the parent process is started from the console this ensures
2034 * the call to setsid(2) in the jailed process succeeds.
2035 *
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002036 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
2037 * the process is already a process group leader.
2038 */
2039 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
2040 if (errno != EPERM) {
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002041 pdie("setpgid(0, 0) failed");
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002042 }
2043 }
2044
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002045 if (use_preload) {
2046 /*
2047 * Before we fork(2) and execve(2) the child process, we need
2048 * to open a pipe(2) to send the minijail configuration over.
2049 */
2050 if (setup_pipe(pipe_fds))
2051 return -EFAULT;
2052 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002053
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002054 /*
2055 * If we want to write to the child process' standard input,
2056 * create the pipe(2) now.
2057 */
2058 if (pstdin_fd) {
2059 if (pipe(stdin_fds))
2060 return -EFAULT;
2061 }
2062
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002063 /*
2064 * If we want to read from the child process' standard output,
2065 * create the pipe(2) now.
2066 */
2067 if (pstdout_fd) {
2068 if (pipe(stdout_fds))
2069 return -EFAULT;
2070 }
2071
2072 /*
2073 * If we want to read from the child process' standard error,
2074 * create the pipe(2) now.
2075 */
2076 if (pstderr_fd) {
2077 if (pipe(stderr_fds))
2078 return -EFAULT;
2079 }
2080
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002081 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002082 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002083 * or if we need to add the child process to cgroups, create the pipe(2)
2084 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002085 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002086 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002087 sync_child = 1;
2088 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002089 return -EFAULT;
2090 }
2091
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002092 /*
2093 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002094 *
2095 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2096 *
2097 * In multithreaded programs, there are a bunch of locks inside libc,
2098 * some of which may be held by other threads at the time that we call
2099 * minijail_run_pid(). If we call fork(), glibc does its level best to
2100 * ensure that we hold all of these locks before it calls clone()
2101 * internally and drop them after clone() returns, but when we call
2102 * sys_clone(2) directly, all that gets bypassed and we end up with a
2103 * child address space where some of libc's important locks are held by
2104 * other threads (which did not get cloned, and hence will never release
2105 * those locks). This is okay so long as we call exec() immediately
2106 * after, but a bunch of seemingly-innocent libc functions like setenv()
2107 * take locks.
2108 *
2109 * Hence, only call sys_clone() if we need to, in order to get at pid
2110 * namespacing. If we follow this path, the child's address space might
2111 * have broken locks; you may only call functions that do not acquire
2112 * any locks.
2113 *
2114 * Unfortunately, fork() acquires every lock it can get its hands on, as
2115 * previously detailed, so this function is highly likely to deadlock
2116 * later on (see "deadlock here") if we're multithreaded.
2117 *
2118 * We might hack around this by having the clone()d child (init of the
2119 * pid namespace) return directly, rather than leaving the clone()d
2120 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002121 * its fork()ed child return in turn), but that process would be
2122 * crippled with its libc locks potentially broken. We might try
2123 * fork()ing in the parent before we clone() to ensure that we own all
2124 * the locks, but then we have to have the forked child hanging around
2125 * consuming resources (and possibly having file descriptors / shared
2126 * memory regions / etc attached). We'd need to keep the child around to
2127 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002128 *
2129 * TODO(ellyjones): figure out if the "forked child hanging around"
2130 * problem is fixable or not. It would be nice if we worked in this
2131 * case.
2132 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002133 if (pid_namespace) {
2134 int clone_flags = CLONE_NEWPID | SIGCHLD;
2135 if (j->flags.userns)
2136 clone_flags |= CLONE_NEWUSER;
2137 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002138 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002139 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002140 }
Elly Jones761b7412012-06-13 15:49:52 -04002141
Elly Jonese1749eb2011-10-07 13:54:59 -04002142 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002143 if (use_preload) {
2144 free(oldenv_copy);
2145 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002146 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002147 }
Will Drewryf89aef52011-09-16 16:48:57 -05002148
Elly Jonese1749eb2011-10-07 13:54:59 -04002149 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002150 if (use_preload) {
2151 /* Restore parent's LD_PRELOAD. */
2152 if (oldenv_copy) {
2153 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2154 free(oldenv_copy);
2155 } else {
2156 unsetenv(kLdPreloadEnvVar);
2157 }
2158 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002159 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002160
Elly Jonese1749eb2011-10-07 13:54:59 -04002161 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002162
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002163 if (j->flags.forward_signals) {
2164 forward_pid = child_pid;
2165 install_signal_handlers();
2166 }
2167
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002168 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002169 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002170
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002171 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002172 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002173
Dylan Reid0f72ef42017-06-06 15:42:49 -07002174 if (j->rlimit_count)
2175 set_rlimits_or_die(j);
2176
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002177 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002178 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002179
2180 if (sync_child)
2181 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002182
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002183 if (use_preload) {
2184 /* Send marshalled minijail. */
2185 close(pipe_fds[0]); /* read endpoint */
2186 ret = minijail_to_fd(j, pipe_fds[1]);
2187 close(pipe_fds[1]); /* write endpoint */
2188 if (ret) {
2189 kill(j->initpid, SIGKILL);
2190 die("failed to send marshalled minijail");
2191 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002192 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002193
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002194 if (pchild_pid)
2195 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002196
2197 /*
2198 * If we want to write to the child process' standard input,
2199 * set up the write end of the pipe.
2200 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002201 if (pstdin_fd)
2202 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002203 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002204
2205 /*
2206 * If we want to read from the child process' standard output,
2207 * set up the read end of the pipe.
2208 */
2209 if (pstdout_fd)
2210 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002211 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002212
2213 /*
2214 * If we want to read from the child process' standard error,
2215 * set up the read end of the pipe.
2216 */
2217 if (pstderr_fd)
2218 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002219 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002220
Elly Jonese1749eb2011-10-07 13:54:59 -04002221 return 0;
2222 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002223 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002224 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002225
Peter Qiu2860c462015-12-16 15:13:06 -08002226 if (j->flags.reset_signal_mask) {
2227 sigset_t signal_mask;
2228 if (sigemptyset(&signal_mask) != 0)
2229 pdie("sigemptyset failed");
2230 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2231 pdie("sigprocmask failed");
2232 }
2233
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002234 if (j->flags.close_open_fds) {
2235 const size_t kMaxInheritableFdsSize = 10;
2236 int inheritable_fds[kMaxInheritableFdsSize];
2237 size_t size = 0;
2238 if (use_preload) {
2239 inheritable_fds[size++] = pipe_fds[0];
2240 inheritable_fds[size++] = pipe_fds[1];
2241 }
2242 if (sync_child) {
2243 inheritable_fds[size++] = child_sync_pipe_fds[0];
2244 inheritable_fds[size++] = child_sync_pipe_fds[1];
2245 }
2246 if (pstdin_fd) {
2247 inheritable_fds[size++] = stdin_fds[0];
2248 inheritable_fds[size++] = stdin_fds[1];
2249 }
2250 if (pstdout_fd) {
2251 inheritable_fds[size++] = stdout_fds[0];
2252 inheritable_fds[size++] = stdout_fds[1];
2253 }
2254 if (pstderr_fd) {
2255 inheritable_fds[size++] = stderr_fds[0];
2256 inheritable_fds[size++] = stderr_fds[1];
2257 }
2258
2259 if (close_open_fds(inheritable_fds, size) < 0)
2260 die("failed to close open file descriptors");
2261 }
2262
Dylan Reidce5b55e2016-01-13 11:04:16 -08002263 if (sync_child)
2264 wait_for_parent_setup(child_sync_pipe_fds);
2265
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002266 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002267 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002268
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002269 /*
2270 * If we want to write to the jailed process' standard input,
2271 * set up the read end of the pipe.
2272 */
2273 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002274 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2275 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002276 die("failed to set up stdin pipe");
2277 }
2278
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002279 /*
2280 * If we want to read from the jailed process' standard output,
2281 * set up the write end of the pipe.
2282 */
2283 if (pstdout_fd) {
2284 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2285 STDOUT_FILENO) < 0)
2286 die("failed to set up stdout pipe");
2287 }
2288
2289 /*
2290 * If we want to read from the jailed process' standard error,
2291 * set up the write end of the pipe.
2292 */
2293 if (pstderr_fd) {
2294 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2295 STDERR_FILENO) < 0)
2296 die("failed to set up stderr pipe");
2297 }
2298
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002299 /*
2300 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2301 * This prevents the jailed process from using the TIOCSTI ioctl
2302 * to push characters into the parent process terminal's input buffer,
2303 * therefore escaping the jail.
2304 */
2305 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2306 isatty(STDERR_FILENO)) {
2307 if (setsid() < 0) {
2308 pdie("setsid() failed");
2309 }
2310 }
2311
Dylan Reid791f5772015-09-14 20:02:42 -07002312 /* If running an init program, let it decide when/how to mount /proc. */
2313 if (pid_namespace && !do_init)
2314 j->flags.remount_proc_ro = 0;
2315
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002316 if (use_preload) {
2317 /* Strip out flags that cannot be inherited across execve(2). */
2318 minijail_preexec(j);
2319 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002320 /*
2321 * If not using LD_PRELOAD, do all jailing before execve(2).
2322 * Note that PID namespaces can only be entered on fork(2),
2323 * so that flag is still cleared.
2324 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002325 j->flags.pids = 0;
2326 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002327 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002328 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002329
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002330 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002331 /*
2332 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002333 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002334 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002335 * a child to actually run the program. If |do_init == 0|, we
2336 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002337 *
2338 * If we're multithreaded, we'll probably deadlock here. See
2339 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002340 */
2341 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002342 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002343 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002344 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002345 /*
2346 * Best effort. Don't bother checking the return value.
2347 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002348 prctl(PR_SET_NAME, "minijail-init");
2349 init(child_pid); /* Never returns. */
2350 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002351 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002352
Elly Jonesdd3e8512012-01-23 15:13:38 -05002353 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002354 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002355 * calling process
2356 * -> execve()-ing process
2357 * If we are:
2358 * calling process
2359 * -> init()-ing process
2360 * -> execve()-ing process
2361 */
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002362 ret = execve(filename, argv, environ);
2363 if (ret == -1) {
2364 pwarn("execve(%s) failed", filename);
2365 }
2366 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002367}
2368
Will Drewry6ac91122011-10-21 16:38:58 -05002369int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002370{
2371 int st;
2372 if (kill(j->initpid, SIGTERM))
2373 return -errno;
2374 if (waitpid(j->initpid, &st, 0) < 0)
2375 return -errno;
2376 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002377}
2378
Will Drewry6ac91122011-10-21 16:38:58 -05002379int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002380{
2381 int st;
2382 if (waitpid(j->initpid, &st, 0) < 0)
2383 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002384
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002385 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002386 int error_status = st;
2387 if (WIFSIGNALED(st)) {
2388 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002389 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002390 j->initpid, signum);
2391 /*
2392 * We return MINIJAIL_ERR_JAIL if the process received
2393 * SIGSYS, which happens when a syscall is blocked by
2394 * seccomp filters.
2395 * If not, we do what bash(1) does:
2396 * $? = 128 + signum
2397 */
2398 if (signum == SIGSYS) {
2399 error_status = MINIJAIL_ERR_JAIL;
2400 } else {
2401 error_status = 128 + signum;
2402 }
2403 }
2404 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002405 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002406
2407 int exit_status = WEXITSTATUS(st);
2408 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002409 info("child process %d exited with status %d",
2410 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002411
2412 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002413}
2414
Will Drewry6ac91122011-10-21 16:38:58 -05002415void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002416{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002417 size_t i;
2418
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002419 if (j->flags.seccomp_filter && j->filter_prog) {
2420 free(j->filter_prog->filter);
2421 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002422 }
Dylan Reid648b2202015-10-23 00:50:00 -07002423 while (j->mounts_head) {
2424 struct mountpoint *m = j->mounts_head;
2425 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002426 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002427 free(m->type);
2428 free(m->dest);
2429 free(m->src);
2430 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002431 }
Dylan Reid648b2202015-10-23 00:50:00 -07002432 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002433 if (j->user)
2434 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002435 if (j->suppl_gid_list)
2436 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002437 if (j->chrootdir)
2438 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002439 if (j->pid_file_path)
2440 free(j->pid_file_path);
2441 if (j->uidmap)
2442 free(j->uidmap);
2443 if (j->gidmap)
2444 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002445 if (j->hostname)
2446 free(j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08002447 if (j->alt_syscall_table)
2448 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002449 for (i = 0; i < j->cgroup_count; ++i)
2450 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002451 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002452}