blob: d06aeeba8ed7b18294407f473e1065db51bb40be [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 <pwd.h>
17#include <sched.h>
18#include <signal.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070019#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080020#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040024#include <sys/capability.h>
25#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050026#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040027#include <sys/prctl.h>
Dylan Reid0f72ef42017-06-06 15:42:49 -070028#include <sys/resource.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070029#include <sys/stat.h>
30#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
Luis Hector Chavez1617f632017-08-01 18:32:30 -070080#define MAX_PRESERVED_FDS 10
81
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -080082/* Keyctl commands. */
83#define KEYCTL_JOIN_SESSION_KEYRING 1
84
Dylan Reid0f72ef42017-06-06 15:42:49 -070085struct minijail_rlimit {
86 int type;
87 uint32_t cur;
88 uint32_t max;
89};
90
Dylan Reid648b2202015-10-23 00:50:00 -070091struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040092 char *src;
93 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070094 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -070095 char *data;
96 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -070097 unsigned long flags;
98 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040099};
100
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700101struct hook {
102 minijail_hook_t hook;
103 void *payload;
104 minijail_hook_event_t event;
105 struct hook *next;
106};
107
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700108struct preserved_fd {
109 int parent_fd;
110 int child_fd;
111};
112
Will Drewryf89aef52011-09-16 16:48:57 -0500113struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700114 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700115 * WARNING: if you add a flag here you need to make sure it's
116 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700117 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400118 struct {
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700119 int uid : 1;
120 int gid : 1;
Lutz Justen13807cb2017-01-03 17:11:55 +0100121 int inherit_suppl_gids : 1;
122 int set_suppl_gids : 1;
123 int keep_suppl_gids : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700124 int use_caps : 1;
125 int capbset_drop : 1;
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400126 int set_ambient_caps : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700127 int vfs : 1;
128 int enter_vfs : 1;
129 int skip_remount_private : 1;
130 int pids : 1;
131 int ipc : 1;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400132 int uts : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700133 int net : 1;
134 int enter_net : 1;
135 int ns_cgroups : 1;
136 int userns : 1;
137 int disable_setgroups : 1;
138 int seccomp : 1;
139 int remount_proc_ro : 1;
140 int no_new_privs : 1;
141 int seccomp_filter : 1;
142 int seccomp_filter_tsync : 1;
143 int seccomp_filter_logging : 1;
144 int chroot : 1;
145 int pivot_root : 1;
146 int mount_tmp : 1;
147 int do_init : 1;
148 int pid_file : 1;
149 int cgroups : 1;
150 int alt_syscall : 1;
151 int reset_signal_mask : 1;
152 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800153 int new_session_keyring : 1;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400154 int forward_signals : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400155 } flags;
156 uid_t uid;
157 gid_t gid;
158 gid_t usergid;
159 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800160 size_t suppl_gid_count;
161 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400162 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800163 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400164 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700165 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700166 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400167 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800168 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800169 char *uidmap;
170 char *gidmap;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400171 char *hostname;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800172 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800173 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800174 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700175 struct mountpoint *mounts_head;
176 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800177 size_t mounts_count;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100178 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800179 char *cgroups[MAX_CGROUPS];
180 size_t cgroup_count;
Dylan Reid0f72ef42017-06-06 15:42:49 -0700181 struct minijail_rlimit rlimits[MAX_RLIMITS];
182 size_t rlimit_count;
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700183 uint64_t securebits_skip_mask;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700184 struct hook *hooks_head;
185 struct hook *hooks_tail;
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700186 struct preserved_fd preserved_fds[MAX_PRESERVED_FDS];
187 size_t preserved_fd_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500188};
189
Luis Hector Chavez64730af2017-09-13 13:18:59 -0700190static void run_hooks_or_die(const struct minijail *j,
191 minijail_hook_event_t event);
192
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700193/*
194 * Strip out flags meant for the parent.
195 * We keep things that are not inherited across execve(2) (e.g. capabilities),
196 * or are easier to set after execve(2) (e.g. seccomp filters).
197 */
198void minijail_preenter(struct minijail *j)
199{
200 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700201 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900202 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700203 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700204 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800205 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800206 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800207 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400208 j->flags.forward_signals = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700209}
210
211/*
212 * Strip out flags meant for the child.
213 * We keep things that are inherited across execve(2).
214 */
215void minijail_preexec(struct minijail *j)
216{
217 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700218 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800219 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700220 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800221 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700222 if (j->user)
223 free(j->user);
224 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800225 if (j->suppl_gid_list)
226 free(j->suppl_gid_list);
227 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700228 memset(&j->flags, 0, sizeof(j->flags));
229 /* Now restore anything we meant to keep. */
230 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700231 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800232 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700233 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800234 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700235 /* Note, |pids| will already have been used before this call. */
236}
237
238/* Minijail API. */
239
Will Drewry6ac91122011-10-21 16:38:58 -0500240struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400241{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400242 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400243}
244
Will Drewry6ac91122011-10-21 16:38:58 -0500245void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400246{
247 if (uid == 0)
248 die("useless change to uid 0");
249 j->uid = uid;
250 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400251}
252
Will Drewry6ac91122011-10-21 16:38:58 -0500253void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400254{
255 if (gid == 0)
256 die("useless change to gid 0");
257 j->gid = gid;
258 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400259}
260
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800261void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
262 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800263{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800264 size_t i;
265
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500266 if (j->flags.inherit_suppl_gids)
267 die("cannot inherit *and* set supplementary groups");
268 if (j->flags.keep_suppl_gids)
269 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800270
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800271 if (size == 0) {
272 /* Clear supplementary groups. */
273 j->suppl_gid_list = NULL;
274 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100275 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800276 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800277 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800278
279 /* Copy the gid_t array. */
280 j->suppl_gid_list = calloc(size, sizeof(gid_t));
281 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800282 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800283 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800284 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800285 j->suppl_gid_list[i] = list[i];
286 }
287 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100288 j->flags.set_suppl_gids = 1;
289}
290
291void API minijail_keep_supplementary_gids(struct minijail *j) {
292 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800293}
294
Will Drewry6ac91122011-10-21 16:38:58 -0500295int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400296{
297 char *buf = NULL;
298 struct passwd pw;
299 struct passwd *ppw = NULL;
300 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
301 if (sz == -1)
302 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400303
Elly Jonesdd3e8512012-01-23 15:13:38 -0500304 /*
305 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400306 * the maximum needed size of the buffer, so we don't have to search.
307 */
308 buf = malloc(sz);
309 if (!buf)
310 return -ENOMEM;
311 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500312 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800313 * We're safe to free the buffer here. The strings inside |pw| point
314 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800315 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
316 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500317 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400318 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700319 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400320 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700321 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400322 minijail_change_uid(j, ppw->pw_uid);
323 j->user = strdup(user);
324 if (!j->user)
325 return -ENOMEM;
326 j->usergid = ppw->pw_gid;
327 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400328}
329
Will Drewry6ac91122011-10-21 16:38:58 -0500330int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400331{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700332 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700333 struct group gr;
334 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400335 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
336 if (sz == -1)
337 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400338
Elly Jonesdd3e8512012-01-23 15:13:38 -0500339 /*
340 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400341 * the maximum needed size of the buffer, so we don't have to search.
342 */
343 buf = malloc(sz);
344 if (!buf)
345 return -ENOMEM;
346 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500347 /*
348 * We're safe to free the buffer here. The strings inside gr point
349 * inside buf, but we don't use any of them; this leaves the pointers
350 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
351 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400352 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700353 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400354 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700355 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400356 minijail_change_gid(j, pgr->gr_gid);
357 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400358}
359
Will Drewry6ac91122011-10-21 16:38:58 -0500360void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400361{
362 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400363}
364
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700365void API minijail_no_new_privs(struct minijail *j)
366{
367 j->flags.no_new_privs = 1;
368}
369
Will Drewry6ac91122011-10-21 16:38:58 -0500370void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400371{
372 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500373}
374
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400375void API minijail_set_seccomp_filter_tsync(struct minijail *j)
376{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400377 if (j->filter_len > 0 && j->filter_prog != NULL) {
378 die("minijail_set_seccomp_filter_tsync() must be called "
379 "before minijail_parse_seccomp_filters()");
380 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400381 j->flags.seccomp_filter_tsync = 1;
382}
383
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700384void API minijail_log_seccomp_filter_failures(struct minijail *j)
385{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400386 if (j->filter_len > 0 && j->filter_prog != NULL) {
387 die("minijail_log_seccomp_filter_failures() must be called "
388 "before minijail_parse_seccomp_filters()");
389 }
390 j->flags.seccomp_filter_logging = 1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700391}
392
Will Drewry6ac91122011-10-21 16:38:58 -0500393void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400394{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800395 /*
396 * 'minijail_use_caps' configures a runtime-capabilities-only
397 * environment, including a bounding set matching the thread's runtime
398 * (permitted|inheritable|effective) sets.
399 * Therefore, it will override any existing bounding set configurations
400 * since the latter would allow gaining extra runtime capabilities from
401 * file capabilities.
402 */
403 if (j->flags.capbset_drop) {
404 warn("overriding bounding set configuration");
405 j->cap_bset = 0;
406 j->flags.capbset_drop = 0;
407 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400408 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800409 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400410}
411
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800412void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
413{
414 if (j->flags.use_caps) {
415 /*
416 * 'minijail_use_caps' will have already configured a capability
417 * bounding set matching the (permitted|inheritable|effective)
418 * sets. Abort if the user tries to configure a separate
419 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
420 * are mutually exclusive.
421 */
422 die("runtime capabilities already configured, can't drop "
423 "bounding set separately");
424 }
425 j->cap_bset = capmask;
426 j->flags.capbset_drop = 1;
427}
428
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400429void API minijail_set_ambient_caps(struct minijail *j)
430{
431 j->flags.set_ambient_caps = 1;
432}
433
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800434void API minijail_reset_signal_mask(struct minijail *j)
435{
Peter Qiu2860c462015-12-16 15:13:06 -0800436 j->flags.reset_signal_mask = 1;
437}
438
Will Drewry6ac91122011-10-21 16:38:58 -0500439void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400440{
441 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400442}
443
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700444void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
445{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800446 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700447 if (ns_fd < 0) {
448 pdie("failed to open namespace '%s'", ns_path);
449 }
450 j->mountns_fd = ns_fd;
451 j->flags.enter_vfs = 1;
452}
453
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800454void API minijail_new_session_keyring(struct minijail *j)
455{
456 j->flags.new_session_keyring = 1;
457}
458
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700459void API minijail_skip_setting_securebits(struct minijail *j,
460 uint64_t securebits_skip_mask)
461{
462 j->securebits_skip_mask = securebits_skip_mask;
463}
464
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800465void API minijail_skip_remount_private(struct minijail *j)
466{
467 j->flags.skip_remount_private = 1;
468}
469
Will Drewry6ac91122011-10-21 16:38:58 -0500470void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400471{
Elly Jonese58176c2012-01-23 11:46:17 -0500472 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700473 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400474 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800475 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400476}
477
Dylan Reidf7942472015-11-18 17:55:26 -0800478void API minijail_namespace_ipc(struct minijail *j)
479{
480 j->flags.ipc = 1;
481}
482
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400483void API minijail_namespace_uts(struct minijail *j)
484{
485 j->flags.uts = 1;
486}
487
488int API minijail_namespace_set_hostname(struct minijail *j, const char *name)
489{
490 if (j->hostname)
491 return -EINVAL;
492 minijail_namespace_uts(j);
493 j->hostname = strdup(name);
494 if (!j->hostname)
495 return -ENOMEM;
496 return 0;
497}
498
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400499void API minijail_namespace_net(struct minijail *j)
500{
501 j->flags.net = 1;
502}
503
Dylan Reid1102f5a2015-09-15 11:52:20 -0700504void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
505{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800506 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700507 if (ns_fd < 0) {
508 pdie("failed to open namespace '%s'", ns_path);
509 }
510 j->netns_fd = ns_fd;
511 j->flags.enter_net = 1;
512}
513
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700514void API minijail_namespace_cgroups(struct minijail *j)
515{
516 j->flags.ns_cgroups = 1;
517}
518
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700519void API minijail_close_open_fds(struct minijail *j)
520{
521 j->flags.close_open_fds = 1;
522}
523
Dylan Reid791f5772015-09-14 20:02:42 -0700524void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400525{
526 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700527 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400528}
529
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800530void API minijail_namespace_user(struct minijail *j)
531{
532 j->flags.userns = 1;
533}
534
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400535void API minijail_namespace_user_disable_setgroups(struct minijail *j)
536{
537 j->flags.disable_setgroups = 1;
538}
539
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800540int API minijail_uidmap(struct minijail *j, const char *uidmap)
541{
542 j->uidmap = strdup(uidmap);
543 if (!j->uidmap)
544 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800545 char *ch;
546 for (ch = j->uidmap; *ch; ch++) {
547 if (*ch == ',')
548 *ch = '\n';
549 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800550 return 0;
551}
552
553int API minijail_gidmap(struct minijail *j, const char *gidmap)
554{
555 j->gidmap = strdup(gidmap);
556 if (!j->gidmap)
557 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800558 char *ch;
559 for (ch = j->gidmap; *ch; ch++) {
560 if (*ch == ',')
561 *ch = '\n';
562 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800563 return 0;
564}
565
Will Drewry6ac91122011-10-21 16:38:58 -0500566void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400567{
Lutz Justen13807cb2017-01-03 17:11:55 +0100568 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400569}
570
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800571void API minijail_run_as_init(struct minijail *j)
572{
573 /*
574 * Since the jailed program will become 'init' in the new PID namespace,
575 * Minijail does not need to fork an 'init' process.
576 */
577 j->flags.do_init = 0;
578}
579
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700580int API minijail_enter_chroot(struct minijail *j, const char *dir)
581{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400582 if (j->chrootdir)
583 return -EINVAL;
584 j->chrootdir = strdup(dir);
585 if (!j->chrootdir)
586 return -ENOMEM;
587 j->flags.chroot = 1;
588 return 0;
589}
590
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800591int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
592{
593 if (j->chrootdir)
594 return -EINVAL;
595 j->chrootdir = strdup(dir);
596 if (!j->chrootdir)
597 return -ENOMEM;
598 j->flags.pivot_root = 1;
599 return 0;
600}
601
Dylan Reida14e08d2015-10-22 21:05:29 -0700602char API *minijail_get_original_path(struct minijail *j,
603 const char *path_inside_chroot)
604{
Dylan Reid648b2202015-10-23 00:50:00 -0700605 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700606
Dylan Reid648b2202015-10-23 00:50:00 -0700607 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700608 while (b) {
609 /*
610 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700611 * mount, then the original path is exactly the source of
612 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700613 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700614 * mount source = /some/path/exe, mount dest =
615 * /chroot/path/exe Then when getting the original path of
616 * "/chroot/path/exe", the source of that mount,
617 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700618 */
619 if (!strcmp(b->dest, path_inside_chroot))
620 return strdup(b->src);
621
622 /*
623 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700624 * mount, take the suffix of the chroot path relative to the
625 * mount destination path, and append it to the mount source
626 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700627 */
628 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
629 const char *relative_path =
630 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400631 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700632 }
633 b = b->next;
634 }
635
636 /* If there is a chroot path, append |path_inside_chroot| to that. */
637 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400638 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700639
640 /* No chroot, so the path outside is the same as it is inside. */
641 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700642}
643
Martin Pelikánab9eb442017-01-25 11:53:58 +1100644size_t minijail_get_tmpfs_size(const struct minijail *j)
645{
646 return j->tmpfs_size;
647}
648
Lee Campbell11af0622014-05-22 12:36:04 -0700649void API minijail_mount_tmp(struct minijail *j)
650{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100651 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
652}
653
654void API minijail_mount_tmp_size(struct minijail *j, size_t size)
655{
656 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700657 j->flags.mount_tmp = 1;
658}
659
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800660int API minijail_write_pid_file(struct minijail *j, const char *path)
661{
662 j->pid_file_path = strdup(path);
663 if (!j->pid_file_path)
664 return -ENOMEM;
665 j->flags.pid_file = 1;
666 return 0;
667}
668
Dylan Reid605ce7f2016-01-19 19:21:00 -0800669int API minijail_add_to_cgroup(struct minijail *j, const char *path)
670{
671 if (j->cgroup_count >= MAX_CGROUPS)
672 return -ENOMEM;
673 j->cgroups[j->cgroup_count] = strdup(path);
674 if (!j->cgroups[j->cgroup_count])
675 return -ENOMEM;
676 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800677 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800678 return 0;
679}
680
Dylan Reid0f72ef42017-06-06 15:42:49 -0700681int API minijail_rlimit(struct minijail *j, int type, uint32_t cur,
682 uint32_t max)
683{
684 size_t i;
685
686 if (j->rlimit_count >= MAX_RLIMITS)
687 return -ENOMEM;
688 /* It's an error if the caller sets the same rlimit multiple times. */
689 for (i = 0; i < j->rlimit_count; i++) {
690 if (j->rlimits[i].type == type)
691 return -EEXIST;
692 }
693
694 j->rlimits[j->rlimit_count].type = type;
695 j->rlimits[j->rlimit_count].cur = cur;
696 j->rlimits[j->rlimit_count].max = max;
697 j->rlimit_count++;
698 return 0;
699}
700
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400701int API minijail_forward_signals(struct minijail *j)
702{
703 j->flags.forward_signals = 1;
704 return 0;
705}
706
Dylan Reid81e23972016-05-18 14:06:35 -0700707int API minijail_mount_with_data(struct minijail *j, const char *src,
708 const char *dest, const char *type,
709 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700710{
Dylan Reid648b2202015-10-23 00:50:00 -0700711 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400712
713 if (*dest != '/')
714 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700715 m = calloc(1, sizeof(*m));
716 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400717 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700718 m->dest = strdup(dest);
719 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400720 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700721 m->src = strdup(src);
722 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400723 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700724 m->type = strdup(type);
725 if (!m->type)
726 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700727 if (data) {
728 m->data = strdup(data);
729 if (!m->data)
730 goto error;
731 m->has_data = 1;
732 }
Dylan Reid648b2202015-10-23 00:50:00 -0700733 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400734
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800735 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400736
Elly Jonesdd3e8512012-01-23 15:13:38 -0500737 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700738 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400739 * containing vfs namespace.
740 */
741 minijail_namespace_vfs(j);
742
Dylan Reid648b2202015-10-23 00:50:00 -0700743 if (j->mounts_tail)
744 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400745 else
Dylan Reid648b2202015-10-23 00:50:00 -0700746 j->mounts_head = m;
747 j->mounts_tail = m;
748 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400749
750 return 0;
751
752error:
Dylan Reid81e23972016-05-18 14:06:35 -0700753 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700754 free(m->src);
755 free(m->dest);
756 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400757 return -ENOMEM;
758}
759
Dylan Reid81e23972016-05-18 14:06:35 -0700760int API minijail_mount(struct minijail *j, const char *src, const char *dest,
761 const char *type, unsigned long flags)
762{
763 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
764}
765
Dylan Reid648b2202015-10-23 00:50:00 -0700766int API minijail_bind(struct minijail *j, const char *src, const char *dest,
767 int writeable)
768{
769 unsigned long flags = MS_BIND;
770
771 if (!writeable)
772 flags |= MS_RDONLY;
773
774 return minijail_mount(j, src, dest, "", flags);
775}
776
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700777int API minijail_add_hook(struct minijail *j, minijail_hook_t hook,
778 void *payload, minijail_hook_event_t event)
779{
780 struct hook *c;
781
782 if (hook == NULL)
783 return -EINVAL;
784 if (event >= MINIJAIL_HOOK_EVENT_MAX)
785 return -EINVAL;
786 c = calloc(1, sizeof(*c));
787 if (!c)
788 return -ENOMEM;
789
790 c->hook = hook;
791 c->payload = payload;
792 c->event = event;
793
794 if (j->hooks_tail)
795 j->hooks_tail->next = c;
796 else
797 j->hooks_head = c;
798 j->hooks_tail = c;
799
800 return 0;
801}
802
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700803int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd)
804{
805 if (parent_fd < 0 || child_fd < 0)
806 return -EINVAL;
807 if (j->preserved_fd_count >= MAX_PRESERVED_FDS)
808 return -ENOMEM;
809 j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd;
810 j->preserved_fds[j->preserved_fd_count].child_fd = child_fd;
811 j->preserved_fd_count++;
812 return 0;
813}
814
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400815static void clear_seccomp_options(struct minijail *j)
816{
817 j->flags.seccomp_filter = 0;
818 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400819 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400820 j->filter_len = 0;
821 j->filter_prog = NULL;
822 j->flags.no_new_privs = 0;
823}
824
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400825static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400826{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400827 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400828 /*
829 * |errno| will be set to EINVAL when seccomp has not been
830 * compiled into the kernel. On certain platforms and kernel
831 * versions this is not a fatal failure. In that case, and only
832 * in that case, disable seccomp and skip loading the filters.
833 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400834 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400835 warn("not loading seccomp filters, seccomp filter not "
836 "supported");
837 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400838 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700839 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400840 /*
841 * If |errno| != EINVAL or seccomp_can_softfail() is false,
842 * we can proceed. Worst case scenario minijail_enter() will
843 * abort() if seccomp fails.
844 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700845 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400846 if (j->flags.seccomp_filter_tsync) {
847 /* Are the seccomp(2) syscall and the TSYNC option supported? */
848 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
849 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
850 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400851 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
852 warn("seccomp(2) syscall not supported");
853 clear_seccomp_options(j);
854 return 0;
855 } else if (saved_errno == EINVAL &&
856 seccomp_can_softfail()) {
857 warn(
858 "seccomp filter thread sync not supported");
859 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400860 return 0;
861 }
862 /*
863 * Similar logic here. If seccomp_can_softfail() is
864 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
865 * we can proceed. Worst case scenario minijail_enter()
866 * will abort() if seccomp or TSYNC fail.
867 */
868 }
869 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400870 return 1;
871}
872
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700873static int parse_seccomp_filters(struct minijail *j, const char *filename,
874 FILE *policy_file)
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400875{
876 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400877 int use_ret_trap =
878 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
879 int allow_logging = j->flags.seccomp_filter_logging;
880
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700881 if (compile_filter(filename, policy_file, fprog, use_ret_trap,
882 allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400883 free(fprog);
884 return -1;
885 }
886
887 j->filter_len = fprog->len;
888 j->filter_prog = fprog;
889 return 0;
890}
891
892void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
893{
894 if (!seccomp_should_parse_filters(j))
895 return;
896
Elly Jonese1749eb2011-10-07 13:54:59 -0400897 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800898 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700899 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400900 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800901
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700902 if (parse_seccomp_filters(j, path, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700903 die("failed to compile seccomp filter BPF program in '%s'",
904 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800905 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400906 fclose(file);
907}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800908
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400909void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
910{
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700911 char *fd_path, *path;
912 FILE *file;
913
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400914 if (!seccomp_should_parse_filters(j))
915 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800916
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700917 file = fdopen(fd, "r");
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400918 if (!file) {
919 pdie("failed to associate stream with fd %d", fd);
920 }
921
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700922 if (asprintf(&fd_path, "/proc/self/fd/%d", fd) == -1)
923 pdie("failed to create path for fd %d", fd);
924 path = realpath(fd_path, NULL);
925 if (path == NULL)
926 pwarn("failed to get path of fd %d", fd);
927 free(fd_path);
928
929 if (parse_seccomp_filters(j, path ? path : "<fd>", file) != 0) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400930 die("failed to compile seccomp filter BPF program from fd %d",
931 fd);
932 }
Luis Hector Chavez7624e712017-08-28 19:30:59 -0700933 free(path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400934 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500935}
936
Andrew Brestickereac28942015-11-11 16:04:46 -0800937int API minijail_use_alt_syscall(struct minijail *j, const char *table)
938{
939 j->alt_syscall_table = strdup(table);
940 if (!j->alt_syscall_table)
941 return -ENOMEM;
942 j->flags.alt_syscall = 1;
943 return 0;
944}
945
Will Drewryf89aef52011-09-16 16:48:57 -0500946struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400947 size_t available;
948 size_t total;
949 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500950};
951
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800952void marshal_state_init(struct marshal_state *state, char *buf,
953 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400954{
955 state->available = available;
956 state->buf = buf;
957 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500958}
959
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800960void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400961{
962 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500963
Elly Jonese1749eb2011-10-07 13:54:59 -0400964 /* Up to |available| will be written. */
965 if (copy_len) {
966 memcpy(state->buf, src, copy_len);
967 state->buf += copy_len;
968 state->available -= copy_len;
969 }
970 /* |total| will contain the expected length. */
971 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500972}
973
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400974void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700975{
976 marshal_append(state, m->src, strlen(m->src) + 1);
977 marshal_append(state, m->dest, strlen(m->dest) + 1);
978 marshal_append(state, m->type, strlen(m->type) + 1);
979 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
980 if (m->has_data)
981 marshal_append(state, m->data, strlen(m->data) + 1);
982 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
983}
984
Will Drewry6ac91122011-10-21 16:38:58 -0500985void minijail_marshal_helper(struct marshal_state *state,
986 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400987{
Dylan Reid648b2202015-10-23 00:50:00 -0700988 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800989 size_t i;
990
Elly Jonese1749eb2011-10-07 13:54:59 -0400991 marshal_append(state, (char *)j, sizeof(*j));
992 if (j->user)
993 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800994 if (j->suppl_gid_list) {
995 marshal_append(state, j->suppl_gid_list,
996 j->suppl_gid_count * sizeof(gid_t));
997 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400998 if (j->chrootdir)
999 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001000 if (j->hostname)
1001 marshal_append(state, j->hostname, strlen(j->hostname) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -08001002 if (j->alt_syscall_table) {
1003 marshal_append(state, j->alt_syscall_table,
1004 strlen(j->alt_syscall_table) + 1);
1005 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001006 if (j->flags.seccomp_filter && j->filter_prog) {
1007 struct sock_fprog *fp = j->filter_prog;
1008 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001009 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -04001010 }
Dylan Reid648b2202015-10-23 00:50:00 -07001011 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001012 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001013 }
Dylan Reid605ce7f2016-01-19 19:21:00 -08001014 for (i = 0; i < j->cgroup_count; ++i)
1015 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -05001016}
1017
Will Drewry6ac91122011-10-21 16:38:58 -05001018size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001019{
1020 struct marshal_state state;
1021 marshal_state_init(&state, NULL, 0);
1022 minijail_marshal_helper(&state, j);
1023 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -05001024}
1025
Elly Jonese1749eb2011-10-07 13:54:59 -04001026int minijail_marshal(const struct minijail *j, char *buf, size_t available)
1027{
1028 struct marshal_state state;
1029 marshal_state_init(&state, buf, available);
1030 minijail_marshal_helper(&state, j);
1031 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -05001032}
1033
Elly Jonese1749eb2011-10-07 13:54:59 -04001034int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
1035{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -08001036 size_t i;
1037 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -05001038 int ret = -EINVAL;
1039
Elly Jonese1749eb2011-10-07 13:54:59 -04001040 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001041 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001042 memcpy((void *)j, serialized, sizeof(*j));
1043 serialized += sizeof(*j);
1044 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001045
Will Drewrybee7ba72011-10-21 20:47:01 -05001046 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001047 j->pid_file_path = NULL;
1048 j->uidmap = NULL;
1049 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001050 j->mounts_head = NULL;
1051 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001052 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001053 j->hooks_head = NULL;
1054 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001055
Elly Jonese1749eb2011-10-07 13:54:59 -04001056 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001057 char *user = consumestr(&serialized, &length);
1058 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001059 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001060 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001061 if (!j->user)
1062 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001063 }
Will Drewryf89aef52011-09-16 16:48:57 -05001064
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001065 if (j->suppl_gid_list) { /* stale pointer */
1066 if (j->suppl_gid_count > NGROUPS_MAX) {
1067 goto bad_gid_list;
1068 }
1069 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1070 void *gid_list_bytes =
1071 consumebytes(gid_list_size, &serialized, &length);
1072 if (!gid_list_bytes)
1073 goto bad_gid_list;
1074
1075 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1076 if (!j->suppl_gid_list)
1077 goto bad_gid_list;
1078
1079 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1080 }
1081
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001082 if (j->chrootdir) { /* stale pointer */
1083 char *chrootdir = consumestr(&serialized, &length);
1084 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001085 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001086 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001087 if (!j->chrootdir)
1088 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001089 }
1090
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001091 if (j->hostname) { /* stale pointer */
1092 char *hostname = consumestr(&serialized, &length);
1093 if (!hostname)
1094 goto bad_hostname;
1095 j->hostname = strdup(hostname);
1096 if (!j->hostname)
1097 goto bad_hostname;
1098 }
1099
Andrew Brestickereac28942015-11-11 16:04:46 -08001100 if (j->alt_syscall_table) { /* stale pointer */
1101 char *alt_syscall_table = consumestr(&serialized, &length);
1102 if (!alt_syscall_table)
1103 goto bad_syscall_table;
1104 j->alt_syscall_table = strdup(alt_syscall_table);
1105 if (!j->alt_syscall_table)
1106 goto bad_syscall_table;
1107 }
1108
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001109 if (j->flags.seccomp_filter && j->filter_len > 0) {
1110 size_t ninstrs = j->filter_len;
1111 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1112 ninstrs > USHRT_MAX)
1113 goto bad_filters;
1114
1115 size_t program_len = ninstrs * sizeof(struct sock_filter);
1116 void *program = consumebytes(program_len, &serialized, &length);
1117 if (!program)
1118 goto bad_filters;
1119
1120 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001121 if (!j->filter_prog)
1122 goto bad_filters;
1123
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001124 j->filter_prog->len = ninstrs;
1125 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001126 if (!j->filter_prog->filter)
1127 goto bad_filter_prog_instrs;
1128
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001129 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001130 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001131
Dylan Reid648b2202015-10-23 00:50:00 -07001132 count = j->mounts_count;
1133 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001134 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001135 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001136 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001137 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001138 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001139 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001140 const char *src = consumestr(&serialized, &length);
1141 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001142 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001143 dest = consumestr(&serialized, &length);
1144 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001145 goto bad_mounts;
1146 type = consumestr(&serialized, &length);
1147 if (!type)
1148 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001149 has_data = consumebytes(sizeof(*has_data), &serialized,
1150 &length);
1151 if (!has_data)
1152 goto bad_mounts;
1153 if (*has_data) {
1154 data = consumestr(&serialized, &length);
1155 if (!data)
1156 goto bad_mounts;
1157 }
Dylan Reid648b2202015-10-23 00:50:00 -07001158 flags = consumebytes(sizeof(*flags), &serialized, &length);
1159 if (!flags)
1160 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001161 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001162 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001163 }
1164
Dylan Reid605ce7f2016-01-19 19:21:00 -08001165 count = j->cgroup_count;
1166 j->cgroup_count = 0;
1167 for (i = 0; i < count; ++i) {
1168 char *cgroup = consumestr(&serialized, &length);
1169 if (!cgroup)
1170 goto bad_cgroups;
1171 j->cgroups[i] = strdup(cgroup);
1172 if (!j->cgroups[i])
1173 goto bad_cgroups;
1174 ++j->cgroup_count;
1175 }
1176
Elly Jonese1749eb2011-10-07 13:54:59 -04001177 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001178
Dylan Reid605ce7f2016-01-19 19:21:00 -08001179bad_cgroups:
1180 while (j->mounts_head) {
1181 struct mountpoint *m = j->mounts_head;
1182 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07001183 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001184 free(m->type);
1185 free(m->dest);
1186 free(m->src);
1187 free(m);
1188 }
1189 for (i = 0; i < j->cgroup_count; ++i)
1190 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001191bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001192 if (j->flags.seccomp_filter && j->filter_len > 0) {
1193 free(j->filter_prog->filter);
1194 free(j->filter_prog);
1195 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001196bad_filter_prog_instrs:
1197 if (j->filter_prog)
1198 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001199bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001200 if (j->alt_syscall_table)
1201 free(j->alt_syscall_table);
1202bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001203 if (j->chrootdir)
1204 free(j->chrootdir);
1205bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001206 if (j->hostname)
1207 free(j->hostname);
1208bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001209 if (j->suppl_gid_list)
1210 free(j->suppl_gid_list);
1211bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001212 if (j->user)
1213 free(j->user);
1214clear_pointers:
1215 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001216 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001217 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001218 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001219 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001220 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001221out:
1222 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001223}
1224
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001225/*
1226 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001227 * @j Minijail these mounts are for
1228 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001229 *
1230 * Returns 0 for success.
1231 */
Dylan Reid648b2202015-10-23 00:50:00 -07001232static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001233{
Dylan Reid648b2202015-10-23 00:50:00 -07001234 int ret;
1235 char *dest;
1236 int remount_ro = 0;
1237
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001238 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001239 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001240 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001241
Mike Frysingereaab4202017-08-14 14:57:21 -04001242 if (setup_mount_destination(m->src, dest, j->uid, j->gid,
1243 (m->flags & MS_BIND)))
Dylan Reideec77962016-06-30 19:35:10 -07001244 pdie("creating mount target '%s' failed", dest);
1245
Dylan Reid648b2202015-10-23 00:50:00 -07001246 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001247 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1248 * can't both be specified in the original bind mount.
1249 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001250 */
1251 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1252 remount_ro = 1;
1253 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001254 }
Dylan Reid648b2202015-10-23 00:50:00 -07001255
Dylan Reid81e23972016-05-18 14:06:35 -07001256 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001257 if (ret)
1258 pdie("mount: %s -> %s", m->src, dest);
1259
1260 if (remount_ro) {
1261 m->flags |= MS_RDONLY;
1262 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001263 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001264 if (ret)
1265 pdie("bind ro: %s -> %s", m->src, dest);
1266 }
1267
Elly Jones51a5b6c2011-10-12 19:09:26 -04001268 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001269 if (m->next)
1270 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001271 return ret;
1272}
1273
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001274static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001275{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001276 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001277
1278 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001279 return ret;
1280
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001281 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1282
Elly Jones51a5b6c2011-10-12 19:09:26 -04001283 if (chroot(j->chrootdir))
1284 return -errno;
1285
1286 if (chdir("/"))
1287 return -errno;
1288
1289 return 0;
1290}
1291
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001292static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001293{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001294 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001295
1296 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001297 return ret;
1298
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001299 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1300
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001301 /*
1302 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001303 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001304 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001305 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001306 if (oldroot < 0)
1307 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001308 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001309 if (newroot < 0)
1310 pdie("failed to open %s for fchdir", j->chrootdir);
1311
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001312 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001313 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001314 * do a self bind mount.
1315 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001316 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1317 pdie("failed to bind mount '%s'", j->chrootdir);
1318 if (chdir(j->chrootdir))
1319 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001320 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001321 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001322
1323 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001324 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001325 * change to the old root and unmount it.
1326 */
1327 if (fchdir(oldroot))
1328 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001329
1330 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001331 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1332 * there could be a shared mount point under |oldroot|. In that case,
1333 * mounts under this shared mount point will be unmounted below, and
1334 * this unmounting will propagate to the original mount namespace
1335 * (because the mount point is shared). To prevent this unexpected
1336 * unmounting, remove these mounts from their peer groups by recursively
1337 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001338 */
1339 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001340 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001341 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001342 if (umount2(".", MNT_DETACH))
1343 pdie("umount(/)");
1344 /* Change back to the new root. */
1345 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001346 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001347 if (close(oldroot))
1348 return -errno;
1349 if (close(newroot))
1350 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001351 if (chroot("/"))
1352 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001353 /* Set correct CWD for getcwd(3). */
1354 if (chdir("/"))
1355 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001356
1357 return 0;
1358}
1359
Martin Pelikánab9eb442017-01-25 11:53:58 +11001360static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001361{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001362 const char fmt[] = "size=%zu,mode=1777";
1363 /* Count for the user storing ULLONG_MAX literally + extra space. */
1364 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1365 int ret;
1366
1367 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1368
1369 if (ret <= 0)
1370 pdie("tmpfs size spec error");
1371 else if ((size_t)ret >= sizeof(data))
1372 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001373 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001374 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001375}
1376
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001377static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001378{
1379 const char *kProcPath = "/proc";
1380 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001381 /*
1382 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001383 * /proc in our namespace, which means using MS_REMOUNT here would
1384 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001385 * namespace (!). Instead, remove their mount from our namespace lazily
1386 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001387 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001388 if (umount2(kProcPath, MNT_DETACH)) {
1389 /*
1390 * If we are in a new user namespace, umount(2) will fail.
1391 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1392 */
1393 if (j->flags.userns) {
1394 info("umount(/proc, MNT_DETACH) failed, "
1395 "this is expected when using user namespaces");
1396 } else {
1397 return -errno;
1398 }
1399 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001400 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001401 return -errno;
1402 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001403}
1404
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001405static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001406{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001407 kill(j->initpid, SIGKILL);
1408 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001409}
1410
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001411static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001412{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001413 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001414 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001415}
1416
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001417static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001418{
1419 size_t i;
1420
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001421 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001422 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001423 kill_child_and_die(j, "failed to add to cgroups");
1424 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001425}
1426
Dylan Reid0f72ef42017-06-06 15:42:49 -07001427static void set_rlimits_or_die(const struct minijail *j)
1428{
1429 size_t i;
1430
1431 for (i = 0; i < j->rlimit_count; ++i) {
1432 struct rlimit limit;
1433 limit.rlim_cur = j->rlimits[i].cur;
1434 limit.rlim_max = j->rlimits[i].max;
1435 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1436 kill_child_and_die(j, "failed to set rlimit");
1437 }
1438}
1439
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001440static void write_ugid_maps_or_die(const struct minijail *j)
1441{
1442 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1443 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001444 if (j->gidmap && j->flags.disable_setgroups) {
1445 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1446 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001447 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001448 if (ret == -ENOENT) {
1449 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1450 warn("could not disable setgroups(2)");
1451 } else
1452 kill_child_and_die(j, "failed to disable setgroups(2)");
1453 }
1454 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001455 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1456 kill_child_and_die(j, "failed to write gid_map");
1457}
1458
1459static void enter_user_namespace(const struct minijail *j)
1460{
1461 if (j->uidmap && setresuid(0, 0, 0))
1462 pdie("user_namespaces: setresuid(0, 0, 0) failed");
1463 if (j->gidmap && setresgid(0, 0, 0))
1464 pdie("user_namespaces: setresgid(0, 0, 0) failed");
1465}
1466
1467static void parent_setup_complete(int *pipe_fds)
1468{
1469 close(pipe_fds[0]);
1470 close(pipe_fds[1]);
1471}
1472
1473/*
1474 * wait_for_parent_setup: Called by the child process to wait for any
1475 * further parent-side setup to complete before continuing.
1476 */
1477static void wait_for_parent_setup(int *pipe_fds)
1478{
1479 char buf;
1480
1481 close(pipe_fds[1]);
1482
1483 /* Wait for parent to complete setup and close the pipe. */
1484 if (read(pipe_fds[0], &buf, 1) != 0)
1485 die("failed to sync with parent");
1486 close(pipe_fds[0]);
1487}
1488
1489static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001490{
Lutz Justen13807cb2017-01-03 17:11:55 +01001491 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1492 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001493 die("can only do one of inherit, keep, or set supplementary "
1494 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001495 }
1496
Lutz Justen13807cb2017-01-03 17:11:55 +01001497 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001498 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001499 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001500 } else if (j->flags.set_suppl_gids) {
1501 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001502 pdie("setgroups(suppl_gids) failed");
Lutz Justen13807cb2017-01-03 17:11:55 +01001503 } else if (!j->flags.keep_suppl_gids) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001504 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001505 * Only attempt to clear supplementary groups if we are changing
Lutz Justen13807cb2017-01-03 17:11:55 +01001506 * users or groups.
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001507 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001508 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001509 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001510 }
1511
1512 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001513 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001514
1515 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001516 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001517}
1518
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001519static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1520{
1521 const uint64_t one = 1;
1522 unsigned int i;
1523 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1524 if (keep_mask & (one << i))
1525 continue;
1526 if (prctl(PR_CAPBSET_DROP, i))
1527 pdie("could not drop capability from bounding set");
1528 }
1529}
1530
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001531static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001532{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001533 if (!j->flags.use_caps)
1534 return;
1535
Elly Jonese1749eb2011-10-07 13:54:59 -04001536 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001537 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001538 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001539 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001540 unsigned int i;
1541 if (!caps)
1542 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001543 if (cap_clear(caps))
1544 die("can't clear caps");
1545
1546 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001547 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001548 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001549 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001550 flag[0] = i;
1551 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001552 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001553 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001554 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001555 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001556 die("can't add inheritable cap");
1557 }
1558 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001559 die("can't apply initial cleaned capset");
1560
1561 /*
1562 * Instead of dropping bounding set first, do it here in case
1563 * the caller had a more permissive bounding set which could
1564 * have been used above to raise a capability that wasn't already
1565 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1566 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001567 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001568
1569 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001570 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001571 flag[0] = CAP_SETPCAP;
1572 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1573 die("can't clear effective cap");
1574 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1575 die("can't clear permitted cap");
1576 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1577 die("can't clear inheritable cap");
1578 }
1579
1580 if (cap_set_proc(caps))
1581 die("can't apply final cleaned capset");
1582
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001583 /*
1584 * If ambient capabilities are supported, clear all capabilities first,
1585 * then raise the requested ones.
1586 */
1587 if (j->flags.set_ambient_caps) {
1588 if (!cap_ambient_supported()) {
1589 pdie("ambient capabilities not supported");
1590 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001591 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1592 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001593 pdie("can't clear ambient capabilities");
1594 }
1595
1596 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1597 if (!(j->caps & (one << i)))
1598 continue;
1599
1600 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1601 0) != 0) {
1602 pdie("prctl(PR_CAP_AMBIENT, "
1603 "PR_CAP_AMBIENT_RAISE, %u) failed",
1604 i);
1605 }
1606 }
1607 }
1608
Kees Cook323878a2013-02-05 15:35:24 -08001609 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001610}
1611
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001612static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001613{
1614 /*
1615 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1616 * in the kernel source tree for an explanation of the parameters.
1617 */
1618 if (j->flags.no_new_privs) {
1619 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1620 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1621 }
1622
1623 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001624 * Code running with ASan
1625 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1626 * will make system calls not included in the syscall filter policy,
1627 * which will likely crash the program. Skip setting seccomp filter in
1628 * that case.
1629 * 'running_with_asan()' has no inputs and is completely defined at
1630 * build time, so this cannot be used by an attacker to skip setting
1631 * seccomp filter.
1632 */
1633 if (j->flags.seccomp_filter && running_with_asan()) {
1634 warn("running with ASan, not setting seccomp filter");
1635 return;
1636 }
1637
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001638 if (j->flags.seccomp_filter) {
1639 if (j->flags.seccomp_filter_logging) {
1640 /*
1641 * If logging seccomp filter failures,
1642 * install the SIGSYS handler first.
1643 */
1644 if (install_sigsys_handler())
1645 pdie("failed to install SIGSYS handler");
1646 warn("logging seccomp filter failures");
1647 } else if (j->flags.seccomp_filter_tsync) {
1648 /*
1649 * If setting thread sync,
1650 * reset the SIGSYS signal handler so that
1651 * the entire thread group is killed.
1652 */
1653 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1654 pdie("failed to reset SIGSYS disposition");
1655 info("reset SIGSYS disposition");
1656 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001657 }
1658
1659 /*
1660 * Install the syscall filter.
1661 */
1662 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001663 if (j->flags.seccomp_filter_tsync) {
1664 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1665 SECCOMP_FILTER_FLAG_TSYNC,
1666 j->filter_prog)) {
1667 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001668 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001669 } else {
1670 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1671 j->filter_prog)) {
1672 pdie("prctl(seccomp_filter) failed");
1673 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001674 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001675 }
1676}
1677
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001678static pid_t forward_pid = -1;
1679
1680static void forward_signal(__attribute__((unused)) int nr,
1681 __attribute__((unused)) siginfo_t *siginfo,
1682 __attribute__((unused)) void *void_context)
1683{
1684 if (forward_pid != -1) {
1685 kill(forward_pid, nr);
1686 }
1687}
1688
1689static void install_signal_handlers(void)
1690{
1691 struct sigaction act;
1692
1693 memset(&act, 0, sizeof(act));
1694 act.sa_sigaction = &forward_signal;
1695 act.sa_flags = SA_SIGINFO | SA_RESTART;
1696
1697 /* Handle all signals, except SIGCHLD. */
1698 for (int nr = 1; nr < NSIG; nr++) {
1699 /*
1700 * We don't care if we get EINVAL: that just means that we
1701 * can't handle this signal, so let's skip it and continue.
1702 */
1703 sigaction(nr, &act, NULL);
1704 }
1705 /* Reset SIGCHLD's handler. */
1706 signal(SIGCHLD, SIG_DFL);
1707
1708 /* Handle real-time signals. */
1709 for (int nr = SIGRTMIN; nr <= SIGRTMAX; nr++) {
1710 sigaction(nr, &act, NULL);
1711 }
1712}
1713
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001714static const char *lookup_hook_name(minijail_hook_event_t event)
1715{
1716 switch (event) {
1717 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
1718 return "pre-drop-caps";
1719 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
1720 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001721 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
1722 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001723 case MINIJAIL_HOOK_EVENT_MAX:
1724 /*
1725 * Adding this in favor of a default case to force the
1726 * compiler to error out if a new enum value is added.
1727 */
1728 break;
1729 }
1730 return "unknown";
1731}
1732
1733static void run_hooks_or_die(const struct minijail *j,
1734 minijail_hook_event_t event)
1735{
1736 int rc;
1737 int hook_index = 0;
1738 for (struct hook *c = j->hooks_head; c; c = c->next) {
1739 if (c->event != event)
1740 continue;
1741 rc = c->hook(c->payload);
1742 if (rc != 0) {
1743 errno = -rc;
1744 pdie("%s hook (index %d) failed",
1745 lookup_hook_name(event), hook_index);
1746 }
1747 /* Only increase the index within the same hook event type. */
1748 ++hook_index;
1749 }
1750}
1751
Will Drewry6ac91122011-10-21 16:38:58 -05001752void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001753{
Dylan Reidf682d472015-09-17 21:39:07 -07001754 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001755 * If we're dropping caps, get the last valid cap from /proc now,
1756 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001757 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001758 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001759 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001760 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001761
Elly Jonese1749eb2011-10-07 13:54:59 -04001762 if (j->flags.pids)
1763 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001764 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001765
Lutz Justen13807cb2017-01-03 17:11:55 +01001766 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001767 die("cannot inherit supplementary groups without setting a "
1768 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001769
Elly Jonesdd3e8512012-01-23 15:13:38 -05001770 /*
1771 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001772 * so we don't even try. If any of our operations fail, we abort() the
1773 * entire process.
1774 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001775 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001776 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001777
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001778 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001779 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001780 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001781 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001782 * Unless asked not to, remount all filesystems as private.
1783 * If they are shared, new bind mounts will creep out of our
1784 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001785 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1786 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001787 if (!j->flags.skip_remount_private) {
1788 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001789 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1790 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001791 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001792 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001793
Dylan Reidf7942472015-11-18 17:55:26 -08001794 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001795 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001796 }
1797
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001798 if (j->flags.uts) {
1799 if (unshare(CLONE_NEWUTS))
1800 pdie("unshare(CLONE_NEWUTS) failed");
1801
1802 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
1803 pdie("sethostname(%s) failed", j->hostname);
1804 }
1805
Dylan Reid1102f5a2015-09-15 11:52:20 -07001806 if (j->flags.enter_net) {
1807 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001808 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001809 } else if (j->flags.net) {
1810 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001811 pdie("unshare(CLONE_NEWNET) failed");
1812 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001813 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001814
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001815 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001816 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001817
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08001818 if (j->flags.new_session_keyring) {
1819 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
1820 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
1821 }
1822
Elly Jones51a5b6c2011-10-12 19:09:26 -04001823 if (j->flags.chroot && enter_chroot(j))
1824 pdie("chroot");
1825
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001826 if (j->flags.pivot_root && enter_pivot_root(j))
1827 pdie("pivot_root");
1828
Martin Pelikánab9eb442017-01-25 11:53:58 +11001829 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07001830 pdie("mount_tmp");
1831
Dylan Reid791f5772015-09-14 20:02:42 -07001832 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001833 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001834
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001835 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
1836
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001837 /*
1838 * If we're only dropping capabilities from the bounding set, but not
1839 * from the thread's (permitted|inheritable|effective) sets, do it now.
1840 */
1841 if (j->flags.capbset_drop) {
1842 drop_capbset(j->cap_bset, last_valid_cap);
1843 }
1844
1845 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001846 /*
1847 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001848 * capability to change uids, our attempt to use setuid()
1849 * below will fail. Hang on to root caps across setuid(), then
1850 * lock securebits.
1851 */
1852 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001853 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001854
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -07001855 if (lock_securebits(j->securebits_skip_mask) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001856 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001857 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001858 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001859
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001860 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001861 /*
1862 * If we're setting no_new_privs, we can drop privileges
1863 * before setting seccomp filter. This way filter policies
1864 * don't need to allow privilege-dropping syscalls.
1865 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001866 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001867 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001868 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001869 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001870 /*
1871 * If we're not setting no_new_privs,
1872 * we need to set seccomp filter *before* dropping privileges.
1873 * WARNING: this means that filter policies *must* allow
1874 * setgroups()/setresgid()/setresuid() for dropping root and
1875 * capget()/capset()/prctl() for dropping caps.
1876 */
1877 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001878 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001879 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001880 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001881
Elly Jonesdd3e8512012-01-23 15:13:38 -05001882 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001883 * Select the specified alternate syscall table. The table must not
1884 * block prctl(2) if we're using seccomp as well.
1885 */
1886 if (j->flags.alt_syscall) {
1887 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001888 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08001889 }
1890
1891 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001892 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001893 * privilege-dropping syscalls :)
1894 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001895 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001896 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001897 warn("seccomp not supported");
1898 return;
1899 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001900 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001901 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001902}
1903
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001904/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001905static int init_exitstatus = 0;
1906
Will Drewry6ac91122011-10-21 16:38:58 -05001907void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001908{
1909 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001910}
1911
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001912void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001913{
1914 pid_t pid;
1915 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001916 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001917 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001918 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001919 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001920 /*
1921 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001922 * left inside our pid namespace or we get a signal.
1923 */
1924 if (pid == rootpid)
1925 init_exitstatus = status;
1926 }
1927 if (!WIFEXITED(init_exitstatus))
1928 _exit(MINIJAIL_ERR_INIT);
1929 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001930}
1931
Will Drewry6ac91122011-10-21 16:38:58 -05001932int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001933{
1934 size_t sz = 0;
1935 size_t bytes = read(fd, &sz, sizeof(sz));
1936 char *buf;
1937 int r;
1938 if (sizeof(sz) != bytes)
1939 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001940 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001941 return -E2BIG;
1942 buf = malloc(sz);
1943 if (!buf)
1944 return -ENOMEM;
1945 bytes = read(fd, buf, sz);
1946 if (bytes != sz) {
1947 free(buf);
1948 return -EINVAL;
1949 }
1950 r = minijail_unmarshal(j, buf, sz);
1951 free(buf);
1952 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001953}
1954
Will Drewry6ac91122011-10-21 16:38:58 -05001955int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001956{
1957 char *buf;
1958 size_t sz = minijail_size(j);
1959 ssize_t written;
1960 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001961
Elly Jonese1749eb2011-10-07 13:54:59 -04001962 if (!sz)
1963 return -EINVAL;
1964 buf = malloc(sz);
1965 r = minijail_marshal(j, buf, sz);
1966 if (r) {
1967 free(buf);
1968 return r;
1969 }
1970 /* Sends [size][minijail]. */
1971 written = write(fd, &sz, sizeof(sz));
1972 if (written != sizeof(sz)) {
1973 free(buf);
1974 return -EFAULT;
1975 }
1976 written = write(fd, buf, sz);
1977 if (written < 0 || (size_t) written != sz) {
1978 free(buf);
1979 return -EFAULT;
1980 }
1981 free(buf);
1982 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001983}
Elly Jonescd7a9042011-07-22 13:56:51 -04001984
Will Drewry6ac91122011-10-21 16:38:58 -05001985int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001986{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001987#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001988 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001989 return 0;
1990#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001991 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1992 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1993 if (!newenv)
1994 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001995
Elly Jonese1749eb2011-10-07 13:54:59 -04001996 /* Only insert a separating space if we have something to separate... */
1997 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1998 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001999
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002000 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002001 setenv(kLdPreloadEnvVar, newenv, 1);
2002 free(newenv);
2003 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07002004#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04002005}
2006
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002007static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04002008{
2009 int r = pipe(fds);
2010 char fd_buf[11];
2011 if (r)
2012 return r;
2013 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2014 if (r <= 0)
2015 return -EINVAL;
2016 setenv(kFdEnvVar, fd_buf, 1);
2017 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05002018}
2019
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002020static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002021{
2022 const char *kFdPath = "/proc/self/fd";
2023
2024 DIR *d = opendir(kFdPath);
2025 struct dirent *dir_entry;
2026
2027 if (d == NULL)
2028 return -1;
2029 int dir_fd = dirfd(d);
2030 while ((dir_entry = readdir(d)) != NULL) {
2031 size_t i;
2032 char *end;
2033 bool should_close = true;
2034 const int fd = strtol(dir_entry->d_name, &end, 10);
2035
2036 if ((*end) != '\0') {
2037 continue;
2038 }
2039 /*
2040 * We might have set up some pipes that we want to share with
2041 * the parent process, and should not be closed.
2042 */
2043 for (i = 0; i < size; ++i) {
2044 if (fd == inheritable_fds[i]) {
2045 should_close = false;
2046 break;
2047 }
2048 }
2049 /* Also avoid closing the directory fd. */
2050 if (should_close && fd != dir_fd)
2051 close(fd);
2052 }
2053 closedir(d);
2054 return 0;
2055}
2056
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002057static int redirect_fds(struct minijail *j)
2058{
2059 size_t i, i2;
2060 int closeable;
2061 for (i = 0; i < j->preserved_fd_count; i++) {
2062 if (dup2(j->preserved_fds[i].parent_fd,
2063 j->preserved_fds[i].child_fd) == -1) {
2064 return -1;
2065 }
2066 }
2067 /*
2068 * After all fds have been duped, we are now free to close all parent
2069 * fds that are *not* child fds.
2070 */
2071 for (i = 0; i < j->preserved_fd_count; i++) {
2072 closeable = true;
2073 for (i2 = 0; i2 < j->preserved_fd_count; i2++) {
2074 closeable &= j->preserved_fds[i].parent_fd !=
2075 j->preserved_fds[i2].child_fd;
2076 }
2077 if (closeable)
2078 close(j->preserved_fds[i].parent_fd);
2079 }
2080 return 0;
2081}
2082
Dylan Reidacfb8be2017-08-25 12:56:51 -07002083/*
2084 * Structure that specifies how to start a minijail.
2085 *
2086 * filename - The program to exec in the child.
2087 * argv - Arguments for the child program.
2088 * use_preload - If true use LD_PRELOAD.
2089 */
2090struct minijail_run_config {
2091 const char *filename;
2092 char *const *argv;
2093 int use_preload;
2094};
2095
2096/*
2097 * Set of pointers to fill with values from minijail_run.
2098 * All arguments are allowed to be NULL if unused.
2099 *
2100 * pstdin_fd - Filled with stdin pipe if non-NULL.
2101 * pstdout_fd - Filled with stdout pipe if non-NULL.
2102 * pstderr_fd - Filled with stderr pipe if non-NULL.
2103 * pchild_pid - Filled with the pid of the child process if non-NULL.
2104 */
2105struct minijail_run_status {
2106 int *pstdin_fd;
2107 int *pstdout_fd;
2108 int *pstderr_fd;
2109 pid_t *pchild_pid;
2110};
2111
Dylan Reid18c49c82017-08-25 14:52:27 -07002112static int minijail_run_internal(struct minijail *j,
2113 const struct minijail_run_config *config,
2114 struct minijail_run_status *status_out);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002115
Will Drewry6ac91122011-10-21 16:38:58 -05002116int API minijail_run(struct minijail *j, const char *filename,
2117 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002118{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002119 struct minijail_run_config config = {
2120 .filename = filename,
2121 .argv = argv,
2122 .use_preload = true,
2123 };
2124 struct minijail_run_status status = {};
2125 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002126}
2127
2128int API minijail_run_pid(struct minijail *j, const char *filename,
2129 char *const argv[], pid_t *pchild_pid)
2130{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002131 struct minijail_run_config config = {
2132 .filename = filename,
2133 .argv = argv,
2134 .use_preload = true,
2135 };
2136 struct minijail_run_status status = {
2137 .pchild_pid = pchild_pid,
2138 };
2139 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002140}
2141
2142int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002143 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002144{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002145 struct minijail_run_config config = {
2146 .filename = filename,
2147 .argv = argv,
2148 .use_preload = true,
2149 };
2150 struct minijail_run_status status = {
2151 .pstdin_fd = pstdin_fd,
2152 };
2153 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002154}
2155
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002156int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002157 char *const argv[], pid_t *pchild_pid,
2158 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002159{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002160 struct minijail_run_config config = {
2161 .filename = filename,
2162 .argv = argv,
2163 .use_preload = true,
2164 };
2165 struct minijail_run_status status = {
2166 .pstdin_fd = pstdin_fd,
2167 .pstdout_fd = pstdout_fd,
2168 .pstderr_fd = pstderr_fd,
2169 .pchild_pid = pchild_pid,
2170 };
2171 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002172}
2173
2174int API minijail_run_no_preload(struct minijail *j, const char *filename,
2175 char *const argv[])
2176{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002177 struct minijail_run_config config = {
2178 .filename = filename,
2179 .argv = argv,
2180 .use_preload = false,
2181 };
2182 struct minijail_run_status status = {};
2183 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002184}
2185
Samuel Tan63187f42015-10-16 13:01:53 -07002186int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002187 const char *filename,
2188 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002189 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002190 int *pstdin_fd,
2191 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002192 int *pstderr_fd)
2193{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002194 struct minijail_run_config config = {
2195 .filename = filename,
2196 .argv = argv,
2197 .use_preload = false,
2198 };
2199 struct minijail_run_status status = {
2200 .pstdin_fd = pstdin_fd,
2201 .pstdout_fd = pstdout_fd,
2202 .pstderr_fd = pstderr_fd,
2203 .pchild_pid = pchild_pid,
2204 };
2205 return minijail_run_internal(j, &config, &status);
Samuel Tan63187f42015-10-16 13:01:53 -07002206}
2207
Dylan Reid18c49c82017-08-25 14:52:27 -07002208static int minijail_run_internal(struct minijail *j,
2209 const struct minijail_run_config *config,
2210 struct minijail_run_status *status_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002211{
Elly Jonese1749eb2011-10-07 13:54:59 -04002212 char *oldenv, *oldenv_copy = NULL;
2213 pid_t child_pid;
2214 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002215 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002216 int stdout_fds[2];
2217 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08002218 int child_sync_pipe_fds[2];
2219 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002220 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002221 /* We need to remember this across the minijail_preexec() call. */
2222 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002223 int do_init = j->flags.do_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002224 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002225
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002226 if (use_preload) {
2227 oldenv = getenv(kLdPreloadEnvVar);
2228 if (oldenv) {
2229 oldenv_copy = strdup(oldenv);
2230 if (!oldenv_copy)
2231 return -ENOMEM;
2232 }
2233
2234 if (setup_preload())
2235 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002236 }
Will Drewryf89aef52011-09-16 16:48:57 -05002237
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002238 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002239 if (j->flags.use_caps && j->caps != 0 &&
2240 !j->flags.set_ambient_caps) {
2241 die("non-empty, non-ambient capabilities are not "
2242 "supported without LD_PRELOAD");
2243 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002244 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002245
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002246 if (use_preload && j->hooks_head != NULL) {
2247 die("Minijail hooks are not supported with LD_PRELOAD");
2248 }
2249
Elly Jonesdd3e8512012-01-23 15:13:38 -05002250 /*
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002251 * Make the process group ID of this process equal to its PID.
2252 * In the non-interactive case (e.g. when the parent process is started
2253 * from init) this ensures the parent process and the jailed process
2254 * can be killed together.
2255 * When the parent process is started from the console this ensures
2256 * the call to setsid(2) in the jailed process succeeds.
2257 *
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002258 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
2259 * the process is already a process group leader.
2260 */
2261 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
2262 if (errno != EPERM) {
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002263 pdie("setpgid(0, 0) failed");
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002264 }
2265 }
2266
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002267 if (use_preload) {
2268 /*
2269 * Before we fork(2) and execve(2) the child process, we need
2270 * to open a pipe(2) to send the minijail configuration over.
2271 */
2272 if (setup_pipe(pipe_fds))
2273 return -EFAULT;
2274 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002275
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002276 /*
2277 * If we want to write to the child process' standard input,
2278 * create the pipe(2) now.
2279 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002280 if (status_out->pstdin_fd) {
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002281 if (pipe(stdin_fds))
2282 return -EFAULT;
2283 }
2284
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002285 /*
2286 * If we want to read from the child process' standard output,
2287 * create the pipe(2) now.
2288 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002289 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002290 if (pipe(stdout_fds))
2291 return -EFAULT;
2292 }
2293
2294 /*
2295 * If we want to read from the child process' standard error,
2296 * create the pipe(2) now.
2297 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002298 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002299 if (pipe(stderr_fds))
2300 return -EFAULT;
2301 }
2302
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002303 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002304 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002305 * or if we need to add the child process to cgroups, create the pipe(2)
2306 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002307 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002308 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002309 sync_child = 1;
2310 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002311 return -EFAULT;
2312 }
2313
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002314 /*
2315 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002316 *
2317 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2318 *
2319 * In multithreaded programs, there are a bunch of locks inside libc,
2320 * some of which may be held by other threads at the time that we call
2321 * minijail_run_pid(). If we call fork(), glibc does its level best to
2322 * ensure that we hold all of these locks before it calls clone()
2323 * internally and drop them after clone() returns, but when we call
2324 * sys_clone(2) directly, all that gets bypassed and we end up with a
2325 * child address space where some of libc's important locks are held by
2326 * other threads (which did not get cloned, and hence will never release
2327 * those locks). This is okay so long as we call exec() immediately
2328 * after, but a bunch of seemingly-innocent libc functions like setenv()
2329 * take locks.
2330 *
2331 * Hence, only call sys_clone() if we need to, in order to get at pid
2332 * namespacing. If we follow this path, the child's address space might
2333 * have broken locks; you may only call functions that do not acquire
2334 * any locks.
2335 *
2336 * Unfortunately, fork() acquires every lock it can get its hands on, as
2337 * previously detailed, so this function is highly likely to deadlock
2338 * later on (see "deadlock here") if we're multithreaded.
2339 *
2340 * We might hack around this by having the clone()d child (init of the
2341 * pid namespace) return directly, rather than leaving the clone()d
2342 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002343 * its fork()ed child return in turn), but that process would be
2344 * crippled with its libc locks potentially broken. We might try
2345 * fork()ing in the parent before we clone() to ensure that we own all
2346 * the locks, but then we have to have the forked child hanging around
2347 * consuming resources (and possibly having file descriptors / shared
2348 * memory regions / etc attached). We'd need to keep the child around to
2349 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002350 *
2351 * TODO(ellyjones): figure out if the "forked child hanging around"
2352 * problem is fixable or not. It would be nice if we worked in this
2353 * case.
2354 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002355 if (pid_namespace) {
2356 int clone_flags = CLONE_NEWPID | SIGCHLD;
2357 if (j->flags.userns)
2358 clone_flags |= CLONE_NEWUSER;
2359 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002360 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002361 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002362 }
Elly Jones761b7412012-06-13 15:49:52 -04002363
Elly Jonese1749eb2011-10-07 13:54:59 -04002364 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002365 if (use_preload) {
2366 free(oldenv_copy);
2367 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002368 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002369 }
Will Drewryf89aef52011-09-16 16:48:57 -05002370
Elly Jonese1749eb2011-10-07 13:54:59 -04002371 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002372 if (use_preload) {
2373 /* Restore parent's LD_PRELOAD. */
2374 if (oldenv_copy) {
2375 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2376 free(oldenv_copy);
2377 } else {
2378 unsetenv(kLdPreloadEnvVar);
2379 }
2380 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002381 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002382
Elly Jonese1749eb2011-10-07 13:54:59 -04002383 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002384
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002385 if (j->flags.forward_signals) {
2386 forward_pid = child_pid;
2387 install_signal_handlers();
2388 }
2389
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002390 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002391 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002392
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002393 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002394 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002395
Dylan Reid0f72ef42017-06-06 15:42:49 -07002396 if (j->rlimit_count)
2397 set_rlimits_or_die(j);
2398
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002399 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002400 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002401
2402 if (sync_child)
2403 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002404
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002405 if (use_preload) {
2406 /* Send marshalled minijail. */
2407 close(pipe_fds[0]); /* read endpoint */
2408 ret = minijail_to_fd(j, pipe_fds[1]);
2409 close(pipe_fds[1]); /* write endpoint */
2410 if (ret) {
2411 kill(j->initpid, SIGKILL);
2412 die("failed to send marshalled minijail");
2413 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002414 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002415
Dylan Reidacfb8be2017-08-25 12:56:51 -07002416 if (status_out->pchild_pid)
2417 *status_out->pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002418
2419 /*
2420 * If we want to write to the child process' standard input,
2421 * set up the write end of the pipe.
2422 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002423 if (status_out->pstdin_fd)
2424 *status_out->pstdin_fd =
2425 setup_pipe_end(stdin_fds, 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002426
2427 /*
2428 * If we want to read from the child process' standard output,
2429 * set up the read end of the pipe.
2430 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002431 if (status_out->pstdout_fd)
2432 *status_out->pstdout_fd =
2433 setup_pipe_end(stdout_fds, 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002434
2435 /*
2436 * If we want to read from the child process' standard error,
2437 * set up the read end of the pipe.
2438 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002439 if (status_out->pstderr_fd)
2440 *status_out->pstderr_fd =
2441 setup_pipe_end(stderr_fds, 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002442
Elly Jonese1749eb2011-10-07 13:54:59 -04002443 return 0;
2444 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002445 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002446 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002447
Peter Qiu2860c462015-12-16 15:13:06 -08002448 if (j->flags.reset_signal_mask) {
2449 sigset_t signal_mask;
2450 if (sigemptyset(&signal_mask) != 0)
2451 pdie("sigemptyset failed");
2452 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2453 pdie("sigprocmask failed");
2454 }
2455
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002456 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002457 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002458 int inheritable_fds[kMaxInheritableFdsSize];
2459 size_t size = 0;
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002460 size_t i;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002461 if (use_preload) {
2462 inheritable_fds[size++] = pipe_fds[0];
2463 inheritable_fds[size++] = pipe_fds[1];
2464 }
2465 if (sync_child) {
2466 inheritable_fds[size++] = child_sync_pipe_fds[0];
2467 inheritable_fds[size++] = child_sync_pipe_fds[1];
2468 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002469 if (status_out->pstdin_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002470 inheritable_fds[size++] = stdin_fds[0];
2471 inheritable_fds[size++] = stdin_fds[1];
2472 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002473 if (status_out->pstdout_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002474 inheritable_fds[size++] = stdout_fds[0];
2475 inheritable_fds[size++] = stdout_fds[1];
2476 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002477 if (status_out->pstderr_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002478 inheritable_fds[size++] = stderr_fds[0];
2479 inheritable_fds[size++] = stderr_fds[1];
2480 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002481 for (i = 0; i < j->preserved_fd_count; i++) {
2482 /*
2483 * Preserve all parent_fds. They will be dup2(2)-ed in
2484 * the child later.
2485 */
2486 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
2487 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002488
2489 if (close_open_fds(inheritable_fds, size) < 0)
2490 die("failed to close open file descriptors");
2491 }
2492
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002493 if (redirect_fds(j))
2494 die("failed to set up fd redirections");
2495
Dylan Reidce5b55e2016-01-13 11:04:16 -08002496 if (sync_child)
2497 wait_for_parent_setup(child_sync_pipe_fds);
2498
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002499 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002500 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002501
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002502 /*
2503 * If we want to write to the jailed process' standard input,
2504 * set up the read end of the pipe.
2505 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002506 if (status_out->pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002507 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2508 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002509 die("failed to set up stdin pipe");
2510 }
2511
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002512 /*
2513 * If we want to read from the jailed process' standard output,
2514 * set up the write end of the pipe.
2515 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002516 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002517 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2518 STDOUT_FILENO) < 0)
2519 die("failed to set up stdout pipe");
2520 }
2521
2522 /*
2523 * If we want to read from the jailed process' standard error,
2524 * set up the write end of the pipe.
2525 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002526 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002527 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2528 STDERR_FILENO) < 0)
2529 die("failed to set up stderr pipe");
2530 }
2531
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002532 /*
2533 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2534 * This prevents the jailed process from using the TIOCSTI ioctl
2535 * to push characters into the parent process terminal's input buffer,
2536 * therefore escaping the jail.
2537 */
2538 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2539 isatty(STDERR_FILENO)) {
2540 if (setsid() < 0) {
2541 pdie("setsid() failed");
2542 }
2543 }
2544
Dylan Reid791f5772015-09-14 20:02:42 -07002545 /* If running an init program, let it decide when/how to mount /proc. */
2546 if (pid_namespace && !do_init)
2547 j->flags.remount_proc_ro = 0;
2548
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002549 if (use_preload) {
2550 /* Strip out flags that cannot be inherited across execve(2). */
2551 minijail_preexec(j);
2552 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002553 /*
2554 * If not using LD_PRELOAD, do all jailing before execve(2).
2555 * Note that PID namespaces can only be entered on fork(2),
2556 * so that flag is still cleared.
2557 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002558 j->flags.pids = 0;
2559 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002560 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002561 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002562
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002563 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002564 /*
2565 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002566 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002567 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002568 * a child to actually run the program. If |do_init == 0|, we
2569 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002570 *
2571 * If we're multithreaded, we'll probably deadlock here. See
2572 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002573 */
2574 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002575 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002576 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002577 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002578 /*
2579 * Best effort. Don't bother checking the return value.
2580 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002581 prctl(PR_SET_NAME, "minijail-init");
2582 init(child_pid); /* Never returns. */
2583 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002584 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002585
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002586 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
2587
Elly Jonesdd3e8512012-01-23 15:13:38 -05002588 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002589 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002590 * calling process
2591 * -> execve()-ing process
2592 * If we are:
2593 * calling process
2594 * -> init()-ing process
2595 * -> execve()-ing process
2596 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002597 ret = execve(config->filename, config->argv, environ);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002598 if (ret == -1) {
Dylan Reidacfb8be2017-08-25 12:56:51 -07002599 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002600 }
2601 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002602}
2603
Will Drewry6ac91122011-10-21 16:38:58 -05002604int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002605{
2606 int st;
2607 if (kill(j->initpid, SIGTERM))
2608 return -errno;
2609 if (waitpid(j->initpid, &st, 0) < 0)
2610 return -errno;
2611 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002612}
2613
Will Drewry6ac91122011-10-21 16:38:58 -05002614int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002615{
2616 int st;
2617 if (waitpid(j->initpid, &st, 0) < 0)
2618 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002619
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002620 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002621 int error_status = st;
2622 if (WIFSIGNALED(st)) {
2623 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002624 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002625 j->initpid, signum);
2626 /*
2627 * We return MINIJAIL_ERR_JAIL if the process received
2628 * SIGSYS, which happens when a syscall is blocked by
2629 * seccomp filters.
2630 * If not, we do what bash(1) does:
2631 * $? = 128 + signum
2632 */
2633 if (signum == SIGSYS) {
2634 error_status = MINIJAIL_ERR_JAIL;
2635 } else {
2636 error_status = 128 + signum;
2637 }
2638 }
2639 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002640 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002641
2642 int exit_status = WEXITSTATUS(st);
2643 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002644 info("child process %d exited with status %d",
2645 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002646
2647 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002648}
2649
Will Drewry6ac91122011-10-21 16:38:58 -05002650void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002651{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002652 size_t i;
2653
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002654 if (j->flags.seccomp_filter && j->filter_prog) {
2655 free(j->filter_prog->filter);
2656 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002657 }
Dylan Reid648b2202015-10-23 00:50:00 -07002658 while (j->mounts_head) {
2659 struct mountpoint *m = j->mounts_head;
2660 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002661 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002662 free(m->type);
2663 free(m->dest);
2664 free(m->src);
2665 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002666 }
Dylan Reid648b2202015-10-23 00:50:00 -07002667 j->mounts_tail = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002668 while (j->hooks_head) {
2669 struct hook *c = j->hooks_head;
2670 j->hooks_head = c->next;
2671 free(c);
2672 }
2673 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002674 if (j->user)
2675 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002676 if (j->suppl_gid_list)
2677 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002678 if (j->chrootdir)
2679 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002680 if (j->pid_file_path)
2681 free(j->pid_file_path);
2682 if (j->uidmap)
2683 free(j->uidmap);
2684 if (j->gidmap)
2685 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002686 if (j->hostname)
2687 free(j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08002688 if (j->alt_syscall_table)
2689 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002690 for (i = 0; i < j->cgroup_count; ++i)
2691 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002692 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002693}