blob: 1d90aefebd8db1a6687a6446ef5af1ff283f282d [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
7#define _GNU_SOURCE
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07008
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08009#include <asm/unistd.h>
Will Drewry32ac9f52011-08-18 21:36:27 -050010#include <ctype.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040011#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070012#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040013#include <grp.h>
14#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050015#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040016#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040017#include <pwd.h>
18#include <sched.h>
19#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050020#include <stdarg.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070021#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080022#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <syscall.h>
27#include <sys/capability.h>
28#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050029#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040030#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070031#include <sys/stat.h>
32#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080033#include <sys/user.h>
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -080034#include <sys/utsname.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040035#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040036#include <unistd.h>
37
38#include "libminijail.h"
39#include "libminijail-private.h"
40
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070041#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080042#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070043#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080044
Lei Zhangeee31552012-10-17 21:27:10 -070045#ifdef HAVE_SECUREBITS_H
46#include <linux/securebits.h>
47#else
48#define SECURE_ALL_BITS 0x15
49#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
50#endif
51
Will Drewry32ac9f52011-08-18 21:36:27 -050052/* Until these are reliably available in linux/prctl.h */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080053#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070054# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080055#endif
56
Andrew Brestickereac28942015-11-11 16:04:46 -080057#ifndef PR_ALT_SYSCALL
58# define PR_ALT_SYSCALL 0x43724f53
59#endif
60
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080061/* For seccomp_filter using BPF. */
62#ifndef PR_SET_NO_NEW_PRIVS
63# define PR_SET_NO_NEW_PRIVS 38
64#endif
65#ifndef SECCOMP_MODE_FILTER
66# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050067#endif
68
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070069#ifdef USE_SECCOMP_SOFTFAIL
70# define SECCOMP_SOFTFAIL 1
71#else
72# define SECCOMP_SOFTFAIL 0
73#endif
74
Dylan Reid605ce7f2016-01-19 19:21:00 -080075#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
76
Dylan Reid648b2202015-10-23 00:50:00 -070077struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040078 char *src;
79 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070080 char *type;
81 unsigned long flags;
82 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040083};
84
Will Drewryf89aef52011-09-16 16:48:57 -050085struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070086 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070087 * WARNING: if you add a flag here you need to make sure it's
88 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070089 */
Elly Jonese1749eb2011-10-07 13:54:59 -040090 struct {
91 int uid:1;
92 int gid:1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -080093 int usergroups:1;
94 int suppl_gids:1;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -080095 int use_caps:1;
96 int capbset_drop:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040097 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070098 int enter_vfs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040099 int pids:1;
Dylan Reidf7942472015-11-18 17:55:26 -0800100 int ipc:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400101 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700102 int enter_net:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800103 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400104 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -0700105 int remount_proc_ro:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700106 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400107 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700108 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400109 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800110 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700111 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800112 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800113 int pid_file:1;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800114 int cgroups:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800115 int alt_syscall:1;
Peter Qiu2860c462015-12-16 15:13:06 -0800116 int reset_signal_mask:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400117 } flags;
118 uid_t uid;
119 gid_t gid;
120 gid_t usergid;
121 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800122 size_t suppl_gid_count;
123 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400124 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800125 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400126 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700127 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700128 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400129 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800130 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800131 char *uidmap;
132 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800133 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800134 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800135 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700136 struct mountpoint *mounts_head;
137 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800138 size_t mounts_count;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800139 char *cgroups[MAX_CGROUPS];
140 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500141};
142
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700143/*
144 * Strip out flags meant for the parent.
145 * We keep things that are not inherited across execve(2) (e.g. capabilities),
146 * or are easier to set after execve(2) (e.g. seccomp filters).
147 */
148void minijail_preenter(struct minijail *j)
149{
150 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700151 j->flags.enter_vfs = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700152 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700153 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800154 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800155 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800156 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700157}
158
159/*
160 * Strip out flags meant for the child.
161 * We keep things that are inherited across execve(2).
162 */
163void minijail_preexec(struct minijail *j)
164{
165 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700166 int enter_vfs = j->flags.enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700167 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800168 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700169 if (j->user)
170 free(j->user);
171 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800172 if (j->suppl_gid_list)
173 free(j->suppl_gid_list);
174 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700175 memset(&j->flags, 0, sizeof(j->flags));
176 /* Now restore anything we meant to keep. */
177 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700178 j->flags.enter_vfs = enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700179 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800180 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700181 /* Note, |pids| will already have been used before this call. */
182}
183
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800184/* Returns true if the kernel version is less than 3.8. */
185int seccomp_kernel_support_not_required()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800186{
187 int major, minor;
188 struct utsname uts;
189 return (uname(&uts) != -1 &&
190 sscanf(uts.release, "%d.%d", &major, &minor) == 2 &&
191 ((major < 3) || ((major == 3) && (minor < 8))));
192}
193
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800194/* Allow seccomp soft-fail on Android devices with kernel version < 3.8. */
195int can_softfail()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800196{
197#if SECCOMP_SOFTFAIL
198 if (is_android()) {
199 if (seccomp_kernel_support_not_required())
200 return 1;
201 else
202 return 0;
203 } else {
204 return 1;
205 }
206#endif
207 return 0;
208}
209
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700210/* Minijail API. */
211
Will Drewry6ac91122011-10-21 16:38:58 -0500212struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400213{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400214 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400215}
216
Will Drewry6ac91122011-10-21 16:38:58 -0500217void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400218{
219 if (uid == 0)
220 die("useless change to uid 0");
221 j->uid = uid;
222 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400223}
224
Will Drewry6ac91122011-10-21 16:38:58 -0500225void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400226{
227 if (gid == 0)
228 die("useless change to gid 0");
229 j->gid = gid;
230 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400231}
232
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800233void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
234 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800235{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800236 size_t i;
237
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800238 if (j->flags.usergroups)
239 die("cannot inherit *and* set supplementary groups");
240
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800241 if (size == 0) {
242 /* Clear supplementary groups. */
243 j->suppl_gid_list = NULL;
244 j->suppl_gid_count = 0;
245 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800246 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800247 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800248
249 /* Copy the gid_t array. */
250 j->suppl_gid_list = calloc(size, sizeof(gid_t));
251 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800252 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800253 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800254 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800255 j->suppl_gid_list[i] = list[i];
256 }
257 j->suppl_gid_count = size;
258 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800259}
260
Will Drewry6ac91122011-10-21 16:38:58 -0500261int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400262{
263 char *buf = NULL;
264 struct passwd pw;
265 struct passwd *ppw = NULL;
266 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
267 if (sz == -1)
268 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400269
Elly Jonesdd3e8512012-01-23 15:13:38 -0500270 /*
271 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400272 * the maximum needed size of the buffer, so we don't have to search.
273 */
274 buf = malloc(sz);
275 if (!buf)
276 return -ENOMEM;
277 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500278 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800279 * We're safe to free the buffer here. The strings inside |pw| point
280 * inside |buf|, but we don't use any of them; this leaves the pointers
281 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3) succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500282 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400283 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700284 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400285 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700286 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400287 minijail_change_uid(j, ppw->pw_uid);
288 j->user = strdup(user);
289 if (!j->user)
290 return -ENOMEM;
291 j->usergid = ppw->pw_gid;
292 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400293}
294
Will Drewry6ac91122011-10-21 16:38:58 -0500295int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400296{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700297 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700298 struct group gr;
299 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400300 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
301 if (sz == -1)
302 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400303
Elly Jonesdd3e8512012-01-23 15:13:38 -0500304 /*
305 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400306 * the maximum needed size of the buffer, so we don't have to search.
307 */
308 buf = malloc(sz);
309 if (!buf)
310 return -ENOMEM;
311 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500312 /*
313 * We're safe to free the buffer here. The strings inside gr point
314 * inside buf, but we don't use any of them; this leaves the pointers
315 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
316 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400317 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700318 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400319 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700320 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400321 minijail_change_gid(j, pgr->gr_gid);
322 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400323}
324
Will Drewry6ac91122011-10-21 16:38:58 -0500325void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400326{
327 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400328}
329
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700330void API minijail_no_new_privs(struct minijail *j)
331{
332 j->flags.no_new_privs = 1;
333}
334
Will Drewry6ac91122011-10-21 16:38:58 -0500335void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400336{
337 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500338}
339
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700340void API minijail_log_seccomp_filter_failures(struct minijail *j)
341{
342 j->flags.log_seccomp_filter = 1;
343}
344
Will Drewry6ac91122011-10-21 16:38:58 -0500345void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400346{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800347 /*
348 * 'minijail_use_caps' configures a runtime-capabilities-only
349 * environment, including a bounding set matching the thread's runtime
350 * (permitted|inheritable|effective) sets.
351 * Therefore, it will override any existing bounding set configurations
352 * since the latter would allow gaining extra runtime capabilities from
353 * file capabilities.
354 */
355 if (j->flags.capbset_drop) {
356 warn("overriding bounding set configuration");
357 j->cap_bset = 0;
358 j->flags.capbset_drop = 0;
359 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400360 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800361 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400362}
363
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800364void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
365{
366 if (j->flags.use_caps) {
367 /*
368 * 'minijail_use_caps' will have already configured a capability
369 * bounding set matching the (permitted|inheritable|effective)
370 * sets. Abort if the user tries to configure a separate
371 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
372 * are mutually exclusive.
373 */
374 die("runtime capabilities already configured, can't drop "
375 "bounding set separately");
376 }
377 j->cap_bset = capmask;
378 j->flags.capbset_drop = 1;
379}
380
381void API minijail_reset_signal_mask(struct minijail *j)
382{
Peter Qiu2860c462015-12-16 15:13:06 -0800383 j->flags.reset_signal_mask = 1;
384}
385
Will Drewry6ac91122011-10-21 16:38:58 -0500386void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400387{
388 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400389}
390
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700391void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
392{
393 int ns_fd = open(ns_path, O_RDONLY);
394 if (ns_fd < 0) {
395 pdie("failed to open namespace '%s'", ns_path);
396 }
397 j->mountns_fd = ns_fd;
398 j->flags.enter_vfs = 1;
399}
400
Will Drewry6ac91122011-10-21 16:38:58 -0500401void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400402{
Elly Jonese58176c2012-01-23 11:46:17 -0500403 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700404 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400405 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800406 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400407}
408
Dylan Reidf7942472015-11-18 17:55:26 -0800409void API minijail_namespace_ipc(struct minijail *j)
410{
411 j->flags.ipc = 1;
412}
413
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400414void API minijail_namespace_net(struct minijail *j)
415{
416 j->flags.net = 1;
417}
418
Dylan Reid1102f5a2015-09-15 11:52:20 -0700419void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
420{
421 int ns_fd = open(ns_path, O_RDONLY);
422 if (ns_fd < 0) {
423 pdie("failed to open namespace '%s'", ns_path);
424 }
425 j->netns_fd = ns_fd;
426 j->flags.enter_net = 1;
427}
428
Dylan Reid791f5772015-09-14 20:02:42 -0700429void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400430{
431 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700432 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400433}
434
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800435void API minijail_namespace_user(struct minijail *j)
436{
437 j->flags.userns = 1;
438}
439
440int API minijail_uidmap(struct minijail *j, const char *uidmap)
441{
442 j->uidmap = strdup(uidmap);
443 if (!j->uidmap)
444 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800445 char *ch;
446 for (ch = j->uidmap; *ch; ch++) {
447 if (*ch == ',')
448 *ch = '\n';
449 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800450 return 0;
451}
452
453int API minijail_gidmap(struct minijail *j, const char *gidmap)
454{
455 j->gidmap = strdup(gidmap);
456 if (!j->gidmap)
457 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800458 char *ch;
459 for (ch = j->gidmap; *ch; ch++) {
460 if (*ch == ',')
461 *ch = '\n';
462 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800463 return 0;
464}
465
Will Drewry6ac91122011-10-21 16:38:58 -0500466void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400467{
468 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400469}
470
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800471void API minijail_run_as_init(struct minijail *j)
472{
473 /*
474 * Since the jailed program will become 'init' in the new PID namespace,
475 * Minijail does not need to fork an 'init' process.
476 */
477 j->flags.do_init = 0;
478}
479
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700480int API minijail_enter_chroot(struct minijail *j, const char *dir)
481{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400482 if (j->chrootdir)
483 return -EINVAL;
484 j->chrootdir = strdup(dir);
485 if (!j->chrootdir)
486 return -ENOMEM;
487 j->flags.chroot = 1;
488 return 0;
489}
490
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800491int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
492{
493 if (j->chrootdir)
494 return -EINVAL;
495 j->chrootdir = strdup(dir);
496 if (!j->chrootdir)
497 return -ENOMEM;
498 j->flags.pivot_root = 1;
499 return 0;
500}
501
Dylan Reida14e08d2015-10-22 21:05:29 -0700502static char *append_external_path(const char *external_path,
503 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700504{
Dylan Reida14e08d2015-10-22 21:05:29 -0700505 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700506 size_t pathlen;
507
Dylan Reid08946cc2015-09-16 19:10:57 -0700508 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700509 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
510 path = malloc(pathlen);
511 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700512
Dylan Reida14e08d2015-10-22 21:05:29 -0700513 return path;
514}
515
516char API *minijail_get_original_path(struct minijail *j,
517 const char *path_inside_chroot)
518{
Dylan Reid648b2202015-10-23 00:50:00 -0700519 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700520
Dylan Reid648b2202015-10-23 00:50:00 -0700521 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700522 while (b) {
523 /*
524 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700525 * mount, then the original path is exactly the source of
526 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700527 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700528 * mount source = /some/path/exe, mount dest =
529 * /chroot/path/exe Then when getting the original path of
530 * "/chroot/path/exe", the source of that mount,
531 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700532 */
533 if (!strcmp(b->dest, path_inside_chroot))
534 return strdup(b->src);
535
536 /*
537 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700538 * mount, take the suffix of the chroot path relative to the
539 * mount destination path, and append it to the mount source
540 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700541 */
542 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
543 const char *relative_path =
544 path_inside_chroot + strlen(b->dest);
545 return append_external_path(b->src, relative_path);
546 }
547 b = b->next;
548 }
549
550 /* If there is a chroot path, append |path_inside_chroot| to that. */
551 if (j->chrootdir)
552 return append_external_path(j->chrootdir, path_inside_chroot);
553
554 /* No chroot, so the path outside is the same as it is inside. */
555 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700556}
557
Lee Campbell11af0622014-05-22 12:36:04 -0700558void API minijail_mount_tmp(struct minijail *j)
559{
560 j->flags.mount_tmp = 1;
561}
562
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800563int API minijail_write_pid_file(struct minijail *j, const char *path)
564{
565 j->pid_file_path = strdup(path);
566 if (!j->pid_file_path)
567 return -ENOMEM;
568 j->flags.pid_file = 1;
569 return 0;
570}
571
Dylan Reid605ce7f2016-01-19 19:21:00 -0800572int API minijail_add_to_cgroup(struct minijail *j, const char *path)
573{
574 if (j->cgroup_count >= MAX_CGROUPS)
575 return -ENOMEM;
576 j->cgroups[j->cgroup_count] = strdup(path);
577 if (!j->cgroups[j->cgroup_count])
578 return -ENOMEM;
579 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800580 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800581 return 0;
582}
583
Dylan Reid648b2202015-10-23 00:50:00 -0700584int API minijail_mount(struct minijail *j, const char *src, const char *dest,
585 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700586{
Dylan Reid648b2202015-10-23 00:50:00 -0700587 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400588
589 if (*dest != '/')
590 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700591 m = calloc(1, sizeof(*m));
592 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400593 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700594 m->dest = strdup(dest);
595 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400596 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700597 m->src = strdup(src);
598 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400599 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700600 m->type = strdup(type);
601 if (!m->type)
602 goto error;
603 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400604
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800605 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400606
Elly Jonesdd3e8512012-01-23 15:13:38 -0500607 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700608 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400609 * containing vfs namespace.
610 */
611 minijail_namespace_vfs(j);
612
Dylan Reid648b2202015-10-23 00:50:00 -0700613 if (j->mounts_tail)
614 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400615 else
Dylan Reid648b2202015-10-23 00:50:00 -0700616 j->mounts_head = m;
617 j->mounts_tail = m;
618 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400619
620 return 0;
621
622error:
Dylan Reid648b2202015-10-23 00:50:00 -0700623 free(m->src);
624 free(m->dest);
625 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400626 return -ENOMEM;
627}
628
Dylan Reid648b2202015-10-23 00:50:00 -0700629int API minijail_bind(struct minijail *j, const char *src, const char *dest,
630 int writeable)
631{
632 unsigned long flags = MS_BIND;
633
634 if (!writeable)
635 flags |= MS_RDONLY;
636
637 return minijail_mount(j, src, dest, "", flags);
638}
639
Will Drewry6ac91122011-10-21 16:38:58 -0500640void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400641{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700642 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800643 if ((errno == EINVAL) && can_softfail()) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800644 warn("not loading seccomp filter,"
645 " seccomp not supported");
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800646 j->flags.seccomp_filter = 0;
647 j->flags.log_seccomp_filter = 0;
648 j->filter_len = 0;
649 j->filter_prog = NULL;
650 j->flags.no_new_privs = 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700651 }
652 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400653 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800654 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700655 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400656 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800657
658 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700659 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
660 die("failed to compile seccomp filter BPF program in '%s'",
661 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800662 }
663
664 j->filter_len = fprog->len;
665 j->filter_prog = fprog;
666
Elly Jonese1749eb2011-10-07 13:54:59 -0400667 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500668}
669
Andrew Brestickereac28942015-11-11 16:04:46 -0800670int API minijail_use_alt_syscall(struct minijail *j, const char *table)
671{
672 j->alt_syscall_table = strdup(table);
673 if (!j->alt_syscall_table)
674 return -ENOMEM;
675 j->flags.alt_syscall = 1;
676 return 0;
677}
678
Will Drewryf89aef52011-09-16 16:48:57 -0500679struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400680 size_t available;
681 size_t total;
682 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500683};
684
Will Drewry6ac91122011-10-21 16:38:58 -0500685void marshal_state_init(struct marshal_state *state,
686 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400687{
688 state->available = available;
689 state->buf = buf;
690 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500691}
692
Will Drewry6ac91122011-10-21 16:38:58 -0500693void marshal_append(struct marshal_state *state,
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800694 void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400695{
696 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500697
Elly Jonese1749eb2011-10-07 13:54:59 -0400698 /* Up to |available| will be written. */
699 if (copy_len) {
700 memcpy(state->buf, src, copy_len);
701 state->buf += copy_len;
702 state->available -= copy_len;
703 }
704 /* |total| will contain the expected length. */
705 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500706}
707
Will Drewry6ac91122011-10-21 16:38:58 -0500708void minijail_marshal_helper(struct marshal_state *state,
709 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400710{
Dylan Reid648b2202015-10-23 00:50:00 -0700711 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800712 size_t i;
713
Elly Jonese1749eb2011-10-07 13:54:59 -0400714 marshal_append(state, (char *)j, sizeof(*j));
715 if (j->user)
716 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800717 if (j->suppl_gid_list) {
718 marshal_append(state, j->suppl_gid_list,
719 j->suppl_gid_count * sizeof(gid_t));
720 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400721 if (j->chrootdir)
722 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800723 if (j->alt_syscall_table) {
724 marshal_append(state, j->alt_syscall_table,
725 strlen(j->alt_syscall_table) + 1);
726 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800727 if (j->flags.seccomp_filter && j->filter_prog) {
728 struct sock_fprog *fp = j->filter_prog;
729 marshal_append(state, (char *)fp->filter,
730 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400731 }
Dylan Reid648b2202015-10-23 00:50:00 -0700732 for (m = j->mounts_head; m; m = m->next) {
733 marshal_append(state, m->src, strlen(m->src) + 1);
734 marshal_append(state, m->dest, strlen(m->dest) + 1);
735 marshal_append(state, m->type, strlen(m->type) + 1);
736 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400737 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800738 for (i = 0; i < j->cgroup_count; ++i)
739 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500740}
741
Will Drewry6ac91122011-10-21 16:38:58 -0500742size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400743{
744 struct marshal_state state;
745 marshal_state_init(&state, NULL, 0);
746 minijail_marshal_helper(&state, j);
747 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500748}
749
Elly Jonese1749eb2011-10-07 13:54:59 -0400750int minijail_marshal(const struct minijail *j, char *buf, size_t available)
751{
752 struct marshal_state state;
753 marshal_state_init(&state, buf, available);
754 minijail_marshal_helper(&state, j);
755 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500756}
757
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800758/*
759 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
Elly Jones51a5b6c2011-10-12 19:09:26 -0400760 * @length Number of bytes to consume
761 * @buf Buffer to consume from
762 * @buflength Size of @buf
763 *
764 * Returns a pointer to the base of the bytes, or NULL for errors.
765 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700766void *consumebytes(size_t length, char **buf, size_t *buflength)
767{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400768 char *p = *buf;
769 if (length > *buflength)
770 return NULL;
771 *buf += length;
772 *buflength -= length;
773 return p;
774}
775
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800776/*
777 * consumestr: consumes a C string from a buffer @buf of length @length
Elly Jones51a5b6c2011-10-12 19:09:26 -0400778 * @buf Buffer to consume
779 * @length Length of buffer
780 *
781 * Returns a pointer to the base of the string, or NULL for errors.
782 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700783char *consumestr(char **buf, size_t *buflength)
784{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400785 size_t len = strnlen(*buf, *buflength);
786 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700787 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400788 return NULL;
789 return consumebytes(len + 1, buf, buflength);
790}
791
Elly Jonese1749eb2011-10-07 13:54:59 -0400792int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
793{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800794 size_t i;
795 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500796 int ret = -EINVAL;
797
Elly Jonese1749eb2011-10-07 13:54:59 -0400798 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500799 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400800 memcpy((void *)j, serialized, sizeof(*j));
801 serialized += sizeof(*j);
802 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500803
Will Drewrybee7ba72011-10-21 20:47:01 -0500804 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700805 j->mounts_head = NULL;
806 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800807 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500808
Elly Jonese1749eb2011-10-07 13:54:59 -0400809 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400810 char *user = consumestr(&serialized, &length);
811 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500812 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400813 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500814 if (!j->user)
815 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400816 }
Will Drewryf89aef52011-09-16 16:48:57 -0500817
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800818 if (j->suppl_gid_list) { /* stale pointer */
819 if (j->suppl_gid_count > NGROUPS_MAX) {
820 goto bad_gid_list;
821 }
822 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
823 void *gid_list_bytes =
824 consumebytes(gid_list_size, &serialized, &length);
825 if (!gid_list_bytes)
826 goto bad_gid_list;
827
828 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
829 if (!j->suppl_gid_list)
830 goto bad_gid_list;
831
832 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
833 }
834
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400835 if (j->chrootdir) { /* stale pointer */
836 char *chrootdir = consumestr(&serialized, &length);
837 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500838 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400839 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500840 if (!j->chrootdir)
841 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400842 }
843
Andrew Brestickereac28942015-11-11 16:04:46 -0800844 if (j->alt_syscall_table) { /* stale pointer */
845 char *alt_syscall_table = consumestr(&serialized, &length);
846 if (!alt_syscall_table)
847 goto bad_syscall_table;
848 j->alt_syscall_table = strdup(alt_syscall_table);
849 if (!j->alt_syscall_table)
850 goto bad_syscall_table;
851 }
852
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800853 if (j->flags.seccomp_filter && j->filter_len > 0) {
854 size_t ninstrs = j->filter_len;
855 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
856 ninstrs > USHRT_MAX)
857 goto bad_filters;
858
859 size_t program_len = ninstrs * sizeof(struct sock_filter);
860 void *program = consumebytes(program_len, &serialized, &length);
861 if (!program)
862 goto bad_filters;
863
864 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800865 if (!j->filter_prog)
866 goto bad_filters;
867
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800868 j->filter_prog->len = ninstrs;
869 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800870 if (!j->filter_prog->filter)
871 goto bad_filter_prog_instrs;
872
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800873 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400874 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400875
Dylan Reid648b2202015-10-23 00:50:00 -0700876 count = j->mounts_count;
877 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400878 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700879 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400880 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700881 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400882 const char *src = consumestr(&serialized, &length);
883 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700884 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400885 dest = consumestr(&serialized, &length);
886 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700887 goto bad_mounts;
888 type = consumestr(&serialized, &length);
889 if (!type)
890 goto bad_mounts;
891 flags = consumebytes(sizeof(*flags), &serialized, &length);
892 if (!flags)
893 goto bad_mounts;
894 if (minijail_mount(j, src, dest, type, *flags))
895 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400896 }
897
Dylan Reid605ce7f2016-01-19 19:21:00 -0800898 count = j->cgroup_count;
899 j->cgroup_count = 0;
900 for (i = 0; i < count; ++i) {
901 char *cgroup = consumestr(&serialized, &length);
902 if (!cgroup)
903 goto bad_cgroups;
904 j->cgroups[i] = strdup(cgroup);
905 if (!j->cgroups[i])
906 goto bad_cgroups;
907 ++j->cgroup_count;
908 }
909
Elly Jonese1749eb2011-10-07 13:54:59 -0400910 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500911
Dylan Reid605ce7f2016-01-19 19:21:00 -0800912bad_cgroups:
913 while (j->mounts_head) {
914 struct mountpoint *m = j->mounts_head;
915 j->mounts_head = j->mounts_head->next;
916 free(m->type);
917 free(m->dest);
918 free(m->src);
919 free(m);
920 }
921 for (i = 0; i < j->cgroup_count; ++i)
922 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -0700923bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800924 if (j->flags.seccomp_filter && j->filter_len > 0) {
925 free(j->filter_prog->filter);
926 free(j->filter_prog);
927 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800928bad_filter_prog_instrs:
929 if (j->filter_prog)
930 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -0500931bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800932 if (j->alt_syscall_table)
933 free(j->alt_syscall_table);
934bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500935 if (j->chrootdir)
936 free(j->chrootdir);
937bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800938 if (j->suppl_gid_list)
939 free(j->suppl_gid_list);
940bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -0500941 if (j->user)
942 free(j->user);
943clear_pointers:
944 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800945 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500946 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800947 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800948 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500949out:
950 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500951}
952
Dylan Reidce5b55e2016-01-13 11:04:16 -0800953static void write_ugid_mappings(const struct minijail *j)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800954{
955 int fd, ret, len;
956 size_t sz;
957 char fname[32];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800958
959 sz = sizeof(fname);
960 if (j->uidmap) {
961 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700962 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800963 die("failed to write file name of uid_map");
964 fd = open(fname, O_WRONLY);
965 if (fd < 0)
966 pdie("failed to open '%s'", fname);
967 len = strlen(j->uidmap);
968 if (write(fd, j->uidmap, len) < len)
969 die("failed to set uid_map");
970 close(fd);
971 }
972 if (j->gidmap) {
973 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700974 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800975 die("failed to write file name of gid_map");
976 fd = open(fname, O_WRONLY);
977 if (fd < 0)
978 pdie("failed to open '%s'", fname);
979 len = strlen(j->gidmap);
980 if (write(fd, j->gidmap, len) < len)
981 die("failed to set gid_map");
982 close(fd);
983 }
Dylan Reidce5b55e2016-01-13 11:04:16 -0800984}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800985
Dylan Reidce5b55e2016-01-13 11:04:16 -0800986static void parent_setup_complete(int *pipe_fds)
987{
988 close(pipe_fds[0]);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800989 close(pipe_fds[1]);
990}
991
Dylan Reidce5b55e2016-01-13 11:04:16 -0800992/*
993 * wait_for_parent_setup: Called by the child process to wait for any
994 * further parent-side setup to complete before continuing.
995 */
996static void wait_for_parent_setup(int *pipe_fds)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800997{
998 char buf;
999
1000 close(pipe_fds[1]);
1001
Dylan Reidce5b55e2016-01-13 11:04:16 -08001002 /* Wait for parent to complete setup and close the pipe. */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001003 if (read(pipe_fds[0], &buf, 1) != 0)
1004 die("failed to sync with parent");
1005 close(pipe_fds[0]);
Dylan Reidce5b55e2016-01-13 11:04:16 -08001006}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001007
Dylan Reidce5b55e2016-01-13 11:04:16 -08001008static void enter_user_namespace(const struct minijail *j)
1009{
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001010 if (j->uidmap && setresuid(0, 0, 0))
1011 pdie("setresuid");
1012 if (j->gidmap && setresgid(0, 0, 0))
1013 pdie("setresgid");
1014}
1015
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001016/*
1017 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001018 * @j Minijail these mounts are for
1019 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001020 *
1021 * Returns 0 for success.
1022 */
Dylan Reid648b2202015-10-23 00:50:00 -07001023static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001024{
Dylan Reid648b2202015-10-23 00:50:00 -07001025 int ret;
1026 char *dest;
1027 int remount_ro = 0;
1028
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001029 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001030 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001031 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001032
1033 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001034 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1035 * can't both be specified in the original bind mount.
1036 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001037 */
1038 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1039 remount_ro = 1;
1040 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001041 }
Dylan Reid648b2202015-10-23 00:50:00 -07001042
1043 ret = mount(m->src, dest, m->type, m->flags, NULL);
1044 if (ret)
1045 pdie("mount: %s -> %s", m->src, dest);
1046
1047 if (remount_ro) {
1048 m->flags |= MS_RDONLY;
1049 ret = mount(m->src, dest, NULL,
1050 m->flags | MS_REMOUNT, NULL);
1051 if (ret)
1052 pdie("bind ro: %s -> %s", m->src, dest);
1053 }
1054
Elly Jones51a5b6c2011-10-12 19:09:26 -04001055 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001056 if (m->next)
1057 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001058 return ret;
1059}
1060
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001061int enter_chroot(const struct minijail *j)
1062{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001063 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001064
1065 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001066 return ret;
1067
1068 if (chroot(j->chrootdir))
1069 return -errno;
1070
1071 if (chdir("/"))
1072 return -errno;
1073
1074 return 0;
1075}
1076
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001077int enter_pivot_root(const struct minijail *j)
1078{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001079 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001080
1081 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001082 return ret;
1083
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001084 /*
1085 * Keep the fd for both old and new root.
1086 * It will be used in fchdir later.
1087 */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001088 oldroot = open("/", O_DIRECTORY | O_RDONLY);
1089 if (oldroot < 0)
1090 pdie("failed to open / for fchdir");
1091 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY);
1092 if (newroot < 0)
1093 pdie("failed to open %s for fchdir", j->chrootdir);
1094
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001095 /*
1096 * To ensure chrootdir is the root of a file system,
1097 * do a self bind mount.
1098 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001099 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1100 pdie("failed to bind mount '%s'", j->chrootdir);
1101 if (chdir(j->chrootdir))
1102 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001103 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001104 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001105
1106 /*
1107 * Now the old root is mounted on top of the new root. Use fchdir to
1108 * change to the old root and unmount it.
1109 */
1110 if (fchdir(oldroot))
1111 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001112 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001113 if (umount2(".", MNT_DETACH))
1114 pdie("umount(/)");
1115 /* Change back to the new root. */
1116 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001117 return -errno;
1118 if (chroot("/"))
1119 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001120 /* Set correct CWD for getcwd(3). */
1121 if (chdir("/"))
1122 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001123
1124 return 0;
1125}
1126
Lee Campbell11af0622014-05-22 12:36:04 -07001127int mount_tmp(void)
1128{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001129 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001130}
1131
Dylan Reid791f5772015-09-14 20:02:42 -07001132int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001133{
1134 const char *kProcPath = "/proc";
1135 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001136 /*
1137 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001138 * /proc in our namespace, which means using MS_REMOUNT here would
1139 * mutate our parent's mount as well, even though we're in a VFS
1140 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001141 * and make our own. However, if we are in a new user namespace, /proc
1142 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -04001143 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001144 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -04001145 return -errno;
1146 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1147 return -errno;
1148 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001149}
1150
Dylan Reid605ce7f2016-01-19 19:21:00 -08001151static void write_pid_to_path(pid_t pid, const char *path)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001152{
Dylan Reid605ce7f2016-01-19 19:21:00 -08001153 FILE *fp = fopen(path, "w");
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001154
1155 if (!fp)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001156 pdie("failed to open '%s'", path);
1157 if (fprintf(fp, "%d\n", (int)pid) < 0)
1158 pdie("fprintf(%s)", path);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001159 if (fclose(fp))
Dylan Reid605ce7f2016-01-19 19:21:00 -08001160 pdie("fclose(%s)", path);
1161}
1162
1163static void write_pid_file(const struct minijail *j)
1164{
1165 write_pid_to_path(j->initpid, j->pid_file_path);
1166}
1167
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001168static void add_to_cgroups(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001169{
1170 size_t i;
1171
1172 for (i = 0; i < j->cgroup_count; ++i)
1173 write_pid_to_path(j->initpid, j->cgroups[i]);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001174}
1175
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001176void drop_ugid(const struct minijail *j)
1177{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001178 if (j->flags.usergroups && j->flags.suppl_gids) {
1179 die("tried to inherit *and* set supplementary groups;"
1180 " can only do one");
1181 }
1182
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001183 if (j->flags.usergroups) {
1184 if (initgroups(j->user, j->usergid))
1185 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001186 } else if (j->flags.suppl_gids) {
1187 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1188 pdie("setgroups");
1189 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001190 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001191 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001192 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001193 * users.
1194 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001195 if ((j->uid || j->gid) && setgroups(0, NULL))
1196 pdie("setgroups");
1197 }
1198
1199 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1200 pdie("setresgid");
1201
1202 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1203 pdie("setresuid");
1204}
1205
Mike Frysinger3adfef72013-05-09 17:19:08 -04001206/*
1207 * We specifically do not use cap_valid() as that only tells us the last
1208 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001209 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001210 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001211 * kernel).
1212 * Normally, we suck up the answer via /proc. On Android, not all processes are
1213 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1214 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001215 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001216static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001217{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001218 unsigned int last_valid_cap = 0;
1219 if (is_android()) {
1220 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1221 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001222
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001223 /* |last_valid_cap| will be the first failing value. */
1224 if (last_valid_cap > 0) {
1225 last_valid_cap--;
1226 }
1227 } else {
1228 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1229 FILE *fp = fopen(cap_file, "re");
1230 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1231 pdie("fscanf(%s)", cap_file);
1232 fclose(fp);
1233 }
Dylan Reidf682d472015-09-17 21:39:07 -07001234 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001235}
1236
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001237static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1238{
1239 const uint64_t one = 1;
1240 unsigned int i;
1241 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1242 if (keep_mask & (one << i))
1243 continue;
1244 if (prctl(PR_CAPBSET_DROP, i))
1245 pdie("could not drop capability from bounding set");
1246 }
1247}
1248
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001249void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001250{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001251 if (!j->flags.use_caps)
1252 return;
1253
Elly Jonese1749eb2011-10-07 13:54:59 -04001254 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001255 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001256 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001257 unsigned int i;
1258 if (!caps)
1259 die("can't get process caps");
1260 if (cap_clear_flag(caps, CAP_INHERITABLE))
1261 die("can't clear inheritable caps");
1262 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1263 die("can't clear effective caps");
1264 if (cap_clear_flag(caps, CAP_PERMITTED))
1265 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001266 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001267 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001268 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001269 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001270 flag[0] = i;
1271 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001272 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001273 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001274 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001275 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001276 die("can't add inheritable cap");
1277 }
1278 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001279 die("can't apply initial cleaned capset");
1280
1281 /*
1282 * Instead of dropping bounding set first, do it here in case
1283 * the caller had a more permissive bounding set which could
1284 * have been used above to raise a capability that wasn't already
1285 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1286 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001287 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001288
1289 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001290 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001291 flag[0] = CAP_SETPCAP;
1292 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1293 die("can't clear effective cap");
1294 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1295 die("can't clear permitted cap");
1296 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1297 die("can't clear inheritable cap");
1298 }
1299
1300 if (cap_set_proc(caps))
1301 die("can't apply final cleaned capset");
1302
1303 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001304}
1305
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001306void set_seccomp_filter(const struct minijail *j)
1307{
1308 /*
1309 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1310 * in the kernel source tree for an explanation of the parameters.
1311 */
1312 if (j->flags.no_new_privs) {
1313 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1314 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1315 }
1316
1317 /*
1318 * If we're logging seccomp filter failures,
1319 * install the SIGSYS handler first.
1320 */
1321 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1322 if (install_sigsys_handler())
1323 pdie("install SIGSYS handler");
1324 warn("logging seccomp filter failures");
1325 }
1326
1327 /*
1328 * Install the syscall filter.
1329 */
1330 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001331 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1332 j->filter_prog)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001333 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001334 warn("seccomp not supported");
1335 return;
1336 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001337 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001338 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001339 }
1340}
1341
Will Drewry6ac91122011-10-21 16:38:58 -05001342void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001343{
Dylan Reidf682d472015-09-17 21:39:07 -07001344 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001345 * If we're dropping caps, get the last valid cap from /proc now,
1346 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001347 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001348 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001349 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001350 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001351
Elly Jonese1749eb2011-10-07 13:54:59 -04001352 if (j->flags.pids)
1353 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001354 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001355
Elly Jonese1749eb2011-10-07 13:54:59 -04001356 if (j->flags.usergroups && !j->user)
1357 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001358
Elly Jonesdd3e8512012-01-23 15:13:38 -05001359 /*
1360 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001361 * so we don't even try. If any of our operations fail, we abort() the
1362 * entire process.
1363 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001364 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1365 pdie("setns(CLONE_NEWNS)");
1366
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001367 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001368 if (unshare(CLONE_NEWNS))
1369 pdie("unshare(vfs)");
1370 /*
1371 * Remount all filesystems as private. If they are shared
1372 * new bind mounts will creep out of our namespace.
1373 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1374 */
1375 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1376 pdie("mount(/, private)");
1377 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001378
Dylan Reidf7942472015-11-18 17:55:26 -08001379 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1380 pdie("unshare(ipc)");
1381 }
1382
Dylan Reid1102f5a2015-09-15 11:52:20 -07001383 if (j->flags.enter_net) {
1384 if (setns(j->netns_fd, CLONE_NEWNET))
1385 pdie("setns(CLONE_NEWNET)");
1386 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001387 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001388 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001389
Elly Jones51a5b6c2011-10-12 19:09:26 -04001390 if (j->flags.chroot && enter_chroot(j))
1391 pdie("chroot");
1392
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001393 if (j->flags.pivot_root && enter_pivot_root(j))
1394 pdie("pivot_root");
1395
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001396 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001397 pdie("mount_tmp");
1398
Dylan Reid791f5772015-09-14 20:02:42 -07001399 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001400 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001401
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001402 /*
1403 * If we're only dropping capabilities from the bounding set, but not
1404 * from the thread's (permitted|inheritable|effective) sets, do it now.
1405 */
1406 if (j->flags.capbset_drop) {
1407 drop_capbset(j->cap_bset, last_valid_cap);
1408 }
1409
1410 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001411 /*
1412 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001413 * capability to change uids, our attempt to use setuid()
1414 * below will fail. Hang on to root caps across setuid(), then
1415 * lock securebits.
1416 */
1417 if (prctl(PR_SET_KEEPCAPS, 1))
1418 pdie("prctl(PR_SET_KEEPCAPS)");
1419 if (prctl
1420 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1421 pdie("prctl(PR_SET_SECUREBITS)");
1422 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001423
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001424 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001425 /*
1426 * If we're setting no_new_privs, we can drop privileges
1427 * before setting seccomp filter. This way filter policies
1428 * don't need to allow privilege-dropping syscalls.
1429 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001430 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001431 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001432 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001433 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001434 /*
1435 * If we're not setting no_new_privs,
1436 * we need to set seccomp filter *before* dropping privileges.
1437 * WARNING: this means that filter policies *must* allow
1438 * setgroups()/setresgid()/setresuid() for dropping root and
1439 * capget()/capset()/prctl() for dropping caps.
1440 */
1441 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001442 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001443 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001444 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001445
Elly Jonesdd3e8512012-01-23 15:13:38 -05001446 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001447 * Select the specified alternate syscall table. The table must not
1448 * block prctl(2) if we're using seccomp as well.
1449 */
1450 if (j->flags.alt_syscall) {
1451 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1452 pdie("prctl(PR_ALT_SYSCALL)");
1453 }
1454
1455 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001456 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001457 * privilege-dropping syscalls :)
1458 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001459 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001460 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001461 warn("seccomp not supported");
1462 return;
1463 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001464 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001465 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001466}
1467
Will Drewry6ac91122011-10-21 16:38:58 -05001468/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001469static int init_exitstatus = 0;
1470
Will Drewry6ac91122011-10-21 16:38:58 -05001471void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001472{
1473 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001474}
1475
Will Drewry6ac91122011-10-21 16:38:58 -05001476int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001477{
1478 pid_t pid;
1479 int status;
1480 /* so that we exit with the right status */
1481 signal(SIGTERM, init_term);
1482 /* TODO(wad) self jail with seccomp_filters here. */
1483 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001484 /*
1485 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001486 * left inside our pid namespace or we get a signal.
1487 */
1488 if (pid == rootpid)
1489 init_exitstatus = status;
1490 }
1491 if (!WIFEXITED(init_exitstatus))
1492 _exit(MINIJAIL_ERR_INIT);
1493 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001494}
1495
Will Drewry6ac91122011-10-21 16:38:58 -05001496int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001497{
1498 size_t sz = 0;
1499 size_t bytes = read(fd, &sz, sizeof(sz));
1500 char *buf;
1501 int r;
1502 if (sizeof(sz) != bytes)
1503 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001504 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001505 return -E2BIG;
1506 buf = malloc(sz);
1507 if (!buf)
1508 return -ENOMEM;
1509 bytes = read(fd, buf, sz);
1510 if (bytes != sz) {
1511 free(buf);
1512 return -EINVAL;
1513 }
1514 r = minijail_unmarshal(j, buf, sz);
1515 free(buf);
1516 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001517}
1518
Will Drewry6ac91122011-10-21 16:38:58 -05001519int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001520{
1521 char *buf;
1522 size_t sz = minijail_size(j);
1523 ssize_t written;
1524 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001525
Elly Jonese1749eb2011-10-07 13:54:59 -04001526 if (!sz)
1527 return -EINVAL;
1528 buf = malloc(sz);
1529 r = minijail_marshal(j, buf, sz);
1530 if (r) {
1531 free(buf);
1532 return r;
1533 }
1534 /* Sends [size][minijail]. */
1535 written = write(fd, &sz, sizeof(sz));
1536 if (written != sizeof(sz)) {
1537 free(buf);
1538 return -EFAULT;
1539 }
1540 written = write(fd, buf, sz);
1541 if (written < 0 || (size_t) written != sz) {
1542 free(buf);
1543 return -EFAULT;
1544 }
1545 free(buf);
1546 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001547}
Elly Jonescd7a9042011-07-22 13:56:51 -04001548
Will Drewry6ac91122011-10-21 16:38:58 -05001549int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001550{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001551#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001552 /* Don't use LDPRELOAD on Brillo. */
1553 return 0;
1554#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001555 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1556 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1557 if (!newenv)
1558 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001559
Elly Jonese1749eb2011-10-07 13:54:59 -04001560 /* Only insert a separating space if we have something to separate... */
1561 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1562 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001563
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001564 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001565 setenv(kLdPreloadEnvVar, newenv, 1);
1566 free(newenv);
1567 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001568#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001569}
1570
Will Drewry6ac91122011-10-21 16:38:58 -05001571int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001572{
1573 int r = pipe(fds);
1574 char fd_buf[11];
1575 if (r)
1576 return r;
1577 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1578 if (r <= 0)
1579 return -EINVAL;
1580 setenv(kFdEnvVar, fd_buf, 1);
1581 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001582}
1583
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001584int setup_pipe_end(int fds[2], size_t index)
1585{
1586 if (index > 1)
1587 return -1;
1588
1589 close(fds[1 - index]);
1590 return fds[index];
1591}
1592
1593int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1594{
1595 if (index > 1)
1596 return -1;
1597
1598 close(fds[1 - index]);
1599 /* dup2(2) the corresponding end of the pipe into |fd|. */
1600 return dup2(fds[index], fd);
1601}
1602
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001603int minijail_run_internal(struct minijail *j, const char *filename,
1604 char *const argv[], pid_t *pchild_pid,
1605 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1606 int use_preload);
1607
Will Drewry6ac91122011-10-21 16:38:58 -05001608int API minijail_run(struct minijail *j, const char *filename,
1609 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001610{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001611 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1612 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001613}
1614
1615int API minijail_run_pid(struct minijail *j, const char *filename,
1616 char *const argv[], pid_t *pchild_pid)
1617{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001618 return minijail_run_internal(j, filename, argv, pchild_pid,
1619 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001620}
1621
1622int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001623 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001624{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001625 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1626 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001627}
1628
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001629int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001630 char *const argv[], pid_t *pchild_pid,
1631 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001632{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001633 return minijail_run_internal(j, filename, argv, pchild_pid,
1634 pstdin_fd, pstdout_fd, pstderr_fd, true);
1635}
1636
1637int API minijail_run_no_preload(struct minijail *j, const char *filename,
1638 char *const argv[])
1639{
1640 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1641 false);
1642}
1643
Samuel Tan63187f42015-10-16 13:01:53 -07001644int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001645 const char *filename,
1646 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001647 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001648 int *pstdin_fd, int *pstdout_fd,
1649 int *pstderr_fd) {
Samuel Tan63187f42015-10-16 13:01:53 -07001650 return minijail_run_internal(j, filename, argv, pchild_pid,
1651 pstdin_fd, pstdout_fd, pstderr_fd, false);
1652}
1653
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001654int minijail_run_internal(struct minijail *j, const char *filename,
1655 char *const argv[], pid_t *pchild_pid,
1656 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1657 int use_preload)
1658{
Elly Jonese1749eb2011-10-07 13:54:59 -04001659 char *oldenv, *oldenv_copy = NULL;
1660 pid_t child_pid;
1661 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001662 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001663 int stdout_fds[2];
1664 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001665 int child_sync_pipe_fds[2];
1666 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001667 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001668 /* We need to remember this across the minijail_preexec() call. */
1669 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001670 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001671
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001672 if (use_preload) {
1673 oldenv = getenv(kLdPreloadEnvVar);
1674 if (oldenv) {
1675 oldenv_copy = strdup(oldenv);
1676 if (!oldenv_copy)
1677 return -ENOMEM;
1678 }
1679
1680 if (setup_preload())
1681 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001682 }
Will Drewryf89aef52011-09-16 16:48:57 -05001683
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001684 if (!use_preload) {
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001685 if (j->flags.use_caps)
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001686 die("capabilities are not supported without "
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001687 "LD_PRELOAD");
1688 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001689
Elly Jonesdd3e8512012-01-23 15:13:38 -05001690 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001691 * Make the process group ID of this process equal to its PID, so that
1692 * both the Minijail process and the jailed process can be killed
1693 * together.
1694 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1695 * the process is already a process group leader.
1696 */
1697 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1698 if (errno != EPERM) {
1699 pdie("setpgid(0, 0)");
1700 }
1701 }
1702
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001703 if (use_preload) {
1704 /*
1705 * Before we fork(2) and execve(2) the child process, we need
1706 * to open a pipe(2) to send the minijail configuration over.
1707 */
1708 if (setup_pipe(pipe_fds))
1709 return -EFAULT;
1710 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001711
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001712 /*
1713 * If we want to write to the child process' standard input,
1714 * create the pipe(2) now.
1715 */
1716 if (pstdin_fd) {
1717 if (pipe(stdin_fds))
1718 return -EFAULT;
1719 }
1720
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001721 /*
1722 * If we want to read from the child process' standard output,
1723 * create the pipe(2) now.
1724 */
1725 if (pstdout_fd) {
1726 if (pipe(stdout_fds))
1727 return -EFAULT;
1728 }
1729
1730 /*
1731 * If we want to read from the child process' standard error,
1732 * create the pipe(2) now.
1733 */
1734 if (pstderr_fd) {
1735 if (pipe(stderr_fds))
1736 return -EFAULT;
1737 }
1738
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001739 /*
1740 * If we want to set up a new uid/gid mapping in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001741 * or if we need to add the child process to cgroups, create the pipe(2)
1742 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001743 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001744 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001745 sync_child = 1;
1746 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001747 return -EFAULT;
1748 }
1749
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001750 /*
1751 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001752 *
1753 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1754 *
1755 * In multithreaded programs, there are a bunch of locks inside libc,
1756 * some of which may be held by other threads at the time that we call
1757 * minijail_run_pid(). If we call fork(), glibc does its level best to
1758 * ensure that we hold all of these locks before it calls clone()
1759 * internally and drop them after clone() returns, but when we call
1760 * sys_clone(2) directly, all that gets bypassed and we end up with a
1761 * child address space where some of libc's important locks are held by
1762 * other threads (which did not get cloned, and hence will never release
1763 * those locks). This is okay so long as we call exec() immediately
1764 * after, but a bunch of seemingly-innocent libc functions like setenv()
1765 * take locks.
1766 *
1767 * Hence, only call sys_clone() if we need to, in order to get at pid
1768 * namespacing. If we follow this path, the child's address space might
1769 * have broken locks; you may only call functions that do not acquire
1770 * any locks.
1771 *
1772 * Unfortunately, fork() acquires every lock it can get its hands on, as
1773 * previously detailed, so this function is highly likely to deadlock
1774 * later on (see "deadlock here") if we're multithreaded.
1775 *
1776 * We might hack around this by having the clone()d child (init of the
1777 * pid namespace) return directly, rather than leaving the clone()d
1778 * process hanging around to be init for the new namespace (and having
1779 * its fork()ed child return in turn), but that process would be crippled
1780 * with its libc locks potentially broken. We might try fork()ing in the
1781 * parent before we clone() to ensure that we own all the locks, but
1782 * then we have to have the forked child hanging around consuming
1783 * resources (and possibly having file descriptors / shared memory
1784 * regions / etc attached). We'd need to keep the child around to avoid
1785 * having its children get reparented to init.
1786 *
1787 * TODO(ellyjones): figure out if the "forked child hanging around"
1788 * problem is fixable or not. It would be nice if we worked in this
1789 * case.
1790 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001791 if (pid_namespace) {
1792 int clone_flags = CLONE_NEWPID | SIGCHLD;
1793 if (j->flags.userns)
1794 clone_flags |= CLONE_NEWUSER;
1795 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001796 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001797 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001798 }
Elly Jones761b7412012-06-13 15:49:52 -04001799
Elly Jonese1749eb2011-10-07 13:54:59 -04001800 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001801 if (use_preload) {
1802 free(oldenv_copy);
1803 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001804 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001805 }
Will Drewryf89aef52011-09-16 16:48:57 -05001806
Elly Jonese1749eb2011-10-07 13:54:59 -04001807 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001808 if (use_preload) {
1809 /* Restore parent's LD_PRELOAD. */
1810 if (oldenv_copy) {
1811 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1812 free(oldenv_copy);
1813 } else {
1814 unsetenv(kLdPreloadEnvVar);
1815 }
1816 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001817 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001818
Elly Jonese1749eb2011-10-07 13:54:59 -04001819 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001820
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001821 if (j->flags.pid_file)
1822 write_pid_file(j);
1823
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001824 if (j->flags.cgroups)
1825 add_to_cgroups(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001826
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001827 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001828 write_ugid_mappings(j);
1829
1830 if (sync_child)
1831 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001832
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001833 if (use_preload) {
1834 /* Send marshalled minijail. */
1835 close(pipe_fds[0]); /* read endpoint */
1836 ret = minijail_to_fd(j, pipe_fds[1]);
1837 close(pipe_fds[1]); /* write endpoint */
1838 if (ret) {
1839 kill(j->initpid, SIGKILL);
1840 die("failed to send marshalled minijail");
1841 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001842 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001843
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001844 if (pchild_pid)
1845 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001846
1847 /*
1848 * If we want to write to the child process' standard input,
1849 * set up the write end of the pipe.
1850 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001851 if (pstdin_fd)
1852 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001853 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001854
1855 /*
1856 * If we want to read from the child process' standard output,
1857 * set up the read end of the pipe.
1858 */
1859 if (pstdout_fd)
1860 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001861 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001862
1863 /*
1864 * If we want to read from the child process' standard error,
1865 * set up the read end of the pipe.
1866 */
1867 if (pstderr_fd)
1868 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001869 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001870
Elly Jonese1749eb2011-10-07 13:54:59 -04001871 return 0;
1872 }
1873 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001874
Peter Qiu2860c462015-12-16 15:13:06 -08001875 if (j->flags.reset_signal_mask) {
1876 sigset_t signal_mask;
1877 if (sigemptyset(&signal_mask) != 0)
1878 pdie("sigemptyset failed");
1879 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
1880 pdie("sigprocmask failed");
1881 }
1882
Dylan Reidce5b55e2016-01-13 11:04:16 -08001883 if (sync_child)
1884 wait_for_parent_setup(child_sync_pipe_fds);
1885
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001886 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001887 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001888
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001889 /*
1890 * If we want to write to the jailed process' standard input,
1891 * set up the read end of the pipe.
1892 */
1893 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001894 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1895 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001896 die("failed to set up stdin pipe");
1897 }
1898
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001899 /*
1900 * If we want to read from the jailed process' standard output,
1901 * set up the write end of the pipe.
1902 */
1903 if (pstdout_fd) {
1904 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1905 STDOUT_FILENO) < 0)
1906 die("failed to set up stdout pipe");
1907 }
1908
1909 /*
1910 * If we want to read from the jailed process' standard error,
1911 * set up the write end of the pipe.
1912 */
1913 if (pstderr_fd) {
1914 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1915 STDERR_FILENO) < 0)
1916 die("failed to set up stderr pipe");
1917 }
1918
Dylan Reid791f5772015-09-14 20:02:42 -07001919 /* If running an init program, let it decide when/how to mount /proc. */
1920 if (pid_namespace && !do_init)
1921 j->flags.remount_proc_ro = 0;
1922
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001923 if (use_preload) {
1924 /* Strip out flags that cannot be inherited across execve(2). */
1925 minijail_preexec(j);
1926 } else {
1927 j->flags.pids = 0;
1928 }
1929 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001930 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001931
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001932 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001933 /*
1934 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001935 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001936 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001937 * a child to actually run the program. If |do_init == 0|, we
1938 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001939 *
1940 * If we're multithreaded, we'll probably deadlock here. See
1941 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001942 */
1943 child_pid = fork();
1944 if (child_pid < 0)
1945 _exit(child_pid);
1946 else if (child_pid > 0)
1947 init(child_pid); /* never returns */
1948 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001949
Elly Jonesdd3e8512012-01-23 15:13:38 -05001950 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001951 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001952 * calling process
1953 * -> execve()-ing process
1954 * If we are:
1955 * calling process
1956 * -> init()-ing process
1957 * -> execve()-ing process
1958 */
1959 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001960}
1961
Will Drewry6ac91122011-10-21 16:38:58 -05001962int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001963{
1964 int st;
1965 if (kill(j->initpid, SIGTERM))
1966 return -errno;
1967 if (waitpid(j->initpid, &st, 0) < 0)
1968 return -errno;
1969 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001970}
1971
Will Drewry6ac91122011-10-21 16:38:58 -05001972int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001973{
1974 int st;
1975 if (waitpid(j->initpid, &st, 0) < 0)
1976 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001977
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001978 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001979 int error_status = st;
1980 if (WIFSIGNALED(st)) {
1981 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001982 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001983 j->initpid, signum);
1984 /*
1985 * We return MINIJAIL_ERR_JAIL if the process received
1986 * SIGSYS, which happens when a syscall is blocked by
1987 * seccomp filters.
1988 * If not, we do what bash(1) does:
1989 * $? = 128 + signum
1990 */
1991 if (signum == SIGSYS) {
1992 error_status = MINIJAIL_ERR_JAIL;
1993 } else {
1994 error_status = 128 + signum;
1995 }
1996 }
1997 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001998 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001999
2000 int exit_status = WEXITSTATUS(st);
2001 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002002 info("child process %d exited with status %d",
2003 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002004
2005 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002006}
2007
Will Drewry6ac91122011-10-21 16:38:58 -05002008void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002009{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002010 size_t i;
2011
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002012 if (j->flags.seccomp_filter && j->filter_prog) {
2013 free(j->filter_prog->filter);
2014 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002015 }
Dylan Reid648b2202015-10-23 00:50:00 -07002016 while (j->mounts_head) {
2017 struct mountpoint *m = j->mounts_head;
2018 j->mounts_head = j->mounts_head->next;
2019 free(m->type);
2020 free(m->dest);
2021 free(m->src);
2022 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002023 }
Dylan Reid648b2202015-10-23 00:50:00 -07002024 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002025 if (j->user)
2026 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002027 if (j->suppl_gid_list)
2028 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002029 if (j->chrootdir)
2030 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08002031 if (j->alt_syscall_table)
2032 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002033 for (i = 0; i < j->cgroup_count; ++i)
2034 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002035 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002036}