blob: b5a2ce141baec66e4c4cd452128b937cf9cdc0a7 [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;
162 memset(&j->flags, 0, sizeof(j->flags));
163 /* Now restore anything we meant to keep. */
164 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700165 j->flags.enter_vfs = enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700166 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800167 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700168 /* Note, |pids| will already have been used before this call. */
169}
170
171/* Minijail API. */
172
Will Drewry6ac91122011-10-21 16:38:58 -0500173struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400174{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400175 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400176}
177
Will Drewry6ac91122011-10-21 16:38:58 -0500178void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400179{
180 if (uid == 0)
181 die("useless change to uid 0");
182 j->uid = uid;
183 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400184}
185
Will Drewry6ac91122011-10-21 16:38:58 -0500186void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400187{
188 if (gid == 0)
189 die("useless change to gid 0");
190 j->gid = gid;
191 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400192}
193
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800194int API minijail_set_supplementary_gids(struct minijail *j, size_t size,
195 const gid_t *list)
196{
197 if (j->flags.usergroups)
198 die("cannot inherit *and* set supplementary groups");
199
200 if (size == 0)
201 return -EINVAL;
202
203 /* Copy the gid_t array. */
204 j->suppl_gid_list = calloc(size, sizeof(gid_t));
205 if (!j->suppl_gid_list) {
206 return -ENOMEM;
207 }
208 for (size_t i = 0; i < size; i++) {
209 j->suppl_gid_list[i] = list[i];
210 }
211 j->suppl_gid_count = size;
212 j->flags.suppl_gids = 1;
213 return 0;
214}
215
Will Drewry6ac91122011-10-21 16:38:58 -0500216int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400217{
218 char *buf = NULL;
219 struct passwd pw;
220 struct passwd *ppw = NULL;
221 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
222 if (sz == -1)
223 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400224
Elly Jonesdd3e8512012-01-23 15:13:38 -0500225 /*
226 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400227 * the maximum needed size of the buffer, so we don't have to search.
228 */
229 buf = malloc(sz);
230 if (!buf)
231 return -ENOMEM;
232 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500233 /*
234 * We're safe to free the buffer here. The strings inside pw point
235 * inside buf, but we don't use any of them; this leaves the pointers
236 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
237 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400238 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700239 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400240 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700241 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400242 minijail_change_uid(j, ppw->pw_uid);
243 j->user = strdup(user);
244 if (!j->user)
245 return -ENOMEM;
246 j->usergid = ppw->pw_gid;
247 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400248}
249
Will Drewry6ac91122011-10-21 16:38:58 -0500250int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400251{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700252 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700253 struct group gr;
254 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400255 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
256 if (sz == -1)
257 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400258
Elly Jonesdd3e8512012-01-23 15:13:38 -0500259 /*
260 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400261 * the maximum needed size of the buffer, so we don't have to search.
262 */
263 buf = malloc(sz);
264 if (!buf)
265 return -ENOMEM;
266 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500267 /*
268 * We're safe to free the buffer here. The strings inside gr point
269 * inside buf, but we don't use any of them; this leaves the pointers
270 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
271 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400272 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700273 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400274 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700275 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400276 minijail_change_gid(j, pgr->gr_gid);
277 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400278}
279
Will Drewry6ac91122011-10-21 16:38:58 -0500280void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400281{
282 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400283}
284
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700285void API minijail_no_new_privs(struct minijail *j)
286{
287 j->flags.no_new_privs = 1;
288}
289
Will Drewry6ac91122011-10-21 16:38:58 -0500290void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400291{
292 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500293}
294
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700295void API minijail_log_seccomp_filter_failures(struct minijail *j)
296{
297 j->flags.log_seccomp_filter = 1;
298}
299
Will Drewry6ac91122011-10-21 16:38:58 -0500300void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400301{
302 j->caps = capmask;
303 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400304}
305
Will Drewry6ac91122011-10-21 16:38:58 -0500306void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400307{
308 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400309}
310
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700311void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
312{
313 int ns_fd = open(ns_path, O_RDONLY);
314 if (ns_fd < 0) {
315 pdie("failed to open namespace '%s'", ns_path);
316 }
317 j->mountns_fd = ns_fd;
318 j->flags.enter_vfs = 1;
319}
320
Will Drewry6ac91122011-10-21 16:38:58 -0500321void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400322{
Elly Jonese58176c2012-01-23 11:46:17 -0500323 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700324 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400325 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800326 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400327}
328
Dylan Reidf7942472015-11-18 17:55:26 -0800329void API minijail_namespace_ipc(struct minijail *j)
330{
331 j->flags.ipc = 1;
332}
333
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400334void API minijail_namespace_net(struct minijail *j)
335{
336 j->flags.net = 1;
337}
338
Dylan Reid1102f5a2015-09-15 11:52:20 -0700339void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
340{
341 int ns_fd = open(ns_path, O_RDONLY);
342 if (ns_fd < 0) {
343 pdie("failed to open namespace '%s'", ns_path);
344 }
345 j->netns_fd = ns_fd;
346 j->flags.enter_net = 1;
347}
348
Dylan Reid791f5772015-09-14 20:02:42 -0700349void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400350{
351 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700352 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400353}
354
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800355void API minijail_namespace_user(struct minijail *j)
356{
357 j->flags.userns = 1;
358}
359
360int API minijail_uidmap(struct minijail *j, const char *uidmap)
361{
362 j->uidmap = strdup(uidmap);
363 if (!j->uidmap)
364 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800365 char *ch;
366 for (ch = j->uidmap; *ch; ch++) {
367 if (*ch == ',')
368 *ch = '\n';
369 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800370 return 0;
371}
372
373int API minijail_gidmap(struct minijail *j, const char *gidmap)
374{
375 j->gidmap = strdup(gidmap);
376 if (!j->gidmap)
377 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800378 char *ch;
379 for (ch = j->gidmap; *ch; ch++) {
380 if (*ch == ',')
381 *ch = '\n';
382 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800383 return 0;
384}
385
Will Drewry6ac91122011-10-21 16:38:58 -0500386void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400387{
388 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400389}
390
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800391void API minijail_run_as_init(struct minijail *j)
392{
393 /*
394 * Since the jailed program will become 'init' in the new PID namespace,
395 * Minijail does not need to fork an 'init' process.
396 */
397 j->flags.do_init = 0;
398}
399
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700400int API minijail_enter_chroot(struct minijail *j, const char *dir)
401{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400402 if (j->chrootdir)
403 return -EINVAL;
404 j->chrootdir = strdup(dir);
405 if (!j->chrootdir)
406 return -ENOMEM;
407 j->flags.chroot = 1;
408 return 0;
409}
410
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800411int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
412{
413 if (j->chrootdir)
414 return -EINVAL;
415 j->chrootdir = strdup(dir);
416 if (!j->chrootdir)
417 return -ENOMEM;
418 j->flags.pivot_root = 1;
419 return 0;
420}
421
Dylan Reida14e08d2015-10-22 21:05:29 -0700422static char *append_external_path(const char *external_path,
423 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700424{
Dylan Reida14e08d2015-10-22 21:05:29 -0700425 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700426 size_t pathlen;
427
Dylan Reid08946cc2015-09-16 19:10:57 -0700428 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700429 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
430 path = malloc(pathlen);
431 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700432
Dylan Reida14e08d2015-10-22 21:05:29 -0700433 return path;
434}
435
436char API *minijail_get_original_path(struct minijail *j,
437 const char *path_inside_chroot)
438{
Dylan Reid648b2202015-10-23 00:50:00 -0700439 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700440
Dylan Reid648b2202015-10-23 00:50:00 -0700441 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700442 while (b) {
443 /*
444 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700445 * mount, then the original path is exactly the source of
446 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700447 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700448 * mount source = /some/path/exe, mount dest =
449 * /chroot/path/exe Then when getting the original path of
450 * "/chroot/path/exe", the source of that mount,
451 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700452 */
453 if (!strcmp(b->dest, path_inside_chroot))
454 return strdup(b->src);
455
456 /*
457 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700458 * mount, take the suffix of the chroot path relative to the
459 * mount destination path, and append it to the mount source
460 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700461 */
462 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
463 const char *relative_path =
464 path_inside_chroot + strlen(b->dest);
465 return append_external_path(b->src, relative_path);
466 }
467 b = b->next;
468 }
469
470 /* If there is a chroot path, append |path_inside_chroot| to that. */
471 if (j->chrootdir)
472 return append_external_path(j->chrootdir, path_inside_chroot);
473
474 /* No chroot, so the path outside is the same as it is inside. */
475 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700476}
477
Lee Campbell11af0622014-05-22 12:36:04 -0700478void API minijail_mount_tmp(struct minijail *j)
479{
480 j->flags.mount_tmp = 1;
481}
482
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800483int API minijail_write_pid_file(struct minijail *j, const char *path)
484{
485 j->pid_file_path = strdup(path);
486 if (!j->pid_file_path)
487 return -ENOMEM;
488 j->flags.pid_file = 1;
489 return 0;
490}
491
Dylan Reid648b2202015-10-23 00:50:00 -0700492int API minijail_mount(struct minijail *j, const char *src, const char *dest,
493 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700494{
Dylan Reid648b2202015-10-23 00:50:00 -0700495 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400496
497 if (*dest != '/')
498 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700499 m = calloc(1, sizeof(*m));
500 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400501 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700502 m->dest = strdup(dest);
503 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400504 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700505 m->src = strdup(src);
506 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400507 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700508 m->type = strdup(type);
509 if (!m->type)
510 goto error;
511 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400512
Dylan Reid648b2202015-10-23 00:50:00 -0700513 info("mount %s -> %s type %s", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400514
Elly Jonesdd3e8512012-01-23 15:13:38 -0500515 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700516 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400517 * containing vfs namespace.
518 */
519 minijail_namespace_vfs(j);
520
Dylan Reid648b2202015-10-23 00:50:00 -0700521 if (j->mounts_tail)
522 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400523 else
Dylan Reid648b2202015-10-23 00:50:00 -0700524 j->mounts_head = m;
525 j->mounts_tail = m;
526 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400527
528 return 0;
529
530error:
Dylan Reid648b2202015-10-23 00:50:00 -0700531 free(m->src);
532 free(m->dest);
533 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400534 return -ENOMEM;
535}
536
Dylan Reid648b2202015-10-23 00:50:00 -0700537int API minijail_bind(struct minijail *j, const char *src, const char *dest,
538 int writeable)
539{
540 unsigned long flags = MS_BIND;
541
542 if (!writeable)
543 flags |= MS_RDONLY;
544
545 return minijail_mount(j, src, dest, "", flags);
546}
547
Will Drewry6ac91122011-10-21 16:38:58 -0500548void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400549{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700550 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
551 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
552 warn("not loading seccomp filter, seccomp not supported");
553 return;
554 }
555 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400556 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800557 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700558 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400559 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800560
561 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700562 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
563 die("failed to compile seccomp filter BPF program in '%s'",
564 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800565 }
566
567 j->filter_len = fprog->len;
568 j->filter_prog = fprog;
569
Elly Jonese1749eb2011-10-07 13:54:59 -0400570 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500571}
572
Andrew Brestickereac28942015-11-11 16:04:46 -0800573int API minijail_use_alt_syscall(struct minijail *j, const char *table)
574{
575 j->alt_syscall_table = strdup(table);
576 if (!j->alt_syscall_table)
577 return -ENOMEM;
578 j->flags.alt_syscall = 1;
579 return 0;
580}
581
Will Drewryf89aef52011-09-16 16:48:57 -0500582struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400583 size_t available;
584 size_t total;
585 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500586};
587
Will Drewry6ac91122011-10-21 16:38:58 -0500588void marshal_state_init(struct marshal_state *state,
589 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400590{
591 state->available = available;
592 state->buf = buf;
593 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500594}
595
Will Drewry6ac91122011-10-21 16:38:58 -0500596void marshal_append(struct marshal_state *state,
597 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400598{
599 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500600
Elly Jonese1749eb2011-10-07 13:54:59 -0400601 /* Up to |available| will be written. */
602 if (copy_len) {
603 memcpy(state->buf, src, copy_len);
604 state->buf += copy_len;
605 state->available -= copy_len;
606 }
607 /* |total| will contain the expected length. */
608 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500609}
610
Will Drewry6ac91122011-10-21 16:38:58 -0500611void minijail_marshal_helper(struct marshal_state *state,
612 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400613{
Dylan Reid648b2202015-10-23 00:50:00 -0700614 struct mountpoint *m = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400615 marshal_append(state, (char *)j, sizeof(*j));
616 if (j->user)
617 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400618 if (j->chrootdir)
619 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800620 if (j->alt_syscall_table) {
621 marshal_append(state, j->alt_syscall_table,
622 strlen(j->alt_syscall_table) + 1);
623 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800624 if (j->flags.seccomp_filter && j->filter_prog) {
625 struct sock_fprog *fp = j->filter_prog;
626 marshal_append(state, (char *)fp->filter,
627 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400628 }
Dylan Reid648b2202015-10-23 00:50:00 -0700629 for (m = j->mounts_head; m; m = m->next) {
630 marshal_append(state, m->src, strlen(m->src) + 1);
631 marshal_append(state, m->dest, strlen(m->dest) + 1);
632 marshal_append(state, m->type, strlen(m->type) + 1);
633 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400634 }
Will Drewryf89aef52011-09-16 16:48:57 -0500635}
636
Will Drewry6ac91122011-10-21 16:38:58 -0500637size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400638{
639 struct marshal_state state;
640 marshal_state_init(&state, NULL, 0);
641 minijail_marshal_helper(&state, j);
642 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500643}
644
Elly Jonese1749eb2011-10-07 13:54:59 -0400645int minijail_marshal(const struct minijail *j, char *buf, size_t available)
646{
647 struct marshal_state state;
648 marshal_state_init(&state, buf, available);
649 minijail_marshal_helper(&state, j);
650 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500651}
652
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800653/*
654 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
Elly Jones51a5b6c2011-10-12 19:09:26 -0400655 * @length Number of bytes to consume
656 * @buf Buffer to consume from
657 * @buflength Size of @buf
658 *
659 * Returns a pointer to the base of the bytes, or NULL for errors.
660 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700661void *consumebytes(size_t length, char **buf, size_t *buflength)
662{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400663 char *p = *buf;
664 if (length > *buflength)
665 return NULL;
666 *buf += length;
667 *buflength -= length;
668 return p;
669}
670
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800671/*
672 * consumestr: consumes a C string from a buffer @buf of length @length
Elly Jones51a5b6c2011-10-12 19:09:26 -0400673 * @buf Buffer to consume
674 * @length Length of buffer
675 *
676 * Returns a pointer to the base of the string, or NULL for errors.
677 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700678char *consumestr(char **buf, size_t *buflength)
679{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400680 size_t len = strnlen(*buf, *buflength);
681 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700682 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400683 return NULL;
684 return consumebytes(len + 1, buf, buflength);
685}
686
Elly Jonese1749eb2011-10-07 13:54:59 -0400687int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
688{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800689 size_t i;
690 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500691 int ret = -EINVAL;
692
Elly Jonese1749eb2011-10-07 13:54:59 -0400693 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500694 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400695 memcpy((void *)j, serialized, sizeof(*j));
696 serialized += sizeof(*j);
697 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500698
Will Drewrybee7ba72011-10-21 20:47:01 -0500699 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700700 j->mounts_head = NULL;
701 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800702 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500703
Elly Jonese1749eb2011-10-07 13:54:59 -0400704 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400705 char *user = consumestr(&serialized, &length);
706 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500707 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400708 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500709 if (!j->user)
710 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400711 }
Will Drewryf89aef52011-09-16 16:48:57 -0500712
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400713 if (j->chrootdir) { /* stale pointer */
714 char *chrootdir = consumestr(&serialized, &length);
715 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500716 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400717 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500718 if (!j->chrootdir)
719 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400720 }
721
Andrew Brestickereac28942015-11-11 16:04:46 -0800722 if (j->alt_syscall_table) { /* stale pointer */
723 char *alt_syscall_table = consumestr(&serialized, &length);
724 if (!alt_syscall_table)
725 goto bad_syscall_table;
726 j->alt_syscall_table = strdup(alt_syscall_table);
727 if (!j->alt_syscall_table)
728 goto bad_syscall_table;
729 }
730
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800731 if (j->flags.seccomp_filter && j->filter_len > 0) {
732 size_t ninstrs = j->filter_len;
733 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
734 ninstrs > USHRT_MAX)
735 goto bad_filters;
736
737 size_t program_len = ninstrs * sizeof(struct sock_filter);
738 void *program = consumebytes(program_len, &serialized, &length);
739 if (!program)
740 goto bad_filters;
741
742 j->filter_prog = malloc(sizeof(struct sock_fprog));
743 j->filter_prog->len = ninstrs;
744 j->filter_prog->filter = malloc(program_len);
745 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400746 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400747
Dylan Reid648b2202015-10-23 00:50:00 -0700748 count = j->mounts_count;
749 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400750 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700751 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400752 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700753 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400754 const char *src = consumestr(&serialized, &length);
755 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700756 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400757 dest = consumestr(&serialized, &length);
758 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700759 goto bad_mounts;
760 type = consumestr(&serialized, &length);
761 if (!type)
762 goto bad_mounts;
763 flags = consumebytes(sizeof(*flags), &serialized, &length);
764 if (!flags)
765 goto bad_mounts;
766 if (minijail_mount(j, src, dest, type, *flags))
767 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400768 }
769
Elly Jonese1749eb2011-10-07 13:54:59 -0400770 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500771
Dylan Reid648b2202015-10-23 00:50:00 -0700772bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800773 if (j->flags.seccomp_filter && j->filter_len > 0) {
774 free(j->filter_prog->filter);
775 free(j->filter_prog);
776 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500777bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800778 if (j->alt_syscall_table)
779 free(j->alt_syscall_table);
780bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500781 if (j->chrootdir)
782 free(j->chrootdir);
783bad_chrootdir:
784 if (j->user)
785 free(j->user);
786clear_pointers:
787 j->user = NULL;
788 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800789 j->alt_syscall_table = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500790out:
791 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500792}
793
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800794static void write_ugid_mappings(const struct minijail *j, int *pipe_fds)
795{
796 int fd, ret, len;
797 size_t sz;
798 char fname[32];
799 close(pipe_fds[0]);
800
801 sz = sizeof(fname);
802 if (j->uidmap) {
803 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700804 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800805 die("failed to write file name of uid_map");
806 fd = open(fname, O_WRONLY);
807 if (fd < 0)
808 pdie("failed to open '%s'", fname);
809 len = strlen(j->uidmap);
810 if (write(fd, j->uidmap, len) < len)
811 die("failed to set uid_map");
812 close(fd);
813 }
814 if (j->gidmap) {
815 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700816 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800817 die("failed to write file name of gid_map");
818 fd = open(fname, O_WRONLY);
819 if (fd < 0)
820 pdie("failed to open '%s'", fname);
821 len = strlen(j->gidmap);
822 if (write(fd, j->gidmap, len) < len)
823 die("failed to set gid_map");
824 close(fd);
825 }
826
827 close(pipe_fds[1]);
828}
829
830static void enter_user_namespace(const struct minijail *j, int *pipe_fds)
831{
832 char buf;
833
834 close(pipe_fds[1]);
835
836 /* Wait for parent to set up uid/gid mappings. */
837 if (read(pipe_fds[0], &buf, 1) != 0)
838 die("failed to sync with parent");
839 close(pipe_fds[0]);
840
841 if (j->uidmap && setresuid(0, 0, 0))
842 pdie("setresuid");
843 if (j->gidmap && setresgid(0, 0, 0))
844 pdie("setresgid");
845}
846
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800847/*
848 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -0700849 * @j Minijail these mounts are for
850 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -0400851 *
852 * Returns 0 for success.
853 */
Dylan Reid648b2202015-10-23 00:50:00 -0700854static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700855{
Dylan Reid648b2202015-10-23 00:50:00 -0700856 int ret;
857 char *dest;
858 int remount_ro = 0;
859
Elly Jones51a5b6c2011-10-12 19:09:26 -0400860 /* dest has a leading "/" */
Dylan Reid648b2202015-10-23 00:50:00 -0700861 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400862 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700863
864 /*
865 * R/O bind mounts have to be remounted since bind and ro can't both be
866 * specified in the original bind mount. Remount R/O after the initial
867 * mount.
868 */
869 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
870 remount_ro = 1;
871 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -0500872 }
Dylan Reid648b2202015-10-23 00:50:00 -0700873
874 ret = mount(m->src, dest, m->type, m->flags, NULL);
875 if (ret)
876 pdie("mount: %s -> %s", m->src, dest);
877
878 if (remount_ro) {
879 m->flags |= MS_RDONLY;
880 ret = mount(m->src, dest, NULL,
881 m->flags | MS_REMOUNT, NULL);
882 if (ret)
883 pdie("bind ro: %s -> %s", m->src, dest);
884 }
885
Elly Jones51a5b6c2011-10-12 19:09:26 -0400886 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -0700887 if (m->next)
888 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400889 return ret;
890}
891
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700892int enter_chroot(const struct minijail *j)
893{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400894 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -0700895
896 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -0400897 return ret;
898
899 if (chroot(j->chrootdir))
900 return -errno;
901
902 if (chdir("/"))
903 return -errno;
904
905 return 0;
906}
907
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800908int enter_pivot_root(const struct minijail *j)
909{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800910 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -0700911
912 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800913 return ret;
914
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800915 /* Keep the fd for both old and new root. It will be used in fchdir later. */
916 oldroot = open("/", O_DIRECTORY | O_RDONLY);
917 if (oldroot < 0)
918 pdie("failed to open / for fchdir");
919 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY);
920 if (newroot < 0)
921 pdie("failed to open %s for fchdir", j->chrootdir);
922
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800923 /* To ensure chrootdir is the root of a file system, do a self bind mount. */
924 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
925 pdie("failed to bind mount '%s'", j->chrootdir);
926 if (chdir(j->chrootdir))
927 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800928 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800929 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800930
931 /*
932 * Now the old root is mounted on top of the new root. Use fchdir to
933 * change to the old root and unmount it.
934 */
935 if (fchdir(oldroot))
936 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800937 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800938 if (umount2(".", MNT_DETACH))
939 pdie("umount(/)");
940 /* Change back to the new root. */
941 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800942 return -errno;
943 if (chroot("/"))
944 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -0700945 /* Set correct CWD for getcwd(3). */
946 if (chdir("/"))
947 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800948
949 return 0;
950}
951
Lee Campbell11af0622014-05-22 12:36:04 -0700952int mount_tmp(void)
953{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800954 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -0700955}
956
Dylan Reid791f5772015-09-14 20:02:42 -0700957int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400958{
959 const char *kProcPath = "/proc";
960 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500961 /*
962 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400963 * /proc in our namespace, which means using MS_REMOUNT here would
964 * mutate our parent's mount as well, even though we're in a VFS
965 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800966 * and make our own. However, if we are in a new user namespace, /proc
967 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -0400968 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -0700969 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -0400970 return -errno;
971 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
972 return -errno;
973 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400974}
975
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800976static void write_pid_file(const struct minijail *j)
977{
978 FILE *fp = fopen(j->pid_file_path, "w");
979
980 if (!fp)
981 pdie("failed to open '%s'", j->pid_file_path);
982 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
983 pdie("fprintf(%s)", j->pid_file_path);
984 if (fclose(fp))
985 pdie("fclose(%s)", j->pid_file_path);
986}
987
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700988void drop_ugid(const struct minijail *j)
989{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800990 if (j->flags.usergroups && j->flags.suppl_gids) {
991 die("tried to inherit *and* set supplementary groups;"
992 " can only do one");
993 }
994
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700995 if (j->flags.usergroups) {
996 if (initgroups(j->user, j->usergid))
997 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800998 } else if (j->flags.suppl_gids) {
999 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1000 pdie("setgroups");
1001 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001002 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001003 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001004 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001005 * users.
1006 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001007 if ((j->uid || j->gid) && setgroups(0, NULL))
1008 pdie("setgroups");
1009 }
1010
1011 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1012 pdie("setresgid");
1013
1014 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1015 pdie("setresuid");
1016}
1017
Mike Frysinger3adfef72013-05-09 17:19:08 -04001018/*
1019 * We specifically do not use cap_valid() as that only tells us the last
1020 * valid cap we were *compiled* against (i.e. what the version of kernel
1021 * headers says). If we run on a different kernel version, then it's not
1022 * uncommon for that to be less (if an older kernel) or more (if a newer
1023 * kernel). So suck up the answer via /proc.
1024 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001025static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001026{
Dylan Reidf682d472015-09-17 21:39:07 -07001027 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1028 FILE *fp = fopen(cap_file, "re");
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001029 unsigned int last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001030
Dylan Reidf682d472015-09-17 21:39:07 -07001031 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1032 pdie("fscanf(%s)", cap_file);
1033 fclose(fp);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001034
Dylan Reidf682d472015-09-17 21:39:07 -07001035 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001036}
1037
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001038void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001039{
1040 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001041 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001042 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001043 unsigned int i;
1044 if (!caps)
1045 die("can't get process caps");
1046 if (cap_clear_flag(caps, CAP_INHERITABLE))
1047 die("can't clear inheritable caps");
1048 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1049 die("can't clear effective caps");
1050 if (cap_clear_flag(caps, CAP_PERMITTED))
1051 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001052 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001053 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001054 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001055 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001056 flag[0] = i;
1057 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001058 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001059 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001060 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001061 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001062 die("can't add inheritable cap");
1063 }
1064 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001065 die("can't apply initial cleaned capset");
1066
1067 /*
1068 * Instead of dropping bounding set first, do it here in case
1069 * the caller had a more permissive bounding set which could
1070 * have been used above to raise a capability that wasn't already
1071 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1072 */
Dylan Reidf682d472015-09-17 21:39:07 -07001073 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -08001074 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -04001075 continue;
1076 if (prctl(PR_CAPBSET_DROP, i))
1077 pdie("prctl(PR_CAPBSET_DROP)");
1078 }
Kees Cook323878a2013-02-05 15:35:24 -08001079
1080 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001081 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001082 flag[0] = CAP_SETPCAP;
1083 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1084 die("can't clear effective cap");
1085 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1086 die("can't clear permitted cap");
1087 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1088 die("can't clear inheritable cap");
1089 }
1090
1091 if (cap_set_proc(caps))
1092 die("can't apply final cleaned capset");
1093
1094 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001095}
1096
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001097void set_seccomp_filter(const struct minijail *j)
1098{
1099 /*
1100 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1101 * in the kernel source tree for an explanation of the parameters.
1102 */
1103 if (j->flags.no_new_privs) {
1104 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1105 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1106 }
1107
1108 /*
1109 * If we're logging seccomp filter failures,
1110 * install the SIGSYS handler first.
1111 */
1112 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1113 if (install_sigsys_handler())
1114 pdie("install SIGSYS handler");
1115 warn("logging seccomp filter failures");
1116 }
1117
1118 /*
1119 * Install the syscall filter.
1120 */
1121 if (j->flags.seccomp_filter) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001122 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) {
1123 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1124 warn("seccomp not supported");
1125 return;
1126 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001127 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001128 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001129 }
1130}
1131
Will Drewry6ac91122011-10-21 16:38:58 -05001132void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001133{
Dylan Reidf682d472015-09-17 21:39:07 -07001134 /*
1135 * Get the last valid cap from /proc, since /proc can be unmounted
1136 * before drop_caps().
1137 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001138 unsigned int last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001139
Elly Jonese1749eb2011-10-07 13:54:59 -04001140 if (j->flags.pids)
1141 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001142 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001143
Elly Jonese1749eb2011-10-07 13:54:59 -04001144 if (j->flags.usergroups && !j->user)
1145 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001146
Elly Jonesdd3e8512012-01-23 15:13:38 -05001147 /*
1148 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001149 * so we don't even try. If any of our operations fail, we abort() the
1150 * entire process.
1151 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001152 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1153 pdie("setns(CLONE_NEWNS)");
1154
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001155 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001156 if (unshare(CLONE_NEWNS))
1157 pdie("unshare(vfs)");
1158 /*
1159 * Remount all filesystems as private. If they are shared
1160 * new bind mounts will creep out of our namespace.
1161 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1162 */
1163 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1164 pdie("mount(/, private)");
1165 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001166
Dylan Reidf7942472015-11-18 17:55:26 -08001167 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1168 pdie("unshare(ipc)");
1169 }
1170
Dylan Reid1102f5a2015-09-15 11:52:20 -07001171 if (j->flags.enter_net) {
1172 if (setns(j->netns_fd, CLONE_NEWNET))
1173 pdie("setns(CLONE_NEWNET)");
1174 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001175 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001176 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001177
Elly Jones51a5b6c2011-10-12 19:09:26 -04001178 if (j->flags.chroot && enter_chroot(j))
1179 pdie("chroot");
1180
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001181 if (j->flags.pivot_root && enter_pivot_root(j))
1182 pdie("pivot_root");
1183
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001184 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001185 pdie("mount_tmp");
1186
Dylan Reid791f5772015-09-14 20:02:42 -07001187 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001188 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001189
Elly Jonese1749eb2011-10-07 13:54:59 -04001190 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001191 /*
1192 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001193 * capability to change uids, our attempt to use setuid()
1194 * below will fail. Hang on to root caps across setuid(), then
1195 * lock securebits.
1196 */
1197 if (prctl(PR_SET_KEEPCAPS, 1))
1198 pdie("prctl(PR_SET_KEEPCAPS)");
1199 if (prctl
1200 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1201 pdie("prctl(PR_SET_SECUREBITS)");
1202 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001203
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001204 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001205 * If we're setting no_new_privs, we can drop privileges
1206 * before setting seccomp filter. This way filter policies
1207 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001208 */
1209 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001210 drop_ugid(j);
1211 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001212 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001213
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001214 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001215 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001216 /*
1217 * If we're not setting no_new_privs,
1218 * we need to set seccomp filter *before* dropping privileges.
1219 * WARNING: this means that filter policies *must* allow
1220 * setgroups()/setresgid()/setresuid() for dropping root and
1221 * capget()/capset()/prctl() for dropping caps.
1222 */
1223 set_seccomp_filter(j);
1224
1225 drop_ugid(j);
1226 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001227 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001228 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001229
Elly Jonesdd3e8512012-01-23 15:13:38 -05001230 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001231 * Select the specified alternate syscall table. The table must not
1232 * block prctl(2) if we're using seccomp as well.
1233 */
1234 if (j->flags.alt_syscall) {
1235 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1236 pdie("prctl(PR_ALT_SYSCALL)");
1237 }
1238
1239 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001240 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001241 * privilege-dropping syscalls :)
1242 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001243 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
1244 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1245 warn("seccomp not supported");
1246 return;
1247 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001248 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001249 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001250}
1251
Will Drewry6ac91122011-10-21 16:38:58 -05001252/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001253static int init_exitstatus = 0;
1254
Will Drewry6ac91122011-10-21 16:38:58 -05001255void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001256{
1257 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001258}
1259
Will Drewry6ac91122011-10-21 16:38:58 -05001260int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001261{
1262 pid_t pid;
1263 int status;
1264 /* so that we exit with the right status */
1265 signal(SIGTERM, init_term);
1266 /* TODO(wad) self jail with seccomp_filters here. */
1267 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001268 /*
1269 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001270 * left inside our pid namespace or we get a signal.
1271 */
1272 if (pid == rootpid)
1273 init_exitstatus = status;
1274 }
1275 if (!WIFEXITED(init_exitstatus))
1276 _exit(MINIJAIL_ERR_INIT);
1277 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001278}
1279
Will Drewry6ac91122011-10-21 16:38:58 -05001280int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001281{
1282 size_t sz = 0;
1283 size_t bytes = read(fd, &sz, sizeof(sz));
1284 char *buf;
1285 int r;
1286 if (sizeof(sz) != bytes)
1287 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001288 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001289 return -E2BIG;
1290 buf = malloc(sz);
1291 if (!buf)
1292 return -ENOMEM;
1293 bytes = read(fd, buf, sz);
1294 if (bytes != sz) {
1295 free(buf);
1296 return -EINVAL;
1297 }
1298 r = minijail_unmarshal(j, buf, sz);
1299 free(buf);
1300 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001301}
1302
Will Drewry6ac91122011-10-21 16:38:58 -05001303int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001304{
1305 char *buf;
1306 size_t sz = minijail_size(j);
1307 ssize_t written;
1308 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001309
Elly Jonese1749eb2011-10-07 13:54:59 -04001310 if (!sz)
1311 return -EINVAL;
1312 buf = malloc(sz);
1313 r = minijail_marshal(j, buf, sz);
1314 if (r) {
1315 free(buf);
1316 return r;
1317 }
1318 /* Sends [size][minijail]. */
1319 written = write(fd, &sz, sizeof(sz));
1320 if (written != sizeof(sz)) {
1321 free(buf);
1322 return -EFAULT;
1323 }
1324 written = write(fd, buf, sz);
1325 if (written < 0 || (size_t) written != sz) {
1326 free(buf);
1327 return -EFAULT;
1328 }
1329 free(buf);
1330 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001331}
Elly Jonescd7a9042011-07-22 13:56:51 -04001332
Will Drewry6ac91122011-10-21 16:38:58 -05001333int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001334{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001335#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001336 /* Don't use LDPRELOAD on Brillo. */
1337 return 0;
1338#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001339 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1340 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1341 if (!newenv)
1342 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001343
Elly Jonese1749eb2011-10-07 13:54:59 -04001344 /* Only insert a separating space if we have something to separate... */
1345 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1346 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001347
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001348 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001349 setenv(kLdPreloadEnvVar, newenv, 1);
1350 free(newenv);
1351 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001352#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001353}
1354
Will Drewry6ac91122011-10-21 16:38:58 -05001355int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001356{
1357 int r = pipe(fds);
1358 char fd_buf[11];
1359 if (r)
1360 return r;
1361 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1362 if (r <= 0)
1363 return -EINVAL;
1364 setenv(kFdEnvVar, fd_buf, 1);
1365 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001366}
1367
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001368int setup_pipe_end(int fds[2], size_t index)
1369{
1370 if (index > 1)
1371 return -1;
1372
1373 close(fds[1 - index]);
1374 return fds[index];
1375}
1376
1377int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1378{
1379 if (index > 1)
1380 return -1;
1381
1382 close(fds[1 - index]);
1383 /* dup2(2) the corresponding end of the pipe into |fd|. */
1384 return dup2(fds[index], fd);
1385}
1386
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001387int minijail_run_internal(struct minijail *j, const char *filename,
1388 char *const argv[], pid_t *pchild_pid,
1389 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1390 int use_preload);
1391
Will Drewry6ac91122011-10-21 16:38:58 -05001392int API minijail_run(struct minijail *j, const char *filename,
1393 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001394{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001395 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1396 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001397}
1398
1399int API minijail_run_pid(struct minijail *j, const char *filename,
1400 char *const argv[], pid_t *pchild_pid)
1401{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001402 return minijail_run_internal(j, filename, argv, pchild_pid,
1403 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001404}
1405
1406int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001407 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001408{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001409 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1410 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001411}
1412
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001413int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001414 char *const argv[], pid_t *pchild_pid,
1415 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001416{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001417 return minijail_run_internal(j, filename, argv, pchild_pid,
1418 pstdin_fd, pstdout_fd, pstderr_fd, true);
1419}
1420
1421int API minijail_run_no_preload(struct minijail *j, const char *filename,
1422 char *const argv[])
1423{
1424 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1425 false);
1426}
1427
Samuel Tan63187f42015-10-16 13:01:53 -07001428int API minijail_run_pid_pipes_no_preload(struct minijail *j,
1429 const char *filename, char *const argv[],
1430 pid_t *pchild_pid,
1431 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd) {
1432 return minijail_run_internal(j, filename, argv, pchild_pid,
1433 pstdin_fd, pstdout_fd, pstderr_fd, false);
1434}
1435
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001436int minijail_run_internal(struct minijail *j, const char *filename,
1437 char *const argv[], pid_t *pchild_pid,
1438 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1439 int use_preload)
1440{
Elly Jonese1749eb2011-10-07 13:54:59 -04001441 char *oldenv, *oldenv_copy = NULL;
1442 pid_t child_pid;
1443 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001444 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001445 int stdout_fds[2];
1446 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001447 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001448 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001449 /* We need to remember this across the minijail_preexec() call. */
1450 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001451 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001452
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001453 if (use_preload) {
1454 oldenv = getenv(kLdPreloadEnvVar);
1455 if (oldenv) {
1456 oldenv_copy = strdup(oldenv);
1457 if (!oldenv_copy)
1458 return -ENOMEM;
1459 }
1460
1461 if (setup_preload())
1462 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001463 }
Will Drewryf89aef52011-09-16 16:48:57 -05001464
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001465 if (!use_preload) {
1466 if (j->flags.caps)
1467 die("Capabilities are not supported without "
1468 "LD_PRELOAD");
1469 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001470
Elly Jonesdd3e8512012-01-23 15:13:38 -05001471 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001472 * Make the process group ID of this process equal to its PID, so that
1473 * both the Minijail process and the jailed process can be killed
1474 * together.
1475 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1476 * the process is already a process group leader.
1477 */
1478 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1479 if (errno != EPERM) {
1480 pdie("setpgid(0, 0)");
1481 }
1482 }
1483
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001484 if (use_preload) {
1485 /*
1486 * Before we fork(2) and execve(2) the child process, we need
1487 * to open a pipe(2) to send the minijail configuration over.
1488 */
1489 if (setup_pipe(pipe_fds))
1490 return -EFAULT;
1491 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001492
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001493 /*
1494 * If we want to write to the child process' standard input,
1495 * create the pipe(2) now.
1496 */
1497 if (pstdin_fd) {
1498 if (pipe(stdin_fds))
1499 return -EFAULT;
1500 }
1501
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001502 /*
1503 * If we want to read from the child process' standard output,
1504 * create the pipe(2) now.
1505 */
1506 if (pstdout_fd) {
1507 if (pipe(stdout_fds))
1508 return -EFAULT;
1509 }
1510
1511 /*
1512 * If we want to read from the child process' standard error,
1513 * create the pipe(2) now.
1514 */
1515 if (pstderr_fd) {
1516 if (pipe(stderr_fds))
1517 return -EFAULT;
1518 }
1519
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001520 /*
1521 * If we want to set up a new uid/gid mapping in the user namespace,
1522 * create the pipe(2) to sync between parent and child.
1523 */
1524 if (j->flags.userns) {
1525 if (pipe(userns_pipe_fds))
1526 return -EFAULT;
1527 }
1528
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001529 /*
1530 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001531 *
1532 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1533 *
1534 * In multithreaded programs, there are a bunch of locks inside libc,
1535 * some of which may be held by other threads at the time that we call
1536 * minijail_run_pid(). If we call fork(), glibc does its level best to
1537 * ensure that we hold all of these locks before it calls clone()
1538 * internally and drop them after clone() returns, but when we call
1539 * sys_clone(2) directly, all that gets bypassed and we end up with a
1540 * child address space where some of libc's important locks are held by
1541 * other threads (which did not get cloned, and hence will never release
1542 * those locks). This is okay so long as we call exec() immediately
1543 * after, but a bunch of seemingly-innocent libc functions like setenv()
1544 * take locks.
1545 *
1546 * Hence, only call sys_clone() if we need to, in order to get at pid
1547 * namespacing. If we follow this path, the child's address space might
1548 * have broken locks; you may only call functions that do not acquire
1549 * any locks.
1550 *
1551 * Unfortunately, fork() acquires every lock it can get its hands on, as
1552 * previously detailed, so this function is highly likely to deadlock
1553 * later on (see "deadlock here") if we're multithreaded.
1554 *
1555 * We might hack around this by having the clone()d child (init of the
1556 * pid namespace) return directly, rather than leaving the clone()d
1557 * process hanging around to be init for the new namespace (and having
1558 * its fork()ed child return in turn), but that process would be crippled
1559 * with its libc locks potentially broken. We might try fork()ing in the
1560 * parent before we clone() to ensure that we own all the locks, but
1561 * then we have to have the forked child hanging around consuming
1562 * resources (and possibly having file descriptors / shared memory
1563 * regions / etc attached). We'd need to keep the child around to avoid
1564 * having its children get reparented to init.
1565 *
1566 * TODO(ellyjones): figure out if the "forked child hanging around"
1567 * problem is fixable or not. It would be nice if we worked in this
1568 * case.
1569 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001570 if (pid_namespace) {
1571 int clone_flags = CLONE_NEWPID | SIGCHLD;
1572 if (j->flags.userns)
1573 clone_flags |= CLONE_NEWUSER;
1574 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001575 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001576 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001577 }
Elly Jones761b7412012-06-13 15:49:52 -04001578
Elly Jonese1749eb2011-10-07 13:54:59 -04001579 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001580 if (use_preload) {
1581 free(oldenv_copy);
1582 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001583 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001584 }
Will Drewryf89aef52011-09-16 16:48:57 -05001585
Elly Jonese1749eb2011-10-07 13:54:59 -04001586 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001587 if (use_preload) {
1588 /* Restore parent's LD_PRELOAD. */
1589 if (oldenv_copy) {
1590 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1591 free(oldenv_copy);
1592 } else {
1593 unsetenv(kLdPreloadEnvVar);
1594 }
1595 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001596 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001597
Elly Jonese1749eb2011-10-07 13:54:59 -04001598 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001599
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001600 if (j->flags.pid_file)
1601 write_pid_file(j);
1602
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001603 if (j->flags.userns)
1604 write_ugid_mappings(j, userns_pipe_fds);
1605
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001606 if (use_preload) {
1607 /* Send marshalled minijail. */
1608 close(pipe_fds[0]); /* read endpoint */
1609 ret = minijail_to_fd(j, pipe_fds[1]);
1610 close(pipe_fds[1]); /* write endpoint */
1611 if (ret) {
1612 kill(j->initpid, SIGKILL);
1613 die("failed to send marshalled minijail");
1614 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001615 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001616
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001617 if (pchild_pid)
1618 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001619
1620 /*
1621 * If we want to write to the child process' standard input,
1622 * set up the write end of the pipe.
1623 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001624 if (pstdin_fd)
1625 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001626 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001627
1628 /*
1629 * If we want to read from the child process' standard output,
1630 * set up the read end of the pipe.
1631 */
1632 if (pstdout_fd)
1633 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001634 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001635
1636 /*
1637 * If we want to read from the child process' standard error,
1638 * set up the read end of the pipe.
1639 */
1640 if (pstderr_fd)
1641 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001642 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001643
Elly Jonese1749eb2011-10-07 13:54:59 -04001644 return 0;
1645 }
1646 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001647
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001648 if (j->flags.userns)
1649 enter_user_namespace(j, userns_pipe_fds);
1650
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001651 /*
1652 * If we want to write to the jailed process' standard input,
1653 * set up the read end of the pipe.
1654 */
1655 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001656 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1657 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001658 die("failed to set up stdin pipe");
1659 }
1660
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001661 /*
1662 * If we want to read from the jailed process' standard output,
1663 * set up the write end of the pipe.
1664 */
1665 if (pstdout_fd) {
1666 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1667 STDOUT_FILENO) < 0)
1668 die("failed to set up stdout pipe");
1669 }
1670
1671 /*
1672 * If we want to read from the jailed process' standard error,
1673 * set up the write end of the pipe.
1674 */
1675 if (pstderr_fd) {
1676 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1677 STDERR_FILENO) < 0)
1678 die("failed to set up stderr pipe");
1679 }
1680
Dylan Reid791f5772015-09-14 20:02:42 -07001681 /* If running an init program, let it decide when/how to mount /proc. */
1682 if (pid_namespace && !do_init)
1683 j->flags.remount_proc_ro = 0;
1684
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001685 if (use_preload) {
1686 /* Strip out flags that cannot be inherited across execve(2). */
1687 minijail_preexec(j);
1688 } else {
1689 j->flags.pids = 0;
1690 }
1691 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001692 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001693
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001694 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001695 /*
1696 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001697 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001698 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001699 * a child to actually run the program. If |do_init == 0|, we
1700 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001701 *
1702 * If we're multithreaded, we'll probably deadlock here. See
1703 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001704 */
1705 child_pid = fork();
1706 if (child_pid < 0)
1707 _exit(child_pid);
1708 else if (child_pid > 0)
1709 init(child_pid); /* never returns */
1710 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001711
Elly Jonesdd3e8512012-01-23 15:13:38 -05001712 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001713 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001714 * calling process
1715 * -> execve()-ing process
1716 * If we are:
1717 * calling process
1718 * -> init()-ing process
1719 * -> execve()-ing process
1720 */
1721 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001722}
1723
Will Drewry6ac91122011-10-21 16:38:58 -05001724int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001725{
1726 int st;
1727 if (kill(j->initpid, SIGTERM))
1728 return -errno;
1729 if (waitpid(j->initpid, &st, 0) < 0)
1730 return -errno;
1731 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001732}
1733
Will Drewry6ac91122011-10-21 16:38:58 -05001734int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001735{
1736 int st;
1737 if (waitpid(j->initpid, &st, 0) < 0)
1738 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001739
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001740 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001741 int error_status = st;
1742 if (WIFSIGNALED(st)) {
1743 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001744 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001745 j->initpid, signum);
1746 /*
1747 * We return MINIJAIL_ERR_JAIL if the process received
1748 * SIGSYS, which happens when a syscall is blocked by
1749 * seccomp filters.
1750 * If not, we do what bash(1) does:
1751 * $? = 128 + signum
1752 */
1753 if (signum == SIGSYS) {
1754 error_status = MINIJAIL_ERR_JAIL;
1755 } else {
1756 error_status = 128 + signum;
1757 }
1758 }
1759 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001760 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001761
1762 int exit_status = WEXITSTATUS(st);
1763 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001764 info("child process %d exited with status %d",
1765 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001766
1767 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001768}
1769
Will Drewry6ac91122011-10-21 16:38:58 -05001770void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001771{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001772 if (j->flags.seccomp_filter && j->filter_prog) {
1773 free(j->filter_prog->filter);
1774 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001775 }
Dylan Reid648b2202015-10-23 00:50:00 -07001776 while (j->mounts_head) {
1777 struct mountpoint *m = j->mounts_head;
1778 j->mounts_head = j->mounts_head->next;
1779 free(m->type);
1780 free(m->dest);
1781 free(m->src);
1782 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001783 }
Dylan Reid648b2202015-10-23 00:50:00 -07001784 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001785 if (j->user)
1786 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001787 if (j->chrootdir)
1788 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08001789 if (j->alt_syscall_table)
1790 free(j->alt_syscall_table);
Elly Jonese1749eb2011-10-07 13:54:59 -04001791 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001792}