blob: 43de3c461241dbac0f233aa8f859332802a82eea [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>
Luis Hector Chavezc3e17722018-10-16 20:43:12 -070016#include <linux/filter.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040017#include <sched.h>
18#include <signal.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070019#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080020#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040024#include <sys/capability.h>
25#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050026#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040027#include <sys/prctl.h>
Dylan Reid0f72ef42017-06-06 15:42:49 -070028#include <sys/resource.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070029#include <sys/stat.h>
Mike Frysinger33ffef32017-01-13 19:53:19 -050030#include <sys/sysmacros.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070031#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080032#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040033#include <sys/wait.h>
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -070034#include <syscall.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040035#include <unistd.h>
36
37#include "libminijail.h"
38#include "libminijail-private.h"
39
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070040#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080041#include "syscall_filter.h"
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040042#include "syscall_wrapper.h"
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040043#include "system.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070044#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080045
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070046/* Until these are reliably available in linux/prctl.h. */
Andrew Brestickereac28942015-11-11 16:04:46 -080047#ifndef PR_ALT_SYSCALL
48# define PR_ALT_SYSCALL 0x43724f53
49#endif
50
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040051/* Seccomp filter related flags. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080052#ifndef PR_SET_NO_NEW_PRIVS
53# define PR_SET_NO_NEW_PRIVS 38
54#endif
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040055
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080056#ifndef SECCOMP_MODE_FILTER
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040057#define SECCOMP_MODE_FILTER 2 /* Uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050058#endif
59
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040060#ifndef SECCOMP_SET_MODE_STRICT
61# define SECCOMP_SET_MODE_STRICT 0
62#endif
63#ifndef SECCOMP_SET_MODE_FILTER
64# define SECCOMP_SET_MODE_FILTER 1
65#endif
66
67#ifndef SECCOMP_FILTER_FLAG_TSYNC
68# define SECCOMP_FILTER_FLAG_TSYNC 1
69#endif
70/* End seccomp filter related flags. */
71
Dylan Reid4cbc2a52016-06-17 19:06:07 -070072/* New cgroup namespace might not be in linux-headers yet. */
73#ifndef CLONE_NEWCGROUP
74# define CLONE_NEWCGROUP 0x02000000
75#endif
76
Dylan Reid605ce7f2016-01-19 19:21:00 -080077#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
78
Dylan Reid0f72ef42017-06-06 15:42:49 -070079#define MAX_RLIMITS 32 /* Currently there are 15 supported by Linux. */
80
Stephen Barber0d1cbf62017-10-16 22:19:38 -070081#define MAX_PRESERVED_FDS 32U
Luis Hector Chavez1617f632017-08-01 18:32:30 -070082
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -080083/* Keyctl commands. */
84#define KEYCTL_JOIN_SESSION_KEYRING 1
85
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -070086/*
87 * The userspace equivalent of MNT_USER_SETTABLE_MASK, which is the mask of all
88 * flags that can be modified by MS_REMOUNT.
89 */
90#define MS_USER_SETTABLE_MASK \
91 (MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME | MS_NODIRATIME | \
92 MS_RELATIME | MS_RDONLY)
93
Dylan Reid0f72ef42017-06-06 15:42:49 -070094struct minijail_rlimit {
95 int type;
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -080096 rlim_t cur;
97 rlim_t max;
Dylan Reid0f72ef42017-06-06 15:42:49 -070098};
99
Dylan Reid648b2202015-10-23 00:50:00 -0700100struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400101 char *src;
102 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700103 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700104 char *data;
105 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -0700106 unsigned long flags;
107 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400108};
109
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700110struct hook {
111 minijail_hook_t hook;
112 void *payload;
113 minijail_hook_event_t event;
114 struct hook *next;
115};
116
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700117struct preserved_fd {
118 int parent_fd;
119 int child_fd;
120};
121
Will Drewryf89aef52011-09-16 16:48:57 -0500122struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700123 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700124 * WARNING: if you add a flag here you need to make sure it's
125 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700126 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400127 struct {
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700128 int uid : 1;
129 int gid : 1;
Lutz Justen13807cb2017-01-03 17:11:55 +0100130 int inherit_suppl_gids : 1;
131 int set_suppl_gids : 1;
132 int keep_suppl_gids : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700133 int use_caps : 1;
134 int capbset_drop : 1;
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400135 int set_ambient_caps : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700136 int vfs : 1;
137 int enter_vfs : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700138 int pids : 1;
139 int ipc : 1;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400140 int uts : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700141 int net : 1;
142 int enter_net : 1;
143 int ns_cgroups : 1;
144 int userns : 1;
145 int disable_setgroups : 1;
146 int seccomp : 1;
147 int remount_proc_ro : 1;
148 int no_new_privs : 1;
149 int seccomp_filter : 1;
150 int seccomp_filter_tsync : 1;
151 int seccomp_filter_logging : 1;
152 int chroot : 1;
153 int pivot_root : 1;
Mike Frysinger33ffef32017-01-13 19:53:19 -0500154 int mount_dev : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700155 int mount_tmp : 1;
156 int do_init : 1;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700157 int run_as_init : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700158 int pid_file : 1;
159 int cgroups : 1;
160 int alt_syscall : 1;
161 int reset_signal_mask : 1;
Luis Hector Chaveza27118a2018-04-04 08:18:01 -0700162 int reset_signal_handlers : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700163 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800164 int new_session_keyring : 1;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400165 int forward_signals : 1;
Xiyuan Xia9b41e652019-05-23 11:03:04 -0700166 int setsid : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400167 } flags;
168 uid_t uid;
169 gid_t gid;
170 gid_t usergid;
171 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800172 size_t suppl_gid_count;
173 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400174 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800175 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400176 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700177 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700178 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400179 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800180 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800181 char *uidmap;
182 char *gidmap;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400183 char *hostname;
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700184 char *preload_path;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800185 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800186 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800187 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700188 struct mountpoint *mounts_head;
189 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800190 size_t mounts_count;
Mike Frysinger785b1c32018-02-23 15:47:24 -0500191 unsigned long remount_mode;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100192 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800193 char *cgroups[MAX_CGROUPS];
194 size_t cgroup_count;
Dylan Reid0f72ef42017-06-06 15:42:49 -0700195 struct minijail_rlimit rlimits[MAX_RLIMITS];
196 size_t rlimit_count;
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700197 uint64_t securebits_skip_mask;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700198 struct hook *hooks_head;
199 struct hook *hooks_tail;
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700200 struct preserved_fd preserved_fds[MAX_PRESERVED_FDS];
201 size_t preserved_fd_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500202};
203
Luis Hector Chavez64730af2017-09-13 13:18:59 -0700204static void run_hooks_or_die(const struct minijail *j,
205 minijail_hook_event_t event);
206
Mike Frysingerac08a682017-10-10 02:04:50 -0400207static void free_mounts_list(struct minijail *j)
208{
209 while (j->mounts_head) {
210 struct mountpoint *m = j->mounts_head;
211 j->mounts_head = j->mounts_head->next;
212 free(m->data);
213 free(m->type);
214 free(m->dest);
215 free(m->src);
216 free(m);
217 }
218 // No need to clear mounts_head as we know it's NULL after the loop.
219 j->mounts_tail = NULL;
220}
221
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700222/*
223 * Strip out flags meant for the parent.
224 * We keep things that are not inherited across execve(2) (e.g. capabilities),
225 * or are easier to set after execve(2) (e.g. seccomp filters).
226 */
227void minijail_preenter(struct minijail *j)
228{
229 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700230 j->flags.enter_vfs = 0;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700231 j->flags.ns_cgroups = 0;
232 j->flags.net = 0;
233 j->flags.uts = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700234 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700235 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800236 j->flags.do_init = 0;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700237 j->flags.run_as_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800238 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800239 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400240 j->flags.forward_signals = 0;
Xiyuan Xia9b41e652019-05-23 11:03:04 -0700241 j->flags.setsid = 0;
Mike Frysinger785b1c32018-02-23 15:47:24 -0500242 j->remount_mode = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700243}
244
245/*
246 * Strip out flags meant for the child.
247 * We keep things that are inherited across execve(2).
248 */
249void minijail_preexec(struct minijail *j)
250{
251 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700252 int enter_vfs = j->flags.enter_vfs;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700253 int ns_cgroups = j->flags.ns_cgroups;
254 int net = j->flags.net;
255 int uts = j->flags.uts;
Dylan Reid791f5772015-09-14 20:02:42 -0700256 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800257 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700258 if (j->user)
259 free(j->user);
260 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800261 if (j->suppl_gid_list)
262 free(j->suppl_gid_list);
263 j->suppl_gid_list = NULL;
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700264 if (j->preload_path)
265 free(j->preload_path);
266 j->preload_path = NULL;
Mike Frysingerac08a682017-10-10 02:04:50 -0400267 free_mounts_list(j);
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700268 memset(&j->flags, 0, sizeof(j->flags));
269 /* Now restore anything we meant to keep. */
270 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700271 j->flags.enter_vfs = enter_vfs;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700272 j->flags.ns_cgroups = ns_cgroups;
273 j->flags.net = net;
274 j->flags.uts = uts;
Dylan Reid791f5772015-09-14 20:02:42 -0700275 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800276 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700277 /* Note, |pids| will already have been used before this call. */
278}
279
280/* Minijail API. */
281
Will Drewry6ac91122011-10-21 16:38:58 -0500282struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400283{
Mike Frysinger785b1c32018-02-23 15:47:24 -0500284 struct minijail *j = calloc(1, sizeof(struct minijail));
285 j->remount_mode = MS_PRIVATE;
286 return j;
Elly Jonescd7a9042011-07-22 13:56:51 -0400287}
288
Will Drewry6ac91122011-10-21 16:38:58 -0500289void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400290{
291 if (uid == 0)
292 die("useless change to uid 0");
293 j->uid = uid;
294 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400295}
296
Will Drewry6ac91122011-10-21 16:38:58 -0500297void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400298{
299 if (gid == 0)
300 die("useless change to gid 0");
301 j->gid = gid;
302 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400303}
304
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800305void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
306 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800307{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800308 size_t i;
309
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500310 if (j->flags.inherit_suppl_gids)
311 die("cannot inherit *and* set supplementary groups");
312 if (j->flags.keep_suppl_gids)
313 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800314
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800315 if (size == 0) {
316 /* Clear supplementary groups. */
317 j->suppl_gid_list = NULL;
318 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100319 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800320 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800321 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800322
323 /* Copy the gid_t array. */
324 j->suppl_gid_list = calloc(size, sizeof(gid_t));
325 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800326 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800327 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800328 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800329 j->suppl_gid_list[i] = list[i];
330 }
331 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100332 j->flags.set_suppl_gids = 1;
333}
334
335void API minijail_keep_supplementary_gids(struct minijail *j) {
336 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800337}
338
Will Drewry6ac91122011-10-21 16:38:58 -0500339int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400340{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700341 uid_t uid;
342 gid_t gid;
343 int rc = lookup_user(user, &uid, &gid);
344 if (rc)
345 return rc;
346 minijail_change_uid(j, uid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400347 j->user = strdup(user);
348 if (!j->user)
349 return -ENOMEM;
Luis Hector Chavez71323552017-09-05 09:17:22 -0700350 j->usergid = gid;
Elly Jonese1749eb2011-10-07 13:54:59 -0400351 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400352}
353
Will Drewry6ac91122011-10-21 16:38:58 -0500354int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400355{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700356 gid_t gid;
357 int rc = lookup_group(group, &gid);
358 if (rc)
359 return rc;
360 minijail_change_gid(j, gid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400361 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400362}
363
Will Drewry6ac91122011-10-21 16:38:58 -0500364void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400365{
366 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400367}
368
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700369void API minijail_no_new_privs(struct minijail *j)
370{
371 j->flags.no_new_privs = 1;
372}
373
Will Drewry6ac91122011-10-21 16:38:58 -0500374void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400375{
376 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500377}
378
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400379void API minijail_set_seccomp_filter_tsync(struct minijail *j)
380{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400381 if (j->filter_len > 0 && j->filter_prog != NULL) {
382 die("minijail_set_seccomp_filter_tsync() must be called "
383 "before minijail_parse_seccomp_filters()");
384 }
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400385
386 if (j->flags.seccomp_filter_logging && !seccomp_ret_log_available()) {
387 /*
388 * If SECCOMP_RET_LOG is not available, we don't want to use
389 * SECCOMP_RET_TRAP to both kill the entire process and report
390 * failing syscalls, since it will be brittle. Just bail.
391 */
Mike Frysinger52f6ada2019-06-26 16:59:36 -0400392 die("SECCOMP_RET_LOG not available, cannot use logging with "
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400393 "thread sync at the same time");
394 }
395
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400396 j->flags.seccomp_filter_tsync = 1;
397}
398
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700399void API minijail_log_seccomp_filter_failures(struct minijail *j)
400{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400401 if (j->filter_len > 0 && j->filter_prog != NULL) {
402 die("minijail_log_seccomp_filter_failures() must be called "
403 "before minijail_parse_seccomp_filters()");
404 }
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400405
406 if (j->flags.seccomp_filter_tsync && !seccomp_ret_log_available()) {
407 /*
408 * If SECCOMP_RET_LOG is not available, we don't want to use
409 * SECCOMP_RET_TRAP to both kill the entire process and report
410 * failing syscalls, since it will be brittle. Just bail.
411 */
Mike Frysinger52f6ada2019-06-26 16:59:36 -0400412 die("SECCOMP_RET_LOG not available, cannot use thread sync with "
413 "logging at the same time");
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400414 }
415
416 if (debug_logging_allowed()) {
417 j->flags.seccomp_filter_logging = 1;
418 } else {
419 warn("non-debug build: ignoring request to enable seccomp "
420 "logging");
421 }
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700422}
423
Will Drewry6ac91122011-10-21 16:38:58 -0500424void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400425{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800426 /*
427 * 'minijail_use_caps' configures a runtime-capabilities-only
428 * environment, including a bounding set matching the thread's runtime
429 * (permitted|inheritable|effective) sets.
430 * Therefore, it will override any existing bounding set configurations
431 * since the latter would allow gaining extra runtime capabilities from
432 * file capabilities.
433 */
434 if (j->flags.capbset_drop) {
435 warn("overriding bounding set configuration");
436 j->cap_bset = 0;
437 j->flags.capbset_drop = 0;
438 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400439 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800440 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400441}
442
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800443void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
444{
445 if (j->flags.use_caps) {
446 /*
447 * 'minijail_use_caps' will have already configured a capability
448 * bounding set matching the (permitted|inheritable|effective)
449 * sets. Abort if the user tries to configure a separate
450 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
451 * are mutually exclusive.
452 */
453 die("runtime capabilities already configured, can't drop "
454 "bounding set separately");
455 }
456 j->cap_bset = capmask;
457 j->flags.capbset_drop = 1;
458}
459
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400460void API minijail_set_ambient_caps(struct minijail *j)
461{
462 j->flags.set_ambient_caps = 1;
463}
464
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800465void API minijail_reset_signal_mask(struct minijail *j)
466{
Peter Qiu2860c462015-12-16 15:13:06 -0800467 j->flags.reset_signal_mask = 1;
468}
469
Luis Hector Chaveza27118a2018-04-04 08:18:01 -0700470void API minijail_reset_signal_handlers(struct minijail *j)
471{
472 j->flags.reset_signal_handlers = 1;
473}
474
Will Drewry6ac91122011-10-21 16:38:58 -0500475void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400476{
477 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400478}
479
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700480void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
481{
Mike Frysinger902a4492018-12-27 05:22:56 -0500482 /* Note: Do not use O_CLOEXEC here. We'll close it after we use it. */
483 int ns_fd = open(ns_path, O_RDONLY);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700484 if (ns_fd < 0) {
485 pdie("failed to open namespace '%s'", ns_path);
486 }
487 j->mountns_fd = ns_fd;
488 j->flags.enter_vfs = 1;
489}
490
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800491void API minijail_new_session_keyring(struct minijail *j)
492{
493 j->flags.new_session_keyring = 1;
494}
495
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700496void API minijail_skip_setting_securebits(struct minijail *j,
497 uint64_t securebits_skip_mask)
498{
499 j->securebits_skip_mask = securebits_skip_mask;
500}
501
Mike Frysinger785b1c32018-02-23 15:47:24 -0500502void API minijail_remount_mode(struct minijail *j, unsigned long mode)
503{
504 j->remount_mode = mode;
505}
506
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800507void API minijail_skip_remount_private(struct minijail *j)
508{
Mike Frysinger785b1c32018-02-23 15:47:24 -0500509 j->remount_mode = 0;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800510}
511
Will Drewry6ac91122011-10-21 16:38:58 -0500512void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400513{
Elly Jonese58176c2012-01-23 11:46:17 -0500514 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700515 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400516 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800517 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400518}
519
Jorge Lucangeli Obes2fa96d12019-02-05 10:51:57 -0500520void API minijail_namespace_pids_rw_proc(struct minijail *j)
521{
522 j->flags.vfs = 1;
523 j->flags.pids = 1;
524 j->flags.do_init = 1;
525}
526
Dylan Reidf7942472015-11-18 17:55:26 -0800527void API minijail_namespace_ipc(struct minijail *j)
528{
529 j->flags.ipc = 1;
530}
531
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400532void API minijail_namespace_uts(struct minijail *j)
533{
534 j->flags.uts = 1;
535}
536
537int API minijail_namespace_set_hostname(struct minijail *j, const char *name)
538{
539 if (j->hostname)
540 return -EINVAL;
541 minijail_namespace_uts(j);
542 j->hostname = strdup(name);
543 if (!j->hostname)
544 return -ENOMEM;
545 return 0;
546}
547
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400548void API minijail_namespace_net(struct minijail *j)
549{
550 j->flags.net = 1;
551}
552
Dylan Reid1102f5a2015-09-15 11:52:20 -0700553void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
554{
Mike Frysinger902a4492018-12-27 05:22:56 -0500555 /* Note: Do not use O_CLOEXEC here. We'll close it after we use it. */
556 int ns_fd = open(ns_path, O_RDONLY);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700557 if (ns_fd < 0) {
558 pdie("failed to open namespace '%s'", ns_path);
559 }
560 j->netns_fd = ns_fd;
561 j->flags.enter_net = 1;
562}
563
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700564void API minijail_namespace_cgroups(struct minijail *j)
565{
566 j->flags.ns_cgroups = 1;
567}
568
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700569void API minijail_close_open_fds(struct minijail *j)
570{
571 j->flags.close_open_fds = 1;
572}
573
Dylan Reid791f5772015-09-14 20:02:42 -0700574void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400575{
576 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700577 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400578}
579
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800580void API minijail_namespace_user(struct minijail *j)
581{
582 j->flags.userns = 1;
583}
584
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400585void API minijail_namespace_user_disable_setgroups(struct minijail *j)
586{
587 j->flags.disable_setgroups = 1;
588}
589
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800590int API minijail_uidmap(struct minijail *j, const char *uidmap)
591{
592 j->uidmap = strdup(uidmap);
593 if (!j->uidmap)
594 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800595 char *ch;
596 for (ch = j->uidmap; *ch; ch++) {
597 if (*ch == ',')
598 *ch = '\n';
599 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800600 return 0;
601}
602
603int API minijail_gidmap(struct minijail *j, const char *gidmap)
604{
605 j->gidmap = strdup(gidmap);
606 if (!j->gidmap)
607 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800608 char *ch;
609 for (ch = j->gidmap; *ch; ch++) {
610 if (*ch == ',')
611 *ch = '\n';
612 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800613 return 0;
614}
615
Will Drewry6ac91122011-10-21 16:38:58 -0500616void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400617{
Lutz Justen13807cb2017-01-03 17:11:55 +0100618 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400619}
620
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800621void API minijail_run_as_init(struct minijail *j)
622{
623 /*
624 * Since the jailed program will become 'init' in the new PID namespace,
625 * Minijail does not need to fork an 'init' process.
626 */
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700627 j->flags.run_as_init = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800628}
629
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700630int API minijail_enter_chroot(struct minijail *j, const char *dir)
631{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400632 if (j->chrootdir)
633 return -EINVAL;
634 j->chrootdir = strdup(dir);
635 if (!j->chrootdir)
636 return -ENOMEM;
637 j->flags.chroot = 1;
638 return 0;
639}
640
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800641int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
642{
643 if (j->chrootdir)
644 return -EINVAL;
645 j->chrootdir = strdup(dir);
646 if (!j->chrootdir)
647 return -ENOMEM;
648 j->flags.pivot_root = 1;
649 return 0;
650}
651
Dylan Reida14e08d2015-10-22 21:05:29 -0700652char API *minijail_get_original_path(struct minijail *j,
653 const char *path_inside_chroot)
654{
Dylan Reid648b2202015-10-23 00:50:00 -0700655 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700656
Dylan Reid648b2202015-10-23 00:50:00 -0700657 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700658 while (b) {
659 /*
660 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700661 * mount, then the original path is exactly the source of
662 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700663 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700664 * mount source = /some/path/exe, mount dest =
665 * /chroot/path/exe Then when getting the original path of
666 * "/chroot/path/exe", the source of that mount,
667 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700668 */
669 if (!strcmp(b->dest, path_inside_chroot))
670 return strdup(b->src);
671
672 /*
673 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700674 * mount, take the suffix of the chroot path relative to the
675 * mount destination path, and append it to the mount source
676 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700677 */
678 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
679 const char *relative_path =
680 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400681 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700682 }
683 b = b->next;
684 }
685
686 /* If there is a chroot path, append |path_inside_chroot| to that. */
687 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400688 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700689
690 /* No chroot, so the path outside is the same as it is inside. */
691 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700692}
693
Martin Pelikánab9eb442017-01-25 11:53:58 +1100694size_t minijail_get_tmpfs_size(const struct minijail *j)
695{
696 return j->tmpfs_size;
697}
698
Mike Frysinger33ffef32017-01-13 19:53:19 -0500699void API minijail_mount_dev(struct minijail *j)
700{
701 j->flags.mount_dev = 1;
702}
703
Lee Campbell11af0622014-05-22 12:36:04 -0700704void API minijail_mount_tmp(struct minijail *j)
705{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100706 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
707}
708
709void API minijail_mount_tmp_size(struct minijail *j, size_t size)
710{
711 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700712 j->flags.mount_tmp = 1;
713}
714
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800715int API minijail_write_pid_file(struct minijail *j, const char *path)
716{
717 j->pid_file_path = strdup(path);
718 if (!j->pid_file_path)
719 return -ENOMEM;
720 j->flags.pid_file = 1;
721 return 0;
722}
723
Dylan Reid605ce7f2016-01-19 19:21:00 -0800724int API minijail_add_to_cgroup(struct minijail *j, const char *path)
725{
726 if (j->cgroup_count >= MAX_CGROUPS)
727 return -ENOMEM;
728 j->cgroups[j->cgroup_count] = strdup(path);
729 if (!j->cgroups[j->cgroup_count])
730 return -ENOMEM;
731 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800732 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800733 return 0;
734}
735
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800736int API minijail_rlimit(struct minijail *j, int type, rlim_t cur, rlim_t max)
Dylan Reid0f72ef42017-06-06 15:42:49 -0700737{
738 size_t i;
739
740 if (j->rlimit_count >= MAX_RLIMITS)
741 return -ENOMEM;
742 /* It's an error if the caller sets the same rlimit multiple times. */
743 for (i = 0; i < j->rlimit_count; i++) {
744 if (j->rlimits[i].type == type)
745 return -EEXIST;
746 }
747
748 j->rlimits[j->rlimit_count].type = type;
749 j->rlimits[j->rlimit_count].cur = cur;
750 j->rlimits[j->rlimit_count].max = max;
751 j->rlimit_count++;
752 return 0;
753}
754
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400755int API minijail_forward_signals(struct minijail *j)
756{
757 j->flags.forward_signals = 1;
758 return 0;
759}
760
Xiyuan Xia9b41e652019-05-23 11:03:04 -0700761int API minijail_create_session(struct minijail *j) {
762 j->flags.setsid = 1;
763 return 0;
764}
765
Dylan Reid81e23972016-05-18 14:06:35 -0700766int API minijail_mount_with_data(struct minijail *j, const char *src,
767 const char *dest, const char *type,
768 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700769{
Dylan Reid648b2202015-10-23 00:50:00 -0700770 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400771
772 if (*dest != '/')
773 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700774 m = calloc(1, sizeof(*m));
775 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400776 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700777 m->dest = strdup(dest);
778 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400779 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700780 m->src = strdup(src);
781 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400782 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700783 m->type = strdup(type);
784 if (!m->type)
785 goto error;
Mike Frysingerb7803c82018-08-23 15:43:15 -0400786
787 if (!data || !data[0]) {
788 /*
789 * Set up secure defaults for certain filesystems. Adding this
790 * fs-specific logic here kind of sucks, but considering how
791 * people use these in practice, it's probably OK. If they want
792 * the kernel defaults, they can pass data="" instead of NULL.
793 */
794 if (!strcmp(type, "tmpfs")) {
795 /* tmpfs defaults to mode=1777 and size=50%. */
796 data = "mode=0755,size=10M";
797 }
798 }
Dylan Reid81e23972016-05-18 14:06:35 -0700799 if (data) {
800 m->data = strdup(data);
801 if (!m->data)
802 goto error;
803 m->has_data = 1;
804 }
Mike Frysingercb8674d2018-08-12 00:53:35 -0400805
806 /* If they don't specify any flags, default to secure ones. */
807 if (flags == 0)
808 flags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Dylan Reid648b2202015-10-23 00:50:00 -0700809 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400810
Elly Jonesdd3e8512012-01-23 15:13:38 -0500811 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700812 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400813 * containing vfs namespace.
814 */
815 minijail_namespace_vfs(j);
816
Dylan Reid648b2202015-10-23 00:50:00 -0700817 if (j->mounts_tail)
818 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400819 else
Dylan Reid648b2202015-10-23 00:50:00 -0700820 j->mounts_head = m;
821 j->mounts_tail = m;
822 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400823
824 return 0;
825
826error:
Dylan Reid81e23972016-05-18 14:06:35 -0700827 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700828 free(m->src);
829 free(m->dest);
830 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400831 return -ENOMEM;
832}
833
Dylan Reid81e23972016-05-18 14:06:35 -0700834int API minijail_mount(struct minijail *j, const char *src, const char *dest,
835 const char *type, unsigned long flags)
836{
837 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
838}
839
Dylan Reid648b2202015-10-23 00:50:00 -0700840int API minijail_bind(struct minijail *j, const char *src, const char *dest,
841 int writeable)
842{
843 unsigned long flags = MS_BIND;
844
845 if (!writeable)
846 flags |= MS_RDONLY;
847
848 return minijail_mount(j, src, dest, "", flags);
849}
850
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700851int API minijail_add_hook(struct minijail *j, minijail_hook_t hook,
852 void *payload, minijail_hook_event_t event)
853{
854 struct hook *c;
855
856 if (hook == NULL)
857 return -EINVAL;
858 if (event >= MINIJAIL_HOOK_EVENT_MAX)
859 return -EINVAL;
860 c = calloc(1, sizeof(*c));
861 if (!c)
862 return -ENOMEM;
863
864 c->hook = hook;
865 c->payload = payload;
866 c->event = event;
867
868 if (j->hooks_tail)
869 j->hooks_tail->next = c;
870 else
871 j->hooks_head = c;
872 j->hooks_tail = c;
873
874 return 0;
875}
876
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700877int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd)
878{
879 if (parent_fd < 0 || child_fd < 0)
880 return -EINVAL;
881 if (j->preserved_fd_count >= MAX_PRESERVED_FDS)
882 return -ENOMEM;
883 j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd;
884 j->preserved_fds[j->preserved_fd_count].child_fd = child_fd;
885 j->preserved_fd_count++;
886 return 0;
887}
888
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700889int API minijail_set_preload_path(struct minijail *j, const char *preload_path)
890{
891 if (j->preload_path)
892 return -EINVAL;
893 j->preload_path = strdup(preload_path);
894 if (!j->preload_path)
895 return -ENOMEM;
896 return 0;
897}
898
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400899static void clear_seccomp_options(struct minijail *j)
900{
901 j->flags.seccomp_filter = 0;
902 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400903 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400904 j->filter_len = 0;
905 j->filter_prog = NULL;
906 j->flags.no_new_privs = 0;
907}
908
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700909static int seccomp_should_use_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400910{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400911 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400912 /*
913 * |errno| will be set to EINVAL when seccomp has not been
914 * compiled into the kernel. On certain platforms and kernel
915 * versions this is not a fatal failure. In that case, and only
916 * in that case, disable seccomp and skip loading the filters.
917 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400918 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400919 warn("not loading seccomp filters, seccomp filter not "
920 "supported");
921 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400922 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700923 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400924 /*
925 * If |errno| != EINVAL or seccomp_can_softfail() is false,
926 * we can proceed. Worst case scenario minijail_enter() will
927 * abort() if seccomp fails.
928 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700929 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400930 if (j->flags.seccomp_filter_tsync) {
931 /* Are the seccomp(2) syscall and the TSYNC option supported? */
932 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
933 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
934 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400935 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
936 warn("seccomp(2) syscall not supported");
937 clear_seccomp_options(j);
938 return 0;
939 } else if (saved_errno == EINVAL &&
940 seccomp_can_softfail()) {
941 warn(
942 "seccomp filter thread sync not supported");
943 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400944 return 0;
945 }
946 /*
947 * Similar logic here. If seccomp_can_softfail() is
948 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
949 * we can proceed. Worst case scenario minijail_enter()
950 * will abort() if seccomp or TSYNC fail.
951 */
952 }
953 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400954 return 1;
955}
956
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700957static int set_seccomp_filters_internal(struct minijail *j,
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400958 const struct sock_fprog *filter,
959 bool owned)
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700960{
961 struct sock_fprog *fprog;
962
963 if (owned) {
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400964 /*
965 * If |owned| is true, it's OK to cast away the const-ness since
966 * we'll own the pointer going forward.
967 */
968 fprog = (struct sock_fprog *)filter;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700969 } else {
970 fprog = malloc(sizeof(struct sock_fprog));
971 if (!fprog)
972 return -ENOMEM;
973 fprog->len = filter->len;
974 fprog->filter = malloc(sizeof(struct sock_filter) * fprog->len);
975 if (!fprog->filter) {
976 free(fprog);
977 return -ENOMEM;
978 }
979 memcpy(fprog->filter, filter->filter,
980 sizeof(struct sock_filter) * fprog->len);
981 }
982
983 if (j->filter_prog) {
984 free(j->filter_prog->filter);
985 free(j->filter_prog);
986 }
987
988 j->filter_len = fprog->len;
989 j->filter_prog = fprog;
990 return 0;
991}
992
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700993static int parse_seccomp_filters(struct minijail *j, const char *filename,
994 FILE *policy_file)
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400995{
996 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700997 if (!fprog)
998 return -ENOMEM;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400999
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001000 struct filter_options filteropts;
1001
1002 /*
1003 * Figure out filter options.
1004 * Allow logging?
1005 */
1006 filteropts.allow_logging =
1007 debug_logging_allowed() && j->flags.seccomp_filter_logging;
1008
1009 /* What to do on a blocked system call? */
1010 if (filteropts.allow_logging) {
1011 if (seccomp_ret_log_available())
1012 filteropts.action = ACTION_RET_LOG;
1013 else
1014 filteropts.action = ACTION_RET_TRAP;
1015 } else {
1016 if (j->flags.seccomp_filter_tsync)
1017 filteropts.action = ACTION_RET_TRAP;
1018 else
1019 filteropts.action = ACTION_RET_KILL;
1020 }
1021
1022 /*
1023 * If SECCOMP_RET_LOG is not available, need to allow extra syscalls
1024 * for logging.
1025 */
1026 filteropts.allow_syscalls_for_logging =
1027 filteropts.allow_logging && !seccomp_ret_log_available();
1028
Jorge Lucangeli Obese1a86892019-06-10 16:17:03 -04001029 if (compile_filter(filename, policy_file, fprog, &filteropts)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001030 free(fprog);
1031 return -1;
1032 }
1033
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001034 return set_seccomp_filters_internal(j, fprog, true /* owned */);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001035}
1036
1037void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
1038{
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001039 if (!seccomp_should_use_filters(j))
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001040 return;
1041
Luis Hector Chaveza30a2062018-07-12 21:10:33 -07001042 FILE *file = fopen(path, "re");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001043 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -07001044 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -04001045 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001046
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001047 if (parse_seccomp_filters(j, path, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -07001048 die("failed to compile seccomp filter BPF program in '%s'",
1049 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001050 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001051 fclose(file);
1052}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001053
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001054void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
1055{
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001056 char *fd_path, *path;
1057 FILE *file;
1058
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001059 if (!seccomp_should_use_filters(j))
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001060 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001061
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001062 file = fdopen(fd, "r");
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001063 if (!file) {
1064 pdie("failed to associate stream with fd %d", fd);
1065 }
1066
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001067 if (asprintf(&fd_path, "/proc/self/fd/%d", fd) == -1)
1068 pdie("failed to create path for fd %d", fd);
1069 path = realpath(fd_path, NULL);
1070 if (path == NULL)
1071 pwarn("failed to get path of fd %d", fd);
1072 free(fd_path);
1073
1074 if (parse_seccomp_filters(j, path ? path : "<fd>", file) != 0) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001075 die("failed to compile seccomp filter BPF program from fd %d",
1076 fd);
1077 }
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001078 free(path);
Elly Jonese1749eb2011-10-07 13:54:59 -04001079 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -05001080}
1081
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001082void API minijail_set_seccomp_filters(struct minijail *j,
1083 const struct sock_fprog *filter)
1084{
1085 if (!seccomp_should_use_filters(j))
1086 return;
1087
1088 if (j->flags.seccomp_filter_logging) {
1089 die("minijail_log_seccomp_filter_failures() is incompatible "
1090 "with minijail_set_seccomp_filters()");
1091 }
1092
1093 /*
1094 * set_seccomp_filters_internal() can only fail with ENOMEM.
1095 * Furthermore, since we won't own the incoming filter, it will not be
1096 * modified.
1097 */
1098 if (set_seccomp_filters_internal(j, filter, false /* owned */) < 0) {
1099 die("failed to set seccomp filter");
1100 }
1101}
1102
Andrew Brestickereac28942015-11-11 16:04:46 -08001103int API minijail_use_alt_syscall(struct minijail *j, const char *table)
1104{
1105 j->alt_syscall_table = strdup(table);
1106 if (!j->alt_syscall_table)
1107 return -ENOMEM;
1108 j->flags.alt_syscall = 1;
1109 return 0;
1110}
1111
Will Drewryf89aef52011-09-16 16:48:57 -05001112struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -04001113 size_t available;
1114 size_t total;
1115 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -05001116};
1117
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001118void marshal_state_init(struct marshal_state *state, char *buf,
1119 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -04001120{
1121 state->available = available;
1122 state->buf = buf;
1123 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001124}
1125
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001126void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -04001127{
1128 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -05001129
Elly Jonese1749eb2011-10-07 13:54:59 -04001130 /* Up to |available| will be written. */
1131 if (copy_len) {
1132 memcpy(state->buf, src, copy_len);
1133 state->buf += copy_len;
1134 state->available -= copy_len;
1135 }
1136 /* |total| will contain the expected length. */
1137 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -05001138}
1139
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001140void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -07001141{
1142 marshal_append(state, m->src, strlen(m->src) + 1);
1143 marshal_append(state, m->dest, strlen(m->dest) + 1);
1144 marshal_append(state, m->type, strlen(m->type) + 1);
1145 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
1146 if (m->has_data)
1147 marshal_append(state, m->data, strlen(m->data) + 1);
1148 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
1149}
1150
Will Drewry6ac91122011-10-21 16:38:58 -05001151void minijail_marshal_helper(struct marshal_state *state,
1152 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001153{
Dylan Reid648b2202015-10-23 00:50:00 -07001154 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001155 size_t i;
1156
Elly Jonese1749eb2011-10-07 13:54:59 -04001157 marshal_append(state, (char *)j, sizeof(*j));
1158 if (j->user)
1159 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001160 if (j->suppl_gid_list) {
1161 marshal_append(state, j->suppl_gid_list,
1162 j->suppl_gid_count * sizeof(gid_t));
1163 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001164 if (j->chrootdir)
1165 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001166 if (j->hostname)
1167 marshal_append(state, j->hostname, strlen(j->hostname) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -08001168 if (j->alt_syscall_table) {
1169 marshal_append(state, j->alt_syscall_table,
1170 strlen(j->alt_syscall_table) + 1);
1171 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001172 if (j->flags.seccomp_filter && j->filter_prog) {
1173 struct sock_fprog *fp = j->filter_prog;
1174 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001175 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -04001176 }
Dylan Reid648b2202015-10-23 00:50:00 -07001177 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001178 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001179 }
Dylan Reid605ce7f2016-01-19 19:21:00 -08001180 for (i = 0; i < j->cgroup_count; ++i)
1181 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -05001182}
1183
Will Drewry6ac91122011-10-21 16:38:58 -05001184size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001185{
1186 struct marshal_state state;
1187 marshal_state_init(&state, NULL, 0);
1188 minijail_marshal_helper(&state, j);
1189 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -05001190}
1191
Elly Jonese1749eb2011-10-07 13:54:59 -04001192int minijail_marshal(const struct minijail *j, char *buf, size_t available)
1193{
1194 struct marshal_state state;
1195 marshal_state_init(&state, buf, available);
1196 minijail_marshal_helper(&state, j);
1197 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -05001198}
1199
Elly Jonese1749eb2011-10-07 13:54:59 -04001200int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
1201{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -08001202 size_t i;
1203 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -05001204 int ret = -EINVAL;
1205
Elly Jonese1749eb2011-10-07 13:54:59 -04001206 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001207 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001208 memcpy((void *)j, serialized, sizeof(*j));
1209 serialized += sizeof(*j);
1210 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001211
Will Drewrybee7ba72011-10-21 20:47:01 -05001212 /* Potentially stale pointers not used as signals. */
Luis Hector Chavez9acba452018-10-11 10:13:25 -07001213 j->preload_path = NULL;
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001214 j->pid_file_path = NULL;
1215 j->uidmap = NULL;
1216 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001217 j->mounts_head = NULL;
1218 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001219 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001220 j->hooks_head = NULL;
1221 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001222
Elly Jonese1749eb2011-10-07 13:54:59 -04001223 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001224 char *user = consumestr(&serialized, &length);
1225 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001226 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001227 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001228 if (!j->user)
1229 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001230 }
Will Drewryf89aef52011-09-16 16:48:57 -05001231
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001232 if (j->suppl_gid_list) { /* stale pointer */
1233 if (j->suppl_gid_count > NGROUPS_MAX) {
1234 goto bad_gid_list;
1235 }
1236 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1237 void *gid_list_bytes =
1238 consumebytes(gid_list_size, &serialized, &length);
1239 if (!gid_list_bytes)
1240 goto bad_gid_list;
1241
1242 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1243 if (!j->suppl_gid_list)
1244 goto bad_gid_list;
1245
1246 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1247 }
1248
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001249 if (j->chrootdir) { /* stale pointer */
1250 char *chrootdir = consumestr(&serialized, &length);
1251 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001252 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001253 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001254 if (!j->chrootdir)
1255 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001256 }
1257
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001258 if (j->hostname) { /* stale pointer */
1259 char *hostname = consumestr(&serialized, &length);
1260 if (!hostname)
1261 goto bad_hostname;
1262 j->hostname = strdup(hostname);
1263 if (!j->hostname)
1264 goto bad_hostname;
1265 }
1266
Andrew Brestickereac28942015-11-11 16:04:46 -08001267 if (j->alt_syscall_table) { /* stale pointer */
1268 char *alt_syscall_table = consumestr(&serialized, &length);
1269 if (!alt_syscall_table)
1270 goto bad_syscall_table;
1271 j->alt_syscall_table = strdup(alt_syscall_table);
1272 if (!j->alt_syscall_table)
1273 goto bad_syscall_table;
1274 }
1275
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001276 if (j->flags.seccomp_filter && j->filter_len > 0) {
1277 size_t ninstrs = j->filter_len;
1278 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1279 ninstrs > USHRT_MAX)
1280 goto bad_filters;
1281
1282 size_t program_len = ninstrs * sizeof(struct sock_filter);
1283 void *program = consumebytes(program_len, &serialized, &length);
1284 if (!program)
1285 goto bad_filters;
1286
1287 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001288 if (!j->filter_prog)
1289 goto bad_filters;
1290
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001291 j->filter_prog->len = ninstrs;
1292 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001293 if (!j->filter_prog->filter)
1294 goto bad_filter_prog_instrs;
1295
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001296 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001297 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001298
Dylan Reid648b2202015-10-23 00:50:00 -07001299 count = j->mounts_count;
1300 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001301 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001302 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001303 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001304 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001305 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001306 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001307 const char *src = consumestr(&serialized, &length);
1308 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001309 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001310 dest = consumestr(&serialized, &length);
1311 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001312 goto bad_mounts;
1313 type = consumestr(&serialized, &length);
1314 if (!type)
1315 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001316 has_data = consumebytes(sizeof(*has_data), &serialized,
1317 &length);
1318 if (!has_data)
1319 goto bad_mounts;
1320 if (*has_data) {
1321 data = consumestr(&serialized, &length);
1322 if (!data)
1323 goto bad_mounts;
1324 }
Dylan Reid648b2202015-10-23 00:50:00 -07001325 flags = consumebytes(sizeof(*flags), &serialized, &length);
1326 if (!flags)
1327 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001328 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001329 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001330 }
1331
Dylan Reid605ce7f2016-01-19 19:21:00 -08001332 count = j->cgroup_count;
1333 j->cgroup_count = 0;
1334 for (i = 0; i < count; ++i) {
1335 char *cgroup = consumestr(&serialized, &length);
1336 if (!cgroup)
1337 goto bad_cgroups;
1338 j->cgroups[i] = strdup(cgroup);
1339 if (!j->cgroups[i])
1340 goto bad_cgroups;
1341 ++j->cgroup_count;
1342 }
1343
Elly Jonese1749eb2011-10-07 13:54:59 -04001344 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001345
Dylan Reid605ce7f2016-01-19 19:21:00 -08001346bad_cgroups:
Mike Frysingerac08a682017-10-10 02:04:50 -04001347 free_mounts_list(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001348 for (i = 0; i < j->cgroup_count; ++i)
1349 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001350bad_mounts:
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001351 if (j->filter_prog && j->filter_prog->filter)
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001352 free(j->filter_prog->filter);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001353bad_filter_prog_instrs:
1354 if (j->filter_prog)
1355 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001356bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001357 if (j->alt_syscall_table)
1358 free(j->alt_syscall_table);
1359bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001360 if (j->chrootdir)
1361 free(j->chrootdir);
1362bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001363 if (j->hostname)
1364 free(j->hostname);
1365bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001366 if (j->suppl_gid_list)
1367 free(j->suppl_gid_list);
1368bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001369 if (j->user)
1370 free(j->user);
1371clear_pointers:
1372 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001373 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001374 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001375 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001376 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001377 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001378out:
1379 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001380}
1381
Mike Frysinger33ffef32017-01-13 19:53:19 -05001382struct dev_spec {
1383 const char *name;
1384 mode_t mode;
1385 dev_t major, minor;
1386};
1387
1388static const struct dev_spec device_nodes[] = {
1389 {
1390 "null",
1391 S_IFCHR | 0666, 1, 3,
1392 },
1393 {
1394 "zero",
1395 S_IFCHR | 0666, 1, 5,
1396 },
1397 {
1398 "full",
1399 S_IFCHR | 0666, 1, 7,
1400 },
1401 {
1402 "urandom",
1403 S_IFCHR | 0444, 1, 9,
1404 },
1405 {
1406 "tty",
1407 S_IFCHR | 0666, 5, 0,
1408 },
1409};
1410
1411struct dev_sym_spec {
1412 const char *source, *dest;
1413};
1414
1415static const struct dev_sym_spec device_symlinks[] = {
1416 { "ptmx", "pts/ptmx", },
1417 { "fd", "/proc/self/fd", },
1418 { "stdin", "fd/0", },
1419 { "stdout", "fd/1", },
1420 { "stderr", "fd/2", },
1421};
1422
1423/*
1424 * Clean up the temporary dev path we had setup previously. In case of errors,
1425 * we don't want to go leaking empty tempdirs.
1426 */
1427static void mount_dev_cleanup(char *dev_path)
1428{
1429 umount2(dev_path, MNT_DETACH);
1430 rmdir(dev_path);
1431 free(dev_path);
1432}
1433
1434/*
1435 * Set up the pseudo /dev path at the temporary location.
1436 * See mount_dev_finalize for more details.
1437 */
1438static int mount_dev(char **dev_path_ret)
1439{
1440 int ret;
1441 int dev_fd;
1442 size_t i;
1443 mode_t mask;
1444 char *dev_path;
1445
1446 /*
1447 * Create a temp path for the /dev init. We'll relocate this to the
1448 * final location later on in the startup process.
1449 */
1450 dev_path = *dev_path_ret = strdup("/tmp/minijail.dev.XXXXXX");
1451 if (dev_path == NULL || mkdtemp(dev_path) == NULL)
1452 pdie("could not create temp path for /dev");
1453
1454 /* Set up the empty /dev mount point first. */
1455 ret = mount("minijail-devfs", dev_path, "tmpfs",
1456 MS_NOEXEC | MS_NOSUID, "size=5M,mode=755");
1457 if (ret) {
1458 rmdir(dev_path);
1459 return ret;
1460 }
1461
1462 /* We want to set the mode directly from the spec. */
1463 mask = umask(0);
1464
1465 /* Get a handle to the temp dev path for *at funcs below. */
1466 dev_fd = open(dev_path, O_DIRECTORY|O_PATH|O_CLOEXEC);
1467 if (dev_fd < 0) {
1468 ret = 1;
1469 goto done;
1470 }
1471
1472 /* Create all the nodes in /dev. */
1473 for (i = 0; i < ARRAY_SIZE(device_nodes); ++i) {
1474 const struct dev_spec *ds = &device_nodes[i];
1475 ret = mknodat(dev_fd, ds->name, ds->mode,
1476 makedev(ds->major, ds->minor));
1477 if (ret)
1478 goto done;
1479 }
1480
1481 /* Create all the symlinks in /dev. */
1482 for (i = 0; i < ARRAY_SIZE(device_symlinks); ++i) {
1483 const struct dev_sym_spec *ds = &device_symlinks[i];
1484 ret = symlinkat(ds->dest, dev_fd, ds->source);
1485 if (ret)
1486 goto done;
1487 }
1488
1489 /* Restore old mask. */
1490 done:
1491 close(dev_fd);
1492 umask(mask);
1493
1494 if (ret)
1495 mount_dev_cleanup(dev_path);
1496
1497 return ret;
1498}
1499
1500/*
1501 * Relocate the temporary /dev mount to its final /dev place.
1502 * We have to do this two step process so people can bind mount extra
1503 * /dev paths like /dev/log.
1504 */
1505static int mount_dev_finalize(const struct minijail *j, char *dev_path)
1506{
1507 int ret = -1;
1508 char *dest = NULL;
1509
1510 /* Unmount the /dev mount if possible. */
1511 if (umount2("/dev", MNT_DETACH))
1512 goto done;
1513
1514 if (asprintf(&dest, "%s/dev", j->chrootdir ? : "") < 0)
1515 goto done;
1516
1517 if (mount(dev_path, dest, NULL, MS_MOVE, NULL))
1518 goto done;
1519
1520 ret = 0;
1521 done:
1522 free(dest);
1523 mount_dev_cleanup(dev_path);
1524
1525 return ret;
1526}
1527
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001528/*
1529 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001530 * @j Minijail these mounts are for
1531 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001532 *
1533 * Returns 0 for success.
1534 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001535static int mount_one(const struct minijail *j, struct mountpoint *m,
1536 const char *dev_path)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001537{
Dylan Reid648b2202015-10-23 00:50:00 -07001538 int ret;
1539 char *dest;
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001540 int remount = 0;
1541 unsigned long original_mnt_flags = 0;
Dylan Reid648b2202015-10-23 00:50:00 -07001542
Mike Frysinger33ffef32017-01-13 19:53:19 -05001543 /* We assume |dest| has a leading "/". */
1544 if (dev_path && strncmp("/dev/", m->dest, 5) == 0) {
1545 /* Since the temp path is rooted at /dev, skip that dest part. */
1546 if (asprintf(&dest, "%s%s", dev_path, m->dest + 4) < 0)
1547 return -ENOMEM;
1548 } else {
Mike Frysingerac08a682017-10-10 02:04:50 -04001549 if (asprintf(&dest, "%s%s", j->chrootdir ?: "", m->dest) < 0)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001550 return -ENOMEM;
1551 }
Dylan Reid648b2202015-10-23 00:50:00 -07001552
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001553 ret =
1554 setup_mount_destination(m->src, dest, j->uid, j->gid,
1555 (m->flags & MS_BIND), &original_mnt_flags);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001556 if (ret) {
yusukes1b32f852018-03-05 10:24:58 -08001557 warn("creating mount target '%s' failed", dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001558 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001559 }
Dylan Reideec77962016-06-30 19:35:10 -07001560
Dylan Reid648b2202015-10-23 00:50:00 -07001561 /*
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001562 * Bind mounts that change the 'ro' flag have to be remounted since
1563 * 'bind' and other flags can't both be specified in the same command.
1564 * Remount after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001565 */
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001566 if ((m->flags & MS_BIND) &&
1567 ((m->flags & MS_RDONLY) != (original_mnt_flags & MS_RDONLY))) {
1568 remount = 1;
1569 /*
1570 * Restrict the mount flags to those that are user-settable in a
1571 * MS_REMOUNT request, but excluding MS_RDONLY. The
1572 * user-requested mount flags will dictate whether the remount
1573 * will have that flag or not.
1574 */
1575 original_mnt_flags &= (MS_USER_SETTABLE_MASK & ~MS_RDONLY);
Elly Jonesa1059632011-12-15 15:17:07 -05001576 }
Dylan Reid648b2202015-10-23 00:50:00 -07001577
Dylan Reid81e23972016-05-18 14:06:35 -07001578 ret = mount(m->src, dest, m->type, m->flags, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001579 if (ret) {
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001580 pwarn("bind: %s -> %s flags=%#lx", m->src, dest, m->flags);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001581 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001582 }
Dylan Reid648b2202015-10-23 00:50:00 -07001583
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001584 if (remount) {
1585 ret =
1586 mount(m->src, dest, NULL,
1587 m->flags | original_mnt_flags | MS_REMOUNT, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001588 if (ret) {
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001589 pwarn("bind remount: %s -> %s flags=%#lx", m->src, dest,
1590 m->flags | original_mnt_flags | MS_REMOUNT);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001591 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001592 }
Dylan Reid648b2202015-10-23 00:50:00 -07001593 }
1594
Elly Jones51a5b6c2011-10-12 19:09:26 -04001595 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001596 if (m->next)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001597 return mount_one(j, m->next, dev_path);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001598 return 0;
1599
1600error:
1601 free(dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001602 return ret;
1603}
1604
Mike Frysingerac08a682017-10-10 02:04:50 -04001605static void process_mounts_or_die(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001606{
Mike Frysingerac08a682017-10-10 02:04:50 -04001607 /*
1608 * We have to mount /dev first in case there are bind mounts from
1609 * the original /dev into the new unique tmpfs one.
1610 */
1611 char *dev_path = NULL;
1612 if (j->flags.mount_dev && mount_dev(&dev_path))
1613 pdie("mount_dev failed");
Dylan Reid648b2202015-10-23 00:50:00 -07001614
Mike Frysingerac08a682017-10-10 02:04:50 -04001615 if (j->mounts_head && mount_one(j, j->mounts_head, dev_path)) {
1616 if (dev_path) {
1617 int saved_errno = errno;
1618 mount_dev_cleanup(dev_path);
1619 errno = saved_errno;
1620 }
1621 pdie("mount_one failed");
1622 }
Mike Frysinger33ffef32017-01-13 19:53:19 -05001623
1624 /*
Mike Frysingerac08a682017-10-10 02:04:50 -04001625 * Once all bind mounts have been processed, move the temp dev to
1626 * its final /dev home.
Mike Frysinger33ffef32017-01-13 19:53:19 -05001627 */
1628 if (j->flags.mount_dev && mount_dev_finalize(j, dev_path))
Mike Frysingerac08a682017-10-10 02:04:50 -04001629 pdie("mount_dev_finalize failed");
1630}
Elly Jones51a5b6c2011-10-12 19:09:26 -04001631
Mike Frysingerac08a682017-10-10 02:04:50 -04001632static int enter_chroot(const struct minijail *j)
1633{
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001634 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1635
Elly Jones51a5b6c2011-10-12 19:09:26 -04001636 if (chroot(j->chrootdir))
1637 return -errno;
1638
1639 if (chdir("/"))
1640 return -errno;
1641
1642 return 0;
1643}
1644
Mike Frysingerac08a682017-10-10 02:04:50 -04001645static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001646{
Mike Frysingerac08a682017-10-10 02:04:50 -04001647 int oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001648
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001649 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1650
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001651 /*
1652 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001653 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001654 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001655 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001656 if (oldroot < 0)
1657 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001658 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001659 if (newroot < 0)
1660 pdie("failed to open %s for fchdir", j->chrootdir);
1661
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001662 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001663 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001664 * do a self bind mount.
1665 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001666 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1667 pdie("failed to bind mount '%s'", j->chrootdir);
1668 if (chdir(j->chrootdir))
1669 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001670 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001671 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001672
1673 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001674 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001675 * change to the old root and unmount it.
1676 */
1677 if (fchdir(oldroot))
1678 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001679
1680 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05001681 * If skip_remount_private was enabled for minijail_enter(),
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001682 * there could be a shared mount point under |oldroot|. In that case,
1683 * mounts under this shared mount point will be unmounted below, and
1684 * this unmounting will propagate to the original mount namespace
1685 * (because the mount point is shared). To prevent this unexpected
1686 * unmounting, remove these mounts from their peer groups by recursively
1687 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001688 */
1689 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001690 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001691 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001692 if (umount2(".", MNT_DETACH))
1693 pdie("umount(/)");
1694 /* Change back to the new root. */
1695 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001696 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001697 if (close(oldroot))
1698 return -errno;
1699 if (close(newroot))
1700 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001701 if (chroot("/"))
1702 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001703 /* Set correct CWD for getcwd(3). */
1704 if (chdir("/"))
1705 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001706
1707 return 0;
1708}
1709
Martin Pelikánab9eb442017-01-25 11:53:58 +11001710static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001711{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001712 const char fmt[] = "size=%zu,mode=1777";
1713 /* Count for the user storing ULLONG_MAX literally + extra space. */
1714 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1715 int ret;
1716
1717 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1718
1719 if (ret <= 0)
1720 pdie("tmpfs size spec error");
1721 else if ((size_t)ret >= sizeof(data))
1722 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001723 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001724 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001725}
1726
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001727static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001728{
1729 const char *kProcPath = "/proc";
1730 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001731 /*
1732 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001733 * /proc in our namespace, which means using MS_REMOUNT here would
1734 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001735 * namespace (!). Instead, remove their mount from our namespace lazily
1736 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001737 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001738 if (umount2(kProcPath, MNT_DETACH)) {
1739 /*
1740 * If we are in a new user namespace, umount(2) will fail.
1741 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1742 */
1743 if (j->flags.userns) {
1744 info("umount(/proc, MNT_DETACH) failed, "
1745 "this is expected when using user namespaces");
1746 } else {
1747 return -errno;
1748 }
1749 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001750 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001751 return -errno;
1752 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001753}
1754
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001755static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001756{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001757 kill(j->initpid, SIGKILL);
1758 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001759}
1760
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001761static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001762{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001763 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001764 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001765}
1766
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001767static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001768{
1769 size_t i;
1770
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001771 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001772 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001773 kill_child_and_die(j, "failed to add to cgroups");
1774 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001775}
1776
Dylan Reid0f72ef42017-06-06 15:42:49 -07001777static void set_rlimits_or_die(const struct minijail *j)
1778{
1779 size_t i;
1780
1781 for (i = 0; i < j->rlimit_count; ++i) {
1782 struct rlimit limit;
1783 limit.rlim_cur = j->rlimits[i].cur;
1784 limit.rlim_max = j->rlimits[i].max;
1785 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1786 kill_child_and_die(j, "failed to set rlimit");
1787 }
1788}
1789
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001790static void write_ugid_maps_or_die(const struct minijail *j)
1791{
1792 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1793 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001794 if (j->gidmap && j->flags.disable_setgroups) {
1795 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1796 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001797 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001798 if (ret == -ENOENT) {
1799 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1800 warn("could not disable setgroups(2)");
1801 } else
1802 kill_child_and_die(j, "failed to disable setgroups(2)");
1803 }
1804 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001805 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1806 kill_child_and_die(j, "failed to write gid_map");
1807}
1808
1809static void enter_user_namespace(const struct minijail *j)
1810{
Luis Hector Chavez71323552017-09-05 09:17:22 -07001811 int uid = j->flags.uid ? j->uid : 0;
1812 int gid = j->flags.gid ? j->gid : 0;
1813 if (j->gidmap && setresgid(gid, gid, gid)) {
1814 pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid,
1815 gid);
1816 }
1817 if (j->uidmap && setresuid(uid, uid, uid)) {
1818 pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid,
1819 uid);
1820 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001821}
1822
1823static void parent_setup_complete(int *pipe_fds)
1824{
1825 close(pipe_fds[0]);
1826 close(pipe_fds[1]);
1827}
1828
1829/*
1830 * wait_for_parent_setup: Called by the child process to wait for any
1831 * further parent-side setup to complete before continuing.
1832 */
1833static void wait_for_parent_setup(int *pipe_fds)
1834{
1835 char buf;
1836
1837 close(pipe_fds[1]);
1838
1839 /* Wait for parent to complete setup and close the pipe. */
1840 if (read(pipe_fds[0], &buf, 1) != 0)
1841 die("failed to sync with parent");
1842 close(pipe_fds[0]);
1843}
1844
1845static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001846{
Lutz Justen13807cb2017-01-03 17:11:55 +01001847 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1848 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001849 die("can only do one of inherit, keep, or set supplementary "
1850 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001851 }
1852
Lutz Justen13807cb2017-01-03 17:11:55 +01001853 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001854 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001855 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001856 } else if (j->flags.set_suppl_gids) {
1857 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001858 pdie("setgroups(suppl_gids) failed");
Luis Hector Chavez71323552017-09-05 09:17:22 -07001859 } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001860 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001861 * Only attempt to clear supplementary groups if we are changing
Luis Hector Chavez71323552017-09-05 09:17:22 -07001862 * users or groups, and if the caller did not request to disable
1863 * setgroups (used when entering a user namespace as a
1864 * non-privileged user).
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001865 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001866 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001867 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001868 }
1869
1870 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001871 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001872
1873 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001874 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001875}
1876
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001877static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1878{
1879 const uint64_t one = 1;
1880 unsigned int i;
1881 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1882 if (keep_mask & (one << i))
1883 continue;
1884 if (prctl(PR_CAPBSET_DROP, i))
1885 pdie("could not drop capability from bounding set");
1886 }
1887}
1888
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001889static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001890{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001891 if (!j->flags.use_caps)
1892 return;
1893
Elly Jonese1749eb2011-10-07 13:54:59 -04001894 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001895 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001896 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001897 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001898 unsigned int i;
1899 if (!caps)
1900 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001901 if (cap_clear(caps))
1902 die("can't clear caps");
1903
1904 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001905 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001906 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001907 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001908 flag[0] = i;
1909 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001910 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001911 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001912 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001913 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001914 die("can't add inheritable cap");
1915 }
1916 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001917 die("can't apply initial cleaned capset");
1918
1919 /*
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001920 * Instead of dropping the bounding set first, do it here in case
Kees Cook323878a2013-02-05 15:35:24 -08001921 * the caller had a more permissive bounding set which could
1922 * have been used above to raise a capability that wasn't already
1923 * present. This requires CAP_SETPCAP, so we raised/kept it above.
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001924 *
1925 * However, if we're asked to skip setting *and* locking the
1926 * SECURE_NOROOT securebit, also skip dropping the bounding set.
1927 * If the caller wants to regain all capabilities when executing a
1928 * set-user-ID-root program, allow them to do so. The default behavior
1929 * (i.e. the behavior without |securebits_skip_mask| set) will still put
1930 * the jailed process tree in a capabilities-only environment.
1931 *
1932 * We check the negated skip mask for SECURE_NOROOT and
1933 * SECURE_NOROOT_LOCKED. If the bits are set in the negated mask they
1934 * will *not* be skipped in lock_securebits(), and therefore we should
1935 * drop the bounding set.
Kees Cook323878a2013-02-05 15:35:24 -08001936 */
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001937 if (secure_noroot_set_and_locked(~j->securebits_skip_mask)) {
1938 drop_capbset(j->caps, last_valid_cap);
1939 } else {
1940 warn("SECURE_NOROOT not set, not dropping bounding set");
1941 }
Kees Cook323878a2013-02-05 15:35:24 -08001942
1943 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001944 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001945 flag[0] = CAP_SETPCAP;
1946 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1947 die("can't clear effective cap");
1948 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1949 die("can't clear permitted cap");
1950 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1951 die("can't clear inheritable cap");
1952 }
1953
1954 if (cap_set_proc(caps))
1955 die("can't apply final cleaned capset");
1956
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001957 /*
1958 * If ambient capabilities are supported, clear all capabilities first,
1959 * then raise the requested ones.
1960 */
1961 if (j->flags.set_ambient_caps) {
1962 if (!cap_ambient_supported()) {
1963 pdie("ambient capabilities not supported");
1964 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001965 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1966 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001967 pdie("can't clear ambient capabilities");
1968 }
1969
1970 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1971 if (!(j->caps & (one << i)))
1972 continue;
1973
1974 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1975 0) != 0) {
1976 pdie("prctl(PR_CAP_AMBIENT, "
1977 "PR_CAP_AMBIENT_RAISE, %u) failed",
1978 i);
1979 }
1980 }
1981 }
1982
Kees Cook323878a2013-02-05 15:35:24 -08001983 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001984}
1985
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001986static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001987{
1988 /*
1989 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1990 * in the kernel source tree for an explanation of the parameters.
1991 */
1992 if (j->flags.no_new_privs) {
1993 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1994 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1995 }
1996
1997 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001998 * Code running with ASan
1999 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
2000 * will make system calls not included in the syscall filter policy,
2001 * which will likely crash the program. Skip setting seccomp filter in
2002 * that case.
2003 * 'running_with_asan()' has no inputs and is completely defined at
2004 * build time, so this cannot be used by an attacker to skip setting
2005 * seccomp filter.
2006 */
Evgenii Stepanov3d98f3c2018-08-23 15:06:50 -07002007 if (j->flags.seccomp_filter && running_with_asan()) {
Evgenii Stepanov825828c2018-07-27 11:57:07 -07002008 warn("running with (HW)ASan, not setting seccomp filter");
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07002009 return;
2010 }
2011
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002012 if (j->flags.seccomp_filter) {
2013 if (j->flags.seccomp_filter_logging) {
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002014 warn("logging seccomp filter failures");
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04002015 if (!seccomp_ret_log_available()) {
2016 /*
2017 * If SECCOMP_RET_LOG is not available,
2018 * install the SIGSYS handler first.
2019 */
2020 if (install_sigsys_handler())
2021 pdie(
2022 "failed to install SIGSYS handler");
2023 }
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002024 } else if (j->flags.seccomp_filter_tsync) {
2025 /*
2026 * If setting thread sync,
2027 * reset the SIGSYS signal handler so that
2028 * the entire thread group is killed.
2029 */
2030 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
2031 pdie("failed to reset SIGSYS disposition");
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002032 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002033 }
2034
2035 /*
2036 * Install the syscall filter.
2037 */
2038 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002039 if (j->flags.seccomp_filter_tsync) {
2040 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
2041 SECCOMP_FILTER_FLAG_TSYNC,
2042 j->filter_prog)) {
2043 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002044 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002045 } else {
2046 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
2047 j->filter_prog)) {
2048 pdie("prctl(seccomp_filter) failed");
2049 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002050 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002051 }
2052}
2053
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002054static pid_t forward_pid = -1;
2055
Mike Frysinger33d051a2018-05-30 16:41:10 -04002056static void forward_signal(int sig,
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04002057 siginfo_t *siginfo attribute_unused,
2058 void *void_context attribute_unused)
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002059{
2060 if (forward_pid != -1) {
Mike Frysinger33d051a2018-05-30 16:41:10 -04002061 kill(forward_pid, sig);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002062 }
2063}
2064
2065static void install_signal_handlers(void)
2066{
2067 struct sigaction act;
2068
2069 memset(&act, 0, sizeof(act));
2070 act.sa_sigaction = &forward_signal;
2071 act.sa_flags = SA_SIGINFO | SA_RESTART;
2072
2073 /* Handle all signals, except SIGCHLD. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002074 for (int sig = 1; sig < NSIG; sig++) {
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002075 /*
2076 * We don't care if we get EINVAL: that just means that we
2077 * can't handle this signal, so let's skip it and continue.
2078 */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002079 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002080 }
2081 /* Reset SIGCHLD's handler. */
2082 signal(SIGCHLD, SIG_DFL);
2083
2084 /* Handle real-time signals. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002085 for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++) {
2086 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002087 }
2088}
2089
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002090static const char *lookup_hook_name(minijail_hook_event_t event)
2091{
2092 switch (event) {
2093 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
2094 return "pre-drop-caps";
2095 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
2096 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07002097 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
2098 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002099 case MINIJAIL_HOOK_EVENT_MAX:
2100 /*
2101 * Adding this in favor of a default case to force the
2102 * compiler to error out if a new enum value is added.
2103 */
2104 break;
2105 }
2106 return "unknown";
2107}
2108
2109static void run_hooks_or_die(const struct minijail *j,
2110 minijail_hook_event_t event)
2111{
2112 int rc;
2113 int hook_index = 0;
2114 for (struct hook *c = j->hooks_head; c; c = c->next) {
2115 if (c->event != event)
2116 continue;
2117 rc = c->hook(c->payload);
2118 if (rc != 0) {
2119 errno = -rc;
2120 pdie("%s hook (index %d) failed",
2121 lookup_hook_name(event), hook_index);
2122 }
2123 /* Only increase the index within the same hook event type. */
2124 ++hook_index;
2125 }
2126}
2127
Will Drewry6ac91122011-10-21 16:38:58 -05002128void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002129{
Dylan Reidf682d472015-09-17 21:39:07 -07002130 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002131 * If we're dropping caps, get the last valid cap from /proc now,
2132 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07002133 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002134 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002135 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002136 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07002137
Elly Jonese1749eb2011-10-07 13:54:59 -04002138 if (j->flags.pids)
2139 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002140 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04002141
Lutz Justen13807cb2017-01-03 17:11:55 +01002142 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05002143 die("cannot inherit supplementary groups without setting a "
2144 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04002145
Elly Jonesdd3e8512012-01-23 15:13:38 -05002146 /*
2147 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04002148 * so we don't even try. If any of our operations fail, we abort() the
2149 * entire process.
2150 */
Mike Frysinger902a4492018-12-27 05:22:56 -05002151 if (j->flags.enter_vfs) {
2152 if (setns(j->mountns_fd, CLONE_NEWNS))
2153 pdie("setns(CLONE_NEWNS) failed");
2154 close(j->mountns_fd);
2155 }
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002156
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07002157 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002158 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002159 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002160 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05002161 * By default, remount all filesystems as private, unless
2162 * - Passed a specific remount mode, in which case remount with that,
2163 * - Asked not to remount at all, in which case skip the mount(2) call.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002164 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
2165 */
Mike Frysinger785b1c32018-02-23 15:47:24 -05002166 if (j->remount_mode) {
2167 if (mount(NULL, "/", NULL, MS_REC | j->remount_mode, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002168 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
2169 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002170 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002171 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04002172
Dylan Reidf7942472015-11-18 17:55:26 -08002173 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002174 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08002175 }
2176
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002177 if (j->flags.uts) {
2178 if (unshare(CLONE_NEWUTS))
2179 pdie("unshare(CLONE_NEWUTS) failed");
2180
2181 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
2182 pdie("sethostname(%s) failed", j->hostname);
2183 }
2184
Dylan Reid1102f5a2015-09-15 11:52:20 -07002185 if (j->flags.enter_net) {
2186 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002187 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger902a4492018-12-27 05:22:56 -05002188 close(j->netns_fd);
Mike Frysinger7559dfe2016-11-15 18:58:39 -05002189 } else if (j->flags.net) {
2190 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002191 pdie("unshare(CLONE_NEWNET) failed");
2192 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07002193 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002194
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002195 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002196 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002197
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08002198 if (j->flags.new_session_keyring) {
2199 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
2200 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
2201 }
2202
Mike Frysingerac08a682017-10-10 02:04:50 -04002203 /* We have to process all the mounts before we chroot/pivot_root. */
2204 process_mounts_or_die(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002205
Mike Frysingerac08a682017-10-10 02:04:50 -04002206 if (j->flags.chroot && enter_chroot(j))
Mike Frysinger33ffef32017-01-13 19:53:19 -05002207 pdie("chroot");
Mike Frysinger33ffef32017-01-13 19:53:19 -05002208
Mike Frysingerac08a682017-10-10 02:04:50 -04002209 if (j->flags.pivot_root && enter_pivot_root(j))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08002210 pdie("pivot_root");
2211
Martin Pelikánab9eb442017-01-25 11:53:58 +11002212 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07002213 pdie("mount_tmp");
2214
Dylan Reid791f5772015-09-14 20:02:42 -07002215 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04002216 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04002217
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002218 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
2219
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002220 /*
2221 * If we're only dropping capabilities from the bounding set, but not
2222 * from the thread's (permitted|inheritable|effective) sets, do it now.
2223 */
2224 if (j->flags.capbset_drop) {
2225 drop_capbset(j->cap_bset, last_valid_cap);
2226 }
2227
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002228 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002229 * POSIX capabilities are a bit tricky. We must set SECBIT_KEEP_CAPS
2230 * before drop_ugid() below as the latter would otherwise drop all
2231 * capabilities.
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002232 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002233 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002234 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002235 * When using ambient capabilities, CAP_SET{GID,UID} can be
2236 * inherited across execve(2), so SECBIT_KEEP_CAPS is not
2237 * strictly needed.
Elly Jonese1749eb2011-10-07 13:54:59 -04002238 */
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002239 bool require_keep_caps = !j->flags.set_ambient_caps;
2240 if (lock_securebits(j->securebits_skip_mask,
2241 require_keep_caps) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002242 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002243 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002244 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002245
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002246 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002247 /*
2248 * If we're setting no_new_privs, we can drop privileges
2249 * before setting seccomp filter. This way filter policies
2250 * don't need to allow privilege-dropping syscalls.
2251 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002252 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002253 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002254 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002255 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002256 /*
2257 * If we're not setting no_new_privs,
2258 * we need to set seccomp filter *before* dropping privileges.
2259 * WARNING: this means that filter policies *must* allow
2260 * setgroups()/setresgid()/setresuid() for dropping root and
2261 * capget()/capset()/prctl() for dropping caps.
2262 */
2263 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002264 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002265 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04002266 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002267
Elly Jonesdd3e8512012-01-23 15:13:38 -05002268 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08002269 * Select the specified alternate syscall table. The table must not
2270 * block prctl(2) if we're using seccomp as well.
2271 */
2272 if (j->flags.alt_syscall) {
2273 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002274 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08002275 }
2276
2277 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05002278 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04002279 * privilege-dropping syscalls :)
2280 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002281 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04002282 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002283 warn("seccomp not supported");
2284 return;
2285 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002286 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002287 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002288}
2289
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002290/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04002291static int init_exitstatus = 0;
2292
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04002293void init_term(int sig attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002294{
2295 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04002296}
2297
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002298void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04002299{
2300 pid_t pid;
2301 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002302 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002303 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002304 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002305 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002306 /*
2307 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04002308 * left inside our pid namespace or we get a signal.
2309 */
2310 if (pid == rootpid)
2311 init_exitstatus = status;
2312 }
2313 if (!WIFEXITED(init_exitstatus))
2314 _exit(MINIJAIL_ERR_INIT);
2315 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04002316}
2317
Will Drewry6ac91122011-10-21 16:38:58 -05002318int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002319{
2320 size_t sz = 0;
2321 size_t bytes = read(fd, &sz, sizeof(sz));
2322 char *buf;
2323 int r;
2324 if (sizeof(sz) != bytes)
2325 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002326 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04002327 return -E2BIG;
2328 buf = malloc(sz);
2329 if (!buf)
2330 return -ENOMEM;
2331 bytes = read(fd, buf, sz);
2332 if (bytes != sz) {
2333 free(buf);
2334 return -EINVAL;
2335 }
2336 r = minijail_unmarshal(j, buf, sz);
2337 free(buf);
2338 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002339}
2340
Will Drewry6ac91122011-10-21 16:38:58 -05002341int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04002342{
2343 char *buf;
2344 size_t sz = minijail_size(j);
2345 ssize_t written;
2346 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04002347
Elly Jonese1749eb2011-10-07 13:54:59 -04002348 if (!sz)
2349 return -EINVAL;
2350 buf = malloc(sz);
2351 r = minijail_marshal(j, buf, sz);
2352 if (r) {
2353 free(buf);
2354 return r;
2355 }
2356 /* Sends [size][minijail]. */
2357 written = write(fd, &sz, sizeof(sz));
2358 if (written != sizeof(sz)) {
2359 free(buf);
2360 return -EFAULT;
2361 }
2362 written = write(fd, buf, sz);
2363 if (written < 0 || (size_t) written != sz) {
2364 free(buf);
2365 return -EFAULT;
2366 }
2367 free(buf);
2368 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002369}
Elly Jonescd7a9042011-07-22 13:56:51 -04002370
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002371static int setup_preload(const struct minijail *j attribute_unused,
2372 const char *oldenv attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002373{
Daniel Erat5b7a3182015-08-19 16:06:22 -06002374#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002375 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002376 return 0;
2377#else
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002378 const char *preload_path = j->preload_path ?: PRELOADPATH;
2379 char *newenv = NULL;
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002380 int ret = 0;
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002381
2382 if (!oldenv)
2383 oldenv = "";
Elly Jonescd7a9042011-07-22 13:56:51 -04002384
Elly Jonese1749eb2011-10-07 13:54:59 -04002385 /* Only insert a separating space if we have something to separate... */
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002386 if (asprintf(&newenv, "%s%s%s", oldenv, oldenv[0] != '\0' ? " " : "",
2387 preload_path) < 0) {
2388 return -1;
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002389 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002390
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002391 /*
2392 * Avoid using putenv(3), since that requires us to hold onto a
2393 * reference to that string until the environment is no longer used to
2394 * prevent a memory leak.
2395 * See https://crbug.com/930189 for more details.
2396 */
2397 ret = setenv(kLdPreloadEnvVar, newenv, 1);
2398 free(newenv);
2399 return ret;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002400#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04002401}
2402
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002403static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04002404{
2405 int r = pipe(fds);
2406 char fd_buf[11];
2407 if (r)
2408 return r;
2409 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2410 if (r <= 0)
2411 return -EINVAL;
2412 setenv(kFdEnvVar, fd_buf, 1);
2413 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05002414}
2415
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002416static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002417{
2418 const char *kFdPath = "/proc/self/fd";
2419
2420 DIR *d = opendir(kFdPath);
2421 struct dirent *dir_entry;
2422
2423 if (d == NULL)
2424 return -1;
2425 int dir_fd = dirfd(d);
2426 while ((dir_entry = readdir(d)) != NULL) {
2427 size_t i;
2428 char *end;
2429 bool should_close = true;
2430 const int fd = strtol(dir_entry->d_name, &end, 10);
2431
2432 if ((*end) != '\0') {
2433 continue;
2434 }
2435 /*
2436 * We might have set up some pipes that we want to share with
2437 * the parent process, and should not be closed.
2438 */
2439 for (i = 0; i < size; ++i) {
2440 if (fd == inheritable_fds[i]) {
2441 should_close = false;
2442 break;
2443 }
2444 }
2445 /* Also avoid closing the directory fd. */
2446 if (should_close && fd != dir_fd)
2447 close(fd);
2448 }
2449 closedir(d);
2450 return 0;
2451}
2452
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002453static int redirect_fds(struct minijail *j)
2454{
2455 size_t i, i2;
2456 int closeable;
2457 for (i = 0; i < j->preserved_fd_count; i++) {
2458 if (dup2(j->preserved_fds[i].parent_fd,
2459 j->preserved_fds[i].child_fd) == -1) {
2460 return -1;
2461 }
2462 }
2463 /*
2464 * After all fds have been duped, we are now free to close all parent
2465 * fds that are *not* child fds.
2466 */
2467 for (i = 0; i < j->preserved_fd_count; i++) {
2468 closeable = true;
2469 for (i2 = 0; i2 < j->preserved_fd_count; i2++) {
2470 closeable &= j->preserved_fds[i].parent_fd !=
2471 j->preserved_fds[i2].child_fd;
2472 }
2473 if (closeable)
2474 close(j->preserved_fds[i].parent_fd);
2475 }
2476 return 0;
2477}
2478
Dylan Reidacfb8be2017-08-25 12:56:51 -07002479/*
2480 * Structure that specifies how to start a minijail.
2481 *
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002482 * filename - The program to exec in the child. Required if |exec_in_child| = 1.
2483 * argv - Arguments for the child program. Required if |exec_in_child| = 1.
2484 * envp - Environment for the child program. Available if |exec_in_child| = 1.
2485 Currently only honored if |use_preload| = 0 and non-NULL.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002486 * use_preload - If true use LD_PRELOAD.
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002487 * exec_in_child - If true, run |filename|. Otherwise, the child will return to
Dylan Reid0412dcc2017-08-24 11:33:15 -07002488 * the caller.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002489 */
2490struct minijail_run_config {
2491 const char *filename;
2492 char *const *argv;
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002493 char *const *envp;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002494 int use_preload;
Dylan Reid0412dcc2017-08-24 11:33:15 -07002495 int exec_in_child;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002496};
2497
2498/*
2499 * Set of pointers to fill with values from minijail_run.
2500 * All arguments are allowed to be NULL if unused.
2501 *
2502 * pstdin_fd - Filled with stdin pipe if non-NULL.
2503 * pstdout_fd - Filled with stdout pipe if non-NULL.
2504 * pstderr_fd - Filled with stderr pipe if non-NULL.
2505 * pchild_pid - Filled with the pid of the child process if non-NULL.
2506 */
2507struct minijail_run_status {
2508 int *pstdin_fd;
2509 int *pstdout_fd;
2510 int *pstderr_fd;
2511 pid_t *pchild_pid;
2512};
2513
Dylan Reid18c49c82017-08-25 14:52:27 -07002514static int minijail_run_internal(struct minijail *j,
2515 const struct minijail_run_config *config,
2516 struct minijail_run_status *status_out);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002517
Will Drewry6ac91122011-10-21 16:38:58 -05002518int API minijail_run(struct minijail *j, const char *filename,
2519 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002520{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002521 struct minijail_run_config config = {
2522 .filename = filename,
2523 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002524 .envp = NULL,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002525 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002526 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002527 };
2528 struct minijail_run_status status = {};
2529 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002530}
2531
2532int API minijail_run_pid(struct minijail *j, const char *filename,
2533 char *const argv[], pid_t *pchild_pid)
2534{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002535 struct minijail_run_config config = {
2536 .filename = filename,
2537 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002538 .envp = NULL,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002539 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002540 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002541 };
2542 struct minijail_run_status status = {
2543 .pchild_pid = pchild_pid,
2544 };
2545 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002546}
2547
2548int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002549 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002550{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002551 struct minijail_run_config config = {
2552 .filename = filename,
2553 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002554 .envp = NULL,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002555 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002556 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002557 };
2558 struct minijail_run_status status = {
2559 .pstdin_fd = pstdin_fd,
2560 };
2561 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002562}
2563
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002564int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002565 char *const argv[], pid_t *pchild_pid,
2566 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002567{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002568 struct minijail_run_config config = {
2569 .filename = filename,
2570 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002571 .envp = NULL,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002572 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002573 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002574 };
2575 struct minijail_run_status status = {
2576 .pstdin_fd = pstdin_fd,
2577 .pstdout_fd = pstdout_fd,
2578 .pstderr_fd = pstderr_fd,
2579 .pchild_pid = pchild_pid,
2580 };
2581 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002582}
2583
2584int API minijail_run_no_preload(struct minijail *j, const char *filename,
2585 char *const argv[])
2586{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002587 struct minijail_run_config config = {
2588 .filename = filename,
2589 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002590 .envp = NULL,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002591 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002592 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002593 };
2594 struct minijail_run_status status = {};
2595 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002596}
2597
Samuel Tan63187f42015-10-16 13:01:53 -07002598int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002599 const char *filename,
2600 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002601 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002602 int *pstdin_fd,
2603 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002604 int *pstderr_fd)
2605{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002606 struct minijail_run_config config = {
2607 .filename = filename,
2608 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002609 .envp = NULL,
2610 .use_preload = false,
2611 .exec_in_child = true,
2612 };
2613 struct minijail_run_status status = {
2614 .pstdin_fd = pstdin_fd,
2615 .pstdout_fd = pstdout_fd,
2616 .pstderr_fd = pstderr_fd,
2617 .pchild_pid = pchild_pid,
2618 };
2619 return minijail_run_internal(j, &config, &status);
2620}
2621
2622int API minijail_run_env_pid_pipes_no_preload(struct minijail *j,
2623 const char *filename,
2624 char *const argv[],
2625 char *const envp[],
2626 pid_t *pchild_pid, int *pstdin_fd,
2627 int *pstdout_fd, int *pstderr_fd)
2628{
2629 struct minijail_run_config config = {
2630 .filename = filename,
2631 .argv = argv,
2632 .envp = envp,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002633 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002634 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002635 };
2636 struct minijail_run_status status = {
2637 .pstdin_fd = pstdin_fd,
2638 .pstdout_fd = pstdout_fd,
2639 .pstderr_fd = pstderr_fd,
2640 .pchild_pid = pchild_pid,
2641 };
2642 return minijail_run_internal(j, &config, &status);
Samuel Tan63187f42015-10-16 13:01:53 -07002643}
2644
Dylan Reid0412dcc2017-08-24 11:33:15 -07002645pid_t API minijail_fork(struct minijail *j)
2646{
2647 struct minijail_run_config config = {};
2648 struct minijail_run_status status = {};
2649 return minijail_run_internal(j, &config, &status);
2650}
2651
Dylan Reid18c49c82017-08-25 14:52:27 -07002652static int minijail_run_internal(struct minijail *j,
2653 const struct minijail_run_config *config,
2654 struct minijail_run_status *status_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002655{
Elly Jonese1749eb2011-10-07 13:54:59 -04002656 char *oldenv, *oldenv_copy = NULL;
2657 pid_t child_pid;
2658 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002659 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002660 int stdout_fds[2];
2661 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08002662 int child_sync_pipe_fds[2];
2663 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002664 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002665 /* We need to remember this across the minijail_preexec() call. */
2666 int pid_namespace = j->flags.pids;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -07002667 /*
2668 * Create an init process if we are entering a pid namespace, unless the
2669 * user has explicitly opted out by calling minijail_run_as_init().
2670 */
2671 int do_init = j->flags.do_init && !j->flags.run_as_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002672 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002673
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002674 if (use_preload) {
Dylan Reid0412dcc2017-08-24 11:33:15 -07002675 if (j->hooks_head != NULL)
2676 die("Minijail hooks are not supported with LD_PRELOAD");
2677 if (!config->exec_in_child)
2678 die("minijail_fork is not supported with LD_PRELOAD");
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002679 if (config->envp != NULL)
2680 die("cannot pass a new environment with LD_PRELOAD");
Dylan Reid0412dcc2017-08-24 11:33:15 -07002681
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002682 oldenv = getenv(kLdPreloadEnvVar);
2683 if (oldenv) {
2684 oldenv_copy = strdup(oldenv);
2685 if (!oldenv_copy)
2686 return -ENOMEM;
2687 }
2688
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002689 if (setup_preload(j, oldenv))
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002690 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002691 }
Will Drewryf89aef52011-09-16 16:48:57 -05002692
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002693 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002694 if (j->flags.use_caps && j->caps != 0 &&
2695 !j->flags.set_ambient_caps) {
2696 die("non-empty, non-ambient capabilities are not "
2697 "supported without LD_PRELOAD");
2698 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002699 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002700
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002701 if (use_preload) {
2702 /*
2703 * Before we fork(2) and execve(2) the child process, we need
2704 * to open a pipe(2) to send the minijail configuration over.
2705 */
2706 if (setup_pipe(pipe_fds))
2707 return -EFAULT;
2708 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002709
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002710 /*
2711 * If we want to write to the child process' standard input,
2712 * create the pipe(2) now.
2713 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002714 if (status_out->pstdin_fd) {
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002715 if (pipe(stdin_fds))
2716 return -EFAULT;
2717 }
2718
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002719 /*
2720 * If we want to read from the child process' standard output,
2721 * create the pipe(2) now.
2722 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002723 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002724 if (pipe(stdout_fds))
2725 return -EFAULT;
2726 }
2727
2728 /*
2729 * If we want to read from the child process' standard error,
2730 * create the pipe(2) now.
2731 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002732 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002733 if (pipe(stderr_fds))
2734 return -EFAULT;
2735 }
2736
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002737 /*
Daniel Erat2d69add2019-04-23 20:58:53 -07002738 * If the parent process needs to configure the child's runtime
2739 * environment after forking, create a pipe(2) to block the child until
2740 * configuration is done.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002741 */
Daniel Erat2d69add2019-04-23 20:58:53 -07002742 if (j->flags.forward_signals || j->flags.pid_file || j->flags.cgroups ||
2743 j->rlimit_count || j->flags.userns) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002744 sync_child = 1;
2745 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002746 return -EFAULT;
2747 }
2748
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002749 /*
2750 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002751 *
2752 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2753 *
2754 * In multithreaded programs, there are a bunch of locks inside libc,
2755 * some of which may be held by other threads at the time that we call
2756 * minijail_run_pid(). If we call fork(), glibc does its level best to
2757 * ensure that we hold all of these locks before it calls clone()
2758 * internally and drop them after clone() returns, but when we call
2759 * sys_clone(2) directly, all that gets bypassed and we end up with a
2760 * child address space where some of libc's important locks are held by
2761 * other threads (which did not get cloned, and hence will never release
2762 * those locks). This is okay so long as we call exec() immediately
2763 * after, but a bunch of seemingly-innocent libc functions like setenv()
2764 * take locks.
2765 *
2766 * Hence, only call sys_clone() if we need to, in order to get at pid
2767 * namespacing. If we follow this path, the child's address space might
2768 * have broken locks; you may only call functions that do not acquire
2769 * any locks.
2770 *
2771 * Unfortunately, fork() acquires every lock it can get its hands on, as
2772 * previously detailed, so this function is highly likely to deadlock
2773 * later on (see "deadlock here") if we're multithreaded.
2774 *
2775 * We might hack around this by having the clone()d child (init of the
2776 * pid namespace) return directly, rather than leaving the clone()d
2777 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002778 * its fork()ed child return in turn), but that process would be
2779 * crippled with its libc locks potentially broken. We might try
2780 * fork()ing in the parent before we clone() to ensure that we own all
2781 * the locks, but then we have to have the forked child hanging around
2782 * consuming resources (and possibly having file descriptors / shared
2783 * memory regions / etc attached). We'd need to keep the child around to
2784 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002785 *
2786 * TODO(ellyjones): figure out if the "forked child hanging around"
2787 * problem is fixable or not. It would be nice if we worked in this
2788 * case.
2789 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002790 if (pid_namespace) {
2791 int clone_flags = CLONE_NEWPID | SIGCHLD;
2792 if (j->flags.userns)
2793 clone_flags |= CLONE_NEWUSER;
2794 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002795 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002796 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002797 }
Elly Jones761b7412012-06-13 15:49:52 -04002798
Elly Jonese1749eb2011-10-07 13:54:59 -04002799 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002800 if (use_preload) {
2801 free(oldenv_copy);
2802 }
Kevin Hamacher64cf3cb2019-06-05 17:28:46 +02002803 pdie("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002804 }
Will Drewryf89aef52011-09-16 16:48:57 -05002805
Elly Jonese1749eb2011-10-07 13:54:59 -04002806 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002807 if (use_preload) {
2808 /* Restore parent's LD_PRELOAD. */
2809 if (oldenv_copy) {
2810 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2811 free(oldenv_copy);
2812 } else {
2813 unsetenv(kLdPreloadEnvVar);
2814 }
2815 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002816 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002817
Elly Jonese1749eb2011-10-07 13:54:59 -04002818 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002819
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002820 if (j->flags.forward_signals) {
2821 forward_pid = child_pid;
2822 install_signal_handlers();
2823 }
2824
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002825 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002826 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002827
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002828 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002829 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002830
Dylan Reid0f72ef42017-06-06 15:42:49 -07002831 if (j->rlimit_count)
2832 set_rlimits_or_die(j);
2833
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002834 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002835 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002836
Mike Frysinger902a4492018-12-27 05:22:56 -05002837 if (j->flags.enter_vfs)
2838 close(j->mountns_fd);
2839
2840 if (j->flags.enter_net)
2841 close(j->netns_fd);
2842
Dylan Reidce5b55e2016-01-13 11:04:16 -08002843 if (sync_child)
2844 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002845
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002846 if (use_preload) {
2847 /* Send marshalled minijail. */
2848 close(pipe_fds[0]); /* read endpoint */
2849 ret = minijail_to_fd(j, pipe_fds[1]);
2850 close(pipe_fds[1]); /* write endpoint */
2851 if (ret) {
2852 kill(j->initpid, SIGKILL);
2853 die("failed to send marshalled minijail");
2854 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002855 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002856
Dylan Reidacfb8be2017-08-25 12:56:51 -07002857 if (status_out->pchild_pid)
2858 *status_out->pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002859
2860 /*
2861 * If we want to write to the child process' standard input,
2862 * set up the write end of the pipe.
2863 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002864 if (status_out->pstdin_fd)
2865 *status_out->pstdin_fd =
2866 setup_pipe_end(stdin_fds, 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002867
2868 /*
2869 * If we want to read from the child process' standard output,
2870 * set up the read end of the pipe.
2871 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002872 if (status_out->pstdout_fd)
2873 *status_out->pstdout_fd =
2874 setup_pipe_end(stdout_fds, 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002875
2876 /*
2877 * If we want to read from the child process' standard error,
2878 * set up the read end of the pipe.
2879 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002880 if (status_out->pstderr_fd)
2881 *status_out->pstderr_fd =
2882 setup_pipe_end(stderr_fds, 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002883
Dylan Reid0412dcc2017-08-24 11:33:15 -07002884 /*
2885 * If forking return the child pid, in the normal exec case
2886 * return 0 for success.
2887 */
2888 if (!config->exec_in_child)
2889 return child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04002890 return 0;
2891 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002892 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002893 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002894
Peter Qiu2860c462015-12-16 15:13:06 -08002895 if (j->flags.reset_signal_mask) {
2896 sigset_t signal_mask;
2897 if (sigemptyset(&signal_mask) != 0)
2898 pdie("sigemptyset failed");
2899 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2900 pdie("sigprocmask failed");
2901 }
2902
Luis Hector Chaveza27118a2018-04-04 08:18:01 -07002903 if (j->flags.reset_signal_handlers) {
2904 int signum;
2905 for (signum = 0; signum <= SIGRTMAX; signum++) {
2906 /*
2907 * Ignore EINVAL since some signal numbers in the range
2908 * might not be valid.
2909 */
2910 if (signal(signum, SIG_DFL) == SIG_ERR &&
2911 errno != EINVAL) {
2912 pdie("failed to reset signal %d disposition",
2913 signum);
2914 }
2915 }
2916 }
2917
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002918 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002919 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002920 int inheritable_fds[kMaxInheritableFdsSize];
2921 size_t size = 0;
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002922 size_t i;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002923 if (use_preload) {
2924 inheritable_fds[size++] = pipe_fds[0];
2925 inheritable_fds[size++] = pipe_fds[1];
2926 }
2927 if (sync_child) {
2928 inheritable_fds[size++] = child_sync_pipe_fds[0];
2929 inheritable_fds[size++] = child_sync_pipe_fds[1];
2930 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002931 if (status_out->pstdin_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002932 inheritable_fds[size++] = stdin_fds[0];
2933 inheritable_fds[size++] = stdin_fds[1];
2934 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002935 if (status_out->pstdout_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002936 inheritable_fds[size++] = stdout_fds[0];
2937 inheritable_fds[size++] = stdout_fds[1];
2938 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002939 if (status_out->pstderr_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002940 inheritable_fds[size++] = stderr_fds[0];
2941 inheritable_fds[size++] = stderr_fds[1];
2942 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002943 for (i = 0; i < j->preserved_fd_count; i++) {
2944 /*
2945 * Preserve all parent_fds. They will be dup2(2)-ed in
2946 * the child later.
2947 */
2948 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
2949 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002950
2951 if (close_open_fds(inheritable_fds, size) < 0)
2952 die("failed to close open file descriptors");
2953 }
2954
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002955 if (redirect_fds(j))
2956 die("failed to set up fd redirections");
2957
Dylan Reidce5b55e2016-01-13 11:04:16 -08002958 if (sync_child)
2959 wait_for_parent_setup(child_sync_pipe_fds);
2960
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002961 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002962 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002963
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002964 /*
2965 * If we want to write to the jailed process' standard input,
2966 * set up the read end of the pipe.
2967 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002968 if (status_out->pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002969 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2970 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002971 die("failed to set up stdin pipe");
2972 }
2973
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002974 /*
2975 * If we want to read from the jailed process' standard output,
2976 * set up the write end of the pipe.
2977 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002978 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002979 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2980 STDOUT_FILENO) < 0)
2981 die("failed to set up stdout pipe");
2982 }
2983
2984 /*
2985 * If we want to read from the jailed process' standard error,
2986 * set up the write end of the pipe.
2987 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002988 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002989 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2990 STDERR_FILENO) < 0)
2991 die("failed to set up stderr pipe");
2992 }
2993
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002994 /*
Xiyuan Xia9b41e652019-05-23 11:03:04 -07002995 * If any of stdin, stdout, or stderr are TTYs, or setsid flag is
2996 * set, create a new session. This prevents the jailed process from
2997 * using the TIOCSTI ioctl to push characters into the parent process
2998 * terminal's input buffer, therefore escaping the jail.
Stephen Barber5dd5b1b2017-10-16 23:02:39 -07002999 *
3000 * Since it has just forked, the child will not be a process group
3001 * leader, and this call to setsid() should always succeed.
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05003002 */
Xiyuan Xia9b41e652019-05-23 11:03:04 -07003003 if (j->flags.setsid || isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05003004 isatty(STDERR_FILENO)) {
3005 if (setsid() < 0) {
3006 pdie("setsid() failed");
3007 }
3008 }
3009
Dylan Reid791f5772015-09-14 20:02:42 -07003010 /* If running an init program, let it decide when/how to mount /proc. */
3011 if (pid_namespace && !do_init)
3012 j->flags.remount_proc_ro = 0;
3013
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003014 if (use_preload) {
3015 /* Strip out flags that cannot be inherited across execve(2). */
3016 minijail_preexec(j);
3017 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003018 /*
3019 * If not using LD_PRELOAD, do all jailing before execve(2).
3020 * Note that PID namespaces can only be entered on fork(2),
3021 * so that flag is still cleared.
3022 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003023 j->flags.pids = 0;
3024 }
Dylan Reid0412dcc2017-08-24 11:33:15 -07003025
3026 /*
3027 * Jail this process.
3028 * If forking, return.
3029 * If not, execve(2) the target.
3030 */
Elly Jonese1749eb2011-10-07 13:54:59 -04003031 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003032
Dylan Reid0412dcc2017-08-24 11:33:15 -07003033 if (config->exec_in_child && pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05003034 /*
3035 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003036 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003037 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003038 * a child to actually run the program. If |do_init == 0|, we
3039 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04003040 *
3041 * If we're multithreaded, we'll probably deadlock here. See
3042 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04003043 */
3044 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003045 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04003046 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003047 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04003048 /*
3049 * Best effort. Don't bother checking the return value.
3050 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003051 prctl(PR_SET_NAME, "minijail-init");
3052 init(child_pid); /* Never returns. */
3053 }
Elly Jonese1749eb2011-10-07 13:54:59 -04003054 }
Elly Jonescd7a9042011-07-22 13:56:51 -04003055
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07003056 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
3057
Dylan Reid0412dcc2017-08-24 11:33:15 -07003058 if (!config->exec_in_child)
3059 return 0;
3060
Elly Jonesdd3e8512012-01-23 15:13:38 -05003061 /*
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05003062 * If not using LD_PRELOAD, support passing a new environment instead of
3063 * inheriting the parent's.
3064 * When not using LD_PRELOAD there is no need to modify the environment
3065 * to add Minijail-related variables, so passing a new environment is
3066 * fine.
3067 */
3068 char *const *child_env = environ;
3069 if (!use_preload && config->envp != NULL) {
3070 child_env = config->envp;
3071 }
3072
3073 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003074 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04003075 * calling process
3076 * -> execve()-ing process
3077 * If we are:
3078 * calling process
3079 * -> init()-ing process
3080 * -> execve()-ing process
3081 */
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05003082 ret = execve(config->filename, config->argv, child_env);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003083 if (ret == -1) {
Dylan Reidacfb8be2017-08-25 12:56:51 -07003084 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003085 }
3086 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04003087}
3088
Will Drewry6ac91122011-10-21 16:38:58 -05003089int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003090{
3091 int st;
3092 if (kill(j->initpid, SIGTERM))
3093 return -errno;
3094 if (waitpid(j->initpid, &st, 0) < 0)
3095 return -errno;
3096 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04003097}
3098
Will Drewry6ac91122011-10-21 16:38:58 -05003099int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003100{
3101 int st;
3102 if (waitpid(j->initpid, &st, 0) < 0)
3103 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003104
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003105 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003106 int error_status = st;
3107 if (WIFSIGNALED(st)) {
3108 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07003109 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003110 j->initpid, signum);
3111 /*
3112 * We return MINIJAIL_ERR_JAIL if the process received
3113 * SIGSYS, which happens when a syscall is blocked by
3114 * seccomp filters.
3115 * If not, we do what bash(1) does:
3116 * $? = 128 + signum
3117 */
3118 if (signum == SIGSYS) {
3119 error_status = MINIJAIL_ERR_JAIL;
3120 } else {
3121 error_status = 128 + signum;
3122 }
3123 }
3124 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003125 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003126
3127 int exit_status = WEXITSTATUS(st);
3128 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07003129 info("child process %d exited with status %d",
3130 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003131
3132 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04003133}
3134
Will Drewry6ac91122011-10-21 16:38:58 -05003135void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003136{
Dylan Reid605ce7f2016-01-19 19:21:00 -08003137 size_t i;
3138
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07003139 if (j->filter_prog) {
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08003140 free(j->filter_prog->filter);
3141 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04003142 }
Mike Frysingerac08a682017-10-10 02:04:50 -04003143 free_mounts_list(j);
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07003144 while (j->hooks_head) {
3145 struct hook *c = j->hooks_head;
3146 j->hooks_head = c->next;
3147 free(c);
3148 }
3149 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04003150 if (j->user)
3151 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08003152 if (j->suppl_gid_list)
3153 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05003154 if (j->chrootdir)
3155 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04003156 if (j->pid_file_path)
3157 free(j->pid_file_path);
3158 if (j->uidmap)
3159 free(j->uidmap);
3160 if (j->gidmap)
3161 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04003162 if (j->hostname)
3163 free(j->hostname);
Luis Hector Chavez9acba452018-10-11 10:13:25 -07003164 if (j->preload_path)
3165 free(j->preload_path);
Andrew Brestickereac28942015-11-11 16:04:46 -08003166 if (j->alt_syscall_table)
3167 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08003168 for (i = 0; i < j->cgroup_count; ++i)
3169 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04003170 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003171}
Luis Hector Chavez114a9302017-09-05 20:36:58 -07003172
3173void API minijail_log_to_fd(int fd, int min_priority)
3174{
3175 init_logging(LOG_TO_FD, fd, min_priority);
3176}