blob: 0820dbb835b69ea06531c8eca12d7d10b2a85357 [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
Stephen Barber0d1cbf62017-10-16 22:19:38 -070062#define MAX_PRESERVED_FDS 32U
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
Mike Frysingerac08a682017-10-10 02:04:50 -0400197static void free_mounts_list(struct minijail *j)
198{
199 while (j->mounts_head) {
200 struct mountpoint *m = j->mounts_head;
201 j->mounts_head = j->mounts_head->next;
202 free(m->data);
203 free(m->type);
204 free(m->dest);
205 free(m->src);
206 free(m);
207 }
208 // No need to clear mounts_head as we know it's NULL after the loop.
209 j->mounts_tail = NULL;
210}
211
Nicole Anderson-Au835f7172021-01-13 21:18:13 +0000212static void free_remounts_list(struct minijail *j)
213{
214 while (j->remounts_head) {
215 struct minijail_remount *m = j->remounts_head;
216 j->remounts_head = j->remounts_head->next;
217 free(m->mount_name);
218 free(m);
219 }
220 // No need to clear remounts_head as we know it's NULL after the loop.
221 j->remounts_tail = NULL;
222}
223
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700224/*
François Degros664eba72019-11-05 13:18:24 +1100225 * Writes exactly n bytes from buf to file descriptor fd.
226 * Returns 0 on success or a negative error code on error.
227 */
228static int write_exactly(int fd, const void *buf, size_t n)
229{
230 const char *p = buf;
231 while (n > 0) {
232 const ssize_t written = write(fd, p, n);
233 if (written < 0) {
234 if (errno == EINTR)
235 continue;
236
237 return -errno;
238 }
239
240 p += written;
241 n -= written;
242 }
243
244 return 0;
245}
246
Mattias Nissler6123e5a2020-02-11 13:38:03 +0100247/* Closes *pfd and sets it to -1. */
248static void close_and_reset(int *pfd)
249{
250 if (*pfd != -1)
251 close(*pfd);
252 *pfd = -1;
253}
254
François Degros664eba72019-11-05 13:18:24 +1100255/*
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700256 * Strip out flags meant for the parent.
257 * We keep things that are not inherited across execve(2) (e.g. capabilities),
258 * or are easier to set after execve(2) (e.g. seccomp filters).
259 */
260void minijail_preenter(struct minijail *j)
261{
262 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700263 j->flags.enter_vfs = 0;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700264 j->flags.ns_cgroups = 0;
265 j->flags.net = 0;
266 j->flags.uts = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700267 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700268 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800269 j->flags.do_init = 0;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700270 j->flags.run_as_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800271 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800272 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400273 j->flags.forward_signals = 0;
Xiyuan Xia9b41e652019-05-23 11:03:04 -0700274 j->flags.setsid = 0;
Mike Frysinger785b1c32018-02-23 15:47:24 -0500275 j->remount_mode = 0;
Nicole Anderson-Au835f7172021-01-13 21:18:13 +0000276 free_remounts_list(j);
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700277}
278
279/*
280 * Strip out flags meant for the child.
281 * We keep things that are inherited across execve(2).
282 */
283void minijail_preexec(struct minijail *j)
284{
285 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700286 int enter_vfs = j->flags.enter_vfs;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700287 int ns_cgroups = j->flags.ns_cgroups;
288 int net = j->flags.net;
289 int uts = j->flags.uts;
Dylan Reid791f5772015-09-14 20:02:42 -0700290 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800291 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700292 if (j->user)
293 free(j->user);
294 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800295 if (j->suppl_gid_list)
296 free(j->suppl_gid_list);
297 j->suppl_gid_list = NULL;
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700298 if (j->preload_path)
299 free(j->preload_path);
300 j->preload_path = NULL;
Mike Frysingerac08a682017-10-10 02:04:50 -0400301 free_mounts_list(j);
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700302 memset(&j->flags, 0, sizeof(j->flags));
303 /* Now restore anything we meant to keep. */
304 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700305 j->flags.enter_vfs = enter_vfs;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700306 j->flags.ns_cgroups = ns_cgroups;
307 j->flags.net = net;
308 j->flags.uts = uts;
Dylan Reid791f5772015-09-14 20:02:42 -0700309 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800310 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700311 /* Note, |pids| will already have been used before this call. */
312}
313
314/* Minijail API. */
315
Will Drewry6ac91122011-10-21 16:38:58 -0500316struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400317{
Mike Frysinger785b1c32018-02-23 15:47:24 -0500318 struct minijail *j = calloc(1, sizeof(struct minijail));
Mike Frysinger1036cd82020-08-28 00:15:59 -0400319 if (j) {
320 j->remount_mode = MS_PRIVATE;
321 }
Mike Frysinger785b1c32018-02-23 15:47:24 -0500322 return j;
Elly Jonescd7a9042011-07-22 13:56:51 -0400323}
324
Will Drewry6ac91122011-10-21 16:38:58 -0500325void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400326{
327 if (uid == 0)
328 die("useless change to uid 0");
329 j->uid = uid;
330 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400331}
332
Will Drewry6ac91122011-10-21 16:38:58 -0500333void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400334{
335 if (gid == 0)
336 die("useless change to gid 0");
337 j->gid = gid;
338 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400339}
340
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800341void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
342 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800343{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800344 size_t i;
345
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500346 if (j->flags.inherit_suppl_gids)
347 die("cannot inherit *and* set supplementary groups");
348 if (j->flags.keep_suppl_gids)
349 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800350
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800351 if (size == 0) {
352 /* Clear supplementary groups. */
353 j->suppl_gid_list = NULL;
354 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100355 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800356 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800357 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800358
359 /* Copy the gid_t array. */
360 j->suppl_gid_list = calloc(size, sizeof(gid_t));
361 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800362 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800363 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800364 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800365 j->suppl_gid_list[i] = list[i];
366 }
367 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100368 j->flags.set_suppl_gids = 1;
369}
370
371void API minijail_keep_supplementary_gids(struct minijail *j) {
372 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800373}
374
Will Drewry6ac91122011-10-21 16:38:58 -0500375int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400376{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700377 uid_t uid;
378 gid_t gid;
379 int rc = lookup_user(user, &uid, &gid);
380 if (rc)
381 return rc;
382 minijail_change_uid(j, uid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400383 j->user = strdup(user);
384 if (!j->user)
385 return -ENOMEM;
Luis Hector Chavez71323552017-09-05 09:17:22 -0700386 j->usergid = gid;
Elly Jonese1749eb2011-10-07 13:54:59 -0400387 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400388}
389
Will Drewry6ac91122011-10-21 16:38:58 -0500390int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400391{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700392 gid_t gid;
393 int rc = lookup_group(group, &gid);
394 if (rc)
395 return rc;
396 minijail_change_gid(j, gid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400397 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400398}
399
Will Drewry6ac91122011-10-21 16:38:58 -0500400void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400401{
402 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400403}
404
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700405void API minijail_no_new_privs(struct minijail *j)
406{
407 j->flags.no_new_privs = 1;
408}
409
Will Drewry6ac91122011-10-21 16:38:58 -0500410void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400411{
412 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500413}
414
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400415void API minijail_set_seccomp_filter_tsync(struct minijail *j)
416{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400417 if (j->filter_len > 0 && j->filter_prog != NULL) {
418 die("minijail_set_seccomp_filter_tsync() must be called "
419 "before minijail_parse_seccomp_filters()");
420 }
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400421
422 if (j->flags.seccomp_filter_logging && !seccomp_ret_log_available()) {
423 /*
424 * If SECCOMP_RET_LOG is not available, we don't want to use
425 * SECCOMP_RET_TRAP to both kill the entire process and report
426 * failing syscalls, since it will be brittle. Just bail.
427 */
Mike Frysinger52f6ada2019-06-26 16:59:36 -0400428 die("SECCOMP_RET_LOG not available, cannot use logging with "
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400429 "thread sync at the same time");
430 }
431
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400432 j->flags.seccomp_filter_tsync = 1;
433}
434
Anand K Mistry31adc6c2020-11-26 11:39:46 +1100435void API minijail_set_seccomp_filter_allow_speculation(struct minijail *j)
436{
437 if (j->filter_len > 0 && j->filter_prog != NULL) {
438 die("minijail_set_seccomp_filter_allow_speculation() must be "
439 "called before minijail_parse_seccomp_filters()");
440 }
441
442 j->flags.seccomp_filter_allow_speculation = 1;
443}
444
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700445void API minijail_log_seccomp_filter_failures(struct minijail *j)
446{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400447 if (j->filter_len > 0 && j->filter_prog != NULL) {
448 die("minijail_log_seccomp_filter_failures() must be called "
449 "before minijail_parse_seccomp_filters()");
450 }
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400451
452 if (j->flags.seccomp_filter_tsync && !seccomp_ret_log_available()) {
453 /*
454 * If SECCOMP_RET_LOG is not available, we don't want to use
455 * SECCOMP_RET_TRAP to both kill the entire process and report
456 * failing syscalls, since it will be brittle. Just bail.
457 */
Mike Frysinger52f6ada2019-06-26 16:59:36 -0400458 die("SECCOMP_RET_LOG not available, cannot use thread sync with "
459 "logging at the same time");
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400460 }
461
462 if (debug_logging_allowed()) {
463 j->flags.seccomp_filter_logging = 1;
464 } else {
465 warn("non-debug build: ignoring request to enable seccomp "
466 "logging");
467 }
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700468}
469
Will Drewry6ac91122011-10-21 16:38:58 -0500470void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400471{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800472 /*
473 * 'minijail_use_caps' configures a runtime-capabilities-only
474 * environment, including a bounding set matching the thread's runtime
475 * (permitted|inheritable|effective) sets.
476 * Therefore, it will override any existing bounding set configurations
477 * since the latter would allow gaining extra runtime capabilities from
478 * file capabilities.
479 */
480 if (j->flags.capbset_drop) {
481 warn("overriding bounding set configuration");
482 j->cap_bset = 0;
483 j->flags.capbset_drop = 0;
484 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400485 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800486 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400487}
488
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800489void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
490{
491 if (j->flags.use_caps) {
492 /*
493 * 'minijail_use_caps' will have already configured a capability
494 * bounding set matching the (permitted|inheritable|effective)
495 * sets. Abort if the user tries to configure a separate
496 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
497 * are mutually exclusive.
498 */
499 die("runtime capabilities already configured, can't drop "
500 "bounding set separately");
501 }
502 j->cap_bset = capmask;
503 j->flags.capbset_drop = 1;
504}
505
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400506void API minijail_set_ambient_caps(struct minijail *j)
507{
508 j->flags.set_ambient_caps = 1;
509}
510
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800511void API minijail_reset_signal_mask(struct minijail *j)
512{
Peter Qiu2860c462015-12-16 15:13:06 -0800513 j->flags.reset_signal_mask = 1;
514}
515
Luis Hector Chaveza27118a2018-04-04 08:18:01 -0700516void API minijail_reset_signal_handlers(struct minijail *j)
517{
518 j->flags.reset_signal_handlers = 1;
519}
520
Will Drewry6ac91122011-10-21 16:38:58 -0500521void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400522{
523 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400524}
525
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700526void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
527{
Mike Frysinger902a4492018-12-27 05:22:56 -0500528 /* Note: Do not use O_CLOEXEC here. We'll close it after we use it. */
529 int ns_fd = open(ns_path, O_RDONLY);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700530 if (ns_fd < 0) {
531 pdie("failed to open namespace '%s'", ns_path);
532 }
533 j->mountns_fd = ns_fd;
534 j->flags.enter_vfs = 1;
535}
536
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800537void API minijail_new_session_keyring(struct minijail *j)
538{
539 j->flags.new_session_keyring = 1;
540}
541
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700542void API minijail_skip_setting_securebits(struct minijail *j,
543 uint64_t securebits_skip_mask)
544{
545 j->securebits_skip_mask = securebits_skip_mask;
546}
547
Mike Frysinger785b1c32018-02-23 15:47:24 -0500548void API minijail_remount_mode(struct minijail *j, unsigned long mode)
549{
550 j->remount_mode = mode;
551}
552
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800553void API minijail_skip_remount_private(struct minijail *j)
554{
Mike Frysinger785b1c32018-02-23 15:47:24 -0500555 j->remount_mode = 0;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800556}
557
Will Drewry6ac91122011-10-21 16:38:58 -0500558void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400559{
Elly Jonese58176c2012-01-23 11:46:17 -0500560 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700561 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400562 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800563 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400564}
565
Jorge Lucangeli Obes2fa96d12019-02-05 10:51:57 -0500566void API minijail_namespace_pids_rw_proc(struct minijail *j)
567{
568 j->flags.vfs = 1;
569 j->flags.pids = 1;
570 j->flags.do_init = 1;
571}
572
Dylan Reidf7942472015-11-18 17:55:26 -0800573void API minijail_namespace_ipc(struct minijail *j)
574{
575 j->flags.ipc = 1;
576}
577
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400578void API minijail_namespace_uts(struct minijail *j)
579{
580 j->flags.uts = 1;
581}
582
583int API minijail_namespace_set_hostname(struct minijail *j, const char *name)
584{
585 if (j->hostname)
586 return -EINVAL;
587 minijail_namespace_uts(j);
588 j->hostname = strdup(name);
589 if (!j->hostname)
590 return -ENOMEM;
591 return 0;
592}
593
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400594void API minijail_namespace_net(struct minijail *j)
595{
596 j->flags.net = 1;
597}
598
Dylan Reid1102f5a2015-09-15 11:52:20 -0700599void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
600{
Mike Frysinger902a4492018-12-27 05:22:56 -0500601 /* Note: Do not use O_CLOEXEC here. We'll close it after we use it. */
602 int ns_fd = open(ns_path, O_RDONLY);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700603 if (ns_fd < 0) {
604 pdie("failed to open namespace '%s'", ns_path);
605 }
606 j->netns_fd = ns_fd;
607 j->flags.enter_net = 1;
608}
609
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700610void API minijail_namespace_cgroups(struct minijail *j)
611{
612 j->flags.ns_cgroups = 1;
613}
614
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700615void API minijail_close_open_fds(struct minijail *j)
616{
617 j->flags.close_open_fds = 1;
618}
619
Dylan Reid791f5772015-09-14 20:02:42 -0700620void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400621{
622 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700623 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400624}
625
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800626void API minijail_namespace_user(struct minijail *j)
627{
628 j->flags.userns = 1;
629}
630
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400631void API minijail_namespace_user_disable_setgroups(struct minijail *j)
632{
633 j->flags.disable_setgroups = 1;
634}
635
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800636int API minijail_uidmap(struct minijail *j, const char *uidmap)
637{
638 j->uidmap = strdup(uidmap);
639 if (!j->uidmap)
640 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800641 char *ch;
642 for (ch = j->uidmap; *ch; ch++) {
643 if (*ch == ',')
644 *ch = '\n';
645 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800646 return 0;
647}
648
649int API minijail_gidmap(struct minijail *j, const char *gidmap)
650{
651 j->gidmap = strdup(gidmap);
652 if (!j->gidmap)
653 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800654 char *ch;
655 for (ch = j->gidmap; *ch; ch++) {
656 if (*ch == ',')
657 *ch = '\n';
658 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800659 return 0;
660}
661
Will Drewry6ac91122011-10-21 16:38:58 -0500662void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400663{
Lutz Justen13807cb2017-01-03 17:11:55 +0100664 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400665}
666
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800667void API minijail_run_as_init(struct minijail *j)
668{
669 /*
670 * Since the jailed program will become 'init' in the new PID namespace,
671 * Minijail does not need to fork an 'init' process.
672 */
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700673 j->flags.run_as_init = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800674}
675
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700676int API minijail_enter_chroot(struct minijail *j, const char *dir)
677{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400678 if (j->chrootdir)
679 return -EINVAL;
680 j->chrootdir = strdup(dir);
681 if (!j->chrootdir)
682 return -ENOMEM;
683 j->flags.chroot = 1;
684 return 0;
685}
686
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800687int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
688{
689 if (j->chrootdir)
690 return -EINVAL;
691 j->chrootdir = strdup(dir);
692 if (!j->chrootdir)
693 return -ENOMEM;
694 j->flags.pivot_root = 1;
695 return 0;
696}
697
Dylan Reida14e08d2015-10-22 21:05:29 -0700698char API *minijail_get_original_path(struct minijail *j,
699 const char *path_inside_chroot)
700{
Dylan Reid648b2202015-10-23 00:50:00 -0700701 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700702
Dylan Reid648b2202015-10-23 00:50:00 -0700703 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700704 while (b) {
705 /*
706 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700707 * mount, then the original path is exactly the source of
708 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700709 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700710 * mount source = /some/path/exe, mount dest =
711 * /chroot/path/exe Then when getting the original path of
712 * "/chroot/path/exe", the source of that mount,
713 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700714 */
715 if (!strcmp(b->dest, path_inside_chroot))
716 return strdup(b->src);
717
718 /*
719 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700720 * mount, take the suffix of the chroot path relative to the
721 * mount destination path, and append it to the mount source
722 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700723 */
724 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
725 const char *relative_path =
726 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400727 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700728 }
729 b = b->next;
730 }
731
732 /* If there is a chroot path, append |path_inside_chroot| to that. */
733 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400734 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700735
736 /* No chroot, so the path outside is the same as it is inside. */
737 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700738}
739
Mike Frysinger33ffef32017-01-13 19:53:19 -0500740void API minijail_mount_dev(struct minijail *j)
741{
742 j->flags.mount_dev = 1;
743}
744
Lee Campbell11af0622014-05-22 12:36:04 -0700745void API minijail_mount_tmp(struct minijail *j)
746{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100747 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
748}
749
750void API minijail_mount_tmp_size(struct minijail *j, size_t size)
751{
752 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700753 j->flags.mount_tmp = 1;
754}
755
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800756int API minijail_write_pid_file(struct minijail *j, const char *path)
757{
758 j->pid_file_path = strdup(path);
759 if (!j->pid_file_path)
760 return -ENOMEM;
761 j->flags.pid_file = 1;
762 return 0;
763}
764
Dylan Reid605ce7f2016-01-19 19:21:00 -0800765int API minijail_add_to_cgroup(struct minijail *j, const char *path)
766{
767 if (j->cgroup_count >= MAX_CGROUPS)
768 return -ENOMEM;
769 j->cgroups[j->cgroup_count] = strdup(path);
770 if (!j->cgroups[j->cgroup_count])
771 return -ENOMEM;
772 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800773 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800774 return 0;
775}
776
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800777int API minijail_rlimit(struct minijail *j, int type, rlim_t cur, rlim_t max)
Dylan Reid0f72ef42017-06-06 15:42:49 -0700778{
779 size_t i;
780
781 if (j->rlimit_count >= MAX_RLIMITS)
782 return -ENOMEM;
783 /* It's an error if the caller sets the same rlimit multiple times. */
784 for (i = 0; i < j->rlimit_count; i++) {
785 if (j->rlimits[i].type == type)
786 return -EEXIST;
787 }
788
789 j->rlimits[j->rlimit_count].type = type;
790 j->rlimits[j->rlimit_count].cur = cur;
791 j->rlimits[j->rlimit_count].max = max;
792 j->rlimit_count++;
793 return 0;
794}
795
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400796int API minijail_forward_signals(struct minijail *j)
797{
798 j->flags.forward_signals = 1;
799 return 0;
800}
801
Xiyuan Xia9b41e652019-05-23 11:03:04 -0700802int API minijail_create_session(struct minijail *j) {
803 j->flags.setsid = 1;
804 return 0;
805}
806
Dylan Reid81e23972016-05-18 14:06:35 -0700807int API minijail_mount_with_data(struct minijail *j, const char *src,
808 const char *dest, const char *type,
809 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700810{
Dylan Reid648b2202015-10-23 00:50:00 -0700811 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400812
813 if (*dest != '/')
814 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700815 m = calloc(1, sizeof(*m));
816 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400817 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700818 m->dest = strdup(dest);
819 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400820 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700821 m->src = strdup(src);
822 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400823 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700824 m->type = strdup(type);
825 if (!m->type)
826 goto error;
Mike Frysingerb7803c82018-08-23 15:43:15 -0400827
828 if (!data || !data[0]) {
829 /*
830 * Set up secure defaults for certain filesystems. Adding this
831 * fs-specific logic here kind of sucks, but considering how
832 * people use these in practice, it's probably OK. If they want
833 * the kernel defaults, they can pass data="" instead of NULL.
834 */
835 if (!strcmp(type, "tmpfs")) {
836 /* tmpfs defaults to mode=1777 and size=50%. */
837 data = "mode=0755,size=10M";
838 }
839 }
Dylan Reid81e23972016-05-18 14:06:35 -0700840 if (data) {
841 m->data = strdup(data);
842 if (!m->data)
843 goto error;
844 m->has_data = 1;
845 }
Mike Frysingercb8674d2018-08-12 00:53:35 -0400846
847 /* If they don't specify any flags, default to secure ones. */
848 if (flags == 0)
849 flags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Dylan Reid648b2202015-10-23 00:50:00 -0700850 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400851
Elly Jonesdd3e8512012-01-23 15:13:38 -0500852 /*
Jorge Lucangeli Obes0a0514c2020-01-03 11:18:32 -0500853 * Unless asked to enter an existing namespace, force vfs namespacing
854 * so the mounts don't leak out into the containing vfs namespace.
855 * If Minijail is being asked to enter the root vfs namespace this will
856 * leak mounts, but it's unlikely that the user would ask to do that by
857 * mistake.
Elly Jones51a5b6c2011-10-12 19:09:26 -0400858 */
Jorge Lucangeli Obes0a0514c2020-01-03 11:18:32 -0500859 if (!j->flags.enter_vfs)
860 minijail_namespace_vfs(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400861
Dylan Reid648b2202015-10-23 00:50:00 -0700862 if (j->mounts_tail)
863 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400864 else
Dylan Reid648b2202015-10-23 00:50:00 -0700865 j->mounts_head = m;
866 j->mounts_tail = m;
867 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400868
869 return 0;
870
871error:
Dylan Reid81e23972016-05-18 14:06:35 -0700872 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700873 free(m->src);
874 free(m->dest);
875 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400876 return -ENOMEM;
877}
878
Dylan Reid81e23972016-05-18 14:06:35 -0700879int API minijail_mount(struct minijail *j, const char *src, const char *dest,
880 const char *type, unsigned long flags)
881{
882 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
883}
884
Dylan Reid648b2202015-10-23 00:50:00 -0700885int API minijail_bind(struct minijail *j, const char *src, const char *dest,
886 int writeable)
887{
888 unsigned long flags = MS_BIND;
889
890 if (!writeable)
891 flags |= MS_RDONLY;
892
893 return minijail_mount(j, src, dest, "", flags);
894}
895
Nicole Anderson-Au835f7172021-01-13 21:18:13 +0000896int API minijail_add_remount(struct minijail *j, const char *mount_name,
897 unsigned long remount_mode)
898{
899 struct minijail_remount *m;
900
901 if (*mount_name != '/')
902 return -EINVAL;
903 m = calloc(1, sizeof(*m));
904 if (!m)
905 return -ENOMEM;
906 m->mount_name = strdup(mount_name);
907 if (!m->mount_name) {
908 free(m);
909 return -ENOMEM;
910 }
911
912 m->remount_mode = remount_mode;
913
914 if (j->remounts_tail)
915 j->remounts_tail->next = m;
916 else
917 j->remounts_head = m;
918 j->remounts_tail = m;
919
920 return 0;
921}
922
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700923int API minijail_add_hook(struct minijail *j, minijail_hook_t hook,
924 void *payload, minijail_hook_event_t event)
925{
926 struct hook *c;
927
928 if (hook == NULL)
929 return -EINVAL;
930 if (event >= MINIJAIL_HOOK_EVENT_MAX)
931 return -EINVAL;
932 c = calloc(1, sizeof(*c));
933 if (!c)
934 return -ENOMEM;
935
936 c->hook = hook;
937 c->payload = payload;
938 c->event = event;
939
940 if (j->hooks_tail)
941 j->hooks_tail->next = c;
942 else
943 j->hooks_head = c;
944 j->hooks_tail = c;
945
946 return 0;
947}
948
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700949int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd)
950{
951 if (parent_fd < 0 || child_fd < 0)
952 return -EINVAL;
953 if (j->preserved_fd_count >= MAX_PRESERVED_FDS)
954 return -ENOMEM;
955 j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd;
956 j->preserved_fds[j->preserved_fd_count].child_fd = child_fd;
957 j->preserved_fd_count++;
958 return 0;
959}
960
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700961int API minijail_set_preload_path(struct minijail *j, const char *preload_path)
962{
963 if (j->preload_path)
964 return -EINVAL;
965 j->preload_path = strdup(preload_path);
966 if (!j->preload_path)
967 return -ENOMEM;
968 return 0;
969}
970
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400971static void clear_seccomp_options(struct minijail *j)
972{
973 j->flags.seccomp_filter = 0;
974 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400975 j->flags.seccomp_filter_logging = 0;
Anand K Mistry31adc6c2020-11-26 11:39:46 +1100976 j->flags.seccomp_filter_allow_speculation = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400977 j->filter_len = 0;
978 j->filter_prog = NULL;
979 j->flags.no_new_privs = 0;
980}
981
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700982static int seccomp_should_use_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400983{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400984 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400985 /*
986 * |errno| will be set to EINVAL when seccomp has not been
987 * compiled into the kernel. On certain platforms and kernel
988 * versions this is not a fatal failure. In that case, and only
989 * in that case, disable seccomp and skip loading the filters.
990 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400991 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400992 warn("not loading seccomp filters, seccomp filter not "
993 "supported");
994 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400995 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700996 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400997 /*
998 * If |errno| != EINVAL or seccomp_can_softfail() is false,
999 * we can proceed. Worst case scenario minijail_enter() will
1000 * abort() if seccomp fails.
1001 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001002 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001003 if (j->flags.seccomp_filter_tsync) {
1004 /* Are the seccomp(2) syscall and the TSYNC option supported? */
1005 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1006 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
1007 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001008 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
1009 warn("seccomp(2) syscall not supported");
1010 clear_seccomp_options(j);
1011 return 0;
1012 } else if (saved_errno == EINVAL &&
1013 seccomp_can_softfail()) {
1014 warn(
1015 "seccomp filter thread sync not supported");
1016 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001017 return 0;
1018 }
1019 /*
1020 * Similar logic here. If seccomp_can_softfail() is
1021 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
1022 * we can proceed. Worst case scenario minijail_enter()
1023 * will abort() if seccomp or TSYNC fail.
1024 */
1025 }
1026 }
Anand K Mistry31adc6c2020-11-26 11:39:46 +11001027 if (j->flags.seccomp_filter_allow_speculation) {
1028 /* Is the SPEC_ALLOW flag supported? */
Luis Héctor Chávez01b628c2021-01-03 05:46:57 -08001029 if (!seccomp_filter_flags_available(
1030 SECCOMP_FILTER_FLAG_SPEC_ALLOW)) {
Anand K Mistry31adc6c2020-11-26 11:39:46 +11001031 warn("allowing speculative execution on seccomp "
1032 "processes not supported");
1033 j->flags.seccomp_filter_allow_speculation = 0;
1034 }
1035 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001036 return 1;
1037}
1038
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001039static int set_seccomp_filters_internal(struct minijail *j,
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001040 const struct sock_fprog *filter,
1041 bool owned)
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001042{
1043 struct sock_fprog *fprog;
1044
1045 if (owned) {
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001046 /*
1047 * If |owned| is true, it's OK to cast away the const-ness since
1048 * we'll own the pointer going forward.
1049 */
1050 fprog = (struct sock_fprog *)filter;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001051 } else {
1052 fprog = malloc(sizeof(struct sock_fprog));
1053 if (!fprog)
1054 return -ENOMEM;
1055 fprog->len = filter->len;
1056 fprog->filter = malloc(sizeof(struct sock_filter) * fprog->len);
1057 if (!fprog->filter) {
1058 free(fprog);
1059 return -ENOMEM;
1060 }
1061 memcpy(fprog->filter, filter->filter,
1062 sizeof(struct sock_filter) * fprog->len);
1063 }
1064
1065 if (j->filter_prog) {
1066 free(j->filter_prog->filter);
1067 free(j->filter_prog);
1068 }
1069
1070 j->filter_len = fprog->len;
1071 j->filter_prog = fprog;
1072 return 0;
1073}
1074
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001075static int parse_seccomp_filters(struct minijail *j, const char *filename,
1076 FILE *policy_file)
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001077{
1078 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001079 if (!fprog)
1080 return -ENOMEM;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001081
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001082 struct filter_options filteropts;
1083
1084 /*
1085 * Figure out filter options.
1086 * Allow logging?
1087 */
1088 filteropts.allow_logging =
1089 debug_logging_allowed() && j->flags.seccomp_filter_logging;
1090
1091 /* What to do on a blocked system call? */
1092 if (filteropts.allow_logging) {
1093 if (seccomp_ret_log_available())
1094 filteropts.action = ACTION_RET_LOG;
1095 else
1096 filteropts.action = ACTION_RET_TRAP;
1097 } else {
Jorge Lucangeli Obesd23ad7922020-10-13 10:26:40 -04001098 if (j->flags.seccomp_filter_tsync) {
1099 if (seccomp_ret_kill_process_available()) {
1100 filteropts.action = ACTION_RET_KILL_PROCESS;
1101 } else {
1102 filteropts.action = ACTION_RET_TRAP;
1103 }
1104 } else {
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001105 filteropts.action = ACTION_RET_KILL;
Jorge Lucangeli Obesd23ad7922020-10-13 10:26:40 -04001106 }
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001107 }
1108
1109 /*
1110 * If SECCOMP_RET_LOG is not available, need to allow extra syscalls
1111 * for logging.
1112 */
1113 filteropts.allow_syscalls_for_logging =
1114 filteropts.allow_logging && !seccomp_ret_log_available();
1115
Nicole Anderson-Aubcc8cfd2020-11-10 20:33:27 +00001116 /* Whether to fail on duplicate syscalls. */
1117 filteropts.allow_duplicate_syscalls = allow_duplicate_syscalls();
1118
Jorge Lucangeli Obese1a86892019-06-10 16:17:03 -04001119 if (compile_filter(filename, policy_file, fprog, &filteropts)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001120 free(fprog);
1121 return -1;
1122 }
1123
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001124 return set_seccomp_filters_internal(j, fprog, true /* owned */);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001125}
1126
1127void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
1128{
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001129 if (!seccomp_should_use_filters(j))
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001130 return;
1131
Luis Hector Chaveza30a2062018-07-12 21:10:33 -07001132 FILE *file = fopen(path, "re");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001133 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -07001134 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -04001135 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001136
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001137 if (parse_seccomp_filters(j, path, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -07001138 die("failed to compile seccomp filter BPF program in '%s'",
1139 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001140 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001141 fclose(file);
1142}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001143
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001144void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
1145{
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001146 char *fd_path, *path;
1147 FILE *file;
1148
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001149 if (!seccomp_should_use_filters(j))
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001150 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001151
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001152 file = fdopen(fd, "r");
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001153 if (!file) {
1154 pdie("failed to associate stream with fd %d", fd);
1155 }
1156
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001157 if (asprintf(&fd_path, "/proc/self/fd/%d", fd) == -1)
1158 pdie("failed to create path for fd %d", fd);
1159 path = realpath(fd_path, NULL);
1160 if (path == NULL)
1161 pwarn("failed to get path of fd %d", fd);
1162 free(fd_path);
1163
1164 if (parse_seccomp_filters(j, path ? path : "<fd>", file) != 0) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001165 die("failed to compile seccomp filter BPF program from fd %d",
1166 fd);
1167 }
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001168 free(path);
Elly Jonese1749eb2011-10-07 13:54:59 -04001169 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -05001170}
1171
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001172void API minijail_set_seccomp_filters(struct minijail *j,
1173 const struct sock_fprog *filter)
1174{
1175 if (!seccomp_should_use_filters(j))
1176 return;
1177
1178 if (j->flags.seccomp_filter_logging) {
1179 die("minijail_log_seccomp_filter_failures() is incompatible "
1180 "with minijail_set_seccomp_filters()");
1181 }
1182
1183 /*
1184 * set_seccomp_filters_internal() can only fail with ENOMEM.
1185 * Furthermore, since we won't own the incoming filter, it will not be
1186 * modified.
1187 */
1188 if (set_seccomp_filters_internal(j, filter, false /* owned */) < 0) {
1189 die("failed to set seccomp filter");
1190 }
1191}
1192
Andrew Brestickereac28942015-11-11 16:04:46 -08001193int API minijail_use_alt_syscall(struct minijail *j, const char *table)
1194{
1195 j->alt_syscall_table = strdup(table);
1196 if (!j->alt_syscall_table)
1197 return -ENOMEM;
1198 j->flags.alt_syscall = 1;
1199 return 0;
1200}
1201
Will Drewryf89aef52011-09-16 16:48:57 -05001202struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -04001203 size_t available;
1204 size_t total;
1205 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -05001206};
1207
Mike Frysinger0a27ab02020-09-04 16:18:12 -04001208static void marshal_state_init(struct marshal_state *state, char *buf,
1209 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -04001210{
1211 state->available = available;
1212 state->buf = buf;
1213 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001214}
1215
Mike Frysinger0a27ab02020-09-04 16:18:12 -04001216static void marshal_append(struct marshal_state *state, const void *src,
1217 size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -04001218{
1219 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -05001220
Elly Jonese1749eb2011-10-07 13:54:59 -04001221 /* Up to |available| will be written. */
1222 if (copy_len) {
1223 memcpy(state->buf, src, copy_len);
1224 state->buf += copy_len;
1225 state->available -= copy_len;
1226 }
1227 /* |total| will contain the expected length. */
1228 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -05001229}
1230
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001231static void marshal_append_string(struct marshal_state *state, const char *src)
1232{
1233 marshal_append(state, src, strlen(src) + 1);
1234}
1235
Mike Frysinger0a27ab02020-09-04 16:18:12 -04001236static void marshal_mount(struct marshal_state *state,
1237 const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -07001238{
1239 marshal_append(state, m->src, strlen(m->src) + 1);
1240 marshal_append(state, m->dest, strlen(m->dest) + 1);
1241 marshal_append(state, m->type, strlen(m->type) + 1);
1242 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
1243 if (m->has_data)
1244 marshal_append(state, m->data, strlen(m->data) + 1);
1245 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
1246}
1247
Mike Frysinger0a27ab02020-09-04 16:18:12 -04001248static void minijail_marshal_helper(struct marshal_state *state,
1249 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001250{
Dylan Reid648b2202015-10-23 00:50:00 -07001251 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001252 size_t i;
1253
Elly Jonese1749eb2011-10-07 13:54:59 -04001254 marshal_append(state, (char *)j, sizeof(*j));
1255 if (j->user)
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001256 marshal_append_string(state, j->user);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001257 if (j->suppl_gid_list) {
1258 marshal_append(state, j->suppl_gid_list,
1259 j->suppl_gid_count * sizeof(gid_t));
1260 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001261 if (j->chrootdir)
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001262 marshal_append_string(state, j->chrootdir);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001263 if (j->hostname)
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001264 marshal_append_string(state, j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08001265 if (j->alt_syscall_table) {
1266 marshal_append(state, j->alt_syscall_table,
1267 strlen(j->alt_syscall_table) + 1);
1268 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001269 if (j->flags.seccomp_filter && j->filter_prog) {
1270 struct sock_fprog *fp = j->filter_prog;
1271 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001272 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -04001273 }
Dylan Reid648b2202015-10-23 00:50:00 -07001274 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001275 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001276 }
Dylan Reid605ce7f2016-01-19 19:21:00 -08001277 for (i = 0; i < j->cgroup_count; ++i)
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001278 marshal_append_string(state, j->cgroups[i]);
Will Drewryf89aef52011-09-16 16:48:57 -05001279}
1280
Will Drewry6ac91122011-10-21 16:38:58 -05001281size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001282{
1283 struct marshal_state state;
1284 marshal_state_init(&state, NULL, 0);
1285 minijail_marshal_helper(&state, j);
1286 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -05001287}
1288
Elly Jonese1749eb2011-10-07 13:54:59 -04001289int minijail_marshal(const struct minijail *j, char *buf, size_t available)
1290{
1291 struct marshal_state state;
1292 marshal_state_init(&state, buf, available);
1293 minijail_marshal_helper(&state, j);
1294 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -05001295}
1296
Elly Jonese1749eb2011-10-07 13:54:59 -04001297int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
1298{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -08001299 size_t i;
1300 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -05001301 int ret = -EINVAL;
1302
Elly Jonese1749eb2011-10-07 13:54:59 -04001303 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001304 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001305 memcpy((void *)j, serialized, sizeof(*j));
1306 serialized += sizeof(*j);
1307 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001308
Will Drewrybee7ba72011-10-21 20:47:01 -05001309 /* Potentially stale pointers not used as signals. */
Luis Hector Chavez9acba452018-10-11 10:13:25 -07001310 j->preload_path = NULL;
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001311 j->pid_file_path = NULL;
1312 j->uidmap = NULL;
1313 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001314 j->mounts_head = NULL;
1315 j->mounts_tail = NULL;
Nicole Anderson-Au835f7172021-01-13 21:18:13 +00001316 j->remounts_head = NULL;
1317 j->remounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001318 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001319 j->hooks_head = NULL;
1320 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001321
Elly Jonese1749eb2011-10-07 13:54:59 -04001322 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001323 char *user = consumestr(&serialized, &length);
1324 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001325 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001326 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001327 if (!j->user)
1328 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001329 }
Will Drewryf89aef52011-09-16 16:48:57 -05001330
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001331 if (j->suppl_gid_list) { /* stale pointer */
1332 if (j->suppl_gid_count > NGROUPS_MAX) {
1333 goto bad_gid_list;
1334 }
1335 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1336 void *gid_list_bytes =
1337 consumebytes(gid_list_size, &serialized, &length);
1338 if (!gid_list_bytes)
1339 goto bad_gid_list;
1340
1341 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1342 if (!j->suppl_gid_list)
1343 goto bad_gid_list;
1344
1345 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1346 }
1347
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001348 if (j->chrootdir) { /* stale pointer */
1349 char *chrootdir = consumestr(&serialized, &length);
1350 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001351 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001352 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001353 if (!j->chrootdir)
1354 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001355 }
1356
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001357 if (j->hostname) { /* stale pointer */
1358 char *hostname = consumestr(&serialized, &length);
1359 if (!hostname)
1360 goto bad_hostname;
1361 j->hostname = strdup(hostname);
1362 if (!j->hostname)
1363 goto bad_hostname;
1364 }
1365
Andrew Brestickereac28942015-11-11 16:04:46 -08001366 if (j->alt_syscall_table) { /* stale pointer */
1367 char *alt_syscall_table = consumestr(&serialized, &length);
1368 if (!alt_syscall_table)
1369 goto bad_syscall_table;
1370 j->alt_syscall_table = strdup(alt_syscall_table);
1371 if (!j->alt_syscall_table)
1372 goto bad_syscall_table;
1373 }
1374
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001375 if (j->flags.seccomp_filter && j->filter_len > 0) {
1376 size_t ninstrs = j->filter_len;
1377 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1378 ninstrs > USHRT_MAX)
1379 goto bad_filters;
1380
1381 size_t program_len = ninstrs * sizeof(struct sock_filter);
1382 void *program = consumebytes(program_len, &serialized, &length);
1383 if (!program)
1384 goto bad_filters;
1385
1386 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001387 if (!j->filter_prog)
1388 goto bad_filters;
1389
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001390 j->filter_prog->len = ninstrs;
1391 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001392 if (!j->filter_prog->filter)
1393 goto bad_filter_prog_instrs;
1394
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001395 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001396 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001397
Dylan Reid648b2202015-10-23 00:50:00 -07001398 count = j->mounts_count;
1399 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001400 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001401 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001402 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001403 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001404 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001405 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001406 const char *src = consumestr(&serialized, &length);
1407 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001408 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001409 dest = consumestr(&serialized, &length);
1410 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001411 goto bad_mounts;
1412 type = consumestr(&serialized, &length);
1413 if (!type)
1414 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001415 has_data = consumebytes(sizeof(*has_data), &serialized,
1416 &length);
1417 if (!has_data)
1418 goto bad_mounts;
1419 if (*has_data) {
1420 data = consumestr(&serialized, &length);
1421 if (!data)
1422 goto bad_mounts;
1423 }
Dylan Reid648b2202015-10-23 00:50:00 -07001424 flags = consumebytes(sizeof(*flags), &serialized, &length);
1425 if (!flags)
1426 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001427 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001428 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001429 }
1430
Dylan Reid605ce7f2016-01-19 19:21:00 -08001431 count = j->cgroup_count;
1432 j->cgroup_count = 0;
1433 for (i = 0; i < count; ++i) {
1434 char *cgroup = consumestr(&serialized, &length);
1435 if (!cgroup)
1436 goto bad_cgroups;
1437 j->cgroups[i] = strdup(cgroup);
1438 if (!j->cgroups[i])
1439 goto bad_cgroups;
1440 ++j->cgroup_count;
1441 }
1442
Elly Jonese1749eb2011-10-07 13:54:59 -04001443 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001444
Dylan Reid605ce7f2016-01-19 19:21:00 -08001445bad_cgroups:
Mike Frysingerac08a682017-10-10 02:04:50 -04001446 free_mounts_list(j);
Nicole Anderson-Au835f7172021-01-13 21:18:13 +00001447 free_remounts_list(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001448 for (i = 0; i < j->cgroup_count; ++i)
1449 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001450bad_mounts:
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001451 if (j->filter_prog && j->filter_prog->filter)
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001452 free(j->filter_prog->filter);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001453bad_filter_prog_instrs:
1454 if (j->filter_prog)
1455 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001456bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001457 if (j->alt_syscall_table)
1458 free(j->alt_syscall_table);
1459bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001460 if (j->chrootdir)
1461 free(j->chrootdir);
1462bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001463 if (j->hostname)
1464 free(j->hostname);
1465bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001466 if (j->suppl_gid_list)
1467 free(j->suppl_gid_list);
1468bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001469 if (j->user)
1470 free(j->user);
1471clear_pointers:
1472 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001473 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001474 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001475 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001476 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001477 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001478out:
1479 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001480}
1481
Mike Frysinger33ffef32017-01-13 19:53:19 -05001482struct dev_spec {
1483 const char *name;
1484 mode_t mode;
1485 dev_t major, minor;
1486};
1487
1488static const struct dev_spec device_nodes[] = {
1489 {
1490 "null",
1491 S_IFCHR | 0666, 1, 3,
1492 },
1493 {
1494 "zero",
1495 S_IFCHR | 0666, 1, 5,
1496 },
1497 {
1498 "full",
1499 S_IFCHR | 0666, 1, 7,
1500 },
1501 {
1502 "urandom",
1503 S_IFCHR | 0444, 1, 9,
1504 },
1505 {
1506 "tty",
1507 S_IFCHR | 0666, 5, 0,
1508 },
1509};
1510
1511struct dev_sym_spec {
1512 const char *source, *dest;
1513};
1514
1515static const struct dev_sym_spec device_symlinks[] = {
1516 { "ptmx", "pts/ptmx", },
1517 { "fd", "/proc/self/fd", },
1518 { "stdin", "fd/0", },
1519 { "stdout", "fd/1", },
1520 { "stderr", "fd/2", },
1521};
1522
1523/*
1524 * Clean up the temporary dev path we had setup previously. In case of errors,
1525 * we don't want to go leaking empty tempdirs.
1526 */
1527static void mount_dev_cleanup(char *dev_path)
1528{
1529 umount2(dev_path, MNT_DETACH);
1530 rmdir(dev_path);
1531 free(dev_path);
1532}
1533
1534/*
1535 * Set up the pseudo /dev path at the temporary location.
1536 * See mount_dev_finalize for more details.
1537 */
1538static int mount_dev(char **dev_path_ret)
1539{
1540 int ret;
1541 int dev_fd;
1542 size_t i;
1543 mode_t mask;
1544 char *dev_path;
1545
1546 /*
1547 * Create a temp path for the /dev init. We'll relocate this to the
1548 * final location later on in the startup process.
1549 */
1550 dev_path = *dev_path_ret = strdup("/tmp/minijail.dev.XXXXXX");
1551 if (dev_path == NULL || mkdtemp(dev_path) == NULL)
1552 pdie("could not create temp path for /dev");
1553
1554 /* Set up the empty /dev mount point first. */
1555 ret = mount("minijail-devfs", dev_path, "tmpfs",
1556 MS_NOEXEC | MS_NOSUID, "size=5M,mode=755");
1557 if (ret) {
1558 rmdir(dev_path);
1559 return ret;
1560 }
1561
1562 /* We want to set the mode directly from the spec. */
1563 mask = umask(0);
1564
1565 /* Get a handle to the temp dev path for *at funcs below. */
1566 dev_fd = open(dev_path, O_DIRECTORY|O_PATH|O_CLOEXEC);
1567 if (dev_fd < 0) {
1568 ret = 1;
1569 goto done;
1570 }
1571
1572 /* Create all the nodes in /dev. */
1573 for (i = 0; i < ARRAY_SIZE(device_nodes); ++i) {
1574 const struct dev_spec *ds = &device_nodes[i];
1575 ret = mknodat(dev_fd, ds->name, ds->mode,
1576 makedev(ds->major, ds->minor));
1577 if (ret)
1578 goto done;
1579 }
1580
1581 /* Create all the symlinks in /dev. */
1582 for (i = 0; i < ARRAY_SIZE(device_symlinks); ++i) {
1583 const struct dev_sym_spec *ds = &device_symlinks[i];
1584 ret = symlinkat(ds->dest, dev_fd, ds->source);
1585 if (ret)
1586 goto done;
1587 }
1588
Mike Frysinger604cc7b2020-12-29 18:18:56 -05001589 /* Create empty dir for glibc shared mem APIs. */
1590 ret = mkdirat(dev_fd, "shm", 01777);
1591 if (ret)
1592 goto done;
1593
Mike Frysinger33ffef32017-01-13 19:53:19 -05001594 /* Restore old mask. */
1595 done:
1596 close(dev_fd);
1597 umask(mask);
1598
1599 if (ret)
1600 mount_dev_cleanup(dev_path);
1601
1602 return ret;
1603}
1604
1605/*
1606 * Relocate the temporary /dev mount to its final /dev place.
1607 * We have to do this two step process so people can bind mount extra
1608 * /dev paths like /dev/log.
1609 */
1610static int mount_dev_finalize(const struct minijail *j, char *dev_path)
1611{
1612 int ret = -1;
1613 char *dest = NULL;
1614
1615 /* Unmount the /dev mount if possible. */
1616 if (umount2("/dev", MNT_DETACH))
1617 goto done;
1618
1619 if (asprintf(&dest, "%s/dev", j->chrootdir ? : "") < 0)
1620 goto done;
1621
1622 if (mount(dev_path, dest, NULL, MS_MOVE, NULL))
1623 goto done;
1624
1625 ret = 0;
1626 done:
1627 free(dest);
1628 mount_dev_cleanup(dev_path);
1629
1630 return ret;
1631}
1632
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001633/*
1634 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001635 * @j Minijail these mounts are for
1636 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001637 *
1638 * Returns 0 for success.
1639 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001640static int mount_one(const struct minijail *j, struct mountpoint *m,
1641 const char *dev_path)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001642{
Dylan Reid648b2202015-10-23 00:50:00 -07001643 int ret;
1644 char *dest;
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001645 int remount = 0;
1646 unsigned long original_mnt_flags = 0;
Dylan Reid648b2202015-10-23 00:50:00 -07001647
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -04001648 /* We assume |dest| has a leading "/". */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001649 if (dev_path && strncmp("/dev/", m->dest, 5) == 0) {
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -04001650 /*
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -04001651 * Since the temp path is rooted at /dev, skip that dest part.
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -04001652 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001653 if (asprintf(&dest, "%s%s", dev_path, m->dest + 4) < 0)
1654 return -ENOMEM;
1655 } else {
Mike Frysingerac08a682017-10-10 02:04:50 -04001656 if (asprintf(&dest, "%s%s", j->chrootdir ?: "", m->dest) < 0)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001657 return -ENOMEM;
1658 }
Dylan Reid648b2202015-10-23 00:50:00 -07001659
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001660 ret =
1661 setup_mount_destination(m->src, dest, j->uid, j->gid,
1662 (m->flags & MS_BIND), &original_mnt_flags);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001663 if (ret) {
François Degrosa42182d2020-04-29 00:41:52 +10001664 warn("cannot create mount target '%s'", dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001665 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001666 }
Dylan Reideec77962016-06-30 19:35:10 -07001667
Dylan Reid648b2202015-10-23 00:50:00 -07001668 /*
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001669 * Bind mounts that change the 'ro' flag have to be remounted since
1670 * 'bind' and other flags can't both be specified in the same command.
1671 * Remount after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001672 */
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001673 if ((m->flags & MS_BIND) &&
1674 ((m->flags & MS_RDONLY) != (original_mnt_flags & MS_RDONLY))) {
1675 remount = 1;
1676 /*
1677 * Restrict the mount flags to those that are user-settable in a
1678 * MS_REMOUNT request, but excluding MS_RDONLY. The
1679 * user-requested mount flags will dictate whether the remount
1680 * will have that flag or not.
1681 */
1682 original_mnt_flags &= (MS_USER_SETTABLE_MASK & ~MS_RDONLY);
Elly Jonesa1059632011-12-15 15:17:07 -05001683 }
Dylan Reid648b2202015-10-23 00:50:00 -07001684
Dylan Reid81e23972016-05-18 14:06:35 -07001685 ret = mount(m->src, dest, m->type, m->flags, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001686 if (ret) {
François Degrosa42182d2020-04-29 00:41:52 +10001687 pwarn("cannot bind-mount '%s' as '%s' with flags %#lx", m->src,
1688 dest, m->flags);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001689 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001690 }
Dylan Reid648b2202015-10-23 00:50:00 -07001691
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001692 if (remount) {
1693 ret =
1694 mount(m->src, dest, NULL,
1695 m->flags | original_mnt_flags | MS_REMOUNT, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001696 if (ret) {
François Degrosa42182d2020-04-29 00:41:52 +10001697 pwarn(
1698 "cannot bind-remount '%s' as '%s' with flags %#lx",
1699 m->src, dest,
1700 m->flags | original_mnt_flags | MS_REMOUNT);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001701 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001702 }
Dylan Reid648b2202015-10-23 00:50:00 -07001703 }
1704
Elly Jones51a5b6c2011-10-12 19:09:26 -04001705 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001706 if (m->next)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001707 return mount_one(j, m->next, dev_path);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001708 return 0;
1709
1710error:
1711 free(dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001712 return ret;
1713}
1714
Mike Frysingerac08a682017-10-10 02:04:50 -04001715static void process_mounts_or_die(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001716{
Mike Frysingerac08a682017-10-10 02:04:50 -04001717 /*
1718 * We have to mount /dev first in case there are bind mounts from
1719 * the original /dev into the new unique tmpfs one.
1720 */
1721 char *dev_path = NULL;
1722 if (j->flags.mount_dev && mount_dev(&dev_path))
1723 pdie("mount_dev failed");
Dylan Reid648b2202015-10-23 00:50:00 -07001724
Mike Frysingerac08a682017-10-10 02:04:50 -04001725 if (j->mounts_head && mount_one(j, j->mounts_head, dev_path)) {
François Degrosa42182d2020-04-29 00:41:52 +10001726 if (dev_path)
Mike Frysingerac08a682017-10-10 02:04:50 -04001727 mount_dev_cleanup(dev_path);
François Degrosa42182d2020-04-29 00:41:52 +10001728
1729 _exit(MINIJAIL_ERR_MOUNT);
Mike Frysingerac08a682017-10-10 02:04:50 -04001730 }
Mike Frysinger33ffef32017-01-13 19:53:19 -05001731
1732 /*
Mike Frysingerac08a682017-10-10 02:04:50 -04001733 * Once all bind mounts have been processed, move the temp dev to
1734 * its final /dev home.
Mike Frysinger33ffef32017-01-13 19:53:19 -05001735 */
1736 if (j->flags.mount_dev && mount_dev_finalize(j, dev_path))
Mike Frysingerac08a682017-10-10 02:04:50 -04001737 pdie("mount_dev_finalize failed");
1738}
Elly Jones51a5b6c2011-10-12 19:09:26 -04001739
Mike Frysingerac08a682017-10-10 02:04:50 -04001740static int enter_chroot(const struct minijail *j)
1741{
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001742 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1743
Elly Jones51a5b6c2011-10-12 19:09:26 -04001744 if (chroot(j->chrootdir))
1745 return -errno;
1746
1747 if (chdir("/"))
1748 return -errno;
1749
1750 return 0;
1751}
1752
Mike Frysingerac08a682017-10-10 02:04:50 -04001753static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001754{
Mike Frysingerac08a682017-10-10 02:04:50 -04001755 int oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001756
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001757 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1758
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001759 /*
1760 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001761 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001762 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001763 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001764 if (oldroot < 0)
1765 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001766 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001767 if (newroot < 0)
1768 pdie("failed to open %s for fchdir", j->chrootdir);
1769
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001770 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001771 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001772 * do a self bind mount.
1773 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001774 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1775 pdie("failed to bind mount '%s'", j->chrootdir);
1776 if (chdir(j->chrootdir))
1777 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001778 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001779 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001780
1781 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001782 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001783 * change to the old root and unmount it.
1784 */
1785 if (fchdir(oldroot))
1786 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001787
1788 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05001789 * If skip_remount_private was enabled for minijail_enter(),
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001790 * there could be a shared mount point under |oldroot|. In that case,
1791 * mounts under this shared mount point will be unmounted below, and
1792 * this unmounting will propagate to the original mount namespace
1793 * (because the mount point is shared). To prevent this unexpected
1794 * unmounting, remove these mounts from their peer groups by recursively
1795 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001796 */
1797 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001798 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001799 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001800 if (umount2(".", MNT_DETACH))
1801 pdie("umount(/)");
1802 /* Change back to the new root. */
1803 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001804 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001805 if (close(oldroot))
1806 return -errno;
1807 if (close(newroot))
1808 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001809 if (chroot("/"))
1810 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001811 /* Set correct CWD for getcwd(3). */
1812 if (chdir("/"))
1813 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001814
1815 return 0;
1816}
1817
Martin Pelikánab9eb442017-01-25 11:53:58 +11001818static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001819{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001820 const char fmt[] = "size=%zu,mode=1777";
1821 /* Count for the user storing ULLONG_MAX literally + extra space. */
1822 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1823 int ret;
1824
1825 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1826
1827 if (ret <= 0)
1828 pdie("tmpfs size spec error");
1829 else if ((size_t)ret >= sizeof(data))
1830 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001831 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001832 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001833}
1834
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001835static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001836{
1837 const char *kProcPath = "/proc";
1838 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001839 /*
1840 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001841 * /proc in our namespace, which means using MS_REMOUNT here would
1842 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001843 * namespace (!). Instead, remove their mount from our namespace lazily
1844 * (MNT_DETACH) and make our own.
Jorge Lucangeli Obes320c4fc2020-12-10 10:38:30 -05001845 *
1846 * However, we skip this in the user namespace case because it will
1847 * invariably fail. Every mount namespace is "owned" by the
1848 * user namespace of the process that creates it. Mount namespace A is
1849 * "less privileged" than mount namespace B if A is created off of B,
1850 * and B is owned by a different user namespace.
1851 * When a less privileged mount namespace is created, the mounts used to
1852 * initialize it (coming from the more privileged mount namespace) come
1853 * as a unit, and are locked together. This means that code running in
1854 * the new mount (and user) namespace cannot piecemeal unmount
1855 * individual mounts inherited from a more privileged mount namespace.
1856 * See https://man7.org/linux/man-pages/man7/mount_namespaces.7.html,
1857 * "Restrictions on mount namespaces" for details.
1858 *
1859 * This happens in our use case because we first enter a new user
1860 * namespace (on clone(2)) and then we unshare(2) a new mount namespace,
1861 * which means the new mount namespace is less privileged than its
1862 * parent mount namespace. This would also happen if we entered a new
1863 * mount namespace on clone(2), since the user namespace is created
1864 * first.
1865 * In all other non-user-namespace cases the new mount namespace is
1866 * similarly privileged as the parent mount namespace so unmounting a
1867 * single mount is allowed.
1868 *
1869 * We still remount /proc as read-only in the user namespace case
1870 * because while a process with CAP_SYS_ADMIN in the new user namespace
1871 * can unmount the RO mount and get at the RW mount, an attacker with
1872 * access only to a write primitive will not be able to modify /proc.
Elly Jonese1749eb2011-10-07 13:54:59 -04001873 */
Jorge Lucangeli Obes320c4fc2020-12-10 10:38:30 -05001874 if (!j->flags.userns && umount2(kProcPath, MNT_DETACH))
1875 return -errno;
Mike Frysinger3ba81572017-01-17 23:33:28 -05001876 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001877 return -errno;
1878 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001879}
1880
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001881static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001882{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001883 kill(j->initpid, SIGKILL);
1884 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001885}
1886
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001887static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001888{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001889 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001890 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001891}
1892
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001893static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001894{
1895 size_t i;
1896
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001897 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001898 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001899 kill_child_and_die(j, "failed to add to cgroups");
1900 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001901}
1902
Dylan Reid0f72ef42017-06-06 15:42:49 -07001903static void set_rlimits_or_die(const struct minijail *j)
1904{
1905 size_t i;
1906
1907 for (i = 0; i < j->rlimit_count; ++i) {
1908 struct rlimit limit;
1909 limit.rlim_cur = j->rlimits[i].cur;
1910 limit.rlim_max = j->rlimits[i].max;
1911 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1912 kill_child_and_die(j, "failed to set rlimit");
1913 }
1914}
1915
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001916static void write_ugid_maps_or_die(const struct minijail *j)
1917{
1918 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1919 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001920 if (j->gidmap && j->flags.disable_setgroups) {
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001921 /*
1922 * Older kernels might not have the /proc/<pid>/setgroups files.
1923 */
Mike Frysinger6b190c02017-01-04 17:18:42 -05001924 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001925 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001926 if (ret == -ENOENT) {
1927 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1928 warn("could not disable setgroups(2)");
1929 } else
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001930 kill_child_and_die(
1931 j, "failed to disable setgroups(2)");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001932 }
1933 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001934 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1935 kill_child_and_die(j, "failed to write gid_map");
1936}
1937
1938static void enter_user_namespace(const struct minijail *j)
1939{
Luis Hector Chavez71323552017-09-05 09:17:22 -07001940 int uid = j->flags.uid ? j->uid : 0;
1941 int gid = j->flags.gid ? j->gid : 0;
1942 if (j->gidmap && setresgid(gid, gid, gid)) {
1943 pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid,
1944 gid);
1945 }
1946 if (j->uidmap && setresuid(uid, uid, uid)) {
1947 pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid,
1948 uid);
1949 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001950}
1951
1952static void parent_setup_complete(int *pipe_fds)
1953{
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001954 close_and_reset(&pipe_fds[0]);
1955 close_and_reset(&pipe_fds[1]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001956}
1957
1958/*
1959 * wait_for_parent_setup: Called by the child process to wait for any
1960 * further parent-side setup to complete before continuing.
1961 */
1962static void wait_for_parent_setup(int *pipe_fds)
1963{
1964 char buf;
1965
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001966 close_and_reset(&pipe_fds[1]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001967
1968 /* Wait for parent to complete setup and close the pipe. */
1969 if (read(pipe_fds[0], &buf, 1) != 0)
1970 die("failed to sync with parent");
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001971 close_and_reset(&pipe_fds[0]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001972}
1973
1974static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001975{
Lutz Justen13807cb2017-01-03 17:11:55 +01001976 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1977 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001978 die("can only do one of inherit, keep, or set supplementary "
1979 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001980 }
1981
Lutz Justen13807cb2017-01-03 17:11:55 +01001982 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001983 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001984 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001985 } else if (j->flags.set_suppl_gids) {
1986 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001987 pdie("setgroups(suppl_gids) failed");
Luis Hector Chavez71323552017-09-05 09:17:22 -07001988 } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001989 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001990 * Only attempt to clear supplementary groups if we are changing
Luis Hector Chavez71323552017-09-05 09:17:22 -07001991 * users or groups, and if the caller did not request to disable
1992 * setgroups (used when entering a user namespace as a
1993 * non-privileged user).
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001994 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001995 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001996 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001997 }
1998
1999 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002000 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002001
2002 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002003 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002004}
2005
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002006static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
2007{
2008 const uint64_t one = 1;
2009 unsigned int i;
2010 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
2011 if (keep_mask & (one << i))
2012 continue;
2013 if (prctl(PR_CAPBSET_DROP, i))
2014 pdie("could not drop capability from bounding set");
2015 }
2016}
2017
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002018static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04002019{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08002020 if (!j->flags.use_caps)
2021 return;
2022
Elly Jonese1749eb2011-10-07 13:54:59 -04002023 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08002024 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04002025 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08002026 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04002027 unsigned int i;
2028 if (!caps)
2029 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04002030 if (cap_clear(caps))
2031 die("can't clear caps");
2032
2033 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08002034 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08002035 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04002036 continue;
Kees Cook323878a2013-02-05 15:35:24 -08002037 flag[0] = i;
2038 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04002039 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08002040 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04002041 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08002042 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04002043 die("can't add inheritable cap");
2044 }
2045 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08002046 die("can't apply initial cleaned capset");
2047
2048 /*
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04002049 * Instead of dropping the bounding set first, do it here in case
Kees Cook323878a2013-02-05 15:35:24 -08002050 * the caller had a more permissive bounding set which could
2051 * have been used above to raise a capability that wasn't already
2052 * present. This requires CAP_SETPCAP, so we raised/kept it above.
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04002053 *
2054 * However, if we're asked to skip setting *and* locking the
2055 * SECURE_NOROOT securebit, also skip dropping the bounding set.
2056 * If the caller wants to regain all capabilities when executing a
2057 * set-user-ID-root program, allow them to do so. The default behavior
2058 * (i.e. the behavior without |securebits_skip_mask| set) will still put
2059 * the jailed process tree in a capabilities-only environment.
2060 *
2061 * We check the negated skip mask for SECURE_NOROOT and
2062 * SECURE_NOROOT_LOCKED. If the bits are set in the negated mask they
2063 * will *not* be skipped in lock_securebits(), and therefore we should
2064 * drop the bounding set.
Kees Cook323878a2013-02-05 15:35:24 -08002065 */
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04002066 if (secure_noroot_set_and_locked(~j->securebits_skip_mask)) {
2067 drop_capbset(j->caps, last_valid_cap);
2068 } else {
2069 warn("SECURE_NOROOT not set, not dropping bounding set");
2070 }
Kees Cook323878a2013-02-05 15:35:24 -08002071
2072 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08002073 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08002074 flag[0] = CAP_SETPCAP;
2075 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
2076 die("can't clear effective cap");
2077 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
2078 die("can't clear permitted cap");
2079 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
2080 die("can't clear inheritable cap");
2081 }
2082
2083 if (cap_set_proc(caps))
2084 die("can't apply final cleaned capset");
2085
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04002086 /*
2087 * If ambient capabilities are supported, clear all capabilities first,
2088 * then raise the requested ones.
2089 */
2090 if (j->flags.set_ambient_caps) {
2091 if (!cap_ambient_supported()) {
2092 pdie("ambient capabilities not supported");
2093 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04002094 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
2095 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04002096 pdie("can't clear ambient capabilities");
2097 }
2098
2099 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
2100 if (!(j->caps & (one << i)))
2101 continue;
2102
2103 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
2104 0) != 0) {
2105 pdie("prctl(PR_CAP_AMBIENT, "
2106 "PR_CAP_AMBIENT_RAISE, %u) failed",
2107 i);
2108 }
2109 }
2110 }
2111
Kees Cook323878a2013-02-05 15:35:24 -08002112 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04002113}
2114
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002115static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002116{
2117 /*
2118 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
2119 * in the kernel source tree for an explanation of the parameters.
2120 */
2121 if (j->flags.no_new_privs) {
2122 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
2123 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
2124 }
2125
2126 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07002127 * Code running with ASan
2128 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
2129 * will make system calls not included in the syscall filter policy,
2130 * which will likely crash the program. Skip setting seccomp filter in
2131 * that case.
2132 * 'running_with_asan()' has no inputs and is completely defined at
2133 * build time, so this cannot be used by an attacker to skip setting
2134 * seccomp filter.
2135 */
Evgenii Stepanov3d98f3c2018-08-23 15:06:50 -07002136 if (j->flags.seccomp_filter && running_with_asan()) {
Evgenii Stepanov825828c2018-07-27 11:57:07 -07002137 warn("running with (HW)ASan, not setting seccomp filter");
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07002138 return;
2139 }
2140
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002141 if (j->flags.seccomp_filter) {
2142 if (j->flags.seccomp_filter_logging) {
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002143 warn("logging seccomp filter failures");
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04002144 if (!seccomp_ret_log_available()) {
2145 /*
2146 * If SECCOMP_RET_LOG is not available,
2147 * install the SIGSYS handler first.
2148 */
2149 if (install_sigsys_handler())
2150 pdie(
2151 "failed to install SIGSYS handler");
2152 }
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002153 } else if (j->flags.seccomp_filter_tsync) {
2154 /*
2155 * If setting thread sync,
2156 * reset the SIGSYS signal handler so that
2157 * the entire thread group is killed.
2158 */
2159 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
2160 pdie("failed to reset SIGSYS disposition");
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002161 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002162 }
2163
2164 /*
2165 * Install the syscall filter.
2166 */
2167 if (j->flags.seccomp_filter) {
Anand K Mistry31adc6c2020-11-26 11:39:46 +11002168 if (j->flags.seccomp_filter_tsync ||
2169 j->flags.seccomp_filter_allow_speculation) {
2170 int filter_flags =
2171 (j->flags.seccomp_filter_tsync
2172 ? SECCOMP_FILTER_FLAG_TSYNC
2173 : 0) |
2174 (j->flags.seccomp_filter_allow_speculation
2175 ? SECCOMP_FILTER_FLAG_SPEC_ALLOW
2176 : 0);
2177 if (sys_seccomp(SECCOMP_SET_MODE_FILTER, filter_flags,
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002178 j->filter_prog)) {
2179 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002180 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002181 } else {
2182 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
2183 j->filter_prog)) {
2184 pdie("prctl(seccomp_filter) failed");
2185 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002186 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002187 }
2188}
2189
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002190static pid_t forward_pid = -1;
2191
Mike Frysinger33d051a2018-05-30 16:41:10 -04002192static void forward_signal(int sig,
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04002193 siginfo_t *siginfo attribute_unused,
2194 void *void_context attribute_unused)
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002195{
2196 if (forward_pid != -1) {
Mike Frysinger33d051a2018-05-30 16:41:10 -04002197 kill(forward_pid, sig);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002198 }
2199}
2200
2201static void install_signal_handlers(void)
2202{
2203 struct sigaction act;
2204
2205 memset(&act, 0, sizeof(act));
2206 act.sa_sigaction = &forward_signal;
2207 act.sa_flags = SA_SIGINFO | SA_RESTART;
2208
2209 /* Handle all signals, except SIGCHLD. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002210 for (int sig = 1; sig < NSIG; sig++) {
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002211 /*
2212 * We don't care if we get EINVAL: that just means that we
2213 * can't handle this signal, so let's skip it and continue.
2214 */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002215 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002216 }
2217 /* Reset SIGCHLD's handler. */
2218 signal(SIGCHLD, SIG_DFL);
2219
2220 /* Handle real-time signals. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002221 for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++) {
2222 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002223 }
2224}
2225
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002226static const char *lookup_hook_name(minijail_hook_event_t event)
2227{
2228 switch (event) {
2229 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
2230 return "pre-drop-caps";
2231 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
2232 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07002233 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
2234 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002235 case MINIJAIL_HOOK_EVENT_MAX:
2236 /*
2237 * Adding this in favor of a default case to force the
2238 * compiler to error out if a new enum value is added.
2239 */
2240 break;
2241 }
2242 return "unknown";
2243}
2244
2245static void run_hooks_or_die(const struct minijail *j,
2246 minijail_hook_event_t event)
2247{
2248 int rc;
2249 int hook_index = 0;
2250 for (struct hook *c = j->hooks_head; c; c = c->next) {
2251 if (c->event != event)
2252 continue;
2253 rc = c->hook(c->payload);
2254 if (rc != 0) {
2255 errno = -rc;
2256 pdie("%s hook (index %d) failed",
2257 lookup_hook_name(event), hook_index);
2258 }
2259 /* Only increase the index within the same hook event type. */
2260 ++hook_index;
2261 }
2262}
2263
Will Drewry6ac91122011-10-21 16:38:58 -05002264void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002265{
Dylan Reidf682d472015-09-17 21:39:07 -07002266 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002267 * If we're dropping caps, get the last valid cap from /proc now,
2268 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07002269 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002270 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002271 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002272 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07002273
Elly Jonese1749eb2011-10-07 13:54:59 -04002274 if (j->flags.pids)
2275 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002276 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04002277
Lutz Justen13807cb2017-01-03 17:11:55 +01002278 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05002279 die("cannot inherit supplementary groups without setting a "
2280 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04002281
Elly Jonesdd3e8512012-01-23 15:13:38 -05002282 /*
2283 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04002284 * so we don't even try. If any of our operations fail, we abort() the
2285 * entire process.
2286 */
Mike Frysinger902a4492018-12-27 05:22:56 -05002287 if (j->flags.enter_vfs) {
2288 if (setns(j->mountns_fd, CLONE_NEWNS))
2289 pdie("setns(CLONE_NEWNS) failed");
2290 close(j->mountns_fd);
2291 }
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002292
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07002293 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002294 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002295 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002296 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05002297 * By default, remount all filesystems as private, unless
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04002298 * - Passed a specific remount mode, in which case remount with
2299 * that,
2300 * - Asked not to remount at all, in which case skip the
2301 * mount(2) call.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002302 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
2303 */
Mike Frysinger785b1c32018-02-23 15:47:24 -05002304 if (j->remount_mode) {
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04002305 if (mount(NULL, "/", NULL, MS_REC | j->remount_mode,
2306 NULL))
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -05002307 pdie("mount(NULL, /, NULL, "
2308 "MS_REC | j->remount_mode, NULL) failed");
Nicole Anderson-Au835f7172021-01-13 21:18:13 +00002309
2310 struct minijail_remount *temp = j->remounts_head;
2311 while (temp) {
Nicole Anderson-Aue119bbb2021-02-04 23:12:12 +00002312 if (temp->remount_mode < j->remount_mode)
2313 die("cannot remount %s as stricter "
2314 "than the root dir",
2315 temp->mount_name);
Nicole Anderson-Au835f7172021-01-13 21:18:13 +00002316 if (mount(NULL, temp->mount_name, NULL,
2317 MS_REC | temp->remount_mode, NULL))
2318 pdie("mount(NULL, %s, NULL, "
2319 "MS_REC | temp->remount_mode, NULL) "
2320 "failed", temp->mount_name);
2321 temp = temp->next;
2322 }
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002323 }
Nicole Anderson-Au835f7172021-01-13 21:18:13 +00002324
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002325 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04002326
Dylan Reidf7942472015-11-18 17:55:26 -08002327 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002328 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08002329 }
2330
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002331 if (j->flags.uts) {
2332 if (unshare(CLONE_NEWUTS))
2333 pdie("unshare(CLONE_NEWUTS) failed");
2334
2335 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
2336 pdie("sethostname(%s) failed", j->hostname);
2337 }
2338
Dylan Reid1102f5a2015-09-15 11:52:20 -07002339 if (j->flags.enter_net) {
2340 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002341 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger902a4492018-12-27 05:22:56 -05002342 close(j->netns_fd);
Mike Frysinger7559dfe2016-11-15 18:58:39 -05002343 } else if (j->flags.net) {
2344 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002345 pdie("unshare(CLONE_NEWNET) failed");
2346 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07002347 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002348
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002349 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002350 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002351
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08002352 if (j->flags.new_session_keyring) {
2353 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
2354 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
2355 }
2356
Mike Frysingerac08a682017-10-10 02:04:50 -04002357 /* We have to process all the mounts before we chroot/pivot_root. */
2358 process_mounts_or_die(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002359
Mike Frysingerac08a682017-10-10 02:04:50 -04002360 if (j->flags.chroot && enter_chroot(j))
Mike Frysinger33ffef32017-01-13 19:53:19 -05002361 pdie("chroot");
Mike Frysinger33ffef32017-01-13 19:53:19 -05002362
Mike Frysingerac08a682017-10-10 02:04:50 -04002363 if (j->flags.pivot_root && enter_pivot_root(j))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08002364 pdie("pivot_root");
2365
Martin Pelikánab9eb442017-01-25 11:53:58 +11002366 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07002367 pdie("mount_tmp");
2368
Dylan Reid791f5772015-09-14 20:02:42 -07002369 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04002370 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04002371
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002372 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
2373
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002374 /*
2375 * If we're only dropping capabilities from the bounding set, but not
2376 * from the thread's (permitted|inheritable|effective) sets, do it now.
2377 */
2378 if (j->flags.capbset_drop) {
2379 drop_capbset(j->cap_bset, last_valid_cap);
2380 }
2381
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002382 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002383 * POSIX capabilities are a bit tricky. We must set SECBIT_KEEP_CAPS
2384 * before drop_ugid() below as the latter would otherwise drop all
2385 * capabilities.
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002386 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002387 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002388 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002389 * When using ambient capabilities, CAP_SET{GID,UID} can be
2390 * inherited across execve(2), so SECBIT_KEEP_CAPS is not
2391 * strictly needed.
Elly Jonese1749eb2011-10-07 13:54:59 -04002392 */
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002393 bool require_keep_caps = !j->flags.set_ambient_caps;
2394 if (lock_securebits(j->securebits_skip_mask,
2395 require_keep_caps) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002396 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002397 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002398 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002399
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002400 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002401 /*
2402 * If we're setting no_new_privs, we can drop privileges
2403 * before setting seccomp filter. This way filter policies
2404 * don't need to allow privilege-dropping syscalls.
2405 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002406 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002407 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002408 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002409 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002410 /*
2411 * If we're not setting no_new_privs,
2412 * we need to set seccomp filter *before* dropping privileges.
2413 * WARNING: this means that filter policies *must* allow
2414 * setgroups()/setresgid()/setresuid() for dropping root and
2415 * capget()/capset()/prctl() for dropping caps.
2416 */
2417 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002418 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002419 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04002420 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002421
Elly Jonesdd3e8512012-01-23 15:13:38 -05002422 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08002423 * Select the specified alternate syscall table. The table must not
2424 * block prctl(2) if we're using seccomp as well.
2425 */
2426 if (j->flags.alt_syscall) {
2427 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002428 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08002429 }
2430
2431 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05002432 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04002433 * privilege-dropping syscalls :)
2434 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002435 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04002436 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002437 warn("seccomp not supported");
2438 return;
2439 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002440 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002441 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002442}
2443
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002444/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04002445static int init_exitstatus = 0;
2446
Mike Frysinger0a27ab02020-09-04 16:18:12 -04002447static void init_term(int sig attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002448{
2449 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04002450}
2451
Mike Frysinger0a27ab02020-09-04 16:18:12 -04002452static void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04002453{
2454 pid_t pid;
2455 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002456 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002457 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002458 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002459 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002460 /*
2461 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04002462 * left inside our pid namespace or we get a signal.
2463 */
2464 if (pid == rootpid)
2465 init_exitstatus = status;
2466 }
2467 if (!WIFEXITED(init_exitstatus))
2468 _exit(MINIJAIL_ERR_INIT);
2469 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04002470}
2471
Will Drewry6ac91122011-10-21 16:38:58 -05002472int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002473{
2474 size_t sz = 0;
2475 size_t bytes = read(fd, &sz, sizeof(sz));
2476 char *buf;
2477 int r;
2478 if (sizeof(sz) != bytes)
2479 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002480 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04002481 return -E2BIG;
2482 buf = malloc(sz);
2483 if (!buf)
2484 return -ENOMEM;
2485 bytes = read(fd, buf, sz);
2486 if (bytes != sz) {
2487 free(buf);
2488 return -EINVAL;
2489 }
2490 r = minijail_unmarshal(j, buf, sz);
2491 free(buf);
2492 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002493}
2494
Will Drewry6ac91122011-10-21 16:38:58 -05002495int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04002496{
Elly Jonese1749eb2011-10-07 13:54:59 -04002497 size_t sz = minijail_size(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002498 if (!sz)
2499 return -EINVAL;
François Degros664eba72019-11-05 13:18:24 +11002500
2501 char *buf = malloc(sz);
2502 if (!buf)
2503 return -ENOMEM;
2504
2505 int err = minijail_marshal(j, buf, sz);
2506 if (err)
2507 goto error;
2508
Elly Jonese1749eb2011-10-07 13:54:59 -04002509 /* Sends [size][minijail]. */
François Degros664eba72019-11-05 13:18:24 +11002510 err = write_exactly(fd, &sz, sizeof(sz));
2511 if (err)
2512 goto error;
2513
2514 err = write_exactly(fd, buf, sz);
2515
2516error:
Elly Jonese1749eb2011-10-07 13:54:59 -04002517 free(buf);
François Degros664eba72019-11-05 13:18:24 +11002518 return err;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002519}
Elly Jonescd7a9042011-07-22 13:56:51 -04002520
Dylan Reid6dc224f2021-05-12 17:06:25 -07002521int API minijail_copy_jail(const struct minijail *from, struct minijail *out)
2522{
2523 size_t sz = minijail_size(from);
2524 if (!sz)
2525 return -EINVAL;
2526
2527 char *buf = malloc(sz);
2528 if (!buf)
2529 return -ENOMEM;
2530
2531 int err = minijail_marshal(from, buf, sz);
2532 if (err)
2533 goto error;
2534
2535 err = minijail_unmarshal(out, buf, sz);
2536error:
2537 free(buf);
2538 return err;
2539}
2540
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002541static int setup_preload(const struct minijail *j attribute_unused,
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002542 char ***child_env attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002543{
Daniel Erat5b7a3182015-08-19 16:06:22 -06002544#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002545 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002546 return 0;
2547#else
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002548 const char *preload_path = j->preload_path ?: PRELOADPATH;
2549 char *newenv = NULL;
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002550 int ret = 0;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002551 const char *oldenv = getenv(kLdPreloadEnvVar);
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002552
2553 if (!oldenv)
2554 oldenv = "";
Elly Jonescd7a9042011-07-22 13:56:51 -04002555
Elly Jonese1749eb2011-10-07 13:54:59 -04002556 /* Only insert a separating space if we have something to separate... */
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002557 if (asprintf(&newenv, "%s%s%s", oldenv, oldenv[0] != '\0' ? " " : "",
2558 preload_path) < 0) {
2559 return -1;
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002560 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002561
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002562 ret = minijail_setenv(child_env, kLdPreloadEnvVar, newenv, 1);
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002563 free(newenv);
2564 return ret;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002565#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04002566}
2567
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002568static int setup_pipe(char ***child_env, int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04002569{
2570 int r = pipe(fds);
2571 char fd_buf[11];
2572 if (r)
2573 return r;
2574 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2575 if (r <= 0)
2576 return -EINVAL;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002577 return minijail_setenv(child_env, kFdEnvVar, fd_buf, 1);
Will Drewryf89aef52011-09-16 16:48:57 -05002578}
2579
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002580static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002581{
2582 const char *kFdPath = "/proc/self/fd";
2583
2584 DIR *d = opendir(kFdPath);
2585 struct dirent *dir_entry;
2586
2587 if (d == NULL)
2588 return -1;
2589 int dir_fd = dirfd(d);
2590 while ((dir_entry = readdir(d)) != NULL) {
2591 size_t i;
2592 char *end;
2593 bool should_close = true;
2594 const int fd = strtol(dir_entry->d_name, &end, 10);
2595
2596 if ((*end) != '\0') {
2597 continue;
2598 }
2599 /*
2600 * We might have set up some pipes that we want to share with
2601 * the parent process, and should not be closed.
2602 */
2603 for (i = 0; i < size; ++i) {
2604 if (fd == inheritable_fds[i]) {
2605 should_close = false;
2606 break;
2607 }
2608 }
2609 /* Also avoid closing the directory fd. */
2610 if (should_close && fd != dir_fd)
2611 close(fd);
2612 }
2613 closedir(d);
2614 return 0;
2615}
2616
Allen Webbc7182682021-04-16 09:44:53 -05002617/* Return true if the specified file descriptor is already open. */
2618static int fd_is_open(int fd)
2619{
2620 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
2621}
2622
2623static_assert(FD_SETSIZE >= MAX_PRESERVED_FDS * 2 - 1,
2624 "If true, ensure_no_fd_conflict will always find an unused fd.");
2625
2626/* If p->parent_fd will be used by a child_fd, move it to an unused fd. */
2627static int ensure_no_fd_conflict(const fd_set* child_fds,
2628 struct preserved_fd* p)
2629{
2630 if (!FD_ISSET(p->parent_fd, child_fds)){
2631 return 0;
2632 }
2633
2634 /*
2635 * If no other parent_fd matches the child_fd then use it instead of a
2636 * temporary.
2637 */
2638 int fd = p->child_fd;
2639 if (fd_is_open(fd)) {
2640 fd = FD_SETSIZE - 1;
2641 while (FD_ISSET(fd, child_fds) || fd_is_open(fd)) {
2642 --fd;
2643 if (fd < 0) {
2644 die("failed to find an unused fd");
2645 }
2646 }
2647 }
2648
2649 int ret = dup2(p->parent_fd, fd);
2650 /*
2651 * warn() opens a file descriptor so it needs to happen after dup2 to
2652 * avoid unintended side effects. This can be avoided by reordering the
2653 * mapping requests so that the source fds with overlap are mapped
2654 * first (unless there are cycles).
2655 */
2656 warn("mapped fd overlap: moving %d to %d", p->parent_fd, fd);
2657 if (ret == -1) {
2658 return -1;
2659 }
2660
2661 p->parent_fd = fd;
2662 return 0;
2663}
2664
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002665static int redirect_fds(struct minijail *j)
2666{
Allen Webbc7182682021-04-16 09:44:53 -05002667 fd_set child_fds;
2668 FD_ZERO(&child_fds);
2669
2670 /* Relocate parent_fds that would be replaced by a child_fd. */
2671 for (size_t i = 0; i < j->preserved_fd_count; i++) {
2672 int child_fd = j->preserved_fds[i].child_fd;
2673 if (FD_ISSET(child_fd, &child_fds)) {
2674 die("fd %d is mapped more than once", child_fd);
2675 }
2676
2677 if (ensure_no_fd_conflict(&child_fds,
2678 &j->preserved_fds[i]) == -1) {
2679 return -1;
2680 }
2681
2682 FD_SET(child_fd, &child_fds);
2683 }
2684
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002685 for (size_t i = 0; i < j->preserved_fd_count; i++) {
Luis Héctor Cháveza63407a2021-01-03 05:47:00 -08002686 if (j->preserved_fds[i].parent_fd ==
2687 j->preserved_fds[i].child_fd) {
2688 continue;
2689 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002690 if (dup2(j->preserved_fds[i].parent_fd,
2691 j->preserved_fds[i].child_fd) == -1) {
2692 return -1;
2693 }
2694 }
Mattias Nissler1cf29fb2020-04-20 23:14:03 +02002695 /*
2696 * After all fds have been duped, we are now free to close all parent
2697 * fds that are *not* child fds.
2698 */
2699 for (size_t i = 0; i < j->preserved_fd_count; i++) {
Allen Webbc7182682021-04-16 09:44:53 -05002700 int parent_fd = j->preserved_fds[i].parent_fd;
2701 if (!FD_ISSET(parent_fd, &child_fds)) {
2702 close(parent_fd);
Mattias Nissler1cf29fb2020-04-20 23:14:03 +02002703 }
Mattias Nissler1cf29fb2020-04-20 23:14:03 +02002704 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002705 return 0;
2706}
2707
Dylan Reidacfb8be2017-08-25 12:56:51 -07002708/*
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002709 * Structure holding resources and state created when running a minijail.
2710 */
2711struct minijail_run_state {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002712 pid_t child_pid;
2713 int pipe_fds[2];
2714 int stdin_fds[2];
2715 int stdout_fds[2];
2716 int stderr_fds[2];
2717 int child_sync_pipe_fds[2];
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002718 char **child_env;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002719};
2720
2721static void minijail_free_run_state(struct minijail_run_state *state)
2722{
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002723 state->child_pid = -1;
2724
2725 int *fd_pairs[] = {state->pipe_fds, state->stdin_fds, state->stdout_fds,
2726 state->stderr_fds, state->child_sync_pipe_fds};
2727 for (size_t i = 0; i < ARRAY_SIZE(fd_pairs); ++i) {
2728 close_and_reset(&fd_pairs[i][0]);
2729 close_and_reset(&fd_pairs[i][1]);
2730 }
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002731
2732 minijail_free_env(state->child_env);
2733 state->child_env = NULL;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002734}
2735
2736/* Set up stdin/stdout/stderr file descriptors in the child. */
2737static void setup_child_std_fds(struct minijail *j,
2738 struct minijail_run_state *state)
2739{
2740 struct {
2741 const char *name;
2742 int from;
2743 int to;
2744 } fd_map[] = {
2745 {"stdin", state->stdin_fds[0], STDIN_FILENO},
2746 {"stdout", state->stdout_fds[1], STDOUT_FILENO},
2747 {"stderr", state->stderr_fds[1], STDERR_FILENO},
2748 };
2749
2750 for (size_t i = 0; i < ARRAY_SIZE(fd_map); ++i) {
Luis Héctor Cháveza63407a2021-01-03 05:47:00 -08002751 if (fd_map[i].from == -1 || fd_map[i].from == fd_map[i].to)
2752 continue;
2753 if (dup2(fd_map[i].from, fd_map[i].to) == -1)
2754 die("failed to set up %s pipe", fd_map[i].name);
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002755 }
2756
2757 /* Close temporary pipe file descriptors. */
2758 int *std_pipes[] = {state->stdin_fds, state->stdout_fds,
2759 state->stderr_fds};
2760 for (size_t i = 0; i < ARRAY_SIZE(std_pipes); ++i) {
2761 close_and_reset(&std_pipes[i][0]);
2762 close_and_reset(&std_pipes[i][1]);
2763 }
2764
2765 /*
2766 * If any of stdin, stdout, or stderr are TTYs, or setsid flag is
2767 * set, create a new session. This prevents the jailed process from
2768 * using the TIOCSTI ioctl to push characters into the parent process
2769 * terminal's input buffer, therefore escaping the jail.
2770 *
2771 * Since it has just forked, the child will not be a process group
2772 * leader, and this call to setsid() should always succeed.
2773 */
2774 if (j->flags.setsid || isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2775 isatty(STDERR_FILENO)) {
2776 if (setsid() < 0) {
2777 pdie("setsid() failed");
2778 }
2779 }
2780}
2781
2782/*
Dylan Reidacfb8be2017-08-25 12:56:51 -07002783 * Structure that specifies how to start a minijail.
2784 *
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002785 * filename - The program to exec in the child. Required if |exec_in_child| = 1.
2786 * argv - Arguments for the child program. Required if |exec_in_child| = 1.
2787 * envp - Environment for the child program. Available if |exec_in_child| = 1.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002788 * use_preload - If true use LD_PRELOAD.
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002789 * exec_in_child - If true, run |filename|. Otherwise, the child will return to
Dylan Reid0412dcc2017-08-24 11:33:15 -07002790 * the caller.
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002791 * pstdin_fd - Filled with stdin pipe if non-NULL.
2792 * pstdout_fd - Filled with stdout pipe if non-NULL.
2793 * pstderr_fd - Filled with stderr pipe if non-NULL.
2794 * pchild_pid - Filled with the pid of the child process if non-NULL.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002795 */
2796struct minijail_run_config {
2797 const char *filename;
2798 char *const *argv;
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002799 char *const *envp;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002800 int use_preload;
Dylan Reid0412dcc2017-08-24 11:33:15 -07002801 int exec_in_child;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002802 int *pstdin_fd;
2803 int *pstdout_fd;
2804 int *pstderr_fd;
2805 pid_t *pchild_pid;
2806};
2807
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002808static int
2809minijail_run_config_internal(struct minijail *j,
2810 const struct minijail_run_config *config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002811
Will Drewry6ac91122011-10-21 16:38:58 -05002812int API minijail_run(struct minijail *j, const char *filename,
2813 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002814{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002815 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002816 .filename = filename,
2817 .argv = argv,
2818 .envp = NULL,
2819 .use_preload = true,
2820 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002821 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002822 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002823}
2824
2825int API minijail_run_pid(struct minijail *j, const char *filename,
2826 char *const argv[], pid_t *pchild_pid)
2827{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002828 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002829 .filename = filename,
2830 .argv = argv,
2831 .envp = NULL,
2832 .use_preload = true,
2833 .exec_in_child = true,
2834 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002835 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002836 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002837}
2838
2839int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002840 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002841{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002842 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002843 .filename = filename,
2844 .argv = argv,
2845 .envp = NULL,
2846 .use_preload = true,
2847 .exec_in_child = true,
2848 .pstdin_fd = pstdin_fd,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002849 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002850 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002851}
2852
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002853int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002854 char *const argv[], pid_t *pchild_pid,
2855 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002856{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002857 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002858 .filename = filename,
2859 .argv = argv,
2860 .envp = NULL,
2861 .use_preload = true,
2862 .exec_in_child = true,
2863 .pstdin_fd = pstdin_fd,
2864 .pstdout_fd = pstdout_fd,
2865 .pstderr_fd = pstderr_fd,
2866 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002867 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002868 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002869}
2870
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002871int API minijail_run_env_pid_pipes(struct minijail *j, const char *filename,
2872 char *const argv[], char *const envp[],
2873 pid_t *pchild_pid, int *pstdin_fd,
2874 int *pstdout_fd, int *pstderr_fd)
2875{
2876 struct minijail_run_config config = {
2877 .filename = filename,
2878 .argv = argv,
2879 .envp = envp,
2880 .use_preload = true,
2881 .exec_in_child = true,
2882 .pstdin_fd = pstdin_fd,
2883 .pstdout_fd = pstdout_fd,
2884 .pstderr_fd = pstderr_fd,
2885 .pchild_pid = pchild_pid,
2886 };
2887 return minijail_run_config_internal(j, &config);
2888}
2889
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002890int API minijail_run_no_preload(struct minijail *j, const char *filename,
2891 char *const argv[])
2892{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002893 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002894 .filename = filename,
2895 .argv = argv,
2896 .envp = NULL,
2897 .use_preload = false,
2898 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002899 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002900 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002901}
2902
Samuel Tan63187f42015-10-16 13:01:53 -07002903int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002904 const char *filename,
2905 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002906 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002907 int *pstdin_fd,
2908 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002909 int *pstderr_fd)
2910{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002911 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002912 .filename = filename,
2913 .argv = argv,
2914 .envp = NULL,
2915 .use_preload = false,
2916 .exec_in_child = true,
2917 .pstdin_fd = pstdin_fd,
2918 .pstdout_fd = pstdout_fd,
2919 .pstderr_fd = pstderr_fd,
2920 .pchild_pid = pchild_pid,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002921 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002922 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002923}
2924
2925int API minijail_run_env_pid_pipes_no_preload(struct minijail *j,
2926 const char *filename,
2927 char *const argv[],
2928 char *const envp[],
2929 pid_t *pchild_pid, int *pstdin_fd,
2930 int *pstdout_fd, int *pstderr_fd)
2931{
2932 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002933 .filename = filename,
2934 .argv = argv,
2935 .envp = envp,
2936 .use_preload = false,
2937 .exec_in_child = true,
2938 .pstdin_fd = pstdin_fd,
2939 .pstdout_fd = pstdout_fd,
2940 .pstderr_fd = pstderr_fd,
2941 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002942 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002943 return minijail_run_config_internal(j, &config);
Samuel Tan63187f42015-10-16 13:01:53 -07002944}
2945
Dylan Reid0412dcc2017-08-24 11:33:15 -07002946pid_t API minijail_fork(struct minijail *j)
2947{
2948 struct minijail_run_config config = {};
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002949 return minijail_run_config_internal(j, &config);
Dylan Reid0412dcc2017-08-24 11:33:15 -07002950}
2951
Dylan Reid18c49c82017-08-25 14:52:27 -07002952static int minijail_run_internal(struct minijail *j,
2953 const struct minijail_run_config *config,
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002954 struct minijail_run_state *state_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002955{
Dylan Reidce5b55e2016-01-13 11:04:16 -08002956 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002957 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002958 /* We need to remember this across the minijail_preexec() call. */
2959 int pid_namespace = j->flags.pids;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -07002960 /*
2961 * Create an init process if we are entering a pid namespace, unless the
2962 * user has explicitly opted out by calling minijail_run_as_init().
2963 */
2964 int do_init = j->flags.do_init && !j->flags.run_as_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002965 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002966
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002967 if (use_preload) {
Dylan Reid0412dcc2017-08-24 11:33:15 -07002968 if (j->hooks_head != NULL)
2969 die("Minijail hooks are not supported with LD_PRELOAD");
2970 if (!config->exec_in_child)
2971 die("minijail_fork is not supported with LD_PRELOAD");
2972
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002973 /*
2974 * Before we fork(2) and execve(2) the child process, we need
2975 * to open a pipe(2) to send the minijail configuration over.
2976 */
2977 state_out->child_env =
2978 minijail_copy_env(config->envp ? config->envp : environ);
2979 if (!state_out->child_env)
2980 return ENOMEM;
2981 if (setup_preload(j, &state_out->child_env) ||
2982 setup_pipe(&state_out->child_env, state_out->pipe_fds))
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002983 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002984 }
Will Drewryf89aef52011-09-16 16:48:57 -05002985
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002986 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002987 if (j->flags.use_caps && j->caps != 0 &&
2988 !j->flags.set_ambient_caps) {
2989 die("non-empty, non-ambient capabilities are not "
2990 "supported without LD_PRELOAD");
2991 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002992 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002993
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002994 /* Create pipes for stdin/stdout/stderr as requested by caller. */
2995 struct {
2996 bool requested;
2997 int *pipe_fds;
2998 } pipe_fd_req[] = {
2999 {config->pstdin_fd != NULL, state_out->stdin_fds},
3000 {config->pstdout_fd != NULL, state_out->stdout_fds},
3001 {config->pstderr_fd != NULL, state_out->stderr_fds},
3002 };
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07003003
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003004 for (size_t i = 0; i < ARRAY_SIZE(pipe_fd_req); ++i) {
3005 if (pipe_fd_req[i].requested &&
3006 pipe(pipe_fd_req[i].pipe_fds) == -1)
3007 return EFAULT;
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08003008 }
3009
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003010 /*
François Degros664eba72019-11-05 13:18:24 +11003011 * If the parent process needs to configure the child's runtime
3012 * environment after forking, create a pipe(2) to block the child until
3013 * configuration is done.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003014 */
Daniel Erat2d69add2019-04-23 20:58:53 -07003015 if (j->flags.forward_signals || j->flags.pid_file || j->flags.cgroups ||
3016 j->rlimit_count || j->flags.userns) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08003017 sync_child = 1;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003018 if (pipe(state_out->child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003019 return -EFAULT;
3020 }
3021
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08003022 /*
3023 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04003024 *
3025 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
3026 *
3027 * In multithreaded programs, there are a bunch of locks inside libc,
3028 * some of which may be held by other threads at the time that we call
3029 * minijail_run_pid(). If we call fork(), glibc does its level best to
3030 * ensure that we hold all of these locks before it calls clone()
3031 * internally and drop them after clone() returns, but when we call
3032 * sys_clone(2) directly, all that gets bypassed and we end up with a
3033 * child address space where some of libc's important locks are held by
3034 * other threads (which did not get cloned, and hence will never release
3035 * those locks). This is okay so long as we call exec() immediately
3036 * after, but a bunch of seemingly-innocent libc functions like setenv()
3037 * take locks.
3038 *
3039 * Hence, only call sys_clone() if we need to, in order to get at pid
3040 * namespacing. If we follow this path, the child's address space might
3041 * have broken locks; you may only call functions that do not acquire
3042 * any locks.
3043 *
3044 * Unfortunately, fork() acquires every lock it can get its hands on, as
3045 * previously detailed, so this function is highly likely to deadlock
3046 * later on (see "deadlock here") if we're multithreaded.
3047 *
3048 * We might hack around this by having the clone()d child (init of the
3049 * pid namespace) return directly, rather than leaving the clone()d
3050 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08003051 * its fork()ed child return in turn), but that process would be
3052 * crippled with its libc locks potentially broken. We might try
3053 * fork()ing in the parent before we clone() to ensure that we own all
3054 * the locks, but then we have to have the forked child hanging around
3055 * consuming resources (and possibly having file descriptors / shared
3056 * memory regions / etc attached). We'd need to keep the child around to
3057 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04003058 *
3059 * TODO(ellyjones): figure out if the "forked child hanging around"
3060 * problem is fixable or not. It would be nice if we worked in this
3061 * case.
3062 */
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003063 pid_t child_pid;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003064 if (pid_namespace) {
François Degros94619842019-11-08 16:37:55 +11003065 unsigned long clone_flags = CLONE_NEWPID | SIGCHLD;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003066 if (j->flags.userns)
3067 clone_flags |= CLONE_NEWUSER;
François Degros94619842019-11-08 16:37:55 +11003068
3069 child_pid = syscall(SYS_clone, clone_flags, NULL, 0L, 0L, 0L);
3070
3071 if (child_pid < 0) {
3072 if (errno == EPERM)
3073 pdie("clone(CLONE_NEWPID | ...) failed with EPERM; "
3074 "is this process missing CAP_SYS_ADMIN?");
3075 pdie("clone(CLONE_NEWPID | ...) failed");
3076 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003077 } else {
Elly Jones761b7412012-06-13 15:49:52 -04003078 child_pid = fork();
3079
François Degros94619842019-11-08 16:37:55 +11003080 if (child_pid < 0)
3081 pdie("fork failed");
Elly Jonese1749eb2011-10-07 13:54:59 -04003082 }
Will Drewryf89aef52011-09-16 16:48:57 -05003083
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003084 state_out->child_pid = child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04003085 if (child_pid) {
Elly Jonese1749eb2011-10-07 13:54:59 -04003086 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07003087
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04003088 if (j->flags.forward_signals) {
3089 forward_pid = child_pid;
3090 install_signal_handlers();
3091 }
3092
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08003093 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04003094 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08003095
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08003096 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04003097 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08003098
Dylan Reid0f72ef42017-06-06 15:42:49 -07003099 if (j->rlimit_count)
3100 set_rlimits_or_die(j);
3101
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003102 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04003103 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08003104
Mike Frysinger902a4492018-12-27 05:22:56 -05003105 if (j->flags.enter_vfs)
3106 close(j->mountns_fd);
3107
3108 if (j->flags.enter_net)
3109 close(j->netns_fd);
3110
Dylan Reidce5b55e2016-01-13 11:04:16 -08003111 if (sync_child)
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003112 parent_setup_complete(state_out->child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003113
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003114 if (use_preload) {
François Degros664eba72019-11-05 13:18:24 +11003115 /*
3116 * Add SIGPIPE to the signal mask to avoid getting
3117 * killed if the child process finishes or closes its
3118 * end of the pipe prematurely.
3119 *
3120 * TODO(crbug.com/1022170): Use pthread_sigmask instead
3121 * of sigprocmask if Minijail is used in multithreaded
3122 * programs.
3123 */
3124 sigset_t to_block, to_restore;
3125 if (sigemptyset(&to_block) < 0)
3126 pdie("sigemptyset failed");
3127 if (sigaddset(&to_block, SIGPIPE) < 0)
3128 pdie("sigaddset failed");
3129 if (sigprocmask(SIG_BLOCK, &to_block, &to_restore) < 0)
3130 pdie("sigprocmask failed");
3131
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003132 /* Send marshalled minijail. */
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003133 close_and_reset(&state_out->pipe_fds[0]);
3134 ret = minijail_to_fd(j, state_out->pipe_fds[1]);
3135 close_and_reset(&state_out->pipe_fds[1]);
François Degros664eba72019-11-05 13:18:24 +11003136
3137 /* Accept any pending SIGPIPE. */
3138 while (true) {
3139 const struct timespec zero_time = {0, 0};
3140 const int sig = sigtimedwait(&to_block, NULL, &zero_time);
3141 if (sig < 0) {
3142 if (errno != EINTR)
3143 break;
3144 } else {
3145 if (sig != SIGPIPE)
3146 die("unexpected signal %d", sig);
3147 }
3148 }
3149
3150 /* Restore the signal mask to its original state. */
3151 if (sigprocmask(SIG_SETMASK, &to_restore, NULL) < 0)
3152 pdie("sigprocmask failed");
3153
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003154 if (ret) {
François Degros664eba72019-11-05 13:18:24 +11003155 warn("failed to send marshalled minijail: %s",
3156 strerror(-ret));
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003157 kill(j->initpid, SIGKILL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003158 }
Elly Jonese1749eb2011-10-07 13:54:59 -04003159 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07003160
Elly Jonese1749eb2011-10-07 13:54:59 -04003161 return 0;
3162 }
Ben Chan541c7e52011-08-26 14:55:53 -07003163
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003164 /* Child process. */
Peter Qiu2860c462015-12-16 15:13:06 -08003165 if (j->flags.reset_signal_mask) {
3166 sigset_t signal_mask;
3167 if (sigemptyset(&signal_mask) != 0)
3168 pdie("sigemptyset failed");
3169 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
3170 pdie("sigprocmask failed");
3171 }
3172
Luis Hector Chaveza27118a2018-04-04 08:18:01 -07003173 if (j->flags.reset_signal_handlers) {
3174 int signum;
3175 for (signum = 0; signum <= SIGRTMAX; signum++) {
3176 /*
3177 * Ignore EINVAL since some signal numbers in the range
3178 * might not be valid.
3179 */
3180 if (signal(signum, SIG_DFL) == SIG_ERR &&
3181 errno != EINVAL) {
3182 pdie("failed to reset signal %d disposition",
3183 signum);
3184 }
3185 }
3186 }
3187
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003188 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07003189 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003190 int inheritable_fds[kMaxInheritableFdsSize];
3191 size_t size = 0;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003192
3193 int *pipe_fds[] = {
3194 state_out->pipe_fds, state_out->child_sync_pipe_fds,
3195 state_out->stdin_fds, state_out->stdout_fds,
3196 state_out->stderr_fds,
3197 };
3198
3199 for (size_t i = 0; i < ARRAY_SIZE(pipe_fds); ++i) {
3200 if (pipe_fds[i][0] != -1) {
3201 inheritable_fds[size++] = pipe_fds[i][0];
3202 }
3203 if (pipe_fds[i][1] != -1) {
3204 inheritable_fds[size++] = pipe_fds[i][1];
3205 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003206 }
Jorge Lucangeli Obes2337f802019-07-18 14:46:03 -04003207
Jorge Lucangeli Obescf3bbea2019-07-24 09:06:40 -04003208 /*
3209 * Preserve namespace file descriptors over the close_open_fds()
3210 * call. These are closed in minijail_enter() so they won't leak
3211 * into the child process.
3212 */
Jorge Lucangeli Obes2337f802019-07-18 14:46:03 -04003213 if (j->flags.enter_vfs)
3214 minijail_preserve_fd(j, j->mountns_fd, j->mountns_fd);
3215 if (j->flags.enter_net)
3216 minijail_preserve_fd(j, j->netns_fd, j->netns_fd);
3217
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003218 for (size_t i = 0; i < j->preserved_fd_count; i++) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07003219 /*
3220 * Preserve all parent_fds. They will be dup2(2)-ed in
3221 * the child later.
3222 */
3223 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
3224 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003225
3226 if (close_open_fds(inheritable_fds, size) < 0)
3227 die("failed to close open file descriptors");
3228 }
3229
Luis Hector Chavez1617f632017-08-01 18:32:30 -07003230 if (redirect_fds(j))
3231 die("failed to set up fd redirections");
3232
Dylan Reidce5b55e2016-01-13 11:04:16 -08003233 if (sync_child)
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003234 wait_for_parent_setup(state_out->child_sync_pipe_fds);
Dylan Reidce5b55e2016-01-13 11:04:16 -08003235
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003236 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08003237 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003238
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003239 setup_child_std_fds(j, state_out);
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05003240
Dylan Reid791f5772015-09-14 20:02:42 -07003241 /* If running an init program, let it decide when/how to mount /proc. */
3242 if (pid_namespace && !do_init)
3243 j->flags.remount_proc_ro = 0;
3244
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003245 if (use_preload) {
3246 /* Strip out flags that cannot be inherited across execve(2). */
3247 minijail_preexec(j);
3248 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003249 /*
3250 * If not using LD_PRELOAD, do all jailing before execve(2).
3251 * Note that PID namespaces can only be entered on fork(2),
3252 * so that flag is still cleared.
3253 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003254 j->flags.pids = 0;
3255 }
Dylan Reid0412dcc2017-08-24 11:33:15 -07003256
3257 /*
3258 * Jail this process.
3259 * If forking, return.
3260 * If not, execve(2) the target.
3261 */
Elly Jonese1749eb2011-10-07 13:54:59 -04003262 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003263
Dylan Reid0412dcc2017-08-24 11:33:15 -07003264 if (config->exec_in_child && pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05003265 /*
3266 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003267 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003268 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003269 * a child to actually run the program. If |do_init == 0|, we
3270 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04003271 *
3272 * If we're multithreaded, we'll probably deadlock here. See
3273 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04003274 */
3275 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003276 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04003277 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003278 } else if (child_pid > 0) {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003279 minijail_free_run_state(state_out);
3280
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04003281 /*
3282 * Best effort. Don't bother checking the return value.
3283 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003284 prctl(PR_SET_NAME, "minijail-init");
3285 init(child_pid); /* Never returns. */
3286 }
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003287 state_out->child_pid = child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04003288 }
Elly Jonescd7a9042011-07-22 13:56:51 -04003289
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07003290 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
3291
Dylan Reid0412dcc2017-08-24 11:33:15 -07003292 if (!config->exec_in_child)
3293 return 0;
3294
Elly Jonesdd3e8512012-01-23 15:13:38 -05003295 /*
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003296 * We're going to execve(), so make sure any remaining resources are
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003297 * freed. Exceptions are:
3298 * 1. The child environment. No need to worry about freeing it since
3299 * execve reinitializes the heap anyways.
3300 * 2. The read side of the LD_PRELOAD pipe, which we need to hand down
3301 * into the target in which the preloaded code will read from it and
3302 * then close it.
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003303 */
3304 state_out->pipe_fds[0] = -1;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003305 char *const *child_env = state_out->child_env;
3306 state_out->child_env = NULL;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003307 minijail_free_run_state(state_out);
3308
3309 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003310 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04003311 * calling process
3312 * -> execve()-ing process
3313 * If we are:
3314 * calling process
3315 * -> init()-ing process
3316 * -> execve()-ing process
3317 */
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003318 if (!child_env)
3319 child_env = config->envp ? config->envp : environ;
François Degros08b10f72019-10-09 12:44:05 +11003320 execve(config->filename, config->argv, child_env);
3321
3322 ret = (errno == ENOENT ? MINIJAIL_ERR_NO_COMMAND : MINIJAIL_ERR_NO_ACCESS);
3323 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003324 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04003325}
3326
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003327static int
3328minijail_run_config_internal(struct minijail *j,
3329 const struct minijail_run_config *config)
3330{
3331 struct minijail_run_state state = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003332 .child_pid = -1,
3333 .pipe_fds = {-1, -1},
3334 .stdin_fds = {-1, -1},
3335 .stdout_fds = {-1, -1},
3336 .stderr_fds = {-1, -1},
3337 .child_sync_pipe_fds = {-1, -1},
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003338 .child_env = NULL,
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003339 };
3340 int ret = minijail_run_internal(j, config, &state);
3341
3342 if (ret == 0) {
3343 if (config->pchild_pid)
3344 *config->pchild_pid = state.child_pid;
3345
3346 /* Grab stdin/stdout/stderr descriptors requested by caller. */
3347 struct {
3348 int *pfd;
3349 int *psrc;
3350 } fd_map[] = {
3351 {config->pstdin_fd, &state.stdin_fds[1]},
3352 {config->pstdout_fd, &state.stdout_fds[0]},
3353 {config->pstderr_fd, &state.stderr_fds[0]},
3354 };
3355
3356 for (size_t i = 0; i < ARRAY_SIZE(fd_map); ++i) {
3357 if (fd_map[i].pfd) {
3358 *fd_map[i].pfd = *fd_map[i].psrc;
3359 *fd_map[i].psrc = -1;
3360 }
3361 }
3362
3363 if (!config->exec_in_child)
3364 ret = state.child_pid;
3365 }
3366
3367 minijail_free_run_state(&state);
3368
3369 return ret;
3370}
3371
Victor Hsieh14036062021-04-30 15:40:36 -07003372static int minijail_wait_internal(struct minijail *j, int expected_signal)
Elly Jonese1749eb2011-10-07 13:54:59 -04003373{
François Degros08b10f72019-10-09 12:44:05 +11003374 if (j->initpid <= 0)
3375 return -ECHILD;
3376
Elly Jonese1749eb2011-10-07 13:54:59 -04003377 int st;
François Degros627deba2019-10-01 12:48:25 +10003378 while (true) {
3379 const int ret = waitpid(j->initpid, &st, 0);
3380 if (ret >= 0)
3381 break;
3382 if (errno != EINTR)
3383 return -errno;
3384 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003385
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003386 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003387 int error_status = st;
3388 if (WIFSIGNALED(st)) {
3389 int signum = WTERMSIG(st);
Victor Hsieh14036062021-04-30 15:40:36 -07003390 if (signum != expected_signal) {
3391 warn("child process %d received signal %d",
3392 j->initpid, signum);
3393 }
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003394 /*
3395 * We return MINIJAIL_ERR_JAIL if the process received
3396 * SIGSYS, which happens when a syscall is blocked by
3397 * seccomp filters.
3398 * If not, we do what bash(1) does:
3399 * $? = 128 + signum
3400 */
3401 if (signum == SIGSYS) {
3402 error_status = MINIJAIL_ERR_JAIL;
3403 } else {
François Degros08b10f72019-10-09 12:44:05 +11003404 error_status = MINIJAIL_ERR_SIG_BASE + signum;
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003405 }
3406 }
3407 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003408 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003409
3410 int exit_status = WEXITSTATUS(st);
3411 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07003412 info("child process %d exited with status %d",
3413 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003414
3415 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04003416}
3417
Victor Hsieh14036062021-04-30 15:40:36 -07003418int API minijail_kill(struct minijail *j)
3419{
3420 if (j->initpid <= 0)
3421 return -ECHILD;
3422
3423 if (kill(j->initpid, SIGTERM))
3424 return -errno;
3425
3426 return minijail_wait_internal(j, SIGTERM);
3427}
3428
3429int API minijail_wait(struct minijail *j)
3430{
3431 return minijail_wait_internal(j, 0);
3432}
3433
Will Drewry6ac91122011-10-21 16:38:58 -05003434void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003435{
Dylan Reid605ce7f2016-01-19 19:21:00 -08003436 size_t i;
3437
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07003438 if (j->filter_prog) {
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08003439 free(j->filter_prog->filter);
3440 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04003441 }
Mike Frysingerac08a682017-10-10 02:04:50 -04003442 free_mounts_list(j);
Nicole Anderson-Au835f7172021-01-13 21:18:13 +00003443 free_remounts_list(j);
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07003444 while (j->hooks_head) {
3445 struct hook *c = j->hooks_head;
3446 j->hooks_head = c->next;
3447 free(c);
3448 }
3449 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04003450 if (j->user)
3451 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08003452 if (j->suppl_gid_list)
3453 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05003454 if (j->chrootdir)
3455 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04003456 if (j->pid_file_path)
3457 free(j->pid_file_path);
3458 if (j->uidmap)
3459 free(j->uidmap);
3460 if (j->gidmap)
3461 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04003462 if (j->hostname)
3463 free(j->hostname);
Luis Hector Chavez9acba452018-10-11 10:13:25 -07003464 if (j->preload_path)
3465 free(j->preload_path);
Andrew Brestickereac28942015-11-11 16:04:46 -08003466 if (j->alt_syscall_table)
3467 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08003468 for (i = 0; i < j->cgroup_count; ++i)
3469 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04003470 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003471}
Luis Hector Chavez114a9302017-09-05 20:36:58 -07003472
3473void API minijail_log_to_fd(int fd, int min_priority)
3474{
3475 init_logging(LOG_TO_FD, fd, min_priority);
3476}