blob: 2c57d4d067ec4a3c90dcd6d50b2a2e48b60d06f7 [file] [log] [blame]
Jorge Lucangeli Obesd613ab22015-03-03 14:22:50 -08001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04002 * Use of this source code is governed by a BSD-style license that can be
Will Drewry32ac9f52011-08-18 21:36:27 -05003 * found in the LICENSE file.
4 */
Elly Jonescd7a9042011-07-22 13:56:51 -04005
6#define _BSD_SOURCE
Arthur Gautier7a569072016-04-23 17:25:20 +00007#define _DEFAULT_SOURCE
Elly Jonescd7a9042011-07-22 13:56:51 -04008#define _GNU_SOURCE
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07009
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080010#include <asm/unistd.h>
Will Drewry32ac9f52011-08-18 21:36:27 -050011#include <ctype.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040012#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070013#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040014#include <grp.h>
15#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050016#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040017#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040018#include <pwd.h>
19#include <sched.h>
20#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050021#include <stdarg.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070022#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080023#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <syscall.h>
28#include <sys/capability.h>
29#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050030#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040031#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070032#include <sys/stat.h>
33#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080034#include <sys/user.h>
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -080035#include <sys/utsname.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040036#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040037#include <unistd.h>
38
39#include "libminijail.h"
40#include "libminijail-private.h"
41
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070042#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080043#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070044#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080045
Lei Zhangeee31552012-10-17 21:27:10 -070046#ifdef HAVE_SECUREBITS_H
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070047# include <linux/securebits.h>
Lei Zhangeee31552012-10-17 21:27:10 -070048#else
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070049# define SECURE_ALL_BITS 0x55
50# define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
51#endif
52/* For kernels < 4.3. */
53#define OLD_SECURE_ALL_BITS 0x15
54#define OLD_SECURE_ALL_LOCKS (OLD_SECURE_ALL_BITS << 1)
55
56/*
57 * Assert the value of SECURE_ALL_BITS at compile-time.
58 * Brillo devices are currently compiled against 4.4 kernel headers. Kernel 4.3
59 * added a new securebit.
60 * When a new securebit is added, the new SECURE_ALL_BITS mask will return EPERM
61 * when used on older kernels. The compile-time assert will catch this situation
62 * at compile time.
63 */
64#ifdef __BRILLO__
65_Static_assert(SECURE_ALL_BITS == 0x55, "SECURE_ALL_BITS == 0x55.");
Lei Zhangeee31552012-10-17 21:27:10 -070066#endif
67
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070068/* Until these are reliably available in linux/prctl.h. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080069#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070070# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080071#endif
72
Andrew Brestickereac28942015-11-11 16:04:46 -080073#ifndef PR_ALT_SYSCALL
74# define PR_ALT_SYSCALL 0x43724f53
75#endif
76
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080077/* For seccomp_filter using BPF. */
78#ifndef PR_SET_NO_NEW_PRIVS
79# define PR_SET_NO_NEW_PRIVS 38
80#endif
81#ifndef SECCOMP_MODE_FILTER
82# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050083#endif
84
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070085#ifdef USE_SECCOMP_SOFTFAIL
86# define SECCOMP_SOFTFAIL 1
87#else
88# define SECCOMP_SOFTFAIL 0
89#endif
90
Dylan Reid605ce7f2016-01-19 19:21:00 -080091#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
92
Dylan Reid648b2202015-10-23 00:50:00 -070093struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040094 char *src;
95 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070096 char *type;
97 unsigned long flags;
98 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040099};
100
Will Drewryf89aef52011-09-16 16:48:57 -0500101struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700102 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700103 * WARNING: if you add a flag here you need to make sure it's
104 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700105 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400106 struct {
107 int uid:1;
108 int gid:1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800109 int usergroups:1;
110 int suppl_gids:1;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800111 int use_caps:1;
112 int capbset_drop:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400113 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700114 int enter_vfs:1;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800115 int skip_remount_private:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400116 int pids:1;
Dylan Reidf7942472015-11-18 17:55:26 -0800117 int ipc:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400118 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700119 int enter_net:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800120 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400121 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -0700122 int remount_proc_ro:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700123 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400124 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700125 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400126 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800127 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700128 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800129 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800130 int pid_file:1;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800131 int cgroups:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800132 int alt_syscall:1;
Peter Qiu2860c462015-12-16 15:13:06 -0800133 int reset_signal_mask:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400134 } flags;
135 uid_t uid;
136 gid_t gid;
137 gid_t usergid;
138 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800139 size_t suppl_gid_count;
140 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400141 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800142 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400143 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700144 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700145 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400146 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800147 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800148 char *uidmap;
149 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800150 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800151 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800152 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700153 struct mountpoint *mounts_head;
154 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800155 size_t mounts_count;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800156 char *cgroups[MAX_CGROUPS];
157 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500158};
159
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700160/*
161 * Strip out flags meant for the parent.
162 * We keep things that are not inherited across execve(2) (e.g. capabilities),
163 * or are easier to set after execve(2) (e.g. seccomp filters).
164 */
165void minijail_preenter(struct minijail *j)
166{
167 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700168 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900169 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700170 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700171 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800172 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800173 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800174 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700175}
176
177/*
178 * Strip out flags meant for the child.
179 * We keep things that are inherited across execve(2).
180 */
181void minijail_preexec(struct minijail *j)
182{
183 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700184 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800185 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700186 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800187 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700188 if (j->user)
189 free(j->user);
190 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800191 if (j->suppl_gid_list)
192 free(j->suppl_gid_list);
193 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700194 memset(&j->flags, 0, sizeof(j->flags));
195 /* Now restore anything we meant to keep. */
196 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700197 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800198 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700199 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800200 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700201 /* Note, |pids| will already have been used before this call. */
202}
203
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800204/* Returns true if the kernel version is less than 3.8. */
205int seccomp_kernel_support_not_required()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800206{
207 int major, minor;
208 struct utsname uts;
209 return (uname(&uts) != -1 &&
210 sscanf(uts.release, "%d.%d", &major, &minor) == 2 &&
211 ((major < 3) || ((major == 3) && (minor < 8))));
212}
213
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800214/* Allow seccomp soft-fail on Android devices with kernel version < 3.8. */
215int can_softfail()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800216{
217#if SECCOMP_SOFTFAIL
218 if (is_android()) {
219 if (seccomp_kernel_support_not_required())
220 return 1;
221 else
222 return 0;
223 } else {
224 return 1;
225 }
226#endif
227 return 0;
228}
229
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700230/* Minijail API. */
231
Will Drewry6ac91122011-10-21 16:38:58 -0500232struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400233{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400234 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400235}
236
Will Drewry6ac91122011-10-21 16:38:58 -0500237void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400238{
239 if (uid == 0)
240 die("useless change to uid 0");
241 j->uid = uid;
242 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400243}
244
Will Drewry6ac91122011-10-21 16:38:58 -0500245void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400246{
247 if (gid == 0)
248 die("useless change to gid 0");
249 j->gid = gid;
250 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400251}
252
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800253void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
254 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800255{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800256 size_t i;
257
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800258 if (j->flags.usergroups)
259 die("cannot inherit *and* set supplementary groups");
260
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800261 if (size == 0) {
262 /* Clear supplementary groups. */
263 j->suppl_gid_list = NULL;
264 j->suppl_gid_count = 0;
265 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800266 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800267 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800268
269 /* Copy the gid_t array. */
270 j->suppl_gid_list = calloc(size, sizeof(gid_t));
271 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800272 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800273 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800274 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800275 j->suppl_gid_list[i] = list[i];
276 }
277 j->suppl_gid_count = size;
278 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800279}
280
Will Drewry6ac91122011-10-21 16:38:58 -0500281int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400282{
283 char *buf = NULL;
284 struct passwd pw;
285 struct passwd *ppw = NULL;
286 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
287 if (sz == -1)
288 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400289
Elly Jonesdd3e8512012-01-23 15:13:38 -0500290 /*
291 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400292 * the maximum needed size of the buffer, so we don't have to search.
293 */
294 buf = malloc(sz);
295 if (!buf)
296 return -ENOMEM;
297 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500298 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800299 * We're safe to free the buffer here. The strings inside |pw| point
300 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800301 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
302 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500303 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400304 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700305 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400306 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700307 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400308 minijail_change_uid(j, ppw->pw_uid);
309 j->user = strdup(user);
310 if (!j->user)
311 return -ENOMEM;
312 j->usergid = ppw->pw_gid;
313 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400314}
315
Will Drewry6ac91122011-10-21 16:38:58 -0500316int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400317{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700318 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700319 struct group gr;
320 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400321 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
322 if (sz == -1)
323 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400324
Elly Jonesdd3e8512012-01-23 15:13:38 -0500325 /*
326 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400327 * the maximum needed size of the buffer, so we don't have to search.
328 */
329 buf = malloc(sz);
330 if (!buf)
331 return -ENOMEM;
332 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500333 /*
334 * We're safe to free the buffer here. The strings inside gr point
335 * inside buf, but we don't use any of them; this leaves the pointers
336 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
337 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400338 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700339 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400340 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700341 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400342 minijail_change_gid(j, pgr->gr_gid);
343 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400344}
345
Will Drewry6ac91122011-10-21 16:38:58 -0500346void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400347{
348 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400349}
350
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700351void API minijail_no_new_privs(struct minijail *j)
352{
353 j->flags.no_new_privs = 1;
354}
355
Will Drewry6ac91122011-10-21 16:38:58 -0500356void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400357{
358 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500359}
360
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700361void API minijail_log_seccomp_filter_failures(struct minijail *j)
362{
363 j->flags.log_seccomp_filter = 1;
364}
365
Will Drewry6ac91122011-10-21 16:38:58 -0500366void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400367{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800368 /*
369 * 'minijail_use_caps' configures a runtime-capabilities-only
370 * environment, including a bounding set matching the thread's runtime
371 * (permitted|inheritable|effective) sets.
372 * Therefore, it will override any existing bounding set configurations
373 * since the latter would allow gaining extra runtime capabilities from
374 * file capabilities.
375 */
376 if (j->flags.capbset_drop) {
377 warn("overriding bounding set configuration");
378 j->cap_bset = 0;
379 j->flags.capbset_drop = 0;
380 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400381 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800382 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400383}
384
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800385void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
386{
387 if (j->flags.use_caps) {
388 /*
389 * 'minijail_use_caps' will have already configured a capability
390 * bounding set matching the (permitted|inheritable|effective)
391 * sets. Abort if the user tries to configure a separate
392 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
393 * are mutually exclusive.
394 */
395 die("runtime capabilities already configured, can't drop "
396 "bounding set separately");
397 }
398 j->cap_bset = capmask;
399 j->flags.capbset_drop = 1;
400}
401
402void API minijail_reset_signal_mask(struct minijail *j)
403{
Peter Qiu2860c462015-12-16 15:13:06 -0800404 j->flags.reset_signal_mask = 1;
405}
406
Will Drewry6ac91122011-10-21 16:38:58 -0500407void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400408{
409 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400410}
411
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700412void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
413{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800414 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700415 if (ns_fd < 0) {
416 pdie("failed to open namespace '%s'", ns_path);
417 }
418 j->mountns_fd = ns_fd;
419 j->flags.enter_vfs = 1;
420}
421
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800422void API minijail_skip_remount_private(struct minijail *j)
423{
424 j->flags.skip_remount_private = 1;
425}
426
Will Drewry6ac91122011-10-21 16:38:58 -0500427void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400428{
Elly Jonese58176c2012-01-23 11:46:17 -0500429 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700430 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400431 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800432 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400433}
434
Dylan Reidf7942472015-11-18 17:55:26 -0800435void API minijail_namespace_ipc(struct minijail *j)
436{
437 j->flags.ipc = 1;
438}
439
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400440void API minijail_namespace_net(struct minijail *j)
441{
442 j->flags.net = 1;
443}
444
Dylan Reid1102f5a2015-09-15 11:52:20 -0700445void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
446{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800447 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700448 if (ns_fd < 0) {
449 pdie("failed to open namespace '%s'", ns_path);
450 }
451 j->netns_fd = ns_fd;
452 j->flags.enter_net = 1;
453}
454
Dylan Reid791f5772015-09-14 20:02:42 -0700455void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400456{
457 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700458 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400459}
460
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800461void API minijail_namespace_user(struct minijail *j)
462{
463 j->flags.userns = 1;
464}
465
466int API minijail_uidmap(struct minijail *j, const char *uidmap)
467{
468 j->uidmap = strdup(uidmap);
469 if (!j->uidmap)
470 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800471 char *ch;
472 for (ch = j->uidmap; *ch; ch++) {
473 if (*ch == ',')
474 *ch = '\n';
475 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800476 return 0;
477}
478
479int API minijail_gidmap(struct minijail *j, const char *gidmap)
480{
481 j->gidmap = strdup(gidmap);
482 if (!j->gidmap)
483 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800484 char *ch;
485 for (ch = j->gidmap; *ch; ch++) {
486 if (*ch == ',')
487 *ch = '\n';
488 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800489 return 0;
490}
491
Will Drewry6ac91122011-10-21 16:38:58 -0500492void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400493{
494 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400495}
496
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800497void API minijail_run_as_init(struct minijail *j)
498{
499 /*
500 * Since the jailed program will become 'init' in the new PID namespace,
501 * Minijail does not need to fork an 'init' process.
502 */
503 j->flags.do_init = 0;
504}
505
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700506int API minijail_enter_chroot(struct minijail *j, const char *dir)
507{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400508 if (j->chrootdir)
509 return -EINVAL;
510 j->chrootdir = strdup(dir);
511 if (!j->chrootdir)
512 return -ENOMEM;
513 j->flags.chroot = 1;
514 return 0;
515}
516
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800517int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
518{
519 if (j->chrootdir)
520 return -EINVAL;
521 j->chrootdir = strdup(dir);
522 if (!j->chrootdir)
523 return -ENOMEM;
524 j->flags.pivot_root = 1;
525 return 0;
526}
527
Dylan Reida14e08d2015-10-22 21:05:29 -0700528static char *append_external_path(const char *external_path,
529 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700530{
Dylan Reida14e08d2015-10-22 21:05:29 -0700531 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700532 size_t pathlen;
533
Dylan Reid08946cc2015-09-16 19:10:57 -0700534 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700535 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
536 path = malloc(pathlen);
537 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700538
Dylan Reida14e08d2015-10-22 21:05:29 -0700539 return path;
540}
541
542char API *minijail_get_original_path(struct minijail *j,
543 const char *path_inside_chroot)
544{
Dylan Reid648b2202015-10-23 00:50:00 -0700545 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700546
Dylan Reid648b2202015-10-23 00:50:00 -0700547 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700548 while (b) {
549 /*
550 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700551 * mount, then the original path is exactly the source of
552 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700553 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700554 * mount source = /some/path/exe, mount dest =
555 * /chroot/path/exe Then when getting the original path of
556 * "/chroot/path/exe", the source of that mount,
557 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700558 */
559 if (!strcmp(b->dest, path_inside_chroot))
560 return strdup(b->src);
561
562 /*
563 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700564 * mount, take the suffix of the chroot path relative to the
565 * mount destination path, and append it to the mount source
566 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700567 */
568 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
569 const char *relative_path =
570 path_inside_chroot + strlen(b->dest);
571 return append_external_path(b->src, relative_path);
572 }
573 b = b->next;
574 }
575
576 /* If there is a chroot path, append |path_inside_chroot| to that. */
577 if (j->chrootdir)
578 return append_external_path(j->chrootdir, path_inside_chroot);
579
580 /* No chroot, so the path outside is the same as it is inside. */
581 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700582}
583
Lee Campbell11af0622014-05-22 12:36:04 -0700584void API minijail_mount_tmp(struct minijail *j)
585{
586 j->flags.mount_tmp = 1;
587}
588
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800589int API minijail_write_pid_file(struct minijail *j, const char *path)
590{
591 j->pid_file_path = strdup(path);
592 if (!j->pid_file_path)
593 return -ENOMEM;
594 j->flags.pid_file = 1;
595 return 0;
596}
597
Dylan Reid605ce7f2016-01-19 19:21:00 -0800598int API minijail_add_to_cgroup(struct minijail *j, const char *path)
599{
600 if (j->cgroup_count >= MAX_CGROUPS)
601 return -ENOMEM;
602 j->cgroups[j->cgroup_count] = strdup(path);
603 if (!j->cgroups[j->cgroup_count])
604 return -ENOMEM;
605 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800606 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800607 return 0;
608}
609
Dylan Reid648b2202015-10-23 00:50:00 -0700610int API minijail_mount(struct minijail *j, const char *src, const char *dest,
611 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700612{
Dylan Reid648b2202015-10-23 00:50:00 -0700613 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400614
615 if (*dest != '/')
616 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700617 m = calloc(1, sizeof(*m));
618 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400619 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700620 m->dest = strdup(dest);
621 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400622 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700623 m->src = strdup(src);
624 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400625 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700626 m->type = strdup(type);
627 if (!m->type)
628 goto error;
629 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400630
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800631 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400632
Elly Jonesdd3e8512012-01-23 15:13:38 -0500633 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700634 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400635 * containing vfs namespace.
636 */
637 minijail_namespace_vfs(j);
638
Dylan Reid648b2202015-10-23 00:50:00 -0700639 if (j->mounts_tail)
640 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400641 else
Dylan Reid648b2202015-10-23 00:50:00 -0700642 j->mounts_head = m;
643 j->mounts_tail = m;
644 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400645
646 return 0;
647
648error:
Dylan Reid648b2202015-10-23 00:50:00 -0700649 free(m->src);
650 free(m->dest);
651 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400652 return -ENOMEM;
653}
654
Dylan Reid648b2202015-10-23 00:50:00 -0700655int API minijail_bind(struct minijail *j, const char *src, const char *dest,
656 int writeable)
657{
658 unsigned long flags = MS_BIND;
659
660 if (!writeable)
661 flags |= MS_RDONLY;
662
663 return minijail_mount(j, src, dest, "", flags);
664}
665
Will Drewry6ac91122011-10-21 16:38:58 -0500666void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400667{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700668 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800669 if ((errno == EINVAL) && can_softfail()) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800670 warn("not loading seccomp filter,"
671 " seccomp not supported");
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800672 j->flags.seccomp_filter = 0;
673 j->flags.log_seccomp_filter = 0;
674 j->filter_len = 0;
675 j->filter_prog = NULL;
676 j->flags.no_new_privs = 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700677 }
678 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400679 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800680 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700681 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400682 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800683
684 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700685 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
686 die("failed to compile seccomp filter BPF program in '%s'",
687 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800688 }
689
690 j->filter_len = fprog->len;
691 j->filter_prog = fprog;
692
Elly Jonese1749eb2011-10-07 13:54:59 -0400693 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500694}
695
Andrew Brestickereac28942015-11-11 16:04:46 -0800696int API minijail_use_alt_syscall(struct minijail *j, const char *table)
697{
698 j->alt_syscall_table = strdup(table);
699 if (!j->alt_syscall_table)
700 return -ENOMEM;
701 j->flags.alt_syscall = 1;
702 return 0;
703}
704
Will Drewryf89aef52011-09-16 16:48:57 -0500705struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400706 size_t available;
707 size_t total;
708 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500709};
710
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800711void marshal_state_init(struct marshal_state *state, char *buf,
712 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400713{
714 state->available = available;
715 state->buf = buf;
716 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500717}
718
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800719void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400720{
721 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500722
Elly Jonese1749eb2011-10-07 13:54:59 -0400723 /* Up to |available| will be written. */
724 if (copy_len) {
725 memcpy(state->buf, src, copy_len);
726 state->buf += copy_len;
727 state->available -= copy_len;
728 }
729 /* |total| will contain the expected length. */
730 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500731}
732
Will Drewry6ac91122011-10-21 16:38:58 -0500733void minijail_marshal_helper(struct marshal_state *state,
734 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400735{
Dylan Reid648b2202015-10-23 00:50:00 -0700736 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800737 size_t i;
738
Elly Jonese1749eb2011-10-07 13:54:59 -0400739 marshal_append(state, (char *)j, sizeof(*j));
740 if (j->user)
741 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800742 if (j->suppl_gid_list) {
743 marshal_append(state, j->suppl_gid_list,
744 j->suppl_gid_count * sizeof(gid_t));
745 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400746 if (j->chrootdir)
747 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800748 if (j->alt_syscall_table) {
749 marshal_append(state, j->alt_syscall_table,
750 strlen(j->alt_syscall_table) + 1);
751 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800752 if (j->flags.seccomp_filter && j->filter_prog) {
753 struct sock_fprog *fp = j->filter_prog;
754 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800755 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400756 }
Dylan Reid648b2202015-10-23 00:50:00 -0700757 for (m = j->mounts_head; m; m = m->next) {
758 marshal_append(state, m->src, strlen(m->src) + 1);
759 marshal_append(state, m->dest, strlen(m->dest) + 1);
760 marshal_append(state, m->type, strlen(m->type) + 1);
761 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400762 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800763 for (i = 0; i < j->cgroup_count; ++i)
764 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500765}
766
Will Drewry6ac91122011-10-21 16:38:58 -0500767size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400768{
769 struct marshal_state state;
770 marshal_state_init(&state, NULL, 0);
771 minijail_marshal_helper(&state, j);
772 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500773}
774
Elly Jonese1749eb2011-10-07 13:54:59 -0400775int minijail_marshal(const struct minijail *j, char *buf, size_t available)
776{
777 struct marshal_state state;
778 marshal_state_init(&state, buf, available);
779 minijail_marshal_helper(&state, j);
780 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500781}
782
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800783/*
784 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
Elly Jones51a5b6c2011-10-12 19:09:26 -0400785 * @length Number of bytes to consume
786 * @buf Buffer to consume from
787 * @buflength Size of @buf
788 *
789 * Returns a pointer to the base of the bytes, or NULL for errors.
790 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700791void *consumebytes(size_t length, char **buf, size_t *buflength)
792{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400793 char *p = *buf;
794 if (length > *buflength)
795 return NULL;
796 *buf += length;
797 *buflength -= length;
798 return p;
799}
800
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800801/*
802 * consumestr: consumes a C string from a buffer @buf of length @length
Elly Jones51a5b6c2011-10-12 19:09:26 -0400803 * @buf Buffer to consume
804 * @length Length of buffer
805 *
806 * Returns a pointer to the base of the string, or NULL for errors.
807 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700808char *consumestr(char **buf, size_t *buflength)
809{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400810 size_t len = strnlen(*buf, *buflength);
811 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700812 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400813 return NULL;
814 return consumebytes(len + 1, buf, buflength);
815}
816
Elly Jonese1749eb2011-10-07 13:54:59 -0400817int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
818{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800819 size_t i;
820 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500821 int ret = -EINVAL;
822
Elly Jonese1749eb2011-10-07 13:54:59 -0400823 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500824 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400825 memcpy((void *)j, serialized, sizeof(*j));
826 serialized += sizeof(*j);
827 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500828
Will Drewrybee7ba72011-10-21 20:47:01 -0500829 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700830 j->mounts_head = NULL;
831 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800832 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500833
Elly Jonese1749eb2011-10-07 13:54:59 -0400834 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400835 char *user = consumestr(&serialized, &length);
836 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500837 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400838 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500839 if (!j->user)
840 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400841 }
Will Drewryf89aef52011-09-16 16:48:57 -0500842
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800843 if (j->suppl_gid_list) { /* stale pointer */
844 if (j->suppl_gid_count > NGROUPS_MAX) {
845 goto bad_gid_list;
846 }
847 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
848 void *gid_list_bytes =
849 consumebytes(gid_list_size, &serialized, &length);
850 if (!gid_list_bytes)
851 goto bad_gid_list;
852
853 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
854 if (!j->suppl_gid_list)
855 goto bad_gid_list;
856
857 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
858 }
859
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400860 if (j->chrootdir) { /* stale pointer */
861 char *chrootdir = consumestr(&serialized, &length);
862 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500863 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400864 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500865 if (!j->chrootdir)
866 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400867 }
868
Andrew Brestickereac28942015-11-11 16:04:46 -0800869 if (j->alt_syscall_table) { /* stale pointer */
870 char *alt_syscall_table = consumestr(&serialized, &length);
871 if (!alt_syscall_table)
872 goto bad_syscall_table;
873 j->alt_syscall_table = strdup(alt_syscall_table);
874 if (!j->alt_syscall_table)
875 goto bad_syscall_table;
876 }
877
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800878 if (j->flags.seccomp_filter && j->filter_len > 0) {
879 size_t ninstrs = j->filter_len;
880 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
881 ninstrs > USHRT_MAX)
882 goto bad_filters;
883
884 size_t program_len = ninstrs * sizeof(struct sock_filter);
885 void *program = consumebytes(program_len, &serialized, &length);
886 if (!program)
887 goto bad_filters;
888
889 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800890 if (!j->filter_prog)
891 goto bad_filters;
892
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800893 j->filter_prog->len = ninstrs;
894 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800895 if (!j->filter_prog->filter)
896 goto bad_filter_prog_instrs;
897
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800898 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400899 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400900
Dylan Reid648b2202015-10-23 00:50:00 -0700901 count = j->mounts_count;
902 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400903 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700904 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400905 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700906 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400907 const char *src = consumestr(&serialized, &length);
908 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700909 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400910 dest = consumestr(&serialized, &length);
911 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700912 goto bad_mounts;
913 type = consumestr(&serialized, &length);
914 if (!type)
915 goto bad_mounts;
916 flags = consumebytes(sizeof(*flags), &serialized, &length);
917 if (!flags)
918 goto bad_mounts;
919 if (minijail_mount(j, src, dest, type, *flags))
920 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400921 }
922
Dylan Reid605ce7f2016-01-19 19:21:00 -0800923 count = j->cgroup_count;
924 j->cgroup_count = 0;
925 for (i = 0; i < count; ++i) {
926 char *cgroup = consumestr(&serialized, &length);
927 if (!cgroup)
928 goto bad_cgroups;
929 j->cgroups[i] = strdup(cgroup);
930 if (!j->cgroups[i])
931 goto bad_cgroups;
932 ++j->cgroup_count;
933 }
934
Elly Jonese1749eb2011-10-07 13:54:59 -0400935 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500936
Dylan Reid605ce7f2016-01-19 19:21:00 -0800937bad_cgroups:
938 while (j->mounts_head) {
939 struct mountpoint *m = j->mounts_head;
940 j->mounts_head = j->mounts_head->next;
941 free(m->type);
942 free(m->dest);
943 free(m->src);
944 free(m);
945 }
946 for (i = 0; i < j->cgroup_count; ++i)
947 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -0700948bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800949 if (j->flags.seccomp_filter && j->filter_len > 0) {
950 free(j->filter_prog->filter);
951 free(j->filter_prog);
952 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800953bad_filter_prog_instrs:
954 if (j->filter_prog)
955 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -0500956bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800957 if (j->alt_syscall_table)
958 free(j->alt_syscall_table);
959bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500960 if (j->chrootdir)
961 free(j->chrootdir);
962bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800963 if (j->suppl_gid_list)
964 free(j->suppl_gid_list);
965bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -0500966 if (j->user)
967 free(j->user);
968clear_pointers:
969 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800970 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500971 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800972 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800973 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500974out:
975 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500976}
977
Dylan Reidce5b55e2016-01-13 11:04:16 -0800978static void write_ugid_mappings(const struct minijail *j)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800979{
980 int fd, ret, len;
981 size_t sz;
982 char fname[32];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800983
984 sz = sizeof(fname);
985 if (j->uidmap) {
986 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700987 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800988 die("failed to write file name of uid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -0800989 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800990 if (fd < 0)
991 pdie("failed to open '%s'", fname);
992 len = strlen(j->uidmap);
993 if (write(fd, j->uidmap, len) < len)
994 die("failed to set uid_map");
995 close(fd);
996 }
997 if (j->gidmap) {
998 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700999 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001000 die("failed to write file name of gid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001001 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001002 if (fd < 0)
1003 pdie("failed to open '%s'", fname);
1004 len = strlen(j->gidmap);
1005 if (write(fd, j->gidmap, len) < len)
1006 die("failed to set gid_map");
1007 close(fd);
1008 }
Dylan Reidce5b55e2016-01-13 11:04:16 -08001009}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001010
Dylan Reidce5b55e2016-01-13 11:04:16 -08001011static void parent_setup_complete(int *pipe_fds)
1012{
1013 close(pipe_fds[0]);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001014 close(pipe_fds[1]);
1015}
1016
Dylan Reidce5b55e2016-01-13 11:04:16 -08001017/*
1018 * wait_for_parent_setup: Called by the child process to wait for any
1019 * further parent-side setup to complete before continuing.
1020 */
1021static void wait_for_parent_setup(int *pipe_fds)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001022{
1023 char buf;
1024
1025 close(pipe_fds[1]);
1026
Dylan Reidce5b55e2016-01-13 11:04:16 -08001027 /* Wait for parent to complete setup and close the pipe. */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001028 if (read(pipe_fds[0], &buf, 1) != 0)
1029 die("failed to sync with parent");
1030 close(pipe_fds[0]);
Dylan Reidce5b55e2016-01-13 11:04:16 -08001031}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001032
Dylan Reidce5b55e2016-01-13 11:04:16 -08001033static void enter_user_namespace(const struct minijail *j)
1034{
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001035 if (j->uidmap && setresuid(0, 0, 0))
1036 pdie("setresuid");
1037 if (j->gidmap && setresgid(0, 0, 0))
1038 pdie("setresgid");
1039}
1040
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001041/*
1042 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001043 * @j Minijail these mounts are for
1044 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001045 *
1046 * Returns 0 for success.
1047 */
Dylan Reid648b2202015-10-23 00:50:00 -07001048static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001049{
Dylan Reid648b2202015-10-23 00:50:00 -07001050 int ret;
1051 char *dest;
1052 int remount_ro = 0;
1053
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001054 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001055 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001056 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001057
1058 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001059 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1060 * can't both be specified in the original bind mount.
1061 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001062 */
1063 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1064 remount_ro = 1;
1065 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001066 }
Dylan Reid648b2202015-10-23 00:50:00 -07001067
1068 ret = mount(m->src, dest, m->type, m->flags, NULL);
1069 if (ret)
1070 pdie("mount: %s -> %s", m->src, dest);
1071
1072 if (remount_ro) {
1073 m->flags |= MS_RDONLY;
1074 ret = mount(m->src, dest, NULL,
1075 m->flags | MS_REMOUNT, NULL);
1076 if (ret)
1077 pdie("bind ro: %s -> %s", m->src, dest);
1078 }
1079
Elly Jones51a5b6c2011-10-12 19:09:26 -04001080 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001081 if (m->next)
1082 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001083 return ret;
1084}
1085
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001086int enter_chroot(const struct minijail *j)
1087{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001088 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001089
1090 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001091 return ret;
1092
1093 if (chroot(j->chrootdir))
1094 return -errno;
1095
1096 if (chdir("/"))
1097 return -errno;
1098
1099 return 0;
1100}
1101
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001102int enter_pivot_root(const struct minijail *j)
1103{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001104 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001105
1106 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001107 return ret;
1108
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001109 /*
1110 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001111 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001112 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001113 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001114 if (oldroot < 0)
1115 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001116 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001117 if (newroot < 0)
1118 pdie("failed to open %s for fchdir", j->chrootdir);
1119
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001120 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001121 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001122 * do a self bind mount.
1123 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001124 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1125 pdie("failed to bind mount '%s'", j->chrootdir);
1126 if (chdir(j->chrootdir))
1127 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001128 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001129 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001130
1131 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001132 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001133 * change to the old root and unmount it.
1134 */
1135 if (fchdir(oldroot))
1136 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001137
1138 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001139 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1140 * there could be a shared mount point under |oldroot|. In that case,
1141 * mounts under this shared mount point will be unmounted below, and
1142 * this unmounting will propagate to the original mount namespace
1143 * (because the mount point is shared). To prevent this unexpected
1144 * unmounting, remove these mounts from their peer groups by recursively
1145 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001146 */
1147 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001148 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001149 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001150 if (umount2(".", MNT_DETACH))
1151 pdie("umount(/)");
1152 /* Change back to the new root. */
1153 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001154 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001155 if (close(oldroot))
1156 return -errno;
1157 if (close(newroot))
1158 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001159 if (chroot("/"))
1160 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001161 /* Set correct CWD for getcwd(3). */
1162 if (chdir("/"))
1163 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001164
1165 return 0;
1166}
1167
Lee Campbell11af0622014-05-22 12:36:04 -07001168int mount_tmp(void)
1169{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001170 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001171}
1172
Dylan Reid791f5772015-09-14 20:02:42 -07001173int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001174{
1175 const char *kProcPath = "/proc";
1176 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001177 /*
1178 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001179 * /proc in our namespace, which means using MS_REMOUNT here would
1180 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001181 * namespace (!). Instead, remove their mount from our namespace lazily
1182 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001183 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001184 if (umount2(kProcPath, MNT_DETACH)) {
1185 /*
1186 * If we are in a new user namespace, umount(2) will fail.
1187 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1188 */
1189 if (j->flags.userns) {
1190 info("umount(/proc, MNT_DETACH) failed, "
1191 "this is expected when using user namespaces");
1192 } else {
1193 return -errno;
1194 }
1195 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001196 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1197 return -errno;
1198 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001199}
1200
Dylan Reid605ce7f2016-01-19 19:21:00 -08001201static void write_pid_to_path(pid_t pid, const char *path)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001202{
Dylan Reid605ce7f2016-01-19 19:21:00 -08001203 FILE *fp = fopen(path, "w");
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001204
1205 if (!fp)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001206 pdie("failed to open '%s'", path);
1207 if (fprintf(fp, "%d\n", (int)pid) < 0)
1208 pdie("fprintf(%s)", path);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001209 if (fclose(fp))
Dylan Reid605ce7f2016-01-19 19:21:00 -08001210 pdie("fclose(%s)", path);
1211}
1212
1213static void write_pid_file(const struct minijail *j)
1214{
1215 write_pid_to_path(j->initpid, j->pid_file_path);
1216}
1217
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001218static void add_to_cgroups(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001219{
1220 size_t i;
1221
1222 for (i = 0; i < j->cgroup_count; ++i)
1223 write_pid_to_path(j->initpid, j->cgroups[i]);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001224}
1225
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001226void drop_ugid(const struct minijail *j)
1227{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001228 if (j->flags.usergroups && j->flags.suppl_gids) {
1229 die("tried to inherit *and* set supplementary groups;"
1230 " can only do one");
1231 }
1232
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001233 if (j->flags.usergroups) {
1234 if (initgroups(j->user, j->usergid))
1235 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001236 } else if (j->flags.suppl_gids) {
1237 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1238 pdie("setgroups");
1239 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001240 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001241 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001242 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001243 * users.
1244 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001245 if ((j->uid || j->gid) && setgroups(0, NULL))
1246 pdie("setgroups");
1247 }
1248
1249 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1250 pdie("setresgid");
1251
1252 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1253 pdie("setresuid");
1254}
1255
Mike Frysinger3adfef72013-05-09 17:19:08 -04001256/*
1257 * We specifically do not use cap_valid() as that only tells us the last
1258 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001259 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001260 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001261 * kernel).
1262 * Normally, we suck up the answer via /proc. On Android, not all processes are
1263 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1264 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001265 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001266static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001267{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001268 unsigned int last_valid_cap = 0;
1269 if (is_android()) {
1270 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1271 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001272
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001273 /* |last_valid_cap| will be the first failing value. */
1274 if (last_valid_cap > 0) {
1275 last_valid_cap--;
1276 }
1277 } else {
1278 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1279 FILE *fp = fopen(cap_file, "re");
1280 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1281 pdie("fscanf(%s)", cap_file);
1282 fclose(fp);
1283 }
Dylan Reidf682d472015-09-17 21:39:07 -07001284 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001285}
1286
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001287static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1288{
1289 const uint64_t one = 1;
1290 unsigned int i;
1291 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1292 if (keep_mask & (one << i))
1293 continue;
1294 if (prctl(PR_CAPBSET_DROP, i))
1295 pdie("could not drop capability from bounding set");
1296 }
1297}
1298
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001299void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001300{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001301 if (!j->flags.use_caps)
1302 return;
1303
Elly Jonese1749eb2011-10-07 13:54:59 -04001304 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001305 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001306 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001307 unsigned int i;
1308 if (!caps)
1309 die("can't get process caps");
1310 if (cap_clear_flag(caps, CAP_INHERITABLE))
1311 die("can't clear inheritable caps");
1312 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1313 die("can't clear effective caps");
1314 if (cap_clear_flag(caps, CAP_PERMITTED))
1315 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001316 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001317 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001318 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001319 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001320 flag[0] = i;
1321 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001322 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001323 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001324 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001325 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001326 die("can't add inheritable cap");
1327 }
1328 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001329 die("can't apply initial cleaned capset");
1330
1331 /*
1332 * Instead of dropping bounding set first, do it here in case
1333 * the caller had a more permissive bounding set which could
1334 * have been used above to raise a capability that wasn't already
1335 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1336 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001337 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001338
1339 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001340 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001341 flag[0] = CAP_SETPCAP;
1342 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1343 die("can't clear effective cap");
1344 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1345 die("can't clear permitted cap");
1346 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1347 die("can't clear inheritable cap");
1348 }
1349
1350 if (cap_set_proc(caps))
1351 die("can't apply final cleaned capset");
1352
1353 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001354}
1355
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001356void set_seccomp_filter(const struct minijail *j)
1357{
1358 /*
1359 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1360 * in the kernel source tree for an explanation of the parameters.
1361 */
1362 if (j->flags.no_new_privs) {
1363 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1364 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1365 }
1366
1367 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001368 * Code running with ASan
1369 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1370 * will make system calls not included in the syscall filter policy,
1371 * which will likely crash the program. Skip setting seccomp filter in
1372 * that case.
1373 * 'running_with_asan()' has no inputs and is completely defined at
1374 * build time, so this cannot be used by an attacker to skip setting
1375 * seccomp filter.
1376 */
1377 if (j->flags.seccomp_filter && running_with_asan()) {
1378 warn("running with ASan, not setting seccomp filter");
1379 return;
1380 }
1381
1382 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001383 * If we're logging seccomp filter failures,
1384 * install the SIGSYS handler first.
1385 */
1386 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1387 if (install_sigsys_handler())
1388 pdie("install SIGSYS handler");
1389 warn("logging seccomp filter failures");
1390 }
1391
1392 /*
1393 * Install the syscall filter.
1394 */
1395 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001396 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1397 j->filter_prog)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001398 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001399 warn("seccomp not supported");
1400 return;
1401 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001402 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001403 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001404 }
1405}
1406
Will Drewry6ac91122011-10-21 16:38:58 -05001407void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001408{
Dylan Reidf682d472015-09-17 21:39:07 -07001409 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001410 * If we're dropping caps, get the last valid cap from /proc now,
1411 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001412 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001413 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001414 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001415 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001416
Elly Jonese1749eb2011-10-07 13:54:59 -04001417 if (j->flags.pids)
1418 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001419 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001420
Elly Jonese1749eb2011-10-07 13:54:59 -04001421 if (j->flags.usergroups && !j->user)
1422 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001423
Elly Jonesdd3e8512012-01-23 15:13:38 -05001424 /*
1425 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001426 * so we don't even try. If any of our operations fail, we abort() the
1427 * entire process.
1428 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001429 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1430 pdie("setns(CLONE_NEWNS)");
1431
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001432 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001433 if (unshare(CLONE_NEWNS))
1434 pdie("unshare(vfs)");
1435 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001436 * Unless asked not to, remount all filesystems as private.
1437 * If they are shared, new bind mounts will creep out of our
1438 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001439 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1440 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001441 if (!j->flags.skip_remount_private) {
1442 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1443 pdie("mount(/, private)");
1444 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001445 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001446
Dylan Reidf7942472015-11-18 17:55:26 -08001447 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1448 pdie("unshare(ipc)");
1449 }
1450
Dylan Reid1102f5a2015-09-15 11:52:20 -07001451 if (j->flags.enter_net) {
1452 if (setns(j->netns_fd, CLONE_NEWNET))
1453 pdie("setns(CLONE_NEWNET)");
1454 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001455 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001456 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001457
Elly Jones51a5b6c2011-10-12 19:09:26 -04001458 if (j->flags.chroot && enter_chroot(j))
1459 pdie("chroot");
1460
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001461 if (j->flags.pivot_root && enter_pivot_root(j))
1462 pdie("pivot_root");
1463
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001464 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001465 pdie("mount_tmp");
1466
Dylan Reid791f5772015-09-14 20:02:42 -07001467 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001468 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001469
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001470 /*
1471 * If we're only dropping capabilities from the bounding set, but not
1472 * from the thread's (permitted|inheritable|effective) sets, do it now.
1473 */
1474 if (j->flags.capbset_drop) {
1475 drop_capbset(j->cap_bset, last_valid_cap);
1476 }
1477
1478 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001479 /*
1480 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001481 * capability to change uids, our attempt to use setuid()
1482 * below will fail. Hang on to root caps across setuid(), then
1483 * lock securebits.
1484 */
1485 if (prctl(PR_SET_KEEPCAPS, 1))
1486 pdie("prctl(PR_SET_KEEPCAPS)");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001487
1488 /*
1489 * Kernels 4.3+ define a new securebit
1490 * (SECURE_NO_CAP_AMBIENT_RAISE), so using the SECURE_ALL_BITS
1491 * and SECURE_ALL_LOCKS masks from newer kernel headers will
1492 * return EPERM on older kernels. Detect this, and retry with
1493 * the right mask for older (2.6.26-4.2) kernels.
1494 */
1495 int securebits_ret = prctl(PR_SET_SECUREBITS,
1496 SECURE_ALL_BITS | SECURE_ALL_LOCKS);
1497 if (securebits_ret < 0) {
1498 if (errno == EPERM) {
1499 /* Possibly running on kernel < 4.3. */
1500 securebits_ret = prctl(
1501 PR_SET_SECUREBITS,
1502 OLD_SECURE_ALL_BITS | OLD_SECURE_ALL_LOCKS);
1503 }
1504 }
1505 if (securebits_ret < 0)
Elly Jonese1749eb2011-10-07 13:54:59 -04001506 pdie("prctl(PR_SET_SECUREBITS)");
1507 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001508
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001509 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001510 /*
1511 * If we're setting no_new_privs, we can drop privileges
1512 * before setting seccomp filter. This way filter policies
1513 * don't need to allow privilege-dropping syscalls.
1514 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001515 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001516 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001517 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001518 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001519 /*
1520 * If we're not setting no_new_privs,
1521 * we need to set seccomp filter *before* dropping privileges.
1522 * WARNING: this means that filter policies *must* allow
1523 * setgroups()/setresgid()/setresuid() for dropping root and
1524 * capget()/capset()/prctl() for dropping caps.
1525 */
1526 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001527 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001528 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001529 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001530
Elly Jonesdd3e8512012-01-23 15:13:38 -05001531 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001532 * Select the specified alternate syscall table. The table must not
1533 * block prctl(2) if we're using seccomp as well.
1534 */
1535 if (j->flags.alt_syscall) {
1536 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1537 pdie("prctl(PR_ALT_SYSCALL)");
1538 }
1539
1540 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001541 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001542 * privilege-dropping syscalls :)
1543 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001544 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001545 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001546 warn("seccomp not supported");
1547 return;
1548 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001549 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001550 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001551}
1552
Will Drewry6ac91122011-10-21 16:38:58 -05001553/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001554static int init_exitstatus = 0;
1555
Will Drewry6ac91122011-10-21 16:38:58 -05001556void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001557{
1558 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001559}
1560
Will Drewry6ac91122011-10-21 16:38:58 -05001561int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001562{
1563 pid_t pid;
1564 int status;
1565 /* so that we exit with the right status */
1566 signal(SIGTERM, init_term);
1567 /* TODO(wad) self jail with seccomp_filters here. */
1568 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001569 /*
1570 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001571 * left inside our pid namespace or we get a signal.
1572 */
1573 if (pid == rootpid)
1574 init_exitstatus = status;
1575 }
1576 if (!WIFEXITED(init_exitstatus))
1577 _exit(MINIJAIL_ERR_INIT);
1578 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001579}
1580
Will Drewry6ac91122011-10-21 16:38:58 -05001581int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001582{
1583 size_t sz = 0;
1584 size_t bytes = read(fd, &sz, sizeof(sz));
1585 char *buf;
1586 int r;
1587 if (sizeof(sz) != bytes)
1588 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001589 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001590 return -E2BIG;
1591 buf = malloc(sz);
1592 if (!buf)
1593 return -ENOMEM;
1594 bytes = read(fd, buf, sz);
1595 if (bytes != sz) {
1596 free(buf);
1597 return -EINVAL;
1598 }
1599 r = minijail_unmarshal(j, buf, sz);
1600 free(buf);
1601 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001602}
1603
Will Drewry6ac91122011-10-21 16:38:58 -05001604int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001605{
1606 char *buf;
1607 size_t sz = minijail_size(j);
1608 ssize_t written;
1609 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001610
Elly Jonese1749eb2011-10-07 13:54:59 -04001611 if (!sz)
1612 return -EINVAL;
1613 buf = malloc(sz);
1614 r = minijail_marshal(j, buf, sz);
1615 if (r) {
1616 free(buf);
1617 return r;
1618 }
1619 /* Sends [size][minijail]. */
1620 written = write(fd, &sz, sizeof(sz));
1621 if (written != sizeof(sz)) {
1622 free(buf);
1623 return -EFAULT;
1624 }
1625 written = write(fd, buf, sz);
1626 if (written < 0 || (size_t) written != sz) {
1627 free(buf);
1628 return -EFAULT;
1629 }
1630 free(buf);
1631 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001632}
Elly Jonescd7a9042011-07-22 13:56:51 -04001633
Will Drewry6ac91122011-10-21 16:38:58 -05001634int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001635{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001636#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001637 /* Don't use LDPRELOAD on Brillo. */
1638 return 0;
1639#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001640 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1641 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1642 if (!newenv)
1643 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001644
Elly Jonese1749eb2011-10-07 13:54:59 -04001645 /* Only insert a separating space if we have something to separate... */
1646 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1647 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001648
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001649 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001650 setenv(kLdPreloadEnvVar, newenv, 1);
1651 free(newenv);
1652 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001653#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001654}
1655
Will Drewry6ac91122011-10-21 16:38:58 -05001656int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001657{
1658 int r = pipe(fds);
1659 char fd_buf[11];
1660 if (r)
1661 return r;
1662 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1663 if (r <= 0)
1664 return -EINVAL;
1665 setenv(kFdEnvVar, fd_buf, 1);
1666 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001667}
1668
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001669int setup_pipe_end(int fds[2], size_t index)
1670{
1671 if (index > 1)
1672 return -1;
1673
1674 close(fds[1 - index]);
1675 return fds[index];
1676}
1677
1678int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1679{
1680 if (index > 1)
1681 return -1;
1682
1683 close(fds[1 - index]);
1684 /* dup2(2) the corresponding end of the pipe into |fd|. */
1685 return dup2(fds[index], fd);
1686}
1687
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001688int minijail_run_internal(struct minijail *j, const char *filename,
1689 char *const argv[], pid_t *pchild_pid,
1690 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1691 int use_preload);
1692
Will Drewry6ac91122011-10-21 16:38:58 -05001693int API minijail_run(struct minijail *j, const char *filename,
1694 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001695{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001696 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1697 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001698}
1699
1700int API minijail_run_pid(struct minijail *j, const char *filename,
1701 char *const argv[], pid_t *pchild_pid)
1702{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001703 return minijail_run_internal(j, filename, argv, pchild_pid,
1704 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001705}
1706
1707int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001708 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001709{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001710 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1711 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001712}
1713
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001714int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001715 char *const argv[], pid_t *pchild_pid,
1716 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001717{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001718 return minijail_run_internal(j, filename, argv, pchild_pid,
1719 pstdin_fd, pstdout_fd, pstderr_fd, true);
1720}
1721
1722int API minijail_run_no_preload(struct minijail *j, const char *filename,
1723 char *const argv[])
1724{
1725 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1726 false);
1727}
1728
Samuel Tan63187f42015-10-16 13:01:53 -07001729int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001730 const char *filename,
1731 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001732 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001733 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001734 int *pstderr_fd)
1735{
Samuel Tan63187f42015-10-16 13:01:53 -07001736 return minijail_run_internal(j, filename, argv, pchild_pid,
1737 pstdin_fd, pstdout_fd, pstderr_fd, false);
1738}
1739
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001740int minijail_run_internal(struct minijail *j, const char *filename,
1741 char *const argv[], pid_t *pchild_pid,
1742 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1743 int use_preload)
1744{
Elly Jonese1749eb2011-10-07 13:54:59 -04001745 char *oldenv, *oldenv_copy = NULL;
1746 pid_t child_pid;
1747 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001748 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001749 int stdout_fds[2];
1750 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001751 int child_sync_pipe_fds[2];
1752 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001753 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001754 /* We need to remember this across the minijail_preexec() call. */
1755 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001756 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001757
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001758 if (use_preload) {
1759 oldenv = getenv(kLdPreloadEnvVar);
1760 if (oldenv) {
1761 oldenv_copy = strdup(oldenv);
1762 if (!oldenv_copy)
1763 return -ENOMEM;
1764 }
1765
1766 if (setup_preload())
1767 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001768 }
Will Drewryf89aef52011-09-16 16:48:57 -05001769
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001770 if (!use_preload) {
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001771 if (j->flags.use_caps)
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001772 die("capabilities are not supported without "
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001773 "LD_PRELOAD");
1774 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001775
Elly Jonesdd3e8512012-01-23 15:13:38 -05001776 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001777 * Make the process group ID of this process equal to its PID, so that
1778 * both the Minijail process and the jailed process can be killed
1779 * together.
1780 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1781 * the process is already a process group leader.
1782 */
1783 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1784 if (errno != EPERM) {
1785 pdie("setpgid(0, 0)");
1786 }
1787 }
1788
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001789 if (use_preload) {
1790 /*
1791 * Before we fork(2) and execve(2) the child process, we need
1792 * to open a pipe(2) to send the minijail configuration over.
1793 */
1794 if (setup_pipe(pipe_fds))
1795 return -EFAULT;
1796 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001797
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001798 /*
1799 * If we want to write to the child process' standard input,
1800 * create the pipe(2) now.
1801 */
1802 if (pstdin_fd) {
1803 if (pipe(stdin_fds))
1804 return -EFAULT;
1805 }
1806
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001807 /*
1808 * If we want to read from the child process' standard output,
1809 * create the pipe(2) now.
1810 */
1811 if (pstdout_fd) {
1812 if (pipe(stdout_fds))
1813 return -EFAULT;
1814 }
1815
1816 /*
1817 * If we want to read from the child process' standard error,
1818 * create the pipe(2) now.
1819 */
1820 if (pstderr_fd) {
1821 if (pipe(stderr_fds))
1822 return -EFAULT;
1823 }
1824
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001825 /*
1826 * If we want to set up a new uid/gid mapping in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001827 * or if we need to add the child process to cgroups, create the pipe(2)
1828 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001829 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001830 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001831 sync_child = 1;
1832 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001833 return -EFAULT;
1834 }
1835
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001836 /*
1837 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001838 *
1839 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1840 *
1841 * In multithreaded programs, there are a bunch of locks inside libc,
1842 * some of which may be held by other threads at the time that we call
1843 * minijail_run_pid(). If we call fork(), glibc does its level best to
1844 * ensure that we hold all of these locks before it calls clone()
1845 * internally and drop them after clone() returns, but when we call
1846 * sys_clone(2) directly, all that gets bypassed and we end up with a
1847 * child address space where some of libc's important locks are held by
1848 * other threads (which did not get cloned, and hence will never release
1849 * those locks). This is okay so long as we call exec() immediately
1850 * after, but a bunch of seemingly-innocent libc functions like setenv()
1851 * take locks.
1852 *
1853 * Hence, only call sys_clone() if we need to, in order to get at pid
1854 * namespacing. If we follow this path, the child's address space might
1855 * have broken locks; you may only call functions that do not acquire
1856 * any locks.
1857 *
1858 * Unfortunately, fork() acquires every lock it can get its hands on, as
1859 * previously detailed, so this function is highly likely to deadlock
1860 * later on (see "deadlock here") if we're multithreaded.
1861 *
1862 * We might hack around this by having the clone()d child (init of the
1863 * pid namespace) return directly, rather than leaving the clone()d
1864 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001865 * its fork()ed child return in turn), but that process would be
1866 * crippled with its libc locks potentially broken. We might try
1867 * fork()ing in the parent before we clone() to ensure that we own all
1868 * the locks, but then we have to have the forked child hanging around
1869 * consuming resources (and possibly having file descriptors / shared
1870 * memory regions / etc attached). We'd need to keep the child around to
1871 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04001872 *
1873 * TODO(ellyjones): figure out if the "forked child hanging around"
1874 * problem is fixable or not. It would be nice if we worked in this
1875 * case.
1876 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001877 if (pid_namespace) {
1878 int clone_flags = CLONE_NEWPID | SIGCHLD;
1879 if (j->flags.userns)
1880 clone_flags |= CLONE_NEWUSER;
1881 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001882 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001883 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001884 }
Elly Jones761b7412012-06-13 15:49:52 -04001885
Elly Jonese1749eb2011-10-07 13:54:59 -04001886 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001887 if (use_preload) {
1888 free(oldenv_copy);
1889 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001890 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001891 }
Will Drewryf89aef52011-09-16 16:48:57 -05001892
Elly Jonese1749eb2011-10-07 13:54:59 -04001893 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001894 if (use_preload) {
1895 /* Restore parent's LD_PRELOAD. */
1896 if (oldenv_copy) {
1897 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1898 free(oldenv_copy);
1899 } else {
1900 unsetenv(kLdPreloadEnvVar);
1901 }
1902 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001903 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001904
Elly Jonese1749eb2011-10-07 13:54:59 -04001905 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001906
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001907 if (j->flags.pid_file)
1908 write_pid_file(j);
1909
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001910 if (j->flags.cgroups)
1911 add_to_cgroups(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001912
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001913 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001914 write_ugid_mappings(j);
1915
1916 if (sync_child)
1917 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001918
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001919 if (use_preload) {
1920 /* Send marshalled minijail. */
1921 close(pipe_fds[0]); /* read endpoint */
1922 ret = minijail_to_fd(j, pipe_fds[1]);
1923 close(pipe_fds[1]); /* write endpoint */
1924 if (ret) {
1925 kill(j->initpid, SIGKILL);
1926 die("failed to send marshalled minijail");
1927 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001928 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001929
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001930 if (pchild_pid)
1931 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001932
1933 /*
1934 * If we want to write to the child process' standard input,
1935 * set up the write end of the pipe.
1936 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001937 if (pstdin_fd)
1938 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001939 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001940
1941 /*
1942 * If we want to read from the child process' standard output,
1943 * set up the read end of the pipe.
1944 */
1945 if (pstdout_fd)
1946 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001947 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001948
1949 /*
1950 * If we want to read from the child process' standard error,
1951 * set up the read end of the pipe.
1952 */
1953 if (pstderr_fd)
1954 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001955 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001956
Elly Jonese1749eb2011-10-07 13:54:59 -04001957 return 0;
1958 }
1959 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001960
Peter Qiu2860c462015-12-16 15:13:06 -08001961 if (j->flags.reset_signal_mask) {
1962 sigset_t signal_mask;
1963 if (sigemptyset(&signal_mask) != 0)
1964 pdie("sigemptyset failed");
1965 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
1966 pdie("sigprocmask failed");
1967 }
1968
Dylan Reidce5b55e2016-01-13 11:04:16 -08001969 if (sync_child)
1970 wait_for_parent_setup(child_sync_pipe_fds);
1971
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001972 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001973 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001974
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001975 /*
1976 * If we want to write to the jailed process' standard input,
1977 * set up the read end of the pipe.
1978 */
1979 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001980 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1981 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001982 die("failed to set up stdin pipe");
1983 }
1984
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001985 /*
1986 * If we want to read from the jailed process' standard output,
1987 * set up the write end of the pipe.
1988 */
1989 if (pstdout_fd) {
1990 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1991 STDOUT_FILENO) < 0)
1992 die("failed to set up stdout pipe");
1993 }
1994
1995 /*
1996 * If we want to read from the jailed process' standard error,
1997 * set up the write end of the pipe.
1998 */
1999 if (pstderr_fd) {
2000 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2001 STDERR_FILENO) < 0)
2002 die("failed to set up stderr pipe");
2003 }
2004
Dylan Reid791f5772015-09-14 20:02:42 -07002005 /* If running an init program, let it decide when/how to mount /proc. */
2006 if (pid_namespace && !do_init)
2007 j->flags.remount_proc_ro = 0;
2008
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002009 if (use_preload) {
2010 /* Strip out flags that cannot be inherited across execve(2). */
2011 minijail_preexec(j);
2012 } else {
2013 j->flags.pids = 0;
2014 }
2015 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002016 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002017
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002018 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002019 /*
2020 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002021 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002022 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002023 * a child to actually run the program. If |do_init == 0|, we
2024 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002025 *
2026 * If we're multithreaded, we'll probably deadlock here. See
2027 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002028 */
2029 child_pid = fork();
2030 if (child_pid < 0)
2031 _exit(child_pid);
2032 else if (child_pid > 0)
2033 init(child_pid); /* never returns */
2034 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002035
Elly Jonesdd3e8512012-01-23 15:13:38 -05002036 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002037 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002038 * calling process
2039 * -> execve()-ing process
2040 * If we are:
2041 * calling process
2042 * -> init()-ing process
2043 * -> execve()-ing process
2044 */
2045 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04002046}
2047
Will Drewry6ac91122011-10-21 16:38:58 -05002048int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002049{
2050 int st;
2051 if (kill(j->initpid, SIGTERM))
2052 return -errno;
2053 if (waitpid(j->initpid, &st, 0) < 0)
2054 return -errno;
2055 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002056}
2057
Will Drewry6ac91122011-10-21 16:38:58 -05002058int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002059{
2060 int st;
2061 if (waitpid(j->initpid, &st, 0) < 0)
2062 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002063
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002064 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002065 int error_status = st;
2066 if (WIFSIGNALED(st)) {
2067 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002068 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002069 j->initpid, signum);
2070 /*
2071 * We return MINIJAIL_ERR_JAIL if the process received
2072 * SIGSYS, which happens when a syscall is blocked by
2073 * seccomp filters.
2074 * If not, we do what bash(1) does:
2075 * $? = 128 + signum
2076 */
2077 if (signum == SIGSYS) {
2078 error_status = MINIJAIL_ERR_JAIL;
2079 } else {
2080 error_status = 128 + signum;
2081 }
2082 }
2083 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002084 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002085
2086 int exit_status = WEXITSTATUS(st);
2087 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002088 info("child process %d exited with status %d",
2089 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002090
2091 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002092}
2093
Will Drewry6ac91122011-10-21 16:38:58 -05002094void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002095{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002096 size_t i;
2097
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002098 if (j->flags.seccomp_filter && j->filter_prog) {
2099 free(j->filter_prog->filter);
2100 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002101 }
Dylan Reid648b2202015-10-23 00:50:00 -07002102 while (j->mounts_head) {
2103 struct mountpoint *m = j->mounts_head;
2104 j->mounts_head = j->mounts_head->next;
2105 free(m->type);
2106 free(m->dest);
2107 free(m->src);
2108 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002109 }
Dylan Reid648b2202015-10-23 00:50:00 -07002110 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002111 if (j->user)
2112 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002113 if (j->suppl_gid_list)
2114 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002115 if (j->chrootdir)
2116 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08002117 if (j->alt_syscall_table)
2118 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002119 for (i = 0; i < j->cgroup_count; ++i)
2120 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002121 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002122}