blob: 974346bae60406a2b5e5b784391005cd2346f9d5 [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>
Mike Frysinger33ffef32017-01-13 19:53:19 -050029#include <sys/sysmacros.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070030#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080031#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040032#include <sys/wait.h>
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -070033#include <syscall.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040034#include <unistd.h>
35
36#include "libminijail.h"
37#include "libminijail-private.h"
38
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070039#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080040#include "syscall_filter.h"
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040041#include "syscall_wrapper.h"
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040042#include "system.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070043#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080044
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070045/* Until these are reliably available in linux/prctl.h. */
Andrew Brestickereac28942015-11-11 16:04:46 -080046#ifndef PR_ALT_SYSCALL
47# define PR_ALT_SYSCALL 0x43724f53
48#endif
49
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040050/* Seccomp filter related flags. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080051#ifndef PR_SET_NO_NEW_PRIVS
52# define PR_SET_NO_NEW_PRIVS 38
53#endif
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040054
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080055#ifndef SECCOMP_MODE_FILTER
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040056#define SECCOMP_MODE_FILTER 2 /* Uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050057#endif
58
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040059#ifndef SECCOMP_SET_MODE_STRICT
60# define SECCOMP_SET_MODE_STRICT 0
61#endif
62#ifndef SECCOMP_SET_MODE_FILTER
63# define SECCOMP_SET_MODE_FILTER 1
64#endif
65
66#ifndef SECCOMP_FILTER_FLAG_TSYNC
67# define SECCOMP_FILTER_FLAG_TSYNC 1
68#endif
69/* End seccomp filter related flags. */
70
Dylan Reid4cbc2a52016-06-17 19:06:07 -070071/* New cgroup namespace might not be in linux-headers yet. */
72#ifndef CLONE_NEWCGROUP
73# define CLONE_NEWCGROUP 0x02000000
74#endif
75
Dylan Reid605ce7f2016-01-19 19:21:00 -080076#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
77
Dylan Reid0f72ef42017-06-06 15:42:49 -070078#define MAX_RLIMITS 32 /* Currently there are 15 supported by Linux. */
79
Stephen Barber0d1cbf62017-10-16 22:19:38 -070080#define MAX_PRESERVED_FDS 32U
Luis Hector Chavez1617f632017-08-01 18:32:30 -070081
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -080082/* Keyctl commands. */
83#define KEYCTL_JOIN_SESSION_KEYRING 1
84
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -070085/*
86 * The userspace equivalent of MNT_USER_SETTABLE_MASK, which is the mask of all
87 * flags that can be modified by MS_REMOUNT.
88 */
89#define MS_USER_SETTABLE_MASK \
90 (MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME | MS_NODIRATIME | \
91 MS_RELATIME | MS_RDONLY)
92
Dylan Reid0f72ef42017-06-06 15:42:49 -070093struct minijail_rlimit {
94 int type;
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -080095 rlim_t cur;
96 rlim_t max;
Dylan Reid0f72ef42017-06-06 15:42:49 -070097};
98
Dylan Reid648b2202015-10-23 00:50:00 -070099struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400100 char *src;
101 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700102 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700103 char *data;
104 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -0700105 unsigned long flags;
106 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400107};
108
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700109struct hook {
110 minijail_hook_t hook;
111 void *payload;
112 minijail_hook_event_t event;
113 struct hook *next;
114};
115
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700116struct preserved_fd {
117 int parent_fd;
118 int child_fd;
119};
120
Will Drewryf89aef52011-09-16 16:48:57 -0500121struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700122 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700123 * WARNING: if you add a flag here you need to make sure it's
124 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700125 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400126 struct {
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700127 int uid : 1;
128 int gid : 1;
Lutz Justen13807cb2017-01-03 17:11:55 +0100129 int inherit_suppl_gids : 1;
130 int set_suppl_gids : 1;
131 int keep_suppl_gids : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700132 int use_caps : 1;
133 int capbset_drop : 1;
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400134 int set_ambient_caps : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700135 int vfs : 1;
136 int enter_vfs : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700137 int pids : 1;
138 int ipc : 1;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400139 int uts : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700140 int net : 1;
141 int enter_net : 1;
142 int ns_cgroups : 1;
143 int userns : 1;
144 int disable_setgroups : 1;
145 int seccomp : 1;
146 int remount_proc_ro : 1;
147 int no_new_privs : 1;
148 int seccomp_filter : 1;
149 int seccomp_filter_tsync : 1;
150 int seccomp_filter_logging : 1;
151 int chroot : 1;
152 int pivot_root : 1;
Mike Frysinger33ffef32017-01-13 19:53:19 -0500153 int mount_dev : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700154 int mount_tmp : 1;
155 int do_init : 1;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700156 int run_as_init : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700157 int pid_file : 1;
158 int cgroups : 1;
159 int alt_syscall : 1;
160 int reset_signal_mask : 1;
Luis Hector Chaveza27118a2018-04-04 08:18:01 -0700161 int reset_signal_handlers : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700162 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800163 int new_session_keyring : 1;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400164 int forward_signals : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400165 } flags;
166 uid_t uid;
167 gid_t gid;
168 gid_t usergid;
169 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800170 size_t suppl_gid_count;
171 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400172 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800173 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400174 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700175 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700176 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400177 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800178 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800179 char *uidmap;
180 char *gidmap;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400181 char *hostname;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800182 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800183 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800184 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700185 struct mountpoint *mounts_head;
186 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800187 size_t mounts_count;
Mike Frysinger785b1c32018-02-23 15:47:24 -0500188 unsigned long remount_mode;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100189 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800190 char *cgroups[MAX_CGROUPS];
191 size_t cgroup_count;
Dylan Reid0f72ef42017-06-06 15:42:49 -0700192 struct minijail_rlimit rlimits[MAX_RLIMITS];
193 size_t rlimit_count;
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700194 uint64_t securebits_skip_mask;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700195 struct hook *hooks_head;
196 struct hook *hooks_tail;
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700197 struct preserved_fd preserved_fds[MAX_PRESERVED_FDS];
198 size_t preserved_fd_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500199};
200
Luis Hector Chavez64730af2017-09-13 13:18:59 -0700201static void run_hooks_or_die(const struct minijail *j,
202 minijail_hook_event_t event);
203
Mike Frysingerac08a682017-10-10 02:04:50 -0400204static void free_mounts_list(struct minijail *j)
205{
206 while (j->mounts_head) {
207 struct mountpoint *m = j->mounts_head;
208 j->mounts_head = j->mounts_head->next;
209 free(m->data);
210 free(m->type);
211 free(m->dest);
212 free(m->src);
213 free(m);
214 }
215 // No need to clear mounts_head as we know it's NULL after the loop.
216 j->mounts_tail = NULL;
217}
218
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700219/*
220 * Strip out flags meant for the parent.
221 * We keep things that are not inherited across execve(2) (e.g. capabilities),
222 * or are easier to set after execve(2) (e.g. seccomp filters).
223 */
224void minijail_preenter(struct minijail *j)
225{
226 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700227 j->flags.enter_vfs = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700228 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700229 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800230 j->flags.do_init = 0;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700231 j->flags.run_as_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800232 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800233 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400234 j->flags.forward_signals = 0;
Mike Frysinger785b1c32018-02-23 15:47:24 -0500235 j->remount_mode = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700236}
237
238/*
239 * Strip out flags meant for the child.
240 * We keep things that are inherited across execve(2).
241 */
242void minijail_preexec(struct minijail *j)
243{
244 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700245 int enter_vfs = j->flags.enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700246 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800247 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700248 if (j->user)
249 free(j->user);
250 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800251 if (j->suppl_gid_list)
252 free(j->suppl_gid_list);
253 j->suppl_gid_list = NULL;
Mike Frysingerac08a682017-10-10 02:04:50 -0400254 free_mounts_list(j);
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700255 memset(&j->flags, 0, sizeof(j->flags));
256 /* Now restore anything we meant to keep. */
257 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700258 j->flags.enter_vfs = enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700259 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800260 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700261 /* Note, |pids| will already have been used before this call. */
262}
263
264/* Minijail API. */
265
Will Drewry6ac91122011-10-21 16:38:58 -0500266struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400267{
Mike Frysinger785b1c32018-02-23 15:47:24 -0500268 struct minijail *j = calloc(1, sizeof(struct minijail));
269 j->remount_mode = MS_PRIVATE;
270 return j;
Elly Jonescd7a9042011-07-22 13:56:51 -0400271}
272
Will Drewry6ac91122011-10-21 16:38:58 -0500273void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400274{
275 if (uid == 0)
276 die("useless change to uid 0");
277 j->uid = uid;
278 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400279}
280
Will Drewry6ac91122011-10-21 16:38:58 -0500281void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400282{
283 if (gid == 0)
284 die("useless change to gid 0");
285 j->gid = gid;
286 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400287}
288
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800289void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
290 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800291{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800292 size_t i;
293
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500294 if (j->flags.inherit_suppl_gids)
295 die("cannot inherit *and* set supplementary groups");
296 if (j->flags.keep_suppl_gids)
297 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800298
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800299 if (size == 0) {
300 /* Clear supplementary groups. */
301 j->suppl_gid_list = NULL;
302 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100303 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800304 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800305 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800306
307 /* Copy the gid_t array. */
308 j->suppl_gid_list = calloc(size, sizeof(gid_t));
309 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800310 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800311 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800312 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800313 j->suppl_gid_list[i] = list[i];
314 }
315 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100316 j->flags.set_suppl_gids = 1;
317}
318
319void API minijail_keep_supplementary_gids(struct minijail *j) {
320 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800321}
322
Will Drewry6ac91122011-10-21 16:38:58 -0500323int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400324{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700325 uid_t uid;
326 gid_t gid;
327 int rc = lookup_user(user, &uid, &gid);
328 if (rc)
329 return rc;
330 minijail_change_uid(j, uid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400331 j->user = strdup(user);
332 if (!j->user)
333 return -ENOMEM;
Luis Hector Chavez71323552017-09-05 09:17:22 -0700334 j->usergid = gid;
Elly Jonese1749eb2011-10-07 13:54:59 -0400335 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400336}
337
Will Drewry6ac91122011-10-21 16:38:58 -0500338int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400339{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700340 gid_t gid;
341 int rc = lookup_group(group, &gid);
342 if (rc)
343 return rc;
344 minijail_change_gid(j, gid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400345 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400346}
347
Will Drewry6ac91122011-10-21 16:38:58 -0500348void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400349{
350 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400351}
352
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700353void API minijail_no_new_privs(struct minijail *j)
354{
355 j->flags.no_new_privs = 1;
356}
357
Will Drewry6ac91122011-10-21 16:38:58 -0500358void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400359{
360 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500361}
362
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400363void API minijail_set_seccomp_filter_tsync(struct minijail *j)
364{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400365 if (j->filter_len > 0 && j->filter_prog != NULL) {
366 die("minijail_set_seccomp_filter_tsync() must be called "
367 "before minijail_parse_seccomp_filters()");
368 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400369 j->flags.seccomp_filter_tsync = 1;
370}
371
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700372void API minijail_log_seccomp_filter_failures(struct minijail *j)
373{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400374 if (j->filter_len > 0 && j->filter_prog != NULL) {
375 die("minijail_log_seccomp_filter_failures() must be called "
376 "before minijail_parse_seccomp_filters()");
377 }
378 j->flags.seccomp_filter_logging = 1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700379}
380
Will Drewry6ac91122011-10-21 16:38:58 -0500381void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400382{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800383 /*
384 * 'minijail_use_caps' configures a runtime-capabilities-only
385 * environment, including a bounding set matching the thread's runtime
386 * (permitted|inheritable|effective) sets.
387 * Therefore, it will override any existing bounding set configurations
388 * since the latter would allow gaining extra runtime capabilities from
389 * file capabilities.
390 */
391 if (j->flags.capbset_drop) {
392 warn("overriding bounding set configuration");
393 j->cap_bset = 0;
394 j->flags.capbset_drop = 0;
395 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400396 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800397 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400398}
399
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800400void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
401{
402 if (j->flags.use_caps) {
403 /*
404 * 'minijail_use_caps' will have already configured a capability
405 * bounding set matching the (permitted|inheritable|effective)
406 * sets. Abort if the user tries to configure a separate
407 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
408 * are mutually exclusive.
409 */
410 die("runtime capabilities already configured, can't drop "
411 "bounding set separately");
412 }
413 j->cap_bset = capmask;
414 j->flags.capbset_drop = 1;
415}
416
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400417void API minijail_set_ambient_caps(struct minijail *j)
418{
419 j->flags.set_ambient_caps = 1;
420}
421
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800422void API minijail_reset_signal_mask(struct minijail *j)
423{
Peter Qiu2860c462015-12-16 15:13:06 -0800424 j->flags.reset_signal_mask = 1;
425}
426
Luis Hector Chaveza27118a2018-04-04 08:18:01 -0700427void API minijail_reset_signal_handlers(struct minijail *j)
428{
429 j->flags.reset_signal_handlers = 1;
430}
431
Will Drewry6ac91122011-10-21 16:38:58 -0500432void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400433{
434 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400435}
436
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700437void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
438{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800439 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700440 if (ns_fd < 0) {
441 pdie("failed to open namespace '%s'", ns_path);
442 }
443 j->mountns_fd = ns_fd;
444 j->flags.enter_vfs = 1;
445}
446
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800447void API minijail_new_session_keyring(struct minijail *j)
448{
449 j->flags.new_session_keyring = 1;
450}
451
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700452void API minijail_skip_setting_securebits(struct minijail *j,
453 uint64_t securebits_skip_mask)
454{
455 j->securebits_skip_mask = securebits_skip_mask;
456}
457
Mike Frysinger785b1c32018-02-23 15:47:24 -0500458void API minijail_remount_mode(struct minijail *j, unsigned long mode)
459{
460 j->remount_mode = mode;
461}
462
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800463void API minijail_skip_remount_private(struct minijail *j)
464{
Mike Frysinger785b1c32018-02-23 15:47:24 -0500465 j->remount_mode = 0;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800466}
467
Will Drewry6ac91122011-10-21 16:38:58 -0500468void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400469{
Elly Jonese58176c2012-01-23 11:46:17 -0500470 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700471 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400472 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800473 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400474}
475
Dylan Reidf7942472015-11-18 17:55:26 -0800476void API minijail_namespace_ipc(struct minijail *j)
477{
478 j->flags.ipc = 1;
479}
480
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400481void API minijail_namespace_uts(struct minijail *j)
482{
483 j->flags.uts = 1;
484}
485
486int API minijail_namespace_set_hostname(struct minijail *j, const char *name)
487{
488 if (j->hostname)
489 return -EINVAL;
490 minijail_namespace_uts(j);
491 j->hostname = strdup(name);
492 if (!j->hostname)
493 return -ENOMEM;
494 return 0;
495}
496
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400497void API minijail_namespace_net(struct minijail *j)
498{
499 j->flags.net = 1;
500}
501
Dylan Reid1102f5a2015-09-15 11:52:20 -0700502void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
503{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800504 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700505 if (ns_fd < 0) {
506 pdie("failed to open namespace '%s'", ns_path);
507 }
508 j->netns_fd = ns_fd;
509 j->flags.enter_net = 1;
510}
511
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700512void API minijail_namespace_cgroups(struct minijail *j)
513{
514 j->flags.ns_cgroups = 1;
515}
516
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700517void API minijail_close_open_fds(struct minijail *j)
518{
519 j->flags.close_open_fds = 1;
520}
521
Dylan Reid791f5772015-09-14 20:02:42 -0700522void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400523{
524 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700525 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400526}
527
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800528void API minijail_namespace_user(struct minijail *j)
529{
530 j->flags.userns = 1;
531}
532
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400533void API minijail_namespace_user_disable_setgroups(struct minijail *j)
534{
535 j->flags.disable_setgroups = 1;
536}
537
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800538int API minijail_uidmap(struct minijail *j, const char *uidmap)
539{
540 j->uidmap = strdup(uidmap);
541 if (!j->uidmap)
542 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800543 char *ch;
544 for (ch = j->uidmap; *ch; ch++) {
545 if (*ch == ',')
546 *ch = '\n';
547 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800548 return 0;
549}
550
551int API minijail_gidmap(struct minijail *j, const char *gidmap)
552{
553 j->gidmap = strdup(gidmap);
554 if (!j->gidmap)
555 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800556 char *ch;
557 for (ch = j->gidmap; *ch; ch++) {
558 if (*ch == ',')
559 *ch = '\n';
560 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800561 return 0;
562}
563
Will Drewry6ac91122011-10-21 16:38:58 -0500564void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400565{
Lutz Justen13807cb2017-01-03 17:11:55 +0100566 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400567}
568
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800569void API minijail_run_as_init(struct minijail *j)
570{
571 /*
572 * Since the jailed program will become 'init' in the new PID namespace,
573 * Minijail does not need to fork an 'init' process.
574 */
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700575 j->flags.run_as_init = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800576}
577
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700578int API minijail_enter_chroot(struct minijail *j, const char *dir)
579{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400580 if (j->chrootdir)
581 return -EINVAL;
582 j->chrootdir = strdup(dir);
583 if (!j->chrootdir)
584 return -ENOMEM;
585 j->flags.chroot = 1;
586 return 0;
587}
588
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800589int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
590{
591 if (j->chrootdir)
592 return -EINVAL;
593 j->chrootdir = strdup(dir);
594 if (!j->chrootdir)
595 return -ENOMEM;
596 j->flags.pivot_root = 1;
597 return 0;
598}
599
Dylan Reida14e08d2015-10-22 21:05:29 -0700600char API *minijail_get_original_path(struct minijail *j,
601 const char *path_inside_chroot)
602{
Dylan Reid648b2202015-10-23 00:50:00 -0700603 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700604
Dylan Reid648b2202015-10-23 00:50:00 -0700605 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700606 while (b) {
607 /*
608 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700609 * mount, then the original path is exactly the source of
610 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700611 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700612 * mount source = /some/path/exe, mount dest =
613 * /chroot/path/exe Then when getting the original path of
614 * "/chroot/path/exe", the source of that mount,
615 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700616 */
617 if (!strcmp(b->dest, path_inside_chroot))
618 return strdup(b->src);
619
620 /*
621 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700622 * mount, take the suffix of the chroot path relative to the
623 * mount destination path, and append it to the mount source
624 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700625 */
626 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
627 const char *relative_path =
628 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400629 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700630 }
631 b = b->next;
632 }
633
634 /* If there is a chroot path, append |path_inside_chroot| to that. */
635 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400636 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700637
638 /* No chroot, so the path outside is the same as it is inside. */
639 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700640}
641
Martin Pelikánab9eb442017-01-25 11:53:58 +1100642size_t minijail_get_tmpfs_size(const struct minijail *j)
643{
644 return j->tmpfs_size;
645}
646
Mike Frysinger33ffef32017-01-13 19:53:19 -0500647void API minijail_mount_dev(struct minijail *j)
648{
649 j->flags.mount_dev = 1;
650}
651
Lee Campbell11af0622014-05-22 12:36:04 -0700652void API minijail_mount_tmp(struct minijail *j)
653{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100654 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
655}
656
657void API minijail_mount_tmp_size(struct minijail *j, size_t size)
658{
659 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700660 j->flags.mount_tmp = 1;
661}
662
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800663int API minijail_write_pid_file(struct minijail *j, const char *path)
664{
665 j->pid_file_path = strdup(path);
666 if (!j->pid_file_path)
667 return -ENOMEM;
668 j->flags.pid_file = 1;
669 return 0;
670}
671
Dylan Reid605ce7f2016-01-19 19:21:00 -0800672int API minijail_add_to_cgroup(struct minijail *j, const char *path)
673{
674 if (j->cgroup_count >= MAX_CGROUPS)
675 return -ENOMEM;
676 j->cgroups[j->cgroup_count] = strdup(path);
677 if (!j->cgroups[j->cgroup_count])
678 return -ENOMEM;
679 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800680 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800681 return 0;
682}
683
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800684int API minijail_rlimit(struct minijail *j, int type, rlim_t cur, rlim_t max)
Dylan Reid0f72ef42017-06-06 15:42:49 -0700685{
686 size_t i;
687
688 if (j->rlimit_count >= MAX_RLIMITS)
689 return -ENOMEM;
690 /* It's an error if the caller sets the same rlimit multiple times. */
691 for (i = 0; i < j->rlimit_count; i++) {
692 if (j->rlimits[i].type == type)
693 return -EEXIST;
694 }
695
696 j->rlimits[j->rlimit_count].type = type;
697 j->rlimits[j->rlimit_count].cur = cur;
698 j->rlimits[j->rlimit_count].max = max;
699 j->rlimit_count++;
700 return 0;
701}
702
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400703int API minijail_forward_signals(struct minijail *j)
704{
705 j->flags.forward_signals = 1;
706 return 0;
707}
708
Dylan Reid81e23972016-05-18 14:06:35 -0700709int API minijail_mount_with_data(struct minijail *j, const char *src,
710 const char *dest, const char *type,
711 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700712{
Dylan Reid648b2202015-10-23 00:50:00 -0700713 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400714
715 if (*dest != '/')
716 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700717 m = calloc(1, sizeof(*m));
718 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400719 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700720 m->dest = strdup(dest);
721 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400722 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700723 m->src = strdup(src);
724 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400725 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700726 m->type = strdup(type);
727 if (!m->type)
728 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700729 if (data) {
730 m->data = strdup(data);
731 if (!m->data)
732 goto error;
733 m->has_data = 1;
734 }
Dylan Reid648b2202015-10-23 00:50:00 -0700735 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400736
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800737 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400738
Elly Jonesdd3e8512012-01-23 15:13:38 -0500739 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700740 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400741 * containing vfs namespace.
742 */
743 minijail_namespace_vfs(j);
744
Dylan Reid648b2202015-10-23 00:50:00 -0700745 if (j->mounts_tail)
746 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400747 else
Dylan Reid648b2202015-10-23 00:50:00 -0700748 j->mounts_head = m;
749 j->mounts_tail = m;
750 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400751
752 return 0;
753
754error:
Dylan Reid81e23972016-05-18 14:06:35 -0700755 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700756 free(m->src);
757 free(m->dest);
758 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400759 return -ENOMEM;
760}
761
Dylan Reid81e23972016-05-18 14:06:35 -0700762int API minijail_mount(struct minijail *j, const char *src, const char *dest,
763 const char *type, unsigned long flags)
764{
765 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
766}
767
Dylan Reid648b2202015-10-23 00:50:00 -0700768int API minijail_bind(struct minijail *j, const char *src, const char *dest,
769 int writeable)
770{
771 unsigned long flags = MS_BIND;
772
773 if (!writeable)
774 flags |= MS_RDONLY;
775
776 return minijail_mount(j, src, dest, "", flags);
777}
778
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700779int API minijail_add_hook(struct minijail *j, minijail_hook_t hook,
780 void *payload, minijail_hook_event_t event)
781{
782 struct hook *c;
783
784 if (hook == NULL)
785 return -EINVAL;
786 if (event >= MINIJAIL_HOOK_EVENT_MAX)
787 return -EINVAL;
788 c = calloc(1, sizeof(*c));
789 if (!c)
790 return -ENOMEM;
791
792 c->hook = hook;
793 c->payload = payload;
794 c->event = event;
795
796 if (j->hooks_tail)
797 j->hooks_tail->next = c;
798 else
799 j->hooks_head = c;
800 j->hooks_tail = c;
801
802 return 0;
803}
804
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700805int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd)
806{
807 if (parent_fd < 0 || child_fd < 0)
808 return -EINVAL;
809 if (j->preserved_fd_count >= MAX_PRESERVED_FDS)
810 return -ENOMEM;
811 j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd;
812 j->preserved_fds[j->preserved_fd_count].child_fd = child_fd;
813 j->preserved_fd_count++;
814 return 0;
815}
816
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400817static void clear_seccomp_options(struct minijail *j)
818{
819 j->flags.seccomp_filter = 0;
820 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400821 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400822 j->filter_len = 0;
823 j->filter_prog = NULL;
824 j->flags.no_new_privs = 0;
825}
826
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400827static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400828{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400829 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400830 /*
831 * |errno| will be set to EINVAL when seccomp has not been
832 * compiled into the kernel. On certain platforms and kernel
833 * versions this is not a fatal failure. In that case, and only
834 * in that case, disable seccomp and skip loading the filters.
835 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400836 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400837 warn("not loading seccomp filters, seccomp filter not "
838 "supported");
839 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400840 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700841 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400842 /*
843 * If |errno| != EINVAL or seccomp_can_softfail() is false,
844 * we can proceed. Worst case scenario minijail_enter() will
845 * abort() if seccomp fails.
846 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700847 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400848 if (j->flags.seccomp_filter_tsync) {
849 /* Are the seccomp(2) syscall and the TSYNC option supported? */
850 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
851 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
852 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400853 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
854 warn("seccomp(2) syscall not supported");
855 clear_seccomp_options(j);
856 return 0;
857 } else if (saved_errno == EINVAL &&
858 seccomp_can_softfail()) {
859 warn(
860 "seccomp filter thread sync not supported");
861 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400862 return 0;
863 }
864 /*
865 * Similar logic here. If seccomp_can_softfail() is
866 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
867 * we can proceed. Worst case scenario minijail_enter()
868 * will abort() if seccomp or TSYNC fail.
869 */
870 }
871 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400872 return 1;
873}
874
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700875static int parse_seccomp_filters(struct minijail *j, const char *filename,
876 FILE *policy_file)
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400877{
878 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400879 int use_ret_trap =
880 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
881 int allow_logging = j->flags.seccomp_filter_logging;
882
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700883 if (compile_filter(filename, policy_file, fprog, use_ret_trap,
884 allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400885 free(fprog);
886 return -1;
887 }
888
889 j->filter_len = fprog->len;
890 j->filter_prog = fprog;
891 return 0;
892}
893
894void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
895{
896 if (!seccomp_should_parse_filters(j))
897 return;
898
Elly Jonese1749eb2011-10-07 13:54:59 -0400899 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800900 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700901 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400902 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800903
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700904 if (parse_seccomp_filters(j, path, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700905 die("failed to compile seccomp filter BPF program in '%s'",
906 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800907 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400908 fclose(file);
909}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800910
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400911void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
912{
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700913 char *fd_path, *path;
914 FILE *file;
915
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400916 if (!seccomp_should_parse_filters(j))
917 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800918
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700919 file = fdopen(fd, "r");
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400920 if (!file) {
921 pdie("failed to associate stream with fd %d", fd);
922 }
923
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700924 if (asprintf(&fd_path, "/proc/self/fd/%d", fd) == -1)
925 pdie("failed to create path for fd %d", fd);
926 path = realpath(fd_path, NULL);
927 if (path == NULL)
928 pwarn("failed to get path of fd %d", fd);
929 free(fd_path);
930
931 if (parse_seccomp_filters(j, path ? path : "<fd>", file) != 0) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400932 die("failed to compile seccomp filter BPF program from fd %d",
933 fd);
934 }
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700935 free(path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400936 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500937}
938
Andrew Brestickereac28942015-11-11 16:04:46 -0800939int API minijail_use_alt_syscall(struct minijail *j, const char *table)
940{
941 j->alt_syscall_table = strdup(table);
942 if (!j->alt_syscall_table)
943 return -ENOMEM;
944 j->flags.alt_syscall = 1;
945 return 0;
946}
947
Will Drewryf89aef52011-09-16 16:48:57 -0500948struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400949 size_t available;
950 size_t total;
951 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500952};
953
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800954void marshal_state_init(struct marshal_state *state, char *buf,
955 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400956{
957 state->available = available;
958 state->buf = buf;
959 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500960}
961
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800962void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400963{
964 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500965
Elly Jonese1749eb2011-10-07 13:54:59 -0400966 /* Up to |available| will be written. */
967 if (copy_len) {
968 memcpy(state->buf, src, copy_len);
969 state->buf += copy_len;
970 state->available -= copy_len;
971 }
972 /* |total| will contain the expected length. */
973 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500974}
975
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400976void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700977{
978 marshal_append(state, m->src, strlen(m->src) + 1);
979 marshal_append(state, m->dest, strlen(m->dest) + 1);
980 marshal_append(state, m->type, strlen(m->type) + 1);
981 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
982 if (m->has_data)
983 marshal_append(state, m->data, strlen(m->data) + 1);
984 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
985}
986
Will Drewry6ac91122011-10-21 16:38:58 -0500987void minijail_marshal_helper(struct marshal_state *state,
988 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400989{
Dylan Reid648b2202015-10-23 00:50:00 -0700990 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800991 size_t i;
992
Elly Jonese1749eb2011-10-07 13:54:59 -0400993 marshal_append(state, (char *)j, sizeof(*j));
994 if (j->user)
995 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800996 if (j->suppl_gid_list) {
997 marshal_append(state, j->suppl_gid_list,
998 j->suppl_gid_count * sizeof(gid_t));
999 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001000 if (j->chrootdir)
1001 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001002 if (j->hostname)
1003 marshal_append(state, j->hostname, strlen(j->hostname) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -08001004 if (j->alt_syscall_table) {
1005 marshal_append(state, j->alt_syscall_table,
1006 strlen(j->alt_syscall_table) + 1);
1007 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001008 if (j->flags.seccomp_filter && j->filter_prog) {
1009 struct sock_fprog *fp = j->filter_prog;
1010 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001011 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -04001012 }
Dylan Reid648b2202015-10-23 00:50:00 -07001013 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001014 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001015 }
Dylan Reid605ce7f2016-01-19 19:21:00 -08001016 for (i = 0; i < j->cgroup_count; ++i)
1017 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -05001018}
1019
Will Drewry6ac91122011-10-21 16:38:58 -05001020size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001021{
1022 struct marshal_state state;
1023 marshal_state_init(&state, NULL, 0);
1024 minijail_marshal_helper(&state, j);
1025 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -05001026}
1027
Elly Jonese1749eb2011-10-07 13:54:59 -04001028int minijail_marshal(const struct minijail *j, char *buf, size_t available)
1029{
1030 struct marshal_state state;
1031 marshal_state_init(&state, buf, available);
1032 minijail_marshal_helper(&state, j);
1033 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -05001034}
1035
Elly Jonese1749eb2011-10-07 13:54:59 -04001036int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
1037{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -08001038 size_t i;
1039 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -05001040 int ret = -EINVAL;
1041
Elly Jonese1749eb2011-10-07 13:54:59 -04001042 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001043 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001044 memcpy((void *)j, serialized, sizeof(*j));
1045 serialized += sizeof(*j);
1046 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001047
Will Drewrybee7ba72011-10-21 20:47:01 -05001048 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001049 j->pid_file_path = NULL;
1050 j->uidmap = NULL;
1051 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001052 j->mounts_head = NULL;
1053 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001054 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001055 j->hooks_head = NULL;
1056 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001057
Elly Jonese1749eb2011-10-07 13:54:59 -04001058 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001059 char *user = consumestr(&serialized, &length);
1060 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001061 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001062 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001063 if (!j->user)
1064 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001065 }
Will Drewryf89aef52011-09-16 16:48:57 -05001066
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001067 if (j->suppl_gid_list) { /* stale pointer */
1068 if (j->suppl_gid_count > NGROUPS_MAX) {
1069 goto bad_gid_list;
1070 }
1071 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1072 void *gid_list_bytes =
1073 consumebytes(gid_list_size, &serialized, &length);
1074 if (!gid_list_bytes)
1075 goto bad_gid_list;
1076
1077 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1078 if (!j->suppl_gid_list)
1079 goto bad_gid_list;
1080
1081 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1082 }
1083
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001084 if (j->chrootdir) { /* stale pointer */
1085 char *chrootdir = consumestr(&serialized, &length);
1086 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001087 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001088 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001089 if (!j->chrootdir)
1090 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001091 }
1092
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001093 if (j->hostname) { /* stale pointer */
1094 char *hostname = consumestr(&serialized, &length);
1095 if (!hostname)
1096 goto bad_hostname;
1097 j->hostname = strdup(hostname);
1098 if (!j->hostname)
1099 goto bad_hostname;
1100 }
1101
Andrew Brestickereac28942015-11-11 16:04:46 -08001102 if (j->alt_syscall_table) { /* stale pointer */
1103 char *alt_syscall_table = consumestr(&serialized, &length);
1104 if (!alt_syscall_table)
1105 goto bad_syscall_table;
1106 j->alt_syscall_table = strdup(alt_syscall_table);
1107 if (!j->alt_syscall_table)
1108 goto bad_syscall_table;
1109 }
1110
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001111 if (j->flags.seccomp_filter && j->filter_len > 0) {
1112 size_t ninstrs = j->filter_len;
1113 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1114 ninstrs > USHRT_MAX)
1115 goto bad_filters;
1116
1117 size_t program_len = ninstrs * sizeof(struct sock_filter);
1118 void *program = consumebytes(program_len, &serialized, &length);
1119 if (!program)
1120 goto bad_filters;
1121
1122 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001123 if (!j->filter_prog)
1124 goto bad_filters;
1125
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001126 j->filter_prog->len = ninstrs;
1127 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001128 if (!j->filter_prog->filter)
1129 goto bad_filter_prog_instrs;
1130
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001131 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001132 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001133
Dylan Reid648b2202015-10-23 00:50:00 -07001134 count = j->mounts_count;
1135 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001136 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001137 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001138 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001139 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001140 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001141 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001142 const char *src = consumestr(&serialized, &length);
1143 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001144 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001145 dest = consumestr(&serialized, &length);
1146 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001147 goto bad_mounts;
1148 type = consumestr(&serialized, &length);
1149 if (!type)
1150 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001151 has_data = consumebytes(sizeof(*has_data), &serialized,
1152 &length);
1153 if (!has_data)
1154 goto bad_mounts;
1155 if (*has_data) {
1156 data = consumestr(&serialized, &length);
1157 if (!data)
1158 goto bad_mounts;
1159 }
Dylan Reid648b2202015-10-23 00:50:00 -07001160 flags = consumebytes(sizeof(*flags), &serialized, &length);
1161 if (!flags)
1162 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001163 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001164 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001165 }
1166
Dylan Reid605ce7f2016-01-19 19:21:00 -08001167 count = j->cgroup_count;
1168 j->cgroup_count = 0;
1169 for (i = 0; i < count; ++i) {
1170 char *cgroup = consumestr(&serialized, &length);
1171 if (!cgroup)
1172 goto bad_cgroups;
1173 j->cgroups[i] = strdup(cgroup);
1174 if (!j->cgroups[i])
1175 goto bad_cgroups;
1176 ++j->cgroup_count;
1177 }
1178
Elly Jonese1749eb2011-10-07 13:54:59 -04001179 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001180
Dylan Reid605ce7f2016-01-19 19:21:00 -08001181bad_cgroups:
Mike Frysingerac08a682017-10-10 02:04:50 -04001182 free_mounts_list(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001183 for (i = 0; i < j->cgroup_count; ++i)
1184 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001185bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001186 if (j->flags.seccomp_filter && j->filter_len > 0) {
1187 free(j->filter_prog->filter);
1188 free(j->filter_prog);
1189 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001190bad_filter_prog_instrs:
1191 if (j->filter_prog)
1192 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001193bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001194 if (j->alt_syscall_table)
1195 free(j->alt_syscall_table);
1196bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001197 if (j->chrootdir)
1198 free(j->chrootdir);
1199bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001200 if (j->hostname)
1201 free(j->hostname);
1202bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001203 if (j->suppl_gid_list)
1204 free(j->suppl_gid_list);
1205bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001206 if (j->user)
1207 free(j->user);
1208clear_pointers:
1209 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001210 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001211 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001212 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001213 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001214 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001215out:
1216 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001217}
1218
Mike Frysinger33ffef32017-01-13 19:53:19 -05001219struct dev_spec {
1220 const char *name;
1221 mode_t mode;
1222 dev_t major, minor;
1223};
1224
1225static const struct dev_spec device_nodes[] = {
1226 {
1227 "null",
1228 S_IFCHR | 0666, 1, 3,
1229 },
1230 {
1231 "zero",
1232 S_IFCHR | 0666, 1, 5,
1233 },
1234 {
1235 "full",
1236 S_IFCHR | 0666, 1, 7,
1237 },
1238 {
1239 "urandom",
1240 S_IFCHR | 0444, 1, 9,
1241 },
1242 {
1243 "tty",
1244 S_IFCHR | 0666, 5, 0,
1245 },
1246};
1247
1248struct dev_sym_spec {
1249 const char *source, *dest;
1250};
1251
1252static const struct dev_sym_spec device_symlinks[] = {
1253 { "ptmx", "pts/ptmx", },
1254 { "fd", "/proc/self/fd", },
1255 { "stdin", "fd/0", },
1256 { "stdout", "fd/1", },
1257 { "stderr", "fd/2", },
1258};
1259
1260/*
1261 * Clean up the temporary dev path we had setup previously. In case of errors,
1262 * we don't want to go leaking empty tempdirs.
1263 */
1264static void mount_dev_cleanup(char *dev_path)
1265{
1266 umount2(dev_path, MNT_DETACH);
1267 rmdir(dev_path);
1268 free(dev_path);
1269}
1270
1271/*
1272 * Set up the pseudo /dev path at the temporary location.
1273 * See mount_dev_finalize for more details.
1274 */
1275static int mount_dev(char **dev_path_ret)
1276{
1277 int ret;
1278 int dev_fd;
1279 size_t i;
1280 mode_t mask;
1281 char *dev_path;
1282
1283 /*
1284 * Create a temp path for the /dev init. We'll relocate this to the
1285 * final location later on in the startup process.
1286 */
1287 dev_path = *dev_path_ret = strdup("/tmp/minijail.dev.XXXXXX");
1288 if (dev_path == NULL || mkdtemp(dev_path) == NULL)
1289 pdie("could not create temp path for /dev");
1290
1291 /* Set up the empty /dev mount point first. */
1292 ret = mount("minijail-devfs", dev_path, "tmpfs",
1293 MS_NOEXEC | MS_NOSUID, "size=5M,mode=755");
1294 if (ret) {
1295 rmdir(dev_path);
1296 return ret;
1297 }
1298
1299 /* We want to set the mode directly from the spec. */
1300 mask = umask(0);
1301
1302 /* Get a handle to the temp dev path for *at funcs below. */
1303 dev_fd = open(dev_path, O_DIRECTORY|O_PATH|O_CLOEXEC);
1304 if (dev_fd < 0) {
1305 ret = 1;
1306 goto done;
1307 }
1308
1309 /* Create all the nodes in /dev. */
1310 for (i = 0; i < ARRAY_SIZE(device_nodes); ++i) {
1311 const struct dev_spec *ds = &device_nodes[i];
1312 ret = mknodat(dev_fd, ds->name, ds->mode,
1313 makedev(ds->major, ds->minor));
1314 if (ret)
1315 goto done;
1316 }
1317
1318 /* Create all the symlinks in /dev. */
1319 for (i = 0; i < ARRAY_SIZE(device_symlinks); ++i) {
1320 const struct dev_sym_spec *ds = &device_symlinks[i];
1321 ret = symlinkat(ds->dest, dev_fd, ds->source);
1322 if (ret)
1323 goto done;
1324 }
1325
1326 /* Restore old mask. */
1327 done:
1328 close(dev_fd);
1329 umask(mask);
1330
1331 if (ret)
1332 mount_dev_cleanup(dev_path);
1333
1334 return ret;
1335}
1336
1337/*
1338 * Relocate the temporary /dev mount to its final /dev place.
1339 * We have to do this two step process so people can bind mount extra
1340 * /dev paths like /dev/log.
1341 */
1342static int mount_dev_finalize(const struct minijail *j, char *dev_path)
1343{
1344 int ret = -1;
1345 char *dest = NULL;
1346
1347 /* Unmount the /dev mount if possible. */
1348 if (umount2("/dev", MNT_DETACH))
1349 goto done;
1350
1351 if (asprintf(&dest, "%s/dev", j->chrootdir ? : "") < 0)
1352 goto done;
1353
1354 if (mount(dev_path, dest, NULL, MS_MOVE, NULL))
1355 goto done;
1356
1357 ret = 0;
1358 done:
1359 free(dest);
1360 mount_dev_cleanup(dev_path);
1361
1362 return ret;
1363}
1364
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001365/*
1366 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001367 * @j Minijail these mounts are for
1368 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001369 *
1370 * Returns 0 for success.
1371 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001372static int mount_one(const struct minijail *j, struct mountpoint *m,
1373 const char *dev_path)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001374{
Dylan Reid648b2202015-10-23 00:50:00 -07001375 int ret;
1376 char *dest;
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001377 int remount = 0;
1378 unsigned long original_mnt_flags = 0;
Dylan Reid648b2202015-10-23 00:50:00 -07001379
Mike Frysinger33ffef32017-01-13 19:53:19 -05001380 /* We assume |dest| has a leading "/". */
1381 if (dev_path && strncmp("/dev/", m->dest, 5) == 0) {
1382 /* Since the temp path is rooted at /dev, skip that dest part. */
1383 if (asprintf(&dest, "%s%s", dev_path, m->dest + 4) < 0)
1384 return -ENOMEM;
1385 } else {
Mike Frysingerac08a682017-10-10 02:04:50 -04001386 if (asprintf(&dest, "%s%s", j->chrootdir ?: "", m->dest) < 0)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001387 return -ENOMEM;
1388 }
Dylan Reid648b2202015-10-23 00:50:00 -07001389
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001390 ret =
1391 setup_mount_destination(m->src, dest, j->uid, j->gid,
1392 (m->flags & MS_BIND), &original_mnt_flags);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001393 if (ret) {
yusukes1b32f852018-03-05 10:24:58 -08001394 warn("creating mount target '%s' failed", dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001395 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001396 }
Dylan Reideec77962016-06-30 19:35:10 -07001397
Dylan Reid648b2202015-10-23 00:50:00 -07001398 /*
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001399 * Bind mounts that change the 'ro' flag have to be remounted since
1400 * 'bind' and other flags can't both be specified in the same command.
1401 * Remount after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001402 */
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001403 if ((m->flags & MS_BIND) &&
1404 ((m->flags & MS_RDONLY) != (original_mnt_flags & MS_RDONLY))) {
1405 remount = 1;
1406 /*
1407 * Restrict the mount flags to those that are user-settable in a
1408 * MS_REMOUNT request, but excluding MS_RDONLY. The
1409 * user-requested mount flags will dictate whether the remount
1410 * will have that flag or not.
1411 */
1412 original_mnt_flags &= (MS_USER_SETTABLE_MASK & ~MS_RDONLY);
Elly Jonesa1059632011-12-15 15:17:07 -05001413 }
Dylan Reid648b2202015-10-23 00:50:00 -07001414
Dylan Reid81e23972016-05-18 14:06:35 -07001415 ret = mount(m->src, dest, m->type, m->flags, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001416 if (ret) {
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001417 pwarn("bind: %s -> %s flags=%#lx", m->src, dest, m->flags);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001418 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001419 }
Dylan Reid648b2202015-10-23 00:50:00 -07001420
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001421 if (remount) {
1422 ret =
1423 mount(m->src, dest, NULL,
1424 m->flags | original_mnt_flags | MS_REMOUNT, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001425 if (ret) {
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001426 pwarn("bind remount: %s -> %s flags=%#lx", m->src, dest,
1427 m->flags | original_mnt_flags | MS_REMOUNT);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001428 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001429 }
Dylan Reid648b2202015-10-23 00:50:00 -07001430 }
1431
Elly Jones51a5b6c2011-10-12 19:09:26 -04001432 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001433 if (m->next)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001434 return mount_one(j, m->next, dev_path);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001435 return 0;
1436
1437error:
1438 free(dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001439 return ret;
1440}
1441
Mike Frysingerac08a682017-10-10 02:04:50 -04001442static void process_mounts_or_die(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001443{
Mike Frysingerac08a682017-10-10 02:04:50 -04001444 /*
1445 * We have to mount /dev first in case there are bind mounts from
1446 * the original /dev into the new unique tmpfs one.
1447 */
1448 char *dev_path = NULL;
1449 if (j->flags.mount_dev && mount_dev(&dev_path))
1450 pdie("mount_dev failed");
Dylan Reid648b2202015-10-23 00:50:00 -07001451
Mike Frysingerac08a682017-10-10 02:04:50 -04001452 if (j->mounts_head && mount_one(j, j->mounts_head, dev_path)) {
1453 if (dev_path) {
1454 int saved_errno = errno;
1455 mount_dev_cleanup(dev_path);
1456 errno = saved_errno;
1457 }
1458 pdie("mount_one failed");
1459 }
Mike Frysinger33ffef32017-01-13 19:53:19 -05001460
1461 /*
Mike Frysingerac08a682017-10-10 02:04:50 -04001462 * Once all bind mounts have been processed, move the temp dev to
1463 * its final /dev home.
Mike Frysinger33ffef32017-01-13 19:53:19 -05001464 */
1465 if (j->flags.mount_dev && mount_dev_finalize(j, dev_path))
Mike Frysingerac08a682017-10-10 02:04:50 -04001466 pdie("mount_dev_finalize failed");
1467}
Elly Jones51a5b6c2011-10-12 19:09:26 -04001468
Mike Frysingerac08a682017-10-10 02:04:50 -04001469static int enter_chroot(const struct minijail *j)
1470{
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001471 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1472
Elly Jones51a5b6c2011-10-12 19:09:26 -04001473 if (chroot(j->chrootdir))
1474 return -errno;
1475
1476 if (chdir("/"))
1477 return -errno;
1478
1479 return 0;
1480}
1481
Mike Frysingerac08a682017-10-10 02:04:50 -04001482static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001483{
Mike Frysingerac08a682017-10-10 02:04:50 -04001484 int oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001485
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001486 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1487
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001488 /*
1489 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001490 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001491 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001492 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001493 if (oldroot < 0)
1494 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001495 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001496 if (newroot < 0)
1497 pdie("failed to open %s for fchdir", j->chrootdir);
1498
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001499 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001500 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001501 * do a self bind mount.
1502 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001503 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1504 pdie("failed to bind mount '%s'", j->chrootdir);
1505 if (chdir(j->chrootdir))
1506 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001507 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001508 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001509
1510 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001511 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001512 * change to the old root and unmount it.
1513 */
1514 if (fchdir(oldroot))
1515 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001516
1517 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05001518 * If skip_remount_private was enabled for minijail_enter(),
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001519 * there could be a shared mount point under |oldroot|. In that case,
1520 * mounts under this shared mount point will be unmounted below, and
1521 * this unmounting will propagate to the original mount namespace
1522 * (because the mount point is shared). To prevent this unexpected
1523 * unmounting, remove these mounts from their peer groups by recursively
1524 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001525 */
1526 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001527 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001528 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001529 if (umount2(".", MNT_DETACH))
1530 pdie("umount(/)");
1531 /* Change back to the new root. */
1532 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001533 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001534 if (close(oldroot))
1535 return -errno;
1536 if (close(newroot))
1537 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001538 if (chroot("/"))
1539 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001540 /* Set correct CWD for getcwd(3). */
1541 if (chdir("/"))
1542 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001543
1544 return 0;
1545}
1546
Martin Pelikánab9eb442017-01-25 11:53:58 +11001547static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001548{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001549 const char fmt[] = "size=%zu,mode=1777";
1550 /* Count for the user storing ULLONG_MAX literally + extra space. */
1551 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1552 int ret;
1553
1554 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1555
1556 if (ret <= 0)
1557 pdie("tmpfs size spec error");
1558 else if ((size_t)ret >= sizeof(data))
1559 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001560 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001561 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001562}
1563
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001564static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001565{
1566 const char *kProcPath = "/proc";
1567 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001568 /*
1569 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001570 * /proc in our namespace, which means using MS_REMOUNT here would
1571 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001572 * namespace (!). Instead, remove their mount from our namespace lazily
1573 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001574 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001575 if (umount2(kProcPath, MNT_DETACH)) {
1576 /*
1577 * If we are in a new user namespace, umount(2) will fail.
1578 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1579 */
1580 if (j->flags.userns) {
1581 info("umount(/proc, MNT_DETACH) failed, "
1582 "this is expected when using user namespaces");
1583 } else {
1584 return -errno;
1585 }
1586 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001587 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001588 return -errno;
1589 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001590}
1591
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001592static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001593{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001594 kill(j->initpid, SIGKILL);
1595 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001596}
1597
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001598static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001599{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001600 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001601 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001602}
1603
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001604static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001605{
1606 size_t i;
1607
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001608 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001609 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001610 kill_child_and_die(j, "failed to add to cgroups");
1611 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001612}
1613
Dylan Reid0f72ef42017-06-06 15:42:49 -07001614static void set_rlimits_or_die(const struct minijail *j)
1615{
1616 size_t i;
1617
1618 for (i = 0; i < j->rlimit_count; ++i) {
1619 struct rlimit limit;
1620 limit.rlim_cur = j->rlimits[i].cur;
1621 limit.rlim_max = j->rlimits[i].max;
1622 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1623 kill_child_and_die(j, "failed to set rlimit");
1624 }
1625}
1626
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001627static void write_ugid_maps_or_die(const struct minijail *j)
1628{
1629 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1630 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001631 if (j->gidmap && j->flags.disable_setgroups) {
1632 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1633 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001634 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001635 if (ret == -ENOENT) {
1636 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1637 warn("could not disable setgroups(2)");
1638 } else
1639 kill_child_and_die(j, "failed to disable setgroups(2)");
1640 }
1641 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001642 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1643 kill_child_and_die(j, "failed to write gid_map");
1644}
1645
1646static void enter_user_namespace(const struct minijail *j)
1647{
Luis Hector Chavez71323552017-09-05 09:17:22 -07001648 int uid = j->flags.uid ? j->uid : 0;
1649 int gid = j->flags.gid ? j->gid : 0;
1650 if (j->gidmap && setresgid(gid, gid, gid)) {
1651 pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid,
1652 gid);
1653 }
1654 if (j->uidmap && setresuid(uid, uid, uid)) {
1655 pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid,
1656 uid);
1657 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001658}
1659
1660static void parent_setup_complete(int *pipe_fds)
1661{
1662 close(pipe_fds[0]);
1663 close(pipe_fds[1]);
1664}
1665
1666/*
1667 * wait_for_parent_setup: Called by the child process to wait for any
1668 * further parent-side setup to complete before continuing.
1669 */
1670static void wait_for_parent_setup(int *pipe_fds)
1671{
1672 char buf;
1673
1674 close(pipe_fds[1]);
1675
1676 /* Wait for parent to complete setup and close the pipe. */
1677 if (read(pipe_fds[0], &buf, 1) != 0)
1678 die("failed to sync with parent");
1679 close(pipe_fds[0]);
1680}
1681
1682static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001683{
Lutz Justen13807cb2017-01-03 17:11:55 +01001684 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1685 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001686 die("can only do one of inherit, keep, or set supplementary "
1687 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001688 }
1689
Lutz Justen13807cb2017-01-03 17:11:55 +01001690 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001691 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001692 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001693 } else if (j->flags.set_suppl_gids) {
1694 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001695 pdie("setgroups(suppl_gids) failed");
Luis Hector Chavez71323552017-09-05 09:17:22 -07001696 } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001697 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001698 * Only attempt to clear supplementary groups if we are changing
Luis Hector Chavez71323552017-09-05 09:17:22 -07001699 * users or groups, and if the caller did not request to disable
1700 * setgroups (used when entering a user namespace as a
1701 * non-privileged user).
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001702 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001703 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001704 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001705 }
1706
1707 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001708 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001709
1710 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001711 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001712}
1713
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001714static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1715{
1716 const uint64_t one = 1;
1717 unsigned int i;
1718 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1719 if (keep_mask & (one << i))
1720 continue;
1721 if (prctl(PR_CAPBSET_DROP, i))
1722 pdie("could not drop capability from bounding set");
1723 }
1724}
1725
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001726static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001727{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001728 if (!j->flags.use_caps)
1729 return;
1730
Elly Jonese1749eb2011-10-07 13:54:59 -04001731 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001732 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001733 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001734 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001735 unsigned int i;
1736 if (!caps)
1737 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001738 if (cap_clear(caps))
1739 die("can't clear caps");
1740
1741 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001742 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001743 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001744 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001745 flag[0] = i;
1746 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001747 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001748 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001749 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001750 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001751 die("can't add inheritable cap");
1752 }
1753 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001754 die("can't apply initial cleaned capset");
1755
1756 /*
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001757 * Instead of dropping the bounding set first, do it here in case
Kees Cook323878a2013-02-05 15:35:24 -08001758 * the caller had a more permissive bounding set which could
1759 * have been used above to raise a capability that wasn't already
1760 * present. This requires CAP_SETPCAP, so we raised/kept it above.
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001761 *
1762 * However, if we're asked to skip setting *and* locking the
1763 * SECURE_NOROOT securebit, also skip dropping the bounding set.
1764 * If the caller wants to regain all capabilities when executing a
1765 * set-user-ID-root program, allow them to do so. The default behavior
1766 * (i.e. the behavior without |securebits_skip_mask| set) will still put
1767 * the jailed process tree in a capabilities-only environment.
1768 *
1769 * We check the negated skip mask for SECURE_NOROOT and
1770 * SECURE_NOROOT_LOCKED. If the bits are set in the negated mask they
1771 * will *not* be skipped in lock_securebits(), and therefore we should
1772 * drop the bounding set.
Kees Cook323878a2013-02-05 15:35:24 -08001773 */
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001774 if (secure_noroot_set_and_locked(~j->securebits_skip_mask)) {
1775 drop_capbset(j->caps, last_valid_cap);
1776 } else {
1777 warn("SECURE_NOROOT not set, not dropping bounding set");
1778 }
Kees Cook323878a2013-02-05 15:35:24 -08001779
1780 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001781 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001782 flag[0] = CAP_SETPCAP;
1783 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1784 die("can't clear effective cap");
1785 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1786 die("can't clear permitted cap");
1787 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1788 die("can't clear inheritable cap");
1789 }
1790
1791 if (cap_set_proc(caps))
1792 die("can't apply final cleaned capset");
1793
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001794 /*
1795 * If ambient capabilities are supported, clear all capabilities first,
1796 * then raise the requested ones.
1797 */
1798 if (j->flags.set_ambient_caps) {
1799 if (!cap_ambient_supported()) {
1800 pdie("ambient capabilities not supported");
1801 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001802 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1803 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001804 pdie("can't clear ambient capabilities");
1805 }
1806
1807 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1808 if (!(j->caps & (one << i)))
1809 continue;
1810
1811 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1812 0) != 0) {
1813 pdie("prctl(PR_CAP_AMBIENT, "
1814 "PR_CAP_AMBIENT_RAISE, %u) failed",
1815 i);
1816 }
1817 }
1818 }
1819
Kees Cook323878a2013-02-05 15:35:24 -08001820 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001821}
1822
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001823static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001824{
1825 /*
1826 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1827 * in the kernel source tree for an explanation of the parameters.
1828 */
1829 if (j->flags.no_new_privs) {
1830 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1831 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1832 }
1833
1834 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001835 * Code running with ASan
1836 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1837 * will make system calls not included in the syscall filter policy,
1838 * which will likely crash the program. Skip setting seccomp filter in
1839 * that case.
1840 * 'running_with_asan()' has no inputs and is completely defined at
1841 * build time, so this cannot be used by an attacker to skip setting
1842 * seccomp filter.
1843 */
1844 if (j->flags.seccomp_filter && running_with_asan()) {
1845 warn("running with ASan, not setting seccomp filter");
1846 return;
1847 }
1848
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001849 if (j->flags.seccomp_filter) {
1850 if (j->flags.seccomp_filter_logging) {
1851 /*
1852 * If logging seccomp filter failures,
1853 * install the SIGSYS handler first.
1854 */
1855 if (install_sigsys_handler())
1856 pdie("failed to install SIGSYS handler");
1857 warn("logging seccomp filter failures");
1858 } else if (j->flags.seccomp_filter_tsync) {
1859 /*
1860 * If setting thread sync,
1861 * reset the SIGSYS signal handler so that
1862 * the entire thread group is killed.
1863 */
1864 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1865 pdie("failed to reset SIGSYS disposition");
1866 info("reset SIGSYS disposition");
1867 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001868 }
1869
1870 /*
1871 * Install the syscall filter.
1872 */
1873 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001874 if (j->flags.seccomp_filter_tsync) {
1875 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1876 SECCOMP_FILTER_FLAG_TSYNC,
1877 j->filter_prog)) {
1878 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001879 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001880 } else {
1881 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1882 j->filter_prog)) {
1883 pdie("prctl(seccomp_filter) failed");
1884 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001885 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001886 }
1887}
1888
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001889static pid_t forward_pid = -1;
1890
Mike Frysinger33d051a2018-05-30 16:41:10 -04001891static void forward_signal(int sig,
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04001892 siginfo_t *siginfo attribute_unused,
1893 void *void_context attribute_unused)
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001894{
1895 if (forward_pid != -1) {
Mike Frysinger33d051a2018-05-30 16:41:10 -04001896 kill(forward_pid, sig);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001897 }
1898}
1899
1900static void install_signal_handlers(void)
1901{
1902 struct sigaction act;
1903
1904 memset(&act, 0, sizeof(act));
1905 act.sa_sigaction = &forward_signal;
1906 act.sa_flags = SA_SIGINFO | SA_RESTART;
1907
1908 /* Handle all signals, except SIGCHLD. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04001909 for (int sig = 1; sig < NSIG; sig++) {
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001910 /*
1911 * We don't care if we get EINVAL: that just means that we
1912 * can't handle this signal, so let's skip it and continue.
1913 */
Mike Frysinger33d051a2018-05-30 16:41:10 -04001914 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001915 }
1916 /* Reset SIGCHLD's handler. */
1917 signal(SIGCHLD, SIG_DFL);
1918
1919 /* Handle real-time signals. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04001920 for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++) {
1921 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001922 }
1923}
1924
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001925static const char *lookup_hook_name(minijail_hook_event_t event)
1926{
1927 switch (event) {
1928 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
1929 return "pre-drop-caps";
1930 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
1931 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001932 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
1933 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001934 case MINIJAIL_HOOK_EVENT_MAX:
1935 /*
1936 * Adding this in favor of a default case to force the
1937 * compiler to error out if a new enum value is added.
1938 */
1939 break;
1940 }
1941 return "unknown";
1942}
1943
1944static void run_hooks_or_die(const struct minijail *j,
1945 minijail_hook_event_t event)
1946{
1947 int rc;
1948 int hook_index = 0;
1949 for (struct hook *c = j->hooks_head; c; c = c->next) {
1950 if (c->event != event)
1951 continue;
1952 rc = c->hook(c->payload);
1953 if (rc != 0) {
1954 errno = -rc;
1955 pdie("%s hook (index %d) failed",
1956 lookup_hook_name(event), hook_index);
1957 }
1958 /* Only increase the index within the same hook event type. */
1959 ++hook_index;
1960 }
1961}
1962
Will Drewry6ac91122011-10-21 16:38:58 -05001963void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001964{
Dylan Reidf682d472015-09-17 21:39:07 -07001965 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001966 * If we're dropping caps, get the last valid cap from /proc now,
1967 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001968 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001969 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001970 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001971 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001972
Elly Jonese1749eb2011-10-07 13:54:59 -04001973 if (j->flags.pids)
1974 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001975 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001976
Lutz Justen13807cb2017-01-03 17:11:55 +01001977 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001978 die("cannot inherit supplementary groups without setting a "
1979 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001980
Elly Jonesdd3e8512012-01-23 15:13:38 -05001981 /*
1982 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001983 * so we don't even try. If any of our operations fail, we abort() the
1984 * entire process.
1985 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001986 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001987 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001988
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001989 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001990 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001991 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001992 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05001993 * By default, remount all filesystems as private, unless
1994 * - Passed a specific remount mode, in which case remount with that,
1995 * - Asked not to remount at all, in which case skip the mount(2) call.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001996 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1997 */
Mike Frysinger785b1c32018-02-23 15:47:24 -05001998 if (j->remount_mode) {
1999 if (mount(NULL, "/", NULL, MS_REC | j->remount_mode, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002000 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
2001 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002002 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002003 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04002004
Dylan Reidf7942472015-11-18 17:55:26 -08002005 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002006 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08002007 }
2008
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002009 if (j->flags.uts) {
2010 if (unshare(CLONE_NEWUTS))
2011 pdie("unshare(CLONE_NEWUTS) failed");
2012
2013 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
2014 pdie("sethostname(%s) failed", j->hostname);
2015 }
2016
Dylan Reid1102f5a2015-09-15 11:52:20 -07002017 if (j->flags.enter_net) {
2018 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002019 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05002020 } else if (j->flags.net) {
2021 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002022 pdie("unshare(CLONE_NEWNET) failed");
2023 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07002024 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002025
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002026 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002027 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002028
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08002029 if (j->flags.new_session_keyring) {
2030 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
2031 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
2032 }
2033
Mike Frysingerac08a682017-10-10 02:04:50 -04002034 /* We have to process all the mounts before we chroot/pivot_root. */
2035 process_mounts_or_die(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002036
Mike Frysingerac08a682017-10-10 02:04:50 -04002037 if (j->flags.chroot && enter_chroot(j))
Mike Frysinger33ffef32017-01-13 19:53:19 -05002038 pdie("chroot");
Mike Frysinger33ffef32017-01-13 19:53:19 -05002039
Mike Frysingerac08a682017-10-10 02:04:50 -04002040 if (j->flags.pivot_root && enter_pivot_root(j))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08002041 pdie("pivot_root");
2042
Martin Pelikánab9eb442017-01-25 11:53:58 +11002043 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07002044 pdie("mount_tmp");
2045
Dylan Reid791f5772015-09-14 20:02:42 -07002046 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04002047 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04002048
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002049 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
2050
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002051 /*
2052 * If we're only dropping capabilities from the bounding set, but not
2053 * from the thread's (permitted|inheritable|effective) sets, do it now.
2054 */
2055 if (j->flags.capbset_drop) {
2056 drop_capbset(j->cap_bset, last_valid_cap);
2057 }
2058
2059 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002060 /*
2061 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04002062 * capability to change uids, our attempt to use setuid()
2063 * below will fail. Hang on to root caps across setuid(), then
2064 * lock securebits.
2065 */
2066 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002067 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002068
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -07002069 if (lock_securebits(j->securebits_skip_mask) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002070 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002071 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002072 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002073
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002074 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002075 /*
2076 * If we're setting no_new_privs, we can drop privileges
2077 * before setting seccomp filter. This way filter policies
2078 * don't need to allow privilege-dropping syscalls.
2079 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002080 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002081 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002082 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002083 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002084 /*
2085 * If we're not setting no_new_privs,
2086 * we need to set seccomp filter *before* dropping privileges.
2087 * WARNING: this means that filter policies *must* allow
2088 * setgroups()/setresgid()/setresuid() for dropping root and
2089 * capget()/capset()/prctl() for dropping caps.
2090 */
2091 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002092 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002093 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04002094 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002095
Elly Jonesdd3e8512012-01-23 15:13:38 -05002096 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08002097 * Select the specified alternate syscall table. The table must not
2098 * block prctl(2) if we're using seccomp as well.
2099 */
2100 if (j->flags.alt_syscall) {
2101 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002102 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08002103 }
2104
2105 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05002106 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04002107 * privilege-dropping syscalls :)
2108 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002109 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04002110 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002111 warn("seccomp not supported");
2112 return;
2113 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002114 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002115 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002116}
2117
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002118/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04002119static int init_exitstatus = 0;
2120
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04002121void init_term(int sig attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002122{
2123 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04002124}
2125
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002126void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04002127{
2128 pid_t pid;
2129 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002130 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002131 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002132 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002133 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002134 /*
2135 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04002136 * left inside our pid namespace or we get a signal.
2137 */
2138 if (pid == rootpid)
2139 init_exitstatus = status;
2140 }
2141 if (!WIFEXITED(init_exitstatus))
2142 _exit(MINIJAIL_ERR_INIT);
2143 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04002144}
2145
Will Drewry6ac91122011-10-21 16:38:58 -05002146int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002147{
2148 size_t sz = 0;
2149 size_t bytes = read(fd, &sz, sizeof(sz));
2150 char *buf;
2151 int r;
2152 if (sizeof(sz) != bytes)
2153 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002154 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04002155 return -E2BIG;
2156 buf = malloc(sz);
2157 if (!buf)
2158 return -ENOMEM;
2159 bytes = read(fd, buf, sz);
2160 if (bytes != sz) {
2161 free(buf);
2162 return -EINVAL;
2163 }
2164 r = minijail_unmarshal(j, buf, sz);
2165 free(buf);
2166 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002167}
2168
Will Drewry6ac91122011-10-21 16:38:58 -05002169int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04002170{
2171 char *buf;
2172 size_t sz = minijail_size(j);
2173 ssize_t written;
2174 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04002175
Elly Jonese1749eb2011-10-07 13:54:59 -04002176 if (!sz)
2177 return -EINVAL;
2178 buf = malloc(sz);
2179 r = minijail_marshal(j, buf, sz);
2180 if (r) {
2181 free(buf);
2182 return r;
2183 }
2184 /* Sends [size][minijail]. */
2185 written = write(fd, &sz, sizeof(sz));
2186 if (written != sizeof(sz)) {
2187 free(buf);
2188 return -EFAULT;
2189 }
2190 written = write(fd, buf, sz);
2191 if (written < 0 || (size_t) written != sz) {
2192 free(buf);
2193 return -EFAULT;
2194 }
2195 free(buf);
2196 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002197}
Elly Jonescd7a9042011-07-22 13:56:51 -04002198
Will Drewry6ac91122011-10-21 16:38:58 -05002199int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04002200{
Daniel Erat5b7a3182015-08-19 16:06:22 -06002201#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002202 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002203 return 0;
2204#else
Elly Jonese1749eb2011-10-07 13:54:59 -04002205 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
2206 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
2207 if (!newenv)
2208 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04002209
Elly Jonese1749eb2011-10-07 13:54:59 -04002210 /* Only insert a separating space if we have something to separate... */
2211 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
2212 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04002213
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002214 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002215 setenv(kLdPreloadEnvVar, newenv, 1);
2216 free(newenv);
2217 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002218#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04002219}
2220
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002221static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04002222{
2223 int r = pipe(fds);
2224 char fd_buf[11];
2225 if (r)
2226 return r;
2227 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2228 if (r <= 0)
2229 return -EINVAL;
2230 setenv(kFdEnvVar, fd_buf, 1);
2231 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05002232}
2233
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002234static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002235{
2236 const char *kFdPath = "/proc/self/fd";
2237
2238 DIR *d = opendir(kFdPath);
2239 struct dirent *dir_entry;
2240
2241 if (d == NULL)
2242 return -1;
2243 int dir_fd = dirfd(d);
2244 while ((dir_entry = readdir(d)) != NULL) {
2245 size_t i;
2246 char *end;
2247 bool should_close = true;
2248 const int fd = strtol(dir_entry->d_name, &end, 10);
2249
2250 if ((*end) != '\0') {
2251 continue;
2252 }
2253 /*
2254 * We might have set up some pipes that we want to share with
2255 * the parent process, and should not be closed.
2256 */
2257 for (i = 0; i < size; ++i) {
2258 if (fd == inheritable_fds[i]) {
2259 should_close = false;
2260 break;
2261 }
2262 }
2263 /* Also avoid closing the directory fd. */
2264 if (should_close && fd != dir_fd)
2265 close(fd);
2266 }
2267 closedir(d);
2268 return 0;
2269}
2270
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002271static int redirect_fds(struct minijail *j)
2272{
2273 size_t i, i2;
2274 int closeable;
2275 for (i = 0; i < j->preserved_fd_count; i++) {
2276 if (dup2(j->preserved_fds[i].parent_fd,
2277 j->preserved_fds[i].child_fd) == -1) {
2278 return -1;
2279 }
2280 }
2281 /*
2282 * After all fds have been duped, we are now free to close all parent
2283 * fds that are *not* child fds.
2284 */
2285 for (i = 0; i < j->preserved_fd_count; i++) {
2286 closeable = true;
2287 for (i2 = 0; i2 < j->preserved_fd_count; i2++) {
2288 closeable &= j->preserved_fds[i].parent_fd !=
2289 j->preserved_fds[i2].child_fd;
2290 }
2291 if (closeable)
2292 close(j->preserved_fds[i].parent_fd);
2293 }
2294 return 0;
2295}
2296
Dylan Reidacfb8be2017-08-25 12:56:51 -07002297/*
2298 * Structure that specifies how to start a minijail.
2299 *
Dylan Reid0412dcc2017-08-24 11:33:15 -07002300 * filename - The program to exec in the child. Required if `exec_in_child` = 1.
2301 * argv - Arguments for the child program. Required if `exec_in_child` = 1.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002302 * use_preload - If true use LD_PRELOAD.
Dylan Reid0412dcc2017-08-24 11:33:15 -07002303 * exec_in_child - If true, run `filename`. Otherwise, the child will return to
2304 * the caller.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002305 */
2306struct minijail_run_config {
2307 const char *filename;
2308 char *const *argv;
2309 int use_preload;
Dylan Reid0412dcc2017-08-24 11:33:15 -07002310 int exec_in_child;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002311};
2312
2313/*
2314 * Set of pointers to fill with values from minijail_run.
2315 * All arguments are allowed to be NULL if unused.
2316 *
2317 * pstdin_fd - Filled with stdin pipe if non-NULL.
2318 * pstdout_fd - Filled with stdout pipe if non-NULL.
2319 * pstderr_fd - Filled with stderr pipe if non-NULL.
2320 * pchild_pid - Filled with the pid of the child process if non-NULL.
2321 */
2322struct minijail_run_status {
2323 int *pstdin_fd;
2324 int *pstdout_fd;
2325 int *pstderr_fd;
2326 pid_t *pchild_pid;
2327};
2328
Dylan Reid18c49c82017-08-25 14:52:27 -07002329static int minijail_run_internal(struct minijail *j,
2330 const struct minijail_run_config *config,
2331 struct minijail_run_status *status_out);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002332
Will Drewry6ac91122011-10-21 16:38:58 -05002333int API minijail_run(struct minijail *j, const char *filename,
2334 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002335{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002336 struct minijail_run_config config = {
2337 .filename = filename,
2338 .argv = argv,
2339 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002340 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002341 };
2342 struct minijail_run_status status = {};
2343 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002344}
2345
2346int API minijail_run_pid(struct minijail *j, const char *filename,
2347 char *const argv[], pid_t *pchild_pid)
2348{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002349 struct minijail_run_config config = {
2350 .filename = filename,
2351 .argv = argv,
2352 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002353 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002354 };
2355 struct minijail_run_status status = {
2356 .pchild_pid = pchild_pid,
2357 };
2358 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002359}
2360
2361int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002362 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002363{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002364 struct minijail_run_config config = {
2365 .filename = filename,
2366 .argv = argv,
2367 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002368 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002369 };
2370 struct minijail_run_status status = {
2371 .pstdin_fd = pstdin_fd,
2372 };
2373 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002374}
2375
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002376int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002377 char *const argv[], pid_t *pchild_pid,
2378 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002379{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002380 struct minijail_run_config config = {
2381 .filename = filename,
2382 .argv = argv,
2383 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002384 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002385 };
2386 struct minijail_run_status status = {
2387 .pstdin_fd = pstdin_fd,
2388 .pstdout_fd = pstdout_fd,
2389 .pstderr_fd = pstderr_fd,
2390 .pchild_pid = pchild_pid,
2391 };
2392 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002393}
2394
2395int API minijail_run_no_preload(struct minijail *j, const char *filename,
2396 char *const argv[])
2397{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002398 struct minijail_run_config config = {
2399 .filename = filename,
2400 .argv = argv,
2401 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002402 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002403 };
2404 struct minijail_run_status status = {};
2405 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002406}
2407
Samuel Tan63187f42015-10-16 13:01:53 -07002408int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002409 const char *filename,
2410 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002411 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002412 int *pstdin_fd,
2413 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002414 int *pstderr_fd)
2415{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002416 struct minijail_run_config config = {
2417 .filename = filename,
2418 .argv = argv,
2419 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002420 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002421 };
2422 struct minijail_run_status status = {
2423 .pstdin_fd = pstdin_fd,
2424 .pstdout_fd = pstdout_fd,
2425 .pstderr_fd = pstderr_fd,
2426 .pchild_pid = pchild_pid,
2427 };
2428 return minijail_run_internal(j, &config, &status);
Samuel Tan63187f42015-10-16 13:01:53 -07002429}
2430
Dylan Reid0412dcc2017-08-24 11:33:15 -07002431pid_t API minijail_fork(struct minijail *j)
2432{
2433 struct minijail_run_config config = {};
2434 struct minijail_run_status status = {};
2435 return minijail_run_internal(j, &config, &status);
2436}
2437
Dylan Reid18c49c82017-08-25 14:52:27 -07002438static int minijail_run_internal(struct minijail *j,
2439 const struct minijail_run_config *config,
2440 struct minijail_run_status *status_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002441{
Elly Jonese1749eb2011-10-07 13:54:59 -04002442 char *oldenv, *oldenv_copy = NULL;
2443 pid_t child_pid;
2444 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002445 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002446 int stdout_fds[2];
2447 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08002448 int child_sync_pipe_fds[2];
2449 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002450 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002451 /* We need to remember this across the minijail_preexec() call. */
2452 int pid_namespace = j->flags.pids;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -07002453 /*
2454 * Create an init process if we are entering a pid namespace, unless the
2455 * user has explicitly opted out by calling minijail_run_as_init().
2456 */
2457 int do_init = j->flags.do_init && !j->flags.run_as_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002458 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002459
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002460 if (use_preload) {
Dylan Reid0412dcc2017-08-24 11:33:15 -07002461 if (j->hooks_head != NULL)
2462 die("Minijail hooks are not supported with LD_PRELOAD");
2463 if (!config->exec_in_child)
2464 die("minijail_fork is not supported with LD_PRELOAD");
2465
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002466 oldenv = getenv(kLdPreloadEnvVar);
2467 if (oldenv) {
2468 oldenv_copy = strdup(oldenv);
2469 if (!oldenv_copy)
2470 return -ENOMEM;
2471 }
2472
2473 if (setup_preload())
2474 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002475 }
Will Drewryf89aef52011-09-16 16:48:57 -05002476
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002477 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002478 if (j->flags.use_caps && j->caps != 0 &&
2479 !j->flags.set_ambient_caps) {
2480 die("non-empty, non-ambient capabilities are not "
2481 "supported without LD_PRELOAD");
2482 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002483 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002484
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002485 if (use_preload) {
2486 /*
2487 * Before we fork(2) and execve(2) the child process, we need
2488 * to open a pipe(2) to send the minijail configuration over.
2489 */
2490 if (setup_pipe(pipe_fds))
2491 return -EFAULT;
2492 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002493
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002494 /*
2495 * If we want to write to the child process' standard input,
2496 * create the pipe(2) now.
2497 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002498 if (status_out->pstdin_fd) {
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002499 if (pipe(stdin_fds))
2500 return -EFAULT;
2501 }
2502
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002503 /*
2504 * If we want to read from the child process' standard output,
2505 * create the pipe(2) now.
2506 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002507 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002508 if (pipe(stdout_fds))
2509 return -EFAULT;
2510 }
2511
2512 /*
2513 * If we want to read from the child process' standard error,
2514 * create the pipe(2) now.
2515 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002516 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002517 if (pipe(stderr_fds))
2518 return -EFAULT;
2519 }
2520
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002521 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002522 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002523 * or if we need to add the child process to cgroups, create the pipe(2)
2524 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002525 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002526 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002527 sync_child = 1;
2528 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002529 return -EFAULT;
2530 }
2531
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002532 /*
2533 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002534 *
2535 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2536 *
2537 * In multithreaded programs, there are a bunch of locks inside libc,
2538 * some of which may be held by other threads at the time that we call
2539 * minijail_run_pid(). If we call fork(), glibc does its level best to
2540 * ensure that we hold all of these locks before it calls clone()
2541 * internally and drop them after clone() returns, but when we call
2542 * sys_clone(2) directly, all that gets bypassed and we end up with a
2543 * child address space where some of libc's important locks are held by
2544 * other threads (which did not get cloned, and hence will never release
2545 * those locks). This is okay so long as we call exec() immediately
2546 * after, but a bunch of seemingly-innocent libc functions like setenv()
2547 * take locks.
2548 *
2549 * Hence, only call sys_clone() if we need to, in order to get at pid
2550 * namespacing. If we follow this path, the child's address space might
2551 * have broken locks; you may only call functions that do not acquire
2552 * any locks.
2553 *
2554 * Unfortunately, fork() acquires every lock it can get its hands on, as
2555 * previously detailed, so this function is highly likely to deadlock
2556 * later on (see "deadlock here") if we're multithreaded.
2557 *
2558 * We might hack around this by having the clone()d child (init of the
2559 * pid namespace) return directly, rather than leaving the clone()d
2560 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002561 * its fork()ed child return in turn), but that process would be
2562 * crippled with its libc locks potentially broken. We might try
2563 * fork()ing in the parent before we clone() to ensure that we own all
2564 * the locks, but then we have to have the forked child hanging around
2565 * consuming resources (and possibly having file descriptors / shared
2566 * memory regions / etc attached). We'd need to keep the child around to
2567 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002568 *
2569 * TODO(ellyjones): figure out if the "forked child hanging around"
2570 * problem is fixable or not. It would be nice if we worked in this
2571 * case.
2572 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002573 if (pid_namespace) {
2574 int clone_flags = CLONE_NEWPID | SIGCHLD;
2575 if (j->flags.userns)
2576 clone_flags |= CLONE_NEWUSER;
2577 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002578 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002579 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002580 }
Elly Jones761b7412012-06-13 15:49:52 -04002581
Elly Jonese1749eb2011-10-07 13:54:59 -04002582 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002583 if (use_preload) {
2584 free(oldenv_copy);
2585 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002586 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002587 }
Will Drewryf89aef52011-09-16 16:48:57 -05002588
Elly Jonese1749eb2011-10-07 13:54:59 -04002589 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002590 if (use_preload) {
2591 /* Restore parent's LD_PRELOAD. */
2592 if (oldenv_copy) {
2593 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2594 free(oldenv_copy);
2595 } else {
2596 unsetenv(kLdPreloadEnvVar);
2597 }
2598 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002599 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002600
Elly Jonese1749eb2011-10-07 13:54:59 -04002601 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002602
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002603 if (j->flags.forward_signals) {
2604 forward_pid = child_pid;
2605 install_signal_handlers();
2606 }
2607
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002608 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002609 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002610
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002611 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002612 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002613
Dylan Reid0f72ef42017-06-06 15:42:49 -07002614 if (j->rlimit_count)
2615 set_rlimits_or_die(j);
2616
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002617 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002618 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002619
2620 if (sync_child)
2621 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002622
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002623 if (use_preload) {
2624 /* Send marshalled minijail. */
2625 close(pipe_fds[0]); /* read endpoint */
2626 ret = minijail_to_fd(j, pipe_fds[1]);
2627 close(pipe_fds[1]); /* write endpoint */
2628 if (ret) {
2629 kill(j->initpid, SIGKILL);
2630 die("failed to send marshalled minijail");
2631 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002632 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002633
Dylan Reidacfb8be2017-08-25 12:56:51 -07002634 if (status_out->pchild_pid)
2635 *status_out->pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002636
2637 /*
2638 * If we want to write to the child process' standard input,
2639 * set up the write end of the pipe.
2640 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002641 if (status_out->pstdin_fd)
2642 *status_out->pstdin_fd =
2643 setup_pipe_end(stdin_fds, 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002644
2645 /*
2646 * If we want to read from the child process' standard output,
2647 * set up the read end of the pipe.
2648 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002649 if (status_out->pstdout_fd)
2650 *status_out->pstdout_fd =
2651 setup_pipe_end(stdout_fds, 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002652
2653 /*
2654 * If we want to read from the child process' standard error,
2655 * set up the read end of the pipe.
2656 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002657 if (status_out->pstderr_fd)
2658 *status_out->pstderr_fd =
2659 setup_pipe_end(stderr_fds, 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002660
Dylan Reid0412dcc2017-08-24 11:33:15 -07002661 /*
2662 * If forking return the child pid, in the normal exec case
2663 * return 0 for success.
2664 */
2665 if (!config->exec_in_child)
2666 return child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04002667 return 0;
2668 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002669 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002670 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002671
Peter Qiu2860c462015-12-16 15:13:06 -08002672 if (j->flags.reset_signal_mask) {
2673 sigset_t signal_mask;
2674 if (sigemptyset(&signal_mask) != 0)
2675 pdie("sigemptyset failed");
2676 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2677 pdie("sigprocmask failed");
2678 }
2679
Luis Hector Chaveza27118a2018-04-04 08:18:01 -07002680 if (j->flags.reset_signal_handlers) {
2681 int signum;
2682 for (signum = 0; signum <= SIGRTMAX; signum++) {
2683 /*
2684 * Ignore EINVAL since some signal numbers in the range
2685 * might not be valid.
2686 */
2687 if (signal(signum, SIG_DFL) == SIG_ERR &&
2688 errno != EINVAL) {
2689 pdie("failed to reset signal %d disposition",
2690 signum);
2691 }
2692 }
2693 }
2694
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002695 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002696 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002697 int inheritable_fds[kMaxInheritableFdsSize];
2698 size_t size = 0;
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002699 size_t i;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002700 if (use_preload) {
2701 inheritable_fds[size++] = pipe_fds[0];
2702 inheritable_fds[size++] = pipe_fds[1];
2703 }
2704 if (sync_child) {
2705 inheritable_fds[size++] = child_sync_pipe_fds[0];
2706 inheritable_fds[size++] = child_sync_pipe_fds[1];
2707 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002708 if (status_out->pstdin_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002709 inheritable_fds[size++] = stdin_fds[0];
2710 inheritable_fds[size++] = stdin_fds[1];
2711 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002712 if (status_out->pstdout_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002713 inheritable_fds[size++] = stdout_fds[0];
2714 inheritable_fds[size++] = stdout_fds[1];
2715 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002716 if (status_out->pstderr_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002717 inheritable_fds[size++] = stderr_fds[0];
2718 inheritable_fds[size++] = stderr_fds[1];
2719 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002720 for (i = 0; i < j->preserved_fd_count; i++) {
2721 /*
2722 * Preserve all parent_fds. They will be dup2(2)-ed in
2723 * the child later.
2724 */
2725 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
2726 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002727
2728 if (close_open_fds(inheritable_fds, size) < 0)
2729 die("failed to close open file descriptors");
2730 }
2731
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002732 if (redirect_fds(j))
2733 die("failed to set up fd redirections");
2734
Dylan Reidce5b55e2016-01-13 11:04:16 -08002735 if (sync_child)
2736 wait_for_parent_setup(child_sync_pipe_fds);
2737
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002738 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002739 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002740
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002741 /*
2742 * If we want to write to the jailed process' standard input,
2743 * set up the read end of the pipe.
2744 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002745 if (status_out->pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002746 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2747 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002748 die("failed to set up stdin pipe");
2749 }
2750
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002751 /*
2752 * If we want to read from the jailed process' standard output,
2753 * set up the write end of the pipe.
2754 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002755 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002756 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2757 STDOUT_FILENO) < 0)
2758 die("failed to set up stdout pipe");
2759 }
2760
2761 /*
2762 * If we want to read from the jailed process' standard error,
2763 * set up the write end of the pipe.
2764 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002765 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002766 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2767 STDERR_FILENO) < 0)
2768 die("failed to set up stderr pipe");
2769 }
2770
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002771 /*
2772 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2773 * This prevents the jailed process from using the TIOCSTI ioctl
2774 * to push characters into the parent process terminal's input buffer,
2775 * therefore escaping the jail.
Stephen Barber5dd5b1b2017-10-16 23:02:39 -07002776 *
2777 * Since it has just forked, the child will not be a process group
2778 * leader, and this call to setsid() should always succeed.
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002779 */
2780 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2781 isatty(STDERR_FILENO)) {
2782 if (setsid() < 0) {
2783 pdie("setsid() failed");
2784 }
2785 }
2786
Dylan Reid791f5772015-09-14 20:02:42 -07002787 /* If running an init program, let it decide when/how to mount /proc. */
2788 if (pid_namespace && !do_init)
2789 j->flags.remount_proc_ro = 0;
2790
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002791 if (use_preload) {
2792 /* Strip out flags that cannot be inherited across execve(2). */
2793 minijail_preexec(j);
2794 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002795 /*
2796 * If not using LD_PRELOAD, do all jailing before execve(2).
2797 * Note that PID namespaces can only be entered on fork(2),
2798 * so that flag is still cleared.
2799 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002800 j->flags.pids = 0;
2801 }
Dylan Reid0412dcc2017-08-24 11:33:15 -07002802
2803 /*
2804 * Jail this process.
2805 * If forking, return.
2806 * If not, execve(2) the target.
2807 */
Elly Jonese1749eb2011-10-07 13:54:59 -04002808 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002809
Dylan Reid0412dcc2017-08-24 11:33:15 -07002810 if (config->exec_in_child && pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002811 /*
2812 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002813 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002814 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002815 * a child to actually run the program. If |do_init == 0|, we
2816 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002817 *
2818 * If we're multithreaded, we'll probably deadlock here. See
2819 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002820 */
2821 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002822 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002823 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002824 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002825 /*
2826 * Best effort. Don't bother checking the return value.
2827 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002828 prctl(PR_SET_NAME, "minijail-init");
2829 init(child_pid); /* Never returns. */
2830 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002831 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002832
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002833 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
2834
Dylan Reid0412dcc2017-08-24 11:33:15 -07002835 if (!config->exec_in_child)
2836 return 0;
2837
Elly Jonesdd3e8512012-01-23 15:13:38 -05002838 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002839 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002840 * calling process
2841 * -> execve()-ing process
2842 * If we are:
2843 * calling process
2844 * -> init()-ing process
2845 * -> execve()-ing process
2846 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002847 ret = execve(config->filename, config->argv, environ);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002848 if (ret == -1) {
Dylan Reidacfb8be2017-08-25 12:56:51 -07002849 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002850 }
2851 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002852}
2853
Will Drewry6ac91122011-10-21 16:38:58 -05002854int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002855{
2856 int st;
2857 if (kill(j->initpid, SIGTERM))
2858 return -errno;
2859 if (waitpid(j->initpid, &st, 0) < 0)
2860 return -errno;
2861 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002862}
2863
Will Drewry6ac91122011-10-21 16:38:58 -05002864int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002865{
2866 int st;
2867 if (waitpid(j->initpid, &st, 0) < 0)
2868 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002869
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002870 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002871 int error_status = st;
2872 if (WIFSIGNALED(st)) {
2873 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002874 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002875 j->initpid, signum);
2876 /*
2877 * We return MINIJAIL_ERR_JAIL if the process received
2878 * SIGSYS, which happens when a syscall is blocked by
2879 * seccomp filters.
2880 * If not, we do what bash(1) does:
2881 * $? = 128 + signum
2882 */
2883 if (signum == SIGSYS) {
2884 error_status = MINIJAIL_ERR_JAIL;
2885 } else {
2886 error_status = 128 + signum;
2887 }
2888 }
2889 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002890 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002891
2892 int exit_status = WEXITSTATUS(st);
2893 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002894 info("child process %d exited with status %d",
2895 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002896
2897 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002898}
2899
Will Drewry6ac91122011-10-21 16:38:58 -05002900void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002901{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002902 size_t i;
2903
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002904 if (j->flags.seccomp_filter && j->filter_prog) {
2905 free(j->filter_prog->filter);
2906 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002907 }
Mike Frysingerac08a682017-10-10 02:04:50 -04002908 free_mounts_list(j);
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002909 while (j->hooks_head) {
2910 struct hook *c = j->hooks_head;
2911 j->hooks_head = c->next;
2912 free(c);
2913 }
2914 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002915 if (j->user)
2916 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002917 if (j->suppl_gid_list)
2918 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002919 if (j->chrootdir)
2920 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002921 if (j->pid_file_path)
2922 free(j->pid_file_path);
2923 if (j->uidmap)
2924 free(j->uidmap);
2925 if (j->gidmap)
2926 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002927 if (j->hostname)
2928 free(j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08002929 if (j->alt_syscall_table)
2930 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002931 for (i = 0; i < j->cgroup_count; ++i)
2932 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002933 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002934}
Luis Hector Chavez114a9302017-09-05 20:36:58 -07002935
2936void API minijail_log_to_fd(int fd, int min_priority)
2937{
2938 init_logging(LOG_TO_FD, fd, min_priority);
2939}