blob: d005803dd44babb3ab2caa1d1614e449c72a5cd8 [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>
Will Drewry32ac9f52011-08-18 21:36:27 -050011#include <ctype.h>
Luis Hector Chavez43ff0802016-10-07 12:21:07 -070012#include <dirent.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040013#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070014#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040015#include <grp.h>
16#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050017#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040018#include <linux/capability.h>
Mike Frysinger7559dfe2016-11-15 18:58:39 -050019#include <net/if.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040020#include <pwd.h>
21#include <sched.h>
22#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050023#include <stdarg.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070024#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080025#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040029#include <sys/capability.h>
30#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050031#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040032#include <sys/prctl.h>
Mike Frysinger7559dfe2016-11-15 18:58:39 -050033#include <sys/socket.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070034#include <sys/stat.h>
35#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080036#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040037#include <sys/wait.h>
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -070038#include <syscall.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040039#include <unistd.h>
40
41#include "libminijail.h"
42#include "libminijail-private.h"
43
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070044#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080045#include "syscall_filter.h"
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040046#include "syscall_wrapper.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070047#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080048
Lei Zhangeee31552012-10-17 21:27:10 -070049#ifdef HAVE_SECUREBITS_H
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070050# include <linux/securebits.h>
Lei Zhangeee31552012-10-17 21:27:10 -070051#else
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070052# define SECURE_ALL_BITS 0x55
53# define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
54#endif
55/* For kernels < 4.3. */
56#define OLD_SECURE_ALL_BITS 0x15
57#define OLD_SECURE_ALL_LOCKS (OLD_SECURE_ALL_BITS << 1)
58
59/*
60 * Assert the value of SECURE_ALL_BITS at compile-time.
61 * Brillo devices are currently compiled against 4.4 kernel headers. Kernel 4.3
62 * added a new securebit.
63 * When a new securebit is added, the new SECURE_ALL_BITS mask will return EPERM
64 * when used on older kernels. The compile-time assert will catch this situation
65 * at compile time.
66 */
67#ifdef __BRILLO__
68_Static_assert(SECURE_ALL_BITS == 0x55, "SECURE_ALL_BITS == 0x55.");
Lei Zhangeee31552012-10-17 21:27:10 -070069#endif
70
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070071/* Until these are reliably available in linux/prctl.h. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080072#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070073# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080074#endif
75
Andrew Brestickereac28942015-11-11 16:04:46 -080076#ifndef PR_ALT_SYSCALL
77# define PR_ALT_SYSCALL 0x43724f53
78#endif
79
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040080/* Seccomp filter related flags. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080081#ifndef PR_SET_NO_NEW_PRIVS
82# define PR_SET_NO_NEW_PRIVS 38
83#endif
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040084
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080085#ifndef SECCOMP_MODE_FILTER
86# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050087#endif
88
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040089#ifndef SECCOMP_SET_MODE_STRICT
90# define SECCOMP_SET_MODE_STRICT 0
91#endif
92#ifndef SECCOMP_SET_MODE_FILTER
93# define SECCOMP_SET_MODE_FILTER 1
94#endif
95
96#ifndef SECCOMP_FILTER_FLAG_TSYNC
97# define SECCOMP_FILTER_FLAG_TSYNC 1
98#endif
99/* End seccomp filter related flags. */
100
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700101/* New cgroup namespace might not be in linux-headers yet. */
102#ifndef CLONE_NEWCGROUP
103# define CLONE_NEWCGROUP 0x02000000
104#endif
105
Dylan Reid605ce7f2016-01-19 19:21:00 -0800106#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
107
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800108/* Keyctl commands. */
109#define KEYCTL_JOIN_SESSION_KEYRING 1
110
Dylan Reid648b2202015-10-23 00:50:00 -0700111struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400112 char *src;
113 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700114 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700115 char *data;
116 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -0700117 unsigned long flags;
118 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400119};
120
Will Drewryf89aef52011-09-16 16:48:57 -0500121struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700122 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700123 * WARNING: if you add a flag here you need to make sure it's
124 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700125 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400126 struct {
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700127 int uid : 1;
128 int gid : 1;
Lutz Justen13807cb2017-01-03 17:11:55 +0100129 int inherit_suppl_gids : 1;
130 int set_suppl_gids : 1;
131 int keep_suppl_gids : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700132 int use_caps : 1;
133 int capbset_drop : 1;
134 int vfs : 1;
135 int enter_vfs : 1;
136 int skip_remount_private : 1;
137 int pids : 1;
138 int ipc : 1;
139 int net : 1;
140 int enter_net : 1;
141 int ns_cgroups : 1;
142 int userns : 1;
143 int disable_setgroups : 1;
144 int seccomp : 1;
145 int remount_proc_ro : 1;
146 int no_new_privs : 1;
147 int seccomp_filter : 1;
148 int seccomp_filter_tsync : 1;
149 int seccomp_filter_logging : 1;
150 int chroot : 1;
151 int pivot_root : 1;
152 int mount_tmp : 1;
153 int do_init : 1;
154 int pid_file : 1;
155 int cgroups : 1;
156 int alt_syscall : 1;
157 int reset_signal_mask : 1;
158 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800159 int new_session_keyring : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400160 } flags;
161 uid_t uid;
162 gid_t gid;
163 gid_t usergid;
164 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800165 size_t suppl_gid_count;
166 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400167 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800168 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400169 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700170 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700171 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400172 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800173 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800174 char *uidmap;
175 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800176 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800177 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800178 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700179 struct mountpoint *mounts_head;
180 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800181 size_t mounts_count;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100182 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800183 char *cgroups[MAX_CGROUPS];
184 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500185};
186
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700187/*
188 * Strip out flags meant for the parent.
189 * We keep things that are not inherited across execve(2) (e.g. capabilities),
190 * or are easier to set after execve(2) (e.g. seccomp filters).
191 */
192void minijail_preenter(struct minijail *j)
193{
194 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700195 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900196 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700197 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700198 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800199 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800200 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800201 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700202}
203
204/*
205 * Strip out flags meant for the child.
206 * We keep things that are inherited across execve(2).
207 */
208void minijail_preexec(struct minijail *j)
209{
210 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700211 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800212 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700213 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800214 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700215 if (j->user)
216 free(j->user);
217 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800218 if (j->suppl_gid_list)
219 free(j->suppl_gid_list);
220 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700221 memset(&j->flags, 0, sizeof(j->flags));
222 /* Now restore anything we meant to keep. */
223 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700224 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800225 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700226 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800227 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700228 /* Note, |pids| will already have been used before this call. */
229}
230
231/* Minijail API. */
232
Will Drewry6ac91122011-10-21 16:38:58 -0500233struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400234{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400235 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400236}
237
Will Drewry6ac91122011-10-21 16:38:58 -0500238void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400239{
240 if (uid == 0)
241 die("useless change to uid 0");
242 j->uid = uid;
243 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400244}
245
Will Drewry6ac91122011-10-21 16:38:58 -0500246void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400247{
248 if (gid == 0)
249 die("useless change to gid 0");
250 j->gid = gid;
251 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400252}
253
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800254void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
255 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800256{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800257 size_t i;
258
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500259 if (j->flags.inherit_suppl_gids)
260 die("cannot inherit *and* set supplementary groups");
261 if (j->flags.keep_suppl_gids)
262 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800263
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800264 if (size == 0) {
265 /* Clear supplementary groups. */
266 j->suppl_gid_list = NULL;
267 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100268 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800269 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800270 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800271
272 /* Copy the gid_t array. */
273 j->suppl_gid_list = calloc(size, sizeof(gid_t));
274 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800275 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800276 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800277 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800278 j->suppl_gid_list[i] = list[i];
279 }
280 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100281 j->flags.set_suppl_gids = 1;
282}
283
284void API minijail_keep_supplementary_gids(struct minijail *j) {
285 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800286}
287
Will Drewry6ac91122011-10-21 16:38:58 -0500288int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400289{
290 char *buf = NULL;
291 struct passwd pw;
292 struct passwd *ppw = NULL;
293 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
294 if (sz == -1)
295 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400296
Elly Jonesdd3e8512012-01-23 15:13:38 -0500297 /*
298 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400299 * the maximum needed size of the buffer, so we don't have to search.
300 */
301 buf = malloc(sz);
302 if (!buf)
303 return -ENOMEM;
304 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500305 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800306 * We're safe to free the buffer here. The strings inside |pw| point
307 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800308 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
309 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500310 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400311 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700312 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400313 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700314 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400315 minijail_change_uid(j, ppw->pw_uid);
316 j->user = strdup(user);
317 if (!j->user)
318 return -ENOMEM;
319 j->usergid = ppw->pw_gid;
320 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400321}
322
Will Drewry6ac91122011-10-21 16:38:58 -0500323int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400324{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700325 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700326 struct group gr;
327 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400328 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
329 if (sz == -1)
330 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400331
Elly Jonesdd3e8512012-01-23 15:13:38 -0500332 /*
333 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400334 * the maximum needed size of the buffer, so we don't have to search.
335 */
336 buf = malloc(sz);
337 if (!buf)
338 return -ENOMEM;
339 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500340 /*
341 * We're safe to free the buffer here. The strings inside gr point
342 * inside buf, but we don't use any of them; this leaves the pointers
343 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
344 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400345 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700346 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400347 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700348 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400349 minijail_change_gid(j, pgr->gr_gid);
350 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400351}
352
Will Drewry6ac91122011-10-21 16:38:58 -0500353void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400354{
355 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400356}
357
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700358void API minijail_no_new_privs(struct minijail *j)
359{
360 j->flags.no_new_privs = 1;
361}
362
Will Drewry6ac91122011-10-21 16:38:58 -0500363void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400364{
365 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500366}
367
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400368void API minijail_set_seccomp_filter_tsync(struct minijail *j)
369{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400370 if (j->filter_len > 0 && j->filter_prog != NULL) {
371 die("minijail_set_seccomp_filter_tsync() must be called "
372 "before minijail_parse_seccomp_filters()");
373 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400374 j->flags.seccomp_filter_tsync = 1;
375}
376
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700377void API minijail_log_seccomp_filter_failures(struct minijail *j)
378{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400379 if (j->filter_len > 0 && j->filter_prog != NULL) {
380 die("minijail_log_seccomp_filter_failures() must be called "
381 "before minijail_parse_seccomp_filters()");
382 }
383 j->flags.seccomp_filter_logging = 1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700384}
385
Will Drewry6ac91122011-10-21 16:38:58 -0500386void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400387{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800388 /*
389 * 'minijail_use_caps' configures a runtime-capabilities-only
390 * environment, including a bounding set matching the thread's runtime
391 * (permitted|inheritable|effective) sets.
392 * Therefore, it will override any existing bounding set configurations
393 * since the latter would allow gaining extra runtime capabilities from
394 * file capabilities.
395 */
396 if (j->flags.capbset_drop) {
397 warn("overriding bounding set configuration");
398 j->cap_bset = 0;
399 j->flags.capbset_drop = 0;
400 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400401 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800402 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400403}
404
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800405void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
406{
407 if (j->flags.use_caps) {
408 /*
409 * 'minijail_use_caps' will have already configured a capability
410 * bounding set matching the (permitted|inheritable|effective)
411 * sets. Abort if the user tries to configure a separate
412 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
413 * are mutually exclusive.
414 */
415 die("runtime capabilities already configured, can't drop "
416 "bounding set separately");
417 }
418 j->cap_bset = capmask;
419 j->flags.capbset_drop = 1;
420}
421
422void API minijail_reset_signal_mask(struct minijail *j)
423{
Peter Qiu2860c462015-12-16 15:13:06 -0800424 j->flags.reset_signal_mask = 1;
425}
426
Will Drewry6ac91122011-10-21 16:38:58 -0500427void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400428{
429 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400430}
431
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700432void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
433{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800434 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700435 if (ns_fd < 0) {
436 pdie("failed to open namespace '%s'", ns_path);
437 }
438 j->mountns_fd = ns_fd;
439 j->flags.enter_vfs = 1;
440}
441
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800442void API minijail_new_session_keyring(struct minijail *j)
443{
444 j->flags.new_session_keyring = 1;
445}
446
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800447void API minijail_skip_remount_private(struct minijail *j)
448{
449 j->flags.skip_remount_private = 1;
450}
451
Will Drewry6ac91122011-10-21 16:38:58 -0500452void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400453{
Elly Jonese58176c2012-01-23 11:46:17 -0500454 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700455 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400456 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800457 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400458}
459
Dylan Reidf7942472015-11-18 17:55:26 -0800460void API minijail_namespace_ipc(struct minijail *j)
461{
462 j->flags.ipc = 1;
463}
464
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400465void API minijail_namespace_net(struct minijail *j)
466{
467 j->flags.net = 1;
468}
469
Dylan Reid1102f5a2015-09-15 11:52:20 -0700470void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
471{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800472 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700473 if (ns_fd < 0) {
474 pdie("failed to open namespace '%s'", ns_path);
475 }
476 j->netns_fd = ns_fd;
477 j->flags.enter_net = 1;
478}
479
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700480void API minijail_namespace_cgroups(struct minijail *j)
481{
482 j->flags.ns_cgroups = 1;
483}
484
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700485void API minijail_close_open_fds(struct minijail *j)
486{
487 j->flags.close_open_fds = 1;
488}
489
Dylan Reid791f5772015-09-14 20:02:42 -0700490void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400491{
492 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700493 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400494}
495
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800496void API minijail_namespace_user(struct minijail *j)
497{
498 j->flags.userns = 1;
499}
500
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400501void API minijail_namespace_user_disable_setgroups(struct minijail *j)
502{
503 j->flags.disable_setgroups = 1;
504}
505
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800506int API minijail_uidmap(struct minijail *j, const char *uidmap)
507{
508 j->uidmap = strdup(uidmap);
509 if (!j->uidmap)
510 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800511 char *ch;
512 for (ch = j->uidmap; *ch; ch++) {
513 if (*ch == ',')
514 *ch = '\n';
515 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800516 return 0;
517}
518
519int API minijail_gidmap(struct minijail *j, const char *gidmap)
520{
521 j->gidmap = strdup(gidmap);
522 if (!j->gidmap)
523 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800524 char *ch;
525 for (ch = j->gidmap; *ch; ch++) {
526 if (*ch == ',')
527 *ch = '\n';
528 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800529 return 0;
530}
531
Will Drewry6ac91122011-10-21 16:38:58 -0500532void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400533{
Lutz Justen13807cb2017-01-03 17:11:55 +0100534 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400535}
536
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800537void API minijail_run_as_init(struct minijail *j)
538{
539 /*
540 * Since the jailed program will become 'init' in the new PID namespace,
541 * Minijail does not need to fork an 'init' process.
542 */
543 j->flags.do_init = 0;
544}
545
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700546int API minijail_enter_chroot(struct minijail *j, const char *dir)
547{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400548 if (j->chrootdir)
549 return -EINVAL;
550 j->chrootdir = strdup(dir);
551 if (!j->chrootdir)
552 return -ENOMEM;
553 j->flags.chroot = 1;
554 return 0;
555}
556
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800557int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
558{
559 if (j->chrootdir)
560 return -EINVAL;
561 j->chrootdir = strdup(dir);
562 if (!j->chrootdir)
563 return -ENOMEM;
564 j->flags.pivot_root = 1;
565 return 0;
566}
567
Dylan Reida14e08d2015-10-22 21:05:29 -0700568char API *minijail_get_original_path(struct minijail *j,
569 const char *path_inside_chroot)
570{
Dylan Reid648b2202015-10-23 00:50:00 -0700571 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700572
Dylan Reid648b2202015-10-23 00:50:00 -0700573 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700574 while (b) {
575 /*
576 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700577 * mount, then the original path is exactly the source of
578 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700579 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700580 * mount source = /some/path/exe, mount dest =
581 * /chroot/path/exe Then when getting the original path of
582 * "/chroot/path/exe", the source of that mount,
583 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700584 */
585 if (!strcmp(b->dest, path_inside_chroot))
586 return strdup(b->src);
587
588 /*
589 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700590 * mount, take the suffix of the chroot path relative to the
591 * mount destination path, and append it to the mount source
592 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700593 */
594 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
595 const char *relative_path =
596 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400597 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700598 }
599 b = b->next;
600 }
601
602 /* If there is a chroot path, append |path_inside_chroot| to that. */
603 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400604 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700605
606 /* No chroot, so the path outside is the same as it is inside. */
607 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700608}
609
Martin Pelikánab9eb442017-01-25 11:53:58 +1100610size_t minijail_get_tmpfs_size(const struct minijail *j)
611{
612 return j->tmpfs_size;
613}
614
Lee Campbell11af0622014-05-22 12:36:04 -0700615void API minijail_mount_tmp(struct minijail *j)
616{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100617 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
618}
619
620void API minijail_mount_tmp_size(struct minijail *j, size_t size)
621{
622 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700623 j->flags.mount_tmp = 1;
624}
625
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800626int API minijail_write_pid_file(struct minijail *j, const char *path)
627{
628 j->pid_file_path = strdup(path);
629 if (!j->pid_file_path)
630 return -ENOMEM;
631 j->flags.pid_file = 1;
632 return 0;
633}
634
Dylan Reid605ce7f2016-01-19 19:21:00 -0800635int API minijail_add_to_cgroup(struct minijail *j, const char *path)
636{
637 if (j->cgroup_count >= MAX_CGROUPS)
638 return -ENOMEM;
639 j->cgroups[j->cgroup_count] = strdup(path);
640 if (!j->cgroups[j->cgroup_count])
641 return -ENOMEM;
642 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800643 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800644 return 0;
645}
646
Dylan Reid81e23972016-05-18 14:06:35 -0700647int API minijail_mount_with_data(struct minijail *j, const char *src,
648 const char *dest, const char *type,
649 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700650{
Dylan Reid648b2202015-10-23 00:50:00 -0700651 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400652
653 if (*dest != '/')
654 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700655 m = calloc(1, sizeof(*m));
656 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400657 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700658 m->dest = strdup(dest);
659 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400660 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700661 m->src = strdup(src);
662 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400663 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700664 m->type = strdup(type);
665 if (!m->type)
666 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700667 if (data) {
668 m->data = strdup(data);
669 if (!m->data)
670 goto error;
671 m->has_data = 1;
672 }
Dylan Reid648b2202015-10-23 00:50:00 -0700673 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400674
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800675 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400676
Elly Jonesdd3e8512012-01-23 15:13:38 -0500677 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700678 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400679 * containing vfs namespace.
680 */
681 minijail_namespace_vfs(j);
682
Dylan Reid648b2202015-10-23 00:50:00 -0700683 if (j->mounts_tail)
684 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400685 else
Dylan Reid648b2202015-10-23 00:50:00 -0700686 j->mounts_head = m;
687 j->mounts_tail = m;
688 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400689
690 return 0;
691
692error:
Dylan Reid81e23972016-05-18 14:06:35 -0700693 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700694 free(m->src);
695 free(m->dest);
696 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400697 return -ENOMEM;
698}
699
Dylan Reid81e23972016-05-18 14:06:35 -0700700int API minijail_mount(struct minijail *j, const char *src, const char *dest,
701 const char *type, unsigned long flags)
702{
703 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
704}
705
Dylan Reid648b2202015-10-23 00:50:00 -0700706int API minijail_bind(struct minijail *j, const char *src, const char *dest,
707 int writeable)
708{
709 unsigned long flags = MS_BIND;
710
711 if (!writeable)
712 flags |= MS_RDONLY;
713
714 return minijail_mount(j, src, dest, "", flags);
715}
716
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400717static void clear_seccomp_options(struct minijail *j)
718{
719 j->flags.seccomp_filter = 0;
720 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400721 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400722 j->filter_len = 0;
723 j->filter_prog = NULL;
724 j->flags.no_new_privs = 0;
725}
726
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400727static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400728{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400729 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400730 /*
731 * |errno| will be set to EINVAL when seccomp has not been
732 * compiled into the kernel. On certain platforms and kernel
733 * versions this is not a fatal failure. In that case, and only
734 * in that case, disable seccomp and skip loading the filters.
735 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400736 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400737 warn("not loading seccomp filters, seccomp filter not "
738 "supported");
739 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400740 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700741 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400742 /*
743 * If |errno| != EINVAL or seccomp_can_softfail() is false,
744 * we can proceed. Worst case scenario minijail_enter() will
745 * abort() if seccomp fails.
746 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700747 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400748 if (j->flags.seccomp_filter_tsync) {
749 /* Are the seccomp(2) syscall and the TSYNC option supported? */
750 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
751 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
752 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400753 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
754 warn("seccomp(2) syscall not supported");
755 clear_seccomp_options(j);
756 return 0;
757 } else if (saved_errno == EINVAL &&
758 seccomp_can_softfail()) {
759 warn(
760 "seccomp filter thread sync not supported");
761 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400762 return 0;
763 }
764 /*
765 * Similar logic here. If seccomp_can_softfail() is
766 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
767 * we can proceed. Worst case scenario minijail_enter()
768 * will abort() if seccomp or TSYNC fail.
769 */
770 }
771 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400772 return 1;
773}
774
775static int parse_seccomp_filters(struct minijail *j, FILE *policy_file)
776{
777 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400778 int use_ret_trap =
779 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
780 int allow_logging = j->flags.seccomp_filter_logging;
781
782 if (compile_filter(policy_file, fprog, use_ret_trap, allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400783 free(fprog);
784 return -1;
785 }
786
787 j->filter_len = fprog->len;
788 j->filter_prog = fprog;
789 return 0;
790}
791
792void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
793{
794 if (!seccomp_should_parse_filters(j))
795 return;
796
Elly Jonese1749eb2011-10-07 13:54:59 -0400797 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800798 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700799 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400800 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800801
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400802 if (parse_seccomp_filters(j, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700803 die("failed to compile seccomp filter BPF program in '%s'",
804 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800805 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400806 fclose(file);
807}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800808
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400809void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
810{
811 if (!seccomp_should_parse_filters(j))
812 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800813
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400814 FILE *file = fdopen(fd, "r");
815 if (!file) {
816 pdie("failed to associate stream with fd %d", fd);
817 }
818
819 if (parse_seccomp_filters(j, file) != 0) {
820 die("failed to compile seccomp filter BPF program from fd %d",
821 fd);
822 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400823 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500824}
825
Andrew Brestickereac28942015-11-11 16:04:46 -0800826int API minijail_use_alt_syscall(struct minijail *j, const char *table)
827{
828 j->alt_syscall_table = strdup(table);
829 if (!j->alt_syscall_table)
830 return -ENOMEM;
831 j->flags.alt_syscall = 1;
832 return 0;
833}
834
Will Drewryf89aef52011-09-16 16:48:57 -0500835struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400836 size_t available;
837 size_t total;
838 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500839};
840
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800841void marshal_state_init(struct marshal_state *state, char *buf,
842 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400843{
844 state->available = available;
845 state->buf = buf;
846 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500847}
848
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800849void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400850{
851 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500852
Elly Jonese1749eb2011-10-07 13:54:59 -0400853 /* Up to |available| will be written. */
854 if (copy_len) {
855 memcpy(state->buf, src, copy_len);
856 state->buf += copy_len;
857 state->available -= copy_len;
858 }
859 /* |total| will contain the expected length. */
860 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500861}
862
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400863void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700864{
865 marshal_append(state, m->src, strlen(m->src) + 1);
866 marshal_append(state, m->dest, strlen(m->dest) + 1);
867 marshal_append(state, m->type, strlen(m->type) + 1);
868 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
869 if (m->has_data)
870 marshal_append(state, m->data, strlen(m->data) + 1);
871 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
872}
873
Will Drewry6ac91122011-10-21 16:38:58 -0500874void minijail_marshal_helper(struct marshal_state *state,
875 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400876{
Dylan Reid648b2202015-10-23 00:50:00 -0700877 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800878 size_t i;
879
Elly Jonese1749eb2011-10-07 13:54:59 -0400880 marshal_append(state, (char *)j, sizeof(*j));
881 if (j->user)
882 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800883 if (j->suppl_gid_list) {
884 marshal_append(state, j->suppl_gid_list,
885 j->suppl_gid_count * sizeof(gid_t));
886 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400887 if (j->chrootdir)
888 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800889 if (j->alt_syscall_table) {
890 marshal_append(state, j->alt_syscall_table,
891 strlen(j->alt_syscall_table) + 1);
892 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800893 if (j->flags.seccomp_filter && j->filter_prog) {
894 struct sock_fprog *fp = j->filter_prog;
895 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800896 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400897 }
Dylan Reid648b2202015-10-23 00:50:00 -0700898 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400899 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400900 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800901 for (i = 0; i < j->cgroup_count; ++i)
902 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500903}
904
Will Drewry6ac91122011-10-21 16:38:58 -0500905size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400906{
907 struct marshal_state state;
908 marshal_state_init(&state, NULL, 0);
909 minijail_marshal_helper(&state, j);
910 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500911}
912
Elly Jonese1749eb2011-10-07 13:54:59 -0400913int minijail_marshal(const struct minijail *j, char *buf, size_t available)
914{
915 struct marshal_state state;
916 marshal_state_init(&state, buf, available);
917 minijail_marshal_helper(&state, j);
918 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500919}
920
Elly Jonese1749eb2011-10-07 13:54:59 -0400921int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
922{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800923 size_t i;
924 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500925 int ret = -EINVAL;
926
Elly Jonese1749eb2011-10-07 13:54:59 -0400927 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500928 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400929 memcpy((void *)j, serialized, sizeof(*j));
930 serialized += sizeof(*j);
931 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500932
Will Drewrybee7ba72011-10-21 20:47:01 -0500933 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -0400934 j->pid_file_path = NULL;
935 j->uidmap = NULL;
936 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -0700937 j->mounts_head = NULL;
938 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800939 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500940
Elly Jonese1749eb2011-10-07 13:54:59 -0400941 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400942 char *user = consumestr(&serialized, &length);
943 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500944 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400945 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500946 if (!j->user)
947 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400948 }
Will Drewryf89aef52011-09-16 16:48:57 -0500949
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800950 if (j->suppl_gid_list) { /* stale pointer */
951 if (j->suppl_gid_count > NGROUPS_MAX) {
952 goto bad_gid_list;
953 }
954 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
955 void *gid_list_bytes =
956 consumebytes(gid_list_size, &serialized, &length);
957 if (!gid_list_bytes)
958 goto bad_gid_list;
959
960 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
961 if (!j->suppl_gid_list)
962 goto bad_gid_list;
963
964 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
965 }
966
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400967 if (j->chrootdir) { /* stale pointer */
968 char *chrootdir = consumestr(&serialized, &length);
969 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500970 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400971 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500972 if (!j->chrootdir)
973 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400974 }
975
Andrew Brestickereac28942015-11-11 16:04:46 -0800976 if (j->alt_syscall_table) { /* stale pointer */
977 char *alt_syscall_table = consumestr(&serialized, &length);
978 if (!alt_syscall_table)
979 goto bad_syscall_table;
980 j->alt_syscall_table = strdup(alt_syscall_table);
981 if (!j->alt_syscall_table)
982 goto bad_syscall_table;
983 }
984
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800985 if (j->flags.seccomp_filter && j->filter_len > 0) {
986 size_t ninstrs = j->filter_len;
987 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
988 ninstrs > USHRT_MAX)
989 goto bad_filters;
990
991 size_t program_len = ninstrs * sizeof(struct sock_filter);
992 void *program = consumebytes(program_len, &serialized, &length);
993 if (!program)
994 goto bad_filters;
995
996 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800997 if (!j->filter_prog)
998 goto bad_filters;
999
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001000 j->filter_prog->len = ninstrs;
1001 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001002 if (!j->filter_prog->filter)
1003 goto bad_filter_prog_instrs;
1004
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001005 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001006 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001007
Dylan Reid648b2202015-10-23 00:50:00 -07001008 count = j->mounts_count;
1009 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001010 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001011 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001012 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001013 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001014 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001015 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001016 const char *src = consumestr(&serialized, &length);
1017 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001018 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001019 dest = consumestr(&serialized, &length);
1020 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001021 goto bad_mounts;
1022 type = consumestr(&serialized, &length);
1023 if (!type)
1024 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001025 has_data = consumebytes(sizeof(*has_data), &serialized,
1026 &length);
1027 if (!has_data)
1028 goto bad_mounts;
1029 if (*has_data) {
1030 data = consumestr(&serialized, &length);
1031 if (!data)
1032 goto bad_mounts;
1033 }
Dylan Reid648b2202015-10-23 00:50:00 -07001034 flags = consumebytes(sizeof(*flags), &serialized, &length);
1035 if (!flags)
1036 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001037 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001038 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001039 }
1040
Dylan Reid605ce7f2016-01-19 19:21:00 -08001041 count = j->cgroup_count;
1042 j->cgroup_count = 0;
1043 for (i = 0; i < count; ++i) {
1044 char *cgroup = consumestr(&serialized, &length);
1045 if (!cgroup)
1046 goto bad_cgroups;
1047 j->cgroups[i] = strdup(cgroup);
1048 if (!j->cgroups[i])
1049 goto bad_cgroups;
1050 ++j->cgroup_count;
1051 }
1052
Elly Jonese1749eb2011-10-07 13:54:59 -04001053 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001054
Dylan Reid605ce7f2016-01-19 19:21:00 -08001055bad_cgroups:
1056 while (j->mounts_head) {
1057 struct mountpoint *m = j->mounts_head;
1058 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07001059 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001060 free(m->type);
1061 free(m->dest);
1062 free(m->src);
1063 free(m);
1064 }
1065 for (i = 0; i < j->cgroup_count; ++i)
1066 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001067bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001068 if (j->flags.seccomp_filter && j->filter_len > 0) {
1069 free(j->filter_prog->filter);
1070 free(j->filter_prog);
1071 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001072bad_filter_prog_instrs:
1073 if (j->filter_prog)
1074 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001075bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001076 if (j->alt_syscall_table)
1077 free(j->alt_syscall_table);
1078bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001079 if (j->chrootdir)
1080 free(j->chrootdir);
1081bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001082 if (j->suppl_gid_list)
1083 free(j->suppl_gid_list);
1084bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001085 if (j->user)
1086 free(j->user);
1087clear_pointers:
1088 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001089 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001090 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001091 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001092 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001093out:
1094 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001095}
1096
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001097/*
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001098 * setup_mount_destination: Ensures the mount target exists.
1099 * Creates it if needed and possible.
Dylan Reideec77962016-06-30 19:35:10 -07001100 */
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001101static int setup_mount_destination(const char *source, const char *dest,
1102 uid_t uid, uid_t gid)
Dylan Reideec77962016-06-30 19:35:10 -07001103{
1104 int rc;
1105 struct stat st_buf;
1106
1107 rc = stat(dest, &st_buf);
1108 if (rc == 0) /* destination exists */
1109 return 0;
1110
1111 /*
1112 * Try to create the destination.
1113 * Either make a directory or touch a file depending on the source type.
1114 * If the source doesn't exist, assume it is a filesystem type such as
1115 * "tmpfs" and create a directory to mount it on.
1116 */
1117 rc = stat(source, &st_buf);
1118 if (rc || S_ISDIR(st_buf.st_mode) || S_ISBLK(st_buf.st_mode)) {
1119 if (mkdir(dest, 0700))
1120 return -errno;
1121 } else {
1122 int fd = open(dest, O_RDWR | O_CREAT, 0700);
1123 if (fd < 0)
1124 return -errno;
1125 close(fd);
1126 }
1127 return chown(dest, uid, gid);
1128}
1129
1130/*
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001131 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001132 * @j Minijail these mounts are for
1133 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001134 *
1135 * Returns 0 for success.
1136 */
Dylan Reid648b2202015-10-23 00:50:00 -07001137static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001138{
Dylan Reid648b2202015-10-23 00:50:00 -07001139 int ret;
1140 char *dest;
1141 int remount_ro = 0;
1142
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001143 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001144 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001145 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001146
Dylan Reideec77962016-06-30 19:35:10 -07001147 if (setup_mount_destination(m->src, dest, j->uid, j->gid))
1148 pdie("creating mount target '%s' failed", dest);
1149
Dylan Reid648b2202015-10-23 00:50:00 -07001150 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001151 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1152 * can't both be specified in the original bind mount.
1153 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001154 */
1155 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1156 remount_ro = 1;
1157 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001158 }
Dylan Reid648b2202015-10-23 00:50:00 -07001159
Dylan Reid81e23972016-05-18 14:06:35 -07001160 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001161 if (ret)
1162 pdie("mount: %s -> %s", m->src, dest);
1163
1164 if (remount_ro) {
1165 m->flags |= MS_RDONLY;
1166 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001167 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001168 if (ret)
1169 pdie("bind ro: %s -> %s", m->src, dest);
1170 }
1171
Elly Jones51a5b6c2011-10-12 19:09:26 -04001172 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001173 if (m->next)
1174 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001175 return ret;
1176}
1177
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001178static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001179{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001180 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001181
1182 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001183 return ret;
1184
1185 if (chroot(j->chrootdir))
1186 return -errno;
1187
1188 if (chdir("/"))
1189 return -errno;
1190
1191 return 0;
1192}
1193
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001194static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001195{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001196 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001197
1198 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001199 return ret;
1200
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001201 /*
1202 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001203 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001204 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001205 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001206 if (oldroot < 0)
1207 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001208 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001209 if (newroot < 0)
1210 pdie("failed to open %s for fchdir", j->chrootdir);
1211
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001212 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001213 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001214 * do a self bind mount.
1215 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001216 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1217 pdie("failed to bind mount '%s'", j->chrootdir);
1218 if (chdir(j->chrootdir))
1219 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001220 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001221 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001222
1223 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001224 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001225 * change to the old root and unmount it.
1226 */
1227 if (fchdir(oldroot))
1228 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001229
1230 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001231 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1232 * there could be a shared mount point under |oldroot|. In that case,
1233 * mounts under this shared mount point will be unmounted below, and
1234 * this unmounting will propagate to the original mount namespace
1235 * (because the mount point is shared). To prevent this unexpected
1236 * unmounting, remove these mounts from their peer groups by recursively
1237 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001238 */
1239 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001240 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001241 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001242 if (umount2(".", MNT_DETACH))
1243 pdie("umount(/)");
1244 /* Change back to the new root. */
1245 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001246 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001247 if (close(oldroot))
1248 return -errno;
1249 if (close(newroot))
1250 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001251 if (chroot("/"))
1252 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001253 /* Set correct CWD for getcwd(3). */
1254 if (chdir("/"))
1255 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001256
1257 return 0;
1258}
1259
Martin Pelikánab9eb442017-01-25 11:53:58 +11001260static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001261{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001262 const char fmt[] = "size=%zu,mode=1777";
1263 /* Count for the user storing ULLONG_MAX literally + extra space. */
1264 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1265 int ret;
1266
1267 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1268
1269 if (ret <= 0)
1270 pdie("tmpfs size spec error");
1271 else if ((size_t)ret >= sizeof(data))
1272 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001273 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001274 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001275}
1276
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001277static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001278{
1279 const char *kProcPath = "/proc";
1280 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001281 /*
1282 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001283 * /proc in our namespace, which means using MS_REMOUNT here would
1284 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001285 * namespace (!). Instead, remove their mount from our namespace lazily
1286 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001287 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001288 if (umount2(kProcPath, MNT_DETACH)) {
1289 /*
1290 * If we are in a new user namespace, umount(2) will fail.
1291 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1292 */
1293 if (j->flags.userns) {
1294 info("umount(/proc, MNT_DETACH) failed, "
1295 "this is expected when using user namespaces");
1296 } else {
1297 return -errno;
1298 }
1299 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001300 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001301 return -errno;
1302 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001303}
1304
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001305static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001306{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001307 kill(j->initpid, SIGKILL);
1308 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001309}
1310
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001311static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001312{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001313 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001314 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001315}
1316
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001317static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001318{
1319 size_t i;
1320
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001321 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001322 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001323 kill_child_and_die(j, "failed to add to cgroups");
1324 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001325}
1326
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001327static void write_ugid_maps_or_die(const struct minijail *j)
1328{
1329 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1330 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001331 if (j->gidmap && j->flags.disable_setgroups) {
1332 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1333 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001334 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001335 if (ret == -ENOENT) {
1336 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1337 warn("could not disable setgroups(2)");
1338 } else
1339 kill_child_and_die(j, "failed to disable setgroups(2)");
1340 }
1341 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001342 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1343 kill_child_and_die(j, "failed to write gid_map");
1344}
1345
1346static void enter_user_namespace(const struct minijail *j)
1347{
1348 if (j->uidmap && setresuid(0, 0, 0))
1349 pdie("user_namespaces: setresuid(0, 0, 0) failed");
1350 if (j->gidmap && setresgid(0, 0, 0))
1351 pdie("user_namespaces: setresgid(0, 0, 0) failed");
1352}
1353
1354static void parent_setup_complete(int *pipe_fds)
1355{
1356 close(pipe_fds[0]);
1357 close(pipe_fds[1]);
1358}
1359
1360/*
1361 * wait_for_parent_setup: Called by the child process to wait for any
1362 * further parent-side setup to complete before continuing.
1363 */
1364static void wait_for_parent_setup(int *pipe_fds)
1365{
1366 char buf;
1367
1368 close(pipe_fds[1]);
1369
1370 /* Wait for parent to complete setup and close the pipe. */
1371 if (read(pipe_fds[0], &buf, 1) != 0)
1372 die("failed to sync with parent");
1373 close(pipe_fds[0]);
1374}
1375
1376static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001377{
Lutz Justen13807cb2017-01-03 17:11:55 +01001378 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1379 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001380 die("can only do one of inherit, keep, or set supplementary "
1381 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001382 }
1383
Lutz Justen13807cb2017-01-03 17:11:55 +01001384 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001385 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001386 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001387 } else if (j->flags.set_suppl_gids) {
1388 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001389 pdie("setgroups(suppl_gids) failed");
Lutz Justen13807cb2017-01-03 17:11:55 +01001390 } else if (!j->flags.keep_suppl_gids) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001391 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001392 * Only attempt to clear supplementary groups if we are changing
Lutz Justen13807cb2017-01-03 17:11:55 +01001393 * users or groups.
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001394 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001395 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001396 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001397 }
1398
1399 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001400 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001401
1402 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001403 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001404}
1405
Mike Frysinger3adfef72013-05-09 17:19:08 -04001406/*
1407 * We specifically do not use cap_valid() as that only tells us the last
1408 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001409 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001410 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001411 * kernel).
1412 * Normally, we suck up the answer via /proc. On Android, not all processes are
1413 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1414 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001415 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001416static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001417{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001418 unsigned int last_valid_cap = 0;
1419 if (is_android()) {
1420 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1421 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001422
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001423 /* |last_valid_cap| will be the first failing value. */
1424 if (last_valid_cap > 0) {
1425 last_valid_cap--;
1426 }
1427 } else {
1428 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1429 FILE *fp = fopen(cap_file, "re");
1430 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1431 pdie("fscanf(%s)", cap_file);
1432 fclose(fp);
1433 }
Dylan Reidf682d472015-09-17 21:39:07 -07001434 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001435}
1436
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001437static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1438{
1439 const uint64_t one = 1;
1440 unsigned int i;
1441 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1442 if (keep_mask & (one << i))
1443 continue;
1444 if (prctl(PR_CAPBSET_DROP, i))
1445 pdie("could not drop capability from bounding set");
1446 }
1447}
1448
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001449static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001450{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001451 if (!j->flags.use_caps)
1452 return;
1453
Elly Jonese1749eb2011-10-07 13:54:59 -04001454 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001455 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001456 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001457 unsigned int i;
1458 if (!caps)
1459 die("can't get process caps");
1460 if (cap_clear_flag(caps, CAP_INHERITABLE))
1461 die("can't clear inheritable caps");
1462 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1463 die("can't clear effective caps");
1464 if (cap_clear_flag(caps, CAP_PERMITTED))
1465 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001466 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001467 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001468 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001469 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001470 flag[0] = i;
1471 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001472 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001473 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001474 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001475 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001476 die("can't add inheritable cap");
1477 }
1478 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001479 die("can't apply initial cleaned capset");
1480
1481 /*
1482 * Instead of dropping bounding set first, do it here in case
1483 * the caller had a more permissive bounding set which could
1484 * have been used above to raise a capability that wasn't already
1485 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1486 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001487 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001488
1489 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001490 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001491 flag[0] = CAP_SETPCAP;
1492 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1493 die("can't clear effective cap");
1494 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1495 die("can't clear permitted cap");
1496 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1497 die("can't clear inheritable cap");
1498 }
1499
1500 if (cap_set_proc(caps))
1501 die("can't apply final cleaned capset");
1502
1503 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001504}
1505
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001506static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001507{
1508 /*
1509 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1510 * in the kernel source tree for an explanation of the parameters.
1511 */
1512 if (j->flags.no_new_privs) {
1513 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1514 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1515 }
1516
1517 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001518 * Code running with ASan
1519 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1520 * will make system calls not included in the syscall filter policy,
1521 * which will likely crash the program. Skip setting seccomp filter in
1522 * that case.
1523 * 'running_with_asan()' has no inputs and is completely defined at
1524 * build time, so this cannot be used by an attacker to skip setting
1525 * seccomp filter.
1526 */
1527 if (j->flags.seccomp_filter && running_with_asan()) {
1528 warn("running with ASan, not setting seccomp filter");
1529 return;
1530 }
1531
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001532 if (j->flags.seccomp_filter) {
1533 if (j->flags.seccomp_filter_logging) {
1534 /*
1535 * If logging seccomp filter failures,
1536 * install the SIGSYS handler first.
1537 */
1538 if (install_sigsys_handler())
1539 pdie("failed to install SIGSYS handler");
1540 warn("logging seccomp filter failures");
1541 } else if (j->flags.seccomp_filter_tsync) {
1542 /*
1543 * If setting thread sync,
1544 * reset the SIGSYS signal handler so that
1545 * the entire thread group is killed.
1546 */
1547 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1548 pdie("failed to reset SIGSYS disposition");
1549 info("reset SIGSYS disposition");
1550 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001551 }
1552
1553 /*
1554 * Install the syscall filter.
1555 */
1556 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001557 if (j->flags.seccomp_filter_tsync) {
1558 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1559 SECCOMP_FILTER_FLAG_TSYNC,
1560 j->filter_prog)) {
1561 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001562 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001563 } else {
1564 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1565 j->filter_prog)) {
1566 pdie("prctl(seccomp_filter) failed");
1567 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001568 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001569 }
1570}
1571
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001572static void config_net_loopback(void)
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001573{
1574 static const char ifname[] = "lo";
1575 int sock;
1576 struct ifreq ifr;
1577
1578 /* Make sure people don't try to add really long names. */
1579 _Static_assert(sizeof(ifname) <= IFNAMSIZ, "interface name too long");
1580
1581 sock = socket(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0);
1582 if (sock < 0)
1583 pdie("socket(AF_LOCAL) failed");
1584
1585 /*
1586 * Do the equiv of `ip link set up lo`. The kernel will assign
1587 * IPv4 (127.0.0.1) & IPv6 (::1) addresses automatically!
1588 */
1589 strcpy(ifr.ifr_name, ifname);
1590 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0)
1591 pdie("ioctl(SIOCGIFFLAGS) failed");
1592
1593 /* The kernel preserves ifr.ifr_name for use. */
1594 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
1595 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0)
1596 pdie("ioctl(SIOCSIFFLAGS) failed");
1597
1598 close(sock);
1599}
1600
Will Drewry6ac91122011-10-21 16:38:58 -05001601void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001602{
Dylan Reidf682d472015-09-17 21:39:07 -07001603 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001604 * If we're dropping caps, get the last valid cap from /proc now,
1605 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001606 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001607 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001608 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001609 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001610
Elly Jonese1749eb2011-10-07 13:54:59 -04001611 if (j->flags.pids)
1612 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001613 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001614
Lutz Justen13807cb2017-01-03 17:11:55 +01001615 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001616 die("cannot inherit supplementary groups without setting a "
1617 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001618
Elly Jonesdd3e8512012-01-23 15:13:38 -05001619 /*
1620 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001621 * so we don't even try. If any of our operations fail, we abort() the
1622 * entire process.
1623 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001624 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001625 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001626
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001627 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001628 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001629 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001630 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001631 * Unless asked not to, remount all filesystems as private.
1632 * If they are shared, new bind mounts will creep out of our
1633 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001634 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1635 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001636 if (!j->flags.skip_remount_private) {
1637 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001638 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1639 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001640 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001641 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001642
Dylan Reidf7942472015-11-18 17:55:26 -08001643 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001644 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001645 }
1646
Dylan Reid1102f5a2015-09-15 11:52:20 -07001647 if (j->flags.enter_net) {
1648 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001649 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001650 } else if (j->flags.net) {
1651 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001652 pdie("unshare(CLONE_NEWNET) failed");
1653 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001654 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001655
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001656 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001657 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001658
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08001659 if (j->flags.new_session_keyring) {
1660 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
1661 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
1662 }
1663
Elly Jones51a5b6c2011-10-12 19:09:26 -04001664 if (j->flags.chroot && enter_chroot(j))
1665 pdie("chroot");
1666
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001667 if (j->flags.pivot_root && enter_pivot_root(j))
1668 pdie("pivot_root");
1669
Martin Pelikánab9eb442017-01-25 11:53:58 +11001670 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07001671 pdie("mount_tmp");
1672
Dylan Reid791f5772015-09-14 20:02:42 -07001673 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001674 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001675
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001676 /*
1677 * If we're only dropping capabilities from the bounding set, but not
1678 * from the thread's (permitted|inheritable|effective) sets, do it now.
1679 */
1680 if (j->flags.capbset_drop) {
1681 drop_capbset(j->cap_bset, last_valid_cap);
1682 }
1683
1684 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001685 /*
1686 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001687 * capability to change uids, our attempt to use setuid()
1688 * below will fail. Hang on to root caps across setuid(), then
1689 * lock securebits.
1690 */
1691 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001692 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001693
1694 /*
1695 * Kernels 4.3+ define a new securebit
1696 * (SECURE_NO_CAP_AMBIENT_RAISE), so using the SECURE_ALL_BITS
1697 * and SECURE_ALL_LOCKS masks from newer kernel headers will
1698 * return EPERM on older kernels. Detect this, and retry with
1699 * the right mask for older (2.6.26-4.2) kernels.
1700 */
1701 int securebits_ret = prctl(PR_SET_SECUREBITS,
1702 SECURE_ALL_BITS | SECURE_ALL_LOCKS);
1703 if (securebits_ret < 0) {
1704 if (errno == EPERM) {
1705 /* Possibly running on kernel < 4.3. */
1706 securebits_ret = prctl(
1707 PR_SET_SECUREBITS,
1708 OLD_SECURE_ALL_BITS | OLD_SECURE_ALL_LOCKS);
1709 }
1710 }
1711 if (securebits_ret < 0)
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001712 pdie("prctl(PR_SET_SECUREBITS) failed");
Elly Jonese1749eb2011-10-07 13:54:59 -04001713 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001714
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001715 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001716 /*
1717 * If we're setting no_new_privs, we can drop privileges
1718 * before setting seccomp filter. This way filter policies
1719 * don't need to allow privilege-dropping syscalls.
1720 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001721 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001722 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001723 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001724 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001725 /*
1726 * If we're not setting no_new_privs,
1727 * we need to set seccomp filter *before* dropping privileges.
1728 * WARNING: this means that filter policies *must* allow
1729 * setgroups()/setresgid()/setresuid() for dropping root and
1730 * capget()/capset()/prctl() for dropping caps.
1731 */
1732 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001733 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001734 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001735 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001736
Elly Jonesdd3e8512012-01-23 15:13:38 -05001737 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001738 * Select the specified alternate syscall table. The table must not
1739 * block prctl(2) if we're using seccomp as well.
1740 */
1741 if (j->flags.alt_syscall) {
1742 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001743 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08001744 }
1745
1746 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001747 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001748 * privilege-dropping syscalls :)
1749 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001750 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001751 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001752 warn("seccomp not supported");
1753 return;
1754 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001755 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001756 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001757}
1758
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001759/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001760static int init_exitstatus = 0;
1761
Will Drewry6ac91122011-10-21 16:38:58 -05001762void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001763{
1764 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001765}
1766
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001767void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001768{
1769 pid_t pid;
1770 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001771 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001772 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001773 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001774 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001775 /*
1776 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001777 * left inside our pid namespace or we get a signal.
1778 */
1779 if (pid == rootpid)
1780 init_exitstatus = status;
1781 }
1782 if (!WIFEXITED(init_exitstatus))
1783 _exit(MINIJAIL_ERR_INIT);
1784 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001785}
1786
Will Drewry6ac91122011-10-21 16:38:58 -05001787int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001788{
1789 size_t sz = 0;
1790 size_t bytes = read(fd, &sz, sizeof(sz));
1791 char *buf;
1792 int r;
1793 if (sizeof(sz) != bytes)
1794 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001795 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001796 return -E2BIG;
1797 buf = malloc(sz);
1798 if (!buf)
1799 return -ENOMEM;
1800 bytes = read(fd, buf, sz);
1801 if (bytes != sz) {
1802 free(buf);
1803 return -EINVAL;
1804 }
1805 r = minijail_unmarshal(j, buf, sz);
1806 free(buf);
1807 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001808}
1809
Will Drewry6ac91122011-10-21 16:38:58 -05001810int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001811{
1812 char *buf;
1813 size_t sz = minijail_size(j);
1814 ssize_t written;
1815 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001816
Elly Jonese1749eb2011-10-07 13:54:59 -04001817 if (!sz)
1818 return -EINVAL;
1819 buf = malloc(sz);
1820 r = minijail_marshal(j, buf, sz);
1821 if (r) {
1822 free(buf);
1823 return r;
1824 }
1825 /* Sends [size][minijail]. */
1826 written = write(fd, &sz, sizeof(sz));
1827 if (written != sizeof(sz)) {
1828 free(buf);
1829 return -EFAULT;
1830 }
1831 written = write(fd, buf, sz);
1832 if (written < 0 || (size_t) written != sz) {
1833 free(buf);
1834 return -EFAULT;
1835 }
1836 free(buf);
1837 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001838}
Elly Jonescd7a9042011-07-22 13:56:51 -04001839
Will Drewry6ac91122011-10-21 16:38:58 -05001840int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001841{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001842#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001843 /* Don't use LDPRELOAD on Brillo. */
1844 return 0;
1845#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001846 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1847 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1848 if (!newenv)
1849 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001850
Elly Jonese1749eb2011-10-07 13:54:59 -04001851 /* Only insert a separating space if we have something to separate... */
1852 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1853 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001854
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001855 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001856 setenv(kLdPreloadEnvVar, newenv, 1);
1857 free(newenv);
1858 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001859#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001860}
1861
Will Drewry6ac91122011-10-21 16:38:58 -05001862int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001863{
1864 int r = pipe(fds);
1865 char fd_buf[11];
1866 if (r)
1867 return r;
1868 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1869 if (r <= 0)
1870 return -EINVAL;
1871 setenv(kFdEnvVar, fd_buf, 1);
1872 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001873}
1874
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001875int setup_pipe_end(int fds[2], size_t index)
1876{
1877 if (index > 1)
1878 return -1;
1879
1880 close(fds[1 - index]);
1881 return fds[index];
1882}
1883
1884int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1885{
1886 if (index > 1)
1887 return -1;
1888
1889 close(fds[1 - index]);
1890 /* dup2(2) the corresponding end of the pipe into |fd|. */
1891 return dup2(fds[index], fd);
1892}
1893
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07001894int close_open_fds(int *inheritable_fds, size_t size)
1895{
1896 const char *kFdPath = "/proc/self/fd";
1897
1898 DIR *d = opendir(kFdPath);
1899 struct dirent *dir_entry;
1900
1901 if (d == NULL)
1902 return -1;
1903 int dir_fd = dirfd(d);
1904 while ((dir_entry = readdir(d)) != NULL) {
1905 size_t i;
1906 char *end;
1907 bool should_close = true;
1908 const int fd = strtol(dir_entry->d_name, &end, 10);
1909
1910 if ((*end) != '\0') {
1911 continue;
1912 }
1913 /*
1914 * We might have set up some pipes that we want to share with
1915 * the parent process, and should not be closed.
1916 */
1917 for (i = 0; i < size; ++i) {
1918 if (fd == inheritable_fds[i]) {
1919 should_close = false;
1920 break;
1921 }
1922 }
1923 /* Also avoid closing the directory fd. */
1924 if (should_close && fd != dir_fd)
1925 close(fd);
1926 }
1927 closedir(d);
1928 return 0;
1929}
1930
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001931int minijail_run_internal(struct minijail *j, const char *filename,
1932 char *const argv[], pid_t *pchild_pid,
1933 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1934 int use_preload);
1935
Will Drewry6ac91122011-10-21 16:38:58 -05001936int API minijail_run(struct minijail *j, const char *filename,
1937 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001938{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001939 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1940 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001941}
1942
1943int API minijail_run_pid(struct minijail *j, const char *filename,
1944 char *const argv[], pid_t *pchild_pid)
1945{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001946 return minijail_run_internal(j, filename, argv, pchild_pid,
1947 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001948}
1949
1950int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001951 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001952{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001953 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1954 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001955}
1956
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001957int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001958 char *const argv[], pid_t *pchild_pid,
1959 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001960{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001961 return minijail_run_internal(j, filename, argv, pchild_pid,
1962 pstdin_fd, pstdout_fd, pstderr_fd, true);
1963}
1964
1965int API minijail_run_no_preload(struct minijail *j, const char *filename,
1966 char *const argv[])
1967{
1968 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1969 false);
1970}
1971
Samuel Tan63187f42015-10-16 13:01:53 -07001972int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001973 const char *filename,
1974 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001975 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001976 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001977 int *pstderr_fd)
1978{
Samuel Tan63187f42015-10-16 13:01:53 -07001979 return minijail_run_internal(j, filename, argv, pchild_pid,
1980 pstdin_fd, pstdout_fd, pstderr_fd, false);
1981}
1982
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001983int minijail_run_internal(struct minijail *j, const char *filename,
1984 char *const argv[], pid_t *pchild_pid,
1985 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1986 int use_preload)
1987{
Elly Jonese1749eb2011-10-07 13:54:59 -04001988 char *oldenv, *oldenv_copy = NULL;
1989 pid_t child_pid;
1990 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001991 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001992 int stdout_fds[2];
1993 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001994 int child_sync_pipe_fds[2];
1995 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001996 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001997 /* We need to remember this across the minijail_preexec() call. */
1998 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001999 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07002000
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002001 if (use_preload) {
2002 oldenv = getenv(kLdPreloadEnvVar);
2003 if (oldenv) {
2004 oldenv_copy = strdup(oldenv);
2005 if (!oldenv_copy)
2006 return -ENOMEM;
2007 }
2008
2009 if (setup_preload())
2010 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002011 }
Will Drewryf89aef52011-09-16 16:48:57 -05002012
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002013 if (!use_preload) {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002014 if (j->flags.use_caps && j->caps != 0)
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002015 die("non-empty capabilities are not supported without "
2016 "LD_PRELOAD");
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002017 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002018
Elly Jonesdd3e8512012-01-23 15:13:38 -05002019 /*
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002020 * Make the process group ID of this process equal to its PID.
2021 * In the non-interactive case (e.g. when the parent process is started
2022 * from init) this ensures the parent process and the jailed process
2023 * can be killed together.
2024 * When the parent process is started from the console this ensures
2025 * the call to setsid(2) in the jailed process succeeds.
2026 *
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002027 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
2028 * the process is already a process group leader.
2029 */
2030 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
2031 if (errno != EPERM) {
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002032 pdie("setpgid(0, 0) failed");
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002033 }
2034 }
2035
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002036 if (use_preload) {
2037 /*
2038 * Before we fork(2) and execve(2) the child process, we need
2039 * to open a pipe(2) to send the minijail configuration over.
2040 */
2041 if (setup_pipe(pipe_fds))
2042 return -EFAULT;
2043 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002044
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002045 /*
2046 * If we want to write to the child process' standard input,
2047 * create the pipe(2) now.
2048 */
2049 if (pstdin_fd) {
2050 if (pipe(stdin_fds))
2051 return -EFAULT;
2052 }
2053
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002054 /*
2055 * If we want to read from the child process' standard output,
2056 * create the pipe(2) now.
2057 */
2058 if (pstdout_fd) {
2059 if (pipe(stdout_fds))
2060 return -EFAULT;
2061 }
2062
2063 /*
2064 * If we want to read from the child process' standard error,
2065 * create the pipe(2) now.
2066 */
2067 if (pstderr_fd) {
2068 if (pipe(stderr_fds))
2069 return -EFAULT;
2070 }
2071
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002072 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002073 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002074 * or if we need to add the child process to cgroups, create the pipe(2)
2075 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002076 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002077 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002078 sync_child = 1;
2079 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002080 return -EFAULT;
2081 }
2082
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002083 /*
2084 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002085 *
2086 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2087 *
2088 * In multithreaded programs, there are a bunch of locks inside libc,
2089 * some of which may be held by other threads at the time that we call
2090 * minijail_run_pid(). If we call fork(), glibc does its level best to
2091 * ensure that we hold all of these locks before it calls clone()
2092 * internally and drop them after clone() returns, but when we call
2093 * sys_clone(2) directly, all that gets bypassed and we end up with a
2094 * child address space where some of libc's important locks are held by
2095 * other threads (which did not get cloned, and hence will never release
2096 * those locks). This is okay so long as we call exec() immediately
2097 * after, but a bunch of seemingly-innocent libc functions like setenv()
2098 * take locks.
2099 *
2100 * Hence, only call sys_clone() if we need to, in order to get at pid
2101 * namespacing. If we follow this path, the child's address space might
2102 * have broken locks; you may only call functions that do not acquire
2103 * any locks.
2104 *
2105 * Unfortunately, fork() acquires every lock it can get its hands on, as
2106 * previously detailed, so this function is highly likely to deadlock
2107 * later on (see "deadlock here") if we're multithreaded.
2108 *
2109 * We might hack around this by having the clone()d child (init of the
2110 * pid namespace) return directly, rather than leaving the clone()d
2111 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002112 * its fork()ed child return in turn), but that process would be
2113 * crippled with its libc locks potentially broken. We might try
2114 * fork()ing in the parent before we clone() to ensure that we own all
2115 * the locks, but then we have to have the forked child hanging around
2116 * consuming resources (and possibly having file descriptors / shared
2117 * memory regions / etc attached). We'd need to keep the child around to
2118 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002119 *
2120 * TODO(ellyjones): figure out if the "forked child hanging around"
2121 * problem is fixable or not. It would be nice if we worked in this
2122 * case.
2123 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002124 if (pid_namespace) {
2125 int clone_flags = CLONE_NEWPID | SIGCHLD;
2126 if (j->flags.userns)
2127 clone_flags |= CLONE_NEWUSER;
2128 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002129 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002130 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002131 }
Elly Jones761b7412012-06-13 15:49:52 -04002132
Elly Jonese1749eb2011-10-07 13:54:59 -04002133 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002134 if (use_preload) {
2135 free(oldenv_copy);
2136 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002137 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002138 }
Will Drewryf89aef52011-09-16 16:48:57 -05002139
Elly Jonese1749eb2011-10-07 13:54:59 -04002140 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002141 if (use_preload) {
2142 /* Restore parent's LD_PRELOAD. */
2143 if (oldenv_copy) {
2144 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2145 free(oldenv_copy);
2146 } else {
2147 unsetenv(kLdPreloadEnvVar);
2148 }
2149 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002150 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002151
Elly Jonese1749eb2011-10-07 13:54:59 -04002152 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002153
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002154 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002155 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002156
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002157 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002158 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002159
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002160 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002161 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002162
2163 if (sync_child)
2164 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002165
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002166 if (use_preload) {
2167 /* Send marshalled minijail. */
2168 close(pipe_fds[0]); /* read endpoint */
2169 ret = minijail_to_fd(j, pipe_fds[1]);
2170 close(pipe_fds[1]); /* write endpoint */
2171 if (ret) {
2172 kill(j->initpid, SIGKILL);
2173 die("failed to send marshalled minijail");
2174 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002175 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002176
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002177 if (pchild_pid)
2178 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002179
2180 /*
2181 * If we want to write to the child process' standard input,
2182 * set up the write end of the pipe.
2183 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002184 if (pstdin_fd)
2185 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002186 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002187
2188 /*
2189 * If we want to read from the child process' standard output,
2190 * set up the read end of the pipe.
2191 */
2192 if (pstdout_fd)
2193 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002194 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002195
2196 /*
2197 * If we want to read from the child process' standard error,
2198 * set up the read end of the pipe.
2199 */
2200 if (pstderr_fd)
2201 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002202 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002203
Elly Jonese1749eb2011-10-07 13:54:59 -04002204 return 0;
2205 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002206 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002207 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002208
Peter Qiu2860c462015-12-16 15:13:06 -08002209 if (j->flags.reset_signal_mask) {
2210 sigset_t signal_mask;
2211 if (sigemptyset(&signal_mask) != 0)
2212 pdie("sigemptyset failed");
2213 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2214 pdie("sigprocmask failed");
2215 }
2216
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002217 if (j->flags.close_open_fds) {
2218 const size_t kMaxInheritableFdsSize = 10;
2219 int inheritable_fds[kMaxInheritableFdsSize];
2220 size_t size = 0;
2221 if (use_preload) {
2222 inheritable_fds[size++] = pipe_fds[0];
2223 inheritable_fds[size++] = pipe_fds[1];
2224 }
2225 if (sync_child) {
2226 inheritable_fds[size++] = child_sync_pipe_fds[0];
2227 inheritable_fds[size++] = child_sync_pipe_fds[1];
2228 }
2229 if (pstdin_fd) {
2230 inheritable_fds[size++] = stdin_fds[0];
2231 inheritable_fds[size++] = stdin_fds[1];
2232 }
2233 if (pstdout_fd) {
2234 inheritable_fds[size++] = stdout_fds[0];
2235 inheritable_fds[size++] = stdout_fds[1];
2236 }
2237 if (pstderr_fd) {
2238 inheritable_fds[size++] = stderr_fds[0];
2239 inheritable_fds[size++] = stderr_fds[1];
2240 }
2241
2242 if (close_open_fds(inheritable_fds, size) < 0)
2243 die("failed to close open file descriptors");
2244 }
2245
Dylan Reidce5b55e2016-01-13 11:04:16 -08002246 if (sync_child)
2247 wait_for_parent_setup(child_sync_pipe_fds);
2248
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002249 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002250 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002251
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002252 /*
2253 * If we want to write to the jailed process' standard input,
2254 * set up the read end of the pipe.
2255 */
2256 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002257 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2258 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002259 die("failed to set up stdin pipe");
2260 }
2261
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002262 /*
2263 * If we want to read from the jailed process' standard output,
2264 * set up the write end of the pipe.
2265 */
2266 if (pstdout_fd) {
2267 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2268 STDOUT_FILENO) < 0)
2269 die("failed to set up stdout pipe");
2270 }
2271
2272 /*
2273 * If we want to read from the jailed process' standard error,
2274 * set up the write end of the pipe.
2275 */
2276 if (pstderr_fd) {
2277 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2278 STDERR_FILENO) < 0)
2279 die("failed to set up stderr pipe");
2280 }
2281
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002282 /*
2283 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2284 * This prevents the jailed process from using the TIOCSTI ioctl
2285 * to push characters into the parent process terminal's input buffer,
2286 * therefore escaping the jail.
2287 */
2288 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2289 isatty(STDERR_FILENO)) {
2290 if (setsid() < 0) {
2291 pdie("setsid() failed");
2292 }
2293 }
2294
Dylan Reid791f5772015-09-14 20:02:42 -07002295 /* If running an init program, let it decide when/how to mount /proc. */
2296 if (pid_namespace && !do_init)
2297 j->flags.remount_proc_ro = 0;
2298
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002299 if (use_preload) {
2300 /* Strip out flags that cannot be inherited across execve(2). */
2301 minijail_preexec(j);
2302 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002303 /*
2304 * If not using LD_PRELOAD, do all jailing before execve(2).
2305 * Note that PID namespaces can only be entered on fork(2),
2306 * so that flag is still cleared.
2307 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002308 j->flags.pids = 0;
2309 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002310 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002311 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002312
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002313 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002314 /*
2315 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002316 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002317 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002318 * a child to actually run the program. If |do_init == 0|, we
2319 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002320 *
2321 * If we're multithreaded, we'll probably deadlock here. See
2322 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002323 */
2324 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002325 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002326 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002327 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002328 /*
2329 * Best effort. Don't bother checking the return value.
2330 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002331 prctl(PR_SET_NAME, "minijail-init");
2332 init(child_pid); /* Never returns. */
2333 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002334 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002335
Elly Jonesdd3e8512012-01-23 15:13:38 -05002336 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002337 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002338 * calling process
2339 * -> execve()-ing process
2340 * If we are:
2341 * calling process
2342 * -> init()-ing process
2343 * -> execve()-ing process
2344 */
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002345 ret = execve(filename, argv, environ);
2346 if (ret == -1) {
2347 pwarn("execve(%s) failed", filename);
2348 }
2349 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002350}
2351
Will Drewry6ac91122011-10-21 16:38:58 -05002352int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002353{
2354 int st;
2355 if (kill(j->initpid, SIGTERM))
2356 return -errno;
2357 if (waitpid(j->initpid, &st, 0) < 0)
2358 return -errno;
2359 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002360}
2361
Will Drewry6ac91122011-10-21 16:38:58 -05002362int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002363{
2364 int st;
2365 if (waitpid(j->initpid, &st, 0) < 0)
2366 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002367
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002368 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002369 int error_status = st;
2370 if (WIFSIGNALED(st)) {
2371 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002372 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002373 j->initpid, signum);
2374 /*
2375 * We return MINIJAIL_ERR_JAIL if the process received
2376 * SIGSYS, which happens when a syscall is blocked by
2377 * seccomp filters.
2378 * If not, we do what bash(1) does:
2379 * $? = 128 + signum
2380 */
2381 if (signum == SIGSYS) {
2382 error_status = MINIJAIL_ERR_JAIL;
2383 } else {
2384 error_status = 128 + signum;
2385 }
2386 }
2387 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002388 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002389
2390 int exit_status = WEXITSTATUS(st);
2391 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002392 info("child process %d exited with status %d",
2393 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002394
2395 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002396}
2397
Will Drewry6ac91122011-10-21 16:38:58 -05002398void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002399{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002400 size_t i;
2401
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002402 if (j->flags.seccomp_filter && j->filter_prog) {
2403 free(j->filter_prog->filter);
2404 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002405 }
Dylan Reid648b2202015-10-23 00:50:00 -07002406 while (j->mounts_head) {
2407 struct mountpoint *m = j->mounts_head;
2408 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002409 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002410 free(m->type);
2411 free(m->dest);
2412 free(m->src);
2413 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002414 }
Dylan Reid648b2202015-10-23 00:50:00 -07002415 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002416 if (j->user)
2417 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002418 if (j->suppl_gid_list)
2419 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002420 if (j->chrootdir)
2421 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002422 if (j->pid_file_path)
2423 free(j->pid_file_path);
2424 if (j->uidmap)
2425 free(j->uidmap);
2426 if (j->gidmap)
2427 free(j->gidmap);
Andrew Brestickereac28942015-11-11 16:04:46 -08002428 if (j->alt_syscall_table)
2429 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002430 for (i = 0; i < j->cgroup_count; ++i)
2431 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002432 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002433}