blob: f2a937b89afd65b0d87ca26f9ed38f631a1a2be1 [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>
Luis Hector Chavez43ff0802016-10-07 12:21:07 -070011#include <dirent.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040012#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070013#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040014#include <grp.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040015#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040016#include <sched.h>
17#include <signal.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070018#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080019#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040023#include <sys/capability.h>
24#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050025#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040026#include <sys/prctl.h>
Dylan Reid0f72ef42017-06-06 15:42:49 -070027#include <sys/resource.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070028#include <sys/stat.h>
29#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080030#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040031#include <sys/wait.h>
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -070032#include <syscall.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040033#include <unistd.h>
34
35#include "libminijail.h"
36#include "libminijail-private.h"
37
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070038#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080039#include "syscall_filter.h"
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040040#include "syscall_wrapper.h"
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040041#include "system.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070042#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080043
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070044/* Until these are reliably available in linux/prctl.h. */
Andrew Brestickereac28942015-11-11 16:04:46 -080045#ifndef PR_ALT_SYSCALL
46# define PR_ALT_SYSCALL 0x43724f53
47#endif
48
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040049/* Seccomp filter related flags. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080050#ifndef PR_SET_NO_NEW_PRIVS
51# define PR_SET_NO_NEW_PRIVS 38
52#endif
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040053
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080054#ifndef SECCOMP_MODE_FILTER
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040055#define SECCOMP_MODE_FILTER 2 /* Uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050056#endif
57
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040058#ifndef SECCOMP_SET_MODE_STRICT
59# define SECCOMP_SET_MODE_STRICT 0
60#endif
61#ifndef SECCOMP_SET_MODE_FILTER
62# define SECCOMP_SET_MODE_FILTER 1
63#endif
64
65#ifndef SECCOMP_FILTER_FLAG_TSYNC
66# define SECCOMP_FILTER_FLAG_TSYNC 1
67#endif
68/* End seccomp filter related flags. */
69
Dylan Reid4cbc2a52016-06-17 19:06:07 -070070/* New cgroup namespace might not be in linux-headers yet. */
71#ifndef CLONE_NEWCGROUP
72# define CLONE_NEWCGROUP 0x02000000
73#endif
74
Dylan Reid605ce7f2016-01-19 19:21:00 -080075#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
76
Dylan Reid0f72ef42017-06-06 15:42:49 -070077#define MAX_RLIMITS 32 /* Currently there are 15 supported by Linux. */
78
Luis Hector Chavez1617f632017-08-01 18:32:30 -070079#define MAX_PRESERVED_FDS 10
80
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -080081/* Keyctl commands. */
82#define KEYCTL_JOIN_SESSION_KEYRING 1
83
Dylan Reid0f72ef42017-06-06 15:42:49 -070084struct minijail_rlimit {
85 int type;
86 uint32_t cur;
87 uint32_t max;
88};
89
Dylan Reid648b2202015-10-23 00:50:00 -070090struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040091 char *src;
92 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070093 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -070094 char *data;
95 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -070096 unsigned long flags;
97 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040098};
99
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700100struct hook {
101 minijail_hook_t hook;
102 void *payload;
103 minijail_hook_event_t event;
104 struct hook *next;
105};
106
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700107struct preserved_fd {
108 int parent_fd;
109 int child_fd;
110};
111
Will Drewryf89aef52011-09-16 16:48:57 -0500112struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700113 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700114 * WARNING: if you add a flag here you need to make sure it's
115 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700116 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400117 struct {
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700118 int uid : 1;
119 int gid : 1;
Lutz Justen13807cb2017-01-03 17:11:55 +0100120 int inherit_suppl_gids : 1;
121 int set_suppl_gids : 1;
122 int keep_suppl_gids : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700123 int use_caps : 1;
124 int capbset_drop : 1;
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400125 int set_ambient_caps : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700126 int vfs : 1;
127 int enter_vfs : 1;
128 int skip_remount_private : 1;
129 int pids : 1;
130 int ipc : 1;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400131 int uts : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700132 int net : 1;
133 int enter_net : 1;
134 int ns_cgroups : 1;
135 int userns : 1;
136 int disable_setgroups : 1;
137 int seccomp : 1;
138 int remount_proc_ro : 1;
139 int no_new_privs : 1;
140 int seccomp_filter : 1;
141 int seccomp_filter_tsync : 1;
142 int seccomp_filter_logging : 1;
143 int chroot : 1;
144 int pivot_root : 1;
145 int mount_tmp : 1;
146 int do_init : 1;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700147 int run_as_init : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700148 int pid_file : 1;
149 int cgroups : 1;
150 int alt_syscall : 1;
151 int reset_signal_mask : 1;
152 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800153 int new_session_keyring : 1;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400154 int forward_signals : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400155 } flags;
156 uid_t uid;
157 gid_t gid;
158 gid_t usergid;
159 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800160 size_t suppl_gid_count;
161 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400162 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800163 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400164 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700165 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700166 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400167 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800168 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800169 char *uidmap;
170 char *gidmap;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400171 char *hostname;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800172 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800173 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800174 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700175 struct mountpoint *mounts_head;
176 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800177 size_t mounts_count;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100178 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800179 char *cgroups[MAX_CGROUPS];
180 size_t cgroup_count;
Dylan Reid0f72ef42017-06-06 15:42:49 -0700181 struct minijail_rlimit rlimits[MAX_RLIMITS];
182 size_t rlimit_count;
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700183 uint64_t securebits_skip_mask;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700184 struct hook *hooks_head;
185 struct hook *hooks_tail;
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700186 struct preserved_fd preserved_fds[MAX_PRESERVED_FDS];
187 size_t preserved_fd_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500188};
189
Luis Hector Chavez64730af2017-09-13 13:18:59 -0700190static void run_hooks_or_die(const struct minijail *j,
191 minijail_hook_event_t event);
192
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700193/*
194 * Strip out flags meant for the parent.
195 * We keep things that are not inherited across execve(2) (e.g. capabilities),
196 * or are easier to set after execve(2) (e.g. seccomp filters).
197 */
198void minijail_preenter(struct minijail *j)
199{
200 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700201 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900202 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700203 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700204 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800205 j->flags.do_init = 0;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700206 j->flags.run_as_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800207 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800208 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400209 j->flags.forward_signals = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700210}
211
212/*
213 * Strip out flags meant for the child.
214 * We keep things that are inherited across execve(2).
215 */
216void minijail_preexec(struct minijail *j)
217{
218 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700219 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800220 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700221 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800222 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700223 if (j->user)
224 free(j->user);
225 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800226 if (j->suppl_gid_list)
227 free(j->suppl_gid_list);
228 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700229 memset(&j->flags, 0, sizeof(j->flags));
230 /* Now restore anything we meant to keep. */
231 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700232 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800233 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700234 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800235 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700236 /* Note, |pids| will already have been used before this call. */
237}
238
239/* Minijail API. */
240
Will Drewry6ac91122011-10-21 16:38:58 -0500241struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400242{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400243 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400244}
245
Will Drewry6ac91122011-10-21 16:38:58 -0500246void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400247{
248 if (uid == 0)
249 die("useless change to uid 0");
250 j->uid = uid;
251 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400252}
253
Will Drewry6ac91122011-10-21 16:38:58 -0500254void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400255{
256 if (gid == 0)
257 die("useless change to gid 0");
258 j->gid = gid;
259 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400260}
261
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800262void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
263 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800264{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800265 size_t i;
266
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500267 if (j->flags.inherit_suppl_gids)
268 die("cannot inherit *and* set supplementary groups");
269 if (j->flags.keep_suppl_gids)
270 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800271
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800272 if (size == 0) {
273 /* Clear supplementary groups. */
274 j->suppl_gid_list = NULL;
275 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100276 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800277 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800278 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800279
280 /* Copy the gid_t array. */
281 j->suppl_gid_list = calloc(size, sizeof(gid_t));
282 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800283 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800284 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800285 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800286 j->suppl_gid_list[i] = list[i];
287 }
288 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100289 j->flags.set_suppl_gids = 1;
290}
291
292void API minijail_keep_supplementary_gids(struct minijail *j) {
293 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800294}
295
Will Drewry6ac91122011-10-21 16:38:58 -0500296int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400297{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700298 uid_t uid;
299 gid_t gid;
300 int rc = lookup_user(user, &uid, &gid);
301 if (rc)
302 return rc;
303 minijail_change_uid(j, uid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400304 j->user = strdup(user);
305 if (!j->user)
306 return -ENOMEM;
Luis Hector Chavez71323552017-09-05 09:17:22 -0700307 j->usergid = gid;
Elly Jonese1749eb2011-10-07 13:54:59 -0400308 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400309}
310
Will Drewry6ac91122011-10-21 16:38:58 -0500311int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400312{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700313 gid_t gid;
314 int rc = lookup_group(group, &gid);
315 if (rc)
316 return rc;
317 minijail_change_gid(j, gid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400318 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400319}
320
Will Drewry6ac91122011-10-21 16:38:58 -0500321void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400322{
323 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400324}
325
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700326void API minijail_no_new_privs(struct minijail *j)
327{
328 j->flags.no_new_privs = 1;
329}
330
Will Drewry6ac91122011-10-21 16:38:58 -0500331void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400332{
333 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500334}
335
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400336void API minijail_set_seccomp_filter_tsync(struct minijail *j)
337{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400338 if (j->filter_len > 0 && j->filter_prog != NULL) {
339 die("minijail_set_seccomp_filter_tsync() must be called "
340 "before minijail_parse_seccomp_filters()");
341 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400342 j->flags.seccomp_filter_tsync = 1;
343}
344
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700345void API minijail_log_seccomp_filter_failures(struct minijail *j)
346{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400347 if (j->filter_len > 0 && j->filter_prog != NULL) {
348 die("minijail_log_seccomp_filter_failures() must be called "
349 "before minijail_parse_seccomp_filters()");
350 }
351 j->flags.seccomp_filter_logging = 1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700352}
353
Will Drewry6ac91122011-10-21 16:38:58 -0500354void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400355{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800356 /*
357 * 'minijail_use_caps' configures a runtime-capabilities-only
358 * environment, including a bounding set matching the thread's runtime
359 * (permitted|inheritable|effective) sets.
360 * Therefore, it will override any existing bounding set configurations
361 * since the latter would allow gaining extra runtime capabilities from
362 * file capabilities.
363 */
364 if (j->flags.capbset_drop) {
365 warn("overriding bounding set configuration");
366 j->cap_bset = 0;
367 j->flags.capbset_drop = 0;
368 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400369 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800370 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400371}
372
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800373void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
374{
375 if (j->flags.use_caps) {
376 /*
377 * 'minijail_use_caps' will have already configured a capability
378 * bounding set matching the (permitted|inheritable|effective)
379 * sets. Abort if the user tries to configure a separate
380 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
381 * are mutually exclusive.
382 */
383 die("runtime capabilities already configured, can't drop "
384 "bounding set separately");
385 }
386 j->cap_bset = capmask;
387 j->flags.capbset_drop = 1;
388}
389
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400390void API minijail_set_ambient_caps(struct minijail *j)
391{
392 j->flags.set_ambient_caps = 1;
393}
394
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800395void API minijail_reset_signal_mask(struct minijail *j)
396{
Peter Qiu2860c462015-12-16 15:13:06 -0800397 j->flags.reset_signal_mask = 1;
398}
399
Will Drewry6ac91122011-10-21 16:38:58 -0500400void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400401{
402 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400403}
404
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700405void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
406{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800407 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700408 if (ns_fd < 0) {
409 pdie("failed to open namespace '%s'", ns_path);
410 }
411 j->mountns_fd = ns_fd;
412 j->flags.enter_vfs = 1;
413}
414
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800415void API minijail_new_session_keyring(struct minijail *j)
416{
417 j->flags.new_session_keyring = 1;
418}
419
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700420void API minijail_skip_setting_securebits(struct minijail *j,
421 uint64_t securebits_skip_mask)
422{
423 j->securebits_skip_mask = securebits_skip_mask;
424}
425
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800426void API minijail_skip_remount_private(struct minijail *j)
427{
428 j->flags.skip_remount_private = 1;
429}
430
Will Drewry6ac91122011-10-21 16:38:58 -0500431void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400432{
Elly Jonese58176c2012-01-23 11:46:17 -0500433 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700434 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400435 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800436 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400437}
438
Dylan Reidf7942472015-11-18 17:55:26 -0800439void API minijail_namespace_ipc(struct minijail *j)
440{
441 j->flags.ipc = 1;
442}
443
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400444void API minijail_namespace_uts(struct minijail *j)
445{
446 j->flags.uts = 1;
447}
448
449int API minijail_namespace_set_hostname(struct minijail *j, const char *name)
450{
451 if (j->hostname)
452 return -EINVAL;
453 minijail_namespace_uts(j);
454 j->hostname = strdup(name);
455 if (!j->hostname)
456 return -ENOMEM;
457 return 0;
458}
459
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400460void API minijail_namespace_net(struct minijail *j)
461{
462 j->flags.net = 1;
463}
464
Dylan Reid1102f5a2015-09-15 11:52:20 -0700465void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
466{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800467 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700468 if (ns_fd < 0) {
469 pdie("failed to open namespace '%s'", ns_path);
470 }
471 j->netns_fd = ns_fd;
472 j->flags.enter_net = 1;
473}
474
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700475void API minijail_namespace_cgroups(struct minijail *j)
476{
477 j->flags.ns_cgroups = 1;
478}
479
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700480void API minijail_close_open_fds(struct minijail *j)
481{
482 j->flags.close_open_fds = 1;
483}
484
Dylan Reid791f5772015-09-14 20:02:42 -0700485void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400486{
487 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700488 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400489}
490
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800491void API minijail_namespace_user(struct minijail *j)
492{
493 j->flags.userns = 1;
494}
495
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400496void API minijail_namespace_user_disable_setgroups(struct minijail *j)
497{
498 j->flags.disable_setgroups = 1;
499}
500
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800501int API minijail_uidmap(struct minijail *j, const char *uidmap)
502{
503 j->uidmap = strdup(uidmap);
504 if (!j->uidmap)
505 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800506 char *ch;
507 for (ch = j->uidmap; *ch; ch++) {
508 if (*ch == ',')
509 *ch = '\n';
510 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800511 return 0;
512}
513
514int API minijail_gidmap(struct minijail *j, const char *gidmap)
515{
516 j->gidmap = strdup(gidmap);
517 if (!j->gidmap)
518 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800519 char *ch;
520 for (ch = j->gidmap; *ch; ch++) {
521 if (*ch == ',')
522 *ch = '\n';
523 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800524 return 0;
525}
526
Will Drewry6ac91122011-10-21 16:38:58 -0500527void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400528{
Lutz Justen13807cb2017-01-03 17:11:55 +0100529 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400530}
531
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800532void API minijail_run_as_init(struct minijail *j)
533{
534 /*
535 * Since the jailed program will become 'init' in the new PID namespace,
536 * Minijail does not need to fork an 'init' process.
537 */
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700538 j->flags.run_as_init = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800539}
540
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700541int API minijail_enter_chroot(struct minijail *j, const char *dir)
542{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400543 if (j->chrootdir)
544 return -EINVAL;
545 j->chrootdir = strdup(dir);
546 if (!j->chrootdir)
547 return -ENOMEM;
548 j->flags.chroot = 1;
549 return 0;
550}
551
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800552int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
553{
554 if (j->chrootdir)
555 return -EINVAL;
556 j->chrootdir = strdup(dir);
557 if (!j->chrootdir)
558 return -ENOMEM;
559 j->flags.pivot_root = 1;
560 return 0;
561}
562
Dylan Reida14e08d2015-10-22 21:05:29 -0700563char API *minijail_get_original_path(struct minijail *j,
564 const char *path_inside_chroot)
565{
Dylan Reid648b2202015-10-23 00:50:00 -0700566 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700567
Dylan Reid648b2202015-10-23 00:50:00 -0700568 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700569 while (b) {
570 /*
571 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700572 * mount, then the original path is exactly the source of
573 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700574 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700575 * mount source = /some/path/exe, mount dest =
576 * /chroot/path/exe Then when getting the original path of
577 * "/chroot/path/exe", the source of that mount,
578 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700579 */
580 if (!strcmp(b->dest, path_inside_chroot))
581 return strdup(b->src);
582
583 /*
584 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700585 * mount, take the suffix of the chroot path relative to the
586 * mount destination path, and append it to the mount source
587 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700588 */
589 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
590 const char *relative_path =
591 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400592 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700593 }
594 b = b->next;
595 }
596
597 /* If there is a chroot path, append |path_inside_chroot| to that. */
598 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400599 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700600
601 /* No chroot, so the path outside is the same as it is inside. */
602 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700603}
604
Martin Pelikánab9eb442017-01-25 11:53:58 +1100605size_t minijail_get_tmpfs_size(const struct minijail *j)
606{
607 return j->tmpfs_size;
608}
609
Lee Campbell11af0622014-05-22 12:36:04 -0700610void API minijail_mount_tmp(struct minijail *j)
611{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100612 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
613}
614
615void API minijail_mount_tmp_size(struct minijail *j, size_t size)
616{
617 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700618 j->flags.mount_tmp = 1;
619}
620
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800621int API minijail_write_pid_file(struct minijail *j, const char *path)
622{
623 j->pid_file_path = strdup(path);
624 if (!j->pid_file_path)
625 return -ENOMEM;
626 j->flags.pid_file = 1;
627 return 0;
628}
629
Dylan Reid605ce7f2016-01-19 19:21:00 -0800630int API minijail_add_to_cgroup(struct minijail *j, const char *path)
631{
632 if (j->cgroup_count >= MAX_CGROUPS)
633 return -ENOMEM;
634 j->cgroups[j->cgroup_count] = strdup(path);
635 if (!j->cgroups[j->cgroup_count])
636 return -ENOMEM;
637 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800638 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800639 return 0;
640}
641
Dylan Reid0f72ef42017-06-06 15:42:49 -0700642int API minijail_rlimit(struct minijail *j, int type, uint32_t cur,
643 uint32_t max)
644{
645 size_t i;
646
647 if (j->rlimit_count >= MAX_RLIMITS)
648 return -ENOMEM;
649 /* It's an error if the caller sets the same rlimit multiple times. */
650 for (i = 0; i < j->rlimit_count; i++) {
651 if (j->rlimits[i].type == type)
652 return -EEXIST;
653 }
654
655 j->rlimits[j->rlimit_count].type = type;
656 j->rlimits[j->rlimit_count].cur = cur;
657 j->rlimits[j->rlimit_count].max = max;
658 j->rlimit_count++;
659 return 0;
660}
661
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400662int API minijail_forward_signals(struct minijail *j)
663{
664 j->flags.forward_signals = 1;
665 return 0;
666}
667
Dylan Reid81e23972016-05-18 14:06:35 -0700668int API minijail_mount_with_data(struct minijail *j, const char *src,
669 const char *dest, const char *type,
670 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700671{
Dylan Reid648b2202015-10-23 00:50:00 -0700672 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400673
674 if (*dest != '/')
675 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700676 m = calloc(1, sizeof(*m));
677 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400678 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700679 m->dest = strdup(dest);
680 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400681 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700682 m->src = strdup(src);
683 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400684 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700685 m->type = strdup(type);
686 if (!m->type)
687 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700688 if (data) {
689 m->data = strdup(data);
690 if (!m->data)
691 goto error;
692 m->has_data = 1;
693 }
Dylan Reid648b2202015-10-23 00:50:00 -0700694 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400695
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800696 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400697
Elly Jonesdd3e8512012-01-23 15:13:38 -0500698 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700699 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400700 * containing vfs namespace.
701 */
702 minijail_namespace_vfs(j);
703
Dylan Reid648b2202015-10-23 00:50:00 -0700704 if (j->mounts_tail)
705 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400706 else
Dylan Reid648b2202015-10-23 00:50:00 -0700707 j->mounts_head = m;
708 j->mounts_tail = m;
709 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400710
711 return 0;
712
713error:
Dylan Reid81e23972016-05-18 14:06:35 -0700714 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700715 free(m->src);
716 free(m->dest);
717 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400718 return -ENOMEM;
719}
720
Dylan Reid81e23972016-05-18 14:06:35 -0700721int API minijail_mount(struct minijail *j, const char *src, const char *dest,
722 const char *type, unsigned long flags)
723{
724 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
725}
726
Dylan Reid648b2202015-10-23 00:50:00 -0700727int API minijail_bind(struct minijail *j, const char *src, const char *dest,
728 int writeable)
729{
730 unsigned long flags = MS_BIND;
731
732 if (!writeable)
733 flags |= MS_RDONLY;
734
735 return minijail_mount(j, src, dest, "", flags);
736}
737
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700738int API minijail_add_hook(struct minijail *j, minijail_hook_t hook,
739 void *payload, minijail_hook_event_t event)
740{
741 struct hook *c;
742
743 if (hook == NULL)
744 return -EINVAL;
745 if (event >= MINIJAIL_HOOK_EVENT_MAX)
746 return -EINVAL;
747 c = calloc(1, sizeof(*c));
748 if (!c)
749 return -ENOMEM;
750
751 c->hook = hook;
752 c->payload = payload;
753 c->event = event;
754
755 if (j->hooks_tail)
756 j->hooks_tail->next = c;
757 else
758 j->hooks_head = c;
759 j->hooks_tail = c;
760
761 return 0;
762}
763
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700764int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd)
765{
766 if (parent_fd < 0 || child_fd < 0)
767 return -EINVAL;
768 if (j->preserved_fd_count >= MAX_PRESERVED_FDS)
769 return -ENOMEM;
770 j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd;
771 j->preserved_fds[j->preserved_fd_count].child_fd = child_fd;
772 j->preserved_fd_count++;
773 return 0;
774}
775
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400776static void clear_seccomp_options(struct minijail *j)
777{
778 j->flags.seccomp_filter = 0;
779 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400780 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400781 j->filter_len = 0;
782 j->filter_prog = NULL;
783 j->flags.no_new_privs = 0;
784}
785
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400786static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400787{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400788 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400789 /*
790 * |errno| will be set to EINVAL when seccomp has not been
791 * compiled into the kernel. On certain platforms and kernel
792 * versions this is not a fatal failure. In that case, and only
793 * in that case, disable seccomp and skip loading the filters.
794 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400795 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400796 warn("not loading seccomp filters, seccomp filter not "
797 "supported");
798 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400799 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700800 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400801 /*
802 * If |errno| != EINVAL or seccomp_can_softfail() is false,
803 * we can proceed. Worst case scenario minijail_enter() will
804 * abort() if seccomp fails.
805 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700806 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400807 if (j->flags.seccomp_filter_tsync) {
808 /* Are the seccomp(2) syscall and the TSYNC option supported? */
809 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
810 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
811 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400812 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
813 warn("seccomp(2) syscall not supported");
814 clear_seccomp_options(j);
815 return 0;
816 } else if (saved_errno == EINVAL &&
817 seccomp_can_softfail()) {
818 warn(
819 "seccomp filter thread sync not supported");
820 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400821 return 0;
822 }
823 /*
824 * Similar logic here. If seccomp_can_softfail() is
825 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
826 * we can proceed. Worst case scenario minijail_enter()
827 * will abort() if seccomp or TSYNC fail.
828 */
829 }
830 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400831 return 1;
832}
833
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700834static int parse_seccomp_filters(struct minijail *j, const char *filename,
835 FILE *policy_file)
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400836{
837 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400838 int use_ret_trap =
839 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
840 int allow_logging = j->flags.seccomp_filter_logging;
841
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700842 if (compile_filter(filename, policy_file, fprog, use_ret_trap,
843 allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400844 free(fprog);
845 return -1;
846 }
847
848 j->filter_len = fprog->len;
849 j->filter_prog = fprog;
850 return 0;
851}
852
853void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
854{
855 if (!seccomp_should_parse_filters(j))
856 return;
857
Elly Jonese1749eb2011-10-07 13:54:59 -0400858 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800859 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700860 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400861 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800862
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700863 if (parse_seccomp_filters(j, path, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700864 die("failed to compile seccomp filter BPF program in '%s'",
865 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800866 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400867 fclose(file);
868}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800869
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400870void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
871{
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700872 char *fd_path, *path;
873 FILE *file;
874
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400875 if (!seccomp_should_parse_filters(j))
876 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800877
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700878 file = fdopen(fd, "r");
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400879 if (!file) {
880 pdie("failed to associate stream with fd %d", fd);
881 }
882
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700883 if (asprintf(&fd_path, "/proc/self/fd/%d", fd) == -1)
884 pdie("failed to create path for fd %d", fd);
885 path = realpath(fd_path, NULL);
886 if (path == NULL)
887 pwarn("failed to get path of fd %d", fd);
888 free(fd_path);
889
890 if (parse_seccomp_filters(j, path ? path : "<fd>", file) != 0) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400891 die("failed to compile seccomp filter BPF program from fd %d",
892 fd);
893 }
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700894 free(path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400895 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500896}
897
Andrew Brestickereac28942015-11-11 16:04:46 -0800898int API minijail_use_alt_syscall(struct minijail *j, const char *table)
899{
900 j->alt_syscall_table = strdup(table);
901 if (!j->alt_syscall_table)
902 return -ENOMEM;
903 j->flags.alt_syscall = 1;
904 return 0;
905}
906
Will Drewryf89aef52011-09-16 16:48:57 -0500907struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400908 size_t available;
909 size_t total;
910 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500911};
912
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800913void marshal_state_init(struct marshal_state *state, char *buf,
914 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400915{
916 state->available = available;
917 state->buf = buf;
918 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500919}
920
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800921void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400922{
923 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500924
Elly Jonese1749eb2011-10-07 13:54:59 -0400925 /* Up to |available| will be written. */
926 if (copy_len) {
927 memcpy(state->buf, src, copy_len);
928 state->buf += copy_len;
929 state->available -= copy_len;
930 }
931 /* |total| will contain the expected length. */
932 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500933}
934
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400935void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700936{
937 marshal_append(state, m->src, strlen(m->src) + 1);
938 marshal_append(state, m->dest, strlen(m->dest) + 1);
939 marshal_append(state, m->type, strlen(m->type) + 1);
940 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
941 if (m->has_data)
942 marshal_append(state, m->data, strlen(m->data) + 1);
943 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
944}
945
Will Drewry6ac91122011-10-21 16:38:58 -0500946void minijail_marshal_helper(struct marshal_state *state,
947 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400948{
Dylan Reid648b2202015-10-23 00:50:00 -0700949 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800950 size_t i;
951
Elly Jonese1749eb2011-10-07 13:54:59 -0400952 marshal_append(state, (char *)j, sizeof(*j));
953 if (j->user)
954 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800955 if (j->suppl_gid_list) {
956 marshal_append(state, j->suppl_gid_list,
957 j->suppl_gid_count * sizeof(gid_t));
958 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400959 if (j->chrootdir)
960 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400961 if (j->hostname)
962 marshal_append(state, j->hostname, strlen(j->hostname) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800963 if (j->alt_syscall_table) {
964 marshal_append(state, j->alt_syscall_table,
965 strlen(j->alt_syscall_table) + 1);
966 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800967 if (j->flags.seccomp_filter && j->filter_prog) {
968 struct sock_fprog *fp = j->filter_prog;
969 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800970 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400971 }
Dylan Reid648b2202015-10-23 00:50:00 -0700972 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400973 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400974 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800975 for (i = 0; i < j->cgroup_count; ++i)
976 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500977}
978
Will Drewry6ac91122011-10-21 16:38:58 -0500979size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400980{
981 struct marshal_state state;
982 marshal_state_init(&state, NULL, 0);
983 minijail_marshal_helper(&state, j);
984 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500985}
986
Elly Jonese1749eb2011-10-07 13:54:59 -0400987int minijail_marshal(const struct minijail *j, char *buf, size_t available)
988{
989 struct marshal_state state;
990 marshal_state_init(&state, buf, available);
991 minijail_marshal_helper(&state, j);
992 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500993}
994
Elly Jonese1749eb2011-10-07 13:54:59 -0400995int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
996{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800997 size_t i;
998 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500999 int ret = -EINVAL;
1000
Elly Jonese1749eb2011-10-07 13:54:59 -04001001 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001002 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001003 memcpy((void *)j, serialized, sizeof(*j));
1004 serialized += sizeof(*j);
1005 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001006
Will Drewrybee7ba72011-10-21 20:47:01 -05001007 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001008 j->pid_file_path = NULL;
1009 j->uidmap = NULL;
1010 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001011 j->mounts_head = NULL;
1012 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001013 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001014 j->hooks_head = NULL;
1015 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001016
Elly Jonese1749eb2011-10-07 13:54:59 -04001017 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001018 char *user = consumestr(&serialized, &length);
1019 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001020 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001021 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001022 if (!j->user)
1023 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001024 }
Will Drewryf89aef52011-09-16 16:48:57 -05001025
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001026 if (j->suppl_gid_list) { /* stale pointer */
1027 if (j->suppl_gid_count > NGROUPS_MAX) {
1028 goto bad_gid_list;
1029 }
1030 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1031 void *gid_list_bytes =
1032 consumebytes(gid_list_size, &serialized, &length);
1033 if (!gid_list_bytes)
1034 goto bad_gid_list;
1035
1036 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1037 if (!j->suppl_gid_list)
1038 goto bad_gid_list;
1039
1040 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1041 }
1042
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001043 if (j->chrootdir) { /* stale pointer */
1044 char *chrootdir = consumestr(&serialized, &length);
1045 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001046 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001047 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001048 if (!j->chrootdir)
1049 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001050 }
1051
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001052 if (j->hostname) { /* stale pointer */
1053 char *hostname = consumestr(&serialized, &length);
1054 if (!hostname)
1055 goto bad_hostname;
1056 j->hostname = strdup(hostname);
1057 if (!j->hostname)
1058 goto bad_hostname;
1059 }
1060
Andrew Brestickereac28942015-11-11 16:04:46 -08001061 if (j->alt_syscall_table) { /* stale pointer */
1062 char *alt_syscall_table = consumestr(&serialized, &length);
1063 if (!alt_syscall_table)
1064 goto bad_syscall_table;
1065 j->alt_syscall_table = strdup(alt_syscall_table);
1066 if (!j->alt_syscall_table)
1067 goto bad_syscall_table;
1068 }
1069
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001070 if (j->flags.seccomp_filter && j->filter_len > 0) {
1071 size_t ninstrs = j->filter_len;
1072 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1073 ninstrs > USHRT_MAX)
1074 goto bad_filters;
1075
1076 size_t program_len = ninstrs * sizeof(struct sock_filter);
1077 void *program = consumebytes(program_len, &serialized, &length);
1078 if (!program)
1079 goto bad_filters;
1080
1081 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001082 if (!j->filter_prog)
1083 goto bad_filters;
1084
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001085 j->filter_prog->len = ninstrs;
1086 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001087 if (!j->filter_prog->filter)
1088 goto bad_filter_prog_instrs;
1089
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001090 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001091 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001092
Dylan Reid648b2202015-10-23 00:50:00 -07001093 count = j->mounts_count;
1094 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001095 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001096 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001097 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001098 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001099 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001100 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001101 const char *src = consumestr(&serialized, &length);
1102 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001103 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001104 dest = consumestr(&serialized, &length);
1105 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001106 goto bad_mounts;
1107 type = consumestr(&serialized, &length);
1108 if (!type)
1109 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001110 has_data = consumebytes(sizeof(*has_data), &serialized,
1111 &length);
1112 if (!has_data)
1113 goto bad_mounts;
1114 if (*has_data) {
1115 data = consumestr(&serialized, &length);
1116 if (!data)
1117 goto bad_mounts;
1118 }
Dylan Reid648b2202015-10-23 00:50:00 -07001119 flags = consumebytes(sizeof(*flags), &serialized, &length);
1120 if (!flags)
1121 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001122 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001123 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001124 }
1125
Dylan Reid605ce7f2016-01-19 19:21:00 -08001126 count = j->cgroup_count;
1127 j->cgroup_count = 0;
1128 for (i = 0; i < count; ++i) {
1129 char *cgroup = consumestr(&serialized, &length);
1130 if (!cgroup)
1131 goto bad_cgroups;
1132 j->cgroups[i] = strdup(cgroup);
1133 if (!j->cgroups[i])
1134 goto bad_cgroups;
1135 ++j->cgroup_count;
1136 }
1137
Elly Jonese1749eb2011-10-07 13:54:59 -04001138 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001139
Dylan Reid605ce7f2016-01-19 19:21:00 -08001140bad_cgroups:
1141 while (j->mounts_head) {
1142 struct mountpoint *m = j->mounts_head;
1143 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07001144 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001145 free(m->type);
1146 free(m->dest);
1147 free(m->src);
1148 free(m);
1149 }
1150 for (i = 0; i < j->cgroup_count; ++i)
1151 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001152bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001153 if (j->flags.seccomp_filter && j->filter_len > 0) {
1154 free(j->filter_prog->filter);
1155 free(j->filter_prog);
1156 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001157bad_filter_prog_instrs:
1158 if (j->filter_prog)
1159 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001160bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001161 if (j->alt_syscall_table)
1162 free(j->alt_syscall_table);
1163bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001164 if (j->chrootdir)
1165 free(j->chrootdir);
1166bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001167 if (j->hostname)
1168 free(j->hostname);
1169bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001170 if (j->suppl_gid_list)
1171 free(j->suppl_gid_list);
1172bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001173 if (j->user)
1174 free(j->user);
1175clear_pointers:
1176 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001177 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001178 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001179 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001180 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001181 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001182out:
1183 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001184}
1185
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001186/*
1187 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001188 * @j Minijail these mounts are for
1189 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001190 *
1191 * Returns 0 for success.
1192 */
Dylan Reid648b2202015-10-23 00:50:00 -07001193static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001194{
Dylan Reid648b2202015-10-23 00:50:00 -07001195 int ret;
1196 char *dest;
1197 int remount_ro = 0;
1198
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001199 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001200 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001201 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001202
Mike Frysingereaab4202017-08-14 14:57:21 -04001203 if (setup_mount_destination(m->src, dest, j->uid, j->gid,
1204 (m->flags & MS_BIND)))
Dylan Reideec77962016-06-30 19:35:10 -07001205 pdie("creating mount target '%s' failed", dest);
1206
Dylan Reid648b2202015-10-23 00:50:00 -07001207 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001208 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1209 * can't both be specified in the original bind mount.
1210 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001211 */
1212 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1213 remount_ro = 1;
1214 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001215 }
Dylan Reid648b2202015-10-23 00:50:00 -07001216
Dylan Reid81e23972016-05-18 14:06:35 -07001217 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001218 if (ret)
1219 pdie("mount: %s -> %s", m->src, dest);
1220
1221 if (remount_ro) {
1222 m->flags |= MS_RDONLY;
1223 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001224 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001225 if (ret)
1226 pdie("bind ro: %s -> %s", m->src, dest);
1227 }
1228
Elly Jones51a5b6c2011-10-12 19:09:26 -04001229 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001230 if (m->next)
1231 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001232 return ret;
1233}
1234
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001235static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001236{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001237 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001238
1239 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001240 return ret;
1241
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001242 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1243
Elly Jones51a5b6c2011-10-12 19:09:26 -04001244 if (chroot(j->chrootdir))
1245 return -errno;
1246
1247 if (chdir("/"))
1248 return -errno;
1249
1250 return 0;
1251}
1252
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001253static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001254{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001255 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001256
1257 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001258 return ret;
1259
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001260 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1261
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001262 /*
1263 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001264 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001265 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001266 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001267 if (oldroot < 0)
1268 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001269 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001270 if (newroot < 0)
1271 pdie("failed to open %s for fchdir", j->chrootdir);
1272
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001273 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001274 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001275 * do a self bind mount.
1276 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001277 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1278 pdie("failed to bind mount '%s'", j->chrootdir);
1279 if (chdir(j->chrootdir))
1280 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001281 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001282 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001283
1284 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001285 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001286 * change to the old root and unmount it.
1287 */
1288 if (fchdir(oldroot))
1289 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001290
1291 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001292 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1293 * there could be a shared mount point under |oldroot|. In that case,
1294 * mounts under this shared mount point will be unmounted below, and
1295 * this unmounting will propagate to the original mount namespace
1296 * (because the mount point is shared). To prevent this unexpected
1297 * unmounting, remove these mounts from their peer groups by recursively
1298 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001299 */
1300 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001301 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001302 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001303 if (umount2(".", MNT_DETACH))
1304 pdie("umount(/)");
1305 /* Change back to the new root. */
1306 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001307 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001308 if (close(oldroot))
1309 return -errno;
1310 if (close(newroot))
1311 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001312 if (chroot("/"))
1313 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001314 /* Set correct CWD for getcwd(3). */
1315 if (chdir("/"))
1316 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001317
1318 return 0;
1319}
1320
Martin Pelikánab9eb442017-01-25 11:53:58 +11001321static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001322{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001323 const char fmt[] = "size=%zu,mode=1777";
1324 /* Count for the user storing ULLONG_MAX literally + extra space. */
1325 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1326 int ret;
1327
1328 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1329
1330 if (ret <= 0)
1331 pdie("tmpfs size spec error");
1332 else if ((size_t)ret >= sizeof(data))
1333 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001334 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001335 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001336}
1337
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001338static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001339{
1340 const char *kProcPath = "/proc";
1341 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001342 /*
1343 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001344 * /proc in our namespace, which means using MS_REMOUNT here would
1345 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001346 * namespace (!). Instead, remove their mount from our namespace lazily
1347 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001348 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001349 if (umount2(kProcPath, MNT_DETACH)) {
1350 /*
1351 * If we are in a new user namespace, umount(2) will fail.
1352 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1353 */
1354 if (j->flags.userns) {
1355 info("umount(/proc, MNT_DETACH) failed, "
1356 "this is expected when using user namespaces");
1357 } else {
1358 return -errno;
1359 }
1360 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001361 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001362 return -errno;
1363 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001364}
1365
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001366static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001367{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001368 kill(j->initpid, SIGKILL);
1369 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001370}
1371
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001372static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001373{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001374 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001375 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001376}
1377
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001378static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001379{
1380 size_t i;
1381
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001382 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001383 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001384 kill_child_and_die(j, "failed to add to cgroups");
1385 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001386}
1387
Dylan Reid0f72ef42017-06-06 15:42:49 -07001388static void set_rlimits_or_die(const struct minijail *j)
1389{
1390 size_t i;
1391
1392 for (i = 0; i < j->rlimit_count; ++i) {
1393 struct rlimit limit;
1394 limit.rlim_cur = j->rlimits[i].cur;
1395 limit.rlim_max = j->rlimits[i].max;
1396 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1397 kill_child_and_die(j, "failed to set rlimit");
1398 }
1399}
1400
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001401static void write_ugid_maps_or_die(const struct minijail *j)
1402{
1403 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1404 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001405 if (j->gidmap && j->flags.disable_setgroups) {
1406 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1407 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001408 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001409 if (ret == -ENOENT) {
1410 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1411 warn("could not disable setgroups(2)");
1412 } else
1413 kill_child_and_die(j, "failed to disable setgroups(2)");
1414 }
1415 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001416 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1417 kill_child_and_die(j, "failed to write gid_map");
1418}
1419
1420static void enter_user_namespace(const struct minijail *j)
1421{
Luis Hector Chavez71323552017-09-05 09:17:22 -07001422 int uid = j->flags.uid ? j->uid : 0;
1423 int gid = j->flags.gid ? j->gid : 0;
1424 if (j->gidmap && setresgid(gid, gid, gid)) {
1425 pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid,
1426 gid);
1427 }
1428 if (j->uidmap && setresuid(uid, uid, uid)) {
1429 pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid,
1430 uid);
1431 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001432}
1433
1434static void parent_setup_complete(int *pipe_fds)
1435{
1436 close(pipe_fds[0]);
1437 close(pipe_fds[1]);
1438}
1439
1440/*
1441 * wait_for_parent_setup: Called by the child process to wait for any
1442 * further parent-side setup to complete before continuing.
1443 */
1444static void wait_for_parent_setup(int *pipe_fds)
1445{
1446 char buf;
1447
1448 close(pipe_fds[1]);
1449
1450 /* Wait for parent to complete setup and close the pipe. */
1451 if (read(pipe_fds[0], &buf, 1) != 0)
1452 die("failed to sync with parent");
1453 close(pipe_fds[0]);
1454}
1455
1456static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001457{
Lutz Justen13807cb2017-01-03 17:11:55 +01001458 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1459 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001460 die("can only do one of inherit, keep, or set supplementary "
1461 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001462 }
1463
Lutz Justen13807cb2017-01-03 17:11:55 +01001464 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001465 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001466 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001467 } else if (j->flags.set_suppl_gids) {
1468 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001469 pdie("setgroups(suppl_gids) failed");
Luis Hector Chavez71323552017-09-05 09:17:22 -07001470 } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001471 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001472 * Only attempt to clear supplementary groups if we are changing
Luis Hector Chavez71323552017-09-05 09:17:22 -07001473 * users or groups, and if the caller did not request to disable
1474 * setgroups (used when entering a user namespace as a
1475 * non-privileged user).
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001476 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001477 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001478 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001479 }
1480
1481 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001482 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001483
1484 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001485 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001486}
1487
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001488static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1489{
1490 const uint64_t one = 1;
1491 unsigned int i;
1492 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1493 if (keep_mask & (one << i))
1494 continue;
1495 if (prctl(PR_CAPBSET_DROP, i))
1496 pdie("could not drop capability from bounding set");
1497 }
1498}
1499
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001500static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001501{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001502 if (!j->flags.use_caps)
1503 return;
1504
Elly Jonese1749eb2011-10-07 13:54:59 -04001505 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001506 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001507 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001508 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001509 unsigned int i;
1510 if (!caps)
1511 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001512 if (cap_clear(caps))
1513 die("can't clear caps");
1514
1515 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001516 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001517 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001518 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001519 flag[0] = i;
1520 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001521 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001522 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001523 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001524 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001525 die("can't add inheritable cap");
1526 }
1527 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001528 die("can't apply initial cleaned capset");
1529
1530 /*
1531 * Instead of dropping bounding set first, do it here in case
1532 * the caller had a more permissive bounding set which could
1533 * have been used above to raise a capability that wasn't already
1534 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1535 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001536 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001537
1538 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001539 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001540 flag[0] = CAP_SETPCAP;
1541 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1542 die("can't clear effective cap");
1543 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1544 die("can't clear permitted cap");
1545 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1546 die("can't clear inheritable cap");
1547 }
1548
1549 if (cap_set_proc(caps))
1550 die("can't apply final cleaned capset");
1551
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001552 /*
1553 * If ambient capabilities are supported, clear all capabilities first,
1554 * then raise the requested ones.
1555 */
1556 if (j->flags.set_ambient_caps) {
1557 if (!cap_ambient_supported()) {
1558 pdie("ambient capabilities not supported");
1559 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001560 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1561 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001562 pdie("can't clear ambient capabilities");
1563 }
1564
1565 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1566 if (!(j->caps & (one << i)))
1567 continue;
1568
1569 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1570 0) != 0) {
1571 pdie("prctl(PR_CAP_AMBIENT, "
1572 "PR_CAP_AMBIENT_RAISE, %u) failed",
1573 i);
1574 }
1575 }
1576 }
1577
Kees Cook323878a2013-02-05 15:35:24 -08001578 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001579}
1580
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001581static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001582{
1583 /*
1584 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1585 * in the kernel source tree for an explanation of the parameters.
1586 */
1587 if (j->flags.no_new_privs) {
1588 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1589 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1590 }
1591
1592 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001593 * Code running with ASan
1594 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1595 * will make system calls not included in the syscall filter policy,
1596 * which will likely crash the program. Skip setting seccomp filter in
1597 * that case.
1598 * 'running_with_asan()' has no inputs and is completely defined at
1599 * build time, so this cannot be used by an attacker to skip setting
1600 * seccomp filter.
1601 */
1602 if (j->flags.seccomp_filter && running_with_asan()) {
1603 warn("running with ASan, not setting seccomp filter");
1604 return;
1605 }
1606
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001607 if (j->flags.seccomp_filter) {
1608 if (j->flags.seccomp_filter_logging) {
1609 /*
1610 * If logging seccomp filter failures,
1611 * install the SIGSYS handler first.
1612 */
1613 if (install_sigsys_handler())
1614 pdie("failed to install SIGSYS handler");
1615 warn("logging seccomp filter failures");
1616 } else if (j->flags.seccomp_filter_tsync) {
1617 /*
1618 * If setting thread sync,
1619 * reset the SIGSYS signal handler so that
1620 * the entire thread group is killed.
1621 */
1622 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1623 pdie("failed to reset SIGSYS disposition");
1624 info("reset SIGSYS disposition");
1625 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001626 }
1627
1628 /*
1629 * Install the syscall filter.
1630 */
1631 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001632 if (j->flags.seccomp_filter_tsync) {
1633 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1634 SECCOMP_FILTER_FLAG_TSYNC,
1635 j->filter_prog)) {
1636 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001637 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001638 } else {
1639 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1640 j->filter_prog)) {
1641 pdie("prctl(seccomp_filter) failed");
1642 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001643 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001644 }
1645}
1646
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001647static pid_t forward_pid = -1;
1648
1649static void forward_signal(__attribute__((unused)) int nr,
1650 __attribute__((unused)) siginfo_t *siginfo,
1651 __attribute__((unused)) void *void_context)
1652{
1653 if (forward_pid != -1) {
1654 kill(forward_pid, nr);
1655 }
1656}
1657
1658static void install_signal_handlers(void)
1659{
1660 struct sigaction act;
1661
1662 memset(&act, 0, sizeof(act));
1663 act.sa_sigaction = &forward_signal;
1664 act.sa_flags = SA_SIGINFO | SA_RESTART;
1665
1666 /* Handle all signals, except SIGCHLD. */
1667 for (int nr = 1; nr < NSIG; nr++) {
1668 /*
1669 * We don't care if we get EINVAL: that just means that we
1670 * can't handle this signal, so let's skip it and continue.
1671 */
1672 sigaction(nr, &act, NULL);
1673 }
1674 /* Reset SIGCHLD's handler. */
1675 signal(SIGCHLD, SIG_DFL);
1676
1677 /* Handle real-time signals. */
1678 for (int nr = SIGRTMIN; nr <= SIGRTMAX; nr++) {
1679 sigaction(nr, &act, NULL);
1680 }
1681}
1682
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001683static const char *lookup_hook_name(minijail_hook_event_t event)
1684{
1685 switch (event) {
1686 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
1687 return "pre-drop-caps";
1688 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
1689 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001690 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
1691 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001692 case MINIJAIL_HOOK_EVENT_MAX:
1693 /*
1694 * Adding this in favor of a default case to force the
1695 * compiler to error out if a new enum value is added.
1696 */
1697 break;
1698 }
1699 return "unknown";
1700}
1701
1702static void run_hooks_or_die(const struct minijail *j,
1703 minijail_hook_event_t event)
1704{
1705 int rc;
1706 int hook_index = 0;
1707 for (struct hook *c = j->hooks_head; c; c = c->next) {
1708 if (c->event != event)
1709 continue;
1710 rc = c->hook(c->payload);
1711 if (rc != 0) {
1712 errno = -rc;
1713 pdie("%s hook (index %d) failed",
1714 lookup_hook_name(event), hook_index);
1715 }
1716 /* Only increase the index within the same hook event type. */
1717 ++hook_index;
1718 }
1719}
1720
Will Drewry6ac91122011-10-21 16:38:58 -05001721void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001722{
Dylan Reidf682d472015-09-17 21:39:07 -07001723 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001724 * If we're dropping caps, get the last valid cap from /proc now,
1725 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001726 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001727 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001728 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001729 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001730
Elly Jonese1749eb2011-10-07 13:54:59 -04001731 if (j->flags.pids)
1732 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001733 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001734
Lutz Justen13807cb2017-01-03 17:11:55 +01001735 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001736 die("cannot inherit supplementary groups without setting a "
1737 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001738
Elly Jonesdd3e8512012-01-23 15:13:38 -05001739 /*
1740 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001741 * so we don't even try. If any of our operations fail, we abort() the
1742 * entire process.
1743 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001744 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001745 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001746
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001747 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001748 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001749 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001750 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001751 * Unless asked not to, remount all filesystems as private.
1752 * If they are shared, new bind mounts will creep out of our
1753 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001754 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1755 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001756 if (!j->flags.skip_remount_private) {
1757 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001758 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1759 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001760 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001761 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001762
Dylan Reidf7942472015-11-18 17:55:26 -08001763 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001764 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001765 }
1766
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001767 if (j->flags.uts) {
1768 if (unshare(CLONE_NEWUTS))
1769 pdie("unshare(CLONE_NEWUTS) failed");
1770
1771 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
1772 pdie("sethostname(%s) failed", j->hostname);
1773 }
1774
Dylan Reid1102f5a2015-09-15 11:52:20 -07001775 if (j->flags.enter_net) {
1776 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001777 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001778 } else if (j->flags.net) {
1779 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001780 pdie("unshare(CLONE_NEWNET) failed");
1781 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001782 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001783
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001784 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001785 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001786
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08001787 if (j->flags.new_session_keyring) {
1788 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
1789 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
1790 }
1791
Elly Jones51a5b6c2011-10-12 19:09:26 -04001792 if (j->flags.chroot && enter_chroot(j))
1793 pdie("chroot");
1794
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001795 if (j->flags.pivot_root && enter_pivot_root(j))
1796 pdie("pivot_root");
1797
Martin Pelikánab9eb442017-01-25 11:53:58 +11001798 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07001799 pdie("mount_tmp");
1800
Dylan Reid791f5772015-09-14 20:02:42 -07001801 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001802 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001803
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001804 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
1805
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001806 /*
1807 * If we're only dropping capabilities from the bounding set, but not
1808 * from the thread's (permitted|inheritable|effective) sets, do it now.
1809 */
1810 if (j->flags.capbset_drop) {
1811 drop_capbset(j->cap_bset, last_valid_cap);
1812 }
1813
1814 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001815 /*
1816 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001817 * capability to change uids, our attempt to use setuid()
1818 * below will fail. Hang on to root caps across setuid(), then
1819 * lock securebits.
1820 */
1821 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001822 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001823
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -07001824 if (lock_securebits(j->securebits_skip_mask) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001825 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001826 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001827 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001828
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001829 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001830 /*
1831 * If we're setting no_new_privs, we can drop privileges
1832 * before setting seccomp filter. This way filter policies
1833 * don't need to allow privilege-dropping syscalls.
1834 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001835 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001836 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001837 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001838 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001839 /*
1840 * If we're not setting no_new_privs,
1841 * we need to set seccomp filter *before* dropping privileges.
1842 * WARNING: this means that filter policies *must* allow
1843 * setgroups()/setresgid()/setresuid() for dropping root and
1844 * capget()/capset()/prctl() for dropping caps.
1845 */
1846 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001847 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001848 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001849 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001850
Elly Jonesdd3e8512012-01-23 15:13:38 -05001851 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001852 * Select the specified alternate syscall table. The table must not
1853 * block prctl(2) if we're using seccomp as well.
1854 */
1855 if (j->flags.alt_syscall) {
1856 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001857 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08001858 }
1859
1860 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001861 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001862 * privilege-dropping syscalls :)
1863 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001864 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001865 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001866 warn("seccomp not supported");
1867 return;
1868 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001869 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001870 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001871}
1872
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001873/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001874static int init_exitstatus = 0;
1875
Will Drewry6ac91122011-10-21 16:38:58 -05001876void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001877{
1878 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001879}
1880
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001881void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001882{
1883 pid_t pid;
1884 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001885 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001886 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001887 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001888 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001889 /*
1890 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001891 * left inside our pid namespace or we get a signal.
1892 */
1893 if (pid == rootpid)
1894 init_exitstatus = status;
1895 }
1896 if (!WIFEXITED(init_exitstatus))
1897 _exit(MINIJAIL_ERR_INIT);
1898 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001899}
1900
Will Drewry6ac91122011-10-21 16:38:58 -05001901int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001902{
1903 size_t sz = 0;
1904 size_t bytes = read(fd, &sz, sizeof(sz));
1905 char *buf;
1906 int r;
1907 if (sizeof(sz) != bytes)
1908 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001909 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001910 return -E2BIG;
1911 buf = malloc(sz);
1912 if (!buf)
1913 return -ENOMEM;
1914 bytes = read(fd, buf, sz);
1915 if (bytes != sz) {
1916 free(buf);
1917 return -EINVAL;
1918 }
1919 r = minijail_unmarshal(j, buf, sz);
1920 free(buf);
1921 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001922}
1923
Will Drewry6ac91122011-10-21 16:38:58 -05001924int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001925{
1926 char *buf;
1927 size_t sz = minijail_size(j);
1928 ssize_t written;
1929 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001930
Elly Jonese1749eb2011-10-07 13:54:59 -04001931 if (!sz)
1932 return -EINVAL;
1933 buf = malloc(sz);
1934 r = minijail_marshal(j, buf, sz);
1935 if (r) {
1936 free(buf);
1937 return r;
1938 }
1939 /* Sends [size][minijail]. */
1940 written = write(fd, &sz, sizeof(sz));
1941 if (written != sizeof(sz)) {
1942 free(buf);
1943 return -EFAULT;
1944 }
1945 written = write(fd, buf, sz);
1946 if (written < 0 || (size_t) written != sz) {
1947 free(buf);
1948 return -EFAULT;
1949 }
1950 free(buf);
1951 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001952}
Elly Jonescd7a9042011-07-22 13:56:51 -04001953
Will Drewry6ac91122011-10-21 16:38:58 -05001954int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001955{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001956#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001957 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001958 return 0;
1959#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001960 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1961 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1962 if (!newenv)
1963 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001964
Elly Jonese1749eb2011-10-07 13:54:59 -04001965 /* Only insert a separating space if we have something to separate... */
1966 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1967 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001968
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001969 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001970 setenv(kLdPreloadEnvVar, newenv, 1);
1971 free(newenv);
1972 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001973#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001974}
1975
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001976static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001977{
1978 int r = pipe(fds);
1979 char fd_buf[11];
1980 if (r)
1981 return r;
1982 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1983 if (r <= 0)
1984 return -EINVAL;
1985 setenv(kFdEnvVar, fd_buf, 1);
1986 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001987}
1988
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001989static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07001990{
1991 const char *kFdPath = "/proc/self/fd";
1992
1993 DIR *d = opendir(kFdPath);
1994 struct dirent *dir_entry;
1995
1996 if (d == NULL)
1997 return -1;
1998 int dir_fd = dirfd(d);
1999 while ((dir_entry = readdir(d)) != NULL) {
2000 size_t i;
2001 char *end;
2002 bool should_close = true;
2003 const int fd = strtol(dir_entry->d_name, &end, 10);
2004
2005 if ((*end) != '\0') {
2006 continue;
2007 }
2008 /*
2009 * We might have set up some pipes that we want to share with
2010 * the parent process, and should not be closed.
2011 */
2012 for (i = 0; i < size; ++i) {
2013 if (fd == inheritable_fds[i]) {
2014 should_close = false;
2015 break;
2016 }
2017 }
2018 /* Also avoid closing the directory fd. */
2019 if (should_close && fd != dir_fd)
2020 close(fd);
2021 }
2022 closedir(d);
2023 return 0;
2024}
2025
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002026static int redirect_fds(struct minijail *j)
2027{
2028 size_t i, i2;
2029 int closeable;
2030 for (i = 0; i < j->preserved_fd_count; i++) {
2031 if (dup2(j->preserved_fds[i].parent_fd,
2032 j->preserved_fds[i].child_fd) == -1) {
2033 return -1;
2034 }
2035 }
2036 /*
2037 * After all fds have been duped, we are now free to close all parent
2038 * fds that are *not* child fds.
2039 */
2040 for (i = 0; i < j->preserved_fd_count; i++) {
2041 closeable = true;
2042 for (i2 = 0; i2 < j->preserved_fd_count; i2++) {
2043 closeable &= j->preserved_fds[i].parent_fd !=
2044 j->preserved_fds[i2].child_fd;
2045 }
2046 if (closeable)
2047 close(j->preserved_fds[i].parent_fd);
2048 }
2049 return 0;
2050}
2051
Dylan Reidacfb8be2017-08-25 12:56:51 -07002052/*
2053 * Structure that specifies how to start a minijail.
2054 *
Dylan Reid0412dcc2017-08-24 11:33:15 -07002055 * filename - The program to exec in the child. Required if `exec_in_child` = 1.
2056 * argv - Arguments for the child program. Required if `exec_in_child` = 1.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002057 * use_preload - If true use LD_PRELOAD.
Dylan Reid0412dcc2017-08-24 11:33:15 -07002058 * exec_in_child - If true, run `filename`. Otherwise, the child will return to
2059 * the caller.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002060 */
2061struct minijail_run_config {
2062 const char *filename;
2063 char *const *argv;
2064 int use_preload;
Dylan Reid0412dcc2017-08-24 11:33:15 -07002065 int exec_in_child;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002066};
2067
2068/*
2069 * Set of pointers to fill with values from minijail_run.
2070 * All arguments are allowed to be NULL if unused.
2071 *
2072 * pstdin_fd - Filled with stdin pipe if non-NULL.
2073 * pstdout_fd - Filled with stdout pipe if non-NULL.
2074 * pstderr_fd - Filled with stderr pipe if non-NULL.
2075 * pchild_pid - Filled with the pid of the child process if non-NULL.
2076 */
2077struct minijail_run_status {
2078 int *pstdin_fd;
2079 int *pstdout_fd;
2080 int *pstderr_fd;
2081 pid_t *pchild_pid;
2082};
2083
Dylan Reid18c49c82017-08-25 14:52:27 -07002084static int minijail_run_internal(struct minijail *j,
2085 const struct minijail_run_config *config,
2086 struct minijail_run_status *status_out);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002087
Will Drewry6ac91122011-10-21 16:38:58 -05002088int API minijail_run(struct minijail *j, const char *filename,
2089 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002090{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002091 struct minijail_run_config config = {
2092 .filename = filename,
2093 .argv = argv,
2094 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002095 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002096 };
2097 struct minijail_run_status status = {};
2098 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002099}
2100
2101int API minijail_run_pid(struct minijail *j, const char *filename,
2102 char *const argv[], pid_t *pchild_pid)
2103{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002104 struct minijail_run_config config = {
2105 .filename = filename,
2106 .argv = argv,
2107 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002108 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002109 };
2110 struct minijail_run_status status = {
2111 .pchild_pid = pchild_pid,
2112 };
2113 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002114}
2115
2116int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002117 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002118{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002119 struct minijail_run_config config = {
2120 .filename = filename,
2121 .argv = argv,
2122 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002123 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002124 };
2125 struct minijail_run_status status = {
2126 .pstdin_fd = pstdin_fd,
2127 };
2128 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002129}
2130
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002131int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002132 char *const argv[], pid_t *pchild_pid,
2133 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002134{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002135 struct minijail_run_config config = {
2136 .filename = filename,
2137 .argv = argv,
2138 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002139 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002140 };
2141 struct minijail_run_status status = {
2142 .pstdin_fd = pstdin_fd,
2143 .pstdout_fd = pstdout_fd,
2144 .pstderr_fd = pstderr_fd,
2145 .pchild_pid = pchild_pid,
2146 };
2147 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002148}
2149
2150int API minijail_run_no_preload(struct minijail *j, const char *filename,
2151 char *const argv[])
2152{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002153 struct minijail_run_config config = {
2154 .filename = filename,
2155 .argv = argv,
2156 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002157 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002158 };
2159 struct minijail_run_status status = {};
2160 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002161}
2162
Samuel Tan63187f42015-10-16 13:01:53 -07002163int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002164 const char *filename,
2165 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002166 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002167 int *pstdin_fd,
2168 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002169 int *pstderr_fd)
2170{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002171 struct minijail_run_config config = {
2172 .filename = filename,
2173 .argv = argv,
2174 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002175 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002176 };
2177 struct minijail_run_status status = {
2178 .pstdin_fd = pstdin_fd,
2179 .pstdout_fd = pstdout_fd,
2180 .pstderr_fd = pstderr_fd,
2181 .pchild_pid = pchild_pid,
2182 };
2183 return minijail_run_internal(j, &config, &status);
Samuel Tan63187f42015-10-16 13:01:53 -07002184}
2185
Dylan Reid0412dcc2017-08-24 11:33:15 -07002186pid_t API minijail_fork(struct minijail *j)
2187{
2188 struct minijail_run_config config = {};
2189 struct minijail_run_status status = {};
2190 return minijail_run_internal(j, &config, &status);
2191}
2192
Dylan Reid18c49c82017-08-25 14:52:27 -07002193static int minijail_run_internal(struct minijail *j,
2194 const struct minijail_run_config *config,
2195 struct minijail_run_status *status_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002196{
Elly Jonese1749eb2011-10-07 13:54:59 -04002197 char *oldenv, *oldenv_copy = NULL;
2198 pid_t child_pid;
2199 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002200 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002201 int stdout_fds[2];
2202 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08002203 int child_sync_pipe_fds[2];
2204 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002205 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002206 /* We need to remember this across the minijail_preexec() call. */
2207 int pid_namespace = j->flags.pids;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -07002208 /*
2209 * Create an init process if we are entering a pid namespace, unless the
2210 * user has explicitly opted out by calling minijail_run_as_init().
2211 */
2212 int do_init = j->flags.do_init && !j->flags.run_as_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002213 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002214
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002215 if (use_preload) {
Dylan Reid0412dcc2017-08-24 11:33:15 -07002216 if (j->hooks_head != NULL)
2217 die("Minijail hooks are not supported with LD_PRELOAD");
2218 if (!config->exec_in_child)
2219 die("minijail_fork is not supported with LD_PRELOAD");
2220
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002221 oldenv = getenv(kLdPreloadEnvVar);
2222 if (oldenv) {
2223 oldenv_copy = strdup(oldenv);
2224 if (!oldenv_copy)
2225 return -ENOMEM;
2226 }
2227
2228 if (setup_preload())
2229 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002230 }
Will Drewryf89aef52011-09-16 16:48:57 -05002231
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002232 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002233 if (j->flags.use_caps && j->caps != 0 &&
2234 !j->flags.set_ambient_caps) {
2235 die("non-empty, non-ambient capabilities are not "
2236 "supported without LD_PRELOAD");
2237 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002238 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002239
Elly Jonesdd3e8512012-01-23 15:13:38 -05002240 /*
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002241 * Make the process group ID of this process equal to its PID.
2242 * In the non-interactive case (e.g. when the parent process is started
2243 * from init) this ensures the parent process and the jailed process
2244 * can be killed together.
2245 * When the parent process is started from the console this ensures
2246 * the call to setsid(2) in the jailed process succeeds.
2247 *
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002248 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
2249 * the process is already a process group leader.
2250 */
2251 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
2252 if (errno != EPERM) {
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002253 pdie("setpgid(0, 0) failed");
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002254 }
2255 }
2256
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002257 if (use_preload) {
2258 /*
2259 * Before we fork(2) and execve(2) the child process, we need
2260 * to open a pipe(2) to send the minijail configuration over.
2261 */
2262 if (setup_pipe(pipe_fds))
2263 return -EFAULT;
2264 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002265
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002266 /*
2267 * If we want to write to the child process' standard input,
2268 * create the pipe(2) now.
2269 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002270 if (status_out->pstdin_fd) {
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002271 if (pipe(stdin_fds))
2272 return -EFAULT;
2273 }
2274
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002275 /*
2276 * If we want to read from the child process' standard output,
2277 * create the pipe(2) now.
2278 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002279 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002280 if (pipe(stdout_fds))
2281 return -EFAULT;
2282 }
2283
2284 /*
2285 * If we want to read from the child process' standard error,
2286 * create the pipe(2) now.
2287 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002288 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002289 if (pipe(stderr_fds))
2290 return -EFAULT;
2291 }
2292
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002293 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002294 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002295 * or if we need to add the child process to cgroups, create the pipe(2)
2296 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002297 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002298 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002299 sync_child = 1;
2300 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002301 return -EFAULT;
2302 }
2303
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002304 /*
2305 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002306 *
2307 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2308 *
2309 * In multithreaded programs, there are a bunch of locks inside libc,
2310 * some of which may be held by other threads at the time that we call
2311 * minijail_run_pid(). If we call fork(), glibc does its level best to
2312 * ensure that we hold all of these locks before it calls clone()
2313 * internally and drop them after clone() returns, but when we call
2314 * sys_clone(2) directly, all that gets bypassed and we end up with a
2315 * child address space where some of libc's important locks are held by
2316 * other threads (which did not get cloned, and hence will never release
2317 * those locks). This is okay so long as we call exec() immediately
2318 * after, but a bunch of seemingly-innocent libc functions like setenv()
2319 * take locks.
2320 *
2321 * Hence, only call sys_clone() if we need to, in order to get at pid
2322 * namespacing. If we follow this path, the child's address space might
2323 * have broken locks; you may only call functions that do not acquire
2324 * any locks.
2325 *
2326 * Unfortunately, fork() acquires every lock it can get its hands on, as
2327 * previously detailed, so this function is highly likely to deadlock
2328 * later on (see "deadlock here") if we're multithreaded.
2329 *
2330 * We might hack around this by having the clone()d child (init of the
2331 * pid namespace) return directly, rather than leaving the clone()d
2332 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002333 * its fork()ed child return in turn), but that process would be
2334 * crippled with its libc locks potentially broken. We might try
2335 * fork()ing in the parent before we clone() to ensure that we own all
2336 * the locks, but then we have to have the forked child hanging around
2337 * consuming resources (and possibly having file descriptors / shared
2338 * memory regions / etc attached). We'd need to keep the child around to
2339 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002340 *
2341 * TODO(ellyjones): figure out if the "forked child hanging around"
2342 * problem is fixable or not. It would be nice if we worked in this
2343 * case.
2344 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002345 if (pid_namespace) {
2346 int clone_flags = CLONE_NEWPID | SIGCHLD;
2347 if (j->flags.userns)
2348 clone_flags |= CLONE_NEWUSER;
2349 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002350 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002351 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002352 }
Elly Jones761b7412012-06-13 15:49:52 -04002353
Elly Jonese1749eb2011-10-07 13:54:59 -04002354 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002355 if (use_preload) {
2356 free(oldenv_copy);
2357 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002358 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002359 }
Will Drewryf89aef52011-09-16 16:48:57 -05002360
Elly Jonese1749eb2011-10-07 13:54:59 -04002361 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002362 if (use_preload) {
2363 /* Restore parent's LD_PRELOAD. */
2364 if (oldenv_copy) {
2365 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2366 free(oldenv_copy);
2367 } else {
2368 unsetenv(kLdPreloadEnvVar);
2369 }
2370 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002371 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002372
Elly Jonese1749eb2011-10-07 13:54:59 -04002373 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002374
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002375 if (j->flags.forward_signals) {
2376 forward_pid = child_pid;
2377 install_signal_handlers();
2378 }
2379
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002380 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002381 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002382
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002383 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002384 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002385
Dylan Reid0f72ef42017-06-06 15:42:49 -07002386 if (j->rlimit_count)
2387 set_rlimits_or_die(j);
2388
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002389 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002390 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002391
2392 if (sync_child)
2393 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002394
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002395 if (use_preload) {
2396 /* Send marshalled minijail. */
2397 close(pipe_fds[0]); /* read endpoint */
2398 ret = minijail_to_fd(j, pipe_fds[1]);
2399 close(pipe_fds[1]); /* write endpoint */
2400 if (ret) {
2401 kill(j->initpid, SIGKILL);
2402 die("failed to send marshalled minijail");
2403 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002404 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002405
Dylan Reidacfb8be2017-08-25 12:56:51 -07002406 if (status_out->pchild_pid)
2407 *status_out->pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002408
2409 /*
2410 * If we want to write to the child process' standard input,
2411 * set up the write end of the pipe.
2412 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002413 if (status_out->pstdin_fd)
2414 *status_out->pstdin_fd =
2415 setup_pipe_end(stdin_fds, 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002416
2417 /*
2418 * If we want to read from the child process' standard output,
2419 * set up the read end of the pipe.
2420 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002421 if (status_out->pstdout_fd)
2422 *status_out->pstdout_fd =
2423 setup_pipe_end(stdout_fds, 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002424
2425 /*
2426 * If we want to read from the child process' standard error,
2427 * set up the read end of the pipe.
2428 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002429 if (status_out->pstderr_fd)
2430 *status_out->pstderr_fd =
2431 setup_pipe_end(stderr_fds, 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002432
Dylan Reid0412dcc2017-08-24 11:33:15 -07002433 /*
2434 * If forking return the child pid, in the normal exec case
2435 * return 0 for success.
2436 */
2437 if (!config->exec_in_child)
2438 return child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04002439 return 0;
2440 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002441 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002442 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002443
Peter Qiu2860c462015-12-16 15:13:06 -08002444 if (j->flags.reset_signal_mask) {
2445 sigset_t signal_mask;
2446 if (sigemptyset(&signal_mask) != 0)
2447 pdie("sigemptyset failed");
2448 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2449 pdie("sigprocmask failed");
2450 }
2451
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002452 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002453 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002454 int inheritable_fds[kMaxInheritableFdsSize];
2455 size_t size = 0;
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002456 size_t i;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002457 if (use_preload) {
2458 inheritable_fds[size++] = pipe_fds[0];
2459 inheritable_fds[size++] = pipe_fds[1];
2460 }
2461 if (sync_child) {
2462 inheritable_fds[size++] = child_sync_pipe_fds[0];
2463 inheritable_fds[size++] = child_sync_pipe_fds[1];
2464 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002465 if (status_out->pstdin_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002466 inheritable_fds[size++] = stdin_fds[0];
2467 inheritable_fds[size++] = stdin_fds[1];
2468 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002469 if (status_out->pstdout_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002470 inheritable_fds[size++] = stdout_fds[0];
2471 inheritable_fds[size++] = stdout_fds[1];
2472 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002473 if (status_out->pstderr_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002474 inheritable_fds[size++] = stderr_fds[0];
2475 inheritable_fds[size++] = stderr_fds[1];
2476 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002477 for (i = 0; i < j->preserved_fd_count; i++) {
2478 /*
2479 * Preserve all parent_fds. They will be dup2(2)-ed in
2480 * the child later.
2481 */
2482 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
2483 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002484
2485 if (close_open_fds(inheritable_fds, size) < 0)
2486 die("failed to close open file descriptors");
2487 }
2488
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002489 if (redirect_fds(j))
2490 die("failed to set up fd redirections");
2491
Dylan Reidce5b55e2016-01-13 11:04:16 -08002492 if (sync_child)
2493 wait_for_parent_setup(child_sync_pipe_fds);
2494
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002495 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002496 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002497
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002498 /*
2499 * If we want to write to the jailed process' standard input,
2500 * set up the read end of the pipe.
2501 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002502 if (status_out->pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002503 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2504 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002505 die("failed to set up stdin pipe");
2506 }
2507
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002508 /*
2509 * If we want to read from the jailed process' standard output,
2510 * set up the write end of the pipe.
2511 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002512 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002513 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2514 STDOUT_FILENO) < 0)
2515 die("failed to set up stdout pipe");
2516 }
2517
2518 /*
2519 * If we want to read from the jailed process' standard error,
2520 * set up the write end of the pipe.
2521 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002522 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002523 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2524 STDERR_FILENO) < 0)
2525 die("failed to set up stderr pipe");
2526 }
2527
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002528 /*
2529 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2530 * This prevents the jailed process from using the TIOCSTI ioctl
2531 * to push characters into the parent process terminal's input buffer,
2532 * therefore escaping the jail.
2533 */
2534 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2535 isatty(STDERR_FILENO)) {
2536 if (setsid() < 0) {
2537 pdie("setsid() failed");
2538 }
2539 }
2540
Dylan Reid791f5772015-09-14 20:02:42 -07002541 /* If running an init program, let it decide when/how to mount /proc. */
2542 if (pid_namespace && !do_init)
2543 j->flags.remount_proc_ro = 0;
2544
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002545 if (use_preload) {
2546 /* Strip out flags that cannot be inherited across execve(2). */
2547 minijail_preexec(j);
2548 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002549 /*
2550 * If not using LD_PRELOAD, do all jailing before execve(2).
2551 * Note that PID namespaces can only be entered on fork(2),
2552 * so that flag is still cleared.
2553 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002554 j->flags.pids = 0;
2555 }
Dylan Reid0412dcc2017-08-24 11:33:15 -07002556
2557 /*
2558 * Jail this process.
2559 * If forking, return.
2560 * If not, execve(2) the target.
2561 */
Elly Jonese1749eb2011-10-07 13:54:59 -04002562 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002563
Dylan Reid0412dcc2017-08-24 11:33:15 -07002564 if (config->exec_in_child && pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002565 /*
2566 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002567 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002568 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002569 * a child to actually run the program. If |do_init == 0|, we
2570 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002571 *
2572 * If we're multithreaded, we'll probably deadlock here. See
2573 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002574 */
2575 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002576 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002577 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002578 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002579 /*
2580 * Best effort. Don't bother checking the return value.
2581 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002582 prctl(PR_SET_NAME, "minijail-init");
2583 init(child_pid); /* Never returns. */
2584 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002585 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002586
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002587 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
2588
Dylan Reid0412dcc2017-08-24 11:33:15 -07002589 if (!config->exec_in_child)
2590 return 0;
2591
Elly Jonesdd3e8512012-01-23 15:13:38 -05002592 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002593 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002594 * calling process
2595 * -> execve()-ing process
2596 * If we are:
2597 * calling process
2598 * -> init()-ing process
2599 * -> execve()-ing process
2600 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002601 ret = execve(config->filename, config->argv, environ);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002602 if (ret == -1) {
Dylan Reidacfb8be2017-08-25 12:56:51 -07002603 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002604 }
2605 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002606}
2607
Will Drewry6ac91122011-10-21 16:38:58 -05002608int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002609{
2610 int st;
2611 if (kill(j->initpid, SIGTERM))
2612 return -errno;
2613 if (waitpid(j->initpid, &st, 0) < 0)
2614 return -errno;
2615 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002616}
2617
Will Drewry6ac91122011-10-21 16:38:58 -05002618int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002619{
2620 int st;
2621 if (waitpid(j->initpid, &st, 0) < 0)
2622 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002623
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002624 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002625 int error_status = st;
2626 if (WIFSIGNALED(st)) {
2627 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002628 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002629 j->initpid, signum);
2630 /*
2631 * We return MINIJAIL_ERR_JAIL if the process received
2632 * SIGSYS, which happens when a syscall is blocked by
2633 * seccomp filters.
2634 * If not, we do what bash(1) does:
2635 * $? = 128 + signum
2636 */
2637 if (signum == SIGSYS) {
2638 error_status = MINIJAIL_ERR_JAIL;
2639 } else {
2640 error_status = 128 + signum;
2641 }
2642 }
2643 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002644 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002645
2646 int exit_status = WEXITSTATUS(st);
2647 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002648 info("child process %d exited with status %d",
2649 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002650
2651 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002652}
2653
Will Drewry6ac91122011-10-21 16:38:58 -05002654void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002655{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002656 size_t i;
2657
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002658 if (j->flags.seccomp_filter && j->filter_prog) {
2659 free(j->filter_prog->filter);
2660 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002661 }
Dylan Reid648b2202015-10-23 00:50:00 -07002662 while (j->mounts_head) {
2663 struct mountpoint *m = j->mounts_head;
2664 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002665 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002666 free(m->type);
2667 free(m->dest);
2668 free(m->src);
2669 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002670 }
Dylan Reid648b2202015-10-23 00:50:00 -07002671 j->mounts_tail = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002672 while (j->hooks_head) {
2673 struct hook *c = j->hooks_head;
2674 j->hooks_head = c->next;
2675 free(c);
2676 }
2677 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002678 if (j->user)
2679 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002680 if (j->suppl_gid_list)
2681 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002682 if (j->chrootdir)
2683 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002684 if (j->pid_file_path)
2685 free(j->pid_file_path);
2686 if (j->uidmap)
2687 free(j->uidmap);
2688 if (j->gidmap)
2689 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002690 if (j->hostname)
2691 free(j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08002692 if (j->alt_syscall_table)
2693 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002694 for (i = 0; i < j->cgroup_count; ++i)
2695 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002696 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002697}
Luis Hector Chavez114a9302017-09-05 20:36:58 -07002698
2699void API minijail_log_to_fd(int fd, int min_priority)
2700{
2701 init_logging(LOG_TO_FD, fd, min_priority);
2702}