blob: 8c47c3346e27074d4b50d1cbf07ac529f2a73bdd [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>
Allen Webbc7182682021-04-16 09:44:53 -050011#include <assert.h>
Luis Hector Chavez43ff0802016-10-07 12:21:07 -070012#include <dirent.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040013#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070014#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040015#include <grp.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040016#include <linux/capability.h>
Luis Hector Chavezc3e17722018-10-16 20:43:12 -070017#include <linux/filter.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040018#include <sched.h>
19#include <signal.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070020#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080021#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040025#include <sys/capability.h>
26#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050027#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040028#include <sys/prctl.h>
Dylan Reid0f72ef42017-06-06 15:42:49 -070029#include <sys/resource.h>
Allen Webbc7182682021-04-16 09:44:53 -050030#include <sys/select.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070031#include <sys/stat.h>
Mike Frysinger33ffef32017-01-13 19:53:19 -050032#include <sys/sysmacros.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070033#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080034#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040035#include <sys/wait.h>
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -070036#include <syscall.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040037#include <unistd.h>
38
39#include "libminijail.h"
40#include "libminijail-private.h"
41
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070042#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080043#include "syscall_filter.h"
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040044#include "syscall_wrapper.h"
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040045#include "system.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070046#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080047
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070048/* Until these are reliably available in linux/prctl.h. */
Andrew Brestickereac28942015-11-11 16:04:46 -080049#ifndef PR_ALT_SYSCALL
50# define PR_ALT_SYSCALL 0x43724f53
51#endif
52
Dylan Reid4cbc2a52016-06-17 19:06:07 -070053/* New cgroup namespace might not be in linux-headers yet. */
54#ifndef CLONE_NEWCGROUP
55# define CLONE_NEWCGROUP 0x02000000
56#endif
57
Dylan Reid605ce7f2016-01-19 19:21:00 -080058#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
59
Dylan Reid0f72ef42017-06-06 15:42:49 -070060#define MAX_RLIMITS 32 /* Currently there are 15 supported by Linux. */
61
Richard Fungb06ce9b2021-05-11 20:11:57 +000062#define MAX_PRESERVED_FDS 128U
Luis Hector Chavez1617f632017-08-01 18:32:30 -070063
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -080064/* Keyctl commands. */
65#define KEYCTL_JOIN_SESSION_KEYRING 1
66
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -070067/*
68 * The userspace equivalent of MNT_USER_SETTABLE_MASK, which is the mask of all
69 * flags that can be modified by MS_REMOUNT.
70 */
71#define MS_USER_SETTABLE_MASK \
72 (MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME | MS_NODIRATIME | \
73 MS_RELATIME | MS_RDONLY)
74
Dylan Reid0f72ef42017-06-06 15:42:49 -070075struct minijail_rlimit {
76 int type;
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -080077 rlim_t cur;
78 rlim_t max;
Dylan Reid0f72ef42017-06-06 15:42:49 -070079};
80
Dylan Reid648b2202015-10-23 00:50:00 -070081struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040082 char *src;
83 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070084 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -070085 char *data;
86 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -070087 unsigned long flags;
88 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040089};
90
Nicole Anderson-Au835f7172021-01-13 21:18:13 +000091struct minijail_remount {
92 unsigned long remount_mode;
93 char *mount_name;
94 struct minijail_remount *next;
95};
96
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -070097struct hook {
98 minijail_hook_t hook;
99 void *payload;
100 minijail_hook_event_t event;
101 struct hook *next;
102};
103
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700104struct preserved_fd {
105 int parent_fd;
106 int child_fd;
107};
108
Will Drewryf89aef52011-09-16 16:48:57 -0500109struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700110 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700111 * WARNING: if you add a flag here you need to make sure it's
112 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700113 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400114 struct {
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700115 int uid : 1;
116 int gid : 1;
Lutz Justen13807cb2017-01-03 17:11:55 +0100117 int inherit_suppl_gids : 1;
118 int set_suppl_gids : 1;
119 int keep_suppl_gids : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700120 int use_caps : 1;
121 int capbset_drop : 1;
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400122 int set_ambient_caps : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700123 int vfs : 1;
124 int enter_vfs : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700125 int pids : 1;
126 int ipc : 1;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400127 int uts : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700128 int net : 1;
129 int enter_net : 1;
130 int ns_cgroups : 1;
131 int userns : 1;
132 int disable_setgroups : 1;
133 int seccomp : 1;
134 int remount_proc_ro : 1;
135 int no_new_privs : 1;
136 int seccomp_filter : 1;
137 int seccomp_filter_tsync : 1;
138 int seccomp_filter_logging : 1;
Anand K Mistry31adc6c2020-11-26 11:39:46 +1100139 int seccomp_filter_allow_speculation : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700140 int chroot : 1;
141 int pivot_root : 1;
Mike Frysinger33ffef32017-01-13 19:53:19 -0500142 int mount_dev : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700143 int mount_tmp : 1;
144 int do_init : 1;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700145 int run_as_init : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700146 int pid_file : 1;
147 int cgroups : 1;
148 int alt_syscall : 1;
149 int reset_signal_mask : 1;
Luis Hector Chaveza27118a2018-04-04 08:18:01 -0700150 int reset_signal_handlers : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700151 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800152 int new_session_keyring : 1;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400153 int forward_signals : 1;
Xiyuan Xia9b41e652019-05-23 11:03:04 -0700154 int setsid : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400155 } flags;
156 uid_t uid;
157 gid_t gid;
158 gid_t usergid;
159 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800160 size_t suppl_gid_count;
161 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400162 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800163 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400164 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700165 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700166 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400167 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800168 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800169 char *uidmap;
170 char *gidmap;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400171 char *hostname;
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700172 char *preload_path;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800173 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800174 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800175 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700176 struct mountpoint *mounts_head;
177 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800178 size_t mounts_count;
Mike Frysinger785b1c32018-02-23 15:47:24 -0500179 unsigned long remount_mode;
Nicole Anderson-Au835f7172021-01-13 21:18:13 +0000180 struct minijail_remount *remounts_head;
181 struct minijail_remount *remounts_tail;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100182 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800183 char *cgroups[MAX_CGROUPS];
184 size_t cgroup_count;
Dylan Reid0f72ef42017-06-06 15:42:49 -0700185 struct minijail_rlimit rlimits[MAX_RLIMITS];
186 size_t rlimit_count;
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700187 uint64_t securebits_skip_mask;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700188 struct hook *hooks_head;
189 struct hook *hooks_tail;
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700190 struct preserved_fd preserved_fds[MAX_PRESERVED_FDS];
191 size_t preserved_fd_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500192};
193
Luis Hector Chavez64730af2017-09-13 13:18:59 -0700194static void run_hooks_or_die(const struct minijail *j,
195 minijail_hook_event_t event);
196
Adrian Ratiu8ef61252021-06-08 03:46:24 +0300197
198static bool seccomp_is_logging_allowed(const struct minijail *j)
199{
200 return seccomp_default_ret_log() || j->flags.seccomp_filter_logging;
201}
202
Mike Frysingerac08a682017-10-10 02:04:50 -0400203static void free_mounts_list(struct minijail *j)
204{
205 while (j->mounts_head) {
206 struct mountpoint *m = j->mounts_head;
207 j->mounts_head = j->mounts_head->next;
208 free(m->data);
209 free(m->type);
210 free(m->dest);
211 free(m->src);
212 free(m);
213 }
214 // No need to clear mounts_head as we know it's NULL after the loop.
215 j->mounts_tail = NULL;
216}
217
Nicole Anderson-Au835f7172021-01-13 21:18:13 +0000218static void free_remounts_list(struct minijail *j)
219{
220 while (j->remounts_head) {
221 struct minijail_remount *m = j->remounts_head;
222 j->remounts_head = j->remounts_head->next;
223 free(m->mount_name);
224 free(m);
225 }
226 // No need to clear remounts_head as we know it's NULL after the loop.
227 j->remounts_tail = NULL;
228}
229
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700230/*
François Degros664eba72019-11-05 13:18:24 +1100231 * Writes exactly n bytes from buf to file descriptor fd.
232 * Returns 0 on success or a negative error code on error.
233 */
234static int write_exactly(int fd, const void *buf, size_t n)
235{
236 const char *p = buf;
237 while (n > 0) {
238 const ssize_t written = write(fd, p, n);
239 if (written < 0) {
240 if (errno == EINTR)
241 continue;
242
243 return -errno;
244 }
245
246 p += written;
247 n -= written;
248 }
249
250 return 0;
251}
252
Mattias Nissler6123e5a2020-02-11 13:38:03 +0100253/* Closes *pfd and sets it to -1. */
254static void close_and_reset(int *pfd)
255{
256 if (*pfd != -1)
257 close(*pfd);
258 *pfd = -1;
259}
260
François Degros664eba72019-11-05 13:18:24 +1100261/*
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700262 * Strip out flags meant for the parent.
263 * We keep things that are not inherited across execve(2) (e.g. capabilities),
264 * or are easier to set after execve(2) (e.g. seccomp filters).
265 */
266void minijail_preenter(struct minijail *j)
267{
268 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700269 j->flags.enter_vfs = 0;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700270 j->flags.ns_cgroups = 0;
271 j->flags.net = 0;
272 j->flags.uts = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700273 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700274 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800275 j->flags.do_init = 0;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700276 j->flags.run_as_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800277 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800278 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400279 j->flags.forward_signals = 0;
Xiyuan Xia9b41e652019-05-23 11:03:04 -0700280 j->flags.setsid = 0;
Mike Frysinger785b1c32018-02-23 15:47:24 -0500281 j->remount_mode = 0;
Nicole Anderson-Au835f7172021-01-13 21:18:13 +0000282 free_remounts_list(j);
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700283}
284
285/*
286 * Strip out flags meant for the child.
287 * We keep things that are inherited across execve(2).
288 */
289void minijail_preexec(struct minijail *j)
290{
291 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700292 int enter_vfs = j->flags.enter_vfs;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700293 int ns_cgroups = j->flags.ns_cgroups;
294 int net = j->flags.net;
295 int uts = j->flags.uts;
Dylan Reid791f5772015-09-14 20:02:42 -0700296 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800297 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700298 if (j->user)
299 free(j->user);
300 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800301 if (j->suppl_gid_list)
302 free(j->suppl_gid_list);
303 j->suppl_gid_list = NULL;
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700304 if (j->preload_path)
305 free(j->preload_path);
306 j->preload_path = NULL;
Mike Frysingerac08a682017-10-10 02:04:50 -0400307 free_mounts_list(j);
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700308 memset(&j->flags, 0, sizeof(j->flags));
309 /* Now restore anything we meant to keep. */
310 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700311 j->flags.enter_vfs = enter_vfs;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700312 j->flags.ns_cgroups = ns_cgroups;
313 j->flags.net = net;
314 j->flags.uts = uts;
Dylan Reid791f5772015-09-14 20:02:42 -0700315 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800316 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700317 /* Note, |pids| will already have been used before this call. */
318}
319
320/* Minijail API. */
321
Will Drewry6ac91122011-10-21 16:38:58 -0500322struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400323{
Mike Frysinger785b1c32018-02-23 15:47:24 -0500324 struct minijail *j = calloc(1, sizeof(struct minijail));
Mike Frysinger1036cd82020-08-28 00:15:59 -0400325 if (j) {
326 j->remount_mode = MS_PRIVATE;
327 }
Mike Frysinger785b1c32018-02-23 15:47:24 -0500328 return j;
Elly Jonescd7a9042011-07-22 13:56:51 -0400329}
330
Will Drewry6ac91122011-10-21 16:38:58 -0500331void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400332{
333 if (uid == 0)
334 die("useless change to uid 0");
335 j->uid = uid;
336 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400337}
338
Will Drewry6ac91122011-10-21 16:38:58 -0500339void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400340{
341 if (gid == 0)
342 die("useless change to gid 0");
343 j->gid = gid;
344 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400345}
346
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800347void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
348 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800349{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800350 size_t i;
351
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500352 if (j->flags.inherit_suppl_gids)
353 die("cannot inherit *and* set supplementary groups");
354 if (j->flags.keep_suppl_gids)
355 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800356
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800357 if (size == 0) {
358 /* Clear supplementary groups. */
359 j->suppl_gid_list = NULL;
360 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100361 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800362 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800363 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800364
365 /* Copy the gid_t array. */
366 j->suppl_gid_list = calloc(size, sizeof(gid_t));
367 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800368 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800369 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800370 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800371 j->suppl_gid_list[i] = list[i];
372 }
373 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100374 j->flags.set_suppl_gids = 1;
375}
376
377void API minijail_keep_supplementary_gids(struct minijail *j) {
378 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800379}
380
Will Drewry6ac91122011-10-21 16:38:58 -0500381int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400382{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700383 uid_t uid;
384 gid_t gid;
385 int rc = lookup_user(user, &uid, &gid);
386 if (rc)
387 return rc;
388 minijail_change_uid(j, uid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400389 j->user = strdup(user);
390 if (!j->user)
391 return -ENOMEM;
Luis Hector Chavez71323552017-09-05 09:17:22 -0700392 j->usergid = gid;
Elly Jonese1749eb2011-10-07 13:54:59 -0400393 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400394}
395
Will Drewry6ac91122011-10-21 16:38:58 -0500396int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400397{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700398 gid_t gid;
399 int rc = lookup_group(group, &gid);
400 if (rc)
401 return rc;
402 minijail_change_gid(j, gid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400403 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400404}
405
Will Drewry6ac91122011-10-21 16:38:58 -0500406void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400407{
408 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400409}
410
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700411void API minijail_no_new_privs(struct minijail *j)
412{
413 j->flags.no_new_privs = 1;
414}
415
Will Drewry6ac91122011-10-21 16:38:58 -0500416void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400417{
418 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500419}
420
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400421void API minijail_set_seccomp_filter_tsync(struct minijail *j)
422{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400423 if (j->filter_len > 0 && j->filter_prog != NULL) {
424 die("minijail_set_seccomp_filter_tsync() must be called "
425 "before minijail_parse_seccomp_filters()");
426 }
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400427
Adrian Ratiu8ef61252021-06-08 03:46:24 +0300428 if (seccomp_is_logging_allowed(j) && !seccomp_ret_log_available()) {
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400429 /*
430 * If SECCOMP_RET_LOG is not available, we don't want to use
431 * SECCOMP_RET_TRAP to both kill the entire process and report
432 * failing syscalls, since it will be brittle. Just bail.
433 */
Mike Frysinger52f6ada2019-06-26 16:59:36 -0400434 die("SECCOMP_RET_LOG not available, cannot use logging with "
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400435 "thread sync at the same time");
436 }
437
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400438 j->flags.seccomp_filter_tsync = 1;
439}
440
Anand K Mistry31adc6c2020-11-26 11:39:46 +1100441void API minijail_set_seccomp_filter_allow_speculation(struct minijail *j)
442{
443 if (j->filter_len > 0 && j->filter_prog != NULL) {
444 die("minijail_set_seccomp_filter_allow_speculation() must be "
445 "called before minijail_parse_seccomp_filters()");
446 }
447
448 j->flags.seccomp_filter_allow_speculation = 1;
449}
450
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700451void API minijail_log_seccomp_filter_failures(struct minijail *j)
452{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400453 if (j->filter_len > 0 && j->filter_prog != NULL) {
454 die("minijail_log_seccomp_filter_failures() must be called "
455 "before minijail_parse_seccomp_filters()");
456 }
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400457
458 if (j->flags.seccomp_filter_tsync && !seccomp_ret_log_available()) {
459 /*
460 * If SECCOMP_RET_LOG is not available, we don't want to use
461 * SECCOMP_RET_TRAP to both kill the entire process and report
462 * failing syscalls, since it will be brittle. Just bail.
463 */
Mike Frysinger52f6ada2019-06-26 16:59:36 -0400464 die("SECCOMP_RET_LOG not available, cannot use thread sync with "
465 "logging at the same time");
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400466 }
467
468 if (debug_logging_allowed()) {
469 j->flags.seccomp_filter_logging = 1;
470 } else {
471 warn("non-debug build: ignoring request to enable seccomp "
472 "logging");
473 }
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700474}
475
Will Drewry6ac91122011-10-21 16:38:58 -0500476void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400477{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800478 /*
479 * 'minijail_use_caps' configures a runtime-capabilities-only
480 * environment, including a bounding set matching the thread's runtime
481 * (permitted|inheritable|effective) sets.
482 * Therefore, it will override any existing bounding set configurations
483 * since the latter would allow gaining extra runtime capabilities from
484 * file capabilities.
485 */
486 if (j->flags.capbset_drop) {
487 warn("overriding bounding set configuration");
488 j->cap_bset = 0;
489 j->flags.capbset_drop = 0;
490 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400491 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800492 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400493}
494
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800495void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
496{
497 if (j->flags.use_caps) {
498 /*
499 * 'minijail_use_caps' will have already configured a capability
500 * bounding set matching the (permitted|inheritable|effective)
501 * sets. Abort if the user tries to configure a separate
502 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
503 * are mutually exclusive.
504 */
505 die("runtime capabilities already configured, can't drop "
506 "bounding set separately");
507 }
508 j->cap_bset = capmask;
509 j->flags.capbset_drop = 1;
510}
511
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400512void API minijail_set_ambient_caps(struct minijail *j)
513{
514 j->flags.set_ambient_caps = 1;
515}
516
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800517void API minijail_reset_signal_mask(struct minijail *j)
518{
Peter Qiu2860c462015-12-16 15:13:06 -0800519 j->flags.reset_signal_mask = 1;
520}
521
Luis Hector Chaveza27118a2018-04-04 08:18:01 -0700522void API minijail_reset_signal_handlers(struct minijail *j)
523{
524 j->flags.reset_signal_handlers = 1;
525}
526
Will Drewry6ac91122011-10-21 16:38:58 -0500527void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400528{
529 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400530}
531
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700532void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
533{
Mike Frysinger902a4492018-12-27 05:22:56 -0500534 /* Note: Do not use O_CLOEXEC here. We'll close it after we use it. */
535 int ns_fd = open(ns_path, O_RDONLY);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700536 if (ns_fd < 0) {
537 pdie("failed to open namespace '%s'", ns_path);
538 }
539 j->mountns_fd = ns_fd;
540 j->flags.enter_vfs = 1;
541}
542
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800543void API minijail_new_session_keyring(struct minijail *j)
544{
545 j->flags.new_session_keyring = 1;
546}
547
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700548void API minijail_skip_setting_securebits(struct minijail *j,
549 uint64_t securebits_skip_mask)
550{
551 j->securebits_skip_mask = securebits_skip_mask;
552}
553
Mike Frysinger785b1c32018-02-23 15:47:24 -0500554void API minijail_remount_mode(struct minijail *j, unsigned long mode)
555{
556 j->remount_mode = mode;
557}
558
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800559void API minijail_skip_remount_private(struct minijail *j)
560{
Mike Frysinger785b1c32018-02-23 15:47:24 -0500561 j->remount_mode = 0;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800562}
563
Will Drewry6ac91122011-10-21 16:38:58 -0500564void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400565{
Elly Jonese58176c2012-01-23 11:46:17 -0500566 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700567 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400568 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800569 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400570}
571
Jorge Lucangeli Obes2fa96d12019-02-05 10:51:57 -0500572void API minijail_namespace_pids_rw_proc(struct minijail *j)
573{
574 j->flags.vfs = 1;
575 j->flags.pids = 1;
576 j->flags.do_init = 1;
577}
578
Dylan Reidf7942472015-11-18 17:55:26 -0800579void API minijail_namespace_ipc(struct minijail *j)
580{
581 j->flags.ipc = 1;
582}
583
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400584void API minijail_namespace_uts(struct minijail *j)
585{
586 j->flags.uts = 1;
587}
588
589int API minijail_namespace_set_hostname(struct minijail *j, const char *name)
590{
591 if (j->hostname)
592 return -EINVAL;
593 minijail_namespace_uts(j);
594 j->hostname = strdup(name);
595 if (!j->hostname)
596 return -ENOMEM;
597 return 0;
598}
599
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400600void API minijail_namespace_net(struct minijail *j)
601{
602 j->flags.net = 1;
603}
604
Dylan Reid1102f5a2015-09-15 11:52:20 -0700605void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
606{
Mike Frysinger902a4492018-12-27 05:22:56 -0500607 /* Note: Do not use O_CLOEXEC here. We'll close it after we use it. */
608 int ns_fd = open(ns_path, O_RDONLY);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700609 if (ns_fd < 0) {
610 pdie("failed to open namespace '%s'", ns_path);
611 }
612 j->netns_fd = ns_fd;
613 j->flags.enter_net = 1;
614}
615
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700616void API minijail_namespace_cgroups(struct minijail *j)
617{
618 j->flags.ns_cgroups = 1;
619}
620
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700621void API minijail_close_open_fds(struct minijail *j)
622{
623 j->flags.close_open_fds = 1;
624}
625
Dylan Reid791f5772015-09-14 20:02:42 -0700626void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400627{
628 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700629 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400630}
631
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800632void API minijail_namespace_user(struct minijail *j)
633{
634 j->flags.userns = 1;
635}
636
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400637void API minijail_namespace_user_disable_setgroups(struct minijail *j)
638{
639 j->flags.disable_setgroups = 1;
640}
641
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800642int API minijail_uidmap(struct minijail *j, const char *uidmap)
643{
644 j->uidmap = strdup(uidmap);
645 if (!j->uidmap)
646 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800647 char *ch;
648 for (ch = j->uidmap; *ch; ch++) {
649 if (*ch == ',')
650 *ch = '\n';
651 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800652 return 0;
653}
654
655int API minijail_gidmap(struct minijail *j, const char *gidmap)
656{
657 j->gidmap = strdup(gidmap);
658 if (!j->gidmap)
659 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800660 char *ch;
661 for (ch = j->gidmap; *ch; ch++) {
662 if (*ch == ',')
663 *ch = '\n';
664 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800665 return 0;
666}
667
Will Drewry6ac91122011-10-21 16:38:58 -0500668void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400669{
Lutz Justen13807cb2017-01-03 17:11:55 +0100670 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400671}
672
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800673void API minijail_run_as_init(struct minijail *j)
674{
675 /*
676 * Since the jailed program will become 'init' in the new PID namespace,
677 * Minijail does not need to fork an 'init' process.
678 */
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700679 j->flags.run_as_init = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800680}
681
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700682int API minijail_enter_chroot(struct minijail *j, const char *dir)
683{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400684 if (j->chrootdir)
685 return -EINVAL;
686 j->chrootdir = strdup(dir);
687 if (!j->chrootdir)
688 return -ENOMEM;
689 j->flags.chroot = 1;
690 return 0;
691}
692
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800693int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
694{
695 if (j->chrootdir)
696 return -EINVAL;
697 j->chrootdir = strdup(dir);
698 if (!j->chrootdir)
699 return -ENOMEM;
700 j->flags.pivot_root = 1;
701 return 0;
702}
703
Dylan Reida14e08d2015-10-22 21:05:29 -0700704char API *minijail_get_original_path(struct minijail *j,
705 const char *path_inside_chroot)
706{
Dylan Reid648b2202015-10-23 00:50:00 -0700707 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700708
Dylan Reid648b2202015-10-23 00:50:00 -0700709 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700710 while (b) {
711 /*
712 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700713 * mount, then the original path is exactly the source of
714 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700715 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700716 * mount source = /some/path/exe, mount dest =
717 * /chroot/path/exe Then when getting the original path of
718 * "/chroot/path/exe", the source of that mount,
719 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700720 */
721 if (!strcmp(b->dest, path_inside_chroot))
722 return strdup(b->src);
723
724 /*
725 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700726 * mount, take the suffix of the chroot path relative to the
727 * mount destination path, and append it to the mount source
728 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700729 */
730 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
731 const char *relative_path =
732 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400733 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700734 }
735 b = b->next;
736 }
737
738 /* If there is a chroot path, append |path_inside_chroot| to that. */
739 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400740 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700741
742 /* No chroot, so the path outside is the same as it is inside. */
743 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700744}
745
Mike Frysinger33ffef32017-01-13 19:53:19 -0500746void API minijail_mount_dev(struct minijail *j)
747{
748 j->flags.mount_dev = 1;
749}
750
Lee Campbell11af0622014-05-22 12:36:04 -0700751void API minijail_mount_tmp(struct minijail *j)
752{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100753 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
754}
755
756void API minijail_mount_tmp_size(struct minijail *j, size_t size)
757{
758 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700759 j->flags.mount_tmp = 1;
760}
761
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800762int API minijail_write_pid_file(struct minijail *j, const char *path)
763{
764 j->pid_file_path = strdup(path);
765 if (!j->pid_file_path)
766 return -ENOMEM;
767 j->flags.pid_file = 1;
768 return 0;
769}
770
Dylan Reid605ce7f2016-01-19 19:21:00 -0800771int API minijail_add_to_cgroup(struct minijail *j, const char *path)
772{
773 if (j->cgroup_count >= MAX_CGROUPS)
774 return -ENOMEM;
775 j->cgroups[j->cgroup_count] = strdup(path);
776 if (!j->cgroups[j->cgroup_count])
777 return -ENOMEM;
778 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800779 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800780 return 0;
781}
782
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800783int API minijail_rlimit(struct minijail *j, int type, rlim_t cur, rlim_t max)
Dylan Reid0f72ef42017-06-06 15:42:49 -0700784{
785 size_t i;
786
787 if (j->rlimit_count >= MAX_RLIMITS)
788 return -ENOMEM;
789 /* It's an error if the caller sets the same rlimit multiple times. */
790 for (i = 0; i < j->rlimit_count; i++) {
791 if (j->rlimits[i].type == type)
792 return -EEXIST;
793 }
794
795 j->rlimits[j->rlimit_count].type = type;
796 j->rlimits[j->rlimit_count].cur = cur;
797 j->rlimits[j->rlimit_count].max = max;
798 j->rlimit_count++;
799 return 0;
800}
801
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400802int API minijail_forward_signals(struct minijail *j)
803{
804 j->flags.forward_signals = 1;
805 return 0;
806}
807
Xiyuan Xia9b41e652019-05-23 11:03:04 -0700808int API minijail_create_session(struct minijail *j) {
809 j->flags.setsid = 1;
810 return 0;
811}
812
Dylan Reid81e23972016-05-18 14:06:35 -0700813int API minijail_mount_with_data(struct minijail *j, const char *src,
814 const char *dest, const char *type,
815 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700816{
Dylan Reid648b2202015-10-23 00:50:00 -0700817 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400818
819 if (*dest != '/')
820 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700821 m = calloc(1, sizeof(*m));
822 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400823 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700824 m->dest = strdup(dest);
825 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400826 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700827 m->src = strdup(src);
828 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400829 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700830 m->type = strdup(type);
831 if (!m->type)
832 goto error;
Mike Frysingerb7803c82018-08-23 15:43:15 -0400833
834 if (!data || !data[0]) {
835 /*
836 * Set up secure defaults for certain filesystems. Adding this
837 * fs-specific logic here kind of sucks, but considering how
838 * people use these in practice, it's probably OK. If they want
839 * the kernel defaults, they can pass data="" instead of NULL.
840 */
841 if (!strcmp(type, "tmpfs")) {
842 /* tmpfs defaults to mode=1777 and size=50%. */
843 data = "mode=0755,size=10M";
844 }
845 }
Dylan Reid81e23972016-05-18 14:06:35 -0700846 if (data) {
847 m->data = strdup(data);
848 if (!m->data)
849 goto error;
850 m->has_data = 1;
851 }
Mike Frysingercb8674d2018-08-12 00:53:35 -0400852
853 /* If they don't specify any flags, default to secure ones. */
854 if (flags == 0)
855 flags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Dylan Reid648b2202015-10-23 00:50:00 -0700856 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400857
Elly Jonesdd3e8512012-01-23 15:13:38 -0500858 /*
Jorge Lucangeli Obes0a0514c2020-01-03 11:18:32 -0500859 * Unless asked to enter an existing namespace, force vfs namespacing
860 * so the mounts don't leak out into the containing vfs namespace.
861 * If Minijail is being asked to enter the root vfs namespace this will
862 * leak mounts, but it's unlikely that the user would ask to do that by
863 * mistake.
Elly Jones51a5b6c2011-10-12 19:09:26 -0400864 */
Jorge Lucangeli Obes0a0514c2020-01-03 11:18:32 -0500865 if (!j->flags.enter_vfs)
866 minijail_namespace_vfs(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400867
Dylan Reid648b2202015-10-23 00:50:00 -0700868 if (j->mounts_tail)
869 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400870 else
Dylan Reid648b2202015-10-23 00:50:00 -0700871 j->mounts_head = m;
872 j->mounts_tail = m;
873 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400874
875 return 0;
876
877error:
Dylan Reid81e23972016-05-18 14:06:35 -0700878 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700879 free(m->src);
880 free(m->dest);
881 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400882 return -ENOMEM;
883}
884
Dylan Reid81e23972016-05-18 14:06:35 -0700885int API minijail_mount(struct minijail *j, const char *src, const char *dest,
886 const char *type, unsigned long flags)
887{
888 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
889}
890
Dylan Reid648b2202015-10-23 00:50:00 -0700891int API minijail_bind(struct minijail *j, const char *src, const char *dest,
892 int writeable)
893{
894 unsigned long flags = MS_BIND;
895
896 if (!writeable)
897 flags |= MS_RDONLY;
898
899 return minijail_mount(j, src, dest, "", flags);
900}
901
Nicole Anderson-Au835f7172021-01-13 21:18:13 +0000902int API minijail_add_remount(struct minijail *j, const char *mount_name,
903 unsigned long remount_mode)
904{
905 struct minijail_remount *m;
906
907 if (*mount_name != '/')
908 return -EINVAL;
909 m = calloc(1, sizeof(*m));
910 if (!m)
911 return -ENOMEM;
912 m->mount_name = strdup(mount_name);
913 if (!m->mount_name) {
914 free(m);
915 return -ENOMEM;
916 }
917
918 m->remount_mode = remount_mode;
919
920 if (j->remounts_tail)
921 j->remounts_tail->next = m;
922 else
923 j->remounts_head = m;
924 j->remounts_tail = m;
925
926 return 0;
927}
928
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700929int API minijail_add_hook(struct minijail *j, minijail_hook_t hook,
930 void *payload, minijail_hook_event_t event)
931{
932 struct hook *c;
933
934 if (hook == NULL)
935 return -EINVAL;
936 if (event >= MINIJAIL_HOOK_EVENT_MAX)
937 return -EINVAL;
938 c = calloc(1, sizeof(*c));
939 if (!c)
940 return -ENOMEM;
941
942 c->hook = hook;
943 c->payload = payload;
944 c->event = event;
945
946 if (j->hooks_tail)
947 j->hooks_tail->next = c;
948 else
949 j->hooks_head = c;
950 j->hooks_tail = c;
951
952 return 0;
953}
954
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700955int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd)
956{
957 if (parent_fd < 0 || child_fd < 0)
958 return -EINVAL;
959 if (j->preserved_fd_count >= MAX_PRESERVED_FDS)
960 return -ENOMEM;
961 j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd;
962 j->preserved_fds[j->preserved_fd_count].child_fd = child_fd;
963 j->preserved_fd_count++;
964 return 0;
965}
966
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700967int API minijail_set_preload_path(struct minijail *j, const char *preload_path)
968{
969 if (j->preload_path)
970 return -EINVAL;
971 j->preload_path = strdup(preload_path);
972 if (!j->preload_path)
973 return -ENOMEM;
974 return 0;
975}
976
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400977static void clear_seccomp_options(struct minijail *j)
978{
979 j->flags.seccomp_filter = 0;
980 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400981 j->flags.seccomp_filter_logging = 0;
Anand K Mistry31adc6c2020-11-26 11:39:46 +1100982 j->flags.seccomp_filter_allow_speculation = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400983 j->filter_len = 0;
984 j->filter_prog = NULL;
985 j->flags.no_new_privs = 0;
986}
987
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700988static int seccomp_should_use_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400989{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400990 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400991 /*
992 * |errno| will be set to EINVAL when seccomp has not been
993 * compiled into the kernel. On certain platforms and kernel
994 * versions this is not a fatal failure. In that case, and only
995 * in that case, disable seccomp and skip loading the filters.
996 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400997 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400998 warn("not loading seccomp filters, seccomp filter not "
999 "supported");
1000 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001001 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001002 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001003 /*
1004 * If |errno| != EINVAL or seccomp_can_softfail() is false,
1005 * we can proceed. Worst case scenario minijail_enter() will
1006 * abort() if seccomp fails.
1007 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001008 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001009 if (j->flags.seccomp_filter_tsync) {
1010 /* Are the seccomp(2) syscall and the TSYNC option supported? */
1011 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1012 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
1013 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001014 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
1015 warn("seccomp(2) syscall not supported");
1016 clear_seccomp_options(j);
1017 return 0;
1018 } else if (saved_errno == EINVAL &&
1019 seccomp_can_softfail()) {
1020 warn(
1021 "seccomp filter thread sync not supported");
1022 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001023 return 0;
1024 }
1025 /*
1026 * Similar logic here. If seccomp_can_softfail() is
1027 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
1028 * we can proceed. Worst case scenario minijail_enter()
1029 * will abort() if seccomp or TSYNC fail.
1030 */
1031 }
1032 }
Anand K Mistry31adc6c2020-11-26 11:39:46 +11001033 if (j->flags.seccomp_filter_allow_speculation) {
1034 /* Is the SPEC_ALLOW flag supported? */
Luis Héctor Chávez01b628c2021-01-03 05:46:57 -08001035 if (!seccomp_filter_flags_available(
1036 SECCOMP_FILTER_FLAG_SPEC_ALLOW)) {
Anand K Mistry31adc6c2020-11-26 11:39:46 +11001037 warn("allowing speculative execution on seccomp "
1038 "processes not supported");
1039 j->flags.seccomp_filter_allow_speculation = 0;
1040 }
1041 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001042 return 1;
1043}
1044
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001045static int set_seccomp_filters_internal(struct minijail *j,
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001046 const struct sock_fprog *filter,
1047 bool owned)
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001048{
1049 struct sock_fprog *fprog;
1050
1051 if (owned) {
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001052 /*
1053 * If |owned| is true, it's OK to cast away the const-ness since
1054 * we'll own the pointer going forward.
1055 */
1056 fprog = (struct sock_fprog *)filter;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001057 } else {
1058 fprog = malloc(sizeof(struct sock_fprog));
1059 if (!fprog)
1060 return -ENOMEM;
1061 fprog->len = filter->len;
1062 fprog->filter = malloc(sizeof(struct sock_filter) * fprog->len);
1063 if (!fprog->filter) {
1064 free(fprog);
1065 return -ENOMEM;
1066 }
1067 memcpy(fprog->filter, filter->filter,
1068 sizeof(struct sock_filter) * fprog->len);
1069 }
1070
1071 if (j->filter_prog) {
1072 free(j->filter_prog->filter);
1073 free(j->filter_prog);
1074 }
1075
1076 j->filter_len = fprog->len;
1077 j->filter_prog = fprog;
1078 return 0;
1079}
1080
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001081static int parse_seccomp_filters(struct minijail *j, const char *filename,
1082 FILE *policy_file)
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001083{
1084 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001085 if (!fprog)
1086 return -ENOMEM;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001087
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001088 struct filter_options filteropts;
1089
1090 /*
1091 * Figure out filter options.
1092 * Allow logging?
1093 */
1094 filteropts.allow_logging =
Adrian Ratiu8ef61252021-06-08 03:46:24 +03001095 debug_logging_allowed() && seccomp_is_logging_allowed(j);
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001096
1097 /* What to do on a blocked system call? */
1098 if (filteropts.allow_logging) {
1099 if (seccomp_ret_log_available())
1100 filteropts.action = ACTION_RET_LOG;
1101 else
1102 filteropts.action = ACTION_RET_TRAP;
1103 } else {
Jorge Lucangeli Obesd23ad7922020-10-13 10:26:40 -04001104 if (j->flags.seccomp_filter_tsync) {
1105 if (seccomp_ret_kill_process_available()) {
1106 filteropts.action = ACTION_RET_KILL_PROCESS;
1107 } else {
1108 filteropts.action = ACTION_RET_TRAP;
1109 }
1110 } else {
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001111 filteropts.action = ACTION_RET_KILL;
Jorge Lucangeli Obesd23ad7922020-10-13 10:26:40 -04001112 }
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001113 }
1114
1115 /*
1116 * If SECCOMP_RET_LOG is not available, need to allow extra syscalls
1117 * for logging.
1118 */
1119 filteropts.allow_syscalls_for_logging =
1120 filteropts.allow_logging && !seccomp_ret_log_available();
1121
Nicole Anderson-Aubcc8cfd2020-11-10 20:33:27 +00001122 /* Whether to fail on duplicate syscalls. */
1123 filteropts.allow_duplicate_syscalls = allow_duplicate_syscalls();
1124
Jorge Lucangeli Obese1a86892019-06-10 16:17:03 -04001125 if (compile_filter(filename, policy_file, fprog, &filteropts)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001126 free(fprog);
1127 return -1;
1128 }
1129
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001130 return set_seccomp_filters_internal(j, fprog, true /* owned */);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001131}
1132
1133void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
1134{
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001135 if (!seccomp_should_use_filters(j))
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001136 return;
1137
Mike Frysingerdebdf5d2021-06-21 09:52:06 -04001138 attribute_cleanup_fp FILE *file = fopen(path, "re");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001139 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -07001140 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -04001141 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001142
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001143 if (parse_seccomp_filters(j, path, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -07001144 die("failed to compile seccomp filter BPF program in '%s'",
1145 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001146 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001147}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001148
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001149void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
1150{
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001151 char *fd_path, *path;
Mike Frysingerdebdf5d2021-06-21 09:52:06 -04001152 attribute_cleanup_fp FILE *file = NULL;
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001153
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001154 if (!seccomp_should_use_filters(j))
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001155 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001156
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001157 file = fdopen(fd, "r");
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001158 if (!file) {
1159 pdie("failed to associate stream with fd %d", fd);
1160 }
1161
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001162 if (asprintf(&fd_path, "/proc/self/fd/%d", fd) == -1)
1163 pdie("failed to create path for fd %d", fd);
1164 path = realpath(fd_path, NULL);
1165 if (path == NULL)
1166 pwarn("failed to get path of fd %d", fd);
1167 free(fd_path);
1168
1169 if (parse_seccomp_filters(j, path ? path : "<fd>", file) != 0) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001170 die("failed to compile seccomp filter BPF program from fd %d",
1171 fd);
1172 }
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001173 free(path);
Will Drewry32ac9f52011-08-18 21:36:27 -05001174}
1175
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001176void API minijail_set_seccomp_filters(struct minijail *j,
1177 const struct sock_fprog *filter)
1178{
1179 if (!seccomp_should_use_filters(j))
1180 return;
1181
Adrian Ratiu8ef61252021-06-08 03:46:24 +03001182 if (seccomp_is_logging_allowed(j)) {
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001183 die("minijail_log_seccomp_filter_failures() is incompatible "
1184 "with minijail_set_seccomp_filters()");
1185 }
1186
1187 /*
1188 * set_seccomp_filters_internal() can only fail with ENOMEM.
1189 * Furthermore, since we won't own the incoming filter, it will not be
1190 * modified.
1191 */
1192 if (set_seccomp_filters_internal(j, filter, false /* owned */) < 0) {
1193 die("failed to set seccomp filter");
1194 }
1195}
1196
Andrew Brestickereac28942015-11-11 16:04:46 -08001197int API minijail_use_alt_syscall(struct minijail *j, const char *table)
1198{
1199 j->alt_syscall_table = strdup(table);
1200 if (!j->alt_syscall_table)
1201 return -ENOMEM;
1202 j->flags.alt_syscall = 1;
1203 return 0;
1204}
1205
Will Drewryf89aef52011-09-16 16:48:57 -05001206struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -04001207 size_t available;
1208 size_t total;
1209 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -05001210};
1211
Mike Frysinger0a27ab02020-09-04 16:18:12 -04001212static void marshal_state_init(struct marshal_state *state, char *buf,
1213 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -04001214{
1215 state->available = available;
1216 state->buf = buf;
1217 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001218}
1219
Mike Frysinger0a27ab02020-09-04 16:18:12 -04001220static void marshal_append(struct marshal_state *state, const void *src,
1221 size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -04001222{
1223 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -05001224
Elly Jonese1749eb2011-10-07 13:54:59 -04001225 /* Up to |available| will be written. */
1226 if (copy_len) {
1227 memcpy(state->buf, src, copy_len);
1228 state->buf += copy_len;
1229 state->available -= copy_len;
1230 }
1231 /* |total| will contain the expected length. */
1232 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -05001233}
1234
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001235static void marshal_append_string(struct marshal_state *state, const char *src)
1236{
1237 marshal_append(state, src, strlen(src) + 1);
1238}
1239
Mike Frysinger0a27ab02020-09-04 16:18:12 -04001240static void marshal_mount(struct marshal_state *state,
1241 const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -07001242{
1243 marshal_append(state, m->src, strlen(m->src) + 1);
1244 marshal_append(state, m->dest, strlen(m->dest) + 1);
1245 marshal_append(state, m->type, strlen(m->type) + 1);
1246 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
1247 if (m->has_data)
1248 marshal_append(state, m->data, strlen(m->data) + 1);
1249 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
1250}
1251
Mike Frysinger0a27ab02020-09-04 16:18:12 -04001252static void minijail_marshal_helper(struct marshal_state *state,
1253 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001254{
Dylan Reid648b2202015-10-23 00:50:00 -07001255 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001256 size_t i;
1257
Elly Jonese1749eb2011-10-07 13:54:59 -04001258 marshal_append(state, (char *)j, sizeof(*j));
1259 if (j->user)
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001260 marshal_append_string(state, j->user);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001261 if (j->suppl_gid_list) {
1262 marshal_append(state, j->suppl_gid_list,
1263 j->suppl_gid_count * sizeof(gid_t));
1264 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001265 if (j->chrootdir)
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001266 marshal_append_string(state, j->chrootdir);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001267 if (j->hostname)
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001268 marshal_append_string(state, j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08001269 if (j->alt_syscall_table) {
1270 marshal_append(state, j->alt_syscall_table,
1271 strlen(j->alt_syscall_table) + 1);
1272 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001273 if (j->flags.seccomp_filter && j->filter_prog) {
1274 struct sock_fprog *fp = j->filter_prog;
1275 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001276 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -04001277 }
Dylan Reid648b2202015-10-23 00:50:00 -07001278 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001279 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001280 }
Dylan Reid605ce7f2016-01-19 19:21:00 -08001281 for (i = 0; i < j->cgroup_count; ++i)
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001282 marshal_append_string(state, j->cgroups[i]);
Will Drewryf89aef52011-09-16 16:48:57 -05001283}
1284
Will Drewry6ac91122011-10-21 16:38:58 -05001285size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001286{
1287 struct marshal_state state;
1288 marshal_state_init(&state, NULL, 0);
1289 minijail_marshal_helper(&state, j);
1290 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -05001291}
1292
Elly Jonese1749eb2011-10-07 13:54:59 -04001293int minijail_marshal(const struct minijail *j, char *buf, size_t available)
1294{
1295 struct marshal_state state;
1296 marshal_state_init(&state, buf, available);
1297 minijail_marshal_helper(&state, j);
1298 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -05001299}
1300
Elly Jonese1749eb2011-10-07 13:54:59 -04001301int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
1302{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -08001303 size_t i;
1304 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -05001305 int ret = -EINVAL;
1306
Elly Jonese1749eb2011-10-07 13:54:59 -04001307 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001308 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001309 memcpy((void *)j, serialized, sizeof(*j));
1310 serialized += sizeof(*j);
1311 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001312
Will Drewrybee7ba72011-10-21 20:47:01 -05001313 /* Potentially stale pointers not used as signals. */
Luis Hector Chavez9acba452018-10-11 10:13:25 -07001314 j->preload_path = NULL;
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001315 j->pid_file_path = NULL;
1316 j->uidmap = NULL;
1317 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001318 j->mounts_head = NULL;
1319 j->mounts_tail = NULL;
Nicole Anderson-Au835f7172021-01-13 21:18:13 +00001320 j->remounts_head = NULL;
1321 j->remounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001322 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001323 j->hooks_head = NULL;
1324 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001325
Elly Jonese1749eb2011-10-07 13:54:59 -04001326 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001327 char *user = consumestr(&serialized, &length);
1328 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001329 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001330 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001331 if (!j->user)
1332 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001333 }
Will Drewryf89aef52011-09-16 16:48:57 -05001334
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001335 if (j->suppl_gid_list) { /* stale pointer */
1336 if (j->suppl_gid_count > NGROUPS_MAX) {
1337 goto bad_gid_list;
1338 }
1339 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1340 void *gid_list_bytes =
1341 consumebytes(gid_list_size, &serialized, &length);
1342 if (!gid_list_bytes)
1343 goto bad_gid_list;
1344
1345 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1346 if (!j->suppl_gid_list)
1347 goto bad_gid_list;
1348
1349 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1350 }
1351
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001352 if (j->chrootdir) { /* stale pointer */
1353 char *chrootdir = consumestr(&serialized, &length);
1354 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001355 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001356 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001357 if (!j->chrootdir)
1358 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001359 }
1360
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001361 if (j->hostname) { /* stale pointer */
1362 char *hostname = consumestr(&serialized, &length);
1363 if (!hostname)
1364 goto bad_hostname;
1365 j->hostname = strdup(hostname);
1366 if (!j->hostname)
1367 goto bad_hostname;
1368 }
1369
Andrew Brestickereac28942015-11-11 16:04:46 -08001370 if (j->alt_syscall_table) { /* stale pointer */
1371 char *alt_syscall_table = consumestr(&serialized, &length);
1372 if (!alt_syscall_table)
1373 goto bad_syscall_table;
1374 j->alt_syscall_table = strdup(alt_syscall_table);
1375 if (!j->alt_syscall_table)
1376 goto bad_syscall_table;
1377 }
1378
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001379 if (j->flags.seccomp_filter && j->filter_len > 0) {
1380 size_t ninstrs = j->filter_len;
1381 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1382 ninstrs > USHRT_MAX)
1383 goto bad_filters;
1384
1385 size_t program_len = ninstrs * sizeof(struct sock_filter);
1386 void *program = consumebytes(program_len, &serialized, &length);
1387 if (!program)
1388 goto bad_filters;
1389
1390 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001391 if (!j->filter_prog)
1392 goto bad_filters;
1393
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001394 j->filter_prog->len = ninstrs;
1395 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001396 if (!j->filter_prog->filter)
1397 goto bad_filter_prog_instrs;
1398
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001399 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001400 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001401
Dylan Reid648b2202015-10-23 00:50:00 -07001402 count = j->mounts_count;
1403 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001404 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001405 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001406 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001407 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001408 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001409 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001410 const char *src = consumestr(&serialized, &length);
1411 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001412 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001413 dest = consumestr(&serialized, &length);
1414 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001415 goto bad_mounts;
1416 type = consumestr(&serialized, &length);
1417 if (!type)
1418 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001419 has_data = consumebytes(sizeof(*has_data), &serialized,
1420 &length);
1421 if (!has_data)
1422 goto bad_mounts;
1423 if (*has_data) {
1424 data = consumestr(&serialized, &length);
1425 if (!data)
1426 goto bad_mounts;
1427 }
Dylan Reid648b2202015-10-23 00:50:00 -07001428 flags = consumebytes(sizeof(*flags), &serialized, &length);
1429 if (!flags)
1430 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001431 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001432 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001433 }
1434
Dylan Reid605ce7f2016-01-19 19:21:00 -08001435 count = j->cgroup_count;
1436 j->cgroup_count = 0;
1437 for (i = 0; i < count; ++i) {
1438 char *cgroup = consumestr(&serialized, &length);
1439 if (!cgroup)
1440 goto bad_cgroups;
1441 j->cgroups[i] = strdup(cgroup);
1442 if (!j->cgroups[i])
1443 goto bad_cgroups;
1444 ++j->cgroup_count;
1445 }
1446
Elly Jonese1749eb2011-10-07 13:54:59 -04001447 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001448
Dylan Reid605ce7f2016-01-19 19:21:00 -08001449bad_cgroups:
Mike Frysingerac08a682017-10-10 02:04:50 -04001450 free_mounts_list(j);
Nicole Anderson-Au835f7172021-01-13 21:18:13 +00001451 free_remounts_list(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001452 for (i = 0; i < j->cgroup_count; ++i)
1453 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001454bad_mounts:
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001455 if (j->filter_prog && j->filter_prog->filter)
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001456 free(j->filter_prog->filter);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001457bad_filter_prog_instrs:
1458 if (j->filter_prog)
1459 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001460bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001461 if (j->alt_syscall_table)
1462 free(j->alt_syscall_table);
1463bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001464 if (j->chrootdir)
1465 free(j->chrootdir);
1466bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001467 if (j->hostname)
1468 free(j->hostname);
1469bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001470 if (j->suppl_gid_list)
1471 free(j->suppl_gid_list);
1472bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001473 if (j->user)
1474 free(j->user);
1475clear_pointers:
1476 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001477 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001478 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001479 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001480 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001481 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001482out:
1483 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001484}
1485
Mike Frysinger33ffef32017-01-13 19:53:19 -05001486struct dev_spec {
1487 const char *name;
1488 mode_t mode;
1489 dev_t major, minor;
1490};
1491
1492static const struct dev_spec device_nodes[] = {
1493 {
1494 "null",
1495 S_IFCHR | 0666, 1, 3,
1496 },
1497 {
1498 "zero",
1499 S_IFCHR | 0666, 1, 5,
1500 },
1501 {
1502 "full",
1503 S_IFCHR | 0666, 1, 7,
1504 },
1505 {
1506 "urandom",
1507 S_IFCHR | 0444, 1, 9,
1508 },
1509 {
1510 "tty",
1511 S_IFCHR | 0666, 5, 0,
1512 },
1513};
1514
1515struct dev_sym_spec {
1516 const char *source, *dest;
1517};
1518
1519static const struct dev_sym_spec device_symlinks[] = {
1520 { "ptmx", "pts/ptmx", },
1521 { "fd", "/proc/self/fd", },
1522 { "stdin", "fd/0", },
1523 { "stdout", "fd/1", },
1524 { "stderr", "fd/2", },
1525};
1526
1527/*
1528 * Clean up the temporary dev path we had setup previously. In case of errors,
1529 * we don't want to go leaking empty tempdirs.
1530 */
1531static void mount_dev_cleanup(char *dev_path)
1532{
1533 umount2(dev_path, MNT_DETACH);
1534 rmdir(dev_path);
1535 free(dev_path);
1536}
1537
1538/*
1539 * Set up the pseudo /dev path at the temporary location.
1540 * See mount_dev_finalize for more details.
1541 */
1542static int mount_dev(char **dev_path_ret)
1543{
1544 int ret;
1545 int dev_fd;
1546 size_t i;
1547 mode_t mask;
1548 char *dev_path;
1549
1550 /*
1551 * Create a temp path for the /dev init. We'll relocate this to the
1552 * final location later on in the startup process.
1553 */
1554 dev_path = *dev_path_ret = strdup("/tmp/minijail.dev.XXXXXX");
1555 if (dev_path == NULL || mkdtemp(dev_path) == NULL)
1556 pdie("could not create temp path for /dev");
1557
1558 /* Set up the empty /dev mount point first. */
1559 ret = mount("minijail-devfs", dev_path, "tmpfs",
1560 MS_NOEXEC | MS_NOSUID, "size=5M,mode=755");
1561 if (ret) {
1562 rmdir(dev_path);
1563 return ret;
1564 }
1565
1566 /* We want to set the mode directly from the spec. */
1567 mask = umask(0);
1568
1569 /* Get a handle to the temp dev path for *at funcs below. */
1570 dev_fd = open(dev_path, O_DIRECTORY|O_PATH|O_CLOEXEC);
1571 if (dev_fd < 0) {
1572 ret = 1;
1573 goto done;
1574 }
1575
1576 /* Create all the nodes in /dev. */
1577 for (i = 0; i < ARRAY_SIZE(device_nodes); ++i) {
1578 const struct dev_spec *ds = &device_nodes[i];
1579 ret = mknodat(dev_fd, ds->name, ds->mode,
1580 makedev(ds->major, ds->minor));
1581 if (ret)
1582 goto done;
1583 }
1584
1585 /* Create all the symlinks in /dev. */
1586 for (i = 0; i < ARRAY_SIZE(device_symlinks); ++i) {
1587 const struct dev_sym_spec *ds = &device_symlinks[i];
1588 ret = symlinkat(ds->dest, dev_fd, ds->source);
1589 if (ret)
1590 goto done;
1591 }
1592
Mike Frysinger604cc7b2020-12-29 18:18:56 -05001593 /* Create empty dir for glibc shared mem APIs. */
1594 ret = mkdirat(dev_fd, "shm", 01777);
1595 if (ret)
1596 goto done;
1597
Mike Frysinger33ffef32017-01-13 19:53:19 -05001598 /* Restore old mask. */
1599 done:
1600 close(dev_fd);
1601 umask(mask);
1602
1603 if (ret)
1604 mount_dev_cleanup(dev_path);
1605
1606 return ret;
1607}
1608
1609/*
1610 * Relocate the temporary /dev mount to its final /dev place.
1611 * We have to do this two step process so people can bind mount extra
1612 * /dev paths like /dev/log.
1613 */
1614static int mount_dev_finalize(const struct minijail *j, char *dev_path)
1615{
1616 int ret = -1;
1617 char *dest = NULL;
1618
1619 /* Unmount the /dev mount if possible. */
1620 if (umount2("/dev", MNT_DETACH))
1621 goto done;
1622
1623 if (asprintf(&dest, "%s/dev", j->chrootdir ? : "") < 0)
1624 goto done;
1625
1626 if (mount(dev_path, dest, NULL, MS_MOVE, NULL))
1627 goto done;
1628
1629 ret = 0;
1630 done:
1631 free(dest);
1632 mount_dev_cleanup(dev_path);
1633
1634 return ret;
1635}
1636
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001637/*
1638 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001639 * @j Minijail these mounts are for
1640 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001641 *
1642 * Returns 0 for success.
1643 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001644static int mount_one(const struct minijail *j, struct mountpoint *m,
1645 const char *dev_path)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001646{
Dylan Reid648b2202015-10-23 00:50:00 -07001647 int ret;
1648 char *dest;
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001649 int remount = 0;
1650 unsigned long original_mnt_flags = 0;
Dylan Reid648b2202015-10-23 00:50:00 -07001651
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -04001652 /* We assume |dest| has a leading "/". */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001653 if (dev_path && strncmp("/dev/", m->dest, 5) == 0) {
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -04001654 /*
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -04001655 * Since the temp path is rooted at /dev, skip that dest part.
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -04001656 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001657 if (asprintf(&dest, "%s%s", dev_path, m->dest + 4) < 0)
1658 return -ENOMEM;
1659 } else {
Mike Frysingerac08a682017-10-10 02:04:50 -04001660 if (asprintf(&dest, "%s%s", j->chrootdir ?: "", m->dest) < 0)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001661 return -ENOMEM;
1662 }
Dylan Reid648b2202015-10-23 00:50:00 -07001663
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001664 ret =
1665 setup_mount_destination(m->src, dest, j->uid, j->gid,
1666 (m->flags & MS_BIND), &original_mnt_flags);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001667 if (ret) {
François Degrosa42182d2020-04-29 00:41:52 +10001668 warn("cannot create mount target '%s'", dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001669 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001670 }
Dylan Reideec77962016-06-30 19:35:10 -07001671
Dylan Reid648b2202015-10-23 00:50:00 -07001672 /*
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001673 * Bind mounts that change the 'ro' flag have to be remounted since
1674 * 'bind' and other flags can't both be specified in the same command.
1675 * Remount after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001676 */
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001677 if ((m->flags & MS_BIND) &&
1678 ((m->flags & MS_RDONLY) != (original_mnt_flags & MS_RDONLY))) {
1679 remount = 1;
1680 /*
1681 * Restrict the mount flags to those that are user-settable in a
1682 * MS_REMOUNT request, but excluding MS_RDONLY. The
1683 * user-requested mount flags will dictate whether the remount
1684 * will have that flag or not.
1685 */
1686 original_mnt_flags &= (MS_USER_SETTABLE_MASK & ~MS_RDONLY);
Elly Jonesa1059632011-12-15 15:17:07 -05001687 }
Dylan Reid648b2202015-10-23 00:50:00 -07001688
Dylan Reid81e23972016-05-18 14:06:35 -07001689 ret = mount(m->src, dest, m->type, m->flags, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001690 if (ret) {
François Degrosa42182d2020-04-29 00:41:52 +10001691 pwarn("cannot bind-mount '%s' as '%s' with flags %#lx", m->src,
1692 dest, m->flags);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001693 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001694 }
Dylan Reid648b2202015-10-23 00:50:00 -07001695
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001696 if (remount) {
1697 ret =
1698 mount(m->src, dest, NULL,
1699 m->flags | original_mnt_flags | MS_REMOUNT, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001700 if (ret) {
François Degrosa42182d2020-04-29 00:41:52 +10001701 pwarn(
1702 "cannot bind-remount '%s' as '%s' with flags %#lx",
1703 m->src, dest,
1704 m->flags | original_mnt_flags | MS_REMOUNT);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001705 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001706 }
Dylan Reid648b2202015-10-23 00:50:00 -07001707 }
1708
Elly Jones51a5b6c2011-10-12 19:09:26 -04001709 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001710 if (m->next)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001711 return mount_one(j, m->next, dev_path);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001712 return 0;
1713
1714error:
1715 free(dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001716 return ret;
1717}
1718
Mike Frysingerac08a682017-10-10 02:04:50 -04001719static void process_mounts_or_die(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001720{
Mike Frysingerac08a682017-10-10 02:04:50 -04001721 /*
1722 * We have to mount /dev first in case there are bind mounts from
1723 * the original /dev into the new unique tmpfs one.
1724 */
1725 char *dev_path = NULL;
1726 if (j->flags.mount_dev && mount_dev(&dev_path))
1727 pdie("mount_dev failed");
Dylan Reid648b2202015-10-23 00:50:00 -07001728
Mike Frysingerac08a682017-10-10 02:04:50 -04001729 if (j->mounts_head && mount_one(j, j->mounts_head, dev_path)) {
François Degrosa42182d2020-04-29 00:41:52 +10001730 if (dev_path)
Mike Frysingerac08a682017-10-10 02:04:50 -04001731 mount_dev_cleanup(dev_path);
François Degrosa42182d2020-04-29 00:41:52 +10001732
1733 _exit(MINIJAIL_ERR_MOUNT);
Mike Frysingerac08a682017-10-10 02:04:50 -04001734 }
Mike Frysinger33ffef32017-01-13 19:53:19 -05001735
1736 /*
Mike Frysingerac08a682017-10-10 02:04:50 -04001737 * Once all bind mounts have been processed, move the temp dev to
1738 * its final /dev home.
Mike Frysinger33ffef32017-01-13 19:53:19 -05001739 */
1740 if (j->flags.mount_dev && mount_dev_finalize(j, dev_path))
Mike Frysingerac08a682017-10-10 02:04:50 -04001741 pdie("mount_dev_finalize failed");
1742}
Elly Jones51a5b6c2011-10-12 19:09:26 -04001743
Mike Frysingerac08a682017-10-10 02:04:50 -04001744static int enter_chroot(const struct minijail *j)
1745{
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001746 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1747
Elly Jones51a5b6c2011-10-12 19:09:26 -04001748 if (chroot(j->chrootdir))
1749 return -errno;
1750
1751 if (chdir("/"))
1752 return -errno;
1753
1754 return 0;
1755}
1756
Mike Frysingerac08a682017-10-10 02:04:50 -04001757static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001758{
Mike Frysingerac08a682017-10-10 02:04:50 -04001759 int oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001760
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001761 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1762
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001763 /*
1764 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001765 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001766 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001767 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001768 if (oldroot < 0)
1769 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001770 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001771 if (newroot < 0)
1772 pdie("failed to open %s for fchdir", j->chrootdir);
1773
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001774 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001775 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001776 * do a self bind mount.
1777 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001778 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1779 pdie("failed to bind mount '%s'", j->chrootdir);
1780 if (chdir(j->chrootdir))
1781 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001782 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001783 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001784
1785 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001786 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001787 * change to the old root and unmount it.
1788 */
1789 if (fchdir(oldroot))
1790 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001791
1792 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05001793 * If skip_remount_private was enabled for minijail_enter(),
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001794 * there could be a shared mount point under |oldroot|. In that case,
1795 * mounts under this shared mount point will be unmounted below, and
1796 * this unmounting will propagate to the original mount namespace
1797 * (because the mount point is shared). To prevent this unexpected
1798 * unmounting, remove these mounts from their peer groups by recursively
1799 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001800 */
1801 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001802 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001803 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001804 if (umount2(".", MNT_DETACH))
1805 pdie("umount(/)");
1806 /* Change back to the new root. */
1807 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001808 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001809 if (close(oldroot))
1810 return -errno;
1811 if (close(newroot))
1812 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001813 if (chroot("/"))
1814 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001815 /* Set correct CWD for getcwd(3). */
1816 if (chdir("/"))
1817 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001818
1819 return 0;
1820}
1821
Martin Pelikánab9eb442017-01-25 11:53:58 +11001822static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001823{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001824 const char fmt[] = "size=%zu,mode=1777";
1825 /* Count for the user storing ULLONG_MAX literally + extra space. */
1826 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1827 int ret;
1828
1829 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1830
1831 if (ret <= 0)
1832 pdie("tmpfs size spec error");
1833 else if ((size_t)ret >= sizeof(data))
1834 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001835 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001836 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001837}
1838
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001839static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001840{
1841 const char *kProcPath = "/proc";
1842 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001843 /*
1844 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001845 * /proc in our namespace, which means using MS_REMOUNT here would
1846 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001847 * namespace (!). Instead, remove their mount from our namespace lazily
1848 * (MNT_DETACH) and make our own.
Jorge Lucangeli Obes320c4fc2020-12-10 10:38:30 -05001849 *
1850 * However, we skip this in the user namespace case because it will
1851 * invariably fail. Every mount namespace is "owned" by the
1852 * user namespace of the process that creates it. Mount namespace A is
1853 * "less privileged" than mount namespace B if A is created off of B,
1854 * and B is owned by a different user namespace.
1855 * When a less privileged mount namespace is created, the mounts used to
1856 * initialize it (coming from the more privileged mount namespace) come
1857 * as a unit, and are locked together. This means that code running in
1858 * the new mount (and user) namespace cannot piecemeal unmount
1859 * individual mounts inherited from a more privileged mount namespace.
1860 * See https://man7.org/linux/man-pages/man7/mount_namespaces.7.html,
1861 * "Restrictions on mount namespaces" for details.
1862 *
1863 * This happens in our use case because we first enter a new user
1864 * namespace (on clone(2)) and then we unshare(2) a new mount namespace,
1865 * which means the new mount namespace is less privileged than its
1866 * parent mount namespace. This would also happen if we entered a new
1867 * mount namespace on clone(2), since the user namespace is created
1868 * first.
1869 * In all other non-user-namespace cases the new mount namespace is
1870 * similarly privileged as the parent mount namespace so unmounting a
1871 * single mount is allowed.
1872 *
1873 * We still remount /proc as read-only in the user namespace case
1874 * because while a process with CAP_SYS_ADMIN in the new user namespace
1875 * can unmount the RO mount and get at the RW mount, an attacker with
1876 * access only to a write primitive will not be able to modify /proc.
Elly Jonese1749eb2011-10-07 13:54:59 -04001877 */
Jorge Lucangeli Obes320c4fc2020-12-10 10:38:30 -05001878 if (!j->flags.userns && umount2(kProcPath, MNT_DETACH))
1879 return -errno;
Mike Frysinger3ba81572017-01-17 23:33:28 -05001880 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001881 return -errno;
1882 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001883}
1884
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001885static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001886{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001887 kill(j->initpid, SIGKILL);
1888 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001889}
1890
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001891static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001892{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001893 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001894 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001895}
1896
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001897static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001898{
1899 size_t i;
1900
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001901 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001902 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001903 kill_child_and_die(j, "failed to add to cgroups");
1904 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001905}
1906
Dylan Reid0f72ef42017-06-06 15:42:49 -07001907static void set_rlimits_or_die(const struct minijail *j)
1908{
1909 size_t i;
1910
1911 for (i = 0; i < j->rlimit_count; ++i) {
1912 struct rlimit limit;
1913 limit.rlim_cur = j->rlimits[i].cur;
1914 limit.rlim_max = j->rlimits[i].max;
1915 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1916 kill_child_and_die(j, "failed to set rlimit");
1917 }
1918}
1919
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001920static void write_ugid_maps_or_die(const struct minijail *j)
1921{
1922 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1923 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001924 if (j->gidmap && j->flags.disable_setgroups) {
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001925 /*
1926 * Older kernels might not have the /proc/<pid>/setgroups files.
1927 */
Mike Frysinger6b190c02017-01-04 17:18:42 -05001928 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001929 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001930 if (ret == -ENOENT) {
1931 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1932 warn("could not disable setgroups(2)");
1933 } else
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001934 kill_child_and_die(
1935 j, "failed to disable setgroups(2)");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001936 }
1937 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001938 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1939 kill_child_and_die(j, "failed to write gid_map");
1940}
1941
1942static void enter_user_namespace(const struct minijail *j)
1943{
Luis Hector Chavez71323552017-09-05 09:17:22 -07001944 int uid = j->flags.uid ? j->uid : 0;
1945 int gid = j->flags.gid ? j->gid : 0;
1946 if (j->gidmap && setresgid(gid, gid, gid)) {
1947 pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid,
1948 gid);
1949 }
1950 if (j->uidmap && setresuid(uid, uid, uid)) {
1951 pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid,
1952 uid);
1953 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001954}
1955
1956static void parent_setup_complete(int *pipe_fds)
1957{
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001958 close_and_reset(&pipe_fds[0]);
1959 close_and_reset(&pipe_fds[1]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001960}
1961
1962/*
1963 * wait_for_parent_setup: Called by the child process to wait for any
1964 * further parent-side setup to complete before continuing.
1965 */
1966static void wait_for_parent_setup(int *pipe_fds)
1967{
1968 char buf;
1969
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001970 close_and_reset(&pipe_fds[1]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001971
1972 /* Wait for parent to complete setup and close the pipe. */
1973 if (read(pipe_fds[0], &buf, 1) != 0)
1974 die("failed to sync with parent");
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001975 close_and_reset(&pipe_fds[0]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001976}
1977
1978static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001979{
Lutz Justen13807cb2017-01-03 17:11:55 +01001980 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1981 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001982 die("can only do one of inherit, keep, or set supplementary "
1983 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001984 }
1985
Lutz Justen13807cb2017-01-03 17:11:55 +01001986 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001987 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001988 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001989 } else if (j->flags.set_suppl_gids) {
1990 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001991 pdie("setgroups(suppl_gids) failed");
Luis Hector Chavez71323552017-09-05 09:17:22 -07001992 } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001993 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001994 * Only attempt to clear supplementary groups if we are changing
Luis Hector Chavez71323552017-09-05 09:17:22 -07001995 * users or groups, and if the caller did not request to disable
1996 * setgroups (used when entering a user namespace as a
1997 * non-privileged user).
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001998 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001999 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002000 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002001 }
2002
2003 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002004 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002005
2006 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002007 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002008}
2009
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002010static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
2011{
2012 const uint64_t one = 1;
2013 unsigned int i;
2014 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
2015 if (keep_mask & (one << i))
2016 continue;
2017 if (prctl(PR_CAPBSET_DROP, i))
2018 pdie("could not drop capability from bounding set");
2019 }
2020}
2021
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002022static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04002023{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08002024 if (!j->flags.use_caps)
2025 return;
2026
Elly Jonese1749eb2011-10-07 13:54:59 -04002027 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08002028 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04002029 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08002030 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04002031 unsigned int i;
2032 if (!caps)
2033 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04002034 if (cap_clear(caps))
2035 die("can't clear caps");
2036
2037 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08002038 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08002039 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04002040 continue;
Kees Cook323878a2013-02-05 15:35:24 -08002041 flag[0] = i;
2042 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04002043 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08002044 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04002045 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08002046 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04002047 die("can't add inheritable cap");
2048 }
2049 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08002050 die("can't apply initial cleaned capset");
2051
2052 /*
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04002053 * Instead of dropping the bounding set first, do it here in case
Kees Cook323878a2013-02-05 15:35:24 -08002054 * the caller had a more permissive bounding set which could
2055 * have been used above to raise a capability that wasn't already
2056 * present. This requires CAP_SETPCAP, so we raised/kept it above.
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04002057 *
2058 * However, if we're asked to skip setting *and* locking the
2059 * SECURE_NOROOT securebit, also skip dropping the bounding set.
2060 * If the caller wants to regain all capabilities when executing a
2061 * set-user-ID-root program, allow them to do so. The default behavior
2062 * (i.e. the behavior without |securebits_skip_mask| set) will still put
2063 * the jailed process tree in a capabilities-only environment.
2064 *
2065 * We check the negated skip mask for SECURE_NOROOT and
2066 * SECURE_NOROOT_LOCKED. If the bits are set in the negated mask they
2067 * will *not* be skipped in lock_securebits(), and therefore we should
2068 * drop the bounding set.
Kees Cook323878a2013-02-05 15:35:24 -08002069 */
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04002070 if (secure_noroot_set_and_locked(~j->securebits_skip_mask)) {
2071 drop_capbset(j->caps, last_valid_cap);
2072 } else {
2073 warn("SECURE_NOROOT not set, not dropping bounding set");
2074 }
Kees Cook323878a2013-02-05 15:35:24 -08002075
2076 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08002077 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08002078 flag[0] = CAP_SETPCAP;
2079 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
2080 die("can't clear effective cap");
2081 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
2082 die("can't clear permitted cap");
2083 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
2084 die("can't clear inheritable cap");
2085 }
2086
2087 if (cap_set_proc(caps))
2088 die("can't apply final cleaned capset");
2089
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04002090 /*
2091 * If ambient capabilities are supported, clear all capabilities first,
2092 * then raise the requested ones.
2093 */
2094 if (j->flags.set_ambient_caps) {
2095 if (!cap_ambient_supported()) {
2096 pdie("ambient capabilities not supported");
2097 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04002098 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
2099 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04002100 pdie("can't clear ambient capabilities");
2101 }
2102
2103 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
2104 if (!(j->caps & (one << i)))
2105 continue;
2106
2107 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
2108 0) != 0) {
2109 pdie("prctl(PR_CAP_AMBIENT, "
2110 "PR_CAP_AMBIENT_RAISE, %u) failed",
2111 i);
2112 }
2113 }
2114 }
2115
Kees Cook323878a2013-02-05 15:35:24 -08002116 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04002117}
2118
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002119static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002120{
2121 /*
2122 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
2123 * in the kernel source tree for an explanation of the parameters.
2124 */
2125 if (j->flags.no_new_privs) {
2126 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
2127 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
2128 }
2129
2130 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07002131 * Code running with ASan
2132 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
2133 * will make system calls not included in the syscall filter policy,
2134 * which will likely crash the program. Skip setting seccomp filter in
2135 * that case.
2136 * 'running_with_asan()' has no inputs and is completely defined at
2137 * build time, so this cannot be used by an attacker to skip setting
2138 * seccomp filter.
2139 */
Evgenii Stepanov3d98f3c2018-08-23 15:06:50 -07002140 if (j->flags.seccomp_filter && running_with_asan()) {
Evgenii Stepanov825828c2018-07-27 11:57:07 -07002141 warn("running with (HW)ASan, not setting seccomp filter");
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07002142 return;
2143 }
2144
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002145 if (j->flags.seccomp_filter) {
Adrian Ratiu8ef61252021-06-08 03:46:24 +03002146 if (seccomp_is_logging_allowed(j)) {
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002147 warn("logging seccomp filter failures");
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04002148 if (!seccomp_ret_log_available()) {
2149 /*
2150 * If SECCOMP_RET_LOG is not available,
2151 * install the SIGSYS handler first.
2152 */
2153 if (install_sigsys_handler())
2154 pdie(
2155 "failed to install SIGSYS handler");
2156 }
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002157 } else if (j->flags.seccomp_filter_tsync) {
2158 /*
2159 * If setting thread sync,
2160 * reset the SIGSYS signal handler so that
2161 * the entire thread group is killed.
2162 */
2163 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
2164 pdie("failed to reset SIGSYS disposition");
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002165 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002166 }
2167
2168 /*
2169 * Install the syscall filter.
2170 */
2171 if (j->flags.seccomp_filter) {
Anand K Mistry31adc6c2020-11-26 11:39:46 +11002172 if (j->flags.seccomp_filter_tsync ||
2173 j->flags.seccomp_filter_allow_speculation) {
2174 int filter_flags =
2175 (j->flags.seccomp_filter_tsync
2176 ? SECCOMP_FILTER_FLAG_TSYNC
2177 : 0) |
2178 (j->flags.seccomp_filter_allow_speculation
2179 ? SECCOMP_FILTER_FLAG_SPEC_ALLOW
2180 : 0);
2181 if (sys_seccomp(SECCOMP_SET_MODE_FILTER, filter_flags,
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002182 j->filter_prog)) {
2183 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002184 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002185 } else {
2186 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
2187 j->filter_prog)) {
2188 pdie("prctl(seccomp_filter) failed");
2189 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002190 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002191 }
2192}
2193
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002194static pid_t forward_pid = -1;
2195
Mike Frysinger33d051a2018-05-30 16:41:10 -04002196static void forward_signal(int sig,
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04002197 siginfo_t *siginfo attribute_unused,
2198 void *void_context attribute_unused)
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002199{
2200 if (forward_pid != -1) {
Mike Frysinger33d051a2018-05-30 16:41:10 -04002201 kill(forward_pid, sig);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002202 }
2203}
2204
2205static void install_signal_handlers(void)
2206{
2207 struct sigaction act;
2208
2209 memset(&act, 0, sizeof(act));
2210 act.sa_sigaction = &forward_signal;
2211 act.sa_flags = SA_SIGINFO | SA_RESTART;
2212
2213 /* Handle all signals, except SIGCHLD. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002214 for (int sig = 1; sig < NSIG; sig++) {
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002215 /*
2216 * We don't care if we get EINVAL: that just means that we
2217 * can't handle this signal, so let's skip it and continue.
2218 */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002219 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002220 }
2221 /* Reset SIGCHLD's handler. */
2222 signal(SIGCHLD, SIG_DFL);
2223
2224 /* Handle real-time signals. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002225 for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++) {
2226 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002227 }
2228}
2229
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002230static const char *lookup_hook_name(minijail_hook_event_t event)
2231{
2232 switch (event) {
2233 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
2234 return "pre-drop-caps";
2235 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
2236 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07002237 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
2238 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002239 case MINIJAIL_HOOK_EVENT_MAX:
2240 /*
2241 * Adding this in favor of a default case to force the
2242 * compiler to error out if a new enum value is added.
2243 */
2244 break;
2245 }
2246 return "unknown";
2247}
2248
2249static void run_hooks_or_die(const struct minijail *j,
2250 minijail_hook_event_t event)
2251{
2252 int rc;
2253 int hook_index = 0;
2254 for (struct hook *c = j->hooks_head; c; c = c->next) {
2255 if (c->event != event)
2256 continue;
2257 rc = c->hook(c->payload);
2258 if (rc != 0) {
2259 errno = -rc;
2260 pdie("%s hook (index %d) failed",
2261 lookup_hook_name(event), hook_index);
2262 }
2263 /* Only increase the index within the same hook event type. */
2264 ++hook_index;
2265 }
2266}
2267
Will Drewry6ac91122011-10-21 16:38:58 -05002268void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002269{
Dylan Reidf682d472015-09-17 21:39:07 -07002270 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002271 * If we're dropping caps, get the last valid cap from /proc now,
2272 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07002273 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002274 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002275 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002276 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07002277
Elly Jonese1749eb2011-10-07 13:54:59 -04002278 if (j->flags.pids)
2279 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002280 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04002281
Lutz Justen13807cb2017-01-03 17:11:55 +01002282 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05002283 die("cannot inherit supplementary groups without setting a "
2284 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04002285
Elly Jonesdd3e8512012-01-23 15:13:38 -05002286 /*
2287 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04002288 * so we don't even try. If any of our operations fail, we abort() the
2289 * entire process.
2290 */
Mike Frysinger902a4492018-12-27 05:22:56 -05002291 if (j->flags.enter_vfs) {
2292 if (setns(j->mountns_fd, CLONE_NEWNS))
2293 pdie("setns(CLONE_NEWNS) failed");
2294 close(j->mountns_fd);
2295 }
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002296
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07002297 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002298 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002299 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002300 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05002301 * By default, remount all filesystems as private, unless
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04002302 * - Passed a specific remount mode, in which case remount with
2303 * that,
2304 * - Asked not to remount at all, in which case skip the
2305 * mount(2) call.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002306 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
2307 */
Mike Frysinger785b1c32018-02-23 15:47:24 -05002308 if (j->remount_mode) {
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04002309 if (mount(NULL, "/", NULL, MS_REC | j->remount_mode,
2310 NULL))
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -05002311 pdie("mount(NULL, /, NULL, "
2312 "MS_REC | j->remount_mode, NULL) failed");
Nicole Anderson-Au835f7172021-01-13 21:18:13 +00002313
2314 struct minijail_remount *temp = j->remounts_head;
2315 while (temp) {
Nicole Anderson-Aue119bbb2021-02-04 23:12:12 +00002316 if (temp->remount_mode < j->remount_mode)
2317 die("cannot remount %s as stricter "
2318 "than the root dir",
2319 temp->mount_name);
Nicole Anderson-Au835f7172021-01-13 21:18:13 +00002320 if (mount(NULL, temp->mount_name, NULL,
2321 MS_REC | temp->remount_mode, NULL))
2322 pdie("mount(NULL, %s, NULL, "
2323 "MS_REC | temp->remount_mode, NULL) "
2324 "failed", temp->mount_name);
2325 temp = temp->next;
2326 }
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002327 }
Nicole Anderson-Au835f7172021-01-13 21:18:13 +00002328
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002329 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04002330
Dylan Reidf7942472015-11-18 17:55:26 -08002331 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002332 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08002333 }
2334
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002335 if (j->flags.uts) {
2336 if (unshare(CLONE_NEWUTS))
2337 pdie("unshare(CLONE_NEWUTS) failed");
2338
2339 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
2340 pdie("sethostname(%s) failed", j->hostname);
2341 }
2342
Dylan Reid1102f5a2015-09-15 11:52:20 -07002343 if (j->flags.enter_net) {
2344 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002345 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger902a4492018-12-27 05:22:56 -05002346 close(j->netns_fd);
Mike Frysinger7559dfe2016-11-15 18:58:39 -05002347 } else if (j->flags.net) {
2348 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002349 pdie("unshare(CLONE_NEWNET) failed");
2350 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07002351 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002352
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002353 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002354 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002355
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08002356 if (j->flags.new_session_keyring) {
2357 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
2358 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
2359 }
2360
Mike Frysingerac08a682017-10-10 02:04:50 -04002361 /* We have to process all the mounts before we chroot/pivot_root. */
2362 process_mounts_or_die(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002363
Mike Frysingerac08a682017-10-10 02:04:50 -04002364 if (j->flags.chroot && enter_chroot(j))
Mike Frysinger33ffef32017-01-13 19:53:19 -05002365 pdie("chroot");
Mike Frysinger33ffef32017-01-13 19:53:19 -05002366
Mike Frysingerac08a682017-10-10 02:04:50 -04002367 if (j->flags.pivot_root && enter_pivot_root(j))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08002368 pdie("pivot_root");
2369
Martin Pelikánab9eb442017-01-25 11:53:58 +11002370 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07002371 pdie("mount_tmp");
2372
Dylan Reid791f5772015-09-14 20:02:42 -07002373 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04002374 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04002375
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002376 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
2377
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002378 /*
2379 * If we're only dropping capabilities from the bounding set, but not
2380 * from the thread's (permitted|inheritable|effective) sets, do it now.
2381 */
2382 if (j->flags.capbset_drop) {
2383 drop_capbset(j->cap_bset, last_valid_cap);
2384 }
2385
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002386 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002387 * POSIX capabilities are a bit tricky. We must set SECBIT_KEEP_CAPS
2388 * before drop_ugid() below as the latter would otherwise drop all
2389 * capabilities.
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002390 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002391 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002392 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002393 * When using ambient capabilities, CAP_SET{GID,UID} can be
2394 * inherited across execve(2), so SECBIT_KEEP_CAPS is not
2395 * strictly needed.
Elly Jonese1749eb2011-10-07 13:54:59 -04002396 */
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002397 bool require_keep_caps = !j->flags.set_ambient_caps;
2398 if (lock_securebits(j->securebits_skip_mask,
2399 require_keep_caps) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002400 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002401 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002402 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002403
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002404 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002405 /*
2406 * If we're setting no_new_privs, we can drop privileges
2407 * before setting seccomp filter. This way filter policies
2408 * don't need to allow privilege-dropping syscalls.
2409 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002410 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002411 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002412 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002413 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002414 /*
2415 * If we're not setting no_new_privs,
2416 * we need to set seccomp filter *before* dropping privileges.
2417 * WARNING: this means that filter policies *must* allow
2418 * setgroups()/setresgid()/setresuid() for dropping root and
2419 * capget()/capset()/prctl() for dropping caps.
2420 */
2421 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002422 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002423 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04002424 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002425
Elly Jonesdd3e8512012-01-23 15:13:38 -05002426 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08002427 * Select the specified alternate syscall table. The table must not
2428 * block prctl(2) if we're using seccomp as well.
2429 */
2430 if (j->flags.alt_syscall) {
2431 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002432 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08002433 }
2434
2435 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05002436 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04002437 * privilege-dropping syscalls :)
2438 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002439 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04002440 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002441 warn("seccomp not supported");
2442 return;
2443 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002444 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002445 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002446}
2447
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002448/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04002449static int init_exitstatus = 0;
2450
Mike Frysinger0a27ab02020-09-04 16:18:12 -04002451static void init_term(int sig attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002452{
2453 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04002454}
2455
Mike Frysinger0a27ab02020-09-04 16:18:12 -04002456static void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04002457{
2458 pid_t pid;
2459 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002460 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002461 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002462 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002463 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002464 /*
2465 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04002466 * left inside our pid namespace or we get a signal.
2467 */
2468 if (pid == rootpid)
2469 init_exitstatus = status;
2470 }
2471 if (!WIFEXITED(init_exitstatus))
2472 _exit(MINIJAIL_ERR_INIT);
2473 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04002474}
2475
Will Drewry6ac91122011-10-21 16:38:58 -05002476int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002477{
2478 size_t sz = 0;
2479 size_t bytes = read(fd, &sz, sizeof(sz));
2480 char *buf;
2481 int r;
2482 if (sizeof(sz) != bytes)
2483 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002484 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04002485 return -E2BIG;
2486 buf = malloc(sz);
2487 if (!buf)
2488 return -ENOMEM;
2489 bytes = read(fd, buf, sz);
2490 if (bytes != sz) {
2491 free(buf);
2492 return -EINVAL;
2493 }
2494 r = minijail_unmarshal(j, buf, sz);
2495 free(buf);
2496 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002497}
2498
Will Drewry6ac91122011-10-21 16:38:58 -05002499int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04002500{
Elly Jonese1749eb2011-10-07 13:54:59 -04002501 size_t sz = minijail_size(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002502 if (!sz)
2503 return -EINVAL;
François Degros664eba72019-11-05 13:18:24 +11002504
2505 char *buf = malloc(sz);
2506 if (!buf)
2507 return -ENOMEM;
2508
2509 int err = minijail_marshal(j, buf, sz);
2510 if (err)
2511 goto error;
2512
Elly Jonese1749eb2011-10-07 13:54:59 -04002513 /* Sends [size][minijail]. */
François Degros664eba72019-11-05 13:18:24 +11002514 err = write_exactly(fd, &sz, sizeof(sz));
2515 if (err)
2516 goto error;
2517
2518 err = write_exactly(fd, buf, sz);
2519
2520error:
Elly Jonese1749eb2011-10-07 13:54:59 -04002521 free(buf);
François Degros664eba72019-11-05 13:18:24 +11002522 return err;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002523}
Elly Jonescd7a9042011-07-22 13:56:51 -04002524
Dylan Reid6dc224f2021-05-12 17:06:25 -07002525int API minijail_copy_jail(const struct minijail *from, struct minijail *out)
2526{
2527 size_t sz = minijail_size(from);
2528 if (!sz)
2529 return -EINVAL;
2530
2531 char *buf = malloc(sz);
2532 if (!buf)
2533 return -ENOMEM;
2534
2535 int err = minijail_marshal(from, buf, sz);
2536 if (err)
2537 goto error;
2538
2539 err = minijail_unmarshal(out, buf, sz);
2540error:
2541 free(buf);
2542 return err;
2543}
2544
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002545static int setup_preload(const struct minijail *j attribute_unused,
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002546 char ***child_env attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002547{
Daniel Erat5b7a3182015-08-19 16:06:22 -06002548#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002549 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002550 return 0;
2551#else
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002552 const char *preload_path = j->preload_path ?: PRELOADPATH;
2553 char *newenv = NULL;
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002554 int ret = 0;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002555 const char *oldenv = getenv(kLdPreloadEnvVar);
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002556
2557 if (!oldenv)
2558 oldenv = "";
Elly Jonescd7a9042011-07-22 13:56:51 -04002559
Elly Jonese1749eb2011-10-07 13:54:59 -04002560 /* Only insert a separating space if we have something to separate... */
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002561 if (asprintf(&newenv, "%s%s%s", oldenv, oldenv[0] != '\0' ? " " : "",
2562 preload_path) < 0) {
2563 return -1;
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002564 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002565
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002566 ret = minijail_setenv(child_env, kLdPreloadEnvVar, newenv, 1);
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002567 free(newenv);
2568 return ret;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002569#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04002570}
2571
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002572static int setup_pipe(char ***child_env, int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04002573{
2574 int r = pipe(fds);
2575 char fd_buf[11];
2576 if (r)
2577 return r;
2578 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2579 if (r <= 0)
2580 return -EINVAL;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002581 return minijail_setenv(child_env, kFdEnvVar, fd_buf, 1);
Will Drewryf89aef52011-09-16 16:48:57 -05002582}
2583
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002584static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002585{
2586 const char *kFdPath = "/proc/self/fd";
2587
2588 DIR *d = opendir(kFdPath);
2589 struct dirent *dir_entry;
2590
2591 if (d == NULL)
2592 return -1;
2593 int dir_fd = dirfd(d);
2594 while ((dir_entry = readdir(d)) != NULL) {
2595 size_t i;
2596 char *end;
2597 bool should_close = true;
2598 const int fd = strtol(dir_entry->d_name, &end, 10);
2599
2600 if ((*end) != '\0') {
2601 continue;
2602 }
2603 /*
2604 * We might have set up some pipes that we want to share with
2605 * the parent process, and should not be closed.
2606 */
2607 for (i = 0; i < size; ++i) {
2608 if (fd == inheritable_fds[i]) {
2609 should_close = false;
2610 break;
2611 }
2612 }
2613 /* Also avoid closing the directory fd. */
2614 if (should_close && fd != dir_fd)
2615 close(fd);
2616 }
2617 closedir(d);
2618 return 0;
2619}
2620
Allen Webbc7182682021-04-16 09:44:53 -05002621/* Return true if the specified file descriptor is already open. */
2622static int fd_is_open(int fd)
2623{
2624 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
2625}
2626
2627static_assert(FD_SETSIZE >= MAX_PRESERVED_FDS * 2 - 1,
2628 "If true, ensure_no_fd_conflict will always find an unused fd.");
2629
Allen Webb66417bd2021-07-16 15:07:24 -07002630/* If parent_fd will be used by a child fd, move it to an unused fd. */
Allen Webbc7182682021-04-16 09:44:53 -05002631static int ensure_no_fd_conflict(const fd_set* child_fds,
Allen Webb66417bd2021-07-16 15:07:24 -07002632 int child_fd, int *parent_fd)
Allen Webbc7182682021-04-16 09:44:53 -05002633{
Allen Webb66417bd2021-07-16 15:07:24 -07002634 if (!FD_ISSET(*parent_fd, child_fds)) {
Allen Webbc7182682021-04-16 09:44:53 -05002635 return 0;
2636 }
2637
2638 /*
2639 * If no other parent_fd matches the child_fd then use it instead of a
2640 * temporary.
2641 */
Allen Webb66417bd2021-07-16 15:07:24 -07002642 int fd = child_fd;
2643 if (fd == -1 || fd_is_open(fd)) {
Allen Webbc7182682021-04-16 09:44:53 -05002644 fd = FD_SETSIZE - 1;
2645 while (FD_ISSET(fd, child_fds) || fd_is_open(fd)) {
2646 --fd;
2647 if (fd < 0) {
2648 die("failed to find an unused fd");
2649 }
2650 }
2651 }
2652
Allen Webb66417bd2021-07-16 15:07:24 -07002653 int ret = dup2(*parent_fd, fd);
Allen Webbc7182682021-04-16 09:44:53 -05002654 /*
2655 * warn() opens a file descriptor so it needs to happen after dup2 to
2656 * avoid unintended side effects. This can be avoided by reordering the
2657 * mapping requests so that the source fds with overlap are mapped
2658 * first (unless there are cycles).
2659 */
Allen Webb66417bd2021-07-16 15:07:24 -07002660 warn("mapped fd overlap: moving %d to %d", *parent_fd, fd);
Allen Webbc7182682021-04-16 09:44:53 -05002661 if (ret == -1) {
2662 return -1;
2663 }
2664
Allen Webb66417bd2021-07-16 15:07:24 -07002665 *parent_fd = fd;
Allen Webbc7182682021-04-16 09:44:53 -05002666 return 0;
2667}
2668
Allen Webb66417bd2021-07-16 15:07:24 -07002669/*
2670 * Structure holding resources and state created when running a minijail.
2671 */
2672struct minijail_run_state {
2673 pid_t child_pid;
2674 int pipe_fds[2];
2675 int stdin_fds[2];
2676 int stdout_fds[2];
2677 int stderr_fds[2];
2678 int child_sync_pipe_fds[2];
2679 char **child_env;
2680};
2681
2682static int redirect_fds(struct minijail *j, struct minijail_run_state *state)
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002683{
Allen Webbc7182682021-04-16 09:44:53 -05002684 fd_set child_fds;
2685 FD_ZERO(&child_fds);
2686
2687 /* Relocate parent_fds that would be replaced by a child_fd. */
2688 for (size_t i = 0; i < j->preserved_fd_count; i++) {
2689 int child_fd = j->preserved_fds[i].child_fd;
2690 if (FD_ISSET(child_fd, &child_fds)) {
2691 die("fd %d is mapped more than once", child_fd);
2692 }
2693
Allen Webb66417bd2021-07-16 15:07:24 -07002694 int *parent_fd = &j->preserved_fds[i].parent_fd;
2695 if (ensure_no_fd_conflict(&child_fds, child_fd,
2696 parent_fd) == -1) {
Allen Webbc7182682021-04-16 09:44:53 -05002697 return -1;
2698 }
2699
2700 FD_SET(child_fd, &child_fds);
2701 }
2702
Allen Webb66417bd2021-07-16 15:07:24 -07002703 /* Move pipe_fds if they conflict with a child_fd. */
2704 int *pipe_fds[] = {
2705 state->pipe_fds, state->child_sync_pipe_fds,
2706 state->stdin_fds, state->stdout_fds,
2707 state->stderr_fds,
2708 };
2709 for (size_t i = 0; i < ARRAY_SIZE(pipe_fds); ++i) {
2710 if (pipe_fds[i][0] != -1 &&
2711 ensure_no_fd_conflict(&child_fds, -1,
2712 &pipe_fds[i][0]) == -1) {
2713 return -1;
2714 }
2715 if (pipe_fds[i][1] != -1 &&
2716 ensure_no_fd_conflict(&child_fds, -1,
2717 &pipe_fds[i][1]) == -1) {
2718 return -1;
2719 }
2720 }
2721
2722 /* Perform redirection. */
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002723 for (size_t i = 0; i < j->preserved_fd_count; i++) {
Luis Héctor Cháveza63407a2021-01-03 05:47:00 -08002724 if (j->preserved_fds[i].parent_fd ==
2725 j->preserved_fds[i].child_fd) {
Allen Webb1e925f72021-07-16 15:39:29 -05002726 // Clear CLOEXEC if it is set so the FD will be
2727 // inherited by the child.
2728 int flags =
2729 fcntl(j->preserved_fds[i].child_fd, F_GETFD);
2730 if (flags == -1 || (flags & FD_CLOEXEC) == 0) {
2731 continue;
2732 }
2733
2734 // Currently FD_CLOEXEC is cleared without being
2735 // restored. It may make sense to track when this
2736 // happens and restore FD_CLOEXEC in the child process.
2737 flags &= ~FD_CLOEXEC;
2738 if (fcntl(j->preserved_fds[i].child_fd, F_SETFD,
2739 flags) == -1) {
2740 pwarn("failed to clear CLOEXEC for %d",
2741 j->preserved_fds[i].parent_fd);
2742 }
Luis Héctor Cháveza63407a2021-01-03 05:47:00 -08002743 continue;
2744 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002745 if (dup2(j->preserved_fds[i].parent_fd,
2746 j->preserved_fds[i].child_fd) == -1) {
2747 return -1;
2748 }
2749 }
Allen Webb66417bd2021-07-16 15:07:24 -07002750
Mattias Nissler1cf29fb2020-04-20 23:14:03 +02002751 /*
2752 * After all fds have been duped, we are now free to close all parent
2753 * fds that are *not* child fds.
2754 */
2755 for (size_t i = 0; i < j->preserved_fd_count; i++) {
Allen Webbc7182682021-04-16 09:44:53 -05002756 int parent_fd = j->preserved_fds[i].parent_fd;
2757 if (!FD_ISSET(parent_fd, &child_fds)) {
2758 close(parent_fd);
Mattias Nissler1cf29fb2020-04-20 23:14:03 +02002759 }
Mattias Nissler1cf29fb2020-04-20 23:14:03 +02002760 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002761 return 0;
2762}
2763
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002764static void minijail_free_run_state(struct minijail_run_state *state)
2765{
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002766 state->child_pid = -1;
2767
2768 int *fd_pairs[] = {state->pipe_fds, state->stdin_fds, state->stdout_fds,
2769 state->stderr_fds, state->child_sync_pipe_fds};
2770 for (size_t i = 0; i < ARRAY_SIZE(fd_pairs); ++i) {
2771 close_and_reset(&fd_pairs[i][0]);
2772 close_and_reset(&fd_pairs[i][1]);
2773 }
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002774
2775 minijail_free_env(state->child_env);
2776 state->child_env = NULL;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002777}
2778
2779/* Set up stdin/stdout/stderr file descriptors in the child. */
2780static void setup_child_std_fds(struct minijail *j,
2781 struct minijail_run_state *state)
2782{
2783 struct {
2784 const char *name;
2785 int from;
2786 int to;
2787 } fd_map[] = {
2788 {"stdin", state->stdin_fds[0], STDIN_FILENO},
2789 {"stdout", state->stdout_fds[1], STDOUT_FILENO},
2790 {"stderr", state->stderr_fds[1], STDERR_FILENO},
2791 };
2792
2793 for (size_t i = 0; i < ARRAY_SIZE(fd_map); ++i) {
Luis Héctor Cháveza63407a2021-01-03 05:47:00 -08002794 if (fd_map[i].from == -1 || fd_map[i].from == fd_map[i].to)
2795 continue;
2796 if (dup2(fd_map[i].from, fd_map[i].to) == -1)
2797 die("failed to set up %s pipe", fd_map[i].name);
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002798 }
2799
2800 /* Close temporary pipe file descriptors. */
2801 int *std_pipes[] = {state->stdin_fds, state->stdout_fds,
2802 state->stderr_fds};
2803 for (size_t i = 0; i < ARRAY_SIZE(std_pipes); ++i) {
2804 close_and_reset(&std_pipes[i][0]);
2805 close_and_reset(&std_pipes[i][1]);
2806 }
2807
2808 /*
2809 * If any of stdin, stdout, or stderr are TTYs, or setsid flag is
2810 * set, create a new session. This prevents the jailed process from
2811 * using the TIOCSTI ioctl to push characters into the parent process
2812 * terminal's input buffer, therefore escaping the jail.
2813 *
2814 * Since it has just forked, the child will not be a process group
2815 * leader, and this call to setsid() should always succeed.
2816 */
2817 if (j->flags.setsid || isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2818 isatty(STDERR_FILENO)) {
2819 if (setsid() < 0) {
2820 pdie("setsid() failed");
2821 }
2822 }
2823}
2824
2825/*
Dylan Reidacfb8be2017-08-25 12:56:51 -07002826 * Structure that specifies how to start a minijail.
2827 *
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002828 * filename - The program to exec in the child. Required if |exec_in_child| = 1.
2829 * argv - Arguments for the child program. Required if |exec_in_child| = 1.
2830 * envp - Environment for the child program. Available if |exec_in_child| = 1.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002831 * use_preload - If true use LD_PRELOAD.
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002832 * exec_in_child - If true, run |filename|. Otherwise, the child will return to
Dylan Reid0412dcc2017-08-24 11:33:15 -07002833 * the caller.
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002834 * pstdin_fd - Filled with stdin pipe if non-NULL.
2835 * pstdout_fd - Filled with stdout pipe if non-NULL.
2836 * pstderr_fd - Filled with stderr pipe if non-NULL.
2837 * pchild_pid - Filled with the pid of the child process if non-NULL.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002838 */
2839struct minijail_run_config {
2840 const char *filename;
2841 char *const *argv;
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002842 char *const *envp;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002843 int use_preload;
Dylan Reid0412dcc2017-08-24 11:33:15 -07002844 int exec_in_child;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002845 int *pstdin_fd;
2846 int *pstdout_fd;
2847 int *pstderr_fd;
2848 pid_t *pchild_pid;
2849};
2850
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002851static int
2852minijail_run_config_internal(struct minijail *j,
2853 const struct minijail_run_config *config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002854
Will Drewry6ac91122011-10-21 16:38:58 -05002855int API minijail_run(struct minijail *j, const char *filename,
2856 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002857{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002858 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002859 .filename = filename,
2860 .argv = argv,
2861 .envp = NULL,
2862 .use_preload = true,
2863 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002864 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002865 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002866}
2867
2868int API minijail_run_pid(struct minijail *j, const char *filename,
2869 char *const argv[], pid_t *pchild_pid)
2870{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002871 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002872 .filename = filename,
2873 .argv = argv,
2874 .envp = NULL,
2875 .use_preload = true,
2876 .exec_in_child = true,
2877 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002878 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002879 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002880}
2881
2882int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002883 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002884{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002885 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002886 .filename = filename,
2887 .argv = argv,
2888 .envp = NULL,
2889 .use_preload = true,
2890 .exec_in_child = true,
2891 .pstdin_fd = pstdin_fd,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002892 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002893 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002894}
2895
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002896int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002897 char *const argv[], pid_t *pchild_pid,
2898 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002899{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002900 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002901 .filename = filename,
2902 .argv = argv,
2903 .envp = NULL,
2904 .use_preload = true,
2905 .exec_in_child = true,
2906 .pstdin_fd = pstdin_fd,
2907 .pstdout_fd = pstdout_fd,
2908 .pstderr_fd = pstderr_fd,
2909 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002910 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002911 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002912}
2913
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002914int API minijail_run_env_pid_pipes(struct minijail *j, const char *filename,
2915 char *const argv[], char *const envp[],
2916 pid_t *pchild_pid, int *pstdin_fd,
2917 int *pstdout_fd, int *pstderr_fd)
2918{
2919 struct minijail_run_config config = {
2920 .filename = filename,
2921 .argv = argv,
2922 .envp = envp,
2923 .use_preload = true,
2924 .exec_in_child = true,
2925 .pstdin_fd = pstdin_fd,
2926 .pstdout_fd = pstdout_fd,
2927 .pstderr_fd = pstderr_fd,
2928 .pchild_pid = pchild_pid,
2929 };
2930 return minijail_run_config_internal(j, &config);
2931}
2932
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002933int API minijail_run_no_preload(struct minijail *j, const char *filename,
2934 char *const argv[])
2935{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002936 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002937 .filename = filename,
2938 .argv = argv,
2939 .envp = NULL,
2940 .use_preload = false,
2941 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002942 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002943 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002944}
2945
Samuel Tan63187f42015-10-16 13:01:53 -07002946int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002947 const char *filename,
2948 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002949 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002950 int *pstdin_fd,
2951 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002952 int *pstderr_fd)
2953{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002954 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002955 .filename = filename,
2956 .argv = argv,
2957 .envp = NULL,
2958 .use_preload = false,
2959 .exec_in_child = true,
2960 .pstdin_fd = pstdin_fd,
2961 .pstdout_fd = pstdout_fd,
2962 .pstderr_fd = pstderr_fd,
2963 .pchild_pid = pchild_pid,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002964 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002965 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002966}
2967
2968int API minijail_run_env_pid_pipes_no_preload(struct minijail *j,
2969 const char *filename,
2970 char *const argv[],
2971 char *const envp[],
2972 pid_t *pchild_pid, int *pstdin_fd,
2973 int *pstdout_fd, int *pstderr_fd)
2974{
2975 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002976 .filename = filename,
2977 .argv = argv,
2978 .envp = envp,
2979 .use_preload = false,
2980 .exec_in_child = true,
2981 .pstdin_fd = pstdin_fd,
2982 .pstdout_fd = pstdout_fd,
2983 .pstderr_fd = pstderr_fd,
2984 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002985 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002986 return minijail_run_config_internal(j, &config);
Samuel Tan63187f42015-10-16 13:01:53 -07002987}
2988
Dylan Reid0412dcc2017-08-24 11:33:15 -07002989pid_t API minijail_fork(struct minijail *j)
2990{
2991 struct minijail_run_config config = {};
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002992 return minijail_run_config_internal(j, &config);
Dylan Reid0412dcc2017-08-24 11:33:15 -07002993}
2994
Dylan Reid18c49c82017-08-25 14:52:27 -07002995static int minijail_run_internal(struct minijail *j,
2996 const struct minijail_run_config *config,
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002997 struct minijail_run_state *state_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002998{
Dylan Reidce5b55e2016-01-13 11:04:16 -08002999 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04003000 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04003001 /* We need to remember this across the minijail_preexec() call. */
3002 int pid_namespace = j->flags.pids;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -07003003 /*
3004 * Create an init process if we are entering a pid namespace, unless the
3005 * user has explicitly opted out by calling minijail_run_as_init().
3006 */
3007 int do_init = j->flags.do_init && !j->flags.run_as_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07003008 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07003009
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003010 if (use_preload) {
Dylan Reid0412dcc2017-08-24 11:33:15 -07003011 if (j->hooks_head != NULL)
3012 die("Minijail hooks are not supported with LD_PRELOAD");
3013 if (!config->exec_in_child)
3014 die("minijail_fork is not supported with LD_PRELOAD");
3015
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003016 /*
3017 * Before we fork(2) and execve(2) the child process, we need
3018 * to open a pipe(2) to send the minijail configuration over.
3019 */
3020 state_out->child_env =
3021 minijail_copy_env(config->envp ? config->envp : environ);
3022 if (!state_out->child_env)
3023 return ENOMEM;
3024 if (setup_preload(j, &state_out->child_env) ||
3025 setup_pipe(&state_out->child_env, state_out->pipe_fds))
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003026 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04003027 }
Will Drewryf89aef52011-09-16 16:48:57 -05003028
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003029 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07003030 if (j->flags.use_caps && j->caps != 0 &&
3031 !j->flags.set_ambient_caps) {
3032 die("non-empty, non-ambient capabilities are not "
3033 "supported without LD_PRELOAD");
3034 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003035 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05003036
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003037 /* Create pipes for stdin/stdout/stderr as requested by caller. */
3038 struct {
3039 bool requested;
3040 int *pipe_fds;
3041 } pipe_fd_req[] = {
3042 {config->pstdin_fd != NULL, state_out->stdin_fds},
3043 {config->pstdout_fd != NULL, state_out->stdout_fds},
3044 {config->pstderr_fd != NULL, state_out->stderr_fds},
3045 };
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07003046
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003047 for (size_t i = 0; i < ARRAY_SIZE(pipe_fd_req); ++i) {
3048 if (pipe_fd_req[i].requested &&
3049 pipe(pipe_fd_req[i].pipe_fds) == -1)
3050 return EFAULT;
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08003051 }
3052
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003053 /*
François Degros664eba72019-11-05 13:18:24 +11003054 * If the parent process needs to configure the child's runtime
3055 * environment after forking, create a pipe(2) to block the child until
3056 * configuration is done.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003057 */
Daniel Erat2d69add2019-04-23 20:58:53 -07003058 if (j->flags.forward_signals || j->flags.pid_file || j->flags.cgroups ||
3059 j->rlimit_count || j->flags.userns) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08003060 sync_child = 1;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003061 if (pipe(state_out->child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003062 return -EFAULT;
3063 }
3064
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08003065 /*
3066 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04003067 *
3068 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
3069 *
3070 * In multithreaded programs, there are a bunch of locks inside libc,
3071 * some of which may be held by other threads at the time that we call
3072 * minijail_run_pid(). If we call fork(), glibc does its level best to
3073 * ensure that we hold all of these locks before it calls clone()
3074 * internally and drop them after clone() returns, but when we call
3075 * sys_clone(2) directly, all that gets bypassed and we end up with a
3076 * child address space where some of libc's important locks are held by
3077 * other threads (which did not get cloned, and hence will never release
3078 * those locks). This is okay so long as we call exec() immediately
3079 * after, but a bunch of seemingly-innocent libc functions like setenv()
3080 * take locks.
3081 *
3082 * Hence, only call sys_clone() if we need to, in order to get at pid
3083 * namespacing. If we follow this path, the child's address space might
3084 * have broken locks; you may only call functions that do not acquire
3085 * any locks.
3086 *
3087 * Unfortunately, fork() acquires every lock it can get its hands on, as
3088 * previously detailed, so this function is highly likely to deadlock
3089 * later on (see "deadlock here") if we're multithreaded.
3090 *
3091 * We might hack around this by having the clone()d child (init of the
3092 * pid namespace) return directly, rather than leaving the clone()d
3093 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08003094 * its fork()ed child return in turn), but that process would be
3095 * crippled with its libc locks potentially broken. We might try
3096 * fork()ing in the parent before we clone() to ensure that we own all
3097 * the locks, but then we have to have the forked child hanging around
3098 * consuming resources (and possibly having file descriptors / shared
3099 * memory regions / etc attached). We'd need to keep the child around to
3100 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04003101 *
3102 * TODO(ellyjones): figure out if the "forked child hanging around"
3103 * problem is fixable or not. It would be nice if we worked in this
3104 * case.
3105 */
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003106 pid_t child_pid;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003107 if (pid_namespace) {
François Degros94619842019-11-08 16:37:55 +11003108 unsigned long clone_flags = CLONE_NEWPID | SIGCHLD;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003109 if (j->flags.userns)
3110 clone_flags |= CLONE_NEWUSER;
François Degros94619842019-11-08 16:37:55 +11003111
3112 child_pid = syscall(SYS_clone, clone_flags, NULL, 0L, 0L, 0L);
3113
3114 if (child_pid < 0) {
3115 if (errno == EPERM)
3116 pdie("clone(CLONE_NEWPID | ...) failed with EPERM; "
3117 "is this process missing CAP_SYS_ADMIN?");
3118 pdie("clone(CLONE_NEWPID | ...) failed");
3119 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003120 } else {
Elly Jones761b7412012-06-13 15:49:52 -04003121 child_pid = fork();
3122
François Degros94619842019-11-08 16:37:55 +11003123 if (child_pid < 0)
3124 pdie("fork failed");
Elly Jonese1749eb2011-10-07 13:54:59 -04003125 }
Will Drewryf89aef52011-09-16 16:48:57 -05003126
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003127 state_out->child_pid = child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04003128 if (child_pid) {
Elly Jonese1749eb2011-10-07 13:54:59 -04003129 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07003130
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04003131 if (j->flags.forward_signals) {
3132 forward_pid = child_pid;
3133 install_signal_handlers();
3134 }
3135
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08003136 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04003137 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08003138
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08003139 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04003140 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08003141
Dylan Reid0f72ef42017-06-06 15:42:49 -07003142 if (j->rlimit_count)
3143 set_rlimits_or_die(j);
3144
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003145 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04003146 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08003147
Mike Frysinger902a4492018-12-27 05:22:56 -05003148 if (j->flags.enter_vfs)
3149 close(j->mountns_fd);
3150
3151 if (j->flags.enter_net)
3152 close(j->netns_fd);
3153
Dylan Reidce5b55e2016-01-13 11:04:16 -08003154 if (sync_child)
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003155 parent_setup_complete(state_out->child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003156
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003157 if (use_preload) {
François Degros664eba72019-11-05 13:18:24 +11003158 /*
3159 * Add SIGPIPE to the signal mask to avoid getting
3160 * killed if the child process finishes or closes its
3161 * end of the pipe prematurely.
3162 *
3163 * TODO(crbug.com/1022170): Use pthread_sigmask instead
3164 * of sigprocmask if Minijail is used in multithreaded
3165 * programs.
3166 */
3167 sigset_t to_block, to_restore;
3168 if (sigemptyset(&to_block) < 0)
3169 pdie("sigemptyset failed");
3170 if (sigaddset(&to_block, SIGPIPE) < 0)
3171 pdie("sigaddset failed");
3172 if (sigprocmask(SIG_BLOCK, &to_block, &to_restore) < 0)
3173 pdie("sigprocmask failed");
3174
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003175 /* Send marshalled minijail. */
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003176 close_and_reset(&state_out->pipe_fds[0]);
3177 ret = minijail_to_fd(j, state_out->pipe_fds[1]);
3178 close_and_reset(&state_out->pipe_fds[1]);
François Degros664eba72019-11-05 13:18:24 +11003179
3180 /* Accept any pending SIGPIPE. */
3181 while (true) {
3182 const struct timespec zero_time = {0, 0};
3183 const int sig = sigtimedwait(&to_block, NULL, &zero_time);
3184 if (sig < 0) {
3185 if (errno != EINTR)
3186 break;
3187 } else {
3188 if (sig != SIGPIPE)
3189 die("unexpected signal %d", sig);
3190 }
3191 }
3192
3193 /* Restore the signal mask to its original state. */
3194 if (sigprocmask(SIG_SETMASK, &to_restore, NULL) < 0)
3195 pdie("sigprocmask failed");
3196
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003197 if (ret) {
François Degros664eba72019-11-05 13:18:24 +11003198 warn("failed to send marshalled minijail: %s",
3199 strerror(-ret));
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003200 kill(j->initpid, SIGKILL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003201 }
Elly Jonese1749eb2011-10-07 13:54:59 -04003202 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07003203
Elly Jonese1749eb2011-10-07 13:54:59 -04003204 return 0;
3205 }
Ben Chan541c7e52011-08-26 14:55:53 -07003206
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003207 /* Child process. */
Peter Qiu2860c462015-12-16 15:13:06 -08003208 if (j->flags.reset_signal_mask) {
3209 sigset_t signal_mask;
3210 if (sigemptyset(&signal_mask) != 0)
3211 pdie("sigemptyset failed");
3212 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
3213 pdie("sigprocmask failed");
3214 }
3215
Luis Hector Chaveza27118a2018-04-04 08:18:01 -07003216 if (j->flags.reset_signal_handlers) {
3217 int signum;
3218 for (signum = 0; signum <= SIGRTMAX; signum++) {
3219 /*
3220 * Ignore EINVAL since some signal numbers in the range
3221 * might not be valid.
3222 */
3223 if (signal(signum, SIG_DFL) == SIG_ERR &&
3224 errno != EINVAL) {
3225 pdie("failed to reset signal %d disposition",
3226 signum);
3227 }
3228 }
3229 }
3230
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003231 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07003232 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003233 int inheritable_fds[kMaxInheritableFdsSize];
3234 size_t size = 0;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003235
3236 int *pipe_fds[] = {
3237 state_out->pipe_fds, state_out->child_sync_pipe_fds,
3238 state_out->stdin_fds, state_out->stdout_fds,
3239 state_out->stderr_fds,
3240 };
3241
3242 for (size_t i = 0; i < ARRAY_SIZE(pipe_fds); ++i) {
3243 if (pipe_fds[i][0] != -1) {
3244 inheritable_fds[size++] = pipe_fds[i][0];
3245 }
3246 if (pipe_fds[i][1] != -1) {
3247 inheritable_fds[size++] = pipe_fds[i][1];
3248 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003249 }
Jorge Lucangeli Obes2337f802019-07-18 14:46:03 -04003250
Jorge Lucangeli Obescf3bbea2019-07-24 09:06:40 -04003251 /*
3252 * Preserve namespace file descriptors over the close_open_fds()
3253 * call. These are closed in minijail_enter() so they won't leak
3254 * into the child process.
3255 */
Jorge Lucangeli Obes2337f802019-07-18 14:46:03 -04003256 if (j->flags.enter_vfs)
3257 minijail_preserve_fd(j, j->mountns_fd, j->mountns_fd);
3258 if (j->flags.enter_net)
3259 minijail_preserve_fd(j, j->netns_fd, j->netns_fd);
3260
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003261 for (size_t i = 0; i < j->preserved_fd_count; i++) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07003262 /*
3263 * Preserve all parent_fds. They will be dup2(2)-ed in
3264 * the child later.
3265 */
3266 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
3267 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003268
3269 if (close_open_fds(inheritable_fds, size) < 0)
3270 die("failed to close open file descriptors");
3271 }
3272
Allen Webb66417bd2021-07-16 15:07:24 -07003273 if (redirect_fds(j, state_out))
Luis Hector Chavez1617f632017-08-01 18:32:30 -07003274 die("failed to set up fd redirections");
3275
Dylan Reidce5b55e2016-01-13 11:04:16 -08003276 if (sync_child)
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003277 wait_for_parent_setup(state_out->child_sync_pipe_fds);
Dylan Reidce5b55e2016-01-13 11:04:16 -08003278
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003279 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08003280 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003281
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003282 setup_child_std_fds(j, state_out);
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05003283
Dylan Reid791f5772015-09-14 20:02:42 -07003284 /* If running an init program, let it decide when/how to mount /proc. */
3285 if (pid_namespace && !do_init)
3286 j->flags.remount_proc_ro = 0;
3287
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003288 if (use_preload) {
3289 /* Strip out flags that cannot be inherited across execve(2). */
3290 minijail_preexec(j);
3291 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003292 /*
3293 * If not using LD_PRELOAD, do all jailing before execve(2).
3294 * Note that PID namespaces can only be entered on fork(2),
3295 * so that flag is still cleared.
3296 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003297 j->flags.pids = 0;
3298 }
Dylan Reid0412dcc2017-08-24 11:33:15 -07003299
3300 /*
3301 * Jail this process.
3302 * If forking, return.
3303 * If not, execve(2) the target.
3304 */
Elly Jonese1749eb2011-10-07 13:54:59 -04003305 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003306
Dylan Reid0412dcc2017-08-24 11:33:15 -07003307 if (config->exec_in_child && pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05003308 /*
3309 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003310 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003311 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003312 * a child to actually run the program. If |do_init == 0|, we
3313 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04003314 *
3315 * If we're multithreaded, we'll probably deadlock here. See
3316 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04003317 */
3318 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003319 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04003320 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003321 } else if (child_pid > 0) {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003322 minijail_free_run_state(state_out);
3323
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04003324 /*
3325 * Best effort. Don't bother checking the return value.
3326 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003327 prctl(PR_SET_NAME, "minijail-init");
3328 init(child_pid); /* Never returns. */
3329 }
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003330 state_out->child_pid = child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04003331 }
Elly Jonescd7a9042011-07-22 13:56:51 -04003332
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07003333 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
3334
Dylan Reid0412dcc2017-08-24 11:33:15 -07003335 if (!config->exec_in_child)
3336 return 0;
3337
Elly Jonesdd3e8512012-01-23 15:13:38 -05003338 /*
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003339 * We're going to execve(), so make sure any remaining resources are
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003340 * freed. Exceptions are:
3341 * 1. The child environment. No need to worry about freeing it since
3342 * execve reinitializes the heap anyways.
3343 * 2. The read side of the LD_PRELOAD pipe, which we need to hand down
3344 * into the target in which the preloaded code will read from it and
3345 * then close it.
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003346 */
3347 state_out->pipe_fds[0] = -1;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003348 char *const *child_env = state_out->child_env;
3349 state_out->child_env = NULL;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003350 minijail_free_run_state(state_out);
3351
3352 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003353 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04003354 * calling process
3355 * -> execve()-ing process
3356 * If we are:
3357 * calling process
3358 * -> init()-ing process
3359 * -> execve()-ing process
3360 */
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003361 if (!child_env)
3362 child_env = config->envp ? config->envp : environ;
François Degros08b10f72019-10-09 12:44:05 +11003363 execve(config->filename, config->argv, child_env);
3364
3365 ret = (errno == ENOENT ? MINIJAIL_ERR_NO_COMMAND : MINIJAIL_ERR_NO_ACCESS);
3366 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003367 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04003368}
3369
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003370static int
3371minijail_run_config_internal(struct minijail *j,
3372 const struct minijail_run_config *config)
3373{
3374 struct minijail_run_state state = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003375 .child_pid = -1,
3376 .pipe_fds = {-1, -1},
3377 .stdin_fds = {-1, -1},
3378 .stdout_fds = {-1, -1},
3379 .stderr_fds = {-1, -1},
3380 .child_sync_pipe_fds = {-1, -1},
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003381 .child_env = NULL,
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003382 };
3383 int ret = minijail_run_internal(j, config, &state);
3384
3385 if (ret == 0) {
3386 if (config->pchild_pid)
3387 *config->pchild_pid = state.child_pid;
3388
3389 /* Grab stdin/stdout/stderr descriptors requested by caller. */
3390 struct {
3391 int *pfd;
3392 int *psrc;
3393 } fd_map[] = {
3394 {config->pstdin_fd, &state.stdin_fds[1]},
3395 {config->pstdout_fd, &state.stdout_fds[0]},
3396 {config->pstderr_fd, &state.stderr_fds[0]},
3397 };
3398
3399 for (size_t i = 0; i < ARRAY_SIZE(fd_map); ++i) {
3400 if (fd_map[i].pfd) {
3401 *fd_map[i].pfd = *fd_map[i].psrc;
3402 *fd_map[i].psrc = -1;
3403 }
3404 }
3405
3406 if (!config->exec_in_child)
3407 ret = state.child_pid;
3408 }
3409
3410 minijail_free_run_state(&state);
3411
3412 return ret;
3413}
3414
Victor Hsieh14036062021-04-30 15:40:36 -07003415static int minijail_wait_internal(struct minijail *j, int expected_signal)
Elly Jonese1749eb2011-10-07 13:54:59 -04003416{
François Degros08b10f72019-10-09 12:44:05 +11003417 if (j->initpid <= 0)
3418 return -ECHILD;
3419
Elly Jonese1749eb2011-10-07 13:54:59 -04003420 int st;
François Degros627deba2019-10-01 12:48:25 +10003421 while (true) {
3422 const int ret = waitpid(j->initpid, &st, 0);
3423 if (ret >= 0)
3424 break;
3425 if (errno != EINTR)
3426 return -errno;
3427 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003428
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003429 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003430 int error_status = st;
3431 if (WIFSIGNALED(st)) {
3432 int signum = WTERMSIG(st);
Victor Hsieh14036062021-04-30 15:40:36 -07003433 if (signum != expected_signal) {
3434 warn("child process %d received signal %d",
3435 j->initpid, signum);
3436 }
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003437 /*
3438 * We return MINIJAIL_ERR_JAIL if the process received
3439 * SIGSYS, which happens when a syscall is blocked by
3440 * seccomp filters.
3441 * If not, we do what bash(1) does:
3442 * $? = 128 + signum
3443 */
3444 if (signum == SIGSYS) {
3445 error_status = MINIJAIL_ERR_JAIL;
3446 } else {
François Degros08b10f72019-10-09 12:44:05 +11003447 error_status = MINIJAIL_ERR_SIG_BASE + signum;
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003448 }
3449 }
3450 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003451 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003452
3453 int exit_status = WEXITSTATUS(st);
3454 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07003455 info("child process %d exited with status %d",
3456 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003457
3458 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04003459}
3460
Victor Hsieh14036062021-04-30 15:40:36 -07003461int API minijail_kill(struct minijail *j)
3462{
3463 if (j->initpid <= 0)
3464 return -ECHILD;
3465
3466 if (kill(j->initpid, SIGTERM))
3467 return -errno;
3468
3469 return minijail_wait_internal(j, SIGTERM);
3470}
3471
3472int API minijail_wait(struct minijail *j)
3473{
3474 return minijail_wait_internal(j, 0);
3475}
3476
Will Drewry6ac91122011-10-21 16:38:58 -05003477void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003478{
Dylan Reid605ce7f2016-01-19 19:21:00 -08003479 size_t i;
3480
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07003481 if (j->filter_prog) {
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08003482 free(j->filter_prog->filter);
3483 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04003484 }
Mike Frysingerac08a682017-10-10 02:04:50 -04003485 free_mounts_list(j);
Nicole Anderson-Au835f7172021-01-13 21:18:13 +00003486 free_remounts_list(j);
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07003487 while (j->hooks_head) {
3488 struct hook *c = j->hooks_head;
3489 j->hooks_head = c->next;
3490 free(c);
3491 }
3492 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04003493 if (j->user)
3494 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08003495 if (j->suppl_gid_list)
3496 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05003497 if (j->chrootdir)
3498 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04003499 if (j->pid_file_path)
3500 free(j->pid_file_path);
3501 if (j->uidmap)
3502 free(j->uidmap);
3503 if (j->gidmap)
3504 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04003505 if (j->hostname)
3506 free(j->hostname);
Luis Hector Chavez9acba452018-10-11 10:13:25 -07003507 if (j->preload_path)
3508 free(j->preload_path);
Andrew Brestickereac28942015-11-11 16:04:46 -08003509 if (j->alt_syscall_table)
3510 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08003511 for (i = 0; i < j->cgroup_count; ++i)
3512 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04003513 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003514}
Luis Hector Chavez114a9302017-09-05 20:36:58 -07003515
3516void API minijail_log_to_fd(int fd, int min_priority)
3517{
3518 init_logging(LOG_TO_FD, fd, min_priority);
3519}