blob: 74f64644098fb73c58a7f4591f629c59db640a89 [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
Jorge Lucangeli Obese1a86892019-06-10 16:17:03 -04001066 if (compile_filter(filename, policy_file, fprog, &filteropts)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001067 free(fprog);
1068 return -1;
1069 }
1070
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001071 return set_seccomp_filters_internal(j, fprog, true /* owned */);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001072}
1073
1074void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
1075{
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001076 if (!seccomp_should_use_filters(j))
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001077 return;
1078
Luis Hector Chaveza30a2062018-07-12 21:10:33 -07001079 FILE *file = fopen(path, "re");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001080 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -07001081 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -04001082 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001083
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001084 if (parse_seccomp_filters(j, path, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -07001085 die("failed to compile seccomp filter BPF program in '%s'",
1086 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001087 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001088 fclose(file);
1089}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001090
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001091void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
1092{
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001093 char *fd_path, *path;
1094 FILE *file;
1095
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001096 if (!seccomp_should_use_filters(j))
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001097 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001098
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001099 file = fdopen(fd, "r");
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001100 if (!file) {
1101 pdie("failed to associate stream with fd %d", fd);
1102 }
1103
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001104 if (asprintf(&fd_path, "/proc/self/fd/%d", fd) == -1)
1105 pdie("failed to create path for fd %d", fd);
1106 path = realpath(fd_path, NULL);
1107 if (path == NULL)
1108 pwarn("failed to get path of fd %d", fd);
1109 free(fd_path);
1110
1111 if (parse_seccomp_filters(j, path ? path : "<fd>", file) != 0) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -04001112 die("failed to compile seccomp filter BPF program from fd %d",
1113 fd);
1114 }
Luis Hector Chavez7624e712017-08-28 19:30:59 -07001115 free(path);
Elly Jonese1749eb2011-10-07 13:54:59 -04001116 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -05001117}
1118
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04001119void API minijail_set_seccomp_filters(struct minijail *j,
1120 const struct sock_fprog *filter)
1121{
1122 if (!seccomp_should_use_filters(j))
1123 return;
1124
1125 if (j->flags.seccomp_filter_logging) {
1126 die("minijail_log_seccomp_filter_failures() is incompatible "
1127 "with minijail_set_seccomp_filters()");
1128 }
1129
1130 /*
1131 * set_seccomp_filters_internal() can only fail with ENOMEM.
1132 * Furthermore, since we won't own the incoming filter, it will not be
1133 * modified.
1134 */
1135 if (set_seccomp_filters_internal(j, filter, false /* owned */) < 0) {
1136 die("failed to set seccomp filter");
1137 }
1138}
1139
Andrew Brestickereac28942015-11-11 16:04:46 -08001140int API minijail_use_alt_syscall(struct minijail *j, const char *table)
1141{
1142 j->alt_syscall_table = strdup(table);
1143 if (!j->alt_syscall_table)
1144 return -ENOMEM;
1145 j->flags.alt_syscall = 1;
1146 return 0;
1147}
1148
Will Drewryf89aef52011-09-16 16:48:57 -05001149struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -04001150 size_t available;
1151 size_t total;
1152 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -05001153};
1154
Mike Frysinger0a27ab02020-09-04 16:18:12 -04001155static void marshal_state_init(struct marshal_state *state, char *buf,
1156 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -04001157{
1158 state->available = available;
1159 state->buf = buf;
1160 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001161}
1162
Mike Frysinger0a27ab02020-09-04 16:18:12 -04001163static void marshal_append(struct marshal_state *state, const void *src,
1164 size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -04001165{
1166 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -05001167
Elly Jonese1749eb2011-10-07 13:54:59 -04001168 /* Up to |available| will be written. */
1169 if (copy_len) {
1170 memcpy(state->buf, src, copy_len);
1171 state->buf += copy_len;
1172 state->available -= copy_len;
1173 }
1174 /* |total| will contain the expected length. */
1175 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -05001176}
1177
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001178static void marshal_append_string(struct marshal_state *state, const char *src)
1179{
1180 marshal_append(state, src, strlen(src) + 1);
1181}
1182
Mike Frysinger0a27ab02020-09-04 16:18:12 -04001183static void marshal_mount(struct marshal_state *state,
1184 const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -07001185{
1186 marshal_append(state, m->src, strlen(m->src) + 1);
1187 marshal_append(state, m->dest, strlen(m->dest) + 1);
1188 marshal_append(state, m->type, strlen(m->type) + 1);
1189 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
1190 if (m->has_data)
1191 marshal_append(state, m->data, strlen(m->data) + 1);
1192 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
1193}
1194
Mike Frysinger0a27ab02020-09-04 16:18:12 -04001195static void minijail_marshal_helper(struct marshal_state *state,
1196 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001197{
Dylan Reid648b2202015-10-23 00:50:00 -07001198 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001199 size_t i;
1200
Elly Jonese1749eb2011-10-07 13:54:59 -04001201 marshal_append(state, (char *)j, sizeof(*j));
1202 if (j->user)
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001203 marshal_append_string(state, j->user);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001204 if (j->suppl_gid_list) {
1205 marshal_append(state, j->suppl_gid_list,
1206 j->suppl_gid_count * sizeof(gid_t));
1207 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001208 if (j->chrootdir)
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001209 marshal_append_string(state, j->chrootdir);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001210 if (j->hostname)
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001211 marshal_append_string(state, j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08001212 if (j->alt_syscall_table) {
1213 marshal_append(state, j->alt_syscall_table,
1214 strlen(j->alt_syscall_table) + 1);
1215 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001216 if (j->flags.seccomp_filter && j->filter_prog) {
1217 struct sock_fprog *fp = j->filter_prog;
1218 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001219 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -04001220 }
Dylan Reid648b2202015-10-23 00:50:00 -07001221 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001222 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001223 }
Dylan Reid605ce7f2016-01-19 19:21:00 -08001224 for (i = 0; i < j->cgroup_count; ++i)
Mike Frysinger5f9e3002020-09-04 16:20:36 -04001225 marshal_append_string(state, j->cgroups[i]);
Will Drewryf89aef52011-09-16 16:48:57 -05001226}
1227
Will Drewry6ac91122011-10-21 16:38:58 -05001228size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001229{
1230 struct marshal_state state;
1231 marshal_state_init(&state, NULL, 0);
1232 minijail_marshal_helper(&state, j);
1233 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -05001234}
1235
Elly Jonese1749eb2011-10-07 13:54:59 -04001236int minijail_marshal(const struct minijail *j, char *buf, size_t available)
1237{
1238 struct marshal_state state;
1239 marshal_state_init(&state, buf, available);
1240 minijail_marshal_helper(&state, j);
1241 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -05001242}
1243
Elly Jonese1749eb2011-10-07 13:54:59 -04001244int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
1245{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -08001246 size_t i;
1247 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -05001248 int ret = -EINVAL;
1249
Elly Jonese1749eb2011-10-07 13:54:59 -04001250 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001251 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001252 memcpy((void *)j, serialized, sizeof(*j));
1253 serialized += sizeof(*j);
1254 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001255
Will Drewrybee7ba72011-10-21 20:47:01 -05001256 /* Potentially stale pointers not used as signals. */
Luis Hector Chavez9acba452018-10-11 10:13:25 -07001257 j->preload_path = NULL;
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001258 j->pid_file_path = NULL;
1259 j->uidmap = NULL;
1260 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001261 j->mounts_head = NULL;
1262 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001263 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001264 j->hooks_head = NULL;
1265 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001266
Elly Jonese1749eb2011-10-07 13:54:59 -04001267 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001268 char *user = consumestr(&serialized, &length);
1269 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001270 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001271 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001272 if (!j->user)
1273 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001274 }
Will Drewryf89aef52011-09-16 16:48:57 -05001275
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001276 if (j->suppl_gid_list) { /* stale pointer */
1277 if (j->suppl_gid_count > NGROUPS_MAX) {
1278 goto bad_gid_list;
1279 }
1280 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1281 void *gid_list_bytes =
1282 consumebytes(gid_list_size, &serialized, &length);
1283 if (!gid_list_bytes)
1284 goto bad_gid_list;
1285
1286 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1287 if (!j->suppl_gid_list)
1288 goto bad_gid_list;
1289
1290 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1291 }
1292
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001293 if (j->chrootdir) { /* stale pointer */
1294 char *chrootdir = consumestr(&serialized, &length);
1295 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001296 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001297 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001298 if (!j->chrootdir)
1299 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001300 }
1301
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001302 if (j->hostname) { /* stale pointer */
1303 char *hostname = consumestr(&serialized, &length);
1304 if (!hostname)
1305 goto bad_hostname;
1306 j->hostname = strdup(hostname);
1307 if (!j->hostname)
1308 goto bad_hostname;
1309 }
1310
Andrew Brestickereac28942015-11-11 16:04:46 -08001311 if (j->alt_syscall_table) { /* stale pointer */
1312 char *alt_syscall_table = consumestr(&serialized, &length);
1313 if (!alt_syscall_table)
1314 goto bad_syscall_table;
1315 j->alt_syscall_table = strdup(alt_syscall_table);
1316 if (!j->alt_syscall_table)
1317 goto bad_syscall_table;
1318 }
1319
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001320 if (j->flags.seccomp_filter && j->filter_len > 0) {
1321 size_t ninstrs = j->filter_len;
1322 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1323 ninstrs > USHRT_MAX)
1324 goto bad_filters;
1325
1326 size_t program_len = ninstrs * sizeof(struct sock_filter);
1327 void *program = consumebytes(program_len, &serialized, &length);
1328 if (!program)
1329 goto bad_filters;
1330
1331 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001332 if (!j->filter_prog)
1333 goto bad_filters;
1334
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001335 j->filter_prog->len = ninstrs;
1336 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001337 if (!j->filter_prog->filter)
1338 goto bad_filter_prog_instrs;
1339
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001340 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001341 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001342
Dylan Reid648b2202015-10-23 00:50:00 -07001343 count = j->mounts_count;
1344 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001345 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001346 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001347 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001348 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001349 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001350 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001351 const char *src = consumestr(&serialized, &length);
1352 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001353 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001354 dest = consumestr(&serialized, &length);
1355 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001356 goto bad_mounts;
1357 type = consumestr(&serialized, &length);
1358 if (!type)
1359 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001360 has_data = consumebytes(sizeof(*has_data), &serialized,
1361 &length);
1362 if (!has_data)
1363 goto bad_mounts;
1364 if (*has_data) {
1365 data = consumestr(&serialized, &length);
1366 if (!data)
1367 goto bad_mounts;
1368 }
Dylan Reid648b2202015-10-23 00:50:00 -07001369 flags = consumebytes(sizeof(*flags), &serialized, &length);
1370 if (!flags)
1371 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001372 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001373 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001374 }
1375
Dylan Reid605ce7f2016-01-19 19:21:00 -08001376 count = j->cgroup_count;
1377 j->cgroup_count = 0;
1378 for (i = 0; i < count; ++i) {
1379 char *cgroup = consumestr(&serialized, &length);
1380 if (!cgroup)
1381 goto bad_cgroups;
1382 j->cgroups[i] = strdup(cgroup);
1383 if (!j->cgroups[i])
1384 goto bad_cgroups;
1385 ++j->cgroup_count;
1386 }
1387
Elly Jonese1749eb2011-10-07 13:54:59 -04001388 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001389
Dylan Reid605ce7f2016-01-19 19:21:00 -08001390bad_cgroups:
Mike Frysingerac08a682017-10-10 02:04:50 -04001391 free_mounts_list(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001392 for (i = 0; i < j->cgroup_count; ++i)
1393 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001394bad_mounts:
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001395 if (j->filter_prog && j->filter_prog->filter)
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001396 free(j->filter_prog->filter);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001397bad_filter_prog_instrs:
1398 if (j->filter_prog)
1399 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001400bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001401 if (j->alt_syscall_table)
1402 free(j->alt_syscall_table);
1403bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001404 if (j->chrootdir)
1405 free(j->chrootdir);
1406bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001407 if (j->hostname)
1408 free(j->hostname);
1409bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001410 if (j->suppl_gid_list)
1411 free(j->suppl_gid_list);
1412bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001413 if (j->user)
1414 free(j->user);
1415clear_pointers:
1416 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001417 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001418 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001419 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001420 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001421 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001422out:
1423 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001424}
1425
Mike Frysinger33ffef32017-01-13 19:53:19 -05001426struct dev_spec {
1427 const char *name;
1428 mode_t mode;
1429 dev_t major, minor;
1430};
1431
1432static const struct dev_spec device_nodes[] = {
1433 {
1434 "null",
1435 S_IFCHR | 0666, 1, 3,
1436 },
1437 {
1438 "zero",
1439 S_IFCHR | 0666, 1, 5,
1440 },
1441 {
1442 "full",
1443 S_IFCHR | 0666, 1, 7,
1444 },
1445 {
1446 "urandom",
1447 S_IFCHR | 0444, 1, 9,
1448 },
1449 {
1450 "tty",
1451 S_IFCHR | 0666, 5, 0,
1452 },
1453};
1454
1455struct dev_sym_spec {
1456 const char *source, *dest;
1457};
1458
1459static const struct dev_sym_spec device_symlinks[] = {
1460 { "ptmx", "pts/ptmx", },
1461 { "fd", "/proc/self/fd", },
1462 { "stdin", "fd/0", },
1463 { "stdout", "fd/1", },
1464 { "stderr", "fd/2", },
1465};
1466
1467/*
1468 * Clean up the temporary dev path we had setup previously. In case of errors,
1469 * we don't want to go leaking empty tempdirs.
1470 */
1471static void mount_dev_cleanup(char *dev_path)
1472{
1473 umount2(dev_path, MNT_DETACH);
1474 rmdir(dev_path);
1475 free(dev_path);
1476}
1477
1478/*
1479 * Set up the pseudo /dev path at the temporary location.
1480 * See mount_dev_finalize for more details.
1481 */
1482static int mount_dev(char **dev_path_ret)
1483{
1484 int ret;
1485 int dev_fd;
1486 size_t i;
1487 mode_t mask;
1488 char *dev_path;
1489
1490 /*
1491 * Create a temp path for the /dev init. We'll relocate this to the
1492 * final location later on in the startup process.
1493 */
1494 dev_path = *dev_path_ret = strdup("/tmp/minijail.dev.XXXXXX");
1495 if (dev_path == NULL || mkdtemp(dev_path) == NULL)
1496 pdie("could not create temp path for /dev");
1497
1498 /* Set up the empty /dev mount point first. */
1499 ret = mount("minijail-devfs", dev_path, "tmpfs",
1500 MS_NOEXEC | MS_NOSUID, "size=5M,mode=755");
1501 if (ret) {
1502 rmdir(dev_path);
1503 return ret;
1504 }
1505
1506 /* We want to set the mode directly from the spec. */
1507 mask = umask(0);
1508
1509 /* Get a handle to the temp dev path for *at funcs below. */
1510 dev_fd = open(dev_path, O_DIRECTORY|O_PATH|O_CLOEXEC);
1511 if (dev_fd < 0) {
1512 ret = 1;
1513 goto done;
1514 }
1515
1516 /* Create all the nodes in /dev. */
1517 for (i = 0; i < ARRAY_SIZE(device_nodes); ++i) {
1518 const struct dev_spec *ds = &device_nodes[i];
1519 ret = mknodat(dev_fd, ds->name, ds->mode,
1520 makedev(ds->major, ds->minor));
1521 if (ret)
1522 goto done;
1523 }
1524
1525 /* Create all the symlinks in /dev. */
1526 for (i = 0; i < ARRAY_SIZE(device_symlinks); ++i) {
1527 const struct dev_sym_spec *ds = &device_symlinks[i];
1528 ret = symlinkat(ds->dest, dev_fd, ds->source);
1529 if (ret)
1530 goto done;
1531 }
1532
1533 /* Restore old mask. */
1534 done:
1535 close(dev_fd);
1536 umask(mask);
1537
1538 if (ret)
1539 mount_dev_cleanup(dev_path);
1540
1541 return ret;
1542}
1543
1544/*
1545 * Relocate the temporary /dev mount to its final /dev place.
1546 * We have to do this two step process so people can bind mount extra
1547 * /dev paths like /dev/log.
1548 */
1549static int mount_dev_finalize(const struct minijail *j, char *dev_path)
1550{
1551 int ret = -1;
1552 char *dest = NULL;
1553
1554 /* Unmount the /dev mount if possible. */
1555 if (umount2("/dev", MNT_DETACH))
1556 goto done;
1557
1558 if (asprintf(&dest, "%s/dev", j->chrootdir ? : "") < 0)
1559 goto done;
1560
1561 if (mount(dev_path, dest, NULL, MS_MOVE, NULL))
1562 goto done;
1563
1564 ret = 0;
1565 done:
1566 free(dest);
1567 mount_dev_cleanup(dev_path);
1568
1569 return ret;
1570}
1571
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001572/*
1573 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001574 * @j Minijail these mounts are for
1575 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001576 *
1577 * Returns 0 for success.
1578 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001579static int mount_one(const struct minijail *j, struct mountpoint *m,
1580 const char *dev_path)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001581{
Dylan Reid648b2202015-10-23 00:50:00 -07001582 int ret;
1583 char *dest;
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001584 int remount = 0;
1585 unsigned long original_mnt_flags = 0;
Dylan Reid648b2202015-10-23 00:50:00 -07001586
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -04001587 /* We assume |dest| has a leading "/". */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001588 if (dev_path && strncmp("/dev/", m->dest, 5) == 0) {
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -04001589 /*
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -04001590 * Since the temp path is rooted at /dev, skip that dest part.
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -04001591 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001592 if (asprintf(&dest, "%s%s", dev_path, m->dest + 4) < 0)
1593 return -ENOMEM;
1594 } else {
Mike Frysingerac08a682017-10-10 02:04:50 -04001595 if (asprintf(&dest, "%s%s", j->chrootdir ?: "", m->dest) < 0)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001596 return -ENOMEM;
1597 }
Dylan Reid648b2202015-10-23 00:50:00 -07001598
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001599 ret =
1600 setup_mount_destination(m->src, dest, j->uid, j->gid,
1601 (m->flags & MS_BIND), &original_mnt_flags);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001602 if (ret) {
François Degrosa42182d2020-04-29 00:41:52 +10001603 warn("cannot create mount target '%s'", dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001604 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001605 }
Dylan Reideec77962016-06-30 19:35:10 -07001606
Dylan Reid648b2202015-10-23 00:50:00 -07001607 /*
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001608 * Bind mounts that change the 'ro' flag have to be remounted since
1609 * 'bind' and other flags can't both be specified in the same command.
1610 * Remount after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001611 */
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001612 if ((m->flags & MS_BIND) &&
1613 ((m->flags & MS_RDONLY) != (original_mnt_flags & MS_RDONLY))) {
1614 remount = 1;
1615 /*
1616 * Restrict the mount flags to those that are user-settable in a
1617 * MS_REMOUNT request, but excluding MS_RDONLY. The
1618 * user-requested mount flags will dictate whether the remount
1619 * will have that flag or not.
1620 */
1621 original_mnt_flags &= (MS_USER_SETTABLE_MASK & ~MS_RDONLY);
Elly Jonesa1059632011-12-15 15:17:07 -05001622 }
Dylan Reid648b2202015-10-23 00:50:00 -07001623
Dylan Reid81e23972016-05-18 14:06:35 -07001624 ret = mount(m->src, dest, m->type, m->flags, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001625 if (ret) {
François Degrosa42182d2020-04-29 00:41:52 +10001626 pwarn("cannot bind-mount '%s' as '%s' with flags %#lx", m->src,
1627 dest, m->flags);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001628 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001629 }
Dylan Reid648b2202015-10-23 00:50:00 -07001630
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001631 if (remount) {
1632 ret =
1633 mount(m->src, dest, NULL,
1634 m->flags | original_mnt_flags | MS_REMOUNT, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001635 if (ret) {
François Degrosa42182d2020-04-29 00:41:52 +10001636 pwarn(
1637 "cannot bind-remount '%s' as '%s' with flags %#lx",
1638 m->src, dest,
1639 m->flags | original_mnt_flags | MS_REMOUNT);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001640 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001641 }
Dylan Reid648b2202015-10-23 00:50:00 -07001642 }
1643
Elly Jones51a5b6c2011-10-12 19:09:26 -04001644 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001645 if (m->next)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001646 return mount_one(j, m->next, dev_path);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001647 return 0;
1648
1649error:
1650 free(dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001651 return ret;
1652}
1653
Mike Frysingerac08a682017-10-10 02:04:50 -04001654static void process_mounts_or_die(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001655{
Mike Frysingerac08a682017-10-10 02:04:50 -04001656 /*
1657 * We have to mount /dev first in case there are bind mounts from
1658 * the original /dev into the new unique tmpfs one.
1659 */
1660 char *dev_path = NULL;
1661 if (j->flags.mount_dev && mount_dev(&dev_path))
1662 pdie("mount_dev failed");
Dylan Reid648b2202015-10-23 00:50:00 -07001663
Mike Frysingerac08a682017-10-10 02:04:50 -04001664 if (j->mounts_head && mount_one(j, j->mounts_head, dev_path)) {
François Degrosa42182d2020-04-29 00:41:52 +10001665 if (dev_path)
Mike Frysingerac08a682017-10-10 02:04:50 -04001666 mount_dev_cleanup(dev_path);
François Degrosa42182d2020-04-29 00:41:52 +10001667
1668 _exit(MINIJAIL_ERR_MOUNT);
Mike Frysingerac08a682017-10-10 02:04:50 -04001669 }
Mike Frysinger33ffef32017-01-13 19:53:19 -05001670
1671 /*
Mike Frysingerac08a682017-10-10 02:04:50 -04001672 * Once all bind mounts have been processed, move the temp dev to
1673 * its final /dev home.
Mike Frysinger33ffef32017-01-13 19:53:19 -05001674 */
1675 if (j->flags.mount_dev && mount_dev_finalize(j, dev_path))
Mike Frysingerac08a682017-10-10 02:04:50 -04001676 pdie("mount_dev_finalize failed");
1677}
Elly Jones51a5b6c2011-10-12 19:09:26 -04001678
Mike Frysingerac08a682017-10-10 02:04:50 -04001679static int enter_chroot(const struct minijail *j)
1680{
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001681 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1682
Elly Jones51a5b6c2011-10-12 19:09:26 -04001683 if (chroot(j->chrootdir))
1684 return -errno;
1685
1686 if (chdir("/"))
1687 return -errno;
1688
1689 return 0;
1690}
1691
Mike Frysingerac08a682017-10-10 02:04:50 -04001692static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001693{
Mike Frysingerac08a682017-10-10 02:04:50 -04001694 int oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001695
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001696 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1697
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001698 /*
1699 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001700 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001701 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001702 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001703 if (oldroot < 0)
1704 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001705 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001706 if (newroot < 0)
1707 pdie("failed to open %s for fchdir", j->chrootdir);
1708
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001709 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001710 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001711 * do a self bind mount.
1712 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001713 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1714 pdie("failed to bind mount '%s'", j->chrootdir);
1715 if (chdir(j->chrootdir))
1716 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001717 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001718 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001719
1720 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001721 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001722 * change to the old root and unmount it.
1723 */
1724 if (fchdir(oldroot))
1725 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001726
1727 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05001728 * If skip_remount_private was enabled for minijail_enter(),
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001729 * there could be a shared mount point under |oldroot|. In that case,
1730 * mounts under this shared mount point will be unmounted below, and
1731 * this unmounting will propagate to the original mount namespace
1732 * (because the mount point is shared). To prevent this unexpected
1733 * unmounting, remove these mounts from their peer groups by recursively
1734 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001735 */
1736 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001737 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001738 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001739 if (umount2(".", MNT_DETACH))
1740 pdie("umount(/)");
1741 /* Change back to the new root. */
1742 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001743 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001744 if (close(oldroot))
1745 return -errno;
1746 if (close(newroot))
1747 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001748 if (chroot("/"))
1749 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001750 /* Set correct CWD for getcwd(3). */
1751 if (chdir("/"))
1752 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001753
1754 return 0;
1755}
1756
Martin Pelikánab9eb442017-01-25 11:53:58 +11001757static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001758{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001759 const char fmt[] = "size=%zu,mode=1777";
1760 /* Count for the user storing ULLONG_MAX literally + extra space. */
1761 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1762 int ret;
1763
1764 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1765
1766 if (ret <= 0)
1767 pdie("tmpfs size spec error");
1768 else if ((size_t)ret >= sizeof(data))
1769 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001770 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001771 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001772}
1773
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001774static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001775{
1776 const char *kProcPath = "/proc";
1777 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001778 /*
1779 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001780 * /proc in our namespace, which means using MS_REMOUNT here would
1781 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001782 * namespace (!). Instead, remove their mount from our namespace lazily
1783 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001784 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001785 if (umount2(kProcPath, MNT_DETACH)) {
1786 /*
1787 * If we are in a new user namespace, umount(2) will fail.
1788 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1789 */
1790 if (j->flags.userns) {
1791 info("umount(/proc, MNT_DETACH) failed, "
1792 "this is expected when using user namespaces");
1793 } else {
1794 return -errno;
1795 }
1796 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001797 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001798 return -errno;
1799 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001800}
1801
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001802static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001803{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001804 kill(j->initpid, SIGKILL);
1805 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001806}
1807
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001808static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001809{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001810 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001811 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001812}
1813
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001814static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001815{
1816 size_t i;
1817
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001818 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001819 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001820 kill_child_and_die(j, "failed to add to cgroups");
1821 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001822}
1823
Dylan Reid0f72ef42017-06-06 15:42:49 -07001824static void set_rlimits_or_die(const struct minijail *j)
1825{
1826 size_t i;
1827
1828 for (i = 0; i < j->rlimit_count; ++i) {
1829 struct rlimit limit;
1830 limit.rlim_cur = j->rlimits[i].cur;
1831 limit.rlim_max = j->rlimits[i].max;
1832 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1833 kill_child_and_die(j, "failed to set rlimit");
1834 }
1835}
1836
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001837static void write_ugid_maps_or_die(const struct minijail *j)
1838{
1839 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1840 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001841 if (j->gidmap && j->flags.disable_setgroups) {
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001842 /*
1843 * Older kernels might not have the /proc/<pid>/setgroups files.
1844 */
Mike Frysinger6b190c02017-01-04 17:18:42 -05001845 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001846 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001847 if (ret == -ENOENT) {
1848 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1849 warn("could not disable setgroups(2)");
1850 } else
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001851 kill_child_and_die(
1852 j, "failed to disable setgroups(2)");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001853 }
1854 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001855 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1856 kill_child_and_die(j, "failed to write gid_map");
1857}
1858
1859static void enter_user_namespace(const struct minijail *j)
1860{
Luis Hector Chavez71323552017-09-05 09:17:22 -07001861 int uid = j->flags.uid ? j->uid : 0;
1862 int gid = j->flags.gid ? j->gid : 0;
1863 if (j->gidmap && setresgid(gid, gid, gid)) {
1864 pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid,
1865 gid);
1866 }
1867 if (j->uidmap && setresuid(uid, uid, uid)) {
1868 pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid,
1869 uid);
1870 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001871}
1872
1873static void parent_setup_complete(int *pipe_fds)
1874{
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001875 close_and_reset(&pipe_fds[0]);
1876 close_and_reset(&pipe_fds[1]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001877}
1878
1879/*
1880 * wait_for_parent_setup: Called by the child process to wait for any
1881 * further parent-side setup to complete before continuing.
1882 */
1883static void wait_for_parent_setup(int *pipe_fds)
1884{
1885 char buf;
1886
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001887 close_and_reset(&pipe_fds[1]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001888
1889 /* Wait for parent to complete setup and close the pipe. */
1890 if (read(pipe_fds[0], &buf, 1) != 0)
1891 die("failed to sync with parent");
Mattias Nissler6123e5a2020-02-11 13:38:03 +01001892 close_and_reset(&pipe_fds[0]);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001893}
1894
1895static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001896{
Lutz Justen13807cb2017-01-03 17:11:55 +01001897 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1898 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001899 die("can only do one of inherit, keep, or set supplementary "
1900 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001901 }
1902
Lutz Justen13807cb2017-01-03 17:11:55 +01001903 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001904 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001905 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001906 } else if (j->flags.set_suppl_gids) {
1907 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001908 pdie("setgroups(suppl_gids) failed");
Luis Hector Chavez71323552017-09-05 09:17:22 -07001909 } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001910 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001911 * Only attempt to clear supplementary groups if we are changing
Luis Hector Chavez71323552017-09-05 09:17:22 -07001912 * users or groups, and if the caller did not request to disable
1913 * setgroups (used when entering a user namespace as a
1914 * non-privileged user).
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001915 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001916 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001917 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001918 }
1919
1920 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001921 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001922
1923 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001924 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001925}
1926
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001927static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1928{
1929 const uint64_t one = 1;
1930 unsigned int i;
1931 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1932 if (keep_mask & (one << i))
1933 continue;
1934 if (prctl(PR_CAPBSET_DROP, i))
1935 pdie("could not drop capability from bounding set");
1936 }
1937}
1938
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001939static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001940{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001941 if (!j->flags.use_caps)
1942 return;
1943
Elly Jonese1749eb2011-10-07 13:54:59 -04001944 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001945 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001946 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001947 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001948 unsigned int i;
1949 if (!caps)
1950 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001951 if (cap_clear(caps))
1952 die("can't clear caps");
1953
1954 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001955 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001956 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001957 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001958 flag[0] = i;
1959 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001960 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001961 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001962 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001963 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001964 die("can't add inheritable cap");
1965 }
1966 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001967 die("can't apply initial cleaned capset");
1968
1969 /*
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001970 * Instead of dropping the bounding set first, do it here in case
Kees Cook323878a2013-02-05 15:35:24 -08001971 * the caller had a more permissive bounding set which could
1972 * have been used above to raise a capability that wasn't already
1973 * present. This requires CAP_SETPCAP, so we raised/kept it above.
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001974 *
1975 * However, if we're asked to skip setting *and* locking the
1976 * SECURE_NOROOT securebit, also skip dropping the bounding set.
1977 * If the caller wants to regain all capabilities when executing a
1978 * set-user-ID-root program, allow them to do so. The default behavior
1979 * (i.e. the behavior without |securebits_skip_mask| set) will still put
1980 * the jailed process tree in a capabilities-only environment.
1981 *
1982 * We check the negated skip mask for SECURE_NOROOT and
1983 * SECURE_NOROOT_LOCKED. If the bits are set in the negated mask they
1984 * will *not* be skipped in lock_securebits(), and therefore we should
1985 * drop the bounding set.
Kees Cook323878a2013-02-05 15:35:24 -08001986 */
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001987 if (secure_noroot_set_and_locked(~j->securebits_skip_mask)) {
1988 drop_capbset(j->caps, last_valid_cap);
1989 } else {
1990 warn("SECURE_NOROOT not set, not dropping bounding set");
1991 }
Kees Cook323878a2013-02-05 15:35:24 -08001992
1993 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001994 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001995 flag[0] = CAP_SETPCAP;
1996 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1997 die("can't clear effective cap");
1998 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1999 die("can't clear permitted cap");
2000 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
2001 die("can't clear inheritable cap");
2002 }
2003
2004 if (cap_set_proc(caps))
2005 die("can't apply final cleaned capset");
2006
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04002007 /*
2008 * If ambient capabilities are supported, clear all capabilities first,
2009 * then raise the requested ones.
2010 */
2011 if (j->flags.set_ambient_caps) {
2012 if (!cap_ambient_supported()) {
2013 pdie("ambient capabilities not supported");
2014 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04002015 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
2016 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04002017 pdie("can't clear ambient capabilities");
2018 }
2019
2020 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
2021 if (!(j->caps & (one << i)))
2022 continue;
2023
2024 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
2025 0) != 0) {
2026 pdie("prctl(PR_CAP_AMBIENT, "
2027 "PR_CAP_AMBIENT_RAISE, %u) failed",
2028 i);
2029 }
2030 }
2031 }
2032
Kees Cook323878a2013-02-05 15:35:24 -08002033 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04002034}
2035
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002036static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002037{
2038 /*
2039 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
2040 * in the kernel source tree for an explanation of the parameters.
2041 */
2042 if (j->flags.no_new_privs) {
2043 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
2044 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
2045 }
2046
2047 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07002048 * Code running with ASan
2049 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
2050 * will make system calls not included in the syscall filter policy,
2051 * which will likely crash the program. Skip setting seccomp filter in
2052 * that case.
2053 * 'running_with_asan()' has no inputs and is completely defined at
2054 * build time, so this cannot be used by an attacker to skip setting
2055 * seccomp filter.
2056 */
Evgenii Stepanov3d98f3c2018-08-23 15:06:50 -07002057 if (j->flags.seccomp_filter && running_with_asan()) {
Evgenii Stepanov825828c2018-07-27 11:57:07 -07002058 warn("running with (HW)ASan, not setting seccomp filter");
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07002059 return;
2060 }
2061
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002062 if (j->flags.seccomp_filter) {
2063 if (j->flags.seccomp_filter_logging) {
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002064 warn("logging seccomp filter failures");
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -04002065 if (!seccomp_ret_log_available()) {
2066 /*
2067 * If SECCOMP_RET_LOG is not available,
2068 * install the SIGSYS handler first.
2069 */
2070 if (install_sigsys_handler())
2071 pdie(
2072 "failed to install SIGSYS handler");
2073 }
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002074 } else if (j->flags.seccomp_filter_tsync) {
2075 /*
2076 * If setting thread sync,
2077 * reset the SIGSYS signal handler so that
2078 * the entire thread group is killed.
2079 */
2080 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
2081 pdie("failed to reset SIGSYS disposition");
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04002082 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002083 }
2084
2085 /*
2086 * Install the syscall filter.
2087 */
2088 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002089 if (j->flags.seccomp_filter_tsync) {
2090 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
2091 SECCOMP_FILTER_FLAG_TSYNC,
2092 j->filter_prog)) {
2093 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002094 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002095 } else {
2096 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
2097 j->filter_prog)) {
2098 pdie("prctl(seccomp_filter) failed");
2099 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002100 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002101 }
2102}
2103
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002104static pid_t forward_pid = -1;
2105
Mike Frysinger33d051a2018-05-30 16:41:10 -04002106static void forward_signal(int sig,
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04002107 siginfo_t *siginfo attribute_unused,
2108 void *void_context attribute_unused)
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002109{
2110 if (forward_pid != -1) {
Mike Frysinger33d051a2018-05-30 16:41:10 -04002111 kill(forward_pid, sig);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002112 }
2113}
2114
2115static void install_signal_handlers(void)
2116{
2117 struct sigaction act;
2118
2119 memset(&act, 0, sizeof(act));
2120 act.sa_sigaction = &forward_signal;
2121 act.sa_flags = SA_SIGINFO | SA_RESTART;
2122
2123 /* Handle all signals, except SIGCHLD. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002124 for (int sig = 1; sig < NSIG; sig++) {
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002125 /*
2126 * We don't care if we get EINVAL: that just means that we
2127 * can't handle this signal, so let's skip it and continue.
2128 */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002129 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002130 }
2131 /* Reset SIGCHLD's handler. */
2132 signal(SIGCHLD, SIG_DFL);
2133
2134 /* Handle real-time signals. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04002135 for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++) {
2136 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002137 }
2138}
2139
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002140static const char *lookup_hook_name(minijail_hook_event_t event)
2141{
2142 switch (event) {
2143 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
2144 return "pre-drop-caps";
2145 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
2146 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07002147 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
2148 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002149 case MINIJAIL_HOOK_EVENT_MAX:
2150 /*
2151 * Adding this in favor of a default case to force the
2152 * compiler to error out if a new enum value is added.
2153 */
2154 break;
2155 }
2156 return "unknown";
2157}
2158
2159static void run_hooks_or_die(const struct minijail *j,
2160 minijail_hook_event_t event)
2161{
2162 int rc;
2163 int hook_index = 0;
2164 for (struct hook *c = j->hooks_head; c; c = c->next) {
2165 if (c->event != event)
2166 continue;
2167 rc = c->hook(c->payload);
2168 if (rc != 0) {
2169 errno = -rc;
2170 pdie("%s hook (index %d) failed",
2171 lookup_hook_name(event), hook_index);
2172 }
2173 /* Only increase the index within the same hook event type. */
2174 ++hook_index;
2175 }
2176}
2177
Will Drewry6ac91122011-10-21 16:38:58 -05002178void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002179{
Dylan Reidf682d472015-09-17 21:39:07 -07002180 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002181 * If we're dropping caps, get the last valid cap from /proc now,
2182 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07002183 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002184 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002185 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002186 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07002187
Elly Jonese1749eb2011-10-07 13:54:59 -04002188 if (j->flags.pids)
2189 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002190 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04002191
Lutz Justen13807cb2017-01-03 17:11:55 +01002192 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05002193 die("cannot inherit supplementary groups without setting a "
2194 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04002195
Elly Jonesdd3e8512012-01-23 15:13:38 -05002196 /*
2197 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04002198 * so we don't even try. If any of our operations fail, we abort() the
2199 * entire process.
2200 */
Mike Frysinger902a4492018-12-27 05:22:56 -05002201 if (j->flags.enter_vfs) {
2202 if (setns(j->mountns_fd, CLONE_NEWNS))
2203 pdie("setns(CLONE_NEWNS) failed");
2204 close(j->mountns_fd);
2205 }
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002206
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07002207 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002208 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002209 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002210 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05002211 * By default, remount all filesystems as private, unless
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04002212 * - Passed a specific remount mode, in which case remount with
2213 * that,
2214 * - Asked not to remount at all, in which case skip the
2215 * mount(2) call.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002216 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
2217 */
Mike Frysinger785b1c32018-02-23 15:47:24 -05002218 if (j->remount_mode) {
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04002219 if (mount(NULL, "/", NULL, MS_REC | j->remount_mode,
2220 NULL))
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -05002221 pdie("mount(NULL, /, NULL, "
2222 "MS_REC | j->remount_mode, NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002223 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002224 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04002225
Dylan Reidf7942472015-11-18 17:55:26 -08002226 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002227 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08002228 }
2229
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002230 if (j->flags.uts) {
2231 if (unshare(CLONE_NEWUTS))
2232 pdie("unshare(CLONE_NEWUTS) failed");
2233
2234 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
2235 pdie("sethostname(%s) failed", j->hostname);
2236 }
2237
Dylan Reid1102f5a2015-09-15 11:52:20 -07002238 if (j->flags.enter_net) {
2239 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002240 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger902a4492018-12-27 05:22:56 -05002241 close(j->netns_fd);
Mike Frysinger7559dfe2016-11-15 18:58:39 -05002242 } else if (j->flags.net) {
2243 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002244 pdie("unshare(CLONE_NEWNET) failed");
2245 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07002246 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002247
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002248 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002249 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002250
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08002251 if (j->flags.new_session_keyring) {
2252 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
2253 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
2254 }
2255
Mike Frysingerac08a682017-10-10 02:04:50 -04002256 /* We have to process all the mounts before we chroot/pivot_root. */
2257 process_mounts_or_die(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002258
Mike Frysingerac08a682017-10-10 02:04:50 -04002259 if (j->flags.chroot && enter_chroot(j))
Mike Frysinger33ffef32017-01-13 19:53:19 -05002260 pdie("chroot");
Mike Frysinger33ffef32017-01-13 19:53:19 -05002261
Mike Frysingerac08a682017-10-10 02:04:50 -04002262 if (j->flags.pivot_root && enter_pivot_root(j))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08002263 pdie("pivot_root");
2264
Martin Pelikánab9eb442017-01-25 11:53:58 +11002265 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07002266 pdie("mount_tmp");
2267
Dylan Reid791f5772015-09-14 20:02:42 -07002268 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04002269 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04002270
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002271 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
2272
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002273 /*
2274 * If we're only dropping capabilities from the bounding set, but not
2275 * from the thread's (permitted|inheritable|effective) sets, do it now.
2276 */
2277 if (j->flags.capbset_drop) {
2278 drop_capbset(j->cap_bset, last_valid_cap);
2279 }
2280
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002281 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002282 * POSIX capabilities are a bit tricky. We must set SECBIT_KEEP_CAPS
2283 * before drop_ugid() below as the latter would otherwise drop all
2284 * capabilities.
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002285 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002286 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002287 /*
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002288 * When using ambient capabilities, CAP_SET{GID,UID} can be
2289 * inherited across execve(2), so SECBIT_KEEP_CAPS is not
2290 * strictly needed.
Elly Jonese1749eb2011-10-07 13:54:59 -04002291 */
Mattias Nissler48b5ff12018-10-11 15:31:41 +02002292 bool require_keep_caps = !j->flags.set_ambient_caps;
2293 if (lock_securebits(j->securebits_skip_mask,
2294 require_keep_caps) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002295 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002296 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002297 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002298
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002299 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002300 /*
2301 * If we're setting no_new_privs, we can drop privileges
2302 * before setting seccomp filter. This way filter policies
2303 * don't need to allow privilege-dropping syscalls.
2304 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002305 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002306 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002307 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002308 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002309 /*
2310 * If we're not setting no_new_privs,
2311 * we need to set seccomp filter *before* dropping privileges.
2312 * WARNING: this means that filter policies *must* allow
2313 * setgroups()/setresgid()/setresuid() for dropping root and
2314 * capget()/capset()/prctl() for dropping caps.
2315 */
2316 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002317 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002318 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04002319 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002320
Elly Jonesdd3e8512012-01-23 15:13:38 -05002321 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08002322 * Select the specified alternate syscall table. The table must not
2323 * block prctl(2) if we're using seccomp as well.
2324 */
2325 if (j->flags.alt_syscall) {
2326 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002327 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08002328 }
2329
2330 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05002331 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04002332 * privilege-dropping syscalls :)
2333 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002334 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04002335 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002336 warn("seccomp not supported");
2337 return;
2338 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002339 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002340 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002341}
2342
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002343/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04002344static int init_exitstatus = 0;
2345
Mike Frysinger0a27ab02020-09-04 16:18:12 -04002346static void init_term(int sig attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002347{
2348 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04002349}
2350
Mike Frysinger0a27ab02020-09-04 16:18:12 -04002351static void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04002352{
2353 pid_t pid;
2354 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002355 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002356 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002357 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002358 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002359 /*
2360 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04002361 * left inside our pid namespace or we get a signal.
2362 */
2363 if (pid == rootpid)
2364 init_exitstatus = status;
2365 }
2366 if (!WIFEXITED(init_exitstatus))
2367 _exit(MINIJAIL_ERR_INIT);
2368 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04002369}
2370
Will Drewry6ac91122011-10-21 16:38:58 -05002371int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002372{
2373 size_t sz = 0;
2374 size_t bytes = read(fd, &sz, sizeof(sz));
2375 char *buf;
2376 int r;
2377 if (sizeof(sz) != bytes)
2378 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002379 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04002380 return -E2BIG;
2381 buf = malloc(sz);
2382 if (!buf)
2383 return -ENOMEM;
2384 bytes = read(fd, buf, sz);
2385 if (bytes != sz) {
2386 free(buf);
2387 return -EINVAL;
2388 }
2389 r = minijail_unmarshal(j, buf, sz);
2390 free(buf);
2391 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002392}
2393
Will Drewry6ac91122011-10-21 16:38:58 -05002394int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04002395{
Elly Jonese1749eb2011-10-07 13:54:59 -04002396 size_t sz = minijail_size(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002397 if (!sz)
2398 return -EINVAL;
François Degros664eba72019-11-05 13:18:24 +11002399
2400 char *buf = malloc(sz);
2401 if (!buf)
2402 return -ENOMEM;
2403
2404 int err = minijail_marshal(j, buf, sz);
2405 if (err)
2406 goto error;
2407
Elly Jonese1749eb2011-10-07 13:54:59 -04002408 /* Sends [size][minijail]. */
François Degros664eba72019-11-05 13:18:24 +11002409 err = write_exactly(fd, &sz, sizeof(sz));
2410 if (err)
2411 goto error;
2412
2413 err = write_exactly(fd, buf, sz);
2414
2415error:
Elly Jonese1749eb2011-10-07 13:54:59 -04002416 free(buf);
François Degros664eba72019-11-05 13:18:24 +11002417 return err;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002418}
Elly Jonescd7a9042011-07-22 13:56:51 -04002419
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002420static int setup_preload(const struct minijail *j attribute_unused,
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002421 char ***child_env attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002422{
Daniel Erat5b7a3182015-08-19 16:06:22 -06002423#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002424 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002425 return 0;
2426#else
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002427 const char *preload_path = j->preload_path ?: PRELOADPATH;
2428 char *newenv = NULL;
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002429 int ret = 0;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002430 const char *oldenv = getenv(kLdPreloadEnvVar);
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002431
2432 if (!oldenv)
2433 oldenv = "";
Elly Jonescd7a9042011-07-22 13:56:51 -04002434
Elly Jonese1749eb2011-10-07 13:54:59 -04002435 /* Only insert a separating space if we have something to separate... */
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002436 if (asprintf(&newenv, "%s%s%s", oldenv, oldenv[0] != '\0' ? " " : "",
2437 preload_path) < 0) {
2438 return -1;
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002439 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002440
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002441 ret = minijail_setenv(child_env, kLdPreloadEnvVar, newenv, 1);
Luis Hector Chavezd1d24d22019-02-11 17:59:21 -08002442 free(newenv);
2443 return ret;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002444#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04002445}
2446
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002447static int setup_pipe(char ***child_env, int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04002448{
2449 int r = pipe(fds);
2450 char fd_buf[11];
2451 if (r)
2452 return r;
2453 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2454 if (r <= 0)
2455 return -EINVAL;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002456 return minijail_setenv(child_env, kFdEnvVar, fd_buf, 1);
Will Drewryf89aef52011-09-16 16:48:57 -05002457}
2458
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002459static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002460{
2461 const char *kFdPath = "/proc/self/fd";
2462
2463 DIR *d = opendir(kFdPath);
2464 struct dirent *dir_entry;
2465
2466 if (d == NULL)
2467 return -1;
2468 int dir_fd = dirfd(d);
2469 while ((dir_entry = readdir(d)) != NULL) {
2470 size_t i;
2471 char *end;
2472 bool should_close = true;
2473 const int fd = strtol(dir_entry->d_name, &end, 10);
2474
2475 if ((*end) != '\0') {
2476 continue;
2477 }
2478 /*
2479 * We might have set up some pipes that we want to share with
2480 * the parent process, and should not be closed.
2481 */
2482 for (i = 0; i < size; ++i) {
2483 if (fd == inheritable_fds[i]) {
2484 should_close = false;
2485 break;
2486 }
2487 }
2488 /* Also avoid closing the directory fd. */
2489 if (should_close && fd != dir_fd)
2490 close(fd);
2491 }
2492 closedir(d);
2493 return 0;
2494}
2495
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002496static int redirect_fds(struct minijail *j)
2497{
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002498 for (size_t i = 0; i < j->preserved_fd_count; i++) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002499 if (dup2(j->preserved_fds[i].parent_fd,
2500 j->preserved_fds[i].child_fd) == -1) {
2501 return -1;
2502 }
2503 }
Mattias Nissler1cf29fb2020-04-20 23:14:03 +02002504 /*
2505 * After all fds have been duped, we are now free to close all parent
2506 * fds that are *not* child fds.
2507 */
2508 for (size_t i = 0; i < j->preserved_fd_count; i++) {
2509 int closeable = true;
2510 for (size_t i2 = 0; i2 < j->preserved_fd_count; i2++) {
2511 closeable &= j->preserved_fds[i].parent_fd !=
2512 j->preserved_fds[i2].child_fd;
2513 }
2514 if (closeable)
2515 close(j->preserved_fds[i].parent_fd);
2516 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002517 return 0;
2518}
2519
Dylan Reidacfb8be2017-08-25 12:56:51 -07002520/*
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002521 * Structure holding resources and state created when running a minijail.
2522 */
2523struct minijail_run_state {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002524 pid_t child_pid;
2525 int pipe_fds[2];
2526 int stdin_fds[2];
2527 int stdout_fds[2];
2528 int stderr_fds[2];
2529 int child_sync_pipe_fds[2];
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002530 char **child_env;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002531};
2532
2533static void minijail_free_run_state(struct minijail_run_state *state)
2534{
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002535 state->child_pid = -1;
2536
2537 int *fd_pairs[] = {state->pipe_fds, state->stdin_fds, state->stdout_fds,
2538 state->stderr_fds, state->child_sync_pipe_fds};
2539 for (size_t i = 0; i < ARRAY_SIZE(fd_pairs); ++i) {
2540 close_and_reset(&fd_pairs[i][0]);
2541 close_and_reset(&fd_pairs[i][1]);
2542 }
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002543
2544 minijail_free_env(state->child_env);
2545 state->child_env = NULL;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002546}
2547
2548/* Set up stdin/stdout/stderr file descriptors in the child. */
2549static void setup_child_std_fds(struct minijail *j,
2550 struct minijail_run_state *state)
2551{
2552 struct {
2553 const char *name;
2554 int from;
2555 int to;
2556 } fd_map[] = {
2557 {"stdin", state->stdin_fds[0], STDIN_FILENO},
2558 {"stdout", state->stdout_fds[1], STDOUT_FILENO},
2559 {"stderr", state->stderr_fds[1], STDERR_FILENO},
2560 };
2561
2562 for (size_t i = 0; i < ARRAY_SIZE(fd_map); ++i) {
2563 if (fd_map[i].from != -1) {
2564 if (dup2(fd_map[i].from, fd_map[i].to) == -1)
2565 die("failed to set up %s pipe", fd_map[i].name);
2566 }
2567 }
2568
2569 /* Close temporary pipe file descriptors. */
2570 int *std_pipes[] = {state->stdin_fds, state->stdout_fds,
2571 state->stderr_fds};
2572 for (size_t i = 0; i < ARRAY_SIZE(std_pipes); ++i) {
2573 close_and_reset(&std_pipes[i][0]);
2574 close_and_reset(&std_pipes[i][1]);
2575 }
2576
2577 /*
2578 * If any of stdin, stdout, or stderr are TTYs, or setsid flag is
2579 * set, create a new session. This prevents the jailed process from
2580 * using the TIOCSTI ioctl to push characters into the parent process
2581 * terminal's input buffer, therefore escaping the jail.
2582 *
2583 * Since it has just forked, the child will not be a process group
2584 * leader, and this call to setsid() should always succeed.
2585 */
2586 if (j->flags.setsid || isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2587 isatty(STDERR_FILENO)) {
2588 if (setsid() < 0) {
2589 pdie("setsid() failed");
2590 }
2591 }
2592}
2593
2594/*
Dylan Reidacfb8be2017-08-25 12:56:51 -07002595 * Structure that specifies how to start a minijail.
2596 *
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002597 * filename - The program to exec in the child. Required if |exec_in_child| = 1.
2598 * argv - Arguments for the child program. Required if |exec_in_child| = 1.
2599 * envp - Environment for the child program. Available if |exec_in_child| = 1.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002600 * use_preload - If true use LD_PRELOAD.
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002601 * exec_in_child - If true, run |filename|. Otherwise, the child will return to
Dylan Reid0412dcc2017-08-24 11:33:15 -07002602 * the caller.
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002603 * pstdin_fd - Filled with stdin pipe if non-NULL.
2604 * pstdout_fd - Filled with stdout pipe if non-NULL.
2605 * pstderr_fd - Filled with stderr pipe if non-NULL.
2606 * pchild_pid - Filled with the pid of the child process if non-NULL.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002607 */
2608struct minijail_run_config {
2609 const char *filename;
2610 char *const *argv;
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002611 char *const *envp;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002612 int use_preload;
Dylan Reid0412dcc2017-08-24 11:33:15 -07002613 int exec_in_child;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002614 int *pstdin_fd;
2615 int *pstdout_fd;
2616 int *pstderr_fd;
2617 pid_t *pchild_pid;
2618};
2619
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002620static int
2621minijail_run_config_internal(struct minijail *j,
2622 const struct minijail_run_config *config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002623
Will Drewry6ac91122011-10-21 16:38:58 -05002624int API minijail_run(struct minijail *j, const char *filename,
2625 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002626{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002627 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002628 .filename = filename,
2629 .argv = argv,
2630 .envp = NULL,
2631 .use_preload = true,
2632 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002633 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002634 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002635}
2636
2637int API minijail_run_pid(struct minijail *j, const char *filename,
2638 char *const argv[], pid_t *pchild_pid)
2639{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002640 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002641 .filename = filename,
2642 .argv = argv,
2643 .envp = NULL,
2644 .use_preload = true,
2645 .exec_in_child = true,
2646 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002647 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002648 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002649}
2650
2651int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002652 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002653{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002654 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002655 .filename = filename,
2656 .argv = argv,
2657 .envp = NULL,
2658 .use_preload = true,
2659 .exec_in_child = true,
2660 .pstdin_fd = pstdin_fd,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002661 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002662 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002663}
2664
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002665int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002666 char *const argv[], pid_t *pchild_pid,
2667 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002668{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002669 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002670 .filename = filename,
2671 .argv = argv,
2672 .envp = NULL,
2673 .use_preload = true,
2674 .exec_in_child = true,
2675 .pstdin_fd = pstdin_fd,
2676 .pstdout_fd = pstdout_fd,
2677 .pstderr_fd = pstderr_fd,
2678 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002679 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002680 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002681}
2682
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002683int API minijail_run_env_pid_pipes(struct minijail *j, const char *filename,
2684 char *const argv[], char *const envp[],
2685 pid_t *pchild_pid, int *pstdin_fd,
2686 int *pstdout_fd, int *pstderr_fd)
2687{
2688 struct minijail_run_config config = {
2689 .filename = filename,
2690 .argv = argv,
2691 .envp = envp,
2692 .use_preload = true,
2693 .exec_in_child = true,
2694 .pstdin_fd = pstdin_fd,
2695 .pstdout_fd = pstdout_fd,
2696 .pstderr_fd = pstderr_fd,
2697 .pchild_pid = pchild_pid,
2698 };
2699 return minijail_run_config_internal(j, &config);
2700}
2701
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002702int API minijail_run_no_preload(struct minijail *j, const char *filename,
2703 char *const argv[])
2704{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002705 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002706 .filename = filename,
2707 .argv = argv,
2708 .envp = NULL,
2709 .use_preload = false,
2710 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002711 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002712 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002713}
2714
Samuel Tan63187f42015-10-16 13:01:53 -07002715int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002716 const char *filename,
2717 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002718 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002719 int *pstdin_fd,
2720 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002721 int *pstderr_fd)
2722{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002723 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002724 .filename = filename,
2725 .argv = argv,
2726 .envp = NULL,
2727 .use_preload = false,
2728 .exec_in_child = true,
2729 .pstdin_fd = pstdin_fd,
2730 .pstdout_fd = pstdout_fd,
2731 .pstderr_fd = pstderr_fd,
2732 .pchild_pid = pchild_pid,
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002733 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002734 return minijail_run_config_internal(j, &config);
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -05002735}
2736
2737int API minijail_run_env_pid_pipes_no_preload(struct minijail *j,
2738 const char *filename,
2739 char *const argv[],
2740 char *const envp[],
2741 pid_t *pchild_pid, int *pstdin_fd,
2742 int *pstdout_fd, int *pstderr_fd)
2743{
2744 struct minijail_run_config config = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002745 .filename = filename,
2746 .argv = argv,
2747 .envp = envp,
2748 .use_preload = false,
2749 .exec_in_child = true,
2750 .pstdin_fd = pstdin_fd,
2751 .pstdout_fd = pstdout_fd,
2752 .pstderr_fd = pstderr_fd,
2753 .pchild_pid = pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002754 };
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002755 return minijail_run_config_internal(j, &config);
Samuel Tan63187f42015-10-16 13:01:53 -07002756}
2757
Dylan Reid0412dcc2017-08-24 11:33:15 -07002758pid_t API minijail_fork(struct minijail *j)
2759{
2760 struct minijail_run_config config = {};
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002761 return minijail_run_config_internal(j, &config);
Dylan Reid0412dcc2017-08-24 11:33:15 -07002762}
2763
Dylan Reid18c49c82017-08-25 14:52:27 -07002764static int minijail_run_internal(struct minijail *j,
2765 const struct minijail_run_config *config,
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002766 struct minijail_run_state *state_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002767{
Dylan Reidce5b55e2016-01-13 11:04:16 -08002768 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002769 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002770 /* We need to remember this across the minijail_preexec() call. */
2771 int pid_namespace = j->flags.pids;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -07002772 /*
2773 * Create an init process if we are entering a pid namespace, unless the
2774 * user has explicitly opted out by calling minijail_run_as_init().
2775 */
2776 int do_init = j->flags.do_init && !j->flags.run_as_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002777 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002778
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002779 if (use_preload) {
Dylan Reid0412dcc2017-08-24 11:33:15 -07002780 if (j->hooks_head != NULL)
2781 die("Minijail hooks are not supported with LD_PRELOAD");
2782 if (!config->exec_in_child)
2783 die("minijail_fork is not supported with LD_PRELOAD");
2784
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01002785 /*
2786 * Before we fork(2) and execve(2) the child process, we need
2787 * to open a pipe(2) to send the minijail configuration over.
2788 */
2789 state_out->child_env =
2790 minijail_copy_env(config->envp ? config->envp : environ);
2791 if (!state_out->child_env)
2792 return ENOMEM;
2793 if (setup_preload(j, &state_out->child_env) ||
2794 setup_pipe(&state_out->child_env, state_out->pipe_fds))
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002795 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002796 }
Will Drewryf89aef52011-09-16 16:48:57 -05002797
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002798 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002799 if (j->flags.use_caps && j->caps != 0 &&
2800 !j->flags.set_ambient_caps) {
2801 die("non-empty, non-ambient capabilities are not "
2802 "supported without LD_PRELOAD");
2803 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002804 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002805
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002806 /* Create pipes for stdin/stdout/stderr as requested by caller. */
2807 struct {
2808 bool requested;
2809 int *pipe_fds;
2810 } pipe_fd_req[] = {
2811 {config->pstdin_fd != NULL, state_out->stdin_fds},
2812 {config->pstdout_fd != NULL, state_out->stdout_fds},
2813 {config->pstderr_fd != NULL, state_out->stderr_fds},
2814 };
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002815
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002816 for (size_t i = 0; i < ARRAY_SIZE(pipe_fd_req); ++i) {
2817 if (pipe_fd_req[i].requested &&
2818 pipe(pipe_fd_req[i].pipe_fds) == -1)
2819 return EFAULT;
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002820 }
2821
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002822 /*
François Degros664eba72019-11-05 13:18:24 +11002823 * If the parent process needs to configure the child's runtime
2824 * environment after forking, create a pipe(2) to block the child until
2825 * configuration is done.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002826 */
Daniel Erat2d69add2019-04-23 20:58:53 -07002827 if (j->flags.forward_signals || j->flags.pid_file || j->flags.cgroups ||
2828 j->rlimit_count || j->flags.userns) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002829 sync_child = 1;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002830 if (pipe(state_out->child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002831 return -EFAULT;
2832 }
2833
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002834 /*
2835 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002836 *
2837 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2838 *
2839 * In multithreaded programs, there are a bunch of locks inside libc,
2840 * some of which may be held by other threads at the time that we call
2841 * minijail_run_pid(). If we call fork(), glibc does its level best to
2842 * ensure that we hold all of these locks before it calls clone()
2843 * internally and drop them after clone() returns, but when we call
2844 * sys_clone(2) directly, all that gets bypassed and we end up with a
2845 * child address space where some of libc's important locks are held by
2846 * other threads (which did not get cloned, and hence will never release
2847 * those locks). This is okay so long as we call exec() immediately
2848 * after, but a bunch of seemingly-innocent libc functions like setenv()
2849 * take locks.
2850 *
2851 * Hence, only call sys_clone() if we need to, in order to get at pid
2852 * namespacing. If we follow this path, the child's address space might
2853 * have broken locks; you may only call functions that do not acquire
2854 * any locks.
2855 *
2856 * Unfortunately, fork() acquires every lock it can get its hands on, as
2857 * previously detailed, so this function is highly likely to deadlock
2858 * later on (see "deadlock here") if we're multithreaded.
2859 *
2860 * We might hack around this by having the clone()d child (init of the
2861 * pid namespace) return directly, rather than leaving the clone()d
2862 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002863 * its fork()ed child return in turn), but that process would be
2864 * crippled with its libc locks potentially broken. We might try
2865 * fork()ing in the parent before we clone() to ensure that we own all
2866 * the locks, but then we have to have the forked child hanging around
2867 * consuming resources (and possibly having file descriptors / shared
2868 * memory regions / etc attached). We'd need to keep the child around to
2869 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002870 *
2871 * TODO(ellyjones): figure out if the "forked child hanging around"
2872 * problem is fixable or not. It would be nice if we worked in this
2873 * case.
2874 */
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002875 pid_t child_pid;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002876 if (pid_namespace) {
François Degros94619842019-11-08 16:37:55 +11002877 unsigned long clone_flags = CLONE_NEWPID | SIGCHLD;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002878 if (j->flags.userns)
2879 clone_flags |= CLONE_NEWUSER;
François Degros94619842019-11-08 16:37:55 +11002880
2881 child_pid = syscall(SYS_clone, clone_flags, NULL, 0L, 0L, 0L);
2882
2883 if (child_pid < 0) {
2884 if (errno == EPERM)
2885 pdie("clone(CLONE_NEWPID | ...) failed with EPERM; "
2886 "is this process missing CAP_SYS_ADMIN?");
2887 pdie("clone(CLONE_NEWPID | ...) failed");
2888 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002889 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002890 child_pid = fork();
2891
François Degros94619842019-11-08 16:37:55 +11002892 if (child_pid < 0)
2893 pdie("fork failed");
Elly Jonese1749eb2011-10-07 13:54:59 -04002894 }
Will Drewryf89aef52011-09-16 16:48:57 -05002895
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002896 state_out->child_pid = child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04002897 if (child_pid) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002898 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002899
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002900 if (j->flags.forward_signals) {
2901 forward_pid = child_pid;
2902 install_signal_handlers();
2903 }
2904
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002905 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002906 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002907
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002908 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002909 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002910
Dylan Reid0f72ef42017-06-06 15:42:49 -07002911 if (j->rlimit_count)
2912 set_rlimits_or_die(j);
2913
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002914 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002915 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002916
Mike Frysinger902a4492018-12-27 05:22:56 -05002917 if (j->flags.enter_vfs)
2918 close(j->mountns_fd);
2919
2920 if (j->flags.enter_net)
2921 close(j->netns_fd);
2922
Dylan Reidce5b55e2016-01-13 11:04:16 -08002923 if (sync_child)
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002924 parent_setup_complete(state_out->child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002925
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002926 if (use_preload) {
François Degros664eba72019-11-05 13:18:24 +11002927 /*
2928 * Add SIGPIPE to the signal mask to avoid getting
2929 * killed if the child process finishes or closes its
2930 * end of the pipe prematurely.
2931 *
2932 * TODO(crbug.com/1022170): Use pthread_sigmask instead
2933 * of sigprocmask if Minijail is used in multithreaded
2934 * programs.
2935 */
2936 sigset_t to_block, to_restore;
2937 if (sigemptyset(&to_block) < 0)
2938 pdie("sigemptyset failed");
2939 if (sigaddset(&to_block, SIGPIPE) < 0)
2940 pdie("sigaddset failed");
2941 if (sigprocmask(SIG_BLOCK, &to_block, &to_restore) < 0)
2942 pdie("sigprocmask failed");
2943
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002944 /* Send marshalled minijail. */
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002945 close_and_reset(&state_out->pipe_fds[0]);
2946 ret = minijail_to_fd(j, state_out->pipe_fds[1]);
2947 close_and_reset(&state_out->pipe_fds[1]);
François Degros664eba72019-11-05 13:18:24 +11002948
2949 /* Accept any pending SIGPIPE. */
2950 while (true) {
2951 const struct timespec zero_time = {0, 0};
2952 const int sig = sigtimedwait(&to_block, NULL, &zero_time);
2953 if (sig < 0) {
2954 if (errno != EINTR)
2955 break;
2956 } else {
2957 if (sig != SIGPIPE)
2958 die("unexpected signal %d", sig);
2959 }
2960 }
2961
2962 /* Restore the signal mask to its original state. */
2963 if (sigprocmask(SIG_SETMASK, &to_restore, NULL) < 0)
2964 pdie("sigprocmask failed");
2965
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002966 if (ret) {
François Degros664eba72019-11-05 13:18:24 +11002967 warn("failed to send marshalled minijail: %s",
2968 strerror(-ret));
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002969 kill(j->initpid, SIGKILL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002970 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002971 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002972
Elly Jonese1749eb2011-10-07 13:54:59 -04002973 return 0;
2974 }
Ben Chan541c7e52011-08-26 14:55:53 -07002975
Mattias Nissler6123e5a2020-02-11 13:38:03 +01002976 /* Child process. */
Peter Qiu2860c462015-12-16 15:13:06 -08002977 if (j->flags.reset_signal_mask) {
2978 sigset_t signal_mask;
2979 if (sigemptyset(&signal_mask) != 0)
2980 pdie("sigemptyset failed");
2981 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2982 pdie("sigprocmask failed");
2983 }
2984
Luis Hector Chaveza27118a2018-04-04 08:18:01 -07002985 if (j->flags.reset_signal_handlers) {
2986 int signum;
2987 for (signum = 0; signum <= SIGRTMAX; signum++) {
2988 /*
2989 * Ignore EINVAL since some signal numbers in the range
2990 * might not be valid.
2991 */
2992 if (signal(signum, SIG_DFL) == SIG_ERR &&
2993 errno != EINVAL) {
2994 pdie("failed to reset signal %d disposition",
2995 signum);
2996 }
2997 }
2998 }
2999
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003000 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07003001 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003002 int inheritable_fds[kMaxInheritableFdsSize];
3003 size_t size = 0;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003004
3005 int *pipe_fds[] = {
3006 state_out->pipe_fds, state_out->child_sync_pipe_fds,
3007 state_out->stdin_fds, state_out->stdout_fds,
3008 state_out->stderr_fds,
3009 };
3010
3011 for (size_t i = 0; i < ARRAY_SIZE(pipe_fds); ++i) {
3012 if (pipe_fds[i][0] != -1) {
3013 inheritable_fds[size++] = pipe_fds[i][0];
3014 }
3015 if (pipe_fds[i][1] != -1) {
3016 inheritable_fds[size++] = pipe_fds[i][1];
3017 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003018 }
Jorge Lucangeli Obes2337f802019-07-18 14:46:03 -04003019
Jorge Lucangeli Obescf3bbea2019-07-24 09:06:40 -04003020 /*
3021 * Preserve namespace file descriptors over the close_open_fds()
3022 * call. These are closed in minijail_enter() so they won't leak
3023 * into the child process.
3024 */
Jorge Lucangeli Obes2337f802019-07-18 14:46:03 -04003025 if (j->flags.enter_vfs)
3026 minijail_preserve_fd(j, j->mountns_fd, j->mountns_fd);
3027 if (j->flags.enter_net)
3028 minijail_preserve_fd(j, j->netns_fd, j->netns_fd);
3029
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003030 for (size_t i = 0; i < j->preserved_fd_count; i++) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07003031 /*
3032 * Preserve all parent_fds. They will be dup2(2)-ed in
3033 * the child later.
3034 */
3035 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
3036 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07003037
3038 if (close_open_fds(inheritable_fds, size) < 0)
3039 die("failed to close open file descriptors");
3040 }
3041
Luis Hector Chavez1617f632017-08-01 18:32:30 -07003042 if (redirect_fds(j))
3043 die("failed to set up fd redirections");
3044
Dylan Reidce5b55e2016-01-13 11:04:16 -08003045 if (sync_child)
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003046 wait_for_parent_setup(state_out->child_sync_pipe_fds);
Dylan Reidce5b55e2016-01-13 11:04:16 -08003047
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003048 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08003049 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08003050
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003051 setup_child_std_fds(j, state_out);
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05003052
Dylan Reid791f5772015-09-14 20:02:42 -07003053 /* If running an init program, let it decide when/how to mount /proc. */
3054 if (pid_namespace && !do_init)
3055 j->flags.remount_proc_ro = 0;
3056
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003057 if (use_preload) {
3058 /* Strip out flags that cannot be inherited across execve(2). */
3059 minijail_preexec(j);
3060 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003061 /*
3062 * If not using LD_PRELOAD, do all jailing before execve(2).
3063 * Note that PID namespaces can only be entered on fork(2),
3064 * so that flag is still cleared.
3065 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003066 j->flags.pids = 0;
3067 }
Dylan Reid0412dcc2017-08-24 11:33:15 -07003068
3069 /*
3070 * Jail this process.
3071 * If forking, return.
3072 * If not, execve(2) the target.
3073 */
Elly Jonese1749eb2011-10-07 13:54:59 -04003074 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003075
Dylan Reid0412dcc2017-08-24 11:33:15 -07003076 if (config->exec_in_child && pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05003077 /*
3078 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003079 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003080 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08003081 * a child to actually run the program. If |do_init == 0|, we
3082 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04003083 *
3084 * If we're multithreaded, we'll probably deadlock here. See
3085 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04003086 */
3087 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003088 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04003089 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003090 } else if (child_pid > 0) {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003091 minijail_free_run_state(state_out);
3092
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04003093 /*
3094 * Best effort. Don't bother checking the return value.
3095 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04003096 prctl(PR_SET_NAME, "minijail-init");
3097 init(child_pid); /* Never returns. */
3098 }
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003099 state_out->child_pid = child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04003100 }
Elly Jonescd7a9042011-07-22 13:56:51 -04003101
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07003102 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
3103
Dylan Reid0412dcc2017-08-24 11:33:15 -07003104 if (!config->exec_in_child)
3105 return 0;
3106
Elly Jonesdd3e8512012-01-23 15:13:38 -05003107 /*
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003108 * We're going to execve(), so make sure any remaining resources are
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003109 * freed. Exceptions are:
3110 * 1. The child environment. No need to worry about freeing it since
3111 * execve reinitializes the heap anyways.
3112 * 2. The read side of the LD_PRELOAD pipe, which we need to hand down
3113 * into the target in which the preloaded code will read from it and
3114 * then close it.
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003115 */
3116 state_out->pipe_fds[0] = -1;
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003117 char *const *child_env = state_out->child_env;
3118 state_out->child_env = NULL;
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003119 minijail_free_run_state(state_out);
3120
3121 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07003122 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04003123 * calling process
3124 * -> execve()-ing process
3125 * If we are:
3126 * calling process
3127 * -> init()-ing process
3128 * -> execve()-ing process
3129 */
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003130 if (!child_env)
3131 child_env = config->envp ? config->envp : environ;
François Degros08b10f72019-10-09 12:44:05 +11003132 execve(config->filename, config->argv, child_env);
3133
3134 ret = (errno == ENOENT ? MINIJAIL_ERR_NO_COMMAND : MINIJAIL_ERR_NO_ACCESS);
3135 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04003136 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04003137}
3138
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003139static int
3140minijail_run_config_internal(struct minijail *j,
3141 const struct minijail_run_config *config)
3142{
3143 struct minijail_run_state state = {
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003144 .child_pid = -1,
3145 .pipe_fds = {-1, -1},
3146 .stdin_fds = {-1, -1},
3147 .stdout_fds = {-1, -1},
3148 .stderr_fds = {-1, -1},
3149 .child_sync_pipe_fds = {-1, -1},
Mattias Nisslerb35f2c12020-02-07 13:37:36 +01003150 .child_env = NULL,
Mattias Nissler6123e5a2020-02-11 13:38:03 +01003151 };
3152 int ret = minijail_run_internal(j, config, &state);
3153
3154 if (ret == 0) {
3155 if (config->pchild_pid)
3156 *config->pchild_pid = state.child_pid;
3157
3158 /* Grab stdin/stdout/stderr descriptors requested by caller. */
3159 struct {
3160 int *pfd;
3161 int *psrc;
3162 } fd_map[] = {
3163 {config->pstdin_fd, &state.stdin_fds[1]},
3164 {config->pstdout_fd, &state.stdout_fds[0]},
3165 {config->pstderr_fd, &state.stderr_fds[0]},
3166 };
3167
3168 for (size_t i = 0; i < ARRAY_SIZE(fd_map); ++i) {
3169 if (fd_map[i].pfd) {
3170 *fd_map[i].pfd = *fd_map[i].psrc;
3171 *fd_map[i].psrc = -1;
3172 }
3173 }
3174
3175 if (!config->exec_in_child)
3176 ret = state.child_pid;
3177 }
3178
3179 minijail_free_run_state(&state);
3180
3181 return ret;
3182}
3183
Will Drewry6ac91122011-10-21 16:38:58 -05003184int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003185{
François Degros47e63352019-10-01 13:01:53 +10003186 if (j->initpid <= 0)
3187 return -ECHILD;
3188
Elly Jonese1749eb2011-10-07 13:54:59 -04003189 if (kill(j->initpid, SIGTERM))
3190 return -errno;
François Degros47e63352019-10-01 13:01:53 +10003191
3192 return minijail_wait(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003193}
3194
Will Drewry6ac91122011-10-21 16:38:58 -05003195int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003196{
François Degros08b10f72019-10-09 12:44:05 +11003197 if (j->initpid <= 0)
3198 return -ECHILD;
3199
Elly Jonese1749eb2011-10-07 13:54:59 -04003200 int st;
François Degros627deba2019-10-01 12:48:25 +10003201 while (true) {
3202 const int ret = waitpid(j->initpid, &st, 0);
3203 if (ret >= 0)
3204 break;
3205 if (errno != EINTR)
3206 return -errno;
3207 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003208
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003209 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003210 int error_status = st;
3211 if (WIFSIGNALED(st)) {
3212 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07003213 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003214 j->initpid, signum);
3215 /*
3216 * We return MINIJAIL_ERR_JAIL if the process received
3217 * SIGSYS, which happens when a syscall is blocked by
3218 * seccomp filters.
3219 * If not, we do what bash(1) does:
3220 * $? = 128 + signum
3221 */
3222 if (signum == SIGSYS) {
3223 error_status = MINIJAIL_ERR_JAIL;
3224 } else {
François Degros08b10f72019-10-09 12:44:05 +11003225 error_status = MINIJAIL_ERR_SIG_BASE + signum;
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07003226 }
3227 }
3228 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07003229 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003230
3231 int exit_status = WEXITSTATUS(st);
3232 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07003233 info("child process %d exited with status %d",
3234 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08003235
3236 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04003237}
3238
Will Drewry6ac91122011-10-21 16:38:58 -05003239void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04003240{
Dylan Reid605ce7f2016-01-19 19:21:00 -08003241 size_t i;
3242
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07003243 if (j->filter_prog) {
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08003244 free(j->filter_prog->filter);
3245 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04003246 }
Mike Frysingerac08a682017-10-10 02:04:50 -04003247 free_mounts_list(j);
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07003248 while (j->hooks_head) {
3249 struct hook *c = j->hooks_head;
3250 j->hooks_head = c->next;
3251 free(c);
3252 }
3253 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04003254 if (j->user)
3255 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08003256 if (j->suppl_gid_list)
3257 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05003258 if (j->chrootdir)
3259 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04003260 if (j->pid_file_path)
3261 free(j->pid_file_path);
3262 if (j->uidmap)
3263 free(j->uidmap);
3264 if (j->gidmap)
3265 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04003266 if (j->hostname)
3267 free(j->hostname);
Luis Hector Chavez9acba452018-10-11 10:13:25 -07003268 if (j->preload_path)
3269 free(j->preload_path);
Andrew Brestickereac28942015-11-11 16:04:46 -08003270 if (j->alt_syscall_table)
3271 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08003272 for (i = 0; i < j->cgroup_count; ++i)
3273 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04003274 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04003275}
Luis Hector Chavez114a9302017-09-05 20:36:58 -07003276
3277void API minijail_log_to_fd(int fd, int min_priority)
3278{
3279 init_logging(LOG_TO_FD, fd, min_priority);
3280}