blob: 0b967047c2c45307100adeed107b770c3a908b36 [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
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -04001543 /* We assume |dest| has a leading "/". */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001544 if (dev_path && strncmp("/dev/", m->dest, 5) == 0) {
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -04001545 /*
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -04001546 * Since the temp path is rooted at /dev, skip that dest part.
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -04001547 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001548 if (asprintf(&dest, "%s%s", dev_path, m->dest + 4) < 0)
1549 return -ENOMEM;
1550 } else {
Mike Frysingerac08a682017-10-10 02:04:50 -04001551 if (asprintf(&dest, "%s%s", j->chrootdir ?: "", m->dest) < 0)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001552 return -ENOMEM;
1553 }
Dylan Reid648b2202015-10-23 00:50:00 -07001554
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001555 ret =
1556 setup_mount_destination(m->src, dest, j->uid, j->gid,
1557 (m->flags & MS_BIND), &original_mnt_flags);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001558 if (ret) {
yusukes1b32f852018-03-05 10:24:58 -08001559 warn("creating mount target '%s' failed", dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001560 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001561 }
Dylan Reideec77962016-06-30 19:35:10 -07001562
Dylan Reid648b2202015-10-23 00:50:00 -07001563 /*
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001564 * Bind mounts that change the 'ro' flag have to be remounted since
1565 * 'bind' and other flags can't both be specified in the same command.
1566 * Remount after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001567 */
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001568 if ((m->flags & MS_BIND) &&
1569 ((m->flags & MS_RDONLY) != (original_mnt_flags & MS_RDONLY))) {
1570 remount = 1;
1571 /*
1572 * Restrict the mount flags to those that are user-settable in a
1573 * MS_REMOUNT request, but excluding MS_RDONLY. The
1574 * user-requested mount flags will dictate whether the remount
1575 * will have that flag or not.
1576 */
1577 original_mnt_flags &= (MS_USER_SETTABLE_MASK & ~MS_RDONLY);
Elly Jonesa1059632011-12-15 15:17:07 -05001578 }
Dylan Reid648b2202015-10-23 00:50:00 -07001579
Dylan Reid81e23972016-05-18 14:06:35 -07001580 ret = mount(m->src, dest, m->type, m->flags, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001581 if (ret) {
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001582 pwarn("bind: %s -> %s flags=%#lx", m->src, dest, m->flags);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001583 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001584 }
Dylan Reid648b2202015-10-23 00:50:00 -07001585
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001586 if (remount) {
1587 ret =
1588 mount(m->src, dest, NULL,
1589 m->flags | original_mnt_flags | MS_REMOUNT, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001590 if (ret) {
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001591 pwarn("bind remount: %s -> %s flags=%#lx", m->src, dest,
1592 m->flags | original_mnt_flags | MS_REMOUNT);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001593 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001594 }
Dylan Reid648b2202015-10-23 00:50:00 -07001595 }
1596
Elly Jones51a5b6c2011-10-12 19:09:26 -04001597 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001598 if (m->next)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001599 return mount_one(j, m->next, dev_path);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001600 return 0;
1601
1602error:
1603 free(dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001604 return ret;
1605}
1606
Mike Frysingerac08a682017-10-10 02:04:50 -04001607static void process_mounts_or_die(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001608{
Mike Frysingerac08a682017-10-10 02:04:50 -04001609 /*
1610 * We have to mount /dev first in case there are bind mounts from
1611 * the original /dev into the new unique tmpfs one.
1612 */
1613 char *dev_path = NULL;
1614 if (j->flags.mount_dev && mount_dev(&dev_path))
1615 pdie("mount_dev failed");
Dylan Reid648b2202015-10-23 00:50:00 -07001616
Mike Frysingerac08a682017-10-10 02:04:50 -04001617 if (j->mounts_head && mount_one(j, j->mounts_head, dev_path)) {
1618 if (dev_path) {
1619 int saved_errno = errno;
1620 mount_dev_cleanup(dev_path);
1621 errno = saved_errno;
1622 }
1623 pdie("mount_one failed");
1624 }
Mike Frysinger33ffef32017-01-13 19:53:19 -05001625
1626 /*
Mike Frysingerac08a682017-10-10 02:04:50 -04001627 * Once all bind mounts have been processed, move the temp dev to
1628 * its final /dev home.
Mike Frysinger33ffef32017-01-13 19:53:19 -05001629 */
1630 if (j->flags.mount_dev && mount_dev_finalize(j, dev_path))
Mike Frysingerac08a682017-10-10 02:04:50 -04001631 pdie("mount_dev_finalize failed");
1632}
Elly Jones51a5b6c2011-10-12 19:09:26 -04001633
Mike Frysingerac08a682017-10-10 02:04:50 -04001634static int enter_chroot(const struct minijail *j)
1635{
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001636 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1637
Elly Jones51a5b6c2011-10-12 19:09:26 -04001638 if (chroot(j->chrootdir))
1639 return -errno;
1640
1641 if (chdir("/"))
1642 return -errno;
1643
1644 return 0;
1645}
1646
Mike Frysingerac08a682017-10-10 02:04:50 -04001647static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001648{
Mike Frysingerac08a682017-10-10 02:04:50 -04001649 int oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001650
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001651 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1652
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001653 /*
1654 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001655 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001656 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001657 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001658 if (oldroot < 0)
1659 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001660 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001661 if (newroot < 0)
1662 pdie("failed to open %s for fchdir", j->chrootdir);
1663
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001664 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001665 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001666 * do a self bind mount.
1667 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001668 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1669 pdie("failed to bind mount '%s'", j->chrootdir);
1670 if (chdir(j->chrootdir))
1671 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001672 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001673 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001674
1675 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001676 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001677 * change to the old root and unmount it.
1678 */
1679 if (fchdir(oldroot))
1680 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001681
1682 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05001683 * If skip_remount_private was enabled for minijail_enter(),
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001684 * there could be a shared mount point under |oldroot|. In that case,
1685 * mounts under this shared mount point will be unmounted below, and
1686 * this unmounting will propagate to the original mount namespace
1687 * (because the mount point is shared). To prevent this unexpected
1688 * unmounting, remove these mounts from their peer groups by recursively
1689 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001690 */
1691 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001692 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001693 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001694 if (umount2(".", MNT_DETACH))
1695 pdie("umount(/)");
1696 /* Change back to the new root. */
1697 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001698 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001699 if (close(oldroot))
1700 return -errno;
1701 if (close(newroot))
1702 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001703 if (chroot("/"))
1704 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001705 /* Set correct CWD for getcwd(3). */
1706 if (chdir("/"))
1707 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001708
1709 return 0;
1710}
1711
Martin Pelikánab9eb442017-01-25 11:53:58 +11001712static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001713{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001714 const char fmt[] = "size=%zu,mode=1777";
1715 /* Count for the user storing ULLONG_MAX literally + extra space. */
1716 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1717 int ret;
1718
1719 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1720
1721 if (ret <= 0)
1722 pdie("tmpfs size spec error");
1723 else if ((size_t)ret >= sizeof(data))
1724 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001725 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001726 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001727}
1728
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001729static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001730{
1731 const char *kProcPath = "/proc";
1732 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001733 /*
1734 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001735 * /proc in our namespace, which means using MS_REMOUNT here would
1736 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001737 * namespace (!). Instead, remove their mount from our namespace lazily
1738 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001739 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001740 if (umount2(kProcPath, MNT_DETACH)) {
1741 /*
1742 * If we are in a new user namespace, umount(2) will fail.
1743 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1744 */
1745 if (j->flags.userns) {
1746 info("umount(/proc, MNT_DETACH) failed, "
1747 "this is expected when using user namespaces");
1748 } else {
1749 return -errno;
1750 }
1751 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001752 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001753 return -errno;
1754 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001755}
1756
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001757static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001758{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001759 kill(j->initpid, SIGKILL);
1760 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001761}
1762
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001763static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001764{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001765 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001766 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001767}
1768
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001769static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001770{
1771 size_t i;
1772
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001773 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001774 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001775 kill_child_and_die(j, "failed to add to cgroups");
1776 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001777}
1778
Dylan Reid0f72ef42017-06-06 15:42:49 -07001779static void set_rlimits_or_die(const struct minijail *j)
1780{
1781 size_t i;
1782
1783 for (i = 0; i < j->rlimit_count; ++i) {
1784 struct rlimit limit;
1785 limit.rlim_cur = j->rlimits[i].cur;
1786 limit.rlim_max = j->rlimits[i].max;
1787 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1788 kill_child_and_die(j, "failed to set rlimit");
1789 }
1790}
1791
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001792static void write_ugid_maps_or_die(const struct minijail *j)
1793{
1794 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1795 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001796 if (j->gidmap && j->flags.disable_setgroups) {
1797 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1798 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001799 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001800 if (ret == -ENOENT) {
1801 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1802 warn("could not disable setgroups(2)");
1803 } else
1804 kill_child_and_die(j, "failed to disable setgroups(2)");
1805 }
1806 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001807 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1808 kill_child_and_die(j, "failed to write gid_map");
1809}
1810
1811static void enter_user_namespace(const struct minijail *j)
1812{
Luis Hector Chavez71323552017-09-05 09:17:22 -07001813 int uid = j->flags.uid ? j->uid : 0;
1814 int gid = j->flags.gid ? j->gid : 0;
1815 if (j->gidmap && setresgid(gid, gid, gid)) {
1816 pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid,
1817 gid);
1818 }
1819 if (j->uidmap && setresuid(uid, uid, uid)) {
1820 pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid,
1821 uid);
1822 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001823}
1824
1825static void parent_setup_complete(int *pipe_fds)
1826{
1827 close(pipe_fds[0]);
1828 close(pipe_fds[1]);
1829}
1830
1831/*
1832 * wait_for_parent_setup: Called by the child process to wait for any
1833 * further parent-side setup to complete before continuing.
1834 */
1835static void wait_for_parent_setup(int *pipe_fds)
1836{
1837 char buf;
1838
1839 close(pipe_fds[1]);
1840
1841 /* Wait for parent to complete setup and close the pipe. */
1842 if (read(pipe_fds[0], &buf, 1) != 0)
1843 die("failed to sync with parent");
1844 close(pipe_fds[0]);
1845}
1846
1847static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001848{
Lutz Justen13807cb2017-01-03 17:11:55 +01001849 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1850 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001851 die("can only do one of inherit, keep, or set supplementary "
1852 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001853 }
1854
Lutz Justen13807cb2017-01-03 17:11:55 +01001855 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001856 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001857 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001858 } else if (j->flags.set_suppl_gids) {
1859 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001860 pdie("setgroups(suppl_gids) failed");
Luis Hector Chavez71323552017-09-05 09:17:22 -07001861 } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001862 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001863 * Only attempt to clear supplementary groups if we are changing
Luis Hector Chavez71323552017-09-05 09:17:22 -07001864 * users or groups, and if the caller did not request to disable
1865 * setgroups (used when entering a user namespace as a
1866 * non-privileged user).
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001867 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001868 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001869 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001870 }
1871
1872 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001873 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001874
1875 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001876 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001877}
1878
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001879static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1880{
1881 const uint64_t one = 1;
1882 unsigned int i;
1883 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1884 if (keep_mask & (one << i))
1885 continue;
1886 if (prctl(PR_CAPBSET_DROP, i))
1887 pdie("could not drop capability from bounding set");
1888 }
1889}
1890
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001891static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001892{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001893 if (!j->flags.use_caps)
1894 return;
1895
Elly Jonese1749eb2011-10-07 13:54:59 -04001896 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001897 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001898 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001899 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001900 unsigned int i;
1901 if (!caps)
1902 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001903 if (cap_clear(caps))
1904 die("can't clear caps");
1905
1906 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001907 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001908 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001909 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001910 flag[0] = i;
1911 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001912 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001913 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001914 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001915 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001916 die("can't add inheritable cap");
1917 }
1918 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001919 die("can't apply initial cleaned capset");
1920
1921 /*
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001922 * Instead of dropping the bounding set first, do it here in case
Kees Cook323878a2013-02-05 15:35:24 -08001923 * the caller had a more permissive bounding set which could
1924 * have been used above to raise a capability that wasn't already
1925 * present. This requires CAP_SETPCAP, so we raised/kept it above.
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001926 *
1927 * However, if we're asked to skip setting *and* locking the
1928 * SECURE_NOROOT securebit, also skip dropping the bounding set.
1929 * If the caller wants to regain all capabilities when executing a
1930 * set-user-ID-root program, allow them to do so. The default behavior
1931 * (i.e. the behavior without |securebits_skip_mask| set) will still put
1932 * the jailed process tree in a capabilities-only environment.
1933 *
1934 * We check the negated skip mask for SECURE_NOROOT and
1935 * SECURE_NOROOT_LOCKED. If the bits are set in the negated mask they
1936 * will *not* be skipped in lock_securebits(), and therefore we should
1937 * drop the bounding set.
Kees Cook323878a2013-02-05 15:35:24 -08001938 */
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001939 if (secure_noroot_set_and_locked(~j->securebits_skip_mask)) {
1940 drop_capbset(j->caps, last_valid_cap);
1941 } else {
1942 warn("SECURE_NOROOT not set, not dropping bounding set");
1943 }
Kees Cook323878a2013-02-05 15:35:24 -08001944
1945 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001946 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001947 flag[0] = CAP_SETPCAP;
1948 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1949 die("can't clear effective cap");
1950 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1951 die("can't clear permitted cap");
1952 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1953 die("can't clear inheritable cap");
1954 }
1955
1956 if (cap_set_proc(caps))
1957 die("can't apply final cleaned capset");
1958
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001959 /*
1960 * If ambient capabilities are supported, clear all capabilities first,
1961 * then raise the requested ones.
1962 */
1963 if (j->flags.set_ambient_caps) {
1964 if (!cap_ambient_supported()) {
1965 pdie("ambient capabilities not supported");
1966 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001967 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1968 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001969 pdie("can't clear ambient capabilities");
1970 }
1971
1972 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1973 if (!(j->caps & (one << i)))
1974 continue;
1975
1976 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1977 0) != 0) {
1978 pdie("prctl(PR_CAP_AMBIENT, "
1979 "PR_CAP_AMBIENT_RAISE, %u) failed",
1980 i);
1981 }
1982 }
1983 }
1984
Kees Cook323878a2013-02-05 15:35:24 -08001985 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001986}
1987
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001988static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001989{
1990 /*
1991 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1992 * in the kernel source tree for an explanation of the parameters.
1993 */
1994 if (j->flags.no_new_privs) {
1995 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1996 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1997 }
1998
1999 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07002000 * Code running with ASan
2001 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
2002 * will make system calls not included in the syscall filter policy,
2003 * which will likely crash the program. Skip setting seccomp filter in
2004 * that case.
2005 * 'running_with_asan()' has no inputs and is completely defined at
2006 * build time, so this cannot be used by an attacker to skip setting
2007 * seccomp filter.
2008 */
Evgenii Stepanov3d98f3c2018-08-23 15:06:50 -07002009 if (j->flags.seccomp_filter && running_with_asan()) {
Evgenii Stepanov825828c2018-07-27 11:57:07 -07002010 warn("running with (HW)ASan, not setting seccomp filter");
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07002011 return;
2012 }
2013
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002014 if (j->flags.seccomp_filter) {
2015 if (j->flags.seccomp_filter_logging) {
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002016 warn("logging seccomp filter failures");
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04002017 if (!seccomp_ret_log_available()) {
2018 /*
2019 * If SECCOMP_RET_LOG is not available,
2020 * install the SIGSYS handler first.
2021 */
2022 if (install_sigsys_handler())
2023 pdie(
2024 "failed to install SIGSYS handler");
2025 }
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002026 } else if (j->flags.seccomp_filter_tsync) {
2027 /*
2028 * If setting thread sync,
2029 * reset the SIGSYS signal handler so that
2030 * the entire thread group is killed.
2031 */
2032 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
2033 pdie("failed to reset SIGSYS disposition");
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002034 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002035 }
2036
2037 /*
2038 * Install the syscall filter.
2039 */
2040 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002041 if (j->flags.seccomp_filter_tsync) {
2042 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
2043 SECCOMP_FILTER_FLAG_TSYNC,
2044 j->filter_prog)) {
2045 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002046 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002047 } else {
2048 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
2049 j->filter_prog)) {
2050 pdie("prctl(seccomp_filter) failed");
2051 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002052 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002053 }
2054}
2055
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002056static pid_t forward_pid = -1;
2057
Mike Frysinger33d051a2018-05-30 16:41:10 -04002058static void forward_signal(int sig,
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04002059 siginfo_t *siginfo attribute_unused,
2060 void *void_context attribute_unused)
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002061{
2062 if (forward_pid != -1) {
Mike Frysinger33d051a2018-05-30 16:41:10 -04002063 kill(forward_pid, sig);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002064 }
2065}
2066
2067static void install_signal_handlers(void)
2068{
2069 struct sigaction act;
2070
2071 memset(&act, 0, sizeof(act));
2072 act.sa_sigaction = &forward_signal;
2073 act.sa_flags = SA_SIGINFO | SA_RESTART;
2074
2075 /* Handle all signals, except SIGCHLD. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002076 for (int sig = 1; sig < NSIG; sig++) {
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002077 /*
2078 * We don't care if we get EINVAL: that just means that we
2079 * can't handle this signal, so let's skip it and continue.
2080 */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002081 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002082 }
2083 /* Reset SIGCHLD's handler. */
2084 signal(SIGCHLD, SIG_DFL);
2085
2086 /* Handle real-time signals. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002087 for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++) {
2088 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002089 }
2090}
2091
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002092static const char *lookup_hook_name(minijail_hook_event_t event)
2093{
2094 switch (event) {
2095 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
2096 return "pre-drop-caps";
2097 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
2098 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07002099 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
2100 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002101 case MINIJAIL_HOOK_EVENT_MAX:
2102 /*
2103 * Adding this in favor of a default case to force the
2104 * compiler to error out if a new enum value is added.
2105 */
2106 break;
2107 }
2108 return "unknown";
2109}
2110
2111static void run_hooks_or_die(const struct minijail *j,
2112 minijail_hook_event_t event)
2113{
2114 int rc;
2115 int hook_index = 0;
2116 for (struct hook *c = j->hooks_head; c; c = c->next) {
2117 if (c->event != event)
2118 continue;
2119 rc = c->hook(c->payload);
2120 if (rc != 0) {
2121 errno = -rc;
2122 pdie("%s hook (index %d) failed",
2123 lookup_hook_name(event), hook_index);
2124 }
2125 /* Only increase the index within the same hook event type. */
2126 ++hook_index;
2127 }
2128}
2129
Will Drewry6ac91122011-10-21 16:38:58 -05002130void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002131{
Dylan Reidf682d472015-09-17 21:39:07 -07002132 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002133 * If we're dropping caps, get the last valid cap from /proc now,
2134 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07002135 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002136 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002137 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002138 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07002139
Elly Jonese1749eb2011-10-07 13:54:59 -04002140 if (j->flags.pids)
2141 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002142 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04002143
Lutz Justen13807cb2017-01-03 17:11:55 +01002144 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05002145 die("cannot inherit supplementary groups without setting a "
2146 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04002147
Elly Jonesdd3e8512012-01-23 15:13:38 -05002148 /*
2149 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04002150 * so we don't even try. If any of our operations fail, we abort() the
2151 * entire process.
2152 */
Mike Frysinger902a4492018-12-27 05:22:56 -05002153 if (j->flags.enter_vfs) {
2154 if (setns(j->mountns_fd, CLONE_NEWNS))
2155 pdie("setns(CLONE_NEWNS) failed");
2156 close(j->mountns_fd);
2157 }
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002158
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07002159 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002160 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002161 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002162 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05002163 * By default, remount all filesystems as private, unless
2164 * - Passed a specific remount mode, in which case remount with that,
2165 * - Asked not to remount at all, in which case skip the mount(2) call.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002166 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
2167 */
Mike Frysinger785b1c32018-02-23 15:47:24 -05002168 if (j->remount_mode) {
2169 if (mount(NULL, "/", NULL, MS_REC | j->remount_mode, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002170 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
2171 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002172 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002173 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04002174
Dylan Reidf7942472015-11-18 17:55:26 -08002175 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002176 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08002177 }
2178
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002179 if (j->flags.uts) {
2180 if (unshare(CLONE_NEWUTS))
2181 pdie("unshare(CLONE_NEWUTS) failed");
2182
2183 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
2184 pdie("sethostname(%s) failed", j->hostname);
2185 }
2186
Dylan Reid1102f5a2015-09-15 11:52:20 -07002187 if (j->flags.enter_net) {
2188 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002189 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger902a4492018-12-27 05:22:56 -05002190 close(j->netns_fd);
Mike Frysinger7559dfe2016-11-15 18:58:39 -05002191 } else if (j->flags.net) {
2192 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002193 pdie("unshare(CLONE_NEWNET) failed");
2194 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07002195 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002196
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002197 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002198 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002199
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08002200 if (j->flags.new_session_keyring) {
2201 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
2202 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
2203 }
2204
Mike Frysingerac08a682017-10-10 02:04:50 -04002205 /* We have to process all the mounts before we chroot/pivot_root. */
2206 process_mounts_or_die(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002207
Mike Frysingerac08a682017-10-10 02:04:50 -04002208 if (j->flags.chroot && enter_chroot(j))
Mike Frysinger33ffef32017-01-13 19:53:19 -05002209 pdie("chroot");
Mike Frysinger33ffef32017-01-13 19:53:19 -05002210
Mike Frysingerac08a682017-10-10 02:04:50 -04002211 if (j->flags.pivot_root && enter_pivot_root(j))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08002212 pdie("pivot_root");
2213
Martin Pelikánab9eb442017-01-25 11:53:58 +11002214 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07002215 pdie("mount_tmp");
2216
Dylan Reid791f5772015-09-14 20:02:42 -07002217 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04002218 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04002219
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002220 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
2221
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002222 /*
2223 * If we're only dropping capabilities from the bounding set, but not
2224 * from the thread's (permitted|inheritable|effective) sets, do it now.
2225 */
2226 if (j->flags.capbset_drop) {
2227 drop_capbset(j->cap_bset, last_valid_cap);
2228 }
2229
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002230 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002231 * POSIX capabilities are a bit tricky. We must set SECBIT_KEEP_CAPS
2232 * before drop_ugid() below as the latter would otherwise drop all
2233 * capabilities.
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002234 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002235 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002236 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002237 * When using ambient capabilities, CAP_SET{GID,UID} can be
2238 * inherited across execve(2), so SECBIT_KEEP_CAPS is not
2239 * strictly needed.
Elly Jonese1749eb2011-10-07 13:54:59 -04002240 */
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002241 bool require_keep_caps = !j->flags.set_ambient_caps;
2242 if (lock_securebits(j->securebits_skip_mask,
2243 require_keep_caps) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002244 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002245 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002246 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002247
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002248 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002249 /*
2250 * If we're setting no_new_privs, we can drop privileges
2251 * before setting seccomp filter. This way filter policies
2252 * don't need to allow privilege-dropping syscalls.
2253 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002254 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002255 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002256 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002257 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002258 /*
2259 * If we're not setting no_new_privs,
2260 * we need to set seccomp filter *before* dropping privileges.
2261 * WARNING: this means that filter policies *must* allow
2262 * setgroups()/setresgid()/setresuid() for dropping root and
2263 * capget()/capset()/prctl() for dropping caps.
2264 */
2265 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002266 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002267 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04002268 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002269
Elly Jonesdd3e8512012-01-23 15:13:38 -05002270 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08002271 * Select the specified alternate syscall table. The table must not
2272 * block prctl(2) if we're using seccomp as well.
2273 */
2274 if (j->flags.alt_syscall) {
2275 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002276 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08002277 }
2278
2279 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05002280 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04002281 * privilege-dropping syscalls :)
2282 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002283 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04002284 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002285 warn("seccomp not supported");
2286 return;
2287 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002288 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002289 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002290}
2291
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002292/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04002293static int init_exitstatus = 0;
2294
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04002295void init_term(int sig attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002296{
2297 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04002298}
2299
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002300void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04002301{
2302 pid_t pid;
2303 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002304 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002305 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002306 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002307 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002308 /*
2309 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04002310 * left inside our pid namespace or we get a signal.
2311 */
2312 if (pid == rootpid)
2313 init_exitstatus = status;
2314 }
2315 if (!WIFEXITED(init_exitstatus))
2316 _exit(MINIJAIL_ERR_INIT);
2317 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04002318}
2319
Will Drewry6ac91122011-10-21 16:38:58 -05002320int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002321{
2322 size_t sz = 0;
2323 size_t bytes = read(fd, &sz, sizeof(sz));
2324 char *buf;
2325 int r;
2326 if (sizeof(sz) != bytes)
2327 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002328 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04002329 return -E2BIG;
2330 buf = malloc(sz);
2331 if (!buf)
2332 return -ENOMEM;
2333 bytes = read(fd, buf, sz);
2334 if (bytes != sz) {
2335 free(buf);
2336 return -EINVAL;
2337 }
2338 r = minijail_unmarshal(j, buf, sz);
2339 free(buf);
2340 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002341}
2342
Will Drewry6ac91122011-10-21 16:38:58 -05002343int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04002344{
2345 char *buf;
2346 size_t sz = minijail_size(j);
2347 ssize_t written;
2348 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04002349
Elly Jonese1749eb2011-10-07 13:54:59 -04002350 if (!sz)
2351 return -EINVAL;
2352 buf = malloc(sz);
2353 r = minijail_marshal(j, buf, sz);
2354 if (r) {
2355 free(buf);
2356 return r;
2357 }
2358 /* Sends [size][minijail]. */
2359 written = write(fd, &sz, sizeof(sz));
2360 if (written != sizeof(sz)) {
2361 free(buf);
2362 return -EFAULT;
2363 }
2364 written = write(fd, buf, sz);
2365 if (written < 0 || (size_t) written != sz) {
2366 free(buf);
2367 return -EFAULT;
2368 }
2369 free(buf);
2370 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002371}
Elly Jonescd7a9042011-07-22 13:56:51 -04002372
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002373static int setup_preload(const struct minijail *j attribute_unused,
2374 const char *oldenv attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002375{
Daniel Erat5b7a3182015-08-19 16:06:22 -06002376#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002377 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002378 return 0;
2379#else
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002380 const char *preload_path = j->preload_path ?: PRELOADPATH;
2381 char *newenv = NULL;
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002382 int ret = 0;
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002383
2384 if (!oldenv)
2385 oldenv = "";
Elly Jonescd7a9042011-07-22 13:56:51 -04002386
Elly Jonese1749eb2011-10-07 13:54:59 -04002387 /* Only insert a separating space if we have something to separate... */
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002388 if (asprintf(&newenv, "%s%s%s", oldenv, oldenv[0] != '\0' ? " " : "",
2389 preload_path) < 0) {
2390 return -1;
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002391 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002392
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002393 /*
2394 * Avoid using putenv(3), since that requires us to hold onto a
2395 * reference to that string until the environment is no longer used to
2396 * prevent a memory leak.
2397 * See https://crbug.com/930189 for more details.
2398 */
2399 ret = setenv(kLdPreloadEnvVar, newenv, 1);
2400 free(newenv);
2401 return ret;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002402#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04002403}
2404
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002405static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04002406{
2407 int r = pipe(fds);
2408 char fd_buf[11];
2409 if (r)
2410 return r;
2411 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2412 if (r <= 0)
2413 return -EINVAL;
2414 setenv(kFdEnvVar, fd_buf, 1);
2415 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05002416}
2417
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002418static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002419{
2420 const char *kFdPath = "/proc/self/fd";
2421
2422 DIR *d = opendir(kFdPath);
2423 struct dirent *dir_entry;
2424
2425 if (d == NULL)
2426 return -1;
2427 int dir_fd = dirfd(d);
2428 while ((dir_entry = readdir(d)) != NULL) {
2429 size_t i;
2430 char *end;
2431 bool should_close = true;
2432 const int fd = strtol(dir_entry->d_name, &end, 10);
2433
2434 if ((*end) != '\0') {
2435 continue;
2436 }
2437 /*
2438 * We might have set up some pipes that we want to share with
2439 * the parent process, and should not be closed.
2440 */
2441 for (i = 0; i < size; ++i) {
2442 if (fd == inheritable_fds[i]) {
2443 should_close = false;
2444 break;
2445 }
2446 }
2447 /* Also avoid closing the directory fd. */
2448 if (should_close && fd != dir_fd)
2449 close(fd);
2450 }
2451 closedir(d);
2452 return 0;
2453}
2454
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002455static int redirect_fds(struct minijail *j)
2456{
2457 size_t i, i2;
2458 int closeable;
2459 for (i = 0; i < j->preserved_fd_count; i++) {
2460 if (dup2(j->preserved_fds[i].parent_fd,
2461 j->preserved_fds[i].child_fd) == -1) {
2462 return -1;
2463 }
2464 }
2465 /*
2466 * After all fds have been duped, we are now free to close all parent
2467 * fds that are *not* child fds.
2468 */
2469 for (i = 0; i < j->preserved_fd_count; i++) {
2470 closeable = true;
2471 for (i2 = 0; i2 < j->preserved_fd_count; i2++) {
2472 closeable &= j->preserved_fds[i].parent_fd !=
2473 j->preserved_fds[i2].child_fd;
2474 }
2475 if (closeable)
2476 close(j->preserved_fds[i].parent_fd);
2477 }
2478 return 0;
2479}
2480
Dylan Reidacfb8be2017-08-25 12:56:51 -07002481/*
2482 * Structure that specifies how to start a minijail.
2483 *
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002484 * filename - The program to exec in the child. Required if |exec_in_child| = 1.
2485 * argv - Arguments for the child program. Required if |exec_in_child| = 1.
2486 * envp - Environment for the child program. Available if |exec_in_child| = 1.
2487 Currently only honored if |use_preload| = 0 and non-NULL.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002488 * use_preload - If true use LD_PRELOAD.
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002489 * exec_in_child - If true, run |filename|. Otherwise, the child will return to
Dylan Reid0412dcc2017-08-24 11:33:15 -07002490 * the caller.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002491 */
2492struct minijail_run_config {
2493 const char *filename;
2494 char *const *argv;
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002495 char *const *envp;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002496 int use_preload;
Dylan Reid0412dcc2017-08-24 11:33:15 -07002497 int exec_in_child;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002498};
2499
2500/*
2501 * Set of pointers to fill with values from minijail_run.
2502 * All arguments are allowed to be NULL if unused.
2503 *
2504 * pstdin_fd - Filled with stdin pipe if non-NULL.
2505 * pstdout_fd - Filled with stdout pipe if non-NULL.
2506 * pstderr_fd - Filled with stderr pipe if non-NULL.
2507 * pchild_pid - Filled with the pid of the child process if non-NULL.
2508 */
2509struct minijail_run_status {
2510 int *pstdin_fd;
2511 int *pstdout_fd;
2512 int *pstderr_fd;
2513 pid_t *pchild_pid;
2514};
2515
Dylan Reid18c49c82017-08-25 14:52:27 -07002516static int minijail_run_internal(struct minijail *j,
2517 const struct minijail_run_config *config,
2518 struct minijail_run_status *status_out);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002519
Will Drewry6ac91122011-10-21 16:38:58 -05002520int API minijail_run(struct minijail *j, const char *filename,
2521 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002522{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002523 struct minijail_run_config config = {
2524 .filename = filename,
2525 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002526 .envp = NULL,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002527 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002528 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002529 };
2530 struct minijail_run_status status = {};
2531 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002532}
2533
2534int API minijail_run_pid(struct minijail *j, const char *filename,
2535 char *const argv[], pid_t *pchild_pid)
2536{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002537 struct minijail_run_config config = {
2538 .filename = filename,
2539 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002540 .envp = NULL,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002541 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002542 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002543 };
2544 struct minijail_run_status status = {
2545 .pchild_pid = pchild_pid,
2546 };
2547 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002548}
2549
2550int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002551 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002552{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002553 struct minijail_run_config config = {
2554 .filename = filename,
2555 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002556 .envp = NULL,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002557 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002558 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002559 };
2560 struct minijail_run_status status = {
2561 .pstdin_fd = pstdin_fd,
2562 };
2563 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002564}
2565
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002566int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002567 char *const argv[], pid_t *pchild_pid,
2568 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002569{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002570 struct minijail_run_config config = {
2571 .filename = filename,
2572 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002573 .envp = NULL,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002574 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002575 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002576 };
2577 struct minijail_run_status status = {
2578 .pstdin_fd = pstdin_fd,
2579 .pstdout_fd = pstdout_fd,
2580 .pstderr_fd = pstderr_fd,
2581 .pchild_pid = pchild_pid,
2582 };
2583 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002584}
2585
2586int API minijail_run_no_preload(struct minijail *j, const char *filename,
2587 char *const argv[])
2588{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002589 struct minijail_run_config config = {
2590 .filename = filename,
2591 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002592 .envp = NULL,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002593 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002594 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002595 };
2596 struct minijail_run_status status = {};
2597 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002598}
2599
Samuel Tan63187f42015-10-16 13:01:53 -07002600int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002601 const char *filename,
2602 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002603 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002604 int *pstdin_fd,
2605 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002606 int *pstderr_fd)
2607{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002608 struct minijail_run_config config = {
2609 .filename = filename,
2610 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002611 .envp = NULL,
2612 .use_preload = false,
2613 .exec_in_child = true,
2614 };
2615 struct minijail_run_status status = {
2616 .pstdin_fd = pstdin_fd,
2617 .pstdout_fd = pstdout_fd,
2618 .pstderr_fd = pstderr_fd,
2619 .pchild_pid = pchild_pid,
2620 };
2621 return minijail_run_internal(j, &config, &status);
2622}
2623
2624int API minijail_run_env_pid_pipes_no_preload(struct minijail *j,
2625 const char *filename,
2626 char *const argv[],
2627 char *const envp[],
2628 pid_t *pchild_pid, int *pstdin_fd,
2629 int *pstdout_fd, int *pstderr_fd)
2630{
2631 struct minijail_run_config config = {
2632 .filename = filename,
2633 .argv = argv,
2634 .envp = envp,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002635 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002636 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002637 };
2638 struct minijail_run_status status = {
2639 .pstdin_fd = pstdin_fd,
2640 .pstdout_fd = pstdout_fd,
2641 .pstderr_fd = pstderr_fd,
2642 .pchild_pid = pchild_pid,
2643 };
2644 return minijail_run_internal(j, &config, &status);
Samuel Tan63187f42015-10-16 13:01:53 -07002645}
2646
Dylan Reid0412dcc2017-08-24 11:33:15 -07002647pid_t API minijail_fork(struct minijail *j)
2648{
2649 struct minijail_run_config config = {};
2650 struct minijail_run_status status = {};
2651 return minijail_run_internal(j, &config, &status);
2652}
2653
Dylan Reid18c49c82017-08-25 14:52:27 -07002654static int minijail_run_internal(struct minijail *j,
2655 const struct minijail_run_config *config,
2656 struct minijail_run_status *status_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002657{
Elly Jonese1749eb2011-10-07 13:54:59 -04002658 char *oldenv, *oldenv_copy = NULL;
2659 pid_t child_pid;
2660 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002661 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002662 int stdout_fds[2];
2663 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08002664 int child_sync_pipe_fds[2];
2665 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002666 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002667 /* We need to remember this across the minijail_preexec() call. */
2668 int pid_namespace = j->flags.pids;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -07002669 /*
2670 * Create an init process if we are entering a pid namespace, unless the
2671 * user has explicitly opted out by calling minijail_run_as_init().
2672 */
2673 int do_init = j->flags.do_init && !j->flags.run_as_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002674 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002675
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002676 if (use_preload) {
Dylan Reid0412dcc2017-08-24 11:33:15 -07002677 if (j->hooks_head != NULL)
2678 die("Minijail hooks are not supported with LD_PRELOAD");
2679 if (!config->exec_in_child)
2680 die("minijail_fork is not supported with LD_PRELOAD");
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002681 if (config->envp != NULL)
2682 die("cannot pass a new environment with LD_PRELOAD");
Dylan Reid0412dcc2017-08-24 11:33:15 -07002683
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002684 oldenv = getenv(kLdPreloadEnvVar);
2685 if (oldenv) {
2686 oldenv_copy = strdup(oldenv);
2687 if (!oldenv_copy)
2688 return -ENOMEM;
2689 }
2690
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002691 if (setup_preload(j, oldenv))
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002692 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002693 }
Will Drewryf89aef52011-09-16 16:48:57 -05002694
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002695 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002696 if (j->flags.use_caps && j->caps != 0 &&
2697 !j->flags.set_ambient_caps) {
2698 die("non-empty, non-ambient capabilities are not "
2699 "supported without LD_PRELOAD");
2700 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002701 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002702
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002703 if (use_preload) {
2704 /*
2705 * Before we fork(2) and execve(2) the child process, we need
2706 * to open a pipe(2) to send the minijail configuration over.
2707 */
2708 if (setup_pipe(pipe_fds))
2709 return -EFAULT;
2710 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002711
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002712 /*
2713 * If we want to write to the child process' standard input,
2714 * create the pipe(2) now.
2715 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002716 if (status_out->pstdin_fd) {
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002717 if (pipe(stdin_fds))
2718 return -EFAULT;
2719 }
2720
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002721 /*
2722 * If we want to read from the child process' standard output,
2723 * create the pipe(2) now.
2724 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002725 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002726 if (pipe(stdout_fds))
2727 return -EFAULT;
2728 }
2729
2730 /*
2731 * If we want to read from the child process' standard error,
2732 * create the pipe(2) now.
2733 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002734 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002735 if (pipe(stderr_fds))
2736 return -EFAULT;
2737 }
2738
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002739 /*
Daniel Erat2d69add2019-04-23 20:58:53 -07002740 * If the parent process needs to configure the child's runtime
2741 * environment after forking, create a pipe(2) to block the child until
2742 * configuration is done.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002743 */
Daniel Erat2d69add2019-04-23 20:58:53 -07002744 if (j->flags.forward_signals || j->flags.pid_file || j->flags.cgroups ||
2745 j->rlimit_count || j->flags.userns) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002746 sync_child = 1;
2747 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002748 return -EFAULT;
2749 }
2750
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002751 /*
2752 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002753 *
2754 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2755 *
2756 * In multithreaded programs, there are a bunch of locks inside libc,
2757 * some of which may be held by other threads at the time that we call
2758 * minijail_run_pid(). If we call fork(), glibc does its level best to
2759 * ensure that we hold all of these locks before it calls clone()
2760 * internally and drop them after clone() returns, but when we call
2761 * sys_clone(2) directly, all that gets bypassed and we end up with a
2762 * child address space where some of libc's important locks are held by
2763 * other threads (which did not get cloned, and hence will never release
2764 * those locks). This is okay so long as we call exec() immediately
2765 * after, but a bunch of seemingly-innocent libc functions like setenv()
2766 * take locks.
2767 *
2768 * Hence, only call sys_clone() if we need to, in order to get at pid
2769 * namespacing. If we follow this path, the child's address space might
2770 * have broken locks; you may only call functions that do not acquire
2771 * any locks.
2772 *
2773 * Unfortunately, fork() acquires every lock it can get its hands on, as
2774 * previously detailed, so this function is highly likely to deadlock
2775 * later on (see "deadlock here") if we're multithreaded.
2776 *
2777 * We might hack around this by having the clone()d child (init of the
2778 * pid namespace) return directly, rather than leaving the clone()d
2779 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002780 * its fork()ed child return in turn), but that process would be
2781 * crippled with its libc locks potentially broken. We might try
2782 * fork()ing in the parent before we clone() to ensure that we own all
2783 * the locks, but then we have to have the forked child hanging around
2784 * consuming resources (and possibly having file descriptors / shared
2785 * memory regions / etc attached). We'd need to keep the child around to
2786 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002787 *
2788 * TODO(ellyjones): figure out if the "forked child hanging around"
2789 * problem is fixable or not. It would be nice if we worked in this
2790 * case.
2791 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002792 if (pid_namespace) {
2793 int clone_flags = CLONE_NEWPID | SIGCHLD;
2794 if (j->flags.userns)
2795 clone_flags |= CLONE_NEWUSER;
2796 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002797 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002798 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002799 }
Elly Jones761b7412012-06-13 15:49:52 -04002800
Elly Jonese1749eb2011-10-07 13:54:59 -04002801 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002802 if (use_preload) {
2803 free(oldenv_copy);
2804 }
Jorge Lucangeli Obesb4b7c5a2019-09-09 10:47:36 -04002805 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002806 }
Will Drewryf89aef52011-09-16 16:48:57 -05002807
Elly Jonese1749eb2011-10-07 13:54:59 -04002808 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002809 if (use_preload) {
2810 /* Restore parent's LD_PRELOAD. */
2811 if (oldenv_copy) {
2812 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2813 free(oldenv_copy);
2814 } else {
2815 unsetenv(kLdPreloadEnvVar);
2816 }
2817 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002818 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002819
Elly Jonese1749eb2011-10-07 13:54:59 -04002820 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002821
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002822 if (j->flags.forward_signals) {
2823 forward_pid = child_pid;
2824 install_signal_handlers();
2825 }
2826
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002827 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002828 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002829
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002830 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002831 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002832
Dylan Reid0f72ef42017-06-06 15:42:49 -07002833 if (j->rlimit_count)
2834 set_rlimits_or_die(j);
2835
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002836 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002837 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002838
Mike Frysinger902a4492018-12-27 05:22:56 -05002839 if (j->flags.enter_vfs)
2840 close(j->mountns_fd);
2841
2842 if (j->flags.enter_net)
2843 close(j->netns_fd);
2844
Dylan Reidce5b55e2016-01-13 11:04:16 -08002845 if (sync_child)
2846 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002847
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002848 if (use_preload) {
2849 /* Send marshalled minijail. */
2850 close(pipe_fds[0]); /* read endpoint */
2851 ret = minijail_to_fd(j, pipe_fds[1]);
2852 close(pipe_fds[1]); /* write endpoint */
2853 if (ret) {
2854 kill(j->initpid, SIGKILL);
2855 die("failed to send marshalled minijail");
2856 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002857 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002858
Dylan Reidacfb8be2017-08-25 12:56:51 -07002859 if (status_out->pchild_pid)
2860 *status_out->pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002861
2862 /*
2863 * If we want to write to the child process' standard input,
2864 * set up the write end of the pipe.
2865 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002866 if (status_out->pstdin_fd)
2867 *status_out->pstdin_fd =
2868 setup_pipe_end(stdin_fds, 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002869
2870 /*
2871 * If we want to read from the child process' standard output,
2872 * set up the read end of the pipe.
2873 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002874 if (status_out->pstdout_fd)
2875 *status_out->pstdout_fd =
2876 setup_pipe_end(stdout_fds, 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002877
2878 /*
2879 * If we want to read from the child process' standard error,
2880 * set up the read end of the pipe.
2881 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002882 if (status_out->pstderr_fd)
2883 *status_out->pstderr_fd =
2884 setup_pipe_end(stderr_fds, 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002885
Dylan Reid0412dcc2017-08-24 11:33:15 -07002886 /*
2887 * If forking return the child pid, in the normal exec case
2888 * return 0 for success.
2889 */
2890 if (!config->exec_in_child)
2891 return child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04002892 return 0;
2893 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002894 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002895 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002896
Peter Qiu2860c462015-12-16 15:13:06 -08002897 if (j->flags.reset_signal_mask) {
2898 sigset_t signal_mask;
2899 if (sigemptyset(&signal_mask) != 0)
2900 pdie("sigemptyset failed");
2901 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2902 pdie("sigprocmask failed");
2903 }
2904
Luis Hector Chaveza27118a2018-04-04 08:18:01 -07002905 if (j->flags.reset_signal_handlers) {
2906 int signum;
2907 for (signum = 0; signum <= SIGRTMAX; signum++) {
2908 /*
2909 * Ignore EINVAL since some signal numbers in the range
2910 * might not be valid.
2911 */
2912 if (signal(signum, SIG_DFL) == SIG_ERR &&
2913 errno != EINVAL) {
2914 pdie("failed to reset signal %d disposition",
2915 signum);
2916 }
2917 }
2918 }
2919
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002920 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002921 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002922 int inheritable_fds[kMaxInheritableFdsSize];
2923 size_t size = 0;
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002924 size_t i;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002925 if (use_preload) {
2926 inheritable_fds[size++] = pipe_fds[0];
2927 inheritable_fds[size++] = pipe_fds[1];
2928 }
2929 if (sync_child) {
2930 inheritable_fds[size++] = child_sync_pipe_fds[0];
2931 inheritable_fds[size++] = child_sync_pipe_fds[1];
2932 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002933 if (status_out->pstdin_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002934 inheritable_fds[size++] = stdin_fds[0];
2935 inheritable_fds[size++] = stdin_fds[1];
2936 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002937 if (status_out->pstdout_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002938 inheritable_fds[size++] = stdout_fds[0];
2939 inheritable_fds[size++] = stdout_fds[1];
2940 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002941 if (status_out->pstderr_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002942 inheritable_fds[size++] = stderr_fds[0];
2943 inheritable_fds[size++] = stderr_fds[1];
2944 }
Jorge Lucangeli Obes2337f802019-07-18 14:46:03 -04002945
Jorge Lucangeli Obescf3bbea2019-07-24 09:06:40 -04002946 /*
2947 * Preserve namespace file descriptors over the close_open_fds()
2948 * call. These are closed in minijail_enter() so they won't leak
2949 * into the child process.
2950 */
Jorge Lucangeli Obes2337f802019-07-18 14:46:03 -04002951 if (j->flags.enter_vfs)
2952 minijail_preserve_fd(j, j->mountns_fd, j->mountns_fd);
2953 if (j->flags.enter_net)
2954 minijail_preserve_fd(j, j->netns_fd, j->netns_fd);
2955
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002956 for (i = 0; i < j->preserved_fd_count; i++) {
2957 /*
2958 * Preserve all parent_fds. They will be dup2(2)-ed in
2959 * the child later.
2960 */
2961 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
2962 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002963
2964 if (close_open_fds(inheritable_fds, size) < 0)
2965 die("failed to close open file descriptors");
2966 }
2967
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002968 if (redirect_fds(j))
2969 die("failed to set up fd redirections");
2970
Dylan Reidce5b55e2016-01-13 11:04:16 -08002971 if (sync_child)
2972 wait_for_parent_setup(child_sync_pipe_fds);
2973
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002974 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002975 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002976
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002977 /*
2978 * If we want to write to the jailed process' standard input,
2979 * set up the read end of the pipe.
2980 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002981 if (status_out->pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002982 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2983 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002984 die("failed to set up stdin pipe");
2985 }
2986
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002987 /*
2988 * If we want to read from the jailed process' standard output,
2989 * set up the write end of the pipe.
2990 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002991 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002992 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2993 STDOUT_FILENO) < 0)
2994 die("failed to set up stdout pipe");
2995 }
2996
2997 /*
2998 * If we want to read from the jailed process' standard error,
2999 * set up the write end of the pipe.
3000 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07003001 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08003002 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
3003 STDERR_FILENO) < 0)
3004 die("failed to set up stderr pipe");
3005 }
3006
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05003007 /*
Xiyuan Xia9b41e652019-05-23 11:03:04 -07003008 * If any of stdin, stdout, or stderr are TTYs, or setsid flag is
3009 * set, create a new session. This prevents the jailed process from
3010 * using the TIOCSTI ioctl to push characters into the parent process
3011 * terminal's input buffer, therefore escaping the jail.
Stephen Barber5dd5b1b2017-10-16 23:02:39 -07003012 *
3013 * Since it has just forked, the child will not be a process group
3014 * leader, and this call to setsid() should always succeed.
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05003015 */
Xiyuan Xia9b41e652019-05-23 11:03:04 -07003016 if (j->flags.setsid || isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05003017 isatty(STDERR_FILENO)) {
3018 if (setsid() < 0) {
3019 pdie("setsid() failed");
3020 }
3021 }
3022
Dylan Reid791f5772015-09-14 20:02:42 -07003023 /* If running an init program, let it decide when/how to mount /proc. */
3024 if (pid_namespace && !do_init)
3025 j->flags.remount_proc_ro = 0;
3026
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003027 if (use_preload) {
3028 /* Strip out flags that cannot be inherited across execve(2). */
3029 minijail_preexec(j);
3030 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003031 /*
3032 * If not using LD_PRELOAD, do all jailing before execve(2).
3033 * Note that PID namespaces can only be entered on fork(2),
3034 * so that flag is still cleared.
3035 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003036 j->flags.pids = 0;
3037 }
Dylan Reid0412dcc2017-08-24 11:33:15 -07003038
3039 /*
3040 * Jail this process.
3041 * If forking, return.
3042 * If not, execve(2) the target.
3043 */
Elly Jonese1749eb2011-10-07 13:54:59 -04003044 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003045
Dylan Reid0412dcc2017-08-24 11:33:15 -07003046 if (config->exec_in_child && pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05003047 /*
3048 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003049 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003050 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003051 * a child to actually run the program. If |do_init == 0|, we
3052 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04003053 *
3054 * If we're multithreaded, we'll probably deadlock here. See
3055 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04003056 */
3057 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003058 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04003059 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003060 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04003061 /*
3062 * Best effort. Don't bother checking the return value.
3063 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003064 prctl(PR_SET_NAME, "minijail-init");
3065 init(child_pid); /* Never returns. */
3066 }
Elly Jonese1749eb2011-10-07 13:54:59 -04003067 }
Elly Jonescd7a9042011-07-22 13:56:51 -04003068
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07003069 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
3070
Dylan Reid0412dcc2017-08-24 11:33:15 -07003071 if (!config->exec_in_child)
3072 return 0;
3073
Elly Jonesdd3e8512012-01-23 15:13:38 -05003074 /*
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05003075 * If not using LD_PRELOAD, support passing a new environment instead of
3076 * inheriting the parent's.
3077 * When not using LD_PRELOAD there is no need to modify the environment
3078 * to add Minijail-related variables, so passing a new environment is
3079 * fine.
3080 */
3081 char *const *child_env = environ;
3082 if (!use_preload && config->envp != NULL) {
3083 child_env = config->envp;
3084 }
3085
3086 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003087 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04003088 * calling process
3089 * -> execve()-ing process
3090 * If we are:
3091 * calling process
3092 * -> init()-ing process
3093 * -> execve()-ing process
3094 */
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05003095 ret = execve(config->filename, config->argv, child_env);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003096 if (ret == -1) {
Dylan Reidacfb8be2017-08-25 12:56:51 -07003097 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003098 }
3099 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04003100}
3101
Will Drewry6ac91122011-10-21 16:38:58 -05003102int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003103{
3104 int st;
3105 if (kill(j->initpid, SIGTERM))
3106 return -errno;
3107 if (waitpid(j->initpid, &st, 0) < 0)
3108 return -errno;
3109 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04003110}
3111
Will Drewry6ac91122011-10-21 16:38:58 -05003112int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003113{
3114 int st;
3115 if (waitpid(j->initpid, &st, 0) < 0)
3116 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003117
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003118 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003119 int error_status = st;
3120 if (WIFSIGNALED(st)) {
3121 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07003122 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003123 j->initpid, signum);
3124 /*
3125 * We return MINIJAIL_ERR_JAIL if the process received
3126 * SIGSYS, which happens when a syscall is blocked by
3127 * seccomp filters.
3128 * If not, we do what bash(1) does:
3129 * $? = 128 + signum
3130 */
3131 if (signum == SIGSYS) {
3132 error_status = MINIJAIL_ERR_JAIL;
3133 } else {
3134 error_status = 128 + signum;
3135 }
3136 }
3137 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003138 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003139
3140 int exit_status = WEXITSTATUS(st);
3141 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07003142 info("child process %d exited with status %d",
3143 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003144
3145 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04003146}
3147
Will Drewry6ac91122011-10-21 16:38:58 -05003148void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003149{
Dylan Reid605ce7f2016-01-19 19:21:00 -08003150 size_t i;
3151
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07003152 if (j->filter_prog) {
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08003153 free(j->filter_prog->filter);
3154 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04003155 }
Mike Frysingerac08a682017-10-10 02:04:50 -04003156 free_mounts_list(j);
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07003157 while (j->hooks_head) {
3158 struct hook *c = j->hooks_head;
3159 j->hooks_head = c->next;
3160 free(c);
3161 }
3162 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04003163 if (j->user)
3164 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08003165 if (j->suppl_gid_list)
3166 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05003167 if (j->chrootdir)
3168 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04003169 if (j->pid_file_path)
3170 free(j->pid_file_path);
3171 if (j->uidmap)
3172 free(j->uidmap);
3173 if (j->gidmap)
3174 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04003175 if (j->hostname)
3176 free(j->hostname);
Luis Hector Chavez9acba452018-10-11 10:13:25 -07003177 if (j->preload_path)
3178 free(j->preload_path);
Andrew Brestickereac28942015-11-11 16:04:46 -08003179 if (j->alt_syscall_table)
3180 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08003181 for (i = 0; i < j->cgroup_count; ++i)
3182 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04003183 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003184}
Luis Hector Chavez114a9302017-09-05 20:36:58 -07003185
3186void API minijail_log_to_fd(int fd, int min_priority)
3187{
3188 init_logging(LOG_TO_FD, fd, min_priority);
3189}