blob: 3caafa46feee561f71a186fe094990cce1f486b5 [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 Reid648b2202015-10-23 00:50:00 -070075struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040076 char *src;
77 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070078 char *type;
79 unsigned long flags;
80 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040081};
82
Will Drewryf89aef52011-09-16 16:48:57 -050083struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070084 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070085 * WARNING: if you add a flag here you need to make sure it's
86 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070087 */
Elly Jonese1749eb2011-10-07 13:54:59 -040088 struct {
89 int uid:1;
90 int gid:1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -080091 int usergroups:1;
92 int suppl_gids:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040093 int caps:1;
94 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070095 int enter_vfs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040096 int pids:1;
Dylan Reidf7942472015-11-18 17:55:26 -080097 int ipc:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -040098 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -070099 int enter_net:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800100 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400101 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -0700102 int remount_proc_ro:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700103 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400104 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700105 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400106 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800107 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700108 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800109 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800110 int pid_file:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800111 int alt_syscall:1;
Peter Qiu2860c462015-12-16 15:13:06 -0800112 int reset_signal_mask:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400113 } flags;
114 uid_t uid;
115 gid_t gid;
116 gid_t usergid;
117 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800118 size_t suppl_gid_count;
119 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400120 uint64_t caps;
121 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700122 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700123 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400124 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800125 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800126 char *uidmap;
127 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800128 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800129 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800130 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700131 struct mountpoint *mounts_head;
132 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800133 size_t mounts_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500134};
135
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700136/*
137 * Strip out flags meant for the parent.
138 * We keep things that are not inherited across execve(2) (e.g. capabilities),
139 * or are easier to set after execve(2) (e.g. seccomp filters).
140 */
141void minijail_preenter(struct minijail *j)
142{
143 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700144 j->flags.enter_vfs = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700145 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700146 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800147 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800148 j->flags.pid_file = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700149}
150
151/*
152 * Strip out flags meant for the child.
153 * We keep things that are inherited across execve(2).
154 */
155void minijail_preexec(struct minijail *j)
156{
157 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700158 int enter_vfs = j->flags.enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700159 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800160 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700161 if (j->user)
162 free(j->user);
163 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800164 if (j->suppl_gid_list)
165 free(j->suppl_gid_list);
166 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700167 memset(&j->flags, 0, sizeof(j->flags));
168 /* Now restore anything we meant to keep. */
169 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700170 j->flags.enter_vfs = enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700171 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800172 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700173 /* Note, |pids| will already have been used before this call. */
174}
175
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800176/* Returns true if the kernel version is less than 3.8. */
177int seccomp_kernel_support_not_required()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800178{
179 int major, minor;
180 struct utsname uts;
181 return (uname(&uts) != -1 &&
182 sscanf(uts.release, "%d.%d", &major, &minor) == 2 &&
183 ((major < 3) || ((major == 3) && (minor < 8))));
184}
185
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800186/* Allow seccomp soft-fail on Android devices with kernel version < 3.8. */
187int can_softfail()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800188{
189#if SECCOMP_SOFTFAIL
190 if (is_android()) {
191 if (seccomp_kernel_support_not_required())
192 return 1;
193 else
194 return 0;
195 } else {
196 return 1;
197 }
198#endif
199 return 0;
200}
201
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700202/* Minijail API. */
203
Will Drewry6ac91122011-10-21 16:38:58 -0500204struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400205{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400206 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400207}
208
Will Drewry6ac91122011-10-21 16:38:58 -0500209void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400210{
211 if (uid == 0)
212 die("useless change to uid 0");
213 j->uid = uid;
214 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400215}
216
Will Drewry6ac91122011-10-21 16:38:58 -0500217void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400218{
219 if (gid == 0)
220 die("useless change to gid 0");
221 j->gid = gid;
222 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400223}
224
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800225void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
226 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800227{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800228 size_t i;
229
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800230 if (j->flags.usergroups)
231 die("cannot inherit *and* set supplementary groups");
232
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800233 if (size == 0) {
234 /* Clear supplementary groups. */
235 j->suppl_gid_list = NULL;
236 j->suppl_gid_count = 0;
237 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800238 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800239 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800240
241 /* Copy the gid_t array. */
242 j->suppl_gid_list = calloc(size, sizeof(gid_t));
243 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800244 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800245 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800246 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800247 j->suppl_gid_list[i] = list[i];
248 }
249 j->suppl_gid_count = size;
250 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800251}
252
Will Drewry6ac91122011-10-21 16:38:58 -0500253int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400254{
255 char *buf = NULL;
256 struct passwd pw;
257 struct passwd *ppw = NULL;
258 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
259 if (sz == -1)
260 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400261
Elly Jonesdd3e8512012-01-23 15:13:38 -0500262 /*
263 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400264 * the maximum needed size of the buffer, so we don't have to search.
265 */
266 buf = malloc(sz);
267 if (!buf)
268 return -ENOMEM;
269 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500270 /*
271 * We're safe to free the buffer here. The strings inside pw point
272 * inside buf, but we don't use any of them; this leaves the pointers
273 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
274 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400275 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700276 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400277 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700278 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400279 minijail_change_uid(j, ppw->pw_uid);
280 j->user = strdup(user);
281 if (!j->user)
282 return -ENOMEM;
283 j->usergid = ppw->pw_gid;
284 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400285}
286
Will Drewry6ac91122011-10-21 16:38:58 -0500287int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400288{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700289 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700290 struct group gr;
291 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400292 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
293 if (sz == -1)
294 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400295
Elly Jonesdd3e8512012-01-23 15:13:38 -0500296 /*
297 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400298 * the maximum needed size of the buffer, so we don't have to search.
299 */
300 buf = malloc(sz);
301 if (!buf)
302 return -ENOMEM;
303 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500304 /*
305 * We're safe to free the buffer here. The strings inside gr point
306 * inside buf, but we don't use any of them; this leaves the pointers
307 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
308 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400309 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700310 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400311 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700312 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400313 minijail_change_gid(j, pgr->gr_gid);
314 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400315}
316
Will Drewry6ac91122011-10-21 16:38:58 -0500317void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400318{
319 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400320}
321
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700322void API minijail_no_new_privs(struct minijail *j)
323{
324 j->flags.no_new_privs = 1;
325}
326
Will Drewry6ac91122011-10-21 16:38:58 -0500327void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400328{
329 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500330}
331
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700332void API minijail_log_seccomp_filter_failures(struct minijail *j)
333{
334 j->flags.log_seccomp_filter = 1;
335}
336
Will Drewry6ac91122011-10-21 16:38:58 -0500337void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400338{
339 j->caps = capmask;
340 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400341}
342
Peter Qiu2860c462015-12-16 15:13:06 -0800343void API minijail_reset_signal_mask(struct minijail* j) {
344 j->flags.reset_signal_mask = 1;
345}
346
Will Drewry6ac91122011-10-21 16:38:58 -0500347void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400348{
349 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400350}
351
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700352void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
353{
354 int ns_fd = open(ns_path, O_RDONLY);
355 if (ns_fd < 0) {
356 pdie("failed to open namespace '%s'", ns_path);
357 }
358 j->mountns_fd = ns_fd;
359 j->flags.enter_vfs = 1;
360}
361
Will Drewry6ac91122011-10-21 16:38:58 -0500362void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400363{
Elly Jonese58176c2012-01-23 11:46:17 -0500364 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700365 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400366 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800367 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400368}
369
Dylan Reidf7942472015-11-18 17:55:26 -0800370void API minijail_namespace_ipc(struct minijail *j)
371{
372 j->flags.ipc = 1;
373}
374
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400375void API minijail_namespace_net(struct minijail *j)
376{
377 j->flags.net = 1;
378}
379
Dylan Reid1102f5a2015-09-15 11:52:20 -0700380void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
381{
382 int ns_fd = open(ns_path, O_RDONLY);
383 if (ns_fd < 0) {
384 pdie("failed to open namespace '%s'", ns_path);
385 }
386 j->netns_fd = ns_fd;
387 j->flags.enter_net = 1;
388}
389
Dylan Reid791f5772015-09-14 20:02:42 -0700390void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400391{
392 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700393 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400394}
395
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800396void API minijail_namespace_user(struct minijail *j)
397{
398 j->flags.userns = 1;
399}
400
401int API minijail_uidmap(struct minijail *j, const char *uidmap)
402{
403 j->uidmap = strdup(uidmap);
404 if (!j->uidmap)
405 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800406 char *ch;
407 for (ch = j->uidmap; *ch; ch++) {
408 if (*ch == ',')
409 *ch = '\n';
410 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800411 return 0;
412}
413
414int API minijail_gidmap(struct minijail *j, const char *gidmap)
415{
416 j->gidmap = strdup(gidmap);
417 if (!j->gidmap)
418 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800419 char *ch;
420 for (ch = j->gidmap; *ch; ch++) {
421 if (*ch == ',')
422 *ch = '\n';
423 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800424 return 0;
425}
426
Will Drewry6ac91122011-10-21 16:38:58 -0500427void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400428{
429 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400430}
431
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800432void API minijail_run_as_init(struct minijail *j)
433{
434 /*
435 * Since the jailed program will become 'init' in the new PID namespace,
436 * Minijail does not need to fork an 'init' process.
437 */
438 j->flags.do_init = 0;
439}
440
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700441int API minijail_enter_chroot(struct minijail *j, const char *dir)
442{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400443 if (j->chrootdir)
444 return -EINVAL;
445 j->chrootdir = strdup(dir);
446 if (!j->chrootdir)
447 return -ENOMEM;
448 j->flags.chroot = 1;
449 return 0;
450}
451
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800452int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
453{
454 if (j->chrootdir)
455 return -EINVAL;
456 j->chrootdir = strdup(dir);
457 if (!j->chrootdir)
458 return -ENOMEM;
459 j->flags.pivot_root = 1;
460 return 0;
461}
462
Dylan Reida14e08d2015-10-22 21:05:29 -0700463static char *append_external_path(const char *external_path,
464 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700465{
Dylan Reida14e08d2015-10-22 21:05:29 -0700466 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700467 size_t pathlen;
468
Dylan Reid08946cc2015-09-16 19:10:57 -0700469 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700470 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
471 path = malloc(pathlen);
472 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700473
Dylan Reida14e08d2015-10-22 21:05:29 -0700474 return path;
475}
476
477char API *minijail_get_original_path(struct minijail *j,
478 const char *path_inside_chroot)
479{
Dylan Reid648b2202015-10-23 00:50:00 -0700480 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700481
Dylan Reid648b2202015-10-23 00:50:00 -0700482 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700483 while (b) {
484 /*
485 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700486 * mount, then the original path is exactly the source of
487 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700488 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700489 * mount source = /some/path/exe, mount dest =
490 * /chroot/path/exe Then when getting the original path of
491 * "/chroot/path/exe", the source of that mount,
492 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700493 */
494 if (!strcmp(b->dest, path_inside_chroot))
495 return strdup(b->src);
496
497 /*
498 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700499 * mount, take the suffix of the chroot path relative to the
500 * mount destination path, and append it to the mount source
501 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700502 */
503 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
504 const char *relative_path =
505 path_inside_chroot + strlen(b->dest);
506 return append_external_path(b->src, relative_path);
507 }
508 b = b->next;
509 }
510
511 /* If there is a chroot path, append |path_inside_chroot| to that. */
512 if (j->chrootdir)
513 return append_external_path(j->chrootdir, path_inside_chroot);
514
515 /* No chroot, so the path outside is the same as it is inside. */
516 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700517}
518
Lee Campbell11af0622014-05-22 12:36:04 -0700519void API minijail_mount_tmp(struct minijail *j)
520{
521 j->flags.mount_tmp = 1;
522}
523
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800524int API minijail_write_pid_file(struct minijail *j, const char *path)
525{
526 j->pid_file_path = strdup(path);
527 if (!j->pid_file_path)
528 return -ENOMEM;
529 j->flags.pid_file = 1;
530 return 0;
531}
532
Dylan Reid648b2202015-10-23 00:50:00 -0700533int API minijail_mount(struct minijail *j, const char *src, const char *dest,
534 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700535{
Dylan Reid648b2202015-10-23 00:50:00 -0700536 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400537
538 if (*dest != '/')
539 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700540 m = calloc(1, sizeof(*m));
541 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400542 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700543 m->dest = strdup(dest);
544 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400545 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700546 m->src = strdup(src);
547 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400548 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700549 m->type = strdup(type);
550 if (!m->type)
551 goto error;
552 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400553
Dylan Reid648b2202015-10-23 00:50:00 -0700554 info("mount %s -> %s type %s", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400555
Elly Jonesdd3e8512012-01-23 15:13:38 -0500556 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700557 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400558 * containing vfs namespace.
559 */
560 minijail_namespace_vfs(j);
561
Dylan Reid648b2202015-10-23 00:50:00 -0700562 if (j->mounts_tail)
563 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400564 else
Dylan Reid648b2202015-10-23 00:50:00 -0700565 j->mounts_head = m;
566 j->mounts_tail = m;
567 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400568
569 return 0;
570
571error:
Dylan Reid648b2202015-10-23 00:50:00 -0700572 free(m->src);
573 free(m->dest);
574 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400575 return -ENOMEM;
576}
577
Dylan Reid648b2202015-10-23 00:50:00 -0700578int API minijail_bind(struct minijail *j, const char *src, const char *dest,
579 int writeable)
580{
581 unsigned long flags = MS_BIND;
582
583 if (!writeable)
584 flags |= MS_RDONLY;
585
586 return minijail_mount(j, src, dest, "", flags);
587}
588
Will Drewry6ac91122011-10-21 16:38:58 -0500589void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400590{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700591 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800592 if ((errno == EINVAL) && can_softfail()) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800593 warn("not loading seccomp filter,"
594 " seccomp not supported");
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800595 j->flags.seccomp_filter = 0;
596 j->flags.log_seccomp_filter = 0;
597 j->filter_len = 0;
598 j->filter_prog = NULL;
599 j->flags.no_new_privs = 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700600 }
601 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400602 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800603 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700604 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400605 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800606
607 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700608 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
609 die("failed to compile seccomp filter BPF program in '%s'",
610 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800611 }
612
613 j->filter_len = fprog->len;
614 j->filter_prog = fprog;
615
Elly Jonese1749eb2011-10-07 13:54:59 -0400616 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500617}
618
Andrew Brestickereac28942015-11-11 16:04:46 -0800619int API minijail_use_alt_syscall(struct minijail *j, const char *table)
620{
621 j->alt_syscall_table = strdup(table);
622 if (!j->alt_syscall_table)
623 return -ENOMEM;
624 j->flags.alt_syscall = 1;
625 return 0;
626}
627
Will Drewryf89aef52011-09-16 16:48:57 -0500628struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400629 size_t available;
630 size_t total;
631 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500632};
633
Will Drewry6ac91122011-10-21 16:38:58 -0500634void marshal_state_init(struct marshal_state *state,
635 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400636{
637 state->available = available;
638 state->buf = buf;
639 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500640}
641
Will Drewry6ac91122011-10-21 16:38:58 -0500642void marshal_append(struct marshal_state *state,
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800643 void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400644{
645 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500646
Elly Jonese1749eb2011-10-07 13:54:59 -0400647 /* Up to |available| will be written. */
648 if (copy_len) {
649 memcpy(state->buf, src, copy_len);
650 state->buf += copy_len;
651 state->available -= copy_len;
652 }
653 /* |total| will contain the expected length. */
654 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500655}
656
Will Drewry6ac91122011-10-21 16:38:58 -0500657void minijail_marshal_helper(struct marshal_state *state,
658 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400659{
Dylan Reid648b2202015-10-23 00:50:00 -0700660 struct mountpoint *m = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400661 marshal_append(state, (char *)j, sizeof(*j));
662 if (j->user)
663 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800664 if (j->suppl_gid_list) {
665 marshal_append(state, j->suppl_gid_list,
666 j->suppl_gid_count * sizeof(gid_t));
667 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400668 if (j->chrootdir)
669 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800670 if (j->alt_syscall_table) {
671 marshal_append(state, j->alt_syscall_table,
672 strlen(j->alt_syscall_table) + 1);
673 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800674 if (j->flags.seccomp_filter && j->filter_prog) {
675 struct sock_fprog *fp = j->filter_prog;
676 marshal_append(state, (char *)fp->filter,
677 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400678 }
Dylan Reid648b2202015-10-23 00:50:00 -0700679 for (m = j->mounts_head; m; m = m->next) {
680 marshal_append(state, m->src, strlen(m->src) + 1);
681 marshal_append(state, m->dest, strlen(m->dest) + 1);
682 marshal_append(state, m->type, strlen(m->type) + 1);
683 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400684 }
Will Drewryf89aef52011-09-16 16:48:57 -0500685}
686
Will Drewry6ac91122011-10-21 16:38:58 -0500687size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400688{
689 struct marshal_state state;
690 marshal_state_init(&state, NULL, 0);
691 minijail_marshal_helper(&state, j);
692 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500693}
694
Elly Jonese1749eb2011-10-07 13:54:59 -0400695int minijail_marshal(const struct minijail *j, char *buf, size_t available)
696{
697 struct marshal_state state;
698 marshal_state_init(&state, buf, available);
699 minijail_marshal_helper(&state, j);
700 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500701}
702
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800703/*
704 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
Elly Jones51a5b6c2011-10-12 19:09:26 -0400705 * @length Number of bytes to consume
706 * @buf Buffer to consume from
707 * @buflength Size of @buf
708 *
709 * Returns a pointer to the base of the bytes, or NULL for errors.
710 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700711void *consumebytes(size_t length, char **buf, size_t *buflength)
712{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400713 char *p = *buf;
714 if (length > *buflength)
715 return NULL;
716 *buf += length;
717 *buflength -= length;
718 return p;
719}
720
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800721/*
722 * consumestr: consumes a C string from a buffer @buf of length @length
Elly Jones51a5b6c2011-10-12 19:09:26 -0400723 * @buf Buffer to consume
724 * @length Length of buffer
725 *
726 * Returns a pointer to the base of the string, or NULL for errors.
727 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700728char *consumestr(char **buf, size_t *buflength)
729{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400730 size_t len = strnlen(*buf, *buflength);
731 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700732 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400733 return NULL;
734 return consumebytes(len + 1, buf, buflength);
735}
736
Elly Jonese1749eb2011-10-07 13:54:59 -0400737int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
738{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800739 size_t i;
740 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500741 int ret = -EINVAL;
742
Elly Jonese1749eb2011-10-07 13:54:59 -0400743 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500744 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400745 memcpy((void *)j, serialized, sizeof(*j));
746 serialized += sizeof(*j);
747 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500748
Will Drewrybee7ba72011-10-21 20:47:01 -0500749 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700750 j->mounts_head = NULL;
751 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800752 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500753
Elly Jonese1749eb2011-10-07 13:54:59 -0400754 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400755 char *user = consumestr(&serialized, &length);
756 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500757 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400758 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500759 if (!j->user)
760 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400761 }
Will Drewryf89aef52011-09-16 16:48:57 -0500762
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800763 if (j->suppl_gid_list) { /* stale pointer */
764 if (j->suppl_gid_count > NGROUPS_MAX) {
765 goto bad_gid_list;
766 }
767 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
768 void *gid_list_bytes =
769 consumebytes(gid_list_size, &serialized, &length);
770 if (!gid_list_bytes)
771 goto bad_gid_list;
772
773 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
774 if (!j->suppl_gid_list)
775 goto bad_gid_list;
776
777 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
778 }
779
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400780 if (j->chrootdir) { /* stale pointer */
781 char *chrootdir = consumestr(&serialized, &length);
782 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500783 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400784 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500785 if (!j->chrootdir)
786 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400787 }
788
Andrew Brestickereac28942015-11-11 16:04:46 -0800789 if (j->alt_syscall_table) { /* stale pointer */
790 char *alt_syscall_table = consumestr(&serialized, &length);
791 if (!alt_syscall_table)
792 goto bad_syscall_table;
793 j->alt_syscall_table = strdup(alt_syscall_table);
794 if (!j->alt_syscall_table)
795 goto bad_syscall_table;
796 }
797
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800798 if (j->flags.seccomp_filter && j->filter_len > 0) {
799 size_t ninstrs = j->filter_len;
800 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
801 ninstrs > USHRT_MAX)
802 goto bad_filters;
803
804 size_t program_len = ninstrs * sizeof(struct sock_filter);
805 void *program = consumebytes(program_len, &serialized, &length);
806 if (!program)
807 goto bad_filters;
808
809 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800810 if (!j->filter_prog)
811 goto bad_filters;
812
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800813 j->filter_prog->len = ninstrs;
814 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800815 if (!j->filter_prog->filter)
816 goto bad_filter_prog_instrs;
817
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800818 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400819 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400820
Dylan Reid648b2202015-10-23 00:50:00 -0700821 count = j->mounts_count;
822 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400823 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700824 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400825 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700826 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400827 const char *src = consumestr(&serialized, &length);
828 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700829 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400830 dest = consumestr(&serialized, &length);
831 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700832 goto bad_mounts;
833 type = consumestr(&serialized, &length);
834 if (!type)
835 goto bad_mounts;
836 flags = consumebytes(sizeof(*flags), &serialized, &length);
837 if (!flags)
838 goto bad_mounts;
839 if (minijail_mount(j, src, dest, type, *flags))
840 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400841 }
842
Elly Jonese1749eb2011-10-07 13:54:59 -0400843 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500844
Dylan Reid648b2202015-10-23 00:50:00 -0700845bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800846 if (j->flags.seccomp_filter && j->filter_len > 0) {
847 free(j->filter_prog->filter);
848 free(j->filter_prog);
849 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800850bad_filter_prog_instrs:
851 if (j->filter_prog)
852 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -0500853bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800854 if (j->alt_syscall_table)
855 free(j->alt_syscall_table);
856bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500857 if (j->chrootdir)
858 free(j->chrootdir);
859bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800860 if (j->suppl_gid_list)
861 free(j->suppl_gid_list);
862bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -0500863 if (j->user)
864 free(j->user);
865clear_pointers:
866 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800867 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500868 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800869 j->alt_syscall_table = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500870out:
871 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500872}
873
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800874static void write_ugid_mappings(const struct minijail *j, int *pipe_fds)
875{
876 int fd, ret, len;
877 size_t sz;
878 char fname[32];
879 close(pipe_fds[0]);
880
881 sz = sizeof(fname);
882 if (j->uidmap) {
883 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700884 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800885 die("failed to write file name of uid_map");
886 fd = open(fname, O_WRONLY);
887 if (fd < 0)
888 pdie("failed to open '%s'", fname);
889 len = strlen(j->uidmap);
890 if (write(fd, j->uidmap, len) < len)
891 die("failed to set uid_map");
892 close(fd);
893 }
894 if (j->gidmap) {
895 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700896 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800897 die("failed to write file name of gid_map");
898 fd = open(fname, O_WRONLY);
899 if (fd < 0)
900 pdie("failed to open '%s'", fname);
901 len = strlen(j->gidmap);
902 if (write(fd, j->gidmap, len) < len)
903 die("failed to set gid_map");
904 close(fd);
905 }
906
907 close(pipe_fds[1]);
908}
909
910static void enter_user_namespace(const struct minijail *j, int *pipe_fds)
911{
912 char buf;
913
914 close(pipe_fds[1]);
915
916 /* Wait for parent to set up uid/gid mappings. */
917 if (read(pipe_fds[0], &buf, 1) != 0)
918 die("failed to sync with parent");
919 close(pipe_fds[0]);
920
921 if (j->uidmap && setresuid(0, 0, 0))
922 pdie("setresuid");
923 if (j->gidmap && setresgid(0, 0, 0))
924 pdie("setresgid");
925}
926
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800927/*
928 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -0700929 * @j Minijail these mounts are for
930 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -0400931 *
932 * Returns 0 for success.
933 */
Dylan Reid648b2202015-10-23 00:50:00 -0700934static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700935{
Dylan Reid648b2202015-10-23 00:50:00 -0700936 int ret;
937 char *dest;
938 int remount_ro = 0;
939
Elly Jones51a5b6c2011-10-12 19:09:26 -0400940 /* dest has a leading "/" */
Dylan Reid648b2202015-10-23 00:50:00 -0700941 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400942 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700943
944 /*
945 * R/O bind mounts have to be remounted since bind and ro can't both be
946 * specified in the original bind mount. Remount R/O after the initial
947 * mount.
948 */
949 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
950 remount_ro = 1;
951 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -0500952 }
Dylan Reid648b2202015-10-23 00:50:00 -0700953
954 ret = mount(m->src, dest, m->type, m->flags, NULL);
955 if (ret)
956 pdie("mount: %s -> %s", m->src, dest);
957
958 if (remount_ro) {
959 m->flags |= MS_RDONLY;
960 ret = mount(m->src, dest, NULL,
961 m->flags | MS_REMOUNT, NULL);
962 if (ret)
963 pdie("bind ro: %s -> %s", m->src, dest);
964 }
965
Elly Jones51a5b6c2011-10-12 19:09:26 -0400966 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -0700967 if (m->next)
968 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400969 return ret;
970}
971
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700972int enter_chroot(const struct minijail *j)
973{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400974 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -0700975
976 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -0400977 return ret;
978
979 if (chroot(j->chrootdir))
980 return -errno;
981
982 if (chdir("/"))
983 return -errno;
984
985 return 0;
986}
987
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800988int enter_pivot_root(const struct minijail *j)
989{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800990 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -0700991
992 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800993 return ret;
994
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800995 /*
996 * Keep the fd for both old and new root.
997 * It will be used in fchdir later.
998 */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800999 oldroot = open("/", O_DIRECTORY | O_RDONLY);
1000 if (oldroot < 0)
1001 pdie("failed to open / for fchdir");
1002 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY);
1003 if (newroot < 0)
1004 pdie("failed to open %s for fchdir", j->chrootdir);
1005
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001006 /*
1007 * To ensure chrootdir is the root of a file system,
1008 * do a self bind mount.
1009 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001010 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1011 pdie("failed to bind mount '%s'", j->chrootdir);
1012 if (chdir(j->chrootdir))
1013 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001014 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001015 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001016
1017 /*
1018 * Now the old root is mounted on top of the new root. Use fchdir to
1019 * change to the old root and unmount it.
1020 */
1021 if (fchdir(oldroot))
1022 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001023 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001024 if (umount2(".", MNT_DETACH))
1025 pdie("umount(/)");
1026 /* Change back to the new root. */
1027 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001028 return -errno;
1029 if (chroot("/"))
1030 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001031 /* Set correct CWD for getcwd(3). */
1032 if (chdir("/"))
1033 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001034
1035 return 0;
1036}
1037
Lee Campbell11af0622014-05-22 12:36:04 -07001038int mount_tmp(void)
1039{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001040 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001041}
1042
Dylan Reid791f5772015-09-14 20:02:42 -07001043int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001044{
1045 const char *kProcPath = "/proc";
1046 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001047 /*
1048 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001049 * /proc in our namespace, which means using MS_REMOUNT here would
1050 * mutate our parent's mount as well, even though we're in a VFS
1051 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001052 * and make our own. However, if we are in a new user namespace, /proc
1053 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -04001054 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001055 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -04001056 return -errno;
1057 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1058 return -errno;
1059 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001060}
1061
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001062static void write_pid_file(const struct minijail *j)
1063{
1064 FILE *fp = fopen(j->pid_file_path, "w");
1065
1066 if (!fp)
1067 pdie("failed to open '%s'", j->pid_file_path);
1068 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
1069 pdie("fprintf(%s)", j->pid_file_path);
1070 if (fclose(fp))
1071 pdie("fclose(%s)", j->pid_file_path);
1072}
1073
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001074void drop_ugid(const struct minijail *j)
1075{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001076 if (j->flags.usergroups && j->flags.suppl_gids) {
1077 die("tried to inherit *and* set supplementary groups;"
1078 " can only do one");
1079 }
1080
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001081 if (j->flags.usergroups) {
1082 if (initgroups(j->user, j->usergid))
1083 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001084 } else if (j->flags.suppl_gids) {
1085 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1086 pdie("setgroups");
1087 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001088 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001089 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001090 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001091 * users.
1092 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001093 if ((j->uid || j->gid) && setgroups(0, NULL))
1094 pdie("setgroups");
1095 }
1096
1097 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1098 pdie("setresgid");
1099
1100 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1101 pdie("setresuid");
1102}
1103
Mike Frysinger3adfef72013-05-09 17:19:08 -04001104/*
1105 * We specifically do not use cap_valid() as that only tells us the last
1106 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001107 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001108 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001109 * kernel).
1110 * Normally, we suck up the answer via /proc. On Android, not all processes are
1111 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1112 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001113 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001114static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001115{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001116 unsigned int last_valid_cap = 0;
1117 if (is_android()) {
1118 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1119 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001120
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001121 /* |last_valid_cap| will be the first failing value. */
1122 if (last_valid_cap > 0) {
1123 last_valid_cap--;
1124 }
1125 } else {
1126 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1127 FILE *fp = fopen(cap_file, "re");
1128 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1129 pdie("fscanf(%s)", cap_file);
1130 fclose(fp);
1131 }
Dylan Reidf682d472015-09-17 21:39:07 -07001132 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001133}
1134
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001135void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001136{
1137 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001138 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001139 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001140 unsigned int i;
1141 if (!caps)
1142 die("can't get process caps");
1143 if (cap_clear_flag(caps, CAP_INHERITABLE))
1144 die("can't clear inheritable caps");
1145 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1146 die("can't clear effective caps");
1147 if (cap_clear_flag(caps, CAP_PERMITTED))
1148 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001149 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001150 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001151 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001152 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001153 flag[0] = i;
1154 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001155 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001156 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001157 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001158 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001159 die("can't add inheritable cap");
1160 }
1161 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001162 die("can't apply initial cleaned capset");
1163
1164 /*
1165 * Instead of dropping bounding set first, do it here in case
1166 * the caller had a more permissive bounding set which could
1167 * have been used above to raise a capability that wasn't already
1168 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1169 */
Dylan Reidf682d472015-09-17 21:39:07 -07001170 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -08001171 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -04001172 continue;
1173 if (prctl(PR_CAPBSET_DROP, i))
1174 pdie("prctl(PR_CAPBSET_DROP)");
1175 }
Kees Cook323878a2013-02-05 15:35:24 -08001176
1177 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001178 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001179 flag[0] = CAP_SETPCAP;
1180 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1181 die("can't clear effective cap");
1182 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1183 die("can't clear permitted cap");
1184 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1185 die("can't clear inheritable cap");
1186 }
1187
1188 if (cap_set_proc(caps))
1189 die("can't apply final cleaned capset");
1190
1191 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001192}
1193
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001194void set_seccomp_filter(const struct minijail *j)
1195{
1196 /*
1197 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1198 * in the kernel source tree for an explanation of the parameters.
1199 */
1200 if (j->flags.no_new_privs) {
1201 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1202 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1203 }
1204
1205 /*
1206 * If we're logging seccomp filter failures,
1207 * install the SIGSYS handler first.
1208 */
1209 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1210 if (install_sigsys_handler())
1211 pdie("install SIGSYS handler");
1212 warn("logging seccomp filter failures");
1213 }
1214
1215 /*
1216 * Install the syscall filter.
1217 */
1218 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001219 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1220 j->filter_prog)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001221 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001222 warn("seccomp not supported");
1223 return;
1224 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001225 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001226 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001227 }
1228}
1229
Will Drewry6ac91122011-10-21 16:38:58 -05001230void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001231{
Dylan Reidf682d472015-09-17 21:39:07 -07001232 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001233 * If we're dropping caps, get the last valid cap from /proc now,
1234 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001235 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001236 unsigned int last_valid_cap = 0;
1237 if (j->flags.caps)
1238 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001239
Elly Jonese1749eb2011-10-07 13:54:59 -04001240 if (j->flags.pids)
1241 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001242 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001243
Elly Jonese1749eb2011-10-07 13:54:59 -04001244 if (j->flags.usergroups && !j->user)
1245 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001246
Elly Jonesdd3e8512012-01-23 15:13:38 -05001247 /*
1248 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001249 * so we don't even try. If any of our operations fail, we abort() the
1250 * entire process.
1251 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001252 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1253 pdie("setns(CLONE_NEWNS)");
1254
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001255 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001256 if (unshare(CLONE_NEWNS))
1257 pdie("unshare(vfs)");
1258 /*
1259 * Remount all filesystems as private. If they are shared
1260 * new bind mounts will creep out of our namespace.
1261 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1262 */
1263 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1264 pdie("mount(/, private)");
1265 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001266
Dylan Reidf7942472015-11-18 17:55:26 -08001267 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1268 pdie("unshare(ipc)");
1269 }
1270
Dylan Reid1102f5a2015-09-15 11:52:20 -07001271 if (j->flags.enter_net) {
1272 if (setns(j->netns_fd, CLONE_NEWNET))
1273 pdie("setns(CLONE_NEWNET)");
1274 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001275 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001276 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001277
Elly Jones51a5b6c2011-10-12 19:09:26 -04001278 if (j->flags.chroot && enter_chroot(j))
1279 pdie("chroot");
1280
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001281 if (j->flags.pivot_root && enter_pivot_root(j))
1282 pdie("pivot_root");
1283
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001284 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001285 pdie("mount_tmp");
1286
Dylan Reid791f5772015-09-14 20:02:42 -07001287 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001288 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001289
Elly Jonese1749eb2011-10-07 13:54:59 -04001290 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001291 /*
1292 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001293 * capability to change uids, our attempt to use setuid()
1294 * below will fail. Hang on to root caps across setuid(), then
1295 * lock securebits.
1296 */
1297 if (prctl(PR_SET_KEEPCAPS, 1))
1298 pdie("prctl(PR_SET_KEEPCAPS)");
1299 if (prctl
1300 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1301 pdie("prctl(PR_SET_SECUREBITS)");
1302 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001303
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001304 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001305 * If we're setting no_new_privs, we can drop privileges
1306 * before setting seccomp filter. This way filter policies
1307 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001308 */
1309 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001310 drop_ugid(j);
1311 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001312 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001313
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001314 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001315 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001316 /*
1317 * If we're not setting no_new_privs,
1318 * we need to set seccomp filter *before* dropping privileges.
1319 * WARNING: this means that filter policies *must* allow
1320 * setgroups()/setresgid()/setresuid() for dropping root and
1321 * capget()/capset()/prctl() for dropping caps.
1322 */
1323 set_seccomp_filter(j);
1324
1325 drop_ugid(j);
1326 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001327 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001328 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001329
Elly Jonesdd3e8512012-01-23 15:13:38 -05001330 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001331 * Select the specified alternate syscall table. The table must not
1332 * block prctl(2) if we're using seccomp as well.
1333 */
1334 if (j->flags.alt_syscall) {
1335 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1336 pdie("prctl(PR_ALT_SYSCALL)");
1337 }
1338
1339 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001340 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001341 * privilege-dropping syscalls :)
1342 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001343 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001344 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001345 warn("seccomp not supported");
1346 return;
1347 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001348 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001349 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001350}
1351
Will Drewry6ac91122011-10-21 16:38:58 -05001352/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001353static int init_exitstatus = 0;
1354
Will Drewry6ac91122011-10-21 16:38:58 -05001355void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001356{
1357 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001358}
1359
Will Drewry6ac91122011-10-21 16:38:58 -05001360int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001361{
1362 pid_t pid;
1363 int status;
1364 /* so that we exit with the right status */
1365 signal(SIGTERM, init_term);
1366 /* TODO(wad) self jail with seccomp_filters here. */
1367 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001368 /*
1369 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001370 * left inside our pid namespace or we get a signal.
1371 */
1372 if (pid == rootpid)
1373 init_exitstatus = status;
1374 }
1375 if (!WIFEXITED(init_exitstatus))
1376 _exit(MINIJAIL_ERR_INIT);
1377 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001378}
1379
Will Drewry6ac91122011-10-21 16:38:58 -05001380int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001381{
1382 size_t sz = 0;
1383 size_t bytes = read(fd, &sz, sizeof(sz));
1384 char *buf;
1385 int r;
1386 if (sizeof(sz) != bytes)
1387 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001388 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001389 return -E2BIG;
1390 buf = malloc(sz);
1391 if (!buf)
1392 return -ENOMEM;
1393 bytes = read(fd, buf, sz);
1394 if (bytes != sz) {
1395 free(buf);
1396 return -EINVAL;
1397 }
1398 r = minijail_unmarshal(j, buf, sz);
1399 free(buf);
1400 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001401}
1402
Will Drewry6ac91122011-10-21 16:38:58 -05001403int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001404{
1405 char *buf;
1406 size_t sz = minijail_size(j);
1407 ssize_t written;
1408 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001409
Elly Jonese1749eb2011-10-07 13:54:59 -04001410 if (!sz)
1411 return -EINVAL;
1412 buf = malloc(sz);
1413 r = minijail_marshal(j, buf, sz);
1414 if (r) {
1415 free(buf);
1416 return r;
1417 }
1418 /* Sends [size][minijail]. */
1419 written = write(fd, &sz, sizeof(sz));
1420 if (written != sizeof(sz)) {
1421 free(buf);
1422 return -EFAULT;
1423 }
1424 written = write(fd, buf, sz);
1425 if (written < 0 || (size_t) written != sz) {
1426 free(buf);
1427 return -EFAULT;
1428 }
1429 free(buf);
1430 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001431}
Elly Jonescd7a9042011-07-22 13:56:51 -04001432
Will Drewry6ac91122011-10-21 16:38:58 -05001433int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001434{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001435#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001436 /* Don't use LDPRELOAD on Brillo. */
1437 return 0;
1438#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001439 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1440 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1441 if (!newenv)
1442 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001443
Elly Jonese1749eb2011-10-07 13:54:59 -04001444 /* Only insert a separating space if we have something to separate... */
1445 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1446 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001447
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001448 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001449 setenv(kLdPreloadEnvVar, newenv, 1);
1450 free(newenv);
1451 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001452#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001453}
1454
Will Drewry6ac91122011-10-21 16:38:58 -05001455int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001456{
1457 int r = pipe(fds);
1458 char fd_buf[11];
1459 if (r)
1460 return r;
1461 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1462 if (r <= 0)
1463 return -EINVAL;
1464 setenv(kFdEnvVar, fd_buf, 1);
1465 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001466}
1467
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001468int setup_pipe_end(int fds[2], size_t index)
1469{
1470 if (index > 1)
1471 return -1;
1472
1473 close(fds[1 - index]);
1474 return fds[index];
1475}
1476
1477int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1478{
1479 if (index > 1)
1480 return -1;
1481
1482 close(fds[1 - index]);
1483 /* dup2(2) the corresponding end of the pipe into |fd|. */
1484 return dup2(fds[index], fd);
1485}
1486
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001487int minijail_run_internal(struct minijail *j, const char *filename,
1488 char *const argv[], pid_t *pchild_pid,
1489 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1490 int use_preload);
1491
Will Drewry6ac91122011-10-21 16:38:58 -05001492int API minijail_run(struct minijail *j, const char *filename,
1493 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001494{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001495 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1496 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001497}
1498
1499int API minijail_run_pid(struct minijail *j, const char *filename,
1500 char *const argv[], pid_t *pchild_pid)
1501{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001502 return minijail_run_internal(j, filename, argv, pchild_pid,
1503 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001504}
1505
1506int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001507 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001508{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001509 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1510 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001511}
1512
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001513int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001514 char *const argv[], pid_t *pchild_pid,
1515 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001516{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001517 return minijail_run_internal(j, filename, argv, pchild_pid,
1518 pstdin_fd, pstdout_fd, pstderr_fd, true);
1519}
1520
1521int API minijail_run_no_preload(struct minijail *j, const char *filename,
1522 char *const argv[])
1523{
1524 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1525 false);
1526}
1527
Samuel Tan63187f42015-10-16 13:01:53 -07001528int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001529 const char *filename,
1530 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001531 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001532 int *pstdin_fd, int *pstdout_fd,
1533 int *pstderr_fd) {
Samuel Tan63187f42015-10-16 13:01:53 -07001534 return minijail_run_internal(j, filename, argv, pchild_pid,
1535 pstdin_fd, pstdout_fd, pstderr_fd, false);
1536}
1537
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001538int minijail_run_internal(struct minijail *j, const char *filename,
1539 char *const argv[], pid_t *pchild_pid,
1540 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1541 int use_preload)
1542{
Elly Jonese1749eb2011-10-07 13:54:59 -04001543 char *oldenv, *oldenv_copy = NULL;
1544 pid_t child_pid;
1545 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001546 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001547 int stdout_fds[2];
1548 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001549 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001550 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001551 /* We need to remember this across the minijail_preexec() call. */
1552 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001553 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001554
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001555 if (use_preload) {
1556 oldenv = getenv(kLdPreloadEnvVar);
1557 if (oldenv) {
1558 oldenv_copy = strdup(oldenv);
1559 if (!oldenv_copy)
1560 return -ENOMEM;
1561 }
1562
1563 if (setup_preload())
1564 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001565 }
Will Drewryf89aef52011-09-16 16:48:57 -05001566
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001567 if (!use_preload) {
1568 if (j->flags.caps)
1569 die("Capabilities are not supported without "
1570 "LD_PRELOAD");
1571 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001572
Elly Jonesdd3e8512012-01-23 15:13:38 -05001573 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001574 * Make the process group ID of this process equal to its PID, so that
1575 * both the Minijail process and the jailed process can be killed
1576 * together.
1577 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1578 * the process is already a process group leader.
1579 */
1580 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1581 if (errno != EPERM) {
1582 pdie("setpgid(0, 0)");
1583 }
1584 }
1585
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001586 if (use_preload) {
1587 /*
1588 * Before we fork(2) and execve(2) the child process, we need
1589 * to open a pipe(2) to send the minijail configuration over.
1590 */
1591 if (setup_pipe(pipe_fds))
1592 return -EFAULT;
1593 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001594
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001595 /*
1596 * If we want to write to the child process' standard input,
1597 * create the pipe(2) now.
1598 */
1599 if (pstdin_fd) {
1600 if (pipe(stdin_fds))
1601 return -EFAULT;
1602 }
1603
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001604 /*
1605 * If we want to read from the child process' standard output,
1606 * create the pipe(2) now.
1607 */
1608 if (pstdout_fd) {
1609 if (pipe(stdout_fds))
1610 return -EFAULT;
1611 }
1612
1613 /*
1614 * If we want to read from the child process' standard error,
1615 * create the pipe(2) now.
1616 */
1617 if (pstderr_fd) {
1618 if (pipe(stderr_fds))
1619 return -EFAULT;
1620 }
1621
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001622 /*
1623 * If we want to set up a new uid/gid mapping in the user namespace,
1624 * create the pipe(2) to sync between parent and child.
1625 */
1626 if (j->flags.userns) {
1627 if (pipe(userns_pipe_fds))
1628 return -EFAULT;
1629 }
1630
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001631 /*
1632 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001633 *
1634 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1635 *
1636 * In multithreaded programs, there are a bunch of locks inside libc,
1637 * some of which may be held by other threads at the time that we call
1638 * minijail_run_pid(). If we call fork(), glibc does its level best to
1639 * ensure that we hold all of these locks before it calls clone()
1640 * internally and drop them after clone() returns, but when we call
1641 * sys_clone(2) directly, all that gets bypassed and we end up with a
1642 * child address space where some of libc's important locks are held by
1643 * other threads (which did not get cloned, and hence will never release
1644 * those locks). This is okay so long as we call exec() immediately
1645 * after, but a bunch of seemingly-innocent libc functions like setenv()
1646 * take locks.
1647 *
1648 * Hence, only call sys_clone() if we need to, in order to get at pid
1649 * namespacing. If we follow this path, the child's address space might
1650 * have broken locks; you may only call functions that do not acquire
1651 * any locks.
1652 *
1653 * Unfortunately, fork() acquires every lock it can get its hands on, as
1654 * previously detailed, so this function is highly likely to deadlock
1655 * later on (see "deadlock here") if we're multithreaded.
1656 *
1657 * We might hack around this by having the clone()d child (init of the
1658 * pid namespace) return directly, rather than leaving the clone()d
1659 * process hanging around to be init for the new namespace (and having
1660 * its fork()ed child return in turn), but that process would be crippled
1661 * with its libc locks potentially broken. We might try fork()ing in the
1662 * parent before we clone() to ensure that we own all the locks, but
1663 * then we have to have the forked child hanging around consuming
1664 * resources (and possibly having file descriptors / shared memory
1665 * regions / etc attached). We'd need to keep the child around to avoid
1666 * having its children get reparented to init.
1667 *
1668 * TODO(ellyjones): figure out if the "forked child hanging around"
1669 * problem is fixable or not. It would be nice if we worked in this
1670 * case.
1671 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001672 if (pid_namespace) {
1673 int clone_flags = CLONE_NEWPID | SIGCHLD;
1674 if (j->flags.userns)
1675 clone_flags |= CLONE_NEWUSER;
1676 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001677 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001678 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001679 }
Elly Jones761b7412012-06-13 15:49:52 -04001680
Elly Jonese1749eb2011-10-07 13:54:59 -04001681 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001682 if (use_preload) {
1683 free(oldenv_copy);
1684 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001685 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001686 }
Will Drewryf89aef52011-09-16 16:48:57 -05001687
Elly Jonese1749eb2011-10-07 13:54:59 -04001688 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001689 if (use_preload) {
1690 /* Restore parent's LD_PRELOAD. */
1691 if (oldenv_copy) {
1692 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1693 free(oldenv_copy);
1694 } else {
1695 unsetenv(kLdPreloadEnvVar);
1696 }
1697 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001698 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001699
Elly Jonese1749eb2011-10-07 13:54:59 -04001700 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001701
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001702 if (j->flags.pid_file)
1703 write_pid_file(j);
1704
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001705 if (j->flags.userns)
1706 write_ugid_mappings(j, userns_pipe_fds);
1707
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001708 if (use_preload) {
1709 /* Send marshalled minijail. */
1710 close(pipe_fds[0]); /* read endpoint */
1711 ret = minijail_to_fd(j, pipe_fds[1]);
1712 close(pipe_fds[1]); /* write endpoint */
1713 if (ret) {
1714 kill(j->initpid, SIGKILL);
1715 die("failed to send marshalled minijail");
1716 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001717 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001718
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001719 if (pchild_pid)
1720 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001721
1722 /*
1723 * If we want to write to the child process' standard input,
1724 * set up the write end of the pipe.
1725 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001726 if (pstdin_fd)
1727 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001728 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001729
1730 /*
1731 * If we want to read from the child process' standard output,
1732 * set up the read end of the pipe.
1733 */
1734 if (pstdout_fd)
1735 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001736 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001737
1738 /*
1739 * If we want to read from the child process' standard error,
1740 * set up the read end of the pipe.
1741 */
1742 if (pstderr_fd)
1743 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001744 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001745
Elly Jonese1749eb2011-10-07 13:54:59 -04001746 return 0;
1747 }
1748 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001749
Peter Qiu2860c462015-12-16 15:13:06 -08001750 if (j->flags.reset_signal_mask) {
1751 sigset_t signal_mask;
1752 if (sigemptyset(&signal_mask) != 0)
1753 pdie("sigemptyset failed");
1754 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
1755 pdie("sigprocmask failed");
1756 }
1757
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001758 if (j->flags.userns)
1759 enter_user_namespace(j, userns_pipe_fds);
1760
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001761 /*
1762 * If we want to write to the jailed process' standard input,
1763 * set up the read end of the pipe.
1764 */
1765 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001766 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1767 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001768 die("failed to set up stdin pipe");
1769 }
1770
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001771 /*
1772 * If we want to read from the jailed process' standard output,
1773 * set up the write end of the pipe.
1774 */
1775 if (pstdout_fd) {
1776 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1777 STDOUT_FILENO) < 0)
1778 die("failed to set up stdout pipe");
1779 }
1780
1781 /*
1782 * If we want to read from the jailed process' standard error,
1783 * set up the write end of the pipe.
1784 */
1785 if (pstderr_fd) {
1786 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1787 STDERR_FILENO) < 0)
1788 die("failed to set up stderr pipe");
1789 }
1790
Dylan Reid791f5772015-09-14 20:02:42 -07001791 /* If running an init program, let it decide when/how to mount /proc. */
1792 if (pid_namespace && !do_init)
1793 j->flags.remount_proc_ro = 0;
1794
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001795 if (use_preload) {
1796 /* Strip out flags that cannot be inherited across execve(2). */
1797 minijail_preexec(j);
1798 } else {
1799 j->flags.pids = 0;
1800 }
1801 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001802 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001803
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001804 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001805 /*
1806 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001807 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001808 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001809 * a child to actually run the program. If |do_init == 0|, we
1810 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001811 *
1812 * If we're multithreaded, we'll probably deadlock here. See
1813 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001814 */
1815 child_pid = fork();
1816 if (child_pid < 0)
1817 _exit(child_pid);
1818 else if (child_pid > 0)
1819 init(child_pid); /* never returns */
1820 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001821
Elly Jonesdd3e8512012-01-23 15:13:38 -05001822 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001823 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001824 * calling process
1825 * -> execve()-ing process
1826 * If we are:
1827 * calling process
1828 * -> init()-ing process
1829 * -> execve()-ing process
1830 */
1831 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001832}
1833
Will Drewry6ac91122011-10-21 16:38:58 -05001834int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001835{
1836 int st;
1837 if (kill(j->initpid, SIGTERM))
1838 return -errno;
1839 if (waitpid(j->initpid, &st, 0) < 0)
1840 return -errno;
1841 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001842}
1843
Will Drewry6ac91122011-10-21 16:38:58 -05001844int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001845{
1846 int st;
1847 if (waitpid(j->initpid, &st, 0) < 0)
1848 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001849
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001850 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001851 int error_status = st;
1852 if (WIFSIGNALED(st)) {
1853 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001854 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001855 j->initpid, signum);
1856 /*
1857 * We return MINIJAIL_ERR_JAIL if the process received
1858 * SIGSYS, which happens when a syscall is blocked by
1859 * seccomp filters.
1860 * If not, we do what bash(1) does:
1861 * $? = 128 + signum
1862 */
1863 if (signum == SIGSYS) {
1864 error_status = MINIJAIL_ERR_JAIL;
1865 } else {
1866 error_status = 128 + signum;
1867 }
1868 }
1869 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001870 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001871
1872 int exit_status = WEXITSTATUS(st);
1873 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001874 info("child process %d exited with status %d",
1875 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001876
1877 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001878}
1879
Will Drewry6ac91122011-10-21 16:38:58 -05001880void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001881{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001882 if (j->flags.seccomp_filter && j->filter_prog) {
1883 free(j->filter_prog->filter);
1884 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001885 }
Dylan Reid648b2202015-10-23 00:50:00 -07001886 while (j->mounts_head) {
1887 struct mountpoint *m = j->mounts_head;
1888 j->mounts_head = j->mounts_head->next;
1889 free(m->type);
1890 free(m->dest);
1891 free(m->src);
1892 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001893 }
Dylan Reid648b2202015-10-23 00:50:00 -07001894 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001895 if (j->user)
1896 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08001897 if (j->suppl_gid_list)
1898 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05001899 if (j->chrootdir)
1900 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08001901 if (j->alt_syscall_table)
1902 free(j->alt_syscall_table);
Elly Jonese1749eb2011-10-07 13:54:59 -04001903 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001904}