blob: 8f63b8567d7989821166dfe2cc2a020c7c265f3b [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>
Elly Jonescd7a9042011-07-22 13:56:51 -040012#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070013#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040014#include <grp.h>
15#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050016#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040017#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040018#include <pwd.h>
19#include <sched.h>
20#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050021#include <stdarg.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070022#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080023#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <syscall.h>
28#include <sys/capability.h>
29#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050030#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040031#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070032#include <sys/stat.h>
33#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080034#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040035#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040036#include <unistd.h>
37
38#include "libminijail.h"
39#include "libminijail-private.h"
40
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070041#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080042#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070043#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080044
Lei Zhangeee31552012-10-17 21:27:10 -070045#ifdef HAVE_SECUREBITS_H
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070046# include <linux/securebits.h>
Lei Zhangeee31552012-10-17 21:27:10 -070047#else
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070048# define SECURE_ALL_BITS 0x55
49# define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
50#endif
51/* For kernels < 4.3. */
52#define OLD_SECURE_ALL_BITS 0x15
53#define OLD_SECURE_ALL_LOCKS (OLD_SECURE_ALL_BITS << 1)
54
55/*
56 * Assert the value of SECURE_ALL_BITS at compile-time.
57 * Brillo devices are currently compiled against 4.4 kernel headers. Kernel 4.3
58 * added a new securebit.
59 * When a new securebit is added, the new SECURE_ALL_BITS mask will return EPERM
60 * when used on older kernels. The compile-time assert will catch this situation
61 * at compile time.
62 */
63#ifdef __BRILLO__
64_Static_assert(SECURE_ALL_BITS == 0x55, "SECURE_ALL_BITS == 0x55.");
Lei Zhangeee31552012-10-17 21:27:10 -070065#endif
66
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070067/* Until these are reliably available in linux/prctl.h. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080068#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070069# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080070#endif
71
Andrew Brestickereac28942015-11-11 16:04:46 -080072#ifndef PR_ALT_SYSCALL
73# define PR_ALT_SYSCALL 0x43724f53
74#endif
75
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080076/* For seccomp_filter using BPF. */
77#ifndef PR_SET_NO_NEW_PRIVS
78# define PR_SET_NO_NEW_PRIVS 38
79#endif
80#ifndef SECCOMP_MODE_FILTER
81# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050082#endif
83
Dylan Reid4cbc2a52016-06-17 19:06:07 -070084/* New cgroup namespace might not be in linux-headers yet. */
85#ifndef CLONE_NEWCGROUP
86# define CLONE_NEWCGROUP 0x02000000
87#endif
88
Dylan Reid605ce7f2016-01-19 19:21:00 -080089#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
90
Dylan Reid648b2202015-10-23 00:50:00 -070091struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040092 char *src;
93 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070094 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -070095 char *data;
96 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -070097 unsigned long flags;
98 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040099};
100
Will Drewryf89aef52011-09-16 16:48:57 -0500101struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700102 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700103 * WARNING: if you add a flag here you need to make sure it's
104 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700105 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400106 struct {
107 int uid:1;
108 int gid:1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800109 int usergroups:1;
110 int suppl_gids:1;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800111 int use_caps:1;
112 int capbset_drop:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400113 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700114 int enter_vfs:1;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800115 int skip_remount_private:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400116 int pids:1;
Dylan Reidf7942472015-11-18 17:55:26 -0800117 int ipc:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400118 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700119 int enter_net:1;
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700120 int ns_cgroups:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800121 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400122 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -0700123 int remount_proc_ro:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700124 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400125 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700126 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400127 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800128 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700129 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800130 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800131 int pid_file:1;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800132 int cgroups:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800133 int alt_syscall:1;
Peter Qiu2860c462015-12-16 15:13:06 -0800134 int reset_signal_mask:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400135 } flags;
136 uid_t uid;
137 gid_t gid;
138 gid_t usergid;
139 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800140 size_t suppl_gid_count;
141 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400142 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800143 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400144 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700145 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700146 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400147 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800148 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800149 char *uidmap;
150 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800151 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800152 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800153 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700154 struct mountpoint *mounts_head;
155 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800156 size_t mounts_count;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800157 char *cgroups[MAX_CGROUPS];
158 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500159};
160
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700161/*
162 * Strip out flags meant for the parent.
163 * We keep things that are not inherited across execve(2) (e.g. capabilities),
164 * or are easier to set after execve(2) (e.g. seccomp filters).
165 */
166void minijail_preenter(struct minijail *j)
167{
168 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700169 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900170 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700171 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700172 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800173 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800174 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800175 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700176}
177
178/*
179 * Strip out flags meant for the child.
180 * We keep things that are inherited across execve(2).
181 */
182void minijail_preexec(struct minijail *j)
183{
184 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700185 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800186 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700187 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800188 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700189 if (j->user)
190 free(j->user);
191 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800192 if (j->suppl_gid_list)
193 free(j->suppl_gid_list);
194 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700195 memset(&j->flags, 0, sizeof(j->flags));
196 /* Now restore anything we meant to keep. */
197 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700198 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800199 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700200 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800201 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700202 /* Note, |pids| will already have been used before this call. */
203}
204
205/* Minijail API. */
206
Will Drewry6ac91122011-10-21 16:38:58 -0500207struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400208{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400209 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400210}
211
Will Drewry6ac91122011-10-21 16:38:58 -0500212void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400213{
214 if (uid == 0)
215 die("useless change to uid 0");
216 j->uid = uid;
217 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400218}
219
Will Drewry6ac91122011-10-21 16:38:58 -0500220void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400221{
222 if (gid == 0)
223 die("useless change to gid 0");
224 j->gid = gid;
225 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400226}
227
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800228void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
229 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800230{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800231 size_t i;
232
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800233 if (j->flags.usergroups)
234 die("cannot inherit *and* set supplementary groups");
235
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800236 if (size == 0) {
237 /* Clear supplementary groups. */
238 j->suppl_gid_list = NULL;
239 j->suppl_gid_count = 0;
240 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800241 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800242 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800243
244 /* Copy the gid_t array. */
245 j->suppl_gid_list = calloc(size, sizeof(gid_t));
246 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800247 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800248 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800249 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800250 j->suppl_gid_list[i] = list[i];
251 }
252 j->suppl_gid_count = size;
253 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800254}
255
Will Drewry6ac91122011-10-21 16:38:58 -0500256int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400257{
258 char *buf = NULL;
259 struct passwd pw;
260 struct passwd *ppw = NULL;
261 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
262 if (sz == -1)
263 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400264
Elly Jonesdd3e8512012-01-23 15:13:38 -0500265 /*
266 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400267 * the maximum needed size of the buffer, so we don't have to search.
268 */
269 buf = malloc(sz);
270 if (!buf)
271 return -ENOMEM;
272 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500273 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800274 * We're safe to free the buffer here. The strings inside |pw| point
275 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800276 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
277 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500278 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400279 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700280 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400281 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700282 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400283 minijail_change_uid(j, ppw->pw_uid);
284 j->user = strdup(user);
285 if (!j->user)
286 return -ENOMEM;
287 j->usergid = ppw->pw_gid;
288 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400289}
290
Will Drewry6ac91122011-10-21 16:38:58 -0500291int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400292{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700293 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700294 struct group gr;
295 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400296 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
297 if (sz == -1)
298 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400299
Elly Jonesdd3e8512012-01-23 15:13:38 -0500300 /*
301 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400302 * the maximum needed size of the buffer, so we don't have to search.
303 */
304 buf = malloc(sz);
305 if (!buf)
306 return -ENOMEM;
307 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500308 /*
309 * We're safe to free the buffer here. The strings inside gr point
310 * inside buf, but we don't use any of them; this leaves the pointers
311 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
312 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400313 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700314 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400315 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700316 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400317 minijail_change_gid(j, pgr->gr_gid);
318 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400319}
320
Will Drewry6ac91122011-10-21 16:38:58 -0500321void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400322{
323 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400324}
325
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700326void API minijail_no_new_privs(struct minijail *j)
327{
328 j->flags.no_new_privs = 1;
329}
330
Will Drewry6ac91122011-10-21 16:38:58 -0500331void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400332{
333 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500334}
335
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700336void API minijail_log_seccomp_filter_failures(struct minijail *j)
337{
338 j->flags.log_seccomp_filter = 1;
339}
340
Will Drewry6ac91122011-10-21 16:38:58 -0500341void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400342{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800343 /*
344 * 'minijail_use_caps' configures a runtime-capabilities-only
345 * environment, including a bounding set matching the thread's runtime
346 * (permitted|inheritable|effective) sets.
347 * Therefore, it will override any existing bounding set configurations
348 * since the latter would allow gaining extra runtime capabilities from
349 * file capabilities.
350 */
351 if (j->flags.capbset_drop) {
352 warn("overriding bounding set configuration");
353 j->cap_bset = 0;
354 j->flags.capbset_drop = 0;
355 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400356 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800357 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400358}
359
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800360void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
361{
362 if (j->flags.use_caps) {
363 /*
364 * 'minijail_use_caps' will have already configured a capability
365 * bounding set matching the (permitted|inheritable|effective)
366 * sets. Abort if the user tries to configure a separate
367 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
368 * are mutually exclusive.
369 */
370 die("runtime capabilities already configured, can't drop "
371 "bounding set separately");
372 }
373 j->cap_bset = capmask;
374 j->flags.capbset_drop = 1;
375}
376
377void API minijail_reset_signal_mask(struct minijail *j)
378{
Peter Qiu2860c462015-12-16 15:13:06 -0800379 j->flags.reset_signal_mask = 1;
380}
381
Will Drewry6ac91122011-10-21 16:38:58 -0500382void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400383{
384 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400385}
386
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700387void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
388{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800389 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700390 if (ns_fd < 0) {
391 pdie("failed to open namespace '%s'", ns_path);
392 }
393 j->mountns_fd = ns_fd;
394 j->flags.enter_vfs = 1;
395}
396
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800397void API minijail_skip_remount_private(struct minijail *j)
398{
399 j->flags.skip_remount_private = 1;
400}
401
Will Drewry6ac91122011-10-21 16:38:58 -0500402void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400403{
Elly Jonese58176c2012-01-23 11:46:17 -0500404 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700405 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400406 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800407 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400408}
409
Dylan Reidf7942472015-11-18 17:55:26 -0800410void API minijail_namespace_ipc(struct minijail *j)
411{
412 j->flags.ipc = 1;
413}
414
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400415void API minijail_namespace_net(struct minijail *j)
416{
417 j->flags.net = 1;
418}
419
Dylan Reid1102f5a2015-09-15 11:52:20 -0700420void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
421{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800422 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700423 if (ns_fd < 0) {
424 pdie("failed to open namespace '%s'", ns_path);
425 }
426 j->netns_fd = ns_fd;
427 j->flags.enter_net = 1;
428}
429
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700430void API minijail_namespace_cgroups(struct minijail *j)
431{
432 j->flags.ns_cgroups = 1;
433}
434
Dylan Reid791f5772015-09-14 20:02:42 -0700435void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400436{
437 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700438 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400439}
440
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800441void API minijail_namespace_user(struct minijail *j)
442{
443 j->flags.userns = 1;
444}
445
446int API minijail_uidmap(struct minijail *j, const char *uidmap)
447{
448 j->uidmap = strdup(uidmap);
449 if (!j->uidmap)
450 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800451 char *ch;
452 for (ch = j->uidmap; *ch; ch++) {
453 if (*ch == ',')
454 *ch = '\n';
455 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800456 return 0;
457}
458
459int API minijail_gidmap(struct minijail *j, const char *gidmap)
460{
461 j->gidmap = strdup(gidmap);
462 if (!j->gidmap)
463 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800464 char *ch;
465 for (ch = j->gidmap; *ch; ch++) {
466 if (*ch == ',')
467 *ch = '\n';
468 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800469 return 0;
470}
471
Will Drewry6ac91122011-10-21 16:38:58 -0500472void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400473{
474 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400475}
476
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800477void API minijail_run_as_init(struct minijail *j)
478{
479 /*
480 * Since the jailed program will become 'init' in the new PID namespace,
481 * Minijail does not need to fork an 'init' process.
482 */
483 j->flags.do_init = 0;
484}
485
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700486int API minijail_enter_chroot(struct minijail *j, const char *dir)
487{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400488 if (j->chrootdir)
489 return -EINVAL;
490 j->chrootdir = strdup(dir);
491 if (!j->chrootdir)
492 return -ENOMEM;
493 j->flags.chroot = 1;
494 return 0;
495}
496
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800497int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
498{
499 if (j->chrootdir)
500 return -EINVAL;
501 j->chrootdir = strdup(dir);
502 if (!j->chrootdir)
503 return -ENOMEM;
504 j->flags.pivot_root = 1;
505 return 0;
506}
507
Dylan Reida14e08d2015-10-22 21:05:29 -0700508char API *minijail_get_original_path(struct minijail *j,
509 const char *path_inside_chroot)
510{
Dylan Reid648b2202015-10-23 00:50:00 -0700511 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700512
Dylan Reid648b2202015-10-23 00:50:00 -0700513 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700514 while (b) {
515 /*
516 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700517 * mount, then the original path is exactly the source of
518 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700519 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700520 * mount source = /some/path/exe, mount dest =
521 * /chroot/path/exe Then when getting the original path of
522 * "/chroot/path/exe", the source of that mount,
523 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700524 */
525 if (!strcmp(b->dest, path_inside_chroot))
526 return strdup(b->src);
527
528 /*
529 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700530 * mount, take the suffix of the chroot path relative to the
531 * mount destination path, and append it to the mount source
532 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700533 */
534 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
535 const char *relative_path =
536 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400537 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700538 }
539 b = b->next;
540 }
541
542 /* If there is a chroot path, append |path_inside_chroot| to that. */
543 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400544 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700545
546 /* No chroot, so the path outside is the same as it is inside. */
547 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700548}
549
Lee Campbell11af0622014-05-22 12:36:04 -0700550void API minijail_mount_tmp(struct minijail *j)
551{
552 j->flags.mount_tmp = 1;
553}
554
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800555int API minijail_write_pid_file(struct minijail *j, const char *path)
556{
557 j->pid_file_path = strdup(path);
558 if (!j->pid_file_path)
559 return -ENOMEM;
560 j->flags.pid_file = 1;
561 return 0;
562}
563
Dylan Reid605ce7f2016-01-19 19:21:00 -0800564int API minijail_add_to_cgroup(struct minijail *j, const char *path)
565{
566 if (j->cgroup_count >= MAX_CGROUPS)
567 return -ENOMEM;
568 j->cgroups[j->cgroup_count] = strdup(path);
569 if (!j->cgroups[j->cgroup_count])
570 return -ENOMEM;
571 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800572 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800573 return 0;
574}
575
Dylan Reid81e23972016-05-18 14:06:35 -0700576int API minijail_mount_with_data(struct minijail *j, const char *src,
577 const char *dest, const char *type,
578 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700579{
Dylan Reid648b2202015-10-23 00:50:00 -0700580 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400581
582 if (*dest != '/')
583 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700584 m = calloc(1, sizeof(*m));
585 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400586 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700587 m->dest = strdup(dest);
588 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400589 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700590 m->src = strdup(src);
591 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400592 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700593 m->type = strdup(type);
594 if (!m->type)
595 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700596 if (data) {
597 m->data = strdup(data);
598 if (!m->data)
599 goto error;
600 m->has_data = 1;
601 }
Dylan Reid648b2202015-10-23 00:50:00 -0700602 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400603
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800604 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400605
Elly Jonesdd3e8512012-01-23 15:13:38 -0500606 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700607 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400608 * containing vfs namespace.
609 */
610 minijail_namespace_vfs(j);
611
Dylan Reid648b2202015-10-23 00:50:00 -0700612 if (j->mounts_tail)
613 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400614 else
Dylan Reid648b2202015-10-23 00:50:00 -0700615 j->mounts_head = m;
616 j->mounts_tail = m;
617 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400618
619 return 0;
620
621error:
Dylan Reid81e23972016-05-18 14:06:35 -0700622 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700623 free(m->src);
624 free(m->dest);
625 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400626 return -ENOMEM;
627}
628
Dylan Reid81e23972016-05-18 14:06:35 -0700629int API minijail_mount(struct minijail *j, const char *src, const char *dest,
630 const char *type, unsigned long flags)
631{
632 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
633}
634
Dylan Reid648b2202015-10-23 00:50:00 -0700635int API minijail_bind(struct minijail *j, const char *src, const char *dest,
636 int writeable)
637{
638 unsigned long flags = MS_BIND;
639
640 if (!writeable)
641 flags |= MS_RDONLY;
642
643 return minijail_mount(j, src, dest, "", flags);
644}
645
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400646static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400647{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700648 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400649 /*
650 * |errno| will be set to EINVAL when seccomp has not been
651 * compiled into the kernel. On certain platforms and kernel
652 * versions this is not a fatal failure. In that case, and only
653 * in that case, disable seccomp and skip loading the filters.
654 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400655 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400656 warn("not loading seccomp filters,"
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800657 " seccomp not supported");
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800658 j->flags.seccomp_filter = 0;
659 j->flags.log_seccomp_filter = 0;
660 j->filter_len = 0;
661 j->filter_prog = NULL;
662 j->flags.no_new_privs = 0;
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400663 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700664 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400665 /*
666 * If |errno| != EINVAL or seccomp_can_softfail() is false,
667 * we can proceed. Worst case scenario minijail_enter() will
668 * abort() if seccomp fails.
669 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700670 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400671 return 1;
672}
673
674static int parse_seccomp_filters(struct minijail *j, FILE *policy_file)
675{
676 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
677 if (compile_filter(policy_file, fprog, j->flags.log_seccomp_filter)) {
678 free(fprog);
679 return -1;
680 }
681
682 j->filter_len = fprog->len;
683 j->filter_prog = fprog;
684 return 0;
685}
686
687void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
688{
689 if (!seccomp_should_parse_filters(j))
690 return;
691
Elly Jonese1749eb2011-10-07 13:54:59 -0400692 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800693 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700694 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400695 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800696
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400697 if (parse_seccomp_filters(j, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700698 die("failed to compile seccomp filter BPF program in '%s'",
699 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800700 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400701 fclose(file);
702}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800703
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400704void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
705{
706 if (!seccomp_should_parse_filters(j))
707 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800708
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400709 FILE *file = fdopen(fd, "r");
710 if (!file) {
711 pdie("failed to associate stream with fd %d", fd);
712 }
713
714 if (parse_seccomp_filters(j, file) != 0) {
715 die("failed to compile seccomp filter BPF program from fd %d",
716 fd);
717 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400718 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500719}
720
Andrew Brestickereac28942015-11-11 16:04:46 -0800721int API minijail_use_alt_syscall(struct minijail *j, const char *table)
722{
723 j->alt_syscall_table = strdup(table);
724 if (!j->alt_syscall_table)
725 return -ENOMEM;
726 j->flags.alt_syscall = 1;
727 return 0;
728}
729
Will Drewryf89aef52011-09-16 16:48:57 -0500730struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400731 size_t available;
732 size_t total;
733 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500734};
735
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800736void marshal_state_init(struct marshal_state *state, char *buf,
737 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400738{
739 state->available = available;
740 state->buf = buf;
741 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500742}
743
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800744void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400745{
746 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500747
Elly Jonese1749eb2011-10-07 13:54:59 -0400748 /* Up to |available| will be written. */
749 if (copy_len) {
750 memcpy(state->buf, src, copy_len);
751 state->buf += copy_len;
752 state->available -= copy_len;
753 }
754 /* |total| will contain the expected length. */
755 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500756}
757
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400758void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700759{
760 marshal_append(state, m->src, strlen(m->src) + 1);
761 marshal_append(state, m->dest, strlen(m->dest) + 1);
762 marshal_append(state, m->type, strlen(m->type) + 1);
763 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
764 if (m->has_data)
765 marshal_append(state, m->data, strlen(m->data) + 1);
766 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
767}
768
Will Drewry6ac91122011-10-21 16:38:58 -0500769void minijail_marshal_helper(struct marshal_state *state,
770 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400771{
Dylan Reid648b2202015-10-23 00:50:00 -0700772 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800773 size_t i;
774
Elly Jonese1749eb2011-10-07 13:54:59 -0400775 marshal_append(state, (char *)j, sizeof(*j));
776 if (j->user)
777 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800778 if (j->suppl_gid_list) {
779 marshal_append(state, j->suppl_gid_list,
780 j->suppl_gid_count * sizeof(gid_t));
781 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400782 if (j->chrootdir)
783 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800784 if (j->alt_syscall_table) {
785 marshal_append(state, j->alt_syscall_table,
786 strlen(j->alt_syscall_table) + 1);
787 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800788 if (j->flags.seccomp_filter && j->filter_prog) {
789 struct sock_fprog *fp = j->filter_prog;
790 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800791 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400792 }
Dylan Reid648b2202015-10-23 00:50:00 -0700793 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400794 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400795 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800796 for (i = 0; i < j->cgroup_count; ++i)
797 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500798}
799
Will Drewry6ac91122011-10-21 16:38:58 -0500800size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400801{
802 struct marshal_state state;
803 marshal_state_init(&state, NULL, 0);
804 minijail_marshal_helper(&state, j);
805 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500806}
807
Elly Jonese1749eb2011-10-07 13:54:59 -0400808int minijail_marshal(const struct minijail *j, char *buf, size_t available)
809{
810 struct marshal_state state;
811 marshal_state_init(&state, buf, available);
812 minijail_marshal_helper(&state, j);
813 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500814}
815
Elly Jonese1749eb2011-10-07 13:54:59 -0400816int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
817{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800818 size_t i;
819 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500820 int ret = -EINVAL;
821
Elly Jonese1749eb2011-10-07 13:54:59 -0400822 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500823 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400824 memcpy((void *)j, serialized, sizeof(*j));
825 serialized += sizeof(*j);
826 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500827
Will Drewrybee7ba72011-10-21 20:47:01 -0500828 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -0400829 j->pid_file_path = NULL;
830 j->uidmap = NULL;
831 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -0700832 j->mounts_head = NULL;
833 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800834 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500835
Elly Jonese1749eb2011-10-07 13:54:59 -0400836 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400837 char *user = consumestr(&serialized, &length);
838 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500839 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400840 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500841 if (!j->user)
842 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400843 }
Will Drewryf89aef52011-09-16 16:48:57 -0500844
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800845 if (j->suppl_gid_list) { /* stale pointer */
846 if (j->suppl_gid_count > NGROUPS_MAX) {
847 goto bad_gid_list;
848 }
849 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
850 void *gid_list_bytes =
851 consumebytes(gid_list_size, &serialized, &length);
852 if (!gid_list_bytes)
853 goto bad_gid_list;
854
855 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
856 if (!j->suppl_gid_list)
857 goto bad_gid_list;
858
859 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
860 }
861
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400862 if (j->chrootdir) { /* stale pointer */
863 char *chrootdir = consumestr(&serialized, &length);
864 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500865 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400866 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500867 if (!j->chrootdir)
868 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400869 }
870
Andrew Brestickereac28942015-11-11 16:04:46 -0800871 if (j->alt_syscall_table) { /* stale pointer */
872 char *alt_syscall_table = consumestr(&serialized, &length);
873 if (!alt_syscall_table)
874 goto bad_syscall_table;
875 j->alt_syscall_table = strdup(alt_syscall_table);
876 if (!j->alt_syscall_table)
877 goto bad_syscall_table;
878 }
879
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800880 if (j->flags.seccomp_filter && j->filter_len > 0) {
881 size_t ninstrs = j->filter_len;
882 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
883 ninstrs > USHRT_MAX)
884 goto bad_filters;
885
886 size_t program_len = ninstrs * sizeof(struct sock_filter);
887 void *program = consumebytes(program_len, &serialized, &length);
888 if (!program)
889 goto bad_filters;
890
891 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800892 if (!j->filter_prog)
893 goto bad_filters;
894
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800895 j->filter_prog->len = ninstrs;
896 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800897 if (!j->filter_prog->filter)
898 goto bad_filter_prog_instrs;
899
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800900 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400901 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400902
Dylan Reid648b2202015-10-23 00:50:00 -0700903 count = j->mounts_count;
904 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400905 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700906 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -0700907 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400908 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700909 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700910 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400911 const char *src = consumestr(&serialized, &length);
912 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700913 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400914 dest = consumestr(&serialized, &length);
915 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700916 goto bad_mounts;
917 type = consumestr(&serialized, &length);
918 if (!type)
919 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -0700920 has_data = consumebytes(sizeof(*has_data), &serialized,
921 &length);
922 if (!has_data)
923 goto bad_mounts;
924 if (*has_data) {
925 data = consumestr(&serialized, &length);
926 if (!data)
927 goto bad_mounts;
928 }
Dylan Reid648b2202015-10-23 00:50:00 -0700929 flags = consumebytes(sizeof(*flags), &serialized, &length);
930 if (!flags)
931 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -0700932 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -0700933 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400934 }
935
Dylan Reid605ce7f2016-01-19 19:21:00 -0800936 count = j->cgroup_count;
937 j->cgroup_count = 0;
938 for (i = 0; i < count; ++i) {
939 char *cgroup = consumestr(&serialized, &length);
940 if (!cgroup)
941 goto bad_cgroups;
942 j->cgroups[i] = strdup(cgroup);
943 if (!j->cgroups[i])
944 goto bad_cgroups;
945 ++j->cgroup_count;
946 }
947
Elly Jonese1749eb2011-10-07 13:54:59 -0400948 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500949
Dylan Reid605ce7f2016-01-19 19:21:00 -0800950bad_cgroups:
951 while (j->mounts_head) {
952 struct mountpoint *m = j->mounts_head;
953 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -0700954 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -0800955 free(m->type);
956 free(m->dest);
957 free(m->src);
958 free(m);
959 }
960 for (i = 0; i < j->cgroup_count; ++i)
961 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -0700962bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800963 if (j->flags.seccomp_filter && j->filter_len > 0) {
964 free(j->filter_prog->filter);
965 free(j->filter_prog);
966 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800967bad_filter_prog_instrs:
968 if (j->filter_prog)
969 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -0500970bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800971 if (j->alt_syscall_table)
972 free(j->alt_syscall_table);
973bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500974 if (j->chrootdir)
975 free(j->chrootdir);
976bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800977 if (j->suppl_gid_list)
978 free(j->suppl_gid_list);
979bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -0500980 if (j->user)
981 free(j->user);
982clear_pointers:
983 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800984 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500985 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800986 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800987 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500988out:
989 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500990}
991
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800992/*
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400993 * setup_mount_destination: Ensures the mount target exists.
994 * Creates it if needed and possible.
Dylan Reideec77962016-06-30 19:35:10 -0700995 */
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -0400996static int setup_mount_destination(const char *source, const char *dest,
997 uid_t uid, uid_t gid)
Dylan Reideec77962016-06-30 19:35:10 -0700998{
999 int rc;
1000 struct stat st_buf;
1001
1002 rc = stat(dest, &st_buf);
1003 if (rc == 0) /* destination exists */
1004 return 0;
1005
1006 /*
1007 * Try to create the destination.
1008 * Either make a directory or touch a file depending on the source type.
1009 * If the source doesn't exist, assume it is a filesystem type such as
1010 * "tmpfs" and create a directory to mount it on.
1011 */
1012 rc = stat(source, &st_buf);
1013 if (rc || S_ISDIR(st_buf.st_mode) || S_ISBLK(st_buf.st_mode)) {
1014 if (mkdir(dest, 0700))
1015 return -errno;
1016 } else {
1017 int fd = open(dest, O_RDWR | O_CREAT, 0700);
1018 if (fd < 0)
1019 return -errno;
1020 close(fd);
1021 }
1022 return chown(dest, uid, gid);
1023}
1024
1025/*
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001026 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001027 * @j Minijail these mounts are for
1028 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001029 *
1030 * Returns 0 for success.
1031 */
Dylan Reid648b2202015-10-23 00:50:00 -07001032static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001033{
Dylan Reid648b2202015-10-23 00:50:00 -07001034 int ret;
1035 char *dest;
1036 int remount_ro = 0;
1037
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001038 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001039 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001040 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001041
Dylan Reideec77962016-06-30 19:35:10 -07001042 if (setup_mount_destination(m->src, dest, j->uid, j->gid))
1043 pdie("creating mount target '%s' failed", dest);
1044
Dylan Reid648b2202015-10-23 00:50:00 -07001045 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001046 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1047 * can't both be specified in the original bind mount.
1048 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001049 */
1050 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1051 remount_ro = 1;
1052 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001053 }
Dylan Reid648b2202015-10-23 00:50:00 -07001054
Dylan Reid81e23972016-05-18 14:06:35 -07001055 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001056 if (ret)
1057 pdie("mount: %s -> %s", m->src, dest);
1058
1059 if (remount_ro) {
1060 m->flags |= MS_RDONLY;
1061 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001062 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001063 if (ret)
1064 pdie("bind ro: %s -> %s", m->src, dest);
1065 }
1066
Elly Jones51a5b6c2011-10-12 19:09:26 -04001067 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001068 if (m->next)
1069 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001070 return ret;
1071}
1072
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001073static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001074{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001075 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001076
1077 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001078 return ret;
1079
1080 if (chroot(j->chrootdir))
1081 return -errno;
1082
1083 if (chdir("/"))
1084 return -errno;
1085
1086 return 0;
1087}
1088
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001089static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001090{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001091 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001092
1093 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001094 return ret;
1095
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001096 /*
1097 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001098 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001099 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001100 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001101 if (oldroot < 0)
1102 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001103 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001104 if (newroot < 0)
1105 pdie("failed to open %s for fchdir", j->chrootdir);
1106
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001107 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001108 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001109 * do a self bind mount.
1110 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001111 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1112 pdie("failed to bind mount '%s'", j->chrootdir);
1113 if (chdir(j->chrootdir))
1114 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001115 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001116 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001117
1118 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001119 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001120 * change to the old root and unmount it.
1121 */
1122 if (fchdir(oldroot))
1123 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001124
1125 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001126 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1127 * there could be a shared mount point under |oldroot|. In that case,
1128 * mounts under this shared mount point will be unmounted below, and
1129 * this unmounting will propagate to the original mount namespace
1130 * (because the mount point is shared). To prevent this unexpected
1131 * unmounting, remove these mounts from their peer groups by recursively
1132 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001133 */
1134 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001135 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001136 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001137 if (umount2(".", MNT_DETACH))
1138 pdie("umount(/)");
1139 /* Change back to the new root. */
1140 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001141 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001142 if (close(oldroot))
1143 return -errno;
1144 if (close(newroot))
1145 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001146 if (chroot("/"))
1147 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001148 /* Set correct CWD for getcwd(3). */
1149 if (chdir("/"))
1150 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001151
1152 return 0;
1153}
1154
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001155static int mount_tmp(void)
Lee Campbell11af0622014-05-22 12:36:04 -07001156{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001157 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001158}
1159
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001160static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001161{
1162 const char *kProcPath = "/proc";
1163 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001164 /*
1165 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001166 * /proc in our namespace, which means using MS_REMOUNT here would
1167 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001168 * namespace (!). Instead, remove their mount from our namespace lazily
1169 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001170 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001171 if (umount2(kProcPath, MNT_DETACH)) {
1172 /*
1173 * If we are in a new user namespace, umount(2) will fail.
1174 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1175 */
1176 if (j->flags.userns) {
1177 info("umount(/proc, MNT_DETACH) failed, "
1178 "this is expected when using user namespaces");
1179 } else {
1180 return -errno;
1181 }
1182 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001183 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1184 return -errno;
1185 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001186}
1187
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001188static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001189{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001190 kill(j->initpid, SIGKILL);
1191 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001192}
1193
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001194static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001195{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001196 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001197 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001198}
1199
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001200static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001201{
1202 size_t i;
1203
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001204 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001205 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001206 kill_child_and_die(j, "failed to add to cgroups");
1207 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001208}
1209
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001210static void write_ugid_maps_or_die(const struct minijail *j)
1211{
1212 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1213 kill_child_and_die(j, "failed to write uid_map");
1214 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1215 kill_child_and_die(j, "failed to write gid_map");
1216}
1217
1218static void enter_user_namespace(const struct minijail *j)
1219{
1220 if (j->uidmap && setresuid(0, 0, 0))
1221 pdie("user_namespaces: setresuid(0, 0, 0) failed");
1222 if (j->gidmap && setresgid(0, 0, 0))
1223 pdie("user_namespaces: setresgid(0, 0, 0) failed");
1224}
1225
1226static void parent_setup_complete(int *pipe_fds)
1227{
1228 close(pipe_fds[0]);
1229 close(pipe_fds[1]);
1230}
1231
1232/*
1233 * wait_for_parent_setup: Called by the child process to wait for any
1234 * further parent-side setup to complete before continuing.
1235 */
1236static void wait_for_parent_setup(int *pipe_fds)
1237{
1238 char buf;
1239
1240 close(pipe_fds[1]);
1241
1242 /* Wait for parent to complete setup and close the pipe. */
1243 if (read(pipe_fds[0], &buf, 1) != 0)
1244 die("failed to sync with parent");
1245 close(pipe_fds[0]);
1246}
1247
1248static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001249{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001250 if (j->flags.usergroups && j->flags.suppl_gids) {
1251 die("tried to inherit *and* set supplementary groups;"
1252 " can only do one");
1253 }
1254
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001255 if (j->flags.usergroups) {
1256 if (initgroups(j->user, j->usergid))
1257 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001258 } else if (j->flags.suppl_gids) {
1259 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1260 pdie("setgroups");
1261 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001262 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001263 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001264 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001265 * users.
1266 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001267 if ((j->uid || j->gid) && setgroups(0, NULL))
1268 pdie("setgroups");
1269 }
1270
1271 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1272 pdie("setresgid");
1273
1274 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1275 pdie("setresuid");
1276}
1277
Mike Frysinger3adfef72013-05-09 17:19:08 -04001278/*
1279 * We specifically do not use cap_valid() as that only tells us the last
1280 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001281 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001282 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001283 * kernel).
1284 * Normally, we suck up the answer via /proc. On Android, not all processes are
1285 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1286 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001287 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001288static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001289{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001290 unsigned int last_valid_cap = 0;
1291 if (is_android()) {
1292 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1293 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001294
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001295 /* |last_valid_cap| will be the first failing value. */
1296 if (last_valid_cap > 0) {
1297 last_valid_cap--;
1298 }
1299 } else {
1300 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1301 FILE *fp = fopen(cap_file, "re");
1302 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1303 pdie("fscanf(%s)", cap_file);
1304 fclose(fp);
1305 }
Dylan Reidf682d472015-09-17 21:39:07 -07001306 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001307}
1308
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001309static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1310{
1311 const uint64_t one = 1;
1312 unsigned int i;
1313 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1314 if (keep_mask & (one << i))
1315 continue;
1316 if (prctl(PR_CAPBSET_DROP, i))
1317 pdie("could not drop capability from bounding set");
1318 }
1319}
1320
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001321static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001322{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001323 if (!j->flags.use_caps)
1324 return;
1325
Elly Jonese1749eb2011-10-07 13:54:59 -04001326 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001327 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001328 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001329 unsigned int i;
1330 if (!caps)
1331 die("can't get process caps");
1332 if (cap_clear_flag(caps, CAP_INHERITABLE))
1333 die("can't clear inheritable caps");
1334 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1335 die("can't clear effective caps");
1336 if (cap_clear_flag(caps, CAP_PERMITTED))
1337 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001338 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001339 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001340 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001341 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001342 flag[0] = i;
1343 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001344 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001345 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001346 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001347 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001348 die("can't add inheritable cap");
1349 }
1350 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001351 die("can't apply initial cleaned capset");
1352
1353 /*
1354 * Instead of dropping bounding set first, do it here in case
1355 * the caller had a more permissive bounding set which could
1356 * have been used above to raise a capability that wasn't already
1357 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1358 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001359 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001360
1361 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001362 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001363 flag[0] = CAP_SETPCAP;
1364 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1365 die("can't clear effective cap");
1366 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1367 die("can't clear permitted cap");
1368 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1369 die("can't clear inheritable cap");
1370 }
1371
1372 if (cap_set_proc(caps))
1373 die("can't apply final cleaned capset");
1374
1375 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001376}
1377
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001378static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001379{
1380 /*
1381 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1382 * in the kernel source tree for an explanation of the parameters.
1383 */
1384 if (j->flags.no_new_privs) {
1385 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1386 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1387 }
1388
1389 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001390 * Code running with ASan
1391 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1392 * will make system calls not included in the syscall filter policy,
1393 * which will likely crash the program. Skip setting seccomp filter in
1394 * that case.
1395 * 'running_with_asan()' has no inputs and is completely defined at
1396 * build time, so this cannot be used by an attacker to skip setting
1397 * seccomp filter.
1398 */
1399 if (j->flags.seccomp_filter && running_with_asan()) {
1400 warn("running with ASan, not setting seccomp filter");
1401 return;
1402 }
1403
1404 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001405 * If we're logging seccomp filter failures,
1406 * install the SIGSYS handler first.
1407 */
1408 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1409 if (install_sigsys_handler())
1410 pdie("install SIGSYS handler");
1411 warn("logging seccomp filter failures");
1412 }
1413
1414 /*
1415 * Install the syscall filter.
1416 */
1417 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001418 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1419 j->filter_prog)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001420 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001421 warn("seccomp not supported");
1422 return;
1423 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001424 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001425 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001426 }
1427}
1428
Will Drewry6ac91122011-10-21 16:38:58 -05001429void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001430{
Dylan Reidf682d472015-09-17 21:39:07 -07001431 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001432 * If we're dropping caps, get the last valid cap from /proc now,
1433 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001434 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001435 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001436 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001437 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001438
Elly Jonese1749eb2011-10-07 13:54:59 -04001439 if (j->flags.pids)
1440 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001441 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001442
Elly Jonese1749eb2011-10-07 13:54:59 -04001443 if (j->flags.usergroups && !j->user)
1444 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001445
Elly Jonesdd3e8512012-01-23 15:13:38 -05001446 /*
1447 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001448 * so we don't even try. If any of our operations fail, we abort() the
1449 * entire process.
1450 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001451 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1452 pdie("setns(CLONE_NEWNS)");
1453
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001454 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001455 if (unshare(CLONE_NEWNS))
1456 pdie("unshare(vfs)");
1457 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001458 * Unless asked not to, remount all filesystems as private.
1459 * If they are shared, new bind mounts will creep out of our
1460 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001461 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1462 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001463 if (!j->flags.skip_remount_private) {
1464 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1465 pdie("mount(/, private)");
1466 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001467 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001468
Dylan Reidf7942472015-11-18 17:55:26 -08001469 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1470 pdie("unshare(ipc)");
1471 }
1472
Dylan Reid1102f5a2015-09-15 11:52:20 -07001473 if (j->flags.enter_net) {
1474 if (setns(j->netns_fd, CLONE_NEWNET))
1475 pdie("setns(CLONE_NEWNET)");
1476 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001477 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001478 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001479
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001480 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
1481 pdie("unshare(cgroups)");
1482
Elly Jones51a5b6c2011-10-12 19:09:26 -04001483 if (j->flags.chroot && enter_chroot(j))
1484 pdie("chroot");
1485
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001486 if (j->flags.pivot_root && enter_pivot_root(j))
1487 pdie("pivot_root");
1488
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001489 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001490 pdie("mount_tmp");
1491
Dylan Reid791f5772015-09-14 20:02:42 -07001492 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001493 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001494
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001495 /*
1496 * If we're only dropping capabilities from the bounding set, but not
1497 * from the thread's (permitted|inheritable|effective) sets, do it now.
1498 */
1499 if (j->flags.capbset_drop) {
1500 drop_capbset(j->cap_bset, last_valid_cap);
1501 }
1502
1503 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001504 /*
1505 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001506 * capability to change uids, our attempt to use setuid()
1507 * below will fail. Hang on to root caps across setuid(), then
1508 * lock securebits.
1509 */
1510 if (prctl(PR_SET_KEEPCAPS, 1))
1511 pdie("prctl(PR_SET_KEEPCAPS)");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001512
1513 /*
1514 * Kernels 4.3+ define a new securebit
1515 * (SECURE_NO_CAP_AMBIENT_RAISE), so using the SECURE_ALL_BITS
1516 * and SECURE_ALL_LOCKS masks from newer kernel headers will
1517 * return EPERM on older kernels. Detect this, and retry with
1518 * the right mask for older (2.6.26-4.2) kernels.
1519 */
1520 int securebits_ret = prctl(PR_SET_SECUREBITS,
1521 SECURE_ALL_BITS | SECURE_ALL_LOCKS);
1522 if (securebits_ret < 0) {
1523 if (errno == EPERM) {
1524 /* Possibly running on kernel < 4.3. */
1525 securebits_ret = prctl(
1526 PR_SET_SECUREBITS,
1527 OLD_SECURE_ALL_BITS | OLD_SECURE_ALL_LOCKS);
1528 }
1529 }
1530 if (securebits_ret < 0)
Elly Jonese1749eb2011-10-07 13:54:59 -04001531 pdie("prctl(PR_SET_SECUREBITS)");
1532 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001533
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001534 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001535 /*
1536 * If we're setting no_new_privs, we can drop privileges
1537 * before setting seccomp filter. This way filter policies
1538 * don't need to allow privilege-dropping syscalls.
1539 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001540 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001541 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001542 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001543 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001544 /*
1545 * If we're not setting no_new_privs,
1546 * we need to set seccomp filter *before* dropping privileges.
1547 * WARNING: this means that filter policies *must* allow
1548 * setgroups()/setresgid()/setresuid() for dropping root and
1549 * capget()/capset()/prctl() for dropping caps.
1550 */
1551 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001552 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001553 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001554 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001555
Elly Jonesdd3e8512012-01-23 15:13:38 -05001556 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001557 * Select the specified alternate syscall table. The table must not
1558 * block prctl(2) if we're using seccomp as well.
1559 */
1560 if (j->flags.alt_syscall) {
1561 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1562 pdie("prctl(PR_ALT_SYSCALL)");
1563 }
1564
1565 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001566 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001567 * privilege-dropping syscalls :)
1568 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001569 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001570 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001571 warn("seccomp not supported");
1572 return;
1573 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001574 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001575 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001576}
1577
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001578/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001579static int init_exitstatus = 0;
1580
Will Drewry6ac91122011-10-21 16:38:58 -05001581void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001582{
1583 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001584}
1585
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001586void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001587{
1588 pid_t pid;
1589 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001590 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001591 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001592 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001593 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001594 /*
1595 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001596 * left inside our pid namespace or we get a signal.
1597 */
1598 if (pid == rootpid)
1599 init_exitstatus = status;
1600 }
1601 if (!WIFEXITED(init_exitstatus))
1602 _exit(MINIJAIL_ERR_INIT);
1603 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001604}
1605
Will Drewry6ac91122011-10-21 16:38:58 -05001606int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001607{
1608 size_t sz = 0;
1609 size_t bytes = read(fd, &sz, sizeof(sz));
1610 char *buf;
1611 int r;
1612 if (sizeof(sz) != bytes)
1613 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001614 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001615 return -E2BIG;
1616 buf = malloc(sz);
1617 if (!buf)
1618 return -ENOMEM;
1619 bytes = read(fd, buf, sz);
1620 if (bytes != sz) {
1621 free(buf);
1622 return -EINVAL;
1623 }
1624 r = minijail_unmarshal(j, buf, sz);
1625 free(buf);
1626 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001627}
1628
Will Drewry6ac91122011-10-21 16:38:58 -05001629int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001630{
1631 char *buf;
1632 size_t sz = minijail_size(j);
1633 ssize_t written;
1634 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001635
Elly Jonese1749eb2011-10-07 13:54:59 -04001636 if (!sz)
1637 return -EINVAL;
1638 buf = malloc(sz);
1639 r = minijail_marshal(j, buf, sz);
1640 if (r) {
1641 free(buf);
1642 return r;
1643 }
1644 /* Sends [size][minijail]. */
1645 written = write(fd, &sz, sizeof(sz));
1646 if (written != sizeof(sz)) {
1647 free(buf);
1648 return -EFAULT;
1649 }
1650 written = write(fd, buf, sz);
1651 if (written < 0 || (size_t) written != sz) {
1652 free(buf);
1653 return -EFAULT;
1654 }
1655 free(buf);
1656 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001657}
Elly Jonescd7a9042011-07-22 13:56:51 -04001658
Will Drewry6ac91122011-10-21 16:38:58 -05001659int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001660{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001661#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001662 /* Don't use LDPRELOAD on Brillo. */
1663 return 0;
1664#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001665 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1666 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1667 if (!newenv)
1668 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001669
Elly Jonese1749eb2011-10-07 13:54:59 -04001670 /* Only insert a separating space if we have something to separate... */
1671 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1672 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001673
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001674 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001675 setenv(kLdPreloadEnvVar, newenv, 1);
1676 free(newenv);
1677 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001678#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001679}
1680
Will Drewry6ac91122011-10-21 16:38:58 -05001681int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001682{
1683 int r = pipe(fds);
1684 char fd_buf[11];
1685 if (r)
1686 return r;
1687 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1688 if (r <= 0)
1689 return -EINVAL;
1690 setenv(kFdEnvVar, fd_buf, 1);
1691 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001692}
1693
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001694int setup_pipe_end(int fds[2], size_t index)
1695{
1696 if (index > 1)
1697 return -1;
1698
1699 close(fds[1 - index]);
1700 return fds[index];
1701}
1702
1703int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1704{
1705 if (index > 1)
1706 return -1;
1707
1708 close(fds[1 - index]);
1709 /* dup2(2) the corresponding end of the pipe into |fd|. */
1710 return dup2(fds[index], fd);
1711}
1712
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001713int minijail_run_internal(struct minijail *j, const char *filename,
1714 char *const argv[], pid_t *pchild_pid,
1715 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1716 int use_preload);
1717
Will Drewry6ac91122011-10-21 16:38:58 -05001718int API minijail_run(struct minijail *j, const char *filename,
1719 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001720{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001721 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1722 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001723}
1724
1725int API minijail_run_pid(struct minijail *j, const char *filename,
1726 char *const argv[], pid_t *pchild_pid)
1727{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001728 return minijail_run_internal(j, filename, argv, pchild_pid,
1729 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001730}
1731
1732int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001733 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001734{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001735 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1736 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001737}
1738
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001739int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001740 char *const argv[], pid_t *pchild_pid,
1741 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001742{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001743 return minijail_run_internal(j, filename, argv, pchild_pid,
1744 pstdin_fd, pstdout_fd, pstderr_fd, true);
1745}
1746
1747int API minijail_run_no_preload(struct minijail *j, const char *filename,
1748 char *const argv[])
1749{
1750 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1751 false);
1752}
1753
Samuel Tan63187f42015-10-16 13:01:53 -07001754int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001755 const char *filename,
1756 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001757 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001758 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001759 int *pstderr_fd)
1760{
Samuel Tan63187f42015-10-16 13:01:53 -07001761 return minijail_run_internal(j, filename, argv, pchild_pid,
1762 pstdin_fd, pstdout_fd, pstderr_fd, false);
1763}
1764
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001765int minijail_run_internal(struct minijail *j, const char *filename,
1766 char *const argv[], pid_t *pchild_pid,
1767 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1768 int use_preload)
1769{
Elly Jonese1749eb2011-10-07 13:54:59 -04001770 char *oldenv, *oldenv_copy = NULL;
1771 pid_t child_pid;
1772 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001773 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001774 int stdout_fds[2];
1775 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001776 int child_sync_pipe_fds[2];
1777 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001778 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001779 /* We need to remember this across the minijail_preexec() call. */
1780 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001781 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001782
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001783 if (use_preload) {
1784 oldenv = getenv(kLdPreloadEnvVar);
1785 if (oldenv) {
1786 oldenv_copy = strdup(oldenv);
1787 if (!oldenv_copy)
1788 return -ENOMEM;
1789 }
1790
1791 if (setup_preload())
1792 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001793 }
Will Drewryf89aef52011-09-16 16:48:57 -05001794
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001795 if (!use_preload) {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04001796 if (j->flags.use_caps && j->caps != 0)
1797 die("non-empty capabilities are not supported without LD_PRELOAD");
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001798 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001799
Elly Jonesdd3e8512012-01-23 15:13:38 -05001800 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001801 * Make the process group ID of this process equal to its PID, so that
1802 * both the Minijail process and the jailed process can be killed
1803 * together.
1804 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1805 * the process is already a process group leader.
1806 */
1807 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1808 if (errno != EPERM) {
1809 pdie("setpgid(0, 0)");
1810 }
1811 }
1812
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001813 if (use_preload) {
1814 /*
1815 * Before we fork(2) and execve(2) the child process, we need
1816 * to open a pipe(2) to send the minijail configuration over.
1817 */
1818 if (setup_pipe(pipe_fds))
1819 return -EFAULT;
1820 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001821
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001822 /*
1823 * If we want to write to the child process' standard input,
1824 * create the pipe(2) now.
1825 */
1826 if (pstdin_fd) {
1827 if (pipe(stdin_fds))
1828 return -EFAULT;
1829 }
1830
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001831 /*
1832 * If we want to read from the child process' standard output,
1833 * create the pipe(2) now.
1834 */
1835 if (pstdout_fd) {
1836 if (pipe(stdout_fds))
1837 return -EFAULT;
1838 }
1839
1840 /*
1841 * If we want to read from the child process' standard error,
1842 * create the pipe(2) now.
1843 */
1844 if (pstderr_fd) {
1845 if (pipe(stderr_fds))
1846 return -EFAULT;
1847 }
1848
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001849 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04001850 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001851 * or if we need to add the child process to cgroups, create the pipe(2)
1852 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001853 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001854 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001855 sync_child = 1;
1856 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001857 return -EFAULT;
1858 }
1859
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001860 /*
1861 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001862 *
1863 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1864 *
1865 * In multithreaded programs, there are a bunch of locks inside libc,
1866 * some of which may be held by other threads at the time that we call
1867 * minijail_run_pid(). If we call fork(), glibc does its level best to
1868 * ensure that we hold all of these locks before it calls clone()
1869 * internally and drop them after clone() returns, but when we call
1870 * sys_clone(2) directly, all that gets bypassed and we end up with a
1871 * child address space where some of libc's important locks are held by
1872 * other threads (which did not get cloned, and hence will never release
1873 * those locks). This is okay so long as we call exec() immediately
1874 * after, but a bunch of seemingly-innocent libc functions like setenv()
1875 * take locks.
1876 *
1877 * Hence, only call sys_clone() if we need to, in order to get at pid
1878 * namespacing. If we follow this path, the child's address space might
1879 * have broken locks; you may only call functions that do not acquire
1880 * any locks.
1881 *
1882 * Unfortunately, fork() acquires every lock it can get its hands on, as
1883 * previously detailed, so this function is highly likely to deadlock
1884 * later on (see "deadlock here") if we're multithreaded.
1885 *
1886 * We might hack around this by having the clone()d child (init of the
1887 * pid namespace) return directly, rather than leaving the clone()d
1888 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001889 * its fork()ed child return in turn), but that process would be
1890 * crippled with its libc locks potentially broken. We might try
1891 * fork()ing in the parent before we clone() to ensure that we own all
1892 * the locks, but then we have to have the forked child hanging around
1893 * consuming resources (and possibly having file descriptors / shared
1894 * memory regions / etc attached). We'd need to keep the child around to
1895 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04001896 *
1897 * TODO(ellyjones): figure out if the "forked child hanging around"
1898 * problem is fixable or not. It would be nice if we worked in this
1899 * case.
1900 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001901 if (pid_namespace) {
1902 int clone_flags = CLONE_NEWPID | SIGCHLD;
1903 if (j->flags.userns)
1904 clone_flags |= CLONE_NEWUSER;
1905 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001906 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001907 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001908 }
Elly Jones761b7412012-06-13 15:49:52 -04001909
Elly Jonese1749eb2011-10-07 13:54:59 -04001910 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001911 if (use_preload) {
1912 free(oldenv_copy);
1913 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001914 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001915 }
Will Drewryf89aef52011-09-16 16:48:57 -05001916
Elly Jonese1749eb2011-10-07 13:54:59 -04001917 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001918 if (use_preload) {
1919 /* Restore parent's LD_PRELOAD. */
1920 if (oldenv_copy) {
1921 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1922 free(oldenv_copy);
1923 } else {
1924 unsetenv(kLdPreloadEnvVar);
1925 }
1926 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001927 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001928
Elly Jonese1749eb2011-10-07 13:54:59 -04001929 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001930
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001931 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001932 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001933
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001934 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001935 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001936
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001937 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001938 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08001939
1940 if (sync_child)
1941 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001942
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001943 if (use_preload) {
1944 /* Send marshalled minijail. */
1945 close(pipe_fds[0]); /* read endpoint */
1946 ret = minijail_to_fd(j, pipe_fds[1]);
1947 close(pipe_fds[1]); /* write endpoint */
1948 if (ret) {
1949 kill(j->initpid, SIGKILL);
1950 die("failed to send marshalled minijail");
1951 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001952 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001953
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001954 if (pchild_pid)
1955 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001956
1957 /*
1958 * If we want to write to the child process' standard input,
1959 * set up the write end of the pipe.
1960 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001961 if (pstdin_fd)
1962 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001963 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001964
1965 /*
1966 * If we want to read from the child process' standard output,
1967 * set up the read end of the pipe.
1968 */
1969 if (pstdout_fd)
1970 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001971 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001972
1973 /*
1974 * If we want to read from the child process' standard error,
1975 * set up the read end of the pipe.
1976 */
1977 if (pstderr_fd)
1978 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001979 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001980
Elly Jonese1749eb2011-10-07 13:54:59 -04001981 return 0;
1982 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04001983 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001984 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001985
Peter Qiu2860c462015-12-16 15:13:06 -08001986 if (j->flags.reset_signal_mask) {
1987 sigset_t signal_mask;
1988 if (sigemptyset(&signal_mask) != 0)
1989 pdie("sigemptyset failed");
1990 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
1991 pdie("sigprocmask failed");
1992 }
1993
Dylan Reidce5b55e2016-01-13 11:04:16 -08001994 if (sync_child)
1995 wait_for_parent_setup(child_sync_pipe_fds);
1996
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001997 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001998 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001999
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002000 /*
2001 * If we want to write to the jailed process' standard input,
2002 * set up the read end of the pipe.
2003 */
2004 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002005 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2006 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002007 die("failed to set up stdin pipe");
2008 }
2009
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002010 /*
2011 * If we want to read from the jailed process' standard output,
2012 * set up the write end of the pipe.
2013 */
2014 if (pstdout_fd) {
2015 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2016 STDOUT_FILENO) < 0)
2017 die("failed to set up stdout pipe");
2018 }
2019
2020 /*
2021 * If we want to read from the jailed process' standard error,
2022 * set up the write end of the pipe.
2023 */
2024 if (pstderr_fd) {
2025 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2026 STDERR_FILENO) < 0)
2027 die("failed to set up stderr pipe");
2028 }
2029
Dylan Reid791f5772015-09-14 20:02:42 -07002030 /* If running an init program, let it decide when/how to mount /proc. */
2031 if (pid_namespace && !do_init)
2032 j->flags.remount_proc_ro = 0;
2033
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002034 if (use_preload) {
2035 /* Strip out flags that cannot be inherited across execve(2). */
2036 minijail_preexec(j);
2037 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002038 /*
2039 * If not using LD_PRELOAD, do all jailing before execve(2).
2040 * Note that PID namespaces can only be entered on fork(2),
2041 * so that flag is still cleared.
2042 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002043 j->flags.pids = 0;
2044 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002045 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002046 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002047
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002048 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002049 /*
2050 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002051 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002052 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002053 * a child to actually run the program. If |do_init == 0|, we
2054 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002055 *
2056 * If we're multithreaded, we'll probably deadlock here. See
2057 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002058 */
2059 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002060 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002061 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002062 } else if (child_pid > 0) {
2063 /* Best effort. Don't bother checking the return value. */
2064 prctl(PR_SET_NAME, "minijail-init");
2065 init(child_pid); /* Never returns. */
2066 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002067 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002068
Elly Jonesdd3e8512012-01-23 15:13:38 -05002069 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002070 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002071 * calling process
2072 * -> execve()-ing process
2073 * If we are:
2074 * calling process
2075 * -> init()-ing process
2076 * -> execve()-ing process
2077 */
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002078 ret = execve(filename, argv, environ);
2079 if (ret == -1) {
2080 pwarn("execve(%s) failed", filename);
2081 }
2082 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002083}
2084
Will Drewry6ac91122011-10-21 16:38:58 -05002085int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002086{
2087 int st;
2088 if (kill(j->initpid, SIGTERM))
2089 return -errno;
2090 if (waitpid(j->initpid, &st, 0) < 0)
2091 return -errno;
2092 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002093}
2094
Will Drewry6ac91122011-10-21 16:38:58 -05002095int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002096{
2097 int st;
2098 if (waitpid(j->initpid, &st, 0) < 0)
2099 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002100
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002101 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002102 int error_status = st;
2103 if (WIFSIGNALED(st)) {
2104 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002105 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002106 j->initpid, signum);
2107 /*
2108 * We return MINIJAIL_ERR_JAIL if the process received
2109 * SIGSYS, which happens when a syscall is blocked by
2110 * seccomp filters.
2111 * If not, we do what bash(1) does:
2112 * $? = 128 + signum
2113 */
2114 if (signum == SIGSYS) {
2115 error_status = MINIJAIL_ERR_JAIL;
2116 } else {
2117 error_status = 128 + signum;
2118 }
2119 }
2120 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002121 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002122
2123 int exit_status = WEXITSTATUS(st);
2124 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002125 info("child process %d exited with status %d",
2126 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002127
2128 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002129}
2130
Will Drewry6ac91122011-10-21 16:38:58 -05002131void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002132{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002133 size_t i;
2134
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002135 if (j->flags.seccomp_filter && j->filter_prog) {
2136 free(j->filter_prog->filter);
2137 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002138 }
Dylan Reid648b2202015-10-23 00:50:00 -07002139 while (j->mounts_head) {
2140 struct mountpoint *m = j->mounts_head;
2141 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002142 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002143 free(m->type);
2144 free(m->dest);
2145 free(m->src);
2146 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002147 }
Dylan Reid648b2202015-10-23 00:50:00 -07002148 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002149 if (j->user)
2150 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002151 if (j->suppl_gid_list)
2152 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002153 if (j->chrootdir)
2154 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002155 if (j->pid_file_path)
2156 free(j->pid_file_path);
2157 if (j->uidmap)
2158 free(j->uidmap);
2159 if (j->gidmap)
2160 free(j->gidmap);
Andrew Brestickereac28942015-11-11 16:04:46 -08002161 if (j->alt_syscall_table)
2162 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002163 for (i = 0; i < j->cgroup_count; ++i)
2164 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002165 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002166}