blob: 6de6b6ac2727907cb5139e3e138d7fec12b4d5cf [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
7#define _GNU_SOURCE
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07008
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08009#include <asm/unistd.h>
Will Drewry32ac9f52011-08-18 21:36:27 -050010#include <ctype.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040011#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070012#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040013#include <grp.h>
14#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050015#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040016#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040017#include <pwd.h>
18#include <sched.h>
19#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050020#include <stdarg.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070021#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080022#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <syscall.h>
27#include <sys/capability.h>
28#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050029#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040030#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070031#include <sys/stat.h>
32#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080033#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040034#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040035#include <unistd.h>
36
37#include "libminijail.h"
38#include "libminijail-private.h"
39
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070040#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080041#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070042#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080043
Lei Zhangeee31552012-10-17 21:27:10 -070044#ifdef HAVE_SECUREBITS_H
45#include <linux/securebits.h>
46#else
47#define SECURE_ALL_BITS 0x15
48#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
49#endif
50
Will Drewry32ac9f52011-08-18 21:36:27 -050051/* Until these are reliably available in linux/prctl.h */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080052#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070053# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080054#endif
55
Andrew Brestickereac28942015-11-11 16:04:46 -080056#ifndef PR_ALT_SYSCALL
57# define PR_ALT_SYSCALL 0x43724f53
58#endif
59
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080060/* For seccomp_filter using BPF. */
61#ifndef PR_SET_NO_NEW_PRIVS
62# define PR_SET_NO_NEW_PRIVS 38
63#endif
64#ifndef SECCOMP_MODE_FILTER
65# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050066#endif
67
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070068#ifdef USE_SECCOMP_SOFTFAIL
69# define SECCOMP_SOFTFAIL 1
70#else
71# define SECCOMP_SOFTFAIL 0
72#endif
73
Dylan Reid648b2202015-10-23 00:50:00 -070074struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040075 char *src;
76 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070077 char *type;
78 unsigned long flags;
79 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040080};
81
Will Drewryf89aef52011-09-16 16:48:57 -050082struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070083 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070084 * WARNING: if you add a flag here you need to make sure it's
85 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070086 */
Elly Jonese1749eb2011-10-07 13:54:59 -040087 struct {
88 int uid:1;
89 int gid:1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -080090 int usergroups:1;
91 int suppl_gids:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040092 int caps:1;
93 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070094 int enter_vfs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040095 int pids:1;
Dylan Reidf7942472015-11-18 17:55:26 -080096 int ipc:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -040097 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -070098 int enter_net:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +080099 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400100 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -0700101 int remount_proc_ro:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700102 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400103 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700104 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400105 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800106 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700107 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800108 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800109 int pid_file:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800110 int alt_syscall:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400111 } flags;
112 uid_t uid;
113 gid_t gid;
114 gid_t usergid;
115 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800116 size_t suppl_gid_count;
117 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400118 uint64_t caps;
119 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700120 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700121 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400122 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800123 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800124 char *uidmap;
125 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800126 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800127 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800128 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700129 struct mountpoint *mounts_head;
130 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800131 size_t mounts_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500132};
133
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700134/*
135 * Strip out flags meant for the parent.
136 * We keep things that are not inherited across execve(2) (e.g. capabilities),
137 * or are easier to set after execve(2) (e.g. seccomp filters).
138 */
139void minijail_preenter(struct minijail *j)
140{
141 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700142 j->flags.enter_vfs = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700143 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700144 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800145 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800146 j->flags.pid_file = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700147}
148
149/*
150 * Strip out flags meant for the child.
151 * We keep things that are inherited across execve(2).
152 */
153void minijail_preexec(struct minijail *j)
154{
155 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700156 int enter_vfs = j->flags.enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700157 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800158 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700159 if (j->user)
160 free(j->user);
161 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800162 if (j->suppl_gid_list)
163 free(j->suppl_gid_list);
164 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700165 memset(&j->flags, 0, sizeof(j->flags));
166 /* Now restore anything we meant to keep. */
167 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700168 j->flags.enter_vfs = enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700169 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800170 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700171 /* Note, |pids| will already have been used before this call. */
172}
173
174/* Minijail API. */
175
Will Drewry6ac91122011-10-21 16:38:58 -0500176struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400177{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400178 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400179}
180
Will Drewry6ac91122011-10-21 16:38:58 -0500181void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400182{
183 if (uid == 0)
184 die("useless change to uid 0");
185 j->uid = uid;
186 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400187}
188
Will Drewry6ac91122011-10-21 16:38:58 -0500189void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400190{
191 if (gid == 0)
192 die("useless change to gid 0");
193 j->gid = gid;
194 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400195}
196
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800197int API minijail_set_supplementary_gids(struct minijail *j, size_t size,
198 const gid_t *list)
199{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800200 size_t i;
201
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800202 if (j->flags.usergroups)
203 die("cannot inherit *and* set supplementary groups");
204
205 if (size == 0)
206 return -EINVAL;
207
208 /* Copy the gid_t array. */
209 j->suppl_gid_list = calloc(size, sizeof(gid_t));
210 if (!j->suppl_gid_list) {
211 return -ENOMEM;
212 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800213 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800214 j->suppl_gid_list[i] = list[i];
215 }
216 j->suppl_gid_count = size;
217 j->flags.suppl_gids = 1;
218 return 0;
219}
220
Will Drewry6ac91122011-10-21 16:38:58 -0500221int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400222{
223 char *buf = NULL;
224 struct passwd pw;
225 struct passwd *ppw = NULL;
226 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
227 if (sz == -1)
228 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400229
Elly Jonesdd3e8512012-01-23 15:13:38 -0500230 /*
231 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400232 * the maximum needed size of the buffer, so we don't have to search.
233 */
234 buf = malloc(sz);
235 if (!buf)
236 return -ENOMEM;
237 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500238 /*
239 * We're safe to free the buffer here. The strings inside pw point
240 * inside buf, but we don't use any of them; this leaves the pointers
241 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
242 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400243 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700244 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400245 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700246 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400247 minijail_change_uid(j, ppw->pw_uid);
248 j->user = strdup(user);
249 if (!j->user)
250 return -ENOMEM;
251 j->usergid = ppw->pw_gid;
252 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400253}
254
Will Drewry6ac91122011-10-21 16:38:58 -0500255int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400256{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700257 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700258 struct group gr;
259 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400260 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
261 if (sz == -1)
262 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400263
Elly Jonesdd3e8512012-01-23 15:13:38 -0500264 /*
265 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400266 * the maximum needed size of the buffer, so we don't have to search.
267 */
268 buf = malloc(sz);
269 if (!buf)
270 return -ENOMEM;
271 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500272 /*
273 * We're safe to free the buffer here. The strings inside gr point
274 * inside buf, but we don't use any of them; this leaves the pointers
275 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
276 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400277 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700278 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400279 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700280 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400281 minijail_change_gid(j, pgr->gr_gid);
282 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400283}
284
Will Drewry6ac91122011-10-21 16:38:58 -0500285void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400286{
287 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400288}
289
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700290void API minijail_no_new_privs(struct minijail *j)
291{
292 j->flags.no_new_privs = 1;
293}
294
Will Drewry6ac91122011-10-21 16:38:58 -0500295void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400296{
297 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500298}
299
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700300void API minijail_log_seccomp_filter_failures(struct minijail *j)
301{
302 j->flags.log_seccomp_filter = 1;
303}
304
Will Drewry6ac91122011-10-21 16:38:58 -0500305void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400306{
307 j->caps = capmask;
308 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400309}
310
Will Drewry6ac91122011-10-21 16:38:58 -0500311void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400312{
313 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400314}
315
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700316void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
317{
318 int ns_fd = open(ns_path, O_RDONLY);
319 if (ns_fd < 0) {
320 pdie("failed to open namespace '%s'", ns_path);
321 }
322 j->mountns_fd = ns_fd;
323 j->flags.enter_vfs = 1;
324}
325
Will Drewry6ac91122011-10-21 16:38:58 -0500326void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400327{
Elly Jonese58176c2012-01-23 11:46:17 -0500328 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700329 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400330 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800331 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400332}
333
Dylan Reidf7942472015-11-18 17:55:26 -0800334void API minijail_namespace_ipc(struct minijail *j)
335{
336 j->flags.ipc = 1;
337}
338
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400339void API minijail_namespace_net(struct minijail *j)
340{
341 j->flags.net = 1;
342}
343
Dylan Reid1102f5a2015-09-15 11:52:20 -0700344void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
345{
346 int ns_fd = open(ns_path, O_RDONLY);
347 if (ns_fd < 0) {
348 pdie("failed to open namespace '%s'", ns_path);
349 }
350 j->netns_fd = ns_fd;
351 j->flags.enter_net = 1;
352}
353
Dylan Reid791f5772015-09-14 20:02:42 -0700354void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400355{
356 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700357 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400358}
359
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800360void API minijail_namespace_user(struct minijail *j)
361{
362 j->flags.userns = 1;
363}
364
365int API minijail_uidmap(struct minijail *j, const char *uidmap)
366{
367 j->uidmap = strdup(uidmap);
368 if (!j->uidmap)
369 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800370 char *ch;
371 for (ch = j->uidmap; *ch; ch++) {
372 if (*ch == ',')
373 *ch = '\n';
374 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800375 return 0;
376}
377
378int API minijail_gidmap(struct minijail *j, const char *gidmap)
379{
380 j->gidmap = strdup(gidmap);
381 if (!j->gidmap)
382 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800383 char *ch;
384 for (ch = j->gidmap; *ch; ch++) {
385 if (*ch == ',')
386 *ch = '\n';
387 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800388 return 0;
389}
390
Will Drewry6ac91122011-10-21 16:38:58 -0500391void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400392{
393 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400394}
395
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800396void API minijail_run_as_init(struct minijail *j)
397{
398 /*
399 * Since the jailed program will become 'init' in the new PID namespace,
400 * Minijail does not need to fork an 'init' process.
401 */
402 j->flags.do_init = 0;
403}
404
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700405int API minijail_enter_chroot(struct minijail *j, const char *dir)
406{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400407 if (j->chrootdir)
408 return -EINVAL;
409 j->chrootdir = strdup(dir);
410 if (!j->chrootdir)
411 return -ENOMEM;
412 j->flags.chroot = 1;
413 return 0;
414}
415
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800416int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
417{
418 if (j->chrootdir)
419 return -EINVAL;
420 j->chrootdir = strdup(dir);
421 if (!j->chrootdir)
422 return -ENOMEM;
423 j->flags.pivot_root = 1;
424 return 0;
425}
426
Dylan Reida14e08d2015-10-22 21:05:29 -0700427static char *append_external_path(const char *external_path,
428 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700429{
Dylan Reida14e08d2015-10-22 21:05:29 -0700430 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700431 size_t pathlen;
432
Dylan Reid08946cc2015-09-16 19:10:57 -0700433 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700434 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
435 path = malloc(pathlen);
436 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700437
Dylan Reida14e08d2015-10-22 21:05:29 -0700438 return path;
439}
440
441char API *minijail_get_original_path(struct minijail *j,
442 const char *path_inside_chroot)
443{
Dylan Reid648b2202015-10-23 00:50:00 -0700444 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700445
Dylan Reid648b2202015-10-23 00:50:00 -0700446 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700447 while (b) {
448 /*
449 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700450 * mount, then the original path is exactly the source of
451 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700452 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700453 * mount source = /some/path/exe, mount dest =
454 * /chroot/path/exe Then when getting the original path of
455 * "/chroot/path/exe", the source of that mount,
456 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700457 */
458 if (!strcmp(b->dest, path_inside_chroot))
459 return strdup(b->src);
460
461 /*
462 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700463 * mount, take the suffix of the chroot path relative to the
464 * mount destination path, and append it to the mount source
465 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700466 */
467 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
468 const char *relative_path =
469 path_inside_chroot + strlen(b->dest);
470 return append_external_path(b->src, relative_path);
471 }
472 b = b->next;
473 }
474
475 /* If there is a chroot path, append |path_inside_chroot| to that. */
476 if (j->chrootdir)
477 return append_external_path(j->chrootdir, path_inside_chroot);
478
479 /* No chroot, so the path outside is the same as it is inside. */
480 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700481}
482
Lee Campbell11af0622014-05-22 12:36:04 -0700483void API minijail_mount_tmp(struct minijail *j)
484{
485 j->flags.mount_tmp = 1;
486}
487
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800488int API minijail_write_pid_file(struct minijail *j, const char *path)
489{
490 j->pid_file_path = strdup(path);
491 if (!j->pid_file_path)
492 return -ENOMEM;
493 j->flags.pid_file = 1;
494 return 0;
495}
496
Dylan Reid648b2202015-10-23 00:50:00 -0700497int API minijail_mount(struct minijail *j, const char *src, const char *dest,
498 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700499{
Dylan Reid648b2202015-10-23 00:50:00 -0700500 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400501
502 if (*dest != '/')
503 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700504 m = calloc(1, sizeof(*m));
505 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400506 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700507 m->dest = strdup(dest);
508 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400509 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700510 m->src = strdup(src);
511 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400512 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700513 m->type = strdup(type);
514 if (!m->type)
515 goto error;
516 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400517
Dylan Reid648b2202015-10-23 00:50:00 -0700518 info("mount %s -> %s type %s", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400519
Elly Jonesdd3e8512012-01-23 15:13:38 -0500520 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700521 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400522 * containing vfs namespace.
523 */
524 minijail_namespace_vfs(j);
525
Dylan Reid648b2202015-10-23 00:50:00 -0700526 if (j->mounts_tail)
527 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400528 else
Dylan Reid648b2202015-10-23 00:50:00 -0700529 j->mounts_head = m;
530 j->mounts_tail = m;
531 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400532
533 return 0;
534
535error:
Dylan Reid648b2202015-10-23 00:50:00 -0700536 free(m->src);
537 free(m->dest);
538 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400539 return -ENOMEM;
540}
541
Dylan Reid648b2202015-10-23 00:50:00 -0700542int API minijail_bind(struct minijail *j, const char *src, const char *dest,
543 int writeable)
544{
545 unsigned long flags = MS_BIND;
546
547 if (!writeable)
548 flags |= MS_RDONLY;
549
550 return minijail_mount(j, src, dest, "", flags);
551}
552
Will Drewry6ac91122011-10-21 16:38:58 -0500553void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400554{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700555 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
556 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800557 warn("not loading seccomp filter,"
558 " seccomp not supported");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700559 return;
560 }
561 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400562 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800563 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700564 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400565 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800566
567 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700568 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
569 die("failed to compile seccomp filter BPF program in '%s'",
570 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800571 }
572
573 j->filter_len = fprog->len;
574 j->filter_prog = fprog;
575
Elly Jonese1749eb2011-10-07 13:54:59 -0400576 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500577}
578
Andrew Brestickereac28942015-11-11 16:04:46 -0800579int API minijail_use_alt_syscall(struct minijail *j, const char *table)
580{
581 j->alt_syscall_table = strdup(table);
582 if (!j->alt_syscall_table)
583 return -ENOMEM;
584 j->flags.alt_syscall = 1;
585 return 0;
586}
587
Will Drewryf89aef52011-09-16 16:48:57 -0500588struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400589 size_t available;
590 size_t total;
591 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500592};
593
Will Drewry6ac91122011-10-21 16:38:58 -0500594void marshal_state_init(struct marshal_state *state,
595 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400596{
597 state->available = available;
598 state->buf = buf;
599 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500600}
601
Will Drewry6ac91122011-10-21 16:38:58 -0500602void marshal_append(struct marshal_state *state,
603 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400604{
605 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500606
Elly Jonese1749eb2011-10-07 13:54:59 -0400607 /* Up to |available| will be written. */
608 if (copy_len) {
609 memcpy(state->buf, src, copy_len);
610 state->buf += copy_len;
611 state->available -= copy_len;
612 }
613 /* |total| will contain the expected length. */
614 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500615}
616
Will Drewry6ac91122011-10-21 16:38:58 -0500617void minijail_marshal_helper(struct marshal_state *state,
618 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400619{
Dylan Reid648b2202015-10-23 00:50:00 -0700620 struct mountpoint *m = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400621 marshal_append(state, (char *)j, sizeof(*j));
622 if (j->user)
623 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400624 if (j->chrootdir)
625 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800626 if (j->alt_syscall_table) {
627 marshal_append(state, j->alt_syscall_table,
628 strlen(j->alt_syscall_table) + 1);
629 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800630 if (j->flags.seccomp_filter && j->filter_prog) {
631 struct sock_fprog *fp = j->filter_prog;
632 marshal_append(state, (char *)fp->filter,
633 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400634 }
Dylan Reid648b2202015-10-23 00:50:00 -0700635 for (m = j->mounts_head; m; m = m->next) {
636 marshal_append(state, m->src, strlen(m->src) + 1);
637 marshal_append(state, m->dest, strlen(m->dest) + 1);
638 marshal_append(state, m->type, strlen(m->type) + 1);
639 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400640 }
Will Drewryf89aef52011-09-16 16:48:57 -0500641}
642
Will Drewry6ac91122011-10-21 16:38:58 -0500643size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400644{
645 struct marshal_state state;
646 marshal_state_init(&state, NULL, 0);
647 minijail_marshal_helper(&state, j);
648 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500649}
650
Elly Jonese1749eb2011-10-07 13:54:59 -0400651int minijail_marshal(const struct minijail *j, char *buf, size_t available)
652{
653 struct marshal_state state;
654 marshal_state_init(&state, buf, available);
655 minijail_marshal_helper(&state, j);
656 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500657}
658
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800659/*
660 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
Elly Jones51a5b6c2011-10-12 19:09:26 -0400661 * @length Number of bytes to consume
662 * @buf Buffer to consume from
663 * @buflength Size of @buf
664 *
665 * Returns a pointer to the base of the bytes, or NULL for errors.
666 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700667void *consumebytes(size_t length, char **buf, size_t *buflength)
668{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400669 char *p = *buf;
670 if (length > *buflength)
671 return NULL;
672 *buf += length;
673 *buflength -= length;
674 return p;
675}
676
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800677/*
678 * consumestr: consumes a C string from a buffer @buf of length @length
Elly Jones51a5b6c2011-10-12 19:09:26 -0400679 * @buf Buffer to consume
680 * @length Length of buffer
681 *
682 * Returns a pointer to the base of the string, or NULL for errors.
683 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700684char *consumestr(char **buf, size_t *buflength)
685{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400686 size_t len = strnlen(*buf, *buflength);
687 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700688 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400689 return NULL;
690 return consumebytes(len + 1, buf, buflength);
691}
692
Elly Jonese1749eb2011-10-07 13:54:59 -0400693int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
694{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800695 size_t i;
696 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500697 int ret = -EINVAL;
698
Elly Jonese1749eb2011-10-07 13:54:59 -0400699 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500700 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400701 memcpy((void *)j, serialized, sizeof(*j));
702 serialized += sizeof(*j);
703 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500704
Will Drewrybee7ba72011-10-21 20:47:01 -0500705 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700706 j->mounts_head = NULL;
707 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800708 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500709
Elly Jonese1749eb2011-10-07 13:54:59 -0400710 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400711 char *user = consumestr(&serialized, &length);
712 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500713 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400714 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500715 if (!j->user)
716 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400717 }
Will Drewryf89aef52011-09-16 16:48:57 -0500718
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400719 if (j->chrootdir) { /* stale pointer */
720 char *chrootdir = consumestr(&serialized, &length);
721 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500722 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400723 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500724 if (!j->chrootdir)
725 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400726 }
727
Andrew Brestickereac28942015-11-11 16:04:46 -0800728 if (j->alt_syscall_table) { /* stale pointer */
729 char *alt_syscall_table = consumestr(&serialized, &length);
730 if (!alt_syscall_table)
731 goto bad_syscall_table;
732 j->alt_syscall_table = strdup(alt_syscall_table);
733 if (!j->alt_syscall_table)
734 goto bad_syscall_table;
735 }
736
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800737 if (j->flags.seccomp_filter && j->filter_len > 0) {
738 size_t ninstrs = j->filter_len;
739 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
740 ninstrs > USHRT_MAX)
741 goto bad_filters;
742
743 size_t program_len = ninstrs * sizeof(struct sock_filter);
744 void *program = consumebytes(program_len, &serialized, &length);
745 if (!program)
746 goto bad_filters;
747
748 j->filter_prog = malloc(sizeof(struct sock_fprog));
749 j->filter_prog->len = ninstrs;
750 j->filter_prog->filter = malloc(program_len);
751 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400752 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400753
Dylan Reid648b2202015-10-23 00:50:00 -0700754 count = j->mounts_count;
755 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400756 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700757 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400758 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700759 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400760 const char *src = consumestr(&serialized, &length);
761 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700762 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400763 dest = consumestr(&serialized, &length);
764 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700765 goto bad_mounts;
766 type = consumestr(&serialized, &length);
767 if (!type)
768 goto bad_mounts;
769 flags = consumebytes(sizeof(*flags), &serialized, &length);
770 if (!flags)
771 goto bad_mounts;
772 if (minijail_mount(j, src, dest, type, *flags))
773 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400774 }
775
Elly Jonese1749eb2011-10-07 13:54:59 -0400776 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500777
Dylan Reid648b2202015-10-23 00:50:00 -0700778bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800779 if (j->flags.seccomp_filter && j->filter_len > 0) {
780 free(j->filter_prog->filter);
781 free(j->filter_prog);
782 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500783bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800784 if (j->alt_syscall_table)
785 free(j->alt_syscall_table);
786bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500787 if (j->chrootdir)
788 free(j->chrootdir);
789bad_chrootdir:
790 if (j->user)
791 free(j->user);
792clear_pointers:
793 j->user = NULL;
794 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800795 j->alt_syscall_table = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500796out:
797 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500798}
799
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800800static void write_ugid_mappings(const struct minijail *j, int *pipe_fds)
801{
802 int fd, ret, len;
803 size_t sz;
804 char fname[32];
805 close(pipe_fds[0]);
806
807 sz = sizeof(fname);
808 if (j->uidmap) {
809 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700810 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800811 die("failed to write file name of uid_map");
812 fd = open(fname, O_WRONLY);
813 if (fd < 0)
814 pdie("failed to open '%s'", fname);
815 len = strlen(j->uidmap);
816 if (write(fd, j->uidmap, len) < len)
817 die("failed to set uid_map");
818 close(fd);
819 }
820 if (j->gidmap) {
821 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700822 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800823 die("failed to write file name of gid_map");
824 fd = open(fname, O_WRONLY);
825 if (fd < 0)
826 pdie("failed to open '%s'", fname);
827 len = strlen(j->gidmap);
828 if (write(fd, j->gidmap, len) < len)
829 die("failed to set gid_map");
830 close(fd);
831 }
832
833 close(pipe_fds[1]);
834}
835
836static void enter_user_namespace(const struct minijail *j, int *pipe_fds)
837{
838 char buf;
839
840 close(pipe_fds[1]);
841
842 /* Wait for parent to set up uid/gid mappings. */
843 if (read(pipe_fds[0], &buf, 1) != 0)
844 die("failed to sync with parent");
845 close(pipe_fds[0]);
846
847 if (j->uidmap && setresuid(0, 0, 0))
848 pdie("setresuid");
849 if (j->gidmap && setresgid(0, 0, 0))
850 pdie("setresgid");
851}
852
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800853/*
854 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -0700855 * @j Minijail these mounts are for
856 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -0400857 *
858 * Returns 0 for success.
859 */
Dylan Reid648b2202015-10-23 00:50:00 -0700860static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700861{
Dylan Reid648b2202015-10-23 00:50:00 -0700862 int ret;
863 char *dest;
864 int remount_ro = 0;
865
Elly Jones51a5b6c2011-10-12 19:09:26 -0400866 /* dest has a leading "/" */
Dylan Reid648b2202015-10-23 00:50:00 -0700867 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400868 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700869
870 /*
871 * R/O bind mounts have to be remounted since bind and ro can't both be
872 * specified in the original bind mount. Remount R/O after the initial
873 * mount.
874 */
875 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
876 remount_ro = 1;
877 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -0500878 }
Dylan Reid648b2202015-10-23 00:50:00 -0700879
880 ret = mount(m->src, dest, m->type, m->flags, NULL);
881 if (ret)
882 pdie("mount: %s -> %s", m->src, dest);
883
884 if (remount_ro) {
885 m->flags |= MS_RDONLY;
886 ret = mount(m->src, dest, NULL,
887 m->flags | MS_REMOUNT, NULL);
888 if (ret)
889 pdie("bind ro: %s -> %s", m->src, dest);
890 }
891
Elly Jones51a5b6c2011-10-12 19:09:26 -0400892 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -0700893 if (m->next)
894 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400895 return ret;
896}
897
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700898int enter_chroot(const struct minijail *j)
899{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400900 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -0700901
902 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -0400903 return ret;
904
905 if (chroot(j->chrootdir))
906 return -errno;
907
908 if (chdir("/"))
909 return -errno;
910
911 return 0;
912}
913
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800914int enter_pivot_root(const struct minijail *j)
915{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800916 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -0700917
918 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800919 return ret;
920
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800921 /*
922 * Keep the fd for both old and new root.
923 * It will be used in fchdir later.
924 */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800925 oldroot = open("/", O_DIRECTORY | O_RDONLY);
926 if (oldroot < 0)
927 pdie("failed to open / for fchdir");
928 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY);
929 if (newroot < 0)
930 pdie("failed to open %s for fchdir", j->chrootdir);
931
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800932 /*
933 * To ensure chrootdir is the root of a file system,
934 * do a self bind mount.
935 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800936 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
937 pdie("failed to bind mount '%s'", j->chrootdir);
938 if (chdir(j->chrootdir))
939 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800940 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800941 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800942
943 /*
944 * Now the old root is mounted on top of the new root. Use fchdir to
945 * change to the old root and unmount it.
946 */
947 if (fchdir(oldroot))
948 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800949 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800950 if (umount2(".", MNT_DETACH))
951 pdie("umount(/)");
952 /* Change back to the new root. */
953 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800954 return -errno;
955 if (chroot("/"))
956 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -0700957 /* Set correct CWD for getcwd(3). */
958 if (chdir("/"))
959 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800960
961 return 0;
962}
963
Lee Campbell11af0622014-05-22 12:36:04 -0700964int mount_tmp(void)
965{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800966 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -0700967}
968
Dylan Reid791f5772015-09-14 20:02:42 -0700969int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400970{
971 const char *kProcPath = "/proc";
972 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500973 /*
974 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400975 * /proc in our namespace, which means using MS_REMOUNT here would
976 * mutate our parent's mount as well, even though we're in a VFS
977 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800978 * and make our own. However, if we are in a new user namespace, /proc
979 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -0400980 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -0700981 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -0400982 return -errno;
983 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
984 return -errno;
985 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400986}
987
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800988static void write_pid_file(const struct minijail *j)
989{
990 FILE *fp = fopen(j->pid_file_path, "w");
991
992 if (!fp)
993 pdie("failed to open '%s'", j->pid_file_path);
994 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
995 pdie("fprintf(%s)", j->pid_file_path);
996 if (fclose(fp))
997 pdie("fclose(%s)", j->pid_file_path);
998}
999
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001000void drop_ugid(const struct minijail *j)
1001{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001002 if (j->flags.usergroups && j->flags.suppl_gids) {
1003 die("tried to inherit *and* set supplementary groups;"
1004 " can only do one");
1005 }
1006
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001007 if (j->flags.usergroups) {
1008 if (initgroups(j->user, j->usergid))
1009 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001010 } else if (j->flags.suppl_gids) {
1011 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1012 pdie("setgroups");
1013 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001014 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001015 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001016 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001017 * users.
1018 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001019 if ((j->uid || j->gid) && setgroups(0, NULL))
1020 pdie("setgroups");
1021 }
1022
1023 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1024 pdie("setresgid");
1025
1026 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1027 pdie("setresuid");
1028}
1029
Mike Frysinger3adfef72013-05-09 17:19:08 -04001030/*
1031 * We specifically do not use cap_valid() as that only tells us the last
1032 * valid cap we were *compiled* against (i.e. what the version of kernel
1033 * headers says). If we run on a different kernel version, then it's not
1034 * uncommon for that to be less (if an older kernel) or more (if a newer
1035 * kernel). So suck up the answer via /proc.
1036 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001037static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001038{
Dylan Reidf682d472015-09-17 21:39:07 -07001039 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1040 FILE *fp = fopen(cap_file, "re");
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001041 unsigned int last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001042
Dylan Reidf682d472015-09-17 21:39:07 -07001043 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1044 pdie("fscanf(%s)", cap_file);
1045 fclose(fp);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001046
Dylan Reidf682d472015-09-17 21:39:07 -07001047 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001048}
1049
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001050void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001051{
1052 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001053 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001054 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001055 unsigned int i;
1056 if (!caps)
1057 die("can't get process caps");
1058 if (cap_clear_flag(caps, CAP_INHERITABLE))
1059 die("can't clear inheritable caps");
1060 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1061 die("can't clear effective caps");
1062 if (cap_clear_flag(caps, CAP_PERMITTED))
1063 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001064 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001065 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001066 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001067 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001068 flag[0] = i;
1069 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001070 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001071 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001072 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001073 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001074 die("can't add inheritable cap");
1075 }
1076 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001077 die("can't apply initial cleaned capset");
1078
1079 /*
1080 * Instead of dropping bounding set first, do it here in case
1081 * the caller had a more permissive bounding set which could
1082 * have been used above to raise a capability that wasn't already
1083 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1084 */
Dylan Reidf682d472015-09-17 21:39:07 -07001085 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -08001086 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -04001087 continue;
1088 if (prctl(PR_CAPBSET_DROP, i))
1089 pdie("prctl(PR_CAPBSET_DROP)");
1090 }
Kees Cook323878a2013-02-05 15:35:24 -08001091
1092 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001093 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001094 flag[0] = CAP_SETPCAP;
1095 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1096 die("can't clear effective cap");
1097 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1098 die("can't clear permitted cap");
1099 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1100 die("can't clear inheritable cap");
1101 }
1102
1103 if (cap_set_proc(caps))
1104 die("can't apply final cleaned capset");
1105
1106 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001107}
1108
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001109void set_seccomp_filter(const struct minijail *j)
1110{
1111 /*
1112 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1113 * in the kernel source tree for an explanation of the parameters.
1114 */
1115 if (j->flags.no_new_privs) {
1116 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1117 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1118 }
1119
1120 /*
1121 * If we're logging seccomp filter failures,
1122 * install the SIGSYS handler first.
1123 */
1124 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1125 if (install_sigsys_handler())
1126 pdie("install SIGSYS handler");
1127 warn("logging seccomp filter failures");
1128 }
1129
1130 /*
1131 * Install the syscall filter.
1132 */
1133 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001134 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1135 j->filter_prog)) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001136 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1137 warn("seccomp not supported");
1138 return;
1139 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001140 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001141 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001142 }
1143}
1144
Will Drewry6ac91122011-10-21 16:38:58 -05001145void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001146{
Dylan Reidf682d472015-09-17 21:39:07 -07001147 /*
1148 * Get the last valid cap from /proc, since /proc can be unmounted
1149 * before drop_caps().
1150 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001151 unsigned int last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001152
Elly Jonese1749eb2011-10-07 13:54:59 -04001153 if (j->flags.pids)
1154 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001155 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001156
Elly Jonese1749eb2011-10-07 13:54:59 -04001157 if (j->flags.usergroups && !j->user)
1158 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001159
Elly Jonesdd3e8512012-01-23 15:13:38 -05001160 /*
1161 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001162 * so we don't even try. If any of our operations fail, we abort() the
1163 * entire process.
1164 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001165 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1166 pdie("setns(CLONE_NEWNS)");
1167
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001168 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001169 if (unshare(CLONE_NEWNS))
1170 pdie("unshare(vfs)");
1171 /*
1172 * Remount all filesystems as private. If they are shared
1173 * new bind mounts will creep out of our namespace.
1174 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1175 */
1176 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1177 pdie("mount(/, private)");
1178 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001179
Dylan Reidf7942472015-11-18 17:55:26 -08001180 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1181 pdie("unshare(ipc)");
1182 }
1183
Dylan Reid1102f5a2015-09-15 11:52:20 -07001184 if (j->flags.enter_net) {
1185 if (setns(j->netns_fd, CLONE_NEWNET))
1186 pdie("setns(CLONE_NEWNET)");
1187 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001188 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001189 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001190
Elly Jones51a5b6c2011-10-12 19:09:26 -04001191 if (j->flags.chroot && enter_chroot(j))
1192 pdie("chroot");
1193
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001194 if (j->flags.pivot_root && enter_pivot_root(j))
1195 pdie("pivot_root");
1196
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001197 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001198 pdie("mount_tmp");
1199
Dylan Reid791f5772015-09-14 20:02:42 -07001200 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001201 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001202
Elly Jonese1749eb2011-10-07 13:54:59 -04001203 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001204 /*
1205 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001206 * capability to change uids, our attempt to use setuid()
1207 * below will fail. Hang on to root caps across setuid(), then
1208 * lock securebits.
1209 */
1210 if (prctl(PR_SET_KEEPCAPS, 1))
1211 pdie("prctl(PR_SET_KEEPCAPS)");
1212 if (prctl
1213 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1214 pdie("prctl(PR_SET_SECUREBITS)");
1215 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001216
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001217 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001218 * If we're setting no_new_privs, we can drop privileges
1219 * before setting seccomp filter. This way filter policies
1220 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001221 */
1222 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001223 drop_ugid(j);
1224 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001225 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001226
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001227 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001228 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001229 /*
1230 * If we're not setting no_new_privs,
1231 * we need to set seccomp filter *before* dropping privileges.
1232 * WARNING: this means that filter policies *must* allow
1233 * setgroups()/setresgid()/setresuid() for dropping root and
1234 * capget()/capset()/prctl() for dropping caps.
1235 */
1236 set_seccomp_filter(j);
1237
1238 drop_ugid(j);
1239 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001240 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001241 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001242
Elly Jonesdd3e8512012-01-23 15:13:38 -05001243 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001244 * Select the specified alternate syscall table. The table must not
1245 * block prctl(2) if we're using seccomp as well.
1246 */
1247 if (j->flags.alt_syscall) {
1248 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1249 pdie("prctl(PR_ALT_SYSCALL)");
1250 }
1251
1252 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001253 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001254 * privilege-dropping syscalls :)
1255 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001256 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
1257 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1258 warn("seccomp not supported");
1259 return;
1260 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001261 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001262 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001263}
1264
Will Drewry6ac91122011-10-21 16:38:58 -05001265/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001266static int init_exitstatus = 0;
1267
Will Drewry6ac91122011-10-21 16:38:58 -05001268void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001269{
1270 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001271}
1272
Will Drewry6ac91122011-10-21 16:38:58 -05001273int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001274{
1275 pid_t pid;
1276 int status;
1277 /* so that we exit with the right status */
1278 signal(SIGTERM, init_term);
1279 /* TODO(wad) self jail with seccomp_filters here. */
1280 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001281 /*
1282 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001283 * left inside our pid namespace or we get a signal.
1284 */
1285 if (pid == rootpid)
1286 init_exitstatus = status;
1287 }
1288 if (!WIFEXITED(init_exitstatus))
1289 _exit(MINIJAIL_ERR_INIT);
1290 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001291}
1292
Will Drewry6ac91122011-10-21 16:38:58 -05001293int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001294{
1295 size_t sz = 0;
1296 size_t bytes = read(fd, &sz, sizeof(sz));
1297 char *buf;
1298 int r;
1299 if (sizeof(sz) != bytes)
1300 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001301 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001302 return -E2BIG;
1303 buf = malloc(sz);
1304 if (!buf)
1305 return -ENOMEM;
1306 bytes = read(fd, buf, sz);
1307 if (bytes != sz) {
1308 free(buf);
1309 return -EINVAL;
1310 }
1311 r = minijail_unmarshal(j, buf, sz);
1312 free(buf);
1313 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001314}
1315
Will Drewry6ac91122011-10-21 16:38:58 -05001316int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001317{
1318 char *buf;
1319 size_t sz = minijail_size(j);
1320 ssize_t written;
1321 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001322
Elly Jonese1749eb2011-10-07 13:54:59 -04001323 if (!sz)
1324 return -EINVAL;
1325 buf = malloc(sz);
1326 r = minijail_marshal(j, buf, sz);
1327 if (r) {
1328 free(buf);
1329 return r;
1330 }
1331 /* Sends [size][minijail]. */
1332 written = write(fd, &sz, sizeof(sz));
1333 if (written != sizeof(sz)) {
1334 free(buf);
1335 return -EFAULT;
1336 }
1337 written = write(fd, buf, sz);
1338 if (written < 0 || (size_t) written != sz) {
1339 free(buf);
1340 return -EFAULT;
1341 }
1342 free(buf);
1343 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001344}
Elly Jonescd7a9042011-07-22 13:56:51 -04001345
Will Drewry6ac91122011-10-21 16:38:58 -05001346int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001347{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001348#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001349 /* Don't use LDPRELOAD on Brillo. */
1350 return 0;
1351#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001352 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1353 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1354 if (!newenv)
1355 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001356
Elly Jonese1749eb2011-10-07 13:54:59 -04001357 /* Only insert a separating space if we have something to separate... */
1358 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1359 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001360
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001361 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001362 setenv(kLdPreloadEnvVar, newenv, 1);
1363 free(newenv);
1364 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001365#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001366}
1367
Will Drewry6ac91122011-10-21 16:38:58 -05001368int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001369{
1370 int r = pipe(fds);
1371 char fd_buf[11];
1372 if (r)
1373 return r;
1374 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1375 if (r <= 0)
1376 return -EINVAL;
1377 setenv(kFdEnvVar, fd_buf, 1);
1378 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001379}
1380
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001381int setup_pipe_end(int fds[2], size_t index)
1382{
1383 if (index > 1)
1384 return -1;
1385
1386 close(fds[1 - index]);
1387 return fds[index];
1388}
1389
1390int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1391{
1392 if (index > 1)
1393 return -1;
1394
1395 close(fds[1 - index]);
1396 /* dup2(2) the corresponding end of the pipe into |fd|. */
1397 return dup2(fds[index], fd);
1398}
1399
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001400int minijail_run_internal(struct minijail *j, const char *filename,
1401 char *const argv[], pid_t *pchild_pid,
1402 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1403 int use_preload);
1404
Will Drewry6ac91122011-10-21 16:38:58 -05001405int API minijail_run(struct minijail *j, const char *filename,
1406 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001407{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001408 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1409 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001410}
1411
1412int API minijail_run_pid(struct minijail *j, const char *filename,
1413 char *const argv[], pid_t *pchild_pid)
1414{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001415 return minijail_run_internal(j, filename, argv, pchild_pid,
1416 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001417}
1418
1419int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001420 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001421{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001422 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1423 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001424}
1425
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001426int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001427 char *const argv[], pid_t *pchild_pid,
1428 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001429{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001430 return minijail_run_internal(j, filename, argv, pchild_pid,
1431 pstdin_fd, pstdout_fd, pstderr_fd, true);
1432}
1433
1434int API minijail_run_no_preload(struct minijail *j, const char *filename,
1435 char *const argv[])
1436{
1437 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1438 false);
1439}
1440
Samuel Tan63187f42015-10-16 13:01:53 -07001441int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001442 const char *filename,
1443 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001444 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001445 int *pstdin_fd, int *pstdout_fd,
1446 int *pstderr_fd) {
Samuel Tan63187f42015-10-16 13:01:53 -07001447 return minijail_run_internal(j, filename, argv, pchild_pid,
1448 pstdin_fd, pstdout_fd, pstderr_fd, false);
1449}
1450
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001451int minijail_run_internal(struct minijail *j, const char *filename,
1452 char *const argv[], pid_t *pchild_pid,
1453 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1454 int use_preload)
1455{
Elly Jonese1749eb2011-10-07 13:54:59 -04001456 char *oldenv, *oldenv_copy = NULL;
1457 pid_t child_pid;
1458 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001459 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001460 int stdout_fds[2];
1461 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001462 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001463 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001464 /* We need to remember this across the minijail_preexec() call. */
1465 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001466 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001467
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001468 if (use_preload) {
1469 oldenv = getenv(kLdPreloadEnvVar);
1470 if (oldenv) {
1471 oldenv_copy = strdup(oldenv);
1472 if (!oldenv_copy)
1473 return -ENOMEM;
1474 }
1475
1476 if (setup_preload())
1477 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001478 }
Will Drewryf89aef52011-09-16 16:48:57 -05001479
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001480 if (!use_preload) {
1481 if (j->flags.caps)
1482 die("Capabilities are not supported without "
1483 "LD_PRELOAD");
1484 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001485
Elly Jonesdd3e8512012-01-23 15:13:38 -05001486 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001487 * Make the process group ID of this process equal to its PID, so that
1488 * both the Minijail process and the jailed process can be killed
1489 * together.
1490 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1491 * the process is already a process group leader.
1492 */
1493 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1494 if (errno != EPERM) {
1495 pdie("setpgid(0, 0)");
1496 }
1497 }
1498
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001499 if (use_preload) {
1500 /*
1501 * Before we fork(2) and execve(2) the child process, we need
1502 * to open a pipe(2) to send the minijail configuration over.
1503 */
1504 if (setup_pipe(pipe_fds))
1505 return -EFAULT;
1506 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001507
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001508 /*
1509 * If we want to write to the child process' standard input,
1510 * create the pipe(2) now.
1511 */
1512 if (pstdin_fd) {
1513 if (pipe(stdin_fds))
1514 return -EFAULT;
1515 }
1516
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001517 /*
1518 * If we want to read from the child process' standard output,
1519 * create the pipe(2) now.
1520 */
1521 if (pstdout_fd) {
1522 if (pipe(stdout_fds))
1523 return -EFAULT;
1524 }
1525
1526 /*
1527 * If we want to read from the child process' standard error,
1528 * create the pipe(2) now.
1529 */
1530 if (pstderr_fd) {
1531 if (pipe(stderr_fds))
1532 return -EFAULT;
1533 }
1534
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001535 /*
1536 * If we want to set up a new uid/gid mapping in the user namespace,
1537 * create the pipe(2) to sync between parent and child.
1538 */
1539 if (j->flags.userns) {
1540 if (pipe(userns_pipe_fds))
1541 return -EFAULT;
1542 }
1543
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001544 /*
1545 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001546 *
1547 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1548 *
1549 * In multithreaded programs, there are a bunch of locks inside libc,
1550 * some of which may be held by other threads at the time that we call
1551 * minijail_run_pid(). If we call fork(), glibc does its level best to
1552 * ensure that we hold all of these locks before it calls clone()
1553 * internally and drop them after clone() returns, but when we call
1554 * sys_clone(2) directly, all that gets bypassed and we end up with a
1555 * child address space where some of libc's important locks are held by
1556 * other threads (which did not get cloned, and hence will never release
1557 * those locks). This is okay so long as we call exec() immediately
1558 * after, but a bunch of seemingly-innocent libc functions like setenv()
1559 * take locks.
1560 *
1561 * Hence, only call sys_clone() if we need to, in order to get at pid
1562 * namespacing. If we follow this path, the child's address space might
1563 * have broken locks; you may only call functions that do not acquire
1564 * any locks.
1565 *
1566 * Unfortunately, fork() acquires every lock it can get its hands on, as
1567 * previously detailed, so this function is highly likely to deadlock
1568 * later on (see "deadlock here") if we're multithreaded.
1569 *
1570 * We might hack around this by having the clone()d child (init of the
1571 * pid namespace) return directly, rather than leaving the clone()d
1572 * process hanging around to be init for the new namespace (and having
1573 * its fork()ed child return in turn), but that process would be crippled
1574 * with its libc locks potentially broken. We might try fork()ing in the
1575 * parent before we clone() to ensure that we own all the locks, but
1576 * then we have to have the forked child hanging around consuming
1577 * resources (and possibly having file descriptors / shared memory
1578 * regions / etc attached). We'd need to keep the child around to avoid
1579 * having its children get reparented to init.
1580 *
1581 * TODO(ellyjones): figure out if the "forked child hanging around"
1582 * problem is fixable or not. It would be nice if we worked in this
1583 * case.
1584 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001585 if (pid_namespace) {
1586 int clone_flags = CLONE_NEWPID | SIGCHLD;
1587 if (j->flags.userns)
1588 clone_flags |= CLONE_NEWUSER;
1589 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001590 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001591 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001592 }
Elly Jones761b7412012-06-13 15:49:52 -04001593
Elly Jonese1749eb2011-10-07 13:54:59 -04001594 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001595 if (use_preload) {
1596 free(oldenv_copy);
1597 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001598 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001599 }
Will Drewryf89aef52011-09-16 16:48:57 -05001600
Elly Jonese1749eb2011-10-07 13:54:59 -04001601 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001602 if (use_preload) {
1603 /* Restore parent's LD_PRELOAD. */
1604 if (oldenv_copy) {
1605 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1606 free(oldenv_copy);
1607 } else {
1608 unsetenv(kLdPreloadEnvVar);
1609 }
1610 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001611 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001612
Elly Jonese1749eb2011-10-07 13:54:59 -04001613 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001614
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001615 if (j->flags.pid_file)
1616 write_pid_file(j);
1617
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001618 if (j->flags.userns)
1619 write_ugid_mappings(j, userns_pipe_fds);
1620
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001621 if (use_preload) {
1622 /* Send marshalled minijail. */
1623 close(pipe_fds[0]); /* read endpoint */
1624 ret = minijail_to_fd(j, pipe_fds[1]);
1625 close(pipe_fds[1]); /* write endpoint */
1626 if (ret) {
1627 kill(j->initpid, SIGKILL);
1628 die("failed to send marshalled minijail");
1629 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001630 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001631
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001632 if (pchild_pid)
1633 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001634
1635 /*
1636 * If we want to write to the child process' standard input,
1637 * set up the write end of the pipe.
1638 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001639 if (pstdin_fd)
1640 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001641 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001642
1643 /*
1644 * If we want to read from the child process' standard output,
1645 * set up the read end of the pipe.
1646 */
1647 if (pstdout_fd)
1648 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001649 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001650
1651 /*
1652 * If we want to read from the child process' standard error,
1653 * set up the read end of the pipe.
1654 */
1655 if (pstderr_fd)
1656 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001657 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001658
Elly Jonese1749eb2011-10-07 13:54:59 -04001659 return 0;
1660 }
1661 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001662
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001663 if (j->flags.userns)
1664 enter_user_namespace(j, userns_pipe_fds);
1665
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001666 /*
1667 * If we want to write to the jailed process' standard input,
1668 * set up the read end of the pipe.
1669 */
1670 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001671 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1672 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001673 die("failed to set up stdin pipe");
1674 }
1675
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001676 /*
1677 * If we want to read from the jailed process' standard output,
1678 * set up the write end of the pipe.
1679 */
1680 if (pstdout_fd) {
1681 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1682 STDOUT_FILENO) < 0)
1683 die("failed to set up stdout pipe");
1684 }
1685
1686 /*
1687 * If we want to read from the jailed process' standard error,
1688 * set up the write end of the pipe.
1689 */
1690 if (pstderr_fd) {
1691 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1692 STDERR_FILENO) < 0)
1693 die("failed to set up stderr pipe");
1694 }
1695
Dylan Reid791f5772015-09-14 20:02:42 -07001696 /* If running an init program, let it decide when/how to mount /proc. */
1697 if (pid_namespace && !do_init)
1698 j->flags.remount_proc_ro = 0;
1699
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001700 if (use_preload) {
1701 /* Strip out flags that cannot be inherited across execve(2). */
1702 minijail_preexec(j);
1703 } else {
1704 j->flags.pids = 0;
1705 }
1706 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001707 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001708
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001709 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001710 /*
1711 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001712 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001713 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001714 * a child to actually run the program. If |do_init == 0|, we
1715 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001716 *
1717 * If we're multithreaded, we'll probably deadlock here. See
1718 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001719 */
1720 child_pid = fork();
1721 if (child_pid < 0)
1722 _exit(child_pid);
1723 else if (child_pid > 0)
1724 init(child_pid); /* never returns */
1725 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001726
Elly Jonesdd3e8512012-01-23 15:13:38 -05001727 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001728 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001729 * calling process
1730 * -> execve()-ing process
1731 * If we are:
1732 * calling process
1733 * -> init()-ing process
1734 * -> execve()-ing process
1735 */
1736 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001737}
1738
Will Drewry6ac91122011-10-21 16:38:58 -05001739int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001740{
1741 int st;
1742 if (kill(j->initpid, SIGTERM))
1743 return -errno;
1744 if (waitpid(j->initpid, &st, 0) < 0)
1745 return -errno;
1746 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001747}
1748
Will Drewry6ac91122011-10-21 16:38:58 -05001749int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001750{
1751 int st;
1752 if (waitpid(j->initpid, &st, 0) < 0)
1753 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001754
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001755 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001756 int error_status = st;
1757 if (WIFSIGNALED(st)) {
1758 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001759 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001760 j->initpid, signum);
1761 /*
1762 * We return MINIJAIL_ERR_JAIL if the process received
1763 * SIGSYS, which happens when a syscall is blocked by
1764 * seccomp filters.
1765 * If not, we do what bash(1) does:
1766 * $? = 128 + signum
1767 */
1768 if (signum == SIGSYS) {
1769 error_status = MINIJAIL_ERR_JAIL;
1770 } else {
1771 error_status = 128 + signum;
1772 }
1773 }
1774 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001775 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001776
1777 int exit_status = WEXITSTATUS(st);
1778 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001779 info("child process %d exited with status %d",
1780 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001781
1782 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001783}
1784
Will Drewry6ac91122011-10-21 16:38:58 -05001785void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001786{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001787 if (j->flags.seccomp_filter && j->filter_prog) {
1788 free(j->filter_prog->filter);
1789 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001790 }
Dylan Reid648b2202015-10-23 00:50:00 -07001791 while (j->mounts_head) {
1792 struct mountpoint *m = j->mounts_head;
1793 j->mounts_head = j->mounts_head->next;
1794 free(m->type);
1795 free(m->dest);
1796 free(m->src);
1797 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001798 }
Dylan Reid648b2202015-10-23 00:50:00 -07001799 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001800 if (j->user)
1801 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08001802 if (j->suppl_gid_list)
1803 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05001804 if (j->chrootdir)
1805 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08001806 if (j->alt_syscall_table)
1807 free(j->alt_syscall_table);
Elly Jonese1749eb2011-10-07 13:54:59 -04001808 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001809}