blob: 92f16d9e5068d303abd6017ddb61f164bc80c23c [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
Mike Frysinger33ffef32017-01-13 19:53:19 -0500727void API minijail_mount_dev(struct minijail *j)
728{
729 j->flags.mount_dev = 1;
730}
731
Lee Campbell11af0622014-05-22 12:36:04 -0700732void API minijail_mount_tmp(struct minijail *j)
733{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100734 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
735}
736
737void API minijail_mount_tmp_size(struct minijail *j, size_t size)
738{
739 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700740 j->flags.mount_tmp = 1;
741}
742
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800743int API minijail_write_pid_file(struct minijail *j, const char *path)
744{
745 j->pid_file_path = strdup(path);
746 if (!j->pid_file_path)
747 return -ENOMEM;
748 j->flags.pid_file = 1;
749 return 0;
750}
751
Dylan Reid605ce7f2016-01-19 19:21:00 -0800752int API minijail_add_to_cgroup(struct minijail *j, const char *path)
753{
754 if (j->cgroup_count >= MAX_CGROUPS)
755 return -ENOMEM;
756 j->cgroups[j->cgroup_count] = strdup(path);
757 if (!j->cgroups[j->cgroup_count])
758 return -ENOMEM;
759 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800760 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800761 return 0;
762}
763
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800764int API minijail_rlimit(struct minijail *j, int type, rlim_t cur, rlim_t max)
Dylan Reid0f72ef42017-06-06 15:42:49 -0700765{
766 size_t i;
767
768 if (j->rlimit_count >= MAX_RLIMITS)
769 return -ENOMEM;
770 /* It's an error if the caller sets the same rlimit multiple times. */
771 for (i = 0; i < j->rlimit_count; i++) {
772 if (j->rlimits[i].type == type)
773 return -EEXIST;
774 }
775
776 j->rlimits[j->rlimit_count].type = type;
777 j->rlimits[j->rlimit_count].cur = cur;
778 j->rlimits[j->rlimit_count].max = max;
779 j->rlimit_count++;
780 return 0;
781}
782
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400783int API minijail_forward_signals(struct minijail *j)
784{
785 j->flags.forward_signals = 1;
786 return 0;
787}
788
Xiyuan Xia9b41e652019-05-23 11:03:04 -0700789int API minijail_create_session(struct minijail *j) {
790 j->flags.setsid = 1;
791 return 0;
792}
793
Dylan Reid81e23972016-05-18 14:06:35 -0700794int API minijail_mount_with_data(struct minijail *j, const char *src,
795 const char *dest, const char *type,
796 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700797{
Dylan Reid648b2202015-10-23 00:50:00 -0700798 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400799
800 if (*dest != '/')
801 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700802 m = calloc(1, sizeof(*m));
803 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400804 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700805 m->dest = strdup(dest);
806 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400807 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700808 m->src = strdup(src);
809 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400810 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700811 m->type = strdup(type);
812 if (!m->type)
813 goto error;
Mike Frysingerb7803c82018-08-23 15:43:15 -0400814
815 if (!data || !data[0]) {
816 /*
817 * Set up secure defaults for certain filesystems. Adding this
818 * fs-specific logic here kind of sucks, but considering how
819 * people use these in practice, it's probably OK. If they want
820 * the kernel defaults, they can pass data="" instead of NULL.
821 */
822 if (!strcmp(type, "tmpfs")) {
823 /* tmpfs defaults to mode=1777 and size=50%. */
824 data = "mode=0755,size=10M";
825 }
826 }
Dylan Reid81e23972016-05-18 14:06:35 -0700827 if (data) {
828 m->data = strdup(data);
829 if (!m->data)
830 goto error;
831 m->has_data = 1;
832 }
Mike Frysingercb8674d2018-08-12 00:53:35 -0400833
834 /* If they don't specify any flags, default to secure ones. */
835 if (flags == 0)
836 flags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Dylan Reid648b2202015-10-23 00:50:00 -0700837 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400838
Elly Jonesdd3e8512012-01-23 15:13:38 -0500839 /*
Jorge Lucangeli Obes0a0514c2020-01-03 11:18:32 -0500840 * Unless asked to enter an existing namespace, force vfs namespacing
841 * so the mounts don't leak out into the containing vfs namespace.
842 * If Minijail is being asked to enter the root vfs namespace this will
843 * leak mounts, but it's unlikely that the user would ask to do that by
844 * mistake.
Elly Jones51a5b6c2011-10-12 19:09:26 -0400845 */
Jorge Lucangeli Obes0a0514c2020-01-03 11:18:32 -0500846 if (!j->flags.enter_vfs)
847 minijail_namespace_vfs(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400848
Dylan Reid648b2202015-10-23 00:50:00 -0700849 if (j->mounts_tail)
850 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400851 else
Dylan Reid648b2202015-10-23 00:50:00 -0700852 j->mounts_head = m;
853 j->mounts_tail = m;
854 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400855
856 return 0;
857
858error:
Dylan Reid81e23972016-05-18 14:06:35 -0700859 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700860 free(m->src);
861 free(m->dest);
862 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400863 return -ENOMEM;
864}
865
Dylan Reid81e23972016-05-18 14:06:35 -0700866int API minijail_mount(struct minijail *j, const char *src, const char *dest,
867 const char *type, unsigned long flags)
868{
869 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
870}
871
Dylan Reid648b2202015-10-23 00:50:00 -0700872int API minijail_bind(struct minijail *j, const char *src, const char *dest,
873 int writeable)
874{
875 unsigned long flags = MS_BIND;
876
877 if (!writeable)
878 flags |= MS_RDONLY;
879
880 return minijail_mount(j, src, dest, "", flags);
881}
882
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700883int API minijail_add_hook(struct minijail *j, minijail_hook_t hook,
884 void *payload, minijail_hook_event_t event)
885{
886 struct hook *c;
887
888 if (hook == NULL)
889 return -EINVAL;
890 if (event >= MINIJAIL_HOOK_EVENT_MAX)
891 return -EINVAL;
892 c = calloc(1, sizeof(*c));
893 if (!c)
894 return -ENOMEM;
895
896 c->hook = hook;
897 c->payload = payload;
898 c->event = event;
899
900 if (j->hooks_tail)
901 j->hooks_tail->next = c;
902 else
903 j->hooks_head = c;
904 j->hooks_tail = c;
905
906 return 0;
907}
908
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700909int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd)
910{
911 if (parent_fd < 0 || child_fd < 0)
912 return -EINVAL;
913 if (j->preserved_fd_count >= MAX_PRESERVED_FDS)
914 return -ENOMEM;
915 j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd;
916 j->preserved_fds[j->preserved_fd_count].child_fd = child_fd;
917 j->preserved_fd_count++;
918 return 0;
919}
920
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700921int API minijail_set_preload_path(struct minijail *j, const char *preload_path)
922{
923 if (j->preload_path)
924 return -EINVAL;
925 j->preload_path = strdup(preload_path);
926 if (!j->preload_path)
927 return -ENOMEM;
928 return 0;
929}
930
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400931static void clear_seccomp_options(struct minijail *j)
932{
933 j->flags.seccomp_filter = 0;
934 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400935 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400936 j->filter_len = 0;
937 j->filter_prog = NULL;
938 j->flags.no_new_privs = 0;
939}
940
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700941static int seccomp_should_use_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400942{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400943 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400944 /*
945 * |errno| will be set to EINVAL when seccomp has not been
946 * compiled into the kernel. On certain platforms and kernel
947 * versions this is not a fatal failure. In that case, and only
948 * in that case, disable seccomp and skip loading the filters.
949 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400950 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400951 warn("not loading seccomp filters, seccomp filter not "
952 "supported");
953 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400954 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700955 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400956 /*
957 * If |errno| != EINVAL or seccomp_can_softfail() is false,
958 * we can proceed. Worst case scenario minijail_enter() will
959 * abort() if seccomp fails.
960 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700961 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400962 if (j->flags.seccomp_filter_tsync) {
963 /* Are the seccomp(2) syscall and the TSYNC option supported? */
964 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
965 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
966 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400967 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
968 warn("seccomp(2) syscall not supported");
969 clear_seccomp_options(j);
970 return 0;
971 } else if (saved_errno == EINVAL &&
972 seccomp_can_softfail()) {
973 warn(
974 "seccomp filter thread sync not supported");
975 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400976 return 0;
977 }
978 /*
979 * Similar logic here. If seccomp_can_softfail() is
980 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
981 * we can proceed. Worst case scenario minijail_enter()
982 * will abort() if seccomp or TSYNC fail.
983 */
984 }
985 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400986 return 1;
987}
988
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700989static int set_seccomp_filters_internal(struct minijail *j,
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400990 const struct sock_fprog *filter,
991 bool owned)
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700992{
993 struct sock_fprog *fprog;
994
995 if (owned) {
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400996 /*
997 * If |owned| is true, it's OK to cast away the const-ness since
998 * we'll own the pointer going forward.
999 */
1000 fprog = (struct sock_fprog *)filter;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001001 } else {
1002 fprog = malloc(sizeof(struct sock_fprog));
1003 if (!fprog)
1004 return -ENOMEM;
1005 fprog->len = filter->len;
1006 fprog->filter = malloc(sizeof(struct sock_filter) * fprog->len);
1007 if (!fprog->filter) {
1008 free(fprog);
1009 return -ENOMEM;
1010 }
1011 memcpy(fprog->filter, filter->filter,
1012 sizeof(struct sock_filter) * fprog->len);
1013 }
1014
1015 if (j->filter_prog) {
1016 free(j->filter_prog->filter);
1017 free(j->filter_prog);
1018 }
1019
1020 j->filter_len = fprog->len;
1021 j->filter_prog = fprog;
1022 return 0;
1023}
1024
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001025static int parse_seccomp_filters(struct minijail *j, const char *filename,
1026 FILE *policy_file)
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001027{
1028 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001029 if (!fprog)
1030 return -ENOMEM;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001031
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001032 struct filter_options filteropts;
1033
1034 /*
1035 * Figure out filter options.
1036 * Allow logging?
1037 */
1038 filteropts.allow_logging =
1039 debug_logging_allowed() && j->flags.seccomp_filter_logging;
1040
1041 /* What to do on a blocked system call? */
1042 if (filteropts.allow_logging) {
1043 if (seccomp_ret_log_available())
1044 filteropts.action = ACTION_RET_LOG;
1045 else
1046 filteropts.action = ACTION_RET_TRAP;
1047 } else {
Jorge Lucangeli Obesd23ad7922020-10-13 10:26:40 -04001048 if (j->flags.seccomp_filter_tsync) {
1049 if (seccomp_ret_kill_process_available()) {
1050 filteropts.action = ACTION_RET_KILL_PROCESS;
1051 } else {
1052 filteropts.action = ACTION_RET_TRAP;
1053 }
1054 } else {
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001055 filteropts.action = ACTION_RET_KILL;
Jorge Lucangeli Obesd23ad7922020-10-13 10:26:40 -04001056 }
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001057 }
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
Nicole Anderson-Aubcc8cfd2020-11-10 20:33:27 +00001066 /* Whether to fail on duplicate syscalls. */
1067 filteropts.allow_duplicate_syscalls = allow_duplicate_syscalls();
1068
Jorge Lucangeli Obese1a86892019-06-10 16:17:03 -04001069 if (compile_filter(filename, policy_file, fprog, &filteropts)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001070 free(fprog);
1071 return -1;
1072 }
1073
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001074 return set_seccomp_filters_internal(j, fprog, true /* owned */);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001075}
1076
1077void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
1078{
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001079 if (!seccomp_should_use_filters(j))
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001080 return;
1081
Luis Hector Chaveza30a2062018-07-12 21:10:33 -07001082 FILE *file = fopen(path, "re");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001083 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -07001084 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -04001085 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001086
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001087 if (parse_seccomp_filters(j, path, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -07001088 die("failed to compile seccomp filter BPF program in '%s'",
1089 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001090 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001091 fclose(file);
1092}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001093
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001094void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
1095{
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001096 char *fd_path, *path;
1097 FILE *file;
1098
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001099 if (!seccomp_should_use_filters(j))
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001100 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001101
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001102 file = fdopen(fd, "r");
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001103 if (!file) {
1104 pdie("failed to associate stream with fd %d", fd);
1105 }
1106
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001107 if (asprintf(&fd_path, "/proc/self/fd/%d", fd) == -1)
1108 pdie("failed to create path for fd %d", fd);
1109 path = realpath(fd_path, NULL);
1110 if (path == NULL)
1111 pwarn("failed to get path of fd %d", fd);
1112 free(fd_path);
1113
1114 if (parse_seccomp_filters(j, path ? path : "<fd>", file) != 0) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001115 die("failed to compile seccomp filter BPF program from fd %d",
1116 fd);
1117 }
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001118 free(path);
Elly Jonese1749eb2011-10-07 13:54:59 -04001119 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -05001120}
1121
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001122void API minijail_set_seccomp_filters(struct minijail *j,
1123 const struct sock_fprog *filter)
1124{
1125 if (!seccomp_should_use_filters(j))
1126 return;
1127
1128 if (j->flags.seccomp_filter_logging) {
1129 die("minijail_log_seccomp_filter_failures() is incompatible "
1130 "with minijail_set_seccomp_filters()");
1131 }
1132
1133 /*
1134 * set_seccomp_filters_internal() can only fail with ENOMEM.
1135 * Furthermore, since we won't own the incoming filter, it will not be
1136 * modified.
1137 */
1138 if (set_seccomp_filters_internal(j, filter, false /* owned */) < 0) {
1139 die("failed to set seccomp filter");
1140 }
1141}
1142
Andrew Brestickereac28942015-11-11 16:04:46 -08001143int API minijail_use_alt_syscall(struct minijail *j, const char *table)
1144{
1145 j->alt_syscall_table = strdup(table);
1146 if (!j->alt_syscall_table)
1147 return -ENOMEM;
1148 j->flags.alt_syscall = 1;
1149 return 0;
1150}
1151
Will Drewryf89aef52011-09-16 16:48:57 -05001152struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -04001153 size_t available;
1154 size_t total;
1155 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -05001156};
1157
Mike Frysinger0a27ab02020-09-04 16:18:12 -04001158static void marshal_state_init(struct marshal_state *state, char *buf,
1159 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -04001160{
1161 state->available = available;
1162 state->buf = buf;
1163 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001164}
1165
Mike Frysinger0a27ab02020-09-04 16:18:12 -04001166static void marshal_append(struct marshal_state *state, const void *src,
1167 size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -04001168{
1169 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -05001170
Elly Jonese1749eb2011-10-07 13:54:59 -04001171 /* Up to |available| will be written. */
1172 if (copy_len) {
1173 memcpy(state->buf, src, copy_len);
1174 state->buf += copy_len;
1175 state->available -= copy_len;
1176 }
1177 /* |total| will contain the expected length. */
1178 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -05001179}
1180
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001181static void marshal_append_string(struct marshal_state *state, const char *src)
1182{
1183 marshal_append(state, src, strlen(src) + 1);
1184}
1185
Mike Frysinger0a27ab02020-09-04 16:18:12 -04001186static void marshal_mount(struct marshal_state *state,
1187 const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -07001188{
1189 marshal_append(state, m->src, strlen(m->src) + 1);
1190 marshal_append(state, m->dest, strlen(m->dest) + 1);
1191 marshal_append(state, m->type, strlen(m->type) + 1);
1192 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
1193 if (m->has_data)
1194 marshal_append(state, m->data, strlen(m->data) + 1);
1195 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
1196}
1197
Mike Frysinger0a27ab02020-09-04 16:18:12 -04001198static void minijail_marshal_helper(struct marshal_state *state,
1199 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001200{
Dylan Reid648b2202015-10-23 00:50:00 -07001201 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001202 size_t i;
1203
Elly Jonese1749eb2011-10-07 13:54:59 -04001204 marshal_append(state, (char *)j, sizeof(*j));
1205 if (j->user)
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001206 marshal_append_string(state, j->user);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001207 if (j->suppl_gid_list) {
1208 marshal_append(state, j->suppl_gid_list,
1209 j->suppl_gid_count * sizeof(gid_t));
1210 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001211 if (j->chrootdir)
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001212 marshal_append_string(state, j->chrootdir);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001213 if (j->hostname)
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001214 marshal_append_string(state, j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08001215 if (j->alt_syscall_table) {
1216 marshal_append(state, j->alt_syscall_table,
1217 strlen(j->alt_syscall_table) + 1);
1218 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001219 if (j->flags.seccomp_filter && j->filter_prog) {
1220 struct sock_fprog *fp = j->filter_prog;
1221 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001222 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -04001223 }
Dylan Reid648b2202015-10-23 00:50:00 -07001224 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001225 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001226 }
Dylan Reid605ce7f2016-01-19 19:21:00 -08001227 for (i = 0; i < j->cgroup_count; ++i)
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001228 marshal_append_string(state, j->cgroups[i]);
Will Drewryf89aef52011-09-16 16:48:57 -05001229}
1230
Will Drewry6ac91122011-10-21 16:38:58 -05001231size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001232{
1233 struct marshal_state state;
1234 marshal_state_init(&state, NULL, 0);
1235 minijail_marshal_helper(&state, j);
1236 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -05001237}
1238
Elly Jonese1749eb2011-10-07 13:54:59 -04001239int minijail_marshal(const struct minijail *j, char *buf, size_t available)
1240{
1241 struct marshal_state state;
1242 marshal_state_init(&state, buf, available);
1243 minijail_marshal_helper(&state, j);
1244 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -05001245}
1246
Elly Jonese1749eb2011-10-07 13:54:59 -04001247int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
1248{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -08001249 size_t i;
1250 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -05001251 int ret = -EINVAL;
1252
Elly Jonese1749eb2011-10-07 13:54:59 -04001253 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001254 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001255 memcpy((void *)j, serialized, sizeof(*j));
1256 serialized += sizeof(*j);
1257 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001258
Will Drewrybee7ba72011-10-21 20:47:01 -05001259 /* Potentially stale pointers not used as signals. */
Luis Hector Chavez9acba452018-10-11 10:13:25 -07001260 j->preload_path = NULL;
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001261 j->pid_file_path = NULL;
1262 j->uidmap = NULL;
1263 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001264 j->mounts_head = NULL;
1265 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001266 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001267 j->hooks_head = NULL;
1268 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001269
Elly Jonese1749eb2011-10-07 13:54:59 -04001270 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001271 char *user = consumestr(&serialized, &length);
1272 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001273 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001274 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001275 if (!j->user)
1276 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001277 }
Will Drewryf89aef52011-09-16 16:48:57 -05001278
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001279 if (j->suppl_gid_list) { /* stale pointer */
1280 if (j->suppl_gid_count > NGROUPS_MAX) {
1281 goto bad_gid_list;
1282 }
1283 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1284 void *gid_list_bytes =
1285 consumebytes(gid_list_size, &serialized, &length);
1286 if (!gid_list_bytes)
1287 goto bad_gid_list;
1288
1289 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1290 if (!j->suppl_gid_list)
1291 goto bad_gid_list;
1292
1293 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1294 }
1295
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001296 if (j->chrootdir) { /* stale pointer */
1297 char *chrootdir = consumestr(&serialized, &length);
1298 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001299 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001300 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001301 if (!j->chrootdir)
1302 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001303 }
1304
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001305 if (j->hostname) { /* stale pointer */
1306 char *hostname = consumestr(&serialized, &length);
1307 if (!hostname)
1308 goto bad_hostname;
1309 j->hostname = strdup(hostname);
1310 if (!j->hostname)
1311 goto bad_hostname;
1312 }
1313
Andrew Brestickereac28942015-11-11 16:04:46 -08001314 if (j->alt_syscall_table) { /* stale pointer */
1315 char *alt_syscall_table = consumestr(&serialized, &length);
1316 if (!alt_syscall_table)
1317 goto bad_syscall_table;
1318 j->alt_syscall_table = strdup(alt_syscall_table);
1319 if (!j->alt_syscall_table)
1320 goto bad_syscall_table;
1321 }
1322
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001323 if (j->flags.seccomp_filter && j->filter_len > 0) {
1324 size_t ninstrs = j->filter_len;
1325 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1326 ninstrs > USHRT_MAX)
1327 goto bad_filters;
1328
1329 size_t program_len = ninstrs * sizeof(struct sock_filter);
1330 void *program = consumebytes(program_len, &serialized, &length);
1331 if (!program)
1332 goto bad_filters;
1333
1334 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001335 if (!j->filter_prog)
1336 goto bad_filters;
1337
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001338 j->filter_prog->len = ninstrs;
1339 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001340 if (!j->filter_prog->filter)
1341 goto bad_filter_prog_instrs;
1342
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001343 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001344 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001345
Dylan Reid648b2202015-10-23 00:50:00 -07001346 count = j->mounts_count;
1347 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001348 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001349 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001350 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001351 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001352 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001353 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001354 const char *src = consumestr(&serialized, &length);
1355 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001356 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001357 dest = consumestr(&serialized, &length);
1358 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001359 goto bad_mounts;
1360 type = consumestr(&serialized, &length);
1361 if (!type)
1362 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001363 has_data = consumebytes(sizeof(*has_data), &serialized,
1364 &length);
1365 if (!has_data)
1366 goto bad_mounts;
1367 if (*has_data) {
1368 data = consumestr(&serialized, &length);
1369 if (!data)
1370 goto bad_mounts;
1371 }
Dylan Reid648b2202015-10-23 00:50:00 -07001372 flags = consumebytes(sizeof(*flags), &serialized, &length);
1373 if (!flags)
1374 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001375 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001376 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001377 }
1378
Dylan Reid605ce7f2016-01-19 19:21:00 -08001379 count = j->cgroup_count;
1380 j->cgroup_count = 0;
1381 for (i = 0; i < count; ++i) {
1382 char *cgroup = consumestr(&serialized, &length);
1383 if (!cgroup)
1384 goto bad_cgroups;
1385 j->cgroups[i] = strdup(cgroup);
1386 if (!j->cgroups[i])
1387 goto bad_cgroups;
1388 ++j->cgroup_count;
1389 }
1390
Elly Jonese1749eb2011-10-07 13:54:59 -04001391 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001392
Dylan Reid605ce7f2016-01-19 19:21:00 -08001393bad_cgroups:
Mike Frysingerac08a682017-10-10 02:04:50 -04001394 free_mounts_list(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001395 for (i = 0; i < j->cgroup_count; ++i)
1396 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001397bad_mounts:
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001398 if (j->filter_prog && j->filter_prog->filter)
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001399 free(j->filter_prog->filter);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001400bad_filter_prog_instrs:
1401 if (j->filter_prog)
1402 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001403bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001404 if (j->alt_syscall_table)
1405 free(j->alt_syscall_table);
1406bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001407 if (j->chrootdir)
1408 free(j->chrootdir);
1409bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001410 if (j->hostname)
1411 free(j->hostname);
1412bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001413 if (j->suppl_gid_list)
1414 free(j->suppl_gid_list);
1415bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001416 if (j->user)
1417 free(j->user);
1418clear_pointers:
1419 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001420 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001421 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001422 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001423 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001424 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001425out:
1426 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001427}
1428
Mike Frysinger33ffef32017-01-13 19:53:19 -05001429struct dev_spec {
1430 const char *name;
1431 mode_t mode;
1432 dev_t major, minor;
1433};
1434
1435static const struct dev_spec device_nodes[] = {
1436 {
1437 "null",
1438 S_IFCHR | 0666, 1, 3,
1439 },
1440 {
1441 "zero",
1442 S_IFCHR | 0666, 1, 5,
1443 },
1444 {
1445 "full",
1446 S_IFCHR | 0666, 1, 7,
1447 },
1448 {
1449 "urandom",
1450 S_IFCHR | 0444, 1, 9,
1451 },
1452 {
1453 "tty",
1454 S_IFCHR | 0666, 5, 0,
1455 },
1456};
1457
1458struct dev_sym_spec {
1459 const char *source, *dest;
1460};
1461
1462static const struct dev_sym_spec device_symlinks[] = {
1463 { "ptmx", "pts/ptmx", },
1464 { "fd", "/proc/self/fd", },
1465 { "stdin", "fd/0", },
1466 { "stdout", "fd/1", },
1467 { "stderr", "fd/2", },
1468};
1469
1470/*
1471 * Clean up the temporary dev path we had setup previously. In case of errors,
1472 * we don't want to go leaking empty tempdirs.
1473 */
1474static void mount_dev_cleanup(char *dev_path)
1475{
1476 umount2(dev_path, MNT_DETACH);
1477 rmdir(dev_path);
1478 free(dev_path);
1479}
1480
1481/*
1482 * Set up the pseudo /dev path at the temporary location.
1483 * See mount_dev_finalize for more details.
1484 */
1485static int mount_dev(char **dev_path_ret)
1486{
1487 int ret;
1488 int dev_fd;
1489 size_t i;
1490 mode_t mask;
1491 char *dev_path;
1492
1493 /*
1494 * Create a temp path for the /dev init. We'll relocate this to the
1495 * final location later on in the startup process.
1496 */
1497 dev_path = *dev_path_ret = strdup("/tmp/minijail.dev.XXXXXX");
1498 if (dev_path == NULL || mkdtemp(dev_path) == NULL)
1499 pdie("could not create temp path for /dev");
1500
1501 /* Set up the empty /dev mount point first. */
1502 ret = mount("minijail-devfs", dev_path, "tmpfs",
1503 MS_NOEXEC | MS_NOSUID, "size=5M,mode=755");
1504 if (ret) {
1505 rmdir(dev_path);
1506 return ret;
1507 }
1508
1509 /* We want to set the mode directly from the spec. */
1510 mask = umask(0);
1511
1512 /* Get a handle to the temp dev path for *at funcs below. */
1513 dev_fd = open(dev_path, O_DIRECTORY|O_PATH|O_CLOEXEC);
1514 if (dev_fd < 0) {
1515 ret = 1;
1516 goto done;
1517 }
1518
1519 /* Create all the nodes in /dev. */
1520 for (i = 0; i < ARRAY_SIZE(device_nodes); ++i) {
1521 const struct dev_spec *ds = &device_nodes[i];
1522 ret = mknodat(dev_fd, ds->name, ds->mode,
1523 makedev(ds->major, ds->minor));
1524 if (ret)
1525 goto done;
1526 }
1527
1528 /* Create all the symlinks in /dev. */
1529 for (i = 0; i < ARRAY_SIZE(device_symlinks); ++i) {
1530 const struct dev_sym_spec *ds = &device_symlinks[i];
1531 ret = symlinkat(ds->dest, dev_fd, ds->source);
1532 if (ret)
1533 goto done;
1534 }
1535
1536 /* Restore old mask. */
1537 done:
1538 close(dev_fd);
1539 umask(mask);
1540
1541 if (ret)
1542 mount_dev_cleanup(dev_path);
1543
1544 return ret;
1545}
1546
1547/*
1548 * Relocate the temporary /dev mount to its final /dev place.
1549 * We have to do this two step process so people can bind mount extra
1550 * /dev paths like /dev/log.
1551 */
1552static int mount_dev_finalize(const struct minijail *j, char *dev_path)
1553{
1554 int ret = -1;
1555 char *dest = NULL;
1556
1557 /* Unmount the /dev mount if possible. */
1558 if (umount2("/dev", MNT_DETACH))
1559 goto done;
1560
1561 if (asprintf(&dest, "%s/dev", j->chrootdir ? : "") < 0)
1562 goto done;
1563
1564 if (mount(dev_path, dest, NULL, MS_MOVE, NULL))
1565 goto done;
1566
1567 ret = 0;
1568 done:
1569 free(dest);
1570 mount_dev_cleanup(dev_path);
1571
1572 return ret;
1573}
1574
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001575/*
1576 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001577 * @j Minijail these mounts are for
1578 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001579 *
1580 * Returns 0 for success.
1581 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001582static int mount_one(const struct minijail *j, struct mountpoint *m,
1583 const char *dev_path)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001584{
Dylan Reid648b2202015-10-23 00:50:00 -07001585 int ret;
1586 char *dest;
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001587 int remount = 0;
1588 unsigned long original_mnt_flags = 0;
Dylan Reid648b2202015-10-23 00:50:00 -07001589
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -04001590 /* We assume |dest| has a leading "/". */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001591 if (dev_path && strncmp("/dev/", m->dest, 5) == 0) {
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -04001592 /*
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -04001593 * Since the temp path is rooted at /dev, skip that dest part.
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -04001594 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001595 if (asprintf(&dest, "%s%s", dev_path, m->dest + 4) < 0)
1596 return -ENOMEM;
1597 } else {
Mike Frysingerac08a682017-10-10 02:04:50 -04001598 if (asprintf(&dest, "%s%s", j->chrootdir ?: "", m->dest) < 0)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001599 return -ENOMEM;
1600 }
Dylan Reid648b2202015-10-23 00:50:00 -07001601
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001602 ret =
1603 setup_mount_destination(m->src, dest, j->uid, j->gid,
1604 (m->flags & MS_BIND), &original_mnt_flags);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001605 if (ret) {
François Degrosa42182d2020-04-29 00:41:52 +10001606 warn("cannot create mount target '%s'", dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001607 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001608 }
Dylan Reideec77962016-06-30 19:35:10 -07001609
Dylan Reid648b2202015-10-23 00:50:00 -07001610 /*
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001611 * Bind mounts that change the 'ro' flag have to be remounted since
1612 * 'bind' and other flags can't both be specified in the same command.
1613 * Remount after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001614 */
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001615 if ((m->flags & MS_BIND) &&
1616 ((m->flags & MS_RDONLY) != (original_mnt_flags & MS_RDONLY))) {
1617 remount = 1;
1618 /*
1619 * Restrict the mount flags to those that are user-settable in a
1620 * MS_REMOUNT request, but excluding MS_RDONLY. The
1621 * user-requested mount flags will dictate whether the remount
1622 * will have that flag or not.
1623 */
1624 original_mnt_flags &= (MS_USER_SETTABLE_MASK & ~MS_RDONLY);
Elly Jonesa1059632011-12-15 15:17:07 -05001625 }
Dylan Reid648b2202015-10-23 00:50:00 -07001626
Dylan Reid81e23972016-05-18 14:06:35 -07001627 ret = mount(m->src, dest, m->type, m->flags, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001628 if (ret) {
François Degrosa42182d2020-04-29 00:41:52 +10001629 pwarn("cannot bind-mount '%s' as '%s' with flags %#lx", m->src,
1630 dest, m->flags);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001631 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001632 }
Dylan Reid648b2202015-10-23 00:50:00 -07001633
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001634 if (remount) {
1635 ret =
1636 mount(m->src, dest, NULL,
1637 m->flags | original_mnt_flags | MS_REMOUNT, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001638 if (ret) {
François Degrosa42182d2020-04-29 00:41:52 +10001639 pwarn(
1640 "cannot bind-remount '%s' as '%s' with flags %#lx",
1641 m->src, dest,
1642 m->flags | original_mnt_flags | MS_REMOUNT);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001643 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001644 }
Dylan Reid648b2202015-10-23 00:50:00 -07001645 }
1646
Elly Jones51a5b6c2011-10-12 19:09:26 -04001647 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001648 if (m->next)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001649 return mount_one(j, m->next, dev_path);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001650 return 0;
1651
1652error:
1653 free(dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001654 return ret;
1655}
1656
Mike Frysingerac08a682017-10-10 02:04:50 -04001657static void process_mounts_or_die(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001658{
Mike Frysingerac08a682017-10-10 02:04:50 -04001659 /*
1660 * We have to mount /dev first in case there are bind mounts from
1661 * the original /dev into the new unique tmpfs one.
1662 */
1663 char *dev_path = NULL;
1664 if (j->flags.mount_dev && mount_dev(&dev_path))
1665 pdie("mount_dev failed");
Dylan Reid648b2202015-10-23 00:50:00 -07001666
Mike Frysingerac08a682017-10-10 02:04:50 -04001667 if (j->mounts_head && mount_one(j, j->mounts_head, dev_path)) {
François Degrosa42182d2020-04-29 00:41:52 +10001668 if (dev_path)
Mike Frysingerac08a682017-10-10 02:04:50 -04001669 mount_dev_cleanup(dev_path);
François Degrosa42182d2020-04-29 00:41:52 +10001670
1671 _exit(MINIJAIL_ERR_MOUNT);
Mike Frysingerac08a682017-10-10 02:04:50 -04001672 }
Mike Frysinger33ffef32017-01-13 19:53:19 -05001673
1674 /*
Mike Frysingerac08a682017-10-10 02:04:50 -04001675 * Once all bind mounts have been processed, move the temp dev to
1676 * its final /dev home.
Mike Frysinger33ffef32017-01-13 19:53:19 -05001677 */
1678 if (j->flags.mount_dev && mount_dev_finalize(j, dev_path))
Mike Frysingerac08a682017-10-10 02:04:50 -04001679 pdie("mount_dev_finalize failed");
1680}
Elly Jones51a5b6c2011-10-12 19:09:26 -04001681
Mike Frysingerac08a682017-10-10 02:04:50 -04001682static int enter_chroot(const struct minijail *j)
1683{
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001684 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1685
Elly Jones51a5b6c2011-10-12 19:09:26 -04001686 if (chroot(j->chrootdir))
1687 return -errno;
1688
1689 if (chdir("/"))
1690 return -errno;
1691
1692 return 0;
1693}
1694
Mike Frysingerac08a682017-10-10 02:04:50 -04001695static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001696{
Mike Frysingerac08a682017-10-10 02:04:50 -04001697 int oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001698
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001699 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1700
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001701 /*
1702 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001703 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001704 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001705 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001706 if (oldroot < 0)
1707 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001708 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001709 if (newroot < 0)
1710 pdie("failed to open %s for fchdir", j->chrootdir);
1711
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001712 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001713 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001714 * do a self bind mount.
1715 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001716 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1717 pdie("failed to bind mount '%s'", j->chrootdir);
1718 if (chdir(j->chrootdir))
1719 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001720 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001721 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001722
1723 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001724 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001725 * change to the old root and unmount it.
1726 */
1727 if (fchdir(oldroot))
1728 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001729
1730 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05001731 * If skip_remount_private was enabled for minijail_enter(),
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001732 * there could be a shared mount point under |oldroot|. In that case,
1733 * mounts under this shared mount point will be unmounted below, and
1734 * this unmounting will propagate to the original mount namespace
1735 * (because the mount point is shared). To prevent this unexpected
1736 * unmounting, remove these mounts from their peer groups by recursively
1737 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001738 */
1739 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001740 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001741 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001742 if (umount2(".", MNT_DETACH))
1743 pdie("umount(/)");
1744 /* Change back to the new root. */
1745 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001746 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001747 if (close(oldroot))
1748 return -errno;
1749 if (close(newroot))
1750 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001751 if (chroot("/"))
1752 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001753 /* Set correct CWD for getcwd(3). */
1754 if (chdir("/"))
1755 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001756
1757 return 0;
1758}
1759
Martin Pelikánab9eb442017-01-25 11:53:58 +11001760static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001761{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001762 const char fmt[] = "size=%zu,mode=1777";
1763 /* Count for the user storing ULLONG_MAX literally + extra space. */
1764 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1765 int ret;
1766
1767 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1768
1769 if (ret <= 0)
1770 pdie("tmpfs size spec error");
1771 else if ((size_t)ret >= sizeof(data))
1772 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001773 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001774 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001775}
1776
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001777static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001778{
1779 const char *kProcPath = "/proc";
1780 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001781 /*
1782 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001783 * /proc in our namespace, which means using MS_REMOUNT here would
1784 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001785 * namespace (!). Instead, remove their mount from our namespace lazily
1786 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001787 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001788 if (umount2(kProcPath, MNT_DETACH)) {
1789 /*
1790 * If we are in a new user namespace, umount(2) will fail.
1791 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1792 */
1793 if (j->flags.userns) {
1794 info("umount(/proc, MNT_DETACH) failed, "
1795 "this is expected when using user namespaces");
1796 } else {
1797 return -errno;
1798 }
1799 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001800 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001801 return -errno;
1802 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001803}
1804
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001805static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001806{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001807 kill(j->initpid, SIGKILL);
1808 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001809}
1810
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001811static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001812{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001813 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001814 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001815}
1816
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001817static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001818{
1819 size_t i;
1820
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001821 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001822 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001823 kill_child_and_die(j, "failed to add to cgroups");
1824 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001825}
1826
Dylan Reid0f72ef42017-06-06 15:42:49 -07001827static void set_rlimits_or_die(const struct minijail *j)
1828{
1829 size_t i;
1830
1831 for (i = 0; i < j->rlimit_count; ++i) {
1832 struct rlimit limit;
1833 limit.rlim_cur = j->rlimits[i].cur;
1834 limit.rlim_max = j->rlimits[i].max;
1835 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1836 kill_child_and_die(j, "failed to set rlimit");
1837 }
1838}
1839
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001840static void write_ugid_maps_or_die(const struct minijail *j)
1841{
1842 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1843 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001844 if (j->gidmap && j->flags.disable_setgroups) {
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001845 /*
1846 * Older kernels might not have the /proc/<pid>/setgroups files.
1847 */
Mike Frysinger6b190c02017-01-04 17:18:42 -05001848 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001849 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001850 if (ret == -ENOENT) {
1851 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1852 warn("could not disable setgroups(2)");
1853 } else
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001854 kill_child_and_die(
1855 j, "failed to disable setgroups(2)");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001856 }
1857 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001858 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1859 kill_child_and_die(j, "failed to write gid_map");
1860}
1861
1862static void enter_user_namespace(const struct minijail *j)
1863{
Luis Hector Chavez71323552017-09-05 09:17:22 -07001864 int uid = j->flags.uid ? j->uid : 0;
1865 int gid = j->flags.gid ? j->gid : 0;
1866 if (j->gidmap && setresgid(gid, gid, gid)) {
1867 pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid,
1868 gid);
1869 }
1870 if (j->uidmap && setresuid(uid, uid, uid)) {
1871 pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid,
1872 uid);
1873 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001874}
1875
1876static void parent_setup_complete(int *pipe_fds)
1877{
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001878 close_and_reset(&pipe_fds[0]);
1879 close_and_reset(&pipe_fds[1]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001880}
1881
1882/*
1883 * wait_for_parent_setup: Called by the child process to wait for any
1884 * further parent-side setup to complete before continuing.
1885 */
1886static void wait_for_parent_setup(int *pipe_fds)
1887{
1888 char buf;
1889
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001890 close_and_reset(&pipe_fds[1]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001891
1892 /* Wait for parent to complete setup and close the pipe. */
1893 if (read(pipe_fds[0], &buf, 1) != 0)
1894 die("failed to sync with parent");
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001895 close_and_reset(&pipe_fds[0]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001896}
1897
1898static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001899{
Lutz Justen13807cb2017-01-03 17:11:55 +01001900 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1901 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001902 die("can only do one of inherit, keep, or set supplementary "
1903 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001904 }
1905
Lutz Justen13807cb2017-01-03 17:11:55 +01001906 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001907 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001908 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001909 } else if (j->flags.set_suppl_gids) {
1910 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001911 pdie("setgroups(suppl_gids) failed");
Luis Hector Chavez71323552017-09-05 09:17:22 -07001912 } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001913 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001914 * Only attempt to clear supplementary groups if we are changing
Luis Hector Chavez71323552017-09-05 09:17:22 -07001915 * users or groups, and if the caller did not request to disable
1916 * setgroups (used when entering a user namespace as a
1917 * non-privileged user).
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001918 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001919 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001920 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001921 }
1922
1923 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001924 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001925
1926 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001927 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001928}
1929
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001930static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1931{
1932 const uint64_t one = 1;
1933 unsigned int i;
1934 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1935 if (keep_mask & (one << i))
1936 continue;
1937 if (prctl(PR_CAPBSET_DROP, i))
1938 pdie("could not drop capability from bounding set");
1939 }
1940}
1941
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001942static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001943{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001944 if (!j->flags.use_caps)
1945 return;
1946
Elly Jonese1749eb2011-10-07 13:54:59 -04001947 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001948 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001949 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001950 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001951 unsigned int i;
1952 if (!caps)
1953 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001954 if (cap_clear(caps))
1955 die("can't clear caps");
1956
1957 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001958 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001959 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001960 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001961 flag[0] = i;
1962 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001963 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001964 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001965 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001966 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001967 die("can't add inheritable cap");
1968 }
1969 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001970 die("can't apply initial cleaned capset");
1971
1972 /*
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001973 * Instead of dropping the bounding set first, do it here in case
Kees Cook323878a2013-02-05 15:35:24 -08001974 * the caller had a more permissive bounding set which could
1975 * have been used above to raise a capability that wasn't already
1976 * present. This requires CAP_SETPCAP, so we raised/kept it above.
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001977 *
1978 * However, if we're asked to skip setting *and* locking the
1979 * SECURE_NOROOT securebit, also skip dropping the bounding set.
1980 * If the caller wants to regain all capabilities when executing a
1981 * set-user-ID-root program, allow them to do so. The default behavior
1982 * (i.e. the behavior without |securebits_skip_mask| set) will still put
1983 * the jailed process tree in a capabilities-only environment.
1984 *
1985 * We check the negated skip mask for SECURE_NOROOT and
1986 * SECURE_NOROOT_LOCKED. If the bits are set in the negated mask they
1987 * will *not* be skipped in lock_securebits(), and therefore we should
1988 * drop the bounding set.
Kees Cook323878a2013-02-05 15:35:24 -08001989 */
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001990 if (secure_noroot_set_and_locked(~j->securebits_skip_mask)) {
1991 drop_capbset(j->caps, last_valid_cap);
1992 } else {
1993 warn("SECURE_NOROOT not set, not dropping bounding set");
1994 }
Kees Cook323878a2013-02-05 15:35:24 -08001995
1996 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001997 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001998 flag[0] = CAP_SETPCAP;
1999 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
2000 die("can't clear effective cap");
2001 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
2002 die("can't clear permitted cap");
2003 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
2004 die("can't clear inheritable cap");
2005 }
2006
2007 if (cap_set_proc(caps))
2008 die("can't apply final cleaned capset");
2009
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04002010 /*
2011 * If ambient capabilities are supported, clear all capabilities first,
2012 * then raise the requested ones.
2013 */
2014 if (j->flags.set_ambient_caps) {
2015 if (!cap_ambient_supported()) {
2016 pdie("ambient capabilities not supported");
2017 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04002018 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
2019 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04002020 pdie("can't clear ambient capabilities");
2021 }
2022
2023 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
2024 if (!(j->caps & (one << i)))
2025 continue;
2026
2027 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
2028 0) != 0) {
2029 pdie("prctl(PR_CAP_AMBIENT, "
2030 "PR_CAP_AMBIENT_RAISE, %u) failed",
2031 i);
2032 }
2033 }
2034 }
2035
Kees Cook323878a2013-02-05 15:35:24 -08002036 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04002037}
2038
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002039static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002040{
2041 /*
2042 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
2043 * in the kernel source tree for an explanation of the parameters.
2044 */
2045 if (j->flags.no_new_privs) {
2046 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
2047 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
2048 }
2049
2050 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07002051 * Code running with ASan
2052 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
2053 * will make system calls not included in the syscall filter policy,
2054 * which will likely crash the program. Skip setting seccomp filter in
2055 * that case.
2056 * 'running_with_asan()' has no inputs and is completely defined at
2057 * build time, so this cannot be used by an attacker to skip setting
2058 * seccomp filter.
2059 */
Evgenii Stepanov3d98f3c2018-08-23 15:06:50 -07002060 if (j->flags.seccomp_filter && running_with_asan()) {
Evgenii Stepanov825828c2018-07-27 11:57:07 -07002061 warn("running with (HW)ASan, not setting seccomp filter");
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07002062 return;
2063 }
2064
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002065 if (j->flags.seccomp_filter) {
2066 if (j->flags.seccomp_filter_logging) {
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002067 warn("logging seccomp filter failures");
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04002068 if (!seccomp_ret_log_available()) {
2069 /*
2070 * If SECCOMP_RET_LOG is not available,
2071 * install the SIGSYS handler first.
2072 */
2073 if (install_sigsys_handler())
2074 pdie(
2075 "failed to install SIGSYS handler");
2076 }
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002077 } else if (j->flags.seccomp_filter_tsync) {
2078 /*
2079 * If setting thread sync,
2080 * reset the SIGSYS signal handler so that
2081 * the entire thread group is killed.
2082 */
2083 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
2084 pdie("failed to reset SIGSYS disposition");
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002085 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002086 }
2087
2088 /*
2089 * Install the syscall filter.
2090 */
2091 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002092 if (j->flags.seccomp_filter_tsync) {
2093 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
2094 SECCOMP_FILTER_FLAG_TSYNC,
2095 j->filter_prog)) {
2096 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002097 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002098 } else {
2099 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
2100 j->filter_prog)) {
2101 pdie("prctl(seccomp_filter) failed");
2102 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002103 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002104 }
2105}
2106
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002107static pid_t forward_pid = -1;
2108
Mike Frysinger33d051a2018-05-30 16:41:10 -04002109static void forward_signal(int sig,
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04002110 siginfo_t *siginfo attribute_unused,
2111 void *void_context attribute_unused)
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002112{
2113 if (forward_pid != -1) {
Mike Frysinger33d051a2018-05-30 16:41:10 -04002114 kill(forward_pid, sig);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002115 }
2116}
2117
2118static void install_signal_handlers(void)
2119{
2120 struct sigaction act;
2121
2122 memset(&act, 0, sizeof(act));
2123 act.sa_sigaction = &forward_signal;
2124 act.sa_flags = SA_SIGINFO | SA_RESTART;
2125
2126 /* Handle all signals, except SIGCHLD. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002127 for (int sig = 1; sig < NSIG; sig++) {
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002128 /*
2129 * We don't care if we get EINVAL: that just means that we
2130 * can't handle this signal, so let's skip it and continue.
2131 */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002132 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002133 }
2134 /* Reset SIGCHLD's handler. */
2135 signal(SIGCHLD, SIG_DFL);
2136
2137 /* Handle real-time signals. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002138 for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++) {
2139 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002140 }
2141}
2142
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002143static const char *lookup_hook_name(minijail_hook_event_t event)
2144{
2145 switch (event) {
2146 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
2147 return "pre-drop-caps";
2148 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
2149 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07002150 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
2151 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002152 case MINIJAIL_HOOK_EVENT_MAX:
2153 /*
2154 * Adding this in favor of a default case to force the
2155 * compiler to error out if a new enum value is added.
2156 */
2157 break;
2158 }
2159 return "unknown";
2160}
2161
2162static void run_hooks_or_die(const struct minijail *j,
2163 minijail_hook_event_t event)
2164{
2165 int rc;
2166 int hook_index = 0;
2167 for (struct hook *c = j->hooks_head; c; c = c->next) {
2168 if (c->event != event)
2169 continue;
2170 rc = c->hook(c->payload);
2171 if (rc != 0) {
2172 errno = -rc;
2173 pdie("%s hook (index %d) failed",
2174 lookup_hook_name(event), hook_index);
2175 }
2176 /* Only increase the index within the same hook event type. */
2177 ++hook_index;
2178 }
2179}
2180
Will Drewry6ac91122011-10-21 16:38:58 -05002181void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002182{
Dylan Reidf682d472015-09-17 21:39:07 -07002183 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002184 * If we're dropping caps, get the last valid cap from /proc now,
2185 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07002186 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002187 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002188 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002189 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07002190
Elly Jonese1749eb2011-10-07 13:54:59 -04002191 if (j->flags.pids)
2192 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002193 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04002194
Lutz Justen13807cb2017-01-03 17:11:55 +01002195 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05002196 die("cannot inherit supplementary groups without setting a "
2197 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04002198
Elly Jonesdd3e8512012-01-23 15:13:38 -05002199 /*
2200 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04002201 * so we don't even try. If any of our operations fail, we abort() the
2202 * entire process.
2203 */
Mike Frysinger902a4492018-12-27 05:22:56 -05002204 if (j->flags.enter_vfs) {
2205 if (setns(j->mountns_fd, CLONE_NEWNS))
2206 pdie("setns(CLONE_NEWNS) failed");
2207 close(j->mountns_fd);
2208 }
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002209
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07002210 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002211 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002212 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002213 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05002214 * By default, remount all filesystems as private, unless
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04002215 * - Passed a specific remount mode, in which case remount with
2216 * that,
2217 * - Asked not to remount at all, in which case skip the
2218 * mount(2) call.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002219 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
2220 */
Mike Frysinger785b1c32018-02-23 15:47:24 -05002221 if (j->remount_mode) {
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04002222 if (mount(NULL, "/", NULL, MS_REC | j->remount_mode,
2223 NULL))
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -05002224 pdie("mount(NULL, /, NULL, "
2225 "MS_REC | j->remount_mode, NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002226 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002227 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04002228
Dylan Reidf7942472015-11-18 17:55:26 -08002229 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002230 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08002231 }
2232
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002233 if (j->flags.uts) {
2234 if (unshare(CLONE_NEWUTS))
2235 pdie("unshare(CLONE_NEWUTS) failed");
2236
2237 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
2238 pdie("sethostname(%s) failed", j->hostname);
2239 }
2240
Dylan Reid1102f5a2015-09-15 11:52:20 -07002241 if (j->flags.enter_net) {
2242 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002243 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger902a4492018-12-27 05:22:56 -05002244 close(j->netns_fd);
Mike Frysinger7559dfe2016-11-15 18:58:39 -05002245 } else if (j->flags.net) {
2246 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002247 pdie("unshare(CLONE_NEWNET) failed");
2248 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07002249 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002250
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002251 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002252 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002253
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08002254 if (j->flags.new_session_keyring) {
2255 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
2256 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
2257 }
2258
Mike Frysingerac08a682017-10-10 02:04:50 -04002259 /* We have to process all the mounts before we chroot/pivot_root. */
2260 process_mounts_or_die(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002261
Mike Frysingerac08a682017-10-10 02:04:50 -04002262 if (j->flags.chroot && enter_chroot(j))
Mike Frysinger33ffef32017-01-13 19:53:19 -05002263 pdie("chroot");
Mike Frysinger33ffef32017-01-13 19:53:19 -05002264
Mike Frysingerac08a682017-10-10 02:04:50 -04002265 if (j->flags.pivot_root && enter_pivot_root(j))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08002266 pdie("pivot_root");
2267
Martin Pelikánab9eb442017-01-25 11:53:58 +11002268 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07002269 pdie("mount_tmp");
2270
Dylan Reid791f5772015-09-14 20:02:42 -07002271 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04002272 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04002273
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002274 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
2275
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002276 /*
2277 * If we're only dropping capabilities from the bounding set, but not
2278 * from the thread's (permitted|inheritable|effective) sets, do it now.
2279 */
2280 if (j->flags.capbset_drop) {
2281 drop_capbset(j->cap_bset, last_valid_cap);
2282 }
2283
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002284 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002285 * POSIX capabilities are a bit tricky. We must set SECBIT_KEEP_CAPS
2286 * before drop_ugid() below as the latter would otherwise drop all
2287 * capabilities.
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002288 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002289 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002290 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002291 * When using ambient capabilities, CAP_SET{GID,UID} can be
2292 * inherited across execve(2), so SECBIT_KEEP_CAPS is not
2293 * strictly needed.
Elly Jonese1749eb2011-10-07 13:54:59 -04002294 */
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002295 bool require_keep_caps = !j->flags.set_ambient_caps;
2296 if (lock_securebits(j->securebits_skip_mask,
2297 require_keep_caps) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002298 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002299 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002300 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002301
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002302 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002303 /*
2304 * If we're setting no_new_privs, we can drop privileges
2305 * before setting seccomp filter. This way filter policies
2306 * don't need to allow privilege-dropping syscalls.
2307 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002308 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002309 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002310 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002311 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002312 /*
2313 * If we're not setting no_new_privs,
2314 * we need to set seccomp filter *before* dropping privileges.
2315 * WARNING: this means that filter policies *must* allow
2316 * setgroups()/setresgid()/setresuid() for dropping root and
2317 * capget()/capset()/prctl() for dropping caps.
2318 */
2319 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002320 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002321 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04002322 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002323
Elly Jonesdd3e8512012-01-23 15:13:38 -05002324 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08002325 * Select the specified alternate syscall table. The table must not
2326 * block prctl(2) if we're using seccomp as well.
2327 */
2328 if (j->flags.alt_syscall) {
2329 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002330 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08002331 }
2332
2333 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05002334 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04002335 * privilege-dropping syscalls :)
2336 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002337 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04002338 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002339 warn("seccomp not supported");
2340 return;
2341 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002342 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002343 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002344}
2345
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002346/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04002347static int init_exitstatus = 0;
2348
Mike Frysinger0a27ab02020-09-04 16:18:12 -04002349static void init_term(int sig attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002350{
2351 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04002352}
2353
Mike Frysinger0a27ab02020-09-04 16:18:12 -04002354static void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04002355{
2356 pid_t pid;
2357 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002358 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002359 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002360 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002361 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002362 /*
2363 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04002364 * left inside our pid namespace or we get a signal.
2365 */
2366 if (pid == rootpid)
2367 init_exitstatus = status;
2368 }
2369 if (!WIFEXITED(init_exitstatus))
2370 _exit(MINIJAIL_ERR_INIT);
2371 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04002372}
2373
Will Drewry6ac91122011-10-21 16:38:58 -05002374int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002375{
2376 size_t sz = 0;
2377 size_t bytes = read(fd, &sz, sizeof(sz));
2378 char *buf;
2379 int r;
2380 if (sizeof(sz) != bytes)
2381 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002382 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04002383 return -E2BIG;
2384 buf = malloc(sz);
2385 if (!buf)
2386 return -ENOMEM;
2387 bytes = read(fd, buf, sz);
2388 if (bytes != sz) {
2389 free(buf);
2390 return -EINVAL;
2391 }
2392 r = minijail_unmarshal(j, buf, sz);
2393 free(buf);
2394 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002395}
2396
Will Drewry6ac91122011-10-21 16:38:58 -05002397int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04002398{
Elly Jonese1749eb2011-10-07 13:54:59 -04002399 size_t sz = minijail_size(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002400 if (!sz)
2401 return -EINVAL;
François Degros664eba72019-11-05 13:18:24 +11002402
2403 char *buf = malloc(sz);
2404 if (!buf)
2405 return -ENOMEM;
2406
2407 int err = minijail_marshal(j, buf, sz);
2408 if (err)
2409 goto error;
2410
Elly Jonese1749eb2011-10-07 13:54:59 -04002411 /* Sends [size][minijail]. */
François Degros664eba72019-11-05 13:18:24 +11002412 err = write_exactly(fd, &sz, sizeof(sz));
2413 if (err)
2414 goto error;
2415
2416 err = write_exactly(fd, buf, sz);
2417
2418error:
Elly Jonese1749eb2011-10-07 13:54:59 -04002419 free(buf);
François Degros664eba72019-11-05 13:18:24 +11002420 return err;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002421}
Elly Jonescd7a9042011-07-22 13:56:51 -04002422
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002423static int setup_preload(const struct minijail *j attribute_unused,
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002424 char ***child_env attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002425{
Daniel Erat5b7a3182015-08-19 16:06:22 -06002426#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002427 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002428 return 0;
2429#else
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002430 const char *preload_path = j->preload_path ?: PRELOADPATH;
2431 char *newenv = NULL;
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002432 int ret = 0;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002433 const char *oldenv = getenv(kLdPreloadEnvVar);
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002434
2435 if (!oldenv)
2436 oldenv = "";
Elly Jonescd7a9042011-07-22 13:56:51 -04002437
Elly Jonese1749eb2011-10-07 13:54:59 -04002438 /* Only insert a separating space if we have something to separate... */
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002439 if (asprintf(&newenv, "%s%s%s", oldenv, oldenv[0] != '\0' ? " " : "",
2440 preload_path) < 0) {
2441 return -1;
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002442 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002443
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002444 ret = minijail_setenv(child_env, kLdPreloadEnvVar, newenv, 1);
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002445 free(newenv);
2446 return ret;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002447#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04002448}
2449
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002450static int setup_pipe(char ***child_env, int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04002451{
2452 int r = pipe(fds);
2453 char fd_buf[11];
2454 if (r)
2455 return r;
2456 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2457 if (r <= 0)
2458 return -EINVAL;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002459 return minijail_setenv(child_env, kFdEnvVar, fd_buf, 1);
Will Drewryf89aef52011-09-16 16:48:57 -05002460}
2461
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002462static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002463{
2464 const char *kFdPath = "/proc/self/fd";
2465
2466 DIR *d = opendir(kFdPath);
2467 struct dirent *dir_entry;
2468
2469 if (d == NULL)
2470 return -1;
2471 int dir_fd = dirfd(d);
2472 while ((dir_entry = readdir(d)) != NULL) {
2473 size_t i;
2474 char *end;
2475 bool should_close = true;
2476 const int fd = strtol(dir_entry->d_name, &end, 10);
2477
2478 if ((*end) != '\0') {
2479 continue;
2480 }
2481 /*
2482 * We might have set up some pipes that we want to share with
2483 * the parent process, and should not be closed.
2484 */
2485 for (i = 0; i < size; ++i) {
2486 if (fd == inheritable_fds[i]) {
2487 should_close = false;
2488 break;
2489 }
2490 }
2491 /* Also avoid closing the directory fd. */
2492 if (should_close && fd != dir_fd)
2493 close(fd);
2494 }
2495 closedir(d);
2496 return 0;
2497}
2498
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002499static int redirect_fds(struct minijail *j)
2500{
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002501 for (size_t i = 0; i < j->preserved_fd_count; i++) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002502 if (dup2(j->preserved_fds[i].parent_fd,
2503 j->preserved_fds[i].child_fd) == -1) {
2504 return -1;
2505 }
2506 }
Mattias Nissler1cf29fb2020-04-20 23:14:03 +02002507 /*
2508 * After all fds have been duped, we are now free to close all parent
2509 * fds that are *not* child fds.
2510 */
2511 for (size_t i = 0; i < j->preserved_fd_count; i++) {
2512 int closeable = true;
2513 for (size_t i2 = 0; i2 < j->preserved_fd_count; i2++) {
2514 closeable &= j->preserved_fds[i].parent_fd !=
2515 j->preserved_fds[i2].child_fd;
2516 }
2517 if (closeable)
2518 close(j->preserved_fds[i].parent_fd);
2519 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002520 return 0;
2521}
2522
Dylan Reidacfb8be2017-08-25 12:56:51 -07002523/*
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002524 * Structure holding resources and state created when running a minijail.
2525 */
2526struct minijail_run_state {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002527 pid_t child_pid;
2528 int pipe_fds[2];
2529 int stdin_fds[2];
2530 int stdout_fds[2];
2531 int stderr_fds[2];
2532 int child_sync_pipe_fds[2];
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002533 char **child_env;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002534};
2535
2536static void minijail_free_run_state(struct minijail_run_state *state)
2537{
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002538 state->child_pid = -1;
2539
2540 int *fd_pairs[] = {state->pipe_fds, state->stdin_fds, state->stdout_fds,
2541 state->stderr_fds, state->child_sync_pipe_fds};
2542 for (size_t i = 0; i < ARRAY_SIZE(fd_pairs); ++i) {
2543 close_and_reset(&fd_pairs[i][0]);
2544 close_and_reset(&fd_pairs[i][1]);
2545 }
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002546
2547 minijail_free_env(state->child_env);
2548 state->child_env = NULL;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002549}
2550
2551/* Set up stdin/stdout/stderr file descriptors in the child. */
2552static void setup_child_std_fds(struct minijail *j,
2553 struct minijail_run_state *state)
2554{
2555 struct {
2556 const char *name;
2557 int from;
2558 int to;
2559 } fd_map[] = {
2560 {"stdin", state->stdin_fds[0], STDIN_FILENO},
2561 {"stdout", state->stdout_fds[1], STDOUT_FILENO},
2562 {"stderr", state->stderr_fds[1], STDERR_FILENO},
2563 };
2564
2565 for (size_t i = 0; i < ARRAY_SIZE(fd_map); ++i) {
2566 if (fd_map[i].from != -1) {
2567 if (dup2(fd_map[i].from, fd_map[i].to) == -1)
2568 die("failed to set up %s pipe", fd_map[i].name);
2569 }
2570 }
2571
2572 /* Close temporary pipe file descriptors. */
2573 int *std_pipes[] = {state->stdin_fds, state->stdout_fds,
2574 state->stderr_fds};
2575 for (size_t i = 0; i < ARRAY_SIZE(std_pipes); ++i) {
2576 close_and_reset(&std_pipes[i][0]);
2577 close_and_reset(&std_pipes[i][1]);
2578 }
2579
2580 /*
2581 * If any of stdin, stdout, or stderr are TTYs, or setsid flag is
2582 * set, create a new session. This prevents the jailed process from
2583 * using the TIOCSTI ioctl to push characters into the parent process
2584 * terminal's input buffer, therefore escaping the jail.
2585 *
2586 * Since it has just forked, the child will not be a process group
2587 * leader, and this call to setsid() should always succeed.
2588 */
2589 if (j->flags.setsid || isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2590 isatty(STDERR_FILENO)) {
2591 if (setsid() < 0) {
2592 pdie("setsid() failed");
2593 }
2594 }
2595}
2596
2597/*
Dylan Reidacfb8be2017-08-25 12:56:51 -07002598 * Structure that specifies how to start a minijail.
2599 *
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002600 * filename - The program to exec in the child. Required if |exec_in_child| = 1.
2601 * argv - Arguments for the child program. Required if |exec_in_child| = 1.
2602 * envp - Environment for the child program. Available if |exec_in_child| = 1.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002603 * use_preload - If true use LD_PRELOAD.
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002604 * exec_in_child - If true, run |filename|. Otherwise, the child will return to
Dylan Reid0412dcc2017-08-24 11:33:15 -07002605 * the caller.
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002606 * pstdin_fd - Filled with stdin pipe if non-NULL.
2607 * pstdout_fd - Filled with stdout pipe if non-NULL.
2608 * pstderr_fd - Filled with stderr pipe if non-NULL.
2609 * pchild_pid - Filled with the pid of the child process if non-NULL.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002610 */
2611struct minijail_run_config {
2612 const char *filename;
2613 char *const *argv;
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002614 char *const *envp;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002615 int use_preload;
Dylan Reid0412dcc2017-08-24 11:33:15 -07002616 int exec_in_child;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002617 int *pstdin_fd;
2618 int *pstdout_fd;
2619 int *pstderr_fd;
2620 pid_t *pchild_pid;
2621};
2622
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002623static int
2624minijail_run_config_internal(struct minijail *j,
2625 const struct minijail_run_config *config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002626
Will Drewry6ac91122011-10-21 16:38:58 -05002627int API minijail_run(struct minijail *j, const char *filename,
2628 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002629{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002630 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002631 .filename = filename,
2632 .argv = argv,
2633 .envp = NULL,
2634 .use_preload = true,
2635 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002636 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002637 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002638}
2639
2640int API minijail_run_pid(struct minijail *j, const char *filename,
2641 char *const argv[], pid_t *pchild_pid)
2642{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002643 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002644 .filename = filename,
2645 .argv = argv,
2646 .envp = NULL,
2647 .use_preload = true,
2648 .exec_in_child = true,
2649 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002650 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002651 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002652}
2653
2654int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002655 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002656{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002657 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002658 .filename = filename,
2659 .argv = argv,
2660 .envp = NULL,
2661 .use_preload = true,
2662 .exec_in_child = true,
2663 .pstdin_fd = pstdin_fd,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002664 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002665 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002666}
2667
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002668int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002669 char *const argv[], pid_t *pchild_pid,
2670 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002671{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002672 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002673 .filename = filename,
2674 .argv = argv,
2675 .envp = NULL,
2676 .use_preload = true,
2677 .exec_in_child = true,
2678 .pstdin_fd = pstdin_fd,
2679 .pstdout_fd = pstdout_fd,
2680 .pstderr_fd = pstderr_fd,
2681 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002682 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002683 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002684}
2685
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002686int API minijail_run_env_pid_pipes(struct minijail *j, const char *filename,
2687 char *const argv[], char *const envp[],
2688 pid_t *pchild_pid, int *pstdin_fd,
2689 int *pstdout_fd, int *pstderr_fd)
2690{
2691 struct minijail_run_config config = {
2692 .filename = filename,
2693 .argv = argv,
2694 .envp = envp,
2695 .use_preload = true,
2696 .exec_in_child = true,
2697 .pstdin_fd = pstdin_fd,
2698 .pstdout_fd = pstdout_fd,
2699 .pstderr_fd = pstderr_fd,
2700 .pchild_pid = pchild_pid,
2701 };
2702 return minijail_run_config_internal(j, &config);
2703}
2704
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002705int API minijail_run_no_preload(struct minijail *j, const char *filename,
2706 char *const argv[])
2707{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002708 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002709 .filename = filename,
2710 .argv = argv,
2711 .envp = NULL,
2712 .use_preload = false,
2713 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002714 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002715 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002716}
2717
Samuel Tan63187f42015-10-16 13:01:53 -07002718int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002719 const char *filename,
2720 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002721 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002722 int *pstdin_fd,
2723 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002724 int *pstderr_fd)
2725{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002726 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002727 .filename = filename,
2728 .argv = argv,
2729 .envp = NULL,
2730 .use_preload = false,
2731 .exec_in_child = true,
2732 .pstdin_fd = pstdin_fd,
2733 .pstdout_fd = pstdout_fd,
2734 .pstderr_fd = pstderr_fd,
2735 .pchild_pid = pchild_pid,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002736 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002737 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002738}
2739
2740int API minijail_run_env_pid_pipes_no_preload(struct minijail *j,
2741 const char *filename,
2742 char *const argv[],
2743 char *const envp[],
2744 pid_t *pchild_pid, int *pstdin_fd,
2745 int *pstdout_fd, int *pstderr_fd)
2746{
2747 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002748 .filename = filename,
2749 .argv = argv,
2750 .envp = envp,
2751 .use_preload = false,
2752 .exec_in_child = true,
2753 .pstdin_fd = pstdin_fd,
2754 .pstdout_fd = pstdout_fd,
2755 .pstderr_fd = pstderr_fd,
2756 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002757 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002758 return minijail_run_config_internal(j, &config);
Samuel Tan63187f42015-10-16 13:01:53 -07002759}
2760
Dylan Reid0412dcc2017-08-24 11:33:15 -07002761pid_t API minijail_fork(struct minijail *j)
2762{
2763 struct minijail_run_config config = {};
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002764 return minijail_run_config_internal(j, &config);
Dylan Reid0412dcc2017-08-24 11:33:15 -07002765}
2766
Dylan Reid18c49c82017-08-25 14:52:27 -07002767static int minijail_run_internal(struct minijail *j,
2768 const struct minijail_run_config *config,
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002769 struct minijail_run_state *state_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002770{
Dylan Reidce5b55e2016-01-13 11:04:16 -08002771 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002772 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002773 /* We need to remember this across the minijail_preexec() call. */
2774 int pid_namespace = j->flags.pids;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -07002775 /*
2776 * Create an init process if we are entering a pid namespace, unless the
2777 * user has explicitly opted out by calling minijail_run_as_init().
2778 */
2779 int do_init = j->flags.do_init && !j->flags.run_as_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002780 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002781
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002782 if (use_preload) {
Dylan Reid0412dcc2017-08-24 11:33:15 -07002783 if (j->hooks_head != NULL)
2784 die("Minijail hooks are not supported with LD_PRELOAD");
2785 if (!config->exec_in_child)
2786 die("minijail_fork is not supported with LD_PRELOAD");
2787
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002788 /*
2789 * Before we fork(2) and execve(2) the child process, we need
2790 * to open a pipe(2) to send the minijail configuration over.
2791 */
2792 state_out->child_env =
2793 minijail_copy_env(config->envp ? config->envp : environ);
2794 if (!state_out->child_env)
2795 return ENOMEM;
2796 if (setup_preload(j, &state_out->child_env) ||
2797 setup_pipe(&state_out->child_env, state_out->pipe_fds))
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002798 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002799 }
Will Drewryf89aef52011-09-16 16:48:57 -05002800
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002801 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002802 if (j->flags.use_caps && j->caps != 0 &&
2803 !j->flags.set_ambient_caps) {
2804 die("non-empty, non-ambient capabilities are not "
2805 "supported without LD_PRELOAD");
2806 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002807 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002808
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002809 /* Create pipes for stdin/stdout/stderr as requested by caller. */
2810 struct {
2811 bool requested;
2812 int *pipe_fds;
2813 } pipe_fd_req[] = {
2814 {config->pstdin_fd != NULL, state_out->stdin_fds},
2815 {config->pstdout_fd != NULL, state_out->stdout_fds},
2816 {config->pstderr_fd != NULL, state_out->stderr_fds},
2817 };
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002818
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002819 for (size_t i = 0; i < ARRAY_SIZE(pipe_fd_req); ++i) {
2820 if (pipe_fd_req[i].requested &&
2821 pipe(pipe_fd_req[i].pipe_fds) == -1)
2822 return EFAULT;
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002823 }
2824
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002825 /*
François Degros664eba72019-11-05 13:18:24 +11002826 * If the parent process needs to configure the child's runtime
2827 * environment after forking, create a pipe(2) to block the child until
2828 * configuration is done.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002829 */
Daniel Erat2d69add2019-04-23 20:58:53 -07002830 if (j->flags.forward_signals || j->flags.pid_file || j->flags.cgroups ||
2831 j->rlimit_count || j->flags.userns) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002832 sync_child = 1;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002833 if (pipe(state_out->child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002834 return -EFAULT;
2835 }
2836
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002837 /*
2838 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002839 *
2840 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2841 *
2842 * In multithreaded programs, there are a bunch of locks inside libc,
2843 * some of which may be held by other threads at the time that we call
2844 * minijail_run_pid(). If we call fork(), glibc does its level best to
2845 * ensure that we hold all of these locks before it calls clone()
2846 * internally and drop them after clone() returns, but when we call
2847 * sys_clone(2) directly, all that gets bypassed and we end up with a
2848 * child address space where some of libc's important locks are held by
2849 * other threads (which did not get cloned, and hence will never release
2850 * those locks). This is okay so long as we call exec() immediately
2851 * after, but a bunch of seemingly-innocent libc functions like setenv()
2852 * take locks.
2853 *
2854 * Hence, only call sys_clone() if we need to, in order to get at pid
2855 * namespacing. If we follow this path, the child's address space might
2856 * have broken locks; you may only call functions that do not acquire
2857 * any locks.
2858 *
2859 * Unfortunately, fork() acquires every lock it can get its hands on, as
2860 * previously detailed, so this function is highly likely to deadlock
2861 * later on (see "deadlock here") if we're multithreaded.
2862 *
2863 * We might hack around this by having the clone()d child (init of the
2864 * pid namespace) return directly, rather than leaving the clone()d
2865 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002866 * its fork()ed child return in turn), but that process would be
2867 * crippled with its libc locks potentially broken. We might try
2868 * fork()ing in the parent before we clone() to ensure that we own all
2869 * the locks, but then we have to have the forked child hanging around
2870 * consuming resources (and possibly having file descriptors / shared
2871 * memory regions / etc attached). We'd need to keep the child around to
2872 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002873 *
2874 * TODO(ellyjones): figure out if the "forked child hanging around"
2875 * problem is fixable or not. It would be nice if we worked in this
2876 * case.
2877 */
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002878 pid_t child_pid;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002879 if (pid_namespace) {
François Degros94619842019-11-08 16:37:55 +11002880 unsigned long clone_flags = CLONE_NEWPID | SIGCHLD;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002881 if (j->flags.userns)
2882 clone_flags |= CLONE_NEWUSER;
François Degros94619842019-11-08 16:37:55 +11002883
2884 child_pid = syscall(SYS_clone, clone_flags, NULL, 0L, 0L, 0L);
2885
2886 if (child_pid < 0) {
2887 if (errno == EPERM)
2888 pdie("clone(CLONE_NEWPID | ...) failed with EPERM; "
2889 "is this process missing CAP_SYS_ADMIN?");
2890 pdie("clone(CLONE_NEWPID | ...) failed");
2891 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002892 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002893 child_pid = fork();
2894
François Degros94619842019-11-08 16:37:55 +11002895 if (child_pid < 0)
2896 pdie("fork failed");
Elly Jonese1749eb2011-10-07 13:54:59 -04002897 }
Will Drewryf89aef52011-09-16 16:48:57 -05002898
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002899 state_out->child_pid = child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04002900 if (child_pid) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002901 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002902
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002903 if (j->flags.forward_signals) {
2904 forward_pid = child_pid;
2905 install_signal_handlers();
2906 }
2907
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002908 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002909 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002910
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002911 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002912 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002913
Dylan Reid0f72ef42017-06-06 15:42:49 -07002914 if (j->rlimit_count)
2915 set_rlimits_or_die(j);
2916
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002917 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002918 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002919
Mike Frysinger902a4492018-12-27 05:22:56 -05002920 if (j->flags.enter_vfs)
2921 close(j->mountns_fd);
2922
2923 if (j->flags.enter_net)
2924 close(j->netns_fd);
2925
Dylan Reidce5b55e2016-01-13 11:04:16 -08002926 if (sync_child)
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002927 parent_setup_complete(state_out->child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002928
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002929 if (use_preload) {
François Degros664eba72019-11-05 13:18:24 +11002930 /*
2931 * Add SIGPIPE to the signal mask to avoid getting
2932 * killed if the child process finishes or closes its
2933 * end of the pipe prematurely.
2934 *
2935 * TODO(crbug.com/1022170): Use pthread_sigmask instead
2936 * of sigprocmask if Minijail is used in multithreaded
2937 * programs.
2938 */
2939 sigset_t to_block, to_restore;
2940 if (sigemptyset(&to_block) < 0)
2941 pdie("sigemptyset failed");
2942 if (sigaddset(&to_block, SIGPIPE) < 0)
2943 pdie("sigaddset failed");
2944 if (sigprocmask(SIG_BLOCK, &to_block, &to_restore) < 0)
2945 pdie("sigprocmask failed");
2946
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002947 /* Send marshalled minijail. */
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002948 close_and_reset(&state_out->pipe_fds[0]);
2949 ret = minijail_to_fd(j, state_out->pipe_fds[1]);
2950 close_and_reset(&state_out->pipe_fds[1]);
François Degros664eba72019-11-05 13:18:24 +11002951
2952 /* Accept any pending SIGPIPE. */
2953 while (true) {
2954 const struct timespec zero_time = {0, 0};
2955 const int sig = sigtimedwait(&to_block, NULL, &zero_time);
2956 if (sig < 0) {
2957 if (errno != EINTR)
2958 break;
2959 } else {
2960 if (sig != SIGPIPE)
2961 die("unexpected signal %d", sig);
2962 }
2963 }
2964
2965 /* Restore the signal mask to its original state. */
2966 if (sigprocmask(SIG_SETMASK, &to_restore, NULL) < 0)
2967 pdie("sigprocmask failed");
2968
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002969 if (ret) {
François Degros664eba72019-11-05 13:18:24 +11002970 warn("failed to send marshalled minijail: %s",
2971 strerror(-ret));
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002972 kill(j->initpid, SIGKILL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002973 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002974 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002975
Elly Jonese1749eb2011-10-07 13:54:59 -04002976 return 0;
2977 }
Ben Chan541c7e52011-08-26 14:55:53 -07002978
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002979 /* Child process. */
Peter Qiu2860c462015-12-16 15:13:06 -08002980 if (j->flags.reset_signal_mask) {
2981 sigset_t signal_mask;
2982 if (sigemptyset(&signal_mask) != 0)
2983 pdie("sigemptyset failed");
2984 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2985 pdie("sigprocmask failed");
2986 }
2987
Luis Hector Chaveza27118a2018-04-04 08:18:01 -07002988 if (j->flags.reset_signal_handlers) {
2989 int signum;
2990 for (signum = 0; signum <= SIGRTMAX; signum++) {
2991 /*
2992 * Ignore EINVAL since some signal numbers in the range
2993 * might not be valid.
2994 */
2995 if (signal(signum, SIG_DFL) == SIG_ERR &&
2996 errno != EINVAL) {
2997 pdie("failed to reset signal %d disposition",
2998 signum);
2999 }
3000 }
3001 }
3002
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003003 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07003004 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003005 int inheritable_fds[kMaxInheritableFdsSize];
3006 size_t size = 0;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003007
3008 int *pipe_fds[] = {
3009 state_out->pipe_fds, state_out->child_sync_pipe_fds,
3010 state_out->stdin_fds, state_out->stdout_fds,
3011 state_out->stderr_fds,
3012 };
3013
3014 for (size_t i = 0; i < ARRAY_SIZE(pipe_fds); ++i) {
3015 if (pipe_fds[i][0] != -1) {
3016 inheritable_fds[size++] = pipe_fds[i][0];
3017 }
3018 if (pipe_fds[i][1] != -1) {
3019 inheritable_fds[size++] = pipe_fds[i][1];
3020 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003021 }
Jorge Lucangeli Obes2337f802019-07-18 14:46:03 -04003022
Jorge Lucangeli Obescf3bbea2019-07-24 09:06:40 -04003023 /*
3024 * Preserve namespace file descriptors over the close_open_fds()
3025 * call. These are closed in minijail_enter() so they won't leak
3026 * into the child process.
3027 */
Jorge Lucangeli Obes2337f802019-07-18 14:46:03 -04003028 if (j->flags.enter_vfs)
3029 minijail_preserve_fd(j, j->mountns_fd, j->mountns_fd);
3030 if (j->flags.enter_net)
3031 minijail_preserve_fd(j, j->netns_fd, j->netns_fd);
3032
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003033 for (size_t i = 0; i < j->preserved_fd_count; i++) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07003034 /*
3035 * Preserve all parent_fds. They will be dup2(2)-ed in
3036 * the child later.
3037 */
3038 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
3039 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003040
3041 if (close_open_fds(inheritable_fds, size) < 0)
3042 die("failed to close open file descriptors");
3043 }
3044
Luis Hector Chavez1617f632017-08-01 18:32:30 -07003045 if (redirect_fds(j))
3046 die("failed to set up fd redirections");
3047
Dylan Reidce5b55e2016-01-13 11:04:16 -08003048 if (sync_child)
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003049 wait_for_parent_setup(state_out->child_sync_pipe_fds);
Dylan Reidce5b55e2016-01-13 11:04:16 -08003050
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003051 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08003052 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003053
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003054 setup_child_std_fds(j, state_out);
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05003055
Dylan Reid791f5772015-09-14 20:02:42 -07003056 /* If running an init program, let it decide when/how to mount /proc. */
3057 if (pid_namespace && !do_init)
3058 j->flags.remount_proc_ro = 0;
3059
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003060 if (use_preload) {
3061 /* Strip out flags that cannot be inherited across execve(2). */
3062 minijail_preexec(j);
3063 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003064 /*
3065 * If not using LD_PRELOAD, do all jailing before execve(2).
3066 * Note that PID namespaces can only be entered on fork(2),
3067 * so that flag is still cleared.
3068 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003069 j->flags.pids = 0;
3070 }
Dylan Reid0412dcc2017-08-24 11:33:15 -07003071
3072 /*
3073 * Jail this process.
3074 * If forking, return.
3075 * If not, execve(2) the target.
3076 */
Elly Jonese1749eb2011-10-07 13:54:59 -04003077 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003078
Dylan Reid0412dcc2017-08-24 11:33:15 -07003079 if (config->exec_in_child && pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05003080 /*
3081 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003082 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003083 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003084 * a child to actually run the program. If |do_init == 0|, we
3085 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04003086 *
3087 * If we're multithreaded, we'll probably deadlock here. See
3088 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04003089 */
3090 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003091 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04003092 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003093 } else if (child_pid > 0) {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003094 minijail_free_run_state(state_out);
3095
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04003096 /*
3097 * Best effort. Don't bother checking the return value.
3098 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003099 prctl(PR_SET_NAME, "minijail-init");
3100 init(child_pid); /* Never returns. */
3101 }
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003102 state_out->child_pid = child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04003103 }
Elly Jonescd7a9042011-07-22 13:56:51 -04003104
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07003105 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
3106
Dylan Reid0412dcc2017-08-24 11:33:15 -07003107 if (!config->exec_in_child)
3108 return 0;
3109
Elly Jonesdd3e8512012-01-23 15:13:38 -05003110 /*
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003111 * We're going to execve(), so make sure any remaining resources are
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003112 * freed. Exceptions are:
3113 * 1. The child environment. No need to worry about freeing it since
3114 * execve reinitializes the heap anyways.
3115 * 2. The read side of the LD_PRELOAD pipe, which we need to hand down
3116 * into the target in which the preloaded code will read from it and
3117 * then close it.
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003118 */
3119 state_out->pipe_fds[0] = -1;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003120 char *const *child_env = state_out->child_env;
3121 state_out->child_env = NULL;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003122 minijail_free_run_state(state_out);
3123
3124 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003125 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04003126 * calling process
3127 * -> execve()-ing process
3128 * If we are:
3129 * calling process
3130 * -> init()-ing process
3131 * -> execve()-ing process
3132 */
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003133 if (!child_env)
3134 child_env = config->envp ? config->envp : environ;
François Degros08b10f72019-10-09 12:44:05 +11003135 execve(config->filename, config->argv, child_env);
3136
3137 ret = (errno == ENOENT ? MINIJAIL_ERR_NO_COMMAND : MINIJAIL_ERR_NO_ACCESS);
3138 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003139 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04003140}
3141
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003142static int
3143minijail_run_config_internal(struct minijail *j,
3144 const struct minijail_run_config *config)
3145{
3146 struct minijail_run_state state = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003147 .child_pid = -1,
3148 .pipe_fds = {-1, -1},
3149 .stdin_fds = {-1, -1},
3150 .stdout_fds = {-1, -1},
3151 .stderr_fds = {-1, -1},
3152 .child_sync_pipe_fds = {-1, -1},
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003153 .child_env = NULL,
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003154 };
3155 int ret = minijail_run_internal(j, config, &state);
3156
3157 if (ret == 0) {
3158 if (config->pchild_pid)
3159 *config->pchild_pid = state.child_pid;
3160
3161 /* Grab stdin/stdout/stderr descriptors requested by caller. */
3162 struct {
3163 int *pfd;
3164 int *psrc;
3165 } fd_map[] = {
3166 {config->pstdin_fd, &state.stdin_fds[1]},
3167 {config->pstdout_fd, &state.stdout_fds[0]},
3168 {config->pstderr_fd, &state.stderr_fds[0]},
3169 };
3170
3171 for (size_t i = 0; i < ARRAY_SIZE(fd_map); ++i) {
3172 if (fd_map[i].pfd) {
3173 *fd_map[i].pfd = *fd_map[i].psrc;
3174 *fd_map[i].psrc = -1;
3175 }
3176 }
3177
3178 if (!config->exec_in_child)
3179 ret = state.child_pid;
3180 }
3181
3182 minijail_free_run_state(&state);
3183
3184 return ret;
3185}
3186
Will Drewry6ac91122011-10-21 16:38:58 -05003187int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003188{
François Degros47e63352019-10-01 13:01:53 +10003189 if (j->initpid <= 0)
3190 return -ECHILD;
3191
Elly Jonese1749eb2011-10-07 13:54:59 -04003192 if (kill(j->initpid, SIGTERM))
3193 return -errno;
François Degros47e63352019-10-01 13:01:53 +10003194
3195 return minijail_wait(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003196}
3197
Will Drewry6ac91122011-10-21 16:38:58 -05003198int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003199{
François Degros08b10f72019-10-09 12:44:05 +11003200 if (j->initpid <= 0)
3201 return -ECHILD;
3202
Elly Jonese1749eb2011-10-07 13:54:59 -04003203 int st;
François Degros627deba2019-10-01 12:48:25 +10003204 while (true) {
3205 const int ret = waitpid(j->initpid, &st, 0);
3206 if (ret >= 0)
3207 break;
3208 if (errno != EINTR)
3209 return -errno;
3210 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003211
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003212 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003213 int error_status = st;
3214 if (WIFSIGNALED(st)) {
3215 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07003216 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003217 j->initpid, signum);
3218 /*
3219 * We return MINIJAIL_ERR_JAIL if the process received
3220 * SIGSYS, which happens when a syscall is blocked by
3221 * seccomp filters.
3222 * If not, we do what bash(1) does:
3223 * $? = 128 + signum
3224 */
3225 if (signum == SIGSYS) {
3226 error_status = MINIJAIL_ERR_JAIL;
3227 } else {
François Degros08b10f72019-10-09 12:44:05 +11003228 error_status = MINIJAIL_ERR_SIG_BASE + signum;
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003229 }
3230 }
3231 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003232 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003233
3234 int exit_status = WEXITSTATUS(st);
3235 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07003236 info("child process %d exited with status %d",
3237 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003238
3239 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04003240}
3241
Will Drewry6ac91122011-10-21 16:38:58 -05003242void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003243{
Dylan Reid605ce7f2016-01-19 19:21:00 -08003244 size_t i;
3245
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07003246 if (j->filter_prog) {
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08003247 free(j->filter_prog->filter);
3248 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04003249 }
Mike Frysingerac08a682017-10-10 02:04:50 -04003250 free_mounts_list(j);
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07003251 while (j->hooks_head) {
3252 struct hook *c = j->hooks_head;
3253 j->hooks_head = c->next;
3254 free(c);
3255 }
3256 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04003257 if (j->user)
3258 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08003259 if (j->suppl_gid_list)
3260 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05003261 if (j->chrootdir)
3262 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04003263 if (j->pid_file_path)
3264 free(j->pid_file_path);
3265 if (j->uidmap)
3266 free(j->uidmap);
3267 if (j->gidmap)
3268 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04003269 if (j->hostname)
3270 free(j->hostname);
Luis Hector Chavez9acba452018-10-11 10:13:25 -07003271 if (j->preload_path)
3272 free(j->preload_path);
Andrew Brestickereac28942015-11-11 16:04:46 -08003273 if (j->alt_syscall_table)
3274 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08003275 for (i = 0; i < j->cgroup_count; ++i)
3276 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04003277 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003278}
Luis Hector Chavez114a9302017-09-05 20:36:58 -07003279
3280void API minijail_log_to_fd(int fd, int min_priority)
3281{
3282 init_logging(LOG_TO_FD, fd, min_priority);
3283}