blob: 1da4e19a29f4b849aa082dc7ad4fc8962011fc99 [file] [log] [blame]
Jorge Lucangeli Obesd613ab22015-03-03 14:22:50 -08001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04002 * Use of this source code is governed by a BSD-style license that can be
Will Drewry32ac9f52011-08-18 21:36:27 -05003 * found in the LICENSE file.
4 */
Elly Jonescd7a9042011-07-22 13:56:51 -04005
6#define _BSD_SOURCE
Arthur Gautier7a569072016-04-23 17:25:20 +00007#define _DEFAULT_SOURCE
Elly Jonescd7a9042011-07-22 13:56:51 -04008#define _GNU_SOURCE
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07009
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080010#include <asm/unistd.h>
Luis Hector Chavez43ff0802016-10-07 12:21:07 -070011#include <dirent.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040012#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070013#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040014#include <grp.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040015#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040016#include <pwd.h>
17#include <sched.h>
18#include <signal.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070019#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080020#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040024#include <sys/capability.h>
25#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050026#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040027#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070028#include <sys/stat.h>
29#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080030#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040031#include <sys/wait.h>
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -070032#include <syscall.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040033#include <unistd.h>
34
35#include "libminijail.h"
36#include "libminijail-private.h"
37
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070038#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080039#include "syscall_filter.h"
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040040#include "syscall_wrapper.h"
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040041#include "system.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070042#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080043
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070044/* Until these are reliably available in linux/prctl.h. */
Andrew Brestickereac28942015-11-11 16:04:46 -080045#ifndef PR_ALT_SYSCALL
46# define PR_ALT_SYSCALL 0x43724f53
47#endif
48
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040049/* Seccomp filter related flags. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080050#ifndef PR_SET_NO_NEW_PRIVS
51# define PR_SET_NO_NEW_PRIVS 38
52#endif
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040053
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080054#ifndef SECCOMP_MODE_FILTER
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040055#define SECCOMP_MODE_FILTER 2 /* Uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050056#endif
57
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040058#ifndef SECCOMP_SET_MODE_STRICT
59# define SECCOMP_SET_MODE_STRICT 0
60#endif
61#ifndef SECCOMP_SET_MODE_FILTER
62# define SECCOMP_SET_MODE_FILTER 1
63#endif
64
65#ifndef SECCOMP_FILTER_FLAG_TSYNC
66# define SECCOMP_FILTER_FLAG_TSYNC 1
67#endif
68/* End seccomp filter related flags. */
69
Dylan Reid4cbc2a52016-06-17 19:06:07 -070070/* New cgroup namespace might not be in linux-headers yet. */
71#ifndef CLONE_NEWCGROUP
72# define CLONE_NEWCGROUP 0x02000000
73#endif
74
Dylan Reid605ce7f2016-01-19 19:21:00 -080075#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
76
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -080077/* Keyctl commands. */
78#define KEYCTL_JOIN_SESSION_KEYRING 1
79
Dylan Reid648b2202015-10-23 00:50:00 -070080struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040081 char *src;
82 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070083 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -070084 char *data;
85 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -070086 unsigned long flags;
87 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040088};
89
Will Drewryf89aef52011-09-16 16:48:57 -050090struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070091 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070092 * WARNING: if you add a flag here you need to make sure it's
93 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070094 */
Elly Jonese1749eb2011-10-07 13:54:59 -040095 struct {
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -070096 int uid : 1;
97 int gid : 1;
Lutz Justen13807cb2017-01-03 17:11:55 +010098 int inherit_suppl_gids : 1;
99 int set_suppl_gids : 1;
100 int keep_suppl_gids : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700101 int use_caps : 1;
102 int capbset_drop : 1;
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400103 int set_ambient_caps : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700104 int vfs : 1;
105 int enter_vfs : 1;
106 int skip_remount_private : 1;
107 int pids : 1;
108 int ipc : 1;
109 int net : 1;
110 int enter_net : 1;
111 int ns_cgroups : 1;
112 int userns : 1;
113 int disable_setgroups : 1;
114 int seccomp : 1;
115 int remount_proc_ro : 1;
116 int no_new_privs : 1;
117 int seccomp_filter : 1;
118 int seccomp_filter_tsync : 1;
119 int seccomp_filter_logging : 1;
120 int chroot : 1;
121 int pivot_root : 1;
122 int mount_tmp : 1;
123 int do_init : 1;
124 int pid_file : 1;
125 int cgroups : 1;
126 int alt_syscall : 1;
127 int reset_signal_mask : 1;
128 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800129 int new_session_keyring : 1;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400130 int forward_signals : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400131 } flags;
132 uid_t uid;
133 gid_t gid;
134 gid_t usergid;
135 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800136 size_t suppl_gid_count;
137 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400138 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800139 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400140 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700141 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700142 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400143 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800144 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800145 char *uidmap;
146 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800147 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800148 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800149 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700150 struct mountpoint *mounts_head;
151 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800152 size_t mounts_count;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100153 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800154 char *cgroups[MAX_CGROUPS];
155 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500156};
157
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700158/*
159 * Strip out flags meant for the parent.
160 * We keep things that are not inherited across execve(2) (e.g. capabilities),
161 * or are easier to set after execve(2) (e.g. seccomp filters).
162 */
163void minijail_preenter(struct minijail *j)
164{
165 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700166 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900167 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700168 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700169 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800170 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800171 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800172 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400173 j->flags.forward_signals = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700174}
175
176/*
177 * Strip out flags meant for the child.
178 * We keep things that are inherited across execve(2).
179 */
180void minijail_preexec(struct minijail *j)
181{
182 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700183 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800184 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700185 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800186 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700187 if (j->user)
188 free(j->user);
189 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800190 if (j->suppl_gid_list)
191 free(j->suppl_gid_list);
192 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700193 memset(&j->flags, 0, sizeof(j->flags));
194 /* Now restore anything we meant to keep. */
195 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700196 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800197 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700198 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800199 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700200 /* Note, |pids| will already have been used before this call. */
201}
202
203/* Minijail API. */
204
Will Drewry6ac91122011-10-21 16:38:58 -0500205struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400206{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400207 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400208}
209
Will Drewry6ac91122011-10-21 16:38:58 -0500210void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400211{
212 if (uid == 0)
213 die("useless change to uid 0");
214 j->uid = uid;
215 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400216}
217
Will Drewry6ac91122011-10-21 16:38:58 -0500218void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400219{
220 if (gid == 0)
221 die("useless change to gid 0");
222 j->gid = gid;
223 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400224}
225
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800226void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
227 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800228{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800229 size_t i;
230
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500231 if (j->flags.inherit_suppl_gids)
232 die("cannot inherit *and* set supplementary groups");
233 if (j->flags.keep_suppl_gids)
234 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800235
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800236 if (size == 0) {
237 /* Clear supplementary groups. */
238 j->suppl_gid_list = NULL;
239 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100240 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800241 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800242 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800243
244 /* Copy the gid_t array. */
245 j->suppl_gid_list = calloc(size, sizeof(gid_t));
246 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800247 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800248 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800249 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800250 j->suppl_gid_list[i] = list[i];
251 }
252 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100253 j->flags.set_suppl_gids = 1;
254}
255
256void API minijail_keep_supplementary_gids(struct minijail *j) {
257 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800258}
259
Will Drewry6ac91122011-10-21 16:38:58 -0500260int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400261{
262 char *buf = NULL;
263 struct passwd pw;
264 struct passwd *ppw = NULL;
265 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
266 if (sz == -1)
267 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400268
Elly Jonesdd3e8512012-01-23 15:13:38 -0500269 /*
270 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400271 * the maximum needed size of the buffer, so we don't have to search.
272 */
273 buf = malloc(sz);
274 if (!buf)
275 return -ENOMEM;
276 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500277 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800278 * We're safe to free the buffer here. The strings inside |pw| point
279 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800280 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
281 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500282 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400283 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700284 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400285 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700286 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400287 minijail_change_uid(j, ppw->pw_uid);
288 j->user = strdup(user);
289 if (!j->user)
290 return -ENOMEM;
291 j->usergid = ppw->pw_gid;
292 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400293}
294
Will Drewry6ac91122011-10-21 16:38:58 -0500295int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400296{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700297 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700298 struct group gr;
299 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400300 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
301 if (sz == -1)
302 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400303
Elly Jonesdd3e8512012-01-23 15:13:38 -0500304 /*
305 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400306 * the maximum needed size of the buffer, so we don't have to search.
307 */
308 buf = malloc(sz);
309 if (!buf)
310 return -ENOMEM;
311 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500312 /*
313 * We're safe to free the buffer here. The strings inside gr point
314 * inside buf, but we don't use any of them; this leaves the pointers
315 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
316 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400317 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700318 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400319 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700320 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400321 minijail_change_gid(j, pgr->gr_gid);
322 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400323}
324
Will Drewry6ac91122011-10-21 16:38:58 -0500325void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400326{
327 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400328}
329
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700330void API minijail_no_new_privs(struct minijail *j)
331{
332 j->flags.no_new_privs = 1;
333}
334
Will Drewry6ac91122011-10-21 16:38:58 -0500335void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400336{
337 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500338}
339
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400340void API minijail_set_seccomp_filter_tsync(struct minijail *j)
341{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400342 if (j->filter_len > 0 && j->filter_prog != NULL) {
343 die("minijail_set_seccomp_filter_tsync() must be called "
344 "before minijail_parse_seccomp_filters()");
345 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400346 j->flags.seccomp_filter_tsync = 1;
347}
348
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700349void API minijail_log_seccomp_filter_failures(struct minijail *j)
350{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400351 if (j->filter_len > 0 && j->filter_prog != NULL) {
352 die("minijail_log_seccomp_filter_failures() must be called "
353 "before minijail_parse_seccomp_filters()");
354 }
355 j->flags.seccomp_filter_logging = 1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700356}
357
Will Drewry6ac91122011-10-21 16:38:58 -0500358void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400359{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800360 /*
361 * 'minijail_use_caps' configures a runtime-capabilities-only
362 * environment, including a bounding set matching the thread's runtime
363 * (permitted|inheritable|effective) sets.
364 * Therefore, it will override any existing bounding set configurations
365 * since the latter would allow gaining extra runtime capabilities from
366 * file capabilities.
367 */
368 if (j->flags.capbset_drop) {
369 warn("overriding bounding set configuration");
370 j->cap_bset = 0;
371 j->flags.capbset_drop = 0;
372 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400373 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800374 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400375}
376
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800377void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
378{
379 if (j->flags.use_caps) {
380 /*
381 * 'minijail_use_caps' will have already configured a capability
382 * bounding set matching the (permitted|inheritable|effective)
383 * sets. Abort if the user tries to configure a separate
384 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
385 * are mutually exclusive.
386 */
387 die("runtime capabilities already configured, can't drop "
388 "bounding set separately");
389 }
390 j->cap_bset = capmask;
391 j->flags.capbset_drop = 1;
392}
393
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400394void API minijail_set_ambient_caps(struct minijail *j)
395{
396 j->flags.set_ambient_caps = 1;
397}
398
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800399void API minijail_reset_signal_mask(struct minijail *j)
400{
Peter Qiu2860c462015-12-16 15:13:06 -0800401 j->flags.reset_signal_mask = 1;
402}
403
Will Drewry6ac91122011-10-21 16:38:58 -0500404void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400405{
406 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400407}
408
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700409void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
410{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800411 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700412 if (ns_fd < 0) {
413 pdie("failed to open namespace '%s'", ns_path);
414 }
415 j->mountns_fd = ns_fd;
416 j->flags.enter_vfs = 1;
417}
418
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800419void API minijail_new_session_keyring(struct minijail *j)
420{
421 j->flags.new_session_keyring = 1;
422}
423
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800424void API minijail_skip_remount_private(struct minijail *j)
425{
426 j->flags.skip_remount_private = 1;
427}
428
Will Drewry6ac91122011-10-21 16:38:58 -0500429void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400430{
Elly Jonese58176c2012-01-23 11:46:17 -0500431 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700432 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400433 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800434 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400435}
436
Dylan Reidf7942472015-11-18 17:55:26 -0800437void API minijail_namespace_ipc(struct minijail *j)
438{
439 j->flags.ipc = 1;
440}
441
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400442void API minijail_namespace_net(struct minijail *j)
443{
444 j->flags.net = 1;
445}
446
Dylan Reid1102f5a2015-09-15 11:52:20 -0700447void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
448{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800449 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700450 if (ns_fd < 0) {
451 pdie("failed to open namespace '%s'", ns_path);
452 }
453 j->netns_fd = ns_fd;
454 j->flags.enter_net = 1;
455}
456
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700457void API minijail_namespace_cgroups(struct minijail *j)
458{
459 j->flags.ns_cgroups = 1;
460}
461
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700462void API minijail_close_open_fds(struct minijail *j)
463{
464 j->flags.close_open_fds = 1;
465}
466
Dylan Reid791f5772015-09-14 20:02:42 -0700467void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400468{
469 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700470 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400471}
472
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800473void API minijail_namespace_user(struct minijail *j)
474{
475 j->flags.userns = 1;
476}
477
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400478void API minijail_namespace_user_disable_setgroups(struct minijail *j)
479{
480 j->flags.disable_setgroups = 1;
481}
482
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800483int API minijail_uidmap(struct minijail *j, const char *uidmap)
484{
485 j->uidmap = strdup(uidmap);
486 if (!j->uidmap)
487 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800488 char *ch;
489 for (ch = j->uidmap; *ch; ch++) {
490 if (*ch == ',')
491 *ch = '\n';
492 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800493 return 0;
494}
495
496int API minijail_gidmap(struct minijail *j, const char *gidmap)
497{
498 j->gidmap = strdup(gidmap);
499 if (!j->gidmap)
500 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800501 char *ch;
502 for (ch = j->gidmap; *ch; ch++) {
503 if (*ch == ',')
504 *ch = '\n';
505 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800506 return 0;
507}
508
Will Drewry6ac91122011-10-21 16:38:58 -0500509void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400510{
Lutz Justen13807cb2017-01-03 17:11:55 +0100511 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400512}
513
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800514void API minijail_run_as_init(struct minijail *j)
515{
516 /*
517 * Since the jailed program will become 'init' in the new PID namespace,
518 * Minijail does not need to fork an 'init' process.
519 */
520 j->flags.do_init = 0;
521}
522
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700523int API minijail_enter_chroot(struct minijail *j, const char *dir)
524{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400525 if (j->chrootdir)
526 return -EINVAL;
527 j->chrootdir = strdup(dir);
528 if (!j->chrootdir)
529 return -ENOMEM;
530 j->flags.chroot = 1;
531 return 0;
532}
533
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800534int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
535{
536 if (j->chrootdir)
537 return -EINVAL;
538 j->chrootdir = strdup(dir);
539 if (!j->chrootdir)
540 return -ENOMEM;
541 j->flags.pivot_root = 1;
542 return 0;
543}
544
Dylan Reida14e08d2015-10-22 21:05:29 -0700545char API *minijail_get_original_path(struct minijail *j,
546 const char *path_inside_chroot)
547{
Dylan Reid648b2202015-10-23 00:50:00 -0700548 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700549
Dylan Reid648b2202015-10-23 00:50:00 -0700550 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700551 while (b) {
552 /*
553 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700554 * mount, then the original path is exactly the source of
555 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700556 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700557 * mount source = /some/path/exe, mount dest =
558 * /chroot/path/exe Then when getting the original path of
559 * "/chroot/path/exe", the source of that mount,
560 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700561 */
562 if (!strcmp(b->dest, path_inside_chroot))
563 return strdup(b->src);
564
565 /*
566 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700567 * mount, take the suffix of the chroot path relative to the
568 * mount destination path, and append it to the mount source
569 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700570 */
571 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
572 const char *relative_path =
573 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400574 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700575 }
576 b = b->next;
577 }
578
579 /* If there is a chroot path, append |path_inside_chroot| to that. */
580 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400581 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700582
583 /* No chroot, so the path outside is the same as it is inside. */
584 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700585}
586
Martin Pelikánab9eb442017-01-25 11:53:58 +1100587size_t minijail_get_tmpfs_size(const struct minijail *j)
588{
589 return j->tmpfs_size;
590}
591
Lee Campbell11af0622014-05-22 12:36:04 -0700592void API minijail_mount_tmp(struct minijail *j)
593{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100594 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
595}
596
597void API minijail_mount_tmp_size(struct minijail *j, size_t size)
598{
599 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700600 j->flags.mount_tmp = 1;
601}
602
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800603int API minijail_write_pid_file(struct minijail *j, const char *path)
604{
605 j->pid_file_path = strdup(path);
606 if (!j->pid_file_path)
607 return -ENOMEM;
608 j->flags.pid_file = 1;
609 return 0;
610}
611
Dylan Reid605ce7f2016-01-19 19:21:00 -0800612int API minijail_add_to_cgroup(struct minijail *j, const char *path)
613{
614 if (j->cgroup_count >= MAX_CGROUPS)
615 return -ENOMEM;
616 j->cgroups[j->cgroup_count] = strdup(path);
617 if (!j->cgroups[j->cgroup_count])
618 return -ENOMEM;
619 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800620 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800621 return 0;
622}
623
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400624int API minijail_forward_signals(struct minijail *j)
625{
626 j->flags.forward_signals = 1;
627 return 0;
628}
629
Dylan Reid81e23972016-05-18 14:06:35 -0700630int API minijail_mount_with_data(struct minijail *j, const char *src,
631 const char *dest, const char *type,
632 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700633{
Dylan Reid648b2202015-10-23 00:50:00 -0700634 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400635
636 if (*dest != '/')
637 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700638 m = calloc(1, sizeof(*m));
639 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400640 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700641 m->dest = strdup(dest);
642 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400643 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700644 m->src = strdup(src);
645 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400646 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700647 m->type = strdup(type);
648 if (!m->type)
649 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700650 if (data) {
651 m->data = strdup(data);
652 if (!m->data)
653 goto error;
654 m->has_data = 1;
655 }
Dylan Reid648b2202015-10-23 00:50:00 -0700656 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400657
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800658 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400659
Elly Jonesdd3e8512012-01-23 15:13:38 -0500660 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700661 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400662 * containing vfs namespace.
663 */
664 minijail_namespace_vfs(j);
665
Dylan Reid648b2202015-10-23 00:50:00 -0700666 if (j->mounts_tail)
667 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400668 else
Dylan Reid648b2202015-10-23 00:50:00 -0700669 j->mounts_head = m;
670 j->mounts_tail = m;
671 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400672
673 return 0;
674
675error:
Dylan Reid81e23972016-05-18 14:06:35 -0700676 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700677 free(m->src);
678 free(m->dest);
679 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400680 return -ENOMEM;
681}
682
Dylan Reid81e23972016-05-18 14:06:35 -0700683int API minijail_mount(struct minijail *j, const char *src, const char *dest,
684 const char *type, unsigned long flags)
685{
686 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
687}
688
Dylan Reid648b2202015-10-23 00:50:00 -0700689int API minijail_bind(struct minijail *j, const char *src, const char *dest,
690 int writeable)
691{
692 unsigned long flags = MS_BIND;
693
694 if (!writeable)
695 flags |= MS_RDONLY;
696
697 return minijail_mount(j, src, dest, "", flags);
698}
699
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400700static void clear_seccomp_options(struct minijail *j)
701{
702 j->flags.seccomp_filter = 0;
703 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400704 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400705 j->filter_len = 0;
706 j->filter_prog = NULL;
707 j->flags.no_new_privs = 0;
708}
709
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400710static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400711{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400712 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400713 /*
714 * |errno| will be set to EINVAL when seccomp has not been
715 * compiled into the kernel. On certain platforms and kernel
716 * versions this is not a fatal failure. In that case, and only
717 * in that case, disable seccomp and skip loading the filters.
718 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400719 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400720 warn("not loading seccomp filters, seccomp filter not "
721 "supported");
722 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400723 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700724 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400725 /*
726 * If |errno| != EINVAL or seccomp_can_softfail() is false,
727 * we can proceed. Worst case scenario minijail_enter() will
728 * abort() if seccomp fails.
729 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700730 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400731 if (j->flags.seccomp_filter_tsync) {
732 /* Are the seccomp(2) syscall and the TSYNC option supported? */
733 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
734 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
735 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400736 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
737 warn("seccomp(2) syscall not supported");
738 clear_seccomp_options(j);
739 return 0;
740 } else if (saved_errno == EINVAL &&
741 seccomp_can_softfail()) {
742 warn(
743 "seccomp filter thread sync not supported");
744 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400745 return 0;
746 }
747 /*
748 * Similar logic here. If seccomp_can_softfail() is
749 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
750 * we can proceed. Worst case scenario minijail_enter()
751 * will abort() if seccomp or TSYNC fail.
752 */
753 }
754 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400755 return 1;
756}
757
758static int parse_seccomp_filters(struct minijail *j, FILE *policy_file)
759{
760 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400761 int use_ret_trap =
762 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
763 int allow_logging = j->flags.seccomp_filter_logging;
764
765 if (compile_filter(policy_file, fprog, use_ret_trap, allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400766 free(fprog);
767 return -1;
768 }
769
770 j->filter_len = fprog->len;
771 j->filter_prog = fprog;
772 return 0;
773}
774
775void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
776{
777 if (!seccomp_should_parse_filters(j))
778 return;
779
Elly Jonese1749eb2011-10-07 13:54:59 -0400780 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800781 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700782 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400783 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800784
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400785 if (parse_seccomp_filters(j, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700786 die("failed to compile seccomp filter BPF program in '%s'",
787 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800788 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400789 fclose(file);
790}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800791
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400792void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
793{
794 if (!seccomp_should_parse_filters(j))
795 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800796
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400797 FILE *file = fdopen(fd, "r");
798 if (!file) {
799 pdie("failed to associate stream with fd %d", fd);
800 }
801
802 if (parse_seccomp_filters(j, file) != 0) {
803 die("failed to compile seccomp filter BPF program from fd %d",
804 fd);
805 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400806 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500807}
808
Andrew Brestickereac28942015-11-11 16:04:46 -0800809int API minijail_use_alt_syscall(struct minijail *j, const char *table)
810{
811 j->alt_syscall_table = strdup(table);
812 if (!j->alt_syscall_table)
813 return -ENOMEM;
814 j->flags.alt_syscall = 1;
815 return 0;
816}
817
Will Drewryf89aef52011-09-16 16:48:57 -0500818struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400819 size_t available;
820 size_t total;
821 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500822};
823
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800824void marshal_state_init(struct marshal_state *state, char *buf,
825 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400826{
827 state->available = available;
828 state->buf = buf;
829 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500830}
831
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800832void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400833{
834 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500835
Elly Jonese1749eb2011-10-07 13:54:59 -0400836 /* Up to |available| will be written. */
837 if (copy_len) {
838 memcpy(state->buf, src, copy_len);
839 state->buf += copy_len;
840 state->available -= copy_len;
841 }
842 /* |total| will contain the expected length. */
843 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500844}
845
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400846void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700847{
848 marshal_append(state, m->src, strlen(m->src) + 1);
849 marshal_append(state, m->dest, strlen(m->dest) + 1);
850 marshal_append(state, m->type, strlen(m->type) + 1);
851 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
852 if (m->has_data)
853 marshal_append(state, m->data, strlen(m->data) + 1);
854 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
855}
856
Will Drewry6ac91122011-10-21 16:38:58 -0500857void minijail_marshal_helper(struct marshal_state *state,
858 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400859{
Dylan Reid648b2202015-10-23 00:50:00 -0700860 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800861 size_t i;
862
Elly Jonese1749eb2011-10-07 13:54:59 -0400863 marshal_append(state, (char *)j, sizeof(*j));
864 if (j->user)
865 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800866 if (j->suppl_gid_list) {
867 marshal_append(state, j->suppl_gid_list,
868 j->suppl_gid_count * sizeof(gid_t));
869 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400870 if (j->chrootdir)
871 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800872 if (j->alt_syscall_table) {
873 marshal_append(state, j->alt_syscall_table,
874 strlen(j->alt_syscall_table) + 1);
875 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800876 if (j->flags.seccomp_filter && j->filter_prog) {
877 struct sock_fprog *fp = j->filter_prog;
878 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800879 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400880 }
Dylan Reid648b2202015-10-23 00:50:00 -0700881 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400882 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400883 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800884 for (i = 0; i < j->cgroup_count; ++i)
885 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500886}
887
Will Drewry6ac91122011-10-21 16:38:58 -0500888size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400889{
890 struct marshal_state state;
891 marshal_state_init(&state, NULL, 0);
892 minijail_marshal_helper(&state, j);
893 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500894}
895
Elly Jonese1749eb2011-10-07 13:54:59 -0400896int minijail_marshal(const struct minijail *j, char *buf, size_t available)
897{
898 struct marshal_state state;
899 marshal_state_init(&state, buf, available);
900 minijail_marshal_helper(&state, j);
901 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500902}
903
Elly Jonese1749eb2011-10-07 13:54:59 -0400904int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
905{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800906 size_t i;
907 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500908 int ret = -EINVAL;
909
Elly Jonese1749eb2011-10-07 13:54:59 -0400910 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500911 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400912 memcpy((void *)j, serialized, sizeof(*j));
913 serialized += sizeof(*j);
914 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500915
Will Drewrybee7ba72011-10-21 20:47:01 -0500916 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -0400917 j->pid_file_path = NULL;
918 j->uidmap = NULL;
919 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -0700920 j->mounts_head = NULL;
921 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800922 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500923
Elly Jonese1749eb2011-10-07 13:54:59 -0400924 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400925 char *user = consumestr(&serialized, &length);
926 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500927 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400928 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500929 if (!j->user)
930 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400931 }
Will Drewryf89aef52011-09-16 16:48:57 -0500932
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800933 if (j->suppl_gid_list) { /* stale pointer */
934 if (j->suppl_gid_count > NGROUPS_MAX) {
935 goto bad_gid_list;
936 }
937 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
938 void *gid_list_bytes =
939 consumebytes(gid_list_size, &serialized, &length);
940 if (!gid_list_bytes)
941 goto bad_gid_list;
942
943 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
944 if (!j->suppl_gid_list)
945 goto bad_gid_list;
946
947 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
948 }
949
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400950 if (j->chrootdir) { /* stale pointer */
951 char *chrootdir = consumestr(&serialized, &length);
952 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500953 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400954 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500955 if (!j->chrootdir)
956 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400957 }
958
Andrew Brestickereac28942015-11-11 16:04:46 -0800959 if (j->alt_syscall_table) { /* stale pointer */
960 char *alt_syscall_table = consumestr(&serialized, &length);
961 if (!alt_syscall_table)
962 goto bad_syscall_table;
963 j->alt_syscall_table = strdup(alt_syscall_table);
964 if (!j->alt_syscall_table)
965 goto bad_syscall_table;
966 }
967
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800968 if (j->flags.seccomp_filter && j->filter_len > 0) {
969 size_t ninstrs = j->filter_len;
970 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
971 ninstrs > USHRT_MAX)
972 goto bad_filters;
973
974 size_t program_len = ninstrs * sizeof(struct sock_filter);
975 void *program = consumebytes(program_len, &serialized, &length);
976 if (!program)
977 goto bad_filters;
978
979 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800980 if (!j->filter_prog)
981 goto bad_filters;
982
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800983 j->filter_prog->len = ninstrs;
984 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800985 if (!j->filter_prog->filter)
986 goto bad_filter_prog_instrs;
987
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800988 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400989 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400990
Dylan Reid648b2202015-10-23 00:50:00 -0700991 count = j->mounts_count;
992 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400993 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700994 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -0700995 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400996 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700997 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700998 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400999 const char *src = consumestr(&serialized, &length);
1000 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001001 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001002 dest = consumestr(&serialized, &length);
1003 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001004 goto bad_mounts;
1005 type = consumestr(&serialized, &length);
1006 if (!type)
1007 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001008 has_data = consumebytes(sizeof(*has_data), &serialized,
1009 &length);
1010 if (!has_data)
1011 goto bad_mounts;
1012 if (*has_data) {
1013 data = consumestr(&serialized, &length);
1014 if (!data)
1015 goto bad_mounts;
1016 }
Dylan Reid648b2202015-10-23 00:50:00 -07001017 flags = consumebytes(sizeof(*flags), &serialized, &length);
1018 if (!flags)
1019 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001020 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001021 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001022 }
1023
Dylan Reid605ce7f2016-01-19 19:21:00 -08001024 count = j->cgroup_count;
1025 j->cgroup_count = 0;
1026 for (i = 0; i < count; ++i) {
1027 char *cgroup = consumestr(&serialized, &length);
1028 if (!cgroup)
1029 goto bad_cgroups;
1030 j->cgroups[i] = strdup(cgroup);
1031 if (!j->cgroups[i])
1032 goto bad_cgroups;
1033 ++j->cgroup_count;
1034 }
1035
Elly Jonese1749eb2011-10-07 13:54:59 -04001036 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001037
Dylan Reid605ce7f2016-01-19 19:21:00 -08001038bad_cgroups:
1039 while (j->mounts_head) {
1040 struct mountpoint *m = j->mounts_head;
1041 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07001042 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001043 free(m->type);
1044 free(m->dest);
1045 free(m->src);
1046 free(m);
1047 }
1048 for (i = 0; i < j->cgroup_count; ++i)
1049 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001050bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001051 if (j->flags.seccomp_filter && j->filter_len > 0) {
1052 free(j->filter_prog->filter);
1053 free(j->filter_prog);
1054 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001055bad_filter_prog_instrs:
1056 if (j->filter_prog)
1057 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001058bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001059 if (j->alt_syscall_table)
1060 free(j->alt_syscall_table);
1061bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001062 if (j->chrootdir)
1063 free(j->chrootdir);
1064bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001065 if (j->suppl_gid_list)
1066 free(j->suppl_gid_list);
1067bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001068 if (j->user)
1069 free(j->user);
1070clear_pointers:
1071 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001072 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001073 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001074 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001075 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001076out:
1077 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001078}
1079
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001080/*
1081 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001082 * @j Minijail these mounts are for
1083 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001084 *
1085 * Returns 0 for success.
1086 */
Dylan Reid648b2202015-10-23 00:50:00 -07001087static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001088{
Dylan Reid648b2202015-10-23 00:50:00 -07001089 int ret;
1090 char *dest;
1091 int remount_ro = 0;
1092
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001093 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001094 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001095 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001096
Dylan Reideec77962016-06-30 19:35:10 -07001097 if (setup_mount_destination(m->src, dest, j->uid, j->gid))
1098 pdie("creating mount target '%s' failed", dest);
1099
Dylan Reid648b2202015-10-23 00:50:00 -07001100 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001101 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1102 * can't both be specified in the original bind mount.
1103 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001104 */
1105 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1106 remount_ro = 1;
1107 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001108 }
Dylan Reid648b2202015-10-23 00:50:00 -07001109
Dylan Reid81e23972016-05-18 14:06:35 -07001110 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001111 if (ret)
1112 pdie("mount: %s -> %s", m->src, dest);
1113
1114 if (remount_ro) {
1115 m->flags |= MS_RDONLY;
1116 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001117 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001118 if (ret)
1119 pdie("bind ro: %s -> %s", m->src, dest);
1120 }
1121
Elly Jones51a5b6c2011-10-12 19:09:26 -04001122 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001123 if (m->next)
1124 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001125 return ret;
1126}
1127
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001128static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001129{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001130 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001131
1132 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001133 return ret;
1134
1135 if (chroot(j->chrootdir))
1136 return -errno;
1137
1138 if (chdir("/"))
1139 return -errno;
1140
1141 return 0;
1142}
1143
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001144static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001145{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001146 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001147
1148 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001149 return ret;
1150
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001151 /*
1152 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001153 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001154 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001155 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001156 if (oldroot < 0)
1157 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001158 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001159 if (newroot < 0)
1160 pdie("failed to open %s for fchdir", j->chrootdir);
1161
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001162 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001163 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001164 * do a self bind mount.
1165 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001166 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1167 pdie("failed to bind mount '%s'", j->chrootdir);
1168 if (chdir(j->chrootdir))
1169 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001170 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001171 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001172
1173 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001174 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001175 * change to the old root and unmount it.
1176 */
1177 if (fchdir(oldroot))
1178 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001179
1180 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001181 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1182 * there could be a shared mount point under |oldroot|. In that case,
1183 * mounts under this shared mount point will be unmounted below, and
1184 * this unmounting will propagate to the original mount namespace
1185 * (because the mount point is shared). To prevent this unexpected
1186 * unmounting, remove these mounts from their peer groups by recursively
1187 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001188 */
1189 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001190 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001191 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001192 if (umount2(".", MNT_DETACH))
1193 pdie("umount(/)");
1194 /* Change back to the new root. */
1195 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001196 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001197 if (close(oldroot))
1198 return -errno;
1199 if (close(newroot))
1200 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001201 if (chroot("/"))
1202 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001203 /* Set correct CWD for getcwd(3). */
1204 if (chdir("/"))
1205 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001206
1207 return 0;
1208}
1209
Martin Pelikánab9eb442017-01-25 11:53:58 +11001210static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001211{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001212 const char fmt[] = "size=%zu,mode=1777";
1213 /* Count for the user storing ULLONG_MAX literally + extra space. */
1214 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1215 int ret;
1216
1217 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1218
1219 if (ret <= 0)
1220 pdie("tmpfs size spec error");
1221 else if ((size_t)ret >= sizeof(data))
1222 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001223 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001224 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001225}
1226
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001227static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001228{
1229 const char *kProcPath = "/proc";
1230 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001231 /*
1232 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001233 * /proc in our namespace, which means using MS_REMOUNT here would
1234 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001235 * namespace (!). Instead, remove their mount from our namespace lazily
1236 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001237 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001238 if (umount2(kProcPath, MNT_DETACH)) {
1239 /*
1240 * If we are in a new user namespace, umount(2) will fail.
1241 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1242 */
1243 if (j->flags.userns) {
1244 info("umount(/proc, MNT_DETACH) failed, "
1245 "this is expected when using user namespaces");
1246 } else {
1247 return -errno;
1248 }
1249 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001250 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001251 return -errno;
1252 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001253}
1254
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001255static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001256{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001257 kill(j->initpid, SIGKILL);
1258 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001259}
1260
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001261static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001262{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001263 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001264 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001265}
1266
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001267static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001268{
1269 size_t i;
1270
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001271 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001272 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001273 kill_child_and_die(j, "failed to add to cgroups");
1274 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001275}
1276
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001277static void write_ugid_maps_or_die(const struct minijail *j)
1278{
1279 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1280 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001281 if (j->gidmap && j->flags.disable_setgroups) {
1282 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1283 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001284 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001285 if (ret == -ENOENT) {
1286 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1287 warn("could not disable setgroups(2)");
1288 } else
1289 kill_child_and_die(j, "failed to disable setgroups(2)");
1290 }
1291 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001292 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1293 kill_child_and_die(j, "failed to write gid_map");
1294}
1295
1296static void enter_user_namespace(const struct minijail *j)
1297{
1298 if (j->uidmap && setresuid(0, 0, 0))
1299 pdie("user_namespaces: setresuid(0, 0, 0) failed");
1300 if (j->gidmap && setresgid(0, 0, 0))
1301 pdie("user_namespaces: setresgid(0, 0, 0) failed");
1302}
1303
1304static void parent_setup_complete(int *pipe_fds)
1305{
1306 close(pipe_fds[0]);
1307 close(pipe_fds[1]);
1308}
1309
1310/*
1311 * wait_for_parent_setup: Called by the child process to wait for any
1312 * further parent-side setup to complete before continuing.
1313 */
1314static void wait_for_parent_setup(int *pipe_fds)
1315{
1316 char buf;
1317
1318 close(pipe_fds[1]);
1319
1320 /* Wait for parent to complete setup and close the pipe. */
1321 if (read(pipe_fds[0], &buf, 1) != 0)
1322 die("failed to sync with parent");
1323 close(pipe_fds[0]);
1324}
1325
1326static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001327{
Lutz Justen13807cb2017-01-03 17:11:55 +01001328 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1329 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001330 die("can only do one of inherit, keep, or set supplementary "
1331 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001332 }
1333
Lutz Justen13807cb2017-01-03 17:11:55 +01001334 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001335 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001336 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001337 } else if (j->flags.set_suppl_gids) {
1338 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001339 pdie("setgroups(suppl_gids) failed");
Lutz Justen13807cb2017-01-03 17:11:55 +01001340 } else if (!j->flags.keep_suppl_gids) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001341 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001342 * Only attempt to clear supplementary groups if we are changing
Lutz Justen13807cb2017-01-03 17:11:55 +01001343 * users or groups.
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001344 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001345 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001346 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001347 }
1348
1349 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001350 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001351
1352 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001353 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001354}
1355
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001356static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1357{
1358 const uint64_t one = 1;
1359 unsigned int i;
1360 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1361 if (keep_mask & (one << i))
1362 continue;
1363 if (prctl(PR_CAPBSET_DROP, i))
1364 pdie("could not drop capability from bounding set");
1365 }
1366}
1367
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001368static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001369{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001370 if (!j->flags.use_caps)
1371 return;
1372
Elly Jonese1749eb2011-10-07 13:54:59 -04001373 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001374 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001375 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001376 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001377 unsigned int i;
1378 if (!caps)
1379 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001380 if (cap_clear(caps))
1381 die("can't clear caps");
1382
1383 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001384 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001385 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001386 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001387 flag[0] = i;
1388 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001389 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001390 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001391 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001392 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001393 die("can't add inheritable cap");
1394 }
1395 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001396 die("can't apply initial cleaned capset");
1397
1398 /*
1399 * Instead of dropping bounding set first, do it here in case
1400 * the caller had a more permissive bounding set which could
1401 * have been used above to raise a capability that wasn't already
1402 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1403 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001404 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001405
1406 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001407 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001408 flag[0] = CAP_SETPCAP;
1409 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1410 die("can't clear effective cap");
1411 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1412 die("can't clear permitted cap");
1413 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1414 die("can't clear inheritable cap");
1415 }
1416
1417 if (cap_set_proc(caps))
1418 die("can't apply final cleaned capset");
1419
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001420 /*
1421 * If ambient capabilities are supported, clear all capabilities first,
1422 * then raise the requested ones.
1423 */
1424 if (j->flags.set_ambient_caps) {
1425 if (!cap_ambient_supported()) {
1426 pdie("ambient capabilities not supported");
1427 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001428 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1429 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001430 pdie("can't clear ambient capabilities");
1431 }
1432
1433 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1434 if (!(j->caps & (one << i)))
1435 continue;
1436
1437 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1438 0) != 0) {
1439 pdie("prctl(PR_CAP_AMBIENT, "
1440 "PR_CAP_AMBIENT_RAISE, %u) failed",
1441 i);
1442 }
1443 }
1444 }
1445
Kees Cook323878a2013-02-05 15:35:24 -08001446 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001447}
1448
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001449static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001450{
1451 /*
1452 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1453 * in the kernel source tree for an explanation of the parameters.
1454 */
1455 if (j->flags.no_new_privs) {
1456 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1457 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1458 }
1459
1460 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001461 * Code running with ASan
1462 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1463 * will make system calls not included in the syscall filter policy,
1464 * which will likely crash the program. Skip setting seccomp filter in
1465 * that case.
1466 * 'running_with_asan()' has no inputs and is completely defined at
1467 * build time, so this cannot be used by an attacker to skip setting
1468 * seccomp filter.
1469 */
1470 if (j->flags.seccomp_filter && running_with_asan()) {
1471 warn("running with ASan, not setting seccomp filter");
1472 return;
1473 }
1474
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001475 if (j->flags.seccomp_filter) {
1476 if (j->flags.seccomp_filter_logging) {
1477 /*
1478 * If logging seccomp filter failures,
1479 * install the SIGSYS handler first.
1480 */
1481 if (install_sigsys_handler())
1482 pdie("failed to install SIGSYS handler");
1483 warn("logging seccomp filter failures");
1484 } else if (j->flags.seccomp_filter_tsync) {
1485 /*
1486 * If setting thread sync,
1487 * reset the SIGSYS signal handler so that
1488 * the entire thread group is killed.
1489 */
1490 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1491 pdie("failed to reset SIGSYS disposition");
1492 info("reset SIGSYS disposition");
1493 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001494 }
1495
1496 /*
1497 * Install the syscall filter.
1498 */
1499 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001500 if (j->flags.seccomp_filter_tsync) {
1501 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1502 SECCOMP_FILTER_FLAG_TSYNC,
1503 j->filter_prog)) {
1504 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001505 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001506 } else {
1507 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1508 j->filter_prog)) {
1509 pdie("prctl(seccomp_filter) failed");
1510 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001511 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001512 }
1513}
1514
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001515static pid_t forward_pid = -1;
1516
1517static void forward_signal(__attribute__((unused)) int nr,
1518 __attribute__((unused)) siginfo_t *siginfo,
1519 __attribute__((unused)) void *void_context)
1520{
1521 if (forward_pid != -1) {
1522 kill(forward_pid, nr);
1523 }
1524}
1525
1526static void install_signal_handlers(void)
1527{
1528 struct sigaction act;
1529
1530 memset(&act, 0, sizeof(act));
1531 act.sa_sigaction = &forward_signal;
1532 act.sa_flags = SA_SIGINFO | SA_RESTART;
1533
1534 /* Handle all signals, except SIGCHLD. */
1535 for (int nr = 1; nr < NSIG; nr++) {
1536 /*
1537 * We don't care if we get EINVAL: that just means that we
1538 * can't handle this signal, so let's skip it and continue.
1539 */
1540 sigaction(nr, &act, NULL);
1541 }
1542 /* Reset SIGCHLD's handler. */
1543 signal(SIGCHLD, SIG_DFL);
1544
1545 /* Handle real-time signals. */
1546 for (int nr = SIGRTMIN; nr <= SIGRTMAX; nr++) {
1547 sigaction(nr, &act, NULL);
1548 }
1549}
1550
Will Drewry6ac91122011-10-21 16:38:58 -05001551void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001552{
Dylan Reidf682d472015-09-17 21:39:07 -07001553 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001554 * If we're dropping caps, get the last valid cap from /proc now,
1555 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001556 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001557 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001558 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001559 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001560
Elly Jonese1749eb2011-10-07 13:54:59 -04001561 if (j->flags.pids)
1562 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001563 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001564
Lutz Justen13807cb2017-01-03 17:11:55 +01001565 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001566 die("cannot inherit supplementary groups without setting a "
1567 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001568
Elly Jonesdd3e8512012-01-23 15:13:38 -05001569 /*
1570 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001571 * so we don't even try. If any of our operations fail, we abort() the
1572 * entire process.
1573 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001574 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001575 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001576
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001577 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001578 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001579 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001580 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001581 * Unless asked not to, remount all filesystems as private.
1582 * If they are shared, new bind mounts will creep out of our
1583 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001584 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1585 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001586 if (!j->flags.skip_remount_private) {
1587 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001588 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1589 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001590 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001591 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001592
Dylan Reidf7942472015-11-18 17:55:26 -08001593 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001594 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001595 }
1596
Dylan Reid1102f5a2015-09-15 11:52:20 -07001597 if (j->flags.enter_net) {
1598 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001599 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001600 } else if (j->flags.net) {
1601 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001602 pdie("unshare(CLONE_NEWNET) failed");
1603 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001604 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001605
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001606 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001607 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001608
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08001609 if (j->flags.new_session_keyring) {
1610 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
1611 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
1612 }
1613
Elly Jones51a5b6c2011-10-12 19:09:26 -04001614 if (j->flags.chroot && enter_chroot(j))
1615 pdie("chroot");
1616
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001617 if (j->flags.pivot_root && enter_pivot_root(j))
1618 pdie("pivot_root");
1619
Martin Pelikánab9eb442017-01-25 11:53:58 +11001620 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07001621 pdie("mount_tmp");
1622
Dylan Reid791f5772015-09-14 20:02:42 -07001623 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001624 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001625
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001626 /*
1627 * If we're only dropping capabilities from the bounding set, but not
1628 * from the thread's (permitted|inheritable|effective) sets, do it now.
1629 */
1630 if (j->flags.capbset_drop) {
1631 drop_capbset(j->cap_bset, last_valid_cap);
1632 }
1633
1634 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001635 /*
1636 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001637 * capability to change uids, our attempt to use setuid()
1638 * below will fail. Hang on to root caps across setuid(), then
1639 * lock securebits.
1640 */
1641 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001642 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001643
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001644 if (lock_securebits() < 0) {
1645 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001646 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001647 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001648
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001649 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001650 /*
1651 * If we're setting no_new_privs, we can drop privileges
1652 * before setting seccomp filter. This way filter policies
1653 * don't need to allow privilege-dropping syscalls.
1654 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001655 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001656 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001657 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001658 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001659 /*
1660 * If we're not setting no_new_privs,
1661 * we need to set seccomp filter *before* dropping privileges.
1662 * WARNING: this means that filter policies *must* allow
1663 * setgroups()/setresgid()/setresuid() for dropping root and
1664 * capget()/capset()/prctl() for dropping caps.
1665 */
1666 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001667 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001668 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001669 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001670
Elly Jonesdd3e8512012-01-23 15:13:38 -05001671 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001672 * Select the specified alternate syscall table. The table must not
1673 * block prctl(2) if we're using seccomp as well.
1674 */
1675 if (j->flags.alt_syscall) {
1676 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001677 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08001678 }
1679
1680 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001681 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001682 * privilege-dropping syscalls :)
1683 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001684 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001685 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001686 warn("seccomp not supported");
1687 return;
1688 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001689 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001690 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001691}
1692
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001693/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001694static int init_exitstatus = 0;
1695
Will Drewry6ac91122011-10-21 16:38:58 -05001696void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001697{
1698 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001699}
1700
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001701void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001702{
1703 pid_t pid;
1704 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001705 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001706 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001707 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001708 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001709 /*
1710 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001711 * left inside our pid namespace or we get a signal.
1712 */
1713 if (pid == rootpid)
1714 init_exitstatus = status;
1715 }
1716 if (!WIFEXITED(init_exitstatus))
1717 _exit(MINIJAIL_ERR_INIT);
1718 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001719}
1720
Will Drewry6ac91122011-10-21 16:38:58 -05001721int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001722{
1723 size_t sz = 0;
1724 size_t bytes = read(fd, &sz, sizeof(sz));
1725 char *buf;
1726 int r;
1727 if (sizeof(sz) != bytes)
1728 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001729 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001730 return -E2BIG;
1731 buf = malloc(sz);
1732 if (!buf)
1733 return -ENOMEM;
1734 bytes = read(fd, buf, sz);
1735 if (bytes != sz) {
1736 free(buf);
1737 return -EINVAL;
1738 }
1739 r = minijail_unmarshal(j, buf, sz);
1740 free(buf);
1741 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001742}
1743
Will Drewry6ac91122011-10-21 16:38:58 -05001744int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001745{
1746 char *buf;
1747 size_t sz = minijail_size(j);
1748 ssize_t written;
1749 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001750
Elly Jonese1749eb2011-10-07 13:54:59 -04001751 if (!sz)
1752 return -EINVAL;
1753 buf = malloc(sz);
1754 r = minijail_marshal(j, buf, sz);
1755 if (r) {
1756 free(buf);
1757 return r;
1758 }
1759 /* Sends [size][minijail]. */
1760 written = write(fd, &sz, sizeof(sz));
1761 if (written != sizeof(sz)) {
1762 free(buf);
1763 return -EFAULT;
1764 }
1765 written = write(fd, buf, sz);
1766 if (written < 0 || (size_t) written != sz) {
1767 free(buf);
1768 return -EFAULT;
1769 }
1770 free(buf);
1771 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001772}
Elly Jonescd7a9042011-07-22 13:56:51 -04001773
Will Drewry6ac91122011-10-21 16:38:58 -05001774int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001775{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001776#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001777 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001778 return 0;
1779#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001780 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1781 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1782 if (!newenv)
1783 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001784
Elly Jonese1749eb2011-10-07 13:54:59 -04001785 /* Only insert a separating space if we have something to separate... */
1786 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1787 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001788
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001789 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001790 setenv(kLdPreloadEnvVar, newenv, 1);
1791 free(newenv);
1792 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001793#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001794}
1795
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001796static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001797{
1798 int r = pipe(fds);
1799 char fd_buf[11];
1800 if (r)
1801 return r;
1802 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1803 if (r <= 0)
1804 return -EINVAL;
1805 setenv(kFdEnvVar, fd_buf, 1);
1806 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001807}
1808
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001809static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07001810{
1811 const char *kFdPath = "/proc/self/fd";
1812
1813 DIR *d = opendir(kFdPath);
1814 struct dirent *dir_entry;
1815
1816 if (d == NULL)
1817 return -1;
1818 int dir_fd = dirfd(d);
1819 while ((dir_entry = readdir(d)) != NULL) {
1820 size_t i;
1821 char *end;
1822 bool should_close = true;
1823 const int fd = strtol(dir_entry->d_name, &end, 10);
1824
1825 if ((*end) != '\0') {
1826 continue;
1827 }
1828 /*
1829 * We might have set up some pipes that we want to share with
1830 * the parent process, and should not be closed.
1831 */
1832 for (i = 0; i < size; ++i) {
1833 if (fd == inheritable_fds[i]) {
1834 should_close = false;
1835 break;
1836 }
1837 }
1838 /* Also avoid closing the directory fd. */
1839 if (should_close && fd != dir_fd)
1840 close(fd);
1841 }
1842 closedir(d);
1843 return 0;
1844}
1845
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001846int minijail_run_internal(struct minijail *j, const char *filename,
1847 char *const argv[], pid_t *pchild_pid,
1848 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1849 int use_preload);
1850
Will Drewry6ac91122011-10-21 16:38:58 -05001851int API minijail_run(struct minijail *j, const char *filename,
1852 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001853{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001854 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1855 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001856}
1857
1858int API minijail_run_pid(struct minijail *j, const char *filename,
1859 char *const argv[], pid_t *pchild_pid)
1860{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001861 return minijail_run_internal(j, filename, argv, pchild_pid,
1862 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001863}
1864
1865int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001866 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001867{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001868 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1869 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001870}
1871
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001872int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001873 char *const argv[], pid_t *pchild_pid,
1874 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001875{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001876 return minijail_run_internal(j, filename, argv, pchild_pid,
1877 pstdin_fd, pstdout_fd, pstderr_fd, true);
1878}
1879
1880int API minijail_run_no_preload(struct minijail *j, const char *filename,
1881 char *const argv[])
1882{
1883 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1884 false);
1885}
1886
Samuel Tan63187f42015-10-16 13:01:53 -07001887int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001888 const char *filename,
1889 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001890 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001891 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001892 int *pstderr_fd)
1893{
Samuel Tan63187f42015-10-16 13:01:53 -07001894 return minijail_run_internal(j, filename, argv, pchild_pid,
1895 pstdin_fd, pstdout_fd, pstderr_fd, false);
1896}
1897
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001898int minijail_run_internal(struct minijail *j, const char *filename,
1899 char *const argv[], pid_t *pchild_pid,
1900 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1901 int use_preload)
1902{
Elly Jonese1749eb2011-10-07 13:54:59 -04001903 char *oldenv, *oldenv_copy = NULL;
1904 pid_t child_pid;
1905 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001906 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001907 int stdout_fds[2];
1908 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001909 int child_sync_pipe_fds[2];
1910 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001911 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001912 /* We need to remember this across the minijail_preexec() call. */
1913 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001914 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001915
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001916 if (use_preload) {
1917 oldenv = getenv(kLdPreloadEnvVar);
1918 if (oldenv) {
1919 oldenv_copy = strdup(oldenv);
1920 if (!oldenv_copy)
1921 return -ENOMEM;
1922 }
1923
1924 if (setup_preload())
1925 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001926 }
Will Drewryf89aef52011-09-16 16:48:57 -05001927
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001928 if (!use_preload) {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04001929 if (j->flags.use_caps && j->caps != 0)
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001930 die("non-empty capabilities are not supported without "
1931 "LD_PRELOAD");
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001932 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001933
Elly Jonesdd3e8512012-01-23 15:13:38 -05001934 /*
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05001935 * Make the process group ID of this process equal to its PID.
1936 * In the non-interactive case (e.g. when the parent process is started
1937 * from init) this ensures the parent process and the jailed process
1938 * can be killed together.
1939 * When the parent process is started from the console this ensures
1940 * the call to setsid(2) in the jailed process succeeds.
1941 *
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001942 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1943 * the process is already a process group leader.
1944 */
1945 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1946 if (errno != EPERM) {
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05001947 pdie("setpgid(0, 0) failed");
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001948 }
1949 }
1950
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001951 if (use_preload) {
1952 /*
1953 * Before we fork(2) and execve(2) the child process, we need
1954 * to open a pipe(2) to send the minijail configuration over.
1955 */
1956 if (setup_pipe(pipe_fds))
1957 return -EFAULT;
1958 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001959
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001960 /*
1961 * If we want to write to the child process' standard input,
1962 * create the pipe(2) now.
1963 */
1964 if (pstdin_fd) {
1965 if (pipe(stdin_fds))
1966 return -EFAULT;
1967 }
1968
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001969 /*
1970 * If we want to read from the child process' standard output,
1971 * create the pipe(2) now.
1972 */
1973 if (pstdout_fd) {
1974 if (pipe(stdout_fds))
1975 return -EFAULT;
1976 }
1977
1978 /*
1979 * If we want to read from the child process' standard error,
1980 * create the pipe(2) now.
1981 */
1982 if (pstderr_fd) {
1983 if (pipe(stderr_fds))
1984 return -EFAULT;
1985 }
1986
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001987 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04001988 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001989 * or if we need to add the child process to cgroups, create the pipe(2)
1990 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001991 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001992 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001993 sync_child = 1;
1994 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001995 return -EFAULT;
1996 }
1997
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001998 /*
1999 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002000 *
2001 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2002 *
2003 * In multithreaded programs, there are a bunch of locks inside libc,
2004 * some of which may be held by other threads at the time that we call
2005 * minijail_run_pid(). If we call fork(), glibc does its level best to
2006 * ensure that we hold all of these locks before it calls clone()
2007 * internally and drop them after clone() returns, but when we call
2008 * sys_clone(2) directly, all that gets bypassed and we end up with a
2009 * child address space where some of libc's important locks are held by
2010 * other threads (which did not get cloned, and hence will never release
2011 * those locks). This is okay so long as we call exec() immediately
2012 * after, but a bunch of seemingly-innocent libc functions like setenv()
2013 * take locks.
2014 *
2015 * Hence, only call sys_clone() if we need to, in order to get at pid
2016 * namespacing. If we follow this path, the child's address space might
2017 * have broken locks; you may only call functions that do not acquire
2018 * any locks.
2019 *
2020 * Unfortunately, fork() acquires every lock it can get its hands on, as
2021 * previously detailed, so this function is highly likely to deadlock
2022 * later on (see "deadlock here") if we're multithreaded.
2023 *
2024 * We might hack around this by having the clone()d child (init of the
2025 * pid namespace) return directly, rather than leaving the clone()d
2026 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002027 * its fork()ed child return in turn), but that process would be
2028 * crippled with its libc locks potentially broken. We might try
2029 * fork()ing in the parent before we clone() to ensure that we own all
2030 * the locks, but then we have to have the forked child hanging around
2031 * consuming resources (and possibly having file descriptors / shared
2032 * memory regions / etc attached). We'd need to keep the child around to
2033 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002034 *
2035 * TODO(ellyjones): figure out if the "forked child hanging around"
2036 * problem is fixable or not. It would be nice if we worked in this
2037 * case.
2038 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002039 if (pid_namespace) {
2040 int clone_flags = CLONE_NEWPID | SIGCHLD;
2041 if (j->flags.userns)
2042 clone_flags |= CLONE_NEWUSER;
2043 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002044 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002045 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002046 }
Elly Jones761b7412012-06-13 15:49:52 -04002047
Elly Jonese1749eb2011-10-07 13:54:59 -04002048 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002049 if (use_preload) {
2050 free(oldenv_copy);
2051 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002052 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002053 }
Will Drewryf89aef52011-09-16 16:48:57 -05002054
Elly Jonese1749eb2011-10-07 13:54:59 -04002055 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002056 if (use_preload) {
2057 /* Restore parent's LD_PRELOAD. */
2058 if (oldenv_copy) {
2059 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2060 free(oldenv_copy);
2061 } else {
2062 unsetenv(kLdPreloadEnvVar);
2063 }
2064 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002065 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002066
Elly Jonese1749eb2011-10-07 13:54:59 -04002067 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002068
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002069 if (j->flags.forward_signals) {
2070 forward_pid = child_pid;
2071 install_signal_handlers();
2072 }
2073
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002074 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002075 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002076
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002077 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002078 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002079
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002080 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002081 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002082
2083 if (sync_child)
2084 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002085
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002086 if (use_preload) {
2087 /* Send marshalled minijail. */
2088 close(pipe_fds[0]); /* read endpoint */
2089 ret = minijail_to_fd(j, pipe_fds[1]);
2090 close(pipe_fds[1]); /* write endpoint */
2091 if (ret) {
2092 kill(j->initpid, SIGKILL);
2093 die("failed to send marshalled minijail");
2094 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002095 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002096
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002097 if (pchild_pid)
2098 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002099
2100 /*
2101 * If we want to write to the child process' standard input,
2102 * set up the write end of the pipe.
2103 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002104 if (pstdin_fd)
2105 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002106 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002107
2108 /*
2109 * If we want to read from the child process' standard output,
2110 * set up the read end of the pipe.
2111 */
2112 if (pstdout_fd)
2113 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002114 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002115
2116 /*
2117 * If we want to read from the child process' standard error,
2118 * set up the read end of the pipe.
2119 */
2120 if (pstderr_fd)
2121 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002122 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002123
Elly Jonese1749eb2011-10-07 13:54:59 -04002124 return 0;
2125 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002126 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002127 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002128
Peter Qiu2860c462015-12-16 15:13:06 -08002129 if (j->flags.reset_signal_mask) {
2130 sigset_t signal_mask;
2131 if (sigemptyset(&signal_mask) != 0)
2132 pdie("sigemptyset failed");
2133 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2134 pdie("sigprocmask failed");
2135 }
2136
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002137 if (j->flags.close_open_fds) {
2138 const size_t kMaxInheritableFdsSize = 10;
2139 int inheritable_fds[kMaxInheritableFdsSize];
2140 size_t size = 0;
2141 if (use_preload) {
2142 inheritable_fds[size++] = pipe_fds[0];
2143 inheritable_fds[size++] = pipe_fds[1];
2144 }
2145 if (sync_child) {
2146 inheritable_fds[size++] = child_sync_pipe_fds[0];
2147 inheritable_fds[size++] = child_sync_pipe_fds[1];
2148 }
2149 if (pstdin_fd) {
2150 inheritable_fds[size++] = stdin_fds[0];
2151 inheritable_fds[size++] = stdin_fds[1];
2152 }
2153 if (pstdout_fd) {
2154 inheritable_fds[size++] = stdout_fds[0];
2155 inheritable_fds[size++] = stdout_fds[1];
2156 }
2157 if (pstderr_fd) {
2158 inheritable_fds[size++] = stderr_fds[0];
2159 inheritable_fds[size++] = stderr_fds[1];
2160 }
2161
2162 if (close_open_fds(inheritable_fds, size) < 0)
2163 die("failed to close open file descriptors");
2164 }
2165
Dylan Reidce5b55e2016-01-13 11:04:16 -08002166 if (sync_child)
2167 wait_for_parent_setup(child_sync_pipe_fds);
2168
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002169 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002170 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002171
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002172 /*
2173 * If we want to write to the jailed process' standard input,
2174 * set up the read end of the pipe.
2175 */
2176 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002177 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2178 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002179 die("failed to set up stdin pipe");
2180 }
2181
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002182 /*
2183 * If we want to read from the jailed process' standard output,
2184 * set up the write end of the pipe.
2185 */
2186 if (pstdout_fd) {
2187 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2188 STDOUT_FILENO) < 0)
2189 die("failed to set up stdout pipe");
2190 }
2191
2192 /*
2193 * If we want to read from the jailed process' standard error,
2194 * set up the write end of the pipe.
2195 */
2196 if (pstderr_fd) {
2197 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2198 STDERR_FILENO) < 0)
2199 die("failed to set up stderr pipe");
2200 }
2201
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002202 /*
2203 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2204 * This prevents the jailed process from using the TIOCSTI ioctl
2205 * to push characters into the parent process terminal's input buffer,
2206 * therefore escaping the jail.
2207 */
2208 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2209 isatty(STDERR_FILENO)) {
2210 if (setsid() < 0) {
2211 pdie("setsid() failed");
2212 }
2213 }
2214
Dylan Reid791f5772015-09-14 20:02:42 -07002215 /* If running an init program, let it decide when/how to mount /proc. */
2216 if (pid_namespace && !do_init)
2217 j->flags.remount_proc_ro = 0;
2218
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002219 if (use_preload) {
2220 /* Strip out flags that cannot be inherited across execve(2). */
2221 minijail_preexec(j);
2222 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002223 /*
2224 * If not using LD_PRELOAD, do all jailing before execve(2).
2225 * Note that PID namespaces can only be entered on fork(2),
2226 * so that flag is still cleared.
2227 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002228 j->flags.pids = 0;
2229 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002230 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002231 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002232
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002233 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002234 /*
2235 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002236 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002237 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002238 * a child to actually run the program. If |do_init == 0|, we
2239 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002240 *
2241 * If we're multithreaded, we'll probably deadlock here. See
2242 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002243 */
2244 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002245 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002246 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002247 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002248 /*
2249 * Best effort. Don't bother checking the return value.
2250 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002251 prctl(PR_SET_NAME, "minijail-init");
2252 init(child_pid); /* Never returns. */
2253 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002254 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002255
Elly Jonesdd3e8512012-01-23 15:13:38 -05002256 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002257 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002258 * calling process
2259 * -> execve()-ing process
2260 * If we are:
2261 * calling process
2262 * -> init()-ing process
2263 * -> execve()-ing process
2264 */
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002265 ret = execve(filename, argv, environ);
2266 if (ret == -1) {
2267 pwarn("execve(%s) failed", filename);
2268 }
2269 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002270}
2271
Will Drewry6ac91122011-10-21 16:38:58 -05002272int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002273{
2274 int st;
2275 if (kill(j->initpid, SIGTERM))
2276 return -errno;
2277 if (waitpid(j->initpid, &st, 0) < 0)
2278 return -errno;
2279 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002280}
2281
Will Drewry6ac91122011-10-21 16:38:58 -05002282int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002283{
2284 int st;
2285 if (waitpid(j->initpid, &st, 0) < 0)
2286 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002287
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002288 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002289 int error_status = st;
2290 if (WIFSIGNALED(st)) {
2291 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002292 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002293 j->initpid, signum);
2294 /*
2295 * We return MINIJAIL_ERR_JAIL if the process received
2296 * SIGSYS, which happens when a syscall is blocked by
2297 * seccomp filters.
2298 * If not, we do what bash(1) does:
2299 * $? = 128 + signum
2300 */
2301 if (signum == SIGSYS) {
2302 error_status = MINIJAIL_ERR_JAIL;
2303 } else {
2304 error_status = 128 + signum;
2305 }
2306 }
2307 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002308 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002309
2310 int exit_status = WEXITSTATUS(st);
2311 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002312 info("child process %d exited with status %d",
2313 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002314
2315 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002316}
2317
Will Drewry6ac91122011-10-21 16:38:58 -05002318void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002319{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002320 size_t i;
2321
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002322 if (j->flags.seccomp_filter && j->filter_prog) {
2323 free(j->filter_prog->filter);
2324 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002325 }
Dylan Reid648b2202015-10-23 00:50:00 -07002326 while (j->mounts_head) {
2327 struct mountpoint *m = j->mounts_head;
2328 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002329 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002330 free(m->type);
2331 free(m->dest);
2332 free(m->src);
2333 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002334 }
Dylan Reid648b2202015-10-23 00:50:00 -07002335 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002336 if (j->user)
2337 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002338 if (j->suppl_gid_list)
2339 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002340 if (j->chrootdir)
2341 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002342 if (j->pid_file_path)
2343 free(j->pid_file_path);
2344 if (j->uidmap)
2345 free(j->uidmap);
2346 if (j->gidmap)
2347 free(j->gidmap);
Andrew Brestickereac28942015-11-11 16:04:46 -08002348 if (j->alt_syscall_table)
2349 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002350 for (i = 0; i < j->cgroup_count; ++i)
2351 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002352 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002353}