blob: 3a519429f0a7b217b28c713360cc7a6b627ce786 [file] [log] [blame]
Jorge Lucangeli Obesd613ab22015-03-03 14:22:50 -08001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04002 * Use of this source code is governed by a BSD-style license that can be
Will Drewry32ac9f52011-08-18 21:36:27 -05003 * found in the LICENSE file.
4 */
Elly Jonescd7a9042011-07-22 13:56:51 -04005
6#define _BSD_SOURCE
Arthur Gautier7a569072016-04-23 17:25:20 +00007#define _DEFAULT_SOURCE
Elly Jonescd7a9042011-07-22 13:56:51 -04008#define _GNU_SOURCE
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07009
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080010#include <asm/unistd.h>
Will Drewry32ac9f52011-08-18 21:36:27 -050011#include <ctype.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040012#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070013#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040014#include <grp.h>
15#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050016#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040017#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040018#include <pwd.h>
19#include <sched.h>
20#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050021#include <stdarg.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070022#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080023#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <syscall.h>
28#include <sys/capability.h>
29#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050030#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040031#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070032#include <sys/stat.h>
33#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080034#include <sys/user.h>
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -080035#include <sys/utsname.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040036#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040037#include <unistd.h>
38
39#include "libminijail.h"
40#include "libminijail-private.h"
41
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070042#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080043#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070044#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080045
Lei Zhangeee31552012-10-17 21:27:10 -070046#ifdef HAVE_SECUREBITS_H
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070047# include <linux/securebits.h>
Lei Zhangeee31552012-10-17 21:27:10 -070048#else
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070049# define SECURE_ALL_BITS 0x55
50# define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
51#endif
52/* For kernels < 4.3. */
53#define OLD_SECURE_ALL_BITS 0x15
54#define OLD_SECURE_ALL_LOCKS (OLD_SECURE_ALL_BITS << 1)
55
56/*
57 * Assert the value of SECURE_ALL_BITS at compile-time.
58 * Brillo devices are currently compiled against 4.4 kernel headers. Kernel 4.3
59 * added a new securebit.
60 * When a new securebit is added, the new SECURE_ALL_BITS mask will return EPERM
61 * when used on older kernels. The compile-time assert will catch this situation
62 * at compile time.
63 */
64#ifdef __BRILLO__
65_Static_assert(SECURE_ALL_BITS == 0x55, "SECURE_ALL_BITS == 0x55.");
Lei Zhangeee31552012-10-17 21:27:10 -070066#endif
67
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070068/* Until these are reliably available in linux/prctl.h. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080069#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070070# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080071#endif
72
Andrew Brestickereac28942015-11-11 16:04:46 -080073#ifndef PR_ALT_SYSCALL
74# define PR_ALT_SYSCALL 0x43724f53
75#endif
76
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080077/* For seccomp_filter using BPF. */
78#ifndef PR_SET_NO_NEW_PRIVS
79# define PR_SET_NO_NEW_PRIVS 38
80#endif
81#ifndef SECCOMP_MODE_FILTER
82# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050083#endif
84
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070085#ifdef USE_SECCOMP_SOFTFAIL
86# define SECCOMP_SOFTFAIL 1
87#else
88# define SECCOMP_SOFTFAIL 0
89#endif
90
Dylan Reid4cbc2a52016-06-17 19:06:07 -070091/* New cgroup namespace might not be in linux-headers yet. */
92#ifndef CLONE_NEWCGROUP
93# define CLONE_NEWCGROUP 0x02000000
94#endif
95
Dylan Reid605ce7f2016-01-19 19:21:00 -080096#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
97
Dylan Reid648b2202015-10-23 00:50:00 -070098struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040099 char *src;
100 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700101 char *type;
102 unsigned long flags;
103 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400104};
105
Will Drewryf89aef52011-09-16 16:48:57 -0500106struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700107 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700108 * WARNING: if you add a flag here you need to make sure it's
109 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700110 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400111 struct {
112 int uid:1;
113 int gid:1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800114 int usergroups:1;
115 int suppl_gids:1;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800116 int use_caps:1;
117 int capbset_drop:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400118 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700119 int enter_vfs:1;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800120 int skip_remount_private:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400121 int pids:1;
Dylan Reidf7942472015-11-18 17:55:26 -0800122 int ipc:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400123 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700124 int enter_net:1;
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700125 int ns_cgroups:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800126 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400127 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -0700128 int remount_proc_ro:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700129 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400130 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700131 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400132 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800133 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700134 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800135 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800136 int pid_file:1;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800137 int cgroups:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800138 int alt_syscall:1;
Peter Qiu2860c462015-12-16 15:13:06 -0800139 int reset_signal_mask:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400140 } flags;
141 uid_t uid;
142 gid_t gid;
143 gid_t usergid;
144 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800145 size_t suppl_gid_count;
146 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400147 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800148 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400149 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700150 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700151 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400152 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800153 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800154 char *uidmap;
155 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800156 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800157 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800158 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700159 struct mountpoint *mounts_head;
160 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800161 size_t mounts_count;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800162 char *cgroups[MAX_CGROUPS];
163 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500164};
165
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700166/*
167 * Strip out flags meant for the parent.
168 * We keep things that are not inherited across execve(2) (e.g. capabilities),
169 * or are easier to set after execve(2) (e.g. seccomp filters).
170 */
171void minijail_preenter(struct minijail *j)
172{
173 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700174 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900175 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700176 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700177 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800178 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800179 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800180 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700181}
182
183/*
184 * Strip out flags meant for the child.
185 * We keep things that are inherited across execve(2).
186 */
187void minijail_preexec(struct minijail *j)
188{
189 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700190 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800191 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700192 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800193 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700194 if (j->user)
195 free(j->user);
196 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800197 if (j->suppl_gid_list)
198 free(j->suppl_gid_list);
199 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700200 memset(&j->flags, 0, sizeof(j->flags));
201 /* Now restore anything we meant to keep. */
202 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700203 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800204 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700205 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800206 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700207 /* Note, |pids| will already have been used before this call. */
208}
209
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800210/* Returns true if the kernel version is less than 3.8. */
211int seccomp_kernel_support_not_required()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800212{
213 int major, minor;
214 struct utsname uts;
215 return (uname(&uts) != -1 &&
216 sscanf(uts.release, "%d.%d", &major, &minor) == 2 &&
217 ((major < 3) || ((major == 3) && (minor < 8))));
218}
219
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800220/* Allow seccomp soft-fail on Android devices with kernel version < 3.8. */
221int can_softfail()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800222{
223#if SECCOMP_SOFTFAIL
224 if (is_android()) {
225 if (seccomp_kernel_support_not_required())
226 return 1;
227 else
228 return 0;
229 } else {
230 return 1;
231 }
232#endif
233 return 0;
234}
235
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700236/* Minijail API. */
237
Will Drewry6ac91122011-10-21 16:38:58 -0500238struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400239{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400240 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400241}
242
Will Drewry6ac91122011-10-21 16:38:58 -0500243void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400244{
245 if (uid == 0)
246 die("useless change to uid 0");
247 j->uid = uid;
248 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400249}
250
Will Drewry6ac91122011-10-21 16:38:58 -0500251void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400252{
253 if (gid == 0)
254 die("useless change to gid 0");
255 j->gid = gid;
256 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400257}
258
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800259void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
260 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800261{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800262 size_t i;
263
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800264 if (j->flags.usergroups)
265 die("cannot inherit *and* set supplementary groups");
266
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800267 if (size == 0) {
268 /* Clear supplementary groups. */
269 j->suppl_gid_list = NULL;
270 j->suppl_gid_count = 0;
271 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800272 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800273 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800274
275 /* Copy the gid_t array. */
276 j->suppl_gid_list = calloc(size, sizeof(gid_t));
277 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800278 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800279 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800280 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800281 j->suppl_gid_list[i] = list[i];
282 }
283 j->suppl_gid_count = size;
284 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800285}
286
Will Drewry6ac91122011-10-21 16:38:58 -0500287int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400288{
289 char *buf = NULL;
290 struct passwd pw;
291 struct passwd *ppw = NULL;
292 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
293 if (sz == -1)
294 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400295
Elly Jonesdd3e8512012-01-23 15:13:38 -0500296 /*
297 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400298 * the maximum needed size of the buffer, so we don't have to search.
299 */
300 buf = malloc(sz);
301 if (!buf)
302 return -ENOMEM;
303 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500304 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800305 * We're safe to free the buffer here. The strings inside |pw| point
306 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800307 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
308 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500309 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400310 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700311 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400312 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700313 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400314 minijail_change_uid(j, ppw->pw_uid);
315 j->user = strdup(user);
316 if (!j->user)
317 return -ENOMEM;
318 j->usergid = ppw->pw_gid;
319 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400320}
321
Will Drewry6ac91122011-10-21 16:38:58 -0500322int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400323{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700324 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700325 struct group gr;
326 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400327 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
328 if (sz == -1)
329 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400330
Elly Jonesdd3e8512012-01-23 15:13:38 -0500331 /*
332 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400333 * the maximum needed size of the buffer, so we don't have to search.
334 */
335 buf = malloc(sz);
336 if (!buf)
337 return -ENOMEM;
338 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500339 /*
340 * We're safe to free the buffer here. The strings inside gr point
341 * inside buf, but we don't use any of them; this leaves the pointers
342 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
343 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400344 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700345 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400346 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700347 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400348 minijail_change_gid(j, pgr->gr_gid);
349 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400350}
351
Will Drewry6ac91122011-10-21 16:38:58 -0500352void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400353{
354 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400355}
356
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700357void API minijail_no_new_privs(struct minijail *j)
358{
359 j->flags.no_new_privs = 1;
360}
361
Will Drewry6ac91122011-10-21 16:38:58 -0500362void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400363{
364 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500365}
366
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700367void API minijail_log_seccomp_filter_failures(struct minijail *j)
368{
369 j->flags.log_seccomp_filter = 1;
370}
371
Will Drewry6ac91122011-10-21 16:38:58 -0500372void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400373{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800374 /*
375 * 'minijail_use_caps' configures a runtime-capabilities-only
376 * environment, including a bounding set matching the thread's runtime
377 * (permitted|inheritable|effective) sets.
378 * Therefore, it will override any existing bounding set configurations
379 * since the latter would allow gaining extra runtime capabilities from
380 * file capabilities.
381 */
382 if (j->flags.capbset_drop) {
383 warn("overriding bounding set configuration");
384 j->cap_bset = 0;
385 j->flags.capbset_drop = 0;
386 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400387 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800388 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400389}
390
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800391void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
392{
393 if (j->flags.use_caps) {
394 /*
395 * 'minijail_use_caps' will have already configured a capability
396 * bounding set matching the (permitted|inheritable|effective)
397 * sets. Abort if the user tries to configure a separate
398 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
399 * are mutually exclusive.
400 */
401 die("runtime capabilities already configured, can't drop "
402 "bounding set separately");
403 }
404 j->cap_bset = capmask;
405 j->flags.capbset_drop = 1;
406}
407
408void API minijail_reset_signal_mask(struct minijail *j)
409{
Peter Qiu2860c462015-12-16 15:13:06 -0800410 j->flags.reset_signal_mask = 1;
411}
412
Will Drewry6ac91122011-10-21 16:38:58 -0500413void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400414{
415 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400416}
417
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700418void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
419{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800420 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700421 if (ns_fd < 0) {
422 pdie("failed to open namespace '%s'", ns_path);
423 }
424 j->mountns_fd = ns_fd;
425 j->flags.enter_vfs = 1;
426}
427
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800428void API minijail_skip_remount_private(struct minijail *j)
429{
430 j->flags.skip_remount_private = 1;
431}
432
Will Drewry6ac91122011-10-21 16:38:58 -0500433void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400434{
Elly Jonese58176c2012-01-23 11:46:17 -0500435 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700436 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400437 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800438 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400439}
440
Dylan Reidf7942472015-11-18 17:55:26 -0800441void API minijail_namespace_ipc(struct minijail *j)
442{
443 j->flags.ipc = 1;
444}
445
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400446void API minijail_namespace_net(struct minijail *j)
447{
448 j->flags.net = 1;
449}
450
Dylan Reid1102f5a2015-09-15 11:52:20 -0700451void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
452{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800453 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700454 if (ns_fd < 0) {
455 pdie("failed to open namespace '%s'", ns_path);
456 }
457 j->netns_fd = ns_fd;
458 j->flags.enter_net = 1;
459}
460
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700461void API minijail_namespace_cgroups(struct minijail *j)
462{
463 j->flags.ns_cgroups = 1;
464}
465
Dylan Reid791f5772015-09-14 20:02:42 -0700466void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400467{
468 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700469 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400470}
471
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800472void API minijail_namespace_user(struct minijail *j)
473{
474 j->flags.userns = 1;
475}
476
477int API minijail_uidmap(struct minijail *j, const char *uidmap)
478{
479 j->uidmap = strdup(uidmap);
480 if (!j->uidmap)
481 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800482 char *ch;
483 for (ch = j->uidmap; *ch; ch++) {
484 if (*ch == ',')
485 *ch = '\n';
486 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800487 return 0;
488}
489
490int API minijail_gidmap(struct minijail *j, const char *gidmap)
491{
492 j->gidmap = strdup(gidmap);
493 if (!j->gidmap)
494 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800495 char *ch;
496 for (ch = j->gidmap; *ch; ch++) {
497 if (*ch == ',')
498 *ch = '\n';
499 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800500 return 0;
501}
502
Will Drewry6ac91122011-10-21 16:38:58 -0500503void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400504{
505 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400506}
507
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800508void API minijail_run_as_init(struct minijail *j)
509{
510 /*
511 * Since the jailed program will become 'init' in the new PID namespace,
512 * Minijail does not need to fork an 'init' process.
513 */
514 j->flags.do_init = 0;
515}
516
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700517int API minijail_enter_chroot(struct minijail *j, const char *dir)
518{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400519 if (j->chrootdir)
520 return -EINVAL;
521 j->chrootdir = strdup(dir);
522 if (!j->chrootdir)
523 return -ENOMEM;
524 j->flags.chroot = 1;
525 return 0;
526}
527
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800528int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
529{
530 if (j->chrootdir)
531 return -EINVAL;
532 j->chrootdir = strdup(dir);
533 if (!j->chrootdir)
534 return -ENOMEM;
535 j->flags.pivot_root = 1;
536 return 0;
537}
538
Dylan Reida14e08d2015-10-22 21:05:29 -0700539static char *append_external_path(const char *external_path,
540 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700541{
Dylan Reida14e08d2015-10-22 21:05:29 -0700542 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700543 size_t pathlen;
544
Dylan Reid08946cc2015-09-16 19:10:57 -0700545 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700546 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
547 path = malloc(pathlen);
548 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700549
Dylan Reida14e08d2015-10-22 21:05:29 -0700550 return path;
551}
552
553char API *minijail_get_original_path(struct minijail *j,
554 const char *path_inside_chroot)
555{
Dylan Reid648b2202015-10-23 00:50:00 -0700556 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700557
Dylan Reid648b2202015-10-23 00:50:00 -0700558 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700559 while (b) {
560 /*
561 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700562 * mount, then the original path is exactly the source of
563 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700564 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700565 * mount source = /some/path/exe, mount dest =
566 * /chroot/path/exe Then when getting the original path of
567 * "/chroot/path/exe", the source of that mount,
568 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700569 */
570 if (!strcmp(b->dest, path_inside_chroot))
571 return strdup(b->src);
572
573 /*
574 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700575 * mount, take the suffix of the chroot path relative to the
576 * mount destination path, and append it to the mount source
577 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700578 */
579 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
580 const char *relative_path =
581 path_inside_chroot + strlen(b->dest);
582 return append_external_path(b->src, relative_path);
583 }
584 b = b->next;
585 }
586
587 /* If there is a chroot path, append |path_inside_chroot| to that. */
588 if (j->chrootdir)
589 return append_external_path(j->chrootdir, path_inside_chroot);
590
591 /* No chroot, so the path outside is the same as it is inside. */
592 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700593}
594
Lee Campbell11af0622014-05-22 12:36:04 -0700595void API minijail_mount_tmp(struct minijail *j)
596{
597 j->flags.mount_tmp = 1;
598}
599
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800600int API minijail_write_pid_file(struct minijail *j, const char *path)
601{
602 j->pid_file_path = strdup(path);
603 if (!j->pid_file_path)
604 return -ENOMEM;
605 j->flags.pid_file = 1;
606 return 0;
607}
608
Dylan Reid605ce7f2016-01-19 19:21:00 -0800609int API minijail_add_to_cgroup(struct minijail *j, const char *path)
610{
611 if (j->cgroup_count >= MAX_CGROUPS)
612 return -ENOMEM;
613 j->cgroups[j->cgroup_count] = strdup(path);
614 if (!j->cgroups[j->cgroup_count])
615 return -ENOMEM;
616 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800617 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800618 return 0;
619}
620
Dylan Reid648b2202015-10-23 00:50:00 -0700621int API minijail_mount(struct minijail *j, const char *src, const char *dest,
622 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700623{
Dylan Reid648b2202015-10-23 00:50:00 -0700624 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400625
626 if (*dest != '/')
627 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700628 m = calloc(1, sizeof(*m));
629 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400630 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700631 m->dest = strdup(dest);
632 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400633 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700634 m->src = strdup(src);
635 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400636 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700637 m->type = strdup(type);
638 if (!m->type)
639 goto error;
640 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400641
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800642 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400643
Elly Jonesdd3e8512012-01-23 15:13:38 -0500644 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700645 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400646 * containing vfs namespace.
647 */
648 minijail_namespace_vfs(j);
649
Dylan Reid648b2202015-10-23 00:50:00 -0700650 if (j->mounts_tail)
651 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400652 else
Dylan Reid648b2202015-10-23 00:50:00 -0700653 j->mounts_head = m;
654 j->mounts_tail = m;
655 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400656
657 return 0;
658
659error:
Dylan Reid648b2202015-10-23 00:50:00 -0700660 free(m->src);
661 free(m->dest);
662 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400663 return -ENOMEM;
664}
665
Dylan Reid648b2202015-10-23 00:50:00 -0700666int API minijail_bind(struct minijail *j, const char *src, const char *dest,
667 int writeable)
668{
669 unsigned long flags = MS_BIND;
670
671 if (!writeable)
672 flags |= MS_RDONLY;
673
674 return minijail_mount(j, src, dest, "", flags);
675}
676
Will Drewry6ac91122011-10-21 16:38:58 -0500677void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400678{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700679 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800680 if ((errno == EINVAL) && can_softfail()) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800681 warn("not loading seccomp filter,"
682 " seccomp not supported");
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800683 j->flags.seccomp_filter = 0;
684 j->flags.log_seccomp_filter = 0;
685 j->filter_len = 0;
686 j->filter_prog = NULL;
687 j->flags.no_new_privs = 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700688 }
689 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400690 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800691 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700692 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400693 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800694
695 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700696 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
697 die("failed to compile seccomp filter BPF program in '%s'",
698 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800699 }
700
701 j->filter_len = fprog->len;
702 j->filter_prog = fprog;
703
Elly Jonese1749eb2011-10-07 13:54:59 -0400704 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500705}
706
Andrew Brestickereac28942015-11-11 16:04:46 -0800707int API minijail_use_alt_syscall(struct minijail *j, const char *table)
708{
709 j->alt_syscall_table = strdup(table);
710 if (!j->alt_syscall_table)
711 return -ENOMEM;
712 j->flags.alt_syscall = 1;
713 return 0;
714}
715
Will Drewryf89aef52011-09-16 16:48:57 -0500716struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400717 size_t available;
718 size_t total;
719 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500720};
721
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800722void marshal_state_init(struct marshal_state *state, char *buf,
723 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400724{
725 state->available = available;
726 state->buf = buf;
727 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500728}
729
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800730void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400731{
732 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500733
Elly Jonese1749eb2011-10-07 13:54:59 -0400734 /* Up to |available| will be written. */
735 if (copy_len) {
736 memcpy(state->buf, src, copy_len);
737 state->buf += copy_len;
738 state->available -= copy_len;
739 }
740 /* |total| will contain the expected length. */
741 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500742}
743
Will Drewry6ac91122011-10-21 16:38:58 -0500744void minijail_marshal_helper(struct marshal_state *state,
745 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400746{
Dylan Reid648b2202015-10-23 00:50:00 -0700747 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800748 size_t i;
749
Elly Jonese1749eb2011-10-07 13:54:59 -0400750 marshal_append(state, (char *)j, sizeof(*j));
751 if (j->user)
752 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800753 if (j->suppl_gid_list) {
754 marshal_append(state, j->suppl_gid_list,
755 j->suppl_gid_count * sizeof(gid_t));
756 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400757 if (j->chrootdir)
758 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800759 if (j->alt_syscall_table) {
760 marshal_append(state, j->alt_syscall_table,
761 strlen(j->alt_syscall_table) + 1);
762 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800763 if (j->flags.seccomp_filter && j->filter_prog) {
764 struct sock_fprog *fp = j->filter_prog;
765 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800766 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400767 }
Dylan Reid648b2202015-10-23 00:50:00 -0700768 for (m = j->mounts_head; m; m = m->next) {
769 marshal_append(state, m->src, strlen(m->src) + 1);
770 marshal_append(state, m->dest, strlen(m->dest) + 1);
771 marshal_append(state, m->type, strlen(m->type) + 1);
772 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400773 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800774 for (i = 0; i < j->cgroup_count; ++i)
775 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500776}
777
Will Drewry6ac91122011-10-21 16:38:58 -0500778size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400779{
780 struct marshal_state state;
781 marshal_state_init(&state, NULL, 0);
782 minijail_marshal_helper(&state, j);
783 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500784}
785
Elly Jonese1749eb2011-10-07 13:54:59 -0400786int minijail_marshal(const struct minijail *j, char *buf, size_t available)
787{
788 struct marshal_state state;
789 marshal_state_init(&state, buf, available);
790 minijail_marshal_helper(&state, j);
791 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500792}
793
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800794/*
795 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
Elly Jones51a5b6c2011-10-12 19:09:26 -0400796 * @length Number of bytes to consume
797 * @buf Buffer to consume from
798 * @buflength Size of @buf
799 *
800 * Returns a pointer to the base of the bytes, or NULL for errors.
801 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700802void *consumebytes(size_t length, char **buf, size_t *buflength)
803{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400804 char *p = *buf;
805 if (length > *buflength)
806 return NULL;
807 *buf += length;
808 *buflength -= length;
809 return p;
810}
811
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800812/*
813 * consumestr: consumes a C string from a buffer @buf of length @length
Elly Jones51a5b6c2011-10-12 19:09:26 -0400814 * @buf Buffer to consume
815 * @length Length of buffer
816 *
817 * Returns a pointer to the base of the string, or NULL for errors.
818 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700819char *consumestr(char **buf, size_t *buflength)
820{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400821 size_t len = strnlen(*buf, *buflength);
822 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700823 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400824 return NULL;
825 return consumebytes(len + 1, buf, buflength);
826}
827
Elly Jonese1749eb2011-10-07 13:54:59 -0400828int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
829{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800830 size_t i;
831 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500832 int ret = -EINVAL;
833
Elly Jonese1749eb2011-10-07 13:54:59 -0400834 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500835 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400836 memcpy((void *)j, serialized, sizeof(*j));
837 serialized += sizeof(*j);
838 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500839
Will Drewrybee7ba72011-10-21 20:47:01 -0500840 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700841 j->mounts_head = NULL;
842 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800843 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500844
Elly Jonese1749eb2011-10-07 13:54:59 -0400845 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400846 char *user = consumestr(&serialized, &length);
847 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500848 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400849 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500850 if (!j->user)
851 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400852 }
Will Drewryf89aef52011-09-16 16:48:57 -0500853
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800854 if (j->suppl_gid_list) { /* stale pointer */
855 if (j->suppl_gid_count > NGROUPS_MAX) {
856 goto bad_gid_list;
857 }
858 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
859 void *gid_list_bytes =
860 consumebytes(gid_list_size, &serialized, &length);
861 if (!gid_list_bytes)
862 goto bad_gid_list;
863
864 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
865 if (!j->suppl_gid_list)
866 goto bad_gid_list;
867
868 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
869 }
870
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400871 if (j->chrootdir) { /* stale pointer */
872 char *chrootdir = consumestr(&serialized, &length);
873 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500874 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400875 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500876 if (!j->chrootdir)
877 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400878 }
879
Andrew Brestickereac28942015-11-11 16:04:46 -0800880 if (j->alt_syscall_table) { /* stale pointer */
881 char *alt_syscall_table = consumestr(&serialized, &length);
882 if (!alt_syscall_table)
883 goto bad_syscall_table;
884 j->alt_syscall_table = strdup(alt_syscall_table);
885 if (!j->alt_syscall_table)
886 goto bad_syscall_table;
887 }
888
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800889 if (j->flags.seccomp_filter && j->filter_len > 0) {
890 size_t ninstrs = j->filter_len;
891 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
892 ninstrs > USHRT_MAX)
893 goto bad_filters;
894
895 size_t program_len = ninstrs * sizeof(struct sock_filter);
896 void *program = consumebytes(program_len, &serialized, &length);
897 if (!program)
898 goto bad_filters;
899
900 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800901 if (!j->filter_prog)
902 goto bad_filters;
903
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800904 j->filter_prog->len = ninstrs;
905 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800906 if (!j->filter_prog->filter)
907 goto bad_filter_prog_instrs;
908
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800909 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400910 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400911
Dylan Reid648b2202015-10-23 00:50:00 -0700912 count = j->mounts_count;
913 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400914 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700915 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400916 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700917 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400918 const char *src = consumestr(&serialized, &length);
919 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700920 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400921 dest = consumestr(&serialized, &length);
922 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700923 goto bad_mounts;
924 type = consumestr(&serialized, &length);
925 if (!type)
926 goto bad_mounts;
927 flags = consumebytes(sizeof(*flags), &serialized, &length);
928 if (!flags)
929 goto bad_mounts;
930 if (minijail_mount(j, src, dest, type, *flags))
931 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400932 }
933
Dylan Reid605ce7f2016-01-19 19:21:00 -0800934 count = j->cgroup_count;
935 j->cgroup_count = 0;
936 for (i = 0; i < count; ++i) {
937 char *cgroup = consumestr(&serialized, &length);
938 if (!cgroup)
939 goto bad_cgroups;
940 j->cgroups[i] = strdup(cgroup);
941 if (!j->cgroups[i])
942 goto bad_cgroups;
943 ++j->cgroup_count;
944 }
945
Elly Jonese1749eb2011-10-07 13:54:59 -0400946 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500947
Dylan Reid605ce7f2016-01-19 19:21:00 -0800948bad_cgroups:
949 while (j->mounts_head) {
950 struct mountpoint *m = j->mounts_head;
951 j->mounts_head = j->mounts_head->next;
952 free(m->type);
953 free(m->dest);
954 free(m->src);
955 free(m);
956 }
957 for (i = 0; i < j->cgroup_count; ++i)
958 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -0700959bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800960 if (j->flags.seccomp_filter && j->filter_len > 0) {
961 free(j->filter_prog->filter);
962 free(j->filter_prog);
963 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800964bad_filter_prog_instrs:
965 if (j->filter_prog)
966 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -0500967bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800968 if (j->alt_syscall_table)
969 free(j->alt_syscall_table);
970bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500971 if (j->chrootdir)
972 free(j->chrootdir);
973bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800974 if (j->suppl_gid_list)
975 free(j->suppl_gid_list);
976bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -0500977 if (j->user)
978 free(j->user);
979clear_pointers:
980 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800981 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500982 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800983 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800984 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500985out:
986 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500987}
988
Dylan Reidce5b55e2016-01-13 11:04:16 -0800989static void write_ugid_mappings(const struct minijail *j)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800990{
991 int fd, ret, len;
992 size_t sz;
993 char fname[32];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800994
995 sz = sizeof(fname);
996 if (j->uidmap) {
997 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700998 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800999 die("failed to write file name of uid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001000 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001001 if (fd < 0)
1002 pdie("failed to open '%s'", fname);
1003 len = strlen(j->uidmap);
1004 if (write(fd, j->uidmap, len) < len)
1005 die("failed to set uid_map");
1006 close(fd);
1007 }
1008 if (j->gidmap) {
1009 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001010 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001011 die("failed to write file name of gid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001012 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001013 if (fd < 0)
1014 pdie("failed to open '%s'", fname);
1015 len = strlen(j->gidmap);
1016 if (write(fd, j->gidmap, len) < len)
1017 die("failed to set gid_map");
1018 close(fd);
1019 }
Dylan Reidce5b55e2016-01-13 11:04:16 -08001020}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001021
Dylan Reidce5b55e2016-01-13 11:04:16 -08001022static void parent_setup_complete(int *pipe_fds)
1023{
1024 close(pipe_fds[0]);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001025 close(pipe_fds[1]);
1026}
1027
Dylan Reidce5b55e2016-01-13 11:04:16 -08001028/*
1029 * wait_for_parent_setup: Called by the child process to wait for any
1030 * further parent-side setup to complete before continuing.
1031 */
1032static void wait_for_parent_setup(int *pipe_fds)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001033{
1034 char buf;
1035
1036 close(pipe_fds[1]);
1037
Dylan Reidce5b55e2016-01-13 11:04:16 -08001038 /* Wait for parent to complete setup and close the pipe. */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001039 if (read(pipe_fds[0], &buf, 1) != 0)
1040 die("failed to sync with parent");
1041 close(pipe_fds[0]);
Dylan Reidce5b55e2016-01-13 11:04:16 -08001042}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001043
Dylan Reidce5b55e2016-01-13 11:04:16 -08001044static void enter_user_namespace(const struct minijail *j)
1045{
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001046 if (j->uidmap && setresuid(0, 0, 0))
1047 pdie("setresuid");
1048 if (j->gidmap && setresgid(0, 0, 0))
1049 pdie("setresgid");
1050}
1051
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001052/*
1053 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001054 * @j Minijail these mounts are for
1055 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001056 *
1057 * Returns 0 for success.
1058 */
Dylan Reid648b2202015-10-23 00:50:00 -07001059static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001060{
Dylan Reid648b2202015-10-23 00:50:00 -07001061 int ret;
1062 char *dest;
1063 int remount_ro = 0;
1064
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001065 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001066 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001067 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001068
1069 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001070 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1071 * can't both be specified in the original bind mount.
1072 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001073 */
1074 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1075 remount_ro = 1;
1076 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001077 }
Dylan Reid648b2202015-10-23 00:50:00 -07001078
1079 ret = mount(m->src, dest, m->type, m->flags, NULL);
1080 if (ret)
1081 pdie("mount: %s -> %s", m->src, dest);
1082
1083 if (remount_ro) {
1084 m->flags |= MS_RDONLY;
1085 ret = mount(m->src, dest, NULL,
1086 m->flags | MS_REMOUNT, NULL);
1087 if (ret)
1088 pdie("bind ro: %s -> %s", m->src, dest);
1089 }
1090
Elly Jones51a5b6c2011-10-12 19:09:26 -04001091 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001092 if (m->next)
1093 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001094 return ret;
1095}
1096
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001097int enter_chroot(const struct minijail *j)
1098{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001099 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001100
1101 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001102 return ret;
1103
1104 if (chroot(j->chrootdir))
1105 return -errno;
1106
1107 if (chdir("/"))
1108 return -errno;
1109
1110 return 0;
1111}
1112
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001113int enter_pivot_root(const struct minijail *j)
1114{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001115 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001116
1117 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001118 return ret;
1119
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001120 /*
1121 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001122 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001123 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001124 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001125 if (oldroot < 0)
1126 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001127 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001128 if (newroot < 0)
1129 pdie("failed to open %s for fchdir", j->chrootdir);
1130
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001131 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001132 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001133 * do a self bind mount.
1134 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001135 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1136 pdie("failed to bind mount '%s'", j->chrootdir);
1137 if (chdir(j->chrootdir))
1138 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001139 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001140 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001141
1142 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001143 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001144 * change to the old root and unmount it.
1145 */
1146 if (fchdir(oldroot))
1147 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001148
1149 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001150 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1151 * there could be a shared mount point under |oldroot|. In that case,
1152 * mounts under this shared mount point will be unmounted below, and
1153 * this unmounting will propagate to the original mount namespace
1154 * (because the mount point is shared). To prevent this unexpected
1155 * unmounting, remove these mounts from their peer groups by recursively
1156 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001157 */
1158 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001159 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001160 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001161 if (umount2(".", MNT_DETACH))
1162 pdie("umount(/)");
1163 /* Change back to the new root. */
1164 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001165 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001166 if (close(oldroot))
1167 return -errno;
1168 if (close(newroot))
1169 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001170 if (chroot("/"))
1171 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001172 /* Set correct CWD for getcwd(3). */
1173 if (chdir("/"))
1174 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001175
1176 return 0;
1177}
1178
Lee Campbell11af0622014-05-22 12:36:04 -07001179int mount_tmp(void)
1180{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001181 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001182}
1183
Dylan Reid791f5772015-09-14 20:02:42 -07001184int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001185{
1186 const char *kProcPath = "/proc";
1187 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001188 /*
1189 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001190 * /proc in our namespace, which means using MS_REMOUNT here would
1191 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001192 * namespace (!). Instead, remove their mount from our namespace lazily
1193 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001194 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001195 if (umount2(kProcPath, MNT_DETACH)) {
1196 /*
1197 * If we are in a new user namespace, umount(2) will fail.
1198 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1199 */
1200 if (j->flags.userns) {
1201 info("umount(/proc, MNT_DETACH) failed, "
1202 "this is expected when using user namespaces");
1203 } else {
1204 return -errno;
1205 }
1206 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001207 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1208 return -errno;
1209 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001210}
1211
Dylan Reid605ce7f2016-01-19 19:21:00 -08001212static void write_pid_to_path(pid_t pid, const char *path)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001213{
Dylan Reid605ce7f2016-01-19 19:21:00 -08001214 FILE *fp = fopen(path, "w");
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001215
1216 if (!fp)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001217 pdie("failed to open '%s'", path);
1218 if (fprintf(fp, "%d\n", (int)pid) < 0)
1219 pdie("fprintf(%s)", path);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001220 if (fclose(fp))
Dylan Reid605ce7f2016-01-19 19:21:00 -08001221 pdie("fclose(%s)", path);
1222}
1223
1224static void write_pid_file(const struct minijail *j)
1225{
1226 write_pid_to_path(j->initpid, j->pid_file_path);
1227}
1228
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001229static void add_to_cgroups(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001230{
1231 size_t i;
1232
1233 for (i = 0; i < j->cgroup_count; ++i)
1234 write_pid_to_path(j->initpid, j->cgroups[i]);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001235}
1236
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001237void drop_ugid(const struct minijail *j)
1238{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001239 if (j->flags.usergroups && j->flags.suppl_gids) {
1240 die("tried to inherit *and* set supplementary groups;"
1241 " can only do one");
1242 }
1243
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001244 if (j->flags.usergroups) {
1245 if (initgroups(j->user, j->usergid))
1246 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001247 } else if (j->flags.suppl_gids) {
1248 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1249 pdie("setgroups");
1250 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001251 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001252 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001253 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001254 * users.
1255 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001256 if ((j->uid || j->gid) && setgroups(0, NULL))
1257 pdie("setgroups");
1258 }
1259
1260 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1261 pdie("setresgid");
1262
1263 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1264 pdie("setresuid");
1265}
1266
Mike Frysinger3adfef72013-05-09 17:19:08 -04001267/*
1268 * We specifically do not use cap_valid() as that only tells us the last
1269 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001270 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001271 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001272 * kernel).
1273 * Normally, we suck up the answer via /proc. On Android, not all processes are
1274 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1275 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001276 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001277static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001278{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001279 unsigned int last_valid_cap = 0;
1280 if (is_android()) {
1281 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1282 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001283
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001284 /* |last_valid_cap| will be the first failing value. */
1285 if (last_valid_cap > 0) {
1286 last_valid_cap--;
1287 }
1288 } else {
1289 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1290 FILE *fp = fopen(cap_file, "re");
1291 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1292 pdie("fscanf(%s)", cap_file);
1293 fclose(fp);
1294 }
Dylan Reidf682d472015-09-17 21:39:07 -07001295 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001296}
1297
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001298static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1299{
1300 const uint64_t one = 1;
1301 unsigned int i;
1302 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1303 if (keep_mask & (one << i))
1304 continue;
1305 if (prctl(PR_CAPBSET_DROP, i))
1306 pdie("could not drop capability from bounding set");
1307 }
1308}
1309
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001310void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001311{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001312 if (!j->flags.use_caps)
1313 return;
1314
Elly Jonese1749eb2011-10-07 13:54:59 -04001315 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001316 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001317 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001318 unsigned int i;
1319 if (!caps)
1320 die("can't get process caps");
1321 if (cap_clear_flag(caps, CAP_INHERITABLE))
1322 die("can't clear inheritable caps");
1323 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1324 die("can't clear effective caps");
1325 if (cap_clear_flag(caps, CAP_PERMITTED))
1326 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001327 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001328 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001329 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001330 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001331 flag[0] = i;
1332 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001333 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001334 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001335 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001336 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001337 die("can't add inheritable cap");
1338 }
1339 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001340 die("can't apply initial cleaned capset");
1341
1342 /*
1343 * Instead of dropping bounding set first, do it here in case
1344 * the caller had a more permissive bounding set which could
1345 * have been used above to raise a capability that wasn't already
1346 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1347 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001348 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001349
1350 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001351 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001352 flag[0] = CAP_SETPCAP;
1353 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1354 die("can't clear effective cap");
1355 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1356 die("can't clear permitted cap");
1357 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1358 die("can't clear inheritable cap");
1359 }
1360
1361 if (cap_set_proc(caps))
1362 die("can't apply final cleaned capset");
1363
1364 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001365}
1366
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001367void set_seccomp_filter(const struct minijail *j)
1368{
1369 /*
1370 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1371 * in the kernel source tree for an explanation of the parameters.
1372 */
1373 if (j->flags.no_new_privs) {
1374 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1375 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1376 }
1377
1378 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001379 * Code running with ASan
1380 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1381 * will make system calls not included in the syscall filter policy,
1382 * which will likely crash the program. Skip setting seccomp filter in
1383 * that case.
1384 * 'running_with_asan()' has no inputs and is completely defined at
1385 * build time, so this cannot be used by an attacker to skip setting
1386 * seccomp filter.
1387 */
1388 if (j->flags.seccomp_filter && running_with_asan()) {
1389 warn("running with ASan, not setting seccomp filter");
1390 return;
1391 }
1392
1393 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001394 * If we're logging seccomp filter failures,
1395 * install the SIGSYS handler first.
1396 */
1397 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1398 if (install_sigsys_handler())
1399 pdie("install SIGSYS handler");
1400 warn("logging seccomp filter failures");
1401 }
1402
1403 /*
1404 * Install the syscall filter.
1405 */
1406 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001407 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1408 j->filter_prog)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001409 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001410 warn("seccomp not supported");
1411 return;
1412 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001413 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001414 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001415 }
1416}
1417
Will Drewry6ac91122011-10-21 16:38:58 -05001418void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001419{
Dylan Reidf682d472015-09-17 21:39:07 -07001420 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001421 * If we're dropping caps, get the last valid cap from /proc now,
1422 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001423 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001424 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001425 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001426 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001427
Elly Jonese1749eb2011-10-07 13:54:59 -04001428 if (j->flags.pids)
1429 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001430 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001431
Elly Jonese1749eb2011-10-07 13:54:59 -04001432 if (j->flags.usergroups && !j->user)
1433 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001434
Elly Jonesdd3e8512012-01-23 15:13:38 -05001435 /*
1436 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001437 * so we don't even try. If any of our operations fail, we abort() the
1438 * entire process.
1439 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001440 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1441 pdie("setns(CLONE_NEWNS)");
1442
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001443 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001444 if (unshare(CLONE_NEWNS))
1445 pdie("unshare(vfs)");
1446 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001447 * Unless asked not to, remount all filesystems as private.
1448 * If they are shared, new bind mounts will creep out of our
1449 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001450 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1451 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001452 if (!j->flags.skip_remount_private) {
1453 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1454 pdie("mount(/, private)");
1455 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001456 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001457
Dylan Reidf7942472015-11-18 17:55:26 -08001458 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1459 pdie("unshare(ipc)");
1460 }
1461
Dylan Reid1102f5a2015-09-15 11:52:20 -07001462 if (j->flags.enter_net) {
1463 if (setns(j->netns_fd, CLONE_NEWNET))
1464 pdie("setns(CLONE_NEWNET)");
1465 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001466 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001467 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001468
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001469 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
1470 pdie("unshare(cgroups)");
1471
Elly Jones51a5b6c2011-10-12 19:09:26 -04001472 if (j->flags.chroot && enter_chroot(j))
1473 pdie("chroot");
1474
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001475 if (j->flags.pivot_root && enter_pivot_root(j))
1476 pdie("pivot_root");
1477
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001478 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001479 pdie("mount_tmp");
1480
Dylan Reid791f5772015-09-14 20:02:42 -07001481 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001482 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001483
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001484 /*
1485 * If we're only dropping capabilities from the bounding set, but not
1486 * from the thread's (permitted|inheritable|effective) sets, do it now.
1487 */
1488 if (j->flags.capbset_drop) {
1489 drop_capbset(j->cap_bset, last_valid_cap);
1490 }
1491
1492 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001493 /*
1494 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001495 * capability to change uids, our attempt to use setuid()
1496 * below will fail. Hang on to root caps across setuid(), then
1497 * lock securebits.
1498 */
1499 if (prctl(PR_SET_KEEPCAPS, 1))
1500 pdie("prctl(PR_SET_KEEPCAPS)");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001501
1502 /*
1503 * Kernels 4.3+ define a new securebit
1504 * (SECURE_NO_CAP_AMBIENT_RAISE), so using the SECURE_ALL_BITS
1505 * and SECURE_ALL_LOCKS masks from newer kernel headers will
1506 * return EPERM on older kernels. Detect this, and retry with
1507 * the right mask for older (2.6.26-4.2) kernels.
1508 */
1509 int securebits_ret = prctl(PR_SET_SECUREBITS,
1510 SECURE_ALL_BITS | SECURE_ALL_LOCKS);
1511 if (securebits_ret < 0) {
1512 if (errno == EPERM) {
1513 /* Possibly running on kernel < 4.3. */
1514 securebits_ret = prctl(
1515 PR_SET_SECUREBITS,
1516 OLD_SECURE_ALL_BITS | OLD_SECURE_ALL_LOCKS);
1517 }
1518 }
1519 if (securebits_ret < 0)
Elly Jonese1749eb2011-10-07 13:54:59 -04001520 pdie("prctl(PR_SET_SECUREBITS)");
1521 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001522
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001523 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001524 /*
1525 * If we're setting no_new_privs, we can drop privileges
1526 * before setting seccomp filter. This way filter policies
1527 * don't need to allow privilege-dropping syscalls.
1528 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001529 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001530 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001531 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001532 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001533 /*
1534 * If we're not setting no_new_privs,
1535 * we need to set seccomp filter *before* dropping privileges.
1536 * WARNING: this means that filter policies *must* allow
1537 * setgroups()/setresgid()/setresuid() for dropping root and
1538 * capget()/capset()/prctl() for dropping caps.
1539 */
1540 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001541 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001542 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001543 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001544
Elly Jonesdd3e8512012-01-23 15:13:38 -05001545 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001546 * Select the specified alternate syscall table. The table must not
1547 * block prctl(2) if we're using seccomp as well.
1548 */
1549 if (j->flags.alt_syscall) {
1550 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1551 pdie("prctl(PR_ALT_SYSCALL)");
1552 }
1553
1554 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001555 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001556 * privilege-dropping syscalls :)
1557 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001558 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001559 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001560 warn("seccomp not supported");
1561 return;
1562 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001563 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001564 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001565}
1566
Will Drewry6ac91122011-10-21 16:38:58 -05001567/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001568static int init_exitstatus = 0;
1569
Will Drewry6ac91122011-10-21 16:38:58 -05001570void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001571{
1572 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001573}
1574
Will Drewry6ac91122011-10-21 16:38:58 -05001575int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001576{
1577 pid_t pid;
1578 int status;
1579 /* so that we exit with the right status */
1580 signal(SIGTERM, init_term);
1581 /* TODO(wad) self jail with seccomp_filters here. */
1582 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001583 /*
1584 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001585 * left inside our pid namespace or we get a signal.
1586 */
1587 if (pid == rootpid)
1588 init_exitstatus = status;
1589 }
1590 if (!WIFEXITED(init_exitstatus))
1591 _exit(MINIJAIL_ERR_INIT);
1592 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001593}
1594
Will Drewry6ac91122011-10-21 16:38:58 -05001595int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001596{
1597 size_t sz = 0;
1598 size_t bytes = read(fd, &sz, sizeof(sz));
1599 char *buf;
1600 int r;
1601 if (sizeof(sz) != bytes)
1602 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001603 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001604 return -E2BIG;
1605 buf = malloc(sz);
1606 if (!buf)
1607 return -ENOMEM;
1608 bytes = read(fd, buf, sz);
1609 if (bytes != sz) {
1610 free(buf);
1611 return -EINVAL;
1612 }
1613 r = minijail_unmarshal(j, buf, sz);
1614 free(buf);
1615 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001616}
1617
Will Drewry6ac91122011-10-21 16:38:58 -05001618int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001619{
1620 char *buf;
1621 size_t sz = minijail_size(j);
1622 ssize_t written;
1623 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001624
Elly Jonese1749eb2011-10-07 13:54:59 -04001625 if (!sz)
1626 return -EINVAL;
1627 buf = malloc(sz);
1628 r = minijail_marshal(j, buf, sz);
1629 if (r) {
1630 free(buf);
1631 return r;
1632 }
1633 /* Sends [size][minijail]. */
1634 written = write(fd, &sz, sizeof(sz));
1635 if (written != sizeof(sz)) {
1636 free(buf);
1637 return -EFAULT;
1638 }
1639 written = write(fd, buf, sz);
1640 if (written < 0 || (size_t) written != sz) {
1641 free(buf);
1642 return -EFAULT;
1643 }
1644 free(buf);
1645 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001646}
Elly Jonescd7a9042011-07-22 13:56:51 -04001647
Will Drewry6ac91122011-10-21 16:38:58 -05001648int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001649{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001650#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001651 /* Don't use LDPRELOAD on Brillo. */
1652 return 0;
1653#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001654 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1655 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1656 if (!newenv)
1657 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001658
Elly Jonese1749eb2011-10-07 13:54:59 -04001659 /* Only insert a separating space if we have something to separate... */
1660 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1661 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001662
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001663 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001664 setenv(kLdPreloadEnvVar, newenv, 1);
1665 free(newenv);
1666 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001667#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001668}
1669
Will Drewry6ac91122011-10-21 16:38:58 -05001670int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001671{
1672 int r = pipe(fds);
1673 char fd_buf[11];
1674 if (r)
1675 return r;
1676 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1677 if (r <= 0)
1678 return -EINVAL;
1679 setenv(kFdEnvVar, fd_buf, 1);
1680 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001681}
1682
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001683int setup_pipe_end(int fds[2], size_t index)
1684{
1685 if (index > 1)
1686 return -1;
1687
1688 close(fds[1 - index]);
1689 return fds[index];
1690}
1691
1692int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1693{
1694 if (index > 1)
1695 return -1;
1696
1697 close(fds[1 - index]);
1698 /* dup2(2) the corresponding end of the pipe into |fd|. */
1699 return dup2(fds[index], fd);
1700}
1701
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001702int minijail_run_internal(struct minijail *j, const char *filename,
1703 char *const argv[], pid_t *pchild_pid,
1704 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1705 int use_preload);
1706
Will Drewry6ac91122011-10-21 16:38:58 -05001707int API minijail_run(struct minijail *j, const char *filename,
1708 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001709{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001710 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1711 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001712}
1713
1714int API minijail_run_pid(struct minijail *j, const char *filename,
1715 char *const argv[], pid_t *pchild_pid)
1716{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001717 return minijail_run_internal(j, filename, argv, pchild_pid,
1718 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001719}
1720
1721int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001722 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001723{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001724 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1725 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001726}
1727
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001728int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001729 char *const argv[], pid_t *pchild_pid,
1730 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001731{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001732 return minijail_run_internal(j, filename, argv, pchild_pid,
1733 pstdin_fd, pstdout_fd, pstderr_fd, true);
1734}
1735
1736int API minijail_run_no_preload(struct minijail *j, const char *filename,
1737 char *const argv[])
1738{
1739 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1740 false);
1741}
1742
Samuel Tan63187f42015-10-16 13:01:53 -07001743int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001744 const char *filename,
1745 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001746 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001747 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001748 int *pstderr_fd)
1749{
Samuel Tan63187f42015-10-16 13:01:53 -07001750 return minijail_run_internal(j, filename, argv, pchild_pid,
1751 pstdin_fd, pstdout_fd, pstderr_fd, false);
1752}
1753
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001754int minijail_run_internal(struct minijail *j, const char *filename,
1755 char *const argv[], pid_t *pchild_pid,
1756 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1757 int use_preload)
1758{
Elly Jonese1749eb2011-10-07 13:54:59 -04001759 char *oldenv, *oldenv_copy = NULL;
1760 pid_t child_pid;
1761 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001762 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001763 int stdout_fds[2];
1764 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001765 int child_sync_pipe_fds[2];
1766 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001767 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001768 /* We need to remember this across the minijail_preexec() call. */
1769 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001770 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001771
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001772 if (use_preload) {
1773 oldenv = getenv(kLdPreloadEnvVar);
1774 if (oldenv) {
1775 oldenv_copy = strdup(oldenv);
1776 if (!oldenv_copy)
1777 return -ENOMEM;
1778 }
1779
1780 if (setup_preload())
1781 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001782 }
Will Drewryf89aef52011-09-16 16:48:57 -05001783
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001784 if (!use_preload) {
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001785 if (j->flags.use_caps)
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001786 die("capabilities are not supported without "
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001787 "LD_PRELOAD");
1788 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001789
Elly Jonesdd3e8512012-01-23 15:13:38 -05001790 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001791 * Make the process group ID of this process equal to its PID, so that
1792 * both the Minijail process and the jailed process can be killed
1793 * together.
1794 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1795 * the process is already a process group leader.
1796 */
1797 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1798 if (errno != EPERM) {
1799 pdie("setpgid(0, 0)");
1800 }
1801 }
1802
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001803 if (use_preload) {
1804 /*
1805 * Before we fork(2) and execve(2) the child process, we need
1806 * to open a pipe(2) to send the minijail configuration over.
1807 */
1808 if (setup_pipe(pipe_fds))
1809 return -EFAULT;
1810 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001811
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001812 /*
1813 * If we want to write to the child process' standard input,
1814 * create the pipe(2) now.
1815 */
1816 if (pstdin_fd) {
1817 if (pipe(stdin_fds))
1818 return -EFAULT;
1819 }
1820
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001821 /*
1822 * If we want to read from the child process' standard output,
1823 * create the pipe(2) now.
1824 */
1825 if (pstdout_fd) {
1826 if (pipe(stdout_fds))
1827 return -EFAULT;
1828 }
1829
1830 /*
1831 * If we want to read from the child process' standard error,
1832 * create the pipe(2) now.
1833 */
1834 if (pstderr_fd) {
1835 if (pipe(stderr_fds))
1836 return -EFAULT;
1837 }
1838
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001839 /*
1840 * If we want to set up a new uid/gid mapping in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001841 * or if we need to add the child process to cgroups, create the pipe(2)
1842 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001843 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001844 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001845 sync_child = 1;
1846 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001847 return -EFAULT;
1848 }
1849
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001850 /*
1851 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001852 *
1853 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1854 *
1855 * In multithreaded programs, there are a bunch of locks inside libc,
1856 * some of which may be held by other threads at the time that we call
1857 * minijail_run_pid(). If we call fork(), glibc does its level best to
1858 * ensure that we hold all of these locks before it calls clone()
1859 * internally and drop them after clone() returns, but when we call
1860 * sys_clone(2) directly, all that gets bypassed and we end up with a
1861 * child address space where some of libc's important locks are held by
1862 * other threads (which did not get cloned, and hence will never release
1863 * those locks). This is okay so long as we call exec() immediately
1864 * after, but a bunch of seemingly-innocent libc functions like setenv()
1865 * take locks.
1866 *
1867 * Hence, only call sys_clone() if we need to, in order to get at pid
1868 * namespacing. If we follow this path, the child's address space might
1869 * have broken locks; you may only call functions that do not acquire
1870 * any locks.
1871 *
1872 * Unfortunately, fork() acquires every lock it can get its hands on, as
1873 * previously detailed, so this function is highly likely to deadlock
1874 * later on (see "deadlock here") if we're multithreaded.
1875 *
1876 * We might hack around this by having the clone()d child (init of the
1877 * pid namespace) return directly, rather than leaving the clone()d
1878 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001879 * its fork()ed child return in turn), but that process would be
1880 * crippled with its libc locks potentially broken. We might try
1881 * fork()ing in the parent before we clone() to ensure that we own all
1882 * the locks, but then we have to have the forked child hanging around
1883 * consuming resources (and possibly having file descriptors / shared
1884 * memory regions / etc attached). We'd need to keep the child around to
1885 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04001886 *
1887 * TODO(ellyjones): figure out if the "forked child hanging around"
1888 * problem is fixable or not. It would be nice if we worked in this
1889 * case.
1890 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001891 if (pid_namespace) {
1892 int clone_flags = CLONE_NEWPID | SIGCHLD;
1893 if (j->flags.userns)
1894 clone_flags |= CLONE_NEWUSER;
1895 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001896 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001897 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001898 }
Elly Jones761b7412012-06-13 15:49:52 -04001899
Elly Jonese1749eb2011-10-07 13:54:59 -04001900 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001901 if (use_preload) {
1902 free(oldenv_copy);
1903 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001904 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001905 }
Will Drewryf89aef52011-09-16 16:48:57 -05001906
Elly Jonese1749eb2011-10-07 13:54:59 -04001907 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001908 if (use_preload) {
1909 /* Restore parent's LD_PRELOAD. */
1910 if (oldenv_copy) {
1911 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1912 free(oldenv_copy);
1913 } else {
1914 unsetenv(kLdPreloadEnvVar);
1915 }
1916 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001917 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001918
Elly Jonese1749eb2011-10-07 13:54:59 -04001919 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001920
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001921 if (j->flags.pid_file)
1922 write_pid_file(j);
1923
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001924 if (j->flags.cgroups)
1925 add_to_cgroups(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001926
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001927 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001928 write_ugid_mappings(j);
1929
1930 if (sync_child)
1931 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001932
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001933 if (use_preload) {
1934 /* Send marshalled minijail. */
1935 close(pipe_fds[0]); /* read endpoint */
1936 ret = minijail_to_fd(j, pipe_fds[1]);
1937 close(pipe_fds[1]); /* write endpoint */
1938 if (ret) {
1939 kill(j->initpid, SIGKILL);
1940 die("failed to send marshalled minijail");
1941 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001942 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001943
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001944 if (pchild_pid)
1945 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001946
1947 /*
1948 * If we want to write to the child process' standard input,
1949 * set up the write end of the pipe.
1950 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001951 if (pstdin_fd)
1952 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001953 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001954
1955 /*
1956 * If we want to read from the child process' standard output,
1957 * set up the read end of the pipe.
1958 */
1959 if (pstdout_fd)
1960 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001961 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001962
1963 /*
1964 * If we want to read from the child process' standard error,
1965 * set up the read end of the pipe.
1966 */
1967 if (pstderr_fd)
1968 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001969 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001970
Elly Jonese1749eb2011-10-07 13:54:59 -04001971 return 0;
1972 }
1973 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001974
Peter Qiu2860c462015-12-16 15:13:06 -08001975 if (j->flags.reset_signal_mask) {
1976 sigset_t signal_mask;
1977 if (sigemptyset(&signal_mask) != 0)
1978 pdie("sigemptyset failed");
1979 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
1980 pdie("sigprocmask failed");
1981 }
1982
Dylan Reidce5b55e2016-01-13 11:04:16 -08001983 if (sync_child)
1984 wait_for_parent_setup(child_sync_pipe_fds);
1985
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001986 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001987 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001988
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001989 /*
1990 * If we want to write to the jailed process' standard input,
1991 * set up the read end of the pipe.
1992 */
1993 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001994 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1995 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001996 die("failed to set up stdin pipe");
1997 }
1998
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001999 /*
2000 * If we want to read from the jailed process' standard output,
2001 * set up the write end of the pipe.
2002 */
2003 if (pstdout_fd) {
2004 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2005 STDOUT_FILENO) < 0)
2006 die("failed to set up stdout pipe");
2007 }
2008
2009 /*
2010 * If we want to read from the jailed process' standard error,
2011 * set up the write end of the pipe.
2012 */
2013 if (pstderr_fd) {
2014 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2015 STDERR_FILENO) < 0)
2016 die("failed to set up stderr pipe");
2017 }
2018
Dylan Reid791f5772015-09-14 20:02:42 -07002019 /* If running an init program, let it decide when/how to mount /proc. */
2020 if (pid_namespace && !do_init)
2021 j->flags.remount_proc_ro = 0;
2022
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002023 if (use_preload) {
2024 /* Strip out flags that cannot be inherited across execve(2). */
2025 minijail_preexec(j);
2026 } else {
2027 j->flags.pids = 0;
2028 }
2029 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002030 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002031
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002032 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002033 /*
2034 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002035 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002036 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002037 * a child to actually run the program. If |do_init == 0|, we
2038 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002039 *
2040 * If we're multithreaded, we'll probably deadlock here. See
2041 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002042 */
2043 child_pid = fork();
2044 if (child_pid < 0)
2045 _exit(child_pid);
2046 else if (child_pid > 0)
2047 init(child_pid); /* never returns */
2048 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002049
Elly Jonesdd3e8512012-01-23 15:13:38 -05002050 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002051 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002052 * calling process
2053 * -> execve()-ing process
2054 * If we are:
2055 * calling process
2056 * -> init()-ing process
2057 * -> execve()-ing process
2058 */
2059 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04002060}
2061
Will Drewry6ac91122011-10-21 16:38:58 -05002062int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002063{
2064 int st;
2065 if (kill(j->initpid, SIGTERM))
2066 return -errno;
2067 if (waitpid(j->initpid, &st, 0) < 0)
2068 return -errno;
2069 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002070}
2071
Will Drewry6ac91122011-10-21 16:38:58 -05002072int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002073{
2074 int st;
2075 if (waitpid(j->initpid, &st, 0) < 0)
2076 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002077
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002078 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002079 int error_status = st;
2080 if (WIFSIGNALED(st)) {
2081 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002082 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002083 j->initpid, signum);
2084 /*
2085 * We return MINIJAIL_ERR_JAIL if the process received
2086 * SIGSYS, which happens when a syscall is blocked by
2087 * seccomp filters.
2088 * If not, we do what bash(1) does:
2089 * $? = 128 + signum
2090 */
2091 if (signum == SIGSYS) {
2092 error_status = MINIJAIL_ERR_JAIL;
2093 } else {
2094 error_status = 128 + signum;
2095 }
2096 }
2097 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002098 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002099
2100 int exit_status = WEXITSTATUS(st);
2101 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002102 info("child process %d exited with status %d",
2103 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002104
2105 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002106}
2107
Will Drewry6ac91122011-10-21 16:38:58 -05002108void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002109{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002110 size_t i;
2111
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002112 if (j->flags.seccomp_filter && j->filter_prog) {
2113 free(j->filter_prog->filter);
2114 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002115 }
Dylan Reid648b2202015-10-23 00:50:00 -07002116 while (j->mounts_head) {
2117 struct mountpoint *m = j->mounts_head;
2118 j->mounts_head = j->mounts_head->next;
2119 free(m->type);
2120 free(m->dest);
2121 free(m->src);
2122 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002123 }
Dylan Reid648b2202015-10-23 00:50:00 -07002124 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002125 if (j->user)
2126 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002127 if (j->suppl_gid_list)
2128 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002129 if (j->chrootdir)
2130 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08002131 if (j->alt_syscall_table)
2132 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002133 for (i = 0; i < j->cgroup_count; ++i)
2134 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002135 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002136}