blob: e1d5cb52b09f1c5fb7ec311179022083b6b586e0 [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
Dylan Reidce5b55e2016-01-13 11:04:16 -0800874static void write_ugid_mappings(const struct minijail *j)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800875{
876 int fd, ret, len;
877 size_t sz;
878 char fname[32];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800879
880 sz = sizeof(fname);
881 if (j->uidmap) {
882 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700883 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800884 die("failed to write file name of uid_map");
885 fd = open(fname, O_WRONLY);
886 if (fd < 0)
887 pdie("failed to open '%s'", fname);
888 len = strlen(j->uidmap);
889 if (write(fd, j->uidmap, len) < len)
890 die("failed to set uid_map");
891 close(fd);
892 }
893 if (j->gidmap) {
894 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700895 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800896 die("failed to write file name of gid_map");
897 fd = open(fname, O_WRONLY);
898 if (fd < 0)
899 pdie("failed to open '%s'", fname);
900 len = strlen(j->gidmap);
901 if (write(fd, j->gidmap, len) < len)
902 die("failed to set gid_map");
903 close(fd);
904 }
Dylan Reidce5b55e2016-01-13 11:04:16 -0800905}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800906
Dylan Reidce5b55e2016-01-13 11:04:16 -0800907static void parent_setup_complete(int *pipe_fds)
908{
909 close(pipe_fds[0]);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800910 close(pipe_fds[1]);
911}
912
Dylan Reidce5b55e2016-01-13 11:04:16 -0800913/*
914 * wait_for_parent_setup: Called by the child process to wait for any
915 * further parent-side setup to complete before continuing.
916 */
917static void wait_for_parent_setup(int *pipe_fds)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800918{
919 char buf;
920
921 close(pipe_fds[1]);
922
Dylan Reidce5b55e2016-01-13 11:04:16 -0800923 /* Wait for parent to complete setup and close the pipe. */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800924 if (read(pipe_fds[0], &buf, 1) != 0)
925 die("failed to sync with parent");
926 close(pipe_fds[0]);
Dylan Reidce5b55e2016-01-13 11:04:16 -0800927}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800928
Dylan Reidce5b55e2016-01-13 11:04:16 -0800929static void enter_user_namespace(const struct minijail *j)
930{
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800931 if (j->uidmap && setresuid(0, 0, 0))
932 pdie("setresuid");
933 if (j->gidmap && setresgid(0, 0, 0))
934 pdie("setresgid");
935}
936
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800937/*
938 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -0700939 * @j Minijail these mounts are for
940 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -0400941 *
942 * Returns 0 for success.
943 */
Dylan Reid648b2202015-10-23 00:50:00 -0700944static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700945{
Dylan Reid648b2202015-10-23 00:50:00 -0700946 int ret;
947 char *dest;
948 int remount_ro = 0;
949
Elly Jones51a5b6c2011-10-12 19:09:26 -0400950 /* dest has a leading "/" */
Dylan Reid648b2202015-10-23 00:50:00 -0700951 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400952 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700953
954 /*
955 * R/O bind mounts have to be remounted since bind and ro can't both be
956 * specified in the original bind mount. Remount R/O after the initial
957 * mount.
958 */
959 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
960 remount_ro = 1;
961 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -0500962 }
Dylan Reid648b2202015-10-23 00:50:00 -0700963
964 ret = mount(m->src, dest, m->type, m->flags, NULL);
965 if (ret)
966 pdie("mount: %s -> %s", m->src, dest);
967
968 if (remount_ro) {
969 m->flags |= MS_RDONLY;
970 ret = mount(m->src, dest, NULL,
971 m->flags | MS_REMOUNT, NULL);
972 if (ret)
973 pdie("bind ro: %s -> %s", m->src, dest);
974 }
975
Elly Jones51a5b6c2011-10-12 19:09:26 -0400976 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -0700977 if (m->next)
978 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400979 return ret;
980}
981
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700982int enter_chroot(const struct minijail *j)
983{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400984 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -0700985
986 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -0400987 return ret;
988
989 if (chroot(j->chrootdir))
990 return -errno;
991
992 if (chdir("/"))
993 return -errno;
994
995 return 0;
996}
997
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800998int enter_pivot_root(const struct minijail *j)
999{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001000 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001001
1002 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001003 return ret;
1004
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001005 /*
1006 * Keep the fd for both old and new root.
1007 * It will be used in fchdir later.
1008 */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001009 oldroot = open("/", O_DIRECTORY | O_RDONLY);
1010 if (oldroot < 0)
1011 pdie("failed to open / for fchdir");
1012 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY);
1013 if (newroot < 0)
1014 pdie("failed to open %s for fchdir", j->chrootdir);
1015
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001016 /*
1017 * To ensure chrootdir is the root of a file system,
1018 * do a self bind mount.
1019 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001020 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1021 pdie("failed to bind mount '%s'", j->chrootdir);
1022 if (chdir(j->chrootdir))
1023 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001024 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001025 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001026
1027 /*
1028 * Now the old root is mounted on top of the new root. Use fchdir to
1029 * change to the old root and unmount it.
1030 */
1031 if (fchdir(oldroot))
1032 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001033 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001034 if (umount2(".", MNT_DETACH))
1035 pdie("umount(/)");
1036 /* Change back to the new root. */
1037 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001038 return -errno;
1039 if (chroot("/"))
1040 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001041 /* Set correct CWD for getcwd(3). */
1042 if (chdir("/"))
1043 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001044
1045 return 0;
1046}
1047
Lee Campbell11af0622014-05-22 12:36:04 -07001048int mount_tmp(void)
1049{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001050 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001051}
1052
Dylan Reid791f5772015-09-14 20:02:42 -07001053int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001054{
1055 const char *kProcPath = "/proc";
1056 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001057 /*
1058 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001059 * /proc in our namespace, which means using MS_REMOUNT here would
1060 * mutate our parent's mount as well, even though we're in a VFS
1061 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001062 * and make our own. However, if we are in a new user namespace, /proc
1063 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -04001064 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001065 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -04001066 return -errno;
1067 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1068 return -errno;
1069 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001070}
1071
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001072static void write_pid_file(const struct minijail *j)
1073{
1074 FILE *fp = fopen(j->pid_file_path, "w");
1075
1076 if (!fp)
1077 pdie("failed to open '%s'", j->pid_file_path);
1078 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
1079 pdie("fprintf(%s)", j->pid_file_path);
1080 if (fclose(fp))
1081 pdie("fclose(%s)", j->pid_file_path);
1082}
1083
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001084void drop_ugid(const struct minijail *j)
1085{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001086 if (j->flags.usergroups && j->flags.suppl_gids) {
1087 die("tried to inherit *and* set supplementary groups;"
1088 " can only do one");
1089 }
1090
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001091 if (j->flags.usergroups) {
1092 if (initgroups(j->user, j->usergid))
1093 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001094 } else if (j->flags.suppl_gids) {
1095 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1096 pdie("setgroups");
1097 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001098 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001099 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001100 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001101 * users.
1102 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001103 if ((j->uid || j->gid) && setgroups(0, NULL))
1104 pdie("setgroups");
1105 }
1106
1107 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1108 pdie("setresgid");
1109
1110 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1111 pdie("setresuid");
1112}
1113
Mike Frysinger3adfef72013-05-09 17:19:08 -04001114/*
1115 * We specifically do not use cap_valid() as that only tells us the last
1116 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001117 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001118 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001119 * kernel).
1120 * Normally, we suck up the answer via /proc. On Android, not all processes are
1121 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1122 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001123 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001124static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001125{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001126 unsigned int last_valid_cap = 0;
1127 if (is_android()) {
1128 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1129 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001130
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001131 /* |last_valid_cap| will be the first failing value. */
1132 if (last_valid_cap > 0) {
1133 last_valid_cap--;
1134 }
1135 } else {
1136 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1137 FILE *fp = fopen(cap_file, "re");
1138 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1139 pdie("fscanf(%s)", cap_file);
1140 fclose(fp);
1141 }
Dylan Reidf682d472015-09-17 21:39:07 -07001142 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001143}
1144
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001145void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001146{
1147 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001148 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001149 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001150 unsigned int i;
1151 if (!caps)
1152 die("can't get process caps");
1153 if (cap_clear_flag(caps, CAP_INHERITABLE))
1154 die("can't clear inheritable caps");
1155 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1156 die("can't clear effective caps");
1157 if (cap_clear_flag(caps, CAP_PERMITTED))
1158 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001159 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001160 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001161 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001162 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001163 flag[0] = i;
1164 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001165 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001166 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001167 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001168 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001169 die("can't add inheritable cap");
1170 }
1171 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001172 die("can't apply initial cleaned capset");
1173
1174 /*
1175 * Instead of dropping bounding set first, do it here in case
1176 * the caller had a more permissive bounding set which could
1177 * have been used above to raise a capability that wasn't already
1178 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1179 */
Dylan Reidf682d472015-09-17 21:39:07 -07001180 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -08001181 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -04001182 continue;
1183 if (prctl(PR_CAPBSET_DROP, i))
1184 pdie("prctl(PR_CAPBSET_DROP)");
1185 }
Kees Cook323878a2013-02-05 15:35:24 -08001186
1187 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001188 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001189 flag[0] = CAP_SETPCAP;
1190 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1191 die("can't clear effective cap");
1192 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1193 die("can't clear permitted cap");
1194 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1195 die("can't clear inheritable cap");
1196 }
1197
1198 if (cap_set_proc(caps))
1199 die("can't apply final cleaned capset");
1200
1201 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001202}
1203
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001204void set_seccomp_filter(const struct minijail *j)
1205{
1206 /*
1207 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1208 * in the kernel source tree for an explanation of the parameters.
1209 */
1210 if (j->flags.no_new_privs) {
1211 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1212 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1213 }
1214
1215 /*
1216 * If we're logging seccomp filter failures,
1217 * install the SIGSYS handler first.
1218 */
1219 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1220 if (install_sigsys_handler())
1221 pdie("install SIGSYS handler");
1222 warn("logging seccomp filter failures");
1223 }
1224
1225 /*
1226 * Install the syscall filter.
1227 */
1228 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001229 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1230 j->filter_prog)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001231 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001232 warn("seccomp not supported");
1233 return;
1234 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001235 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001236 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001237 }
1238}
1239
Will Drewry6ac91122011-10-21 16:38:58 -05001240void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001241{
Dylan Reidf682d472015-09-17 21:39:07 -07001242 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001243 * If we're dropping caps, get the last valid cap from /proc now,
1244 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001245 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001246 unsigned int last_valid_cap = 0;
1247 if (j->flags.caps)
1248 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001249
Elly Jonese1749eb2011-10-07 13:54:59 -04001250 if (j->flags.pids)
1251 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001252 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001253
Elly Jonese1749eb2011-10-07 13:54:59 -04001254 if (j->flags.usergroups && !j->user)
1255 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001256
Elly Jonesdd3e8512012-01-23 15:13:38 -05001257 /*
1258 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001259 * so we don't even try. If any of our operations fail, we abort() the
1260 * entire process.
1261 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001262 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1263 pdie("setns(CLONE_NEWNS)");
1264
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001265 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001266 if (unshare(CLONE_NEWNS))
1267 pdie("unshare(vfs)");
1268 /*
1269 * Remount all filesystems as private. If they are shared
1270 * new bind mounts will creep out of our namespace.
1271 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1272 */
1273 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1274 pdie("mount(/, private)");
1275 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001276
Dylan Reidf7942472015-11-18 17:55:26 -08001277 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1278 pdie("unshare(ipc)");
1279 }
1280
Dylan Reid1102f5a2015-09-15 11:52:20 -07001281 if (j->flags.enter_net) {
1282 if (setns(j->netns_fd, CLONE_NEWNET))
1283 pdie("setns(CLONE_NEWNET)");
1284 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001285 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001286 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001287
Elly Jones51a5b6c2011-10-12 19:09:26 -04001288 if (j->flags.chroot && enter_chroot(j))
1289 pdie("chroot");
1290
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001291 if (j->flags.pivot_root && enter_pivot_root(j))
1292 pdie("pivot_root");
1293
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001294 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001295 pdie("mount_tmp");
1296
Dylan Reid791f5772015-09-14 20:02:42 -07001297 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001298 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001299
Elly Jonese1749eb2011-10-07 13:54:59 -04001300 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001301 /*
1302 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001303 * capability to change uids, our attempt to use setuid()
1304 * below will fail. Hang on to root caps across setuid(), then
1305 * lock securebits.
1306 */
1307 if (prctl(PR_SET_KEEPCAPS, 1))
1308 pdie("prctl(PR_SET_KEEPCAPS)");
1309 if (prctl
1310 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1311 pdie("prctl(PR_SET_SECUREBITS)");
1312 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001313
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001314 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001315 * If we're setting no_new_privs, we can drop privileges
1316 * before setting seccomp filter. This way filter policies
1317 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001318 */
1319 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001320 drop_ugid(j);
1321 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001322 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001323
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001324 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001325 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001326 /*
1327 * If we're not setting no_new_privs,
1328 * we need to set seccomp filter *before* dropping privileges.
1329 * WARNING: this means that filter policies *must* allow
1330 * setgroups()/setresgid()/setresuid() for dropping root and
1331 * capget()/capset()/prctl() for dropping caps.
1332 */
1333 set_seccomp_filter(j);
1334
1335 drop_ugid(j);
1336 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001337 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001338 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001339
Elly Jonesdd3e8512012-01-23 15:13:38 -05001340 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001341 * Select the specified alternate syscall table. The table must not
1342 * block prctl(2) if we're using seccomp as well.
1343 */
1344 if (j->flags.alt_syscall) {
1345 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1346 pdie("prctl(PR_ALT_SYSCALL)");
1347 }
1348
1349 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001350 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001351 * privilege-dropping syscalls :)
1352 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001353 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001354 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001355 warn("seccomp not supported");
1356 return;
1357 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001358 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001359 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001360}
1361
Will Drewry6ac91122011-10-21 16:38:58 -05001362/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001363static int init_exitstatus = 0;
1364
Will Drewry6ac91122011-10-21 16:38:58 -05001365void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001366{
1367 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001368}
1369
Will Drewry6ac91122011-10-21 16:38:58 -05001370int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001371{
1372 pid_t pid;
1373 int status;
1374 /* so that we exit with the right status */
1375 signal(SIGTERM, init_term);
1376 /* TODO(wad) self jail with seccomp_filters here. */
1377 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001378 /*
1379 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001380 * left inside our pid namespace or we get a signal.
1381 */
1382 if (pid == rootpid)
1383 init_exitstatus = status;
1384 }
1385 if (!WIFEXITED(init_exitstatus))
1386 _exit(MINIJAIL_ERR_INIT);
1387 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001388}
1389
Will Drewry6ac91122011-10-21 16:38:58 -05001390int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001391{
1392 size_t sz = 0;
1393 size_t bytes = read(fd, &sz, sizeof(sz));
1394 char *buf;
1395 int r;
1396 if (sizeof(sz) != bytes)
1397 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001398 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001399 return -E2BIG;
1400 buf = malloc(sz);
1401 if (!buf)
1402 return -ENOMEM;
1403 bytes = read(fd, buf, sz);
1404 if (bytes != sz) {
1405 free(buf);
1406 return -EINVAL;
1407 }
1408 r = minijail_unmarshal(j, buf, sz);
1409 free(buf);
1410 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001411}
1412
Will Drewry6ac91122011-10-21 16:38:58 -05001413int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001414{
1415 char *buf;
1416 size_t sz = minijail_size(j);
1417 ssize_t written;
1418 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001419
Elly Jonese1749eb2011-10-07 13:54:59 -04001420 if (!sz)
1421 return -EINVAL;
1422 buf = malloc(sz);
1423 r = minijail_marshal(j, buf, sz);
1424 if (r) {
1425 free(buf);
1426 return r;
1427 }
1428 /* Sends [size][minijail]. */
1429 written = write(fd, &sz, sizeof(sz));
1430 if (written != sizeof(sz)) {
1431 free(buf);
1432 return -EFAULT;
1433 }
1434 written = write(fd, buf, sz);
1435 if (written < 0 || (size_t) written != sz) {
1436 free(buf);
1437 return -EFAULT;
1438 }
1439 free(buf);
1440 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001441}
Elly Jonescd7a9042011-07-22 13:56:51 -04001442
Will Drewry6ac91122011-10-21 16:38:58 -05001443int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001444{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001445#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001446 /* Don't use LDPRELOAD on Brillo. */
1447 return 0;
1448#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001449 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1450 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1451 if (!newenv)
1452 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001453
Elly Jonese1749eb2011-10-07 13:54:59 -04001454 /* Only insert a separating space if we have something to separate... */
1455 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1456 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001457
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001458 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001459 setenv(kLdPreloadEnvVar, newenv, 1);
1460 free(newenv);
1461 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001462#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001463}
1464
Will Drewry6ac91122011-10-21 16:38:58 -05001465int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001466{
1467 int r = pipe(fds);
1468 char fd_buf[11];
1469 if (r)
1470 return r;
1471 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1472 if (r <= 0)
1473 return -EINVAL;
1474 setenv(kFdEnvVar, fd_buf, 1);
1475 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001476}
1477
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001478int setup_pipe_end(int fds[2], size_t index)
1479{
1480 if (index > 1)
1481 return -1;
1482
1483 close(fds[1 - index]);
1484 return fds[index];
1485}
1486
1487int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1488{
1489 if (index > 1)
1490 return -1;
1491
1492 close(fds[1 - index]);
1493 /* dup2(2) the corresponding end of the pipe into |fd|. */
1494 return dup2(fds[index], fd);
1495}
1496
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001497int minijail_run_internal(struct minijail *j, const char *filename,
1498 char *const argv[], pid_t *pchild_pid,
1499 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1500 int use_preload);
1501
Will Drewry6ac91122011-10-21 16:38:58 -05001502int API minijail_run(struct minijail *j, const char *filename,
1503 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001504{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001505 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1506 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001507}
1508
1509int API minijail_run_pid(struct minijail *j, const char *filename,
1510 char *const argv[], pid_t *pchild_pid)
1511{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001512 return minijail_run_internal(j, filename, argv, pchild_pid,
1513 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001514}
1515
1516int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001517 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001518{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001519 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1520 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001521}
1522
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001523int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001524 char *const argv[], pid_t *pchild_pid,
1525 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001526{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001527 return minijail_run_internal(j, filename, argv, pchild_pid,
1528 pstdin_fd, pstdout_fd, pstderr_fd, true);
1529}
1530
1531int API minijail_run_no_preload(struct minijail *j, const char *filename,
1532 char *const argv[])
1533{
1534 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1535 false);
1536}
1537
Samuel Tan63187f42015-10-16 13:01:53 -07001538int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001539 const char *filename,
1540 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001541 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001542 int *pstdin_fd, int *pstdout_fd,
1543 int *pstderr_fd) {
Samuel Tan63187f42015-10-16 13:01:53 -07001544 return minijail_run_internal(j, filename, argv, pchild_pid,
1545 pstdin_fd, pstdout_fd, pstderr_fd, false);
1546}
1547
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001548int minijail_run_internal(struct minijail *j, const char *filename,
1549 char *const argv[], pid_t *pchild_pid,
1550 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1551 int use_preload)
1552{
Elly Jonese1749eb2011-10-07 13:54:59 -04001553 char *oldenv, *oldenv_copy = NULL;
1554 pid_t child_pid;
1555 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001556 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001557 int stdout_fds[2];
1558 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001559 int child_sync_pipe_fds[2];
1560 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001561 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001562 /* We need to remember this across the minijail_preexec() call. */
1563 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001564 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001565
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001566 if (use_preload) {
1567 oldenv = getenv(kLdPreloadEnvVar);
1568 if (oldenv) {
1569 oldenv_copy = strdup(oldenv);
1570 if (!oldenv_copy)
1571 return -ENOMEM;
1572 }
1573
1574 if (setup_preload())
1575 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001576 }
Will Drewryf89aef52011-09-16 16:48:57 -05001577
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001578 if (!use_preload) {
1579 if (j->flags.caps)
1580 die("Capabilities are not supported without "
1581 "LD_PRELOAD");
1582 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001583
Elly Jonesdd3e8512012-01-23 15:13:38 -05001584 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001585 * Make the process group ID of this process equal to its PID, so that
1586 * both the Minijail process and the jailed process can be killed
1587 * together.
1588 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1589 * the process is already a process group leader.
1590 */
1591 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1592 if (errno != EPERM) {
1593 pdie("setpgid(0, 0)");
1594 }
1595 }
1596
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001597 if (use_preload) {
1598 /*
1599 * Before we fork(2) and execve(2) the child process, we need
1600 * to open a pipe(2) to send the minijail configuration over.
1601 */
1602 if (setup_pipe(pipe_fds))
1603 return -EFAULT;
1604 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001605
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001606 /*
1607 * If we want to write to the child process' standard input,
1608 * create the pipe(2) now.
1609 */
1610 if (pstdin_fd) {
1611 if (pipe(stdin_fds))
1612 return -EFAULT;
1613 }
1614
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001615 /*
1616 * If we want to read from the child process' standard output,
1617 * create the pipe(2) now.
1618 */
1619 if (pstdout_fd) {
1620 if (pipe(stdout_fds))
1621 return -EFAULT;
1622 }
1623
1624 /*
1625 * If we want to read from the child process' standard error,
1626 * create the pipe(2) now.
1627 */
1628 if (pstderr_fd) {
1629 if (pipe(stderr_fds))
1630 return -EFAULT;
1631 }
1632
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001633 /*
1634 * If we want to set up a new uid/gid mapping in the user namespace,
1635 * create the pipe(2) to sync between parent and child.
1636 */
1637 if (j->flags.userns) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001638 sync_child = 1;
1639 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001640 return -EFAULT;
1641 }
1642
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001643 /*
1644 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001645 *
1646 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1647 *
1648 * In multithreaded programs, there are a bunch of locks inside libc,
1649 * some of which may be held by other threads at the time that we call
1650 * minijail_run_pid(). If we call fork(), glibc does its level best to
1651 * ensure that we hold all of these locks before it calls clone()
1652 * internally and drop them after clone() returns, but when we call
1653 * sys_clone(2) directly, all that gets bypassed and we end up with a
1654 * child address space where some of libc's important locks are held by
1655 * other threads (which did not get cloned, and hence will never release
1656 * those locks). This is okay so long as we call exec() immediately
1657 * after, but a bunch of seemingly-innocent libc functions like setenv()
1658 * take locks.
1659 *
1660 * Hence, only call sys_clone() if we need to, in order to get at pid
1661 * namespacing. If we follow this path, the child's address space might
1662 * have broken locks; you may only call functions that do not acquire
1663 * any locks.
1664 *
1665 * Unfortunately, fork() acquires every lock it can get its hands on, as
1666 * previously detailed, so this function is highly likely to deadlock
1667 * later on (see "deadlock here") if we're multithreaded.
1668 *
1669 * We might hack around this by having the clone()d child (init of the
1670 * pid namespace) return directly, rather than leaving the clone()d
1671 * process hanging around to be init for the new namespace (and having
1672 * its fork()ed child return in turn), but that process would be crippled
1673 * with its libc locks potentially broken. We might try fork()ing in the
1674 * parent before we clone() to ensure that we own all the locks, but
1675 * then we have to have the forked child hanging around consuming
1676 * resources (and possibly having file descriptors / shared memory
1677 * regions / etc attached). We'd need to keep the child around to avoid
1678 * having its children get reparented to init.
1679 *
1680 * TODO(ellyjones): figure out if the "forked child hanging around"
1681 * problem is fixable or not. It would be nice if we worked in this
1682 * case.
1683 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001684 if (pid_namespace) {
1685 int clone_flags = CLONE_NEWPID | SIGCHLD;
1686 if (j->flags.userns)
1687 clone_flags |= CLONE_NEWUSER;
1688 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001689 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001690 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001691 }
Elly Jones761b7412012-06-13 15:49:52 -04001692
Elly Jonese1749eb2011-10-07 13:54:59 -04001693 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001694 if (use_preload) {
1695 free(oldenv_copy);
1696 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001697 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001698 }
Will Drewryf89aef52011-09-16 16:48:57 -05001699
Elly Jonese1749eb2011-10-07 13:54:59 -04001700 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001701 if (use_preload) {
1702 /* Restore parent's LD_PRELOAD. */
1703 if (oldenv_copy) {
1704 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1705 free(oldenv_copy);
1706 } else {
1707 unsetenv(kLdPreloadEnvVar);
1708 }
1709 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001710 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001711
Elly Jonese1749eb2011-10-07 13:54:59 -04001712 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001713
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001714 if (j->flags.pid_file)
1715 write_pid_file(j);
1716
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001717 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001718 write_ugid_mappings(j);
1719
1720 if (sync_child)
1721 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001722
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001723 if (use_preload) {
1724 /* Send marshalled minijail. */
1725 close(pipe_fds[0]); /* read endpoint */
1726 ret = minijail_to_fd(j, pipe_fds[1]);
1727 close(pipe_fds[1]); /* write endpoint */
1728 if (ret) {
1729 kill(j->initpid, SIGKILL);
1730 die("failed to send marshalled minijail");
1731 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001732 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001733
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001734 if (pchild_pid)
1735 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001736
1737 /*
1738 * If we want to write to the child process' standard input,
1739 * set up the write end of the pipe.
1740 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001741 if (pstdin_fd)
1742 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001743 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001744
1745 /*
1746 * If we want to read from the child process' standard output,
1747 * set up the read end of the pipe.
1748 */
1749 if (pstdout_fd)
1750 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001751 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001752
1753 /*
1754 * If we want to read from the child process' standard error,
1755 * set up the read end of the pipe.
1756 */
1757 if (pstderr_fd)
1758 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001759 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001760
Elly Jonese1749eb2011-10-07 13:54:59 -04001761 return 0;
1762 }
1763 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001764
Peter Qiu2860c462015-12-16 15:13:06 -08001765 if (j->flags.reset_signal_mask) {
1766 sigset_t signal_mask;
1767 if (sigemptyset(&signal_mask) != 0)
1768 pdie("sigemptyset failed");
1769 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
1770 pdie("sigprocmask failed");
1771 }
1772
Dylan Reidce5b55e2016-01-13 11:04:16 -08001773 if (sync_child)
1774 wait_for_parent_setup(child_sync_pipe_fds);
1775
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001776 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001777 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001778
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001779 /*
1780 * If we want to write to the jailed process' standard input,
1781 * set up the read end of the pipe.
1782 */
1783 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001784 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1785 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001786 die("failed to set up stdin pipe");
1787 }
1788
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001789 /*
1790 * If we want to read from the jailed process' standard output,
1791 * set up the write end of the pipe.
1792 */
1793 if (pstdout_fd) {
1794 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1795 STDOUT_FILENO) < 0)
1796 die("failed to set up stdout pipe");
1797 }
1798
1799 /*
1800 * If we want to read from the jailed process' standard error,
1801 * set up the write end of the pipe.
1802 */
1803 if (pstderr_fd) {
1804 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1805 STDERR_FILENO) < 0)
1806 die("failed to set up stderr pipe");
1807 }
1808
Dylan Reid791f5772015-09-14 20:02:42 -07001809 /* If running an init program, let it decide when/how to mount /proc. */
1810 if (pid_namespace && !do_init)
1811 j->flags.remount_proc_ro = 0;
1812
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001813 if (use_preload) {
1814 /* Strip out flags that cannot be inherited across execve(2). */
1815 minijail_preexec(j);
1816 } else {
1817 j->flags.pids = 0;
1818 }
1819 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001820 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001821
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001822 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001823 /*
1824 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001825 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001826 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001827 * a child to actually run the program. If |do_init == 0|, we
1828 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001829 *
1830 * If we're multithreaded, we'll probably deadlock here. See
1831 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001832 */
1833 child_pid = fork();
1834 if (child_pid < 0)
1835 _exit(child_pid);
1836 else if (child_pid > 0)
1837 init(child_pid); /* never returns */
1838 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001839
Elly Jonesdd3e8512012-01-23 15:13:38 -05001840 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001841 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001842 * calling process
1843 * -> execve()-ing process
1844 * If we are:
1845 * calling process
1846 * -> init()-ing process
1847 * -> execve()-ing process
1848 */
1849 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001850}
1851
Will Drewry6ac91122011-10-21 16:38:58 -05001852int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001853{
1854 int st;
1855 if (kill(j->initpid, SIGTERM))
1856 return -errno;
1857 if (waitpid(j->initpid, &st, 0) < 0)
1858 return -errno;
1859 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001860}
1861
Will Drewry6ac91122011-10-21 16:38:58 -05001862int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001863{
1864 int st;
1865 if (waitpid(j->initpid, &st, 0) < 0)
1866 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001867
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001868 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001869 int error_status = st;
1870 if (WIFSIGNALED(st)) {
1871 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001872 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001873 j->initpid, signum);
1874 /*
1875 * We return MINIJAIL_ERR_JAIL if the process received
1876 * SIGSYS, which happens when a syscall is blocked by
1877 * seccomp filters.
1878 * If not, we do what bash(1) does:
1879 * $? = 128 + signum
1880 */
1881 if (signum == SIGSYS) {
1882 error_status = MINIJAIL_ERR_JAIL;
1883 } else {
1884 error_status = 128 + signum;
1885 }
1886 }
1887 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001888 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001889
1890 int exit_status = WEXITSTATUS(st);
1891 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001892 info("child process %d exited with status %d",
1893 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001894
1895 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001896}
1897
Will Drewry6ac91122011-10-21 16:38:58 -05001898void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001899{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001900 if (j->flags.seccomp_filter && j->filter_prog) {
1901 free(j->filter_prog->filter);
1902 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001903 }
Dylan Reid648b2202015-10-23 00:50:00 -07001904 while (j->mounts_head) {
1905 struct mountpoint *m = j->mounts_head;
1906 j->mounts_head = j->mounts_head->next;
1907 free(m->type);
1908 free(m->dest);
1909 free(m->src);
1910 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001911 }
Dylan Reid648b2202015-10-23 00:50:00 -07001912 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001913 if (j->user)
1914 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08001915 if (j->suppl_gid_list)
1916 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05001917 if (j->chrootdir)
1918 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08001919 if (j->alt_syscall_table)
1920 free(j->alt_syscall_table);
Elly Jonese1749eb2011-10-07 13:54:59 -04001921 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001922}