blob: 6d6b2dc4218d623eb3fb87ebfd585bb08d4285fd [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) {
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001797 /*
1798 * Older kernels might not have the /proc/<pid>/setgroups files.
1799 */
Mike Frysinger6b190c02017-01-04 17:18:42 -05001800 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001801 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001802 if (ret == -ENOENT) {
1803 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1804 warn("could not disable setgroups(2)");
1805 } else
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001806 kill_child_and_die(
1807 j, "failed to disable setgroups(2)");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001808 }
1809 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001810 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1811 kill_child_and_die(j, "failed to write gid_map");
1812}
1813
1814static void enter_user_namespace(const struct minijail *j)
1815{
Luis Hector Chavez71323552017-09-05 09:17:22 -07001816 int uid = j->flags.uid ? j->uid : 0;
1817 int gid = j->flags.gid ? j->gid : 0;
1818 if (j->gidmap && setresgid(gid, gid, gid)) {
1819 pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid,
1820 gid);
1821 }
1822 if (j->uidmap && setresuid(uid, uid, uid)) {
1823 pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid,
1824 uid);
1825 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001826}
1827
1828static void parent_setup_complete(int *pipe_fds)
1829{
1830 close(pipe_fds[0]);
1831 close(pipe_fds[1]);
1832}
1833
1834/*
1835 * wait_for_parent_setup: Called by the child process to wait for any
1836 * further parent-side setup to complete before continuing.
1837 */
1838static void wait_for_parent_setup(int *pipe_fds)
1839{
1840 char buf;
1841
1842 close(pipe_fds[1]);
1843
1844 /* Wait for parent to complete setup and close the pipe. */
1845 if (read(pipe_fds[0], &buf, 1) != 0)
1846 die("failed to sync with parent");
1847 close(pipe_fds[0]);
1848}
1849
1850static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001851{
Lutz Justen13807cb2017-01-03 17:11:55 +01001852 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1853 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001854 die("can only do one of inherit, keep, or set supplementary "
1855 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001856 }
1857
Lutz Justen13807cb2017-01-03 17:11:55 +01001858 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001859 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001860 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001861 } else if (j->flags.set_suppl_gids) {
1862 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001863 pdie("setgroups(suppl_gids) failed");
Luis Hector Chavez71323552017-09-05 09:17:22 -07001864 } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001865 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001866 * Only attempt to clear supplementary groups if we are changing
Luis Hector Chavez71323552017-09-05 09:17:22 -07001867 * users or groups, and if the caller did not request to disable
1868 * setgroups (used when entering a user namespace as a
1869 * non-privileged user).
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001870 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001871 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001872 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001873 }
1874
1875 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001876 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001877
1878 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001879 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001880}
1881
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001882static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1883{
1884 const uint64_t one = 1;
1885 unsigned int i;
1886 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1887 if (keep_mask & (one << i))
1888 continue;
1889 if (prctl(PR_CAPBSET_DROP, i))
1890 pdie("could not drop capability from bounding set");
1891 }
1892}
1893
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001894static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001895{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001896 if (!j->flags.use_caps)
1897 return;
1898
Elly Jonese1749eb2011-10-07 13:54:59 -04001899 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001900 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001901 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001902 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001903 unsigned int i;
1904 if (!caps)
1905 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001906 if (cap_clear(caps))
1907 die("can't clear caps");
1908
1909 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001910 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001911 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001912 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001913 flag[0] = i;
1914 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001915 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001916 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001917 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001918 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001919 die("can't add inheritable cap");
1920 }
1921 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001922 die("can't apply initial cleaned capset");
1923
1924 /*
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001925 * Instead of dropping the bounding set first, do it here in case
Kees Cook323878a2013-02-05 15:35:24 -08001926 * the caller had a more permissive bounding set which could
1927 * have been used above to raise a capability that wasn't already
1928 * present. This requires CAP_SETPCAP, so we raised/kept it above.
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001929 *
1930 * However, if we're asked to skip setting *and* locking the
1931 * SECURE_NOROOT securebit, also skip dropping the bounding set.
1932 * If the caller wants to regain all capabilities when executing a
1933 * set-user-ID-root program, allow them to do so. The default behavior
1934 * (i.e. the behavior without |securebits_skip_mask| set) will still put
1935 * the jailed process tree in a capabilities-only environment.
1936 *
1937 * We check the negated skip mask for SECURE_NOROOT and
1938 * SECURE_NOROOT_LOCKED. If the bits are set in the negated mask they
1939 * will *not* be skipped in lock_securebits(), and therefore we should
1940 * drop the bounding set.
Kees Cook323878a2013-02-05 15:35:24 -08001941 */
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001942 if (secure_noroot_set_and_locked(~j->securebits_skip_mask)) {
1943 drop_capbset(j->caps, last_valid_cap);
1944 } else {
1945 warn("SECURE_NOROOT not set, not dropping bounding set");
1946 }
Kees Cook323878a2013-02-05 15:35:24 -08001947
1948 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001949 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001950 flag[0] = CAP_SETPCAP;
1951 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1952 die("can't clear effective cap");
1953 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1954 die("can't clear permitted cap");
1955 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1956 die("can't clear inheritable cap");
1957 }
1958
1959 if (cap_set_proc(caps))
1960 die("can't apply final cleaned capset");
1961
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001962 /*
1963 * If ambient capabilities are supported, clear all capabilities first,
1964 * then raise the requested ones.
1965 */
1966 if (j->flags.set_ambient_caps) {
1967 if (!cap_ambient_supported()) {
1968 pdie("ambient capabilities not supported");
1969 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001970 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1971 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001972 pdie("can't clear ambient capabilities");
1973 }
1974
1975 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1976 if (!(j->caps & (one << i)))
1977 continue;
1978
1979 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1980 0) != 0) {
1981 pdie("prctl(PR_CAP_AMBIENT, "
1982 "PR_CAP_AMBIENT_RAISE, %u) failed",
1983 i);
1984 }
1985 }
1986 }
1987
Kees Cook323878a2013-02-05 15:35:24 -08001988 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001989}
1990
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001991static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001992{
1993 /*
1994 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1995 * in the kernel source tree for an explanation of the parameters.
1996 */
1997 if (j->flags.no_new_privs) {
1998 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1999 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
2000 }
2001
2002 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07002003 * Code running with ASan
2004 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
2005 * will make system calls not included in the syscall filter policy,
2006 * which will likely crash the program. Skip setting seccomp filter in
2007 * that case.
2008 * 'running_with_asan()' has no inputs and is completely defined at
2009 * build time, so this cannot be used by an attacker to skip setting
2010 * seccomp filter.
2011 */
Evgenii Stepanov3d98f3c2018-08-23 15:06:50 -07002012 if (j->flags.seccomp_filter && running_with_asan()) {
Evgenii Stepanov825828c2018-07-27 11:57:07 -07002013 warn("running with (HW)ASan, not setting seccomp filter");
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07002014 return;
2015 }
2016
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002017 if (j->flags.seccomp_filter) {
2018 if (j->flags.seccomp_filter_logging) {
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002019 warn("logging seccomp filter failures");
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04002020 if (!seccomp_ret_log_available()) {
2021 /*
2022 * If SECCOMP_RET_LOG is not available,
2023 * install the SIGSYS handler first.
2024 */
2025 if (install_sigsys_handler())
2026 pdie(
2027 "failed to install SIGSYS handler");
2028 }
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002029 } else if (j->flags.seccomp_filter_tsync) {
2030 /*
2031 * If setting thread sync,
2032 * reset the SIGSYS signal handler so that
2033 * the entire thread group is killed.
2034 */
2035 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
2036 pdie("failed to reset SIGSYS disposition");
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002037 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002038 }
2039
2040 /*
2041 * Install the syscall filter.
2042 */
2043 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002044 if (j->flags.seccomp_filter_tsync) {
2045 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
2046 SECCOMP_FILTER_FLAG_TSYNC,
2047 j->filter_prog)) {
2048 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002049 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002050 } else {
2051 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
2052 j->filter_prog)) {
2053 pdie("prctl(seccomp_filter) failed");
2054 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002055 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002056 }
2057}
2058
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002059static pid_t forward_pid = -1;
2060
Mike Frysinger33d051a2018-05-30 16:41:10 -04002061static void forward_signal(int sig,
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04002062 siginfo_t *siginfo attribute_unused,
2063 void *void_context attribute_unused)
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002064{
2065 if (forward_pid != -1) {
Mike Frysinger33d051a2018-05-30 16:41:10 -04002066 kill(forward_pid, sig);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002067 }
2068}
2069
2070static void install_signal_handlers(void)
2071{
2072 struct sigaction act;
2073
2074 memset(&act, 0, sizeof(act));
2075 act.sa_sigaction = &forward_signal;
2076 act.sa_flags = SA_SIGINFO | SA_RESTART;
2077
2078 /* Handle all signals, except SIGCHLD. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002079 for (int sig = 1; sig < NSIG; sig++) {
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002080 /*
2081 * We don't care if we get EINVAL: that just means that we
2082 * can't handle this signal, so let's skip it and continue.
2083 */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002084 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002085 }
2086 /* Reset SIGCHLD's handler. */
2087 signal(SIGCHLD, SIG_DFL);
2088
2089 /* Handle real-time signals. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002090 for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++) {
2091 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002092 }
2093}
2094
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002095static const char *lookup_hook_name(minijail_hook_event_t event)
2096{
2097 switch (event) {
2098 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
2099 return "pre-drop-caps";
2100 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
2101 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07002102 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
2103 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002104 case MINIJAIL_HOOK_EVENT_MAX:
2105 /*
2106 * Adding this in favor of a default case to force the
2107 * compiler to error out if a new enum value is added.
2108 */
2109 break;
2110 }
2111 return "unknown";
2112}
2113
2114static void run_hooks_or_die(const struct minijail *j,
2115 minijail_hook_event_t event)
2116{
2117 int rc;
2118 int hook_index = 0;
2119 for (struct hook *c = j->hooks_head; c; c = c->next) {
2120 if (c->event != event)
2121 continue;
2122 rc = c->hook(c->payload);
2123 if (rc != 0) {
2124 errno = -rc;
2125 pdie("%s hook (index %d) failed",
2126 lookup_hook_name(event), hook_index);
2127 }
2128 /* Only increase the index within the same hook event type. */
2129 ++hook_index;
2130 }
2131}
2132
Will Drewry6ac91122011-10-21 16:38:58 -05002133void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002134{
Dylan Reidf682d472015-09-17 21:39:07 -07002135 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002136 * If we're dropping caps, get the last valid cap from /proc now,
2137 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07002138 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002139 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002140 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002141 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07002142
Elly Jonese1749eb2011-10-07 13:54:59 -04002143 if (j->flags.pids)
2144 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002145 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04002146
Lutz Justen13807cb2017-01-03 17:11:55 +01002147 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05002148 die("cannot inherit supplementary groups without setting a "
2149 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04002150
Elly Jonesdd3e8512012-01-23 15:13:38 -05002151 /*
2152 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04002153 * so we don't even try. If any of our operations fail, we abort() the
2154 * entire process.
2155 */
Mike Frysinger902a4492018-12-27 05:22:56 -05002156 if (j->flags.enter_vfs) {
2157 if (setns(j->mountns_fd, CLONE_NEWNS))
2158 pdie("setns(CLONE_NEWNS) failed");
2159 close(j->mountns_fd);
2160 }
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002161
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07002162 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002163 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002164 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002165 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05002166 * By default, remount all filesystems as private, unless
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04002167 * - Passed a specific remount mode, in which case remount with
2168 * that,
2169 * - Asked not to remount at all, in which case skip the
2170 * mount(2) call.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002171 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
2172 */
Mike Frysinger785b1c32018-02-23 15:47:24 -05002173 if (j->remount_mode) {
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04002174 if (mount(NULL, "/", NULL, MS_REC | j->remount_mode,
2175 NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002176 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
2177 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002178 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002179 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04002180
Dylan Reidf7942472015-11-18 17:55:26 -08002181 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002182 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08002183 }
2184
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002185 if (j->flags.uts) {
2186 if (unshare(CLONE_NEWUTS))
2187 pdie("unshare(CLONE_NEWUTS) failed");
2188
2189 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
2190 pdie("sethostname(%s) failed", j->hostname);
2191 }
2192
Dylan Reid1102f5a2015-09-15 11:52:20 -07002193 if (j->flags.enter_net) {
2194 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002195 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger902a4492018-12-27 05:22:56 -05002196 close(j->netns_fd);
Mike Frysinger7559dfe2016-11-15 18:58:39 -05002197 } else if (j->flags.net) {
2198 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002199 pdie("unshare(CLONE_NEWNET) failed");
2200 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07002201 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002202
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002203 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002204 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002205
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08002206 if (j->flags.new_session_keyring) {
2207 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
2208 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
2209 }
2210
Mike Frysingerac08a682017-10-10 02:04:50 -04002211 /* We have to process all the mounts before we chroot/pivot_root. */
2212 process_mounts_or_die(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002213
Mike Frysingerac08a682017-10-10 02:04:50 -04002214 if (j->flags.chroot && enter_chroot(j))
Mike Frysinger33ffef32017-01-13 19:53:19 -05002215 pdie("chroot");
Mike Frysinger33ffef32017-01-13 19:53:19 -05002216
Mike Frysingerac08a682017-10-10 02:04:50 -04002217 if (j->flags.pivot_root && enter_pivot_root(j))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08002218 pdie("pivot_root");
2219
Martin Pelikánab9eb442017-01-25 11:53:58 +11002220 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07002221 pdie("mount_tmp");
2222
Dylan Reid791f5772015-09-14 20:02:42 -07002223 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04002224 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04002225
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002226 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
2227
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002228 /*
2229 * If we're only dropping capabilities from the bounding set, but not
2230 * from the thread's (permitted|inheritable|effective) sets, do it now.
2231 */
2232 if (j->flags.capbset_drop) {
2233 drop_capbset(j->cap_bset, last_valid_cap);
2234 }
2235
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002236 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002237 * POSIX capabilities are a bit tricky. We must set SECBIT_KEEP_CAPS
2238 * before drop_ugid() below as the latter would otherwise drop all
2239 * capabilities.
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002240 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002241 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002242 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002243 * When using ambient capabilities, CAP_SET{GID,UID} can be
2244 * inherited across execve(2), so SECBIT_KEEP_CAPS is not
2245 * strictly needed.
Elly Jonese1749eb2011-10-07 13:54:59 -04002246 */
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002247 bool require_keep_caps = !j->flags.set_ambient_caps;
2248 if (lock_securebits(j->securebits_skip_mask,
2249 require_keep_caps) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002250 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002251 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002252 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002253
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002254 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002255 /*
2256 * If we're setting no_new_privs, we can drop privileges
2257 * before setting seccomp filter. This way filter policies
2258 * don't need to allow privilege-dropping syscalls.
2259 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002260 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002261 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002262 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002263 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002264 /*
2265 * If we're not setting no_new_privs,
2266 * we need to set seccomp filter *before* dropping privileges.
2267 * WARNING: this means that filter policies *must* allow
2268 * setgroups()/setresgid()/setresuid() for dropping root and
2269 * capget()/capset()/prctl() for dropping caps.
2270 */
2271 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002272 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002273 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04002274 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002275
Elly Jonesdd3e8512012-01-23 15:13:38 -05002276 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08002277 * Select the specified alternate syscall table. The table must not
2278 * block prctl(2) if we're using seccomp as well.
2279 */
2280 if (j->flags.alt_syscall) {
2281 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002282 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08002283 }
2284
2285 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05002286 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04002287 * privilege-dropping syscalls :)
2288 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002289 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04002290 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002291 warn("seccomp not supported");
2292 return;
2293 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002294 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002295 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002296}
2297
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002298/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04002299static int init_exitstatus = 0;
2300
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04002301void init_term(int sig attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002302{
2303 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04002304}
2305
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002306void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04002307{
2308 pid_t pid;
2309 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002310 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002311 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002312 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002313 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002314 /*
2315 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04002316 * left inside our pid namespace or we get a signal.
2317 */
2318 if (pid == rootpid)
2319 init_exitstatus = status;
2320 }
2321 if (!WIFEXITED(init_exitstatus))
2322 _exit(MINIJAIL_ERR_INIT);
2323 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04002324}
2325
Will Drewry6ac91122011-10-21 16:38:58 -05002326int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002327{
2328 size_t sz = 0;
2329 size_t bytes = read(fd, &sz, sizeof(sz));
2330 char *buf;
2331 int r;
2332 if (sizeof(sz) != bytes)
2333 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002334 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04002335 return -E2BIG;
2336 buf = malloc(sz);
2337 if (!buf)
2338 return -ENOMEM;
2339 bytes = read(fd, buf, sz);
2340 if (bytes != sz) {
2341 free(buf);
2342 return -EINVAL;
2343 }
2344 r = minijail_unmarshal(j, buf, sz);
2345 free(buf);
2346 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002347}
2348
Will Drewry6ac91122011-10-21 16:38:58 -05002349int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04002350{
2351 char *buf;
2352 size_t sz = minijail_size(j);
2353 ssize_t written;
2354 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04002355
Elly Jonese1749eb2011-10-07 13:54:59 -04002356 if (!sz)
2357 return -EINVAL;
2358 buf = malloc(sz);
2359 r = minijail_marshal(j, buf, sz);
2360 if (r) {
2361 free(buf);
2362 return r;
2363 }
2364 /* Sends [size][minijail]. */
2365 written = write(fd, &sz, sizeof(sz));
2366 if (written != sizeof(sz)) {
2367 free(buf);
2368 return -EFAULT;
2369 }
2370 written = write(fd, buf, sz);
2371 if (written < 0 || (size_t) written != sz) {
2372 free(buf);
2373 return -EFAULT;
2374 }
2375 free(buf);
2376 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002377}
Elly Jonescd7a9042011-07-22 13:56:51 -04002378
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002379static int setup_preload(const struct minijail *j attribute_unused,
2380 const char *oldenv attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002381{
Daniel Erat5b7a3182015-08-19 16:06:22 -06002382#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002383 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002384 return 0;
2385#else
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002386 const char *preload_path = j->preload_path ?: PRELOADPATH;
2387 char *newenv = NULL;
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002388 int ret = 0;
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002389
2390 if (!oldenv)
2391 oldenv = "";
Elly Jonescd7a9042011-07-22 13:56:51 -04002392
Elly Jonese1749eb2011-10-07 13:54:59 -04002393 /* Only insert a separating space if we have something to separate... */
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002394 if (asprintf(&newenv, "%s%s%s", oldenv, oldenv[0] != '\0' ? " " : "",
2395 preload_path) < 0) {
2396 return -1;
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002397 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002398
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002399 /*
2400 * Avoid using putenv(3), since that requires us to hold onto a
2401 * reference to that string until the environment is no longer used to
2402 * prevent a memory leak.
2403 * See https://crbug.com/930189 for more details.
2404 */
2405 ret = setenv(kLdPreloadEnvVar, newenv, 1);
2406 free(newenv);
2407 return ret;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002408#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04002409}
2410
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002411static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04002412{
2413 int r = pipe(fds);
2414 char fd_buf[11];
2415 if (r)
2416 return r;
2417 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2418 if (r <= 0)
2419 return -EINVAL;
2420 setenv(kFdEnvVar, fd_buf, 1);
2421 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05002422}
2423
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002424static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002425{
2426 const char *kFdPath = "/proc/self/fd";
2427
2428 DIR *d = opendir(kFdPath);
2429 struct dirent *dir_entry;
2430
2431 if (d == NULL)
2432 return -1;
2433 int dir_fd = dirfd(d);
2434 while ((dir_entry = readdir(d)) != NULL) {
2435 size_t i;
2436 char *end;
2437 bool should_close = true;
2438 const int fd = strtol(dir_entry->d_name, &end, 10);
2439
2440 if ((*end) != '\0') {
2441 continue;
2442 }
2443 /*
2444 * We might have set up some pipes that we want to share with
2445 * the parent process, and should not be closed.
2446 */
2447 for (i = 0; i < size; ++i) {
2448 if (fd == inheritable_fds[i]) {
2449 should_close = false;
2450 break;
2451 }
2452 }
2453 /* Also avoid closing the directory fd. */
2454 if (should_close && fd != dir_fd)
2455 close(fd);
2456 }
2457 closedir(d);
2458 return 0;
2459}
2460
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002461static int redirect_fds(struct minijail *j)
2462{
2463 size_t i, i2;
2464 int closeable;
2465 for (i = 0; i < j->preserved_fd_count; i++) {
2466 if (dup2(j->preserved_fds[i].parent_fd,
2467 j->preserved_fds[i].child_fd) == -1) {
2468 return -1;
2469 }
2470 }
2471 /*
2472 * After all fds have been duped, we are now free to close all parent
2473 * fds that are *not* child fds.
2474 */
2475 for (i = 0; i < j->preserved_fd_count; i++) {
2476 closeable = true;
2477 for (i2 = 0; i2 < j->preserved_fd_count; i2++) {
2478 closeable &= j->preserved_fds[i].parent_fd !=
2479 j->preserved_fds[i2].child_fd;
2480 }
2481 if (closeable)
2482 close(j->preserved_fds[i].parent_fd);
2483 }
2484 return 0;
2485}
2486
Dylan Reidacfb8be2017-08-25 12:56:51 -07002487/*
2488 * Structure that specifies how to start a minijail.
2489 *
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002490 * filename - The program to exec in the child. Required if |exec_in_child| = 1.
2491 * argv - Arguments for the child program. Required if |exec_in_child| = 1.
2492 * envp - Environment for the child program. Available if |exec_in_child| = 1.
2493 Currently only honored if |use_preload| = 0 and non-NULL.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002494 * use_preload - If true use LD_PRELOAD.
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002495 * exec_in_child - If true, run |filename|. Otherwise, the child will return to
Dylan Reid0412dcc2017-08-24 11:33:15 -07002496 * the caller.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002497 */
2498struct minijail_run_config {
2499 const char *filename;
2500 char *const *argv;
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002501 char *const *envp;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002502 int use_preload;
Dylan Reid0412dcc2017-08-24 11:33:15 -07002503 int exec_in_child;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002504};
2505
2506/*
2507 * Set of pointers to fill with values from minijail_run.
2508 * All arguments are allowed to be NULL if unused.
2509 *
2510 * pstdin_fd - Filled with stdin pipe if non-NULL.
2511 * pstdout_fd - Filled with stdout pipe if non-NULL.
2512 * pstderr_fd - Filled with stderr pipe if non-NULL.
2513 * pchild_pid - Filled with the pid of the child process if non-NULL.
2514 */
2515struct minijail_run_status {
2516 int *pstdin_fd;
2517 int *pstdout_fd;
2518 int *pstderr_fd;
2519 pid_t *pchild_pid;
2520};
2521
Dylan Reid18c49c82017-08-25 14:52:27 -07002522static int minijail_run_internal(struct minijail *j,
2523 const struct minijail_run_config *config,
2524 struct minijail_run_status *status_out);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002525
Will Drewry6ac91122011-10-21 16:38:58 -05002526int API minijail_run(struct minijail *j, const char *filename,
2527 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002528{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002529 struct minijail_run_config config = {
2530 .filename = filename,
2531 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002532 .envp = NULL,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002533 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002534 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002535 };
2536 struct minijail_run_status status = {};
2537 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002538}
2539
2540int API minijail_run_pid(struct minijail *j, const char *filename,
2541 char *const argv[], pid_t *pchild_pid)
2542{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002543 struct minijail_run_config config = {
2544 .filename = filename,
2545 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002546 .envp = NULL,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002547 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002548 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002549 };
2550 struct minijail_run_status status = {
2551 .pchild_pid = pchild_pid,
2552 };
2553 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002554}
2555
2556int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002557 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002558{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002559 struct minijail_run_config config = {
2560 .filename = filename,
2561 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002562 .envp = NULL,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002563 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002564 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002565 };
2566 struct minijail_run_status status = {
2567 .pstdin_fd = pstdin_fd,
2568 };
2569 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002570}
2571
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002572int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002573 char *const argv[], pid_t *pchild_pid,
2574 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002575{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002576 struct minijail_run_config config = {
2577 .filename = filename,
2578 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002579 .envp = NULL,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002580 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002581 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002582 };
2583 struct minijail_run_status status = {
2584 .pstdin_fd = pstdin_fd,
2585 .pstdout_fd = pstdout_fd,
2586 .pstderr_fd = pstderr_fd,
2587 .pchild_pid = pchild_pid,
2588 };
2589 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002590}
2591
2592int API minijail_run_no_preload(struct minijail *j, const char *filename,
2593 char *const argv[])
2594{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002595 struct minijail_run_config config = {
2596 .filename = filename,
2597 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002598 .envp = NULL,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002599 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002600 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002601 };
2602 struct minijail_run_status status = {};
2603 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002604}
2605
Samuel Tan63187f42015-10-16 13:01:53 -07002606int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002607 const char *filename,
2608 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002609 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002610 int *pstdin_fd,
2611 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002612 int *pstderr_fd)
2613{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002614 struct minijail_run_config config = {
2615 .filename = filename,
2616 .argv = argv,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002617 .envp = NULL,
2618 .use_preload = false,
2619 .exec_in_child = true,
2620 };
2621 struct minijail_run_status status = {
2622 .pstdin_fd = pstdin_fd,
2623 .pstdout_fd = pstdout_fd,
2624 .pstderr_fd = pstderr_fd,
2625 .pchild_pid = pchild_pid,
2626 };
2627 return minijail_run_internal(j, &config, &status);
2628}
2629
2630int API minijail_run_env_pid_pipes_no_preload(struct minijail *j,
2631 const char *filename,
2632 char *const argv[],
2633 char *const envp[],
2634 pid_t *pchild_pid, int *pstdin_fd,
2635 int *pstdout_fd, int *pstderr_fd)
2636{
2637 struct minijail_run_config config = {
2638 .filename = filename,
2639 .argv = argv,
2640 .envp = envp,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002641 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002642 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002643 };
2644 struct minijail_run_status status = {
2645 .pstdin_fd = pstdin_fd,
2646 .pstdout_fd = pstdout_fd,
2647 .pstderr_fd = pstderr_fd,
2648 .pchild_pid = pchild_pid,
2649 };
2650 return minijail_run_internal(j, &config, &status);
Samuel Tan63187f42015-10-16 13:01:53 -07002651}
2652
Dylan Reid0412dcc2017-08-24 11:33:15 -07002653pid_t API minijail_fork(struct minijail *j)
2654{
2655 struct minijail_run_config config = {};
2656 struct minijail_run_status status = {};
2657 return minijail_run_internal(j, &config, &status);
2658}
2659
Dylan Reid18c49c82017-08-25 14:52:27 -07002660static int minijail_run_internal(struct minijail *j,
2661 const struct minijail_run_config *config,
2662 struct minijail_run_status *status_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002663{
Elly Jonese1749eb2011-10-07 13:54:59 -04002664 char *oldenv, *oldenv_copy = NULL;
2665 pid_t child_pid;
2666 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002667 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002668 int stdout_fds[2];
2669 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08002670 int child_sync_pipe_fds[2];
2671 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002672 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002673 /* We need to remember this across the minijail_preexec() call. */
2674 int pid_namespace = j->flags.pids;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -07002675 /*
2676 * Create an init process if we are entering a pid namespace, unless the
2677 * user has explicitly opted out by calling minijail_run_as_init().
2678 */
2679 int do_init = j->flags.do_init && !j->flags.run_as_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002680 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002681
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002682 if (use_preload) {
Dylan Reid0412dcc2017-08-24 11:33:15 -07002683 if (j->hooks_head != NULL)
2684 die("Minijail hooks are not supported with LD_PRELOAD");
2685 if (!config->exec_in_child)
2686 die("minijail_fork is not supported with LD_PRELOAD");
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002687 if (config->envp != NULL)
2688 die("cannot pass a new environment with LD_PRELOAD");
Dylan Reid0412dcc2017-08-24 11:33:15 -07002689
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002690 oldenv = getenv(kLdPreloadEnvVar);
2691 if (oldenv) {
2692 oldenv_copy = strdup(oldenv);
2693 if (!oldenv_copy)
2694 return -ENOMEM;
2695 }
2696
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002697 if (setup_preload(j, oldenv))
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002698 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002699 }
Will Drewryf89aef52011-09-16 16:48:57 -05002700
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002701 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002702 if (j->flags.use_caps && j->caps != 0 &&
2703 !j->flags.set_ambient_caps) {
2704 die("non-empty, non-ambient capabilities are not "
2705 "supported without LD_PRELOAD");
2706 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002707 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002708
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002709 if (use_preload) {
2710 /*
2711 * Before we fork(2) and execve(2) the child process, we need
2712 * to open a pipe(2) to send the minijail configuration over.
2713 */
2714 if (setup_pipe(pipe_fds))
2715 return -EFAULT;
2716 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002717
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002718 /*
2719 * If we want to write to the child process' standard input,
2720 * create the pipe(2) now.
2721 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002722 if (status_out->pstdin_fd) {
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002723 if (pipe(stdin_fds))
2724 return -EFAULT;
2725 }
2726
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002727 /*
2728 * If we want to read from the child process' standard output,
2729 * create the pipe(2) now.
2730 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002731 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002732 if (pipe(stdout_fds))
2733 return -EFAULT;
2734 }
2735
2736 /*
2737 * If we want to read from the child process' standard error,
2738 * create the pipe(2) now.
2739 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002740 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002741 if (pipe(stderr_fds))
2742 return -EFAULT;
2743 }
2744
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002745 /*
Daniel Erat2d69add2019-04-23 20:58:53 -07002746 * If the parent process needs to configure the child's runtime
2747 * environment after forking, create a pipe(2) to block the child until
2748 * configuration is done.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002749 */
Daniel Erat2d69add2019-04-23 20:58:53 -07002750 if (j->flags.forward_signals || j->flags.pid_file || j->flags.cgroups ||
2751 j->rlimit_count || j->flags.userns) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002752 sync_child = 1;
2753 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002754 return -EFAULT;
2755 }
2756
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002757 /*
2758 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002759 *
2760 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2761 *
2762 * In multithreaded programs, there are a bunch of locks inside libc,
2763 * some of which may be held by other threads at the time that we call
2764 * minijail_run_pid(). If we call fork(), glibc does its level best to
2765 * ensure that we hold all of these locks before it calls clone()
2766 * internally and drop them after clone() returns, but when we call
2767 * sys_clone(2) directly, all that gets bypassed and we end up with a
2768 * child address space where some of libc's important locks are held by
2769 * other threads (which did not get cloned, and hence will never release
2770 * those locks). This is okay so long as we call exec() immediately
2771 * after, but a bunch of seemingly-innocent libc functions like setenv()
2772 * take locks.
2773 *
2774 * Hence, only call sys_clone() if we need to, in order to get at pid
2775 * namespacing. If we follow this path, the child's address space might
2776 * have broken locks; you may only call functions that do not acquire
2777 * any locks.
2778 *
2779 * Unfortunately, fork() acquires every lock it can get its hands on, as
2780 * previously detailed, so this function is highly likely to deadlock
2781 * later on (see "deadlock here") if we're multithreaded.
2782 *
2783 * We might hack around this by having the clone()d child (init of the
2784 * pid namespace) return directly, rather than leaving the clone()d
2785 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002786 * its fork()ed child return in turn), but that process would be
2787 * crippled with its libc locks potentially broken. We might try
2788 * fork()ing in the parent before we clone() to ensure that we own all
2789 * the locks, but then we have to have the forked child hanging around
2790 * consuming resources (and possibly having file descriptors / shared
2791 * memory regions / etc attached). We'd need to keep the child around to
2792 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002793 *
2794 * TODO(ellyjones): figure out if the "forked child hanging around"
2795 * problem is fixable or not. It would be nice if we worked in this
2796 * case.
2797 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002798 if (pid_namespace) {
2799 int clone_flags = CLONE_NEWPID | SIGCHLD;
2800 if (j->flags.userns)
2801 clone_flags |= CLONE_NEWUSER;
2802 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002803 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002804 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002805 }
Elly Jones761b7412012-06-13 15:49:52 -04002806
Elly Jonese1749eb2011-10-07 13:54:59 -04002807 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002808 if (use_preload) {
2809 free(oldenv_copy);
2810 }
Jorge Lucangeli Obesa45ade82019-09-23 11:36:22 -04002811 if (pid_namespace && errno == EPERM) {
2812 warn("clone(CLONE_NEWPID) failed with EPERM, maybe "
2813 "this process is not running with CAP_SYS_ADMIN?");
2814 }
2815 pdie("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002816 }
Will Drewryf89aef52011-09-16 16:48:57 -05002817
Elly Jonese1749eb2011-10-07 13:54:59 -04002818 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002819 if (use_preload) {
2820 /* Restore parent's LD_PRELOAD. */
2821 if (oldenv_copy) {
2822 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2823 free(oldenv_copy);
2824 } else {
2825 unsetenv(kLdPreloadEnvVar);
2826 }
2827 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002828 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002829
Elly Jonese1749eb2011-10-07 13:54:59 -04002830 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002831
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002832 if (j->flags.forward_signals) {
2833 forward_pid = child_pid;
2834 install_signal_handlers();
2835 }
2836
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002837 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002838 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002839
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002840 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002841 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002842
Dylan Reid0f72ef42017-06-06 15:42:49 -07002843 if (j->rlimit_count)
2844 set_rlimits_or_die(j);
2845
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002846 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002847 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002848
Mike Frysinger902a4492018-12-27 05:22:56 -05002849 if (j->flags.enter_vfs)
2850 close(j->mountns_fd);
2851
2852 if (j->flags.enter_net)
2853 close(j->netns_fd);
2854
Dylan Reidce5b55e2016-01-13 11:04:16 -08002855 if (sync_child)
2856 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002857
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002858 if (use_preload) {
2859 /* Send marshalled minijail. */
2860 close(pipe_fds[0]); /* read endpoint */
2861 ret = minijail_to_fd(j, pipe_fds[1]);
2862 close(pipe_fds[1]); /* write endpoint */
2863 if (ret) {
2864 kill(j->initpid, SIGKILL);
2865 die("failed to send marshalled minijail");
2866 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002867 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002868
Dylan Reidacfb8be2017-08-25 12:56:51 -07002869 if (status_out->pchild_pid)
2870 *status_out->pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002871
2872 /*
2873 * If we want to write to the child process' standard input,
2874 * set up the write end of the pipe.
2875 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002876 if (status_out->pstdin_fd)
2877 *status_out->pstdin_fd =
2878 setup_pipe_end(stdin_fds, 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002879
2880 /*
2881 * If we want to read from the child process' standard output,
2882 * set up the read end of the pipe.
2883 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002884 if (status_out->pstdout_fd)
2885 *status_out->pstdout_fd =
2886 setup_pipe_end(stdout_fds, 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002887
2888 /*
2889 * If we want to read from the child process' standard error,
2890 * set up the read end of the pipe.
2891 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002892 if (status_out->pstderr_fd)
2893 *status_out->pstderr_fd =
2894 setup_pipe_end(stderr_fds, 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002895
Dylan Reid0412dcc2017-08-24 11:33:15 -07002896 /*
2897 * If forking return the child pid, in the normal exec case
2898 * return 0 for success.
2899 */
2900 if (!config->exec_in_child)
2901 return child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04002902 return 0;
2903 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002904 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002905 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002906
Peter Qiu2860c462015-12-16 15:13:06 -08002907 if (j->flags.reset_signal_mask) {
2908 sigset_t signal_mask;
2909 if (sigemptyset(&signal_mask) != 0)
2910 pdie("sigemptyset failed");
2911 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2912 pdie("sigprocmask failed");
2913 }
2914
Luis Hector Chaveza27118a2018-04-04 08:18:01 -07002915 if (j->flags.reset_signal_handlers) {
2916 int signum;
2917 for (signum = 0; signum <= SIGRTMAX; signum++) {
2918 /*
2919 * Ignore EINVAL since some signal numbers in the range
2920 * might not be valid.
2921 */
2922 if (signal(signum, SIG_DFL) == SIG_ERR &&
2923 errno != EINVAL) {
2924 pdie("failed to reset signal %d disposition",
2925 signum);
2926 }
2927 }
2928 }
2929
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002930 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002931 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002932 int inheritable_fds[kMaxInheritableFdsSize];
2933 size_t size = 0;
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002934 size_t i;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002935 if (use_preload) {
2936 inheritable_fds[size++] = pipe_fds[0];
2937 inheritable_fds[size++] = pipe_fds[1];
2938 }
2939 if (sync_child) {
2940 inheritable_fds[size++] = child_sync_pipe_fds[0];
2941 inheritable_fds[size++] = child_sync_pipe_fds[1];
2942 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002943 if (status_out->pstdin_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002944 inheritable_fds[size++] = stdin_fds[0];
2945 inheritable_fds[size++] = stdin_fds[1];
2946 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002947 if (status_out->pstdout_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002948 inheritable_fds[size++] = stdout_fds[0];
2949 inheritable_fds[size++] = stdout_fds[1];
2950 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002951 if (status_out->pstderr_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002952 inheritable_fds[size++] = stderr_fds[0];
2953 inheritable_fds[size++] = stderr_fds[1];
2954 }
Jorge Lucangeli Obes2337f802019-07-18 14:46:03 -04002955
Jorge Lucangeli Obescf3bbea2019-07-24 09:06:40 -04002956 /*
2957 * Preserve namespace file descriptors over the close_open_fds()
2958 * call. These are closed in minijail_enter() so they won't leak
2959 * into the child process.
2960 */
Jorge Lucangeli Obes2337f802019-07-18 14:46:03 -04002961 if (j->flags.enter_vfs)
2962 minijail_preserve_fd(j, j->mountns_fd, j->mountns_fd);
2963 if (j->flags.enter_net)
2964 minijail_preserve_fd(j, j->netns_fd, j->netns_fd);
2965
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002966 for (i = 0; i < j->preserved_fd_count; i++) {
2967 /*
2968 * Preserve all parent_fds. They will be dup2(2)-ed in
2969 * the child later.
2970 */
2971 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
2972 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002973
2974 if (close_open_fds(inheritable_fds, size) < 0)
2975 die("failed to close open file descriptors");
2976 }
2977
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002978 if (redirect_fds(j))
2979 die("failed to set up fd redirections");
2980
Dylan Reidce5b55e2016-01-13 11:04:16 -08002981 if (sync_child)
2982 wait_for_parent_setup(child_sync_pipe_fds);
2983
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002984 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002985 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002986
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002987 /*
2988 * If we want to write to the jailed process' standard input,
2989 * set up the read end of the pipe.
2990 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002991 if (status_out->pstdin_fd) {
François Degrosa8be2c42019-10-01 12:06:42 +10002992 if (dupe_and_close_fd(stdin_fds, 0 /* read end */,
2993 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002994 die("failed to set up stdin pipe");
2995 }
2996
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002997 /*
2998 * If we want to read from the jailed process' standard output,
2999 * set up the write end of the pipe.
3000 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07003001 if (status_out->pstdout_fd) {
François Degrosa8be2c42019-10-01 12:06:42 +10003002 if (dupe_and_close_fd(stdout_fds, 1 /* write end */,
3003 STDOUT_FILENO) < 0)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08003004 die("failed to set up stdout pipe");
3005 }
3006
3007 /*
3008 * If we want to read from the jailed process' standard error,
3009 * set up the write end of the pipe.
3010 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07003011 if (status_out->pstderr_fd) {
François Degrosa8be2c42019-10-01 12:06:42 +10003012 if (dupe_and_close_fd(stderr_fds, 1 /* write end */,
3013 STDERR_FILENO) < 0)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08003014 die("failed to set up stderr pipe");
3015 }
3016
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05003017 /*
Xiyuan Xia9b41e652019-05-23 11:03:04 -07003018 * If any of stdin, stdout, or stderr are TTYs, or setsid flag is
3019 * set, create a new session. This prevents the jailed process from
3020 * using the TIOCSTI ioctl to push characters into the parent process
3021 * terminal's input buffer, therefore escaping the jail.
Stephen Barber5dd5b1b2017-10-16 23:02:39 -07003022 *
3023 * Since it has just forked, the child will not be a process group
3024 * leader, and this call to setsid() should always succeed.
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05003025 */
Xiyuan Xia9b41e652019-05-23 11:03:04 -07003026 if (j->flags.setsid || isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05003027 isatty(STDERR_FILENO)) {
3028 if (setsid() < 0) {
3029 pdie("setsid() failed");
3030 }
3031 }
3032
Dylan Reid791f5772015-09-14 20:02:42 -07003033 /* If running an init program, let it decide when/how to mount /proc. */
3034 if (pid_namespace && !do_init)
3035 j->flags.remount_proc_ro = 0;
3036
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003037 if (use_preload) {
3038 /* Strip out flags that cannot be inherited across execve(2). */
3039 minijail_preexec(j);
3040 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003041 /*
3042 * If not using LD_PRELOAD, do all jailing before execve(2).
3043 * Note that PID namespaces can only be entered on fork(2),
3044 * so that flag is still cleared.
3045 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003046 j->flags.pids = 0;
3047 }
Dylan Reid0412dcc2017-08-24 11:33:15 -07003048
3049 /*
3050 * Jail this process.
3051 * If forking, return.
3052 * If not, execve(2) the target.
3053 */
Elly Jonese1749eb2011-10-07 13:54:59 -04003054 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003055
Dylan Reid0412dcc2017-08-24 11:33:15 -07003056 if (config->exec_in_child && pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05003057 /*
3058 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003059 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003060 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003061 * a child to actually run the program. If |do_init == 0|, we
3062 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04003063 *
3064 * If we're multithreaded, we'll probably deadlock here. See
3065 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04003066 */
3067 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003068 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04003069 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003070 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04003071 /*
3072 * Best effort. Don't bother checking the return value.
3073 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003074 prctl(PR_SET_NAME, "minijail-init");
3075 init(child_pid); /* Never returns. */
3076 }
Elly Jonese1749eb2011-10-07 13:54:59 -04003077 }
Elly Jonescd7a9042011-07-22 13:56:51 -04003078
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07003079 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
3080
Dylan Reid0412dcc2017-08-24 11:33:15 -07003081 if (!config->exec_in_child)
3082 return 0;
3083
Elly Jonesdd3e8512012-01-23 15:13:38 -05003084 /*
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05003085 * If not using LD_PRELOAD, support passing a new environment instead of
3086 * inheriting the parent's.
3087 * When not using LD_PRELOAD there is no need to modify the environment
3088 * to add Minijail-related variables, so passing a new environment is
3089 * fine.
3090 */
3091 char *const *child_env = environ;
3092 if (!use_preload && config->envp != NULL) {
3093 child_env = config->envp;
3094 }
3095
3096 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003097 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04003098 * calling process
3099 * -> execve()-ing process
3100 * If we are:
3101 * calling process
3102 * -> init()-ing process
3103 * -> execve()-ing process
3104 */
François Degros08b10f72019-10-09 12:44:05 +11003105 execve(config->filename, config->argv, child_env);
3106
3107 ret = (errno == ENOENT ? MINIJAIL_ERR_NO_COMMAND : MINIJAIL_ERR_NO_ACCESS);
3108 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003109 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04003110}
3111
Will Drewry6ac91122011-10-21 16:38:58 -05003112int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003113{
François Degros47e63352019-10-01 13:01:53 +10003114 if (j->initpid <= 0)
3115 return -ECHILD;
3116
Elly Jonese1749eb2011-10-07 13:54:59 -04003117 if (kill(j->initpid, SIGTERM))
3118 return -errno;
François Degros47e63352019-10-01 13:01:53 +10003119
3120 return minijail_wait(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003121}
3122
Will Drewry6ac91122011-10-21 16:38:58 -05003123int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003124{
François Degros08b10f72019-10-09 12:44:05 +11003125 if (j->initpid <= 0)
3126 return -ECHILD;
3127
Elly Jonese1749eb2011-10-07 13:54:59 -04003128 int st;
François Degros627deba2019-10-01 12:48:25 +10003129 while (true) {
3130 const int ret = waitpid(j->initpid, &st, 0);
3131 if (ret >= 0)
3132 break;
3133 if (errno != EINTR)
3134 return -errno;
3135 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003136
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003137 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003138 int error_status = st;
3139 if (WIFSIGNALED(st)) {
3140 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07003141 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003142 j->initpid, signum);
3143 /*
3144 * We return MINIJAIL_ERR_JAIL if the process received
3145 * SIGSYS, which happens when a syscall is blocked by
3146 * seccomp filters.
3147 * If not, we do what bash(1) does:
3148 * $? = 128 + signum
3149 */
3150 if (signum == SIGSYS) {
3151 error_status = MINIJAIL_ERR_JAIL;
3152 } else {
François Degros08b10f72019-10-09 12:44:05 +11003153 error_status = MINIJAIL_ERR_SIG_BASE + signum;
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003154 }
3155 }
3156 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003157 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003158
3159 int exit_status = WEXITSTATUS(st);
3160 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07003161 info("child process %d exited with status %d",
3162 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003163
3164 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04003165}
3166
Will Drewry6ac91122011-10-21 16:38:58 -05003167void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003168{
Dylan Reid605ce7f2016-01-19 19:21:00 -08003169 size_t i;
3170
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07003171 if (j->filter_prog) {
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08003172 free(j->filter_prog->filter);
3173 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04003174 }
Mike Frysingerac08a682017-10-10 02:04:50 -04003175 free_mounts_list(j);
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07003176 while (j->hooks_head) {
3177 struct hook *c = j->hooks_head;
3178 j->hooks_head = c->next;
3179 free(c);
3180 }
3181 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04003182 if (j->user)
3183 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08003184 if (j->suppl_gid_list)
3185 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05003186 if (j->chrootdir)
3187 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04003188 if (j->pid_file_path)
3189 free(j->pid_file_path);
3190 if (j->uidmap)
3191 free(j->uidmap);
3192 if (j->gidmap)
3193 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04003194 if (j->hostname)
3195 free(j->hostname);
Luis Hector Chavez9acba452018-10-11 10:13:25 -07003196 if (j->preload_path)
3197 free(j->preload_path);
Andrew Brestickereac28942015-11-11 16:04:46 -08003198 if (j->alt_syscall_table)
3199 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08003200 for (i = 0; i < j->cgroup_count; ++i)
3201 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04003202 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003203}
Luis Hector Chavez114a9302017-09-05 20:36:58 -07003204
3205void API minijail_log_to_fd(int fd, int min_priority)
3206{
3207 init_logging(LOG_TO_FD, fd, min_priority);
3208}