blob: 16c3703719e25389dd400db6dbd0eb0c77c590dd [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>
Elly Jonescd7a9042011-07-22 13:56:51 -040035#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040036#include <unistd.h>
37
38#include "libminijail.h"
39#include "libminijail-private.h"
40
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070041#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080042#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070043#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080044
Lei Zhangeee31552012-10-17 21:27:10 -070045#ifdef HAVE_SECUREBITS_H
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070046# include <linux/securebits.h>
Lei Zhangeee31552012-10-17 21:27:10 -070047#else
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070048# define SECURE_ALL_BITS 0x55
49# define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
50#endif
51/* For kernels < 4.3. */
52#define OLD_SECURE_ALL_BITS 0x15
53#define OLD_SECURE_ALL_LOCKS (OLD_SECURE_ALL_BITS << 1)
54
55/*
56 * Assert the value of SECURE_ALL_BITS at compile-time.
57 * Brillo devices are currently compiled against 4.4 kernel headers. Kernel 4.3
58 * added a new securebit.
59 * When a new securebit is added, the new SECURE_ALL_BITS mask will return EPERM
60 * when used on older kernels. The compile-time assert will catch this situation
61 * at compile time.
62 */
63#ifdef __BRILLO__
64_Static_assert(SECURE_ALL_BITS == 0x55, "SECURE_ALL_BITS == 0x55.");
Lei Zhangeee31552012-10-17 21:27:10 -070065#endif
66
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070067/* Until these are reliably available in linux/prctl.h. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080068#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070069# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080070#endif
71
Andrew Brestickereac28942015-11-11 16:04:46 -080072#ifndef PR_ALT_SYSCALL
73# define PR_ALT_SYSCALL 0x43724f53
74#endif
75
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080076/* For seccomp_filter using BPF. */
77#ifndef PR_SET_NO_NEW_PRIVS
78# define PR_SET_NO_NEW_PRIVS 38
79#endif
80#ifndef SECCOMP_MODE_FILTER
81# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050082#endif
83
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070084#ifdef USE_SECCOMP_SOFTFAIL
85# define SECCOMP_SOFTFAIL 1
86#else
87# define SECCOMP_SOFTFAIL 0
88#endif
89
Dylan Reid4cbc2a52016-06-17 19:06:07 -070090/* New cgroup namespace might not be in linux-headers yet. */
91#ifndef CLONE_NEWCGROUP
92# define CLONE_NEWCGROUP 0x02000000
93#endif
94
Dylan Reid605ce7f2016-01-19 19:21:00 -080095#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
96
Dylan Reid648b2202015-10-23 00:50:00 -070097struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040098 char *src;
99 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700100 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700101 char *data;
102 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -0700103 unsigned long flags;
104 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400105};
106
Will Drewryf89aef52011-09-16 16:48:57 -0500107struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700108 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700109 * WARNING: if you add a flag here you need to make sure it's
110 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700111 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400112 struct {
113 int uid:1;
114 int gid:1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800115 int usergroups:1;
116 int suppl_gids:1;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800117 int use_caps:1;
118 int capbset_drop:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400119 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700120 int enter_vfs:1;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800121 int skip_remount_private:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400122 int pids:1;
Dylan Reidf7942472015-11-18 17:55:26 -0800123 int ipc:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400124 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700125 int enter_net:1;
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700126 int ns_cgroups:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800127 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400128 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -0700129 int remount_proc_ro:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700130 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400131 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700132 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400133 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800134 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700135 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800136 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800137 int pid_file:1;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800138 int cgroups:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800139 int alt_syscall:1;
Peter Qiu2860c462015-12-16 15:13:06 -0800140 int reset_signal_mask:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400141 } flags;
142 uid_t uid;
143 gid_t gid;
144 gid_t usergid;
145 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800146 size_t suppl_gid_count;
147 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400148 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800149 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400150 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700151 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700152 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400153 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800154 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800155 char *uidmap;
156 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800157 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800158 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800159 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700160 struct mountpoint *mounts_head;
161 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800162 size_t mounts_count;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800163 char *cgroups[MAX_CGROUPS];
164 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500165};
166
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700167/*
168 * Strip out flags meant for the parent.
169 * We keep things that are not inherited across execve(2) (e.g. capabilities),
170 * or are easier to set after execve(2) (e.g. seccomp filters).
171 */
172void minijail_preenter(struct minijail *j)
173{
174 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700175 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900176 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700177 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700178 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800179 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800180 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800181 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700182}
183
184/*
185 * Strip out flags meant for the child.
186 * We keep things that are inherited across execve(2).
187 */
188void minijail_preexec(struct minijail *j)
189{
190 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700191 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800192 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700193 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800194 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700195 if (j->user)
196 free(j->user);
197 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800198 if (j->suppl_gid_list)
199 free(j->suppl_gid_list);
200 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700201 memset(&j->flags, 0, sizeof(j->flags));
202 /* Now restore anything we meant to keep. */
203 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700204 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800205 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700206 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800207 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700208 /* Note, |pids| will already have been used before this call. */
209}
210
211/* Minijail API. */
212
Will Drewry6ac91122011-10-21 16:38:58 -0500213struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400214{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400215 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400216}
217
Will Drewry6ac91122011-10-21 16:38:58 -0500218void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400219{
220 if (uid == 0)
221 die("useless change to uid 0");
222 j->uid = uid;
223 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400224}
225
Will Drewry6ac91122011-10-21 16:38:58 -0500226void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400227{
228 if (gid == 0)
229 die("useless change to gid 0");
230 j->gid = gid;
231 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400232}
233
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800234void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
235 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800236{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800237 size_t i;
238
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800239 if (j->flags.usergroups)
240 die("cannot inherit *and* set supplementary groups");
241
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800242 if (size == 0) {
243 /* Clear supplementary groups. */
244 j->suppl_gid_list = NULL;
245 j->suppl_gid_count = 0;
246 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800247 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800248 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800249
250 /* Copy the gid_t array. */
251 j->suppl_gid_list = calloc(size, sizeof(gid_t));
252 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800253 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800254 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800255 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800256 j->suppl_gid_list[i] = list[i];
257 }
258 j->suppl_gid_count = size;
259 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800260}
261
Will Drewry6ac91122011-10-21 16:38:58 -0500262int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400263{
264 char *buf = NULL;
265 struct passwd pw;
266 struct passwd *ppw = NULL;
267 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
268 if (sz == -1)
269 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400270
Elly Jonesdd3e8512012-01-23 15:13:38 -0500271 /*
272 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400273 * the maximum needed size of the buffer, so we don't have to search.
274 */
275 buf = malloc(sz);
276 if (!buf)
277 return -ENOMEM;
278 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500279 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800280 * We're safe to free the buffer here. The strings inside |pw| point
281 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800282 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
283 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500284 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400285 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700286 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400287 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700288 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400289 minijail_change_uid(j, ppw->pw_uid);
290 j->user = strdup(user);
291 if (!j->user)
292 return -ENOMEM;
293 j->usergid = ppw->pw_gid;
294 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400295}
296
Will Drewry6ac91122011-10-21 16:38:58 -0500297int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400298{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700299 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700300 struct group gr;
301 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400302 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
303 if (sz == -1)
304 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400305
Elly Jonesdd3e8512012-01-23 15:13:38 -0500306 /*
307 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400308 * the maximum needed size of the buffer, so we don't have to search.
309 */
310 buf = malloc(sz);
311 if (!buf)
312 return -ENOMEM;
313 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500314 /*
315 * We're safe to free the buffer here. The strings inside gr point
316 * inside buf, but we don't use any of them; this leaves the pointers
317 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
318 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400319 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700320 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400321 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700322 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400323 minijail_change_gid(j, pgr->gr_gid);
324 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400325}
326
Will Drewry6ac91122011-10-21 16:38:58 -0500327void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400328{
329 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400330}
331
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700332void API minijail_no_new_privs(struct minijail *j)
333{
334 j->flags.no_new_privs = 1;
335}
336
Will Drewry6ac91122011-10-21 16:38:58 -0500337void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400338{
339 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500340}
341
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700342void API minijail_log_seccomp_filter_failures(struct minijail *j)
343{
344 j->flags.log_seccomp_filter = 1;
345}
346
Will Drewry6ac91122011-10-21 16:38:58 -0500347void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400348{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800349 /*
350 * 'minijail_use_caps' configures a runtime-capabilities-only
351 * environment, including a bounding set matching the thread's runtime
352 * (permitted|inheritable|effective) sets.
353 * Therefore, it will override any existing bounding set configurations
354 * since the latter would allow gaining extra runtime capabilities from
355 * file capabilities.
356 */
357 if (j->flags.capbset_drop) {
358 warn("overriding bounding set configuration");
359 j->cap_bset = 0;
360 j->flags.capbset_drop = 0;
361 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400362 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800363 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400364}
365
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800366void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
367{
368 if (j->flags.use_caps) {
369 /*
370 * 'minijail_use_caps' will have already configured a capability
371 * bounding set matching the (permitted|inheritable|effective)
372 * sets. Abort if the user tries to configure a separate
373 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
374 * are mutually exclusive.
375 */
376 die("runtime capabilities already configured, can't drop "
377 "bounding set separately");
378 }
379 j->cap_bset = capmask;
380 j->flags.capbset_drop = 1;
381}
382
383void API minijail_reset_signal_mask(struct minijail *j)
384{
Peter Qiu2860c462015-12-16 15:13:06 -0800385 j->flags.reset_signal_mask = 1;
386}
387
Will Drewry6ac91122011-10-21 16:38:58 -0500388void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400389{
390 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400391}
392
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700393void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
394{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800395 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700396 if (ns_fd < 0) {
397 pdie("failed to open namespace '%s'", ns_path);
398 }
399 j->mountns_fd = ns_fd;
400 j->flags.enter_vfs = 1;
401}
402
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800403void API minijail_skip_remount_private(struct minijail *j)
404{
405 j->flags.skip_remount_private = 1;
406}
407
Will Drewry6ac91122011-10-21 16:38:58 -0500408void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400409{
Elly Jonese58176c2012-01-23 11:46:17 -0500410 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700411 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400412 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800413 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400414}
415
Dylan Reidf7942472015-11-18 17:55:26 -0800416void API minijail_namespace_ipc(struct minijail *j)
417{
418 j->flags.ipc = 1;
419}
420
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400421void API minijail_namespace_net(struct minijail *j)
422{
423 j->flags.net = 1;
424}
425
Dylan Reid1102f5a2015-09-15 11:52:20 -0700426void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
427{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800428 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700429 if (ns_fd < 0) {
430 pdie("failed to open namespace '%s'", ns_path);
431 }
432 j->netns_fd = ns_fd;
433 j->flags.enter_net = 1;
434}
435
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700436void API minijail_namespace_cgroups(struct minijail *j)
437{
438 j->flags.ns_cgroups = 1;
439}
440
Dylan Reid791f5772015-09-14 20:02:42 -0700441void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400442{
443 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700444 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400445}
446
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800447void API minijail_namespace_user(struct minijail *j)
448{
449 j->flags.userns = 1;
450}
451
452int API minijail_uidmap(struct minijail *j, const char *uidmap)
453{
454 j->uidmap = strdup(uidmap);
455 if (!j->uidmap)
456 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800457 char *ch;
458 for (ch = j->uidmap; *ch; ch++) {
459 if (*ch == ',')
460 *ch = '\n';
461 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800462 return 0;
463}
464
465int API minijail_gidmap(struct minijail *j, const char *gidmap)
466{
467 j->gidmap = strdup(gidmap);
468 if (!j->gidmap)
469 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800470 char *ch;
471 for (ch = j->gidmap; *ch; ch++) {
472 if (*ch == ',')
473 *ch = '\n';
474 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800475 return 0;
476}
477
Will Drewry6ac91122011-10-21 16:38:58 -0500478void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400479{
480 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400481}
482
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800483void API minijail_run_as_init(struct minijail *j)
484{
485 /*
486 * Since the jailed program will become 'init' in the new PID namespace,
487 * Minijail does not need to fork an 'init' process.
488 */
489 j->flags.do_init = 0;
490}
491
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700492int API minijail_enter_chroot(struct minijail *j, const char *dir)
493{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400494 if (j->chrootdir)
495 return -EINVAL;
496 j->chrootdir = strdup(dir);
497 if (!j->chrootdir)
498 return -ENOMEM;
499 j->flags.chroot = 1;
500 return 0;
501}
502
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800503int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
504{
505 if (j->chrootdir)
506 return -EINVAL;
507 j->chrootdir = strdup(dir);
508 if (!j->chrootdir)
509 return -ENOMEM;
510 j->flags.pivot_root = 1;
511 return 0;
512}
513
Dylan Reida14e08d2015-10-22 21:05:29 -0700514char API *minijail_get_original_path(struct minijail *j,
515 const char *path_inside_chroot)
516{
Dylan Reid648b2202015-10-23 00:50:00 -0700517 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700518
Dylan Reid648b2202015-10-23 00:50:00 -0700519 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700520 while (b) {
521 /*
522 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700523 * mount, then the original path is exactly the source of
524 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700525 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700526 * mount source = /some/path/exe, mount dest =
527 * /chroot/path/exe Then when getting the original path of
528 * "/chroot/path/exe", the source of that mount,
529 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700530 */
531 if (!strcmp(b->dest, path_inside_chroot))
532 return strdup(b->src);
533
534 /*
535 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700536 * mount, take the suffix of the chroot path relative to the
537 * mount destination path, and append it to the mount source
538 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700539 */
540 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
541 const char *relative_path =
542 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400543 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700544 }
545 b = b->next;
546 }
547
548 /* If there is a chroot path, append |path_inside_chroot| to that. */
549 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400550 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700551
552 /* No chroot, so the path outside is the same as it is inside. */
553 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700554}
555
Lee Campbell11af0622014-05-22 12:36:04 -0700556void API minijail_mount_tmp(struct minijail *j)
557{
558 j->flags.mount_tmp = 1;
559}
560
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800561int API minijail_write_pid_file(struct minijail *j, const char *path)
562{
563 j->pid_file_path = strdup(path);
564 if (!j->pid_file_path)
565 return -ENOMEM;
566 j->flags.pid_file = 1;
567 return 0;
568}
569
Dylan Reid605ce7f2016-01-19 19:21:00 -0800570int API minijail_add_to_cgroup(struct minijail *j, const char *path)
571{
572 if (j->cgroup_count >= MAX_CGROUPS)
573 return -ENOMEM;
574 j->cgroups[j->cgroup_count] = strdup(path);
575 if (!j->cgroups[j->cgroup_count])
576 return -ENOMEM;
577 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800578 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800579 return 0;
580}
581
Dylan Reid81e23972016-05-18 14:06:35 -0700582int API minijail_mount_with_data(struct minijail *j, const char *src,
583 const char *dest, const char *type,
584 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700585{
Dylan Reid648b2202015-10-23 00:50:00 -0700586 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400587
588 if (*dest != '/')
589 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700590 m = calloc(1, sizeof(*m));
591 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400592 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700593 m->dest = strdup(dest);
594 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400595 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700596 m->src = strdup(src);
597 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400598 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700599 m->type = strdup(type);
600 if (!m->type)
601 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700602 if (data) {
603 m->data = strdup(data);
604 if (!m->data)
605 goto error;
606 m->has_data = 1;
607 }
Dylan Reid648b2202015-10-23 00:50:00 -0700608 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400609
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800610 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400611
Elly Jonesdd3e8512012-01-23 15:13:38 -0500612 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700613 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400614 * containing vfs namespace.
615 */
616 minijail_namespace_vfs(j);
617
Dylan Reid648b2202015-10-23 00:50:00 -0700618 if (j->mounts_tail)
619 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400620 else
Dylan Reid648b2202015-10-23 00:50:00 -0700621 j->mounts_head = m;
622 j->mounts_tail = m;
623 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400624
625 return 0;
626
627error:
Dylan Reid81e23972016-05-18 14:06:35 -0700628 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700629 free(m->src);
630 free(m->dest);
631 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400632 return -ENOMEM;
633}
634
Dylan Reid81e23972016-05-18 14:06:35 -0700635int API minijail_mount(struct minijail *j, const char *src, const char *dest,
636 const char *type, unsigned long flags)
637{
638 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
639}
640
Dylan Reid648b2202015-10-23 00:50:00 -0700641int API minijail_bind(struct minijail *j, const char *src, const char *dest,
642 int writeable)
643{
644 unsigned long flags = MS_BIND;
645
646 if (!writeable)
647 flags |= MS_RDONLY;
648
649 return minijail_mount(j, src, dest, "", flags);
650}
651
Will Drewry6ac91122011-10-21 16:38:58 -0500652void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400653{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700654 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400655 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800656 warn("not loading seccomp filter,"
657 " seccomp not supported");
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800658 j->flags.seccomp_filter = 0;
659 j->flags.log_seccomp_filter = 0;
660 j->filter_len = 0;
661 j->filter_prog = NULL;
662 j->flags.no_new_privs = 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700663 }
664 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400665 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800666 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700667 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400668 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800669
670 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700671 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
672 die("failed to compile seccomp filter BPF program in '%s'",
673 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800674 }
675
676 j->filter_len = fprog->len;
677 j->filter_prog = fprog;
678
Elly Jonese1749eb2011-10-07 13:54:59 -0400679 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500680}
681
Andrew Brestickereac28942015-11-11 16:04:46 -0800682int API minijail_use_alt_syscall(struct minijail *j, const char *table)
683{
684 j->alt_syscall_table = strdup(table);
685 if (!j->alt_syscall_table)
686 return -ENOMEM;
687 j->flags.alt_syscall = 1;
688 return 0;
689}
690
Will Drewryf89aef52011-09-16 16:48:57 -0500691struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400692 size_t available;
693 size_t total;
694 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500695};
696
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800697void marshal_state_init(struct marshal_state *state, char *buf,
698 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400699{
700 state->available = available;
701 state->buf = buf;
702 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500703}
704
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800705void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400706{
707 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500708
Elly Jonese1749eb2011-10-07 13:54:59 -0400709 /* Up to |available| will be written. */
710 if (copy_len) {
711 memcpy(state->buf, src, copy_len);
712 state->buf += copy_len;
713 state->available -= copy_len;
714 }
715 /* |total| will contain the expected length. */
716 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500717}
718
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400719void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700720{
721 marshal_append(state, m->src, strlen(m->src) + 1);
722 marshal_append(state, m->dest, strlen(m->dest) + 1);
723 marshal_append(state, m->type, strlen(m->type) + 1);
724 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
725 if (m->has_data)
726 marshal_append(state, m->data, strlen(m->data) + 1);
727 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
728}
729
Will Drewry6ac91122011-10-21 16:38:58 -0500730void minijail_marshal_helper(struct marshal_state *state,
731 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400732{
Dylan Reid648b2202015-10-23 00:50:00 -0700733 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800734 size_t i;
735
Elly Jonese1749eb2011-10-07 13:54:59 -0400736 marshal_append(state, (char *)j, sizeof(*j));
737 if (j->user)
738 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800739 if (j->suppl_gid_list) {
740 marshal_append(state, j->suppl_gid_list,
741 j->suppl_gid_count * sizeof(gid_t));
742 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400743 if (j->chrootdir)
744 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800745 if (j->alt_syscall_table) {
746 marshal_append(state, j->alt_syscall_table,
747 strlen(j->alt_syscall_table) + 1);
748 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800749 if (j->flags.seccomp_filter && j->filter_prog) {
750 struct sock_fprog *fp = j->filter_prog;
751 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800752 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400753 }
Dylan Reid648b2202015-10-23 00:50:00 -0700754 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400755 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400756 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800757 for (i = 0; i < j->cgroup_count; ++i)
758 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500759}
760
Will Drewry6ac91122011-10-21 16:38:58 -0500761size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400762{
763 struct marshal_state state;
764 marshal_state_init(&state, NULL, 0);
765 minijail_marshal_helper(&state, j);
766 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500767}
768
Elly Jonese1749eb2011-10-07 13:54:59 -0400769int minijail_marshal(const struct minijail *j, char *buf, size_t available)
770{
771 struct marshal_state state;
772 marshal_state_init(&state, buf, available);
773 minijail_marshal_helper(&state, j);
774 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500775}
776
Elly Jonese1749eb2011-10-07 13:54:59 -0400777int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
778{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800779 size_t i;
780 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500781 int ret = -EINVAL;
782
Elly Jonese1749eb2011-10-07 13:54:59 -0400783 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500784 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400785 memcpy((void *)j, serialized, sizeof(*j));
786 serialized += sizeof(*j);
787 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500788
Will Drewrybee7ba72011-10-21 20:47:01 -0500789 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700790 j->mounts_head = NULL;
791 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800792 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500793
Elly Jonese1749eb2011-10-07 13:54:59 -0400794 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400795 char *user = consumestr(&serialized, &length);
796 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500797 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400798 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500799 if (!j->user)
800 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400801 }
Will Drewryf89aef52011-09-16 16:48:57 -0500802
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800803 if (j->suppl_gid_list) { /* stale pointer */
804 if (j->suppl_gid_count > NGROUPS_MAX) {
805 goto bad_gid_list;
806 }
807 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
808 void *gid_list_bytes =
809 consumebytes(gid_list_size, &serialized, &length);
810 if (!gid_list_bytes)
811 goto bad_gid_list;
812
813 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
814 if (!j->suppl_gid_list)
815 goto bad_gid_list;
816
817 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
818 }
819
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400820 if (j->chrootdir) { /* stale pointer */
821 char *chrootdir = consumestr(&serialized, &length);
822 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500823 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400824 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500825 if (!j->chrootdir)
826 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400827 }
828
Andrew Brestickereac28942015-11-11 16:04:46 -0800829 if (j->alt_syscall_table) { /* stale pointer */
830 char *alt_syscall_table = consumestr(&serialized, &length);
831 if (!alt_syscall_table)
832 goto bad_syscall_table;
833 j->alt_syscall_table = strdup(alt_syscall_table);
834 if (!j->alt_syscall_table)
835 goto bad_syscall_table;
836 }
837
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800838 if (j->flags.seccomp_filter && j->filter_len > 0) {
839 size_t ninstrs = j->filter_len;
840 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
841 ninstrs > USHRT_MAX)
842 goto bad_filters;
843
844 size_t program_len = ninstrs * sizeof(struct sock_filter);
845 void *program = consumebytes(program_len, &serialized, &length);
846 if (!program)
847 goto bad_filters;
848
849 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800850 if (!j->filter_prog)
851 goto bad_filters;
852
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800853 j->filter_prog->len = ninstrs;
854 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800855 if (!j->filter_prog->filter)
856 goto bad_filter_prog_instrs;
857
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800858 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400859 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400860
Dylan Reid648b2202015-10-23 00:50:00 -0700861 count = j->mounts_count;
862 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400863 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700864 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -0700865 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400866 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700867 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700868 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400869 const char *src = consumestr(&serialized, &length);
870 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700871 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400872 dest = consumestr(&serialized, &length);
873 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700874 goto bad_mounts;
875 type = consumestr(&serialized, &length);
876 if (!type)
877 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -0700878 has_data = consumebytes(sizeof(*has_data), &serialized,
879 &length);
880 if (!has_data)
881 goto bad_mounts;
882 if (*has_data) {
883 data = consumestr(&serialized, &length);
884 if (!data)
885 goto bad_mounts;
886 }
Dylan Reid648b2202015-10-23 00:50:00 -0700887 flags = consumebytes(sizeof(*flags), &serialized, &length);
888 if (!flags)
889 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -0700890 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -0700891 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400892 }
893
Dylan Reid605ce7f2016-01-19 19:21:00 -0800894 count = j->cgroup_count;
895 j->cgroup_count = 0;
896 for (i = 0; i < count; ++i) {
897 char *cgroup = consumestr(&serialized, &length);
898 if (!cgroup)
899 goto bad_cgroups;
900 j->cgroups[i] = strdup(cgroup);
901 if (!j->cgroups[i])
902 goto bad_cgroups;
903 ++j->cgroup_count;
904 }
905
Elly Jonese1749eb2011-10-07 13:54:59 -0400906 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500907
Dylan Reid605ce7f2016-01-19 19:21:00 -0800908bad_cgroups:
909 while (j->mounts_head) {
910 struct mountpoint *m = j->mounts_head;
911 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -0700912 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -0800913 free(m->type);
914 free(m->dest);
915 free(m->src);
916 free(m);
917 }
918 for (i = 0; i < j->cgroup_count; ++i)
919 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -0700920bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800921 if (j->flags.seccomp_filter && j->filter_len > 0) {
922 free(j->filter_prog->filter);
923 free(j->filter_prog);
924 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800925bad_filter_prog_instrs:
926 if (j->filter_prog)
927 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -0500928bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800929 if (j->alt_syscall_table)
930 free(j->alt_syscall_table);
931bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500932 if (j->chrootdir)
933 free(j->chrootdir);
934bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800935 if (j->suppl_gid_list)
936 free(j->suppl_gid_list);
937bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -0500938 if (j->user)
939 free(j->user);
940clear_pointers:
941 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800942 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500943 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800944 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800945 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500946out:
947 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500948}
949
Dylan Reidce5b55e2016-01-13 11:04:16 -0800950static void write_ugid_mappings(const struct minijail *j)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800951{
952 int fd, ret, len;
953 size_t sz;
954 char fname[32];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800955
956 sz = sizeof(fname);
957 if (j->uidmap) {
958 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700959 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800960 die("failed to write file name of uid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -0800961 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800962 if (fd < 0)
963 pdie("failed to open '%s'", fname);
964 len = strlen(j->uidmap);
965 if (write(fd, j->uidmap, len) < len)
966 die("failed to set uid_map");
967 close(fd);
968 }
969 if (j->gidmap) {
970 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700971 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800972 die("failed to write file name of gid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -0800973 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800974 if (fd < 0)
975 pdie("failed to open '%s'", fname);
976 len = strlen(j->gidmap);
977 if (write(fd, j->gidmap, len) < len)
978 die("failed to set gid_map");
979 close(fd);
980 }
Dylan Reidce5b55e2016-01-13 11:04:16 -0800981}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800982
Dylan Reidce5b55e2016-01-13 11:04:16 -0800983static void parent_setup_complete(int *pipe_fds)
984{
985 close(pipe_fds[0]);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800986 close(pipe_fds[1]);
987}
988
Dylan Reidce5b55e2016-01-13 11:04:16 -0800989/*
990 * wait_for_parent_setup: Called by the child process to wait for any
991 * further parent-side setup to complete before continuing.
992 */
993static void wait_for_parent_setup(int *pipe_fds)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800994{
995 char buf;
996
997 close(pipe_fds[1]);
998
Dylan Reidce5b55e2016-01-13 11:04:16 -0800999 /* Wait for parent to complete setup and close the pipe. */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001000 if (read(pipe_fds[0], &buf, 1) != 0)
1001 die("failed to sync with parent");
1002 close(pipe_fds[0]);
Dylan Reidce5b55e2016-01-13 11:04:16 -08001003}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001004
Dylan Reidce5b55e2016-01-13 11:04:16 -08001005static void enter_user_namespace(const struct minijail *j)
1006{
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001007 if (j->uidmap && setresuid(0, 0, 0))
1008 pdie("setresuid");
1009 if (j->gidmap && setresgid(0, 0, 0))
1010 pdie("setresgid");
1011}
1012
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001013/*
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001014 * setup_mount_destination: Ensures the mount target exists.
1015 * Creates it if needed and possible.
Dylan Reideec77962016-06-30 19:35:10 -07001016 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001017int setup_mount_destination(const char *source, const char *dest, uid_t uid,
1018 uid_t gid)
Dylan Reideec77962016-06-30 19:35:10 -07001019{
1020 int rc;
1021 struct stat st_buf;
1022
1023 rc = stat(dest, &st_buf);
1024 if (rc == 0) /* destination exists */
1025 return 0;
1026
1027 /*
1028 * Try to create the destination.
1029 * Either make a directory or touch a file depending on the source type.
1030 * If the source doesn't exist, assume it is a filesystem type such as
1031 * "tmpfs" and create a directory to mount it on.
1032 */
1033 rc = stat(source, &st_buf);
1034 if (rc || S_ISDIR(st_buf.st_mode) || S_ISBLK(st_buf.st_mode)) {
1035 if (mkdir(dest, 0700))
1036 return -errno;
1037 } else {
1038 int fd = open(dest, O_RDWR | O_CREAT, 0700);
1039 if (fd < 0)
1040 return -errno;
1041 close(fd);
1042 }
1043 return chown(dest, uid, gid);
1044}
1045
1046/*
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001047 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001048 * @j Minijail these mounts are for
1049 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001050 *
1051 * Returns 0 for success.
1052 */
Dylan Reid648b2202015-10-23 00:50:00 -07001053static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001054{
Dylan Reid648b2202015-10-23 00:50:00 -07001055 int ret;
1056 char *dest;
1057 int remount_ro = 0;
1058
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001059 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001060 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001061 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001062
Dylan Reideec77962016-06-30 19:35:10 -07001063 if (setup_mount_destination(m->src, dest, j->uid, j->gid))
1064 pdie("creating mount target '%s' failed", dest);
1065
Dylan Reid648b2202015-10-23 00:50:00 -07001066 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001067 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1068 * can't both be specified in the original bind mount.
1069 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001070 */
1071 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1072 remount_ro = 1;
1073 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001074 }
Dylan Reid648b2202015-10-23 00:50:00 -07001075
Dylan Reid81e23972016-05-18 14:06:35 -07001076 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001077 if (ret)
1078 pdie("mount: %s -> %s", m->src, dest);
1079
1080 if (remount_ro) {
1081 m->flags |= MS_RDONLY;
1082 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001083 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001084 if (ret)
1085 pdie("bind ro: %s -> %s", m->src, dest);
1086 }
1087
Elly Jones51a5b6c2011-10-12 19:09:26 -04001088 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001089 if (m->next)
1090 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001091 return ret;
1092}
1093
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001094int enter_chroot(const struct minijail *j)
1095{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001096 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001097
1098 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001099 return ret;
1100
1101 if (chroot(j->chrootdir))
1102 return -errno;
1103
1104 if (chdir("/"))
1105 return -errno;
1106
1107 return 0;
1108}
1109
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001110int enter_pivot_root(const struct minijail *j)
1111{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001112 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001113
1114 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001115 return ret;
1116
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001117 /*
1118 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001119 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001120 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001121 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001122 if (oldroot < 0)
1123 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001124 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001125 if (newroot < 0)
1126 pdie("failed to open %s for fchdir", j->chrootdir);
1127
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001128 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001129 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001130 * do a self bind mount.
1131 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001132 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1133 pdie("failed to bind mount '%s'", j->chrootdir);
1134 if (chdir(j->chrootdir))
1135 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001136 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001137 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001138
1139 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001140 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001141 * change to the old root and unmount it.
1142 */
1143 if (fchdir(oldroot))
1144 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001145
1146 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001147 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1148 * there could be a shared mount point under |oldroot|. In that case,
1149 * mounts under this shared mount point will be unmounted below, and
1150 * this unmounting will propagate to the original mount namespace
1151 * (because the mount point is shared). To prevent this unexpected
1152 * unmounting, remove these mounts from their peer groups by recursively
1153 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001154 */
1155 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001156 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001157 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001158 if (umount2(".", MNT_DETACH))
1159 pdie("umount(/)");
1160 /* Change back to the new root. */
1161 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001162 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001163 if (close(oldroot))
1164 return -errno;
1165 if (close(newroot))
1166 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001167 if (chroot("/"))
1168 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001169 /* Set correct CWD for getcwd(3). */
1170 if (chdir("/"))
1171 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001172
1173 return 0;
1174}
1175
Lee Campbell11af0622014-05-22 12:36:04 -07001176int mount_tmp(void)
1177{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001178 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001179}
1180
Dylan Reid791f5772015-09-14 20:02:42 -07001181int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001182{
1183 const char *kProcPath = "/proc";
1184 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001185 /*
1186 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001187 * /proc in our namespace, which means using MS_REMOUNT here would
1188 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001189 * namespace (!). Instead, remove their mount from our namespace lazily
1190 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001191 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001192 if (umount2(kProcPath, MNT_DETACH)) {
1193 /*
1194 * If we are in a new user namespace, umount(2) will fail.
1195 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1196 */
1197 if (j->flags.userns) {
1198 info("umount(/proc, MNT_DETACH) failed, "
1199 "this is expected when using user namespaces");
1200 } else {
1201 return -errno;
1202 }
1203 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001204 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1205 return -errno;
1206 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001207}
1208
Dylan Reid605ce7f2016-01-19 19:21:00 -08001209static void write_pid_to_path(pid_t pid, const char *path)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001210{
Dylan Reid605ce7f2016-01-19 19:21:00 -08001211 FILE *fp = fopen(path, "w");
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001212
1213 if (!fp)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001214 pdie("failed to open '%s'", path);
1215 if (fprintf(fp, "%d\n", (int)pid) < 0)
1216 pdie("fprintf(%s)", path);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001217 if (fclose(fp))
Dylan Reid605ce7f2016-01-19 19:21:00 -08001218 pdie("fclose(%s)", path);
1219}
1220
1221static void write_pid_file(const struct minijail *j)
1222{
1223 write_pid_to_path(j->initpid, j->pid_file_path);
1224}
1225
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001226static void add_to_cgroups(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001227{
1228 size_t i;
1229
1230 for (i = 0; i < j->cgroup_count; ++i)
1231 write_pid_to_path(j->initpid, j->cgroups[i]);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001232}
1233
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001234void drop_ugid(const struct minijail *j)
1235{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001236 if (j->flags.usergroups && j->flags.suppl_gids) {
1237 die("tried to inherit *and* set supplementary groups;"
1238 " can only do one");
1239 }
1240
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001241 if (j->flags.usergroups) {
1242 if (initgroups(j->user, j->usergid))
1243 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001244 } else if (j->flags.suppl_gids) {
1245 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1246 pdie("setgroups");
1247 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001248 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001249 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001250 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001251 * users.
1252 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001253 if ((j->uid || j->gid) && setgroups(0, NULL))
1254 pdie("setgroups");
1255 }
1256
1257 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1258 pdie("setresgid");
1259
1260 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1261 pdie("setresuid");
1262}
1263
Mike Frysinger3adfef72013-05-09 17:19:08 -04001264/*
1265 * We specifically do not use cap_valid() as that only tells us the last
1266 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001267 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001268 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001269 * kernel).
1270 * Normally, we suck up the answer via /proc. On Android, not all processes are
1271 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1272 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001273 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001274static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001275{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001276 unsigned int last_valid_cap = 0;
1277 if (is_android()) {
1278 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1279 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001280
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001281 /* |last_valid_cap| will be the first failing value. */
1282 if (last_valid_cap > 0) {
1283 last_valid_cap--;
1284 }
1285 } else {
1286 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1287 FILE *fp = fopen(cap_file, "re");
1288 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1289 pdie("fscanf(%s)", cap_file);
1290 fclose(fp);
1291 }
Dylan Reidf682d472015-09-17 21:39:07 -07001292 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001293}
1294
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001295static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1296{
1297 const uint64_t one = 1;
1298 unsigned int i;
1299 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1300 if (keep_mask & (one << i))
1301 continue;
1302 if (prctl(PR_CAPBSET_DROP, i))
1303 pdie("could not drop capability from bounding set");
1304 }
1305}
1306
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001307void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001308{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001309 if (!j->flags.use_caps)
1310 return;
1311
Elly Jonese1749eb2011-10-07 13:54:59 -04001312 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001313 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001314 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001315 unsigned int i;
1316 if (!caps)
1317 die("can't get process caps");
1318 if (cap_clear_flag(caps, CAP_INHERITABLE))
1319 die("can't clear inheritable caps");
1320 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1321 die("can't clear effective caps");
1322 if (cap_clear_flag(caps, CAP_PERMITTED))
1323 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001324 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001325 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001326 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001327 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001328 flag[0] = i;
1329 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001330 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001331 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001332 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001333 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001334 die("can't add inheritable cap");
1335 }
1336 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001337 die("can't apply initial cleaned capset");
1338
1339 /*
1340 * Instead of dropping bounding set first, do it here in case
1341 * the caller had a more permissive bounding set which could
1342 * have been used above to raise a capability that wasn't already
1343 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1344 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001345 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001346
1347 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001348 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001349 flag[0] = CAP_SETPCAP;
1350 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1351 die("can't clear effective cap");
1352 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1353 die("can't clear permitted cap");
1354 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1355 die("can't clear inheritable cap");
1356 }
1357
1358 if (cap_set_proc(caps))
1359 die("can't apply final cleaned capset");
1360
1361 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001362}
1363
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001364void set_seccomp_filter(const struct minijail *j)
1365{
1366 /*
1367 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1368 * in the kernel source tree for an explanation of the parameters.
1369 */
1370 if (j->flags.no_new_privs) {
1371 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1372 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1373 }
1374
1375 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001376 * Code running with ASan
1377 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1378 * will make system calls not included in the syscall filter policy,
1379 * which will likely crash the program. Skip setting seccomp filter in
1380 * that case.
1381 * 'running_with_asan()' has no inputs and is completely defined at
1382 * build time, so this cannot be used by an attacker to skip setting
1383 * seccomp filter.
1384 */
1385 if (j->flags.seccomp_filter && running_with_asan()) {
1386 warn("running with ASan, not setting seccomp filter");
1387 return;
1388 }
1389
1390 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001391 * If we're logging seccomp filter failures,
1392 * install the SIGSYS handler first.
1393 */
1394 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1395 if (install_sigsys_handler())
1396 pdie("install SIGSYS handler");
1397 warn("logging seccomp filter failures");
1398 }
1399
1400 /*
1401 * Install the syscall filter.
1402 */
1403 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001404 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1405 j->filter_prog)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001406 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001407 warn("seccomp not supported");
1408 return;
1409 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001410 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001411 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001412 }
1413}
1414
Will Drewry6ac91122011-10-21 16:38:58 -05001415void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001416{
Dylan Reidf682d472015-09-17 21:39:07 -07001417 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001418 * If we're dropping caps, get the last valid cap from /proc now,
1419 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001420 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001421 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001422 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001423 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001424
Elly Jonese1749eb2011-10-07 13:54:59 -04001425 if (j->flags.pids)
1426 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001427 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001428
Elly Jonese1749eb2011-10-07 13:54:59 -04001429 if (j->flags.usergroups && !j->user)
1430 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001431
Elly Jonesdd3e8512012-01-23 15:13:38 -05001432 /*
1433 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001434 * so we don't even try. If any of our operations fail, we abort() the
1435 * entire process.
1436 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001437 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1438 pdie("setns(CLONE_NEWNS)");
1439
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001440 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001441 if (unshare(CLONE_NEWNS))
1442 pdie("unshare(vfs)");
1443 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001444 * Unless asked not to, remount all filesystems as private.
1445 * If they are shared, new bind mounts will creep out of our
1446 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001447 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1448 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001449 if (!j->flags.skip_remount_private) {
1450 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1451 pdie("mount(/, private)");
1452 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001453 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001454
Dylan Reidf7942472015-11-18 17:55:26 -08001455 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1456 pdie("unshare(ipc)");
1457 }
1458
Dylan Reid1102f5a2015-09-15 11:52:20 -07001459 if (j->flags.enter_net) {
1460 if (setns(j->netns_fd, CLONE_NEWNET))
1461 pdie("setns(CLONE_NEWNET)");
1462 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001463 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001464 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001465
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001466 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
1467 pdie("unshare(cgroups)");
1468
Elly Jones51a5b6c2011-10-12 19:09:26 -04001469 if (j->flags.chroot && enter_chroot(j))
1470 pdie("chroot");
1471
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001472 if (j->flags.pivot_root && enter_pivot_root(j))
1473 pdie("pivot_root");
1474
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001475 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001476 pdie("mount_tmp");
1477
Dylan Reid791f5772015-09-14 20:02:42 -07001478 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001479 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001480
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001481 /*
1482 * If we're only dropping capabilities from the bounding set, but not
1483 * from the thread's (permitted|inheritable|effective) sets, do it now.
1484 */
1485 if (j->flags.capbset_drop) {
1486 drop_capbset(j->cap_bset, last_valid_cap);
1487 }
1488
1489 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001490 /*
1491 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001492 * capability to change uids, our attempt to use setuid()
1493 * below will fail. Hang on to root caps across setuid(), then
1494 * lock securebits.
1495 */
1496 if (prctl(PR_SET_KEEPCAPS, 1))
1497 pdie("prctl(PR_SET_KEEPCAPS)");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001498
1499 /*
1500 * Kernels 4.3+ define a new securebit
1501 * (SECURE_NO_CAP_AMBIENT_RAISE), so using the SECURE_ALL_BITS
1502 * and SECURE_ALL_LOCKS masks from newer kernel headers will
1503 * return EPERM on older kernels. Detect this, and retry with
1504 * the right mask for older (2.6.26-4.2) kernels.
1505 */
1506 int securebits_ret = prctl(PR_SET_SECUREBITS,
1507 SECURE_ALL_BITS | SECURE_ALL_LOCKS);
1508 if (securebits_ret < 0) {
1509 if (errno == EPERM) {
1510 /* Possibly running on kernel < 4.3. */
1511 securebits_ret = prctl(
1512 PR_SET_SECUREBITS,
1513 OLD_SECURE_ALL_BITS | OLD_SECURE_ALL_LOCKS);
1514 }
1515 }
1516 if (securebits_ret < 0)
Elly Jonese1749eb2011-10-07 13:54:59 -04001517 pdie("prctl(PR_SET_SECUREBITS)");
1518 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001519
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001520 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001521 /*
1522 * If we're setting no_new_privs, we can drop privileges
1523 * before setting seccomp filter. This way filter policies
1524 * don't need to allow privilege-dropping syscalls.
1525 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001526 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001527 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001528 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001529 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001530 /*
1531 * If we're not setting no_new_privs,
1532 * we need to set seccomp filter *before* dropping privileges.
1533 * WARNING: this means that filter policies *must* allow
1534 * setgroups()/setresgid()/setresuid() for dropping root and
1535 * capget()/capset()/prctl() for dropping caps.
1536 */
1537 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001538 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001539 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001540 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001541
Elly Jonesdd3e8512012-01-23 15:13:38 -05001542 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001543 * Select the specified alternate syscall table. The table must not
1544 * block prctl(2) if we're using seccomp as well.
1545 */
1546 if (j->flags.alt_syscall) {
1547 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1548 pdie("prctl(PR_ALT_SYSCALL)");
1549 }
1550
1551 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001552 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001553 * privilege-dropping syscalls :)
1554 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001555 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001556 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001557 warn("seccomp not supported");
1558 return;
1559 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001560 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001561 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001562}
1563
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001564/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001565static int init_exitstatus = 0;
1566
Will Drewry6ac91122011-10-21 16:38:58 -05001567void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001568{
1569 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001570}
1571
Will Drewry6ac91122011-10-21 16:38:58 -05001572int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001573{
1574 pid_t pid;
1575 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001576 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001577 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001578 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001579 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001580 /*
1581 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001582 * left inside our pid namespace or we get a signal.
1583 */
1584 if (pid == rootpid)
1585 init_exitstatus = status;
1586 }
1587 if (!WIFEXITED(init_exitstatus))
1588 _exit(MINIJAIL_ERR_INIT);
1589 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001590}
1591
Will Drewry6ac91122011-10-21 16:38:58 -05001592int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001593{
1594 size_t sz = 0;
1595 size_t bytes = read(fd, &sz, sizeof(sz));
1596 char *buf;
1597 int r;
1598 if (sizeof(sz) != bytes)
1599 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001600 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001601 return -E2BIG;
1602 buf = malloc(sz);
1603 if (!buf)
1604 return -ENOMEM;
1605 bytes = read(fd, buf, sz);
1606 if (bytes != sz) {
1607 free(buf);
1608 return -EINVAL;
1609 }
1610 r = minijail_unmarshal(j, buf, sz);
1611 free(buf);
1612 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001613}
1614
Will Drewry6ac91122011-10-21 16:38:58 -05001615int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001616{
1617 char *buf;
1618 size_t sz = minijail_size(j);
1619 ssize_t written;
1620 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001621
Elly Jonese1749eb2011-10-07 13:54:59 -04001622 if (!sz)
1623 return -EINVAL;
1624 buf = malloc(sz);
1625 r = minijail_marshal(j, buf, sz);
1626 if (r) {
1627 free(buf);
1628 return r;
1629 }
1630 /* Sends [size][minijail]. */
1631 written = write(fd, &sz, sizeof(sz));
1632 if (written != sizeof(sz)) {
1633 free(buf);
1634 return -EFAULT;
1635 }
1636 written = write(fd, buf, sz);
1637 if (written < 0 || (size_t) written != sz) {
1638 free(buf);
1639 return -EFAULT;
1640 }
1641 free(buf);
1642 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001643}
Elly Jonescd7a9042011-07-22 13:56:51 -04001644
Will Drewry6ac91122011-10-21 16:38:58 -05001645int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001646{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001647#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001648 /* Don't use LDPRELOAD on Brillo. */
1649 return 0;
1650#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001651 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1652 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1653 if (!newenv)
1654 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001655
Elly Jonese1749eb2011-10-07 13:54:59 -04001656 /* Only insert a separating space if we have something to separate... */
1657 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1658 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001659
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001660 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001661 setenv(kLdPreloadEnvVar, newenv, 1);
1662 free(newenv);
1663 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001664#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001665}
1666
Will Drewry6ac91122011-10-21 16:38:58 -05001667int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001668{
1669 int r = pipe(fds);
1670 char fd_buf[11];
1671 if (r)
1672 return r;
1673 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1674 if (r <= 0)
1675 return -EINVAL;
1676 setenv(kFdEnvVar, fd_buf, 1);
1677 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001678}
1679
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001680int setup_pipe_end(int fds[2], size_t index)
1681{
1682 if (index > 1)
1683 return -1;
1684
1685 close(fds[1 - index]);
1686 return fds[index];
1687}
1688
1689int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1690{
1691 if (index > 1)
1692 return -1;
1693
1694 close(fds[1 - index]);
1695 /* dup2(2) the corresponding end of the pipe into |fd|. */
1696 return dup2(fds[index], fd);
1697}
1698
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001699int minijail_run_internal(struct minijail *j, const char *filename,
1700 char *const argv[], pid_t *pchild_pid,
1701 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1702 int use_preload);
1703
Will Drewry6ac91122011-10-21 16:38:58 -05001704int API minijail_run(struct minijail *j, const char *filename,
1705 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001706{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001707 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1708 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001709}
1710
1711int API minijail_run_pid(struct minijail *j, const char *filename,
1712 char *const argv[], pid_t *pchild_pid)
1713{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001714 return minijail_run_internal(j, filename, argv, pchild_pid,
1715 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001716}
1717
1718int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001719 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001720{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001721 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1722 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001723}
1724
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001725int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001726 char *const argv[], pid_t *pchild_pid,
1727 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001728{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001729 return minijail_run_internal(j, filename, argv, pchild_pid,
1730 pstdin_fd, pstdout_fd, pstderr_fd, true);
1731}
1732
1733int API minijail_run_no_preload(struct minijail *j, const char *filename,
1734 char *const argv[])
1735{
1736 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1737 false);
1738}
1739
Samuel Tan63187f42015-10-16 13:01:53 -07001740int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001741 const char *filename,
1742 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001743 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001744 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001745 int *pstderr_fd)
1746{
Samuel Tan63187f42015-10-16 13:01:53 -07001747 return minijail_run_internal(j, filename, argv, pchild_pid,
1748 pstdin_fd, pstdout_fd, pstderr_fd, false);
1749}
1750
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001751int minijail_run_internal(struct minijail *j, const char *filename,
1752 char *const argv[], pid_t *pchild_pid,
1753 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1754 int use_preload)
1755{
Elly Jonese1749eb2011-10-07 13:54:59 -04001756 char *oldenv, *oldenv_copy = NULL;
1757 pid_t child_pid;
1758 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001759 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001760 int stdout_fds[2];
1761 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001762 int child_sync_pipe_fds[2];
1763 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001764 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001765 /* We need to remember this across the minijail_preexec() call. */
1766 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001767 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001768
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001769 if (use_preload) {
1770 oldenv = getenv(kLdPreloadEnvVar);
1771 if (oldenv) {
1772 oldenv_copy = strdup(oldenv);
1773 if (!oldenv_copy)
1774 return -ENOMEM;
1775 }
1776
1777 if (setup_preload())
1778 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001779 }
Will Drewryf89aef52011-09-16 16:48:57 -05001780
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001781 if (!use_preload) {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04001782 if (j->flags.use_caps && j->caps != 0)
1783 die("non-empty capabilities are not supported without LD_PRELOAD");
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001784 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001785
Elly Jonesdd3e8512012-01-23 15:13:38 -05001786 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001787 * Make the process group ID of this process equal to its PID, so that
1788 * both the Minijail process and the jailed process can be killed
1789 * together.
1790 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1791 * the process is already a process group leader.
1792 */
1793 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1794 if (errno != EPERM) {
1795 pdie("setpgid(0, 0)");
1796 }
1797 }
1798
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001799 if (use_preload) {
1800 /*
1801 * Before we fork(2) and execve(2) the child process, we need
1802 * to open a pipe(2) to send the minijail configuration over.
1803 */
1804 if (setup_pipe(pipe_fds))
1805 return -EFAULT;
1806 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001807
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001808 /*
1809 * If we want to write to the child process' standard input,
1810 * create the pipe(2) now.
1811 */
1812 if (pstdin_fd) {
1813 if (pipe(stdin_fds))
1814 return -EFAULT;
1815 }
1816
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001817 /*
1818 * If we want to read from the child process' standard output,
1819 * create the pipe(2) now.
1820 */
1821 if (pstdout_fd) {
1822 if (pipe(stdout_fds))
1823 return -EFAULT;
1824 }
1825
1826 /*
1827 * If we want to read from the child process' standard error,
1828 * create the pipe(2) now.
1829 */
1830 if (pstderr_fd) {
1831 if (pipe(stderr_fds))
1832 return -EFAULT;
1833 }
1834
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001835 /*
1836 * If we want to set up a new uid/gid mapping in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001837 * or if we need to add the child process to cgroups, create the pipe(2)
1838 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001839 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001840 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001841 sync_child = 1;
1842 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001843 return -EFAULT;
1844 }
1845
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001846 /*
1847 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001848 *
1849 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1850 *
1851 * In multithreaded programs, there are a bunch of locks inside libc,
1852 * some of which may be held by other threads at the time that we call
1853 * minijail_run_pid(). If we call fork(), glibc does its level best to
1854 * ensure that we hold all of these locks before it calls clone()
1855 * internally and drop them after clone() returns, but when we call
1856 * sys_clone(2) directly, all that gets bypassed and we end up with a
1857 * child address space where some of libc's important locks are held by
1858 * other threads (which did not get cloned, and hence will never release
1859 * those locks). This is okay so long as we call exec() immediately
1860 * after, but a bunch of seemingly-innocent libc functions like setenv()
1861 * take locks.
1862 *
1863 * Hence, only call sys_clone() if we need to, in order to get at pid
1864 * namespacing. If we follow this path, the child's address space might
1865 * have broken locks; you may only call functions that do not acquire
1866 * any locks.
1867 *
1868 * Unfortunately, fork() acquires every lock it can get its hands on, as
1869 * previously detailed, so this function is highly likely to deadlock
1870 * later on (see "deadlock here") if we're multithreaded.
1871 *
1872 * We might hack around this by having the clone()d child (init of the
1873 * pid namespace) return directly, rather than leaving the clone()d
1874 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001875 * its fork()ed child return in turn), but that process would be
1876 * crippled with its libc locks potentially broken. We might try
1877 * fork()ing in the parent before we clone() to ensure that we own all
1878 * the locks, but then we have to have the forked child hanging around
1879 * consuming resources (and possibly having file descriptors / shared
1880 * memory regions / etc attached). We'd need to keep the child around to
1881 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04001882 *
1883 * TODO(ellyjones): figure out if the "forked child hanging around"
1884 * problem is fixable or not. It would be nice if we worked in this
1885 * case.
1886 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001887 if (pid_namespace) {
1888 int clone_flags = CLONE_NEWPID | SIGCHLD;
1889 if (j->flags.userns)
1890 clone_flags |= CLONE_NEWUSER;
1891 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001892 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001893 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001894 }
Elly Jones761b7412012-06-13 15:49:52 -04001895
Elly Jonese1749eb2011-10-07 13:54:59 -04001896 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001897 if (use_preload) {
1898 free(oldenv_copy);
1899 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001900 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001901 }
Will Drewryf89aef52011-09-16 16:48:57 -05001902
Elly Jonese1749eb2011-10-07 13:54:59 -04001903 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001904 if (use_preload) {
1905 /* Restore parent's LD_PRELOAD. */
1906 if (oldenv_copy) {
1907 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1908 free(oldenv_copy);
1909 } else {
1910 unsetenv(kLdPreloadEnvVar);
1911 }
1912 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001913 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001914
Elly Jonese1749eb2011-10-07 13:54:59 -04001915 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001916
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001917 if (j->flags.pid_file)
1918 write_pid_file(j);
1919
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001920 if (j->flags.cgroups)
1921 add_to_cgroups(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001922
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001923 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001924 write_ugid_mappings(j);
1925
1926 if (sync_child)
1927 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001928
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001929 if (use_preload) {
1930 /* Send marshalled minijail. */
1931 close(pipe_fds[0]); /* read endpoint */
1932 ret = minijail_to_fd(j, pipe_fds[1]);
1933 close(pipe_fds[1]); /* write endpoint */
1934 if (ret) {
1935 kill(j->initpid, SIGKILL);
1936 die("failed to send marshalled minijail");
1937 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001938 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001939
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001940 if (pchild_pid)
1941 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001942
1943 /*
1944 * If we want to write to the child process' standard input,
1945 * set up the write end of the pipe.
1946 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001947 if (pstdin_fd)
1948 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001949 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001950
1951 /*
1952 * If we want to read from the child process' standard output,
1953 * set up the read end of the pipe.
1954 */
1955 if (pstdout_fd)
1956 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001957 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001958
1959 /*
1960 * If we want to read from the child process' standard error,
1961 * set up the read end of the pipe.
1962 */
1963 if (pstderr_fd)
1964 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001965 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001966
Elly Jonese1749eb2011-10-07 13:54:59 -04001967 return 0;
1968 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04001969 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001970 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001971
Peter Qiu2860c462015-12-16 15:13:06 -08001972 if (j->flags.reset_signal_mask) {
1973 sigset_t signal_mask;
1974 if (sigemptyset(&signal_mask) != 0)
1975 pdie("sigemptyset failed");
1976 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
1977 pdie("sigprocmask failed");
1978 }
1979
Dylan Reidce5b55e2016-01-13 11:04:16 -08001980 if (sync_child)
1981 wait_for_parent_setup(child_sync_pipe_fds);
1982
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001983 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001984 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001985
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001986 /*
1987 * If we want to write to the jailed process' standard input,
1988 * set up the read end of the pipe.
1989 */
1990 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001991 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1992 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001993 die("failed to set up stdin pipe");
1994 }
1995
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001996 /*
1997 * If we want to read from the jailed process' standard output,
1998 * set up the write end of the pipe.
1999 */
2000 if (pstdout_fd) {
2001 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2002 STDOUT_FILENO) < 0)
2003 die("failed to set up stdout pipe");
2004 }
2005
2006 /*
2007 * If we want to read from the jailed process' standard error,
2008 * set up the write end of the pipe.
2009 */
2010 if (pstderr_fd) {
2011 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2012 STDERR_FILENO) < 0)
2013 die("failed to set up stderr pipe");
2014 }
2015
Dylan Reid791f5772015-09-14 20:02:42 -07002016 /* If running an init program, let it decide when/how to mount /proc. */
2017 if (pid_namespace && !do_init)
2018 j->flags.remount_proc_ro = 0;
2019
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002020 if (use_preload) {
2021 /* Strip out flags that cannot be inherited across execve(2). */
2022 minijail_preexec(j);
2023 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002024 /*
2025 * If not using LD_PRELOAD, do all jailing before execve(2).
2026 * Note that PID namespaces can only be entered on fork(2),
2027 * so that flag is still cleared.
2028 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002029 j->flags.pids = 0;
2030 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002031 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002032 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002033
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002034 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002035 /*
2036 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002037 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002038 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002039 * a child to actually run the program. If |do_init == 0|, we
2040 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002041 *
2042 * If we're multithreaded, we'll probably deadlock here. See
2043 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002044 */
2045 child_pid = fork();
2046 if (child_pid < 0)
2047 _exit(child_pid);
2048 else if (child_pid > 0)
2049 init(child_pid); /* never returns */
2050 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002051
Elly Jonesdd3e8512012-01-23 15:13:38 -05002052 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002053 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002054 * calling process
2055 * -> execve()-ing process
2056 * If we are:
2057 * calling process
2058 * -> init()-ing process
2059 * -> execve()-ing process
2060 */
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002061 ret = execve(filename, argv, environ);
2062 if (ret == -1) {
2063 pwarn("execve(%s) failed", filename);
2064 }
2065 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002066}
2067
Will Drewry6ac91122011-10-21 16:38:58 -05002068int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002069{
2070 int st;
2071 if (kill(j->initpid, SIGTERM))
2072 return -errno;
2073 if (waitpid(j->initpid, &st, 0) < 0)
2074 return -errno;
2075 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002076}
2077
Will Drewry6ac91122011-10-21 16:38:58 -05002078int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002079{
2080 int st;
2081 if (waitpid(j->initpid, &st, 0) < 0)
2082 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002083
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002084 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002085 int error_status = st;
2086 if (WIFSIGNALED(st)) {
2087 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002088 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002089 j->initpid, signum);
2090 /*
2091 * We return MINIJAIL_ERR_JAIL if the process received
2092 * SIGSYS, which happens when a syscall is blocked by
2093 * seccomp filters.
2094 * If not, we do what bash(1) does:
2095 * $? = 128 + signum
2096 */
2097 if (signum == SIGSYS) {
2098 error_status = MINIJAIL_ERR_JAIL;
2099 } else {
2100 error_status = 128 + signum;
2101 }
2102 }
2103 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002104 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002105
2106 int exit_status = WEXITSTATUS(st);
2107 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002108 info("child process %d exited with status %d",
2109 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002110
2111 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002112}
2113
Will Drewry6ac91122011-10-21 16:38:58 -05002114void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002115{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002116 size_t i;
2117
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002118 if (j->flags.seccomp_filter && j->filter_prog) {
2119 free(j->filter_prog->filter);
2120 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002121 }
Dylan Reid648b2202015-10-23 00:50:00 -07002122 while (j->mounts_head) {
2123 struct mountpoint *m = j->mounts_head;
2124 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002125 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002126 free(m->type);
2127 free(m->dest);
2128 free(m->src);
2129 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002130 }
Dylan Reid648b2202015-10-23 00:50:00 -07002131 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002132 if (j->user)
2133 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002134 if (j->suppl_gid_list)
2135 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002136 if (j->chrootdir)
2137 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08002138 if (j->alt_syscall_table)
2139 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002140 for (i = 0; i < j->cgroup_count; ++i)
2141 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002142 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002143}