blob: a44c5d28ed71d52846f6b091ce113a14ff63ecd0 [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>
Luis Hector Chavez43ff0802016-10-07 12:21:07 -070012#include <dirent.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040013#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070014#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040015#include <grp.h>
16#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050017#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040018#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040019#include <pwd.h>
20#include <sched.h>
21#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050022#include <stdarg.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070023#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080024#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <syscall.h>
29#include <sys/capability.h>
30#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050031#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040032#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070033#include <sys/stat.h>
34#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080035#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040036#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040037#include <unistd.h>
38
39#include "libminijail.h"
40#include "libminijail-private.h"
41
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070042#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080043#include "syscall_filter.h"
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040044#include "syscall_wrapper.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070045#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080046
Lei Zhangeee31552012-10-17 21:27:10 -070047#ifdef HAVE_SECUREBITS_H
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070048# include <linux/securebits.h>
Lei Zhangeee31552012-10-17 21:27:10 -070049#else
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070050# define SECURE_ALL_BITS 0x55
51# define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
52#endif
53/* For kernels < 4.3. */
54#define OLD_SECURE_ALL_BITS 0x15
55#define OLD_SECURE_ALL_LOCKS (OLD_SECURE_ALL_BITS << 1)
56
57/*
58 * Assert the value of SECURE_ALL_BITS at compile-time.
59 * Brillo devices are currently compiled against 4.4 kernel headers. Kernel 4.3
60 * added a new securebit.
61 * When a new securebit is added, the new SECURE_ALL_BITS mask will return EPERM
62 * when used on older kernels. The compile-time assert will catch this situation
63 * at compile time.
64 */
65#ifdef __BRILLO__
66_Static_assert(SECURE_ALL_BITS == 0x55, "SECURE_ALL_BITS == 0x55.");
Lei Zhangeee31552012-10-17 21:27:10 -070067#endif
68
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070069/* Until these are reliably available in linux/prctl.h. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080070#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070071# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080072#endif
73
Andrew Brestickereac28942015-11-11 16:04:46 -080074#ifndef PR_ALT_SYSCALL
75# define PR_ALT_SYSCALL 0x43724f53
76#endif
77
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040078/* Seccomp filter related flags. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080079#ifndef PR_SET_NO_NEW_PRIVS
80# define PR_SET_NO_NEW_PRIVS 38
81#endif
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040082
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080083#ifndef SECCOMP_MODE_FILTER
84# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050085#endif
86
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040087#ifndef SECCOMP_SET_MODE_STRICT
88# define SECCOMP_SET_MODE_STRICT 0
89#endif
90#ifndef SECCOMP_SET_MODE_FILTER
91# define SECCOMP_SET_MODE_FILTER 1
92#endif
93
94#ifndef SECCOMP_FILTER_FLAG_TSYNC
95# define SECCOMP_FILTER_FLAG_TSYNC 1
96#endif
97/* End seccomp filter related flags. */
98
Dylan Reid4cbc2a52016-06-17 19:06:07 -070099/* New cgroup namespace might not be in linux-headers yet. */
100#ifndef CLONE_NEWCGROUP
101# define CLONE_NEWCGROUP 0x02000000
102#endif
103
Dylan Reid605ce7f2016-01-19 19:21:00 -0800104#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
105
Dylan Reid648b2202015-10-23 00:50:00 -0700106struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400107 char *src;
108 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700109 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700110 char *data;
111 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -0700112 unsigned long flags;
113 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400114};
115
Will Drewryf89aef52011-09-16 16:48:57 -0500116struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700117 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700118 * WARNING: if you add a flag here you need to make sure it's
119 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700120 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400121 struct {
122 int uid:1;
123 int gid:1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800124 int usergroups:1;
125 int suppl_gids:1;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800126 int use_caps:1;
127 int capbset_drop:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400128 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700129 int enter_vfs:1;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800130 int skip_remount_private:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400131 int pids:1;
Dylan Reidf7942472015-11-18 17:55:26 -0800132 int ipc:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400133 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700134 int enter_net:1;
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700135 int ns_cgroups:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800136 int userns:1;
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400137 int disable_setgroups:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400138 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -0700139 int remount_proc_ro:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700140 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400141 int seccomp_filter:1;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400142 int seccomp_filter_tsync:1;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400143 int seccomp_filter_logging:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400144 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800145 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700146 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800147 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800148 int pid_file:1;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800149 int cgroups:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800150 int alt_syscall:1;
Peter Qiu2860c462015-12-16 15:13:06 -0800151 int reset_signal_mask:1;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700152 int close_open_fds:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400153 } flags;
154 uid_t uid;
155 gid_t gid;
156 gid_t usergid;
157 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800158 size_t suppl_gid_count;
159 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400160 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800161 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400162 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700163 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700164 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400165 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800166 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800167 char *uidmap;
168 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800169 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800170 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800171 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700172 struct mountpoint *mounts_head;
173 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800174 size_t mounts_count;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800175 char *cgroups[MAX_CGROUPS];
176 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500177};
178
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700179/*
180 * Strip out flags meant for the parent.
181 * We keep things that are not inherited across execve(2) (e.g. capabilities),
182 * or are easier to set after execve(2) (e.g. seccomp filters).
183 */
184void minijail_preenter(struct minijail *j)
185{
186 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700187 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900188 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700189 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700190 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800191 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800192 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800193 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700194}
195
196/*
197 * Strip out flags meant for the child.
198 * We keep things that are inherited across execve(2).
199 */
200void minijail_preexec(struct minijail *j)
201{
202 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700203 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800204 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700205 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800206 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700207 if (j->user)
208 free(j->user);
209 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800210 if (j->suppl_gid_list)
211 free(j->suppl_gid_list);
212 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700213 memset(&j->flags, 0, sizeof(j->flags));
214 /* Now restore anything we meant to keep. */
215 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700216 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800217 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700218 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800219 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700220 /* Note, |pids| will already have been used before this call. */
221}
222
223/* Minijail API. */
224
Will Drewry6ac91122011-10-21 16:38:58 -0500225struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400226{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400227 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400228}
229
Will Drewry6ac91122011-10-21 16:38:58 -0500230void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400231{
232 if (uid == 0)
233 die("useless change to uid 0");
234 j->uid = uid;
235 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400236}
237
Will Drewry6ac91122011-10-21 16:38:58 -0500238void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400239{
240 if (gid == 0)
241 die("useless change to gid 0");
242 j->gid = gid;
243 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400244}
245
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800246void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
247 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800248{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800249 size_t i;
250
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800251 if (j->flags.usergroups)
252 die("cannot inherit *and* set supplementary groups");
253
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800254 if (size == 0) {
255 /* Clear supplementary groups. */
256 j->suppl_gid_list = NULL;
257 j->suppl_gid_count = 0;
258 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800259 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800260 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800261
262 /* Copy the gid_t array. */
263 j->suppl_gid_list = calloc(size, sizeof(gid_t));
264 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800265 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800266 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800267 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800268 j->suppl_gid_list[i] = list[i];
269 }
270 j->suppl_gid_count = size;
271 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800272}
273
Will Drewry6ac91122011-10-21 16:38:58 -0500274int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400275{
276 char *buf = NULL;
277 struct passwd pw;
278 struct passwd *ppw = NULL;
279 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
280 if (sz == -1)
281 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400282
Elly Jonesdd3e8512012-01-23 15:13:38 -0500283 /*
284 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400285 * the maximum needed size of the buffer, so we don't have to search.
286 */
287 buf = malloc(sz);
288 if (!buf)
289 return -ENOMEM;
290 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500291 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800292 * We're safe to free the buffer here. The strings inside |pw| point
293 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800294 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
295 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500296 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400297 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700298 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400299 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700300 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400301 minijail_change_uid(j, ppw->pw_uid);
302 j->user = strdup(user);
303 if (!j->user)
304 return -ENOMEM;
305 j->usergid = ppw->pw_gid;
306 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400307}
308
Will Drewry6ac91122011-10-21 16:38:58 -0500309int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400310{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700311 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700312 struct group gr;
313 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400314 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
315 if (sz == -1)
316 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400317
Elly Jonesdd3e8512012-01-23 15:13:38 -0500318 /*
319 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400320 * the maximum needed size of the buffer, so we don't have to search.
321 */
322 buf = malloc(sz);
323 if (!buf)
324 return -ENOMEM;
325 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500326 /*
327 * We're safe to free the buffer here. The strings inside gr point
328 * inside buf, but we don't use any of them; this leaves the pointers
329 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
330 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400331 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700332 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400333 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700334 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400335 minijail_change_gid(j, pgr->gr_gid);
336 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400337}
338
Will Drewry6ac91122011-10-21 16:38:58 -0500339void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400340{
341 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400342}
343
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700344void API minijail_no_new_privs(struct minijail *j)
345{
346 j->flags.no_new_privs = 1;
347}
348
Will Drewry6ac91122011-10-21 16:38:58 -0500349void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400350{
351 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500352}
353
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400354void API minijail_set_seccomp_filter_tsync(struct minijail *j)
355{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400356 if (j->filter_len > 0 && j->filter_prog != NULL) {
357 die("minijail_set_seccomp_filter_tsync() must be called "
358 "before minijail_parse_seccomp_filters()");
359 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400360 j->flags.seccomp_filter_tsync = 1;
361}
362
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700363void API minijail_log_seccomp_filter_failures(struct minijail *j)
364{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400365 if (j->filter_len > 0 && j->filter_prog != NULL) {
366 die("minijail_log_seccomp_filter_failures() must be called "
367 "before minijail_parse_seccomp_filters()");
368 }
369 j->flags.seccomp_filter_logging = 1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700370}
371
Will Drewry6ac91122011-10-21 16:38:58 -0500372void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400373{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800374 /*
375 * 'minijail_use_caps' configures a runtime-capabilities-only
376 * environment, including a bounding set matching the thread's runtime
377 * (permitted|inheritable|effective) sets.
378 * Therefore, it will override any existing bounding set configurations
379 * since the latter would allow gaining extra runtime capabilities from
380 * file capabilities.
381 */
382 if (j->flags.capbset_drop) {
383 warn("overriding bounding set configuration");
384 j->cap_bset = 0;
385 j->flags.capbset_drop = 0;
386 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400387 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800388 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400389}
390
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800391void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
392{
393 if (j->flags.use_caps) {
394 /*
395 * 'minijail_use_caps' will have already configured a capability
396 * bounding set matching the (permitted|inheritable|effective)
397 * sets. Abort if the user tries to configure a separate
398 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
399 * are mutually exclusive.
400 */
401 die("runtime capabilities already configured, can't drop "
402 "bounding set separately");
403 }
404 j->cap_bset = capmask;
405 j->flags.capbset_drop = 1;
406}
407
408void API minijail_reset_signal_mask(struct minijail *j)
409{
Peter Qiu2860c462015-12-16 15:13:06 -0800410 j->flags.reset_signal_mask = 1;
411}
412
Will Drewry6ac91122011-10-21 16:38:58 -0500413void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400414{
415 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400416}
417
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700418void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
419{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800420 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700421 if (ns_fd < 0) {
422 pdie("failed to open namespace '%s'", ns_path);
423 }
424 j->mountns_fd = ns_fd;
425 j->flags.enter_vfs = 1;
426}
427
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800428void API minijail_skip_remount_private(struct minijail *j)
429{
430 j->flags.skip_remount_private = 1;
431}
432
Will Drewry6ac91122011-10-21 16:38:58 -0500433void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400434{
Elly Jonese58176c2012-01-23 11:46:17 -0500435 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700436 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400437 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800438 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400439}
440
Dylan Reidf7942472015-11-18 17:55:26 -0800441void API minijail_namespace_ipc(struct minijail *j)
442{
443 j->flags.ipc = 1;
444}
445
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400446void API minijail_namespace_net(struct minijail *j)
447{
448 j->flags.net = 1;
449}
450
Dylan Reid1102f5a2015-09-15 11:52:20 -0700451void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
452{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800453 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700454 if (ns_fd < 0) {
455 pdie("failed to open namespace '%s'", ns_path);
456 }
457 j->netns_fd = ns_fd;
458 j->flags.enter_net = 1;
459}
460
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700461void API minijail_namespace_cgroups(struct minijail *j)
462{
463 j->flags.ns_cgroups = 1;
464}
465
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700466void API minijail_close_open_fds(struct minijail *j)
467{
468 j->flags.close_open_fds = 1;
469}
470
Dylan Reid791f5772015-09-14 20:02:42 -0700471void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400472{
473 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700474 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400475}
476
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800477void API minijail_namespace_user(struct minijail *j)
478{
479 j->flags.userns = 1;
480}
481
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400482void API minijail_namespace_user_disable_setgroups(struct minijail *j)
483{
484 j->flags.disable_setgroups = 1;
485}
486
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800487int API minijail_uidmap(struct minijail *j, const char *uidmap)
488{
489 j->uidmap = strdup(uidmap);
490 if (!j->uidmap)
491 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800492 char *ch;
493 for (ch = j->uidmap; *ch; ch++) {
494 if (*ch == ',')
495 *ch = '\n';
496 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800497 return 0;
498}
499
500int API minijail_gidmap(struct minijail *j, const char *gidmap)
501{
502 j->gidmap = strdup(gidmap);
503 if (!j->gidmap)
504 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800505 char *ch;
506 for (ch = j->gidmap; *ch; ch++) {
507 if (*ch == ',')
508 *ch = '\n';
509 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800510 return 0;
511}
512
Will Drewry6ac91122011-10-21 16:38:58 -0500513void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400514{
515 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400516}
517
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800518void API minijail_run_as_init(struct minijail *j)
519{
520 /*
521 * Since the jailed program will become 'init' in the new PID namespace,
522 * Minijail does not need to fork an 'init' process.
523 */
524 j->flags.do_init = 0;
525}
526
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700527int API minijail_enter_chroot(struct minijail *j, const char *dir)
528{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400529 if (j->chrootdir)
530 return -EINVAL;
531 j->chrootdir = strdup(dir);
532 if (!j->chrootdir)
533 return -ENOMEM;
534 j->flags.chroot = 1;
535 return 0;
536}
537
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800538int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
539{
540 if (j->chrootdir)
541 return -EINVAL;
542 j->chrootdir = strdup(dir);
543 if (!j->chrootdir)
544 return -ENOMEM;
545 j->flags.pivot_root = 1;
546 return 0;
547}
548
Dylan Reida14e08d2015-10-22 21:05:29 -0700549char API *minijail_get_original_path(struct minijail *j,
550 const char *path_inside_chroot)
551{
Dylan Reid648b2202015-10-23 00:50:00 -0700552 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700553
Dylan Reid648b2202015-10-23 00:50:00 -0700554 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700555 while (b) {
556 /*
557 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700558 * mount, then the original path is exactly the source of
559 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700560 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700561 * mount source = /some/path/exe, mount dest =
562 * /chroot/path/exe Then when getting the original path of
563 * "/chroot/path/exe", the source of that mount,
564 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700565 */
566 if (!strcmp(b->dest, path_inside_chroot))
567 return strdup(b->src);
568
569 /*
570 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700571 * mount, take the suffix of the chroot path relative to the
572 * mount destination path, and append it to the mount source
573 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700574 */
575 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
576 const char *relative_path =
577 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400578 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700579 }
580 b = b->next;
581 }
582
583 /* If there is a chroot path, append |path_inside_chroot| to that. */
584 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400585 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700586
587 /* No chroot, so the path outside is the same as it is inside. */
588 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700589}
590
Lee Campbell11af0622014-05-22 12:36:04 -0700591void API minijail_mount_tmp(struct minijail *j)
592{
593 j->flags.mount_tmp = 1;
594}
595
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800596int API minijail_write_pid_file(struct minijail *j, const char *path)
597{
598 j->pid_file_path = strdup(path);
599 if (!j->pid_file_path)
600 return -ENOMEM;
601 j->flags.pid_file = 1;
602 return 0;
603}
604
Dylan Reid605ce7f2016-01-19 19:21:00 -0800605int API minijail_add_to_cgroup(struct minijail *j, const char *path)
606{
607 if (j->cgroup_count >= MAX_CGROUPS)
608 return -ENOMEM;
609 j->cgroups[j->cgroup_count] = strdup(path);
610 if (!j->cgroups[j->cgroup_count])
611 return -ENOMEM;
612 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800613 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800614 return 0;
615}
616
Dylan Reid81e23972016-05-18 14:06:35 -0700617int API minijail_mount_with_data(struct minijail *j, const char *src,
618 const char *dest, const char *type,
619 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700620{
Dylan Reid648b2202015-10-23 00:50:00 -0700621 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400622
623 if (*dest != '/')
624 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700625 m = calloc(1, sizeof(*m));
626 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400627 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700628 m->dest = strdup(dest);
629 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400630 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700631 m->src = strdup(src);
632 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400633 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700634 m->type = strdup(type);
635 if (!m->type)
636 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700637 if (data) {
638 m->data = strdup(data);
639 if (!m->data)
640 goto error;
641 m->has_data = 1;
642 }
Dylan Reid648b2202015-10-23 00:50:00 -0700643 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400644
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800645 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400646
Elly Jonesdd3e8512012-01-23 15:13:38 -0500647 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700648 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400649 * containing vfs namespace.
650 */
651 minijail_namespace_vfs(j);
652
Dylan Reid648b2202015-10-23 00:50:00 -0700653 if (j->mounts_tail)
654 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400655 else
Dylan Reid648b2202015-10-23 00:50:00 -0700656 j->mounts_head = m;
657 j->mounts_tail = m;
658 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400659
660 return 0;
661
662error:
Dylan Reid81e23972016-05-18 14:06:35 -0700663 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700664 free(m->src);
665 free(m->dest);
666 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400667 return -ENOMEM;
668}
669
Dylan Reid81e23972016-05-18 14:06:35 -0700670int API minijail_mount(struct minijail *j, const char *src, const char *dest,
671 const char *type, unsigned long flags)
672{
673 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
674}
675
Dylan Reid648b2202015-10-23 00:50:00 -0700676int API minijail_bind(struct minijail *j, const char *src, const char *dest,
677 int writeable)
678{
679 unsigned long flags = MS_BIND;
680
681 if (!writeable)
682 flags |= MS_RDONLY;
683
684 return minijail_mount(j, src, dest, "", flags);
685}
686
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400687static void clear_seccomp_options(struct minijail *j)
688{
689 j->flags.seccomp_filter = 0;
690 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400691 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400692 j->filter_len = 0;
693 j->filter_prog = NULL;
694 j->flags.no_new_privs = 0;
695}
696
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400697static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400698{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400699 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400700 /*
701 * |errno| will be set to EINVAL when seccomp has not been
702 * compiled into the kernel. On certain platforms and kernel
703 * versions this is not a fatal failure. In that case, and only
704 * in that case, disable seccomp and skip loading the filters.
705 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400706 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400707 warn("not loading seccomp filters, seccomp filter not "
708 "supported");
709 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400710 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700711 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400712 /*
713 * If |errno| != EINVAL or seccomp_can_softfail() is false,
714 * we can proceed. Worst case scenario minijail_enter() will
715 * abort() if seccomp fails.
716 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700717 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400718 if (j->flags.seccomp_filter_tsync) {
719 /* Are the seccomp(2) syscall and the TSYNC option supported? */
720 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
721 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
722 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400723 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
724 warn("seccomp(2) syscall not supported");
725 clear_seccomp_options(j);
726 return 0;
727 } else if (saved_errno == EINVAL &&
728 seccomp_can_softfail()) {
729 warn(
730 "seccomp filter thread sync not supported");
731 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400732 return 0;
733 }
734 /*
735 * Similar logic here. If seccomp_can_softfail() is
736 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
737 * we can proceed. Worst case scenario minijail_enter()
738 * will abort() if seccomp or TSYNC fail.
739 */
740 }
741 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400742 return 1;
743}
744
745static int parse_seccomp_filters(struct minijail *j, FILE *policy_file)
746{
747 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400748 int use_ret_trap =
749 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
750 int allow_logging = j->flags.seccomp_filter_logging;
751
752 if (compile_filter(policy_file, fprog, use_ret_trap, allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400753 free(fprog);
754 return -1;
755 }
756
757 j->filter_len = fprog->len;
758 j->filter_prog = fprog;
759 return 0;
760}
761
762void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
763{
764 if (!seccomp_should_parse_filters(j))
765 return;
766
Elly Jonese1749eb2011-10-07 13:54:59 -0400767 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800768 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700769 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400770 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800771
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400772 if (parse_seccomp_filters(j, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700773 die("failed to compile seccomp filter BPF program in '%s'",
774 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800775 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400776 fclose(file);
777}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800778
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400779void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
780{
781 if (!seccomp_should_parse_filters(j))
782 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800783
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400784 FILE *file = fdopen(fd, "r");
785 if (!file) {
786 pdie("failed to associate stream with fd %d", fd);
787 }
788
789 if (parse_seccomp_filters(j, file) != 0) {
790 die("failed to compile seccomp filter BPF program from fd %d",
791 fd);
792 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400793 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500794}
795
Andrew Brestickereac28942015-11-11 16:04:46 -0800796int API minijail_use_alt_syscall(struct minijail *j, const char *table)
797{
798 j->alt_syscall_table = strdup(table);
799 if (!j->alt_syscall_table)
800 return -ENOMEM;
801 j->flags.alt_syscall = 1;
802 return 0;
803}
804
Will Drewryf89aef52011-09-16 16:48:57 -0500805struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400806 size_t available;
807 size_t total;
808 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500809};
810
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800811void marshal_state_init(struct marshal_state *state, char *buf,
812 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400813{
814 state->available = available;
815 state->buf = buf;
816 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500817}
818
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800819void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400820{
821 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500822
Elly Jonese1749eb2011-10-07 13:54:59 -0400823 /* Up to |available| will be written. */
824 if (copy_len) {
825 memcpy(state->buf, src, copy_len);
826 state->buf += copy_len;
827 state->available -= copy_len;
828 }
829 /* |total| will contain the expected length. */
830 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500831}
832
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400833void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700834{
835 marshal_append(state, m->src, strlen(m->src) + 1);
836 marshal_append(state, m->dest, strlen(m->dest) + 1);
837 marshal_append(state, m->type, strlen(m->type) + 1);
838 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
839 if (m->has_data)
840 marshal_append(state, m->data, strlen(m->data) + 1);
841 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
842}
843
Will Drewry6ac91122011-10-21 16:38:58 -0500844void minijail_marshal_helper(struct marshal_state *state,
845 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400846{
Dylan Reid648b2202015-10-23 00:50:00 -0700847 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800848 size_t i;
849
Elly Jonese1749eb2011-10-07 13:54:59 -0400850 marshal_append(state, (char *)j, sizeof(*j));
851 if (j->user)
852 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800853 if (j->suppl_gid_list) {
854 marshal_append(state, j->suppl_gid_list,
855 j->suppl_gid_count * sizeof(gid_t));
856 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400857 if (j->chrootdir)
858 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800859 if (j->alt_syscall_table) {
860 marshal_append(state, j->alt_syscall_table,
861 strlen(j->alt_syscall_table) + 1);
862 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800863 if (j->flags.seccomp_filter && j->filter_prog) {
864 struct sock_fprog *fp = j->filter_prog;
865 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800866 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400867 }
Dylan Reid648b2202015-10-23 00:50:00 -0700868 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400869 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400870 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800871 for (i = 0; i < j->cgroup_count; ++i)
872 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500873}
874
Will Drewry6ac91122011-10-21 16:38:58 -0500875size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400876{
877 struct marshal_state state;
878 marshal_state_init(&state, NULL, 0);
879 minijail_marshal_helper(&state, j);
880 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500881}
882
Elly Jonese1749eb2011-10-07 13:54:59 -0400883int minijail_marshal(const struct minijail *j, char *buf, size_t available)
884{
885 struct marshal_state state;
886 marshal_state_init(&state, buf, available);
887 minijail_marshal_helper(&state, j);
888 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500889}
890
Elly Jonese1749eb2011-10-07 13:54:59 -0400891int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
892{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800893 size_t i;
894 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500895 int ret = -EINVAL;
896
Elly Jonese1749eb2011-10-07 13:54:59 -0400897 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500898 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400899 memcpy((void *)j, serialized, sizeof(*j));
900 serialized += sizeof(*j);
901 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500902
Will Drewrybee7ba72011-10-21 20:47:01 -0500903 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -0400904 j->pid_file_path = NULL;
905 j->uidmap = NULL;
906 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -0700907 j->mounts_head = NULL;
908 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800909 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500910
Elly Jonese1749eb2011-10-07 13:54:59 -0400911 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400912 char *user = consumestr(&serialized, &length);
913 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500914 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400915 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500916 if (!j->user)
917 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400918 }
Will Drewryf89aef52011-09-16 16:48:57 -0500919
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800920 if (j->suppl_gid_list) { /* stale pointer */
921 if (j->suppl_gid_count > NGROUPS_MAX) {
922 goto bad_gid_list;
923 }
924 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
925 void *gid_list_bytes =
926 consumebytes(gid_list_size, &serialized, &length);
927 if (!gid_list_bytes)
928 goto bad_gid_list;
929
930 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
931 if (!j->suppl_gid_list)
932 goto bad_gid_list;
933
934 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
935 }
936
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400937 if (j->chrootdir) { /* stale pointer */
938 char *chrootdir = consumestr(&serialized, &length);
939 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500940 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400941 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500942 if (!j->chrootdir)
943 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400944 }
945
Andrew Brestickereac28942015-11-11 16:04:46 -0800946 if (j->alt_syscall_table) { /* stale pointer */
947 char *alt_syscall_table = consumestr(&serialized, &length);
948 if (!alt_syscall_table)
949 goto bad_syscall_table;
950 j->alt_syscall_table = strdup(alt_syscall_table);
951 if (!j->alt_syscall_table)
952 goto bad_syscall_table;
953 }
954
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800955 if (j->flags.seccomp_filter && j->filter_len > 0) {
956 size_t ninstrs = j->filter_len;
957 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
958 ninstrs > USHRT_MAX)
959 goto bad_filters;
960
961 size_t program_len = ninstrs * sizeof(struct sock_filter);
962 void *program = consumebytes(program_len, &serialized, &length);
963 if (!program)
964 goto bad_filters;
965
966 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800967 if (!j->filter_prog)
968 goto bad_filters;
969
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800970 j->filter_prog->len = ninstrs;
971 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800972 if (!j->filter_prog->filter)
973 goto bad_filter_prog_instrs;
974
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800975 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400976 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400977
Dylan Reid648b2202015-10-23 00:50:00 -0700978 count = j->mounts_count;
979 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400980 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700981 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -0700982 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400983 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700984 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700985 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400986 const char *src = consumestr(&serialized, &length);
987 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700988 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400989 dest = consumestr(&serialized, &length);
990 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700991 goto bad_mounts;
992 type = consumestr(&serialized, &length);
993 if (!type)
994 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -0700995 has_data = consumebytes(sizeof(*has_data), &serialized,
996 &length);
997 if (!has_data)
998 goto bad_mounts;
999 if (*has_data) {
1000 data = consumestr(&serialized, &length);
1001 if (!data)
1002 goto bad_mounts;
1003 }
Dylan Reid648b2202015-10-23 00:50:00 -07001004 flags = consumebytes(sizeof(*flags), &serialized, &length);
1005 if (!flags)
1006 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001007 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001008 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001009 }
1010
Dylan Reid605ce7f2016-01-19 19:21:00 -08001011 count = j->cgroup_count;
1012 j->cgroup_count = 0;
1013 for (i = 0; i < count; ++i) {
1014 char *cgroup = consumestr(&serialized, &length);
1015 if (!cgroup)
1016 goto bad_cgroups;
1017 j->cgroups[i] = strdup(cgroup);
1018 if (!j->cgroups[i])
1019 goto bad_cgroups;
1020 ++j->cgroup_count;
1021 }
1022
Elly Jonese1749eb2011-10-07 13:54:59 -04001023 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001024
Dylan Reid605ce7f2016-01-19 19:21:00 -08001025bad_cgroups:
1026 while (j->mounts_head) {
1027 struct mountpoint *m = j->mounts_head;
1028 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07001029 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001030 free(m->type);
1031 free(m->dest);
1032 free(m->src);
1033 free(m);
1034 }
1035 for (i = 0; i < j->cgroup_count; ++i)
1036 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001037bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001038 if (j->flags.seccomp_filter && j->filter_len > 0) {
1039 free(j->filter_prog->filter);
1040 free(j->filter_prog);
1041 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001042bad_filter_prog_instrs:
1043 if (j->filter_prog)
1044 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001045bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001046 if (j->alt_syscall_table)
1047 free(j->alt_syscall_table);
1048bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001049 if (j->chrootdir)
1050 free(j->chrootdir);
1051bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001052 if (j->suppl_gid_list)
1053 free(j->suppl_gid_list);
1054bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001055 if (j->user)
1056 free(j->user);
1057clear_pointers:
1058 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001059 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001060 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001061 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001062 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001063out:
1064 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001065}
1066
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001067/*
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001068 * setup_mount_destination: Ensures the mount target exists.
1069 * Creates it if needed and possible.
Dylan Reideec77962016-06-30 19:35:10 -07001070 */
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001071static int setup_mount_destination(const char *source, const char *dest,
1072 uid_t uid, uid_t gid)
Dylan Reideec77962016-06-30 19:35:10 -07001073{
1074 int rc;
1075 struct stat st_buf;
1076
1077 rc = stat(dest, &st_buf);
1078 if (rc == 0) /* destination exists */
1079 return 0;
1080
1081 /*
1082 * Try to create the destination.
1083 * Either make a directory or touch a file depending on the source type.
1084 * If the source doesn't exist, assume it is a filesystem type such as
1085 * "tmpfs" and create a directory to mount it on.
1086 */
1087 rc = stat(source, &st_buf);
1088 if (rc || S_ISDIR(st_buf.st_mode) || S_ISBLK(st_buf.st_mode)) {
1089 if (mkdir(dest, 0700))
1090 return -errno;
1091 } else {
1092 int fd = open(dest, O_RDWR | O_CREAT, 0700);
1093 if (fd < 0)
1094 return -errno;
1095 close(fd);
1096 }
1097 return chown(dest, uid, gid);
1098}
1099
1100/*
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001101 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001102 * @j Minijail these mounts are for
1103 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001104 *
1105 * Returns 0 for success.
1106 */
Dylan Reid648b2202015-10-23 00:50:00 -07001107static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001108{
Dylan Reid648b2202015-10-23 00:50:00 -07001109 int ret;
1110 char *dest;
1111 int remount_ro = 0;
1112
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001113 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001114 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001115 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001116
Dylan Reideec77962016-06-30 19:35:10 -07001117 if (setup_mount_destination(m->src, dest, j->uid, j->gid))
1118 pdie("creating mount target '%s' failed", dest);
1119
Dylan Reid648b2202015-10-23 00:50:00 -07001120 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001121 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1122 * can't both be specified in the original bind mount.
1123 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001124 */
1125 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1126 remount_ro = 1;
1127 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001128 }
Dylan Reid648b2202015-10-23 00:50:00 -07001129
Dylan Reid81e23972016-05-18 14:06:35 -07001130 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001131 if (ret)
1132 pdie("mount: %s -> %s", m->src, dest);
1133
1134 if (remount_ro) {
1135 m->flags |= MS_RDONLY;
1136 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001137 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001138 if (ret)
1139 pdie("bind ro: %s -> %s", m->src, dest);
1140 }
1141
Elly Jones51a5b6c2011-10-12 19:09:26 -04001142 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001143 if (m->next)
1144 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001145 return ret;
1146}
1147
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001148static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001149{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001150 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001151
1152 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001153 return ret;
1154
1155 if (chroot(j->chrootdir))
1156 return -errno;
1157
1158 if (chdir("/"))
1159 return -errno;
1160
1161 return 0;
1162}
1163
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001164static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001165{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001166 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001167
1168 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001169 return ret;
1170
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001171 /*
1172 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001173 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001174 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001175 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001176 if (oldroot < 0)
1177 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001178 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001179 if (newroot < 0)
1180 pdie("failed to open %s for fchdir", j->chrootdir);
1181
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001182 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001183 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001184 * do a self bind mount.
1185 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001186 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1187 pdie("failed to bind mount '%s'", j->chrootdir);
1188 if (chdir(j->chrootdir))
1189 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001190 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001191 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001192
1193 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001194 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001195 * change to the old root and unmount it.
1196 */
1197 if (fchdir(oldroot))
1198 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001199
1200 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001201 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1202 * there could be a shared mount point under |oldroot|. In that case,
1203 * mounts under this shared mount point will be unmounted below, and
1204 * this unmounting will propagate to the original mount namespace
1205 * (because the mount point is shared). To prevent this unexpected
1206 * unmounting, remove these mounts from their peer groups by recursively
1207 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001208 */
1209 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001210 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001211 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001212 if (umount2(".", MNT_DETACH))
1213 pdie("umount(/)");
1214 /* Change back to the new root. */
1215 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001216 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001217 if (close(oldroot))
1218 return -errno;
1219 if (close(newroot))
1220 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001221 if (chroot("/"))
1222 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001223 /* Set correct CWD for getcwd(3). */
1224 if (chdir("/"))
1225 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001226
1227 return 0;
1228}
1229
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001230static int mount_tmp(void)
Lee Campbell11af0622014-05-22 12:36:04 -07001231{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001232 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001233}
1234
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001235static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001236{
1237 const char *kProcPath = "/proc";
1238 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001239 /*
1240 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001241 * /proc in our namespace, which means using MS_REMOUNT here would
1242 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001243 * namespace (!). Instead, remove their mount from our namespace lazily
1244 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001245 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001246 if (umount2(kProcPath, MNT_DETACH)) {
1247 /*
1248 * If we are in a new user namespace, umount(2) will fail.
1249 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1250 */
1251 if (j->flags.userns) {
1252 info("umount(/proc, MNT_DETACH) failed, "
1253 "this is expected when using user namespaces");
1254 } else {
1255 return -errno;
1256 }
1257 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001258 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1259 return -errno;
1260 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001261}
1262
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001263static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001264{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001265 kill(j->initpid, SIGKILL);
1266 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001267}
1268
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001269static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001270{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001271 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001272 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001273}
1274
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001275static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001276{
1277 size_t i;
1278
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001279 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001280 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001281 kill_child_and_die(j, "failed to add to cgroups");
1282 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001283}
1284
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001285static void write_ugid_maps_or_die(const struct minijail *j)
1286{
1287 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1288 kill_child_and_die(j, "failed to write uid_map");
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -04001289 if (j->gidmap && j->flags.disable_setgroups &&
1290 write_proc_file(j->initpid, "deny", "setgroups") != 0)
1291 kill_child_and_die(j, "failed to disable setgroups(2)");
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001292 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1293 kill_child_and_die(j, "failed to write gid_map");
1294}
1295
1296static void enter_user_namespace(const struct minijail *j)
1297{
1298 if (j->uidmap && setresuid(0, 0, 0))
1299 pdie("user_namespaces: setresuid(0, 0, 0) failed");
1300 if (j->gidmap && setresgid(0, 0, 0))
1301 pdie("user_namespaces: setresgid(0, 0, 0) failed");
1302}
1303
1304static void parent_setup_complete(int *pipe_fds)
1305{
1306 close(pipe_fds[0]);
1307 close(pipe_fds[1]);
1308}
1309
1310/*
1311 * wait_for_parent_setup: Called by the child process to wait for any
1312 * further parent-side setup to complete before continuing.
1313 */
1314static void wait_for_parent_setup(int *pipe_fds)
1315{
1316 char buf;
1317
1318 close(pipe_fds[1]);
1319
1320 /* Wait for parent to complete setup and close the pipe. */
1321 if (read(pipe_fds[0], &buf, 1) != 0)
1322 die("failed to sync with parent");
1323 close(pipe_fds[0]);
1324}
1325
1326static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001327{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001328 if (j->flags.usergroups && j->flags.suppl_gids) {
1329 die("tried to inherit *and* set supplementary groups;"
1330 " can only do one");
1331 }
1332
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001333 if (j->flags.usergroups) {
1334 if (initgroups(j->user, j->usergid))
1335 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001336 } else if (j->flags.suppl_gids) {
1337 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1338 pdie("setgroups");
1339 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001340 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001341 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001342 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001343 * users.
1344 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001345 if ((j->uid || j->gid) && setgroups(0, NULL))
1346 pdie("setgroups");
1347 }
1348
1349 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1350 pdie("setresgid");
1351
1352 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1353 pdie("setresuid");
1354}
1355
Mike Frysinger3adfef72013-05-09 17:19:08 -04001356/*
1357 * We specifically do not use cap_valid() as that only tells us the last
1358 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001359 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001360 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001361 * kernel).
1362 * Normally, we suck up the answer via /proc. On Android, not all processes are
1363 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1364 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001365 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001366static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001367{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001368 unsigned int last_valid_cap = 0;
1369 if (is_android()) {
1370 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1371 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001372
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001373 /* |last_valid_cap| will be the first failing value. */
1374 if (last_valid_cap > 0) {
1375 last_valid_cap--;
1376 }
1377 } else {
1378 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1379 FILE *fp = fopen(cap_file, "re");
1380 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1381 pdie("fscanf(%s)", cap_file);
1382 fclose(fp);
1383 }
Dylan Reidf682d472015-09-17 21:39:07 -07001384 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001385}
1386
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001387static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1388{
1389 const uint64_t one = 1;
1390 unsigned int i;
1391 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1392 if (keep_mask & (one << i))
1393 continue;
1394 if (prctl(PR_CAPBSET_DROP, i))
1395 pdie("could not drop capability from bounding set");
1396 }
1397}
1398
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001399static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001400{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001401 if (!j->flags.use_caps)
1402 return;
1403
Elly Jonese1749eb2011-10-07 13:54:59 -04001404 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001405 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001406 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001407 unsigned int i;
1408 if (!caps)
1409 die("can't get process caps");
1410 if (cap_clear_flag(caps, CAP_INHERITABLE))
1411 die("can't clear inheritable caps");
1412 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1413 die("can't clear effective caps");
1414 if (cap_clear_flag(caps, CAP_PERMITTED))
1415 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001416 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001417 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001418 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001419 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001420 flag[0] = i;
1421 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001422 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001423 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001424 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001425 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001426 die("can't add inheritable cap");
1427 }
1428 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001429 die("can't apply initial cleaned capset");
1430
1431 /*
1432 * Instead of dropping bounding set first, do it here in case
1433 * the caller had a more permissive bounding set which could
1434 * have been used above to raise a capability that wasn't already
1435 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1436 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001437 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001438
1439 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001440 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001441 flag[0] = CAP_SETPCAP;
1442 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1443 die("can't clear effective cap");
1444 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1445 die("can't clear permitted cap");
1446 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1447 die("can't clear inheritable cap");
1448 }
1449
1450 if (cap_set_proc(caps))
1451 die("can't apply final cleaned capset");
1452
1453 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001454}
1455
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001456static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001457{
1458 /*
1459 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1460 * in the kernel source tree for an explanation of the parameters.
1461 */
1462 if (j->flags.no_new_privs) {
1463 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1464 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1465 }
1466
1467 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001468 * Code running with ASan
1469 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1470 * will make system calls not included in the syscall filter policy,
1471 * which will likely crash the program. Skip setting seccomp filter in
1472 * that case.
1473 * 'running_with_asan()' has no inputs and is completely defined at
1474 * build time, so this cannot be used by an attacker to skip setting
1475 * seccomp filter.
1476 */
1477 if (j->flags.seccomp_filter && running_with_asan()) {
1478 warn("running with ASan, not setting seccomp filter");
1479 return;
1480 }
1481
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001482 if (j->flags.seccomp_filter) {
1483 if (j->flags.seccomp_filter_logging) {
1484 /*
1485 * If logging seccomp filter failures,
1486 * install the SIGSYS handler first.
1487 */
1488 if (install_sigsys_handler())
1489 pdie("failed to install SIGSYS handler");
1490 warn("logging seccomp filter failures");
1491 } else if (j->flags.seccomp_filter_tsync) {
1492 /*
1493 * If setting thread sync,
1494 * reset the SIGSYS signal handler so that
1495 * the entire thread group is killed.
1496 */
1497 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1498 pdie("failed to reset SIGSYS disposition");
1499 info("reset SIGSYS disposition");
1500 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001501 }
1502
1503 /*
1504 * Install the syscall filter.
1505 */
1506 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001507 if (j->flags.seccomp_filter_tsync) {
1508 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1509 SECCOMP_FILTER_FLAG_TSYNC,
1510 j->filter_prog)) {
1511 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001512 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001513 } else {
1514 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1515 j->filter_prog)) {
1516 pdie("prctl(seccomp_filter) failed");
1517 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001518 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001519 }
1520}
1521
Will Drewry6ac91122011-10-21 16:38:58 -05001522void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001523{
Dylan Reidf682d472015-09-17 21:39:07 -07001524 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001525 * If we're dropping caps, get the last valid cap from /proc now,
1526 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001527 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001528 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001529 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001530 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001531
Elly Jonese1749eb2011-10-07 13:54:59 -04001532 if (j->flags.pids)
1533 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001534 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001535
Elly Jonese1749eb2011-10-07 13:54:59 -04001536 if (j->flags.usergroups && !j->user)
1537 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001538
Elly Jonesdd3e8512012-01-23 15:13:38 -05001539 /*
1540 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001541 * so we don't even try. If any of our operations fail, we abort() the
1542 * entire process.
1543 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001544 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1545 pdie("setns(CLONE_NEWNS)");
1546
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001547 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001548 if (unshare(CLONE_NEWNS))
1549 pdie("unshare(vfs)");
1550 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001551 * Unless asked not to, remount all filesystems as private.
1552 * If they are shared, new bind mounts will creep out of our
1553 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001554 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1555 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001556 if (!j->flags.skip_remount_private) {
1557 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1558 pdie("mount(/, private)");
1559 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001560 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001561
Dylan Reidf7942472015-11-18 17:55:26 -08001562 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1563 pdie("unshare(ipc)");
1564 }
1565
Dylan Reid1102f5a2015-09-15 11:52:20 -07001566 if (j->flags.enter_net) {
1567 if (setns(j->netns_fd, CLONE_NEWNET))
1568 pdie("setns(CLONE_NEWNET)");
1569 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001570 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001571 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001572
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001573 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
1574 pdie("unshare(cgroups)");
1575
Elly Jones51a5b6c2011-10-12 19:09:26 -04001576 if (j->flags.chroot && enter_chroot(j))
1577 pdie("chroot");
1578
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001579 if (j->flags.pivot_root && enter_pivot_root(j))
1580 pdie("pivot_root");
1581
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001582 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001583 pdie("mount_tmp");
1584
Dylan Reid791f5772015-09-14 20:02:42 -07001585 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001586 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001587
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001588 /*
1589 * If we're only dropping capabilities from the bounding set, but not
1590 * from the thread's (permitted|inheritable|effective) sets, do it now.
1591 */
1592 if (j->flags.capbset_drop) {
1593 drop_capbset(j->cap_bset, last_valid_cap);
1594 }
1595
1596 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001597 /*
1598 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001599 * capability to change uids, our attempt to use setuid()
1600 * below will fail. Hang on to root caps across setuid(), then
1601 * lock securebits.
1602 */
1603 if (prctl(PR_SET_KEEPCAPS, 1))
1604 pdie("prctl(PR_SET_KEEPCAPS)");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001605
1606 /*
1607 * Kernels 4.3+ define a new securebit
1608 * (SECURE_NO_CAP_AMBIENT_RAISE), so using the SECURE_ALL_BITS
1609 * and SECURE_ALL_LOCKS masks from newer kernel headers will
1610 * return EPERM on older kernels. Detect this, and retry with
1611 * the right mask for older (2.6.26-4.2) kernels.
1612 */
1613 int securebits_ret = prctl(PR_SET_SECUREBITS,
1614 SECURE_ALL_BITS | SECURE_ALL_LOCKS);
1615 if (securebits_ret < 0) {
1616 if (errno == EPERM) {
1617 /* Possibly running on kernel < 4.3. */
1618 securebits_ret = prctl(
1619 PR_SET_SECUREBITS,
1620 OLD_SECURE_ALL_BITS | OLD_SECURE_ALL_LOCKS);
1621 }
1622 }
1623 if (securebits_ret < 0)
Elly Jonese1749eb2011-10-07 13:54:59 -04001624 pdie("prctl(PR_SET_SECUREBITS)");
1625 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001626
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001627 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001628 /*
1629 * If we're setting no_new_privs, we can drop privileges
1630 * before setting seccomp filter. This way filter policies
1631 * don't need to allow privilege-dropping syscalls.
1632 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001633 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001634 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001635 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001636 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001637 /*
1638 * If we're not setting no_new_privs,
1639 * we need to set seccomp filter *before* dropping privileges.
1640 * WARNING: this means that filter policies *must* allow
1641 * setgroups()/setresgid()/setresuid() for dropping root and
1642 * capget()/capset()/prctl() for dropping caps.
1643 */
1644 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001645 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001646 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001647 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001648
Elly Jonesdd3e8512012-01-23 15:13:38 -05001649 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001650 * Select the specified alternate syscall table. The table must not
1651 * block prctl(2) if we're using seccomp as well.
1652 */
1653 if (j->flags.alt_syscall) {
1654 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1655 pdie("prctl(PR_ALT_SYSCALL)");
1656 }
1657
1658 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001659 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001660 * privilege-dropping syscalls :)
1661 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001662 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001663 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001664 warn("seccomp not supported");
1665 return;
1666 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001667 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001668 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001669}
1670
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001671/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001672static int init_exitstatus = 0;
1673
Will Drewry6ac91122011-10-21 16:38:58 -05001674void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001675{
1676 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001677}
1678
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001679void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001680{
1681 pid_t pid;
1682 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001683 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001684 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001685 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001686 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001687 /*
1688 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001689 * left inside our pid namespace or we get a signal.
1690 */
1691 if (pid == rootpid)
1692 init_exitstatus = status;
1693 }
1694 if (!WIFEXITED(init_exitstatus))
1695 _exit(MINIJAIL_ERR_INIT);
1696 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001697}
1698
Will Drewry6ac91122011-10-21 16:38:58 -05001699int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001700{
1701 size_t sz = 0;
1702 size_t bytes = read(fd, &sz, sizeof(sz));
1703 char *buf;
1704 int r;
1705 if (sizeof(sz) != bytes)
1706 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001707 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001708 return -E2BIG;
1709 buf = malloc(sz);
1710 if (!buf)
1711 return -ENOMEM;
1712 bytes = read(fd, buf, sz);
1713 if (bytes != sz) {
1714 free(buf);
1715 return -EINVAL;
1716 }
1717 r = minijail_unmarshal(j, buf, sz);
1718 free(buf);
1719 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001720}
1721
Will Drewry6ac91122011-10-21 16:38:58 -05001722int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001723{
1724 char *buf;
1725 size_t sz = minijail_size(j);
1726 ssize_t written;
1727 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001728
Elly Jonese1749eb2011-10-07 13:54:59 -04001729 if (!sz)
1730 return -EINVAL;
1731 buf = malloc(sz);
1732 r = minijail_marshal(j, buf, sz);
1733 if (r) {
1734 free(buf);
1735 return r;
1736 }
1737 /* Sends [size][minijail]. */
1738 written = write(fd, &sz, sizeof(sz));
1739 if (written != sizeof(sz)) {
1740 free(buf);
1741 return -EFAULT;
1742 }
1743 written = write(fd, buf, sz);
1744 if (written < 0 || (size_t) written != sz) {
1745 free(buf);
1746 return -EFAULT;
1747 }
1748 free(buf);
1749 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001750}
Elly Jonescd7a9042011-07-22 13:56:51 -04001751
Will Drewry6ac91122011-10-21 16:38:58 -05001752int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001753{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001754#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001755 /* Don't use LDPRELOAD on Brillo. */
1756 return 0;
1757#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001758 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1759 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1760 if (!newenv)
1761 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001762
Elly Jonese1749eb2011-10-07 13:54:59 -04001763 /* Only insert a separating space if we have something to separate... */
1764 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1765 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001766
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001767 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001768 setenv(kLdPreloadEnvVar, newenv, 1);
1769 free(newenv);
1770 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001771#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001772}
1773
Will Drewry6ac91122011-10-21 16:38:58 -05001774int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001775{
1776 int r = pipe(fds);
1777 char fd_buf[11];
1778 if (r)
1779 return r;
1780 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1781 if (r <= 0)
1782 return -EINVAL;
1783 setenv(kFdEnvVar, fd_buf, 1);
1784 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001785}
1786
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001787int setup_pipe_end(int fds[2], size_t index)
1788{
1789 if (index > 1)
1790 return -1;
1791
1792 close(fds[1 - index]);
1793 return fds[index];
1794}
1795
1796int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1797{
1798 if (index > 1)
1799 return -1;
1800
1801 close(fds[1 - index]);
1802 /* dup2(2) the corresponding end of the pipe into |fd|. */
1803 return dup2(fds[index], fd);
1804}
1805
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07001806int close_open_fds(int *inheritable_fds, size_t size)
1807{
1808 const char *kFdPath = "/proc/self/fd";
1809
1810 DIR *d = opendir(kFdPath);
1811 struct dirent *dir_entry;
1812
1813 if (d == NULL)
1814 return -1;
1815 int dir_fd = dirfd(d);
1816 while ((dir_entry = readdir(d)) != NULL) {
1817 size_t i;
1818 char *end;
1819 bool should_close = true;
1820 const int fd = strtol(dir_entry->d_name, &end, 10);
1821
1822 if ((*end) != '\0') {
1823 continue;
1824 }
1825 /*
1826 * We might have set up some pipes that we want to share with
1827 * the parent process, and should not be closed.
1828 */
1829 for (i = 0; i < size; ++i) {
1830 if (fd == inheritable_fds[i]) {
1831 should_close = false;
1832 break;
1833 }
1834 }
1835 /* Also avoid closing the directory fd. */
1836 if (should_close && fd != dir_fd)
1837 close(fd);
1838 }
1839 closedir(d);
1840 return 0;
1841}
1842
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001843int minijail_run_internal(struct minijail *j, const char *filename,
1844 char *const argv[], pid_t *pchild_pid,
1845 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1846 int use_preload);
1847
Will Drewry6ac91122011-10-21 16:38:58 -05001848int API minijail_run(struct minijail *j, const char *filename,
1849 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001850{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001851 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1852 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001853}
1854
1855int API minijail_run_pid(struct minijail *j, const char *filename,
1856 char *const argv[], pid_t *pchild_pid)
1857{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001858 return minijail_run_internal(j, filename, argv, pchild_pid,
1859 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001860}
1861
1862int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001863 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001864{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001865 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1866 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001867}
1868
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001869int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001870 char *const argv[], pid_t *pchild_pid,
1871 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001872{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001873 return minijail_run_internal(j, filename, argv, pchild_pid,
1874 pstdin_fd, pstdout_fd, pstderr_fd, true);
1875}
1876
1877int API minijail_run_no_preload(struct minijail *j, const char *filename,
1878 char *const argv[])
1879{
1880 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1881 false);
1882}
1883
Samuel Tan63187f42015-10-16 13:01:53 -07001884int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001885 const char *filename,
1886 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001887 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001888 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001889 int *pstderr_fd)
1890{
Samuel Tan63187f42015-10-16 13:01:53 -07001891 return minijail_run_internal(j, filename, argv, pchild_pid,
1892 pstdin_fd, pstdout_fd, pstderr_fd, false);
1893}
1894
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001895int minijail_run_internal(struct minijail *j, const char *filename,
1896 char *const argv[], pid_t *pchild_pid,
1897 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1898 int use_preload)
1899{
Elly Jonese1749eb2011-10-07 13:54:59 -04001900 char *oldenv, *oldenv_copy = NULL;
1901 pid_t child_pid;
1902 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001903 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001904 int stdout_fds[2];
1905 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001906 int child_sync_pipe_fds[2];
1907 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001908 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001909 /* We need to remember this across the minijail_preexec() call. */
1910 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001911 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001912
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001913 if (use_preload) {
1914 oldenv = getenv(kLdPreloadEnvVar);
1915 if (oldenv) {
1916 oldenv_copy = strdup(oldenv);
1917 if (!oldenv_copy)
1918 return -ENOMEM;
1919 }
1920
1921 if (setup_preload())
1922 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001923 }
Will Drewryf89aef52011-09-16 16:48:57 -05001924
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001925 if (!use_preload) {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04001926 if (j->flags.use_caps && j->caps != 0)
1927 die("non-empty capabilities are not supported without LD_PRELOAD");
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001928 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001929
Elly Jonesdd3e8512012-01-23 15:13:38 -05001930 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001931 * Make the process group ID of this process equal to its PID, so that
1932 * both the Minijail process and the jailed process can be killed
1933 * together.
1934 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1935 * the process is already a process group leader.
1936 */
1937 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1938 if (errno != EPERM) {
1939 pdie("setpgid(0, 0)");
1940 }
1941 }
1942
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001943 if (use_preload) {
1944 /*
1945 * Before we fork(2) and execve(2) the child process, we need
1946 * to open a pipe(2) to send the minijail configuration over.
1947 */
1948 if (setup_pipe(pipe_fds))
1949 return -EFAULT;
1950 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001951
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001952 /*
1953 * If we want to write to the child process' standard input,
1954 * create the pipe(2) now.
1955 */
1956 if (pstdin_fd) {
1957 if (pipe(stdin_fds))
1958 return -EFAULT;
1959 }
1960
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001961 /*
1962 * If we want to read from the child process' standard output,
1963 * create the pipe(2) now.
1964 */
1965 if (pstdout_fd) {
1966 if (pipe(stdout_fds))
1967 return -EFAULT;
1968 }
1969
1970 /*
1971 * If we want to read from the child process' standard error,
1972 * create the pipe(2) now.
1973 */
1974 if (pstderr_fd) {
1975 if (pipe(stderr_fds))
1976 return -EFAULT;
1977 }
1978
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001979 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04001980 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001981 * or if we need to add the child process to cgroups, create the pipe(2)
1982 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001983 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001984 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001985 sync_child = 1;
1986 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001987 return -EFAULT;
1988 }
1989
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001990 /*
1991 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001992 *
1993 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1994 *
1995 * In multithreaded programs, there are a bunch of locks inside libc,
1996 * some of which may be held by other threads at the time that we call
1997 * minijail_run_pid(). If we call fork(), glibc does its level best to
1998 * ensure that we hold all of these locks before it calls clone()
1999 * internally and drop them after clone() returns, but when we call
2000 * sys_clone(2) directly, all that gets bypassed and we end up with a
2001 * child address space where some of libc's important locks are held by
2002 * other threads (which did not get cloned, and hence will never release
2003 * those locks). This is okay so long as we call exec() immediately
2004 * after, but a bunch of seemingly-innocent libc functions like setenv()
2005 * take locks.
2006 *
2007 * Hence, only call sys_clone() if we need to, in order to get at pid
2008 * namespacing. If we follow this path, the child's address space might
2009 * have broken locks; you may only call functions that do not acquire
2010 * any locks.
2011 *
2012 * Unfortunately, fork() acquires every lock it can get its hands on, as
2013 * previously detailed, so this function is highly likely to deadlock
2014 * later on (see "deadlock here") if we're multithreaded.
2015 *
2016 * We might hack around this by having the clone()d child (init of the
2017 * pid namespace) return directly, rather than leaving the clone()d
2018 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002019 * its fork()ed child return in turn), but that process would be
2020 * crippled with its libc locks potentially broken. We might try
2021 * fork()ing in the parent before we clone() to ensure that we own all
2022 * the locks, but then we have to have the forked child hanging around
2023 * consuming resources (and possibly having file descriptors / shared
2024 * memory regions / etc attached). We'd need to keep the child around to
2025 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002026 *
2027 * TODO(ellyjones): figure out if the "forked child hanging around"
2028 * problem is fixable or not. It would be nice if we worked in this
2029 * case.
2030 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002031 if (pid_namespace) {
2032 int clone_flags = CLONE_NEWPID | SIGCHLD;
2033 if (j->flags.userns)
2034 clone_flags |= CLONE_NEWUSER;
2035 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002036 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002037 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002038 }
Elly Jones761b7412012-06-13 15:49:52 -04002039
Elly Jonese1749eb2011-10-07 13:54:59 -04002040 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002041 if (use_preload) {
2042 free(oldenv_copy);
2043 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002044 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002045 }
Will Drewryf89aef52011-09-16 16:48:57 -05002046
Elly Jonese1749eb2011-10-07 13:54:59 -04002047 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002048 if (use_preload) {
2049 /* Restore parent's LD_PRELOAD. */
2050 if (oldenv_copy) {
2051 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2052 free(oldenv_copy);
2053 } else {
2054 unsetenv(kLdPreloadEnvVar);
2055 }
2056 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002057 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002058
Elly Jonese1749eb2011-10-07 13:54:59 -04002059 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002060
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002061 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002062 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002063
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002064 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002065 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002066
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002067 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002068 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002069
2070 if (sync_child)
2071 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002072
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002073 if (use_preload) {
2074 /* Send marshalled minijail. */
2075 close(pipe_fds[0]); /* read endpoint */
2076 ret = minijail_to_fd(j, pipe_fds[1]);
2077 close(pipe_fds[1]); /* write endpoint */
2078 if (ret) {
2079 kill(j->initpid, SIGKILL);
2080 die("failed to send marshalled minijail");
2081 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002082 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002083
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002084 if (pchild_pid)
2085 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002086
2087 /*
2088 * If we want to write to the child process' standard input,
2089 * set up the write end of the pipe.
2090 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002091 if (pstdin_fd)
2092 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002093 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002094
2095 /*
2096 * If we want to read from the child process' standard output,
2097 * set up the read end of the pipe.
2098 */
2099 if (pstdout_fd)
2100 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002101 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002102
2103 /*
2104 * If we want to read from the child process' standard error,
2105 * set up the read end of the pipe.
2106 */
2107 if (pstderr_fd)
2108 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002109 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002110
Elly Jonese1749eb2011-10-07 13:54:59 -04002111 return 0;
2112 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002113 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002114 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002115
Peter Qiu2860c462015-12-16 15:13:06 -08002116 if (j->flags.reset_signal_mask) {
2117 sigset_t signal_mask;
2118 if (sigemptyset(&signal_mask) != 0)
2119 pdie("sigemptyset failed");
2120 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2121 pdie("sigprocmask failed");
2122 }
2123
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002124 if (j->flags.close_open_fds) {
2125 const size_t kMaxInheritableFdsSize = 10;
2126 int inheritable_fds[kMaxInheritableFdsSize];
2127 size_t size = 0;
2128 if (use_preload) {
2129 inheritable_fds[size++] = pipe_fds[0];
2130 inheritable_fds[size++] = pipe_fds[1];
2131 }
2132 if (sync_child) {
2133 inheritable_fds[size++] = child_sync_pipe_fds[0];
2134 inheritable_fds[size++] = child_sync_pipe_fds[1];
2135 }
2136 if (pstdin_fd) {
2137 inheritable_fds[size++] = stdin_fds[0];
2138 inheritable_fds[size++] = stdin_fds[1];
2139 }
2140 if (pstdout_fd) {
2141 inheritable_fds[size++] = stdout_fds[0];
2142 inheritable_fds[size++] = stdout_fds[1];
2143 }
2144 if (pstderr_fd) {
2145 inheritable_fds[size++] = stderr_fds[0];
2146 inheritable_fds[size++] = stderr_fds[1];
2147 }
2148
2149 if (close_open_fds(inheritable_fds, size) < 0)
2150 die("failed to close open file descriptors");
2151 }
2152
Dylan Reidce5b55e2016-01-13 11:04:16 -08002153 if (sync_child)
2154 wait_for_parent_setup(child_sync_pipe_fds);
2155
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002156 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002157 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002158
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002159 /*
2160 * If we want to write to the jailed process' standard input,
2161 * set up the read end of the pipe.
2162 */
2163 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002164 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2165 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002166 die("failed to set up stdin pipe");
2167 }
2168
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002169 /*
2170 * If we want to read from the jailed process' standard output,
2171 * set up the write end of the pipe.
2172 */
2173 if (pstdout_fd) {
2174 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2175 STDOUT_FILENO) < 0)
2176 die("failed to set up stdout pipe");
2177 }
2178
2179 /*
2180 * If we want to read from the jailed process' standard error,
2181 * set up the write end of the pipe.
2182 */
2183 if (pstderr_fd) {
2184 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2185 STDERR_FILENO) < 0)
2186 die("failed to set up stderr pipe");
2187 }
2188
Dylan Reid791f5772015-09-14 20:02:42 -07002189 /* If running an init program, let it decide when/how to mount /proc. */
2190 if (pid_namespace && !do_init)
2191 j->flags.remount_proc_ro = 0;
2192
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002193 if (use_preload) {
2194 /* Strip out flags that cannot be inherited across execve(2). */
2195 minijail_preexec(j);
2196 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002197 /*
2198 * If not using LD_PRELOAD, do all jailing before execve(2).
2199 * Note that PID namespaces can only be entered on fork(2),
2200 * so that flag is still cleared.
2201 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002202 j->flags.pids = 0;
2203 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002204 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002205 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002206
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002207 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002208 /*
2209 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002210 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002211 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002212 * a child to actually run the program. If |do_init == 0|, we
2213 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002214 *
2215 * If we're multithreaded, we'll probably deadlock here. See
2216 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002217 */
2218 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002219 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002220 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002221 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002222 /*
2223 * Best effort. Don't bother checking the return value.
2224 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002225 prctl(PR_SET_NAME, "minijail-init");
2226 init(child_pid); /* Never returns. */
2227 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002228 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002229
Elly Jonesdd3e8512012-01-23 15:13:38 -05002230 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002231 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002232 * calling process
2233 * -> execve()-ing process
2234 * If we are:
2235 * calling process
2236 * -> init()-ing process
2237 * -> execve()-ing process
2238 */
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002239 ret = execve(filename, argv, environ);
2240 if (ret == -1) {
2241 pwarn("execve(%s) failed", filename);
2242 }
2243 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002244}
2245
Will Drewry6ac91122011-10-21 16:38:58 -05002246int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002247{
2248 int st;
2249 if (kill(j->initpid, SIGTERM))
2250 return -errno;
2251 if (waitpid(j->initpid, &st, 0) < 0)
2252 return -errno;
2253 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002254}
2255
Will Drewry6ac91122011-10-21 16:38:58 -05002256int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002257{
2258 int st;
2259 if (waitpid(j->initpid, &st, 0) < 0)
2260 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002261
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002262 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002263 int error_status = st;
2264 if (WIFSIGNALED(st)) {
2265 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002266 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002267 j->initpid, signum);
2268 /*
2269 * We return MINIJAIL_ERR_JAIL if the process received
2270 * SIGSYS, which happens when a syscall is blocked by
2271 * seccomp filters.
2272 * If not, we do what bash(1) does:
2273 * $? = 128 + signum
2274 */
2275 if (signum == SIGSYS) {
2276 error_status = MINIJAIL_ERR_JAIL;
2277 } else {
2278 error_status = 128 + signum;
2279 }
2280 }
2281 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002282 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002283
2284 int exit_status = WEXITSTATUS(st);
2285 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002286 info("child process %d exited with status %d",
2287 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002288
2289 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002290}
2291
Will Drewry6ac91122011-10-21 16:38:58 -05002292void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002293{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002294 size_t i;
2295
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002296 if (j->flags.seccomp_filter && j->filter_prog) {
2297 free(j->filter_prog->filter);
2298 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002299 }
Dylan Reid648b2202015-10-23 00:50:00 -07002300 while (j->mounts_head) {
2301 struct mountpoint *m = j->mounts_head;
2302 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002303 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002304 free(m->type);
2305 free(m->dest);
2306 free(m->src);
2307 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002308 }
Dylan Reid648b2202015-10-23 00:50:00 -07002309 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002310 if (j->user)
2311 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002312 if (j->suppl_gid_list)
2313 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002314 if (j->chrootdir)
2315 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002316 if (j->pid_file_path)
2317 free(j->pid_file_path);
2318 if (j->uidmap)
2319 free(j->uidmap);
2320 if (j->gidmap)
2321 free(j->gidmap);
Andrew Brestickereac28942015-11-11 16:04:46 -08002322 if (j->alt_syscall_table)
2323 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002324 for (i = 0; i < j->cgroup_count; ++i)
2325 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002326 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002327}