blob: 597fef2d1e39a6f1936ba0b8736ad41f8ca0da45 [file] [log] [blame]
Jorge Lucangeli Obesd613ab22015-03-03 14:22:50 -08001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04002 * Use of this source code is governed by a BSD-style license that can be
Will Drewry32ac9f52011-08-18 21:36:27 -05003 * found in the LICENSE file.
4 */
Elly Jonescd7a9042011-07-22 13:56:51 -04005
6#define _BSD_SOURCE
Arthur Gautier7a569072016-04-23 17:25:20 +00007#define _DEFAULT_SOURCE
Elly Jonescd7a9042011-07-22 13:56:51 -04008#define _GNU_SOURCE
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07009
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080010#include <asm/unistd.h>
Will Drewry32ac9f52011-08-18 21:36:27 -050011#include <ctype.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040012#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070013#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040014#include <grp.h>
15#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050016#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040017#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040018#include <pwd.h>
19#include <sched.h>
20#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050021#include <stdarg.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070022#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080023#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <syscall.h>
28#include <sys/capability.h>
29#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050030#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040031#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070032#include <sys/stat.h>
33#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080034#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040035#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040036#include <unistd.h>
37
38#include "libminijail.h"
39#include "libminijail-private.h"
40
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070041#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080042#include "syscall_filter.h"
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040043#include "syscall_wrapper.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 Obes13650612016-09-02 11:27:29 -040077/* Seccomp filter related flags. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080078#ifndef PR_SET_NO_NEW_PRIVS
79# define PR_SET_NO_NEW_PRIVS 38
80#endif
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040081
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080082#ifndef SECCOMP_MODE_FILTER
83# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050084#endif
85
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040086#ifndef SECCOMP_SET_MODE_STRICT
87# define SECCOMP_SET_MODE_STRICT 0
88#endif
89#ifndef SECCOMP_SET_MODE_FILTER
90# define SECCOMP_SET_MODE_FILTER 1
91#endif
92
93#ifndef SECCOMP_FILTER_FLAG_TSYNC
94# define SECCOMP_FILTER_FLAG_TSYNC 1
95#endif
96/* End seccomp filter related flags. */
97
Dylan Reid4cbc2a52016-06-17 19:06:07 -070098/* New cgroup namespace might not be in linux-headers yet. */
99#ifndef CLONE_NEWCGROUP
100# define CLONE_NEWCGROUP 0x02000000
101#endif
102
Dylan Reid605ce7f2016-01-19 19:21:00 -0800103#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
104
Dylan Reid648b2202015-10-23 00:50:00 -0700105struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400106 char *src;
107 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700108 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700109 char *data;
110 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -0700111 unsigned long flags;
112 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400113};
114
Will Drewryf89aef52011-09-16 16:48:57 -0500115struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700116 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700117 * WARNING: if you add a flag here you need to make sure it's
118 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700119 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400120 struct {
121 int uid:1;
122 int gid:1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800123 int usergroups:1;
124 int suppl_gids:1;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800125 int use_caps:1;
126 int capbset_drop:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400127 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700128 int enter_vfs:1;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800129 int skip_remount_private:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400130 int pids:1;
Dylan Reidf7942472015-11-18 17:55:26 -0800131 int ipc:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400132 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700133 int enter_net:1;
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700134 int ns_cgroups:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800135 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400136 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -0700137 int remount_proc_ro:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700138 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400139 int seccomp_filter:1;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400140 int seccomp_filter_tsync:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700141 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400142 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800143 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700144 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800145 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800146 int pid_file:1;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800147 int cgroups:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800148 int alt_syscall:1;
Peter Qiu2860c462015-12-16 15:13:06 -0800149 int reset_signal_mask:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400150 } flags;
151 uid_t uid;
152 gid_t gid;
153 gid_t usergid;
154 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800155 size_t suppl_gid_count;
156 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400157 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800158 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400159 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700160 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700161 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400162 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800163 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800164 char *uidmap;
165 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800166 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800167 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800168 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700169 struct mountpoint *mounts_head;
170 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800171 size_t mounts_count;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800172 char *cgroups[MAX_CGROUPS];
173 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500174};
175
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700176/*
177 * Strip out flags meant for the parent.
178 * We keep things that are not inherited across execve(2) (e.g. capabilities),
179 * or are easier to set after execve(2) (e.g. seccomp filters).
180 */
181void minijail_preenter(struct minijail *j)
182{
183 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700184 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900185 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700186 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700187 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800188 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800189 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800190 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700191}
192
193/*
194 * Strip out flags meant for the child.
195 * We keep things that are inherited across execve(2).
196 */
197void minijail_preexec(struct minijail *j)
198{
199 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700200 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800201 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700202 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800203 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700204 if (j->user)
205 free(j->user);
206 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800207 if (j->suppl_gid_list)
208 free(j->suppl_gid_list);
209 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700210 memset(&j->flags, 0, sizeof(j->flags));
211 /* Now restore anything we meant to keep. */
212 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700213 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800214 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700215 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800216 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700217 /* Note, |pids| will already have been used before this call. */
218}
219
220/* Minijail API. */
221
Will Drewry6ac91122011-10-21 16:38:58 -0500222struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400223{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400224 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400225}
226
Will Drewry6ac91122011-10-21 16:38:58 -0500227void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400228{
229 if (uid == 0)
230 die("useless change to uid 0");
231 j->uid = uid;
232 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400233}
234
Will Drewry6ac91122011-10-21 16:38:58 -0500235void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400236{
237 if (gid == 0)
238 die("useless change to gid 0");
239 j->gid = gid;
240 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400241}
242
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800243void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
244 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800245{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800246 size_t i;
247
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800248 if (j->flags.usergroups)
249 die("cannot inherit *and* set supplementary groups");
250
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800251 if (size == 0) {
252 /* Clear supplementary groups. */
253 j->suppl_gid_list = NULL;
254 j->suppl_gid_count = 0;
255 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800256 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800257 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800258
259 /* Copy the gid_t array. */
260 j->suppl_gid_list = calloc(size, sizeof(gid_t));
261 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800262 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800263 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800264 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800265 j->suppl_gid_list[i] = list[i];
266 }
267 j->suppl_gid_count = size;
268 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800269}
270
Will Drewry6ac91122011-10-21 16:38:58 -0500271int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400272{
273 char *buf = NULL;
274 struct passwd pw;
275 struct passwd *ppw = NULL;
276 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
277 if (sz == -1)
278 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400279
Elly Jonesdd3e8512012-01-23 15:13:38 -0500280 /*
281 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400282 * the maximum needed size of the buffer, so we don't have to search.
283 */
284 buf = malloc(sz);
285 if (!buf)
286 return -ENOMEM;
287 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500288 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800289 * We're safe to free the buffer here. The strings inside |pw| point
290 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800291 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
292 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500293 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400294 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700295 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400296 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700297 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400298 minijail_change_uid(j, ppw->pw_uid);
299 j->user = strdup(user);
300 if (!j->user)
301 return -ENOMEM;
302 j->usergid = ppw->pw_gid;
303 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400304}
305
Will Drewry6ac91122011-10-21 16:38:58 -0500306int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400307{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700308 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700309 struct group gr;
310 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400311 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
312 if (sz == -1)
313 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400314
Elly Jonesdd3e8512012-01-23 15:13:38 -0500315 /*
316 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400317 * the maximum needed size of the buffer, so we don't have to search.
318 */
319 buf = malloc(sz);
320 if (!buf)
321 return -ENOMEM;
322 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500323 /*
324 * We're safe to free the buffer here. The strings inside gr point
325 * inside buf, but we don't use any of them; this leaves the pointers
326 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
327 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400328 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700329 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400330 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700331 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400332 minijail_change_gid(j, pgr->gr_gid);
333 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400334}
335
Will Drewry6ac91122011-10-21 16:38:58 -0500336void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400337{
338 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400339}
340
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700341void API minijail_no_new_privs(struct minijail *j)
342{
343 j->flags.no_new_privs = 1;
344}
345
Will Drewry6ac91122011-10-21 16:38:58 -0500346void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400347{
348 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500349}
350
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400351void API minijail_set_seccomp_filter_tsync(struct minijail *j)
352{
353 j->flags.seccomp_filter_tsync = 1;
354}
355
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700356void API minijail_log_seccomp_filter_failures(struct minijail *j)
357{
358 j->flags.log_seccomp_filter = 1;
359}
360
Will Drewry6ac91122011-10-21 16:38:58 -0500361void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400362{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800363 /*
364 * 'minijail_use_caps' configures a runtime-capabilities-only
365 * environment, including a bounding set matching the thread's runtime
366 * (permitted|inheritable|effective) sets.
367 * Therefore, it will override any existing bounding set configurations
368 * since the latter would allow gaining extra runtime capabilities from
369 * file capabilities.
370 */
371 if (j->flags.capbset_drop) {
372 warn("overriding bounding set configuration");
373 j->cap_bset = 0;
374 j->flags.capbset_drop = 0;
375 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400376 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800377 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400378}
379
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800380void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
381{
382 if (j->flags.use_caps) {
383 /*
384 * 'minijail_use_caps' will have already configured a capability
385 * bounding set matching the (permitted|inheritable|effective)
386 * sets. Abort if the user tries to configure a separate
387 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
388 * are mutually exclusive.
389 */
390 die("runtime capabilities already configured, can't drop "
391 "bounding set separately");
392 }
393 j->cap_bset = capmask;
394 j->flags.capbset_drop = 1;
395}
396
397void API minijail_reset_signal_mask(struct minijail *j)
398{
Peter Qiu2860c462015-12-16 15:13:06 -0800399 j->flags.reset_signal_mask = 1;
400}
401
Will Drewry6ac91122011-10-21 16:38:58 -0500402void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400403{
404 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400405}
406
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700407void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
408{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800409 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700410 if (ns_fd < 0) {
411 pdie("failed to open namespace '%s'", ns_path);
412 }
413 j->mountns_fd = ns_fd;
414 j->flags.enter_vfs = 1;
415}
416
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800417void API minijail_skip_remount_private(struct minijail *j)
418{
419 j->flags.skip_remount_private = 1;
420}
421
Will Drewry6ac91122011-10-21 16:38:58 -0500422void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400423{
Elly Jonese58176c2012-01-23 11:46:17 -0500424 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700425 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400426 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800427 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400428}
429
Dylan Reidf7942472015-11-18 17:55:26 -0800430void API minijail_namespace_ipc(struct minijail *j)
431{
432 j->flags.ipc = 1;
433}
434
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400435void API minijail_namespace_net(struct minijail *j)
436{
437 j->flags.net = 1;
438}
439
Dylan Reid1102f5a2015-09-15 11:52:20 -0700440void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
441{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800442 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700443 if (ns_fd < 0) {
444 pdie("failed to open namespace '%s'", ns_path);
445 }
446 j->netns_fd = ns_fd;
447 j->flags.enter_net = 1;
448}
449
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700450void API minijail_namespace_cgroups(struct minijail *j)
451{
452 j->flags.ns_cgroups = 1;
453}
454
Dylan Reid791f5772015-09-14 20:02:42 -0700455void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400456{
457 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700458 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400459}
460
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800461void API minijail_namespace_user(struct minijail *j)
462{
463 j->flags.userns = 1;
464}
465
466int API minijail_uidmap(struct minijail *j, const char *uidmap)
467{
468 j->uidmap = strdup(uidmap);
469 if (!j->uidmap)
470 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800471 char *ch;
472 for (ch = j->uidmap; *ch; ch++) {
473 if (*ch == ',')
474 *ch = '\n';
475 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800476 return 0;
477}
478
479int API minijail_gidmap(struct minijail *j, const char *gidmap)
480{
481 j->gidmap = strdup(gidmap);
482 if (!j->gidmap)
483 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800484 char *ch;
485 for (ch = j->gidmap; *ch; ch++) {
486 if (*ch == ',')
487 *ch = '\n';
488 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800489 return 0;
490}
491
Will Drewry6ac91122011-10-21 16:38:58 -0500492void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400493{
494 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400495}
496
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800497void API minijail_run_as_init(struct minijail *j)
498{
499 /*
500 * Since the jailed program will become 'init' in the new PID namespace,
501 * Minijail does not need to fork an 'init' process.
502 */
503 j->flags.do_init = 0;
504}
505
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700506int API minijail_enter_chroot(struct minijail *j, const char *dir)
507{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400508 if (j->chrootdir)
509 return -EINVAL;
510 j->chrootdir = strdup(dir);
511 if (!j->chrootdir)
512 return -ENOMEM;
513 j->flags.chroot = 1;
514 return 0;
515}
516
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800517int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
518{
519 if (j->chrootdir)
520 return -EINVAL;
521 j->chrootdir = strdup(dir);
522 if (!j->chrootdir)
523 return -ENOMEM;
524 j->flags.pivot_root = 1;
525 return 0;
526}
527
Dylan Reida14e08d2015-10-22 21:05:29 -0700528char API *minijail_get_original_path(struct minijail *j,
529 const char *path_inside_chroot)
530{
Dylan Reid648b2202015-10-23 00:50:00 -0700531 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700532
Dylan Reid648b2202015-10-23 00:50:00 -0700533 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700534 while (b) {
535 /*
536 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700537 * mount, then the original path is exactly the source of
538 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700539 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700540 * mount source = /some/path/exe, mount dest =
541 * /chroot/path/exe Then when getting the original path of
542 * "/chroot/path/exe", the source of that mount,
543 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700544 */
545 if (!strcmp(b->dest, path_inside_chroot))
546 return strdup(b->src);
547
548 /*
549 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700550 * mount, take the suffix of the chroot path relative to the
551 * mount destination path, and append it to the mount source
552 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700553 */
554 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
555 const char *relative_path =
556 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400557 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700558 }
559 b = b->next;
560 }
561
562 /* If there is a chroot path, append |path_inside_chroot| to that. */
563 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400564 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700565
566 /* No chroot, so the path outside is the same as it is inside. */
567 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700568}
569
Lee Campbell11af0622014-05-22 12:36:04 -0700570void API minijail_mount_tmp(struct minijail *j)
571{
572 j->flags.mount_tmp = 1;
573}
574
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800575int API minijail_write_pid_file(struct minijail *j, const char *path)
576{
577 j->pid_file_path = strdup(path);
578 if (!j->pid_file_path)
579 return -ENOMEM;
580 j->flags.pid_file = 1;
581 return 0;
582}
583
Dylan Reid605ce7f2016-01-19 19:21:00 -0800584int API minijail_add_to_cgroup(struct minijail *j, const char *path)
585{
586 if (j->cgroup_count >= MAX_CGROUPS)
587 return -ENOMEM;
588 j->cgroups[j->cgroup_count] = strdup(path);
589 if (!j->cgroups[j->cgroup_count])
590 return -ENOMEM;
591 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800592 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800593 return 0;
594}
595
Dylan Reid81e23972016-05-18 14:06:35 -0700596int API minijail_mount_with_data(struct minijail *j, const char *src,
597 const char *dest, const char *type,
598 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700599{
Dylan Reid648b2202015-10-23 00:50:00 -0700600 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400601
602 if (*dest != '/')
603 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700604 m = calloc(1, sizeof(*m));
605 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400606 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700607 m->dest = strdup(dest);
608 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400609 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700610 m->src = strdup(src);
611 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400612 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700613 m->type = strdup(type);
614 if (!m->type)
615 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700616 if (data) {
617 m->data = strdup(data);
618 if (!m->data)
619 goto error;
620 m->has_data = 1;
621 }
Dylan Reid648b2202015-10-23 00:50:00 -0700622 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400623
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800624 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400625
Elly Jonesdd3e8512012-01-23 15:13:38 -0500626 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700627 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400628 * containing vfs namespace.
629 */
630 minijail_namespace_vfs(j);
631
Dylan Reid648b2202015-10-23 00:50:00 -0700632 if (j->mounts_tail)
633 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400634 else
Dylan Reid648b2202015-10-23 00:50:00 -0700635 j->mounts_head = m;
636 j->mounts_tail = m;
637 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400638
639 return 0;
640
641error:
Dylan Reid81e23972016-05-18 14:06:35 -0700642 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700643 free(m->src);
644 free(m->dest);
645 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400646 return -ENOMEM;
647}
648
Dylan Reid81e23972016-05-18 14:06:35 -0700649int API minijail_mount(struct minijail *j, const char *src, const char *dest,
650 const char *type, unsigned long flags)
651{
652 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
653}
654
Dylan Reid648b2202015-10-23 00:50:00 -0700655int API minijail_bind(struct minijail *j, const char *src, const char *dest,
656 int writeable)
657{
658 unsigned long flags = MS_BIND;
659
660 if (!writeable)
661 flags |= MS_RDONLY;
662
663 return minijail_mount(j, src, dest, "", flags);
664}
665
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400666static void clear_seccomp_options(struct minijail *j)
667{
668 j->flags.seccomp_filter = 0;
669 j->flags.seccomp_filter_tsync = 0;
670 j->flags.log_seccomp_filter = 0;
671 j->filter_len = 0;
672 j->filter_prog = NULL;
673 j->flags.no_new_privs = 0;
674}
675
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400676static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400677{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400678 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400679 /*
680 * |errno| will be set to EINVAL when seccomp has not been
681 * compiled into the kernel. On certain platforms and kernel
682 * versions this is not a fatal failure. In that case, and only
683 * in that case, disable seccomp and skip loading the filters.
684 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400685 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400686 warn("not loading seccomp filters, seccomp filter not "
687 "supported");
688 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400689 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700690 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400691 /*
692 * If |errno| != EINVAL or seccomp_can_softfail() is false,
693 * we can proceed. Worst case scenario minijail_enter() will
694 * abort() if seccomp fails.
695 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700696 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400697 if (j->flags.seccomp_filter_tsync) {
698 /* Are the seccomp(2) syscall and the TSYNC option supported? */
699 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
700 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
701 int saved_errno = errno;
702 if (seccomp_can_softfail()) {
703 if (saved_errno == ENOSYS) {
704 warn(
705 "seccomp(2) syscall not supported");
706 clear_seccomp_options(j);
707 }
708 if (saved_errno == EINVAL) {
709 warn("seccomp filter thread sync not "
710 "supported");
711 clear_seccomp_options(j);
712 }
713 return 0;
714 }
715 /*
716 * Similar logic here. If seccomp_can_softfail() is
717 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
718 * we can proceed. Worst case scenario minijail_enter()
719 * will abort() if seccomp or TSYNC fail.
720 */
721 }
722 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400723 return 1;
724}
725
726static int parse_seccomp_filters(struct minijail *j, FILE *policy_file)
727{
728 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
729 if (compile_filter(policy_file, fprog, j->flags.log_seccomp_filter)) {
730 free(fprog);
731 return -1;
732 }
733
734 j->filter_len = fprog->len;
735 j->filter_prog = fprog;
736 return 0;
737}
738
739void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
740{
741 if (!seccomp_should_parse_filters(j))
742 return;
743
Elly Jonese1749eb2011-10-07 13:54:59 -0400744 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800745 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700746 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400747 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800748
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400749 if (parse_seccomp_filters(j, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700750 die("failed to compile seccomp filter BPF program in '%s'",
751 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800752 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400753 fclose(file);
754}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800755
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400756void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
757{
758 if (!seccomp_should_parse_filters(j))
759 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800760
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400761 FILE *file = fdopen(fd, "r");
762 if (!file) {
763 pdie("failed to associate stream with fd %d", fd);
764 }
765
766 if (parse_seccomp_filters(j, file) != 0) {
767 die("failed to compile seccomp filter BPF program from fd %d",
768 fd);
769 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400770 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500771}
772
Andrew Brestickereac28942015-11-11 16:04:46 -0800773int API minijail_use_alt_syscall(struct minijail *j, const char *table)
774{
775 j->alt_syscall_table = strdup(table);
776 if (!j->alt_syscall_table)
777 return -ENOMEM;
778 j->flags.alt_syscall = 1;
779 return 0;
780}
781
Will Drewryf89aef52011-09-16 16:48:57 -0500782struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400783 size_t available;
784 size_t total;
785 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500786};
787
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800788void marshal_state_init(struct marshal_state *state, char *buf,
789 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400790{
791 state->available = available;
792 state->buf = buf;
793 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500794}
795
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800796void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400797{
798 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500799
Elly Jonese1749eb2011-10-07 13:54:59 -0400800 /* Up to |available| will be written. */
801 if (copy_len) {
802 memcpy(state->buf, src, copy_len);
803 state->buf += copy_len;
804 state->available -= copy_len;
805 }
806 /* |total| will contain the expected length. */
807 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500808}
809
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400810void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700811{
812 marshal_append(state, m->src, strlen(m->src) + 1);
813 marshal_append(state, m->dest, strlen(m->dest) + 1);
814 marshal_append(state, m->type, strlen(m->type) + 1);
815 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
816 if (m->has_data)
817 marshal_append(state, m->data, strlen(m->data) + 1);
818 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
819}
820
Will Drewry6ac91122011-10-21 16:38:58 -0500821void minijail_marshal_helper(struct marshal_state *state,
822 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400823{
Dylan Reid648b2202015-10-23 00:50:00 -0700824 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800825 size_t i;
826
Elly Jonese1749eb2011-10-07 13:54:59 -0400827 marshal_append(state, (char *)j, sizeof(*j));
828 if (j->user)
829 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800830 if (j->suppl_gid_list) {
831 marshal_append(state, j->suppl_gid_list,
832 j->suppl_gid_count * sizeof(gid_t));
833 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400834 if (j->chrootdir)
835 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800836 if (j->alt_syscall_table) {
837 marshal_append(state, j->alt_syscall_table,
838 strlen(j->alt_syscall_table) + 1);
839 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800840 if (j->flags.seccomp_filter && j->filter_prog) {
841 struct sock_fprog *fp = j->filter_prog;
842 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800843 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400844 }
Dylan Reid648b2202015-10-23 00:50:00 -0700845 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400846 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400847 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800848 for (i = 0; i < j->cgroup_count; ++i)
849 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500850}
851
Will Drewry6ac91122011-10-21 16:38:58 -0500852size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400853{
854 struct marshal_state state;
855 marshal_state_init(&state, NULL, 0);
856 minijail_marshal_helper(&state, j);
857 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500858}
859
Elly Jonese1749eb2011-10-07 13:54:59 -0400860int minijail_marshal(const struct minijail *j, char *buf, size_t available)
861{
862 struct marshal_state state;
863 marshal_state_init(&state, buf, available);
864 minijail_marshal_helper(&state, j);
865 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500866}
867
Elly Jonese1749eb2011-10-07 13:54:59 -0400868int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
869{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800870 size_t i;
871 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500872 int ret = -EINVAL;
873
Elly Jonese1749eb2011-10-07 13:54:59 -0400874 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500875 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400876 memcpy((void *)j, serialized, sizeof(*j));
877 serialized += sizeof(*j);
878 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500879
Will Drewrybee7ba72011-10-21 20:47:01 -0500880 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -0400881 j->pid_file_path = NULL;
882 j->uidmap = NULL;
883 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -0700884 j->mounts_head = NULL;
885 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800886 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500887
Elly Jonese1749eb2011-10-07 13:54:59 -0400888 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400889 char *user = consumestr(&serialized, &length);
890 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500891 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400892 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500893 if (!j->user)
894 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400895 }
Will Drewryf89aef52011-09-16 16:48:57 -0500896
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800897 if (j->suppl_gid_list) { /* stale pointer */
898 if (j->suppl_gid_count > NGROUPS_MAX) {
899 goto bad_gid_list;
900 }
901 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
902 void *gid_list_bytes =
903 consumebytes(gid_list_size, &serialized, &length);
904 if (!gid_list_bytes)
905 goto bad_gid_list;
906
907 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
908 if (!j->suppl_gid_list)
909 goto bad_gid_list;
910
911 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
912 }
913
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400914 if (j->chrootdir) { /* stale pointer */
915 char *chrootdir = consumestr(&serialized, &length);
916 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500917 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400918 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500919 if (!j->chrootdir)
920 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400921 }
922
Andrew Brestickereac28942015-11-11 16:04:46 -0800923 if (j->alt_syscall_table) { /* stale pointer */
924 char *alt_syscall_table = consumestr(&serialized, &length);
925 if (!alt_syscall_table)
926 goto bad_syscall_table;
927 j->alt_syscall_table = strdup(alt_syscall_table);
928 if (!j->alt_syscall_table)
929 goto bad_syscall_table;
930 }
931
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800932 if (j->flags.seccomp_filter && j->filter_len > 0) {
933 size_t ninstrs = j->filter_len;
934 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
935 ninstrs > USHRT_MAX)
936 goto bad_filters;
937
938 size_t program_len = ninstrs * sizeof(struct sock_filter);
939 void *program = consumebytes(program_len, &serialized, &length);
940 if (!program)
941 goto bad_filters;
942
943 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800944 if (!j->filter_prog)
945 goto bad_filters;
946
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800947 j->filter_prog->len = ninstrs;
948 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800949 if (!j->filter_prog->filter)
950 goto bad_filter_prog_instrs;
951
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800952 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400953 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400954
Dylan Reid648b2202015-10-23 00:50:00 -0700955 count = j->mounts_count;
956 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400957 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700958 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -0700959 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400960 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700961 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700962 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400963 const char *src = consumestr(&serialized, &length);
964 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700965 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400966 dest = consumestr(&serialized, &length);
967 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700968 goto bad_mounts;
969 type = consumestr(&serialized, &length);
970 if (!type)
971 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -0700972 has_data = consumebytes(sizeof(*has_data), &serialized,
973 &length);
974 if (!has_data)
975 goto bad_mounts;
976 if (*has_data) {
977 data = consumestr(&serialized, &length);
978 if (!data)
979 goto bad_mounts;
980 }
Dylan Reid648b2202015-10-23 00:50:00 -0700981 flags = consumebytes(sizeof(*flags), &serialized, &length);
982 if (!flags)
983 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -0700984 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -0700985 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400986 }
987
Dylan Reid605ce7f2016-01-19 19:21:00 -0800988 count = j->cgroup_count;
989 j->cgroup_count = 0;
990 for (i = 0; i < count; ++i) {
991 char *cgroup = consumestr(&serialized, &length);
992 if (!cgroup)
993 goto bad_cgroups;
994 j->cgroups[i] = strdup(cgroup);
995 if (!j->cgroups[i])
996 goto bad_cgroups;
997 ++j->cgroup_count;
998 }
999
Elly Jonese1749eb2011-10-07 13:54:59 -04001000 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001001
Dylan Reid605ce7f2016-01-19 19:21:00 -08001002bad_cgroups:
1003 while (j->mounts_head) {
1004 struct mountpoint *m = j->mounts_head;
1005 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07001006 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001007 free(m->type);
1008 free(m->dest);
1009 free(m->src);
1010 free(m);
1011 }
1012 for (i = 0; i < j->cgroup_count; ++i)
1013 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001014bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001015 if (j->flags.seccomp_filter && j->filter_len > 0) {
1016 free(j->filter_prog->filter);
1017 free(j->filter_prog);
1018 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001019bad_filter_prog_instrs:
1020 if (j->filter_prog)
1021 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001022bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001023 if (j->alt_syscall_table)
1024 free(j->alt_syscall_table);
1025bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001026 if (j->chrootdir)
1027 free(j->chrootdir);
1028bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001029 if (j->suppl_gid_list)
1030 free(j->suppl_gid_list);
1031bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001032 if (j->user)
1033 free(j->user);
1034clear_pointers:
1035 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001036 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001037 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001038 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001039 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001040out:
1041 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001042}
1043
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001044/*
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001045 * setup_mount_destination: Ensures the mount target exists.
1046 * Creates it if needed and possible.
Dylan Reideec77962016-06-30 19:35:10 -07001047 */
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001048static int setup_mount_destination(const char *source, const char *dest,
1049 uid_t uid, uid_t gid)
Dylan Reideec77962016-06-30 19:35:10 -07001050{
1051 int rc;
1052 struct stat st_buf;
1053
1054 rc = stat(dest, &st_buf);
1055 if (rc == 0) /* destination exists */
1056 return 0;
1057
1058 /*
1059 * Try to create the destination.
1060 * Either make a directory or touch a file depending on the source type.
1061 * If the source doesn't exist, assume it is a filesystem type such as
1062 * "tmpfs" and create a directory to mount it on.
1063 */
1064 rc = stat(source, &st_buf);
1065 if (rc || S_ISDIR(st_buf.st_mode) || S_ISBLK(st_buf.st_mode)) {
1066 if (mkdir(dest, 0700))
1067 return -errno;
1068 } else {
1069 int fd = open(dest, O_RDWR | O_CREAT, 0700);
1070 if (fd < 0)
1071 return -errno;
1072 close(fd);
1073 }
1074 return chown(dest, uid, gid);
1075}
1076
1077/*
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001078 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001079 * @j Minijail these mounts are for
1080 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001081 *
1082 * Returns 0 for success.
1083 */
Dylan Reid648b2202015-10-23 00:50:00 -07001084static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001085{
Dylan Reid648b2202015-10-23 00:50:00 -07001086 int ret;
1087 char *dest;
1088 int remount_ro = 0;
1089
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001090 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001091 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001092 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001093
Dylan Reideec77962016-06-30 19:35:10 -07001094 if (setup_mount_destination(m->src, dest, j->uid, j->gid))
1095 pdie("creating mount target '%s' failed", dest);
1096
Dylan Reid648b2202015-10-23 00:50:00 -07001097 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001098 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1099 * can't both be specified in the original bind mount.
1100 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001101 */
1102 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1103 remount_ro = 1;
1104 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001105 }
Dylan Reid648b2202015-10-23 00:50:00 -07001106
Dylan Reid81e23972016-05-18 14:06:35 -07001107 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001108 if (ret)
1109 pdie("mount: %s -> %s", m->src, dest);
1110
1111 if (remount_ro) {
1112 m->flags |= MS_RDONLY;
1113 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001114 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001115 if (ret)
1116 pdie("bind ro: %s -> %s", m->src, dest);
1117 }
1118
Elly Jones51a5b6c2011-10-12 19:09:26 -04001119 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001120 if (m->next)
1121 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001122 return ret;
1123}
1124
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001125static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001126{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001127 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001128
1129 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001130 return ret;
1131
1132 if (chroot(j->chrootdir))
1133 return -errno;
1134
1135 if (chdir("/"))
1136 return -errno;
1137
1138 return 0;
1139}
1140
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001141static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001142{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001143 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001144
1145 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001146 return ret;
1147
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001148 /*
1149 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001150 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001151 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001152 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001153 if (oldroot < 0)
1154 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001155 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001156 if (newroot < 0)
1157 pdie("failed to open %s for fchdir", j->chrootdir);
1158
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001159 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001160 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001161 * do a self bind mount.
1162 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001163 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1164 pdie("failed to bind mount '%s'", j->chrootdir);
1165 if (chdir(j->chrootdir))
1166 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001167 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001168 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001169
1170 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001171 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001172 * change to the old root and unmount it.
1173 */
1174 if (fchdir(oldroot))
1175 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001176
1177 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001178 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1179 * there could be a shared mount point under |oldroot|. In that case,
1180 * mounts under this shared mount point will be unmounted below, and
1181 * this unmounting will propagate to the original mount namespace
1182 * (because the mount point is shared). To prevent this unexpected
1183 * unmounting, remove these mounts from their peer groups by recursively
1184 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001185 */
1186 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001187 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001188 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001189 if (umount2(".", MNT_DETACH))
1190 pdie("umount(/)");
1191 /* Change back to the new root. */
1192 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001193 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001194 if (close(oldroot))
1195 return -errno;
1196 if (close(newroot))
1197 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001198 if (chroot("/"))
1199 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001200 /* Set correct CWD for getcwd(3). */
1201 if (chdir("/"))
1202 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001203
1204 return 0;
1205}
1206
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001207static int mount_tmp(void)
Lee Campbell11af0622014-05-22 12:36:04 -07001208{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001209 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001210}
1211
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001212static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001213{
1214 const char *kProcPath = "/proc";
1215 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001216 /*
1217 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001218 * /proc in our namespace, which means using MS_REMOUNT here would
1219 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001220 * namespace (!). Instead, remove their mount from our namespace lazily
1221 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001222 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001223 if (umount2(kProcPath, MNT_DETACH)) {
1224 /*
1225 * If we are in a new user namespace, umount(2) will fail.
1226 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1227 */
1228 if (j->flags.userns) {
1229 info("umount(/proc, MNT_DETACH) failed, "
1230 "this is expected when using user namespaces");
1231 } else {
1232 return -errno;
1233 }
1234 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001235 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1236 return -errno;
1237 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001238}
1239
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001240static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001241{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001242 kill(j->initpid, SIGKILL);
1243 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001244}
1245
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001246static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001247{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001248 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001249 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001250}
1251
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001252static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001253{
1254 size_t i;
1255
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001256 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001257 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001258 kill_child_and_die(j, "failed to add to cgroups");
1259 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001260}
1261
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001262static void write_ugid_maps_or_die(const struct minijail *j)
1263{
1264 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1265 kill_child_and_die(j, "failed to write uid_map");
1266 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1267 kill_child_and_die(j, "failed to write gid_map");
1268}
1269
1270static void enter_user_namespace(const struct minijail *j)
1271{
1272 if (j->uidmap && setresuid(0, 0, 0))
1273 pdie("user_namespaces: setresuid(0, 0, 0) failed");
1274 if (j->gidmap && setresgid(0, 0, 0))
1275 pdie("user_namespaces: setresgid(0, 0, 0) failed");
1276}
1277
1278static void parent_setup_complete(int *pipe_fds)
1279{
1280 close(pipe_fds[0]);
1281 close(pipe_fds[1]);
1282}
1283
1284/*
1285 * wait_for_parent_setup: Called by the child process to wait for any
1286 * further parent-side setup to complete before continuing.
1287 */
1288static void wait_for_parent_setup(int *pipe_fds)
1289{
1290 char buf;
1291
1292 close(pipe_fds[1]);
1293
1294 /* Wait for parent to complete setup and close the pipe. */
1295 if (read(pipe_fds[0], &buf, 1) != 0)
1296 die("failed to sync with parent");
1297 close(pipe_fds[0]);
1298}
1299
1300static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001301{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001302 if (j->flags.usergroups && j->flags.suppl_gids) {
1303 die("tried to inherit *and* set supplementary groups;"
1304 " can only do one");
1305 }
1306
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001307 if (j->flags.usergroups) {
1308 if (initgroups(j->user, j->usergid))
1309 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001310 } else if (j->flags.suppl_gids) {
1311 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1312 pdie("setgroups");
1313 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001314 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001315 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001316 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001317 * users.
1318 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001319 if ((j->uid || j->gid) && setgroups(0, NULL))
1320 pdie("setgroups");
1321 }
1322
1323 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1324 pdie("setresgid");
1325
1326 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1327 pdie("setresuid");
1328}
1329
Mike Frysinger3adfef72013-05-09 17:19:08 -04001330/*
1331 * We specifically do not use cap_valid() as that only tells us the last
1332 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001333 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001334 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001335 * kernel).
1336 * Normally, we suck up the answer via /proc. On Android, not all processes are
1337 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1338 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001339 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001340static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001341{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001342 unsigned int last_valid_cap = 0;
1343 if (is_android()) {
1344 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1345 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001346
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001347 /* |last_valid_cap| will be the first failing value. */
1348 if (last_valid_cap > 0) {
1349 last_valid_cap--;
1350 }
1351 } else {
1352 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1353 FILE *fp = fopen(cap_file, "re");
1354 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1355 pdie("fscanf(%s)", cap_file);
1356 fclose(fp);
1357 }
Dylan Reidf682d472015-09-17 21:39:07 -07001358 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001359}
1360
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001361static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1362{
1363 const uint64_t one = 1;
1364 unsigned int i;
1365 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1366 if (keep_mask & (one << i))
1367 continue;
1368 if (prctl(PR_CAPBSET_DROP, i))
1369 pdie("could not drop capability from bounding set");
1370 }
1371}
1372
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001373static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001374{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001375 if (!j->flags.use_caps)
1376 return;
1377
Elly Jonese1749eb2011-10-07 13:54:59 -04001378 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001379 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001380 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001381 unsigned int i;
1382 if (!caps)
1383 die("can't get process caps");
1384 if (cap_clear_flag(caps, CAP_INHERITABLE))
1385 die("can't clear inheritable caps");
1386 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1387 die("can't clear effective caps");
1388 if (cap_clear_flag(caps, CAP_PERMITTED))
1389 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001390 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001391 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001392 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001393 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001394 flag[0] = i;
1395 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001396 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001397 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001398 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001399 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001400 die("can't add inheritable cap");
1401 }
1402 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001403 die("can't apply initial cleaned capset");
1404
1405 /*
1406 * Instead of dropping bounding set first, do it here in case
1407 * the caller had a more permissive bounding set which could
1408 * have been used above to raise a capability that wasn't already
1409 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1410 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001411 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001412
1413 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001414 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001415 flag[0] = CAP_SETPCAP;
1416 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1417 die("can't clear effective cap");
1418 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1419 die("can't clear permitted cap");
1420 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1421 die("can't clear inheritable cap");
1422 }
1423
1424 if (cap_set_proc(caps))
1425 die("can't apply final cleaned capset");
1426
1427 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001428}
1429
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001430static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001431{
1432 /*
1433 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1434 * in the kernel source tree for an explanation of the parameters.
1435 */
1436 if (j->flags.no_new_privs) {
1437 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1438 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1439 }
1440
1441 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001442 * Code running with ASan
1443 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1444 * will make system calls not included in the syscall filter policy,
1445 * which will likely crash the program. Skip setting seccomp filter in
1446 * that case.
1447 * 'running_with_asan()' has no inputs and is completely defined at
1448 * build time, so this cannot be used by an attacker to skip setting
1449 * seccomp filter.
1450 */
1451 if (j->flags.seccomp_filter && running_with_asan()) {
1452 warn("running with ASan, not setting seccomp filter");
1453 return;
1454 }
1455
1456 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001457 * If we're logging seccomp filter failures,
1458 * install the SIGSYS handler first.
1459 */
1460 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1461 if (install_sigsys_handler())
1462 pdie("install SIGSYS handler");
1463 warn("logging seccomp filter failures");
1464 }
1465
1466 /*
1467 * Install the syscall filter.
1468 */
1469 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001470 if (j->flags.seccomp_filter_tsync) {
1471 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1472 SECCOMP_FILTER_FLAG_TSYNC,
1473 j->filter_prog)) {
1474 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001475 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001476 } else {
1477 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1478 j->filter_prog)) {
1479 pdie("prctl(seccomp_filter) failed");
1480 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001481 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001482 }
1483}
1484
Will Drewry6ac91122011-10-21 16:38:58 -05001485void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001486{
Dylan Reidf682d472015-09-17 21:39:07 -07001487 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001488 * If we're dropping caps, get the last valid cap from /proc now,
1489 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001490 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001491 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001492 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001493 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001494
Elly Jonese1749eb2011-10-07 13:54:59 -04001495 if (j->flags.pids)
1496 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001497 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001498
Elly Jonese1749eb2011-10-07 13:54:59 -04001499 if (j->flags.usergroups && !j->user)
1500 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001501
Elly Jonesdd3e8512012-01-23 15:13:38 -05001502 /*
1503 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001504 * so we don't even try. If any of our operations fail, we abort() the
1505 * entire process.
1506 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001507 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1508 pdie("setns(CLONE_NEWNS)");
1509
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001510 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001511 if (unshare(CLONE_NEWNS))
1512 pdie("unshare(vfs)");
1513 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001514 * Unless asked not to, remount all filesystems as private.
1515 * If they are shared, new bind mounts will creep out of our
1516 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001517 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1518 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001519 if (!j->flags.skip_remount_private) {
1520 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1521 pdie("mount(/, private)");
1522 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001523 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001524
Dylan Reidf7942472015-11-18 17:55:26 -08001525 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1526 pdie("unshare(ipc)");
1527 }
1528
Dylan Reid1102f5a2015-09-15 11:52:20 -07001529 if (j->flags.enter_net) {
1530 if (setns(j->netns_fd, CLONE_NEWNET))
1531 pdie("setns(CLONE_NEWNET)");
1532 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001533 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001534 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001535
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001536 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
1537 pdie("unshare(cgroups)");
1538
Elly Jones51a5b6c2011-10-12 19:09:26 -04001539 if (j->flags.chroot && enter_chroot(j))
1540 pdie("chroot");
1541
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001542 if (j->flags.pivot_root && enter_pivot_root(j))
1543 pdie("pivot_root");
1544
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001545 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001546 pdie("mount_tmp");
1547
Dylan Reid791f5772015-09-14 20:02:42 -07001548 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001549 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001550
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001551 /*
1552 * If we're only dropping capabilities from the bounding set, but not
1553 * from the thread's (permitted|inheritable|effective) sets, do it now.
1554 */
1555 if (j->flags.capbset_drop) {
1556 drop_capbset(j->cap_bset, last_valid_cap);
1557 }
1558
1559 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001560 /*
1561 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001562 * capability to change uids, our attempt to use setuid()
1563 * below will fail. Hang on to root caps across setuid(), then
1564 * lock securebits.
1565 */
1566 if (prctl(PR_SET_KEEPCAPS, 1))
1567 pdie("prctl(PR_SET_KEEPCAPS)");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001568
1569 /*
1570 * Kernels 4.3+ define a new securebit
1571 * (SECURE_NO_CAP_AMBIENT_RAISE), so using the SECURE_ALL_BITS
1572 * and SECURE_ALL_LOCKS masks from newer kernel headers will
1573 * return EPERM on older kernels. Detect this, and retry with
1574 * the right mask for older (2.6.26-4.2) kernels.
1575 */
1576 int securebits_ret = prctl(PR_SET_SECUREBITS,
1577 SECURE_ALL_BITS | SECURE_ALL_LOCKS);
1578 if (securebits_ret < 0) {
1579 if (errno == EPERM) {
1580 /* Possibly running on kernel < 4.3. */
1581 securebits_ret = prctl(
1582 PR_SET_SECUREBITS,
1583 OLD_SECURE_ALL_BITS | OLD_SECURE_ALL_LOCKS);
1584 }
1585 }
1586 if (securebits_ret < 0)
Elly Jonese1749eb2011-10-07 13:54:59 -04001587 pdie("prctl(PR_SET_SECUREBITS)");
1588 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001589
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001590 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001591 /*
1592 * If we're setting no_new_privs, we can drop privileges
1593 * before setting seccomp filter. This way filter policies
1594 * don't need to allow privilege-dropping syscalls.
1595 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001596 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001597 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001598 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001599 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001600 /*
1601 * If we're not setting no_new_privs,
1602 * we need to set seccomp filter *before* dropping privileges.
1603 * WARNING: this means that filter policies *must* allow
1604 * setgroups()/setresgid()/setresuid() for dropping root and
1605 * capget()/capset()/prctl() for dropping caps.
1606 */
1607 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001608 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001609 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001610 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001611
Elly Jonesdd3e8512012-01-23 15:13:38 -05001612 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001613 * Select the specified alternate syscall table. The table must not
1614 * block prctl(2) if we're using seccomp as well.
1615 */
1616 if (j->flags.alt_syscall) {
1617 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1618 pdie("prctl(PR_ALT_SYSCALL)");
1619 }
1620
1621 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001622 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001623 * privilege-dropping syscalls :)
1624 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001625 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001626 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001627 warn("seccomp not supported");
1628 return;
1629 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001630 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001631 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001632}
1633
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001634/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001635static int init_exitstatus = 0;
1636
Will Drewry6ac91122011-10-21 16:38:58 -05001637void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001638{
1639 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001640}
1641
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001642void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001643{
1644 pid_t pid;
1645 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001646 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001647 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001648 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001649 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001650 /*
1651 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001652 * left inside our pid namespace or we get a signal.
1653 */
1654 if (pid == rootpid)
1655 init_exitstatus = status;
1656 }
1657 if (!WIFEXITED(init_exitstatus))
1658 _exit(MINIJAIL_ERR_INIT);
1659 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001660}
1661
Will Drewry6ac91122011-10-21 16:38:58 -05001662int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001663{
1664 size_t sz = 0;
1665 size_t bytes = read(fd, &sz, sizeof(sz));
1666 char *buf;
1667 int r;
1668 if (sizeof(sz) != bytes)
1669 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001670 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001671 return -E2BIG;
1672 buf = malloc(sz);
1673 if (!buf)
1674 return -ENOMEM;
1675 bytes = read(fd, buf, sz);
1676 if (bytes != sz) {
1677 free(buf);
1678 return -EINVAL;
1679 }
1680 r = minijail_unmarshal(j, buf, sz);
1681 free(buf);
1682 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001683}
1684
Will Drewry6ac91122011-10-21 16:38:58 -05001685int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001686{
1687 char *buf;
1688 size_t sz = minijail_size(j);
1689 ssize_t written;
1690 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001691
Elly Jonese1749eb2011-10-07 13:54:59 -04001692 if (!sz)
1693 return -EINVAL;
1694 buf = malloc(sz);
1695 r = minijail_marshal(j, buf, sz);
1696 if (r) {
1697 free(buf);
1698 return r;
1699 }
1700 /* Sends [size][minijail]. */
1701 written = write(fd, &sz, sizeof(sz));
1702 if (written != sizeof(sz)) {
1703 free(buf);
1704 return -EFAULT;
1705 }
1706 written = write(fd, buf, sz);
1707 if (written < 0 || (size_t) written != sz) {
1708 free(buf);
1709 return -EFAULT;
1710 }
1711 free(buf);
1712 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001713}
Elly Jonescd7a9042011-07-22 13:56:51 -04001714
Will Drewry6ac91122011-10-21 16:38:58 -05001715int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001716{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001717#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001718 /* Don't use LDPRELOAD on Brillo. */
1719 return 0;
1720#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001721 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1722 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1723 if (!newenv)
1724 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001725
Elly Jonese1749eb2011-10-07 13:54:59 -04001726 /* Only insert a separating space if we have something to separate... */
1727 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1728 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001729
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001730 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001731 setenv(kLdPreloadEnvVar, newenv, 1);
1732 free(newenv);
1733 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001734#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001735}
1736
Will Drewry6ac91122011-10-21 16:38:58 -05001737int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001738{
1739 int r = pipe(fds);
1740 char fd_buf[11];
1741 if (r)
1742 return r;
1743 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1744 if (r <= 0)
1745 return -EINVAL;
1746 setenv(kFdEnvVar, fd_buf, 1);
1747 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001748}
1749
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001750int setup_pipe_end(int fds[2], size_t index)
1751{
1752 if (index > 1)
1753 return -1;
1754
1755 close(fds[1 - index]);
1756 return fds[index];
1757}
1758
1759int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1760{
1761 if (index > 1)
1762 return -1;
1763
1764 close(fds[1 - index]);
1765 /* dup2(2) the corresponding end of the pipe into |fd|. */
1766 return dup2(fds[index], fd);
1767}
1768
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001769int minijail_run_internal(struct minijail *j, const char *filename,
1770 char *const argv[], pid_t *pchild_pid,
1771 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1772 int use_preload);
1773
Will Drewry6ac91122011-10-21 16:38:58 -05001774int API minijail_run(struct minijail *j, const char *filename,
1775 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001776{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001777 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1778 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001779}
1780
1781int API minijail_run_pid(struct minijail *j, const char *filename,
1782 char *const argv[], pid_t *pchild_pid)
1783{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001784 return minijail_run_internal(j, filename, argv, pchild_pid,
1785 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001786}
1787
1788int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001789 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001790{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001791 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1792 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001793}
1794
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001795int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001796 char *const argv[], pid_t *pchild_pid,
1797 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001798{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001799 return minijail_run_internal(j, filename, argv, pchild_pid,
1800 pstdin_fd, pstdout_fd, pstderr_fd, true);
1801}
1802
1803int API minijail_run_no_preload(struct minijail *j, const char *filename,
1804 char *const argv[])
1805{
1806 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1807 false);
1808}
1809
Samuel Tan63187f42015-10-16 13:01:53 -07001810int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001811 const char *filename,
1812 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001813 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001814 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001815 int *pstderr_fd)
1816{
Samuel Tan63187f42015-10-16 13:01:53 -07001817 return minijail_run_internal(j, filename, argv, pchild_pid,
1818 pstdin_fd, pstdout_fd, pstderr_fd, false);
1819}
1820
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001821int minijail_run_internal(struct minijail *j, const char *filename,
1822 char *const argv[], pid_t *pchild_pid,
1823 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1824 int use_preload)
1825{
Elly Jonese1749eb2011-10-07 13:54:59 -04001826 char *oldenv, *oldenv_copy = NULL;
1827 pid_t child_pid;
1828 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001829 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001830 int stdout_fds[2];
1831 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001832 int child_sync_pipe_fds[2];
1833 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001834 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001835 /* We need to remember this across the minijail_preexec() call. */
1836 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001837 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001838
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001839 if (use_preload) {
1840 oldenv = getenv(kLdPreloadEnvVar);
1841 if (oldenv) {
1842 oldenv_copy = strdup(oldenv);
1843 if (!oldenv_copy)
1844 return -ENOMEM;
1845 }
1846
1847 if (setup_preload())
1848 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001849 }
Will Drewryf89aef52011-09-16 16:48:57 -05001850
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001851 if (!use_preload) {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04001852 if (j->flags.use_caps && j->caps != 0)
1853 die("non-empty capabilities are not supported without LD_PRELOAD");
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001854 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001855
Elly Jonesdd3e8512012-01-23 15:13:38 -05001856 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001857 * Make the process group ID of this process equal to its PID, so that
1858 * both the Minijail process and the jailed process can be killed
1859 * together.
1860 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1861 * the process is already a process group leader.
1862 */
1863 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1864 if (errno != EPERM) {
1865 pdie("setpgid(0, 0)");
1866 }
1867 }
1868
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001869 if (use_preload) {
1870 /*
1871 * Before we fork(2) and execve(2) the child process, we need
1872 * to open a pipe(2) to send the minijail configuration over.
1873 */
1874 if (setup_pipe(pipe_fds))
1875 return -EFAULT;
1876 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001877
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001878 /*
1879 * If we want to write to the child process' standard input,
1880 * create the pipe(2) now.
1881 */
1882 if (pstdin_fd) {
1883 if (pipe(stdin_fds))
1884 return -EFAULT;
1885 }
1886
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001887 /*
1888 * If we want to read from the child process' standard output,
1889 * create the pipe(2) now.
1890 */
1891 if (pstdout_fd) {
1892 if (pipe(stdout_fds))
1893 return -EFAULT;
1894 }
1895
1896 /*
1897 * If we want to read from the child process' standard error,
1898 * create the pipe(2) now.
1899 */
1900 if (pstderr_fd) {
1901 if (pipe(stderr_fds))
1902 return -EFAULT;
1903 }
1904
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001905 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04001906 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001907 * or if we need to add the child process to cgroups, create the pipe(2)
1908 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001909 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001910 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001911 sync_child = 1;
1912 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001913 return -EFAULT;
1914 }
1915
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001916 /*
1917 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001918 *
1919 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1920 *
1921 * In multithreaded programs, there are a bunch of locks inside libc,
1922 * some of which may be held by other threads at the time that we call
1923 * minijail_run_pid(). If we call fork(), glibc does its level best to
1924 * ensure that we hold all of these locks before it calls clone()
1925 * internally and drop them after clone() returns, but when we call
1926 * sys_clone(2) directly, all that gets bypassed and we end up with a
1927 * child address space where some of libc's important locks are held by
1928 * other threads (which did not get cloned, and hence will never release
1929 * those locks). This is okay so long as we call exec() immediately
1930 * after, but a bunch of seemingly-innocent libc functions like setenv()
1931 * take locks.
1932 *
1933 * Hence, only call sys_clone() if we need to, in order to get at pid
1934 * namespacing. If we follow this path, the child's address space might
1935 * have broken locks; you may only call functions that do not acquire
1936 * any locks.
1937 *
1938 * Unfortunately, fork() acquires every lock it can get its hands on, as
1939 * previously detailed, so this function is highly likely to deadlock
1940 * later on (see "deadlock here") if we're multithreaded.
1941 *
1942 * We might hack around this by having the clone()d child (init of the
1943 * pid namespace) return directly, rather than leaving the clone()d
1944 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001945 * its fork()ed child return in turn), but that process would be
1946 * crippled with its libc locks potentially broken. We might try
1947 * fork()ing in the parent before we clone() to ensure that we own all
1948 * the locks, but then we have to have the forked child hanging around
1949 * consuming resources (and possibly having file descriptors / shared
1950 * memory regions / etc attached). We'd need to keep the child around to
1951 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04001952 *
1953 * TODO(ellyjones): figure out if the "forked child hanging around"
1954 * problem is fixable or not. It would be nice if we worked in this
1955 * case.
1956 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001957 if (pid_namespace) {
1958 int clone_flags = CLONE_NEWPID | SIGCHLD;
1959 if (j->flags.userns)
1960 clone_flags |= CLONE_NEWUSER;
1961 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001962 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001963 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001964 }
Elly Jones761b7412012-06-13 15:49:52 -04001965
Elly Jonese1749eb2011-10-07 13:54:59 -04001966 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001967 if (use_preload) {
1968 free(oldenv_copy);
1969 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001970 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001971 }
Will Drewryf89aef52011-09-16 16:48:57 -05001972
Elly Jonese1749eb2011-10-07 13:54:59 -04001973 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001974 if (use_preload) {
1975 /* Restore parent's LD_PRELOAD. */
1976 if (oldenv_copy) {
1977 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1978 free(oldenv_copy);
1979 } else {
1980 unsetenv(kLdPreloadEnvVar);
1981 }
1982 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001983 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001984
Elly Jonese1749eb2011-10-07 13:54:59 -04001985 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001986
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001987 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001988 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001989
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001990 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001991 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001992
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001993 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001994 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08001995
1996 if (sync_child)
1997 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001998
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001999 if (use_preload) {
2000 /* Send marshalled minijail. */
2001 close(pipe_fds[0]); /* read endpoint */
2002 ret = minijail_to_fd(j, pipe_fds[1]);
2003 close(pipe_fds[1]); /* write endpoint */
2004 if (ret) {
2005 kill(j->initpid, SIGKILL);
2006 die("failed to send marshalled minijail");
2007 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002008 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002009
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002010 if (pchild_pid)
2011 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002012
2013 /*
2014 * If we want to write to the child process' standard input,
2015 * set up the write end of the pipe.
2016 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002017 if (pstdin_fd)
2018 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002019 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002020
2021 /*
2022 * If we want to read from the child process' standard output,
2023 * set up the read end of the pipe.
2024 */
2025 if (pstdout_fd)
2026 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002027 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002028
2029 /*
2030 * If we want to read from the child process' standard error,
2031 * set up the read end of the pipe.
2032 */
2033 if (pstderr_fd)
2034 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002035 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002036
Elly Jonese1749eb2011-10-07 13:54:59 -04002037 return 0;
2038 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002039 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002040 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002041
Peter Qiu2860c462015-12-16 15:13:06 -08002042 if (j->flags.reset_signal_mask) {
2043 sigset_t signal_mask;
2044 if (sigemptyset(&signal_mask) != 0)
2045 pdie("sigemptyset failed");
2046 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2047 pdie("sigprocmask failed");
2048 }
2049
Dylan Reidce5b55e2016-01-13 11:04:16 -08002050 if (sync_child)
2051 wait_for_parent_setup(child_sync_pipe_fds);
2052
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002053 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002054 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002055
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002056 /*
2057 * If we want to write to the jailed process' standard input,
2058 * set up the read end of the pipe.
2059 */
2060 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002061 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2062 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002063 die("failed to set up stdin pipe");
2064 }
2065
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002066 /*
2067 * If we want to read from the jailed process' standard output,
2068 * set up the write end of the pipe.
2069 */
2070 if (pstdout_fd) {
2071 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2072 STDOUT_FILENO) < 0)
2073 die("failed to set up stdout pipe");
2074 }
2075
2076 /*
2077 * If we want to read from the jailed process' standard error,
2078 * set up the write end of the pipe.
2079 */
2080 if (pstderr_fd) {
2081 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2082 STDERR_FILENO) < 0)
2083 die("failed to set up stderr pipe");
2084 }
2085
Dylan Reid791f5772015-09-14 20:02:42 -07002086 /* If running an init program, let it decide when/how to mount /proc. */
2087 if (pid_namespace && !do_init)
2088 j->flags.remount_proc_ro = 0;
2089
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002090 if (use_preload) {
2091 /* Strip out flags that cannot be inherited across execve(2). */
2092 minijail_preexec(j);
2093 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002094 /*
2095 * If not using LD_PRELOAD, do all jailing before execve(2).
2096 * Note that PID namespaces can only be entered on fork(2),
2097 * so that flag is still cleared.
2098 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002099 j->flags.pids = 0;
2100 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002101 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002102 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002103
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002104 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002105 /*
2106 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002107 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002108 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002109 * a child to actually run the program. If |do_init == 0|, we
2110 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002111 *
2112 * If we're multithreaded, we'll probably deadlock here. See
2113 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002114 */
2115 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002116 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002117 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002118 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002119 /*
2120 * Best effort. Don't bother checking the return value.
2121 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002122 prctl(PR_SET_NAME, "minijail-init");
2123 init(child_pid); /* Never returns. */
2124 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002125 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002126
Elly Jonesdd3e8512012-01-23 15:13:38 -05002127 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002128 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002129 * calling process
2130 * -> execve()-ing process
2131 * If we are:
2132 * calling process
2133 * -> init()-ing process
2134 * -> execve()-ing process
2135 */
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002136 ret = execve(filename, argv, environ);
2137 if (ret == -1) {
2138 pwarn("execve(%s) failed", filename);
2139 }
2140 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002141}
2142
Will Drewry6ac91122011-10-21 16:38:58 -05002143int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002144{
2145 int st;
2146 if (kill(j->initpid, SIGTERM))
2147 return -errno;
2148 if (waitpid(j->initpid, &st, 0) < 0)
2149 return -errno;
2150 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002151}
2152
Will Drewry6ac91122011-10-21 16:38:58 -05002153int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002154{
2155 int st;
2156 if (waitpid(j->initpid, &st, 0) < 0)
2157 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002158
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002159 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002160 int error_status = st;
2161 if (WIFSIGNALED(st)) {
2162 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002163 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002164 j->initpid, signum);
2165 /*
2166 * We return MINIJAIL_ERR_JAIL if the process received
2167 * SIGSYS, which happens when a syscall is blocked by
2168 * seccomp filters.
2169 * If not, we do what bash(1) does:
2170 * $? = 128 + signum
2171 */
2172 if (signum == SIGSYS) {
2173 error_status = MINIJAIL_ERR_JAIL;
2174 } else {
2175 error_status = 128 + signum;
2176 }
2177 }
2178 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002179 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002180
2181 int exit_status = WEXITSTATUS(st);
2182 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002183 info("child process %d exited with status %d",
2184 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002185
2186 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002187}
2188
Will Drewry6ac91122011-10-21 16:38:58 -05002189void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002190{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002191 size_t i;
2192
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002193 if (j->flags.seccomp_filter && j->filter_prog) {
2194 free(j->filter_prog->filter);
2195 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002196 }
Dylan Reid648b2202015-10-23 00:50:00 -07002197 while (j->mounts_head) {
2198 struct mountpoint *m = j->mounts_head;
2199 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002200 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002201 free(m->type);
2202 free(m->dest);
2203 free(m->src);
2204 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002205 }
Dylan Reid648b2202015-10-23 00:50:00 -07002206 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002207 if (j->user)
2208 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002209 if (j->suppl_gid_list)
2210 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002211 if (j->chrootdir)
2212 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002213 if (j->pid_file_path)
2214 free(j->pid_file_path);
2215 if (j->uidmap)
2216 free(j->uidmap);
2217 if (j->gidmap)
2218 free(j->gidmap);
Andrew Brestickereac28942015-11-11 16:04:46 -08002219 if (j->alt_syscall_table)
2220 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002221 for (i = 0; i < j->cgroup_count; ++i)
2222 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002223 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002224}