blob: 5513b19fd9cd9f9898fd4307ac36170ec307349f [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 Obes13650612016-09-02 11:27:29 -040043#include "syscall_wrapper.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070044#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080045
Lei Zhangeee31552012-10-17 21:27:10 -070046#ifdef HAVE_SECUREBITS_H
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070047# include <linux/securebits.h>
Lei Zhangeee31552012-10-17 21:27:10 -070048#else
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070049# define SECURE_ALL_BITS 0x55
50# define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
51#endif
52/* For kernels < 4.3. */
53#define OLD_SECURE_ALL_BITS 0x15
54#define OLD_SECURE_ALL_LOCKS (OLD_SECURE_ALL_BITS << 1)
55
56/*
57 * Assert the value of SECURE_ALL_BITS at compile-time.
58 * Brillo devices are currently compiled against 4.4 kernel headers. Kernel 4.3
59 * added a new securebit.
60 * When a new securebit is added, the new SECURE_ALL_BITS mask will return EPERM
61 * when used on older kernels. The compile-time assert will catch this situation
62 * at compile time.
63 */
64#ifdef __BRILLO__
65_Static_assert(SECURE_ALL_BITS == 0x55, "SECURE_ALL_BITS == 0x55.");
Lei Zhangeee31552012-10-17 21:27:10 -070066#endif
67
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070068/* Until these are reliably available in linux/prctl.h. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080069#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070070# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080071#endif
72
Andrew Brestickereac28942015-11-11 16:04:46 -080073#ifndef PR_ALT_SYSCALL
74# define PR_ALT_SYSCALL 0x43724f53
75#endif
76
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040077/* Seccomp filter related flags. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080078#ifndef PR_SET_NO_NEW_PRIVS
79# define PR_SET_NO_NEW_PRIVS 38
80#endif
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040081
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080082#ifndef SECCOMP_MODE_FILTER
83# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050084#endif
85
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040086#ifndef SECCOMP_SET_MODE_STRICT
87# define SECCOMP_SET_MODE_STRICT 0
88#endif
89#ifndef SECCOMP_SET_MODE_FILTER
90# define SECCOMP_SET_MODE_FILTER 1
91#endif
92
93#ifndef SECCOMP_FILTER_FLAG_TSYNC
94# define SECCOMP_FILTER_FLAG_TSYNC 1
95#endif
96/* End seccomp filter related flags. */
97
Dylan Reid4cbc2a52016-06-17 19:06:07 -070098/* New cgroup namespace might not be in linux-headers yet. */
99#ifndef CLONE_NEWCGROUP
100# define CLONE_NEWCGROUP 0x02000000
101#endif
102
Dylan Reid605ce7f2016-01-19 19:21:00 -0800103#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
104
Dylan Reid648b2202015-10-23 00:50:00 -0700105struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400106 char *src;
107 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700108 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700109 char *data;
110 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -0700111 unsigned long flags;
112 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400113};
114
Will Drewryf89aef52011-09-16 16:48:57 -0500115struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700116 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700117 * WARNING: if you add a flag here you need to make sure it's
118 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700119 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400120 struct {
121 int uid:1;
122 int gid:1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800123 int usergroups:1;
124 int suppl_gids:1;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800125 int use_caps:1;
126 int capbset_drop:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400127 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700128 int enter_vfs:1;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800129 int skip_remount_private:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400130 int pids:1;
Dylan Reidf7942472015-11-18 17:55:26 -0800131 int ipc:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400132 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700133 int enter_net:1;
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700134 int ns_cgroups:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800135 int userns:1;
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400136 int disable_setgroups:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400137 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -0700138 int remount_proc_ro:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700139 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400140 int seccomp_filter:1;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400141 int seccomp_filter_tsync:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700142 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400143 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800144 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700145 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800146 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800147 int pid_file:1;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800148 int cgroups:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800149 int alt_syscall:1;
Peter Qiu2860c462015-12-16 15:13:06 -0800150 int reset_signal_mask:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400151 } flags;
152 uid_t uid;
153 gid_t gid;
154 gid_t usergid;
155 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800156 size_t suppl_gid_count;
157 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400158 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800159 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400160 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700161 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700162 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400163 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800164 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800165 char *uidmap;
166 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800167 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800168 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800169 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700170 struct mountpoint *mounts_head;
171 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800172 size_t mounts_count;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800173 char *cgroups[MAX_CGROUPS];
174 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500175};
176
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700177/*
178 * Strip out flags meant for the parent.
179 * We keep things that are not inherited across execve(2) (e.g. capabilities),
180 * or are easier to set after execve(2) (e.g. seccomp filters).
181 */
182void minijail_preenter(struct minijail *j)
183{
184 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700185 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900186 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700187 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700188 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800189 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800190 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800191 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700192}
193
194/*
195 * Strip out flags meant for the child.
196 * We keep things that are inherited across execve(2).
197 */
198void minijail_preexec(struct minijail *j)
199{
200 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700201 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800202 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700203 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800204 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700205 if (j->user)
206 free(j->user);
207 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800208 if (j->suppl_gid_list)
209 free(j->suppl_gid_list);
210 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700211 memset(&j->flags, 0, sizeof(j->flags));
212 /* Now restore anything we meant to keep. */
213 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700214 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800215 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700216 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800217 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700218 /* Note, |pids| will already have been used before this call. */
219}
220
221/* Minijail API. */
222
Will Drewry6ac91122011-10-21 16:38:58 -0500223struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400224{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400225 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400226}
227
Will Drewry6ac91122011-10-21 16:38:58 -0500228void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400229{
230 if (uid == 0)
231 die("useless change to uid 0");
232 j->uid = uid;
233 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400234}
235
Will Drewry6ac91122011-10-21 16:38:58 -0500236void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400237{
238 if (gid == 0)
239 die("useless change to gid 0");
240 j->gid = gid;
241 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400242}
243
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800244void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
245 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800246{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800247 size_t i;
248
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800249 if (j->flags.usergroups)
250 die("cannot inherit *and* set supplementary groups");
251
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800252 if (size == 0) {
253 /* Clear supplementary groups. */
254 j->suppl_gid_list = NULL;
255 j->suppl_gid_count = 0;
256 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800257 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800258 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800259
260 /* Copy the gid_t array. */
261 j->suppl_gid_list = calloc(size, sizeof(gid_t));
262 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800263 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800264 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800265 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800266 j->suppl_gid_list[i] = list[i];
267 }
268 j->suppl_gid_count = size;
269 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800270}
271
Will Drewry6ac91122011-10-21 16:38:58 -0500272int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400273{
274 char *buf = NULL;
275 struct passwd pw;
276 struct passwd *ppw = NULL;
277 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
278 if (sz == -1)
279 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400280
Elly Jonesdd3e8512012-01-23 15:13:38 -0500281 /*
282 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400283 * the maximum needed size of the buffer, so we don't have to search.
284 */
285 buf = malloc(sz);
286 if (!buf)
287 return -ENOMEM;
288 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500289 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800290 * We're safe to free the buffer here. The strings inside |pw| point
291 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800292 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
293 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500294 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400295 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700296 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400297 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700298 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400299 minijail_change_uid(j, ppw->pw_uid);
300 j->user = strdup(user);
301 if (!j->user)
302 return -ENOMEM;
303 j->usergid = ppw->pw_gid;
304 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400305}
306
Will Drewry6ac91122011-10-21 16:38:58 -0500307int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400308{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700309 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700310 struct group gr;
311 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400312 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
313 if (sz == -1)
314 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400315
Elly Jonesdd3e8512012-01-23 15:13:38 -0500316 /*
317 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400318 * the maximum needed size of the buffer, so we don't have to search.
319 */
320 buf = malloc(sz);
321 if (!buf)
322 return -ENOMEM;
323 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500324 /*
325 * We're safe to free the buffer here. The strings inside gr point
326 * inside buf, but we don't use any of them; this leaves the pointers
327 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
328 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400329 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700330 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400331 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700332 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400333 minijail_change_gid(j, pgr->gr_gid);
334 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400335}
336
Will Drewry6ac91122011-10-21 16:38:58 -0500337void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400338{
339 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400340}
341
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700342void API minijail_no_new_privs(struct minijail *j)
343{
344 j->flags.no_new_privs = 1;
345}
346
Will Drewry6ac91122011-10-21 16:38:58 -0500347void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400348{
349 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500350}
351
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400352void API minijail_set_seccomp_filter_tsync(struct minijail *j)
353{
354 j->flags.seccomp_filter_tsync = 1;
355}
356
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700357void API minijail_log_seccomp_filter_failures(struct minijail *j)
358{
359 j->flags.log_seccomp_filter = 1;
360}
361
Will Drewry6ac91122011-10-21 16:38:58 -0500362void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400363{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800364 /*
365 * 'minijail_use_caps' configures a runtime-capabilities-only
366 * environment, including a bounding set matching the thread's runtime
367 * (permitted|inheritable|effective) sets.
368 * Therefore, it will override any existing bounding set configurations
369 * since the latter would allow gaining extra runtime capabilities from
370 * file capabilities.
371 */
372 if (j->flags.capbset_drop) {
373 warn("overriding bounding set configuration");
374 j->cap_bset = 0;
375 j->flags.capbset_drop = 0;
376 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400377 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800378 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400379}
380
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800381void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
382{
383 if (j->flags.use_caps) {
384 /*
385 * 'minijail_use_caps' will have already configured a capability
386 * bounding set matching the (permitted|inheritable|effective)
387 * sets. Abort if the user tries to configure a separate
388 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
389 * are mutually exclusive.
390 */
391 die("runtime capabilities already configured, can't drop "
392 "bounding set separately");
393 }
394 j->cap_bset = capmask;
395 j->flags.capbset_drop = 1;
396}
397
398void API minijail_reset_signal_mask(struct minijail *j)
399{
Peter Qiu2860c462015-12-16 15:13:06 -0800400 j->flags.reset_signal_mask = 1;
401}
402
Will Drewry6ac91122011-10-21 16:38:58 -0500403void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400404{
405 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400406}
407
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700408void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
409{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800410 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700411 if (ns_fd < 0) {
412 pdie("failed to open namespace '%s'", ns_path);
413 }
414 j->mountns_fd = ns_fd;
415 j->flags.enter_vfs = 1;
416}
417
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800418void API minijail_skip_remount_private(struct minijail *j)
419{
420 j->flags.skip_remount_private = 1;
421}
422
Will Drewry6ac91122011-10-21 16:38:58 -0500423void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400424{
Elly Jonese58176c2012-01-23 11:46:17 -0500425 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700426 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400427 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800428 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400429}
430
Dylan Reidf7942472015-11-18 17:55:26 -0800431void API minijail_namespace_ipc(struct minijail *j)
432{
433 j->flags.ipc = 1;
434}
435
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400436void API minijail_namespace_net(struct minijail *j)
437{
438 j->flags.net = 1;
439}
440
Dylan Reid1102f5a2015-09-15 11:52:20 -0700441void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
442{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800443 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700444 if (ns_fd < 0) {
445 pdie("failed to open namespace '%s'", ns_path);
446 }
447 j->netns_fd = ns_fd;
448 j->flags.enter_net = 1;
449}
450
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700451void API minijail_namespace_cgroups(struct minijail *j)
452{
453 j->flags.ns_cgroups = 1;
454}
455
Dylan Reid791f5772015-09-14 20:02:42 -0700456void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400457{
458 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700459 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400460}
461
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800462void API minijail_namespace_user(struct minijail *j)
463{
464 j->flags.userns = 1;
465}
466
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400467void API minijail_namespace_user_disable_setgroups(struct minijail *j)
468{
469 j->flags.disable_setgroups = 1;
470}
471
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800472int API minijail_uidmap(struct minijail *j, const char *uidmap)
473{
474 j->uidmap = strdup(uidmap);
475 if (!j->uidmap)
476 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800477 char *ch;
478 for (ch = j->uidmap; *ch; ch++) {
479 if (*ch == ',')
480 *ch = '\n';
481 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800482 return 0;
483}
484
485int API minijail_gidmap(struct minijail *j, const char *gidmap)
486{
487 j->gidmap = strdup(gidmap);
488 if (!j->gidmap)
489 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800490 char *ch;
491 for (ch = j->gidmap; *ch; ch++) {
492 if (*ch == ',')
493 *ch = '\n';
494 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800495 return 0;
496}
497
Will Drewry6ac91122011-10-21 16:38:58 -0500498void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400499{
500 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400501}
502
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800503void API minijail_run_as_init(struct minijail *j)
504{
505 /*
506 * Since the jailed program will become 'init' in the new PID namespace,
507 * Minijail does not need to fork an 'init' process.
508 */
509 j->flags.do_init = 0;
510}
511
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700512int API minijail_enter_chroot(struct minijail *j, const char *dir)
513{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400514 if (j->chrootdir)
515 return -EINVAL;
516 j->chrootdir = strdup(dir);
517 if (!j->chrootdir)
518 return -ENOMEM;
519 j->flags.chroot = 1;
520 return 0;
521}
522
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800523int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
524{
525 if (j->chrootdir)
526 return -EINVAL;
527 j->chrootdir = strdup(dir);
528 if (!j->chrootdir)
529 return -ENOMEM;
530 j->flags.pivot_root = 1;
531 return 0;
532}
533
Dylan Reida14e08d2015-10-22 21:05:29 -0700534char API *minijail_get_original_path(struct minijail *j,
535 const char *path_inside_chroot)
536{
Dylan Reid648b2202015-10-23 00:50:00 -0700537 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700538
Dylan Reid648b2202015-10-23 00:50:00 -0700539 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700540 while (b) {
541 /*
542 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700543 * mount, then the original path is exactly the source of
544 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700545 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700546 * mount source = /some/path/exe, mount dest =
547 * /chroot/path/exe Then when getting the original path of
548 * "/chroot/path/exe", the source of that mount,
549 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700550 */
551 if (!strcmp(b->dest, path_inside_chroot))
552 return strdup(b->src);
553
554 /*
555 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700556 * mount, take the suffix of the chroot path relative to the
557 * mount destination path, and append it to the mount source
558 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700559 */
560 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
561 const char *relative_path =
562 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400563 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700564 }
565 b = b->next;
566 }
567
568 /* If there is a chroot path, append |path_inside_chroot| to that. */
569 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400570 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700571
572 /* No chroot, so the path outside is the same as it is inside. */
573 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700574}
575
Lee Campbell11af0622014-05-22 12:36:04 -0700576void API minijail_mount_tmp(struct minijail *j)
577{
578 j->flags.mount_tmp = 1;
579}
580
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800581int API minijail_write_pid_file(struct minijail *j, const char *path)
582{
583 j->pid_file_path = strdup(path);
584 if (!j->pid_file_path)
585 return -ENOMEM;
586 j->flags.pid_file = 1;
587 return 0;
588}
589
Dylan Reid605ce7f2016-01-19 19:21:00 -0800590int API minijail_add_to_cgroup(struct minijail *j, const char *path)
591{
592 if (j->cgroup_count >= MAX_CGROUPS)
593 return -ENOMEM;
594 j->cgroups[j->cgroup_count] = strdup(path);
595 if (!j->cgroups[j->cgroup_count])
596 return -ENOMEM;
597 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800598 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800599 return 0;
600}
601
Dylan Reid81e23972016-05-18 14:06:35 -0700602int API minijail_mount_with_data(struct minijail *j, const char *src,
603 const char *dest, const char *type,
604 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700605{
Dylan Reid648b2202015-10-23 00:50:00 -0700606 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400607
608 if (*dest != '/')
609 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700610 m = calloc(1, sizeof(*m));
611 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400612 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700613 m->dest = strdup(dest);
614 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400615 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700616 m->src = strdup(src);
617 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400618 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700619 m->type = strdup(type);
620 if (!m->type)
621 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700622 if (data) {
623 m->data = strdup(data);
624 if (!m->data)
625 goto error;
626 m->has_data = 1;
627 }
Dylan Reid648b2202015-10-23 00:50:00 -0700628 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400629
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800630 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400631
Elly Jonesdd3e8512012-01-23 15:13:38 -0500632 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700633 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400634 * containing vfs namespace.
635 */
636 minijail_namespace_vfs(j);
637
Dylan Reid648b2202015-10-23 00:50:00 -0700638 if (j->mounts_tail)
639 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400640 else
Dylan Reid648b2202015-10-23 00:50:00 -0700641 j->mounts_head = m;
642 j->mounts_tail = m;
643 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400644
645 return 0;
646
647error:
Dylan Reid81e23972016-05-18 14:06:35 -0700648 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700649 free(m->src);
650 free(m->dest);
651 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400652 return -ENOMEM;
653}
654
Dylan Reid81e23972016-05-18 14:06:35 -0700655int API minijail_mount(struct minijail *j, const char *src, const char *dest,
656 const char *type, unsigned long flags)
657{
658 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
659}
660
Dylan Reid648b2202015-10-23 00:50:00 -0700661int API minijail_bind(struct minijail *j, const char *src, const char *dest,
662 int writeable)
663{
664 unsigned long flags = MS_BIND;
665
666 if (!writeable)
667 flags |= MS_RDONLY;
668
669 return minijail_mount(j, src, dest, "", flags);
670}
671
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400672static void clear_seccomp_options(struct minijail *j)
673{
674 j->flags.seccomp_filter = 0;
675 j->flags.seccomp_filter_tsync = 0;
676 j->flags.log_seccomp_filter = 0;
677 j->filter_len = 0;
678 j->filter_prog = NULL;
679 j->flags.no_new_privs = 0;
680}
681
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400682static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400683{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400684 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400685 /*
686 * |errno| will be set to EINVAL when seccomp has not been
687 * compiled into the kernel. On certain platforms and kernel
688 * versions this is not a fatal failure. In that case, and only
689 * in that case, disable seccomp and skip loading the filters.
690 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400691 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400692 warn("not loading seccomp filters, seccomp filter not "
693 "supported");
694 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400695 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700696 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400697 /*
698 * If |errno| != EINVAL or seccomp_can_softfail() is false,
699 * we can proceed. Worst case scenario minijail_enter() will
700 * abort() if seccomp fails.
701 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700702 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400703 if (j->flags.seccomp_filter_tsync) {
704 /* Are the seccomp(2) syscall and the TSYNC option supported? */
705 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
706 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
707 int saved_errno = errno;
708 if (seccomp_can_softfail()) {
709 if (saved_errno == ENOSYS) {
710 warn(
711 "seccomp(2) syscall not supported");
712 clear_seccomp_options(j);
713 }
714 if (saved_errno == EINVAL) {
715 warn("seccomp filter thread sync not "
716 "supported");
717 clear_seccomp_options(j);
718 }
719 return 0;
720 }
721 /*
722 * Similar logic here. If seccomp_can_softfail() is
723 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
724 * we can proceed. Worst case scenario minijail_enter()
725 * will abort() if seccomp or TSYNC fail.
726 */
727 }
728 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400729 return 1;
730}
731
732static int parse_seccomp_filters(struct minijail *j, FILE *policy_file)
733{
734 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
735 if (compile_filter(policy_file, fprog, j->flags.log_seccomp_filter)) {
736 free(fprog);
737 return -1;
738 }
739
740 j->filter_len = fprog->len;
741 j->filter_prog = fprog;
742 return 0;
743}
744
745void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
746{
747 if (!seccomp_should_parse_filters(j))
748 return;
749
Elly Jonese1749eb2011-10-07 13:54:59 -0400750 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800751 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700752 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400753 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800754
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400755 if (parse_seccomp_filters(j, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700756 die("failed to compile seccomp filter BPF program in '%s'",
757 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800758 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400759 fclose(file);
760}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800761
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400762void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
763{
764 if (!seccomp_should_parse_filters(j))
765 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800766
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400767 FILE *file = fdopen(fd, "r");
768 if (!file) {
769 pdie("failed to associate stream with fd %d", fd);
770 }
771
772 if (parse_seccomp_filters(j, file) != 0) {
773 die("failed to compile seccomp filter BPF program from fd %d",
774 fd);
775 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400776 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500777}
778
Andrew Brestickereac28942015-11-11 16:04:46 -0800779int API minijail_use_alt_syscall(struct minijail *j, const char *table)
780{
781 j->alt_syscall_table = strdup(table);
782 if (!j->alt_syscall_table)
783 return -ENOMEM;
784 j->flags.alt_syscall = 1;
785 return 0;
786}
787
Will Drewryf89aef52011-09-16 16:48:57 -0500788struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400789 size_t available;
790 size_t total;
791 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500792};
793
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800794void marshal_state_init(struct marshal_state *state, char *buf,
795 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400796{
797 state->available = available;
798 state->buf = buf;
799 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500800}
801
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800802void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400803{
804 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500805
Elly Jonese1749eb2011-10-07 13:54:59 -0400806 /* Up to |available| will be written. */
807 if (copy_len) {
808 memcpy(state->buf, src, copy_len);
809 state->buf += copy_len;
810 state->available -= copy_len;
811 }
812 /* |total| will contain the expected length. */
813 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500814}
815
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400816void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700817{
818 marshal_append(state, m->src, strlen(m->src) + 1);
819 marshal_append(state, m->dest, strlen(m->dest) + 1);
820 marshal_append(state, m->type, strlen(m->type) + 1);
821 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
822 if (m->has_data)
823 marshal_append(state, m->data, strlen(m->data) + 1);
824 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
825}
826
Will Drewry6ac91122011-10-21 16:38:58 -0500827void minijail_marshal_helper(struct marshal_state *state,
828 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400829{
Dylan Reid648b2202015-10-23 00:50:00 -0700830 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800831 size_t i;
832
Elly Jonese1749eb2011-10-07 13:54:59 -0400833 marshal_append(state, (char *)j, sizeof(*j));
834 if (j->user)
835 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800836 if (j->suppl_gid_list) {
837 marshal_append(state, j->suppl_gid_list,
838 j->suppl_gid_count * sizeof(gid_t));
839 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400840 if (j->chrootdir)
841 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800842 if (j->alt_syscall_table) {
843 marshal_append(state, j->alt_syscall_table,
844 strlen(j->alt_syscall_table) + 1);
845 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800846 if (j->flags.seccomp_filter && j->filter_prog) {
847 struct sock_fprog *fp = j->filter_prog;
848 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800849 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400850 }
Dylan Reid648b2202015-10-23 00:50:00 -0700851 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400852 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400853 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800854 for (i = 0; i < j->cgroup_count; ++i)
855 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500856}
857
Will Drewry6ac91122011-10-21 16:38:58 -0500858size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400859{
860 struct marshal_state state;
861 marshal_state_init(&state, NULL, 0);
862 minijail_marshal_helper(&state, j);
863 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500864}
865
Elly Jonese1749eb2011-10-07 13:54:59 -0400866int minijail_marshal(const struct minijail *j, char *buf, size_t available)
867{
868 struct marshal_state state;
869 marshal_state_init(&state, buf, available);
870 minijail_marshal_helper(&state, j);
871 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500872}
873
Elly Jonese1749eb2011-10-07 13:54:59 -0400874int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
875{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800876 size_t i;
877 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500878 int ret = -EINVAL;
879
Elly Jonese1749eb2011-10-07 13:54:59 -0400880 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500881 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400882 memcpy((void *)j, serialized, sizeof(*j));
883 serialized += sizeof(*j);
884 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500885
Will Drewrybee7ba72011-10-21 20:47:01 -0500886 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -0400887 j->pid_file_path = NULL;
888 j->uidmap = NULL;
889 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -0700890 j->mounts_head = NULL;
891 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800892 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500893
Elly Jonese1749eb2011-10-07 13:54:59 -0400894 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400895 char *user = consumestr(&serialized, &length);
896 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500897 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400898 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500899 if (!j->user)
900 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400901 }
Will Drewryf89aef52011-09-16 16:48:57 -0500902
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800903 if (j->suppl_gid_list) { /* stale pointer */
904 if (j->suppl_gid_count > NGROUPS_MAX) {
905 goto bad_gid_list;
906 }
907 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
908 void *gid_list_bytes =
909 consumebytes(gid_list_size, &serialized, &length);
910 if (!gid_list_bytes)
911 goto bad_gid_list;
912
913 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
914 if (!j->suppl_gid_list)
915 goto bad_gid_list;
916
917 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
918 }
919
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400920 if (j->chrootdir) { /* stale pointer */
921 char *chrootdir = consumestr(&serialized, &length);
922 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500923 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400924 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500925 if (!j->chrootdir)
926 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400927 }
928
Andrew Brestickereac28942015-11-11 16:04:46 -0800929 if (j->alt_syscall_table) { /* stale pointer */
930 char *alt_syscall_table = consumestr(&serialized, &length);
931 if (!alt_syscall_table)
932 goto bad_syscall_table;
933 j->alt_syscall_table = strdup(alt_syscall_table);
934 if (!j->alt_syscall_table)
935 goto bad_syscall_table;
936 }
937
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800938 if (j->flags.seccomp_filter && j->filter_len > 0) {
939 size_t ninstrs = j->filter_len;
940 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
941 ninstrs > USHRT_MAX)
942 goto bad_filters;
943
944 size_t program_len = ninstrs * sizeof(struct sock_filter);
945 void *program = consumebytes(program_len, &serialized, &length);
946 if (!program)
947 goto bad_filters;
948
949 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800950 if (!j->filter_prog)
951 goto bad_filters;
952
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800953 j->filter_prog->len = ninstrs;
954 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800955 if (!j->filter_prog->filter)
956 goto bad_filter_prog_instrs;
957
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800958 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400959 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400960
Dylan Reid648b2202015-10-23 00:50:00 -0700961 count = j->mounts_count;
962 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400963 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700964 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -0700965 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400966 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700967 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700968 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400969 const char *src = consumestr(&serialized, &length);
970 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700971 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400972 dest = consumestr(&serialized, &length);
973 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700974 goto bad_mounts;
975 type = consumestr(&serialized, &length);
976 if (!type)
977 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -0700978 has_data = consumebytes(sizeof(*has_data), &serialized,
979 &length);
980 if (!has_data)
981 goto bad_mounts;
982 if (*has_data) {
983 data = consumestr(&serialized, &length);
984 if (!data)
985 goto bad_mounts;
986 }
Dylan Reid648b2202015-10-23 00:50:00 -0700987 flags = consumebytes(sizeof(*flags), &serialized, &length);
988 if (!flags)
989 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -0700990 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -0700991 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400992 }
993
Dylan Reid605ce7f2016-01-19 19:21:00 -0800994 count = j->cgroup_count;
995 j->cgroup_count = 0;
996 for (i = 0; i < count; ++i) {
997 char *cgroup = consumestr(&serialized, &length);
998 if (!cgroup)
999 goto bad_cgroups;
1000 j->cgroups[i] = strdup(cgroup);
1001 if (!j->cgroups[i])
1002 goto bad_cgroups;
1003 ++j->cgroup_count;
1004 }
1005
Elly Jonese1749eb2011-10-07 13:54:59 -04001006 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001007
Dylan Reid605ce7f2016-01-19 19:21:00 -08001008bad_cgroups:
1009 while (j->mounts_head) {
1010 struct mountpoint *m = j->mounts_head;
1011 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07001012 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001013 free(m->type);
1014 free(m->dest);
1015 free(m->src);
1016 free(m);
1017 }
1018 for (i = 0; i < j->cgroup_count; ++i)
1019 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001020bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001021 if (j->flags.seccomp_filter && j->filter_len > 0) {
1022 free(j->filter_prog->filter);
1023 free(j->filter_prog);
1024 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001025bad_filter_prog_instrs:
1026 if (j->filter_prog)
1027 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001028bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001029 if (j->alt_syscall_table)
1030 free(j->alt_syscall_table);
1031bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001032 if (j->chrootdir)
1033 free(j->chrootdir);
1034bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001035 if (j->suppl_gid_list)
1036 free(j->suppl_gid_list);
1037bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001038 if (j->user)
1039 free(j->user);
1040clear_pointers:
1041 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001042 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001043 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001044 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001045 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001046out:
1047 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001048}
1049
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001050/*
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001051 * setup_mount_destination: Ensures the mount target exists.
1052 * Creates it if needed and possible.
Dylan Reideec77962016-06-30 19:35:10 -07001053 */
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001054static int setup_mount_destination(const char *source, const char *dest,
1055 uid_t uid, uid_t gid)
Dylan Reideec77962016-06-30 19:35:10 -07001056{
1057 int rc;
1058 struct stat st_buf;
1059
1060 rc = stat(dest, &st_buf);
1061 if (rc == 0) /* destination exists */
1062 return 0;
1063
1064 /*
1065 * Try to create the destination.
1066 * Either make a directory or touch a file depending on the source type.
1067 * If the source doesn't exist, assume it is a filesystem type such as
1068 * "tmpfs" and create a directory to mount it on.
1069 */
1070 rc = stat(source, &st_buf);
1071 if (rc || S_ISDIR(st_buf.st_mode) || S_ISBLK(st_buf.st_mode)) {
1072 if (mkdir(dest, 0700))
1073 return -errno;
1074 } else {
1075 int fd = open(dest, O_RDWR | O_CREAT, 0700);
1076 if (fd < 0)
1077 return -errno;
1078 close(fd);
1079 }
1080 return chown(dest, uid, gid);
1081}
1082
1083/*
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001084 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001085 * @j Minijail these mounts are for
1086 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001087 *
1088 * Returns 0 for success.
1089 */
Dylan Reid648b2202015-10-23 00:50:00 -07001090static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001091{
Dylan Reid648b2202015-10-23 00:50:00 -07001092 int ret;
1093 char *dest;
1094 int remount_ro = 0;
1095
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001096 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001097 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001098 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001099
Dylan Reideec77962016-06-30 19:35:10 -07001100 if (setup_mount_destination(m->src, dest, j->uid, j->gid))
1101 pdie("creating mount target '%s' failed", dest);
1102
Dylan Reid648b2202015-10-23 00:50:00 -07001103 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001104 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1105 * can't both be specified in the original bind mount.
1106 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001107 */
1108 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1109 remount_ro = 1;
1110 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001111 }
Dylan Reid648b2202015-10-23 00:50:00 -07001112
Dylan Reid81e23972016-05-18 14:06:35 -07001113 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001114 if (ret)
1115 pdie("mount: %s -> %s", m->src, dest);
1116
1117 if (remount_ro) {
1118 m->flags |= MS_RDONLY;
1119 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001120 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001121 if (ret)
1122 pdie("bind ro: %s -> %s", m->src, dest);
1123 }
1124
Elly Jones51a5b6c2011-10-12 19:09:26 -04001125 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001126 if (m->next)
1127 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001128 return ret;
1129}
1130
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001131static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001132{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001133 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001134
1135 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001136 return ret;
1137
1138 if (chroot(j->chrootdir))
1139 return -errno;
1140
1141 if (chdir("/"))
1142 return -errno;
1143
1144 return 0;
1145}
1146
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001147static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001148{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001149 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001150
1151 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001152 return ret;
1153
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001154 /*
1155 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001156 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001157 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001158 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001159 if (oldroot < 0)
1160 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001161 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001162 if (newroot < 0)
1163 pdie("failed to open %s for fchdir", j->chrootdir);
1164
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001165 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001166 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001167 * do a self bind mount.
1168 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001169 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1170 pdie("failed to bind mount '%s'", j->chrootdir);
1171 if (chdir(j->chrootdir))
1172 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001173 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001174 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001175
1176 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001177 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001178 * change to the old root and unmount it.
1179 */
1180 if (fchdir(oldroot))
1181 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001182
1183 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001184 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1185 * there could be a shared mount point under |oldroot|. In that case,
1186 * mounts under this shared mount point will be unmounted below, and
1187 * this unmounting will propagate to the original mount namespace
1188 * (because the mount point is shared). To prevent this unexpected
1189 * unmounting, remove these mounts from their peer groups by recursively
1190 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001191 */
1192 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001193 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001194 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001195 if (umount2(".", MNT_DETACH))
1196 pdie("umount(/)");
1197 /* Change back to the new root. */
1198 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001199 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001200 if (close(oldroot))
1201 return -errno;
1202 if (close(newroot))
1203 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001204 if (chroot("/"))
1205 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001206 /* Set correct CWD for getcwd(3). */
1207 if (chdir("/"))
1208 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001209
1210 return 0;
1211}
1212
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001213static int mount_tmp(void)
Lee Campbell11af0622014-05-22 12:36:04 -07001214{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001215 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001216}
1217
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001218static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001219{
1220 const char *kProcPath = "/proc";
1221 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001222 /*
1223 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001224 * /proc in our namespace, which means using MS_REMOUNT here would
1225 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001226 * namespace (!). Instead, remove their mount from our namespace lazily
1227 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001228 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001229 if (umount2(kProcPath, MNT_DETACH)) {
1230 /*
1231 * If we are in a new user namespace, umount(2) will fail.
1232 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1233 */
1234 if (j->flags.userns) {
1235 info("umount(/proc, MNT_DETACH) failed, "
1236 "this is expected when using user namespaces");
1237 } else {
1238 return -errno;
1239 }
1240 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001241 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1242 return -errno;
1243 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001244}
1245
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001246static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001247{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001248 kill(j->initpid, SIGKILL);
1249 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001250}
1251
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001252static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001253{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001254 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001255 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001256}
1257
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001258static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001259{
1260 size_t i;
1261
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001262 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001263 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001264 kill_child_and_die(j, "failed to add to cgroups");
1265 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001266}
1267
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001268static void write_ugid_maps_or_die(const struct minijail *j)
1269{
1270 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1271 kill_child_and_die(j, "failed to write uid_map");
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -04001272 if (j->gidmap && j->flags.disable_setgroups &&
1273 write_proc_file(j->initpid, "deny", "setgroups") != 0)
1274 kill_child_and_die(j, "failed to disable setgroups(2)");
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001275 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1276 kill_child_and_die(j, "failed to write gid_map");
1277}
1278
1279static void enter_user_namespace(const struct minijail *j)
1280{
1281 if (j->uidmap && setresuid(0, 0, 0))
1282 pdie("user_namespaces: setresuid(0, 0, 0) failed");
1283 if (j->gidmap && setresgid(0, 0, 0))
1284 pdie("user_namespaces: setresgid(0, 0, 0) failed");
1285}
1286
1287static void parent_setup_complete(int *pipe_fds)
1288{
1289 close(pipe_fds[0]);
1290 close(pipe_fds[1]);
1291}
1292
1293/*
1294 * wait_for_parent_setup: Called by the child process to wait for any
1295 * further parent-side setup to complete before continuing.
1296 */
1297static void wait_for_parent_setup(int *pipe_fds)
1298{
1299 char buf;
1300
1301 close(pipe_fds[1]);
1302
1303 /* Wait for parent to complete setup and close the pipe. */
1304 if (read(pipe_fds[0], &buf, 1) != 0)
1305 die("failed to sync with parent");
1306 close(pipe_fds[0]);
1307}
1308
1309static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001310{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001311 if (j->flags.usergroups && j->flags.suppl_gids) {
1312 die("tried to inherit *and* set supplementary groups;"
1313 " can only do one");
1314 }
1315
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001316 if (j->flags.usergroups) {
1317 if (initgroups(j->user, j->usergid))
1318 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001319 } else if (j->flags.suppl_gids) {
1320 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1321 pdie("setgroups");
1322 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001323 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001324 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001325 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001326 * users.
1327 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001328 if ((j->uid || j->gid) && setgroups(0, NULL))
1329 pdie("setgroups");
1330 }
1331
1332 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1333 pdie("setresgid");
1334
1335 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1336 pdie("setresuid");
1337}
1338
Mike Frysinger3adfef72013-05-09 17:19:08 -04001339/*
1340 * We specifically do not use cap_valid() as that only tells us the last
1341 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001342 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001343 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001344 * kernel).
1345 * Normally, we suck up the answer via /proc. On Android, not all processes are
1346 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1347 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001348 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001349static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001350{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001351 unsigned int last_valid_cap = 0;
1352 if (is_android()) {
1353 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1354 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001355
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001356 /* |last_valid_cap| will be the first failing value. */
1357 if (last_valid_cap > 0) {
1358 last_valid_cap--;
1359 }
1360 } else {
1361 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1362 FILE *fp = fopen(cap_file, "re");
1363 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1364 pdie("fscanf(%s)", cap_file);
1365 fclose(fp);
1366 }
Dylan Reidf682d472015-09-17 21:39:07 -07001367 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001368}
1369
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001370static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1371{
1372 const uint64_t one = 1;
1373 unsigned int i;
1374 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1375 if (keep_mask & (one << i))
1376 continue;
1377 if (prctl(PR_CAPBSET_DROP, i))
1378 pdie("could not drop capability from bounding set");
1379 }
1380}
1381
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001382static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001383{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001384 if (!j->flags.use_caps)
1385 return;
1386
Elly Jonese1749eb2011-10-07 13:54:59 -04001387 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001388 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001389 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001390 unsigned int i;
1391 if (!caps)
1392 die("can't get process caps");
1393 if (cap_clear_flag(caps, CAP_INHERITABLE))
1394 die("can't clear inheritable caps");
1395 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1396 die("can't clear effective caps");
1397 if (cap_clear_flag(caps, CAP_PERMITTED))
1398 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001399 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001400 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001401 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001402 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001403 flag[0] = i;
1404 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001405 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001406 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001407 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001408 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001409 die("can't add inheritable cap");
1410 }
1411 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001412 die("can't apply initial cleaned capset");
1413
1414 /*
1415 * Instead of dropping bounding set first, do it here in case
1416 * the caller had a more permissive bounding set which could
1417 * have been used above to raise a capability that wasn't already
1418 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1419 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001420 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001421
1422 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001423 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001424 flag[0] = CAP_SETPCAP;
1425 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1426 die("can't clear effective cap");
1427 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1428 die("can't clear permitted cap");
1429 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1430 die("can't clear inheritable cap");
1431 }
1432
1433 if (cap_set_proc(caps))
1434 die("can't apply final cleaned capset");
1435
1436 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001437}
1438
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001439static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001440{
1441 /*
1442 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1443 * in the kernel source tree for an explanation of the parameters.
1444 */
1445 if (j->flags.no_new_privs) {
1446 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1447 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1448 }
1449
1450 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001451 * Code running with ASan
1452 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1453 * will make system calls not included in the syscall filter policy,
1454 * which will likely crash the program. Skip setting seccomp filter in
1455 * that case.
1456 * 'running_with_asan()' has no inputs and is completely defined at
1457 * build time, so this cannot be used by an attacker to skip setting
1458 * seccomp filter.
1459 */
1460 if (j->flags.seccomp_filter && running_with_asan()) {
1461 warn("running with ASan, not setting seccomp filter");
1462 return;
1463 }
1464
1465 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001466 * If we're logging seccomp filter failures,
1467 * install the SIGSYS handler first.
1468 */
1469 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1470 if (install_sigsys_handler())
1471 pdie("install SIGSYS handler");
1472 warn("logging seccomp filter failures");
1473 }
1474
1475 /*
1476 * Install the syscall filter.
1477 */
1478 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001479 if (j->flags.seccomp_filter_tsync) {
1480 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1481 SECCOMP_FILTER_FLAG_TSYNC,
1482 j->filter_prog)) {
1483 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001484 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001485 } else {
1486 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1487 j->filter_prog)) {
1488 pdie("prctl(seccomp_filter) failed");
1489 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001490 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001491 }
1492}
1493
Will Drewry6ac91122011-10-21 16:38:58 -05001494void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001495{
Dylan Reidf682d472015-09-17 21:39:07 -07001496 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001497 * If we're dropping caps, get the last valid cap from /proc now,
1498 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001499 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001500 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001501 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001502 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001503
Elly Jonese1749eb2011-10-07 13:54:59 -04001504 if (j->flags.pids)
1505 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001506 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001507
Elly Jonese1749eb2011-10-07 13:54:59 -04001508 if (j->flags.usergroups && !j->user)
1509 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001510
Elly Jonesdd3e8512012-01-23 15:13:38 -05001511 /*
1512 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001513 * so we don't even try. If any of our operations fail, we abort() the
1514 * entire process.
1515 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001516 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1517 pdie("setns(CLONE_NEWNS)");
1518
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001519 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001520 if (unshare(CLONE_NEWNS))
1521 pdie("unshare(vfs)");
1522 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001523 * Unless asked not to, remount all filesystems as private.
1524 * If they are shared, new bind mounts will creep out of our
1525 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001526 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1527 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001528 if (!j->flags.skip_remount_private) {
1529 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1530 pdie("mount(/, private)");
1531 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001532 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001533
Dylan Reidf7942472015-11-18 17:55:26 -08001534 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1535 pdie("unshare(ipc)");
1536 }
1537
Dylan Reid1102f5a2015-09-15 11:52:20 -07001538 if (j->flags.enter_net) {
1539 if (setns(j->netns_fd, CLONE_NEWNET))
1540 pdie("setns(CLONE_NEWNET)");
1541 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001542 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001543 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001544
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001545 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
1546 pdie("unshare(cgroups)");
1547
Elly Jones51a5b6c2011-10-12 19:09:26 -04001548 if (j->flags.chroot && enter_chroot(j))
1549 pdie("chroot");
1550
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001551 if (j->flags.pivot_root && enter_pivot_root(j))
1552 pdie("pivot_root");
1553
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001554 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001555 pdie("mount_tmp");
1556
Dylan Reid791f5772015-09-14 20:02:42 -07001557 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001558 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001559
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001560 /*
1561 * If we're only dropping capabilities from the bounding set, but not
1562 * from the thread's (permitted|inheritable|effective) sets, do it now.
1563 */
1564 if (j->flags.capbset_drop) {
1565 drop_capbset(j->cap_bset, last_valid_cap);
1566 }
1567
1568 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001569 /*
1570 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001571 * capability to change uids, our attempt to use setuid()
1572 * below will fail. Hang on to root caps across setuid(), then
1573 * lock securebits.
1574 */
1575 if (prctl(PR_SET_KEEPCAPS, 1))
1576 pdie("prctl(PR_SET_KEEPCAPS)");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001577
1578 /*
1579 * Kernels 4.3+ define a new securebit
1580 * (SECURE_NO_CAP_AMBIENT_RAISE), so using the SECURE_ALL_BITS
1581 * and SECURE_ALL_LOCKS masks from newer kernel headers will
1582 * return EPERM on older kernels. Detect this, and retry with
1583 * the right mask for older (2.6.26-4.2) kernels.
1584 */
1585 int securebits_ret = prctl(PR_SET_SECUREBITS,
1586 SECURE_ALL_BITS | SECURE_ALL_LOCKS);
1587 if (securebits_ret < 0) {
1588 if (errno == EPERM) {
1589 /* Possibly running on kernel < 4.3. */
1590 securebits_ret = prctl(
1591 PR_SET_SECUREBITS,
1592 OLD_SECURE_ALL_BITS | OLD_SECURE_ALL_LOCKS);
1593 }
1594 }
1595 if (securebits_ret < 0)
Elly Jonese1749eb2011-10-07 13:54:59 -04001596 pdie("prctl(PR_SET_SECUREBITS)");
1597 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001598
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001599 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001600 /*
1601 * If we're setting no_new_privs, we can drop privileges
1602 * before setting seccomp filter. This way filter policies
1603 * don't need to allow privilege-dropping syscalls.
1604 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001605 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001606 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001607 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001608 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001609 /*
1610 * If we're not setting no_new_privs,
1611 * we need to set seccomp filter *before* dropping privileges.
1612 * WARNING: this means that filter policies *must* allow
1613 * setgroups()/setresgid()/setresuid() for dropping root and
1614 * capget()/capset()/prctl() for dropping caps.
1615 */
1616 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001617 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001618 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001619 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001620
Elly Jonesdd3e8512012-01-23 15:13:38 -05001621 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001622 * Select the specified alternate syscall table. The table must not
1623 * block prctl(2) if we're using seccomp as well.
1624 */
1625 if (j->flags.alt_syscall) {
1626 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1627 pdie("prctl(PR_ALT_SYSCALL)");
1628 }
1629
1630 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001631 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001632 * privilege-dropping syscalls :)
1633 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001634 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001635 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001636 warn("seccomp not supported");
1637 return;
1638 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001639 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001640 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001641}
1642
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001643/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001644static int init_exitstatus = 0;
1645
Will Drewry6ac91122011-10-21 16:38:58 -05001646void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001647{
1648 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001649}
1650
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001651void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001652{
1653 pid_t pid;
1654 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001655 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001656 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001657 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001658 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001659 /*
1660 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001661 * left inside our pid namespace or we get a signal.
1662 */
1663 if (pid == rootpid)
1664 init_exitstatus = status;
1665 }
1666 if (!WIFEXITED(init_exitstatus))
1667 _exit(MINIJAIL_ERR_INIT);
1668 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001669}
1670
Will Drewry6ac91122011-10-21 16:38:58 -05001671int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001672{
1673 size_t sz = 0;
1674 size_t bytes = read(fd, &sz, sizeof(sz));
1675 char *buf;
1676 int r;
1677 if (sizeof(sz) != bytes)
1678 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001679 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001680 return -E2BIG;
1681 buf = malloc(sz);
1682 if (!buf)
1683 return -ENOMEM;
1684 bytes = read(fd, buf, sz);
1685 if (bytes != sz) {
1686 free(buf);
1687 return -EINVAL;
1688 }
1689 r = minijail_unmarshal(j, buf, sz);
1690 free(buf);
1691 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001692}
1693
Will Drewry6ac91122011-10-21 16:38:58 -05001694int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001695{
1696 char *buf;
1697 size_t sz = minijail_size(j);
1698 ssize_t written;
1699 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001700
Elly Jonese1749eb2011-10-07 13:54:59 -04001701 if (!sz)
1702 return -EINVAL;
1703 buf = malloc(sz);
1704 r = minijail_marshal(j, buf, sz);
1705 if (r) {
1706 free(buf);
1707 return r;
1708 }
1709 /* Sends [size][minijail]. */
1710 written = write(fd, &sz, sizeof(sz));
1711 if (written != sizeof(sz)) {
1712 free(buf);
1713 return -EFAULT;
1714 }
1715 written = write(fd, buf, sz);
1716 if (written < 0 || (size_t) written != sz) {
1717 free(buf);
1718 return -EFAULT;
1719 }
1720 free(buf);
1721 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001722}
Elly Jonescd7a9042011-07-22 13:56:51 -04001723
Will Drewry6ac91122011-10-21 16:38:58 -05001724int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001725{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001726#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001727 /* Don't use LDPRELOAD on Brillo. */
1728 return 0;
1729#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001730 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1731 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1732 if (!newenv)
1733 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001734
Elly Jonese1749eb2011-10-07 13:54:59 -04001735 /* Only insert a separating space if we have something to separate... */
1736 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1737 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001738
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001739 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001740 setenv(kLdPreloadEnvVar, newenv, 1);
1741 free(newenv);
1742 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001743#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001744}
1745
Will Drewry6ac91122011-10-21 16:38:58 -05001746int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001747{
1748 int r = pipe(fds);
1749 char fd_buf[11];
1750 if (r)
1751 return r;
1752 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1753 if (r <= 0)
1754 return -EINVAL;
1755 setenv(kFdEnvVar, fd_buf, 1);
1756 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001757}
1758
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001759int setup_pipe_end(int fds[2], size_t index)
1760{
1761 if (index > 1)
1762 return -1;
1763
1764 close(fds[1 - index]);
1765 return fds[index];
1766}
1767
1768int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1769{
1770 if (index > 1)
1771 return -1;
1772
1773 close(fds[1 - index]);
1774 /* dup2(2) the corresponding end of the pipe into |fd|. */
1775 return dup2(fds[index], fd);
1776}
1777
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001778int minijail_run_internal(struct minijail *j, const char *filename,
1779 char *const argv[], pid_t *pchild_pid,
1780 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1781 int use_preload);
1782
Will Drewry6ac91122011-10-21 16:38:58 -05001783int API minijail_run(struct minijail *j, const char *filename,
1784 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001785{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001786 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1787 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001788}
1789
1790int API minijail_run_pid(struct minijail *j, const char *filename,
1791 char *const argv[], pid_t *pchild_pid)
1792{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001793 return minijail_run_internal(j, filename, argv, pchild_pid,
1794 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001795}
1796
1797int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001798 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001799{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001800 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1801 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001802}
1803
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001804int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001805 char *const argv[], pid_t *pchild_pid,
1806 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001807{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001808 return minijail_run_internal(j, filename, argv, pchild_pid,
1809 pstdin_fd, pstdout_fd, pstderr_fd, true);
1810}
1811
1812int API minijail_run_no_preload(struct minijail *j, const char *filename,
1813 char *const argv[])
1814{
1815 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1816 false);
1817}
1818
Samuel Tan63187f42015-10-16 13:01:53 -07001819int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001820 const char *filename,
1821 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001822 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001823 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001824 int *pstderr_fd)
1825{
Samuel Tan63187f42015-10-16 13:01:53 -07001826 return minijail_run_internal(j, filename, argv, pchild_pid,
1827 pstdin_fd, pstdout_fd, pstderr_fd, false);
1828}
1829
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001830int minijail_run_internal(struct minijail *j, const char *filename,
1831 char *const argv[], pid_t *pchild_pid,
1832 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1833 int use_preload)
1834{
Elly Jonese1749eb2011-10-07 13:54:59 -04001835 char *oldenv, *oldenv_copy = NULL;
1836 pid_t child_pid;
1837 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001838 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001839 int stdout_fds[2];
1840 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001841 int child_sync_pipe_fds[2];
1842 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001843 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001844 /* We need to remember this across the minijail_preexec() call. */
1845 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001846 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001847
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001848 if (use_preload) {
1849 oldenv = getenv(kLdPreloadEnvVar);
1850 if (oldenv) {
1851 oldenv_copy = strdup(oldenv);
1852 if (!oldenv_copy)
1853 return -ENOMEM;
1854 }
1855
1856 if (setup_preload())
1857 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001858 }
Will Drewryf89aef52011-09-16 16:48:57 -05001859
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001860 if (!use_preload) {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04001861 if (j->flags.use_caps && j->caps != 0)
1862 die("non-empty capabilities are not supported without LD_PRELOAD");
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001863 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001864
Elly Jonesdd3e8512012-01-23 15:13:38 -05001865 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001866 * Make the process group ID of this process equal to its PID, so that
1867 * both the Minijail process and the jailed process can be killed
1868 * together.
1869 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1870 * the process is already a process group leader.
1871 */
1872 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1873 if (errno != EPERM) {
1874 pdie("setpgid(0, 0)");
1875 }
1876 }
1877
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001878 if (use_preload) {
1879 /*
1880 * Before we fork(2) and execve(2) the child process, we need
1881 * to open a pipe(2) to send the minijail configuration over.
1882 */
1883 if (setup_pipe(pipe_fds))
1884 return -EFAULT;
1885 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001886
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001887 /*
1888 * If we want to write to the child process' standard input,
1889 * create the pipe(2) now.
1890 */
1891 if (pstdin_fd) {
1892 if (pipe(stdin_fds))
1893 return -EFAULT;
1894 }
1895
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001896 /*
1897 * If we want to read from the child process' standard output,
1898 * create the pipe(2) now.
1899 */
1900 if (pstdout_fd) {
1901 if (pipe(stdout_fds))
1902 return -EFAULT;
1903 }
1904
1905 /*
1906 * If we want to read from the child process' standard error,
1907 * create the pipe(2) now.
1908 */
1909 if (pstderr_fd) {
1910 if (pipe(stderr_fds))
1911 return -EFAULT;
1912 }
1913
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001914 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04001915 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001916 * or if we need to add the child process to cgroups, create the pipe(2)
1917 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001918 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001919 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001920 sync_child = 1;
1921 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001922 return -EFAULT;
1923 }
1924
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001925 /*
1926 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001927 *
1928 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1929 *
1930 * In multithreaded programs, there are a bunch of locks inside libc,
1931 * some of which may be held by other threads at the time that we call
1932 * minijail_run_pid(). If we call fork(), glibc does its level best to
1933 * ensure that we hold all of these locks before it calls clone()
1934 * internally and drop them after clone() returns, but when we call
1935 * sys_clone(2) directly, all that gets bypassed and we end up with a
1936 * child address space where some of libc's important locks are held by
1937 * other threads (which did not get cloned, and hence will never release
1938 * those locks). This is okay so long as we call exec() immediately
1939 * after, but a bunch of seemingly-innocent libc functions like setenv()
1940 * take locks.
1941 *
1942 * Hence, only call sys_clone() if we need to, in order to get at pid
1943 * namespacing. If we follow this path, the child's address space might
1944 * have broken locks; you may only call functions that do not acquire
1945 * any locks.
1946 *
1947 * Unfortunately, fork() acquires every lock it can get its hands on, as
1948 * previously detailed, so this function is highly likely to deadlock
1949 * later on (see "deadlock here") if we're multithreaded.
1950 *
1951 * We might hack around this by having the clone()d child (init of the
1952 * pid namespace) return directly, rather than leaving the clone()d
1953 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001954 * its fork()ed child return in turn), but that process would be
1955 * crippled with its libc locks potentially broken. We might try
1956 * fork()ing in the parent before we clone() to ensure that we own all
1957 * the locks, but then we have to have the forked child hanging around
1958 * consuming resources (and possibly having file descriptors / shared
1959 * memory regions / etc attached). We'd need to keep the child around to
1960 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04001961 *
1962 * TODO(ellyjones): figure out if the "forked child hanging around"
1963 * problem is fixable or not. It would be nice if we worked in this
1964 * case.
1965 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001966 if (pid_namespace) {
1967 int clone_flags = CLONE_NEWPID | SIGCHLD;
1968 if (j->flags.userns)
1969 clone_flags |= CLONE_NEWUSER;
1970 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001971 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001972 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001973 }
Elly Jones761b7412012-06-13 15:49:52 -04001974
Elly Jonese1749eb2011-10-07 13:54:59 -04001975 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001976 if (use_preload) {
1977 free(oldenv_copy);
1978 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001979 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001980 }
Will Drewryf89aef52011-09-16 16:48:57 -05001981
Elly Jonese1749eb2011-10-07 13:54:59 -04001982 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001983 if (use_preload) {
1984 /* Restore parent's LD_PRELOAD. */
1985 if (oldenv_copy) {
1986 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1987 free(oldenv_copy);
1988 } else {
1989 unsetenv(kLdPreloadEnvVar);
1990 }
1991 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001992 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001993
Elly Jonese1749eb2011-10-07 13:54:59 -04001994 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001995
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001996 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001997 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001998
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001999 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002000 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002001
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002002 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002003 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002004
2005 if (sync_child)
2006 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002007
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002008 if (use_preload) {
2009 /* Send marshalled minijail. */
2010 close(pipe_fds[0]); /* read endpoint */
2011 ret = minijail_to_fd(j, pipe_fds[1]);
2012 close(pipe_fds[1]); /* write endpoint */
2013 if (ret) {
2014 kill(j->initpid, SIGKILL);
2015 die("failed to send marshalled minijail");
2016 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002017 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002018
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002019 if (pchild_pid)
2020 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002021
2022 /*
2023 * If we want to write to the child process' standard input,
2024 * set up the write end of the pipe.
2025 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002026 if (pstdin_fd)
2027 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002028 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002029
2030 /*
2031 * If we want to read from the child process' standard output,
2032 * set up the read end of the pipe.
2033 */
2034 if (pstdout_fd)
2035 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002036 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002037
2038 /*
2039 * If we want to read from the child process' standard error,
2040 * set up the read end of the pipe.
2041 */
2042 if (pstderr_fd)
2043 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002044 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002045
Elly Jonese1749eb2011-10-07 13:54:59 -04002046 return 0;
2047 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002048 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002049 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002050
Peter Qiu2860c462015-12-16 15:13:06 -08002051 if (j->flags.reset_signal_mask) {
2052 sigset_t signal_mask;
2053 if (sigemptyset(&signal_mask) != 0)
2054 pdie("sigemptyset failed");
2055 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2056 pdie("sigprocmask failed");
2057 }
2058
Dylan Reidce5b55e2016-01-13 11:04:16 -08002059 if (sync_child)
2060 wait_for_parent_setup(child_sync_pipe_fds);
2061
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002062 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002063 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002064
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002065 /*
2066 * If we want to write to the jailed process' standard input,
2067 * set up the read end of the pipe.
2068 */
2069 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002070 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2071 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002072 die("failed to set up stdin pipe");
2073 }
2074
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002075 /*
2076 * If we want to read from the jailed process' standard output,
2077 * set up the write end of the pipe.
2078 */
2079 if (pstdout_fd) {
2080 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2081 STDOUT_FILENO) < 0)
2082 die("failed to set up stdout pipe");
2083 }
2084
2085 /*
2086 * If we want to read from the jailed process' standard error,
2087 * set up the write end of the pipe.
2088 */
2089 if (pstderr_fd) {
2090 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2091 STDERR_FILENO) < 0)
2092 die("failed to set up stderr pipe");
2093 }
2094
Dylan Reid791f5772015-09-14 20:02:42 -07002095 /* If running an init program, let it decide when/how to mount /proc. */
2096 if (pid_namespace && !do_init)
2097 j->flags.remount_proc_ro = 0;
2098
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002099 if (use_preload) {
2100 /* Strip out flags that cannot be inherited across execve(2). */
2101 minijail_preexec(j);
2102 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002103 /*
2104 * If not using LD_PRELOAD, do all jailing before execve(2).
2105 * Note that PID namespaces can only be entered on fork(2),
2106 * so that flag is still cleared.
2107 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002108 j->flags.pids = 0;
2109 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002110 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002111 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002112
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002113 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002114 /*
2115 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002116 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002117 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002118 * a child to actually run the program. If |do_init == 0|, we
2119 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002120 *
2121 * If we're multithreaded, we'll probably deadlock here. See
2122 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002123 */
2124 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002125 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002126 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002127 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002128 /*
2129 * Best effort. Don't bother checking the return value.
2130 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002131 prctl(PR_SET_NAME, "minijail-init");
2132 init(child_pid); /* Never returns. */
2133 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002134 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002135
Elly Jonesdd3e8512012-01-23 15:13:38 -05002136 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002137 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002138 * calling process
2139 * -> execve()-ing process
2140 * If we are:
2141 * calling process
2142 * -> init()-ing process
2143 * -> execve()-ing process
2144 */
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002145 ret = execve(filename, argv, environ);
2146 if (ret == -1) {
2147 pwarn("execve(%s) failed", filename);
2148 }
2149 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002150}
2151
Will Drewry6ac91122011-10-21 16:38:58 -05002152int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002153{
2154 int st;
2155 if (kill(j->initpid, SIGTERM))
2156 return -errno;
2157 if (waitpid(j->initpid, &st, 0) < 0)
2158 return -errno;
2159 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002160}
2161
Will Drewry6ac91122011-10-21 16:38:58 -05002162int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002163{
2164 int st;
2165 if (waitpid(j->initpid, &st, 0) < 0)
2166 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002167
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002168 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002169 int error_status = st;
2170 if (WIFSIGNALED(st)) {
2171 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002172 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002173 j->initpid, signum);
2174 /*
2175 * We return MINIJAIL_ERR_JAIL if the process received
2176 * SIGSYS, which happens when a syscall is blocked by
2177 * seccomp filters.
2178 * If not, we do what bash(1) does:
2179 * $? = 128 + signum
2180 */
2181 if (signum == SIGSYS) {
2182 error_status = MINIJAIL_ERR_JAIL;
2183 } else {
2184 error_status = 128 + signum;
2185 }
2186 }
2187 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002188 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002189
2190 int exit_status = WEXITSTATUS(st);
2191 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002192 info("child process %d exited with status %d",
2193 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002194
2195 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002196}
2197
Will Drewry6ac91122011-10-21 16:38:58 -05002198void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002199{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002200 size_t i;
2201
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002202 if (j->flags.seccomp_filter && j->filter_prog) {
2203 free(j->filter_prog->filter);
2204 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002205 }
Dylan Reid648b2202015-10-23 00:50:00 -07002206 while (j->mounts_head) {
2207 struct mountpoint *m = j->mounts_head;
2208 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002209 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002210 free(m->type);
2211 free(m->dest);
2212 free(m->src);
2213 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002214 }
Dylan Reid648b2202015-10-23 00:50:00 -07002215 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002216 if (j->user)
2217 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002218 if (j->suppl_gid_list)
2219 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002220 if (j->chrootdir)
2221 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002222 if (j->pid_file_path)
2223 free(j->pid_file_path);
2224 if (j->uidmap)
2225 free(j->uidmap);
2226 if (j->gidmap)
2227 free(j->gidmap);
Andrew Brestickereac28942015-11-11 16:04:46 -08002228 if (j->alt_syscall_table)
2229 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002230 for (i = 0; i < j->cgroup_count; ++i)
2231 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002232 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002233}