blob: da2ea54b314c396dca52d56096a0da0daa40c294 [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
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800176/* Return true if kernel version is less than 3.8. */
177static int seccomp_kernel_support_not_required()
178{
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
186/*
187 * Allow softfail on Android devices with kernel version < 3.8.
188 */
189static int can_softfail()
190{
191#if SECCOMP_SOFTFAIL
192 if (is_android()) {
193 if (seccomp_kernel_support_not_required())
194 return 1;
195 else
196 return 0;
197 } else {
198 return 1;
199 }
200#endif
201 return 0;
202}
203
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700204/* Minijail API. */
205
Will Drewry6ac91122011-10-21 16:38:58 -0500206struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400207{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400208 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400209}
210
Will Drewry6ac91122011-10-21 16:38:58 -0500211void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400212{
213 if (uid == 0)
214 die("useless change to uid 0");
215 j->uid = uid;
216 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400217}
218
Will Drewry6ac91122011-10-21 16:38:58 -0500219void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400220{
221 if (gid == 0)
222 die("useless change to gid 0");
223 j->gid = gid;
224 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400225}
226
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800227int API minijail_set_supplementary_gids(struct minijail *j, size_t size,
228 const gid_t *list)
229{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800230 size_t i;
231
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800232 if (j->flags.usergroups)
233 die("cannot inherit *and* set supplementary groups");
234
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800235 if (size == 0) {
236 /* Clear supplementary groups. */
237 j->suppl_gid_list = NULL;
238 j->suppl_gid_count = 0;
239 j->flags.suppl_gids = 1;
240 return 0;
241 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800242
243 /* Copy the gid_t array. */
244 j->suppl_gid_list = calloc(size, sizeof(gid_t));
245 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800246 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800247 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800248 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800249 j->suppl_gid_list[i] = list[i];
250 }
251 j->suppl_gid_count = size;
252 j->flags.suppl_gids = 1;
253 return 0;
254}
255
Will Drewry6ac91122011-10-21 16:38:58 -0500256int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400257{
258 char *buf = NULL;
259 struct passwd pw;
260 struct passwd *ppw = NULL;
261 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
262 if (sz == -1)
263 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400264
Elly Jonesdd3e8512012-01-23 15:13:38 -0500265 /*
266 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400267 * the maximum needed size of the buffer, so we don't have to search.
268 */
269 buf = malloc(sz);
270 if (!buf)
271 return -ENOMEM;
272 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500273 /*
274 * We're safe to free the buffer here. The strings inside pw point
275 * inside buf, but we don't use any of them; this leaves the pointers
276 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
277 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400278 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700279 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400280 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700281 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400282 minijail_change_uid(j, ppw->pw_uid);
283 j->user = strdup(user);
284 if (!j->user)
285 return -ENOMEM;
286 j->usergid = ppw->pw_gid;
287 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400288}
289
Will Drewry6ac91122011-10-21 16:38:58 -0500290int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400291{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700292 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700293 struct group gr;
294 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400295 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
296 if (sz == -1)
297 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400298
Elly Jonesdd3e8512012-01-23 15:13:38 -0500299 /*
300 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400301 * the maximum needed size of the buffer, so we don't have to search.
302 */
303 buf = malloc(sz);
304 if (!buf)
305 return -ENOMEM;
306 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500307 /*
308 * We're safe to free the buffer here. The strings inside gr point
309 * inside buf, but we don't use any of them; this leaves the pointers
310 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
311 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400312 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700313 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400314 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700315 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400316 minijail_change_gid(j, pgr->gr_gid);
317 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400318}
319
Will Drewry6ac91122011-10-21 16:38:58 -0500320void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400321{
322 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400323}
324
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700325void API minijail_no_new_privs(struct minijail *j)
326{
327 j->flags.no_new_privs = 1;
328}
329
Will Drewry6ac91122011-10-21 16:38:58 -0500330void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400331{
332 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500333}
334
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700335void API minijail_log_seccomp_filter_failures(struct minijail *j)
336{
337 j->flags.log_seccomp_filter = 1;
338}
339
Will Drewry6ac91122011-10-21 16:38:58 -0500340void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400341{
342 j->caps = capmask;
343 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400344}
345
Peter Qiu2860c462015-12-16 15:13:06 -0800346void API minijail_reset_signal_mask(struct minijail* j) {
347 j->flags.reset_signal_mask = 1;
348}
349
Will Drewry6ac91122011-10-21 16:38:58 -0500350void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400351{
352 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400353}
354
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700355void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
356{
357 int ns_fd = open(ns_path, O_RDONLY);
358 if (ns_fd < 0) {
359 pdie("failed to open namespace '%s'", ns_path);
360 }
361 j->mountns_fd = ns_fd;
362 j->flags.enter_vfs = 1;
363}
364
Will Drewry6ac91122011-10-21 16:38:58 -0500365void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400366{
Elly Jonese58176c2012-01-23 11:46:17 -0500367 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700368 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400369 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800370 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400371}
372
Dylan Reidf7942472015-11-18 17:55:26 -0800373void API minijail_namespace_ipc(struct minijail *j)
374{
375 j->flags.ipc = 1;
376}
377
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400378void API minijail_namespace_net(struct minijail *j)
379{
380 j->flags.net = 1;
381}
382
Dylan Reid1102f5a2015-09-15 11:52:20 -0700383void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
384{
385 int ns_fd = open(ns_path, O_RDONLY);
386 if (ns_fd < 0) {
387 pdie("failed to open namespace '%s'", ns_path);
388 }
389 j->netns_fd = ns_fd;
390 j->flags.enter_net = 1;
391}
392
Dylan Reid791f5772015-09-14 20:02:42 -0700393void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400394{
395 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700396 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400397}
398
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800399void API minijail_namespace_user(struct minijail *j)
400{
401 j->flags.userns = 1;
402}
403
404int API minijail_uidmap(struct minijail *j, const char *uidmap)
405{
406 j->uidmap = strdup(uidmap);
407 if (!j->uidmap)
408 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800409 char *ch;
410 for (ch = j->uidmap; *ch; ch++) {
411 if (*ch == ',')
412 *ch = '\n';
413 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800414 return 0;
415}
416
417int API minijail_gidmap(struct minijail *j, const char *gidmap)
418{
419 j->gidmap = strdup(gidmap);
420 if (!j->gidmap)
421 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800422 char *ch;
423 for (ch = j->gidmap; *ch; ch++) {
424 if (*ch == ',')
425 *ch = '\n';
426 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800427 return 0;
428}
429
Will Drewry6ac91122011-10-21 16:38:58 -0500430void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400431{
432 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400433}
434
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800435void API minijail_run_as_init(struct minijail *j)
436{
437 /*
438 * Since the jailed program will become 'init' in the new PID namespace,
439 * Minijail does not need to fork an 'init' process.
440 */
441 j->flags.do_init = 0;
442}
443
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700444int API minijail_enter_chroot(struct minijail *j, const char *dir)
445{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400446 if (j->chrootdir)
447 return -EINVAL;
448 j->chrootdir = strdup(dir);
449 if (!j->chrootdir)
450 return -ENOMEM;
451 j->flags.chroot = 1;
452 return 0;
453}
454
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800455int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
456{
457 if (j->chrootdir)
458 return -EINVAL;
459 j->chrootdir = strdup(dir);
460 if (!j->chrootdir)
461 return -ENOMEM;
462 j->flags.pivot_root = 1;
463 return 0;
464}
465
Dylan Reida14e08d2015-10-22 21:05:29 -0700466static char *append_external_path(const char *external_path,
467 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700468{
Dylan Reida14e08d2015-10-22 21:05:29 -0700469 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700470 size_t pathlen;
471
Dylan Reid08946cc2015-09-16 19:10:57 -0700472 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700473 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
474 path = malloc(pathlen);
475 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700476
Dylan Reida14e08d2015-10-22 21:05:29 -0700477 return path;
478}
479
480char API *minijail_get_original_path(struct minijail *j,
481 const char *path_inside_chroot)
482{
Dylan Reid648b2202015-10-23 00:50:00 -0700483 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700484
Dylan Reid648b2202015-10-23 00:50:00 -0700485 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700486 while (b) {
487 /*
488 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700489 * mount, then the original path is exactly the source of
490 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700491 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700492 * mount source = /some/path/exe, mount dest =
493 * /chroot/path/exe Then when getting the original path of
494 * "/chroot/path/exe", the source of that mount,
495 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700496 */
497 if (!strcmp(b->dest, path_inside_chroot))
498 return strdup(b->src);
499
500 /*
501 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700502 * mount, take the suffix of the chroot path relative to the
503 * mount destination path, and append it to the mount source
504 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700505 */
506 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
507 const char *relative_path =
508 path_inside_chroot + strlen(b->dest);
509 return append_external_path(b->src, relative_path);
510 }
511 b = b->next;
512 }
513
514 /* If there is a chroot path, append |path_inside_chroot| to that. */
515 if (j->chrootdir)
516 return append_external_path(j->chrootdir, path_inside_chroot);
517
518 /* No chroot, so the path outside is the same as it is inside. */
519 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700520}
521
Lee Campbell11af0622014-05-22 12:36:04 -0700522void API minijail_mount_tmp(struct minijail *j)
523{
524 j->flags.mount_tmp = 1;
525}
526
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800527int API minijail_write_pid_file(struct minijail *j, const char *path)
528{
529 j->pid_file_path = strdup(path);
530 if (!j->pid_file_path)
531 return -ENOMEM;
532 j->flags.pid_file = 1;
533 return 0;
534}
535
Dylan Reid648b2202015-10-23 00:50:00 -0700536int API minijail_mount(struct minijail *j, const char *src, const char *dest,
537 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700538{
Dylan Reid648b2202015-10-23 00:50:00 -0700539 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400540
541 if (*dest != '/')
542 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700543 m = calloc(1, sizeof(*m));
544 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400545 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700546 m->dest = strdup(dest);
547 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400548 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700549 m->src = strdup(src);
550 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400551 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700552 m->type = strdup(type);
553 if (!m->type)
554 goto error;
555 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400556
Dylan Reid648b2202015-10-23 00:50:00 -0700557 info("mount %s -> %s type %s", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400558
Elly Jonesdd3e8512012-01-23 15:13:38 -0500559 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700560 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400561 * containing vfs namespace.
562 */
563 minijail_namespace_vfs(j);
564
Dylan Reid648b2202015-10-23 00:50:00 -0700565 if (j->mounts_tail)
566 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400567 else
Dylan Reid648b2202015-10-23 00:50:00 -0700568 j->mounts_head = m;
569 j->mounts_tail = m;
570 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400571
572 return 0;
573
574error:
Dylan Reid648b2202015-10-23 00:50:00 -0700575 free(m->src);
576 free(m->dest);
577 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400578 return -ENOMEM;
579}
580
Dylan Reid648b2202015-10-23 00:50:00 -0700581int API minijail_bind(struct minijail *j, const char *src, const char *dest,
582 int writeable)
583{
584 unsigned long flags = MS_BIND;
585
586 if (!writeable)
587 flags |= MS_RDONLY;
588
589 return minijail_mount(j, src, dest, "", flags);
590}
591
Will Drewry6ac91122011-10-21 16:38:58 -0500592void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400593{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700594 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800595 if ((errno == EINVAL) && can_softfail()) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800596 warn("not loading seccomp filter,"
597 " seccomp not supported");
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800598 j->flags.seccomp_filter = 0;
599 j->flags.log_seccomp_filter = 0;
600 j->filter_len = 0;
601 j->filter_prog = NULL;
602 j->flags.no_new_privs = 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700603 }
604 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400605 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800606 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700607 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400608 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800609
610 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700611 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
612 die("failed to compile seccomp filter BPF program in '%s'",
613 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800614 }
615
616 j->filter_len = fprog->len;
617 j->filter_prog = fprog;
618
Elly Jonese1749eb2011-10-07 13:54:59 -0400619 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500620}
621
Andrew Brestickereac28942015-11-11 16:04:46 -0800622int API minijail_use_alt_syscall(struct minijail *j, const char *table)
623{
624 j->alt_syscall_table = strdup(table);
625 if (!j->alt_syscall_table)
626 return -ENOMEM;
627 j->flags.alt_syscall = 1;
628 return 0;
629}
630
Will Drewryf89aef52011-09-16 16:48:57 -0500631struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400632 size_t available;
633 size_t total;
634 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500635};
636
Will Drewry6ac91122011-10-21 16:38:58 -0500637void marshal_state_init(struct marshal_state *state,
638 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400639{
640 state->available = available;
641 state->buf = buf;
642 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500643}
644
Will Drewry6ac91122011-10-21 16:38:58 -0500645void marshal_append(struct marshal_state *state,
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800646 void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400647{
648 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500649
Elly Jonese1749eb2011-10-07 13:54:59 -0400650 /* Up to |available| will be written. */
651 if (copy_len) {
652 memcpy(state->buf, src, copy_len);
653 state->buf += copy_len;
654 state->available -= copy_len;
655 }
656 /* |total| will contain the expected length. */
657 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500658}
659
Will Drewry6ac91122011-10-21 16:38:58 -0500660void minijail_marshal_helper(struct marshal_state *state,
661 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400662{
Dylan Reid648b2202015-10-23 00:50:00 -0700663 struct mountpoint *m = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400664 marshal_append(state, (char *)j, sizeof(*j));
665 if (j->user)
666 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800667 if (j->suppl_gid_list) {
668 marshal_append(state, j->suppl_gid_list,
669 j->suppl_gid_count * sizeof(gid_t));
670 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400671 if (j->chrootdir)
672 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800673 if (j->alt_syscall_table) {
674 marshal_append(state, j->alt_syscall_table,
675 strlen(j->alt_syscall_table) + 1);
676 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800677 if (j->flags.seccomp_filter && j->filter_prog) {
678 struct sock_fprog *fp = j->filter_prog;
679 marshal_append(state, (char *)fp->filter,
680 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400681 }
Dylan Reid648b2202015-10-23 00:50:00 -0700682 for (m = j->mounts_head; m; m = m->next) {
683 marshal_append(state, m->src, strlen(m->src) + 1);
684 marshal_append(state, m->dest, strlen(m->dest) + 1);
685 marshal_append(state, m->type, strlen(m->type) + 1);
686 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400687 }
Will Drewryf89aef52011-09-16 16:48:57 -0500688}
689
Will Drewry6ac91122011-10-21 16:38:58 -0500690size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400691{
692 struct marshal_state state;
693 marshal_state_init(&state, NULL, 0);
694 minijail_marshal_helper(&state, j);
695 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500696}
697
Elly Jonese1749eb2011-10-07 13:54:59 -0400698int minijail_marshal(const struct minijail *j, char *buf, size_t available)
699{
700 struct marshal_state state;
701 marshal_state_init(&state, buf, available);
702 minijail_marshal_helper(&state, j);
703 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500704}
705
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800706/*
707 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
Elly Jones51a5b6c2011-10-12 19:09:26 -0400708 * @length Number of bytes to consume
709 * @buf Buffer to consume from
710 * @buflength Size of @buf
711 *
712 * Returns a pointer to the base of the bytes, or NULL for errors.
713 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700714void *consumebytes(size_t length, char **buf, size_t *buflength)
715{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400716 char *p = *buf;
717 if (length > *buflength)
718 return NULL;
719 *buf += length;
720 *buflength -= length;
721 return p;
722}
723
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800724/*
725 * consumestr: consumes a C string from a buffer @buf of length @length
Elly Jones51a5b6c2011-10-12 19:09:26 -0400726 * @buf Buffer to consume
727 * @length Length of buffer
728 *
729 * Returns a pointer to the base of the string, or NULL for errors.
730 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700731char *consumestr(char **buf, size_t *buflength)
732{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400733 size_t len = strnlen(*buf, *buflength);
734 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700735 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400736 return NULL;
737 return consumebytes(len + 1, buf, buflength);
738}
739
Elly Jonese1749eb2011-10-07 13:54:59 -0400740int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
741{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800742 size_t i;
743 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500744 int ret = -EINVAL;
745
Elly Jonese1749eb2011-10-07 13:54:59 -0400746 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500747 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400748 memcpy((void *)j, serialized, sizeof(*j));
749 serialized += sizeof(*j);
750 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500751
Will Drewrybee7ba72011-10-21 20:47:01 -0500752 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700753 j->mounts_head = NULL;
754 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800755 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500756
Elly Jonese1749eb2011-10-07 13:54:59 -0400757 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400758 char *user = consumestr(&serialized, &length);
759 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500760 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400761 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500762 if (!j->user)
763 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400764 }
Will Drewryf89aef52011-09-16 16:48:57 -0500765
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800766 if (j->suppl_gid_list) { /* stale pointer */
767 if (j->suppl_gid_count > NGROUPS_MAX) {
768 goto bad_gid_list;
769 }
770 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
771 void *gid_list_bytes =
772 consumebytes(gid_list_size, &serialized, &length);
773 if (!gid_list_bytes)
774 goto bad_gid_list;
775
776 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
777 if (!j->suppl_gid_list)
778 goto bad_gid_list;
779
780 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
781 }
782
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400783 if (j->chrootdir) { /* stale pointer */
784 char *chrootdir = consumestr(&serialized, &length);
785 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500786 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400787 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500788 if (!j->chrootdir)
789 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400790 }
791
Andrew Brestickereac28942015-11-11 16:04:46 -0800792 if (j->alt_syscall_table) { /* stale pointer */
793 char *alt_syscall_table = consumestr(&serialized, &length);
794 if (!alt_syscall_table)
795 goto bad_syscall_table;
796 j->alt_syscall_table = strdup(alt_syscall_table);
797 if (!j->alt_syscall_table)
798 goto bad_syscall_table;
799 }
800
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800801 if (j->flags.seccomp_filter && j->filter_len > 0) {
802 size_t ninstrs = j->filter_len;
803 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
804 ninstrs > USHRT_MAX)
805 goto bad_filters;
806
807 size_t program_len = ninstrs * sizeof(struct sock_filter);
808 void *program = consumebytes(program_len, &serialized, &length);
809 if (!program)
810 goto bad_filters;
811
812 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800813 if (!j->filter_prog)
814 goto bad_filters;
815
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800816 j->filter_prog->len = ninstrs;
817 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800818 if (!j->filter_prog->filter)
819 goto bad_filter_prog_instrs;
820
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800821 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400822 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400823
Dylan Reid648b2202015-10-23 00:50:00 -0700824 count = j->mounts_count;
825 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400826 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700827 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400828 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700829 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400830 const char *src = consumestr(&serialized, &length);
831 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700832 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400833 dest = consumestr(&serialized, &length);
834 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700835 goto bad_mounts;
836 type = consumestr(&serialized, &length);
837 if (!type)
838 goto bad_mounts;
839 flags = consumebytes(sizeof(*flags), &serialized, &length);
840 if (!flags)
841 goto bad_mounts;
842 if (minijail_mount(j, src, dest, type, *flags))
843 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400844 }
845
Elly Jonese1749eb2011-10-07 13:54:59 -0400846 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500847
Dylan Reid648b2202015-10-23 00:50:00 -0700848bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800849 if (j->flags.seccomp_filter && j->filter_len > 0) {
850 free(j->filter_prog->filter);
851 free(j->filter_prog);
852 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800853bad_filter_prog_instrs:
854 if (j->filter_prog)
855 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -0500856bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800857 if (j->alt_syscall_table)
858 free(j->alt_syscall_table);
859bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500860 if (j->chrootdir)
861 free(j->chrootdir);
862bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800863 if (j->suppl_gid_list)
864 free(j->suppl_gid_list);
865bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -0500866 if (j->user)
867 free(j->user);
868clear_pointers:
869 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800870 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500871 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800872 j->alt_syscall_table = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500873out:
874 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500875}
876
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800877static void write_ugid_mappings(const struct minijail *j, int *pipe_fds)
878{
879 int fd, ret, len;
880 size_t sz;
881 char fname[32];
882 close(pipe_fds[0]);
883
884 sz = sizeof(fname);
885 if (j->uidmap) {
886 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700887 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800888 die("failed to write file name of uid_map");
889 fd = open(fname, O_WRONLY);
890 if (fd < 0)
891 pdie("failed to open '%s'", fname);
892 len = strlen(j->uidmap);
893 if (write(fd, j->uidmap, len) < len)
894 die("failed to set uid_map");
895 close(fd);
896 }
897 if (j->gidmap) {
898 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700899 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800900 die("failed to write file name of gid_map");
901 fd = open(fname, O_WRONLY);
902 if (fd < 0)
903 pdie("failed to open '%s'", fname);
904 len = strlen(j->gidmap);
905 if (write(fd, j->gidmap, len) < len)
906 die("failed to set gid_map");
907 close(fd);
908 }
909
910 close(pipe_fds[1]);
911}
912
913static void enter_user_namespace(const struct minijail *j, int *pipe_fds)
914{
915 char buf;
916
917 close(pipe_fds[1]);
918
919 /* Wait for parent to set up uid/gid mappings. */
920 if (read(pipe_fds[0], &buf, 1) != 0)
921 die("failed to sync with parent");
922 close(pipe_fds[0]);
923
924 if (j->uidmap && setresuid(0, 0, 0))
925 pdie("setresuid");
926 if (j->gidmap && setresgid(0, 0, 0))
927 pdie("setresgid");
928}
929
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800930/*
931 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -0700932 * @j Minijail these mounts are for
933 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -0400934 *
935 * Returns 0 for success.
936 */
Dylan Reid648b2202015-10-23 00:50:00 -0700937static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700938{
Dylan Reid648b2202015-10-23 00:50:00 -0700939 int ret;
940 char *dest;
941 int remount_ro = 0;
942
Elly Jones51a5b6c2011-10-12 19:09:26 -0400943 /* dest has a leading "/" */
Dylan Reid648b2202015-10-23 00:50:00 -0700944 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400945 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700946
947 /*
948 * R/O bind mounts have to be remounted since bind and ro can't both be
949 * specified in the original bind mount. Remount R/O after the initial
950 * mount.
951 */
952 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
953 remount_ro = 1;
954 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -0500955 }
Dylan Reid648b2202015-10-23 00:50:00 -0700956
957 ret = mount(m->src, dest, m->type, m->flags, NULL);
958 if (ret)
959 pdie("mount: %s -> %s", m->src, dest);
960
961 if (remount_ro) {
962 m->flags |= MS_RDONLY;
963 ret = mount(m->src, dest, NULL,
964 m->flags | MS_REMOUNT, NULL);
965 if (ret)
966 pdie("bind ro: %s -> %s", m->src, dest);
967 }
968
Elly Jones51a5b6c2011-10-12 19:09:26 -0400969 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -0700970 if (m->next)
971 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400972 return ret;
973}
974
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700975int enter_chroot(const struct minijail *j)
976{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400977 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -0700978
979 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -0400980 return ret;
981
982 if (chroot(j->chrootdir))
983 return -errno;
984
985 if (chdir("/"))
986 return -errno;
987
988 return 0;
989}
990
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800991int enter_pivot_root(const struct minijail *j)
992{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800993 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -0700994
995 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800996 return ret;
997
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800998 /*
999 * Keep the fd for both old and new root.
1000 * It will be used in fchdir later.
1001 */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001002 oldroot = open("/", O_DIRECTORY | O_RDONLY);
1003 if (oldroot < 0)
1004 pdie("failed to open / for fchdir");
1005 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY);
1006 if (newroot < 0)
1007 pdie("failed to open %s for fchdir", j->chrootdir);
1008
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001009 /*
1010 * To ensure chrootdir is the root of a file system,
1011 * do a self bind mount.
1012 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001013 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1014 pdie("failed to bind mount '%s'", j->chrootdir);
1015 if (chdir(j->chrootdir))
1016 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001017 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001018 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001019
1020 /*
1021 * Now the old root is mounted on top of the new root. Use fchdir to
1022 * change to the old root and unmount it.
1023 */
1024 if (fchdir(oldroot))
1025 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001026 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001027 if (umount2(".", MNT_DETACH))
1028 pdie("umount(/)");
1029 /* Change back to the new root. */
1030 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001031 return -errno;
1032 if (chroot("/"))
1033 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001034 /* Set correct CWD for getcwd(3). */
1035 if (chdir("/"))
1036 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001037
1038 return 0;
1039}
1040
Lee Campbell11af0622014-05-22 12:36:04 -07001041int mount_tmp(void)
1042{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001043 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001044}
1045
Dylan Reid791f5772015-09-14 20:02:42 -07001046int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001047{
1048 const char *kProcPath = "/proc";
1049 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001050 /*
1051 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001052 * /proc in our namespace, which means using MS_REMOUNT here would
1053 * mutate our parent's mount as well, even though we're in a VFS
1054 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001055 * and make our own. However, if we are in a new user namespace, /proc
1056 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -04001057 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001058 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -04001059 return -errno;
1060 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1061 return -errno;
1062 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001063}
1064
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001065static void write_pid_file(const struct minijail *j)
1066{
1067 FILE *fp = fopen(j->pid_file_path, "w");
1068
1069 if (!fp)
1070 pdie("failed to open '%s'", j->pid_file_path);
1071 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
1072 pdie("fprintf(%s)", j->pid_file_path);
1073 if (fclose(fp))
1074 pdie("fclose(%s)", j->pid_file_path);
1075}
1076
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001077void drop_ugid(const struct minijail *j)
1078{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001079 if (j->flags.usergroups && j->flags.suppl_gids) {
1080 die("tried to inherit *and* set supplementary groups;"
1081 " can only do one");
1082 }
1083
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001084 if (j->flags.usergroups) {
1085 if (initgroups(j->user, j->usergid))
1086 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001087 } else if (j->flags.suppl_gids) {
1088 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1089 pdie("setgroups");
1090 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001091 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001092 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001093 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001094 * users.
1095 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001096 if ((j->uid || j->gid) && setgroups(0, NULL))
1097 pdie("setgroups");
1098 }
1099
1100 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1101 pdie("setresgid");
1102
1103 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1104 pdie("setresuid");
1105}
1106
Mike Frysinger3adfef72013-05-09 17:19:08 -04001107/*
1108 * We specifically do not use cap_valid() as that only tells us the last
1109 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001110 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001111 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001112 * kernel).
1113 * Normally, we suck up the answer via /proc. On Android, not all processes are
1114 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1115 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001116 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001117static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001118{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001119 unsigned int last_valid_cap = 0;
1120 if (is_android()) {
1121 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1122 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001123
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001124 /* |last_valid_cap| will be the first failing value. */
1125 if (last_valid_cap > 0) {
1126 last_valid_cap--;
1127 }
1128 } else {
1129 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1130 FILE *fp = fopen(cap_file, "re");
1131 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1132 pdie("fscanf(%s)", cap_file);
1133 fclose(fp);
1134 }
Dylan Reidf682d472015-09-17 21:39:07 -07001135 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001136}
1137
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001138void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001139{
1140 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001141 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001142 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001143 unsigned int i;
1144 if (!caps)
1145 die("can't get process caps");
1146 if (cap_clear_flag(caps, CAP_INHERITABLE))
1147 die("can't clear inheritable caps");
1148 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1149 die("can't clear effective caps");
1150 if (cap_clear_flag(caps, CAP_PERMITTED))
1151 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001152 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001153 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001154 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001155 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001156 flag[0] = i;
1157 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001158 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001159 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001160 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001161 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001162 die("can't add inheritable cap");
1163 }
1164 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001165 die("can't apply initial cleaned capset");
1166
1167 /*
1168 * Instead of dropping bounding set first, do it here in case
1169 * the caller had a more permissive bounding set which could
1170 * have been used above to raise a capability that wasn't already
1171 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1172 */
Dylan Reidf682d472015-09-17 21:39:07 -07001173 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -08001174 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -04001175 continue;
1176 if (prctl(PR_CAPBSET_DROP, i))
1177 pdie("prctl(PR_CAPBSET_DROP)");
1178 }
Kees Cook323878a2013-02-05 15:35:24 -08001179
1180 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001181 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001182 flag[0] = CAP_SETPCAP;
1183 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1184 die("can't clear effective cap");
1185 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1186 die("can't clear permitted cap");
1187 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1188 die("can't clear inheritable cap");
1189 }
1190
1191 if (cap_set_proc(caps))
1192 die("can't apply final cleaned capset");
1193
1194 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001195}
1196
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001197void set_seccomp_filter(const struct minijail *j)
1198{
1199 /*
1200 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1201 * in the kernel source tree for an explanation of the parameters.
1202 */
1203 if (j->flags.no_new_privs) {
1204 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1205 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1206 }
1207
1208 /*
1209 * If we're logging seccomp filter failures,
1210 * install the SIGSYS handler first.
1211 */
1212 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1213 if (install_sigsys_handler())
1214 pdie("install SIGSYS handler");
1215 warn("logging seccomp filter failures");
1216 }
1217
1218 /*
1219 * Install the syscall filter.
1220 */
1221 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001222 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1223 j->filter_prog)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001224 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001225 warn("seccomp not supported");
1226 return;
1227 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001228 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001229 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001230 }
1231}
1232
Will Drewry6ac91122011-10-21 16:38:58 -05001233void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001234{
Dylan Reidf682d472015-09-17 21:39:07 -07001235 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001236 * If we're dropping caps, get the last valid cap from /proc now,
1237 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001238 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001239 unsigned int last_valid_cap = 0;
1240 if (j->flags.caps)
1241 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001242
Elly Jonese1749eb2011-10-07 13:54:59 -04001243 if (j->flags.pids)
1244 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001245 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001246
Elly Jonese1749eb2011-10-07 13:54:59 -04001247 if (j->flags.usergroups && !j->user)
1248 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001249
Elly Jonesdd3e8512012-01-23 15:13:38 -05001250 /*
1251 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001252 * so we don't even try. If any of our operations fail, we abort() the
1253 * entire process.
1254 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001255 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1256 pdie("setns(CLONE_NEWNS)");
1257
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001258 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001259 if (unshare(CLONE_NEWNS))
1260 pdie("unshare(vfs)");
1261 /*
1262 * Remount all filesystems as private. If they are shared
1263 * new bind mounts will creep out of our namespace.
1264 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1265 */
1266 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1267 pdie("mount(/, private)");
1268 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001269
Dylan Reidf7942472015-11-18 17:55:26 -08001270 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1271 pdie("unshare(ipc)");
1272 }
1273
Dylan Reid1102f5a2015-09-15 11:52:20 -07001274 if (j->flags.enter_net) {
1275 if (setns(j->netns_fd, CLONE_NEWNET))
1276 pdie("setns(CLONE_NEWNET)");
1277 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001278 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001279 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001280
Elly Jones51a5b6c2011-10-12 19:09:26 -04001281 if (j->flags.chroot && enter_chroot(j))
1282 pdie("chroot");
1283
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001284 if (j->flags.pivot_root && enter_pivot_root(j))
1285 pdie("pivot_root");
1286
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001287 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001288 pdie("mount_tmp");
1289
Dylan Reid791f5772015-09-14 20:02:42 -07001290 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001291 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001292
Elly Jonese1749eb2011-10-07 13:54:59 -04001293 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001294 /*
1295 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001296 * capability to change uids, our attempt to use setuid()
1297 * below will fail. Hang on to root caps across setuid(), then
1298 * lock securebits.
1299 */
1300 if (prctl(PR_SET_KEEPCAPS, 1))
1301 pdie("prctl(PR_SET_KEEPCAPS)");
1302 if (prctl
1303 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1304 pdie("prctl(PR_SET_SECUREBITS)");
1305 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001306
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001307 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001308 * If we're setting no_new_privs, we can drop privileges
1309 * before setting seccomp filter. This way filter policies
1310 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001311 */
1312 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001313 drop_ugid(j);
1314 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001315 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001316
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001317 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001318 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001319 /*
1320 * If we're not setting no_new_privs,
1321 * we need to set seccomp filter *before* dropping privileges.
1322 * WARNING: this means that filter policies *must* allow
1323 * setgroups()/setresgid()/setresuid() for dropping root and
1324 * capget()/capset()/prctl() for dropping caps.
1325 */
1326 set_seccomp_filter(j);
1327
1328 drop_ugid(j);
1329 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001330 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001331 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001332
Elly Jonesdd3e8512012-01-23 15:13:38 -05001333 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001334 * Select the specified alternate syscall table. The table must not
1335 * block prctl(2) if we're using seccomp as well.
1336 */
1337 if (j->flags.alt_syscall) {
1338 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1339 pdie("prctl(PR_ALT_SYSCALL)");
1340 }
1341
1342 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001343 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001344 * privilege-dropping syscalls :)
1345 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001346 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001347 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001348 warn("seccomp not supported");
1349 return;
1350 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001351 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001352 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001353}
1354
Will Drewry6ac91122011-10-21 16:38:58 -05001355/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001356static int init_exitstatus = 0;
1357
Will Drewry6ac91122011-10-21 16:38:58 -05001358void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001359{
1360 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001361}
1362
Will Drewry6ac91122011-10-21 16:38:58 -05001363int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001364{
1365 pid_t pid;
1366 int status;
1367 /* so that we exit with the right status */
1368 signal(SIGTERM, init_term);
1369 /* TODO(wad) self jail with seccomp_filters here. */
1370 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001371 /*
1372 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001373 * left inside our pid namespace or we get a signal.
1374 */
1375 if (pid == rootpid)
1376 init_exitstatus = status;
1377 }
1378 if (!WIFEXITED(init_exitstatus))
1379 _exit(MINIJAIL_ERR_INIT);
1380 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001381}
1382
Will Drewry6ac91122011-10-21 16:38:58 -05001383int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001384{
1385 size_t sz = 0;
1386 size_t bytes = read(fd, &sz, sizeof(sz));
1387 char *buf;
1388 int r;
1389 if (sizeof(sz) != bytes)
1390 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001391 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001392 return -E2BIG;
1393 buf = malloc(sz);
1394 if (!buf)
1395 return -ENOMEM;
1396 bytes = read(fd, buf, sz);
1397 if (bytes != sz) {
1398 free(buf);
1399 return -EINVAL;
1400 }
1401 r = minijail_unmarshal(j, buf, sz);
1402 free(buf);
1403 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001404}
1405
Will Drewry6ac91122011-10-21 16:38:58 -05001406int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001407{
1408 char *buf;
1409 size_t sz = minijail_size(j);
1410 ssize_t written;
1411 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001412
Elly Jonese1749eb2011-10-07 13:54:59 -04001413 if (!sz)
1414 return -EINVAL;
1415 buf = malloc(sz);
1416 r = minijail_marshal(j, buf, sz);
1417 if (r) {
1418 free(buf);
1419 return r;
1420 }
1421 /* Sends [size][minijail]. */
1422 written = write(fd, &sz, sizeof(sz));
1423 if (written != sizeof(sz)) {
1424 free(buf);
1425 return -EFAULT;
1426 }
1427 written = write(fd, buf, sz);
1428 if (written < 0 || (size_t) written != sz) {
1429 free(buf);
1430 return -EFAULT;
1431 }
1432 free(buf);
1433 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001434}
Elly Jonescd7a9042011-07-22 13:56:51 -04001435
Will Drewry6ac91122011-10-21 16:38:58 -05001436int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001437{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001438#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001439 /* Don't use LDPRELOAD on Brillo. */
1440 return 0;
1441#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001442 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1443 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1444 if (!newenv)
1445 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001446
Elly Jonese1749eb2011-10-07 13:54:59 -04001447 /* Only insert a separating space if we have something to separate... */
1448 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1449 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001450
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001451 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001452 setenv(kLdPreloadEnvVar, newenv, 1);
1453 free(newenv);
1454 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001455#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001456}
1457
Will Drewry6ac91122011-10-21 16:38:58 -05001458int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001459{
1460 int r = pipe(fds);
1461 char fd_buf[11];
1462 if (r)
1463 return r;
1464 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1465 if (r <= 0)
1466 return -EINVAL;
1467 setenv(kFdEnvVar, fd_buf, 1);
1468 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001469}
1470
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001471int setup_pipe_end(int fds[2], size_t index)
1472{
1473 if (index > 1)
1474 return -1;
1475
1476 close(fds[1 - index]);
1477 return fds[index];
1478}
1479
1480int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1481{
1482 if (index > 1)
1483 return -1;
1484
1485 close(fds[1 - index]);
1486 /* dup2(2) the corresponding end of the pipe into |fd|. */
1487 return dup2(fds[index], fd);
1488}
1489
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001490int minijail_run_internal(struct minijail *j, const char *filename,
1491 char *const argv[], pid_t *pchild_pid,
1492 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1493 int use_preload);
1494
Will Drewry6ac91122011-10-21 16:38:58 -05001495int API minijail_run(struct minijail *j, const char *filename,
1496 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001497{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001498 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1499 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001500}
1501
1502int API minijail_run_pid(struct minijail *j, const char *filename,
1503 char *const argv[], pid_t *pchild_pid)
1504{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001505 return minijail_run_internal(j, filename, argv, pchild_pid,
1506 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001507}
1508
1509int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001510 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001511{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001512 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1513 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001514}
1515
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001516int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001517 char *const argv[], pid_t *pchild_pid,
1518 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001519{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001520 return minijail_run_internal(j, filename, argv, pchild_pid,
1521 pstdin_fd, pstdout_fd, pstderr_fd, true);
1522}
1523
1524int API minijail_run_no_preload(struct minijail *j, const char *filename,
1525 char *const argv[])
1526{
1527 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1528 false);
1529}
1530
Samuel Tan63187f42015-10-16 13:01:53 -07001531int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001532 const char *filename,
1533 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001534 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001535 int *pstdin_fd, int *pstdout_fd,
1536 int *pstderr_fd) {
Samuel Tan63187f42015-10-16 13:01:53 -07001537 return minijail_run_internal(j, filename, argv, pchild_pid,
1538 pstdin_fd, pstdout_fd, pstderr_fd, false);
1539}
1540
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001541int minijail_run_internal(struct minijail *j, const char *filename,
1542 char *const argv[], pid_t *pchild_pid,
1543 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1544 int use_preload)
1545{
Elly Jonese1749eb2011-10-07 13:54:59 -04001546 char *oldenv, *oldenv_copy = NULL;
1547 pid_t child_pid;
1548 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001549 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001550 int stdout_fds[2];
1551 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001552 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001553 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001554 /* We need to remember this across the minijail_preexec() call. */
1555 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001556 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001557
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001558 if (use_preload) {
1559 oldenv = getenv(kLdPreloadEnvVar);
1560 if (oldenv) {
1561 oldenv_copy = strdup(oldenv);
1562 if (!oldenv_copy)
1563 return -ENOMEM;
1564 }
1565
1566 if (setup_preload())
1567 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001568 }
Will Drewryf89aef52011-09-16 16:48:57 -05001569
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001570 if (!use_preload) {
1571 if (j->flags.caps)
1572 die("Capabilities are not supported without "
1573 "LD_PRELOAD");
1574 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001575
Elly Jonesdd3e8512012-01-23 15:13:38 -05001576 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001577 * Make the process group ID of this process equal to its PID, so that
1578 * both the Minijail process and the jailed process can be killed
1579 * together.
1580 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1581 * the process is already a process group leader.
1582 */
1583 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1584 if (errno != EPERM) {
1585 pdie("setpgid(0, 0)");
1586 }
1587 }
1588
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001589 if (use_preload) {
1590 /*
1591 * Before we fork(2) and execve(2) the child process, we need
1592 * to open a pipe(2) to send the minijail configuration over.
1593 */
1594 if (setup_pipe(pipe_fds))
1595 return -EFAULT;
1596 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001597
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001598 /*
1599 * If we want to write to the child process' standard input,
1600 * create the pipe(2) now.
1601 */
1602 if (pstdin_fd) {
1603 if (pipe(stdin_fds))
1604 return -EFAULT;
1605 }
1606
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001607 /*
1608 * If we want to read from the child process' standard output,
1609 * create the pipe(2) now.
1610 */
1611 if (pstdout_fd) {
1612 if (pipe(stdout_fds))
1613 return -EFAULT;
1614 }
1615
1616 /*
1617 * If we want to read from the child process' standard error,
1618 * create the pipe(2) now.
1619 */
1620 if (pstderr_fd) {
1621 if (pipe(stderr_fds))
1622 return -EFAULT;
1623 }
1624
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001625 /*
1626 * If we want to set up a new uid/gid mapping in the user namespace,
1627 * create the pipe(2) to sync between parent and child.
1628 */
1629 if (j->flags.userns) {
1630 if (pipe(userns_pipe_fds))
1631 return -EFAULT;
1632 }
1633
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001634 /*
1635 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001636 *
1637 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1638 *
1639 * In multithreaded programs, there are a bunch of locks inside libc,
1640 * some of which may be held by other threads at the time that we call
1641 * minijail_run_pid(). If we call fork(), glibc does its level best to
1642 * ensure that we hold all of these locks before it calls clone()
1643 * internally and drop them after clone() returns, but when we call
1644 * sys_clone(2) directly, all that gets bypassed and we end up with a
1645 * child address space where some of libc's important locks are held by
1646 * other threads (which did not get cloned, and hence will never release
1647 * those locks). This is okay so long as we call exec() immediately
1648 * after, but a bunch of seemingly-innocent libc functions like setenv()
1649 * take locks.
1650 *
1651 * Hence, only call sys_clone() if we need to, in order to get at pid
1652 * namespacing. If we follow this path, the child's address space might
1653 * have broken locks; you may only call functions that do not acquire
1654 * any locks.
1655 *
1656 * Unfortunately, fork() acquires every lock it can get its hands on, as
1657 * previously detailed, so this function is highly likely to deadlock
1658 * later on (see "deadlock here") if we're multithreaded.
1659 *
1660 * We might hack around this by having the clone()d child (init of the
1661 * pid namespace) return directly, rather than leaving the clone()d
1662 * process hanging around to be init for the new namespace (and having
1663 * its fork()ed child return in turn), but that process would be crippled
1664 * with its libc locks potentially broken. We might try fork()ing in the
1665 * parent before we clone() to ensure that we own all the locks, but
1666 * then we have to have the forked child hanging around consuming
1667 * resources (and possibly having file descriptors / shared memory
1668 * regions / etc attached). We'd need to keep the child around to avoid
1669 * having its children get reparented to init.
1670 *
1671 * TODO(ellyjones): figure out if the "forked child hanging around"
1672 * problem is fixable or not. It would be nice if we worked in this
1673 * case.
1674 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001675 if (pid_namespace) {
1676 int clone_flags = CLONE_NEWPID | SIGCHLD;
1677 if (j->flags.userns)
1678 clone_flags |= CLONE_NEWUSER;
1679 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001680 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001681 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001682 }
Elly Jones761b7412012-06-13 15:49:52 -04001683
Elly Jonese1749eb2011-10-07 13:54:59 -04001684 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001685 if (use_preload) {
1686 free(oldenv_copy);
1687 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001688 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001689 }
Will Drewryf89aef52011-09-16 16:48:57 -05001690
Elly Jonese1749eb2011-10-07 13:54:59 -04001691 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001692 if (use_preload) {
1693 /* Restore parent's LD_PRELOAD. */
1694 if (oldenv_copy) {
1695 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1696 free(oldenv_copy);
1697 } else {
1698 unsetenv(kLdPreloadEnvVar);
1699 }
1700 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001701 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001702
Elly Jonese1749eb2011-10-07 13:54:59 -04001703 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001704
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001705 if (j->flags.pid_file)
1706 write_pid_file(j);
1707
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001708 if (j->flags.userns)
1709 write_ugid_mappings(j, userns_pipe_fds);
1710
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001711 if (use_preload) {
1712 /* Send marshalled minijail. */
1713 close(pipe_fds[0]); /* read endpoint */
1714 ret = minijail_to_fd(j, pipe_fds[1]);
1715 close(pipe_fds[1]); /* write endpoint */
1716 if (ret) {
1717 kill(j->initpid, SIGKILL);
1718 die("failed to send marshalled minijail");
1719 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001720 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001721
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001722 if (pchild_pid)
1723 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001724
1725 /*
1726 * If we want to write to the child process' standard input,
1727 * set up the write end of the pipe.
1728 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001729 if (pstdin_fd)
1730 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001731 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001732
1733 /*
1734 * If we want to read from the child process' standard output,
1735 * set up the read end of the pipe.
1736 */
1737 if (pstdout_fd)
1738 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001739 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001740
1741 /*
1742 * If we want to read from the child process' standard error,
1743 * set up the read end of the pipe.
1744 */
1745 if (pstderr_fd)
1746 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001747 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001748
Elly Jonese1749eb2011-10-07 13:54:59 -04001749 return 0;
1750 }
1751 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001752
Peter Qiu2860c462015-12-16 15:13:06 -08001753 if (j->flags.reset_signal_mask) {
1754 sigset_t signal_mask;
1755 if (sigemptyset(&signal_mask) != 0)
1756 pdie("sigemptyset failed");
1757 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
1758 pdie("sigprocmask failed");
1759 }
1760
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001761 if (j->flags.userns)
1762 enter_user_namespace(j, userns_pipe_fds);
1763
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001764 /*
1765 * If we want to write to the jailed process' standard input,
1766 * set up the read end of the pipe.
1767 */
1768 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001769 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1770 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001771 die("failed to set up stdin pipe");
1772 }
1773
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001774 /*
1775 * If we want to read from the jailed process' standard output,
1776 * set up the write end of the pipe.
1777 */
1778 if (pstdout_fd) {
1779 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1780 STDOUT_FILENO) < 0)
1781 die("failed to set up stdout pipe");
1782 }
1783
1784 /*
1785 * If we want to read from the jailed process' standard error,
1786 * set up the write end of the pipe.
1787 */
1788 if (pstderr_fd) {
1789 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1790 STDERR_FILENO) < 0)
1791 die("failed to set up stderr pipe");
1792 }
1793
Dylan Reid791f5772015-09-14 20:02:42 -07001794 /* If running an init program, let it decide when/how to mount /proc. */
1795 if (pid_namespace && !do_init)
1796 j->flags.remount_proc_ro = 0;
1797
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001798 if (use_preload) {
1799 /* Strip out flags that cannot be inherited across execve(2). */
1800 minijail_preexec(j);
1801 } else {
1802 j->flags.pids = 0;
1803 }
1804 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001805 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001806
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001807 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001808 /*
1809 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001810 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001811 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001812 * a child to actually run the program. If |do_init == 0|, we
1813 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001814 *
1815 * If we're multithreaded, we'll probably deadlock here. See
1816 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001817 */
1818 child_pid = fork();
1819 if (child_pid < 0)
1820 _exit(child_pid);
1821 else if (child_pid > 0)
1822 init(child_pid); /* never returns */
1823 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001824
Elly Jonesdd3e8512012-01-23 15:13:38 -05001825 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001826 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001827 * calling process
1828 * -> execve()-ing process
1829 * If we are:
1830 * calling process
1831 * -> init()-ing process
1832 * -> execve()-ing process
1833 */
1834 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001835}
1836
Will Drewry6ac91122011-10-21 16:38:58 -05001837int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001838{
1839 int st;
1840 if (kill(j->initpid, SIGTERM))
1841 return -errno;
1842 if (waitpid(j->initpid, &st, 0) < 0)
1843 return -errno;
1844 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001845}
1846
Will Drewry6ac91122011-10-21 16:38:58 -05001847int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001848{
1849 int st;
1850 if (waitpid(j->initpid, &st, 0) < 0)
1851 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001852
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001853 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001854 int error_status = st;
1855 if (WIFSIGNALED(st)) {
1856 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001857 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001858 j->initpid, signum);
1859 /*
1860 * We return MINIJAIL_ERR_JAIL if the process received
1861 * SIGSYS, which happens when a syscall is blocked by
1862 * seccomp filters.
1863 * If not, we do what bash(1) does:
1864 * $? = 128 + signum
1865 */
1866 if (signum == SIGSYS) {
1867 error_status = MINIJAIL_ERR_JAIL;
1868 } else {
1869 error_status = 128 + signum;
1870 }
1871 }
1872 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001873 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001874
1875 int exit_status = WEXITSTATUS(st);
1876 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001877 info("child process %d exited with status %d",
1878 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001879
1880 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001881}
1882
Will Drewry6ac91122011-10-21 16:38:58 -05001883void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001884{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001885 if (j->flags.seccomp_filter && j->filter_prog) {
1886 free(j->filter_prog->filter);
1887 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001888 }
Dylan Reid648b2202015-10-23 00:50:00 -07001889 while (j->mounts_head) {
1890 struct mountpoint *m = j->mounts_head;
1891 j->mounts_head = j->mounts_head->next;
1892 free(m->type);
1893 free(m->dest);
1894 free(m->src);
1895 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001896 }
Dylan Reid648b2202015-10-23 00:50:00 -07001897 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001898 if (j->user)
1899 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08001900 if (j->suppl_gid_list)
1901 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05001902 if (j->chrootdir)
1903 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08001904 if (j->alt_syscall_table)
1905 free(j->alt_syscall_table);
Elly Jonese1749eb2011-10-07 13:54:59 -04001906 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001907}