blob: d2f4945eea5abeb853453556f25c31a2d0fcd77c [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>
Elly Jonescd7a9042011-07-22 13:56:51 -040016#include <sched.h>
17#include <signal.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070018#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080019#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040023#include <sys/capability.h>
24#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050025#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040026#include <sys/prctl.h>
Dylan Reid0f72ef42017-06-06 15:42:49 -070027#include <sys/resource.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070028#include <sys/stat.h>
Mike Frysinger33ffef32017-01-13 19:53:19 -050029#include <sys/sysmacros.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070030#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080031#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040032#include <sys/wait.h>
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -070033#include <syscall.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040034#include <unistd.h>
35
36#include "libminijail.h"
37#include "libminijail-private.h"
38
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070039#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080040#include "syscall_filter.h"
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040041#include "syscall_wrapper.h"
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040042#include "system.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070043#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080044
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070045/* Until these are reliably available in linux/prctl.h. */
Andrew Brestickereac28942015-11-11 16:04:46 -080046#ifndef PR_ALT_SYSCALL
47# define PR_ALT_SYSCALL 0x43724f53
48#endif
49
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040050/* Seccomp filter related flags. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080051#ifndef PR_SET_NO_NEW_PRIVS
52# define PR_SET_NO_NEW_PRIVS 38
53#endif
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040054
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080055#ifndef SECCOMP_MODE_FILTER
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040056#define SECCOMP_MODE_FILTER 2 /* Uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050057#endif
58
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040059#ifndef SECCOMP_SET_MODE_STRICT
60# define SECCOMP_SET_MODE_STRICT 0
61#endif
62#ifndef SECCOMP_SET_MODE_FILTER
63# define SECCOMP_SET_MODE_FILTER 1
64#endif
65
66#ifndef SECCOMP_FILTER_FLAG_TSYNC
67# define SECCOMP_FILTER_FLAG_TSYNC 1
68#endif
69/* End seccomp filter related flags. */
70
Dylan Reid4cbc2a52016-06-17 19:06:07 -070071/* New cgroup namespace might not be in linux-headers yet. */
72#ifndef CLONE_NEWCGROUP
73# define CLONE_NEWCGROUP 0x02000000
74#endif
75
Dylan Reid605ce7f2016-01-19 19:21:00 -080076#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
77
Dylan Reid0f72ef42017-06-06 15:42:49 -070078#define MAX_RLIMITS 32 /* Currently there are 15 supported by Linux. */
79
Stephen Barber0d1cbf62017-10-16 22:19:38 -070080#define MAX_PRESERVED_FDS 32U
Luis Hector Chavez1617f632017-08-01 18:32:30 -070081
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -080082/* Keyctl commands. */
83#define KEYCTL_JOIN_SESSION_KEYRING 1
84
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -070085/*
86 * The userspace equivalent of MNT_USER_SETTABLE_MASK, which is the mask of all
87 * flags that can be modified by MS_REMOUNT.
88 */
89#define MS_USER_SETTABLE_MASK \
90 (MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME | MS_NODIRATIME | \
91 MS_RELATIME | MS_RDONLY)
92
Dylan Reid0f72ef42017-06-06 15:42:49 -070093struct minijail_rlimit {
94 int type;
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -080095 rlim_t cur;
96 rlim_t max;
Dylan Reid0f72ef42017-06-06 15:42:49 -070097};
98
Dylan Reid648b2202015-10-23 00:50:00 -070099struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400100 char *src;
101 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700102 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700103 char *data;
104 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -0700105 unsigned long flags;
106 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400107};
108
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700109struct hook {
110 minijail_hook_t hook;
111 void *payload;
112 minijail_hook_event_t event;
113 struct hook *next;
114};
115
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700116struct preserved_fd {
117 int parent_fd;
118 int child_fd;
119};
120
Will Drewryf89aef52011-09-16 16:48:57 -0500121struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700122 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700123 * WARNING: if you add a flag here you need to make sure it's
124 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700125 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400126 struct {
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700127 int uid : 1;
128 int gid : 1;
Lutz Justen13807cb2017-01-03 17:11:55 +0100129 int inherit_suppl_gids : 1;
130 int set_suppl_gids : 1;
131 int keep_suppl_gids : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700132 int use_caps : 1;
133 int capbset_drop : 1;
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400134 int set_ambient_caps : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700135 int vfs : 1;
136 int enter_vfs : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700137 int pids : 1;
138 int ipc : 1;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400139 int uts : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700140 int net : 1;
141 int enter_net : 1;
142 int ns_cgroups : 1;
143 int userns : 1;
144 int disable_setgroups : 1;
145 int seccomp : 1;
146 int remount_proc_ro : 1;
147 int no_new_privs : 1;
148 int seccomp_filter : 1;
149 int seccomp_filter_tsync : 1;
150 int seccomp_filter_logging : 1;
151 int chroot : 1;
152 int pivot_root : 1;
Mike Frysinger33ffef32017-01-13 19:53:19 -0500153 int mount_dev : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700154 int mount_tmp : 1;
155 int do_init : 1;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700156 int run_as_init : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700157 int pid_file : 1;
158 int cgroups : 1;
159 int alt_syscall : 1;
160 int reset_signal_mask : 1;
Luis Hector Chaveza27118a2018-04-04 08:18:01 -0700161 int reset_signal_handlers : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700162 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800163 int new_session_keyring : 1;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400164 int forward_signals : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400165 } flags;
166 uid_t uid;
167 gid_t gid;
168 gid_t usergid;
169 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800170 size_t suppl_gid_count;
171 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400172 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800173 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400174 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700175 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700176 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400177 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800178 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800179 char *uidmap;
180 char *gidmap;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400181 char *hostname;
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700182 char *preload_path;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800183 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800184 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800185 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700186 struct mountpoint *mounts_head;
187 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800188 size_t mounts_count;
Mike Frysinger785b1c32018-02-23 15:47:24 -0500189 unsigned long remount_mode;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100190 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800191 char *cgroups[MAX_CGROUPS];
192 size_t cgroup_count;
Dylan Reid0f72ef42017-06-06 15:42:49 -0700193 struct minijail_rlimit rlimits[MAX_RLIMITS];
194 size_t rlimit_count;
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700195 uint64_t securebits_skip_mask;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700196 struct hook *hooks_head;
197 struct hook *hooks_tail;
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700198 struct preserved_fd preserved_fds[MAX_PRESERVED_FDS];
199 size_t preserved_fd_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500200};
201
Luis Hector Chavez64730af2017-09-13 13:18:59 -0700202static void run_hooks_or_die(const struct minijail *j,
203 minijail_hook_event_t event);
204
Mike Frysingerac08a682017-10-10 02:04:50 -0400205static void free_mounts_list(struct minijail *j)
206{
207 while (j->mounts_head) {
208 struct mountpoint *m = j->mounts_head;
209 j->mounts_head = j->mounts_head->next;
210 free(m->data);
211 free(m->type);
212 free(m->dest);
213 free(m->src);
214 free(m);
215 }
216 // No need to clear mounts_head as we know it's NULL after the loop.
217 j->mounts_tail = NULL;
218}
219
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700220/*
221 * Strip out flags meant for the parent.
222 * We keep things that are not inherited across execve(2) (e.g. capabilities),
223 * or are easier to set after execve(2) (e.g. seccomp filters).
224 */
225void minijail_preenter(struct minijail *j)
226{
227 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700228 j->flags.enter_vfs = 0;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700229 j->flags.ns_cgroups = 0;
230 j->flags.net = 0;
231 j->flags.uts = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700232 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700233 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800234 j->flags.do_init = 0;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700235 j->flags.run_as_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800236 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800237 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400238 j->flags.forward_signals = 0;
Mike Frysinger785b1c32018-02-23 15:47:24 -0500239 j->remount_mode = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700240}
241
242/*
243 * Strip out flags meant for the child.
244 * We keep things that are inherited across execve(2).
245 */
246void minijail_preexec(struct minijail *j)
247{
248 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700249 int enter_vfs = j->flags.enter_vfs;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700250 int ns_cgroups = j->flags.ns_cgroups;
251 int net = j->flags.net;
252 int uts = j->flags.uts;
Dylan Reid791f5772015-09-14 20:02:42 -0700253 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800254 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700255 if (j->user)
256 free(j->user);
257 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800258 if (j->suppl_gid_list)
259 free(j->suppl_gid_list);
260 j->suppl_gid_list = NULL;
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700261 if (j->preload_path)
262 free(j->preload_path);
263 j->preload_path = NULL;
Mike Frysingerac08a682017-10-10 02:04:50 -0400264 free_mounts_list(j);
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700265 memset(&j->flags, 0, sizeof(j->flags));
266 /* Now restore anything we meant to keep. */
267 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700268 j->flags.enter_vfs = enter_vfs;
Luis Hector Chavez83a44892018-10-12 08:56:20 -0700269 j->flags.ns_cgroups = ns_cgroups;
270 j->flags.net = net;
271 j->flags.uts = uts;
Dylan Reid791f5772015-09-14 20:02:42 -0700272 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800273 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700274 /* Note, |pids| will already have been used before this call. */
275}
276
277/* Minijail API. */
278
Will Drewry6ac91122011-10-21 16:38:58 -0500279struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400280{
Mike Frysinger785b1c32018-02-23 15:47:24 -0500281 struct minijail *j = calloc(1, sizeof(struct minijail));
282 j->remount_mode = MS_PRIVATE;
283 return j;
Elly Jonescd7a9042011-07-22 13:56:51 -0400284}
285
Will Drewry6ac91122011-10-21 16:38:58 -0500286void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400287{
288 if (uid == 0)
289 die("useless change to uid 0");
290 j->uid = uid;
291 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400292}
293
Will Drewry6ac91122011-10-21 16:38:58 -0500294void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400295{
296 if (gid == 0)
297 die("useless change to gid 0");
298 j->gid = gid;
299 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400300}
301
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800302void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
303 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800304{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800305 size_t i;
306
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500307 if (j->flags.inherit_suppl_gids)
308 die("cannot inherit *and* set supplementary groups");
309 if (j->flags.keep_suppl_gids)
310 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800311
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800312 if (size == 0) {
313 /* Clear supplementary groups. */
314 j->suppl_gid_list = NULL;
315 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100316 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800317 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800318 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800319
320 /* Copy the gid_t array. */
321 j->suppl_gid_list = calloc(size, sizeof(gid_t));
322 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800323 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800324 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800325 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800326 j->suppl_gid_list[i] = list[i];
327 }
328 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100329 j->flags.set_suppl_gids = 1;
330}
331
332void API minijail_keep_supplementary_gids(struct minijail *j) {
333 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800334}
335
Will Drewry6ac91122011-10-21 16:38:58 -0500336int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400337{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700338 uid_t uid;
339 gid_t gid;
340 int rc = lookup_user(user, &uid, &gid);
341 if (rc)
342 return rc;
343 minijail_change_uid(j, uid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400344 j->user = strdup(user);
345 if (!j->user)
346 return -ENOMEM;
Luis Hector Chavez71323552017-09-05 09:17:22 -0700347 j->usergid = gid;
Elly Jonese1749eb2011-10-07 13:54:59 -0400348 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400349}
350
Will Drewry6ac91122011-10-21 16:38:58 -0500351int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400352{
Luis Hector Chavez71323552017-09-05 09:17:22 -0700353 gid_t gid;
354 int rc = lookup_group(group, &gid);
355 if (rc)
356 return rc;
357 minijail_change_gid(j, gid);
Elly Jonese1749eb2011-10-07 13:54:59 -0400358 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400359}
360
Will Drewry6ac91122011-10-21 16:38:58 -0500361void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400362{
363 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400364}
365
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700366void API minijail_no_new_privs(struct minijail *j)
367{
368 j->flags.no_new_privs = 1;
369}
370
Will Drewry6ac91122011-10-21 16:38:58 -0500371void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400372{
373 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500374}
375
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400376void API minijail_set_seccomp_filter_tsync(struct minijail *j)
377{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400378 if (j->filter_len > 0 && j->filter_prog != NULL) {
379 die("minijail_set_seccomp_filter_tsync() must be called "
380 "before minijail_parse_seccomp_filters()");
381 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400382 j->flags.seccomp_filter_tsync = 1;
383}
384
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700385void API minijail_log_seccomp_filter_failures(struct minijail *j)
386{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400387 if (j->filter_len > 0 && j->filter_prog != NULL) {
388 die("minijail_log_seccomp_filter_failures() must be called "
389 "before minijail_parse_seccomp_filters()");
390 }
Mike Frysinger916c6c32018-09-27 14:17:53 -0400391#ifdef ALLOW_DEBUG_LOGGING
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400392 j->flags.seccomp_filter_logging = 1;
Mike Frysinger916c6c32018-09-27 14:17:53 -0400393#else
394 warn("non-debug build: ignoring request to enable seccomp logging");
395#endif
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700396}
397
Will Drewry6ac91122011-10-21 16:38:58 -0500398void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400399{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800400 /*
401 * 'minijail_use_caps' configures a runtime-capabilities-only
402 * environment, including a bounding set matching the thread's runtime
403 * (permitted|inheritable|effective) sets.
404 * Therefore, it will override any existing bounding set configurations
405 * since the latter would allow gaining extra runtime capabilities from
406 * file capabilities.
407 */
408 if (j->flags.capbset_drop) {
409 warn("overriding bounding set configuration");
410 j->cap_bset = 0;
411 j->flags.capbset_drop = 0;
412 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400413 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800414 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400415}
416
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800417void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
418{
419 if (j->flags.use_caps) {
420 /*
421 * 'minijail_use_caps' will have already configured a capability
422 * bounding set matching the (permitted|inheritable|effective)
423 * sets. Abort if the user tries to configure a separate
424 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
425 * are mutually exclusive.
426 */
427 die("runtime capabilities already configured, can't drop "
428 "bounding set separately");
429 }
430 j->cap_bset = capmask;
431 j->flags.capbset_drop = 1;
432}
433
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400434void API minijail_set_ambient_caps(struct minijail *j)
435{
436 j->flags.set_ambient_caps = 1;
437}
438
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800439void API minijail_reset_signal_mask(struct minijail *j)
440{
Peter Qiu2860c462015-12-16 15:13:06 -0800441 j->flags.reset_signal_mask = 1;
442}
443
Luis Hector Chaveza27118a2018-04-04 08:18:01 -0700444void API minijail_reset_signal_handlers(struct minijail *j)
445{
446 j->flags.reset_signal_handlers = 1;
447}
448
Will Drewry6ac91122011-10-21 16:38:58 -0500449void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400450{
451 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400452}
453
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700454void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
455{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800456 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700457 if (ns_fd < 0) {
458 pdie("failed to open namespace '%s'", ns_path);
459 }
460 j->mountns_fd = ns_fd;
461 j->flags.enter_vfs = 1;
462}
463
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800464void API minijail_new_session_keyring(struct minijail *j)
465{
466 j->flags.new_session_keyring = 1;
467}
468
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700469void API minijail_skip_setting_securebits(struct minijail *j,
470 uint64_t securebits_skip_mask)
471{
472 j->securebits_skip_mask = securebits_skip_mask;
473}
474
Mike Frysinger785b1c32018-02-23 15:47:24 -0500475void API minijail_remount_mode(struct minijail *j, unsigned long mode)
476{
477 j->remount_mode = mode;
478}
479
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800480void API minijail_skip_remount_private(struct minijail *j)
481{
Mike Frysinger785b1c32018-02-23 15:47:24 -0500482 j->remount_mode = 0;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800483}
484
Will Drewry6ac91122011-10-21 16:38:58 -0500485void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400486{
Elly Jonese58176c2012-01-23 11:46:17 -0500487 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700488 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400489 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800490 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400491}
492
Dylan Reidf7942472015-11-18 17:55:26 -0800493void API minijail_namespace_ipc(struct minijail *j)
494{
495 j->flags.ipc = 1;
496}
497
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400498void API minijail_namespace_uts(struct minijail *j)
499{
500 j->flags.uts = 1;
501}
502
503int API minijail_namespace_set_hostname(struct minijail *j, const char *name)
504{
505 if (j->hostname)
506 return -EINVAL;
507 minijail_namespace_uts(j);
508 j->hostname = strdup(name);
509 if (!j->hostname)
510 return -ENOMEM;
511 return 0;
512}
513
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400514void API minijail_namespace_net(struct minijail *j)
515{
516 j->flags.net = 1;
517}
518
Dylan Reid1102f5a2015-09-15 11:52:20 -0700519void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
520{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800521 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700522 if (ns_fd < 0) {
523 pdie("failed to open namespace '%s'", ns_path);
524 }
525 j->netns_fd = ns_fd;
526 j->flags.enter_net = 1;
527}
528
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700529void API minijail_namespace_cgroups(struct minijail *j)
530{
531 j->flags.ns_cgroups = 1;
532}
533
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700534void API minijail_close_open_fds(struct minijail *j)
535{
536 j->flags.close_open_fds = 1;
537}
538
Dylan Reid791f5772015-09-14 20:02:42 -0700539void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400540{
541 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700542 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400543}
544
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800545void API minijail_namespace_user(struct minijail *j)
546{
547 j->flags.userns = 1;
548}
549
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400550void API minijail_namespace_user_disable_setgroups(struct minijail *j)
551{
552 j->flags.disable_setgroups = 1;
553}
554
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800555int API minijail_uidmap(struct minijail *j, const char *uidmap)
556{
557 j->uidmap = strdup(uidmap);
558 if (!j->uidmap)
559 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800560 char *ch;
561 for (ch = j->uidmap; *ch; ch++) {
562 if (*ch == ',')
563 *ch = '\n';
564 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800565 return 0;
566}
567
568int API minijail_gidmap(struct minijail *j, const char *gidmap)
569{
570 j->gidmap = strdup(gidmap);
571 if (!j->gidmap)
572 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800573 char *ch;
574 for (ch = j->gidmap; *ch; ch++) {
575 if (*ch == ',')
576 *ch = '\n';
577 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800578 return 0;
579}
580
Will Drewry6ac91122011-10-21 16:38:58 -0500581void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400582{
Lutz Justen13807cb2017-01-03 17:11:55 +0100583 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400584}
585
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800586void API minijail_run_as_init(struct minijail *j)
587{
588 /*
589 * Since the jailed program will become 'init' in the new PID namespace,
590 * Minijail does not need to fork an 'init' process.
591 */
Luis Hector Chavezac981fc2017-09-18 15:52:38 -0700592 j->flags.run_as_init = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800593}
594
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700595int API minijail_enter_chroot(struct minijail *j, const char *dir)
596{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400597 if (j->chrootdir)
598 return -EINVAL;
599 j->chrootdir = strdup(dir);
600 if (!j->chrootdir)
601 return -ENOMEM;
602 j->flags.chroot = 1;
603 return 0;
604}
605
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800606int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
607{
608 if (j->chrootdir)
609 return -EINVAL;
610 j->chrootdir = strdup(dir);
611 if (!j->chrootdir)
612 return -ENOMEM;
613 j->flags.pivot_root = 1;
614 return 0;
615}
616
Dylan Reida14e08d2015-10-22 21:05:29 -0700617char API *minijail_get_original_path(struct minijail *j,
618 const char *path_inside_chroot)
619{
Dylan Reid648b2202015-10-23 00:50:00 -0700620 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700621
Dylan Reid648b2202015-10-23 00:50:00 -0700622 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700623 while (b) {
624 /*
625 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700626 * mount, then the original path is exactly the source of
627 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700628 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700629 * mount source = /some/path/exe, mount dest =
630 * /chroot/path/exe Then when getting the original path of
631 * "/chroot/path/exe", the source of that mount,
632 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700633 */
634 if (!strcmp(b->dest, path_inside_chroot))
635 return strdup(b->src);
636
637 /*
638 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700639 * mount, take the suffix of the chroot path relative to the
640 * mount destination path, and append it to the mount source
641 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700642 */
643 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
644 const char *relative_path =
645 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400646 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700647 }
648 b = b->next;
649 }
650
651 /* If there is a chroot path, append |path_inside_chroot| to that. */
652 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400653 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700654
655 /* No chroot, so the path outside is the same as it is inside. */
656 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700657}
658
Martin Pelikánab9eb442017-01-25 11:53:58 +1100659size_t minijail_get_tmpfs_size(const struct minijail *j)
660{
661 return j->tmpfs_size;
662}
663
Mike Frysinger33ffef32017-01-13 19:53:19 -0500664void API minijail_mount_dev(struct minijail *j)
665{
666 j->flags.mount_dev = 1;
667}
668
Lee Campbell11af0622014-05-22 12:36:04 -0700669void API minijail_mount_tmp(struct minijail *j)
670{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100671 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
672}
673
674void API minijail_mount_tmp_size(struct minijail *j, size_t size)
675{
676 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700677 j->flags.mount_tmp = 1;
678}
679
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800680int API minijail_write_pid_file(struct minijail *j, const char *path)
681{
682 j->pid_file_path = strdup(path);
683 if (!j->pid_file_path)
684 return -ENOMEM;
685 j->flags.pid_file = 1;
686 return 0;
687}
688
Dylan Reid605ce7f2016-01-19 19:21:00 -0800689int API minijail_add_to_cgroup(struct minijail *j, const char *path)
690{
691 if (j->cgroup_count >= MAX_CGROUPS)
692 return -ENOMEM;
693 j->cgroups[j->cgroup_count] = strdup(path);
694 if (!j->cgroups[j->cgroup_count])
695 return -ENOMEM;
696 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800697 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800698 return 0;
699}
700
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800701int API minijail_rlimit(struct minijail *j, int type, rlim_t cur, rlim_t max)
Dylan Reid0f72ef42017-06-06 15:42:49 -0700702{
703 size_t i;
704
705 if (j->rlimit_count >= MAX_RLIMITS)
706 return -ENOMEM;
707 /* It's an error if the caller sets the same rlimit multiple times. */
708 for (i = 0; i < j->rlimit_count; i++) {
709 if (j->rlimits[i].type == type)
710 return -EEXIST;
711 }
712
713 j->rlimits[j->rlimit_count].type = type;
714 j->rlimits[j->rlimit_count].cur = cur;
715 j->rlimits[j->rlimit_count].max = max;
716 j->rlimit_count++;
717 return 0;
718}
719
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400720int API minijail_forward_signals(struct minijail *j)
721{
722 j->flags.forward_signals = 1;
723 return 0;
724}
725
Dylan Reid81e23972016-05-18 14:06:35 -0700726int API minijail_mount_with_data(struct minijail *j, const char *src,
727 const char *dest, const char *type,
728 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700729{
Dylan Reid648b2202015-10-23 00:50:00 -0700730 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400731
732 if (*dest != '/')
733 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700734 m = calloc(1, sizeof(*m));
735 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400736 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700737 m->dest = strdup(dest);
738 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400739 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700740 m->src = strdup(src);
741 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400742 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700743 m->type = strdup(type);
744 if (!m->type)
745 goto error;
Mike Frysingerb7803c82018-08-23 15:43:15 -0400746
747 if (!data || !data[0]) {
748 /*
749 * Set up secure defaults for certain filesystems. Adding this
750 * fs-specific logic here kind of sucks, but considering how
751 * people use these in practice, it's probably OK. If they want
752 * the kernel defaults, they can pass data="" instead of NULL.
753 */
754 if (!strcmp(type, "tmpfs")) {
755 /* tmpfs defaults to mode=1777 and size=50%. */
756 data = "mode=0755,size=10M";
757 }
758 }
Dylan Reid81e23972016-05-18 14:06:35 -0700759 if (data) {
760 m->data = strdup(data);
761 if (!m->data)
762 goto error;
763 m->has_data = 1;
764 }
Mike Frysingercb8674d2018-08-12 00:53:35 -0400765
766 /* If they don't specify any flags, default to secure ones. */
767 if (flags == 0)
768 flags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Dylan Reid648b2202015-10-23 00:50:00 -0700769 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400770
Mike Frysingercb8674d2018-08-12 00:53:35 -0400771 info("mount '%s' -> '%s' type '%s' flags %#lx", src, dest, type, flags);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400772
Elly Jonesdd3e8512012-01-23 15:13:38 -0500773 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700774 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400775 * containing vfs namespace.
776 */
777 minijail_namespace_vfs(j);
778
Dylan Reid648b2202015-10-23 00:50:00 -0700779 if (j->mounts_tail)
780 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400781 else
Dylan Reid648b2202015-10-23 00:50:00 -0700782 j->mounts_head = m;
783 j->mounts_tail = m;
784 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400785
786 return 0;
787
788error:
Dylan Reid81e23972016-05-18 14:06:35 -0700789 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700790 free(m->src);
791 free(m->dest);
792 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400793 return -ENOMEM;
794}
795
Dylan Reid81e23972016-05-18 14:06:35 -0700796int API minijail_mount(struct minijail *j, const char *src, const char *dest,
797 const char *type, unsigned long flags)
798{
799 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
800}
801
Dylan Reid648b2202015-10-23 00:50:00 -0700802int API minijail_bind(struct minijail *j, const char *src, const char *dest,
803 int writeable)
804{
805 unsigned long flags = MS_BIND;
806
807 if (!writeable)
808 flags |= MS_RDONLY;
809
810 return minijail_mount(j, src, dest, "", flags);
811}
812
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700813int API minijail_add_hook(struct minijail *j, minijail_hook_t hook,
814 void *payload, minijail_hook_event_t event)
815{
816 struct hook *c;
817
818 if (hook == NULL)
819 return -EINVAL;
820 if (event >= MINIJAIL_HOOK_EVENT_MAX)
821 return -EINVAL;
822 c = calloc(1, sizeof(*c));
823 if (!c)
824 return -ENOMEM;
825
826 c->hook = hook;
827 c->payload = payload;
828 c->event = event;
829
830 if (j->hooks_tail)
831 j->hooks_tail->next = c;
832 else
833 j->hooks_head = c;
834 j->hooks_tail = c;
835
836 return 0;
837}
838
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700839int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd)
840{
841 if (parent_fd < 0 || child_fd < 0)
842 return -EINVAL;
843 if (j->preserved_fd_count >= MAX_PRESERVED_FDS)
844 return -ENOMEM;
845 j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd;
846 j->preserved_fds[j->preserved_fd_count].child_fd = child_fd;
847 j->preserved_fd_count++;
848 return 0;
849}
850
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700851int API minijail_set_preload_path(struct minijail *j, const char *preload_path)
852{
853 if (j->preload_path)
854 return -EINVAL;
855 j->preload_path = strdup(preload_path);
856 if (!j->preload_path)
857 return -ENOMEM;
858 return 0;
859}
860
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400861static void clear_seccomp_options(struct minijail *j)
862{
863 j->flags.seccomp_filter = 0;
864 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400865 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400866 j->filter_len = 0;
867 j->filter_prog = NULL;
868 j->flags.no_new_privs = 0;
869}
870
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400871static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400872{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400873 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400874 /*
875 * |errno| will be set to EINVAL when seccomp has not been
876 * compiled into the kernel. On certain platforms and kernel
877 * versions this is not a fatal failure. In that case, and only
878 * in that case, disable seccomp and skip loading the filters.
879 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400880 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400881 warn("not loading seccomp filters, seccomp filter not "
882 "supported");
883 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400884 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700885 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400886 /*
887 * If |errno| != EINVAL or seccomp_can_softfail() is false,
888 * we can proceed. Worst case scenario minijail_enter() will
889 * abort() if seccomp fails.
890 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700891 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400892 if (j->flags.seccomp_filter_tsync) {
893 /* Are the seccomp(2) syscall and the TSYNC option supported? */
894 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
895 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
896 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400897 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
898 warn("seccomp(2) syscall not supported");
899 clear_seccomp_options(j);
900 return 0;
901 } else if (saved_errno == EINVAL &&
902 seccomp_can_softfail()) {
903 warn(
904 "seccomp filter thread sync not supported");
905 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400906 return 0;
907 }
908 /*
909 * Similar logic here. If seccomp_can_softfail() is
910 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
911 * we can proceed. Worst case scenario minijail_enter()
912 * will abort() if seccomp or TSYNC fail.
913 */
914 }
915 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400916 return 1;
917}
918
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700919static int parse_seccomp_filters(struct minijail *j, const char *filename,
920 FILE *policy_file)
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400921{
922 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400923 int use_ret_trap =
924 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
925 int allow_logging = j->flags.seccomp_filter_logging;
926
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700927 if (compile_filter(filename, policy_file, fprog, use_ret_trap,
928 allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400929 free(fprog);
930 return -1;
931 }
932
933 j->filter_len = fprog->len;
934 j->filter_prog = fprog;
935 return 0;
936}
937
938void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
939{
940 if (!seccomp_should_parse_filters(j))
941 return;
942
Luis Hector Chaveza30a2062018-07-12 21:10:33 -0700943 FILE *file = fopen(path, "re");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800944 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700945 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400946 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800947
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700948 if (parse_seccomp_filters(j, path, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700949 die("failed to compile seccomp filter BPF program in '%s'",
950 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800951 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400952 fclose(file);
953}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800954
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400955void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
956{
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700957 char *fd_path, *path;
958 FILE *file;
959
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400960 if (!seccomp_should_parse_filters(j))
961 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800962
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700963 file = fdopen(fd, "r");
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400964 if (!file) {
965 pdie("failed to associate stream with fd %d", fd);
966 }
967
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700968 if (asprintf(&fd_path, "/proc/self/fd/%d", fd) == -1)
969 pdie("failed to create path for fd %d", fd);
970 path = realpath(fd_path, NULL);
971 if (path == NULL)
972 pwarn("failed to get path of fd %d", fd);
973 free(fd_path);
974
975 if (parse_seccomp_filters(j, path ? path : "<fd>", file) != 0) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400976 die("failed to compile seccomp filter BPF program from fd %d",
977 fd);
978 }
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700979 free(path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400980 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500981}
982
Andrew Brestickereac28942015-11-11 16:04:46 -0800983int API minijail_use_alt_syscall(struct minijail *j, const char *table)
984{
985 j->alt_syscall_table = strdup(table);
986 if (!j->alt_syscall_table)
987 return -ENOMEM;
988 j->flags.alt_syscall = 1;
989 return 0;
990}
991
Will Drewryf89aef52011-09-16 16:48:57 -0500992struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400993 size_t available;
994 size_t total;
995 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500996};
997
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800998void marshal_state_init(struct marshal_state *state, char *buf,
999 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -04001000{
1001 state->available = available;
1002 state->buf = buf;
1003 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001004}
1005
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001006void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -04001007{
1008 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -05001009
Elly Jonese1749eb2011-10-07 13:54:59 -04001010 /* Up to |available| will be written. */
1011 if (copy_len) {
1012 memcpy(state->buf, src, copy_len);
1013 state->buf += copy_len;
1014 state->available -= copy_len;
1015 }
1016 /* |total| will contain the expected length. */
1017 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -05001018}
1019
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001020void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -07001021{
1022 marshal_append(state, m->src, strlen(m->src) + 1);
1023 marshal_append(state, m->dest, strlen(m->dest) + 1);
1024 marshal_append(state, m->type, strlen(m->type) + 1);
1025 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
1026 if (m->has_data)
1027 marshal_append(state, m->data, strlen(m->data) + 1);
1028 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
1029}
1030
Will Drewry6ac91122011-10-21 16:38:58 -05001031void minijail_marshal_helper(struct marshal_state *state,
1032 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001033{
Dylan Reid648b2202015-10-23 00:50:00 -07001034 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001035 size_t i;
1036
Elly Jonese1749eb2011-10-07 13:54:59 -04001037 marshal_append(state, (char *)j, sizeof(*j));
1038 if (j->user)
1039 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001040 if (j->suppl_gid_list) {
1041 marshal_append(state, j->suppl_gid_list,
1042 j->suppl_gid_count * sizeof(gid_t));
1043 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001044 if (j->chrootdir)
1045 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001046 if (j->hostname)
1047 marshal_append(state, j->hostname, strlen(j->hostname) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -08001048 if (j->alt_syscall_table) {
1049 marshal_append(state, j->alt_syscall_table,
1050 strlen(j->alt_syscall_table) + 1);
1051 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001052 if (j->flags.seccomp_filter && j->filter_prog) {
1053 struct sock_fprog *fp = j->filter_prog;
1054 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001055 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -04001056 }
Dylan Reid648b2202015-10-23 00:50:00 -07001057 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001058 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001059 }
Dylan Reid605ce7f2016-01-19 19:21:00 -08001060 for (i = 0; i < j->cgroup_count; ++i)
1061 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -05001062}
1063
Will Drewry6ac91122011-10-21 16:38:58 -05001064size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001065{
1066 struct marshal_state state;
1067 marshal_state_init(&state, NULL, 0);
1068 minijail_marshal_helper(&state, j);
1069 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -05001070}
1071
Elly Jonese1749eb2011-10-07 13:54:59 -04001072int minijail_marshal(const struct minijail *j, char *buf, size_t available)
1073{
1074 struct marshal_state state;
1075 marshal_state_init(&state, buf, available);
1076 minijail_marshal_helper(&state, j);
1077 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -05001078}
1079
Elly Jonese1749eb2011-10-07 13:54:59 -04001080int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
1081{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -08001082 size_t i;
1083 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -05001084 int ret = -EINVAL;
1085
Elly Jonese1749eb2011-10-07 13:54:59 -04001086 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001087 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001088 memcpy((void *)j, serialized, sizeof(*j));
1089 serialized += sizeof(*j);
1090 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001091
Will Drewrybee7ba72011-10-21 20:47:01 -05001092 /* Potentially stale pointers not used as signals. */
Luis Hector Chavez9acba452018-10-11 10:13:25 -07001093 j->preload_path = NULL;
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001094 j->pid_file_path = NULL;
1095 j->uidmap = NULL;
1096 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001097 j->mounts_head = NULL;
1098 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001099 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001100 j->hooks_head = NULL;
1101 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001102
Elly Jonese1749eb2011-10-07 13:54:59 -04001103 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001104 char *user = consumestr(&serialized, &length);
1105 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001106 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001107 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001108 if (!j->user)
1109 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001110 }
Will Drewryf89aef52011-09-16 16:48:57 -05001111
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001112 if (j->suppl_gid_list) { /* stale pointer */
1113 if (j->suppl_gid_count > NGROUPS_MAX) {
1114 goto bad_gid_list;
1115 }
1116 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1117 void *gid_list_bytes =
1118 consumebytes(gid_list_size, &serialized, &length);
1119 if (!gid_list_bytes)
1120 goto bad_gid_list;
1121
1122 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1123 if (!j->suppl_gid_list)
1124 goto bad_gid_list;
1125
1126 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1127 }
1128
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001129 if (j->chrootdir) { /* stale pointer */
1130 char *chrootdir = consumestr(&serialized, &length);
1131 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001132 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001133 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001134 if (!j->chrootdir)
1135 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001136 }
1137
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001138 if (j->hostname) { /* stale pointer */
1139 char *hostname = consumestr(&serialized, &length);
1140 if (!hostname)
1141 goto bad_hostname;
1142 j->hostname = strdup(hostname);
1143 if (!j->hostname)
1144 goto bad_hostname;
1145 }
1146
Andrew Brestickereac28942015-11-11 16:04:46 -08001147 if (j->alt_syscall_table) { /* stale pointer */
1148 char *alt_syscall_table = consumestr(&serialized, &length);
1149 if (!alt_syscall_table)
1150 goto bad_syscall_table;
1151 j->alt_syscall_table = strdup(alt_syscall_table);
1152 if (!j->alt_syscall_table)
1153 goto bad_syscall_table;
1154 }
1155
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001156 if (j->flags.seccomp_filter && j->filter_len > 0) {
1157 size_t ninstrs = j->filter_len;
1158 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1159 ninstrs > USHRT_MAX)
1160 goto bad_filters;
1161
1162 size_t program_len = ninstrs * sizeof(struct sock_filter);
1163 void *program = consumebytes(program_len, &serialized, &length);
1164 if (!program)
1165 goto bad_filters;
1166
1167 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001168 if (!j->filter_prog)
1169 goto bad_filters;
1170
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001171 j->filter_prog->len = ninstrs;
1172 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001173 if (!j->filter_prog->filter)
1174 goto bad_filter_prog_instrs;
1175
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001176 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001177 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001178
Dylan Reid648b2202015-10-23 00:50:00 -07001179 count = j->mounts_count;
1180 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001181 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001182 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001183 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001184 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001185 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001186 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001187 const char *src = consumestr(&serialized, &length);
1188 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001189 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001190 dest = consumestr(&serialized, &length);
1191 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001192 goto bad_mounts;
1193 type = consumestr(&serialized, &length);
1194 if (!type)
1195 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001196 has_data = consumebytes(sizeof(*has_data), &serialized,
1197 &length);
1198 if (!has_data)
1199 goto bad_mounts;
1200 if (*has_data) {
1201 data = consumestr(&serialized, &length);
1202 if (!data)
1203 goto bad_mounts;
1204 }
Dylan Reid648b2202015-10-23 00:50:00 -07001205 flags = consumebytes(sizeof(*flags), &serialized, &length);
1206 if (!flags)
1207 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001208 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001209 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001210 }
1211
Dylan Reid605ce7f2016-01-19 19:21:00 -08001212 count = j->cgroup_count;
1213 j->cgroup_count = 0;
1214 for (i = 0; i < count; ++i) {
1215 char *cgroup = consumestr(&serialized, &length);
1216 if (!cgroup)
1217 goto bad_cgroups;
1218 j->cgroups[i] = strdup(cgroup);
1219 if (!j->cgroups[i])
1220 goto bad_cgroups;
1221 ++j->cgroup_count;
1222 }
1223
Elly Jonese1749eb2011-10-07 13:54:59 -04001224 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001225
Dylan Reid605ce7f2016-01-19 19:21:00 -08001226bad_cgroups:
Mike Frysingerac08a682017-10-10 02:04:50 -04001227 free_mounts_list(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001228 for (i = 0; i < j->cgroup_count; ++i)
1229 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001230bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001231 if (j->flags.seccomp_filter && j->filter_len > 0) {
1232 free(j->filter_prog->filter);
1233 free(j->filter_prog);
1234 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001235bad_filter_prog_instrs:
1236 if (j->filter_prog)
1237 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001238bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001239 if (j->alt_syscall_table)
1240 free(j->alt_syscall_table);
1241bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001242 if (j->chrootdir)
1243 free(j->chrootdir);
1244bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001245 if (j->hostname)
1246 free(j->hostname);
1247bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001248 if (j->suppl_gid_list)
1249 free(j->suppl_gid_list);
1250bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001251 if (j->user)
1252 free(j->user);
1253clear_pointers:
1254 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001255 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001256 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001257 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001258 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001259 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001260out:
1261 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001262}
1263
Mike Frysinger33ffef32017-01-13 19:53:19 -05001264struct dev_spec {
1265 const char *name;
1266 mode_t mode;
1267 dev_t major, minor;
1268};
1269
1270static const struct dev_spec device_nodes[] = {
1271 {
1272 "null",
1273 S_IFCHR | 0666, 1, 3,
1274 },
1275 {
1276 "zero",
1277 S_IFCHR | 0666, 1, 5,
1278 },
1279 {
1280 "full",
1281 S_IFCHR | 0666, 1, 7,
1282 },
1283 {
1284 "urandom",
1285 S_IFCHR | 0444, 1, 9,
1286 },
1287 {
1288 "tty",
1289 S_IFCHR | 0666, 5, 0,
1290 },
1291};
1292
1293struct dev_sym_spec {
1294 const char *source, *dest;
1295};
1296
1297static const struct dev_sym_spec device_symlinks[] = {
1298 { "ptmx", "pts/ptmx", },
1299 { "fd", "/proc/self/fd", },
1300 { "stdin", "fd/0", },
1301 { "stdout", "fd/1", },
1302 { "stderr", "fd/2", },
1303};
1304
1305/*
1306 * Clean up the temporary dev path we had setup previously. In case of errors,
1307 * we don't want to go leaking empty tempdirs.
1308 */
1309static void mount_dev_cleanup(char *dev_path)
1310{
1311 umount2(dev_path, MNT_DETACH);
1312 rmdir(dev_path);
1313 free(dev_path);
1314}
1315
1316/*
1317 * Set up the pseudo /dev path at the temporary location.
1318 * See mount_dev_finalize for more details.
1319 */
1320static int mount_dev(char **dev_path_ret)
1321{
1322 int ret;
1323 int dev_fd;
1324 size_t i;
1325 mode_t mask;
1326 char *dev_path;
1327
1328 /*
1329 * Create a temp path for the /dev init. We'll relocate this to the
1330 * final location later on in the startup process.
1331 */
1332 dev_path = *dev_path_ret = strdup("/tmp/minijail.dev.XXXXXX");
1333 if (dev_path == NULL || mkdtemp(dev_path) == NULL)
1334 pdie("could not create temp path for /dev");
1335
1336 /* Set up the empty /dev mount point first. */
1337 ret = mount("minijail-devfs", dev_path, "tmpfs",
1338 MS_NOEXEC | MS_NOSUID, "size=5M,mode=755");
1339 if (ret) {
1340 rmdir(dev_path);
1341 return ret;
1342 }
1343
1344 /* We want to set the mode directly from the spec. */
1345 mask = umask(0);
1346
1347 /* Get a handle to the temp dev path for *at funcs below. */
1348 dev_fd = open(dev_path, O_DIRECTORY|O_PATH|O_CLOEXEC);
1349 if (dev_fd < 0) {
1350 ret = 1;
1351 goto done;
1352 }
1353
1354 /* Create all the nodes in /dev. */
1355 for (i = 0; i < ARRAY_SIZE(device_nodes); ++i) {
1356 const struct dev_spec *ds = &device_nodes[i];
1357 ret = mknodat(dev_fd, ds->name, ds->mode,
1358 makedev(ds->major, ds->minor));
1359 if (ret)
1360 goto done;
1361 }
1362
1363 /* Create all the symlinks in /dev. */
1364 for (i = 0; i < ARRAY_SIZE(device_symlinks); ++i) {
1365 const struct dev_sym_spec *ds = &device_symlinks[i];
1366 ret = symlinkat(ds->dest, dev_fd, ds->source);
1367 if (ret)
1368 goto done;
1369 }
1370
1371 /* Restore old mask. */
1372 done:
1373 close(dev_fd);
1374 umask(mask);
1375
1376 if (ret)
1377 mount_dev_cleanup(dev_path);
1378
1379 return ret;
1380}
1381
1382/*
1383 * Relocate the temporary /dev mount to its final /dev place.
1384 * We have to do this two step process so people can bind mount extra
1385 * /dev paths like /dev/log.
1386 */
1387static int mount_dev_finalize(const struct minijail *j, char *dev_path)
1388{
1389 int ret = -1;
1390 char *dest = NULL;
1391
1392 /* Unmount the /dev mount if possible. */
1393 if (umount2("/dev", MNT_DETACH))
1394 goto done;
1395
1396 if (asprintf(&dest, "%s/dev", j->chrootdir ? : "") < 0)
1397 goto done;
1398
1399 if (mount(dev_path, dest, NULL, MS_MOVE, NULL))
1400 goto done;
1401
1402 ret = 0;
1403 done:
1404 free(dest);
1405 mount_dev_cleanup(dev_path);
1406
1407 return ret;
1408}
1409
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001410/*
1411 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001412 * @j Minijail these mounts are for
1413 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001414 *
1415 * Returns 0 for success.
1416 */
Mike Frysinger33ffef32017-01-13 19:53:19 -05001417static int mount_one(const struct minijail *j, struct mountpoint *m,
1418 const char *dev_path)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001419{
Dylan Reid648b2202015-10-23 00:50:00 -07001420 int ret;
1421 char *dest;
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001422 int remount = 0;
1423 unsigned long original_mnt_flags = 0;
Dylan Reid648b2202015-10-23 00:50:00 -07001424
Mike Frysinger33ffef32017-01-13 19:53:19 -05001425 /* We assume |dest| has a leading "/". */
1426 if (dev_path && strncmp("/dev/", m->dest, 5) == 0) {
1427 /* Since the temp path is rooted at /dev, skip that dest part. */
1428 if (asprintf(&dest, "%s%s", dev_path, m->dest + 4) < 0)
1429 return -ENOMEM;
1430 } else {
Mike Frysingerac08a682017-10-10 02:04:50 -04001431 if (asprintf(&dest, "%s%s", j->chrootdir ?: "", m->dest) < 0)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001432 return -ENOMEM;
1433 }
Dylan Reid648b2202015-10-23 00:50:00 -07001434
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001435 ret =
1436 setup_mount_destination(m->src, dest, j->uid, j->gid,
1437 (m->flags & MS_BIND), &original_mnt_flags);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001438 if (ret) {
yusukes1b32f852018-03-05 10:24:58 -08001439 warn("creating mount target '%s' failed", dest);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001440 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001441 }
Dylan Reideec77962016-06-30 19:35:10 -07001442
Dylan Reid648b2202015-10-23 00:50:00 -07001443 /*
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001444 * Bind mounts that change the 'ro' flag have to be remounted since
1445 * 'bind' and other flags can't both be specified in the same command.
1446 * Remount after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001447 */
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001448 if ((m->flags & MS_BIND) &&
1449 ((m->flags & MS_RDONLY) != (original_mnt_flags & MS_RDONLY))) {
1450 remount = 1;
1451 /*
1452 * Restrict the mount flags to those that are user-settable in a
1453 * MS_REMOUNT request, but excluding MS_RDONLY. The
1454 * user-requested mount flags will dictate whether the remount
1455 * will have that flag or not.
1456 */
1457 original_mnt_flags &= (MS_USER_SETTABLE_MASK & ~MS_RDONLY);
Elly Jonesa1059632011-12-15 15:17:07 -05001458 }
Dylan Reid648b2202015-10-23 00:50:00 -07001459
Dylan Reid81e23972016-05-18 14:06:35 -07001460 ret = mount(m->src, dest, m->type, m->flags, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001461 if (ret) {
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001462 pwarn("bind: %s -> %s flags=%#lx", m->src, dest, m->flags);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001463 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001464 }
Dylan Reid648b2202015-10-23 00:50:00 -07001465
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001466 if (remount) {
1467 ret =
1468 mount(m->src, dest, NULL,
1469 m->flags | original_mnt_flags | MS_REMOUNT, m->data);
Mike Frysinger33ffef32017-01-13 19:53:19 -05001470 if (ret) {
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -07001471 pwarn("bind remount: %s -> %s flags=%#lx", m->src, dest,
1472 m->flags | original_mnt_flags | MS_REMOUNT);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001473 goto error;
Mike Frysinger33ffef32017-01-13 19:53:19 -05001474 }
Dylan Reid648b2202015-10-23 00:50:00 -07001475 }
1476
Elly Jones51a5b6c2011-10-12 19:09:26 -04001477 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001478 if (m->next)
Mike Frysinger33ffef32017-01-13 19:53:19 -05001479 return mount_one(j, m->next, dev_path);
Luis Hector Chavez8c3acbc2017-10-24 16:45:00 -07001480 return 0;
1481
1482error:
1483 free(dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001484 return ret;
1485}
1486
Mike Frysingerac08a682017-10-10 02:04:50 -04001487static void process_mounts_or_die(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001488{
Mike Frysingerac08a682017-10-10 02:04:50 -04001489 /*
1490 * We have to mount /dev first in case there are bind mounts from
1491 * the original /dev into the new unique tmpfs one.
1492 */
1493 char *dev_path = NULL;
1494 if (j->flags.mount_dev && mount_dev(&dev_path))
1495 pdie("mount_dev failed");
Dylan Reid648b2202015-10-23 00:50:00 -07001496
Mike Frysingerac08a682017-10-10 02:04:50 -04001497 if (j->mounts_head && mount_one(j, j->mounts_head, dev_path)) {
1498 if (dev_path) {
1499 int saved_errno = errno;
1500 mount_dev_cleanup(dev_path);
1501 errno = saved_errno;
1502 }
1503 pdie("mount_one failed");
1504 }
Mike Frysinger33ffef32017-01-13 19:53:19 -05001505
1506 /*
Mike Frysingerac08a682017-10-10 02:04:50 -04001507 * Once all bind mounts have been processed, move the temp dev to
1508 * its final /dev home.
Mike Frysinger33ffef32017-01-13 19:53:19 -05001509 */
1510 if (j->flags.mount_dev && mount_dev_finalize(j, dev_path))
Mike Frysingerac08a682017-10-10 02:04:50 -04001511 pdie("mount_dev_finalize failed");
1512}
Elly Jones51a5b6c2011-10-12 19:09:26 -04001513
Mike Frysingerac08a682017-10-10 02:04:50 -04001514static int enter_chroot(const struct minijail *j)
1515{
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001516 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1517
Elly Jones51a5b6c2011-10-12 19:09:26 -04001518 if (chroot(j->chrootdir))
1519 return -errno;
1520
1521 if (chdir("/"))
1522 return -errno;
1523
1524 return 0;
1525}
1526
Mike Frysingerac08a682017-10-10 02:04:50 -04001527static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001528{
Mike Frysingerac08a682017-10-10 02:04:50 -04001529 int oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001530
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001531 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1532
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001533 /*
1534 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001535 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001536 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001537 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001538 if (oldroot < 0)
1539 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001540 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001541 if (newroot < 0)
1542 pdie("failed to open %s for fchdir", j->chrootdir);
1543
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001544 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001545 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001546 * do a self bind mount.
1547 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001548 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1549 pdie("failed to bind mount '%s'", j->chrootdir);
1550 if (chdir(j->chrootdir))
1551 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001552 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001553 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001554
1555 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001556 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001557 * change to the old root and unmount it.
1558 */
1559 if (fchdir(oldroot))
1560 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001561
1562 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05001563 * If skip_remount_private was enabled for minijail_enter(),
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001564 * there could be a shared mount point under |oldroot|. In that case,
1565 * mounts under this shared mount point will be unmounted below, and
1566 * this unmounting will propagate to the original mount namespace
1567 * (because the mount point is shared). To prevent this unexpected
1568 * unmounting, remove these mounts from their peer groups by recursively
1569 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001570 */
1571 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001572 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001573 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001574 if (umount2(".", MNT_DETACH))
1575 pdie("umount(/)");
1576 /* Change back to the new root. */
1577 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001578 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001579 if (close(oldroot))
1580 return -errno;
1581 if (close(newroot))
1582 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001583 if (chroot("/"))
1584 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001585 /* Set correct CWD for getcwd(3). */
1586 if (chdir("/"))
1587 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001588
1589 return 0;
1590}
1591
Martin Pelikánab9eb442017-01-25 11:53:58 +11001592static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001593{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001594 const char fmt[] = "size=%zu,mode=1777";
1595 /* Count for the user storing ULLONG_MAX literally + extra space. */
1596 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1597 int ret;
1598
1599 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1600
1601 if (ret <= 0)
1602 pdie("tmpfs size spec error");
1603 else if ((size_t)ret >= sizeof(data))
1604 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001605 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001606 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001607}
1608
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001609static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001610{
1611 const char *kProcPath = "/proc";
1612 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001613 /*
1614 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001615 * /proc in our namespace, which means using MS_REMOUNT here would
1616 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001617 * namespace (!). Instead, remove their mount from our namespace lazily
1618 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001619 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001620 if (umount2(kProcPath, MNT_DETACH)) {
1621 /*
1622 * If we are in a new user namespace, umount(2) will fail.
1623 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1624 */
1625 if (j->flags.userns) {
1626 info("umount(/proc, MNT_DETACH) failed, "
1627 "this is expected when using user namespaces");
1628 } else {
1629 return -errno;
1630 }
1631 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001632 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001633 return -errno;
1634 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001635}
1636
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001637static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001638{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001639 kill(j->initpid, SIGKILL);
1640 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001641}
1642
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001643static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001644{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001645 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001646 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001647}
1648
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001649static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001650{
1651 size_t i;
1652
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001653 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001654 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001655 kill_child_and_die(j, "failed to add to cgroups");
1656 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001657}
1658
Dylan Reid0f72ef42017-06-06 15:42:49 -07001659static void set_rlimits_or_die(const struct minijail *j)
1660{
1661 size_t i;
1662
1663 for (i = 0; i < j->rlimit_count; ++i) {
1664 struct rlimit limit;
1665 limit.rlim_cur = j->rlimits[i].cur;
1666 limit.rlim_max = j->rlimits[i].max;
1667 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1668 kill_child_and_die(j, "failed to set rlimit");
1669 }
1670}
1671
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001672static void write_ugid_maps_or_die(const struct minijail *j)
1673{
1674 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1675 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001676 if (j->gidmap && j->flags.disable_setgroups) {
1677 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1678 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001679 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001680 if (ret == -ENOENT) {
1681 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1682 warn("could not disable setgroups(2)");
1683 } else
1684 kill_child_and_die(j, "failed to disable setgroups(2)");
1685 }
1686 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001687 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1688 kill_child_and_die(j, "failed to write gid_map");
1689}
1690
1691static void enter_user_namespace(const struct minijail *j)
1692{
Luis Hector Chavez71323552017-09-05 09:17:22 -07001693 int uid = j->flags.uid ? j->uid : 0;
1694 int gid = j->flags.gid ? j->gid : 0;
1695 if (j->gidmap && setresgid(gid, gid, gid)) {
1696 pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid,
1697 gid);
1698 }
1699 if (j->uidmap && setresuid(uid, uid, uid)) {
1700 pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid,
1701 uid);
1702 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001703}
1704
1705static void parent_setup_complete(int *pipe_fds)
1706{
1707 close(pipe_fds[0]);
1708 close(pipe_fds[1]);
1709}
1710
1711/*
1712 * wait_for_parent_setup: Called by the child process to wait for any
1713 * further parent-side setup to complete before continuing.
1714 */
1715static void wait_for_parent_setup(int *pipe_fds)
1716{
1717 char buf;
1718
1719 close(pipe_fds[1]);
1720
1721 /* Wait for parent to complete setup and close the pipe. */
1722 if (read(pipe_fds[0], &buf, 1) != 0)
1723 die("failed to sync with parent");
1724 close(pipe_fds[0]);
1725}
1726
1727static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001728{
Lutz Justen13807cb2017-01-03 17:11:55 +01001729 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1730 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001731 die("can only do one of inherit, keep, or set supplementary "
1732 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001733 }
1734
Lutz Justen13807cb2017-01-03 17:11:55 +01001735 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001736 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001737 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001738 } else if (j->flags.set_suppl_gids) {
1739 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001740 pdie("setgroups(suppl_gids) failed");
Luis Hector Chavez71323552017-09-05 09:17:22 -07001741 } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001742 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001743 * Only attempt to clear supplementary groups if we are changing
Luis Hector Chavez71323552017-09-05 09:17:22 -07001744 * users or groups, and if the caller did not request to disable
1745 * setgroups (used when entering a user namespace as a
1746 * non-privileged user).
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001747 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001748 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001749 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001750 }
1751
1752 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001753 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001754
1755 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001756 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001757}
1758
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001759static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1760{
1761 const uint64_t one = 1;
1762 unsigned int i;
1763 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1764 if (keep_mask & (one << i))
1765 continue;
1766 if (prctl(PR_CAPBSET_DROP, i))
1767 pdie("could not drop capability from bounding set");
1768 }
1769}
1770
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001771static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001772{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001773 if (!j->flags.use_caps)
1774 return;
1775
Elly Jonese1749eb2011-10-07 13:54:59 -04001776 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001777 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001778 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001779 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001780 unsigned int i;
1781 if (!caps)
1782 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001783 if (cap_clear(caps))
1784 die("can't clear caps");
1785
1786 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001787 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001788 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001789 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001790 flag[0] = i;
1791 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001792 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001793 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001794 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001795 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001796 die("can't add inheritable cap");
1797 }
1798 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001799 die("can't apply initial cleaned capset");
1800
1801 /*
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001802 * Instead of dropping the bounding set first, do it here in case
Kees Cook323878a2013-02-05 15:35:24 -08001803 * the caller had a more permissive bounding set which could
1804 * have been used above to raise a capability that wasn't already
1805 * present. This requires CAP_SETPCAP, so we raised/kept it above.
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001806 *
1807 * However, if we're asked to skip setting *and* locking the
1808 * SECURE_NOROOT securebit, also skip dropping the bounding set.
1809 * If the caller wants to regain all capabilities when executing a
1810 * set-user-ID-root program, allow them to do so. The default behavior
1811 * (i.e. the behavior without |securebits_skip_mask| set) will still put
1812 * the jailed process tree in a capabilities-only environment.
1813 *
1814 * We check the negated skip mask for SECURE_NOROOT and
1815 * SECURE_NOROOT_LOCKED. If the bits are set in the negated mask they
1816 * will *not* be skipped in lock_securebits(), and therefore we should
1817 * drop the bounding set.
Kees Cook323878a2013-02-05 15:35:24 -08001818 */
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -04001819 if (secure_noroot_set_and_locked(~j->securebits_skip_mask)) {
1820 drop_capbset(j->caps, last_valid_cap);
1821 } else {
1822 warn("SECURE_NOROOT not set, not dropping bounding set");
1823 }
Kees Cook323878a2013-02-05 15:35:24 -08001824
1825 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001826 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001827 flag[0] = CAP_SETPCAP;
1828 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1829 die("can't clear effective cap");
1830 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1831 die("can't clear permitted cap");
1832 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1833 die("can't clear inheritable cap");
1834 }
1835
1836 if (cap_set_proc(caps))
1837 die("can't apply final cleaned capset");
1838
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001839 /*
1840 * If ambient capabilities are supported, clear all capabilities first,
1841 * then raise the requested ones.
1842 */
1843 if (j->flags.set_ambient_caps) {
1844 if (!cap_ambient_supported()) {
1845 pdie("ambient capabilities not supported");
1846 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001847 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1848 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001849 pdie("can't clear ambient capabilities");
1850 }
1851
1852 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1853 if (!(j->caps & (one << i)))
1854 continue;
1855
1856 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1857 0) != 0) {
1858 pdie("prctl(PR_CAP_AMBIENT, "
1859 "PR_CAP_AMBIENT_RAISE, %u) failed",
1860 i);
1861 }
1862 }
1863 }
1864
Kees Cook323878a2013-02-05 15:35:24 -08001865 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001866}
1867
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001868static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001869{
1870 /*
1871 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1872 * in the kernel source tree for an explanation of the parameters.
1873 */
1874 if (j->flags.no_new_privs) {
1875 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1876 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1877 }
1878
1879 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001880 * Code running with ASan
1881 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1882 * will make system calls not included in the syscall filter policy,
1883 * which will likely crash the program. Skip setting seccomp filter in
1884 * that case.
1885 * 'running_with_asan()' has no inputs and is completely defined at
1886 * build time, so this cannot be used by an attacker to skip setting
1887 * seccomp filter.
1888 */
Evgenii Stepanov3d98f3c2018-08-23 15:06:50 -07001889 if (j->flags.seccomp_filter && running_with_asan()) {
Evgenii Stepanov825828c2018-07-27 11:57:07 -07001890 warn("running with (HW)ASan, not setting seccomp filter");
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001891 return;
1892 }
1893
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001894 if (j->flags.seccomp_filter) {
1895 if (j->flags.seccomp_filter_logging) {
1896 /*
1897 * If logging seccomp filter failures,
1898 * install the SIGSYS handler first.
1899 */
1900 if (install_sigsys_handler())
1901 pdie("failed to install SIGSYS handler");
1902 warn("logging seccomp filter failures");
1903 } else if (j->flags.seccomp_filter_tsync) {
1904 /*
1905 * If setting thread sync,
1906 * reset the SIGSYS signal handler so that
1907 * the entire thread group is killed.
1908 */
1909 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1910 pdie("failed to reset SIGSYS disposition");
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001911 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001912 }
1913
1914 /*
1915 * Install the syscall filter.
1916 */
1917 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001918 if (j->flags.seccomp_filter_tsync) {
1919 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1920 SECCOMP_FILTER_FLAG_TSYNC,
1921 j->filter_prog)) {
1922 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001923 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001924 } else {
1925 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1926 j->filter_prog)) {
1927 pdie("prctl(seccomp_filter) failed");
1928 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001929 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001930 }
1931}
1932
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001933static pid_t forward_pid = -1;
1934
Mike Frysinger33d051a2018-05-30 16:41:10 -04001935static void forward_signal(int sig,
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04001936 siginfo_t *siginfo attribute_unused,
1937 void *void_context attribute_unused)
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001938{
1939 if (forward_pid != -1) {
Mike Frysinger33d051a2018-05-30 16:41:10 -04001940 kill(forward_pid, sig);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001941 }
1942}
1943
1944static void install_signal_handlers(void)
1945{
1946 struct sigaction act;
1947
1948 memset(&act, 0, sizeof(act));
1949 act.sa_sigaction = &forward_signal;
1950 act.sa_flags = SA_SIGINFO | SA_RESTART;
1951
1952 /* Handle all signals, except SIGCHLD. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04001953 for (int sig = 1; sig < NSIG; sig++) {
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001954 /*
1955 * We don't care if we get EINVAL: that just means that we
1956 * can't handle this signal, so let's skip it and continue.
1957 */
Mike Frysinger33d051a2018-05-30 16:41:10 -04001958 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001959 }
1960 /* Reset SIGCHLD's handler. */
1961 signal(SIGCHLD, SIG_DFL);
1962
1963 /* Handle real-time signals. */
Mike Frysinger33d051a2018-05-30 16:41:10 -04001964 for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++) {
1965 sigaction(sig, &act, NULL);
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001966 }
1967}
1968
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001969static const char *lookup_hook_name(minijail_hook_event_t event)
1970{
1971 switch (event) {
1972 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
1973 return "pre-drop-caps";
1974 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
1975 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001976 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
1977 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001978 case MINIJAIL_HOOK_EVENT_MAX:
1979 /*
1980 * Adding this in favor of a default case to force the
1981 * compiler to error out if a new enum value is added.
1982 */
1983 break;
1984 }
1985 return "unknown";
1986}
1987
1988static void run_hooks_or_die(const struct minijail *j,
1989 minijail_hook_event_t event)
1990{
1991 int rc;
1992 int hook_index = 0;
1993 for (struct hook *c = j->hooks_head; c; c = c->next) {
1994 if (c->event != event)
1995 continue;
1996 rc = c->hook(c->payload);
1997 if (rc != 0) {
1998 errno = -rc;
1999 pdie("%s hook (index %d) failed",
2000 lookup_hook_name(event), hook_index);
2001 }
2002 /* Only increase the index within the same hook event type. */
2003 ++hook_index;
2004 }
2005}
2006
Will Drewry6ac91122011-10-21 16:38:58 -05002007void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002008{
Dylan Reidf682d472015-09-17 21:39:07 -07002009 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002010 * If we're dropping caps, get the last valid cap from /proc now,
2011 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07002012 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002013 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002014 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08002015 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07002016
Elly Jonese1749eb2011-10-07 13:54:59 -04002017 if (j->flags.pids)
2018 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002019 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04002020
Lutz Justen13807cb2017-01-03 17:11:55 +01002021 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05002022 die("cannot inherit supplementary groups without setting a "
2023 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04002024
Elly Jonesdd3e8512012-01-23 15:13:38 -05002025 /*
2026 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04002027 * so we don't even try. If any of our operations fail, we abort() the
2028 * entire process.
2029 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002030 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002031 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07002032
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07002033 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002034 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002035 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002036 /*
Mike Frysinger785b1c32018-02-23 15:47:24 -05002037 * By default, remount all filesystems as private, unless
2038 * - Passed a specific remount mode, in which case remount with that,
2039 * - Asked not to remount at all, in which case skip the mount(2) call.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002040 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
2041 */
Mike Frysinger785b1c32018-02-23 15:47:24 -05002042 if (j->remount_mode) {
2043 if (mount(NULL, "/", NULL, MS_REC | j->remount_mode, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002044 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
2045 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002046 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08002047 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04002048
Dylan Reidf7942472015-11-18 17:55:26 -08002049 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002050 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08002051 }
2052
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002053 if (j->flags.uts) {
2054 if (unshare(CLONE_NEWUTS))
2055 pdie("unshare(CLONE_NEWUTS) failed");
2056
2057 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
2058 pdie("sethostname(%s) failed", j->hostname);
2059 }
2060
Dylan Reid1102f5a2015-09-15 11:52:20 -07002061 if (j->flags.enter_net) {
2062 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002063 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05002064 } else if (j->flags.net) {
2065 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002066 pdie("unshare(CLONE_NEWNET) failed");
2067 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07002068 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002069
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002070 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002071 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07002072
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08002073 if (j->flags.new_session_keyring) {
2074 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
2075 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
2076 }
2077
Mike Frysingerac08a682017-10-10 02:04:50 -04002078 /* We have to process all the mounts before we chroot/pivot_root. */
2079 process_mounts_or_die(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002080
Mike Frysingerac08a682017-10-10 02:04:50 -04002081 if (j->flags.chroot && enter_chroot(j))
Mike Frysinger33ffef32017-01-13 19:53:19 -05002082 pdie("chroot");
Mike Frysinger33ffef32017-01-13 19:53:19 -05002083
Mike Frysingerac08a682017-10-10 02:04:50 -04002084 if (j->flags.pivot_root && enter_pivot_root(j))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08002085 pdie("pivot_root");
2086
Martin Pelikánab9eb442017-01-25 11:53:58 +11002087 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07002088 pdie("mount_tmp");
2089
Dylan Reid791f5772015-09-14 20:02:42 -07002090 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04002091 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04002092
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002093 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
2094
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002095 /*
2096 * If we're only dropping capabilities from the bounding set, but not
2097 * from the thread's (permitted|inheritable|effective) sets, do it now.
2098 */
2099 if (j->flags.capbset_drop) {
2100 drop_capbset(j->cap_bset, last_valid_cap);
2101 }
2102
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002103 /*
2104 * POSIX capabilities are a bit tricky. If we drop our capability to
2105 * change uids, our attempt to use drop_ugid() below will fail. Hang on
2106 * to root caps across drop_ugid(), then lock securebits.
2107 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08002108 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002109 /*
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002110 * Using ambient capabilities takes care of most of the cases
2111 * where PR_SET_KEEPCAPS would be needed, but still try to set
2112 * them unless it is locked (maybe due to running minijail
2113 * within an already-minijailed process).
Elly Jonese1749eb2011-10-07 13:54:59 -04002114 */
Luis Hector Chavez89cbc322018-08-06 11:31:15 -07002115 if (!j->flags.set_ambient_caps || !secure_keep_caps_locked()) {
2116 if (prctl(PR_SET_KEEPCAPS, 1))
2117 pdie("prctl(PR_SET_KEEPCAPS) failed");
2118 }
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002119
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -07002120 if (lock_securebits(j->securebits_skip_mask) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002121 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07002122 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002123 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002124
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002125 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002126 /*
2127 * If we're setting no_new_privs, we can drop privileges
2128 * before setting seccomp filter. This way filter policies
2129 * don't need to allow privilege-dropping syscalls.
2130 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002131 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002132 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002133 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04002134 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002135 /*
2136 * If we're not setting no_new_privs,
2137 * we need to set seccomp filter *before* dropping privileges.
2138 * WARNING: this means that filter policies *must* allow
2139 * setgroups()/setresgid()/setresuid() for dropping root and
2140 * capget()/capset()/prctl() for dropping caps.
2141 */
2142 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07002143 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08002144 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04002145 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002146
Elly Jonesdd3e8512012-01-23 15:13:38 -05002147 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08002148 * Select the specified alternate syscall table. The table must not
2149 * block prctl(2) if we're using seccomp as well.
2150 */
2151 if (j->flags.alt_syscall) {
2152 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002153 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08002154 }
2155
2156 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05002157 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04002158 * privilege-dropping syscalls :)
2159 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002160 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04002161 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002162 warn("seccomp not supported");
2163 return;
2164 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002165 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07002166 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002167}
2168
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002169/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04002170static int init_exitstatus = 0;
2171
Mike Frysingerd9ef07c2018-05-30 16:51:36 -04002172void init_term(int sig attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002173{
2174 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04002175}
2176
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002177void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04002178{
2179 pid_t pid;
2180 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002181 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002182 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04002183 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002184 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002185 /*
2186 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04002187 * left inside our pid namespace or we get a signal.
2188 */
2189 if (pid == rootpid)
2190 init_exitstatus = status;
2191 }
2192 if (!WIFEXITED(init_exitstatus))
2193 _exit(MINIJAIL_ERR_INIT);
2194 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04002195}
2196
Will Drewry6ac91122011-10-21 16:38:58 -05002197int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002198{
2199 size_t sz = 0;
2200 size_t bytes = read(fd, &sz, sizeof(sz));
2201 char *buf;
2202 int r;
2203 if (sizeof(sz) != bytes)
2204 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002205 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04002206 return -E2BIG;
2207 buf = malloc(sz);
2208 if (!buf)
2209 return -ENOMEM;
2210 bytes = read(fd, buf, sz);
2211 if (bytes != sz) {
2212 free(buf);
2213 return -EINVAL;
2214 }
2215 r = minijail_unmarshal(j, buf, sz);
2216 free(buf);
2217 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002218}
2219
Will Drewry6ac91122011-10-21 16:38:58 -05002220int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04002221{
2222 char *buf;
2223 size_t sz = minijail_size(j);
2224 ssize_t written;
2225 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04002226
Elly Jonese1749eb2011-10-07 13:54:59 -04002227 if (!sz)
2228 return -EINVAL;
2229 buf = malloc(sz);
2230 r = minijail_marshal(j, buf, sz);
2231 if (r) {
2232 free(buf);
2233 return r;
2234 }
2235 /* Sends [size][minijail]. */
2236 written = write(fd, &sz, sizeof(sz));
2237 if (written != sizeof(sz)) {
2238 free(buf);
2239 return -EFAULT;
2240 }
2241 written = write(fd, buf, sz);
2242 if (written < 0 || (size_t) written != sz) {
2243 free(buf);
2244 return -EFAULT;
2245 }
2246 free(buf);
2247 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05002248}
Elly Jonescd7a9042011-07-22 13:56:51 -04002249
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002250static int setup_preload(const struct minijail *j attribute_unused,
2251 const char *oldenv attribute_unused)
Elly Jonese1749eb2011-10-07 13:54:59 -04002252{
Daniel Erat5b7a3182015-08-19 16:06:22 -06002253#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002254 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002255 return 0;
2256#else
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002257 const char *preload_path = j->preload_path ?: PRELOADPATH;
2258 char *newenv = NULL;
2259
2260 if (!oldenv)
2261 oldenv = "";
Elly Jonescd7a9042011-07-22 13:56:51 -04002262
Elly Jonese1749eb2011-10-07 13:54:59 -04002263 /* Only insert a separating space if we have something to separate... */
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002264 if (asprintf(&newenv, "%s=%s%s%s", kLdPreloadEnvVar, oldenv,
2265 oldenv[0] != '\0' ? " " : "", preload_path) < 0) {
2266 return -ENOMEM;
2267 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002268
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002269 /* putenv(3) will now own the string. */
2270 putenv(newenv);
Elly Jonese1749eb2011-10-07 13:54:59 -04002271 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002272#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04002273}
2274
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002275static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04002276{
2277 int r = pipe(fds);
2278 char fd_buf[11];
2279 if (r)
2280 return r;
2281 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2282 if (r <= 0)
2283 return -EINVAL;
2284 setenv(kFdEnvVar, fd_buf, 1);
2285 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05002286}
2287
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002288static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002289{
2290 const char *kFdPath = "/proc/self/fd";
2291
2292 DIR *d = opendir(kFdPath);
2293 struct dirent *dir_entry;
2294
2295 if (d == NULL)
2296 return -1;
2297 int dir_fd = dirfd(d);
2298 while ((dir_entry = readdir(d)) != NULL) {
2299 size_t i;
2300 char *end;
2301 bool should_close = true;
2302 const int fd = strtol(dir_entry->d_name, &end, 10);
2303
2304 if ((*end) != '\0') {
2305 continue;
2306 }
2307 /*
2308 * We might have set up some pipes that we want to share with
2309 * the parent process, and should not be closed.
2310 */
2311 for (i = 0; i < size; ++i) {
2312 if (fd == inheritable_fds[i]) {
2313 should_close = false;
2314 break;
2315 }
2316 }
2317 /* Also avoid closing the directory fd. */
2318 if (should_close && fd != dir_fd)
2319 close(fd);
2320 }
2321 closedir(d);
2322 return 0;
2323}
2324
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002325static int redirect_fds(struct minijail *j)
2326{
2327 size_t i, i2;
2328 int closeable;
2329 for (i = 0; i < j->preserved_fd_count; i++) {
2330 if (dup2(j->preserved_fds[i].parent_fd,
2331 j->preserved_fds[i].child_fd) == -1) {
2332 return -1;
2333 }
2334 }
2335 /*
2336 * After all fds have been duped, we are now free to close all parent
2337 * fds that are *not* child fds.
2338 */
2339 for (i = 0; i < j->preserved_fd_count; i++) {
2340 closeable = true;
2341 for (i2 = 0; i2 < j->preserved_fd_count; i2++) {
2342 closeable &= j->preserved_fds[i].parent_fd !=
2343 j->preserved_fds[i2].child_fd;
2344 }
2345 if (closeable)
2346 close(j->preserved_fds[i].parent_fd);
2347 }
2348 return 0;
2349}
2350
Dylan Reidacfb8be2017-08-25 12:56:51 -07002351/*
2352 * Structure that specifies how to start a minijail.
2353 *
Dylan Reid0412dcc2017-08-24 11:33:15 -07002354 * filename - The program to exec in the child. Required if `exec_in_child` = 1.
2355 * argv - Arguments for the child program. Required if `exec_in_child` = 1.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002356 * use_preload - If true use LD_PRELOAD.
Dylan Reid0412dcc2017-08-24 11:33:15 -07002357 * exec_in_child - If true, run `filename`. Otherwise, the child will return to
2358 * the caller.
Dylan Reidacfb8be2017-08-25 12:56:51 -07002359 */
2360struct minijail_run_config {
2361 const char *filename;
2362 char *const *argv;
2363 int use_preload;
Dylan Reid0412dcc2017-08-24 11:33:15 -07002364 int exec_in_child;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002365};
2366
2367/*
2368 * Set of pointers to fill with values from minijail_run.
2369 * All arguments are allowed to be NULL if unused.
2370 *
2371 * pstdin_fd - Filled with stdin pipe if non-NULL.
2372 * pstdout_fd - Filled with stdout pipe if non-NULL.
2373 * pstderr_fd - Filled with stderr pipe if non-NULL.
2374 * pchild_pid - Filled with the pid of the child process if non-NULL.
2375 */
2376struct minijail_run_status {
2377 int *pstdin_fd;
2378 int *pstdout_fd;
2379 int *pstderr_fd;
2380 pid_t *pchild_pid;
2381};
2382
Dylan Reid18c49c82017-08-25 14:52:27 -07002383static int minijail_run_internal(struct minijail *j,
2384 const struct minijail_run_config *config,
2385 struct minijail_run_status *status_out);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002386
Will Drewry6ac91122011-10-21 16:38:58 -05002387int API minijail_run(struct minijail *j, const char *filename,
2388 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002389{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002390 struct minijail_run_config config = {
2391 .filename = filename,
2392 .argv = argv,
2393 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002394 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002395 };
2396 struct minijail_run_status status = {};
2397 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002398}
2399
2400int API minijail_run_pid(struct minijail *j, const char *filename,
2401 char *const argv[], pid_t *pchild_pid)
2402{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002403 struct minijail_run_config config = {
2404 .filename = filename,
2405 .argv = argv,
2406 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002407 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002408 };
2409 struct minijail_run_status status = {
2410 .pchild_pid = pchild_pid,
2411 };
2412 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002413}
2414
2415int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002416 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002417{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002418 struct minijail_run_config config = {
2419 .filename = filename,
2420 .argv = argv,
2421 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002422 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002423 };
2424 struct minijail_run_status status = {
2425 .pstdin_fd = pstdin_fd,
2426 };
2427 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002428}
2429
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002430int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002431 char *const argv[], pid_t *pchild_pid,
2432 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002433{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002434 struct minijail_run_config config = {
2435 .filename = filename,
2436 .argv = argv,
2437 .use_preload = true,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002438 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002439 };
2440 struct minijail_run_status status = {
2441 .pstdin_fd = pstdin_fd,
2442 .pstdout_fd = pstdout_fd,
2443 .pstderr_fd = pstderr_fd,
2444 .pchild_pid = pchild_pid,
2445 };
2446 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002447}
2448
2449int API minijail_run_no_preload(struct minijail *j, const char *filename,
2450 char *const argv[])
2451{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002452 struct minijail_run_config config = {
2453 .filename = filename,
2454 .argv = argv,
2455 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002456 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002457 };
2458 struct minijail_run_status status = {};
2459 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002460}
2461
Samuel Tan63187f42015-10-16 13:01:53 -07002462int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002463 const char *filename,
2464 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002465 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002466 int *pstdin_fd,
2467 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002468 int *pstderr_fd)
2469{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002470 struct minijail_run_config config = {
2471 .filename = filename,
2472 .argv = argv,
2473 .use_preload = false,
Dylan Reid0412dcc2017-08-24 11:33:15 -07002474 .exec_in_child = true,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002475 };
2476 struct minijail_run_status status = {
2477 .pstdin_fd = pstdin_fd,
2478 .pstdout_fd = pstdout_fd,
2479 .pstderr_fd = pstderr_fd,
2480 .pchild_pid = pchild_pid,
2481 };
2482 return minijail_run_internal(j, &config, &status);
Samuel Tan63187f42015-10-16 13:01:53 -07002483}
2484
Dylan Reid0412dcc2017-08-24 11:33:15 -07002485pid_t API minijail_fork(struct minijail *j)
2486{
2487 struct minijail_run_config config = {};
2488 struct minijail_run_status status = {};
2489 return minijail_run_internal(j, &config, &status);
2490}
2491
Dylan Reid18c49c82017-08-25 14:52:27 -07002492static int minijail_run_internal(struct minijail *j,
2493 const struct minijail_run_config *config,
2494 struct minijail_run_status *status_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002495{
Elly Jonese1749eb2011-10-07 13:54:59 -04002496 char *oldenv, *oldenv_copy = NULL;
2497 pid_t child_pid;
2498 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002499 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002500 int stdout_fds[2];
2501 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08002502 int child_sync_pipe_fds[2];
2503 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002504 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002505 /* We need to remember this across the minijail_preexec() call. */
2506 int pid_namespace = j->flags.pids;
Luis Hector Chavezac981fc2017-09-18 15:52:38 -07002507 /*
2508 * Create an init process if we are entering a pid namespace, unless the
2509 * user has explicitly opted out by calling minijail_run_as_init().
2510 */
2511 int do_init = j->flags.do_init && !j->flags.run_as_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002512 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002513
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002514 if (use_preload) {
Dylan Reid0412dcc2017-08-24 11:33:15 -07002515 if (j->hooks_head != NULL)
2516 die("Minijail hooks are not supported with LD_PRELOAD");
2517 if (!config->exec_in_child)
2518 die("minijail_fork is not supported with LD_PRELOAD");
2519
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002520 oldenv = getenv(kLdPreloadEnvVar);
2521 if (oldenv) {
2522 oldenv_copy = strdup(oldenv);
2523 if (!oldenv_copy)
2524 return -ENOMEM;
2525 }
2526
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002527 if (setup_preload(j, oldenv))
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002528 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002529 }
Will Drewryf89aef52011-09-16 16:48:57 -05002530
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002531 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002532 if (j->flags.use_caps && j->caps != 0 &&
2533 !j->flags.set_ambient_caps) {
2534 die("non-empty, non-ambient capabilities are not "
2535 "supported without LD_PRELOAD");
2536 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002537 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002538
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002539 if (use_preload) {
2540 /*
2541 * Before we fork(2) and execve(2) the child process, we need
2542 * to open a pipe(2) to send the minijail configuration over.
2543 */
2544 if (setup_pipe(pipe_fds))
2545 return -EFAULT;
2546 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002547
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002548 /*
2549 * If we want to write to the child process' standard input,
2550 * create the pipe(2) now.
2551 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002552 if (status_out->pstdin_fd) {
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002553 if (pipe(stdin_fds))
2554 return -EFAULT;
2555 }
2556
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002557 /*
2558 * If we want to read from the child process' standard output,
2559 * create the pipe(2) now.
2560 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002561 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002562 if (pipe(stdout_fds))
2563 return -EFAULT;
2564 }
2565
2566 /*
2567 * If we want to read from the child process' standard error,
2568 * create the pipe(2) now.
2569 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002570 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002571 if (pipe(stderr_fds))
2572 return -EFAULT;
2573 }
2574
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002575 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002576 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002577 * or if we need to add the child process to cgroups, create the pipe(2)
2578 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002579 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002580 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002581 sync_child = 1;
2582 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002583 return -EFAULT;
2584 }
2585
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002586 /*
2587 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002588 *
2589 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2590 *
2591 * In multithreaded programs, there are a bunch of locks inside libc,
2592 * some of which may be held by other threads at the time that we call
2593 * minijail_run_pid(). If we call fork(), glibc does its level best to
2594 * ensure that we hold all of these locks before it calls clone()
2595 * internally and drop them after clone() returns, but when we call
2596 * sys_clone(2) directly, all that gets bypassed and we end up with a
2597 * child address space where some of libc's important locks are held by
2598 * other threads (which did not get cloned, and hence will never release
2599 * those locks). This is okay so long as we call exec() immediately
2600 * after, but a bunch of seemingly-innocent libc functions like setenv()
2601 * take locks.
2602 *
2603 * Hence, only call sys_clone() if we need to, in order to get at pid
2604 * namespacing. If we follow this path, the child's address space might
2605 * have broken locks; you may only call functions that do not acquire
2606 * any locks.
2607 *
2608 * Unfortunately, fork() acquires every lock it can get its hands on, as
2609 * previously detailed, so this function is highly likely to deadlock
2610 * later on (see "deadlock here") if we're multithreaded.
2611 *
2612 * We might hack around this by having the clone()d child (init of the
2613 * pid namespace) return directly, rather than leaving the clone()d
2614 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002615 * its fork()ed child return in turn), but that process would be
2616 * crippled with its libc locks potentially broken. We might try
2617 * fork()ing in the parent before we clone() to ensure that we own all
2618 * the locks, but then we have to have the forked child hanging around
2619 * consuming resources (and possibly having file descriptors / shared
2620 * memory regions / etc attached). We'd need to keep the child around to
2621 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002622 *
2623 * TODO(ellyjones): figure out if the "forked child hanging around"
2624 * problem is fixable or not. It would be nice if we worked in this
2625 * case.
2626 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002627 if (pid_namespace) {
2628 int clone_flags = CLONE_NEWPID | SIGCHLD;
2629 if (j->flags.userns)
2630 clone_flags |= CLONE_NEWUSER;
2631 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002632 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002633 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002634 }
Elly Jones761b7412012-06-13 15:49:52 -04002635
Elly Jonese1749eb2011-10-07 13:54:59 -04002636 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002637 if (use_preload) {
2638 free(oldenv_copy);
2639 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002640 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002641 }
Will Drewryf89aef52011-09-16 16:48:57 -05002642
Elly Jonese1749eb2011-10-07 13:54:59 -04002643 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002644 if (use_preload) {
2645 /* Restore parent's LD_PRELOAD. */
2646 if (oldenv_copy) {
2647 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2648 free(oldenv_copy);
2649 } else {
2650 unsetenv(kLdPreloadEnvVar);
2651 }
2652 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002653 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002654
Elly Jonese1749eb2011-10-07 13:54:59 -04002655 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002656
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002657 if (j->flags.forward_signals) {
2658 forward_pid = child_pid;
2659 install_signal_handlers();
2660 }
2661
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002662 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002663 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002664
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002665 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002666 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002667
Dylan Reid0f72ef42017-06-06 15:42:49 -07002668 if (j->rlimit_count)
2669 set_rlimits_or_die(j);
2670
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002671 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002672 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002673
2674 if (sync_child)
2675 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002676
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002677 if (use_preload) {
2678 /* Send marshalled minijail. */
2679 close(pipe_fds[0]); /* read endpoint */
2680 ret = minijail_to_fd(j, pipe_fds[1]);
2681 close(pipe_fds[1]); /* write endpoint */
2682 if (ret) {
2683 kill(j->initpid, SIGKILL);
2684 die("failed to send marshalled minijail");
2685 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002686 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002687
Dylan Reidacfb8be2017-08-25 12:56:51 -07002688 if (status_out->pchild_pid)
2689 *status_out->pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002690
2691 /*
2692 * If we want to write to the child process' standard input,
2693 * set up the write end of the pipe.
2694 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002695 if (status_out->pstdin_fd)
2696 *status_out->pstdin_fd =
2697 setup_pipe_end(stdin_fds, 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002698
2699 /*
2700 * If we want to read from the child process' standard output,
2701 * set up the read end of the pipe.
2702 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002703 if (status_out->pstdout_fd)
2704 *status_out->pstdout_fd =
2705 setup_pipe_end(stdout_fds, 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002706
2707 /*
2708 * If we want to read from the child process' standard error,
2709 * set up the read end of the pipe.
2710 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002711 if (status_out->pstderr_fd)
2712 *status_out->pstderr_fd =
2713 setup_pipe_end(stderr_fds, 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002714
Dylan Reid0412dcc2017-08-24 11:33:15 -07002715 /*
2716 * If forking return the child pid, in the normal exec case
2717 * return 0 for success.
2718 */
2719 if (!config->exec_in_child)
2720 return child_pid;
Elly Jonese1749eb2011-10-07 13:54:59 -04002721 return 0;
2722 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002723 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002724 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002725
Peter Qiu2860c462015-12-16 15:13:06 -08002726 if (j->flags.reset_signal_mask) {
2727 sigset_t signal_mask;
2728 if (sigemptyset(&signal_mask) != 0)
2729 pdie("sigemptyset failed");
2730 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2731 pdie("sigprocmask failed");
2732 }
2733
Luis Hector Chaveza27118a2018-04-04 08:18:01 -07002734 if (j->flags.reset_signal_handlers) {
2735 int signum;
2736 for (signum = 0; signum <= SIGRTMAX; signum++) {
2737 /*
2738 * Ignore EINVAL since some signal numbers in the range
2739 * might not be valid.
2740 */
2741 if (signal(signum, SIG_DFL) == SIG_ERR &&
2742 errno != EINVAL) {
2743 pdie("failed to reset signal %d disposition",
2744 signum);
2745 }
2746 }
2747 }
2748
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002749 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002750 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002751 int inheritable_fds[kMaxInheritableFdsSize];
2752 size_t size = 0;
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002753 size_t i;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002754 if (use_preload) {
2755 inheritable_fds[size++] = pipe_fds[0];
2756 inheritable_fds[size++] = pipe_fds[1];
2757 }
2758 if (sync_child) {
2759 inheritable_fds[size++] = child_sync_pipe_fds[0];
2760 inheritable_fds[size++] = child_sync_pipe_fds[1];
2761 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002762 if (status_out->pstdin_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002763 inheritable_fds[size++] = stdin_fds[0];
2764 inheritable_fds[size++] = stdin_fds[1];
2765 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002766 if (status_out->pstdout_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002767 inheritable_fds[size++] = stdout_fds[0];
2768 inheritable_fds[size++] = stdout_fds[1];
2769 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002770 if (status_out->pstderr_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002771 inheritable_fds[size++] = stderr_fds[0];
2772 inheritable_fds[size++] = stderr_fds[1];
2773 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002774 for (i = 0; i < j->preserved_fd_count; i++) {
2775 /*
2776 * Preserve all parent_fds. They will be dup2(2)-ed in
2777 * the child later.
2778 */
2779 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
2780 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002781
2782 if (close_open_fds(inheritable_fds, size) < 0)
2783 die("failed to close open file descriptors");
2784 }
2785
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002786 if (redirect_fds(j))
2787 die("failed to set up fd redirections");
2788
Dylan Reidce5b55e2016-01-13 11:04:16 -08002789 if (sync_child)
2790 wait_for_parent_setup(child_sync_pipe_fds);
2791
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002792 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002793 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002794
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002795 /*
2796 * If we want to write to the jailed process' standard input,
2797 * set up the read end of the pipe.
2798 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002799 if (status_out->pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002800 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2801 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002802 die("failed to set up stdin pipe");
2803 }
2804
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002805 /*
2806 * If we want to read from the jailed process' standard output,
2807 * set up the write end of the pipe.
2808 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002809 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002810 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2811 STDOUT_FILENO) < 0)
2812 die("failed to set up stdout pipe");
2813 }
2814
2815 /*
2816 * If we want to read from the jailed process' standard error,
2817 * set up the write end of the pipe.
2818 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002819 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002820 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2821 STDERR_FILENO) < 0)
2822 die("failed to set up stderr pipe");
2823 }
2824
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002825 /*
2826 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2827 * This prevents the jailed process from using the TIOCSTI ioctl
2828 * to push characters into the parent process terminal's input buffer,
2829 * therefore escaping the jail.
Stephen Barber5dd5b1b2017-10-16 23:02:39 -07002830 *
2831 * Since it has just forked, the child will not be a process group
2832 * leader, and this call to setsid() should always succeed.
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002833 */
2834 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2835 isatty(STDERR_FILENO)) {
2836 if (setsid() < 0) {
2837 pdie("setsid() failed");
2838 }
2839 }
2840
Dylan Reid791f5772015-09-14 20:02:42 -07002841 /* If running an init program, let it decide when/how to mount /proc. */
2842 if (pid_namespace && !do_init)
2843 j->flags.remount_proc_ro = 0;
2844
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002845 if (use_preload) {
2846 /* Strip out flags that cannot be inherited across execve(2). */
2847 minijail_preexec(j);
2848 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002849 /*
2850 * If not using LD_PRELOAD, do all jailing before execve(2).
2851 * Note that PID namespaces can only be entered on fork(2),
2852 * so that flag is still cleared.
2853 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002854 j->flags.pids = 0;
2855 }
Dylan Reid0412dcc2017-08-24 11:33:15 -07002856
2857 /*
2858 * Jail this process.
2859 * If forking, return.
2860 * If not, execve(2) the target.
2861 */
Elly Jonese1749eb2011-10-07 13:54:59 -04002862 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002863
Dylan Reid0412dcc2017-08-24 11:33:15 -07002864 if (config->exec_in_child && pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002865 /*
2866 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002867 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002868 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002869 * a child to actually run the program. If |do_init == 0|, we
2870 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002871 *
2872 * If we're multithreaded, we'll probably deadlock here. See
2873 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002874 */
2875 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002876 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002877 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002878 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002879 /*
2880 * Best effort. Don't bother checking the return value.
2881 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002882 prctl(PR_SET_NAME, "minijail-init");
2883 init(child_pid); /* Never returns. */
2884 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002885 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002886
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002887 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
2888
Dylan Reid0412dcc2017-08-24 11:33:15 -07002889 if (!config->exec_in_child)
2890 return 0;
2891
Elly Jonesdd3e8512012-01-23 15:13:38 -05002892 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002893 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002894 * calling process
2895 * -> execve()-ing process
2896 * If we are:
2897 * calling process
2898 * -> init()-ing process
2899 * -> execve()-ing process
2900 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002901 ret = execve(config->filename, config->argv, environ);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002902 if (ret == -1) {
Dylan Reidacfb8be2017-08-25 12:56:51 -07002903 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002904 }
2905 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002906}
2907
Will Drewry6ac91122011-10-21 16:38:58 -05002908int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002909{
2910 int st;
2911 if (kill(j->initpid, SIGTERM))
2912 return -errno;
2913 if (waitpid(j->initpid, &st, 0) < 0)
2914 return -errno;
2915 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002916}
2917
Will Drewry6ac91122011-10-21 16:38:58 -05002918int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002919{
2920 int st;
2921 if (waitpid(j->initpid, &st, 0) < 0)
2922 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002923
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002924 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002925 int error_status = st;
2926 if (WIFSIGNALED(st)) {
2927 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002928 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002929 j->initpid, signum);
2930 /*
2931 * We return MINIJAIL_ERR_JAIL if the process received
2932 * SIGSYS, which happens when a syscall is blocked by
2933 * seccomp filters.
2934 * If not, we do what bash(1) does:
2935 * $? = 128 + signum
2936 */
2937 if (signum == SIGSYS) {
2938 error_status = MINIJAIL_ERR_JAIL;
2939 } else {
2940 error_status = 128 + signum;
2941 }
2942 }
2943 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002944 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002945
2946 int exit_status = WEXITSTATUS(st);
2947 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002948 info("child process %d exited with status %d",
2949 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002950
2951 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002952}
2953
Will Drewry6ac91122011-10-21 16:38:58 -05002954void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002955{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002956 size_t i;
2957
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002958 if (j->flags.seccomp_filter && j->filter_prog) {
2959 free(j->filter_prog->filter);
2960 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002961 }
Mike Frysingerac08a682017-10-10 02:04:50 -04002962 free_mounts_list(j);
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002963 while (j->hooks_head) {
2964 struct hook *c = j->hooks_head;
2965 j->hooks_head = c->next;
2966 free(c);
2967 }
2968 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002969 if (j->user)
2970 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002971 if (j->suppl_gid_list)
2972 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002973 if (j->chrootdir)
2974 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002975 if (j->pid_file_path)
2976 free(j->pid_file_path);
2977 if (j->uidmap)
2978 free(j->uidmap);
2979 if (j->gidmap)
2980 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002981 if (j->hostname)
2982 free(j->hostname);
Luis Hector Chavez9acba452018-10-11 10:13:25 -07002983 if (j->preload_path)
2984 free(j->preload_path);
Andrew Brestickereac28942015-11-11 16:04:46 -08002985 if (j->alt_syscall_table)
2986 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002987 for (i = 0; i < j->cgroup_count; ++i)
2988 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002989 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002990}
Luis Hector Chavez114a9302017-09-05 20:36:58 -07002991
2992void API minijail_log_to_fd(int fd, int min_priority)
2993{
2994 init_logging(LOG_TO_FD, fd, min_priority);
2995}