blob: b300daf0cfc866d3904eb69b264fc9412aef87d9 [file] [log] [blame]
Jorge Lucangeli Obesd613ab22015-03-03 14:22:50 -08001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04002 * Use of this source code is governed by a BSD-style license that can be
Will Drewry32ac9f52011-08-18 21:36:27 -05003 * found in the LICENSE file.
4 */
Elly Jonescd7a9042011-07-22 13:56:51 -04005
6#define _BSD_SOURCE
Arthur Gautier7a569072016-04-23 17:25:20 +00007#define _DEFAULT_SOURCE
Elly Jonescd7a9042011-07-22 13:56:51 -04008#define _GNU_SOURCE
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07009
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080010#include <asm/unistd.h>
Will Drewry32ac9f52011-08-18 21:36:27 -050011#include <ctype.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040012#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070013#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040014#include <grp.h>
15#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050016#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040017#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040018#include <pwd.h>
19#include <sched.h>
20#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050021#include <stdarg.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070022#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080023#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <syscall.h>
28#include <sys/capability.h>
29#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050030#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040031#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070032#include <sys/stat.h>
33#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080034#include <sys/user.h>
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -080035#include <sys/utsname.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040036#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040037#include <unistd.h>
38
39#include "libminijail.h"
40#include "libminijail-private.h"
41
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070042#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080043#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070044#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080045
Lei Zhangeee31552012-10-17 21:27:10 -070046#ifdef HAVE_SECUREBITS_H
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070047# include <linux/securebits.h>
Lei Zhangeee31552012-10-17 21:27:10 -070048#else
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070049# define SECURE_ALL_BITS 0x55
50# define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
51#endif
52/* For kernels < 4.3. */
53#define OLD_SECURE_ALL_BITS 0x15
54#define OLD_SECURE_ALL_LOCKS (OLD_SECURE_ALL_BITS << 1)
55
56/*
57 * Assert the value of SECURE_ALL_BITS at compile-time.
58 * Brillo devices are currently compiled against 4.4 kernel headers. Kernel 4.3
59 * added a new securebit.
60 * When a new securebit is added, the new SECURE_ALL_BITS mask will return EPERM
61 * when used on older kernels. The compile-time assert will catch this situation
62 * at compile time.
63 */
64#ifdef __BRILLO__
65_Static_assert(SECURE_ALL_BITS == 0x55, "SECURE_ALL_BITS == 0x55.");
Lei Zhangeee31552012-10-17 21:27:10 -070066#endif
67
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070068/* Until these are reliably available in linux/prctl.h. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080069#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070070# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080071#endif
72
Andrew Brestickereac28942015-11-11 16:04:46 -080073#ifndef PR_ALT_SYSCALL
74# define PR_ALT_SYSCALL 0x43724f53
75#endif
76
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080077/* For seccomp_filter using BPF. */
78#ifndef PR_SET_NO_NEW_PRIVS
79# define PR_SET_NO_NEW_PRIVS 38
80#endif
81#ifndef SECCOMP_MODE_FILTER
82# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050083#endif
84
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070085#ifdef USE_SECCOMP_SOFTFAIL
86# define SECCOMP_SOFTFAIL 1
87#else
88# define SECCOMP_SOFTFAIL 0
89#endif
90
Dylan Reid4cbc2a52016-06-17 19:06:07 -070091/* New cgroup namespace might not be in linux-headers yet. */
92#ifndef CLONE_NEWCGROUP
93# define CLONE_NEWCGROUP 0x02000000
94#endif
95
Dylan Reid605ce7f2016-01-19 19:21:00 -080096#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
97
Dylan Reid648b2202015-10-23 00:50:00 -070098struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040099 char *src;
100 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700101 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700102 char *data;
103 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -0700104 unsigned long flags;
105 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400106};
107
Will Drewryf89aef52011-09-16 16:48:57 -0500108struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700109 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700110 * WARNING: if you add a flag here you need to make sure it's
111 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700112 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400113 struct {
114 int uid:1;
115 int gid:1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800116 int usergroups:1;
117 int suppl_gids:1;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800118 int use_caps:1;
119 int capbset_drop:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400120 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700121 int enter_vfs:1;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800122 int skip_remount_private:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400123 int pids:1;
Dylan Reidf7942472015-11-18 17:55:26 -0800124 int ipc:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400125 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700126 int enter_net:1;
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700127 int ns_cgroups:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800128 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400129 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -0700130 int remount_proc_ro:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700131 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400132 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700133 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400134 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800135 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700136 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800137 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800138 int pid_file:1;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800139 int cgroups:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800140 int alt_syscall:1;
Peter Qiu2860c462015-12-16 15:13:06 -0800141 int reset_signal_mask:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400142 } flags;
143 uid_t uid;
144 gid_t gid;
145 gid_t usergid;
146 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800147 size_t suppl_gid_count;
148 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400149 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800150 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400151 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700152 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700153 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400154 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800155 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800156 char *uidmap;
157 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800158 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800159 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800160 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700161 struct mountpoint *mounts_head;
162 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800163 size_t mounts_count;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800164 char *cgroups[MAX_CGROUPS];
165 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500166};
167
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700168/*
169 * Strip out flags meant for the parent.
170 * We keep things that are not inherited across execve(2) (e.g. capabilities),
171 * or are easier to set after execve(2) (e.g. seccomp filters).
172 */
173void minijail_preenter(struct minijail *j)
174{
175 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700176 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900177 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700178 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700179 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800180 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800181 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800182 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700183}
184
185/*
186 * Strip out flags meant for the child.
187 * We keep things that are inherited across execve(2).
188 */
189void minijail_preexec(struct minijail *j)
190{
191 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700192 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800193 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700194 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800195 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700196 if (j->user)
197 free(j->user);
198 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800199 if (j->suppl_gid_list)
200 free(j->suppl_gid_list);
201 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700202 memset(&j->flags, 0, sizeof(j->flags));
203 /* Now restore anything we meant to keep. */
204 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700205 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800206 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700207 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800208 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700209 /* Note, |pids| will already have been used before this call. */
210}
211
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800212/* Returns true if the kernel version is less than 3.8. */
213int seccomp_kernel_support_not_required()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800214{
215 int major, minor;
216 struct utsname uts;
217 return (uname(&uts) != -1 &&
218 sscanf(uts.release, "%d.%d", &major, &minor) == 2 &&
219 ((major < 3) || ((major == 3) && (minor < 8))));
220}
221
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800222/* Allow seccomp soft-fail on Android devices with kernel version < 3.8. */
223int can_softfail()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800224{
225#if SECCOMP_SOFTFAIL
226 if (is_android()) {
227 if (seccomp_kernel_support_not_required())
228 return 1;
229 else
230 return 0;
231 } else {
232 return 1;
233 }
234#endif
235 return 0;
236}
237
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700238/* Minijail API. */
239
Will Drewry6ac91122011-10-21 16:38:58 -0500240struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400241{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400242 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400243}
244
Will Drewry6ac91122011-10-21 16:38:58 -0500245void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400246{
247 if (uid == 0)
248 die("useless change to uid 0");
249 j->uid = uid;
250 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400251}
252
Will Drewry6ac91122011-10-21 16:38:58 -0500253void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400254{
255 if (gid == 0)
256 die("useless change to gid 0");
257 j->gid = gid;
258 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400259}
260
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800261void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
262 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800263{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800264 size_t i;
265
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800266 if (j->flags.usergroups)
267 die("cannot inherit *and* set supplementary groups");
268
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800269 if (size == 0) {
270 /* Clear supplementary groups. */
271 j->suppl_gid_list = NULL;
272 j->suppl_gid_count = 0;
273 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800274 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800275 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800276
277 /* Copy the gid_t array. */
278 j->suppl_gid_list = calloc(size, sizeof(gid_t));
279 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800280 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800281 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800282 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800283 j->suppl_gid_list[i] = list[i];
284 }
285 j->suppl_gid_count = size;
286 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800287}
288
Will Drewry6ac91122011-10-21 16:38:58 -0500289int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400290{
291 char *buf = NULL;
292 struct passwd pw;
293 struct passwd *ppw = NULL;
294 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
295 if (sz == -1)
296 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400297
Elly Jonesdd3e8512012-01-23 15:13:38 -0500298 /*
299 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400300 * the maximum needed size of the buffer, so we don't have to search.
301 */
302 buf = malloc(sz);
303 if (!buf)
304 return -ENOMEM;
305 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500306 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800307 * We're safe to free the buffer here. The strings inside |pw| point
308 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800309 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
310 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500311 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400312 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700313 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400314 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700315 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400316 minijail_change_uid(j, ppw->pw_uid);
317 j->user = strdup(user);
318 if (!j->user)
319 return -ENOMEM;
320 j->usergid = ppw->pw_gid;
321 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400322}
323
Will Drewry6ac91122011-10-21 16:38:58 -0500324int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400325{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700326 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700327 struct group gr;
328 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400329 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
330 if (sz == -1)
331 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400332
Elly Jonesdd3e8512012-01-23 15:13:38 -0500333 /*
334 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400335 * the maximum needed size of the buffer, so we don't have to search.
336 */
337 buf = malloc(sz);
338 if (!buf)
339 return -ENOMEM;
340 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500341 /*
342 * We're safe to free the buffer here. The strings inside gr point
343 * inside buf, but we don't use any of them; this leaves the pointers
344 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
345 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400346 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700347 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400348 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700349 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400350 minijail_change_gid(j, pgr->gr_gid);
351 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400352}
353
Will Drewry6ac91122011-10-21 16:38:58 -0500354void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400355{
356 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400357}
358
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700359void API minijail_no_new_privs(struct minijail *j)
360{
361 j->flags.no_new_privs = 1;
362}
363
Will Drewry6ac91122011-10-21 16:38:58 -0500364void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400365{
366 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500367}
368
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700369void API minijail_log_seccomp_filter_failures(struct minijail *j)
370{
371 j->flags.log_seccomp_filter = 1;
372}
373
Will Drewry6ac91122011-10-21 16:38:58 -0500374void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400375{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800376 /*
377 * 'minijail_use_caps' configures a runtime-capabilities-only
378 * environment, including a bounding set matching the thread's runtime
379 * (permitted|inheritable|effective) sets.
380 * Therefore, it will override any existing bounding set configurations
381 * since the latter would allow gaining extra runtime capabilities from
382 * file capabilities.
383 */
384 if (j->flags.capbset_drop) {
385 warn("overriding bounding set configuration");
386 j->cap_bset = 0;
387 j->flags.capbset_drop = 0;
388 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400389 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800390 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400391}
392
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800393void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
394{
395 if (j->flags.use_caps) {
396 /*
397 * 'minijail_use_caps' will have already configured a capability
398 * bounding set matching the (permitted|inheritable|effective)
399 * sets. Abort if the user tries to configure a separate
400 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
401 * are mutually exclusive.
402 */
403 die("runtime capabilities already configured, can't drop "
404 "bounding set separately");
405 }
406 j->cap_bset = capmask;
407 j->flags.capbset_drop = 1;
408}
409
410void API minijail_reset_signal_mask(struct minijail *j)
411{
Peter Qiu2860c462015-12-16 15:13:06 -0800412 j->flags.reset_signal_mask = 1;
413}
414
Will Drewry6ac91122011-10-21 16:38:58 -0500415void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400416{
417 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400418}
419
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700420void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
421{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800422 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700423 if (ns_fd < 0) {
424 pdie("failed to open namespace '%s'", ns_path);
425 }
426 j->mountns_fd = ns_fd;
427 j->flags.enter_vfs = 1;
428}
429
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800430void API minijail_skip_remount_private(struct minijail *j)
431{
432 j->flags.skip_remount_private = 1;
433}
434
Will Drewry6ac91122011-10-21 16:38:58 -0500435void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400436{
Elly Jonese58176c2012-01-23 11:46:17 -0500437 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700438 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400439 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800440 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400441}
442
Dylan Reidf7942472015-11-18 17:55:26 -0800443void API minijail_namespace_ipc(struct minijail *j)
444{
445 j->flags.ipc = 1;
446}
447
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400448void API minijail_namespace_net(struct minijail *j)
449{
450 j->flags.net = 1;
451}
452
Dylan Reid1102f5a2015-09-15 11:52:20 -0700453void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
454{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800455 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700456 if (ns_fd < 0) {
457 pdie("failed to open namespace '%s'", ns_path);
458 }
459 j->netns_fd = ns_fd;
460 j->flags.enter_net = 1;
461}
462
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700463void API minijail_namespace_cgroups(struct minijail *j)
464{
465 j->flags.ns_cgroups = 1;
466}
467
Dylan Reid791f5772015-09-14 20:02:42 -0700468void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400469{
470 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700471 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400472}
473
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800474void API minijail_namespace_user(struct minijail *j)
475{
476 j->flags.userns = 1;
477}
478
479int API minijail_uidmap(struct minijail *j, const char *uidmap)
480{
481 j->uidmap = strdup(uidmap);
482 if (!j->uidmap)
483 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800484 char *ch;
485 for (ch = j->uidmap; *ch; ch++) {
486 if (*ch == ',')
487 *ch = '\n';
488 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800489 return 0;
490}
491
492int API minijail_gidmap(struct minijail *j, const char *gidmap)
493{
494 j->gidmap = strdup(gidmap);
495 if (!j->gidmap)
496 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800497 char *ch;
498 for (ch = j->gidmap; *ch; ch++) {
499 if (*ch == ',')
500 *ch = '\n';
501 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800502 return 0;
503}
504
Will Drewry6ac91122011-10-21 16:38:58 -0500505void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400506{
507 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400508}
509
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800510void API minijail_run_as_init(struct minijail *j)
511{
512 /*
513 * Since the jailed program will become 'init' in the new PID namespace,
514 * Minijail does not need to fork an 'init' process.
515 */
516 j->flags.do_init = 0;
517}
518
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700519int API minijail_enter_chroot(struct minijail *j, const char *dir)
520{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400521 if (j->chrootdir)
522 return -EINVAL;
523 j->chrootdir = strdup(dir);
524 if (!j->chrootdir)
525 return -ENOMEM;
526 j->flags.chroot = 1;
527 return 0;
528}
529
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800530int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
531{
532 if (j->chrootdir)
533 return -EINVAL;
534 j->chrootdir = strdup(dir);
535 if (!j->chrootdir)
536 return -ENOMEM;
537 j->flags.pivot_root = 1;
538 return 0;
539}
540
Dylan Reida14e08d2015-10-22 21:05:29 -0700541static char *append_external_path(const char *external_path,
542 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700543{
Dylan Reida14e08d2015-10-22 21:05:29 -0700544 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700545 size_t pathlen;
546
Dylan Reid08946cc2015-09-16 19:10:57 -0700547 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700548 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
549 path = malloc(pathlen);
550 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700551
Dylan Reida14e08d2015-10-22 21:05:29 -0700552 return path;
553}
554
555char API *minijail_get_original_path(struct minijail *j,
556 const char *path_inside_chroot)
557{
Dylan Reid648b2202015-10-23 00:50:00 -0700558 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700559
Dylan Reid648b2202015-10-23 00:50:00 -0700560 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700561 while (b) {
562 /*
563 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700564 * mount, then the original path is exactly the source of
565 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700566 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700567 * mount source = /some/path/exe, mount dest =
568 * /chroot/path/exe Then when getting the original path of
569 * "/chroot/path/exe", the source of that mount,
570 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700571 */
572 if (!strcmp(b->dest, path_inside_chroot))
573 return strdup(b->src);
574
575 /*
576 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700577 * mount, take the suffix of the chroot path relative to the
578 * mount destination path, and append it to the mount source
579 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700580 */
581 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
582 const char *relative_path =
583 path_inside_chroot + strlen(b->dest);
584 return append_external_path(b->src, relative_path);
585 }
586 b = b->next;
587 }
588
589 /* If there is a chroot path, append |path_inside_chroot| to that. */
590 if (j->chrootdir)
591 return append_external_path(j->chrootdir, path_inside_chroot);
592
593 /* No chroot, so the path outside is the same as it is inside. */
594 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700595}
596
Lee Campbell11af0622014-05-22 12:36:04 -0700597void API minijail_mount_tmp(struct minijail *j)
598{
599 j->flags.mount_tmp = 1;
600}
601
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800602int API minijail_write_pid_file(struct minijail *j, const char *path)
603{
604 j->pid_file_path = strdup(path);
605 if (!j->pid_file_path)
606 return -ENOMEM;
607 j->flags.pid_file = 1;
608 return 0;
609}
610
Dylan Reid605ce7f2016-01-19 19:21:00 -0800611int API minijail_add_to_cgroup(struct minijail *j, const char *path)
612{
613 if (j->cgroup_count >= MAX_CGROUPS)
614 return -ENOMEM;
615 j->cgroups[j->cgroup_count] = strdup(path);
616 if (!j->cgroups[j->cgroup_count])
617 return -ENOMEM;
618 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800619 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800620 return 0;
621}
622
Dylan Reid81e23972016-05-18 14:06:35 -0700623int API minijail_mount_with_data(struct minijail *j, const char *src,
624 const char *dest, const char *type,
625 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700626{
Dylan Reid648b2202015-10-23 00:50:00 -0700627 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400628
629 if (*dest != '/')
630 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700631 m = calloc(1, sizeof(*m));
632 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400633 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700634 m->dest = strdup(dest);
635 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400636 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700637 m->src = strdup(src);
638 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400639 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700640 m->type = strdup(type);
641 if (!m->type)
642 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700643 if (data) {
644 m->data = strdup(data);
645 if (!m->data)
646 goto error;
647 m->has_data = 1;
648 }
Dylan Reid648b2202015-10-23 00:50:00 -0700649 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400650
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800651 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400652
Elly Jonesdd3e8512012-01-23 15:13:38 -0500653 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700654 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400655 * containing vfs namespace.
656 */
657 minijail_namespace_vfs(j);
658
Dylan Reid648b2202015-10-23 00:50:00 -0700659 if (j->mounts_tail)
660 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400661 else
Dylan Reid648b2202015-10-23 00:50:00 -0700662 j->mounts_head = m;
663 j->mounts_tail = m;
664 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400665
666 return 0;
667
668error:
Dylan Reid81e23972016-05-18 14:06:35 -0700669 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700670 free(m->src);
671 free(m->dest);
672 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400673 return -ENOMEM;
674}
675
Dylan Reid81e23972016-05-18 14:06:35 -0700676int API minijail_mount(struct minijail *j, const char *src, const char *dest,
677 const char *type, unsigned long flags)
678{
679 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
680}
681
Dylan Reid648b2202015-10-23 00:50:00 -0700682int API minijail_bind(struct minijail *j, const char *src, const char *dest,
683 int writeable)
684{
685 unsigned long flags = MS_BIND;
686
687 if (!writeable)
688 flags |= MS_RDONLY;
689
690 return minijail_mount(j, src, dest, "", flags);
691}
692
Will Drewry6ac91122011-10-21 16:38:58 -0500693void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400694{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700695 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800696 if ((errno == EINVAL) && can_softfail()) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800697 warn("not loading seccomp filter,"
698 " seccomp not supported");
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800699 j->flags.seccomp_filter = 0;
700 j->flags.log_seccomp_filter = 0;
701 j->filter_len = 0;
702 j->filter_prog = NULL;
703 j->flags.no_new_privs = 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700704 }
705 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400706 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800707 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700708 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400709 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800710
711 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700712 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
713 die("failed to compile seccomp filter BPF program in '%s'",
714 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800715 }
716
717 j->filter_len = fprog->len;
718 j->filter_prog = fprog;
719
Elly Jonese1749eb2011-10-07 13:54:59 -0400720 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500721}
722
Andrew Brestickereac28942015-11-11 16:04:46 -0800723int API minijail_use_alt_syscall(struct minijail *j, const char *table)
724{
725 j->alt_syscall_table = strdup(table);
726 if (!j->alt_syscall_table)
727 return -ENOMEM;
728 j->flags.alt_syscall = 1;
729 return 0;
730}
731
Will Drewryf89aef52011-09-16 16:48:57 -0500732struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400733 size_t available;
734 size_t total;
735 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500736};
737
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800738void marshal_state_init(struct marshal_state *state, char *buf,
739 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400740{
741 state->available = available;
742 state->buf = buf;
743 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500744}
745
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800746void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400747{
748 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500749
Elly Jonese1749eb2011-10-07 13:54:59 -0400750 /* Up to |available| will be written. */
751 if (copy_len) {
752 memcpy(state->buf, src, copy_len);
753 state->buf += copy_len;
754 state->available -= copy_len;
755 }
756 /* |total| will contain the expected length. */
757 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500758}
759
Dylan Reid81e23972016-05-18 14:06:35 -0700760static void minijail_marshal_mount(struct marshal_state *state,
761 const struct mountpoint *m)
762{
763 marshal_append(state, m->src, strlen(m->src) + 1);
764 marshal_append(state, m->dest, strlen(m->dest) + 1);
765 marshal_append(state, m->type, strlen(m->type) + 1);
766 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
767 if (m->has_data)
768 marshal_append(state, m->data, strlen(m->data) + 1);
769 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
770}
771
Will Drewry6ac91122011-10-21 16:38:58 -0500772void minijail_marshal_helper(struct marshal_state *state,
773 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400774{
Dylan Reid648b2202015-10-23 00:50:00 -0700775 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800776 size_t i;
777
Elly Jonese1749eb2011-10-07 13:54:59 -0400778 marshal_append(state, (char *)j, sizeof(*j));
779 if (j->user)
780 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800781 if (j->suppl_gid_list) {
782 marshal_append(state, j->suppl_gid_list,
783 j->suppl_gid_count * sizeof(gid_t));
784 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400785 if (j->chrootdir)
786 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800787 if (j->alt_syscall_table) {
788 marshal_append(state, j->alt_syscall_table,
789 strlen(j->alt_syscall_table) + 1);
790 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800791 if (j->flags.seccomp_filter && j->filter_prog) {
792 struct sock_fprog *fp = j->filter_prog;
793 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800794 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400795 }
Dylan Reid648b2202015-10-23 00:50:00 -0700796 for (m = j->mounts_head; m; m = m->next) {
Dylan Reid81e23972016-05-18 14:06:35 -0700797 minijail_marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400798 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800799 for (i = 0; i < j->cgroup_count; ++i)
800 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500801}
802
Will Drewry6ac91122011-10-21 16:38:58 -0500803size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400804{
805 struct marshal_state state;
806 marshal_state_init(&state, NULL, 0);
807 minijail_marshal_helper(&state, j);
808 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500809}
810
Elly Jonese1749eb2011-10-07 13:54:59 -0400811int minijail_marshal(const struct minijail *j, char *buf, size_t available)
812{
813 struct marshal_state state;
814 marshal_state_init(&state, buf, available);
815 minijail_marshal_helper(&state, j);
816 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500817}
818
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800819/*
820 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
Elly Jones51a5b6c2011-10-12 19:09:26 -0400821 * @length Number of bytes to consume
822 * @buf Buffer to consume from
823 * @buflength Size of @buf
824 *
825 * Returns a pointer to the base of the bytes, or NULL for errors.
826 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700827void *consumebytes(size_t length, char **buf, size_t *buflength)
828{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400829 char *p = *buf;
830 if (length > *buflength)
831 return NULL;
832 *buf += length;
833 *buflength -= length;
834 return p;
835}
836
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800837/*
838 * consumestr: consumes a C string from a buffer @buf of length @length
Elly Jones51a5b6c2011-10-12 19:09:26 -0400839 * @buf Buffer to consume
840 * @length Length of buffer
841 *
842 * Returns a pointer to the base of the string, or NULL for errors.
843 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700844char *consumestr(char **buf, size_t *buflength)
845{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400846 size_t len = strnlen(*buf, *buflength);
847 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700848 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400849 return NULL;
850 return consumebytes(len + 1, buf, buflength);
851}
852
Elly Jonese1749eb2011-10-07 13:54:59 -0400853int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
854{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800855 size_t i;
856 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500857 int ret = -EINVAL;
858
Elly Jonese1749eb2011-10-07 13:54:59 -0400859 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500860 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400861 memcpy((void *)j, serialized, sizeof(*j));
862 serialized += sizeof(*j);
863 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500864
Will Drewrybee7ba72011-10-21 20:47:01 -0500865 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700866 j->mounts_head = NULL;
867 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800868 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500869
Elly Jonese1749eb2011-10-07 13:54:59 -0400870 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400871 char *user = consumestr(&serialized, &length);
872 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500873 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400874 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500875 if (!j->user)
876 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400877 }
Will Drewryf89aef52011-09-16 16:48:57 -0500878
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800879 if (j->suppl_gid_list) { /* stale pointer */
880 if (j->suppl_gid_count > NGROUPS_MAX) {
881 goto bad_gid_list;
882 }
883 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
884 void *gid_list_bytes =
885 consumebytes(gid_list_size, &serialized, &length);
886 if (!gid_list_bytes)
887 goto bad_gid_list;
888
889 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
890 if (!j->suppl_gid_list)
891 goto bad_gid_list;
892
893 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
894 }
895
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400896 if (j->chrootdir) { /* stale pointer */
897 char *chrootdir = consumestr(&serialized, &length);
898 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500899 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400900 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500901 if (!j->chrootdir)
902 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400903 }
904
Andrew Brestickereac28942015-11-11 16:04:46 -0800905 if (j->alt_syscall_table) { /* stale pointer */
906 char *alt_syscall_table = consumestr(&serialized, &length);
907 if (!alt_syscall_table)
908 goto bad_syscall_table;
909 j->alt_syscall_table = strdup(alt_syscall_table);
910 if (!j->alt_syscall_table)
911 goto bad_syscall_table;
912 }
913
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800914 if (j->flags.seccomp_filter && j->filter_len > 0) {
915 size_t ninstrs = j->filter_len;
916 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
917 ninstrs > USHRT_MAX)
918 goto bad_filters;
919
920 size_t program_len = ninstrs * sizeof(struct sock_filter);
921 void *program = consumebytes(program_len, &serialized, &length);
922 if (!program)
923 goto bad_filters;
924
925 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800926 if (!j->filter_prog)
927 goto bad_filters;
928
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800929 j->filter_prog->len = ninstrs;
930 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800931 if (!j->filter_prog->filter)
932 goto bad_filter_prog_instrs;
933
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800934 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400935 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400936
Dylan Reid648b2202015-10-23 00:50:00 -0700937 count = j->mounts_count;
938 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400939 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700940 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -0700941 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400942 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700943 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700944 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400945 const char *src = consumestr(&serialized, &length);
946 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700947 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400948 dest = consumestr(&serialized, &length);
949 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700950 goto bad_mounts;
951 type = consumestr(&serialized, &length);
952 if (!type)
953 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -0700954 has_data = consumebytes(sizeof(*has_data), &serialized,
955 &length);
956 if (!has_data)
957 goto bad_mounts;
958 if (*has_data) {
959 data = consumestr(&serialized, &length);
960 if (!data)
961 goto bad_mounts;
962 }
Dylan Reid648b2202015-10-23 00:50:00 -0700963 flags = consumebytes(sizeof(*flags), &serialized, &length);
964 if (!flags)
965 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -0700966 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -0700967 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400968 }
969
Dylan Reid605ce7f2016-01-19 19:21:00 -0800970 count = j->cgroup_count;
971 j->cgroup_count = 0;
972 for (i = 0; i < count; ++i) {
973 char *cgroup = consumestr(&serialized, &length);
974 if (!cgroup)
975 goto bad_cgroups;
976 j->cgroups[i] = strdup(cgroup);
977 if (!j->cgroups[i])
978 goto bad_cgroups;
979 ++j->cgroup_count;
980 }
981
Elly Jonese1749eb2011-10-07 13:54:59 -0400982 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500983
Dylan Reid605ce7f2016-01-19 19:21:00 -0800984bad_cgroups:
985 while (j->mounts_head) {
986 struct mountpoint *m = j->mounts_head;
987 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -0700988 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -0800989 free(m->type);
990 free(m->dest);
991 free(m->src);
992 free(m);
993 }
994 for (i = 0; i < j->cgroup_count; ++i)
995 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -0700996bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800997 if (j->flags.seccomp_filter && j->filter_len > 0) {
998 free(j->filter_prog->filter);
999 free(j->filter_prog);
1000 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001001bad_filter_prog_instrs:
1002 if (j->filter_prog)
1003 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001004bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001005 if (j->alt_syscall_table)
1006 free(j->alt_syscall_table);
1007bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001008 if (j->chrootdir)
1009 free(j->chrootdir);
1010bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001011 if (j->suppl_gid_list)
1012 free(j->suppl_gid_list);
1013bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001014 if (j->user)
1015 free(j->user);
1016clear_pointers:
1017 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001018 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001019 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001020 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001021 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001022out:
1023 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001024}
1025
Dylan Reidce5b55e2016-01-13 11:04:16 -08001026static void write_ugid_mappings(const struct minijail *j)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001027{
1028 int fd, ret, len;
1029 size_t sz;
1030 char fname[32];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001031
1032 sz = sizeof(fname);
1033 if (j->uidmap) {
1034 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001035 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001036 die("failed to write file name of uid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001037 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001038 if (fd < 0)
1039 pdie("failed to open '%s'", fname);
1040 len = strlen(j->uidmap);
1041 if (write(fd, j->uidmap, len) < len)
1042 die("failed to set uid_map");
1043 close(fd);
1044 }
1045 if (j->gidmap) {
1046 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001047 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001048 die("failed to write file name of gid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001049 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001050 if (fd < 0)
1051 pdie("failed to open '%s'", fname);
1052 len = strlen(j->gidmap);
1053 if (write(fd, j->gidmap, len) < len)
1054 die("failed to set gid_map");
1055 close(fd);
1056 }
Dylan Reidce5b55e2016-01-13 11:04:16 -08001057}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001058
Dylan Reidce5b55e2016-01-13 11:04:16 -08001059static void parent_setup_complete(int *pipe_fds)
1060{
1061 close(pipe_fds[0]);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001062 close(pipe_fds[1]);
1063}
1064
Dylan Reidce5b55e2016-01-13 11:04:16 -08001065/*
1066 * wait_for_parent_setup: Called by the child process to wait for any
1067 * further parent-side setup to complete before continuing.
1068 */
1069static void wait_for_parent_setup(int *pipe_fds)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001070{
1071 char buf;
1072
1073 close(pipe_fds[1]);
1074
Dylan Reidce5b55e2016-01-13 11:04:16 -08001075 /* Wait for parent to complete setup and close the pipe. */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001076 if (read(pipe_fds[0], &buf, 1) != 0)
1077 die("failed to sync with parent");
1078 close(pipe_fds[0]);
Dylan Reidce5b55e2016-01-13 11:04:16 -08001079}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001080
Dylan Reidce5b55e2016-01-13 11:04:16 -08001081static void enter_user_namespace(const struct minijail *j)
1082{
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001083 if (j->uidmap && setresuid(0, 0, 0))
1084 pdie("setresuid");
1085 if (j->gidmap && setresgid(0, 0, 0))
1086 pdie("setresgid");
1087}
1088
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001089/*
1090 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001091 * @j Minijail these mounts are for
1092 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001093 *
1094 * Returns 0 for success.
1095 */
Dylan Reid648b2202015-10-23 00:50:00 -07001096static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001097{
Dylan Reid648b2202015-10-23 00:50:00 -07001098 int ret;
1099 char *dest;
1100 int remount_ro = 0;
1101
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001102 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001103 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001104 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001105
1106 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001107 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1108 * can't both be specified in the original bind mount.
1109 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001110 */
1111 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1112 remount_ro = 1;
1113 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001114 }
Dylan Reid648b2202015-10-23 00:50:00 -07001115
Dylan Reid81e23972016-05-18 14:06:35 -07001116 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001117 if (ret)
1118 pdie("mount: %s -> %s", m->src, dest);
1119
1120 if (remount_ro) {
1121 m->flags |= MS_RDONLY;
1122 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001123 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001124 if (ret)
1125 pdie("bind ro: %s -> %s", m->src, dest);
1126 }
1127
Elly Jones51a5b6c2011-10-12 19:09:26 -04001128 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001129 if (m->next)
1130 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001131 return ret;
1132}
1133
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001134int enter_chroot(const struct minijail *j)
1135{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001136 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001137
1138 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001139 return ret;
1140
1141 if (chroot(j->chrootdir))
1142 return -errno;
1143
1144 if (chdir("/"))
1145 return -errno;
1146
1147 return 0;
1148}
1149
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001150int enter_pivot_root(const struct minijail *j)
1151{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001152 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001153
1154 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001155 return ret;
1156
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001157 /*
1158 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001159 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001160 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001161 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001162 if (oldroot < 0)
1163 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001164 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001165 if (newroot < 0)
1166 pdie("failed to open %s for fchdir", j->chrootdir);
1167
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001168 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001169 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001170 * do a self bind mount.
1171 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001172 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1173 pdie("failed to bind mount '%s'", j->chrootdir);
1174 if (chdir(j->chrootdir))
1175 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001176 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001177 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001178
1179 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001180 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001181 * change to the old root and unmount it.
1182 */
1183 if (fchdir(oldroot))
1184 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001185
1186 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001187 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1188 * there could be a shared mount point under |oldroot|. In that case,
1189 * mounts under this shared mount point will be unmounted below, and
1190 * this unmounting will propagate to the original mount namespace
1191 * (because the mount point is shared). To prevent this unexpected
1192 * unmounting, remove these mounts from their peer groups by recursively
1193 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001194 */
1195 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001196 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001197 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001198 if (umount2(".", MNT_DETACH))
1199 pdie("umount(/)");
1200 /* Change back to the new root. */
1201 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001202 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001203 if (close(oldroot))
1204 return -errno;
1205 if (close(newroot))
1206 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001207 if (chroot("/"))
1208 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001209 /* Set correct CWD for getcwd(3). */
1210 if (chdir("/"))
1211 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001212
1213 return 0;
1214}
1215
Lee Campbell11af0622014-05-22 12:36:04 -07001216int mount_tmp(void)
1217{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001218 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001219}
1220
Dylan Reid791f5772015-09-14 20:02:42 -07001221int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001222{
1223 const char *kProcPath = "/proc";
1224 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001225 /*
1226 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001227 * /proc in our namespace, which means using MS_REMOUNT here would
1228 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001229 * namespace (!). Instead, remove their mount from our namespace lazily
1230 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001231 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001232 if (umount2(kProcPath, MNT_DETACH)) {
1233 /*
1234 * If we are in a new user namespace, umount(2) will fail.
1235 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1236 */
1237 if (j->flags.userns) {
1238 info("umount(/proc, MNT_DETACH) failed, "
1239 "this is expected when using user namespaces");
1240 } else {
1241 return -errno;
1242 }
1243 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001244 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1245 return -errno;
1246 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001247}
1248
Dylan Reid605ce7f2016-01-19 19:21:00 -08001249static void write_pid_to_path(pid_t pid, const char *path)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001250{
Dylan Reid605ce7f2016-01-19 19:21:00 -08001251 FILE *fp = fopen(path, "w");
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001252
1253 if (!fp)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001254 pdie("failed to open '%s'", path);
1255 if (fprintf(fp, "%d\n", (int)pid) < 0)
1256 pdie("fprintf(%s)", path);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001257 if (fclose(fp))
Dylan Reid605ce7f2016-01-19 19:21:00 -08001258 pdie("fclose(%s)", path);
1259}
1260
1261static void write_pid_file(const struct minijail *j)
1262{
1263 write_pid_to_path(j->initpid, j->pid_file_path);
1264}
1265
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001266static void add_to_cgroups(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001267{
1268 size_t i;
1269
1270 for (i = 0; i < j->cgroup_count; ++i)
1271 write_pid_to_path(j->initpid, j->cgroups[i]);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001272}
1273
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001274void drop_ugid(const struct minijail *j)
1275{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001276 if (j->flags.usergroups && j->flags.suppl_gids) {
1277 die("tried to inherit *and* set supplementary groups;"
1278 " can only do one");
1279 }
1280
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001281 if (j->flags.usergroups) {
1282 if (initgroups(j->user, j->usergid))
1283 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001284 } else if (j->flags.suppl_gids) {
1285 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1286 pdie("setgroups");
1287 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001288 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001289 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001290 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001291 * users.
1292 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001293 if ((j->uid || j->gid) && setgroups(0, NULL))
1294 pdie("setgroups");
1295 }
1296
1297 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1298 pdie("setresgid");
1299
1300 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1301 pdie("setresuid");
1302}
1303
Mike Frysinger3adfef72013-05-09 17:19:08 -04001304/*
1305 * We specifically do not use cap_valid() as that only tells us the last
1306 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001307 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001308 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001309 * kernel).
1310 * Normally, we suck up the answer via /proc. On Android, not all processes are
1311 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1312 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001313 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001314static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001315{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001316 unsigned int last_valid_cap = 0;
1317 if (is_android()) {
1318 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1319 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001320
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001321 /* |last_valid_cap| will be the first failing value. */
1322 if (last_valid_cap > 0) {
1323 last_valid_cap--;
1324 }
1325 } else {
1326 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1327 FILE *fp = fopen(cap_file, "re");
1328 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1329 pdie("fscanf(%s)", cap_file);
1330 fclose(fp);
1331 }
Dylan Reidf682d472015-09-17 21:39:07 -07001332 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001333}
1334
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001335static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1336{
1337 const uint64_t one = 1;
1338 unsigned int i;
1339 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1340 if (keep_mask & (one << i))
1341 continue;
1342 if (prctl(PR_CAPBSET_DROP, i))
1343 pdie("could not drop capability from bounding set");
1344 }
1345}
1346
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001347void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001348{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001349 if (!j->flags.use_caps)
1350 return;
1351
Elly Jonese1749eb2011-10-07 13:54:59 -04001352 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001353 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001354 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001355 unsigned int i;
1356 if (!caps)
1357 die("can't get process caps");
1358 if (cap_clear_flag(caps, CAP_INHERITABLE))
1359 die("can't clear inheritable caps");
1360 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1361 die("can't clear effective caps");
1362 if (cap_clear_flag(caps, CAP_PERMITTED))
1363 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001364 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001365 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001366 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001367 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001368 flag[0] = i;
1369 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001370 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001371 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001372 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001373 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001374 die("can't add inheritable cap");
1375 }
1376 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001377 die("can't apply initial cleaned capset");
1378
1379 /*
1380 * Instead of dropping bounding set first, do it here in case
1381 * the caller had a more permissive bounding set which could
1382 * have been used above to raise a capability that wasn't already
1383 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1384 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001385 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001386
1387 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001388 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001389 flag[0] = CAP_SETPCAP;
1390 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1391 die("can't clear effective cap");
1392 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1393 die("can't clear permitted cap");
1394 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1395 die("can't clear inheritable cap");
1396 }
1397
1398 if (cap_set_proc(caps))
1399 die("can't apply final cleaned capset");
1400
1401 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001402}
1403
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001404void set_seccomp_filter(const struct minijail *j)
1405{
1406 /*
1407 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1408 * in the kernel source tree for an explanation of the parameters.
1409 */
1410 if (j->flags.no_new_privs) {
1411 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1412 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1413 }
1414
1415 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001416 * Code running with ASan
1417 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1418 * will make system calls not included in the syscall filter policy,
1419 * which will likely crash the program. Skip setting seccomp filter in
1420 * that case.
1421 * 'running_with_asan()' has no inputs and is completely defined at
1422 * build time, so this cannot be used by an attacker to skip setting
1423 * seccomp filter.
1424 */
1425 if (j->flags.seccomp_filter && running_with_asan()) {
1426 warn("running with ASan, not setting seccomp filter");
1427 return;
1428 }
1429
1430 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001431 * If we're logging seccomp filter failures,
1432 * install the SIGSYS handler first.
1433 */
1434 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1435 if (install_sigsys_handler())
1436 pdie("install SIGSYS handler");
1437 warn("logging seccomp filter failures");
1438 }
1439
1440 /*
1441 * Install the syscall filter.
1442 */
1443 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001444 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1445 j->filter_prog)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001446 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001447 warn("seccomp not supported");
1448 return;
1449 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001450 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001451 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001452 }
1453}
1454
Will Drewry6ac91122011-10-21 16:38:58 -05001455void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001456{
Dylan Reidf682d472015-09-17 21:39:07 -07001457 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001458 * If we're dropping caps, get the last valid cap from /proc now,
1459 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001460 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001461 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001462 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001463 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001464
Elly Jonese1749eb2011-10-07 13:54:59 -04001465 if (j->flags.pids)
1466 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001467 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001468
Elly Jonese1749eb2011-10-07 13:54:59 -04001469 if (j->flags.usergroups && !j->user)
1470 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001471
Elly Jonesdd3e8512012-01-23 15:13:38 -05001472 /*
1473 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001474 * so we don't even try. If any of our operations fail, we abort() the
1475 * entire process.
1476 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001477 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1478 pdie("setns(CLONE_NEWNS)");
1479
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001480 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001481 if (unshare(CLONE_NEWNS))
1482 pdie("unshare(vfs)");
1483 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001484 * Unless asked not to, remount all filesystems as private.
1485 * If they are shared, new bind mounts will creep out of our
1486 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001487 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1488 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001489 if (!j->flags.skip_remount_private) {
1490 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1491 pdie("mount(/, private)");
1492 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001493 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001494
Dylan Reidf7942472015-11-18 17:55:26 -08001495 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1496 pdie("unshare(ipc)");
1497 }
1498
Dylan Reid1102f5a2015-09-15 11:52:20 -07001499 if (j->flags.enter_net) {
1500 if (setns(j->netns_fd, CLONE_NEWNET))
1501 pdie("setns(CLONE_NEWNET)");
1502 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001503 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001504 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001505
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001506 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
1507 pdie("unshare(cgroups)");
1508
Elly Jones51a5b6c2011-10-12 19:09:26 -04001509 if (j->flags.chroot && enter_chroot(j))
1510 pdie("chroot");
1511
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001512 if (j->flags.pivot_root && enter_pivot_root(j))
1513 pdie("pivot_root");
1514
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001515 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001516 pdie("mount_tmp");
1517
Dylan Reid791f5772015-09-14 20:02:42 -07001518 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001519 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001520
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001521 /*
1522 * If we're only dropping capabilities from the bounding set, but not
1523 * from the thread's (permitted|inheritable|effective) sets, do it now.
1524 */
1525 if (j->flags.capbset_drop) {
1526 drop_capbset(j->cap_bset, last_valid_cap);
1527 }
1528
1529 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001530 /*
1531 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001532 * capability to change uids, our attempt to use setuid()
1533 * below will fail. Hang on to root caps across setuid(), then
1534 * lock securebits.
1535 */
1536 if (prctl(PR_SET_KEEPCAPS, 1))
1537 pdie("prctl(PR_SET_KEEPCAPS)");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001538
1539 /*
1540 * Kernels 4.3+ define a new securebit
1541 * (SECURE_NO_CAP_AMBIENT_RAISE), so using the SECURE_ALL_BITS
1542 * and SECURE_ALL_LOCKS masks from newer kernel headers will
1543 * return EPERM on older kernels. Detect this, and retry with
1544 * the right mask for older (2.6.26-4.2) kernels.
1545 */
1546 int securebits_ret = prctl(PR_SET_SECUREBITS,
1547 SECURE_ALL_BITS | SECURE_ALL_LOCKS);
1548 if (securebits_ret < 0) {
1549 if (errno == EPERM) {
1550 /* Possibly running on kernel < 4.3. */
1551 securebits_ret = prctl(
1552 PR_SET_SECUREBITS,
1553 OLD_SECURE_ALL_BITS | OLD_SECURE_ALL_LOCKS);
1554 }
1555 }
1556 if (securebits_ret < 0)
Elly Jonese1749eb2011-10-07 13:54:59 -04001557 pdie("prctl(PR_SET_SECUREBITS)");
1558 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001559
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001560 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001561 /*
1562 * If we're setting no_new_privs, we can drop privileges
1563 * before setting seccomp filter. This way filter policies
1564 * don't need to allow privilege-dropping syscalls.
1565 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001566 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001567 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001568 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001569 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001570 /*
1571 * If we're not setting no_new_privs,
1572 * we need to set seccomp filter *before* dropping privileges.
1573 * WARNING: this means that filter policies *must* allow
1574 * setgroups()/setresgid()/setresuid() for dropping root and
1575 * capget()/capset()/prctl() for dropping caps.
1576 */
1577 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001578 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001579 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001580 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001581
Elly Jonesdd3e8512012-01-23 15:13:38 -05001582 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001583 * Select the specified alternate syscall table. The table must not
1584 * block prctl(2) if we're using seccomp as well.
1585 */
1586 if (j->flags.alt_syscall) {
1587 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1588 pdie("prctl(PR_ALT_SYSCALL)");
1589 }
1590
1591 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001592 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001593 * privilege-dropping syscalls :)
1594 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001595 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001596 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001597 warn("seccomp not supported");
1598 return;
1599 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001600 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001601 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001602}
1603
Will Drewry6ac91122011-10-21 16:38:58 -05001604/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001605static int init_exitstatus = 0;
1606
Will Drewry6ac91122011-10-21 16:38:58 -05001607void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001608{
1609 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001610}
1611
Will Drewry6ac91122011-10-21 16:38:58 -05001612int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001613{
1614 pid_t pid;
1615 int status;
1616 /* so that we exit with the right status */
1617 signal(SIGTERM, init_term);
1618 /* TODO(wad) self jail with seccomp_filters here. */
1619 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001620 /*
1621 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001622 * left inside our pid namespace or we get a signal.
1623 */
1624 if (pid == rootpid)
1625 init_exitstatus = status;
1626 }
1627 if (!WIFEXITED(init_exitstatus))
1628 _exit(MINIJAIL_ERR_INIT);
1629 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001630}
1631
Will Drewry6ac91122011-10-21 16:38:58 -05001632int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001633{
1634 size_t sz = 0;
1635 size_t bytes = read(fd, &sz, sizeof(sz));
1636 char *buf;
1637 int r;
1638 if (sizeof(sz) != bytes)
1639 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001640 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001641 return -E2BIG;
1642 buf = malloc(sz);
1643 if (!buf)
1644 return -ENOMEM;
1645 bytes = read(fd, buf, sz);
1646 if (bytes != sz) {
1647 free(buf);
1648 return -EINVAL;
1649 }
1650 r = minijail_unmarshal(j, buf, sz);
1651 free(buf);
1652 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001653}
1654
Will Drewry6ac91122011-10-21 16:38:58 -05001655int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001656{
1657 char *buf;
1658 size_t sz = minijail_size(j);
1659 ssize_t written;
1660 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001661
Elly Jonese1749eb2011-10-07 13:54:59 -04001662 if (!sz)
1663 return -EINVAL;
1664 buf = malloc(sz);
1665 r = minijail_marshal(j, buf, sz);
1666 if (r) {
1667 free(buf);
1668 return r;
1669 }
1670 /* Sends [size][minijail]. */
1671 written = write(fd, &sz, sizeof(sz));
1672 if (written != sizeof(sz)) {
1673 free(buf);
1674 return -EFAULT;
1675 }
1676 written = write(fd, buf, sz);
1677 if (written < 0 || (size_t) written != sz) {
1678 free(buf);
1679 return -EFAULT;
1680 }
1681 free(buf);
1682 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001683}
Elly Jonescd7a9042011-07-22 13:56:51 -04001684
Will Drewry6ac91122011-10-21 16:38:58 -05001685int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001686{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001687#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001688 /* Don't use LDPRELOAD on Brillo. */
1689 return 0;
1690#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001691 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1692 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1693 if (!newenv)
1694 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001695
Elly Jonese1749eb2011-10-07 13:54:59 -04001696 /* Only insert a separating space if we have something to separate... */
1697 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1698 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001699
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001700 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001701 setenv(kLdPreloadEnvVar, newenv, 1);
1702 free(newenv);
1703 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001704#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001705}
1706
Will Drewry6ac91122011-10-21 16:38:58 -05001707int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001708{
1709 int r = pipe(fds);
1710 char fd_buf[11];
1711 if (r)
1712 return r;
1713 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1714 if (r <= 0)
1715 return -EINVAL;
1716 setenv(kFdEnvVar, fd_buf, 1);
1717 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001718}
1719
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001720int setup_pipe_end(int fds[2], size_t index)
1721{
1722 if (index > 1)
1723 return -1;
1724
1725 close(fds[1 - index]);
1726 return fds[index];
1727}
1728
1729int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1730{
1731 if (index > 1)
1732 return -1;
1733
1734 close(fds[1 - index]);
1735 /* dup2(2) the corresponding end of the pipe into |fd|. */
1736 return dup2(fds[index], fd);
1737}
1738
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001739int minijail_run_internal(struct minijail *j, const char *filename,
1740 char *const argv[], pid_t *pchild_pid,
1741 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1742 int use_preload);
1743
Will Drewry6ac91122011-10-21 16:38:58 -05001744int API minijail_run(struct minijail *j, const char *filename,
1745 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001746{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001747 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1748 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001749}
1750
1751int API minijail_run_pid(struct minijail *j, const char *filename,
1752 char *const argv[], pid_t *pchild_pid)
1753{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001754 return minijail_run_internal(j, filename, argv, pchild_pid,
1755 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001756}
1757
1758int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001759 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001760{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001761 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1762 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001763}
1764
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001765int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001766 char *const argv[], pid_t *pchild_pid,
1767 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001768{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001769 return minijail_run_internal(j, filename, argv, pchild_pid,
1770 pstdin_fd, pstdout_fd, pstderr_fd, true);
1771}
1772
1773int API minijail_run_no_preload(struct minijail *j, const char *filename,
1774 char *const argv[])
1775{
1776 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1777 false);
1778}
1779
Samuel Tan63187f42015-10-16 13:01:53 -07001780int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001781 const char *filename,
1782 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001783 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001784 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001785 int *pstderr_fd)
1786{
Samuel Tan63187f42015-10-16 13:01:53 -07001787 return minijail_run_internal(j, filename, argv, pchild_pid,
1788 pstdin_fd, pstdout_fd, pstderr_fd, false);
1789}
1790
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001791int minijail_run_internal(struct minijail *j, const char *filename,
1792 char *const argv[], pid_t *pchild_pid,
1793 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1794 int use_preload)
1795{
Elly Jonese1749eb2011-10-07 13:54:59 -04001796 char *oldenv, *oldenv_copy = NULL;
1797 pid_t child_pid;
1798 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001799 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001800 int stdout_fds[2];
1801 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001802 int child_sync_pipe_fds[2];
1803 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001804 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001805 /* We need to remember this across the minijail_preexec() call. */
1806 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001807 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001808
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001809 if (use_preload) {
1810 oldenv = getenv(kLdPreloadEnvVar);
1811 if (oldenv) {
1812 oldenv_copy = strdup(oldenv);
1813 if (!oldenv_copy)
1814 return -ENOMEM;
1815 }
1816
1817 if (setup_preload())
1818 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001819 }
Will Drewryf89aef52011-09-16 16:48:57 -05001820
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001821 if (!use_preload) {
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001822 if (j->flags.use_caps)
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001823 die("capabilities are not supported without "
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001824 "LD_PRELOAD");
1825 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001826
Elly Jonesdd3e8512012-01-23 15:13:38 -05001827 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001828 * Make the process group ID of this process equal to its PID, so that
1829 * both the Minijail process and the jailed process can be killed
1830 * together.
1831 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1832 * the process is already a process group leader.
1833 */
1834 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1835 if (errno != EPERM) {
1836 pdie("setpgid(0, 0)");
1837 }
1838 }
1839
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001840 if (use_preload) {
1841 /*
1842 * Before we fork(2) and execve(2) the child process, we need
1843 * to open a pipe(2) to send the minijail configuration over.
1844 */
1845 if (setup_pipe(pipe_fds))
1846 return -EFAULT;
1847 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001848
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001849 /*
1850 * If we want to write to the child process' standard input,
1851 * create the pipe(2) now.
1852 */
1853 if (pstdin_fd) {
1854 if (pipe(stdin_fds))
1855 return -EFAULT;
1856 }
1857
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001858 /*
1859 * If we want to read from the child process' standard output,
1860 * create the pipe(2) now.
1861 */
1862 if (pstdout_fd) {
1863 if (pipe(stdout_fds))
1864 return -EFAULT;
1865 }
1866
1867 /*
1868 * If we want to read from the child process' standard error,
1869 * create the pipe(2) now.
1870 */
1871 if (pstderr_fd) {
1872 if (pipe(stderr_fds))
1873 return -EFAULT;
1874 }
1875
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001876 /*
1877 * If we want to set up a new uid/gid mapping in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001878 * or if we need to add the child process to cgroups, create the pipe(2)
1879 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001880 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001881 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001882 sync_child = 1;
1883 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001884 return -EFAULT;
1885 }
1886
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001887 /*
1888 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001889 *
1890 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1891 *
1892 * In multithreaded programs, there are a bunch of locks inside libc,
1893 * some of which may be held by other threads at the time that we call
1894 * minijail_run_pid(). If we call fork(), glibc does its level best to
1895 * ensure that we hold all of these locks before it calls clone()
1896 * internally and drop them after clone() returns, but when we call
1897 * sys_clone(2) directly, all that gets bypassed and we end up with a
1898 * child address space where some of libc's important locks are held by
1899 * other threads (which did not get cloned, and hence will never release
1900 * those locks). This is okay so long as we call exec() immediately
1901 * after, but a bunch of seemingly-innocent libc functions like setenv()
1902 * take locks.
1903 *
1904 * Hence, only call sys_clone() if we need to, in order to get at pid
1905 * namespacing. If we follow this path, the child's address space might
1906 * have broken locks; you may only call functions that do not acquire
1907 * any locks.
1908 *
1909 * Unfortunately, fork() acquires every lock it can get its hands on, as
1910 * previously detailed, so this function is highly likely to deadlock
1911 * later on (see "deadlock here") if we're multithreaded.
1912 *
1913 * We might hack around this by having the clone()d child (init of the
1914 * pid namespace) return directly, rather than leaving the clone()d
1915 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001916 * its fork()ed child return in turn), but that process would be
1917 * crippled with its libc locks potentially broken. We might try
1918 * fork()ing in the parent before we clone() to ensure that we own all
1919 * the locks, but then we have to have the forked child hanging around
1920 * consuming resources (and possibly having file descriptors / shared
1921 * memory regions / etc attached). We'd need to keep the child around to
1922 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04001923 *
1924 * TODO(ellyjones): figure out if the "forked child hanging around"
1925 * problem is fixable or not. It would be nice if we worked in this
1926 * case.
1927 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001928 if (pid_namespace) {
1929 int clone_flags = CLONE_NEWPID | SIGCHLD;
1930 if (j->flags.userns)
1931 clone_flags |= CLONE_NEWUSER;
1932 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001933 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001934 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001935 }
Elly Jones761b7412012-06-13 15:49:52 -04001936
Elly Jonese1749eb2011-10-07 13:54:59 -04001937 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001938 if (use_preload) {
1939 free(oldenv_copy);
1940 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001941 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001942 }
Will Drewryf89aef52011-09-16 16:48:57 -05001943
Elly Jonese1749eb2011-10-07 13:54:59 -04001944 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001945 if (use_preload) {
1946 /* Restore parent's LD_PRELOAD. */
1947 if (oldenv_copy) {
1948 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1949 free(oldenv_copy);
1950 } else {
1951 unsetenv(kLdPreloadEnvVar);
1952 }
1953 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001954 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001955
Elly Jonese1749eb2011-10-07 13:54:59 -04001956 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001957
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001958 if (j->flags.pid_file)
1959 write_pid_file(j);
1960
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001961 if (j->flags.cgroups)
1962 add_to_cgroups(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001963
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001964 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001965 write_ugid_mappings(j);
1966
1967 if (sync_child)
1968 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001969
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001970 if (use_preload) {
1971 /* Send marshalled minijail. */
1972 close(pipe_fds[0]); /* read endpoint */
1973 ret = minijail_to_fd(j, pipe_fds[1]);
1974 close(pipe_fds[1]); /* write endpoint */
1975 if (ret) {
1976 kill(j->initpid, SIGKILL);
1977 die("failed to send marshalled minijail");
1978 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001979 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001980
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001981 if (pchild_pid)
1982 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001983
1984 /*
1985 * If we want to write to the child process' standard input,
1986 * set up the write end of the pipe.
1987 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001988 if (pstdin_fd)
1989 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001990 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001991
1992 /*
1993 * If we want to read from the child process' standard output,
1994 * set up the read end of the pipe.
1995 */
1996 if (pstdout_fd)
1997 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001998 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001999
2000 /*
2001 * If we want to read from the child process' standard error,
2002 * set up the read end of the pipe.
2003 */
2004 if (pstderr_fd)
2005 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002006 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002007
Elly Jonese1749eb2011-10-07 13:54:59 -04002008 return 0;
2009 }
2010 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002011
Peter Qiu2860c462015-12-16 15:13:06 -08002012 if (j->flags.reset_signal_mask) {
2013 sigset_t signal_mask;
2014 if (sigemptyset(&signal_mask) != 0)
2015 pdie("sigemptyset failed");
2016 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2017 pdie("sigprocmask failed");
2018 }
2019
Dylan Reidce5b55e2016-01-13 11:04:16 -08002020 if (sync_child)
2021 wait_for_parent_setup(child_sync_pipe_fds);
2022
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002023 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002024 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002025
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002026 /*
2027 * If we want to write to the jailed process' standard input,
2028 * set up the read end of the pipe.
2029 */
2030 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002031 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2032 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002033 die("failed to set up stdin pipe");
2034 }
2035
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002036 /*
2037 * If we want to read from the jailed process' standard output,
2038 * set up the write end of the pipe.
2039 */
2040 if (pstdout_fd) {
2041 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2042 STDOUT_FILENO) < 0)
2043 die("failed to set up stdout pipe");
2044 }
2045
2046 /*
2047 * If we want to read from the jailed process' standard error,
2048 * set up the write end of the pipe.
2049 */
2050 if (pstderr_fd) {
2051 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2052 STDERR_FILENO) < 0)
2053 die("failed to set up stderr pipe");
2054 }
2055
Dylan Reid791f5772015-09-14 20:02:42 -07002056 /* If running an init program, let it decide when/how to mount /proc. */
2057 if (pid_namespace && !do_init)
2058 j->flags.remount_proc_ro = 0;
2059
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002060 if (use_preload) {
2061 /* Strip out flags that cannot be inherited across execve(2). */
2062 minijail_preexec(j);
2063 } else {
2064 j->flags.pids = 0;
2065 }
2066 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002067 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002068
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002069 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002070 /*
2071 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002072 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002073 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002074 * a child to actually run the program. If |do_init == 0|, we
2075 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002076 *
2077 * If we're multithreaded, we'll probably deadlock here. See
2078 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002079 */
2080 child_pid = fork();
2081 if (child_pid < 0)
2082 _exit(child_pid);
2083 else if (child_pid > 0)
2084 init(child_pid); /* never returns */
2085 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002086
Elly Jonesdd3e8512012-01-23 15:13:38 -05002087 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002088 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002089 * calling process
2090 * -> execve()-ing process
2091 * If we are:
2092 * calling process
2093 * -> init()-ing process
2094 * -> execve()-ing process
2095 */
2096 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04002097}
2098
Will Drewry6ac91122011-10-21 16:38:58 -05002099int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002100{
2101 int st;
2102 if (kill(j->initpid, SIGTERM))
2103 return -errno;
2104 if (waitpid(j->initpid, &st, 0) < 0)
2105 return -errno;
2106 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002107}
2108
Will Drewry6ac91122011-10-21 16:38:58 -05002109int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002110{
2111 int st;
2112 if (waitpid(j->initpid, &st, 0) < 0)
2113 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002114
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002115 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002116 int error_status = st;
2117 if (WIFSIGNALED(st)) {
2118 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002119 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002120 j->initpid, signum);
2121 /*
2122 * We return MINIJAIL_ERR_JAIL if the process received
2123 * SIGSYS, which happens when a syscall is blocked by
2124 * seccomp filters.
2125 * If not, we do what bash(1) does:
2126 * $? = 128 + signum
2127 */
2128 if (signum == SIGSYS) {
2129 error_status = MINIJAIL_ERR_JAIL;
2130 } else {
2131 error_status = 128 + signum;
2132 }
2133 }
2134 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002135 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002136
2137 int exit_status = WEXITSTATUS(st);
2138 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002139 info("child process %d exited with status %d",
2140 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002141
2142 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002143}
2144
Will Drewry6ac91122011-10-21 16:38:58 -05002145void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002146{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002147 size_t i;
2148
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002149 if (j->flags.seccomp_filter && j->filter_prog) {
2150 free(j->filter_prog->filter);
2151 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002152 }
Dylan Reid648b2202015-10-23 00:50:00 -07002153 while (j->mounts_head) {
2154 struct mountpoint *m = j->mounts_head;
2155 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002156 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002157 free(m->type);
2158 free(m->dest);
2159 free(m->src);
2160 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002161 }
Dylan Reid648b2202015-10-23 00:50:00 -07002162 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002163 if (j->user)
2164 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002165 if (j->suppl_gid_list)
2166 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002167 if (j->chrootdir)
2168 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08002169 if (j->alt_syscall_table)
2170 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002171 for (i = 0; i < j->cgroup_count; ++i)
2172 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002173 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002174}