blob: b1ec28bcda3f9673ab73ce9808cef348b4dc5239 [file] [log] [blame]
Jorge Lucangeli Obesd613ab22015-03-03 14:22:50 -08001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04002 * Use of this source code is governed by a BSD-style license that can be
Will Drewry32ac9f52011-08-18 21:36:27 -05003 * found in the LICENSE file.
4 */
Elly Jonescd7a9042011-07-22 13:56:51 -04005
6#define _BSD_SOURCE
Arthur Gautier7a569072016-04-23 17:25:20 +00007#define _DEFAULT_SOURCE
Elly Jonescd7a9042011-07-22 13:56:51 -04008#define _GNU_SOURCE
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07009
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080010#include <asm/unistd.h>
Luis Hector Chavez43ff0802016-10-07 12:21:07 -070011#include <dirent.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040012#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070013#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040014#include <grp.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040015#include <linux/capability.h>
Luis Hector Chavezc3e17722018-10-16 20:43:12 -070016#include <linux/filter.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040017#include <sched.h>
18#include <signal.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070019#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080020#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040024#include <sys/capability.h>
25#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050026#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040027#include <sys/prctl.h>
Dylan Reid0f72ef42017-06-06 15:42:49 -070028#include <sys/resource.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070029#include <sys/stat.h>
Mike Frysinger33ffef32017-01-13 19:53:19 -050030#include <sys/sysmacros.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070031#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080032#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040033#include <sys/wait.h>
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -070034#include <syscall.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040035#include <unistd.h>
36
37#include "libminijail.h"
38#include "libminijail-private.h"
39
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070040#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080041#include "syscall_filter.h"
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040042#include "syscall_wrapper.h"
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040043#include "system.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070044#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080045
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070046/* Until these are reliably available in linux/prctl.h. */
Andrew Brestickereac28942015-11-11 16:04:46 -080047#ifndef PR_ALT_SYSCALL
48# define PR_ALT_SYSCALL 0x43724f53
49#endif
50
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040051/* Seccomp filter related flags. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080052#ifndef PR_SET_NO_NEW_PRIVS
53# define PR_SET_NO_NEW_PRIVS 38
54#endif
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040055
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080056#ifndef SECCOMP_MODE_FILTER
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040057#define SECCOMP_MODE_FILTER 2 /* Uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050058#endif
59
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040060#ifndef SECCOMP_SET_MODE_STRICT
61# define SECCOMP_SET_MODE_STRICT 0
62#endif
63#ifndef SECCOMP_SET_MODE_FILTER
64# define SECCOMP_SET_MODE_FILTER 1
65#endif
66
67#ifndef SECCOMP_FILTER_FLAG_TSYNC
68# define SECCOMP_FILTER_FLAG_TSYNC 1
69#endif
70/* End seccomp filter related flags. */
71
Dylan Reid4cbc2a52016-06-17 19:06:07 -070072/* New cgroup namespace might not be in linux-headers yet. */
73#ifndef CLONE_NEWCGROUP
74# define CLONE_NEWCGROUP 0x02000000
75#endif
76
Dylan Reid605ce7f2016-01-19 19:21:00 -080077#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
78
Dylan Reid0f72ef42017-06-06 15:42:49 -070079#define MAX_RLIMITS 32 /* Currently there are 15 supported by Linux. */
80
Stephen Barber0d1cbf62017-10-16 22:19:38 -070081#define MAX_PRESERVED_FDS 32U
Luis Hector Chavez1617f632017-08-01 18:32:30 -070082
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -080083/* Keyctl commands. */
84#define KEYCTL_JOIN_SESSION_KEYRING 1
85
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -070086/*
87 * The userspace equivalent of MNT_USER_SETTABLE_MASK, which is the mask of all
88 * flags that can be modified by MS_REMOUNT.
89 */
90#define MS_USER_SETTABLE_MASK \
91 (MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME | MS_NODIRATIME | \
92 MS_RELATIME | MS_RDONLY)
93
Dylan Reid0f72ef42017-06-06 15:42:49 -070094struct minijail_rlimit {
95 int type;
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -080096 rlim_t cur;
97 rlim_t max;
Dylan Reid0f72ef42017-06-06 15:42:49 -070098};
99
Dylan Reid648b2202015-10-23 00:50:00 -0700100struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400101 char *src;
102 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700103 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700104 char *data;
105 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -0700106 unsigned long flags;
107 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400108};
109
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700110struct hook {
111 minijail_hook_t hook;
112 void *payload;
113 minijail_hook_event_t event;
114 struct hook *next;
115};
116
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700117struct preserved_fd {
118 int parent_fd;
119 int child_fd;
120};
121
Will Drewryf89aef52011-09-16 16:48:57 -0500122struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700123 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700124 * WARNING: if you add a flag here you need to make sure it's
125 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700126 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400127 struct {
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700128 int uid : 1;
129 int gid : 1;
Lutz Justen13807cb2017-01-03 17:11:55 +0100130 int inherit_suppl_gids : 1;
131 int set_suppl_gids : 1;
132 int keep_suppl_gids : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700133 int use_caps : 1;
134 int capbset_drop : 1;
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400135 int set_ambient_caps : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700136 int vfs : 1;
137 int enter_vfs : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700138 int pids : 1;
139 int ipc : 1;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400140 int uts : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700141 int net : 1;
142 int enter_net : 1;
143 int ns_cgroups : 1;
144 int userns : 1;
145 int disable_setgroups : 1;
146 int seccomp : 1;
147 int remount_proc_ro : 1;
148 int no_new_privs : 1;
149 int seccomp_filter : 1;
150 int seccomp_filter_tsync : 1;
151 int seccomp_filter_logging : 1;
152 int chroot : 1;
153 int pivot_root : 1;
Mike Frysinger33ffef32017-01-13 19:53:19 -0500154 int mount_dev : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700155 int mount_tmp : 1;
156 int do_init : 1;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700157 int run_as_init : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700158 int pid_file : 1;
159 int cgroups : 1;
160 int alt_syscall : 1;
161 int reset_signal_mask : 1;
Luis Hector Chaveza27118a2018-04-04 08:18:01 -0700162 int reset_signal_handlers : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700163 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800164 int new_session_keyring : 1;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400165 int forward_signals : 1;
Xiyuan Xia9b41e652019-05-23 11:03:04 -0700166 int setsid : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400167 } flags;
168 uid_t uid;
169 gid_t gid;
170 gid_t usergid;
171 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800172 size_t suppl_gid_count;
173 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400174 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800175 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400176 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700177 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700178 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400179 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800180 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800181 char *uidmap;
182 char *gidmap;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400183 char *hostname;
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700184 char *preload_path;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800185 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800186 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800187 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700188 struct mountpoint *mounts_head;
189 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800190 size_t mounts_count;
Mike Frysinger785b1c32018-02-23 15:47:24 -0500191 unsigned long remount_mode;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100192 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800193 char *cgroups[MAX_CGROUPS];
194 size_t cgroup_count;
Dylan Reid0f72ef42017-06-06 15:42:49 -0700195 struct minijail_rlimit rlimits[MAX_RLIMITS];
196 size_t rlimit_count;
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700197 uint64_t securebits_skip_mask;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700198 struct hook *hooks_head;
199 struct hook *hooks_tail;
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700200 struct preserved_fd preserved_fds[MAX_PRESERVED_FDS];
201 size_t preserved_fd_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500202};
203
Luis Hector Chavez64730af2017-09-13 13:18:59 -0700204static void run_hooks_or_die(const struct minijail *j,
205 minijail_hook_event_t event);
206
Mike Frysingerac08a682017-10-10 02:04:50 -0400207static void free_mounts_list(struct minijail *j)
208{
209 while (j->mounts_head) {
210 struct mountpoint *m = j->mounts_head;
211 j->mounts_head = j->mounts_head->next;
212 free(m->data);
213 free(m->type);
214 free(m->dest);
215 free(m->src);
216 free(m);
217 }
218 // No need to clear mounts_head as we know it's NULL after the loop.
219 j->mounts_tail = NULL;
220}
221
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700222/*
François Degros664eba72019-11-05 13:18:24 +1100223 * Writes exactly n bytes from buf to file descriptor fd.
224 * Returns 0 on success or a negative error code on error.
225 */
226static int write_exactly(int fd, const void *buf, size_t n)
227{
228 const char *p = buf;
229 while (n > 0) {
230 const ssize_t written = write(fd, p, n);
231 if (written < 0) {
232 if (errno == EINTR)
233 continue;
234
235 return -errno;
236 }
237
238 p += written;
239 n -= written;
240 }
241
242 return 0;
243}
244
Mattias Nissler6123e5a2020-02-11 13:38:03 +0100245/* Closes *pfd and sets it to -1. */
246static void close_and_reset(int *pfd)
247{
248 if (*pfd != -1)
249 close(*pfd);
250 *pfd = -1;
251}
252
François Degros664eba72019-11-05 13:18:24 +1100253/*
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700254 * Strip out flags meant for the parent.
255 * We keep things that are not inherited across execve(2) (e.g. capabilities),
256 * or are easier to set after execve(2) (e.g. seccomp filters).
257 */
258void minijail_preenter(struct minijail *j)
259{
260 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700261 j->flags.enter_vfs = 0;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700262 j->flags.ns_cgroups = 0;
263 j->flags.net = 0;
264 j->flags.uts = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700265 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700266 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800267 j->flags.do_init = 0;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700268 j->flags.run_as_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800269 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800270 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400271 j->flags.forward_signals = 0;
Xiyuan Xia9b41e652019-05-23 11:03:04 -0700272 j->flags.setsid = 0;
Mike Frysinger785b1c32018-02-23 15:47:24 -0500273 j->remount_mode = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700274}
275
276/*
277 * Strip out flags meant for the child.
278 * We keep things that are inherited across execve(2).
279 */
280void minijail_preexec(struct minijail *j)
281{
282 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700283 int enter_vfs = j->flags.enter_vfs;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700284 int ns_cgroups = j->flags.ns_cgroups;
285 int net = j->flags.net;
286 int uts = j->flags.uts;
Dylan Reid791f5772015-09-14 20:02:42 -0700287 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800288 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700289 if (j->user)
290 free(j->user);
291 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800292 if (j->suppl_gid_list)
293 free(j->suppl_gid_list);
294 j->suppl_gid_list = NULL;
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700295 if (j->preload_path)
296 free(j->preload_path);
297 j->preload_path = NULL;
Mike Frysingerac08a682017-10-10 02:04:50 -0400298 free_mounts_list(j);
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700299 memset(&j->flags, 0, sizeof(j->flags));
300 /* Now restore anything we meant to keep. */
301 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700302 j->flags.enter_vfs = enter_vfs;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700303 j->flags.ns_cgroups = ns_cgroups;
304 j->flags.net = net;
305 j->flags.uts = uts;
Dylan Reid791f5772015-09-14 20:02:42 -0700306 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800307 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700308 /* Note, |pids| will already have been used before this call. */
309}
310
311/* Minijail API. */
312
Will Drewry6ac91122011-10-21 16:38:58 -0500313struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400314{
Mike Frysinger785b1c32018-02-23 15:47:24 -0500315 struct minijail *j = calloc(1, sizeof(struct minijail));
Mike Frysinger1036cd82020-08-28 00:15:59 -0400316 if (j) {
317 j->remount_mode = MS_PRIVATE;
318 }
Mike Frysinger785b1c32018-02-23 15:47:24 -0500319 return j;
Elly Jonescd7a9042011-07-22 13:56:51 -0400320}
321
Will Drewry6ac91122011-10-21 16:38:58 -0500322void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400323{
324 if (uid == 0)
325 die("useless change to uid 0");
326 j->uid = uid;
327 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400328}
329
Will Drewry6ac91122011-10-21 16:38:58 -0500330void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400331{
332 if (gid == 0)
333 die("useless change to gid 0");
334 j->gid = gid;
335 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400336}
337
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800338void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
339 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800340{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800341 size_t i;
342
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500343 if (j->flags.inherit_suppl_gids)
344 die("cannot inherit *and* set supplementary groups");
345 if (j->flags.keep_suppl_gids)
346 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800347
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800348 if (size == 0) {
349 /* Clear supplementary groups. */
350 j->suppl_gid_list = NULL;
351 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100352 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800353 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800354 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800355
356 /* Copy the gid_t array. */
357 j->suppl_gid_list = calloc(size, sizeof(gid_t));
358 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800359 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800360 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800361 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800362 j->suppl_gid_list[i] = list[i];
363 }
364 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100365 j->flags.set_suppl_gids = 1;
366}
367
368void API minijail_keep_supplementary_gids(struct minijail *j) {
369 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800370}
371
Will Drewry6ac91122011-10-21 16:38:58 -0500372int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400373{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700374 uid_t uid;
375 gid_t gid;
376 int rc = lookup_user(user, &uid, &gid);
377 if (rc)
378 return rc;
379 minijail_change_uid(j, uid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400380 j->user = strdup(user);
381 if (!j->user)
382 return -ENOMEM;
Luis Hector Chavez71323552017-09-05 09:17:22 -0700383 j->usergid = gid;
Elly Jonese1749eb2011-10-07 13:54:59 -0400384 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400385}
386
Will Drewry6ac91122011-10-21 16:38:58 -0500387int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400388{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700389 gid_t gid;
390 int rc = lookup_group(group, &gid);
391 if (rc)
392 return rc;
393 minijail_change_gid(j, gid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400394 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400395}
396
Will Drewry6ac91122011-10-21 16:38:58 -0500397void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400398{
399 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400400}
401
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700402void API minijail_no_new_privs(struct minijail *j)
403{
404 j->flags.no_new_privs = 1;
405}
406
Will Drewry6ac91122011-10-21 16:38:58 -0500407void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400408{
409 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500410}
411
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400412void API minijail_set_seccomp_filter_tsync(struct minijail *j)
413{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400414 if (j->filter_len > 0 && j->filter_prog != NULL) {
415 die("minijail_set_seccomp_filter_tsync() must be called "
416 "before minijail_parse_seccomp_filters()");
417 }
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400418
419 if (j->flags.seccomp_filter_logging && !seccomp_ret_log_available()) {
420 /*
421 * If SECCOMP_RET_LOG is not available, we don't want to use
422 * SECCOMP_RET_TRAP to both kill the entire process and report
423 * failing syscalls, since it will be brittle. Just bail.
424 */
Mike Frysinger52f6ada2019-06-26 16:59:36 -0400425 die("SECCOMP_RET_LOG not available, cannot use logging with "
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400426 "thread sync at the same time");
427 }
428
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400429 j->flags.seccomp_filter_tsync = 1;
430}
431
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700432void API minijail_log_seccomp_filter_failures(struct minijail *j)
433{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400434 if (j->filter_len > 0 && j->filter_prog != NULL) {
435 die("minijail_log_seccomp_filter_failures() must be called "
436 "before minijail_parse_seccomp_filters()");
437 }
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400438
439 if (j->flags.seccomp_filter_tsync && !seccomp_ret_log_available()) {
440 /*
441 * If SECCOMP_RET_LOG is not available, we don't want to use
442 * SECCOMP_RET_TRAP to both kill the entire process and report
443 * failing syscalls, since it will be brittle. Just bail.
444 */
Mike Frysinger52f6ada2019-06-26 16:59:36 -0400445 die("SECCOMP_RET_LOG not available, cannot use thread sync with "
446 "logging at the same time");
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400447 }
448
449 if (debug_logging_allowed()) {
450 j->flags.seccomp_filter_logging = 1;
451 } else {
452 warn("non-debug build: ignoring request to enable seccomp "
453 "logging");
454 }
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700455}
456
Will Drewry6ac91122011-10-21 16:38:58 -0500457void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400458{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800459 /*
460 * 'minijail_use_caps' configures a runtime-capabilities-only
461 * environment, including a bounding set matching the thread's runtime
462 * (permitted|inheritable|effective) sets.
463 * Therefore, it will override any existing bounding set configurations
464 * since the latter would allow gaining extra runtime capabilities from
465 * file capabilities.
466 */
467 if (j->flags.capbset_drop) {
468 warn("overriding bounding set configuration");
469 j->cap_bset = 0;
470 j->flags.capbset_drop = 0;
471 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400472 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800473 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400474}
475
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800476void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
477{
478 if (j->flags.use_caps) {
479 /*
480 * 'minijail_use_caps' will have already configured a capability
481 * bounding set matching the (permitted|inheritable|effective)
482 * sets. Abort if the user tries to configure a separate
483 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
484 * are mutually exclusive.
485 */
486 die("runtime capabilities already configured, can't drop "
487 "bounding set separately");
488 }
489 j->cap_bset = capmask;
490 j->flags.capbset_drop = 1;
491}
492
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400493void API minijail_set_ambient_caps(struct minijail *j)
494{
495 j->flags.set_ambient_caps = 1;
496}
497
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800498void API minijail_reset_signal_mask(struct minijail *j)
499{
Peter Qiu2860c462015-12-16 15:13:06 -0800500 j->flags.reset_signal_mask = 1;
501}
502
Luis Hector Chaveza27118a2018-04-04 08:18:01 -0700503void API minijail_reset_signal_handlers(struct minijail *j)
504{
505 j->flags.reset_signal_handlers = 1;
506}
507
Will Drewry6ac91122011-10-21 16:38:58 -0500508void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400509{
510 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400511}
512
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700513void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
514{
Mike Frysinger902a4492018-12-27 05:22:56 -0500515 /* Note: Do not use O_CLOEXEC here. We'll close it after we use it. */
516 int ns_fd = open(ns_path, O_RDONLY);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700517 if (ns_fd < 0) {
518 pdie("failed to open namespace '%s'", ns_path);
519 }
520 j->mountns_fd = ns_fd;
521 j->flags.enter_vfs = 1;
522}
523
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800524void API minijail_new_session_keyring(struct minijail *j)
525{
526 j->flags.new_session_keyring = 1;
527}
528
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700529void API minijail_skip_setting_securebits(struct minijail *j,
530 uint64_t securebits_skip_mask)
531{
532 j->securebits_skip_mask = securebits_skip_mask;
533}
534
Mike Frysinger785b1c32018-02-23 15:47:24 -0500535void API minijail_remount_mode(struct minijail *j, unsigned long mode)
536{
537 j->remount_mode = mode;
538}
539
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800540void API minijail_skip_remount_private(struct minijail *j)
541{
Mike Frysinger785b1c32018-02-23 15:47:24 -0500542 j->remount_mode = 0;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800543}
544
Will Drewry6ac91122011-10-21 16:38:58 -0500545void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400546{
Elly Jonese58176c2012-01-23 11:46:17 -0500547 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700548 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400549 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800550 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400551}
552
Jorge Lucangeli Obes2fa96d12019-02-05 10:51:57 -0500553void API minijail_namespace_pids_rw_proc(struct minijail *j)
554{
555 j->flags.vfs = 1;
556 j->flags.pids = 1;
557 j->flags.do_init = 1;
558}
559
Dylan Reidf7942472015-11-18 17:55:26 -0800560void API minijail_namespace_ipc(struct minijail *j)
561{
562 j->flags.ipc = 1;
563}
564
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400565void API minijail_namespace_uts(struct minijail *j)
566{
567 j->flags.uts = 1;
568}
569
570int API minijail_namespace_set_hostname(struct minijail *j, const char *name)
571{
572 if (j->hostname)
573 return -EINVAL;
574 minijail_namespace_uts(j);
575 j->hostname = strdup(name);
576 if (!j->hostname)
577 return -ENOMEM;
578 return 0;
579}
580
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400581void API minijail_namespace_net(struct minijail *j)
582{
583 j->flags.net = 1;
584}
585
Dylan Reid1102f5a2015-09-15 11:52:20 -0700586void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
587{
Mike Frysinger902a4492018-12-27 05:22:56 -0500588 /* Note: Do not use O_CLOEXEC here. We'll close it after we use it. */
589 int ns_fd = open(ns_path, O_RDONLY);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700590 if (ns_fd < 0) {
591 pdie("failed to open namespace '%s'", ns_path);
592 }
593 j->netns_fd = ns_fd;
594 j->flags.enter_net = 1;
595}
596
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700597void API minijail_namespace_cgroups(struct minijail *j)
598{
599 j->flags.ns_cgroups = 1;
600}
601
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700602void API minijail_close_open_fds(struct minijail *j)
603{
604 j->flags.close_open_fds = 1;
605}
606
Dylan Reid791f5772015-09-14 20:02:42 -0700607void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400608{
609 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700610 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400611}
612
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800613void API minijail_namespace_user(struct minijail *j)
614{
615 j->flags.userns = 1;
616}
617
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400618void API minijail_namespace_user_disable_setgroups(struct minijail *j)
619{
620 j->flags.disable_setgroups = 1;
621}
622
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800623int API minijail_uidmap(struct minijail *j, const char *uidmap)
624{
625 j->uidmap = strdup(uidmap);
626 if (!j->uidmap)
627 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800628 char *ch;
629 for (ch = j->uidmap; *ch; ch++) {
630 if (*ch == ',')
631 *ch = '\n';
632 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800633 return 0;
634}
635
636int API minijail_gidmap(struct minijail *j, const char *gidmap)
637{
638 j->gidmap = strdup(gidmap);
639 if (!j->gidmap)
640 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800641 char *ch;
642 for (ch = j->gidmap; *ch; ch++) {
643 if (*ch == ',')
644 *ch = '\n';
645 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800646 return 0;
647}
648
Will Drewry6ac91122011-10-21 16:38:58 -0500649void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400650{
Lutz Justen13807cb2017-01-03 17:11:55 +0100651 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400652}
653
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800654void API minijail_run_as_init(struct minijail *j)
655{
656 /*
657 * Since the jailed program will become 'init' in the new PID namespace,
658 * Minijail does not need to fork an 'init' process.
659 */
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700660 j->flags.run_as_init = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800661}
662
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700663int API minijail_enter_chroot(struct minijail *j, const char *dir)
664{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400665 if (j->chrootdir)
666 return -EINVAL;
667 j->chrootdir = strdup(dir);
668 if (!j->chrootdir)
669 return -ENOMEM;
670 j->flags.chroot = 1;
671 return 0;
672}
673
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800674int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
675{
676 if (j->chrootdir)
677 return -EINVAL;
678 j->chrootdir = strdup(dir);
679 if (!j->chrootdir)
680 return -ENOMEM;
681 j->flags.pivot_root = 1;
682 return 0;
683}
684
Dylan Reida14e08d2015-10-22 21:05:29 -0700685char API *minijail_get_original_path(struct minijail *j,
686 const char *path_inside_chroot)
687{
Dylan Reid648b2202015-10-23 00:50:00 -0700688 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700689
Dylan Reid648b2202015-10-23 00:50:00 -0700690 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700691 while (b) {
692 /*
693 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700694 * mount, then the original path is exactly the source of
695 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700696 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700697 * mount source = /some/path/exe, mount dest =
698 * /chroot/path/exe Then when getting the original path of
699 * "/chroot/path/exe", the source of that mount,
700 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700701 */
702 if (!strcmp(b->dest, path_inside_chroot))
703 return strdup(b->src);
704
705 /*
706 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700707 * mount, take the suffix of the chroot path relative to the
708 * mount destination path, and append it to the mount source
709 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700710 */
711 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
712 const char *relative_path =
713 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400714 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700715 }
716 b = b->next;
717 }
718
719 /* If there is a chroot path, append |path_inside_chroot| to that. */
720 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400721 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700722
723 /* No chroot, so the path outside is the same as it is inside. */
724 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700725}
726
Martin Pelikánab9eb442017-01-25 11:53:58 +1100727size_t minijail_get_tmpfs_size(const struct minijail *j)
728{
729 return j->tmpfs_size;
730}
731
Mike Frysinger33ffef32017-01-13 19:53:19 -0500732void API minijail_mount_dev(struct minijail *j)
733{
734 j->flags.mount_dev = 1;
735}
736
Lee Campbell11af0622014-05-22 12:36:04 -0700737void API minijail_mount_tmp(struct minijail *j)
738{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100739 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
740}
741
742void API minijail_mount_tmp_size(struct minijail *j, size_t size)
743{
744 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700745 j->flags.mount_tmp = 1;
746}
747
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800748int API minijail_write_pid_file(struct minijail *j, const char *path)
749{
750 j->pid_file_path = strdup(path);
751 if (!j->pid_file_path)
752 return -ENOMEM;
753 j->flags.pid_file = 1;
754 return 0;
755}
756
Dylan Reid605ce7f2016-01-19 19:21:00 -0800757int API minijail_add_to_cgroup(struct minijail *j, const char *path)
758{
759 if (j->cgroup_count >= MAX_CGROUPS)
760 return -ENOMEM;
761 j->cgroups[j->cgroup_count] = strdup(path);
762 if (!j->cgroups[j->cgroup_count])
763 return -ENOMEM;
764 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800765 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800766 return 0;
767}
768
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800769int API minijail_rlimit(struct minijail *j, int type, rlim_t cur, rlim_t max)
Dylan Reid0f72ef42017-06-06 15:42:49 -0700770{
771 size_t i;
772
773 if (j->rlimit_count >= MAX_RLIMITS)
774 return -ENOMEM;
775 /* It's an error if the caller sets the same rlimit multiple times. */
776 for (i = 0; i < j->rlimit_count; i++) {
777 if (j->rlimits[i].type == type)
778 return -EEXIST;
779 }
780
781 j->rlimits[j->rlimit_count].type = type;
782 j->rlimits[j->rlimit_count].cur = cur;
783 j->rlimits[j->rlimit_count].max = max;
784 j->rlimit_count++;
785 return 0;
786}
787
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400788int API minijail_forward_signals(struct minijail *j)
789{
790 j->flags.forward_signals = 1;
791 return 0;
792}
793
Xiyuan Xia9b41e652019-05-23 11:03:04 -0700794int API minijail_create_session(struct minijail *j) {
795 j->flags.setsid = 1;
796 return 0;
797}
798
Dylan Reid81e23972016-05-18 14:06:35 -0700799int API minijail_mount_with_data(struct minijail *j, const char *src,
800 const char *dest, const char *type,
801 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700802{
Dylan Reid648b2202015-10-23 00:50:00 -0700803 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400804
805 if (*dest != '/')
806 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700807 m = calloc(1, sizeof(*m));
808 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400809 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700810 m->dest = strdup(dest);
811 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400812 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700813 m->src = strdup(src);
814 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400815 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700816 m->type = strdup(type);
817 if (!m->type)
818 goto error;
Mike Frysingerb7803c82018-08-23 15:43:15 -0400819
820 if (!data || !data[0]) {
821 /*
822 * Set up secure defaults for certain filesystems. Adding this
823 * fs-specific logic here kind of sucks, but considering how
824 * people use these in practice, it's probably OK. If they want
825 * the kernel defaults, they can pass data="" instead of NULL.
826 */
827 if (!strcmp(type, "tmpfs")) {
828 /* tmpfs defaults to mode=1777 and size=50%. */
829 data = "mode=0755,size=10M";
830 }
831 }
Dylan Reid81e23972016-05-18 14:06:35 -0700832 if (data) {
833 m->data = strdup(data);
834 if (!m->data)
835 goto error;
836 m->has_data = 1;
837 }
Mike Frysingercb8674d2018-08-12 00:53:35 -0400838
839 /* If they don't specify any flags, default to secure ones. */
840 if (flags == 0)
841 flags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Dylan Reid648b2202015-10-23 00:50:00 -0700842 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400843
Elly Jonesdd3e8512012-01-23 15:13:38 -0500844 /*
Jorge Lucangeli Obes0a0514c2020-01-03 11:18:32 -0500845 * Unless asked to enter an existing namespace, force vfs namespacing
846 * so the mounts don't leak out into the containing vfs namespace.
847 * If Minijail is being asked to enter the root vfs namespace this will
848 * leak mounts, but it's unlikely that the user would ask to do that by
849 * mistake.
Elly Jones51a5b6c2011-10-12 19:09:26 -0400850 */
Jorge Lucangeli Obes0a0514c2020-01-03 11:18:32 -0500851 if (!j->flags.enter_vfs)
852 minijail_namespace_vfs(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400853
Dylan Reid648b2202015-10-23 00:50:00 -0700854 if (j->mounts_tail)
855 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400856 else
Dylan Reid648b2202015-10-23 00:50:00 -0700857 j->mounts_head = m;
858 j->mounts_tail = m;
859 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400860
861 return 0;
862
863error:
Dylan Reid81e23972016-05-18 14:06:35 -0700864 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700865 free(m->src);
866 free(m->dest);
867 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400868 return -ENOMEM;
869}
870
Dylan Reid81e23972016-05-18 14:06:35 -0700871int API minijail_mount(struct minijail *j, const char *src, const char *dest,
872 const char *type, unsigned long flags)
873{
874 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
875}
876
Dylan Reid648b2202015-10-23 00:50:00 -0700877int API minijail_bind(struct minijail *j, const char *src, const char *dest,
878 int writeable)
879{
880 unsigned long flags = MS_BIND;
881
882 if (!writeable)
883 flags |= MS_RDONLY;
884
885 return minijail_mount(j, src, dest, "", flags);
886}
887
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700888int API minijail_add_hook(struct minijail *j, minijail_hook_t hook,
889 void *payload, minijail_hook_event_t event)
890{
891 struct hook *c;
892
893 if (hook == NULL)
894 return -EINVAL;
895 if (event >= MINIJAIL_HOOK_EVENT_MAX)
896 return -EINVAL;
897 c = calloc(1, sizeof(*c));
898 if (!c)
899 return -ENOMEM;
900
901 c->hook = hook;
902 c->payload = payload;
903 c->event = event;
904
905 if (j->hooks_tail)
906 j->hooks_tail->next = c;
907 else
908 j->hooks_head = c;
909 j->hooks_tail = c;
910
911 return 0;
912}
913
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700914int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd)
915{
916 if (parent_fd < 0 || child_fd < 0)
917 return -EINVAL;
918 if (j->preserved_fd_count >= MAX_PRESERVED_FDS)
919 return -ENOMEM;
920 j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd;
921 j->preserved_fds[j->preserved_fd_count].child_fd = child_fd;
922 j->preserved_fd_count++;
923 return 0;
924}
925
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700926int API minijail_set_preload_path(struct minijail *j, const char *preload_path)
927{
928 if (j->preload_path)
929 return -EINVAL;
930 j->preload_path = strdup(preload_path);
931 if (!j->preload_path)
932 return -ENOMEM;
933 return 0;
934}
935
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400936static void clear_seccomp_options(struct minijail *j)
937{
938 j->flags.seccomp_filter = 0;
939 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400940 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400941 j->filter_len = 0;
942 j->filter_prog = NULL;
943 j->flags.no_new_privs = 0;
944}
945
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700946static int seccomp_should_use_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400947{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400948 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400949 /*
950 * |errno| will be set to EINVAL when seccomp has not been
951 * compiled into the kernel. On certain platforms and kernel
952 * versions this is not a fatal failure. In that case, and only
953 * in that case, disable seccomp and skip loading the filters.
954 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400955 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400956 warn("not loading seccomp filters, seccomp filter not "
957 "supported");
958 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400959 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700960 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400961 /*
962 * If |errno| != EINVAL or seccomp_can_softfail() is false,
963 * we can proceed. Worst case scenario minijail_enter() will
964 * abort() if seccomp fails.
965 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700966 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400967 if (j->flags.seccomp_filter_tsync) {
968 /* Are the seccomp(2) syscall and the TSYNC option supported? */
969 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
970 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
971 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400972 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
973 warn("seccomp(2) syscall not supported");
974 clear_seccomp_options(j);
975 return 0;
976 } else if (saved_errno == EINVAL &&
977 seccomp_can_softfail()) {
978 warn(
979 "seccomp filter thread sync not supported");
980 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400981 return 0;
982 }
983 /*
984 * Similar logic here. If seccomp_can_softfail() is
985 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
986 * we can proceed. Worst case scenario minijail_enter()
987 * will abort() if seccomp or TSYNC fail.
988 */
989 }
990 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400991 return 1;
992}
993
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700994static int set_seccomp_filters_internal(struct minijail *j,
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400995 const struct sock_fprog *filter,
996 bool owned)
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700997{
998 struct sock_fprog *fprog;
999
1000 if (owned) {
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001001 /*
1002 * If |owned| is true, it's OK to cast away the const-ness since
1003 * we'll own the pointer going forward.
1004 */
1005 fprog = (struct sock_fprog *)filter;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001006 } else {
1007 fprog = malloc(sizeof(struct sock_fprog));
1008 if (!fprog)
1009 return -ENOMEM;
1010 fprog->len = filter->len;
1011 fprog->filter = malloc(sizeof(struct sock_filter) * fprog->len);
1012 if (!fprog->filter) {
1013 free(fprog);
1014 return -ENOMEM;
1015 }
1016 memcpy(fprog->filter, filter->filter,
1017 sizeof(struct sock_filter) * fprog->len);
1018 }
1019
1020 if (j->filter_prog) {
1021 free(j->filter_prog->filter);
1022 free(j->filter_prog);
1023 }
1024
1025 j->filter_len = fprog->len;
1026 j->filter_prog = fprog;
1027 return 0;
1028}
1029
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001030static int parse_seccomp_filters(struct minijail *j, const char *filename,
1031 FILE *policy_file)
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001032{
1033 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001034 if (!fprog)
1035 return -ENOMEM;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001036
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001037 struct filter_options filteropts;
1038
1039 /*
1040 * Figure out filter options.
1041 * Allow logging?
1042 */
1043 filteropts.allow_logging =
1044 debug_logging_allowed() && j->flags.seccomp_filter_logging;
1045
1046 /* What to do on a blocked system call? */
1047 if (filteropts.allow_logging) {
1048 if (seccomp_ret_log_available())
1049 filteropts.action = ACTION_RET_LOG;
1050 else
1051 filteropts.action = ACTION_RET_TRAP;
1052 } else {
1053 if (j->flags.seccomp_filter_tsync)
1054 filteropts.action = ACTION_RET_TRAP;
1055 else
1056 filteropts.action = ACTION_RET_KILL;
1057 }
1058
1059 /*
1060 * If SECCOMP_RET_LOG is not available, need to allow extra syscalls
1061 * for logging.
1062 */
1063 filteropts.allow_syscalls_for_logging =
1064 filteropts.allow_logging && !seccomp_ret_log_available();
1065
Jorge Lucangeli Obese1a86892019-06-10 16:17:03 -04001066 if (compile_filter(filename, policy_file, fprog, &filteropts)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001067 free(fprog);
1068 return -1;
1069 }
1070
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001071 return set_seccomp_filters_internal(j, fprog, true /* owned */);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001072}
1073
1074void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
1075{
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001076 if (!seccomp_should_use_filters(j))
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001077 return;
1078
Luis Hector Chaveza30a2062018-07-12 21:10:33 -07001079 FILE *file = fopen(path, "re");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001080 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -07001081 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -04001082 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001083
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001084 if (parse_seccomp_filters(j, path, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -07001085 die("failed to compile seccomp filter BPF program in '%s'",
1086 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001087 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001088 fclose(file);
1089}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001090
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001091void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
1092{
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001093 char *fd_path, *path;
1094 FILE *file;
1095
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001096 if (!seccomp_should_use_filters(j))
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001097 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001098
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001099 file = fdopen(fd, "r");
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001100 if (!file) {
1101 pdie("failed to associate stream with fd %d", fd);
1102 }
1103
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001104 if (asprintf(&fd_path, "/proc/self/fd/%d", fd) == -1)
1105 pdie("failed to create path for fd %d", fd);
1106 path = realpath(fd_path, NULL);
1107 if (path == NULL)
1108 pwarn("failed to get path of fd %d", fd);
1109 free(fd_path);
1110
1111 if (parse_seccomp_filters(j, path ? path : "<fd>", file) != 0) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001112 die("failed to compile seccomp filter BPF program from fd %d",
1113 fd);
1114 }
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001115 free(path);
Elly Jonese1749eb2011-10-07 13:54:59 -04001116 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -05001117}
1118
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001119void API minijail_set_seccomp_filters(struct minijail *j,
1120 const struct sock_fprog *filter)
1121{
1122 if (!seccomp_should_use_filters(j))
1123 return;
1124
1125 if (j->flags.seccomp_filter_logging) {
1126 die("minijail_log_seccomp_filter_failures() is incompatible "
1127 "with minijail_set_seccomp_filters()");
1128 }
1129
1130 /*
1131 * set_seccomp_filters_internal() can only fail with ENOMEM.
1132 * Furthermore, since we won't own the incoming filter, it will not be
1133 * modified.
1134 */
1135 if (set_seccomp_filters_internal(j, filter, false /* owned */) < 0) {
1136 die("failed to set seccomp filter");
1137 }
1138}
1139
Andrew Brestickereac28942015-11-11 16:04:46 -08001140int API minijail_use_alt_syscall(struct minijail *j, const char *table)
1141{
1142 j->alt_syscall_table = strdup(table);
1143 if (!j->alt_syscall_table)
1144 return -ENOMEM;
1145 j->flags.alt_syscall = 1;
1146 return 0;
1147}
1148
Will Drewryf89aef52011-09-16 16:48:57 -05001149struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -04001150 size_t available;
1151 size_t total;
1152 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -05001153};
1154
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001155void marshal_state_init(struct marshal_state *state, char *buf,
1156 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -04001157{
1158 state->available = available;
1159 state->buf = buf;
1160 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001161}
1162
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001163void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -04001164{
1165 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -05001166
Elly Jonese1749eb2011-10-07 13:54:59 -04001167 /* Up to |available| will be written. */
1168 if (copy_len) {
1169 memcpy(state->buf, src, copy_len);
1170 state->buf += copy_len;
1171 state->available -= copy_len;
1172 }
1173 /* |total| will contain the expected length. */
1174 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -05001175}
1176
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001177void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -07001178{
1179 marshal_append(state, m->src, strlen(m->src) + 1);
1180 marshal_append(state, m->dest, strlen(m->dest) + 1);
1181 marshal_append(state, m->type, strlen(m->type) + 1);
1182 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
1183 if (m->has_data)
1184 marshal_append(state, m->data, strlen(m->data) + 1);
1185 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
1186}
1187
Will Drewry6ac91122011-10-21 16:38:58 -05001188void minijail_marshal_helper(struct marshal_state *state,
1189 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001190{
Dylan Reid648b2202015-10-23 00:50:00 -07001191 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001192 size_t i;
1193
Elly Jonese1749eb2011-10-07 13:54:59 -04001194 marshal_append(state, (char *)j, sizeof(*j));
1195 if (j->user)
1196 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001197 if (j->suppl_gid_list) {
1198 marshal_append(state, j->suppl_gid_list,
1199 j->suppl_gid_count * sizeof(gid_t));
1200 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001201 if (j->chrootdir)
1202 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001203 if (j->hostname)
1204 marshal_append(state, j->hostname, strlen(j->hostname) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -08001205 if (j->alt_syscall_table) {
1206 marshal_append(state, j->alt_syscall_table,
1207 strlen(j->alt_syscall_table) + 1);
1208 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001209 if (j->flags.seccomp_filter && j->filter_prog) {
1210 struct sock_fprog *fp = j->filter_prog;
1211 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001212 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -04001213 }
Dylan Reid648b2202015-10-23 00:50:00 -07001214 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001215 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001216 }
Dylan Reid605ce7f2016-01-19 19:21:00 -08001217 for (i = 0; i < j->cgroup_count; ++i)
1218 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -05001219}
1220
Will Drewry6ac91122011-10-21 16:38:58 -05001221size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001222{
1223 struct marshal_state state;
1224 marshal_state_init(&state, NULL, 0);
1225 minijail_marshal_helper(&state, j);
1226 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -05001227}
1228
Elly Jonese1749eb2011-10-07 13:54:59 -04001229int minijail_marshal(const struct minijail *j, char *buf, size_t available)
1230{
1231 struct marshal_state state;
1232 marshal_state_init(&state, buf, available);
1233 minijail_marshal_helper(&state, j);
1234 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -05001235}
1236
Elly Jonese1749eb2011-10-07 13:54:59 -04001237int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
1238{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -08001239 size_t i;
1240 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -05001241 int ret = -EINVAL;
1242
Elly Jonese1749eb2011-10-07 13:54:59 -04001243 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001244 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001245 memcpy((void *)j, serialized, sizeof(*j));
1246 serialized += sizeof(*j);
1247 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001248
Will Drewrybee7ba72011-10-21 20:47:01 -05001249 /* Potentially stale pointers not used as signals. */
Luis Hector Chavez9acba452018-10-11 10:13:25 -07001250 j->preload_path = NULL;
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001251 j->pid_file_path = NULL;
1252 j->uidmap = NULL;
1253 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001254 j->mounts_head = NULL;
1255 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001256 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001257 j->hooks_head = NULL;
1258 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001259
Elly Jonese1749eb2011-10-07 13:54:59 -04001260 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001261 char *user = consumestr(&serialized, &length);
1262 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001263 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001264 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001265 if (!j->user)
1266 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001267 }
Will Drewryf89aef52011-09-16 16:48:57 -05001268
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001269 if (j->suppl_gid_list) { /* stale pointer */
1270 if (j->suppl_gid_count > NGROUPS_MAX) {
1271 goto bad_gid_list;
1272 }
1273 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1274 void *gid_list_bytes =
1275 consumebytes(gid_list_size, &serialized, &length);
1276 if (!gid_list_bytes)
1277 goto bad_gid_list;
1278
1279 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1280 if (!j->suppl_gid_list)
1281 goto bad_gid_list;
1282
1283 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1284 }
1285
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001286 if (j->chrootdir) { /* stale pointer */
1287 char *chrootdir = consumestr(&serialized, &length);
1288 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001289 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001290 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001291 if (!j->chrootdir)
1292 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001293 }
1294
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001295 if (j->hostname) { /* stale pointer */
1296 char *hostname = consumestr(&serialized, &length);
1297 if (!hostname)
1298 goto bad_hostname;
1299 j->hostname = strdup(hostname);
1300 if (!j->hostname)
1301 goto bad_hostname;
1302 }
1303
Andrew Brestickereac28942015-11-11 16:04:46 -08001304 if (j->alt_syscall_table) { /* stale pointer */
1305 char *alt_syscall_table = consumestr(&serialized, &length);
1306 if (!alt_syscall_table)
1307 goto bad_syscall_table;
1308 j->alt_syscall_table = strdup(alt_syscall_table);
1309 if (!j->alt_syscall_table)
1310 goto bad_syscall_table;
1311 }
1312
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001313 if (j->flags.seccomp_filter && j->filter_len > 0) {
1314 size_t ninstrs = j->filter_len;
1315 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1316 ninstrs > USHRT_MAX)
1317 goto bad_filters;
1318
1319 size_t program_len = ninstrs * sizeof(struct sock_filter);
1320 void *program = consumebytes(program_len, &serialized, &length);
1321 if (!program)
1322 goto bad_filters;
1323
1324 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001325 if (!j->filter_prog)
1326 goto bad_filters;
1327
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001328 j->filter_prog->len = ninstrs;
1329 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001330 if (!j->filter_prog->filter)
1331 goto bad_filter_prog_instrs;
1332
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001333 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001334 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001335
Dylan Reid648b2202015-10-23 00:50:00 -07001336 count = j->mounts_count;
1337 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001338 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001339 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001340 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001341 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001342 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001343 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001344 const char *src = consumestr(&serialized, &length);
1345 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001346 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001347 dest = consumestr(&serialized, &length);
1348 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001349 goto bad_mounts;
1350 type = consumestr(&serialized, &length);
1351 if (!type)
1352 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001353 has_data = consumebytes(sizeof(*has_data), &serialized,
1354 &length);
1355 if (!has_data)
1356 goto bad_mounts;
1357 if (*has_data) {
1358 data = consumestr(&serialized, &length);
1359 if (!data)
1360 goto bad_mounts;
1361 }
Dylan Reid648b2202015-10-23 00:50:00 -07001362 flags = consumebytes(sizeof(*flags), &serialized, &length);
1363 if (!flags)
1364 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001365 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001366 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001367 }
1368
Dylan Reid605ce7f2016-01-19 19:21:00 -08001369 count = j->cgroup_count;
1370 j->cgroup_count = 0;
1371 for (i = 0; i < count; ++i) {
1372 char *cgroup = consumestr(&serialized, &length);
1373 if (!cgroup)
1374 goto bad_cgroups;
1375 j->cgroups[i] = strdup(cgroup);
1376 if (!j->cgroups[i])
1377 goto bad_cgroups;
1378 ++j->cgroup_count;
1379 }
1380
Elly Jonese1749eb2011-10-07 13:54:59 -04001381 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001382
Dylan Reid605ce7f2016-01-19 19:21:00 -08001383bad_cgroups:
Mike Frysingerac08a682017-10-10 02:04:50 -04001384 free_mounts_list(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001385 for (i = 0; i < j->cgroup_count; ++i)
1386 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001387bad_mounts:
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001388 if (j->filter_prog && j->filter_prog->filter)
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001389 free(j->filter_prog->filter);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001390bad_filter_prog_instrs:
1391 if (j->filter_prog)
1392 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001393bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001394 if (j->alt_syscall_table)
1395 free(j->alt_syscall_table);
1396bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001397 if (j->chrootdir)
1398 free(j->chrootdir);
1399bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001400 if (j->hostname)
1401 free(j->hostname);
1402bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001403 if (j->suppl_gid_list)
1404 free(j->suppl_gid_list);
1405bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001406 if (j->user)
1407 free(j->user);
1408clear_pointers:
1409 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001410 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001411 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001412 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001413 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001414 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001415out:
1416 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001417}
1418
Mike Frysinger33ffef32017-01-13 19:53:19 -05001419struct dev_spec {
1420 const char *name;
1421 mode_t mode;
1422 dev_t major, minor;
1423};
1424
1425static const struct dev_spec device_nodes[] = {
1426 {
1427 "null",
1428 S_IFCHR | 0666, 1, 3,
1429 },
1430 {
1431 "zero",
1432 S_IFCHR | 0666, 1, 5,
1433 },
1434 {
1435 "full",
1436 S_IFCHR | 0666, 1, 7,
1437 },
1438 {
1439 "urandom",
1440 S_IFCHR | 0444, 1, 9,
1441 },
1442 {
1443 "tty",
1444 S_IFCHR | 0666, 5, 0,
1445 },
1446};
1447
1448struct dev_sym_spec {
1449 const char *source, *dest;
1450};
1451
1452static const struct dev_sym_spec device_symlinks[] = {
1453 { "ptmx", "pts/ptmx", },
1454 { "fd", "/proc/self/fd", },
1455 { "stdin", "fd/0", },
1456 { "stdout", "fd/1", },
1457 { "stderr", "fd/2", },
1458};
1459
1460/*
1461 * Clean up the temporary dev path we had setup previously. In case of errors,
1462 * we don't want to go leaking empty tempdirs.
1463 */
1464static void mount_dev_cleanup(char *dev_path)
1465{
1466 umount2(dev_path, MNT_DETACH);
1467 rmdir(dev_path);
1468 free(dev_path);
1469}
1470
1471/*
1472 * Set up the pseudo /dev path at the temporary location.
1473 * See mount_dev_finalize for more details.
1474 */
1475static int mount_dev(char **dev_path_ret)
1476{
1477 int ret;
1478 int dev_fd;
1479 size_t i;
1480 mode_t mask;
1481 char *dev_path;
1482
1483 /*
1484 * Create a temp path for the /dev init. We'll relocate this to the
1485 * final location later on in the startup process.
1486 */
1487 dev_path = *dev_path_ret = strdup("/tmp/minijail.dev.XXXXXX");
1488 if (dev_path == NULL || mkdtemp(dev_path) == NULL)
1489 pdie("could not create temp path for /dev");
1490
1491 /* Set up the empty /dev mount point first. */
1492 ret = mount("minijail-devfs", dev_path, "tmpfs",
1493 MS_NOEXEC | MS_NOSUID, "size=5M,mode=755");
1494 if (ret) {
1495 rmdir(dev_path);
1496 return ret;
1497 }
1498
1499 /* We want to set the mode directly from the spec. */
1500 mask = umask(0);
1501
1502 /* Get a handle to the temp dev path for *at funcs below. */
1503 dev_fd = open(dev_path, O_DIRECTORY|O_PATH|O_CLOEXEC);
1504 if (dev_fd < 0) {
1505 ret = 1;
1506 goto done;
1507 }
1508
1509 /* Create all the nodes in /dev. */
1510 for (i = 0; i < ARRAY_SIZE(device_nodes); ++i) {
1511 const struct dev_spec *ds = &device_nodes[i];
1512 ret = mknodat(dev_fd, ds->name, ds->mode,
1513 makedev(ds->major, ds->minor));
1514 if (ret)
1515 goto done;
1516 }
1517
1518 /* Create all the symlinks in /dev. */
1519 for (i = 0; i < ARRAY_SIZE(device_symlinks); ++i) {
1520 const struct dev_sym_spec *ds = &device_symlinks[i];
1521 ret = symlinkat(ds->dest, dev_fd, ds->source);
1522 if (ret)
1523 goto done;
1524 }
1525
1526 /* Restore old mask. */
1527 done:
1528 close(dev_fd);
1529 umask(mask);
1530
1531 if (ret)
1532 mount_dev_cleanup(dev_path);
1533
1534 return ret;
1535}
1536
1537/*
1538 * Relocate the temporary /dev mount to its final /dev place.
1539 * We have to do this two step process so people can bind mount extra
1540 * /dev paths like /dev/log.
1541 */
1542static int mount_dev_finalize(const struct minijail *j, char *dev_path)
1543{
1544 int ret = -1;
1545 char *dest = NULL;
1546
1547 /* Unmount the /dev mount if possible. */
1548 if (umount2("/dev", MNT_DETACH))
1549 goto done;
1550
1551 if (asprintf(&dest, "%s/dev", j->chrootdir ? : "") < 0)
1552 goto done;
1553
1554 if (mount(dev_path, dest, NULL, MS_MOVE, NULL))
1555 goto done;
1556
1557 ret = 0;
1558 done:
1559 free(dest);
1560 mount_dev_cleanup(dev_path);
1561
1562 return ret;
1563}
1564
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001565/*
1566 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001567 * @j Minijail these mounts are for
1568 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001569 *
1570 * Returns 0 for success.
1571 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001572static int mount_one(const struct minijail *j, struct mountpoint *m,
1573 const char *dev_path)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001574{
Dylan Reid648b2202015-10-23 00:50:00 -07001575 int ret;
1576 char *dest;
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001577 int remount = 0;
1578 unsigned long original_mnt_flags = 0;
Dylan Reid648b2202015-10-23 00:50:00 -07001579
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -04001580 /* We assume |dest| has a leading "/". */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001581 if (dev_path && strncmp("/dev/", m->dest, 5) == 0) {
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -04001582 /*
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -04001583 * Since the temp path is rooted at /dev, skip that dest part.
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -04001584 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001585 if (asprintf(&dest, "%s%s", dev_path, m->dest + 4) < 0)
1586 return -ENOMEM;
1587 } else {
Mike Frysingerac08a682017-10-10 02:04:50 -04001588 if (asprintf(&dest, "%s%s", j->chrootdir ?: "", m->dest) < 0)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001589 return -ENOMEM;
1590 }
Dylan Reid648b2202015-10-23 00:50:00 -07001591
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001592 ret =
1593 setup_mount_destination(m->src, dest, j->uid, j->gid,
1594 (m->flags & MS_BIND), &original_mnt_flags);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001595 if (ret) {
François Degrosa42182d2020-04-29 00:41:52 +10001596 warn("cannot create mount target '%s'", dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001597 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001598 }
Dylan Reideec77962016-06-30 19:35:10 -07001599
Dylan Reid648b2202015-10-23 00:50:00 -07001600 /*
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001601 * Bind mounts that change the 'ro' flag have to be remounted since
1602 * 'bind' and other flags can't both be specified in the same command.
1603 * Remount after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001604 */
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001605 if ((m->flags & MS_BIND) &&
1606 ((m->flags & MS_RDONLY) != (original_mnt_flags & MS_RDONLY))) {
1607 remount = 1;
1608 /*
1609 * Restrict the mount flags to those that are user-settable in a
1610 * MS_REMOUNT request, but excluding MS_RDONLY. The
1611 * user-requested mount flags will dictate whether the remount
1612 * will have that flag or not.
1613 */
1614 original_mnt_flags &= (MS_USER_SETTABLE_MASK & ~MS_RDONLY);
Elly Jonesa1059632011-12-15 15:17:07 -05001615 }
Dylan Reid648b2202015-10-23 00:50:00 -07001616
Dylan Reid81e23972016-05-18 14:06:35 -07001617 ret = mount(m->src, dest, m->type, m->flags, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001618 if (ret) {
François Degrosa42182d2020-04-29 00:41:52 +10001619 pwarn("cannot bind-mount '%s' as '%s' with flags %#lx", m->src,
1620 dest, m->flags);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001621 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001622 }
Dylan Reid648b2202015-10-23 00:50:00 -07001623
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001624 if (remount) {
1625 ret =
1626 mount(m->src, dest, NULL,
1627 m->flags | original_mnt_flags | MS_REMOUNT, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001628 if (ret) {
François Degrosa42182d2020-04-29 00:41:52 +10001629 pwarn(
1630 "cannot bind-remount '%s' as '%s' with flags %#lx",
1631 m->src, dest,
1632 m->flags | original_mnt_flags | MS_REMOUNT);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001633 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001634 }
Dylan Reid648b2202015-10-23 00:50:00 -07001635 }
1636
Elly Jones51a5b6c2011-10-12 19:09:26 -04001637 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001638 if (m->next)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001639 return mount_one(j, m->next, dev_path);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001640 return 0;
1641
1642error:
1643 free(dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001644 return ret;
1645}
1646
Mike Frysingerac08a682017-10-10 02:04:50 -04001647static void process_mounts_or_die(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001648{
Mike Frysingerac08a682017-10-10 02:04:50 -04001649 /*
1650 * We have to mount /dev first in case there are bind mounts from
1651 * the original /dev into the new unique tmpfs one.
1652 */
1653 char *dev_path = NULL;
1654 if (j->flags.mount_dev && mount_dev(&dev_path))
1655 pdie("mount_dev failed");
Dylan Reid648b2202015-10-23 00:50:00 -07001656
Mike Frysingerac08a682017-10-10 02:04:50 -04001657 if (j->mounts_head && mount_one(j, j->mounts_head, dev_path)) {
François Degrosa42182d2020-04-29 00:41:52 +10001658 if (dev_path)
Mike Frysingerac08a682017-10-10 02:04:50 -04001659 mount_dev_cleanup(dev_path);
François Degrosa42182d2020-04-29 00:41:52 +10001660
1661 _exit(MINIJAIL_ERR_MOUNT);
Mike Frysingerac08a682017-10-10 02:04:50 -04001662 }
Mike Frysinger33ffef32017-01-13 19:53:19 -05001663
1664 /*
Mike Frysingerac08a682017-10-10 02:04:50 -04001665 * Once all bind mounts have been processed, move the temp dev to
1666 * its final /dev home.
Mike Frysinger33ffef32017-01-13 19:53:19 -05001667 */
1668 if (j->flags.mount_dev && mount_dev_finalize(j, dev_path))
Mike Frysingerac08a682017-10-10 02:04:50 -04001669 pdie("mount_dev_finalize failed");
1670}
Elly Jones51a5b6c2011-10-12 19:09:26 -04001671
Mike Frysingerac08a682017-10-10 02:04:50 -04001672static int enter_chroot(const struct minijail *j)
1673{
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001674 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1675
Elly Jones51a5b6c2011-10-12 19:09:26 -04001676 if (chroot(j->chrootdir))
1677 return -errno;
1678
1679 if (chdir("/"))
1680 return -errno;
1681
1682 return 0;
1683}
1684
Mike Frysingerac08a682017-10-10 02:04:50 -04001685static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001686{
Mike Frysingerac08a682017-10-10 02:04:50 -04001687 int oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001688
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001689 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1690
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001691 /*
1692 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001693 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001694 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001695 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001696 if (oldroot < 0)
1697 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001698 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001699 if (newroot < 0)
1700 pdie("failed to open %s for fchdir", j->chrootdir);
1701
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001702 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001703 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001704 * do a self bind mount.
1705 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001706 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1707 pdie("failed to bind mount '%s'", j->chrootdir);
1708 if (chdir(j->chrootdir))
1709 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001710 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001711 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001712
1713 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001714 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001715 * change to the old root and unmount it.
1716 */
1717 if (fchdir(oldroot))
1718 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001719
1720 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05001721 * If skip_remount_private was enabled for minijail_enter(),
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001722 * there could be a shared mount point under |oldroot|. In that case,
1723 * mounts under this shared mount point will be unmounted below, and
1724 * this unmounting will propagate to the original mount namespace
1725 * (because the mount point is shared). To prevent this unexpected
1726 * unmounting, remove these mounts from their peer groups by recursively
1727 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001728 */
1729 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001730 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001731 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001732 if (umount2(".", MNT_DETACH))
1733 pdie("umount(/)");
1734 /* Change back to the new root. */
1735 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001736 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001737 if (close(oldroot))
1738 return -errno;
1739 if (close(newroot))
1740 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001741 if (chroot("/"))
1742 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001743 /* Set correct CWD for getcwd(3). */
1744 if (chdir("/"))
1745 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001746
1747 return 0;
1748}
1749
Martin Pelikánab9eb442017-01-25 11:53:58 +11001750static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001751{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001752 const char fmt[] = "size=%zu,mode=1777";
1753 /* Count for the user storing ULLONG_MAX literally + extra space. */
1754 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1755 int ret;
1756
1757 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1758
1759 if (ret <= 0)
1760 pdie("tmpfs size spec error");
1761 else if ((size_t)ret >= sizeof(data))
1762 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001763 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001764 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001765}
1766
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001767static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001768{
1769 const char *kProcPath = "/proc";
1770 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001771 /*
1772 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001773 * /proc in our namespace, which means using MS_REMOUNT here would
1774 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001775 * namespace (!). Instead, remove their mount from our namespace lazily
1776 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001777 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001778 if (umount2(kProcPath, MNT_DETACH)) {
1779 /*
1780 * If we are in a new user namespace, umount(2) will fail.
1781 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1782 */
1783 if (j->flags.userns) {
1784 info("umount(/proc, MNT_DETACH) failed, "
1785 "this is expected when using user namespaces");
1786 } else {
1787 return -errno;
1788 }
1789 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001790 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001791 return -errno;
1792 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001793}
1794
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001795static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001796{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001797 kill(j->initpid, SIGKILL);
1798 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001799}
1800
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001801static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001802{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001803 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001804 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001805}
1806
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001807static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001808{
1809 size_t i;
1810
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001811 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001812 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001813 kill_child_and_die(j, "failed to add to cgroups");
1814 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001815}
1816
Dylan Reid0f72ef42017-06-06 15:42:49 -07001817static void set_rlimits_or_die(const struct minijail *j)
1818{
1819 size_t i;
1820
1821 for (i = 0; i < j->rlimit_count; ++i) {
1822 struct rlimit limit;
1823 limit.rlim_cur = j->rlimits[i].cur;
1824 limit.rlim_max = j->rlimits[i].max;
1825 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1826 kill_child_and_die(j, "failed to set rlimit");
1827 }
1828}
1829
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001830static void write_ugid_maps_or_die(const struct minijail *j)
1831{
1832 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1833 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001834 if (j->gidmap && j->flags.disable_setgroups) {
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001835 /*
1836 * Older kernels might not have the /proc/<pid>/setgroups files.
1837 */
Mike Frysinger6b190c02017-01-04 17:18:42 -05001838 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001839 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001840 if (ret == -ENOENT) {
1841 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1842 warn("could not disable setgroups(2)");
1843 } else
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001844 kill_child_and_die(
1845 j, "failed to disable setgroups(2)");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001846 }
1847 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001848 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1849 kill_child_and_die(j, "failed to write gid_map");
1850}
1851
1852static void enter_user_namespace(const struct minijail *j)
1853{
Luis Hector Chavez71323552017-09-05 09:17:22 -07001854 int uid = j->flags.uid ? j->uid : 0;
1855 int gid = j->flags.gid ? j->gid : 0;
1856 if (j->gidmap && setresgid(gid, gid, gid)) {
1857 pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid,
1858 gid);
1859 }
1860 if (j->uidmap && setresuid(uid, uid, uid)) {
1861 pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid,
1862 uid);
1863 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001864}
1865
1866static void parent_setup_complete(int *pipe_fds)
1867{
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001868 close_and_reset(&pipe_fds[0]);
1869 close_and_reset(&pipe_fds[1]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001870}
1871
1872/*
1873 * wait_for_parent_setup: Called by the child process to wait for any
1874 * further parent-side setup to complete before continuing.
1875 */
1876static void wait_for_parent_setup(int *pipe_fds)
1877{
1878 char buf;
1879
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001880 close_and_reset(&pipe_fds[1]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001881
1882 /* Wait for parent to complete setup and close the pipe. */
1883 if (read(pipe_fds[0], &buf, 1) != 0)
1884 die("failed to sync with parent");
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001885 close_and_reset(&pipe_fds[0]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001886}
1887
1888static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001889{
Lutz Justen13807cb2017-01-03 17:11:55 +01001890 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1891 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001892 die("can only do one of inherit, keep, or set supplementary "
1893 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001894 }
1895
Lutz Justen13807cb2017-01-03 17:11:55 +01001896 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001897 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001898 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001899 } else if (j->flags.set_suppl_gids) {
1900 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001901 pdie("setgroups(suppl_gids) failed");
Luis Hector Chavez71323552017-09-05 09:17:22 -07001902 } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001903 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001904 * Only attempt to clear supplementary groups if we are changing
Luis Hector Chavez71323552017-09-05 09:17:22 -07001905 * users or groups, and if the caller did not request to disable
1906 * setgroups (used when entering a user namespace as a
1907 * non-privileged user).
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001908 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001909 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001910 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001911 }
1912
1913 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001914 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001915
1916 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001917 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001918}
1919
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001920static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1921{
1922 const uint64_t one = 1;
1923 unsigned int i;
1924 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1925 if (keep_mask & (one << i))
1926 continue;
1927 if (prctl(PR_CAPBSET_DROP, i))
1928 pdie("could not drop capability from bounding set");
1929 }
1930}
1931
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001932static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001933{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001934 if (!j->flags.use_caps)
1935 return;
1936
Elly Jonese1749eb2011-10-07 13:54:59 -04001937 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001938 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001939 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001940 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001941 unsigned int i;
1942 if (!caps)
1943 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001944 if (cap_clear(caps))
1945 die("can't clear caps");
1946
1947 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001948 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001949 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001950 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001951 flag[0] = i;
1952 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001953 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001954 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001955 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001956 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001957 die("can't add inheritable cap");
1958 }
1959 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001960 die("can't apply initial cleaned capset");
1961
1962 /*
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001963 * Instead of dropping the bounding set first, do it here in case
Kees Cook323878a2013-02-05 15:35:24 -08001964 * the caller had a more permissive bounding set which could
1965 * have been used above to raise a capability that wasn't already
1966 * present. This requires CAP_SETPCAP, so we raised/kept it above.
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001967 *
1968 * However, if we're asked to skip setting *and* locking the
1969 * SECURE_NOROOT securebit, also skip dropping the bounding set.
1970 * If the caller wants to regain all capabilities when executing a
1971 * set-user-ID-root program, allow them to do so. The default behavior
1972 * (i.e. the behavior without |securebits_skip_mask| set) will still put
1973 * the jailed process tree in a capabilities-only environment.
1974 *
1975 * We check the negated skip mask for SECURE_NOROOT and
1976 * SECURE_NOROOT_LOCKED. If the bits are set in the negated mask they
1977 * will *not* be skipped in lock_securebits(), and therefore we should
1978 * drop the bounding set.
Kees Cook323878a2013-02-05 15:35:24 -08001979 */
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001980 if (secure_noroot_set_and_locked(~j->securebits_skip_mask)) {
1981 drop_capbset(j->caps, last_valid_cap);
1982 } else {
1983 warn("SECURE_NOROOT not set, not dropping bounding set");
1984 }
Kees Cook323878a2013-02-05 15:35:24 -08001985
1986 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001987 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001988 flag[0] = CAP_SETPCAP;
1989 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1990 die("can't clear effective cap");
1991 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1992 die("can't clear permitted cap");
1993 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1994 die("can't clear inheritable cap");
1995 }
1996
1997 if (cap_set_proc(caps))
1998 die("can't apply final cleaned capset");
1999
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04002000 /*
2001 * If ambient capabilities are supported, clear all capabilities first,
2002 * then raise the requested ones.
2003 */
2004 if (j->flags.set_ambient_caps) {
2005 if (!cap_ambient_supported()) {
2006 pdie("ambient capabilities not supported");
2007 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04002008 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
2009 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04002010 pdie("can't clear ambient capabilities");
2011 }
2012
2013 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
2014 if (!(j->caps & (one << i)))
2015 continue;
2016
2017 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
2018 0) != 0) {
2019 pdie("prctl(PR_CAP_AMBIENT, "
2020 "PR_CAP_AMBIENT_RAISE, %u) failed",
2021 i);
2022 }
2023 }
2024 }
2025
Kees Cook323878a2013-02-05 15:35:24 -08002026 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04002027}
2028
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002029static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002030{
2031 /*
2032 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
2033 * in the kernel source tree for an explanation of the parameters.
2034 */
2035 if (j->flags.no_new_privs) {
2036 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
2037 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
2038 }
2039
2040 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07002041 * Code running with ASan
2042 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
2043 * will make system calls not included in the syscall filter policy,
2044 * which will likely crash the program. Skip setting seccomp filter in
2045 * that case.
2046 * 'running_with_asan()' has no inputs and is completely defined at
2047 * build time, so this cannot be used by an attacker to skip setting
2048 * seccomp filter.
2049 */
Evgenii Stepanov3d98f3c2018-08-23 15:06:50 -07002050 if (j->flags.seccomp_filter && running_with_asan()) {
Evgenii Stepanov825828c2018-07-27 11:57:07 -07002051 warn("running with (HW)ASan, not setting seccomp filter");
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07002052 return;
2053 }
2054
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002055 if (j->flags.seccomp_filter) {
2056 if (j->flags.seccomp_filter_logging) {
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002057 warn("logging seccomp filter failures");
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04002058 if (!seccomp_ret_log_available()) {
2059 /*
2060 * If SECCOMP_RET_LOG is not available,
2061 * install the SIGSYS handler first.
2062 */
2063 if (install_sigsys_handler())
2064 pdie(
2065 "failed to install SIGSYS handler");
2066 }
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002067 } else if (j->flags.seccomp_filter_tsync) {
2068 /*
2069 * If setting thread sync,
2070 * reset the SIGSYS signal handler so that
2071 * the entire thread group is killed.
2072 */
2073 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
2074 pdie("failed to reset SIGSYS disposition");
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002075 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002076 }
2077
2078 /*
2079 * Install the syscall filter.
2080 */
2081 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002082 if (j->flags.seccomp_filter_tsync) {
2083 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
2084 SECCOMP_FILTER_FLAG_TSYNC,
2085 j->filter_prog)) {
2086 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002087 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002088 } else {
2089 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
2090 j->filter_prog)) {
2091 pdie("prctl(seccomp_filter) failed");
2092 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002093 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002094 }
2095}
2096
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002097static pid_t forward_pid = -1;
2098
Mike Frysinger33d051a2018-05-30 16:41:10 -04002099static void forward_signal(int sig,
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04002100 siginfo_t *siginfo attribute_unused,
2101 void *void_context attribute_unused)
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002102{
2103 if (forward_pid != -1) {
Mike Frysinger33d051a2018-05-30 16:41:10 -04002104 kill(forward_pid, sig);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002105 }
2106}
2107
2108static void install_signal_handlers(void)
2109{
2110 struct sigaction act;
2111
2112 memset(&act, 0, sizeof(act));
2113 act.sa_sigaction = &forward_signal;
2114 act.sa_flags = SA_SIGINFO | SA_RESTART;
2115
2116 /* Handle all signals, except SIGCHLD. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002117 for (int sig = 1; sig < NSIG; sig++) {
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002118 /*
2119 * We don't care if we get EINVAL: that just means that we
2120 * can't handle this signal, so let's skip it and continue.
2121 */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002122 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002123 }
2124 /* Reset SIGCHLD's handler. */
2125 signal(SIGCHLD, SIG_DFL);
2126
2127 /* Handle real-time signals. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002128 for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++) {
2129 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002130 }
2131}
2132
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002133static const char *lookup_hook_name(minijail_hook_event_t event)
2134{
2135 switch (event) {
2136 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
2137 return "pre-drop-caps";
2138 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
2139 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07002140 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
2141 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002142 case MINIJAIL_HOOK_EVENT_MAX:
2143 /*
2144 * Adding this in favor of a default case to force the
2145 * compiler to error out if a new enum value is added.
2146 */
2147 break;
2148 }
2149 return "unknown";
2150}
2151
2152static void run_hooks_or_die(const struct minijail *j,
2153 minijail_hook_event_t event)
2154{
2155 int rc;
2156 int hook_index = 0;
2157 for (struct hook *c = j->hooks_head; c; c = c->next) {
2158 if (c->event != event)
2159 continue;
2160 rc = c->hook(c->payload);
2161 if (rc != 0) {
2162 errno = -rc;
2163 pdie("%s hook (index %d) failed",
2164 lookup_hook_name(event), hook_index);
2165 }
2166 /* Only increase the index within the same hook event type. */
2167 ++hook_index;
2168 }
2169}
2170
Will Drewry6ac91122011-10-21 16:38:58 -05002171void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002172{
Dylan Reidf682d472015-09-17 21:39:07 -07002173 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002174 * If we're dropping caps, get the last valid cap from /proc now,
2175 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07002176 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002177 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002178 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002179 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07002180
Elly Jonese1749eb2011-10-07 13:54:59 -04002181 if (j->flags.pids)
2182 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002183 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04002184
Lutz Justen13807cb2017-01-03 17:11:55 +01002185 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05002186 die("cannot inherit supplementary groups without setting a "
2187 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04002188
Elly Jonesdd3e8512012-01-23 15:13:38 -05002189 /*
2190 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04002191 * so we don't even try. If any of our operations fail, we abort() the
2192 * entire process.
2193 */
Mike Frysinger902a4492018-12-27 05:22:56 -05002194 if (j->flags.enter_vfs) {
2195 if (setns(j->mountns_fd, CLONE_NEWNS))
2196 pdie("setns(CLONE_NEWNS) failed");
2197 close(j->mountns_fd);
2198 }
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002199
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07002200 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002201 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002202 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002203 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05002204 * By default, remount all filesystems as private, unless
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04002205 * - Passed a specific remount mode, in which case remount with
2206 * that,
2207 * - Asked not to remount at all, in which case skip the
2208 * mount(2) call.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002209 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
2210 */
Mike Frysinger785b1c32018-02-23 15:47:24 -05002211 if (j->remount_mode) {
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04002212 if (mount(NULL, "/", NULL, MS_REC | j->remount_mode,
2213 NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002214 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
2215 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002216 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002217 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04002218
Dylan Reidf7942472015-11-18 17:55:26 -08002219 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002220 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08002221 }
2222
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002223 if (j->flags.uts) {
2224 if (unshare(CLONE_NEWUTS))
2225 pdie("unshare(CLONE_NEWUTS) failed");
2226
2227 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
2228 pdie("sethostname(%s) failed", j->hostname);
2229 }
2230
Dylan Reid1102f5a2015-09-15 11:52:20 -07002231 if (j->flags.enter_net) {
2232 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002233 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger902a4492018-12-27 05:22:56 -05002234 close(j->netns_fd);
Mike Frysinger7559dfe2016-11-15 18:58:39 -05002235 } else if (j->flags.net) {
2236 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002237 pdie("unshare(CLONE_NEWNET) failed");
2238 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07002239 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002240
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002241 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002242 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002243
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08002244 if (j->flags.new_session_keyring) {
2245 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
2246 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
2247 }
2248
Mike Frysingerac08a682017-10-10 02:04:50 -04002249 /* We have to process all the mounts before we chroot/pivot_root. */
2250 process_mounts_or_die(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002251
Mike Frysingerac08a682017-10-10 02:04:50 -04002252 if (j->flags.chroot && enter_chroot(j))
Mike Frysinger33ffef32017-01-13 19:53:19 -05002253 pdie("chroot");
Mike Frysinger33ffef32017-01-13 19:53:19 -05002254
Mike Frysingerac08a682017-10-10 02:04:50 -04002255 if (j->flags.pivot_root && enter_pivot_root(j))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08002256 pdie("pivot_root");
2257
Martin Pelikánab9eb442017-01-25 11:53:58 +11002258 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07002259 pdie("mount_tmp");
2260
Dylan Reid791f5772015-09-14 20:02:42 -07002261 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04002262 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04002263
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002264 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
2265
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002266 /*
2267 * If we're only dropping capabilities from the bounding set, but not
2268 * from the thread's (permitted|inheritable|effective) sets, do it now.
2269 */
2270 if (j->flags.capbset_drop) {
2271 drop_capbset(j->cap_bset, last_valid_cap);
2272 }
2273
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002274 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002275 * POSIX capabilities are a bit tricky. We must set SECBIT_KEEP_CAPS
2276 * before drop_ugid() below as the latter would otherwise drop all
2277 * capabilities.
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002278 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002279 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002280 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002281 * When using ambient capabilities, CAP_SET{GID,UID} can be
2282 * inherited across execve(2), so SECBIT_KEEP_CAPS is not
2283 * strictly needed.
Elly Jonese1749eb2011-10-07 13:54:59 -04002284 */
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002285 bool require_keep_caps = !j->flags.set_ambient_caps;
2286 if (lock_securebits(j->securebits_skip_mask,
2287 require_keep_caps) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002288 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002289 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002290 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002291
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002292 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002293 /*
2294 * If we're setting no_new_privs, we can drop privileges
2295 * before setting seccomp filter. This way filter policies
2296 * don't need to allow privilege-dropping syscalls.
2297 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002298 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002299 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002300 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002301 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002302 /*
2303 * If we're not setting no_new_privs,
2304 * we need to set seccomp filter *before* dropping privileges.
2305 * WARNING: this means that filter policies *must* allow
2306 * setgroups()/setresgid()/setresuid() for dropping root and
2307 * capget()/capset()/prctl() for dropping caps.
2308 */
2309 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002310 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002311 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04002312 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002313
Elly Jonesdd3e8512012-01-23 15:13:38 -05002314 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08002315 * Select the specified alternate syscall table. The table must not
2316 * block prctl(2) if we're using seccomp as well.
2317 */
2318 if (j->flags.alt_syscall) {
2319 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002320 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08002321 }
2322
2323 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05002324 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04002325 * privilege-dropping syscalls :)
2326 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002327 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04002328 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002329 warn("seccomp not supported");
2330 return;
2331 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002332 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002333 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002334}
2335
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002336/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04002337static int init_exitstatus = 0;
2338
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04002339void init_term(int sig attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002340{
2341 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04002342}
2343
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002344void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04002345{
2346 pid_t pid;
2347 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002348 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002349 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002350 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002351 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002352 /*
2353 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04002354 * left inside our pid namespace or we get a signal.
2355 */
2356 if (pid == rootpid)
2357 init_exitstatus = status;
2358 }
2359 if (!WIFEXITED(init_exitstatus))
2360 _exit(MINIJAIL_ERR_INIT);
2361 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04002362}
2363
Will Drewry6ac91122011-10-21 16:38:58 -05002364int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002365{
2366 size_t sz = 0;
2367 size_t bytes = read(fd, &sz, sizeof(sz));
2368 char *buf;
2369 int r;
2370 if (sizeof(sz) != bytes)
2371 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002372 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04002373 return -E2BIG;
2374 buf = malloc(sz);
2375 if (!buf)
2376 return -ENOMEM;
2377 bytes = read(fd, buf, sz);
2378 if (bytes != sz) {
2379 free(buf);
2380 return -EINVAL;
2381 }
2382 r = minijail_unmarshal(j, buf, sz);
2383 free(buf);
2384 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002385}
2386
Will Drewry6ac91122011-10-21 16:38:58 -05002387int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04002388{
Elly Jonese1749eb2011-10-07 13:54:59 -04002389 size_t sz = minijail_size(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002390 if (!sz)
2391 return -EINVAL;
François Degros664eba72019-11-05 13:18:24 +11002392
2393 char *buf = malloc(sz);
2394 if (!buf)
2395 return -ENOMEM;
2396
2397 int err = minijail_marshal(j, buf, sz);
2398 if (err)
2399 goto error;
2400
Elly Jonese1749eb2011-10-07 13:54:59 -04002401 /* Sends [size][minijail]. */
François Degros664eba72019-11-05 13:18:24 +11002402 err = write_exactly(fd, &sz, sizeof(sz));
2403 if (err)
2404 goto error;
2405
2406 err = write_exactly(fd, buf, sz);
2407
2408error:
Elly Jonese1749eb2011-10-07 13:54:59 -04002409 free(buf);
François Degros664eba72019-11-05 13:18:24 +11002410 return err;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002411}
Elly Jonescd7a9042011-07-22 13:56:51 -04002412
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002413static int setup_preload(const struct minijail *j attribute_unused,
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002414 char ***child_env attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002415{
Daniel Erat5b7a3182015-08-19 16:06:22 -06002416#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002417 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002418 return 0;
2419#else
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002420 const char *preload_path = j->preload_path ?: PRELOADPATH;
2421 char *newenv = NULL;
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002422 int ret = 0;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002423 const char *oldenv = getenv(kLdPreloadEnvVar);
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002424
2425 if (!oldenv)
2426 oldenv = "";
Elly Jonescd7a9042011-07-22 13:56:51 -04002427
Elly Jonese1749eb2011-10-07 13:54:59 -04002428 /* Only insert a separating space if we have something to separate... */
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002429 if (asprintf(&newenv, "%s%s%s", oldenv, oldenv[0] != '\0' ? " " : "",
2430 preload_path) < 0) {
2431 return -1;
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002432 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002433
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002434 ret = minijail_setenv(child_env, kLdPreloadEnvVar, newenv, 1);
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002435 free(newenv);
2436 return ret;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002437#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04002438}
2439
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002440static int setup_pipe(char ***child_env, int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04002441{
2442 int r = pipe(fds);
2443 char fd_buf[11];
2444 if (r)
2445 return r;
2446 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2447 if (r <= 0)
2448 return -EINVAL;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002449 return minijail_setenv(child_env, kFdEnvVar, fd_buf, 1);
Will Drewryf89aef52011-09-16 16:48:57 -05002450}
2451
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002452static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002453{
2454 const char *kFdPath = "/proc/self/fd";
2455
2456 DIR *d = opendir(kFdPath);
2457 struct dirent *dir_entry;
2458
2459 if (d == NULL)
2460 return -1;
2461 int dir_fd = dirfd(d);
2462 while ((dir_entry = readdir(d)) != NULL) {
2463 size_t i;
2464 char *end;
2465 bool should_close = true;
2466 const int fd = strtol(dir_entry->d_name, &end, 10);
2467
2468 if ((*end) != '\0') {
2469 continue;
2470 }
2471 /*
2472 * We might have set up some pipes that we want to share with
2473 * the parent process, and should not be closed.
2474 */
2475 for (i = 0; i < size; ++i) {
2476 if (fd == inheritable_fds[i]) {
2477 should_close = false;
2478 break;
2479 }
2480 }
2481 /* Also avoid closing the directory fd. */
2482 if (should_close && fd != dir_fd)
2483 close(fd);
2484 }
2485 closedir(d);
2486 return 0;
2487}
2488
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002489static int redirect_fds(struct minijail *j)
2490{
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002491 for (size_t i = 0; i < j->preserved_fd_count; i++) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002492 if (dup2(j->preserved_fds[i].parent_fd,
2493 j->preserved_fds[i].child_fd) == -1) {
2494 return -1;
2495 }
2496 }
Mattias Nissler1cf29fb2020-04-20 23:14:03 +02002497 /*
2498 * After all fds have been duped, we are now free to close all parent
2499 * fds that are *not* child fds.
2500 */
2501 for (size_t i = 0; i < j->preserved_fd_count; i++) {
2502 int closeable = true;
2503 for (size_t i2 = 0; i2 < j->preserved_fd_count; i2++) {
2504 closeable &= j->preserved_fds[i].parent_fd !=
2505 j->preserved_fds[i2].child_fd;
2506 }
2507 if (closeable)
2508 close(j->preserved_fds[i].parent_fd);
2509 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002510 return 0;
2511}
2512
Dylan Reidacfb8be2017-08-25 12:56:51 -07002513/*
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002514 * Structure holding resources and state created when running a minijail.
2515 */
2516struct minijail_run_state {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002517 pid_t child_pid;
2518 int pipe_fds[2];
2519 int stdin_fds[2];
2520 int stdout_fds[2];
2521 int stderr_fds[2];
2522 int child_sync_pipe_fds[2];
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002523 char **child_env;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002524};
2525
2526static void minijail_free_run_state(struct minijail_run_state *state)
2527{
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002528 state->child_pid = -1;
2529
2530 int *fd_pairs[] = {state->pipe_fds, state->stdin_fds, state->stdout_fds,
2531 state->stderr_fds, state->child_sync_pipe_fds};
2532 for (size_t i = 0; i < ARRAY_SIZE(fd_pairs); ++i) {
2533 close_and_reset(&fd_pairs[i][0]);
2534 close_and_reset(&fd_pairs[i][1]);
2535 }
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002536
2537 minijail_free_env(state->child_env);
2538 state->child_env = NULL;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002539}
2540
2541/* Set up stdin/stdout/stderr file descriptors in the child. */
2542static void setup_child_std_fds(struct minijail *j,
2543 struct minijail_run_state *state)
2544{
2545 struct {
2546 const char *name;
2547 int from;
2548 int to;
2549 } fd_map[] = {
2550 {"stdin", state->stdin_fds[0], STDIN_FILENO},
2551 {"stdout", state->stdout_fds[1], STDOUT_FILENO},
2552 {"stderr", state->stderr_fds[1], STDERR_FILENO},
2553 };
2554
2555 for (size_t i = 0; i < ARRAY_SIZE(fd_map); ++i) {
2556 if (fd_map[i].from != -1) {
2557 if (dup2(fd_map[i].from, fd_map[i].to) == -1)
2558 die("failed to set up %s pipe", fd_map[i].name);
2559 }
2560 }
2561
2562 /* Close temporary pipe file descriptors. */
2563 int *std_pipes[] = {state->stdin_fds, state->stdout_fds,
2564 state->stderr_fds};
2565 for (size_t i = 0; i < ARRAY_SIZE(std_pipes); ++i) {
2566 close_and_reset(&std_pipes[i][0]);
2567 close_and_reset(&std_pipes[i][1]);
2568 }
2569
2570 /*
2571 * If any of stdin, stdout, or stderr are TTYs, or setsid flag is
2572 * set, create a new session. This prevents the jailed process from
2573 * using the TIOCSTI ioctl to push characters into the parent process
2574 * terminal's input buffer, therefore escaping the jail.
2575 *
2576 * Since it has just forked, the child will not be a process group
2577 * leader, and this call to setsid() should always succeed.
2578 */
2579 if (j->flags.setsid || isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2580 isatty(STDERR_FILENO)) {
2581 if (setsid() < 0) {
2582 pdie("setsid() failed");
2583 }
2584 }
2585}
2586
2587/*
Dylan Reidacfb8be2017-08-25 12:56:51 -07002588 * Structure that specifies how to start a minijail.
2589 *
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002590 * filename - The program to exec in the child. Required if |exec_in_child| = 1.
2591 * argv - Arguments for the child program. Required if |exec_in_child| = 1.
2592 * envp - Environment for the child program. Available if |exec_in_child| = 1.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002593 * use_preload - If true use LD_PRELOAD.
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002594 * exec_in_child - If true, run |filename|. Otherwise, the child will return to
Dylan Reid0412dcc2017-08-24 11:33:15 -07002595 * the caller.
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002596 * pstdin_fd - Filled with stdin pipe if non-NULL.
2597 * pstdout_fd - Filled with stdout pipe if non-NULL.
2598 * pstderr_fd - Filled with stderr pipe if non-NULL.
2599 * pchild_pid - Filled with the pid of the child process if non-NULL.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002600 */
2601struct minijail_run_config {
2602 const char *filename;
2603 char *const *argv;
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002604 char *const *envp;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002605 int use_preload;
Dylan Reid0412dcc2017-08-24 11:33:15 -07002606 int exec_in_child;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002607 int *pstdin_fd;
2608 int *pstdout_fd;
2609 int *pstderr_fd;
2610 pid_t *pchild_pid;
2611};
2612
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002613static int
2614minijail_run_config_internal(struct minijail *j,
2615 const struct minijail_run_config *config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002616
Will Drewry6ac91122011-10-21 16:38:58 -05002617int API minijail_run(struct minijail *j, const char *filename,
2618 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002619{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002620 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002621 .filename = filename,
2622 .argv = argv,
2623 .envp = NULL,
2624 .use_preload = true,
2625 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002626 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002627 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002628}
2629
2630int API minijail_run_pid(struct minijail *j, const char *filename,
2631 char *const argv[], pid_t *pchild_pid)
2632{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002633 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002634 .filename = filename,
2635 .argv = argv,
2636 .envp = NULL,
2637 .use_preload = true,
2638 .exec_in_child = true,
2639 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002640 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002641 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002642}
2643
2644int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002645 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002646{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002647 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002648 .filename = filename,
2649 .argv = argv,
2650 .envp = NULL,
2651 .use_preload = true,
2652 .exec_in_child = true,
2653 .pstdin_fd = pstdin_fd,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002654 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002655 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002656}
2657
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002658int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002659 char *const argv[], pid_t *pchild_pid,
2660 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002661{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002662 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002663 .filename = filename,
2664 .argv = argv,
2665 .envp = NULL,
2666 .use_preload = true,
2667 .exec_in_child = true,
2668 .pstdin_fd = pstdin_fd,
2669 .pstdout_fd = pstdout_fd,
2670 .pstderr_fd = pstderr_fd,
2671 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002672 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002673 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002674}
2675
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002676int API minijail_run_env_pid_pipes(struct minijail *j, const char *filename,
2677 char *const argv[], char *const envp[],
2678 pid_t *pchild_pid, int *pstdin_fd,
2679 int *pstdout_fd, int *pstderr_fd)
2680{
2681 struct minijail_run_config config = {
2682 .filename = filename,
2683 .argv = argv,
2684 .envp = envp,
2685 .use_preload = true,
2686 .exec_in_child = true,
2687 .pstdin_fd = pstdin_fd,
2688 .pstdout_fd = pstdout_fd,
2689 .pstderr_fd = pstderr_fd,
2690 .pchild_pid = pchild_pid,
2691 };
2692 return minijail_run_config_internal(j, &config);
2693}
2694
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002695int API minijail_run_no_preload(struct minijail *j, const char *filename,
2696 char *const argv[])
2697{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002698 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002699 .filename = filename,
2700 .argv = argv,
2701 .envp = NULL,
2702 .use_preload = false,
2703 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002704 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002705 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002706}
2707
Samuel Tan63187f42015-10-16 13:01:53 -07002708int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002709 const char *filename,
2710 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002711 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002712 int *pstdin_fd,
2713 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002714 int *pstderr_fd)
2715{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002716 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002717 .filename = filename,
2718 .argv = argv,
2719 .envp = NULL,
2720 .use_preload = false,
2721 .exec_in_child = true,
2722 .pstdin_fd = pstdin_fd,
2723 .pstdout_fd = pstdout_fd,
2724 .pstderr_fd = pstderr_fd,
2725 .pchild_pid = pchild_pid,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002726 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002727 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002728}
2729
2730int API minijail_run_env_pid_pipes_no_preload(struct minijail *j,
2731 const char *filename,
2732 char *const argv[],
2733 char *const envp[],
2734 pid_t *pchild_pid, int *pstdin_fd,
2735 int *pstdout_fd, int *pstderr_fd)
2736{
2737 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002738 .filename = filename,
2739 .argv = argv,
2740 .envp = envp,
2741 .use_preload = false,
2742 .exec_in_child = true,
2743 .pstdin_fd = pstdin_fd,
2744 .pstdout_fd = pstdout_fd,
2745 .pstderr_fd = pstderr_fd,
2746 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002747 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002748 return minijail_run_config_internal(j, &config);
Samuel Tan63187f42015-10-16 13:01:53 -07002749}
2750
Dylan Reid0412dcc2017-08-24 11:33:15 -07002751pid_t API minijail_fork(struct minijail *j)
2752{
2753 struct minijail_run_config config = {};
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002754 return minijail_run_config_internal(j, &config);
Dylan Reid0412dcc2017-08-24 11:33:15 -07002755}
2756
Dylan Reid18c49c82017-08-25 14:52:27 -07002757static int minijail_run_internal(struct minijail *j,
2758 const struct minijail_run_config *config,
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002759 struct minijail_run_state *state_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002760{
Dylan Reidce5b55e2016-01-13 11:04:16 -08002761 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002762 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002763 /* We need to remember this across the minijail_preexec() call. */
2764 int pid_namespace = j->flags.pids;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -07002765 /*
2766 * Create an init process if we are entering a pid namespace, unless the
2767 * user has explicitly opted out by calling minijail_run_as_init().
2768 */
2769 int do_init = j->flags.do_init && !j->flags.run_as_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002770 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002771
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002772 if (use_preload) {
Dylan Reid0412dcc2017-08-24 11:33:15 -07002773 if (j->hooks_head != NULL)
2774 die("Minijail hooks are not supported with LD_PRELOAD");
2775 if (!config->exec_in_child)
2776 die("minijail_fork is not supported with LD_PRELOAD");
2777
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002778 /*
2779 * Before we fork(2) and execve(2) the child process, we need
2780 * to open a pipe(2) to send the minijail configuration over.
2781 */
2782 state_out->child_env =
2783 minijail_copy_env(config->envp ? config->envp : environ);
2784 if (!state_out->child_env)
2785 return ENOMEM;
2786 if (setup_preload(j, &state_out->child_env) ||
2787 setup_pipe(&state_out->child_env, state_out->pipe_fds))
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002788 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002789 }
Will Drewryf89aef52011-09-16 16:48:57 -05002790
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002791 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002792 if (j->flags.use_caps && j->caps != 0 &&
2793 !j->flags.set_ambient_caps) {
2794 die("non-empty, non-ambient capabilities are not "
2795 "supported without LD_PRELOAD");
2796 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002797 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002798
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002799 /* Create pipes for stdin/stdout/stderr as requested by caller. */
2800 struct {
2801 bool requested;
2802 int *pipe_fds;
2803 } pipe_fd_req[] = {
2804 {config->pstdin_fd != NULL, state_out->stdin_fds},
2805 {config->pstdout_fd != NULL, state_out->stdout_fds},
2806 {config->pstderr_fd != NULL, state_out->stderr_fds},
2807 };
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002808
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002809 for (size_t i = 0; i < ARRAY_SIZE(pipe_fd_req); ++i) {
2810 if (pipe_fd_req[i].requested &&
2811 pipe(pipe_fd_req[i].pipe_fds) == -1)
2812 return EFAULT;
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002813 }
2814
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002815 /*
François Degros664eba72019-11-05 13:18:24 +11002816 * If the parent process needs to configure the child's runtime
2817 * environment after forking, create a pipe(2) to block the child until
2818 * configuration is done.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002819 */
Daniel Erat2d69add2019-04-23 20:58:53 -07002820 if (j->flags.forward_signals || j->flags.pid_file || j->flags.cgroups ||
2821 j->rlimit_count || j->flags.userns) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002822 sync_child = 1;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002823 if (pipe(state_out->child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002824 return -EFAULT;
2825 }
2826
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002827 /*
2828 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002829 *
2830 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2831 *
2832 * In multithreaded programs, there are a bunch of locks inside libc,
2833 * some of which may be held by other threads at the time that we call
2834 * minijail_run_pid(). If we call fork(), glibc does its level best to
2835 * ensure that we hold all of these locks before it calls clone()
2836 * internally and drop them after clone() returns, but when we call
2837 * sys_clone(2) directly, all that gets bypassed and we end up with a
2838 * child address space where some of libc's important locks are held by
2839 * other threads (which did not get cloned, and hence will never release
2840 * those locks). This is okay so long as we call exec() immediately
2841 * after, but a bunch of seemingly-innocent libc functions like setenv()
2842 * take locks.
2843 *
2844 * Hence, only call sys_clone() if we need to, in order to get at pid
2845 * namespacing. If we follow this path, the child's address space might
2846 * have broken locks; you may only call functions that do not acquire
2847 * any locks.
2848 *
2849 * Unfortunately, fork() acquires every lock it can get its hands on, as
2850 * previously detailed, so this function is highly likely to deadlock
2851 * later on (see "deadlock here") if we're multithreaded.
2852 *
2853 * We might hack around this by having the clone()d child (init of the
2854 * pid namespace) return directly, rather than leaving the clone()d
2855 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002856 * its fork()ed child return in turn), but that process would be
2857 * crippled with its libc locks potentially broken. We might try
2858 * fork()ing in the parent before we clone() to ensure that we own all
2859 * the locks, but then we have to have the forked child hanging around
2860 * consuming resources (and possibly having file descriptors / shared
2861 * memory regions / etc attached). We'd need to keep the child around to
2862 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002863 *
2864 * TODO(ellyjones): figure out if the "forked child hanging around"
2865 * problem is fixable or not. It would be nice if we worked in this
2866 * case.
2867 */
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002868 pid_t child_pid;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002869 if (pid_namespace) {
François Degros94619842019-11-08 16:37:55 +11002870 unsigned long clone_flags = CLONE_NEWPID | SIGCHLD;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002871 if (j->flags.userns)
2872 clone_flags |= CLONE_NEWUSER;
François Degros94619842019-11-08 16:37:55 +11002873
2874 child_pid = syscall(SYS_clone, clone_flags, NULL, 0L, 0L, 0L);
2875
2876 if (child_pid < 0) {
2877 if (errno == EPERM)
2878 pdie("clone(CLONE_NEWPID | ...) failed with EPERM; "
2879 "is this process missing CAP_SYS_ADMIN?");
2880 pdie("clone(CLONE_NEWPID | ...) failed");
2881 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002882 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002883 child_pid = fork();
2884
François Degros94619842019-11-08 16:37:55 +11002885 if (child_pid < 0)
2886 pdie("fork failed");
Elly Jonese1749eb2011-10-07 13:54:59 -04002887 }
Will Drewryf89aef52011-09-16 16:48:57 -05002888
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002889 state_out->child_pid = child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04002890 if (child_pid) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002891 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002892
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002893 if (j->flags.forward_signals) {
2894 forward_pid = child_pid;
2895 install_signal_handlers();
2896 }
2897
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002898 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002899 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002900
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002901 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002902 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002903
Dylan Reid0f72ef42017-06-06 15:42:49 -07002904 if (j->rlimit_count)
2905 set_rlimits_or_die(j);
2906
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002907 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002908 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002909
Mike Frysinger902a4492018-12-27 05:22:56 -05002910 if (j->flags.enter_vfs)
2911 close(j->mountns_fd);
2912
2913 if (j->flags.enter_net)
2914 close(j->netns_fd);
2915
Dylan Reidce5b55e2016-01-13 11:04:16 -08002916 if (sync_child)
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002917 parent_setup_complete(state_out->child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002918
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002919 if (use_preload) {
François Degros664eba72019-11-05 13:18:24 +11002920 /*
2921 * Add SIGPIPE to the signal mask to avoid getting
2922 * killed if the child process finishes or closes its
2923 * end of the pipe prematurely.
2924 *
2925 * TODO(crbug.com/1022170): Use pthread_sigmask instead
2926 * of sigprocmask if Minijail is used in multithreaded
2927 * programs.
2928 */
2929 sigset_t to_block, to_restore;
2930 if (sigemptyset(&to_block) < 0)
2931 pdie("sigemptyset failed");
2932 if (sigaddset(&to_block, SIGPIPE) < 0)
2933 pdie("sigaddset failed");
2934 if (sigprocmask(SIG_BLOCK, &to_block, &to_restore) < 0)
2935 pdie("sigprocmask failed");
2936
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002937 /* Send marshalled minijail. */
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002938 close_and_reset(&state_out->pipe_fds[0]);
2939 ret = minijail_to_fd(j, state_out->pipe_fds[1]);
2940 close_and_reset(&state_out->pipe_fds[1]);
François Degros664eba72019-11-05 13:18:24 +11002941
2942 /* Accept any pending SIGPIPE. */
2943 while (true) {
2944 const struct timespec zero_time = {0, 0};
2945 const int sig = sigtimedwait(&to_block, NULL, &zero_time);
2946 if (sig < 0) {
2947 if (errno != EINTR)
2948 break;
2949 } else {
2950 if (sig != SIGPIPE)
2951 die("unexpected signal %d", sig);
2952 }
2953 }
2954
2955 /* Restore the signal mask to its original state. */
2956 if (sigprocmask(SIG_SETMASK, &to_restore, NULL) < 0)
2957 pdie("sigprocmask failed");
2958
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002959 if (ret) {
François Degros664eba72019-11-05 13:18:24 +11002960 warn("failed to send marshalled minijail: %s",
2961 strerror(-ret));
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002962 kill(j->initpid, SIGKILL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002963 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002964 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002965
Elly Jonese1749eb2011-10-07 13:54:59 -04002966 return 0;
2967 }
Ben Chan541c7e52011-08-26 14:55:53 -07002968
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002969 /* Child process. */
Peter Qiu2860c462015-12-16 15:13:06 -08002970 if (j->flags.reset_signal_mask) {
2971 sigset_t signal_mask;
2972 if (sigemptyset(&signal_mask) != 0)
2973 pdie("sigemptyset failed");
2974 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2975 pdie("sigprocmask failed");
2976 }
2977
Luis Hector Chaveza27118a2018-04-04 08:18:01 -07002978 if (j->flags.reset_signal_handlers) {
2979 int signum;
2980 for (signum = 0; signum <= SIGRTMAX; signum++) {
2981 /*
2982 * Ignore EINVAL since some signal numbers in the range
2983 * might not be valid.
2984 */
2985 if (signal(signum, SIG_DFL) == SIG_ERR &&
2986 errno != EINVAL) {
2987 pdie("failed to reset signal %d disposition",
2988 signum);
2989 }
2990 }
2991 }
2992
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002993 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002994 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002995 int inheritable_fds[kMaxInheritableFdsSize];
2996 size_t size = 0;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002997
2998 int *pipe_fds[] = {
2999 state_out->pipe_fds, state_out->child_sync_pipe_fds,
3000 state_out->stdin_fds, state_out->stdout_fds,
3001 state_out->stderr_fds,
3002 };
3003
3004 for (size_t i = 0; i < ARRAY_SIZE(pipe_fds); ++i) {
3005 if (pipe_fds[i][0] != -1) {
3006 inheritable_fds[size++] = pipe_fds[i][0];
3007 }
3008 if (pipe_fds[i][1] != -1) {
3009 inheritable_fds[size++] = pipe_fds[i][1];
3010 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003011 }
Jorge Lucangeli Obes2337f802019-07-18 14:46:03 -04003012
Jorge Lucangeli Obescf3bbea2019-07-24 09:06:40 -04003013 /*
3014 * Preserve namespace file descriptors over the close_open_fds()
3015 * call. These are closed in minijail_enter() so they won't leak
3016 * into the child process.
3017 */
Jorge Lucangeli Obes2337f802019-07-18 14:46:03 -04003018 if (j->flags.enter_vfs)
3019 minijail_preserve_fd(j, j->mountns_fd, j->mountns_fd);
3020 if (j->flags.enter_net)
3021 minijail_preserve_fd(j, j->netns_fd, j->netns_fd);
3022
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003023 for (size_t i = 0; i < j->preserved_fd_count; i++) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07003024 /*
3025 * Preserve all parent_fds. They will be dup2(2)-ed in
3026 * the child later.
3027 */
3028 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
3029 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003030
3031 if (close_open_fds(inheritable_fds, size) < 0)
3032 die("failed to close open file descriptors");
3033 }
3034
Luis Hector Chavez1617f632017-08-01 18:32:30 -07003035 if (redirect_fds(j))
3036 die("failed to set up fd redirections");
3037
Dylan Reidce5b55e2016-01-13 11:04:16 -08003038 if (sync_child)
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003039 wait_for_parent_setup(state_out->child_sync_pipe_fds);
Dylan Reidce5b55e2016-01-13 11:04:16 -08003040
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003041 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08003042 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003043
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003044 setup_child_std_fds(j, state_out);
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05003045
Dylan Reid791f5772015-09-14 20:02:42 -07003046 /* If running an init program, let it decide when/how to mount /proc. */
3047 if (pid_namespace && !do_init)
3048 j->flags.remount_proc_ro = 0;
3049
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003050 if (use_preload) {
3051 /* Strip out flags that cannot be inherited across execve(2). */
3052 minijail_preexec(j);
3053 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003054 /*
3055 * If not using LD_PRELOAD, do all jailing before execve(2).
3056 * Note that PID namespaces can only be entered on fork(2),
3057 * so that flag is still cleared.
3058 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003059 j->flags.pids = 0;
3060 }
Dylan Reid0412dcc2017-08-24 11:33:15 -07003061
3062 /*
3063 * Jail this process.
3064 * If forking, return.
3065 * If not, execve(2) the target.
3066 */
Elly Jonese1749eb2011-10-07 13:54:59 -04003067 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003068
Dylan Reid0412dcc2017-08-24 11:33:15 -07003069 if (config->exec_in_child && pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05003070 /*
3071 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003072 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003073 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003074 * a child to actually run the program. If |do_init == 0|, we
3075 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04003076 *
3077 * If we're multithreaded, we'll probably deadlock here. See
3078 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04003079 */
3080 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003081 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04003082 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003083 } else if (child_pid > 0) {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003084 minijail_free_run_state(state_out);
3085
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04003086 /*
3087 * Best effort. Don't bother checking the return value.
3088 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003089 prctl(PR_SET_NAME, "minijail-init");
3090 init(child_pid); /* Never returns. */
3091 }
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003092 state_out->child_pid = child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04003093 }
Elly Jonescd7a9042011-07-22 13:56:51 -04003094
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07003095 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
3096
Dylan Reid0412dcc2017-08-24 11:33:15 -07003097 if (!config->exec_in_child)
3098 return 0;
3099
Elly Jonesdd3e8512012-01-23 15:13:38 -05003100 /*
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003101 * We're going to execve(), so make sure any remaining resources are
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003102 * freed. Exceptions are:
3103 * 1. The child environment. No need to worry about freeing it since
3104 * execve reinitializes the heap anyways.
3105 * 2. The read side of the LD_PRELOAD pipe, which we need to hand down
3106 * into the target in which the preloaded code will read from it and
3107 * then close it.
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003108 */
3109 state_out->pipe_fds[0] = -1;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003110 char *const *child_env = state_out->child_env;
3111 state_out->child_env = NULL;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003112 minijail_free_run_state(state_out);
3113
3114 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003115 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04003116 * calling process
3117 * -> execve()-ing process
3118 * If we are:
3119 * calling process
3120 * -> init()-ing process
3121 * -> execve()-ing process
3122 */
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003123 if (!child_env)
3124 child_env = config->envp ? config->envp : environ;
François Degros08b10f72019-10-09 12:44:05 +11003125 execve(config->filename, config->argv, child_env);
3126
3127 ret = (errno == ENOENT ? MINIJAIL_ERR_NO_COMMAND : MINIJAIL_ERR_NO_ACCESS);
3128 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003129 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04003130}
3131
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003132static int
3133minijail_run_config_internal(struct minijail *j,
3134 const struct minijail_run_config *config)
3135{
3136 struct minijail_run_state state = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003137 .child_pid = -1,
3138 .pipe_fds = {-1, -1},
3139 .stdin_fds = {-1, -1},
3140 .stdout_fds = {-1, -1},
3141 .stderr_fds = {-1, -1},
3142 .child_sync_pipe_fds = {-1, -1},
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003143 .child_env = NULL,
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003144 };
3145 int ret = minijail_run_internal(j, config, &state);
3146
3147 if (ret == 0) {
3148 if (config->pchild_pid)
3149 *config->pchild_pid = state.child_pid;
3150
3151 /* Grab stdin/stdout/stderr descriptors requested by caller. */
3152 struct {
3153 int *pfd;
3154 int *psrc;
3155 } fd_map[] = {
3156 {config->pstdin_fd, &state.stdin_fds[1]},
3157 {config->pstdout_fd, &state.stdout_fds[0]},
3158 {config->pstderr_fd, &state.stderr_fds[0]},
3159 };
3160
3161 for (size_t i = 0; i < ARRAY_SIZE(fd_map); ++i) {
3162 if (fd_map[i].pfd) {
3163 *fd_map[i].pfd = *fd_map[i].psrc;
3164 *fd_map[i].psrc = -1;
3165 }
3166 }
3167
3168 if (!config->exec_in_child)
3169 ret = state.child_pid;
3170 }
3171
3172 minijail_free_run_state(&state);
3173
3174 return ret;
3175}
3176
Will Drewry6ac91122011-10-21 16:38:58 -05003177int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003178{
François Degros47e63352019-10-01 13:01:53 +10003179 if (j->initpid <= 0)
3180 return -ECHILD;
3181
Elly Jonese1749eb2011-10-07 13:54:59 -04003182 if (kill(j->initpid, SIGTERM))
3183 return -errno;
François Degros47e63352019-10-01 13:01:53 +10003184
3185 return minijail_wait(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003186}
3187
Will Drewry6ac91122011-10-21 16:38:58 -05003188int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003189{
François Degros08b10f72019-10-09 12:44:05 +11003190 if (j->initpid <= 0)
3191 return -ECHILD;
3192
Elly Jonese1749eb2011-10-07 13:54:59 -04003193 int st;
François Degros627deba2019-10-01 12:48:25 +10003194 while (true) {
3195 const int ret = waitpid(j->initpid, &st, 0);
3196 if (ret >= 0)
3197 break;
3198 if (errno != EINTR)
3199 return -errno;
3200 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003201
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003202 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003203 int error_status = st;
3204 if (WIFSIGNALED(st)) {
3205 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07003206 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003207 j->initpid, signum);
3208 /*
3209 * We return MINIJAIL_ERR_JAIL if the process received
3210 * SIGSYS, which happens when a syscall is blocked by
3211 * seccomp filters.
3212 * If not, we do what bash(1) does:
3213 * $? = 128 + signum
3214 */
3215 if (signum == SIGSYS) {
3216 error_status = MINIJAIL_ERR_JAIL;
3217 } else {
François Degros08b10f72019-10-09 12:44:05 +11003218 error_status = MINIJAIL_ERR_SIG_BASE + signum;
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003219 }
3220 }
3221 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003222 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003223
3224 int exit_status = WEXITSTATUS(st);
3225 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07003226 info("child process %d exited with status %d",
3227 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003228
3229 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04003230}
3231
Will Drewry6ac91122011-10-21 16:38:58 -05003232void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003233{
Dylan Reid605ce7f2016-01-19 19:21:00 -08003234 size_t i;
3235
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07003236 if (j->filter_prog) {
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08003237 free(j->filter_prog->filter);
3238 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04003239 }
Mike Frysingerac08a682017-10-10 02:04:50 -04003240 free_mounts_list(j);
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07003241 while (j->hooks_head) {
3242 struct hook *c = j->hooks_head;
3243 j->hooks_head = c->next;
3244 free(c);
3245 }
3246 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04003247 if (j->user)
3248 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08003249 if (j->suppl_gid_list)
3250 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05003251 if (j->chrootdir)
3252 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04003253 if (j->pid_file_path)
3254 free(j->pid_file_path);
3255 if (j->uidmap)
3256 free(j->uidmap);
3257 if (j->gidmap)
3258 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04003259 if (j->hostname)
3260 free(j->hostname);
Luis Hector Chavez9acba452018-10-11 10:13:25 -07003261 if (j->preload_path)
3262 free(j->preload_path);
Andrew Brestickereac28942015-11-11 16:04:46 -08003263 if (j->alt_syscall_table)
3264 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08003265 for (i = 0; i < j->cgroup_count; ++i)
3266 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04003267 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003268}
Luis Hector Chavez114a9302017-09-05 20:36:58 -07003269
3270void API minijail_log_to_fd(int fd, int min_priority)
3271{
3272 init_logging(LOG_TO_FD, fd, min_priority);
3273}