blob: 7a82ccc06e08bd9d7fa041812a53dde7c0eb2c80 [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/*
François Degros664eba72019-11-05 13:18:24 +1100223 * Writes exactly n bytes from buf to file descriptor fd.
224 * Returns 0 on success or a negative error code on error.
225 */
226static int write_exactly(int fd, const void *buf, size_t n)
227{
228 const char *p = buf;
229 while (n > 0) {
230 const ssize_t written = write(fd, p, n);
231 if (written < 0) {
232 if (errno == EINTR)
233 continue;
234
235 return -errno;
236 }
237
238 p += written;
239 n -= written;
240 }
241
242 return 0;
243}
244
Mattias Nissler6123e5a2020-02-11 13:38:03 +0100245/* Closes *pfd and sets it to -1. */
246static void close_and_reset(int *pfd)
247{
248 if (*pfd != -1)
249 close(*pfd);
250 *pfd = -1;
251}
252
François Degros664eba72019-11-05 13:18:24 +1100253/*
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700254 * Strip out flags meant for the parent.
255 * We keep things that are not inherited across execve(2) (e.g. capabilities),
256 * or are easier to set after execve(2) (e.g. seccomp filters).
257 */
258void minijail_preenter(struct minijail *j)
259{
260 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700261 j->flags.enter_vfs = 0;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700262 j->flags.ns_cgroups = 0;
263 j->flags.net = 0;
264 j->flags.uts = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700265 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700266 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800267 j->flags.do_init = 0;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700268 j->flags.run_as_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800269 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800270 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400271 j->flags.forward_signals = 0;
Xiyuan Xia9b41e652019-05-23 11:03:04 -0700272 j->flags.setsid = 0;
Mike Frysinger785b1c32018-02-23 15:47:24 -0500273 j->remount_mode = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700274}
275
276/*
277 * Strip out flags meant for the child.
278 * We keep things that are inherited across execve(2).
279 */
280void minijail_preexec(struct minijail *j)
281{
282 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700283 int enter_vfs = j->flags.enter_vfs;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700284 int ns_cgroups = j->flags.ns_cgroups;
285 int net = j->flags.net;
286 int uts = j->flags.uts;
Dylan Reid791f5772015-09-14 20:02:42 -0700287 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800288 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700289 if (j->user)
290 free(j->user);
291 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800292 if (j->suppl_gid_list)
293 free(j->suppl_gid_list);
294 j->suppl_gid_list = NULL;
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700295 if (j->preload_path)
296 free(j->preload_path);
297 j->preload_path = NULL;
Mike Frysingerac08a682017-10-10 02:04:50 -0400298 free_mounts_list(j);
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700299 memset(&j->flags, 0, sizeof(j->flags));
300 /* Now restore anything we meant to keep. */
301 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700302 j->flags.enter_vfs = enter_vfs;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700303 j->flags.ns_cgroups = ns_cgroups;
304 j->flags.net = net;
305 j->flags.uts = uts;
Dylan Reid791f5772015-09-14 20:02:42 -0700306 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800307 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700308 /* Note, |pids| will already have been used before this call. */
309}
310
311/* Minijail API. */
312
Will Drewry6ac91122011-10-21 16:38:58 -0500313struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400314{
Mike Frysinger785b1c32018-02-23 15:47:24 -0500315 struct minijail *j = calloc(1, sizeof(struct minijail));
316 j->remount_mode = MS_PRIVATE;
317 return j;
Elly Jonescd7a9042011-07-22 13:56:51 -0400318}
319
Will Drewry6ac91122011-10-21 16:38:58 -0500320void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400321{
322 if (uid == 0)
323 die("useless change to uid 0");
324 j->uid = uid;
325 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400326}
327
Will Drewry6ac91122011-10-21 16:38:58 -0500328void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400329{
330 if (gid == 0)
331 die("useless change to gid 0");
332 j->gid = gid;
333 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400334}
335
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800336void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
337 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800338{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800339 size_t i;
340
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500341 if (j->flags.inherit_suppl_gids)
342 die("cannot inherit *and* set supplementary groups");
343 if (j->flags.keep_suppl_gids)
344 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800345
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800346 if (size == 0) {
347 /* Clear supplementary groups. */
348 j->suppl_gid_list = NULL;
349 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100350 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800351 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800352 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800353
354 /* Copy the gid_t array. */
355 j->suppl_gid_list = calloc(size, sizeof(gid_t));
356 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800357 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800358 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800359 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800360 j->suppl_gid_list[i] = list[i];
361 }
362 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100363 j->flags.set_suppl_gids = 1;
364}
365
366void API minijail_keep_supplementary_gids(struct minijail *j) {
367 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800368}
369
Will Drewry6ac91122011-10-21 16:38:58 -0500370int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400371{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700372 uid_t uid;
373 gid_t gid;
374 int rc = lookup_user(user, &uid, &gid);
375 if (rc)
376 return rc;
377 minijail_change_uid(j, uid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400378 j->user = strdup(user);
379 if (!j->user)
380 return -ENOMEM;
Luis Hector Chavez71323552017-09-05 09:17:22 -0700381 j->usergid = gid;
Elly Jonese1749eb2011-10-07 13:54:59 -0400382 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400383}
384
Will Drewry6ac91122011-10-21 16:38:58 -0500385int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400386{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700387 gid_t gid;
388 int rc = lookup_group(group, &gid);
389 if (rc)
390 return rc;
391 minijail_change_gid(j, gid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400392 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400393}
394
Will Drewry6ac91122011-10-21 16:38:58 -0500395void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400396{
397 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400398}
399
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700400void API minijail_no_new_privs(struct minijail *j)
401{
402 j->flags.no_new_privs = 1;
403}
404
Will Drewry6ac91122011-10-21 16:38:58 -0500405void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400406{
407 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500408}
409
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400410void API minijail_set_seccomp_filter_tsync(struct minijail *j)
411{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400412 if (j->filter_len > 0 && j->filter_prog != NULL) {
413 die("minijail_set_seccomp_filter_tsync() must be called "
414 "before minijail_parse_seccomp_filters()");
415 }
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400416
417 if (j->flags.seccomp_filter_logging && !seccomp_ret_log_available()) {
418 /*
419 * If SECCOMP_RET_LOG is not available, we don't want to use
420 * SECCOMP_RET_TRAP to both kill the entire process and report
421 * failing syscalls, since it will be brittle. Just bail.
422 */
Mike Frysinger52f6ada2019-06-26 16:59:36 -0400423 die("SECCOMP_RET_LOG not available, cannot use logging with "
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400424 "thread sync at the same time");
425 }
426
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400427 j->flags.seccomp_filter_tsync = 1;
428}
429
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700430void API minijail_log_seccomp_filter_failures(struct minijail *j)
431{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400432 if (j->filter_len > 0 && j->filter_prog != NULL) {
433 die("minijail_log_seccomp_filter_failures() must be called "
434 "before minijail_parse_seccomp_filters()");
435 }
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400436
437 if (j->flags.seccomp_filter_tsync && !seccomp_ret_log_available()) {
438 /*
439 * If SECCOMP_RET_LOG is not available, we don't want to use
440 * SECCOMP_RET_TRAP to both kill the entire process and report
441 * failing syscalls, since it will be brittle. Just bail.
442 */
Mike Frysinger52f6ada2019-06-26 16:59:36 -0400443 die("SECCOMP_RET_LOG not available, cannot use thread sync with "
444 "logging at the same time");
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400445 }
446
447 if (debug_logging_allowed()) {
448 j->flags.seccomp_filter_logging = 1;
449 } else {
450 warn("non-debug build: ignoring request to enable seccomp "
451 "logging");
452 }
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700453}
454
Will Drewry6ac91122011-10-21 16:38:58 -0500455void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400456{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800457 /*
458 * 'minijail_use_caps' configures a runtime-capabilities-only
459 * environment, including a bounding set matching the thread's runtime
460 * (permitted|inheritable|effective) sets.
461 * Therefore, it will override any existing bounding set configurations
462 * since the latter would allow gaining extra runtime capabilities from
463 * file capabilities.
464 */
465 if (j->flags.capbset_drop) {
466 warn("overriding bounding set configuration");
467 j->cap_bset = 0;
468 j->flags.capbset_drop = 0;
469 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400470 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800471 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400472}
473
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800474void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
475{
476 if (j->flags.use_caps) {
477 /*
478 * 'minijail_use_caps' will have already configured a capability
479 * bounding set matching the (permitted|inheritable|effective)
480 * sets. Abort if the user tries to configure a separate
481 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
482 * are mutually exclusive.
483 */
484 die("runtime capabilities already configured, can't drop "
485 "bounding set separately");
486 }
487 j->cap_bset = capmask;
488 j->flags.capbset_drop = 1;
489}
490
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400491void API minijail_set_ambient_caps(struct minijail *j)
492{
493 j->flags.set_ambient_caps = 1;
494}
495
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800496void API minijail_reset_signal_mask(struct minijail *j)
497{
Peter Qiu2860c462015-12-16 15:13:06 -0800498 j->flags.reset_signal_mask = 1;
499}
500
Luis Hector Chaveza27118a2018-04-04 08:18:01 -0700501void API minijail_reset_signal_handlers(struct minijail *j)
502{
503 j->flags.reset_signal_handlers = 1;
504}
505
Will Drewry6ac91122011-10-21 16:38:58 -0500506void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400507{
508 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400509}
510
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700511void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
512{
Mike Frysinger902a4492018-12-27 05:22:56 -0500513 /* Note: Do not use O_CLOEXEC here. We'll close it after we use it. */
514 int ns_fd = open(ns_path, O_RDONLY);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700515 if (ns_fd < 0) {
516 pdie("failed to open namespace '%s'", ns_path);
517 }
518 j->mountns_fd = ns_fd;
519 j->flags.enter_vfs = 1;
520}
521
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800522void API minijail_new_session_keyring(struct minijail *j)
523{
524 j->flags.new_session_keyring = 1;
525}
526
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700527void API minijail_skip_setting_securebits(struct minijail *j,
528 uint64_t securebits_skip_mask)
529{
530 j->securebits_skip_mask = securebits_skip_mask;
531}
532
Mike Frysinger785b1c32018-02-23 15:47:24 -0500533void API minijail_remount_mode(struct minijail *j, unsigned long mode)
534{
535 j->remount_mode = mode;
536}
537
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800538void API minijail_skip_remount_private(struct minijail *j)
539{
Mike Frysinger785b1c32018-02-23 15:47:24 -0500540 j->remount_mode = 0;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800541}
542
Will Drewry6ac91122011-10-21 16:38:58 -0500543void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400544{
Elly Jonese58176c2012-01-23 11:46:17 -0500545 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700546 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400547 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800548 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400549}
550
Jorge Lucangeli Obes2fa96d12019-02-05 10:51:57 -0500551void API minijail_namespace_pids_rw_proc(struct minijail *j)
552{
553 j->flags.vfs = 1;
554 j->flags.pids = 1;
555 j->flags.do_init = 1;
556}
557
Dylan Reidf7942472015-11-18 17:55:26 -0800558void API minijail_namespace_ipc(struct minijail *j)
559{
560 j->flags.ipc = 1;
561}
562
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400563void API minijail_namespace_uts(struct minijail *j)
564{
565 j->flags.uts = 1;
566}
567
568int API minijail_namespace_set_hostname(struct minijail *j, const char *name)
569{
570 if (j->hostname)
571 return -EINVAL;
572 minijail_namespace_uts(j);
573 j->hostname = strdup(name);
574 if (!j->hostname)
575 return -ENOMEM;
576 return 0;
577}
578
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400579void API minijail_namespace_net(struct minijail *j)
580{
581 j->flags.net = 1;
582}
583
Dylan Reid1102f5a2015-09-15 11:52:20 -0700584void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
585{
Mike Frysinger902a4492018-12-27 05:22:56 -0500586 /* Note: Do not use O_CLOEXEC here. We'll close it after we use it. */
587 int ns_fd = open(ns_path, O_RDONLY);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700588 if (ns_fd < 0) {
589 pdie("failed to open namespace '%s'", ns_path);
590 }
591 j->netns_fd = ns_fd;
592 j->flags.enter_net = 1;
593}
594
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700595void API minijail_namespace_cgroups(struct minijail *j)
596{
597 j->flags.ns_cgroups = 1;
598}
599
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700600void API minijail_close_open_fds(struct minijail *j)
601{
602 j->flags.close_open_fds = 1;
603}
604
Dylan Reid791f5772015-09-14 20:02:42 -0700605void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400606{
607 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700608 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400609}
610
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800611void API minijail_namespace_user(struct minijail *j)
612{
613 j->flags.userns = 1;
614}
615
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400616void API minijail_namespace_user_disable_setgroups(struct minijail *j)
617{
618 j->flags.disable_setgroups = 1;
619}
620
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800621int API minijail_uidmap(struct minijail *j, const char *uidmap)
622{
623 j->uidmap = strdup(uidmap);
624 if (!j->uidmap)
625 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800626 char *ch;
627 for (ch = j->uidmap; *ch; ch++) {
628 if (*ch == ',')
629 *ch = '\n';
630 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800631 return 0;
632}
633
634int API minijail_gidmap(struct minijail *j, const char *gidmap)
635{
636 j->gidmap = strdup(gidmap);
637 if (!j->gidmap)
638 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800639 char *ch;
640 for (ch = j->gidmap; *ch; ch++) {
641 if (*ch == ',')
642 *ch = '\n';
643 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800644 return 0;
645}
646
Will Drewry6ac91122011-10-21 16:38:58 -0500647void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400648{
Lutz Justen13807cb2017-01-03 17:11:55 +0100649 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400650}
651
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800652void API minijail_run_as_init(struct minijail *j)
653{
654 /*
655 * Since the jailed program will become 'init' in the new PID namespace,
656 * Minijail does not need to fork an 'init' process.
657 */
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700658 j->flags.run_as_init = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800659}
660
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700661int API minijail_enter_chroot(struct minijail *j, const char *dir)
662{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400663 if (j->chrootdir)
664 return -EINVAL;
665 j->chrootdir = strdup(dir);
666 if (!j->chrootdir)
667 return -ENOMEM;
668 j->flags.chroot = 1;
669 return 0;
670}
671
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800672int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
673{
674 if (j->chrootdir)
675 return -EINVAL;
676 j->chrootdir = strdup(dir);
677 if (!j->chrootdir)
678 return -ENOMEM;
679 j->flags.pivot_root = 1;
680 return 0;
681}
682
Dylan Reida14e08d2015-10-22 21:05:29 -0700683char API *minijail_get_original_path(struct minijail *j,
684 const char *path_inside_chroot)
685{
Dylan Reid648b2202015-10-23 00:50:00 -0700686 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700687
Dylan Reid648b2202015-10-23 00:50:00 -0700688 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700689 while (b) {
690 /*
691 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700692 * mount, then the original path is exactly the source of
693 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700694 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700695 * mount source = /some/path/exe, mount dest =
696 * /chroot/path/exe Then when getting the original path of
697 * "/chroot/path/exe", the source of that mount,
698 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700699 */
700 if (!strcmp(b->dest, path_inside_chroot))
701 return strdup(b->src);
702
703 /*
704 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700705 * mount, take the suffix of the chroot path relative to the
706 * mount destination path, and append it to the mount source
707 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700708 */
709 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
710 const char *relative_path =
711 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400712 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700713 }
714 b = b->next;
715 }
716
717 /* If there is a chroot path, append |path_inside_chroot| to that. */
718 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400719 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700720
721 /* No chroot, so the path outside is the same as it is inside. */
722 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700723}
724
Martin Pelikánab9eb442017-01-25 11:53:58 +1100725size_t minijail_get_tmpfs_size(const struct minijail *j)
726{
727 return j->tmpfs_size;
728}
729
Mike Frysinger33ffef32017-01-13 19:53:19 -0500730void API minijail_mount_dev(struct minijail *j)
731{
732 j->flags.mount_dev = 1;
733}
734
Lee Campbell11af0622014-05-22 12:36:04 -0700735void API minijail_mount_tmp(struct minijail *j)
736{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100737 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
738}
739
740void API minijail_mount_tmp_size(struct minijail *j, size_t size)
741{
742 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700743 j->flags.mount_tmp = 1;
744}
745
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800746int API minijail_write_pid_file(struct minijail *j, const char *path)
747{
748 j->pid_file_path = strdup(path);
749 if (!j->pid_file_path)
750 return -ENOMEM;
751 j->flags.pid_file = 1;
752 return 0;
753}
754
Dylan Reid605ce7f2016-01-19 19:21:00 -0800755int API minijail_add_to_cgroup(struct minijail *j, const char *path)
756{
757 if (j->cgroup_count >= MAX_CGROUPS)
758 return -ENOMEM;
759 j->cgroups[j->cgroup_count] = strdup(path);
760 if (!j->cgroups[j->cgroup_count])
761 return -ENOMEM;
762 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800763 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800764 return 0;
765}
766
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800767int API minijail_rlimit(struct minijail *j, int type, rlim_t cur, rlim_t max)
Dylan Reid0f72ef42017-06-06 15:42:49 -0700768{
769 size_t i;
770
771 if (j->rlimit_count >= MAX_RLIMITS)
772 return -ENOMEM;
773 /* It's an error if the caller sets the same rlimit multiple times. */
774 for (i = 0; i < j->rlimit_count; i++) {
775 if (j->rlimits[i].type == type)
776 return -EEXIST;
777 }
778
779 j->rlimits[j->rlimit_count].type = type;
780 j->rlimits[j->rlimit_count].cur = cur;
781 j->rlimits[j->rlimit_count].max = max;
782 j->rlimit_count++;
783 return 0;
784}
785
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400786int API minijail_forward_signals(struct minijail *j)
787{
788 j->flags.forward_signals = 1;
789 return 0;
790}
791
Xiyuan Xia9b41e652019-05-23 11:03:04 -0700792int API minijail_create_session(struct minijail *j) {
793 j->flags.setsid = 1;
794 return 0;
795}
796
Dylan Reid81e23972016-05-18 14:06:35 -0700797int API minijail_mount_with_data(struct minijail *j, const char *src,
798 const char *dest, const char *type,
799 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700800{
Dylan Reid648b2202015-10-23 00:50:00 -0700801 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400802
803 if (*dest != '/')
804 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700805 m = calloc(1, sizeof(*m));
806 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400807 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700808 m->dest = strdup(dest);
809 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400810 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700811 m->src = strdup(src);
812 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400813 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700814 m->type = strdup(type);
815 if (!m->type)
816 goto error;
Mike Frysingerb7803c82018-08-23 15:43:15 -0400817
818 if (!data || !data[0]) {
819 /*
820 * Set up secure defaults for certain filesystems. Adding this
821 * fs-specific logic here kind of sucks, but considering how
822 * people use these in practice, it's probably OK. If they want
823 * the kernel defaults, they can pass data="" instead of NULL.
824 */
825 if (!strcmp(type, "tmpfs")) {
826 /* tmpfs defaults to mode=1777 and size=50%. */
827 data = "mode=0755,size=10M";
828 }
829 }
Dylan Reid81e23972016-05-18 14:06:35 -0700830 if (data) {
831 m->data = strdup(data);
832 if (!m->data)
833 goto error;
834 m->has_data = 1;
835 }
Mike Frysingercb8674d2018-08-12 00:53:35 -0400836
837 /* If they don't specify any flags, default to secure ones. */
838 if (flags == 0)
839 flags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Dylan Reid648b2202015-10-23 00:50:00 -0700840 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400841
Elly Jonesdd3e8512012-01-23 15:13:38 -0500842 /*
Jorge Lucangeli Obes0a0514c2020-01-03 11:18:32 -0500843 * Unless asked to enter an existing namespace, force vfs namespacing
844 * so the mounts don't leak out into the containing vfs namespace.
845 * If Minijail is being asked to enter the root vfs namespace this will
846 * leak mounts, but it's unlikely that the user would ask to do that by
847 * mistake.
Elly Jones51a5b6c2011-10-12 19:09:26 -0400848 */
Jorge Lucangeli Obes0a0514c2020-01-03 11:18:32 -0500849 if (!j->flags.enter_vfs)
850 minijail_namespace_vfs(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400851
Dylan Reid648b2202015-10-23 00:50:00 -0700852 if (j->mounts_tail)
853 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400854 else
Dylan Reid648b2202015-10-23 00:50:00 -0700855 j->mounts_head = m;
856 j->mounts_tail = m;
857 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400858
859 return 0;
860
861error:
Dylan Reid81e23972016-05-18 14:06:35 -0700862 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700863 free(m->src);
864 free(m->dest);
865 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400866 return -ENOMEM;
867}
868
Dylan Reid81e23972016-05-18 14:06:35 -0700869int API minijail_mount(struct minijail *j, const char *src, const char *dest,
870 const char *type, unsigned long flags)
871{
872 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
873}
874
Dylan Reid648b2202015-10-23 00:50:00 -0700875int API minijail_bind(struct minijail *j, const char *src, const char *dest,
876 int writeable)
877{
878 unsigned long flags = MS_BIND;
879
880 if (!writeable)
881 flags |= MS_RDONLY;
882
883 return minijail_mount(j, src, dest, "", flags);
884}
885
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700886int API minijail_add_hook(struct minijail *j, minijail_hook_t hook,
887 void *payload, minijail_hook_event_t event)
888{
889 struct hook *c;
890
891 if (hook == NULL)
892 return -EINVAL;
893 if (event >= MINIJAIL_HOOK_EVENT_MAX)
894 return -EINVAL;
895 c = calloc(1, sizeof(*c));
896 if (!c)
897 return -ENOMEM;
898
899 c->hook = hook;
900 c->payload = payload;
901 c->event = event;
902
903 if (j->hooks_tail)
904 j->hooks_tail->next = c;
905 else
906 j->hooks_head = c;
907 j->hooks_tail = c;
908
909 return 0;
910}
911
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700912int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd)
913{
914 if (parent_fd < 0 || child_fd < 0)
915 return -EINVAL;
916 if (j->preserved_fd_count >= MAX_PRESERVED_FDS)
917 return -ENOMEM;
918 j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd;
919 j->preserved_fds[j->preserved_fd_count].child_fd = child_fd;
920 j->preserved_fd_count++;
921 return 0;
922}
923
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700924int API minijail_set_preload_path(struct minijail *j, const char *preload_path)
925{
926 if (j->preload_path)
927 return -EINVAL;
928 j->preload_path = strdup(preload_path);
929 if (!j->preload_path)
930 return -ENOMEM;
931 return 0;
932}
933
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400934static void clear_seccomp_options(struct minijail *j)
935{
936 j->flags.seccomp_filter = 0;
937 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400938 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400939 j->filter_len = 0;
940 j->filter_prog = NULL;
941 j->flags.no_new_privs = 0;
942}
943
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700944static int seccomp_should_use_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400945{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400946 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400947 /*
948 * |errno| will be set to EINVAL when seccomp has not been
949 * compiled into the kernel. On certain platforms and kernel
950 * versions this is not a fatal failure. In that case, and only
951 * in that case, disable seccomp and skip loading the filters.
952 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400953 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400954 warn("not loading seccomp filters, seccomp filter not "
955 "supported");
956 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400957 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700958 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400959 /*
960 * If |errno| != EINVAL or seccomp_can_softfail() is false,
961 * we can proceed. Worst case scenario minijail_enter() will
962 * abort() if seccomp fails.
963 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700964 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400965 if (j->flags.seccomp_filter_tsync) {
966 /* Are the seccomp(2) syscall and the TSYNC option supported? */
967 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
968 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
969 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400970 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
971 warn("seccomp(2) syscall not supported");
972 clear_seccomp_options(j);
973 return 0;
974 } else if (saved_errno == EINVAL &&
975 seccomp_can_softfail()) {
976 warn(
977 "seccomp filter thread sync not supported");
978 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400979 return 0;
980 }
981 /*
982 * Similar logic here. If seccomp_can_softfail() is
983 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
984 * we can proceed. Worst case scenario minijail_enter()
985 * will abort() if seccomp or TSYNC fail.
986 */
987 }
988 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400989 return 1;
990}
991
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700992static int set_seccomp_filters_internal(struct minijail *j,
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400993 const struct sock_fprog *filter,
994 bool owned)
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700995{
996 struct sock_fprog *fprog;
997
998 if (owned) {
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400999 /*
1000 * If |owned| is true, it's OK to cast away the const-ness since
1001 * we'll own the pointer going forward.
1002 */
1003 fprog = (struct sock_fprog *)filter;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001004 } else {
1005 fprog = malloc(sizeof(struct sock_fprog));
1006 if (!fprog)
1007 return -ENOMEM;
1008 fprog->len = filter->len;
1009 fprog->filter = malloc(sizeof(struct sock_filter) * fprog->len);
1010 if (!fprog->filter) {
1011 free(fprog);
1012 return -ENOMEM;
1013 }
1014 memcpy(fprog->filter, filter->filter,
1015 sizeof(struct sock_filter) * fprog->len);
1016 }
1017
1018 if (j->filter_prog) {
1019 free(j->filter_prog->filter);
1020 free(j->filter_prog);
1021 }
1022
1023 j->filter_len = fprog->len;
1024 j->filter_prog = fprog;
1025 return 0;
1026}
1027
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001028static int parse_seccomp_filters(struct minijail *j, const char *filename,
1029 FILE *policy_file)
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001030{
1031 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001032 if (!fprog)
1033 return -ENOMEM;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001034
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001035 struct filter_options filteropts;
1036
1037 /*
1038 * Figure out filter options.
1039 * Allow logging?
1040 */
1041 filteropts.allow_logging =
1042 debug_logging_allowed() && j->flags.seccomp_filter_logging;
1043
1044 /* What to do on a blocked system call? */
1045 if (filteropts.allow_logging) {
1046 if (seccomp_ret_log_available())
1047 filteropts.action = ACTION_RET_LOG;
1048 else
1049 filteropts.action = ACTION_RET_TRAP;
1050 } else {
1051 if (j->flags.seccomp_filter_tsync)
1052 filteropts.action = ACTION_RET_TRAP;
1053 else
1054 filteropts.action = ACTION_RET_KILL;
1055 }
1056
1057 /*
1058 * If SECCOMP_RET_LOG is not available, need to allow extra syscalls
1059 * for logging.
1060 */
1061 filteropts.allow_syscalls_for_logging =
1062 filteropts.allow_logging && !seccomp_ret_log_available();
1063
Jorge Lucangeli Obese1a86892019-06-10 16:17:03 -04001064 if (compile_filter(filename, policy_file, fprog, &filteropts)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001065 free(fprog);
1066 return -1;
1067 }
1068
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001069 return set_seccomp_filters_internal(j, fprog, true /* owned */);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001070}
1071
1072void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
1073{
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001074 if (!seccomp_should_use_filters(j))
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001075 return;
1076
Luis Hector Chaveza30a2062018-07-12 21:10:33 -07001077 FILE *file = fopen(path, "re");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001078 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -07001079 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -04001080 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001081
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001082 if (parse_seccomp_filters(j, path, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -07001083 die("failed to compile seccomp filter BPF program in '%s'",
1084 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001085 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001086 fclose(file);
1087}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001088
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001089void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
1090{
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001091 char *fd_path, *path;
1092 FILE *file;
1093
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001094 if (!seccomp_should_use_filters(j))
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001095 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001096
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001097 file = fdopen(fd, "r");
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001098 if (!file) {
1099 pdie("failed to associate stream with fd %d", fd);
1100 }
1101
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001102 if (asprintf(&fd_path, "/proc/self/fd/%d", fd) == -1)
1103 pdie("failed to create path for fd %d", fd);
1104 path = realpath(fd_path, NULL);
1105 if (path == NULL)
1106 pwarn("failed to get path of fd %d", fd);
1107 free(fd_path);
1108
1109 if (parse_seccomp_filters(j, path ? path : "<fd>", file) != 0) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001110 die("failed to compile seccomp filter BPF program from fd %d",
1111 fd);
1112 }
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001113 free(path);
Elly Jonese1749eb2011-10-07 13:54:59 -04001114 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -05001115}
1116
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001117void API minijail_set_seccomp_filters(struct minijail *j,
1118 const struct sock_fprog *filter)
1119{
1120 if (!seccomp_should_use_filters(j))
1121 return;
1122
1123 if (j->flags.seccomp_filter_logging) {
1124 die("minijail_log_seccomp_filter_failures() is incompatible "
1125 "with minijail_set_seccomp_filters()");
1126 }
1127
1128 /*
1129 * set_seccomp_filters_internal() can only fail with ENOMEM.
1130 * Furthermore, since we won't own the incoming filter, it will not be
1131 * modified.
1132 */
1133 if (set_seccomp_filters_internal(j, filter, false /* owned */) < 0) {
1134 die("failed to set seccomp filter");
1135 }
1136}
1137
Andrew Brestickereac28942015-11-11 16:04:46 -08001138int API minijail_use_alt_syscall(struct minijail *j, const char *table)
1139{
1140 j->alt_syscall_table = strdup(table);
1141 if (!j->alt_syscall_table)
1142 return -ENOMEM;
1143 j->flags.alt_syscall = 1;
1144 return 0;
1145}
1146
Will Drewryf89aef52011-09-16 16:48:57 -05001147struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -04001148 size_t available;
1149 size_t total;
1150 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -05001151};
1152
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001153void marshal_state_init(struct marshal_state *state, char *buf,
1154 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -04001155{
1156 state->available = available;
1157 state->buf = buf;
1158 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001159}
1160
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001161void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -04001162{
1163 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -05001164
Elly Jonese1749eb2011-10-07 13:54:59 -04001165 /* Up to |available| will be written. */
1166 if (copy_len) {
1167 memcpy(state->buf, src, copy_len);
1168 state->buf += copy_len;
1169 state->available -= copy_len;
1170 }
1171 /* |total| will contain the expected length. */
1172 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -05001173}
1174
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001175void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -07001176{
1177 marshal_append(state, m->src, strlen(m->src) + 1);
1178 marshal_append(state, m->dest, strlen(m->dest) + 1);
1179 marshal_append(state, m->type, strlen(m->type) + 1);
1180 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
1181 if (m->has_data)
1182 marshal_append(state, m->data, strlen(m->data) + 1);
1183 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
1184}
1185
Will Drewry6ac91122011-10-21 16:38:58 -05001186void minijail_marshal_helper(struct marshal_state *state,
1187 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001188{
Dylan Reid648b2202015-10-23 00:50:00 -07001189 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001190 size_t i;
1191
Elly Jonese1749eb2011-10-07 13:54:59 -04001192 marshal_append(state, (char *)j, sizeof(*j));
1193 if (j->user)
1194 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001195 if (j->suppl_gid_list) {
1196 marshal_append(state, j->suppl_gid_list,
1197 j->suppl_gid_count * sizeof(gid_t));
1198 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001199 if (j->chrootdir)
1200 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001201 if (j->hostname)
1202 marshal_append(state, j->hostname, strlen(j->hostname) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -08001203 if (j->alt_syscall_table) {
1204 marshal_append(state, j->alt_syscall_table,
1205 strlen(j->alt_syscall_table) + 1);
1206 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001207 if (j->flags.seccomp_filter && j->filter_prog) {
1208 struct sock_fprog *fp = j->filter_prog;
1209 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001210 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -04001211 }
Dylan Reid648b2202015-10-23 00:50:00 -07001212 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001213 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001214 }
Dylan Reid605ce7f2016-01-19 19:21:00 -08001215 for (i = 0; i < j->cgroup_count; ++i)
1216 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -05001217}
1218
Will Drewry6ac91122011-10-21 16:38:58 -05001219size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001220{
1221 struct marshal_state state;
1222 marshal_state_init(&state, NULL, 0);
1223 minijail_marshal_helper(&state, j);
1224 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -05001225}
1226
Elly Jonese1749eb2011-10-07 13:54:59 -04001227int minijail_marshal(const struct minijail *j, char *buf, size_t available)
1228{
1229 struct marshal_state state;
1230 marshal_state_init(&state, buf, available);
1231 minijail_marshal_helper(&state, j);
1232 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -05001233}
1234
Elly Jonese1749eb2011-10-07 13:54:59 -04001235int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
1236{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -08001237 size_t i;
1238 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -05001239 int ret = -EINVAL;
1240
Elly Jonese1749eb2011-10-07 13:54:59 -04001241 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001242 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001243 memcpy((void *)j, serialized, sizeof(*j));
1244 serialized += sizeof(*j);
1245 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001246
Will Drewrybee7ba72011-10-21 20:47:01 -05001247 /* Potentially stale pointers not used as signals. */
Luis Hector Chavez9acba452018-10-11 10:13:25 -07001248 j->preload_path = NULL;
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001249 j->pid_file_path = NULL;
1250 j->uidmap = NULL;
1251 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001252 j->mounts_head = NULL;
1253 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001254 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001255 j->hooks_head = NULL;
1256 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001257
Elly Jonese1749eb2011-10-07 13:54:59 -04001258 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001259 char *user = consumestr(&serialized, &length);
1260 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001261 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001262 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001263 if (!j->user)
1264 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001265 }
Will Drewryf89aef52011-09-16 16:48:57 -05001266
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001267 if (j->suppl_gid_list) { /* stale pointer */
1268 if (j->suppl_gid_count > NGROUPS_MAX) {
1269 goto bad_gid_list;
1270 }
1271 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1272 void *gid_list_bytes =
1273 consumebytes(gid_list_size, &serialized, &length);
1274 if (!gid_list_bytes)
1275 goto bad_gid_list;
1276
1277 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1278 if (!j->suppl_gid_list)
1279 goto bad_gid_list;
1280
1281 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1282 }
1283
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001284 if (j->chrootdir) { /* stale pointer */
1285 char *chrootdir = consumestr(&serialized, &length);
1286 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001287 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001288 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001289 if (!j->chrootdir)
1290 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001291 }
1292
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001293 if (j->hostname) { /* stale pointer */
1294 char *hostname = consumestr(&serialized, &length);
1295 if (!hostname)
1296 goto bad_hostname;
1297 j->hostname = strdup(hostname);
1298 if (!j->hostname)
1299 goto bad_hostname;
1300 }
1301
Andrew Brestickereac28942015-11-11 16:04:46 -08001302 if (j->alt_syscall_table) { /* stale pointer */
1303 char *alt_syscall_table = consumestr(&serialized, &length);
1304 if (!alt_syscall_table)
1305 goto bad_syscall_table;
1306 j->alt_syscall_table = strdup(alt_syscall_table);
1307 if (!j->alt_syscall_table)
1308 goto bad_syscall_table;
1309 }
1310
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001311 if (j->flags.seccomp_filter && j->filter_len > 0) {
1312 size_t ninstrs = j->filter_len;
1313 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1314 ninstrs > USHRT_MAX)
1315 goto bad_filters;
1316
1317 size_t program_len = ninstrs * sizeof(struct sock_filter);
1318 void *program = consumebytes(program_len, &serialized, &length);
1319 if (!program)
1320 goto bad_filters;
1321
1322 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001323 if (!j->filter_prog)
1324 goto bad_filters;
1325
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001326 j->filter_prog->len = ninstrs;
1327 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001328 if (!j->filter_prog->filter)
1329 goto bad_filter_prog_instrs;
1330
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001331 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001332 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001333
Dylan Reid648b2202015-10-23 00:50:00 -07001334 count = j->mounts_count;
1335 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001336 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001337 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001338 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001339 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001340 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001341 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001342 const char *src = consumestr(&serialized, &length);
1343 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001344 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001345 dest = consumestr(&serialized, &length);
1346 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001347 goto bad_mounts;
1348 type = consumestr(&serialized, &length);
1349 if (!type)
1350 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001351 has_data = consumebytes(sizeof(*has_data), &serialized,
1352 &length);
1353 if (!has_data)
1354 goto bad_mounts;
1355 if (*has_data) {
1356 data = consumestr(&serialized, &length);
1357 if (!data)
1358 goto bad_mounts;
1359 }
Dylan Reid648b2202015-10-23 00:50:00 -07001360 flags = consumebytes(sizeof(*flags), &serialized, &length);
1361 if (!flags)
1362 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001363 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001364 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001365 }
1366
Dylan Reid605ce7f2016-01-19 19:21:00 -08001367 count = j->cgroup_count;
1368 j->cgroup_count = 0;
1369 for (i = 0; i < count; ++i) {
1370 char *cgroup = consumestr(&serialized, &length);
1371 if (!cgroup)
1372 goto bad_cgroups;
1373 j->cgroups[i] = strdup(cgroup);
1374 if (!j->cgroups[i])
1375 goto bad_cgroups;
1376 ++j->cgroup_count;
1377 }
1378
Elly Jonese1749eb2011-10-07 13:54:59 -04001379 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001380
Dylan Reid605ce7f2016-01-19 19:21:00 -08001381bad_cgroups:
Mike Frysingerac08a682017-10-10 02:04:50 -04001382 free_mounts_list(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001383 for (i = 0; i < j->cgroup_count; ++i)
1384 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001385bad_mounts:
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001386 if (j->filter_prog && j->filter_prog->filter)
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001387 free(j->filter_prog->filter);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001388bad_filter_prog_instrs:
1389 if (j->filter_prog)
1390 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001391bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001392 if (j->alt_syscall_table)
1393 free(j->alt_syscall_table);
1394bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001395 if (j->chrootdir)
1396 free(j->chrootdir);
1397bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001398 if (j->hostname)
1399 free(j->hostname);
1400bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001401 if (j->suppl_gid_list)
1402 free(j->suppl_gid_list);
1403bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001404 if (j->user)
1405 free(j->user);
1406clear_pointers:
1407 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001408 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001409 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001410 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001411 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001412 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001413out:
1414 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001415}
1416
Mike Frysinger33ffef32017-01-13 19:53:19 -05001417struct dev_spec {
1418 const char *name;
1419 mode_t mode;
1420 dev_t major, minor;
1421};
1422
1423static const struct dev_spec device_nodes[] = {
1424 {
1425 "null",
1426 S_IFCHR | 0666, 1, 3,
1427 },
1428 {
1429 "zero",
1430 S_IFCHR | 0666, 1, 5,
1431 },
1432 {
1433 "full",
1434 S_IFCHR | 0666, 1, 7,
1435 },
1436 {
1437 "urandom",
1438 S_IFCHR | 0444, 1, 9,
1439 },
1440 {
1441 "tty",
1442 S_IFCHR | 0666, 5, 0,
1443 },
1444};
1445
1446struct dev_sym_spec {
1447 const char *source, *dest;
1448};
1449
1450static const struct dev_sym_spec device_symlinks[] = {
1451 { "ptmx", "pts/ptmx", },
1452 { "fd", "/proc/self/fd", },
1453 { "stdin", "fd/0", },
1454 { "stdout", "fd/1", },
1455 { "stderr", "fd/2", },
1456};
1457
1458/*
1459 * Clean up the temporary dev path we had setup previously. In case of errors,
1460 * we don't want to go leaking empty tempdirs.
1461 */
1462static void mount_dev_cleanup(char *dev_path)
1463{
1464 umount2(dev_path, MNT_DETACH);
1465 rmdir(dev_path);
1466 free(dev_path);
1467}
1468
1469/*
1470 * Set up the pseudo /dev path at the temporary location.
1471 * See mount_dev_finalize for more details.
1472 */
1473static int mount_dev(char **dev_path_ret)
1474{
1475 int ret;
1476 int dev_fd;
1477 size_t i;
1478 mode_t mask;
1479 char *dev_path;
1480
1481 /*
1482 * Create a temp path for the /dev init. We'll relocate this to the
1483 * final location later on in the startup process.
1484 */
1485 dev_path = *dev_path_ret = strdup("/tmp/minijail.dev.XXXXXX");
1486 if (dev_path == NULL || mkdtemp(dev_path) == NULL)
1487 pdie("could not create temp path for /dev");
1488
1489 /* Set up the empty /dev mount point first. */
1490 ret = mount("minijail-devfs", dev_path, "tmpfs",
1491 MS_NOEXEC | MS_NOSUID, "size=5M,mode=755");
1492 if (ret) {
1493 rmdir(dev_path);
1494 return ret;
1495 }
1496
1497 /* We want to set the mode directly from the spec. */
1498 mask = umask(0);
1499
1500 /* Get a handle to the temp dev path for *at funcs below. */
1501 dev_fd = open(dev_path, O_DIRECTORY|O_PATH|O_CLOEXEC);
1502 if (dev_fd < 0) {
1503 ret = 1;
1504 goto done;
1505 }
1506
1507 /* Create all the nodes in /dev. */
1508 for (i = 0; i < ARRAY_SIZE(device_nodes); ++i) {
1509 const struct dev_spec *ds = &device_nodes[i];
1510 ret = mknodat(dev_fd, ds->name, ds->mode,
1511 makedev(ds->major, ds->minor));
1512 if (ret)
1513 goto done;
1514 }
1515
1516 /* Create all the symlinks in /dev. */
1517 for (i = 0; i < ARRAY_SIZE(device_symlinks); ++i) {
1518 const struct dev_sym_spec *ds = &device_symlinks[i];
1519 ret = symlinkat(ds->dest, dev_fd, ds->source);
1520 if (ret)
1521 goto done;
1522 }
1523
1524 /* Restore old mask. */
1525 done:
1526 close(dev_fd);
1527 umask(mask);
1528
1529 if (ret)
1530 mount_dev_cleanup(dev_path);
1531
1532 return ret;
1533}
1534
1535/*
1536 * Relocate the temporary /dev mount to its final /dev place.
1537 * We have to do this two step process so people can bind mount extra
1538 * /dev paths like /dev/log.
1539 */
1540static int mount_dev_finalize(const struct minijail *j, char *dev_path)
1541{
1542 int ret = -1;
1543 char *dest = NULL;
1544
1545 /* Unmount the /dev mount if possible. */
1546 if (umount2("/dev", MNT_DETACH))
1547 goto done;
1548
1549 if (asprintf(&dest, "%s/dev", j->chrootdir ? : "") < 0)
1550 goto done;
1551
1552 if (mount(dev_path, dest, NULL, MS_MOVE, NULL))
1553 goto done;
1554
1555 ret = 0;
1556 done:
1557 free(dest);
1558 mount_dev_cleanup(dev_path);
1559
1560 return ret;
1561}
1562
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001563/*
1564 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001565 * @j Minijail these mounts are for
1566 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001567 *
1568 * Returns 0 for success.
1569 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001570static int mount_one(const struct minijail *j, struct mountpoint *m,
1571 const char *dev_path)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001572{
Dylan Reid648b2202015-10-23 00:50:00 -07001573 int ret;
1574 char *dest;
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001575 int remount = 0;
1576 unsigned long original_mnt_flags = 0;
Dylan Reid648b2202015-10-23 00:50:00 -07001577
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -04001578 /* We assume |dest| has a leading "/". */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001579 if (dev_path && strncmp("/dev/", m->dest, 5) == 0) {
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -04001580 /*
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -04001581 * Since the temp path is rooted at /dev, skip that dest part.
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -04001582 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001583 if (asprintf(&dest, "%s%s", dev_path, m->dest + 4) < 0)
1584 return -ENOMEM;
1585 } else {
Mike Frysingerac08a682017-10-10 02:04:50 -04001586 if (asprintf(&dest, "%s%s", j->chrootdir ?: "", m->dest) < 0)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001587 return -ENOMEM;
1588 }
Dylan Reid648b2202015-10-23 00:50:00 -07001589
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001590 ret =
1591 setup_mount_destination(m->src, dest, j->uid, j->gid,
1592 (m->flags & MS_BIND), &original_mnt_flags);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001593 if (ret) {
yusukes1b32f852018-03-05 10:24:58 -08001594 warn("creating mount target '%s' failed", dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001595 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001596 }
Dylan Reideec77962016-06-30 19:35:10 -07001597
Dylan Reid648b2202015-10-23 00:50:00 -07001598 /*
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001599 * Bind mounts that change the 'ro' flag have to be remounted since
1600 * 'bind' and other flags can't both be specified in the same command.
1601 * Remount after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001602 */
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001603 if ((m->flags & MS_BIND) &&
1604 ((m->flags & MS_RDONLY) != (original_mnt_flags & MS_RDONLY))) {
1605 remount = 1;
1606 /*
1607 * Restrict the mount flags to those that are user-settable in a
1608 * MS_REMOUNT request, but excluding MS_RDONLY. The
1609 * user-requested mount flags will dictate whether the remount
1610 * will have that flag or not.
1611 */
1612 original_mnt_flags &= (MS_USER_SETTABLE_MASK & ~MS_RDONLY);
Elly Jonesa1059632011-12-15 15:17:07 -05001613 }
Dylan Reid648b2202015-10-23 00:50:00 -07001614
Dylan Reid81e23972016-05-18 14:06:35 -07001615 ret = mount(m->src, dest, m->type, m->flags, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001616 if (ret) {
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001617 pwarn("bind: %s -> %s flags=%#lx", m->src, dest, m->flags);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001618 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001619 }
Dylan Reid648b2202015-10-23 00:50:00 -07001620
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001621 if (remount) {
1622 ret =
1623 mount(m->src, dest, NULL,
1624 m->flags | original_mnt_flags | MS_REMOUNT, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001625 if (ret) {
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001626 pwarn("bind remount: %s -> %s flags=%#lx", m->src, dest,
1627 m->flags | original_mnt_flags | MS_REMOUNT);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001628 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001629 }
Dylan Reid648b2202015-10-23 00:50:00 -07001630 }
1631
Elly Jones51a5b6c2011-10-12 19:09:26 -04001632 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001633 if (m->next)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001634 return mount_one(j, m->next, dev_path);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001635 return 0;
1636
1637error:
1638 free(dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001639 return ret;
1640}
1641
Mike Frysingerac08a682017-10-10 02:04:50 -04001642static void process_mounts_or_die(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001643{
Mike Frysingerac08a682017-10-10 02:04:50 -04001644 /*
1645 * We have to mount /dev first in case there are bind mounts from
1646 * the original /dev into the new unique tmpfs one.
1647 */
1648 char *dev_path = NULL;
1649 if (j->flags.mount_dev && mount_dev(&dev_path))
1650 pdie("mount_dev failed");
Dylan Reid648b2202015-10-23 00:50:00 -07001651
Mike Frysingerac08a682017-10-10 02:04:50 -04001652 if (j->mounts_head && mount_one(j, j->mounts_head, dev_path)) {
1653 if (dev_path) {
1654 int saved_errno = errno;
1655 mount_dev_cleanup(dev_path);
1656 errno = saved_errno;
1657 }
1658 pdie("mount_one failed");
1659 }
Mike Frysinger33ffef32017-01-13 19:53:19 -05001660
1661 /*
Mike Frysingerac08a682017-10-10 02:04:50 -04001662 * Once all bind mounts have been processed, move the temp dev to
1663 * its final /dev home.
Mike Frysinger33ffef32017-01-13 19:53:19 -05001664 */
1665 if (j->flags.mount_dev && mount_dev_finalize(j, dev_path))
Mike Frysingerac08a682017-10-10 02:04:50 -04001666 pdie("mount_dev_finalize failed");
1667}
Elly Jones51a5b6c2011-10-12 19:09:26 -04001668
Mike Frysingerac08a682017-10-10 02:04:50 -04001669static int enter_chroot(const struct minijail *j)
1670{
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001671 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1672
Elly Jones51a5b6c2011-10-12 19:09:26 -04001673 if (chroot(j->chrootdir))
1674 return -errno;
1675
1676 if (chdir("/"))
1677 return -errno;
1678
1679 return 0;
1680}
1681
Mike Frysingerac08a682017-10-10 02:04:50 -04001682static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001683{
Mike Frysingerac08a682017-10-10 02:04:50 -04001684 int oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001685
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001686 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1687
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001688 /*
1689 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001690 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001691 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001692 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001693 if (oldroot < 0)
1694 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001695 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001696 if (newroot < 0)
1697 pdie("failed to open %s for fchdir", j->chrootdir);
1698
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001699 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001700 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001701 * do a self bind mount.
1702 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001703 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1704 pdie("failed to bind mount '%s'", j->chrootdir);
1705 if (chdir(j->chrootdir))
1706 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001707 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001708 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001709
1710 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001711 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001712 * change to the old root and unmount it.
1713 */
1714 if (fchdir(oldroot))
1715 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001716
1717 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05001718 * If skip_remount_private was enabled for minijail_enter(),
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001719 * there could be a shared mount point under |oldroot|. In that case,
1720 * mounts under this shared mount point will be unmounted below, and
1721 * this unmounting will propagate to the original mount namespace
1722 * (because the mount point is shared). To prevent this unexpected
1723 * unmounting, remove these mounts from their peer groups by recursively
1724 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001725 */
1726 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001727 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001728 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001729 if (umount2(".", MNT_DETACH))
1730 pdie("umount(/)");
1731 /* Change back to the new root. */
1732 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001733 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001734 if (close(oldroot))
1735 return -errno;
1736 if (close(newroot))
1737 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001738 if (chroot("/"))
1739 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001740 /* Set correct CWD for getcwd(3). */
1741 if (chdir("/"))
1742 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001743
1744 return 0;
1745}
1746
Martin Pelikánab9eb442017-01-25 11:53:58 +11001747static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001748{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001749 const char fmt[] = "size=%zu,mode=1777";
1750 /* Count for the user storing ULLONG_MAX literally + extra space. */
1751 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1752 int ret;
1753
1754 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1755
1756 if (ret <= 0)
1757 pdie("tmpfs size spec error");
1758 else if ((size_t)ret >= sizeof(data))
1759 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001760 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001761 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001762}
1763
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001764static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001765{
1766 const char *kProcPath = "/proc";
1767 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001768 /*
1769 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001770 * /proc in our namespace, which means using MS_REMOUNT here would
1771 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001772 * namespace (!). Instead, remove their mount from our namespace lazily
1773 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001774 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001775 if (umount2(kProcPath, MNT_DETACH)) {
1776 /*
1777 * If we are in a new user namespace, umount(2) will fail.
1778 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1779 */
1780 if (j->flags.userns) {
1781 info("umount(/proc, MNT_DETACH) failed, "
1782 "this is expected when using user namespaces");
1783 } else {
1784 return -errno;
1785 }
1786 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001787 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001788 return -errno;
1789 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001790}
1791
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001792static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001793{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001794 kill(j->initpid, SIGKILL);
1795 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001796}
1797
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001798static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001799{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001800 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001801 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001802}
1803
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001804static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001805{
1806 size_t i;
1807
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001808 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001809 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001810 kill_child_and_die(j, "failed to add to cgroups");
1811 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001812}
1813
Dylan Reid0f72ef42017-06-06 15:42:49 -07001814static void set_rlimits_or_die(const struct minijail *j)
1815{
1816 size_t i;
1817
1818 for (i = 0; i < j->rlimit_count; ++i) {
1819 struct rlimit limit;
1820 limit.rlim_cur = j->rlimits[i].cur;
1821 limit.rlim_max = j->rlimits[i].max;
1822 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1823 kill_child_and_die(j, "failed to set rlimit");
1824 }
1825}
1826
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001827static void write_ugid_maps_or_die(const struct minijail *j)
1828{
1829 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1830 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001831 if (j->gidmap && j->flags.disable_setgroups) {
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001832 /*
1833 * Older kernels might not have the /proc/<pid>/setgroups files.
1834 */
Mike Frysinger6b190c02017-01-04 17:18:42 -05001835 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001836 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001837 if (ret == -ENOENT) {
1838 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1839 warn("could not disable setgroups(2)");
1840 } else
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001841 kill_child_and_die(
1842 j, "failed to disable setgroups(2)");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001843 }
1844 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001845 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1846 kill_child_and_die(j, "failed to write gid_map");
1847}
1848
1849static void enter_user_namespace(const struct minijail *j)
1850{
Luis Hector Chavez71323552017-09-05 09:17:22 -07001851 int uid = j->flags.uid ? j->uid : 0;
1852 int gid = j->flags.gid ? j->gid : 0;
1853 if (j->gidmap && setresgid(gid, gid, gid)) {
1854 pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid,
1855 gid);
1856 }
1857 if (j->uidmap && setresuid(uid, uid, uid)) {
1858 pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid,
1859 uid);
1860 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001861}
1862
1863static void parent_setup_complete(int *pipe_fds)
1864{
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001865 close_and_reset(&pipe_fds[0]);
1866 close_and_reset(&pipe_fds[1]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001867}
1868
1869/*
1870 * wait_for_parent_setup: Called by the child process to wait for any
1871 * further parent-side setup to complete before continuing.
1872 */
1873static void wait_for_parent_setup(int *pipe_fds)
1874{
1875 char buf;
1876
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001877 close_and_reset(&pipe_fds[1]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001878
1879 /* Wait for parent to complete setup and close the pipe. */
1880 if (read(pipe_fds[0], &buf, 1) != 0)
1881 die("failed to sync with parent");
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001882 close_and_reset(&pipe_fds[0]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001883}
1884
1885static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001886{
Lutz Justen13807cb2017-01-03 17:11:55 +01001887 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1888 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001889 die("can only do one of inherit, keep, or set supplementary "
1890 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001891 }
1892
Lutz Justen13807cb2017-01-03 17:11:55 +01001893 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001894 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001895 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001896 } else if (j->flags.set_suppl_gids) {
1897 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001898 pdie("setgroups(suppl_gids) failed");
Luis Hector Chavez71323552017-09-05 09:17:22 -07001899 } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001900 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001901 * Only attempt to clear supplementary groups if we are changing
Luis Hector Chavez71323552017-09-05 09:17:22 -07001902 * users or groups, and if the caller did not request to disable
1903 * setgroups (used when entering a user namespace as a
1904 * non-privileged user).
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001905 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001906 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001907 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001908 }
1909
1910 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001911 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001912
1913 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001914 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001915}
1916
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001917static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1918{
1919 const uint64_t one = 1;
1920 unsigned int i;
1921 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1922 if (keep_mask & (one << i))
1923 continue;
1924 if (prctl(PR_CAPBSET_DROP, i))
1925 pdie("could not drop capability from bounding set");
1926 }
1927}
1928
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001929static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001930{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001931 if (!j->flags.use_caps)
1932 return;
1933
Elly Jonese1749eb2011-10-07 13:54:59 -04001934 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001935 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001936 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001937 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001938 unsigned int i;
1939 if (!caps)
1940 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001941 if (cap_clear(caps))
1942 die("can't clear caps");
1943
1944 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001945 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001946 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001947 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001948 flag[0] = i;
1949 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001950 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001951 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001952 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001953 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001954 die("can't add inheritable cap");
1955 }
1956 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001957 die("can't apply initial cleaned capset");
1958
1959 /*
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001960 * Instead of dropping the bounding set first, do it here in case
Kees Cook323878a2013-02-05 15:35:24 -08001961 * the caller had a more permissive bounding set which could
1962 * have been used above to raise a capability that wasn't already
1963 * present. This requires CAP_SETPCAP, so we raised/kept it above.
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001964 *
1965 * However, if we're asked to skip setting *and* locking the
1966 * SECURE_NOROOT securebit, also skip dropping the bounding set.
1967 * If the caller wants to regain all capabilities when executing a
1968 * set-user-ID-root program, allow them to do so. The default behavior
1969 * (i.e. the behavior without |securebits_skip_mask| set) will still put
1970 * the jailed process tree in a capabilities-only environment.
1971 *
1972 * We check the negated skip mask for SECURE_NOROOT and
1973 * SECURE_NOROOT_LOCKED. If the bits are set in the negated mask they
1974 * will *not* be skipped in lock_securebits(), and therefore we should
1975 * drop the bounding set.
Kees Cook323878a2013-02-05 15:35:24 -08001976 */
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001977 if (secure_noroot_set_and_locked(~j->securebits_skip_mask)) {
1978 drop_capbset(j->caps, last_valid_cap);
1979 } else {
1980 warn("SECURE_NOROOT not set, not dropping bounding set");
1981 }
Kees Cook323878a2013-02-05 15:35:24 -08001982
1983 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001984 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001985 flag[0] = CAP_SETPCAP;
1986 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1987 die("can't clear effective cap");
1988 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1989 die("can't clear permitted cap");
1990 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1991 die("can't clear inheritable cap");
1992 }
1993
1994 if (cap_set_proc(caps))
1995 die("can't apply final cleaned capset");
1996
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001997 /*
1998 * If ambient capabilities are supported, clear all capabilities first,
1999 * then raise the requested ones.
2000 */
2001 if (j->flags.set_ambient_caps) {
2002 if (!cap_ambient_supported()) {
2003 pdie("ambient capabilities not supported");
2004 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04002005 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
2006 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04002007 pdie("can't clear ambient capabilities");
2008 }
2009
2010 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
2011 if (!(j->caps & (one << i)))
2012 continue;
2013
2014 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
2015 0) != 0) {
2016 pdie("prctl(PR_CAP_AMBIENT, "
2017 "PR_CAP_AMBIENT_RAISE, %u) failed",
2018 i);
2019 }
2020 }
2021 }
2022
Kees Cook323878a2013-02-05 15:35:24 -08002023 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04002024}
2025
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002026static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002027{
2028 /*
2029 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
2030 * in the kernel source tree for an explanation of the parameters.
2031 */
2032 if (j->flags.no_new_privs) {
2033 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
2034 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
2035 }
2036
2037 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07002038 * Code running with ASan
2039 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
2040 * will make system calls not included in the syscall filter policy,
2041 * which will likely crash the program. Skip setting seccomp filter in
2042 * that case.
2043 * 'running_with_asan()' has no inputs and is completely defined at
2044 * build time, so this cannot be used by an attacker to skip setting
2045 * seccomp filter.
2046 */
Evgenii Stepanov3d98f3c2018-08-23 15:06:50 -07002047 if (j->flags.seccomp_filter && running_with_asan()) {
Evgenii Stepanov825828c2018-07-27 11:57:07 -07002048 warn("running with (HW)ASan, not setting seccomp filter");
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07002049 return;
2050 }
2051
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002052 if (j->flags.seccomp_filter) {
2053 if (j->flags.seccomp_filter_logging) {
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002054 warn("logging seccomp filter failures");
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04002055 if (!seccomp_ret_log_available()) {
2056 /*
2057 * If SECCOMP_RET_LOG is not available,
2058 * install the SIGSYS handler first.
2059 */
2060 if (install_sigsys_handler())
2061 pdie(
2062 "failed to install SIGSYS handler");
2063 }
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002064 } else if (j->flags.seccomp_filter_tsync) {
2065 /*
2066 * If setting thread sync,
2067 * reset the SIGSYS signal handler so that
2068 * the entire thread group is killed.
2069 */
2070 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
2071 pdie("failed to reset SIGSYS disposition");
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002072 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002073 }
2074
2075 /*
2076 * Install the syscall filter.
2077 */
2078 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002079 if (j->flags.seccomp_filter_tsync) {
2080 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
2081 SECCOMP_FILTER_FLAG_TSYNC,
2082 j->filter_prog)) {
2083 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002084 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002085 } else {
2086 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
2087 j->filter_prog)) {
2088 pdie("prctl(seccomp_filter) failed");
2089 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002090 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002091 }
2092}
2093
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002094static pid_t forward_pid = -1;
2095
Mike Frysinger33d051a2018-05-30 16:41:10 -04002096static void forward_signal(int sig,
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04002097 siginfo_t *siginfo attribute_unused,
2098 void *void_context attribute_unused)
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002099{
2100 if (forward_pid != -1) {
Mike Frysinger33d051a2018-05-30 16:41:10 -04002101 kill(forward_pid, sig);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002102 }
2103}
2104
2105static void install_signal_handlers(void)
2106{
2107 struct sigaction act;
2108
2109 memset(&act, 0, sizeof(act));
2110 act.sa_sigaction = &forward_signal;
2111 act.sa_flags = SA_SIGINFO | SA_RESTART;
2112
2113 /* Handle all signals, except SIGCHLD. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002114 for (int sig = 1; sig < NSIG; sig++) {
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002115 /*
2116 * We don't care if we get EINVAL: that just means that we
2117 * can't handle this signal, so let's skip it and continue.
2118 */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002119 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002120 }
2121 /* Reset SIGCHLD's handler. */
2122 signal(SIGCHLD, SIG_DFL);
2123
2124 /* Handle real-time signals. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002125 for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++) {
2126 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002127 }
2128}
2129
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002130static const char *lookup_hook_name(minijail_hook_event_t event)
2131{
2132 switch (event) {
2133 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
2134 return "pre-drop-caps";
2135 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
2136 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07002137 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
2138 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002139 case MINIJAIL_HOOK_EVENT_MAX:
2140 /*
2141 * Adding this in favor of a default case to force the
2142 * compiler to error out if a new enum value is added.
2143 */
2144 break;
2145 }
2146 return "unknown";
2147}
2148
2149static void run_hooks_or_die(const struct minijail *j,
2150 minijail_hook_event_t event)
2151{
2152 int rc;
2153 int hook_index = 0;
2154 for (struct hook *c = j->hooks_head; c; c = c->next) {
2155 if (c->event != event)
2156 continue;
2157 rc = c->hook(c->payload);
2158 if (rc != 0) {
2159 errno = -rc;
2160 pdie("%s hook (index %d) failed",
2161 lookup_hook_name(event), hook_index);
2162 }
2163 /* Only increase the index within the same hook event type. */
2164 ++hook_index;
2165 }
2166}
2167
Will Drewry6ac91122011-10-21 16:38:58 -05002168void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002169{
Dylan Reidf682d472015-09-17 21:39:07 -07002170 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002171 * If we're dropping caps, get the last valid cap from /proc now,
2172 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07002173 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002174 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002175 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002176 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07002177
Elly Jonese1749eb2011-10-07 13:54:59 -04002178 if (j->flags.pids)
2179 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002180 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04002181
Lutz Justen13807cb2017-01-03 17:11:55 +01002182 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05002183 die("cannot inherit supplementary groups without setting a "
2184 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04002185
Elly Jonesdd3e8512012-01-23 15:13:38 -05002186 /*
2187 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04002188 * so we don't even try. If any of our operations fail, we abort() the
2189 * entire process.
2190 */
Mike Frysinger902a4492018-12-27 05:22:56 -05002191 if (j->flags.enter_vfs) {
2192 if (setns(j->mountns_fd, CLONE_NEWNS))
2193 pdie("setns(CLONE_NEWNS) failed");
2194 close(j->mountns_fd);
2195 }
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002196
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07002197 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002198 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002199 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002200 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05002201 * By default, remount all filesystems as private, unless
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04002202 * - Passed a specific remount mode, in which case remount with
2203 * that,
2204 * - Asked not to remount at all, in which case skip the
2205 * mount(2) call.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002206 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
2207 */
Mike Frysinger785b1c32018-02-23 15:47:24 -05002208 if (j->remount_mode) {
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04002209 if (mount(NULL, "/", NULL, MS_REC | j->remount_mode,
2210 NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002211 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
2212 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002213 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002214 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04002215
Dylan Reidf7942472015-11-18 17:55:26 -08002216 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002217 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08002218 }
2219
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002220 if (j->flags.uts) {
2221 if (unshare(CLONE_NEWUTS))
2222 pdie("unshare(CLONE_NEWUTS) failed");
2223
2224 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
2225 pdie("sethostname(%s) failed", j->hostname);
2226 }
2227
Dylan Reid1102f5a2015-09-15 11:52:20 -07002228 if (j->flags.enter_net) {
2229 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002230 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger902a4492018-12-27 05:22:56 -05002231 close(j->netns_fd);
Mike Frysinger7559dfe2016-11-15 18:58:39 -05002232 } else if (j->flags.net) {
2233 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002234 pdie("unshare(CLONE_NEWNET) failed");
2235 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07002236 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002237
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002238 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002239 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002240
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08002241 if (j->flags.new_session_keyring) {
2242 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
2243 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
2244 }
2245
Mike Frysingerac08a682017-10-10 02:04:50 -04002246 /* We have to process all the mounts before we chroot/pivot_root. */
2247 process_mounts_or_die(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002248
Mike Frysingerac08a682017-10-10 02:04:50 -04002249 if (j->flags.chroot && enter_chroot(j))
Mike Frysinger33ffef32017-01-13 19:53:19 -05002250 pdie("chroot");
Mike Frysinger33ffef32017-01-13 19:53:19 -05002251
Mike Frysingerac08a682017-10-10 02:04:50 -04002252 if (j->flags.pivot_root && enter_pivot_root(j))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08002253 pdie("pivot_root");
2254
Martin Pelikánab9eb442017-01-25 11:53:58 +11002255 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07002256 pdie("mount_tmp");
2257
Dylan Reid791f5772015-09-14 20:02:42 -07002258 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04002259 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04002260
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002261 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
2262
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002263 /*
2264 * If we're only dropping capabilities from the bounding set, but not
2265 * from the thread's (permitted|inheritable|effective) sets, do it now.
2266 */
2267 if (j->flags.capbset_drop) {
2268 drop_capbset(j->cap_bset, last_valid_cap);
2269 }
2270
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002271 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002272 * POSIX capabilities are a bit tricky. We must set SECBIT_KEEP_CAPS
2273 * before drop_ugid() below as the latter would otherwise drop all
2274 * capabilities.
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002275 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002276 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002277 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002278 * When using ambient capabilities, CAP_SET{GID,UID} can be
2279 * inherited across execve(2), so SECBIT_KEEP_CAPS is not
2280 * strictly needed.
Elly Jonese1749eb2011-10-07 13:54:59 -04002281 */
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002282 bool require_keep_caps = !j->flags.set_ambient_caps;
2283 if (lock_securebits(j->securebits_skip_mask,
2284 require_keep_caps) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002285 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002286 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002287 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002288
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002289 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002290 /*
2291 * If we're setting no_new_privs, we can drop privileges
2292 * before setting seccomp filter. This way filter policies
2293 * don't need to allow privilege-dropping syscalls.
2294 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002295 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002296 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002297 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002298 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002299 /*
2300 * If we're not setting no_new_privs,
2301 * we need to set seccomp filter *before* dropping privileges.
2302 * WARNING: this means that filter policies *must* allow
2303 * setgroups()/setresgid()/setresuid() for dropping root and
2304 * capget()/capset()/prctl() for dropping caps.
2305 */
2306 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002307 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002308 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04002309 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002310
Elly Jonesdd3e8512012-01-23 15:13:38 -05002311 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08002312 * Select the specified alternate syscall table. The table must not
2313 * block prctl(2) if we're using seccomp as well.
2314 */
2315 if (j->flags.alt_syscall) {
2316 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002317 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08002318 }
2319
2320 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05002321 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04002322 * privilege-dropping syscalls :)
2323 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002324 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04002325 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002326 warn("seccomp not supported");
2327 return;
2328 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002329 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002330 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002331}
2332
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002333/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04002334static int init_exitstatus = 0;
2335
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04002336void init_term(int sig attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002337{
2338 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04002339}
2340
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002341void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04002342{
2343 pid_t pid;
2344 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002345 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002346 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002347 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002348 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002349 /*
2350 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04002351 * left inside our pid namespace or we get a signal.
2352 */
2353 if (pid == rootpid)
2354 init_exitstatus = status;
2355 }
2356 if (!WIFEXITED(init_exitstatus))
2357 _exit(MINIJAIL_ERR_INIT);
2358 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04002359}
2360
Will Drewry6ac91122011-10-21 16:38:58 -05002361int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002362{
2363 size_t sz = 0;
2364 size_t bytes = read(fd, &sz, sizeof(sz));
2365 char *buf;
2366 int r;
2367 if (sizeof(sz) != bytes)
2368 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002369 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04002370 return -E2BIG;
2371 buf = malloc(sz);
2372 if (!buf)
2373 return -ENOMEM;
2374 bytes = read(fd, buf, sz);
2375 if (bytes != sz) {
2376 free(buf);
2377 return -EINVAL;
2378 }
2379 r = minijail_unmarshal(j, buf, sz);
2380 free(buf);
2381 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002382}
2383
Will Drewry6ac91122011-10-21 16:38:58 -05002384int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04002385{
Elly Jonese1749eb2011-10-07 13:54:59 -04002386 size_t sz = minijail_size(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002387 if (!sz)
2388 return -EINVAL;
François Degros664eba72019-11-05 13:18:24 +11002389
2390 char *buf = malloc(sz);
2391 if (!buf)
2392 return -ENOMEM;
2393
2394 int err = minijail_marshal(j, buf, sz);
2395 if (err)
2396 goto error;
2397
Elly Jonese1749eb2011-10-07 13:54:59 -04002398 /* Sends [size][minijail]. */
François Degros664eba72019-11-05 13:18:24 +11002399 err = write_exactly(fd, &sz, sizeof(sz));
2400 if (err)
2401 goto error;
2402
2403 err = write_exactly(fd, buf, sz);
2404
2405error:
Elly Jonese1749eb2011-10-07 13:54:59 -04002406 free(buf);
François Degros664eba72019-11-05 13:18:24 +11002407 return err;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002408}
Elly Jonescd7a9042011-07-22 13:56:51 -04002409
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002410static int setup_preload(const struct minijail *j attribute_unused,
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002411 char ***child_env attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002412{
Daniel Erat5b7a3182015-08-19 16:06:22 -06002413#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002414 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002415 return 0;
2416#else
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002417 const char *preload_path = j->preload_path ?: PRELOADPATH;
2418 char *newenv = NULL;
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002419 int ret = 0;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002420 const char *oldenv = getenv(kLdPreloadEnvVar);
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002421
2422 if (!oldenv)
2423 oldenv = "";
Elly Jonescd7a9042011-07-22 13:56:51 -04002424
Elly Jonese1749eb2011-10-07 13:54:59 -04002425 /* Only insert a separating space if we have something to separate... */
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002426 if (asprintf(&newenv, "%s%s%s", oldenv, oldenv[0] != '\0' ? " " : "",
2427 preload_path) < 0) {
2428 return -1;
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002429 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002430
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002431 ret = minijail_setenv(child_env, kLdPreloadEnvVar, newenv, 1);
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002432 free(newenv);
2433 return ret;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002434#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04002435}
2436
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002437static int setup_pipe(char ***child_env, int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04002438{
2439 int r = pipe(fds);
2440 char fd_buf[11];
2441 if (r)
2442 return r;
2443 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2444 if (r <= 0)
2445 return -EINVAL;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002446 return minijail_setenv(child_env, kFdEnvVar, fd_buf, 1);
Will Drewryf89aef52011-09-16 16:48:57 -05002447}
2448
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002449static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002450{
2451 const char *kFdPath = "/proc/self/fd";
2452
2453 DIR *d = opendir(kFdPath);
2454 struct dirent *dir_entry;
2455
2456 if (d == NULL)
2457 return -1;
2458 int dir_fd = dirfd(d);
2459 while ((dir_entry = readdir(d)) != NULL) {
2460 size_t i;
2461 char *end;
2462 bool should_close = true;
2463 const int fd = strtol(dir_entry->d_name, &end, 10);
2464
2465 if ((*end) != '\0') {
2466 continue;
2467 }
2468 /*
2469 * We might have set up some pipes that we want to share with
2470 * the parent process, and should not be closed.
2471 */
2472 for (i = 0; i < size; ++i) {
2473 if (fd == inheritable_fds[i]) {
2474 should_close = false;
2475 break;
2476 }
2477 }
2478 /* Also avoid closing the directory fd. */
2479 if (should_close && fd != dir_fd)
2480 close(fd);
2481 }
2482 closedir(d);
2483 return 0;
2484}
2485
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002486static int redirect_fds(struct minijail *j)
2487{
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002488 for (size_t i = 0; i < j->preserved_fd_count; i++) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002489 if (dup2(j->preserved_fds[i].parent_fd,
2490 j->preserved_fds[i].child_fd) == -1) {
2491 return -1;
2492 }
2493 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002494 return 0;
2495}
2496
Dylan Reidacfb8be2017-08-25 12:56:51 -07002497/*
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002498 * Structure holding resources and state created when running a minijail.
2499 */
2500struct minijail_run_state {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002501 pid_t child_pid;
2502 int pipe_fds[2];
2503 int stdin_fds[2];
2504 int stdout_fds[2];
2505 int stderr_fds[2];
2506 int child_sync_pipe_fds[2];
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002507 char **child_env;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002508};
2509
2510static void minijail_free_run_state(struct minijail_run_state *state)
2511{
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002512 state->child_pid = -1;
2513
2514 int *fd_pairs[] = {state->pipe_fds, state->stdin_fds, state->stdout_fds,
2515 state->stderr_fds, state->child_sync_pipe_fds};
2516 for (size_t i = 0; i < ARRAY_SIZE(fd_pairs); ++i) {
2517 close_and_reset(&fd_pairs[i][0]);
2518 close_and_reset(&fd_pairs[i][1]);
2519 }
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002520
2521 minijail_free_env(state->child_env);
2522 state->child_env = NULL;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002523}
2524
2525/* Set up stdin/stdout/stderr file descriptors in the child. */
2526static void setup_child_std_fds(struct minijail *j,
2527 struct minijail_run_state *state)
2528{
2529 struct {
2530 const char *name;
2531 int from;
2532 int to;
2533 } fd_map[] = {
2534 {"stdin", state->stdin_fds[0], STDIN_FILENO},
2535 {"stdout", state->stdout_fds[1], STDOUT_FILENO},
2536 {"stderr", state->stderr_fds[1], STDERR_FILENO},
2537 };
2538
2539 for (size_t i = 0; i < ARRAY_SIZE(fd_map); ++i) {
2540 if (fd_map[i].from != -1) {
2541 if (dup2(fd_map[i].from, fd_map[i].to) == -1)
2542 die("failed to set up %s pipe", fd_map[i].name);
2543 }
2544 }
2545
2546 /* Close temporary pipe file descriptors. */
2547 int *std_pipes[] = {state->stdin_fds, state->stdout_fds,
2548 state->stderr_fds};
2549 for (size_t i = 0; i < ARRAY_SIZE(std_pipes); ++i) {
2550 close_and_reset(&std_pipes[i][0]);
2551 close_and_reset(&std_pipes[i][1]);
2552 }
2553
2554 /*
2555 * If any of stdin, stdout, or stderr are TTYs, or setsid flag is
2556 * set, create a new session. This prevents the jailed process from
2557 * using the TIOCSTI ioctl to push characters into the parent process
2558 * terminal's input buffer, therefore escaping the jail.
2559 *
2560 * Since it has just forked, the child will not be a process group
2561 * leader, and this call to setsid() should always succeed.
2562 */
2563 if (j->flags.setsid || isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2564 isatty(STDERR_FILENO)) {
2565 if (setsid() < 0) {
2566 pdie("setsid() failed");
2567 }
2568 }
2569}
2570
2571/*
Dylan Reidacfb8be2017-08-25 12:56:51 -07002572 * Structure that specifies how to start a minijail.
2573 *
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002574 * filename - The program to exec in the child. Required if |exec_in_child| = 1.
2575 * argv - Arguments for the child program. Required if |exec_in_child| = 1.
2576 * envp - Environment for the child program. Available if |exec_in_child| = 1.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002577 * use_preload - If true use LD_PRELOAD.
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002578 * exec_in_child - If true, run |filename|. Otherwise, the child will return to
Dylan Reid0412dcc2017-08-24 11:33:15 -07002579 * the caller.
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002580 * pstdin_fd - Filled with stdin pipe if non-NULL.
2581 * pstdout_fd - Filled with stdout pipe if non-NULL.
2582 * pstderr_fd - Filled with stderr pipe if non-NULL.
2583 * pchild_pid - Filled with the pid of the child process if non-NULL.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002584 */
2585struct minijail_run_config {
2586 const char *filename;
2587 char *const *argv;
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002588 char *const *envp;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002589 int use_preload;
Dylan Reid0412dcc2017-08-24 11:33:15 -07002590 int exec_in_child;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002591 int *pstdin_fd;
2592 int *pstdout_fd;
2593 int *pstderr_fd;
2594 pid_t *pchild_pid;
2595};
2596
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002597static int
2598minijail_run_config_internal(struct minijail *j,
2599 const struct minijail_run_config *config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002600
Will Drewry6ac91122011-10-21 16:38:58 -05002601int API minijail_run(struct minijail *j, const char *filename,
2602 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002603{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002604 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002605 .filename = filename,
2606 .argv = argv,
2607 .envp = NULL,
2608 .use_preload = true,
2609 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002610 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002611 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002612}
2613
2614int API minijail_run_pid(struct minijail *j, const char *filename,
2615 char *const argv[], pid_t *pchild_pid)
2616{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002617 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002618 .filename = filename,
2619 .argv = argv,
2620 .envp = NULL,
2621 .use_preload = true,
2622 .exec_in_child = true,
2623 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002624 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002625 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002626}
2627
2628int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002629 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002630{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002631 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002632 .filename = filename,
2633 .argv = argv,
2634 .envp = NULL,
2635 .use_preload = true,
2636 .exec_in_child = true,
2637 .pstdin_fd = pstdin_fd,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002638 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002639 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002640}
2641
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002642int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002643 char *const argv[], pid_t *pchild_pid,
2644 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002645{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002646 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002647 .filename = filename,
2648 .argv = argv,
2649 .envp = NULL,
2650 .use_preload = true,
2651 .exec_in_child = true,
2652 .pstdin_fd = pstdin_fd,
2653 .pstdout_fd = pstdout_fd,
2654 .pstderr_fd = pstderr_fd,
2655 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002656 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002657 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002658}
2659
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002660int API minijail_run_env_pid_pipes(struct minijail *j, const char *filename,
2661 char *const argv[], char *const envp[],
2662 pid_t *pchild_pid, int *pstdin_fd,
2663 int *pstdout_fd, int *pstderr_fd)
2664{
2665 struct minijail_run_config config = {
2666 .filename = filename,
2667 .argv = argv,
2668 .envp = envp,
2669 .use_preload = true,
2670 .exec_in_child = true,
2671 .pstdin_fd = pstdin_fd,
2672 .pstdout_fd = pstdout_fd,
2673 .pstderr_fd = pstderr_fd,
2674 .pchild_pid = pchild_pid,
2675 };
2676 return minijail_run_config_internal(j, &config);
2677}
2678
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002679int API minijail_run_no_preload(struct minijail *j, const char *filename,
2680 char *const argv[])
2681{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002682 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002683 .filename = filename,
2684 .argv = argv,
2685 .envp = NULL,
2686 .use_preload = false,
2687 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002688 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002689 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002690}
2691
Samuel Tan63187f42015-10-16 13:01:53 -07002692int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002693 const char *filename,
2694 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002695 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002696 int *pstdin_fd,
2697 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002698 int *pstderr_fd)
2699{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002700 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002701 .filename = filename,
2702 .argv = argv,
2703 .envp = NULL,
2704 .use_preload = false,
2705 .exec_in_child = true,
2706 .pstdin_fd = pstdin_fd,
2707 .pstdout_fd = pstdout_fd,
2708 .pstderr_fd = pstderr_fd,
2709 .pchild_pid = pchild_pid,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002710 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002711 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002712}
2713
2714int API minijail_run_env_pid_pipes_no_preload(struct minijail *j,
2715 const char *filename,
2716 char *const argv[],
2717 char *const envp[],
2718 pid_t *pchild_pid, int *pstdin_fd,
2719 int *pstdout_fd, int *pstderr_fd)
2720{
2721 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002722 .filename = filename,
2723 .argv = argv,
2724 .envp = envp,
2725 .use_preload = false,
2726 .exec_in_child = true,
2727 .pstdin_fd = pstdin_fd,
2728 .pstdout_fd = pstdout_fd,
2729 .pstderr_fd = pstderr_fd,
2730 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002731 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002732 return minijail_run_config_internal(j, &config);
Samuel Tan63187f42015-10-16 13:01:53 -07002733}
2734
Dylan Reid0412dcc2017-08-24 11:33:15 -07002735pid_t API minijail_fork(struct minijail *j)
2736{
2737 struct minijail_run_config config = {};
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002738 return minijail_run_config_internal(j, &config);
Dylan Reid0412dcc2017-08-24 11:33:15 -07002739}
2740
Dylan Reid18c49c82017-08-25 14:52:27 -07002741static int minijail_run_internal(struct minijail *j,
2742 const struct minijail_run_config *config,
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002743 struct minijail_run_state *state_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002744{
Dylan Reidce5b55e2016-01-13 11:04:16 -08002745 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002746 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002747 /* We need to remember this across the minijail_preexec() call. */
2748 int pid_namespace = j->flags.pids;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -07002749 /*
2750 * Create an init process if we are entering a pid namespace, unless the
2751 * user has explicitly opted out by calling minijail_run_as_init().
2752 */
2753 int do_init = j->flags.do_init && !j->flags.run_as_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002754 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002755
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002756 if (use_preload) {
Dylan Reid0412dcc2017-08-24 11:33:15 -07002757 if (j->hooks_head != NULL)
2758 die("Minijail hooks are not supported with LD_PRELOAD");
2759 if (!config->exec_in_child)
2760 die("minijail_fork is not supported with LD_PRELOAD");
2761
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002762 /*
2763 * Before we fork(2) and execve(2) the child process, we need
2764 * to open a pipe(2) to send the minijail configuration over.
2765 */
2766 state_out->child_env =
2767 minijail_copy_env(config->envp ? config->envp : environ);
2768 if (!state_out->child_env)
2769 return ENOMEM;
2770 if (setup_preload(j, &state_out->child_env) ||
2771 setup_pipe(&state_out->child_env, state_out->pipe_fds))
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002772 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002773 }
Will Drewryf89aef52011-09-16 16:48:57 -05002774
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002775 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002776 if (j->flags.use_caps && j->caps != 0 &&
2777 !j->flags.set_ambient_caps) {
2778 die("non-empty, non-ambient capabilities are not "
2779 "supported without LD_PRELOAD");
2780 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002781 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002782
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002783 /* Create pipes for stdin/stdout/stderr as requested by caller. */
2784 struct {
2785 bool requested;
2786 int *pipe_fds;
2787 } pipe_fd_req[] = {
2788 {config->pstdin_fd != NULL, state_out->stdin_fds},
2789 {config->pstdout_fd != NULL, state_out->stdout_fds},
2790 {config->pstderr_fd != NULL, state_out->stderr_fds},
2791 };
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002792
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002793 for (size_t i = 0; i < ARRAY_SIZE(pipe_fd_req); ++i) {
2794 if (pipe_fd_req[i].requested &&
2795 pipe(pipe_fd_req[i].pipe_fds) == -1)
2796 return EFAULT;
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002797 }
2798
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002799 /*
François Degros664eba72019-11-05 13:18:24 +11002800 * If the parent process needs to configure the child's runtime
2801 * environment after forking, create a pipe(2) to block the child until
2802 * configuration is done.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002803 */
Daniel Erat2d69add2019-04-23 20:58:53 -07002804 if (j->flags.forward_signals || j->flags.pid_file || j->flags.cgroups ||
2805 j->rlimit_count || j->flags.userns) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002806 sync_child = 1;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002807 if (pipe(state_out->child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002808 return -EFAULT;
2809 }
2810
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002811 /*
2812 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002813 *
2814 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2815 *
2816 * In multithreaded programs, there are a bunch of locks inside libc,
2817 * some of which may be held by other threads at the time that we call
2818 * minijail_run_pid(). If we call fork(), glibc does its level best to
2819 * ensure that we hold all of these locks before it calls clone()
2820 * internally and drop them after clone() returns, but when we call
2821 * sys_clone(2) directly, all that gets bypassed and we end up with a
2822 * child address space where some of libc's important locks are held by
2823 * other threads (which did not get cloned, and hence will never release
2824 * those locks). This is okay so long as we call exec() immediately
2825 * after, but a bunch of seemingly-innocent libc functions like setenv()
2826 * take locks.
2827 *
2828 * Hence, only call sys_clone() if we need to, in order to get at pid
2829 * namespacing. If we follow this path, the child's address space might
2830 * have broken locks; you may only call functions that do not acquire
2831 * any locks.
2832 *
2833 * Unfortunately, fork() acquires every lock it can get its hands on, as
2834 * previously detailed, so this function is highly likely to deadlock
2835 * later on (see "deadlock here") if we're multithreaded.
2836 *
2837 * We might hack around this by having the clone()d child (init of the
2838 * pid namespace) return directly, rather than leaving the clone()d
2839 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002840 * its fork()ed child return in turn), but that process would be
2841 * crippled with its libc locks potentially broken. We might try
2842 * fork()ing in the parent before we clone() to ensure that we own all
2843 * the locks, but then we have to have the forked child hanging around
2844 * consuming resources (and possibly having file descriptors / shared
2845 * memory regions / etc attached). We'd need to keep the child around to
2846 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002847 *
2848 * TODO(ellyjones): figure out if the "forked child hanging around"
2849 * problem is fixable or not. It would be nice if we worked in this
2850 * case.
2851 */
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002852 pid_t child_pid;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002853 if (pid_namespace) {
François Degros94619842019-11-08 16:37:55 +11002854 unsigned long clone_flags = CLONE_NEWPID | SIGCHLD;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002855 if (j->flags.userns)
2856 clone_flags |= CLONE_NEWUSER;
François Degros94619842019-11-08 16:37:55 +11002857
2858 child_pid = syscall(SYS_clone, clone_flags, NULL, 0L, 0L, 0L);
2859
2860 if (child_pid < 0) {
2861 if (errno == EPERM)
2862 pdie("clone(CLONE_NEWPID | ...) failed with EPERM; "
2863 "is this process missing CAP_SYS_ADMIN?");
2864 pdie("clone(CLONE_NEWPID | ...) failed");
2865 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002866 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002867 child_pid = fork();
2868
François Degros94619842019-11-08 16:37:55 +11002869 if (child_pid < 0)
2870 pdie("fork failed");
Elly Jonese1749eb2011-10-07 13:54:59 -04002871 }
Will Drewryf89aef52011-09-16 16:48:57 -05002872
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002873 state_out->child_pid = child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04002874 if (child_pid) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002875 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002876
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002877 if (j->flags.forward_signals) {
2878 forward_pid = child_pid;
2879 install_signal_handlers();
2880 }
2881
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002882 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002883 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002884
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002885 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002886 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002887
Dylan Reid0f72ef42017-06-06 15:42:49 -07002888 if (j->rlimit_count)
2889 set_rlimits_or_die(j);
2890
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002891 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002892 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002893
Mike Frysinger902a4492018-12-27 05:22:56 -05002894 if (j->flags.enter_vfs)
2895 close(j->mountns_fd);
2896
2897 if (j->flags.enter_net)
2898 close(j->netns_fd);
2899
Dylan Reidce5b55e2016-01-13 11:04:16 -08002900 if (sync_child)
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002901 parent_setup_complete(state_out->child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002902
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002903 if (use_preload) {
François Degros664eba72019-11-05 13:18:24 +11002904 /*
2905 * Add SIGPIPE to the signal mask to avoid getting
2906 * killed if the child process finishes or closes its
2907 * end of the pipe prematurely.
2908 *
2909 * TODO(crbug.com/1022170): Use pthread_sigmask instead
2910 * of sigprocmask if Minijail is used in multithreaded
2911 * programs.
2912 */
2913 sigset_t to_block, to_restore;
2914 if (sigemptyset(&to_block) < 0)
2915 pdie("sigemptyset failed");
2916 if (sigaddset(&to_block, SIGPIPE) < 0)
2917 pdie("sigaddset failed");
2918 if (sigprocmask(SIG_BLOCK, &to_block, &to_restore) < 0)
2919 pdie("sigprocmask failed");
2920
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002921 /* Send marshalled minijail. */
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002922 close_and_reset(&state_out->pipe_fds[0]);
2923 ret = minijail_to_fd(j, state_out->pipe_fds[1]);
2924 close_and_reset(&state_out->pipe_fds[1]);
François Degros664eba72019-11-05 13:18:24 +11002925
2926 /* Accept any pending SIGPIPE. */
2927 while (true) {
2928 const struct timespec zero_time = {0, 0};
2929 const int sig = sigtimedwait(&to_block, NULL, &zero_time);
2930 if (sig < 0) {
2931 if (errno != EINTR)
2932 break;
2933 } else {
2934 if (sig != SIGPIPE)
2935 die("unexpected signal %d", sig);
2936 }
2937 }
2938
2939 /* Restore the signal mask to its original state. */
2940 if (sigprocmask(SIG_SETMASK, &to_restore, NULL) < 0)
2941 pdie("sigprocmask failed");
2942
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002943 if (ret) {
François Degros664eba72019-11-05 13:18:24 +11002944 warn("failed to send marshalled minijail: %s",
2945 strerror(-ret));
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002946 kill(j->initpid, SIGKILL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002947 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002948 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002949
Elly Jonese1749eb2011-10-07 13:54:59 -04002950 return 0;
2951 }
Ben Chan541c7e52011-08-26 14:55:53 -07002952
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002953 /* Child process. */
Peter Qiu2860c462015-12-16 15:13:06 -08002954 if (j->flags.reset_signal_mask) {
2955 sigset_t signal_mask;
2956 if (sigemptyset(&signal_mask) != 0)
2957 pdie("sigemptyset failed");
2958 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2959 pdie("sigprocmask failed");
2960 }
2961
Luis Hector Chaveza27118a2018-04-04 08:18:01 -07002962 if (j->flags.reset_signal_handlers) {
2963 int signum;
2964 for (signum = 0; signum <= SIGRTMAX; signum++) {
2965 /*
2966 * Ignore EINVAL since some signal numbers in the range
2967 * might not be valid.
2968 */
2969 if (signal(signum, SIG_DFL) == SIG_ERR &&
2970 errno != EINVAL) {
2971 pdie("failed to reset signal %d disposition",
2972 signum);
2973 }
2974 }
2975 }
2976
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002977 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002978 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002979 int inheritable_fds[kMaxInheritableFdsSize];
2980 size_t size = 0;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002981
2982 int *pipe_fds[] = {
2983 state_out->pipe_fds, state_out->child_sync_pipe_fds,
2984 state_out->stdin_fds, state_out->stdout_fds,
2985 state_out->stderr_fds,
2986 };
2987
2988 for (size_t i = 0; i < ARRAY_SIZE(pipe_fds); ++i) {
2989 if (pipe_fds[i][0] != -1) {
2990 inheritable_fds[size++] = pipe_fds[i][0];
2991 }
2992 if (pipe_fds[i][1] != -1) {
2993 inheritable_fds[size++] = pipe_fds[i][1];
2994 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002995 }
Jorge Lucangeli Obes2337f802019-07-18 14:46:03 -04002996
Jorge Lucangeli Obescf3bbea2019-07-24 09:06:40 -04002997 /*
2998 * Preserve namespace file descriptors over the close_open_fds()
2999 * call. These are closed in minijail_enter() so they won't leak
3000 * into the child process.
3001 */
Jorge Lucangeli Obes2337f802019-07-18 14:46:03 -04003002 if (j->flags.enter_vfs)
3003 minijail_preserve_fd(j, j->mountns_fd, j->mountns_fd);
3004 if (j->flags.enter_net)
3005 minijail_preserve_fd(j, j->netns_fd, j->netns_fd);
3006
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003007 for (size_t i = 0; i < j->preserved_fd_count; i++) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07003008 /*
3009 * Preserve all parent_fds. They will be dup2(2)-ed in
3010 * the child later.
3011 */
3012 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
3013 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003014
3015 if (close_open_fds(inheritable_fds, size) < 0)
3016 die("failed to close open file descriptors");
3017 }
3018
Luis Hector Chavez1617f632017-08-01 18:32:30 -07003019 if (redirect_fds(j))
3020 die("failed to set up fd redirections");
3021
Dylan Reidce5b55e2016-01-13 11:04:16 -08003022 if (sync_child)
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003023 wait_for_parent_setup(state_out->child_sync_pipe_fds);
Dylan Reidce5b55e2016-01-13 11:04:16 -08003024
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003025 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08003026 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003027
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003028 setup_child_std_fds(j, state_out);
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05003029
Dylan Reid791f5772015-09-14 20:02:42 -07003030 /* If running an init program, let it decide when/how to mount /proc. */
3031 if (pid_namespace && !do_init)
3032 j->flags.remount_proc_ro = 0;
3033
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003034 if (use_preload) {
3035 /* Strip out flags that cannot be inherited across execve(2). */
3036 minijail_preexec(j);
3037 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003038 /*
3039 * If not using LD_PRELOAD, do all jailing before execve(2).
3040 * Note that PID namespaces can only be entered on fork(2),
3041 * so that flag is still cleared.
3042 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003043 j->flags.pids = 0;
3044 }
Dylan Reid0412dcc2017-08-24 11:33:15 -07003045
3046 /*
3047 * Jail this process.
3048 * If forking, return.
3049 * If not, execve(2) the target.
3050 */
Elly Jonese1749eb2011-10-07 13:54:59 -04003051 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003052
Dylan Reid0412dcc2017-08-24 11:33:15 -07003053 if (config->exec_in_child && pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05003054 /*
3055 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003056 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003057 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003058 * a child to actually run the program. If |do_init == 0|, we
3059 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04003060 *
3061 * If we're multithreaded, we'll probably deadlock here. See
3062 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04003063 */
3064 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003065 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04003066 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003067 } else if (child_pid > 0) {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003068 minijail_free_run_state(state_out);
3069
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04003070 /*
3071 * Best effort. Don't bother checking the return value.
3072 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003073 prctl(PR_SET_NAME, "minijail-init");
3074 init(child_pid); /* Never returns. */
3075 }
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003076 state_out->child_pid = child_pid;
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 /*
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003085 * We're going to execve(), so make sure any remaining resources are
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003086 * freed. Exceptions are:
3087 * 1. The child environment. No need to worry about freeing it since
3088 * execve reinitializes the heap anyways.
3089 * 2. The read side of the LD_PRELOAD pipe, which we need to hand down
3090 * into the target in which the preloaded code will read from it and
3091 * then close it.
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003092 */
3093 state_out->pipe_fds[0] = -1;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003094 char *const *child_env = state_out->child_env;
3095 state_out->child_env = NULL;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003096 minijail_free_run_state(state_out);
3097
3098 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003099 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04003100 * calling process
3101 * -> execve()-ing process
3102 * If we are:
3103 * calling process
3104 * -> init()-ing process
3105 * -> execve()-ing process
3106 */
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003107 if (!child_env)
3108 child_env = config->envp ? config->envp : environ;
François Degros08b10f72019-10-09 12:44:05 +11003109 execve(config->filename, config->argv, child_env);
3110
3111 ret = (errno == ENOENT ? MINIJAIL_ERR_NO_COMMAND : MINIJAIL_ERR_NO_ACCESS);
3112 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003113 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04003114}
3115
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003116static int
3117minijail_run_config_internal(struct minijail *j,
3118 const struct minijail_run_config *config)
3119{
3120 struct minijail_run_state state = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003121 .child_pid = -1,
3122 .pipe_fds = {-1, -1},
3123 .stdin_fds = {-1, -1},
3124 .stdout_fds = {-1, -1},
3125 .stderr_fds = {-1, -1},
3126 .child_sync_pipe_fds = {-1, -1},
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003127 .child_env = NULL,
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003128 };
3129 int ret = minijail_run_internal(j, config, &state);
3130
3131 if (ret == 0) {
3132 if (config->pchild_pid)
3133 *config->pchild_pid = state.child_pid;
3134
3135 /* Grab stdin/stdout/stderr descriptors requested by caller. */
3136 struct {
3137 int *pfd;
3138 int *psrc;
3139 } fd_map[] = {
3140 {config->pstdin_fd, &state.stdin_fds[1]},
3141 {config->pstdout_fd, &state.stdout_fds[0]},
3142 {config->pstderr_fd, &state.stderr_fds[0]},
3143 };
3144
3145 for (size_t i = 0; i < ARRAY_SIZE(fd_map); ++i) {
3146 if (fd_map[i].pfd) {
3147 *fd_map[i].pfd = *fd_map[i].psrc;
3148 *fd_map[i].psrc = -1;
3149 }
3150 }
3151
3152 if (!config->exec_in_child)
3153 ret = state.child_pid;
3154 }
3155
3156 minijail_free_run_state(&state);
3157
3158 return ret;
3159}
3160
Will Drewry6ac91122011-10-21 16:38:58 -05003161int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003162{
François Degros47e63352019-10-01 13:01:53 +10003163 if (j->initpid <= 0)
3164 return -ECHILD;
3165
Elly Jonese1749eb2011-10-07 13:54:59 -04003166 if (kill(j->initpid, SIGTERM))
3167 return -errno;
François Degros47e63352019-10-01 13:01:53 +10003168
3169 return minijail_wait(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003170}
3171
Will Drewry6ac91122011-10-21 16:38:58 -05003172int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003173{
François Degros08b10f72019-10-09 12:44:05 +11003174 if (j->initpid <= 0)
3175 return -ECHILD;
3176
Elly Jonese1749eb2011-10-07 13:54:59 -04003177 int st;
François Degros627deba2019-10-01 12:48:25 +10003178 while (true) {
3179 const int ret = waitpid(j->initpid, &st, 0);
3180 if (ret >= 0)
3181 break;
3182 if (errno != EINTR)
3183 return -errno;
3184 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003185
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003186 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003187 int error_status = st;
3188 if (WIFSIGNALED(st)) {
3189 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07003190 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003191 j->initpid, signum);
3192 /*
3193 * We return MINIJAIL_ERR_JAIL if the process received
3194 * SIGSYS, which happens when a syscall is blocked by
3195 * seccomp filters.
3196 * If not, we do what bash(1) does:
3197 * $? = 128 + signum
3198 */
3199 if (signum == SIGSYS) {
3200 error_status = MINIJAIL_ERR_JAIL;
3201 } else {
François Degros08b10f72019-10-09 12:44:05 +11003202 error_status = MINIJAIL_ERR_SIG_BASE + signum;
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003203 }
3204 }
3205 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003206 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003207
3208 int exit_status = WEXITSTATUS(st);
3209 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07003210 info("child process %d exited with status %d",
3211 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003212
3213 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04003214}
3215
Will Drewry6ac91122011-10-21 16:38:58 -05003216void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003217{
Dylan Reid605ce7f2016-01-19 19:21:00 -08003218 size_t i;
3219
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07003220 if (j->filter_prog) {
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08003221 free(j->filter_prog->filter);
3222 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04003223 }
Mike Frysingerac08a682017-10-10 02:04:50 -04003224 free_mounts_list(j);
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07003225 while (j->hooks_head) {
3226 struct hook *c = j->hooks_head;
3227 j->hooks_head = c->next;
3228 free(c);
3229 }
3230 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04003231 if (j->user)
3232 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08003233 if (j->suppl_gid_list)
3234 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05003235 if (j->chrootdir)
3236 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04003237 if (j->pid_file_path)
3238 free(j->pid_file_path);
3239 if (j->uidmap)
3240 free(j->uidmap);
3241 if (j->gidmap)
3242 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04003243 if (j->hostname)
3244 free(j->hostname);
Luis Hector Chavez9acba452018-10-11 10:13:25 -07003245 if (j->preload_path)
3246 free(j->preload_path);
Andrew Brestickereac28942015-11-11 16:04:46 -08003247 if (j->alt_syscall_table)
3248 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08003249 for (i = 0; i < j->cgroup_count; ++i)
3250 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04003251 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003252}
Luis Hector Chavez114a9302017-09-05 20:36:58 -07003253
3254void API minijail_log_to_fd(int fd, int min_priority)
3255{
3256 init_logging(LOG_TO_FD, fd, min_priority);
3257}