blob: dada8faa096471138bd45003b383ad192fbf1ef9 [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;
103 int vfs : 1;
104 int enter_vfs : 1;
105 int skip_remount_private : 1;
106 int pids : 1;
107 int ipc : 1;
108 int net : 1;
109 int enter_net : 1;
110 int ns_cgroups : 1;
111 int userns : 1;
112 int disable_setgroups : 1;
113 int seccomp : 1;
114 int remount_proc_ro : 1;
115 int no_new_privs : 1;
116 int seccomp_filter : 1;
117 int seccomp_filter_tsync : 1;
118 int seccomp_filter_logging : 1;
119 int chroot : 1;
120 int pivot_root : 1;
121 int mount_tmp : 1;
122 int do_init : 1;
123 int pid_file : 1;
124 int cgroups : 1;
125 int alt_syscall : 1;
126 int reset_signal_mask : 1;
127 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800128 int new_session_keyring : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400129 } flags;
130 uid_t uid;
131 gid_t gid;
132 gid_t usergid;
133 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800134 size_t suppl_gid_count;
135 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400136 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800137 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400138 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700139 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700140 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400141 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800142 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800143 char *uidmap;
144 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800145 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800146 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800147 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700148 struct mountpoint *mounts_head;
149 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800150 size_t mounts_count;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100151 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800152 char *cgroups[MAX_CGROUPS];
153 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500154};
155
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700156/*
157 * Strip out flags meant for the parent.
158 * We keep things that are not inherited across execve(2) (e.g. capabilities),
159 * or are easier to set after execve(2) (e.g. seccomp filters).
160 */
161void minijail_preenter(struct minijail *j)
162{
163 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700164 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900165 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700166 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700167 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800168 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800169 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800170 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700171}
172
173/*
174 * Strip out flags meant for the child.
175 * We keep things that are inherited across execve(2).
176 */
177void minijail_preexec(struct minijail *j)
178{
179 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700180 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800181 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700182 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800183 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700184 if (j->user)
185 free(j->user);
186 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800187 if (j->suppl_gid_list)
188 free(j->suppl_gid_list);
189 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700190 memset(&j->flags, 0, sizeof(j->flags));
191 /* Now restore anything we meant to keep. */
192 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700193 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800194 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700195 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800196 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700197 /* Note, |pids| will already have been used before this call. */
198}
199
200/* Minijail API. */
201
Will Drewry6ac91122011-10-21 16:38:58 -0500202struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400203{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400204 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400205}
206
Will Drewry6ac91122011-10-21 16:38:58 -0500207void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400208{
209 if (uid == 0)
210 die("useless change to uid 0");
211 j->uid = uid;
212 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400213}
214
Will Drewry6ac91122011-10-21 16:38:58 -0500215void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400216{
217 if (gid == 0)
218 die("useless change to gid 0");
219 j->gid = gid;
220 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400221}
222
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800223void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
224 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800225{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800226 size_t i;
227
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500228 if (j->flags.inherit_suppl_gids)
229 die("cannot inherit *and* set supplementary groups");
230 if (j->flags.keep_suppl_gids)
231 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800232
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800233 if (size == 0) {
234 /* Clear supplementary groups. */
235 j->suppl_gid_list = NULL;
236 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100237 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800238 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800239 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800240
241 /* Copy the gid_t array. */
242 j->suppl_gid_list = calloc(size, sizeof(gid_t));
243 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800244 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800245 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800246 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800247 j->suppl_gid_list[i] = list[i];
248 }
249 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100250 j->flags.set_suppl_gids = 1;
251}
252
253void API minijail_keep_supplementary_gids(struct minijail *j) {
254 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800255}
256
Will Drewry6ac91122011-10-21 16:38:58 -0500257int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400258{
259 char *buf = NULL;
260 struct passwd pw;
261 struct passwd *ppw = NULL;
262 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
263 if (sz == -1)
264 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400265
Elly Jonesdd3e8512012-01-23 15:13:38 -0500266 /*
267 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400268 * the maximum needed size of the buffer, so we don't have to search.
269 */
270 buf = malloc(sz);
271 if (!buf)
272 return -ENOMEM;
273 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500274 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800275 * We're safe to free the buffer here. The strings inside |pw| point
276 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800277 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
278 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500279 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400280 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700281 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400282 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700283 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400284 minijail_change_uid(j, ppw->pw_uid);
285 j->user = strdup(user);
286 if (!j->user)
287 return -ENOMEM;
288 j->usergid = ppw->pw_gid;
289 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400290}
291
Will Drewry6ac91122011-10-21 16:38:58 -0500292int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400293{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700294 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700295 struct group gr;
296 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400297 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
298 if (sz == -1)
299 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400300
Elly Jonesdd3e8512012-01-23 15:13:38 -0500301 /*
302 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400303 * the maximum needed size of the buffer, so we don't have to search.
304 */
305 buf = malloc(sz);
306 if (!buf)
307 return -ENOMEM;
308 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500309 /*
310 * We're safe to free the buffer here. The strings inside gr point
311 * inside buf, but we don't use any of them; this leaves the pointers
312 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
313 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400314 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700315 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400316 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700317 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400318 minijail_change_gid(j, pgr->gr_gid);
319 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400320}
321
Will Drewry6ac91122011-10-21 16:38:58 -0500322void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400323{
324 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400325}
326
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700327void API minijail_no_new_privs(struct minijail *j)
328{
329 j->flags.no_new_privs = 1;
330}
331
Will Drewry6ac91122011-10-21 16:38:58 -0500332void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400333{
334 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500335}
336
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400337void API minijail_set_seccomp_filter_tsync(struct minijail *j)
338{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400339 if (j->filter_len > 0 && j->filter_prog != NULL) {
340 die("minijail_set_seccomp_filter_tsync() must be called "
341 "before minijail_parse_seccomp_filters()");
342 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400343 j->flags.seccomp_filter_tsync = 1;
344}
345
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700346void API minijail_log_seccomp_filter_failures(struct minijail *j)
347{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400348 if (j->filter_len > 0 && j->filter_prog != NULL) {
349 die("minijail_log_seccomp_filter_failures() must be called "
350 "before minijail_parse_seccomp_filters()");
351 }
352 j->flags.seccomp_filter_logging = 1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700353}
354
Will Drewry6ac91122011-10-21 16:38:58 -0500355void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400356{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800357 /*
358 * 'minijail_use_caps' configures a runtime-capabilities-only
359 * environment, including a bounding set matching the thread's runtime
360 * (permitted|inheritable|effective) sets.
361 * Therefore, it will override any existing bounding set configurations
362 * since the latter would allow gaining extra runtime capabilities from
363 * file capabilities.
364 */
365 if (j->flags.capbset_drop) {
366 warn("overriding bounding set configuration");
367 j->cap_bset = 0;
368 j->flags.capbset_drop = 0;
369 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400370 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800371 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400372}
373
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800374void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
375{
376 if (j->flags.use_caps) {
377 /*
378 * 'minijail_use_caps' will have already configured a capability
379 * bounding set matching the (permitted|inheritable|effective)
380 * sets. Abort if the user tries to configure a separate
381 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
382 * are mutually exclusive.
383 */
384 die("runtime capabilities already configured, can't drop "
385 "bounding set separately");
386 }
387 j->cap_bset = capmask;
388 j->flags.capbset_drop = 1;
389}
390
391void API minijail_reset_signal_mask(struct minijail *j)
392{
Peter Qiu2860c462015-12-16 15:13:06 -0800393 j->flags.reset_signal_mask = 1;
394}
395
Will Drewry6ac91122011-10-21 16:38:58 -0500396void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400397{
398 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400399}
400
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700401void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
402{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800403 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700404 if (ns_fd < 0) {
405 pdie("failed to open namespace '%s'", ns_path);
406 }
407 j->mountns_fd = ns_fd;
408 j->flags.enter_vfs = 1;
409}
410
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800411void API minijail_new_session_keyring(struct minijail *j)
412{
413 j->flags.new_session_keyring = 1;
414}
415
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800416void API minijail_skip_remount_private(struct minijail *j)
417{
418 j->flags.skip_remount_private = 1;
419}
420
Will Drewry6ac91122011-10-21 16:38:58 -0500421void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400422{
Elly Jonese58176c2012-01-23 11:46:17 -0500423 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700424 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400425 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800426 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400427}
428
Dylan Reidf7942472015-11-18 17:55:26 -0800429void API minijail_namespace_ipc(struct minijail *j)
430{
431 j->flags.ipc = 1;
432}
433
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400434void API minijail_namespace_net(struct minijail *j)
435{
436 j->flags.net = 1;
437}
438
Dylan Reid1102f5a2015-09-15 11:52:20 -0700439void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
440{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800441 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700442 if (ns_fd < 0) {
443 pdie("failed to open namespace '%s'", ns_path);
444 }
445 j->netns_fd = ns_fd;
446 j->flags.enter_net = 1;
447}
448
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700449void API minijail_namespace_cgroups(struct minijail *j)
450{
451 j->flags.ns_cgroups = 1;
452}
453
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700454void API minijail_close_open_fds(struct minijail *j)
455{
456 j->flags.close_open_fds = 1;
457}
458
Dylan Reid791f5772015-09-14 20:02:42 -0700459void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400460{
461 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700462 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400463}
464
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800465void API minijail_namespace_user(struct minijail *j)
466{
467 j->flags.userns = 1;
468}
469
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400470void API minijail_namespace_user_disable_setgroups(struct minijail *j)
471{
472 j->flags.disable_setgroups = 1;
473}
474
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800475int API minijail_uidmap(struct minijail *j, const char *uidmap)
476{
477 j->uidmap = strdup(uidmap);
478 if (!j->uidmap)
479 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800480 char *ch;
481 for (ch = j->uidmap; *ch; ch++) {
482 if (*ch == ',')
483 *ch = '\n';
484 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800485 return 0;
486}
487
488int API minijail_gidmap(struct minijail *j, const char *gidmap)
489{
490 j->gidmap = strdup(gidmap);
491 if (!j->gidmap)
492 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800493 char *ch;
494 for (ch = j->gidmap; *ch; ch++) {
495 if (*ch == ',')
496 *ch = '\n';
497 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800498 return 0;
499}
500
Will Drewry6ac91122011-10-21 16:38:58 -0500501void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400502{
Lutz Justen13807cb2017-01-03 17:11:55 +0100503 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400504}
505
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800506void API minijail_run_as_init(struct minijail *j)
507{
508 /*
509 * Since the jailed program will become 'init' in the new PID namespace,
510 * Minijail does not need to fork an 'init' process.
511 */
512 j->flags.do_init = 0;
513}
514
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700515int API minijail_enter_chroot(struct minijail *j, const char *dir)
516{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400517 if (j->chrootdir)
518 return -EINVAL;
519 j->chrootdir = strdup(dir);
520 if (!j->chrootdir)
521 return -ENOMEM;
522 j->flags.chroot = 1;
523 return 0;
524}
525
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800526int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
527{
528 if (j->chrootdir)
529 return -EINVAL;
530 j->chrootdir = strdup(dir);
531 if (!j->chrootdir)
532 return -ENOMEM;
533 j->flags.pivot_root = 1;
534 return 0;
535}
536
Dylan Reida14e08d2015-10-22 21:05:29 -0700537char API *minijail_get_original_path(struct minijail *j,
538 const char *path_inside_chroot)
539{
Dylan Reid648b2202015-10-23 00:50:00 -0700540 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700541
Dylan Reid648b2202015-10-23 00:50:00 -0700542 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700543 while (b) {
544 /*
545 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700546 * mount, then the original path is exactly the source of
547 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700548 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700549 * mount source = /some/path/exe, mount dest =
550 * /chroot/path/exe Then when getting the original path of
551 * "/chroot/path/exe", the source of that mount,
552 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700553 */
554 if (!strcmp(b->dest, path_inside_chroot))
555 return strdup(b->src);
556
557 /*
558 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700559 * mount, take the suffix of the chroot path relative to the
560 * mount destination path, and append it to the mount source
561 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700562 */
563 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
564 const char *relative_path =
565 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400566 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700567 }
568 b = b->next;
569 }
570
571 /* If there is a chroot path, append |path_inside_chroot| to that. */
572 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400573 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700574
575 /* No chroot, so the path outside is the same as it is inside. */
576 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700577}
578
Martin Pelikánab9eb442017-01-25 11:53:58 +1100579size_t minijail_get_tmpfs_size(const struct minijail *j)
580{
581 return j->tmpfs_size;
582}
583
Lee Campbell11af0622014-05-22 12:36:04 -0700584void API minijail_mount_tmp(struct minijail *j)
585{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100586 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
587}
588
589void API minijail_mount_tmp_size(struct minijail *j, size_t size)
590{
591 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700592 j->flags.mount_tmp = 1;
593}
594
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800595int API minijail_write_pid_file(struct minijail *j, const char *path)
596{
597 j->pid_file_path = strdup(path);
598 if (!j->pid_file_path)
599 return -ENOMEM;
600 j->flags.pid_file = 1;
601 return 0;
602}
603
Dylan Reid605ce7f2016-01-19 19:21:00 -0800604int API minijail_add_to_cgroup(struct minijail *j, const char *path)
605{
606 if (j->cgroup_count >= MAX_CGROUPS)
607 return -ENOMEM;
608 j->cgroups[j->cgroup_count] = strdup(path);
609 if (!j->cgroups[j->cgroup_count])
610 return -ENOMEM;
611 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800612 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800613 return 0;
614}
615
Dylan Reid81e23972016-05-18 14:06:35 -0700616int API minijail_mount_with_data(struct minijail *j, const char *src,
617 const char *dest, const char *type,
618 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700619{
Dylan Reid648b2202015-10-23 00:50:00 -0700620 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400621
622 if (*dest != '/')
623 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700624 m = calloc(1, sizeof(*m));
625 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400626 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700627 m->dest = strdup(dest);
628 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400629 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700630 m->src = strdup(src);
631 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400632 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700633 m->type = strdup(type);
634 if (!m->type)
635 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700636 if (data) {
637 m->data = strdup(data);
638 if (!m->data)
639 goto error;
640 m->has_data = 1;
641 }
Dylan Reid648b2202015-10-23 00:50:00 -0700642 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400643
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800644 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400645
Elly Jonesdd3e8512012-01-23 15:13:38 -0500646 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700647 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400648 * containing vfs namespace.
649 */
650 minijail_namespace_vfs(j);
651
Dylan Reid648b2202015-10-23 00:50:00 -0700652 if (j->mounts_tail)
653 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400654 else
Dylan Reid648b2202015-10-23 00:50:00 -0700655 j->mounts_head = m;
656 j->mounts_tail = m;
657 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400658
659 return 0;
660
661error:
Dylan Reid81e23972016-05-18 14:06:35 -0700662 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700663 free(m->src);
664 free(m->dest);
665 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400666 return -ENOMEM;
667}
668
Dylan Reid81e23972016-05-18 14:06:35 -0700669int API minijail_mount(struct minijail *j, const char *src, const char *dest,
670 const char *type, unsigned long flags)
671{
672 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
673}
674
Dylan Reid648b2202015-10-23 00:50:00 -0700675int API minijail_bind(struct minijail *j, const char *src, const char *dest,
676 int writeable)
677{
678 unsigned long flags = MS_BIND;
679
680 if (!writeable)
681 flags |= MS_RDONLY;
682
683 return minijail_mount(j, src, dest, "", flags);
684}
685
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400686static void clear_seccomp_options(struct minijail *j)
687{
688 j->flags.seccomp_filter = 0;
689 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400690 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400691 j->filter_len = 0;
692 j->filter_prog = NULL;
693 j->flags.no_new_privs = 0;
694}
695
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400696static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400697{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400698 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400699 /*
700 * |errno| will be set to EINVAL when seccomp has not been
701 * compiled into the kernel. On certain platforms and kernel
702 * versions this is not a fatal failure. In that case, and only
703 * in that case, disable seccomp and skip loading the filters.
704 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400705 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400706 warn("not loading seccomp filters, seccomp filter not "
707 "supported");
708 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400709 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700710 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400711 /*
712 * If |errno| != EINVAL or seccomp_can_softfail() is false,
713 * we can proceed. Worst case scenario minijail_enter() will
714 * abort() if seccomp fails.
715 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700716 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400717 if (j->flags.seccomp_filter_tsync) {
718 /* Are the seccomp(2) syscall and the TSYNC option supported? */
719 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
720 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
721 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400722 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
723 warn("seccomp(2) syscall not supported");
724 clear_seccomp_options(j);
725 return 0;
726 } else if (saved_errno == EINVAL &&
727 seccomp_can_softfail()) {
728 warn(
729 "seccomp filter thread sync not supported");
730 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400731 return 0;
732 }
733 /*
734 * Similar logic here. If seccomp_can_softfail() is
735 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
736 * we can proceed. Worst case scenario minijail_enter()
737 * will abort() if seccomp or TSYNC fail.
738 */
739 }
740 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400741 return 1;
742}
743
744static int parse_seccomp_filters(struct minijail *j, FILE *policy_file)
745{
746 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400747 int use_ret_trap =
748 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
749 int allow_logging = j->flags.seccomp_filter_logging;
750
751 if (compile_filter(policy_file, fprog, use_ret_trap, allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400752 free(fprog);
753 return -1;
754 }
755
756 j->filter_len = fprog->len;
757 j->filter_prog = fprog;
758 return 0;
759}
760
761void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
762{
763 if (!seccomp_should_parse_filters(j))
764 return;
765
Elly Jonese1749eb2011-10-07 13:54:59 -0400766 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800767 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700768 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400769 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800770
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400771 if (parse_seccomp_filters(j, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700772 die("failed to compile seccomp filter BPF program in '%s'",
773 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800774 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400775 fclose(file);
776}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800777
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400778void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
779{
780 if (!seccomp_should_parse_filters(j))
781 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800782
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400783 FILE *file = fdopen(fd, "r");
784 if (!file) {
785 pdie("failed to associate stream with fd %d", fd);
786 }
787
788 if (parse_seccomp_filters(j, file) != 0) {
789 die("failed to compile seccomp filter BPF program from fd %d",
790 fd);
791 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400792 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500793}
794
Andrew Brestickereac28942015-11-11 16:04:46 -0800795int API minijail_use_alt_syscall(struct minijail *j, const char *table)
796{
797 j->alt_syscall_table = strdup(table);
798 if (!j->alt_syscall_table)
799 return -ENOMEM;
800 j->flags.alt_syscall = 1;
801 return 0;
802}
803
Will Drewryf89aef52011-09-16 16:48:57 -0500804struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400805 size_t available;
806 size_t total;
807 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500808};
809
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800810void marshal_state_init(struct marshal_state *state, char *buf,
811 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400812{
813 state->available = available;
814 state->buf = buf;
815 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500816}
817
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800818void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400819{
820 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500821
Elly Jonese1749eb2011-10-07 13:54:59 -0400822 /* Up to |available| will be written. */
823 if (copy_len) {
824 memcpy(state->buf, src, copy_len);
825 state->buf += copy_len;
826 state->available -= copy_len;
827 }
828 /* |total| will contain the expected length. */
829 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500830}
831
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400832void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700833{
834 marshal_append(state, m->src, strlen(m->src) + 1);
835 marshal_append(state, m->dest, strlen(m->dest) + 1);
836 marshal_append(state, m->type, strlen(m->type) + 1);
837 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
838 if (m->has_data)
839 marshal_append(state, m->data, strlen(m->data) + 1);
840 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
841}
842
Will Drewry6ac91122011-10-21 16:38:58 -0500843void minijail_marshal_helper(struct marshal_state *state,
844 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400845{
Dylan Reid648b2202015-10-23 00:50:00 -0700846 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800847 size_t i;
848
Elly Jonese1749eb2011-10-07 13:54:59 -0400849 marshal_append(state, (char *)j, sizeof(*j));
850 if (j->user)
851 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800852 if (j->suppl_gid_list) {
853 marshal_append(state, j->suppl_gid_list,
854 j->suppl_gid_count * sizeof(gid_t));
855 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400856 if (j->chrootdir)
857 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800858 if (j->alt_syscall_table) {
859 marshal_append(state, j->alt_syscall_table,
860 strlen(j->alt_syscall_table) + 1);
861 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800862 if (j->flags.seccomp_filter && j->filter_prog) {
863 struct sock_fprog *fp = j->filter_prog;
864 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800865 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400866 }
Dylan Reid648b2202015-10-23 00:50:00 -0700867 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400868 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400869 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800870 for (i = 0; i < j->cgroup_count; ++i)
871 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500872}
873
Will Drewry6ac91122011-10-21 16:38:58 -0500874size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400875{
876 struct marshal_state state;
877 marshal_state_init(&state, NULL, 0);
878 minijail_marshal_helper(&state, j);
879 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500880}
881
Elly Jonese1749eb2011-10-07 13:54:59 -0400882int minijail_marshal(const struct minijail *j, char *buf, size_t available)
883{
884 struct marshal_state state;
885 marshal_state_init(&state, buf, available);
886 minijail_marshal_helper(&state, j);
887 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500888}
889
Elly Jonese1749eb2011-10-07 13:54:59 -0400890int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
891{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800892 size_t i;
893 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500894 int ret = -EINVAL;
895
Elly Jonese1749eb2011-10-07 13:54:59 -0400896 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500897 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400898 memcpy((void *)j, serialized, sizeof(*j));
899 serialized += sizeof(*j);
900 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500901
Will Drewrybee7ba72011-10-21 20:47:01 -0500902 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -0400903 j->pid_file_path = NULL;
904 j->uidmap = NULL;
905 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -0700906 j->mounts_head = NULL;
907 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800908 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500909
Elly Jonese1749eb2011-10-07 13:54:59 -0400910 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400911 char *user = consumestr(&serialized, &length);
912 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500913 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400914 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500915 if (!j->user)
916 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400917 }
Will Drewryf89aef52011-09-16 16:48:57 -0500918
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800919 if (j->suppl_gid_list) { /* stale pointer */
920 if (j->suppl_gid_count > NGROUPS_MAX) {
921 goto bad_gid_list;
922 }
923 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
924 void *gid_list_bytes =
925 consumebytes(gid_list_size, &serialized, &length);
926 if (!gid_list_bytes)
927 goto bad_gid_list;
928
929 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
930 if (!j->suppl_gid_list)
931 goto bad_gid_list;
932
933 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
934 }
935
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400936 if (j->chrootdir) { /* stale pointer */
937 char *chrootdir = consumestr(&serialized, &length);
938 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500939 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400940 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500941 if (!j->chrootdir)
942 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400943 }
944
Andrew Brestickereac28942015-11-11 16:04:46 -0800945 if (j->alt_syscall_table) { /* stale pointer */
946 char *alt_syscall_table = consumestr(&serialized, &length);
947 if (!alt_syscall_table)
948 goto bad_syscall_table;
949 j->alt_syscall_table = strdup(alt_syscall_table);
950 if (!j->alt_syscall_table)
951 goto bad_syscall_table;
952 }
953
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800954 if (j->flags.seccomp_filter && j->filter_len > 0) {
955 size_t ninstrs = j->filter_len;
956 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
957 ninstrs > USHRT_MAX)
958 goto bad_filters;
959
960 size_t program_len = ninstrs * sizeof(struct sock_filter);
961 void *program = consumebytes(program_len, &serialized, &length);
962 if (!program)
963 goto bad_filters;
964
965 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800966 if (!j->filter_prog)
967 goto bad_filters;
968
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800969 j->filter_prog->len = ninstrs;
970 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800971 if (!j->filter_prog->filter)
972 goto bad_filter_prog_instrs;
973
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800974 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400975 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400976
Dylan Reid648b2202015-10-23 00:50:00 -0700977 count = j->mounts_count;
978 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400979 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700980 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -0700981 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400982 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700983 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700984 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400985 const char *src = consumestr(&serialized, &length);
986 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700987 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400988 dest = consumestr(&serialized, &length);
989 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700990 goto bad_mounts;
991 type = consumestr(&serialized, &length);
992 if (!type)
993 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -0700994 has_data = consumebytes(sizeof(*has_data), &serialized,
995 &length);
996 if (!has_data)
997 goto bad_mounts;
998 if (*has_data) {
999 data = consumestr(&serialized, &length);
1000 if (!data)
1001 goto bad_mounts;
1002 }
Dylan Reid648b2202015-10-23 00:50:00 -07001003 flags = consumebytes(sizeof(*flags), &serialized, &length);
1004 if (!flags)
1005 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001006 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001007 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001008 }
1009
Dylan Reid605ce7f2016-01-19 19:21:00 -08001010 count = j->cgroup_count;
1011 j->cgroup_count = 0;
1012 for (i = 0; i < count; ++i) {
1013 char *cgroup = consumestr(&serialized, &length);
1014 if (!cgroup)
1015 goto bad_cgroups;
1016 j->cgroups[i] = strdup(cgroup);
1017 if (!j->cgroups[i])
1018 goto bad_cgroups;
1019 ++j->cgroup_count;
1020 }
1021
Elly Jonese1749eb2011-10-07 13:54:59 -04001022 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001023
Dylan Reid605ce7f2016-01-19 19:21:00 -08001024bad_cgroups:
1025 while (j->mounts_head) {
1026 struct mountpoint *m = j->mounts_head;
1027 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07001028 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001029 free(m->type);
1030 free(m->dest);
1031 free(m->src);
1032 free(m);
1033 }
1034 for (i = 0; i < j->cgroup_count; ++i)
1035 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001036bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001037 if (j->flags.seccomp_filter && j->filter_len > 0) {
1038 free(j->filter_prog->filter);
1039 free(j->filter_prog);
1040 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001041bad_filter_prog_instrs:
1042 if (j->filter_prog)
1043 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001044bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001045 if (j->alt_syscall_table)
1046 free(j->alt_syscall_table);
1047bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001048 if (j->chrootdir)
1049 free(j->chrootdir);
1050bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001051 if (j->suppl_gid_list)
1052 free(j->suppl_gid_list);
1053bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001054 if (j->user)
1055 free(j->user);
1056clear_pointers:
1057 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001058 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001059 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001060 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001061 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001062out:
1063 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001064}
1065
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001066/*
1067 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001068 * @j Minijail these mounts are for
1069 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001070 *
1071 * Returns 0 for success.
1072 */
Dylan Reid648b2202015-10-23 00:50:00 -07001073static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001074{
Dylan Reid648b2202015-10-23 00:50:00 -07001075 int ret;
1076 char *dest;
1077 int remount_ro = 0;
1078
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001079 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001080 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001081 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001082
Dylan Reideec77962016-06-30 19:35:10 -07001083 if (setup_mount_destination(m->src, dest, j->uid, j->gid))
1084 pdie("creating mount target '%s' failed", dest);
1085
Dylan Reid648b2202015-10-23 00:50:00 -07001086 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001087 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1088 * can't both be specified in the original bind mount.
1089 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001090 */
1091 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1092 remount_ro = 1;
1093 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001094 }
Dylan Reid648b2202015-10-23 00:50:00 -07001095
Dylan Reid81e23972016-05-18 14:06:35 -07001096 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001097 if (ret)
1098 pdie("mount: %s -> %s", m->src, dest);
1099
1100 if (remount_ro) {
1101 m->flags |= MS_RDONLY;
1102 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001103 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001104 if (ret)
1105 pdie("bind ro: %s -> %s", m->src, dest);
1106 }
1107
Elly Jones51a5b6c2011-10-12 19:09:26 -04001108 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001109 if (m->next)
1110 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001111 return ret;
1112}
1113
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001114static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001115{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001116 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001117
1118 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001119 return ret;
1120
1121 if (chroot(j->chrootdir))
1122 return -errno;
1123
1124 if (chdir("/"))
1125 return -errno;
1126
1127 return 0;
1128}
1129
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001130static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001131{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001132 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001133
1134 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001135 return ret;
1136
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001137 /*
1138 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001139 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001140 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001141 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001142 if (oldroot < 0)
1143 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001144 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001145 if (newroot < 0)
1146 pdie("failed to open %s for fchdir", j->chrootdir);
1147
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001148 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001149 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001150 * do a self bind mount.
1151 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001152 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1153 pdie("failed to bind mount '%s'", j->chrootdir);
1154 if (chdir(j->chrootdir))
1155 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001156 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001157 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001158
1159 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001160 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001161 * change to the old root and unmount it.
1162 */
1163 if (fchdir(oldroot))
1164 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001165
1166 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001167 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1168 * there could be a shared mount point under |oldroot|. In that case,
1169 * mounts under this shared mount point will be unmounted below, and
1170 * this unmounting will propagate to the original mount namespace
1171 * (because the mount point is shared). To prevent this unexpected
1172 * unmounting, remove these mounts from their peer groups by recursively
1173 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001174 */
1175 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001176 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001177 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001178 if (umount2(".", MNT_DETACH))
1179 pdie("umount(/)");
1180 /* Change back to the new root. */
1181 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001182 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001183 if (close(oldroot))
1184 return -errno;
1185 if (close(newroot))
1186 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001187 if (chroot("/"))
1188 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001189 /* Set correct CWD for getcwd(3). */
1190 if (chdir("/"))
1191 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001192
1193 return 0;
1194}
1195
Martin Pelikánab9eb442017-01-25 11:53:58 +11001196static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001197{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001198 const char fmt[] = "size=%zu,mode=1777";
1199 /* Count for the user storing ULLONG_MAX literally + extra space. */
1200 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1201 int ret;
1202
1203 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1204
1205 if (ret <= 0)
1206 pdie("tmpfs size spec error");
1207 else if ((size_t)ret >= sizeof(data))
1208 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001209 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001210 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001211}
1212
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001213static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001214{
1215 const char *kProcPath = "/proc";
1216 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001217 /*
1218 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001219 * /proc in our namespace, which means using MS_REMOUNT here would
1220 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001221 * namespace (!). Instead, remove their mount from our namespace lazily
1222 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001223 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001224 if (umount2(kProcPath, MNT_DETACH)) {
1225 /*
1226 * If we are in a new user namespace, umount(2) will fail.
1227 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1228 */
1229 if (j->flags.userns) {
1230 info("umount(/proc, MNT_DETACH) failed, "
1231 "this is expected when using user namespaces");
1232 } else {
1233 return -errno;
1234 }
1235 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001236 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001237 return -errno;
1238 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001239}
1240
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001241static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001242{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001243 kill(j->initpid, SIGKILL);
1244 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001245}
1246
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001247static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001248{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001249 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001250 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001251}
1252
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001253static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001254{
1255 size_t i;
1256
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001257 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001258 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001259 kill_child_and_die(j, "failed to add to cgroups");
1260 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001261}
1262
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001263static void write_ugid_maps_or_die(const struct minijail *j)
1264{
1265 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1266 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001267 if (j->gidmap && j->flags.disable_setgroups) {
1268 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1269 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001270 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001271 if (ret == -ENOENT) {
1272 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1273 warn("could not disable setgroups(2)");
1274 } else
1275 kill_child_and_die(j, "failed to disable setgroups(2)");
1276 }
1277 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001278 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1279 kill_child_and_die(j, "failed to write gid_map");
1280}
1281
1282static void enter_user_namespace(const struct minijail *j)
1283{
1284 if (j->uidmap && setresuid(0, 0, 0))
1285 pdie("user_namespaces: setresuid(0, 0, 0) failed");
1286 if (j->gidmap && setresgid(0, 0, 0))
1287 pdie("user_namespaces: setresgid(0, 0, 0) failed");
1288}
1289
1290static void parent_setup_complete(int *pipe_fds)
1291{
1292 close(pipe_fds[0]);
1293 close(pipe_fds[1]);
1294}
1295
1296/*
1297 * wait_for_parent_setup: Called by the child process to wait for any
1298 * further parent-side setup to complete before continuing.
1299 */
1300static void wait_for_parent_setup(int *pipe_fds)
1301{
1302 char buf;
1303
1304 close(pipe_fds[1]);
1305
1306 /* Wait for parent to complete setup and close the pipe. */
1307 if (read(pipe_fds[0], &buf, 1) != 0)
1308 die("failed to sync with parent");
1309 close(pipe_fds[0]);
1310}
1311
1312static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001313{
Lutz Justen13807cb2017-01-03 17:11:55 +01001314 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1315 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001316 die("can only do one of inherit, keep, or set supplementary "
1317 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001318 }
1319
Lutz Justen13807cb2017-01-03 17:11:55 +01001320 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001321 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001322 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001323 } else if (j->flags.set_suppl_gids) {
1324 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001325 pdie("setgroups(suppl_gids) failed");
Lutz Justen13807cb2017-01-03 17:11:55 +01001326 } else if (!j->flags.keep_suppl_gids) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001327 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001328 * Only attempt to clear supplementary groups if we are changing
Lutz Justen13807cb2017-01-03 17:11:55 +01001329 * users or groups.
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001330 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001331 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001332 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001333 }
1334
1335 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001336 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001337
1338 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001339 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001340}
1341
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001342static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1343{
1344 const uint64_t one = 1;
1345 unsigned int i;
1346 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1347 if (keep_mask & (one << i))
1348 continue;
1349 if (prctl(PR_CAPBSET_DROP, i))
1350 pdie("could not drop capability from bounding set");
1351 }
1352}
1353
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001354static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001355{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001356 if (!j->flags.use_caps)
1357 return;
1358
Elly Jonese1749eb2011-10-07 13:54:59 -04001359 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001360 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001361 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001362 unsigned int i;
1363 if (!caps)
1364 die("can't get process caps");
1365 if (cap_clear_flag(caps, CAP_INHERITABLE))
1366 die("can't clear inheritable caps");
1367 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1368 die("can't clear effective caps");
1369 if (cap_clear_flag(caps, CAP_PERMITTED))
1370 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001371 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001372 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001373 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001374 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001375 flag[0] = i;
1376 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001377 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001378 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001379 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001380 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001381 die("can't add inheritable cap");
1382 }
1383 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001384 die("can't apply initial cleaned capset");
1385
1386 /*
1387 * Instead of dropping bounding set first, do it here in case
1388 * the caller had a more permissive bounding set which could
1389 * have been used above to raise a capability that wasn't already
1390 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1391 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001392 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001393
1394 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001395 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001396 flag[0] = CAP_SETPCAP;
1397 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1398 die("can't clear effective cap");
1399 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1400 die("can't clear permitted cap");
1401 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1402 die("can't clear inheritable cap");
1403 }
1404
1405 if (cap_set_proc(caps))
1406 die("can't apply final cleaned capset");
1407
1408 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001409}
1410
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001411static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001412{
1413 /*
1414 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1415 * in the kernel source tree for an explanation of the parameters.
1416 */
1417 if (j->flags.no_new_privs) {
1418 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1419 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1420 }
1421
1422 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001423 * Code running with ASan
1424 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1425 * will make system calls not included in the syscall filter policy,
1426 * which will likely crash the program. Skip setting seccomp filter in
1427 * that case.
1428 * 'running_with_asan()' has no inputs and is completely defined at
1429 * build time, so this cannot be used by an attacker to skip setting
1430 * seccomp filter.
1431 */
1432 if (j->flags.seccomp_filter && running_with_asan()) {
1433 warn("running with ASan, not setting seccomp filter");
1434 return;
1435 }
1436
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001437 if (j->flags.seccomp_filter) {
1438 if (j->flags.seccomp_filter_logging) {
1439 /*
1440 * If logging seccomp filter failures,
1441 * install the SIGSYS handler first.
1442 */
1443 if (install_sigsys_handler())
1444 pdie("failed to install SIGSYS handler");
1445 warn("logging seccomp filter failures");
1446 } else if (j->flags.seccomp_filter_tsync) {
1447 /*
1448 * If setting thread sync,
1449 * reset the SIGSYS signal handler so that
1450 * the entire thread group is killed.
1451 */
1452 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1453 pdie("failed to reset SIGSYS disposition");
1454 info("reset SIGSYS disposition");
1455 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001456 }
1457
1458 /*
1459 * Install the syscall filter.
1460 */
1461 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001462 if (j->flags.seccomp_filter_tsync) {
1463 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1464 SECCOMP_FILTER_FLAG_TSYNC,
1465 j->filter_prog)) {
1466 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001467 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001468 } else {
1469 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1470 j->filter_prog)) {
1471 pdie("prctl(seccomp_filter) failed");
1472 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001473 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001474 }
1475}
1476
Will Drewry6ac91122011-10-21 16:38:58 -05001477void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001478{
Dylan Reidf682d472015-09-17 21:39:07 -07001479 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001480 * If we're dropping caps, get the last valid cap from /proc now,
1481 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001482 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001483 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001484 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001485 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001486
Elly Jonese1749eb2011-10-07 13:54:59 -04001487 if (j->flags.pids)
1488 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001489 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001490
Lutz Justen13807cb2017-01-03 17:11:55 +01001491 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001492 die("cannot inherit supplementary groups without setting a "
1493 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001494
Elly Jonesdd3e8512012-01-23 15:13:38 -05001495 /*
1496 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001497 * so we don't even try. If any of our operations fail, we abort() the
1498 * entire process.
1499 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001500 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001501 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001502
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001503 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001504 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001505 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001506 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001507 * Unless asked not to, remount all filesystems as private.
1508 * If they are shared, new bind mounts will creep out of our
1509 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001510 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1511 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001512 if (!j->flags.skip_remount_private) {
1513 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001514 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1515 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001516 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001517 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001518
Dylan Reidf7942472015-11-18 17:55:26 -08001519 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001520 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001521 }
1522
Dylan Reid1102f5a2015-09-15 11:52:20 -07001523 if (j->flags.enter_net) {
1524 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001525 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001526 } else if (j->flags.net) {
1527 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001528 pdie("unshare(CLONE_NEWNET) failed");
1529 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001530 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001531
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001532 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001533 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001534
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08001535 if (j->flags.new_session_keyring) {
1536 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
1537 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
1538 }
1539
Elly Jones51a5b6c2011-10-12 19:09:26 -04001540 if (j->flags.chroot && enter_chroot(j))
1541 pdie("chroot");
1542
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001543 if (j->flags.pivot_root && enter_pivot_root(j))
1544 pdie("pivot_root");
1545
Martin Pelikánab9eb442017-01-25 11:53:58 +11001546 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07001547 pdie("mount_tmp");
1548
Dylan Reid791f5772015-09-14 20:02:42 -07001549 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001550 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001551
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001552 /*
1553 * If we're only dropping capabilities from the bounding set, but not
1554 * from the thread's (permitted|inheritable|effective) sets, do it now.
1555 */
1556 if (j->flags.capbset_drop) {
1557 drop_capbset(j->cap_bset, last_valid_cap);
1558 }
1559
1560 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001561 /*
1562 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001563 * capability to change uids, our attempt to use setuid()
1564 * below will fail. Hang on to root caps across setuid(), then
1565 * lock securebits.
1566 */
1567 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001568 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001569
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001570 if (lock_securebits() < 0) {
1571 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001572 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001573 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001574
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001575 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001576 /*
1577 * If we're setting no_new_privs, we can drop privileges
1578 * before setting seccomp filter. This way filter policies
1579 * don't need to allow privilege-dropping syscalls.
1580 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001581 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001582 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001583 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001584 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001585 /*
1586 * If we're not setting no_new_privs,
1587 * we need to set seccomp filter *before* dropping privileges.
1588 * WARNING: this means that filter policies *must* allow
1589 * setgroups()/setresgid()/setresuid() for dropping root and
1590 * capget()/capset()/prctl() for dropping caps.
1591 */
1592 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001593 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001594 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001595 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001596
Elly Jonesdd3e8512012-01-23 15:13:38 -05001597 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001598 * Select the specified alternate syscall table. The table must not
1599 * block prctl(2) if we're using seccomp as well.
1600 */
1601 if (j->flags.alt_syscall) {
1602 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001603 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08001604 }
1605
1606 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001607 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001608 * privilege-dropping syscalls :)
1609 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001610 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001611 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001612 warn("seccomp not supported");
1613 return;
1614 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001615 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001616 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001617}
1618
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001619/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001620static int init_exitstatus = 0;
1621
Will Drewry6ac91122011-10-21 16:38:58 -05001622void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001623{
1624 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001625}
1626
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001627void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001628{
1629 pid_t pid;
1630 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001631 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001632 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001633 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001634 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001635 /*
1636 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001637 * left inside our pid namespace or we get a signal.
1638 */
1639 if (pid == rootpid)
1640 init_exitstatus = status;
1641 }
1642 if (!WIFEXITED(init_exitstatus))
1643 _exit(MINIJAIL_ERR_INIT);
1644 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001645}
1646
Will Drewry6ac91122011-10-21 16:38:58 -05001647int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001648{
1649 size_t sz = 0;
1650 size_t bytes = read(fd, &sz, sizeof(sz));
1651 char *buf;
1652 int r;
1653 if (sizeof(sz) != bytes)
1654 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001655 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001656 return -E2BIG;
1657 buf = malloc(sz);
1658 if (!buf)
1659 return -ENOMEM;
1660 bytes = read(fd, buf, sz);
1661 if (bytes != sz) {
1662 free(buf);
1663 return -EINVAL;
1664 }
1665 r = minijail_unmarshal(j, buf, sz);
1666 free(buf);
1667 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001668}
1669
Will Drewry6ac91122011-10-21 16:38:58 -05001670int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001671{
1672 char *buf;
1673 size_t sz = minijail_size(j);
1674 ssize_t written;
1675 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001676
Elly Jonese1749eb2011-10-07 13:54:59 -04001677 if (!sz)
1678 return -EINVAL;
1679 buf = malloc(sz);
1680 r = minijail_marshal(j, buf, sz);
1681 if (r) {
1682 free(buf);
1683 return r;
1684 }
1685 /* Sends [size][minijail]. */
1686 written = write(fd, &sz, sizeof(sz));
1687 if (written != sizeof(sz)) {
1688 free(buf);
1689 return -EFAULT;
1690 }
1691 written = write(fd, buf, sz);
1692 if (written < 0 || (size_t) written != sz) {
1693 free(buf);
1694 return -EFAULT;
1695 }
1696 free(buf);
1697 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001698}
Elly Jonescd7a9042011-07-22 13:56:51 -04001699
Will Drewry6ac91122011-10-21 16:38:58 -05001700int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001701{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001702#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001703 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001704 return 0;
1705#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001706 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1707 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1708 if (!newenv)
1709 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001710
Elly Jonese1749eb2011-10-07 13:54:59 -04001711 /* Only insert a separating space if we have something to separate... */
1712 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1713 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001714
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001715 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001716 setenv(kLdPreloadEnvVar, newenv, 1);
1717 free(newenv);
1718 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001719#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001720}
1721
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001722static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001723{
1724 int r = pipe(fds);
1725 char fd_buf[11];
1726 if (r)
1727 return r;
1728 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1729 if (r <= 0)
1730 return -EINVAL;
1731 setenv(kFdEnvVar, fd_buf, 1);
1732 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001733}
1734
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001735static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07001736{
1737 const char *kFdPath = "/proc/self/fd";
1738
1739 DIR *d = opendir(kFdPath);
1740 struct dirent *dir_entry;
1741
1742 if (d == NULL)
1743 return -1;
1744 int dir_fd = dirfd(d);
1745 while ((dir_entry = readdir(d)) != NULL) {
1746 size_t i;
1747 char *end;
1748 bool should_close = true;
1749 const int fd = strtol(dir_entry->d_name, &end, 10);
1750
1751 if ((*end) != '\0') {
1752 continue;
1753 }
1754 /*
1755 * We might have set up some pipes that we want to share with
1756 * the parent process, and should not be closed.
1757 */
1758 for (i = 0; i < size; ++i) {
1759 if (fd == inheritable_fds[i]) {
1760 should_close = false;
1761 break;
1762 }
1763 }
1764 /* Also avoid closing the directory fd. */
1765 if (should_close && fd != dir_fd)
1766 close(fd);
1767 }
1768 closedir(d);
1769 return 0;
1770}
1771
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001772int minijail_run_internal(struct minijail *j, const char *filename,
1773 char *const argv[], pid_t *pchild_pid,
1774 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1775 int use_preload);
1776
Will Drewry6ac91122011-10-21 16:38:58 -05001777int API minijail_run(struct minijail *j, const char *filename,
1778 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001779{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001780 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1781 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001782}
1783
1784int API minijail_run_pid(struct minijail *j, const char *filename,
1785 char *const argv[], pid_t *pchild_pid)
1786{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001787 return minijail_run_internal(j, filename, argv, pchild_pid,
1788 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001789}
1790
1791int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001792 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001793{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001794 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1795 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001796}
1797
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001798int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001799 char *const argv[], pid_t *pchild_pid,
1800 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001801{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001802 return minijail_run_internal(j, filename, argv, pchild_pid,
1803 pstdin_fd, pstdout_fd, pstderr_fd, true);
1804}
1805
1806int API minijail_run_no_preload(struct minijail *j, const char *filename,
1807 char *const argv[])
1808{
1809 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1810 false);
1811}
1812
Samuel Tan63187f42015-10-16 13:01:53 -07001813int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001814 const char *filename,
1815 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001816 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001817 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001818 int *pstderr_fd)
1819{
Samuel Tan63187f42015-10-16 13:01:53 -07001820 return minijail_run_internal(j, filename, argv, pchild_pid,
1821 pstdin_fd, pstdout_fd, pstderr_fd, false);
1822}
1823
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001824int minijail_run_internal(struct minijail *j, const char *filename,
1825 char *const argv[], pid_t *pchild_pid,
1826 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1827 int use_preload)
1828{
Elly Jonese1749eb2011-10-07 13:54:59 -04001829 char *oldenv, *oldenv_copy = NULL;
1830 pid_t child_pid;
1831 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001832 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001833 int stdout_fds[2];
1834 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001835 int child_sync_pipe_fds[2];
1836 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001837 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001838 /* We need to remember this across the minijail_preexec() call. */
1839 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001840 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001841
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001842 if (use_preload) {
1843 oldenv = getenv(kLdPreloadEnvVar);
1844 if (oldenv) {
1845 oldenv_copy = strdup(oldenv);
1846 if (!oldenv_copy)
1847 return -ENOMEM;
1848 }
1849
1850 if (setup_preload())
1851 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001852 }
Will Drewryf89aef52011-09-16 16:48:57 -05001853
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001854 if (!use_preload) {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04001855 if (j->flags.use_caps && j->caps != 0)
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001856 die("non-empty capabilities are not supported without "
1857 "LD_PRELOAD");
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001858 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001859
Elly Jonesdd3e8512012-01-23 15:13:38 -05001860 /*
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05001861 * Make the process group ID of this process equal to its PID.
1862 * In the non-interactive case (e.g. when the parent process is started
1863 * from init) this ensures the parent process and the jailed process
1864 * can be killed together.
1865 * When the parent process is started from the console this ensures
1866 * the call to setsid(2) in the jailed process succeeds.
1867 *
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001868 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1869 * the process is already a process group leader.
1870 */
1871 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1872 if (errno != EPERM) {
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05001873 pdie("setpgid(0, 0) failed");
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001874 }
1875 }
1876
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001877 if (use_preload) {
1878 /*
1879 * Before we fork(2) and execve(2) the child process, we need
1880 * to open a pipe(2) to send the minijail configuration over.
1881 */
1882 if (setup_pipe(pipe_fds))
1883 return -EFAULT;
1884 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001885
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001886 /*
1887 * If we want to write to the child process' standard input,
1888 * create the pipe(2) now.
1889 */
1890 if (pstdin_fd) {
1891 if (pipe(stdin_fds))
1892 return -EFAULT;
1893 }
1894
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001895 /*
1896 * If we want to read from the child process' standard output,
1897 * create the pipe(2) now.
1898 */
1899 if (pstdout_fd) {
1900 if (pipe(stdout_fds))
1901 return -EFAULT;
1902 }
1903
1904 /*
1905 * If we want to read from the child process' standard error,
1906 * create the pipe(2) now.
1907 */
1908 if (pstderr_fd) {
1909 if (pipe(stderr_fds))
1910 return -EFAULT;
1911 }
1912
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001913 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04001914 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001915 * or if we need to add the child process to cgroups, create the pipe(2)
1916 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001917 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001918 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001919 sync_child = 1;
1920 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001921 return -EFAULT;
1922 }
1923
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001924 /*
1925 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001926 *
1927 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1928 *
1929 * In multithreaded programs, there are a bunch of locks inside libc,
1930 * some of which may be held by other threads at the time that we call
1931 * minijail_run_pid(). If we call fork(), glibc does its level best to
1932 * ensure that we hold all of these locks before it calls clone()
1933 * internally and drop them after clone() returns, but when we call
1934 * sys_clone(2) directly, all that gets bypassed and we end up with a
1935 * child address space where some of libc's important locks are held by
1936 * other threads (which did not get cloned, and hence will never release
1937 * those locks). This is okay so long as we call exec() immediately
1938 * after, but a bunch of seemingly-innocent libc functions like setenv()
1939 * take locks.
1940 *
1941 * Hence, only call sys_clone() if we need to, in order to get at pid
1942 * namespacing. If we follow this path, the child's address space might
1943 * have broken locks; you may only call functions that do not acquire
1944 * any locks.
1945 *
1946 * Unfortunately, fork() acquires every lock it can get its hands on, as
1947 * previously detailed, so this function is highly likely to deadlock
1948 * later on (see "deadlock here") if we're multithreaded.
1949 *
1950 * We might hack around this by having the clone()d child (init of the
1951 * pid namespace) return directly, rather than leaving the clone()d
1952 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001953 * its fork()ed child return in turn), but that process would be
1954 * crippled with its libc locks potentially broken. We might try
1955 * fork()ing in the parent before we clone() to ensure that we own all
1956 * the locks, but then we have to have the forked child hanging around
1957 * consuming resources (and possibly having file descriptors / shared
1958 * memory regions / etc attached). We'd need to keep the child around to
1959 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04001960 *
1961 * TODO(ellyjones): figure out if the "forked child hanging around"
1962 * problem is fixable or not. It would be nice if we worked in this
1963 * case.
1964 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001965 if (pid_namespace) {
1966 int clone_flags = CLONE_NEWPID | SIGCHLD;
1967 if (j->flags.userns)
1968 clone_flags |= CLONE_NEWUSER;
1969 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001970 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001971 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001972 }
Elly Jones761b7412012-06-13 15:49:52 -04001973
Elly Jonese1749eb2011-10-07 13:54:59 -04001974 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001975 if (use_preload) {
1976 free(oldenv_copy);
1977 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001978 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001979 }
Will Drewryf89aef52011-09-16 16:48:57 -05001980
Elly Jonese1749eb2011-10-07 13:54:59 -04001981 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001982 if (use_preload) {
1983 /* Restore parent's LD_PRELOAD. */
1984 if (oldenv_copy) {
1985 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1986 free(oldenv_copy);
1987 } else {
1988 unsetenv(kLdPreloadEnvVar);
1989 }
1990 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001991 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001992
Elly Jonese1749eb2011-10-07 13:54:59 -04001993 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001994
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001995 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001996 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001997
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001998 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001999 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002000
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002001 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002002 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002003
2004 if (sync_child)
2005 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002006
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002007 if (use_preload) {
2008 /* Send marshalled minijail. */
2009 close(pipe_fds[0]); /* read endpoint */
2010 ret = minijail_to_fd(j, pipe_fds[1]);
2011 close(pipe_fds[1]); /* write endpoint */
2012 if (ret) {
2013 kill(j->initpid, SIGKILL);
2014 die("failed to send marshalled minijail");
2015 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002016 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002017
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002018 if (pchild_pid)
2019 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002020
2021 /*
2022 * If we want to write to the child process' standard input,
2023 * set up the write end of the pipe.
2024 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002025 if (pstdin_fd)
2026 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002027 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002028
2029 /*
2030 * If we want to read from the child process' standard output,
2031 * set up the read end of the pipe.
2032 */
2033 if (pstdout_fd)
2034 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002035 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002036
2037 /*
2038 * If we want to read from the child process' standard error,
2039 * set up the read end of the pipe.
2040 */
2041 if (pstderr_fd)
2042 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002043 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002044
Elly Jonese1749eb2011-10-07 13:54:59 -04002045 return 0;
2046 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002047 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002048 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002049
Peter Qiu2860c462015-12-16 15:13:06 -08002050 if (j->flags.reset_signal_mask) {
2051 sigset_t signal_mask;
2052 if (sigemptyset(&signal_mask) != 0)
2053 pdie("sigemptyset failed");
2054 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2055 pdie("sigprocmask failed");
2056 }
2057
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002058 if (j->flags.close_open_fds) {
2059 const size_t kMaxInheritableFdsSize = 10;
2060 int inheritable_fds[kMaxInheritableFdsSize];
2061 size_t size = 0;
2062 if (use_preload) {
2063 inheritable_fds[size++] = pipe_fds[0];
2064 inheritable_fds[size++] = pipe_fds[1];
2065 }
2066 if (sync_child) {
2067 inheritable_fds[size++] = child_sync_pipe_fds[0];
2068 inheritable_fds[size++] = child_sync_pipe_fds[1];
2069 }
2070 if (pstdin_fd) {
2071 inheritable_fds[size++] = stdin_fds[0];
2072 inheritable_fds[size++] = stdin_fds[1];
2073 }
2074 if (pstdout_fd) {
2075 inheritable_fds[size++] = stdout_fds[0];
2076 inheritable_fds[size++] = stdout_fds[1];
2077 }
2078 if (pstderr_fd) {
2079 inheritable_fds[size++] = stderr_fds[0];
2080 inheritable_fds[size++] = stderr_fds[1];
2081 }
2082
2083 if (close_open_fds(inheritable_fds, size) < 0)
2084 die("failed to close open file descriptors");
2085 }
2086
Dylan Reidce5b55e2016-01-13 11:04:16 -08002087 if (sync_child)
2088 wait_for_parent_setup(child_sync_pipe_fds);
2089
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002090 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002091 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002092
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002093 /*
2094 * If we want to write to the jailed process' standard input,
2095 * set up the read end of the pipe.
2096 */
2097 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002098 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2099 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002100 die("failed to set up stdin pipe");
2101 }
2102
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002103 /*
2104 * If we want to read from the jailed process' standard output,
2105 * set up the write end of the pipe.
2106 */
2107 if (pstdout_fd) {
2108 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2109 STDOUT_FILENO) < 0)
2110 die("failed to set up stdout pipe");
2111 }
2112
2113 /*
2114 * If we want to read from the jailed process' standard error,
2115 * set up the write end of the pipe.
2116 */
2117 if (pstderr_fd) {
2118 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2119 STDERR_FILENO) < 0)
2120 die("failed to set up stderr pipe");
2121 }
2122
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002123 /*
2124 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2125 * This prevents the jailed process from using the TIOCSTI ioctl
2126 * to push characters into the parent process terminal's input buffer,
2127 * therefore escaping the jail.
2128 */
2129 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2130 isatty(STDERR_FILENO)) {
2131 if (setsid() < 0) {
2132 pdie("setsid() failed");
2133 }
2134 }
2135
Dylan Reid791f5772015-09-14 20:02:42 -07002136 /* If running an init program, let it decide when/how to mount /proc. */
2137 if (pid_namespace && !do_init)
2138 j->flags.remount_proc_ro = 0;
2139
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002140 if (use_preload) {
2141 /* Strip out flags that cannot be inherited across execve(2). */
2142 minijail_preexec(j);
2143 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002144 /*
2145 * If not using LD_PRELOAD, do all jailing before execve(2).
2146 * Note that PID namespaces can only be entered on fork(2),
2147 * so that flag is still cleared.
2148 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002149 j->flags.pids = 0;
2150 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002151 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002152 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002153
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002154 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002155 /*
2156 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002157 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002158 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002159 * a child to actually run the program. If |do_init == 0|, we
2160 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002161 *
2162 * If we're multithreaded, we'll probably deadlock here. See
2163 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002164 */
2165 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002166 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002167 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002168 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002169 /*
2170 * Best effort. Don't bother checking the return value.
2171 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002172 prctl(PR_SET_NAME, "minijail-init");
2173 init(child_pid); /* Never returns. */
2174 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002175 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002176
Elly Jonesdd3e8512012-01-23 15:13:38 -05002177 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002178 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002179 * calling process
2180 * -> execve()-ing process
2181 * If we are:
2182 * calling process
2183 * -> init()-ing process
2184 * -> execve()-ing process
2185 */
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002186 ret = execve(filename, argv, environ);
2187 if (ret == -1) {
2188 pwarn("execve(%s) failed", filename);
2189 }
2190 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002191}
2192
Will Drewry6ac91122011-10-21 16:38:58 -05002193int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002194{
2195 int st;
2196 if (kill(j->initpid, SIGTERM))
2197 return -errno;
2198 if (waitpid(j->initpid, &st, 0) < 0)
2199 return -errno;
2200 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002201}
2202
Will Drewry6ac91122011-10-21 16:38:58 -05002203int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002204{
2205 int st;
2206 if (waitpid(j->initpid, &st, 0) < 0)
2207 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002208
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002209 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002210 int error_status = st;
2211 if (WIFSIGNALED(st)) {
2212 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002213 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002214 j->initpid, signum);
2215 /*
2216 * We return MINIJAIL_ERR_JAIL if the process received
2217 * SIGSYS, which happens when a syscall is blocked by
2218 * seccomp filters.
2219 * If not, we do what bash(1) does:
2220 * $? = 128 + signum
2221 */
2222 if (signum == SIGSYS) {
2223 error_status = MINIJAIL_ERR_JAIL;
2224 } else {
2225 error_status = 128 + signum;
2226 }
2227 }
2228 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002229 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002230
2231 int exit_status = WEXITSTATUS(st);
2232 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002233 info("child process %d exited with status %d",
2234 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002235
2236 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002237}
2238
Will Drewry6ac91122011-10-21 16:38:58 -05002239void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002240{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002241 size_t i;
2242
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002243 if (j->flags.seccomp_filter && j->filter_prog) {
2244 free(j->filter_prog->filter);
2245 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002246 }
Dylan Reid648b2202015-10-23 00:50:00 -07002247 while (j->mounts_head) {
2248 struct mountpoint *m = j->mounts_head;
2249 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002250 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002251 free(m->type);
2252 free(m->dest);
2253 free(m->src);
2254 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002255 }
Dylan Reid648b2202015-10-23 00:50:00 -07002256 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002257 if (j->user)
2258 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002259 if (j->suppl_gid_list)
2260 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002261 if (j->chrootdir)
2262 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002263 if (j->pid_file_path)
2264 free(j->pid_file_path);
2265 if (j->uidmap)
2266 free(j->uidmap);
2267 if (j->gidmap)
2268 free(j->gidmap);
Andrew Brestickereac28942015-11-11 16:04:46 -08002269 if (j->alt_syscall_table)
2270 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002271 for (i = 0; i < j->cgroup_count; ++i)
2272 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002273 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002274}